# This is an ide startup file intended for use in the field by customers
# and Field Engineers.

if ($1 == "fe") {
    report=4;
    echo "Report level set to VERBOSE";
}
else {
    report=0;
}
fi

# Initialize script variables
#   NOTE: At the end of the startup script each variable that is still set
#         to 2 will indicate some tests that haven't been run (like after
#         an error).
$cpufailed=2;
$fpufailed=2;
$scsifailed=2;
$cpudspfailed=2;
$lg1failed=2;
$gr2failed=2;

# Startup script for the CPU DSP diagnostics

cpudsp {
$cpudspfailed=0;
# DSP static RAM
if (cpudsp_sram) {
    if (!$report) {
	emfail 14;
	wait;
	exit;
    }
    $cpudspfailed=1;
}

if (dsp56) {
    if (!$report) {
	emfail 14;
	wait;
	exit;
    }
    $cpudspfailed=1;
}
# END OF CPU DSP SCRIPT
}

# THE FOLLOWING IS THE STARTUP SCRIPT FOR THE CPU DIAGNOSTICS.
#
# IN ORDER FOR THE SCRIPT TO RUN PROPERLY, THE FOLLOWING MUST BE DONE:
#	1. CONNECT THE TRANSMIT AND RECEIVE PINS ON CHANNEL A AND B OF DUART 0,
#	   CHANNEL B OF DUART 1 (FOR CHANNEL B OF DUART 1, THE REQUEST_TO_SEND
#	   AND CLEAR_TO_SEND PINS MUST ALSO BE CONNECTED).

ip20{
$cpufailed=0;
# Interrupt mask registers data test.
if (int2) {
    if (!$report) {
	emfail 3;
	wait;
	exit;
    }
    $cpufailed=1;
}

# TLB test. Verify different tlb functions.
if (tlb) {
    if (!$report) {
	emfail 4;
	wait;
	exit;
    }
    $cpufailed=1;
}

# UTLB miss exception test. Verify the exception generating capability of the
# mips chip during a utlb miss.
if (utlb) {
    if (!$report) {
	emfail 4;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Real time clock test. Verify the accuracy of the real time clock and the carry
# circuitry by using the timer within the ctl1 gate array.
if (!clock) {
    if (!$report) {
	emfail 7;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Interval timer test. Verify the accuracy of the interval timer by using the
# real time clock.
if (timer) {
    if (!$report) {
	emfail 7;
	wait;
	exit;
    }
    $cpufailed=1;
}

# DUART 0A/keyboard, 0B/mouse, 1B/modem, internal loopback test. Execute the
# internal loopback diagnostics provided by the DUART chips.
$tmp = duart -i0;
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

$tmp = duart -i1;
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

$tmp = duart -i2;
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

$tmp = duart -i3;
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

# DUART 0A/keyboard, 0B/mouse, 1B/modem, external loopback test. verify the
# operation of the DUART channels by simulating actual transmission.
# JUMPERS ARE NEEDED TO CONNECT TRANSMIT AND RECEIVE PINS ON ALL CHANNELS,
# REQUEST_TO_SEND AND CLEAR_TO_SEND PINS ON CHANNEL B OF DUART 1.
# >> FOR FIELD ENGINEERING ONLY <<
if ($report == 4) {
    $tmp = duart -e0;
}
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

if ($report == 4) {
    $tmp = duart -e2;
}
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

if ($report == 4) {
    $tmp = duart -e3;
}
if ($tmp) {
    resetcons 1;
    if (!$report) {
	emfail 8;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Since we just trashed the keyboard and mouse DUARTs let's reset the keyboard.
resetcons 1

# EEPROM pin signal test. Verify the pin signals provided for the EEPROM.
#if (nvram5) {
#    if (!$report) {
#	emfail 9;
#	wait;
#	exit;
#    }
#    $cpufailed=1;
#}

# HPC1 peripheral controller register data test.
if (hpc1) {
    if (!$report) {
	emfail 10;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test floating point unit
$fpufailed=0;
if (fpu) {
    if (!$report) {
	emfail 5;
	wait;
	exit;
    }
    $fpufailed=1;
}

# Test parity checking mechanism
if (parity) {
    if (!$report) {
	emfail 11;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test primary data cache
if (dcache1) {
    if (!$report) {
	emfail 1;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test primary instruction cache
if (icache1) {
    if (!$report) {
	emfail 1;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test primary instruction cache functionality
if (icache2) {
    if (!$report) {
	emfail 1;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test secondary cache
if (scache1) {
    if (!$report) {
	emfail 12;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test local memory 0-4Mbytes
if (ldram) {
    if (!$report) {
	emfail 13;
	wait;
	exit;
    }
    $cpufailed=1;
}

$tmp = ldram -p
if ($tmp) {
    if (!$report) {
	emfail 13;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test local memory above the 4 Mbytes mark
if (dram) {
    if (!$report) {
	emfail 13;
	wait;
	exit;
    }
    $cpufailed=1;
}

$tmp = dram -p
if ($tmp) {
    if (!$report) {
	emfail 13;
	wait;
	exit;
    }
    $cpufailed=1;
}

if (memtest) {
    if (!$report) {
	emfail 13;
	wait;
	exit;
    }
    $cpufailed=1;
}

# Test SCSI devices
$status1=scsi;
$status2=xscsi;
$scsi_status=$status1 | $status2;
$scsifailed = 0;
if ($scsi_status & 257) {
# SCSI chip 0 or 1 failure
    if (!$report) {
	emfail 15;
	wait;
	exit;
    }
    $cpufailed=1;
}
if ($scsi_status & 98175) {
# 0x10000 (cabling) or any scsi device failure (not 1 or 0x100, which
# means chip failure, since for now, chip is always ID 0)
# scsi() puts the failure in a global for emfail.
    if (!$report) {
	emfail 6;
	wait;
	exit;
    }
    $scsifailed=1;
}

# END OF IP20 SCRIPT
}

# LIGHT diags
lg1 {
$lg1failed=0;
buffon;

# REX register test
if (lg1_rexregs) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

# VRAM test
if (lg1_vram) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

# CID test
if (lg1_cid) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

# VC1 register test
if (lg1_vc1regs) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

# VC1 SRAM test
if (lg1_sram) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

# DAC test
if (lg1_dac) {
    if (!$report) {
	emfail 2;
	wait;
	exit;
    }
    $lg1failed=1;
}

resetcons;
buffoff;

# END OF LG1 SCRIPT
}

# Start of GR2 diagnostics 
gr2_vhwinit {
	gr2_videoclk 0x4
	gr2_wrconfig 0x0
	gr2_initclock 0x107b
	gr2_wrconfig 0x40
	gr2_vinitdac
}

gr2_initsys {
	gr2_reset
	gr2_vhwinit
	gr2_lt 3
	gr2_xcol
	$mon = gr2_getMonType;
	if ($mon == 1) gr2_initvc1 4;
	else gr2_initvc1 20;
	fi
	gr2_initvc1 20
	gr2_inithq
	gr2_load_ucode ge
	gr2_load_ucode hq
	gr2_unstall
	gr2_wrfifo 0 0
}

clear {
	gr2_wrfifo 0x35 0
	gr2_wrfifo 6 0
}

gr2_quad {
	gr2_wrfifo 0x33 0
}

gr2_fourquad {
	gr2_wrfifo 0x34 0
}

test_xmap{
	gr2_initsys;
	$a=gr2_txmap;
	return $a;
}

test_xmap_clut{	
	gr2_initsys;
	$a=gr2_txmap_clut;
	return $a;
}

gr2 {
	$vc1_sram_failed=0;
	$tram0_failed=0;
	$tram1_failed=0;
	$tram2_failed=0;
	$tram3_failed=0;
	$tram8_failed=0;
	$tram9_failed=0;
	$tram10_failed=0;
	$hq2test_failed=0;
	$shram_failed=0;
	$ram12_failed=0;
	$seedrom_failed=0;
	$sqrom_failed=0;
	$gebus_failed=0;
	$gefloat_failed=0;
	$chkbt457_failed=0;
	$shramre3_failed=0;
	$cpushram_failed=0;
	$cpure3_failed=0;
	$ctxsw_failed=0;
	$quad_failed=0;
	$fourquad_failed=0;
	$bp_failed=0;
	$stride_failed=0;
	$zb_failed=0;
	$test_xmap_failed=0;
	$test_xmap_clut_failed=0;

	$gr2failed=0;
	gr2_reset;
	buffon

# HQ2 internal register 19  must read 0xdeadbeef
	gr2_rdhqint 0x19;

# Test VC1 SRAM 
	gr2_vhwinit
	if (gr2_vc1_sram) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$vc1_sram_failed = 1;
	}
# Test ram 0 at Ge7 at 0
	$tmp = gr2_tram 0;
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram0_failed = 1;
	}
# Test ram 0 at Ge7 at 1
	$tmp = gr2_tram 1;
	if ($tmp){
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram1_failed = $tmp;
	}

# Test ram 0 at Ge7 at 2
       $tmp = gr2_tram 2;
       if ($tmp){
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram2_failed = 1;
	}
# Test ram 0 at Ge7 at 3
	$tmp = gr2_tram 3 ;
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram3_failed = 1;
	}
# Test shram  
	$tmp = gr2_tram 8 ;
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram8_failed = 1;
	}
# Test Hq2uram
	$tmp = gr2_tram 9 ;
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram9_failed = $tmp;
	}
# Test ge7uram
	$tmp = gr2_tram 10;
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$tram10_failed = $tmp;
	}

	gr2_initsys;
	clear;

# Test HQ2
	
	if (gr2_hq2test) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$hq2test_failed = 1;
	}

# Test shareram using Ucode
	if (gr2_shram) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$shram_failed = 1;
	}

# Test internal GE7 ram
	if (gr2_ram12) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$ram12_failed =1;
	}

# Test seedrom 
	if (gr2_seedrom) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$seedrom_failed =1;
	}

# Test the squaroot rom
	if (gr2_sqrom) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$sqrom_failed =1;
	}

# needed again ???

	gr2_initsys ;

# Test Ge 7 bus
	if (gr2_gebus) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$gebus_failed =1;
	}

# Test ge floating point unit
	if (gr2_gefloat) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$gefloat_failed = 1;
	}
	gr2_initsys;
	clear;

	resetcons
	buffoff
	$err = 0;
	if ($vc1_sram_failed) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "VC1 SRAM  test failed"; 
		$err = 1
	} 
	if ($tram0_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Ram 0 at GE7 - 0 failed "; 
		$err = 1
	} 
	if ($tram1_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Ram 0 at GE7 - 1 failed "; 
		$err = 1
	} 
	if ($tram2_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Ram 0 at GE7 - 2 failed "; 
		$err = 1
	} 
	if ($tram3_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Ram 0 at GE7 - 3 failed "; 
		$err = 1
	} 
	if ($tram8_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Share ram test failed "; 
		$err = 1
	}
	if ($tram9_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "Hq2uram  ram failed "; 
		$err = 1
	}
	if ($tram10_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "ge7uram failed " ;
		$err = 1
	}
	if ($hq2test_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "HQ2 Test failed ";
		$err = 1
	}
	if ($shram_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "shram test failed";
		$err = 1
	}
	if ($ram12_failed) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "GE7 ram 1/2 test failed "; 
		$err = 1
	}
	if ($seedrom_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "seedrom test failed ";
		$err = 1
	}
	if ($sqrom_failed) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "squaroot teste failed";
		$err = 1
	}
	if ($gebus_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "gebus test failed";
		$err = 1
	}
	if ($gefloat_failed) { 
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		echo "gefloat test failed";
		$err = 1
	}
	if ($err) {
		wait
		$err=0
		$gr2failed=1
	}
	else echo "HQ & GE Tests passed\n"
	buffon

# Test bt457 
	if (gr2_chkbt457) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$chkbt457_failed = 1;
		$err=1;
	}
# Test Shared ram and Re
	gr2_initsys
	$tmp = gr2_shramre3
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$shramre3_failed =1;
		$err=1;
	}
# Test DMA between host and GE7 shared ram

	gr2_initsys
	clear
	$tmp = gr2_cpushram
	if ($tmp)  {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$cpushram_failed =1;
		$err=1;
	}

# Test DMA between host and and RE3
	resetcons
	buffon
	gr2_initsys
	$tmp = gr2_cpure3
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$cpure3_failed =1;
		$err=1;
	}

# Test stride DMA between shram and re3
	if(gr2_ctxsw) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
	 	$ctxsw_failed =1;
		$err=1;
	}
	clear;

# Test quad drawing
	$tmp = gr2_quad
	if ($tmp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$quad_failed =1;
		$err=1;
	}
	$tmp = gr2_fourquad
	if ($tmp){
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$fourquad_failed = 1;
		$err=1;
	}
	clear;

	resetcons
	buffoff
	if ($chkbt457_failed) echo "Bt457 test failed";
	if ($shramre3_failed) echo "shramre3 test failed"
	if ($cpushram_failed) echo "CPU shram test failed";
	if ($cpure3_failed) echo "cpure3 test failed ";
	if ($ctxsw_failed) echo "context switching test failed ";
	if ($quad_failed) echo "quad test failed";
	if ($fourquad_failed) echo "fourquad test failed";

	if ($err) {
		echo "******  PRESS RETURN TO CONTINUE OR ^C TO EXIT *********"
		wait
		$err=0
		$gr2failed=1
	}
	else echo "BT457, DMA and QUADs Tests Passed.\n"
	buffon

# Test bitplane VM2
	gr2_initsys
	if (gr2_bp) {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$bp_failed = 1;
		$err=1;
	}
# Test stridedma between shram and re3
	resetcons
	buffon
	gr2_initsys
	$tmp = gr2_stride
	if ($tmp)  {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$stride_failed =1 ;
		$err=1;
	}
# Test Zbuffer
	gr2_initsys
	$tmp=gr2_zb
	if($tmp)  {
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$zb_failed =1;
		$err=1;
	}

# Test Xmap 
	if (test_xmap) {;
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$test_xmap_failed =1;
		$err=1;
	}

# Test Xmap CLUT 
	if (test_xmap_clut) {;
		if (!$report) {
			emfail 1;
			wait;
			exit;
		}
		$test_xmap_clut_failed =1;
		$err=1;
	}

	resetcons
	buffoff
	echo "************************* RESULTS ******************************"
	if ($bp_failed) {
		echo "bitplane test failed";
		$gr2failed = 1;
	}
	if ($stride_failed)  {
		echo "stride test failed";
		$gr2failed = 1;
	}
	if ($zb_failed) { 
		echo "Zbuffer test failed";
		$gr2failed = 1;
	}
	if ($test_xmap_failed) {
		echo "test_xmap test failed";
		$gr2failed = 1;
	}
	if ($test_xmap_clut_failed) {
		echo "test_xmap_clut test failed";
		$gr2failed = 1;
	}
	if (!$err) echo "Frame Buffer, Bit-Plane, and XMAPs Test Passed.\n"
	clear
	resetcons 
	buffoff
	if ($gr2failed) echo "ERROR: Failure detected on graphics board."
	else echo "graphics board tests passed"
	fi
	echo " *****************END OF THE TEST *****************"
}
# END OF GR2 SUPPORT

# Here lies what is actually executed at start-up
hinv;
echo "";

ip20;
cpudsp;

$light = lg1_probe;
$express = gr2_probe;

if ($light) {
    lg1;
}

if ($express) {
    gr2;
}

echo "TEST RESULTS:";
if ($cpufailed == 1) echo "Failure detected on CPU board.";
if ($cpufailed == 2) echo "CPU tests NOT completed.";
if (!$cpufailed) echo "CPU tests completed.";

if ($cpudspfailed == 1) echo "Failure detected on CPU DSP module.";
if ($cpudspfailed == 2) echo "CPU DSP tests NOT completed.";
if (!$cpudspfailed) echo "CPU tests completed.";

if ($fpufailed == 1) echo "Failure detected on FPU.";
if ($fpufailed == 2) echo "FPU tests NOT completed.";
if (!$fpufailed) echo "FPU tests completed.";

if ($scsifailed == 1) echo "Failure detected on SCSI device[s].";
if ($scsifailed == 2) echo "SCSI tests NOT completed.";
if (!$scsifailed) echo "SCSI tests completed.";

if ($light) {
    if ($lg1failed == 1) echo "Failure detected on LG1 graphics board.";
    if ($lg1failed == 2) echo "LG1 graphics board tests NOT completed.";
    if (!$lg1failed) echo "LG1 graphics board tests completed.";
}
fi

if ($express) {
    if ($gr2failed == 1) echo "Failure detected on GR2 graphics board.";
    if ($gr2failed == 2) echo "GR2 graphics board tests NOT completed.";
    if (!$gr2failed) echo "GR2 graphics board tests completed.";
}
fi

wait "Diagnostics completed";

