summaryrefslogtreecommitdiffstats
path: root/private/ntos/fw/mips/j3reset.s
blob: 40e7c488da5270f42ed2e6dff1f7cc7e192c6306 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
#if defined(JAZZ) && defined(R3000)

/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    j3reset.s

Abstract:

    This module is the start of the boot code. This code will
    be the first run upon reset. It contains the self-test and
    initialization.

Author:

    Lluis Abello (lluis)  8-Jan-91

Environment:

    Executes in kernal mode.

Notes:

	***** IMPORTANT *****

	This module must be linked such that it resides in the
	first page of the rom.

Revision History:


	Some code is stolen from johncoop's "reset.s"

--*/
//
// include header file
//

#include "ksmips.h"
#include <jazzprom.h>
#include "selfmap.h"
#include "led.h"
#include "jzconfig.h"
#include "dmaregs.h"
#include "ioaccess.h"

#define PROM_BASE (KSEG1_BASE | 0x1fc00000)
#define PROM_ENTRY(x) (PROM_BASE + ((x) * 8))

//
// redifne bal to be a relative branch and link instead of jal as it's
// defined in kxmips.h
// The cpp will issue a redefinition warning message.
//
#define bal bgezal  zero,
#define DMA_CHANNEL_GAP 0x20	    // distance beetwen DMA channels

//
// create a small sdata section.
//

.sdata
.space 4


.text
.set noreorder
.set noat


	.globl ResetVector
ResetVector:

/*++

Routine Description:

	This routine will provide the jump vectors located
	at the targets of the processor exception vectors.
	This routine must be located at the start of ROM which
	is the location of the reset vector.

Arguments:

    None.

Return Value:

    None.

--*/


	//
	// this instruction must be loaded at location 0 in the
	// rom. This will appear as BFC00000 to the processor
	//

	b	ResetException
	nop

	//
	// This is the jump table for rom routines that other
	// programs can call. They are placed here so that they
	// will be unlikely to move.
	//

	//
	// This becomes PROM_ENTRY(2) as defined in ntmips.h
	//

	.align	4

	li	k0,MONITOR_LINK_ADDRESS 	    // la k0,MonitorReInit
	li	k1,GLOBAL_DATA_BASE
	j	k0
	sw	$31,0x7C(k1)			    // save return address

	//
	// This becomes PROM_ENTRYS(12,13)
	//

	.align 6
	nop					    // entry 8
	nop
	nop					    // entry 9
	nop
	b	TlbInit 			    // entry 10
	nop

	nop					    // entry 11
	nop
GetCharJumpInstruction:
	b	GetCharJumpInstruction		    // entry 12
	nop
PutCharJumpInstruction:
	b	PutCharJumpInstruction		    // entry 13
	nop
	b	PutLedDisplay			    // entry 14
	nop
GetLineJumpInstruction:
	b	GetLineJumpInstruction		    // entry 15
	nop
PutLineJumpInstruction:
	b	PutLineJumpInstruction		    // entry 16
	nop
	// .word   BitMapPointers-LINK_ADDRESS+KSEG0_BASE   // entry 17 address of BitmapPointers

nop_opcode:	nop			    // nop opcode to test the icache
		j	ra		    // return opcode


	//
	// these next vectors should be loaded at BFC00100,BFC00180,
	// They are for the TLBmiss, and
	// common exceptions respectively.
	//

	.align 8
UserTlbMiss:
	//
	// checks if exception occurred in the tlbtest
	//
	mfc0	k1,epc			// we enter this routine a
	la	k0,TlbTestBegin 	// load the start address of the
					// TlbTest code likely to fail

	slt	k0,k1,k0		// check if exception happend
	bne	k0,zero,NormalException // on a smaller address
	la	k0,TlbTestEnd		// load the end address of the
					// TlbTest code likely to fail
	slt	k0,k0,k1		// and check if exception happend
	bne	k0,zero,NormalException // on a bigger address
	nop
	lui	a1,LED_BLINK		//
	bal	PutLedDisplay		//
	or	a0,a1,a0		//

NormalException:
	la	k0,ExceptionDispatch	//
	mfc0	k1,cause		//  read cause register
	j	k0			//  go to Dispatcher
	andi	k1,k1,0xFF		// just execcode field from cause reg.
	.align 7
	mfc0	k1,cause		// get cause
	li	k0,KSEG1_BASE		//
	sw	k1,0(k0)		// write cause reg to phys address zero
	la	k0,ExceptionDispatch	// get address of Dispatcher
	j	k0			// go to dispatcher
	andi	k1,k1,0xFF		// just execcode field from cause reg.

.align 4
ALTERNATE_ENTRY(MemoryRoutines)     // The test code is copied from here to EndMemoryRoutines
				    // into memory to run it cached.
/*++
VOID
WriteAddressTest(
    StartAddress
    Size
    Xor pattern
    )
Routine Description:

	This routine will store the address of each location xored with
	the Pattern into each location.

Arguments:

	a0 - supplies start of memory area to test
	a1 - supplies length of memory area in bytes
	a2 - supplies the pattern to Xor with.

	Note: the values of the arguments are preserved.

Return Value:

	This routine returns no value.
--*/
ALTERNATE_ENTRY(MemoryTest)	    // The monitor calls this
	LEAF_ENTRY(WriteAddressTest)
	    add     t1,a0,a1		    // t1 = last address.
	    xor     t0,a0,a2		    // t0 value to write
	    move    t2,a0		    // t2=current address
writeaddress:
	    sw	    t0,0(t2)		    // store
	    addiu   t2,t2,4		    // compute next address
	    xor     t0,t2,a2		    // next pattern
	    sw	    t0,0(t2)
	    addiu   t2,t2,4		    // compute next address
	    xor     t0,t2,a2		    // next pattern
	    sw	    t0,0(t2)
	    addiu   t2,t2,4		    // compute next address
	    xor     t0,t2,a2		    // next pattern
	    sw	    t0,0(t2)
	    addiu   t2,t2,4		    // compute next address
	    bne     t2,t1, writeaddress     // check for end condition
	    xor     t0,t2,a2		    // value to write
	    j	    ra
	    nop
	.end WriteAddressTest
/*++
VOID
WriteNoXorAddressTest(
    StartAddress
    Size
    )
Routine Description:

	This routine will store the address of each location
	into each location.

Arguments:

	a0 - supplies start of memory area to test
	a1 - supplies length of memory area in bytes

	Note: the values of the arguments are preserved.

Return Value:

	This routine returns no value.
--*/
	LEAF_ENTRY(WriteNoXorAddressTest)
	    add     t1,a0,a1		    // t1 = last address.
	    addiu   t1,t1,-4
	    move    t2,a0		    // t2=current address
writenoXoraddress:
	    sw	    t2,0(t2)		    // store first address
	    addiu   t2,t2,4		    // compute next address
	    sw	    t2,0(t2)		    // store first address
	    addiu   t2,t2,4		    // compute next address
	    sw	    t2,0(t2)		    // store first address
	    addiu   t2,t2,4		    // compute next address
	    sw	    t2,0(t2)		    // store
	    bne     t2,t1, writenoXoraddress // check for end condition
	    addiu   t2,t2,4		    // compute next address
	    j	    ra
	    nop
	.end WriteNoXorAddressTest
/*++
VOID
CheckAddressTest(
    StartAddress
    Size
    Xor pattern
    LedDisplayValue
    )
Routine Description:

	This routine will check that each location contains it's address
	xored with the Pattern as written by WriteAddressTest.

	Note: the values of the arguments are preserved.

Arguments:

	This routine will check that each location contains it's address
	xored with the Pattern as written by WriteAddressTest. The memory
	is read cached or non cached according to the address specified by a0.
	Write address test writes allways KSEG1_ADR=KSEG1_ADR ^ KSEG1_XOR
	if a0 is in KSEG0 to read the data cached, then the XOR_PATTERN
	Must be such that:
	KSEG0_ADR ^ KSEG0_XOR = KSEG1_ADR ^ KSEG1_XOR
	Examples:

	    If XorPattern with which WriteAddressTest was called is KSEG1_PAT
	    and the XorPattern this routine needs is KSEG0_PAT:
	    KSEG1_XOR	  Written	   KSEG0_XOR	So that
	    0x00000000	  0xA0		   0x20000000	0x80 ^ 0x20  = 0xA0
	    0xFFFFFFFF	  0x5F		   0xDFFFFFFF	0x80 ^ 0xDF  = 0x5F
	    0x01010101	  0xA1		   0x21010101	0x80 ^ 0x21  = 0xA1

	Note: the values of the arguments are preserved.
	a0 - supplies start of memory area to test
	a1 - supplies length of memory area in bytes
	a2 - supplies the pattern to Xor with.
	a3 - suplies the value to display in the led in case of failure

Return Value:

	This routine returns no value.
        It will hang displaying the test number if it finds an error
        and not configured in loop on error.

--*/
	LEAF_ENTRY(CheckAddressTest)
	    move    t3,a0		    // t3 first address.
	    add     t2,t3,a1		    // last address.
checkaddress:
	    lw	    t1,0(t3)		    // load from first location
	    xor     t0,t3,a2		    // first expected value
	    bne     t1,t0,PatternFail
	    addiu   t3,t3,4		    // compute next address
	    lw	    t1,0(t3)		    // load from first location
	    xor     t0,t3,a2		    // first expected value
	    bne     t1,t0,PatternFail
	    addiu   t3,t3,4		    // compute next address
	    lw	    t1,0(t3)		    // load from first location
	    xor     t0,t3,a2		    // first expected value
	    bne     t1,t0,PatternFail
	    addiu   t3,t3,4		    // compute next address
	    lw	    t1,0(t3)		    // load from first location
	    xor     t0,t3,a2		    // first expected value
	    bne     t1,t0,PatternFail	    // check last one.
	    addiu   t3,t3,4		    // compute next address
	    bne     t3,t2, checkaddress     // check for end condition
            move    v0,zero                 // set return value to zero.
	    j	    ra			    // return a zero to the caller
PatternFail:
        //
        // check if we are in loop on error
        //
        li      t0,DIAGNOSTIC_VIRTUAL_BASE  // get base address of diag register
        lb      t0,0(t0)                    // read register value.
        li      t1,LOOP_ON_ERROR_MASK       // get value to compare
        andi    t0,DIAGNOSTIC_MASK          // mask diagnostic bits.
        li      v0,PROM_ENTRY(14)           // load address of PutLedDisplay
        beq     t1,t0,10f                   // brnach if loop on error.
        move    s8,a0                       // save register a0
        lui     t0,LED_BLINK                // get LED blink code
        jal     v0                          // Blink LED and hang.
        or      a0,a3,t0                    // pass a3 as argument in a0
10:
        lui     t0,LED_LOOP_ERROR           // get LED LOOP_ERROR code
        jal     v0                          // Set LOOP ON ERROR on LED
        or      a0,a3,t0                    // pass a3 as argument in a0
        b       CheckAddressTest            // Loop back to test again.
        move    a0,s8                       // restoring arguments.
	.end CheckAddressTest
/*++
VOID
CheckNoXorAddressTest(
    StartAddress
    Size
    LedDisplayValue
    )
Routine Description:

	This routine will check that each location contains it's address
	xored with the Pattern as written by WriteAddressTest.

Arguments:

	Note: the values of the arguments are preserved.
	a0 - supplies start of memory area to test
	a1 - supplies length of memory area in bytes
	a2 - supplies the pattern to Xor with.
	a3 - suplies the value to display in the led in case of failure

Return Value:

	This routine returns no value.
	It will hang displaying the test number if it finds an error.
--*/
	LEAF_ENTRY(CheckNoXorAddressTest)
	    addiu   t3,a0,-4		    // t3 first address-4
	    add     t2,a0,a1		    // last address.
	    addiu   t2,t2,-8		    // adjust
	    move    t1,t3		    // get copy of t3 just for first check
checkaddressNX:
	    bne     t1,t3,PatternFailNX
	    lw	    t1,4(t3)		    // load from first location
	    addiu   t3,t3,4		    // compute next address
	    bne     t1,t3,PatternFailNX
	    lw	    t1,4(t3)		    // load from next location
	    addiu   t3,t3,4		    // compute next address
	    bne     t1,t3,PatternFailNX
	    lw	    t1,4(t3)		    // load from next  location
	    addiu   t3,t3,4		    // compute next address
	    bne     t1,t3,PatternFailNX     // check
	    lw	    t1,4(t3)		    // load from next location
	    bne     t3,t2, checkaddressNX   // check for end condition
	    addiu   t3,t3,4		    // compute next address

	    bne     t1,t3,PatternFailNX     // check last
	    nop
	    j	    ra			    // return a zero to the caller
	    move    v0,zero		    //
PatternFailNX:
        //
        // check if we are in loop on error
        //
        li      t0,DIAGNOSTIC_VIRTUAL_BASE  // get base address of diag register
        lb      t0,0(t0)                    // read register value.
        li      t1,LOOP_ON_ERROR_MASK       // get value to compare
        andi    t0,DIAGNOSTIC_MASK          // mask diagnostic bits.
        li      v0,PROM_ENTRY(14)           // load address of PutLedDisplay
        beq     t1,t0,10f                   // brnach if loop on error.
        move    s8,a0                       // save register a0
        lui     t0,LED_BLINK                // get LED blink code
        jal     v0                          // Blink LED and hang.
        or      a0,a3,t0                    // pass a3 as argument in a0
10:
        lui     t0,LED_LOOP_ERROR           // get LED LOOP_ERROR code
        jal     v0                          // Set LOOP ON ERROR on LED
        or      a0,a3,t0                    // pass a3 as argument in a0
        b       CheckNoXorAddressTest       // Loop back to test again.
        move    a0,s8                       // restoring arguments.
        .end CheckNoXorAddressTest

	.globl ResetException
ResetException:
	.globl _start
_start:
/*++

Routine Description:

    This is the handler for the reset exception. It first checks the cause
    of the exception. If it is an NMI, then control is passed to the 
    exception dispatch routine. Otherwise the machine is initialized.
    
    1. Invalidate TLB, and clear coprocessor 0 cause register
    2. Map the diagnostic LED, and MCT_ADR control register, zero remaining TLB
    3. Test the processor
    4. Test the reset state of address chip and then initialize values.
    5. Map all of rom, and begin executing code in virtual address space.
    6. Perform checksum of ROM
    7. Test a small portion of memory. (Test code run from ROM)
    8. Test the TLB
    9. Copy memory test routines to memory so they can execute faster there.
    10. flush and initialize dcache.
    11. Test a larger section of memory. (Test code run uncached from memory)
    12. Flush and initialize icache.
    13. Initialize TlbMiss handler routine.
    14. Test Video Memory
    15. Copy Selftest image from rom to memory, and call.
    16. Copy monitor image from rom to memory, and jump.
    
Note:
    This routine must be loaded into the first page of rom.
    Any routines that are called before jumping to virtual
    addresses must also be loaded into the first page of rom.

Arguments:

    None.

Return Value:

    None.

--*/

        //
        // Initialize cause and status registers.
        //
        li      t0,(1 << PSR_BEV)
        mtc0    t0,psr
        mtc0    zero,cause

	//
        // Map the 7 segment display
	//
MapDisplay:
	li	t0,LED_LO
	li	t1,LED_HI
	mtc0	t0,entrylo
	mtc0	t1,entryhi
	mtc0	zero,index
	nop
	tlbwi
	//
	// Map also the MCTADR
	//
	li	t0,(1<<INDEX_INDEX)	// tlb index entry 1
	mtc0	t0,index
	li	t1,DEVICE_LO		//
	li	t2,DEVICE_HI		//
	mtc0	t1,entrylo
	mtc0	t2,entryhi
	addiu	t0,t0,(1<<INDEX_INDEX)	// compute next index
	tlbwi

//
//  Zero the remaining TLB entries.
//
        li      t1,64*(1<<INDEX_INDEX)  // load last tlb index
	mtc0	zero,entrylo		// clear entrylo -  Invalid entry
	li	t2,RESV_HI		// get VPN on a reserved space
zerotlb:
	mtc0	t2,entryhi	      // clear entryhi
	mtc0	t0,index		// set index
	addiu	t0,t0,(1<<INDEX_INDEX)	// compute next index
	tlbwi
	bne	t0,t1,zerotlb		//
        addiu   t2,PAGE_SIZE

        //
        // Turn off the LED and display that Processor test is starting.
        //
	bal	PutLedDisplay
	ori	a0,zero,LED_BLANK
	bal	PutLedDisplay
	ori	a0,zero,LED_PROCESSOR_TEST
//
// test the processor. Test uses all registers and almost all the instructions
//
ProcessorTest:
	lui	a0,0x1234		// a0=0x12340000
	ori	a1,a0,0x4321		// a1=0x12344321
	add	a2,zero,a0		// a2=0x12340000
	addiu	a3,zero,-0x4321 	// a3=0xFFFFBCDF
	subu	AT,a2,a3		// AT=0x12344321
	bne	a1,AT,ProcessorError	// branch if don't match
	andi	v0,a3,0xFFFF		// v0=0x0000BCDF
	ori	v1,v0,0xFFFF		// v1=0x0000FFFF
	sll	t0,v1,16		// t0=0xFFFF0000
	xor	t1,t0,v1		// t1=0xFFFFFFFF
	sra	t2,t0,16		// t2=0xFFFFFFFF
	beq	t1,t2,10f		// if eq good
	srl	t3,t0,24		// t3=0x000000FF
	j	ProcessorError		// if wasn't eq error.
10:	sltu	s0,t0,v1		// S0=0 because t0 > v1
	bgtz	s0,ProcessorError	// if s0 > zero error
	or	t4,AT,v0		// t4=X
	bltz	s0,ProcessorError	// if s0 < zero error
	nor	t5,v0,AT		// t5=~X
	and	t6,t4,t5		// t6=0
	bltzal	t6,ProcessorError	// if t6 < 0  error. Load ra in any case
	nop
RaAddress:
	la	t7,RaAddress- LINK_ADDRESS + RESET_VECTOR // get expected address in ra
	bne	ra,t7,ProcessorError	// error if don't mach
	ori	s1,zero,0x100		// load constant
	mult	s1,t3			// 0x100*0xFF
	mfhi	s3			// s3=0
	mflo	s2			// s2=0xFF00
	blez	s3,10f			// branch if correct
	sll	s4,t3,zero		// move t3 into s4
	addiu	s4,100			// change value in s4 to produce an error
10:	divu	s5,s2,s4		// divide 0xFF00/0xFF
	nop
	nop
	mfhi	s6			// remainder s6=0
	bne	s5,s1,ProcessorError
	nop
	blez	s6,10f			// branch if no error
	nop
	j	ProcessorError
10:	sub	s7,s5,s4		// s7=1
	mthi	s7
	mtlo	AT
	xori	gp,s5,0x2566		// gp=0x2466
	move	s0,sp	// save sp for now
	srl	sp,gp,s7		// sp=0x1233
	mflo	s8			// s8=0x12344321
	mfhi	k0			// k0=1
	ori	k1,zero,16		// k1=16
	sra	k1,s8,k1		// k1=0x1234
	add	AT,sp,k0		// AT=0x1234
	bne	k1,AT,ProcessorError	// branch on error
	nop

//
// Processor test passed if code gets this far
// Continuue with  test of address chip.
//
	b	MctadrTest

//
// processor error routine
//

ProcessorError:
	lui	a0,LED_BLINK		// blink also means that
	bal	PutLedDisplay	   // the routine hangs.
	ori	a0,LED_PROCESSOR_TEST	// displaying this value.

/*++
VOID
PutLedDisplay(
    a0 - display value.
    )
Routine Description:

	This routine will display in the LED the value specified as argument
	a0.

	This routine must reside in the first page of ROM because it is
	called before mapping the rom.

Arguments:

	a0 value to display.

Return Value:

    None.

--*/
	LEAF_ENTRY(PutLedDisplay)
	li	t0,DIAGNOSTIC_VIRTUAL_BASE		   // load address of display
LedBlinkLoop:
	srl	t1,a0,16		    // get upper bits of a0 in t1
	srl	t3,a0,4 		    // get test number
	li	t4,LED_LOOP_ERROR	    //
	bne	t1,t4, DisplayTestID
	andi	t3,t3,0xF		    // clear other bits.
	ori	t3,t3,LED_DECIMAL_POINT     // Set decimal point
DisplayTestID:
	li	t4,LED_BLINK		    // check if need to hung
	beq	t1,t4, ShowSubtestID
	sb	t3,0(t0)		    // write test ID to led.
	j	ra			    // return to caller.
	nop
ShowSubtestID:
	li	t2,LED_DELAY_LOOP	    // get delay value.
TestWait:
	bne	t2,zero,TestWait	    // loop until zero
	addiu	t2,t2,-1		    // decrrement counter
	li	t3,LED_DECIMAL_POINT+LED_BLANK	//
	sb	t3,0(t0)		    // write decimal point
	li	t2,LED_DELAY_LOOP/2	    // get delay value.
DecPointWait:
	bne	t2,zero,DecPointWait	    // loop until zero
	addiu	t2,t2,-1		    // decrement counter
	andi	t3,a0,0xF		    // get subtest number
	sb	t3,0(t0)		    // write subtest in LED
	li	t2,LED_DELAY_LOOP	    // get delay value.
SubTestWait:
	bne	t2,zero,SubTestWait	    // loop until zero
	addiu	t2,t2,-1		    // decrrement counter
	b	LedBlinkLoop		    // go to it again
	nop
        .end    PutLedDisplay

/*++
VOID
ZeroMemory(
    ULONG   StartAddress
    ULONG   Size
    );
Routine Description:

	This routine will zero a range of memory.

Arguments:

	a0 - supplies start of memory
	a1 - supplies length of memory in bytes

Return Value:

	None.

--*/
    LEAF_ENTRY(ZeroMemory)
	add	a1,a1,a0			// Compute End address
	addiu	a1,a1,-4			//
ZeroMemoryLoop:
	sw	zero,0(a0)			// zero memory.
	bne	a0,a1,ZeroMemoryLoop		// loop until done.
	addiu	a0,a0,4
	j	ra				// return
	nop
    ALTERNATE_ENTRY(ZeroMemoryEnd)
	nop
	.end ZeroMemory

    LEAF_ENTRY(R3000CacheInit)
/*++

Routine Description:

	This routine will flush the R3000 caches. This is done
	by setting the isolate cache bit in the status register
	and then doing partial stores to the cache.
	This routine should be called with the swapcache bit set
	to flush the icache.
	This entire routine should run uncached.

Arguments:

    None.

Return Value:

    None.

--*/

	//
	// save status register and then
	// set isolation bit. This means that operations will
	// not actually go to memory.
	//
	mfc0	t1,psr			// get psr
	li	t0,(1 << PSR_ISC)	// set isolate cache bit
	or	t0,t1,t0		// or them together
	mtc0	t0,psr			// disable interrupts, isolate cache.
	nop				// wait for data cache isolation
	nop
	nop
	nop
	nop
	nop
	nop
	nop

	//
	// by writing partials the caches are invalidated.
	// these writes don't actually go to memory because
	// cache is isolated.
	// write to all cache lines.
	//

	li	t0,KSEG0_BASE		// physical address for flushing
	li	t4,DATA_CACHE_SIZE	// load size of cache.
	add	t4,t0,t4		// value for end of loop condition
flushcacheloop:
	sb	zero,0(t0)		// flush cache block
	sb	zero,4(t0)
	sb	zero,8(t0)
	sb	zero,12(t0)
	sb	zero,16(t0)
	sb	zero,20(t0)
	sb	zero,24(t0)
	addu	t0,t0,32		// advance flush pointer
	sltu	t1,t0,t4		// check if end of cache.
	bne	zero,t1,flushcacheloop // if ne, more to flush
	sb	zero,-4(t0)
	nop				// wait for store oper. to clear pipe
	nop
	nop
	nop
	li	t0,(1 << PSR_BEV)
	nop
	mtc0	t0,psr			// unisolate and swap caches back
	nop				// wait for caches to swap back
	nop
	nop
	j	ra			// return.
	nop
	.end R3000CacheInit

   LEAF_ENTRY(R3000ICacheTest)
/*++

Routine Description:

	This routine will write the Icache with nops plus a return opcode
	and execute them.
	This entire routine should run uncached.

Arguments:

    a0	- nop opcode
    a1	- j ra opcode

Return Value:

    None.

--*/

//
// Copy nops to memory plus a return opcode, and jump to execute them in cached
// space.
//


	li	t0,KSEG1_BASE+MEMTEST_SIZE	// start of tested memory
	li	t1,INSTRUCTION_CACHE_SIZE-4	// size filled with nop opcodes
	add	t1,t1,t0			// last address.
WriteNop:
	sw	a0,0(t0)			// write nop opcode
	bne	t0,t1,WriteNop			// check if last
	addiu	t0,t0,4
	li	t1,KSEG0_BASE+MEMTEST_SIZE	// address of wirtten nops in cached space
	j	t1				// go to execute cached
	sw	a1,-8(t0)			// store return opcode the return
						// opcode will return to the caller
						// leave a nop in the delay slot
	.end	R3000ICacheTest
/*++
VOID
DataCopy(
    ULONG SourceAddress
    ULONG DestinationAddress
    ULONG Length
    );
Routine Description:

	This routine will copy data from one location to another
	Source, destination, and length must be word aligned.

Arguments:

	a0 - supplies source of data
	a1 - supplies destination of data
	a2 - supplies length of data in bytes

Return Value:

	None.
--*/
    LEAF_ENTRY(DataCopy)
	add	a2,a2,a0			// get last address
CopyLoop:
	lw	t5,0(a0)			// get source of data
	addiu	a0,a0,4 			// increment source pointer
	sw	t5,0(a1)			// put to dest.
	bne	a0,a2,CopyLoop			// loop until address=last address
	addiu	a1,a1,4 			// increment destination pointer
	j	ra				// return
	nop
ALTERNATE_ENTRY(EndMemoryRoutines)
	nop
    .end    DataCopy
//++
//
// VOID
// FlushWriteBuffer (
//    VOID
//    )
//
// Routine Description:
//
//    This function flushes the write buffer on the current processor.
//    this routine does the same that NtFlushWriteBuffer except that it
//    doesn't return any value. It's intended to be called just
//    from this module.
//
//    It must reside in the first page of the ROM.
//
// Arguments:
//
//    None.
//
// Return Value:
//
//    None.
//
//--

	LEAF_ENTRY(FlushWriteBuffer)
        .set    noreorder
        .set    noat
        nop                             // four nop's are required
        nop                             //
        nop                             //
        nop                             //
10:                                     //
        bc0f    10b                     // if false, write buffer not empty
	nop				//
        j       ra                      // return
	nop
	.end	FlushWritebuffer

RomRemoteSpeedValues:
	//
	// This table contains the default values for the remote speed regs.
	//
	.byte REMSPEED1 		    // ethernet
	.byte REMSPEED2 		    // SCSI
	.byte REMSPEED3 		    // Floppy
	.byte REMSPEED4 		    // RTC
	.byte REMSPEED5 		    // Kbd/Mouse
	.byte REMSPEED6 		    // Serial port
	.byte 7 			    // New dev
	.byte REMSPEED8 		    // Parallel
	.byte REMSPEED9 		    // NVRAM
	.byte REMSPEED10		    // Int src reg
	.byte REMSPEED11		    // PROM
	.byte REMSPEED12		    // Sound
	.byte 7 			    // New dev
	.byte REMSPEED14		    // External Eisa latch
	.align 4




MctadrTest:

//
// Test the mctadr registers.
//
	bal	PutLedDisplay
	ori	a0,zero,LED_MCTADR_RESET

//
// check chip reset values
//

MctadrReset:
	li	t0,DEVICE_VIRTUAL_BASE		// Get base address of MCTADR
	lw	v0,DmaConfiguration(t0)       // Check Config reset value
	li	t1,CONFIG_RESET 	//
	bne	v0,t1,MctadrResetError
	lw	v0,DmaInvalidAddress(t0)
	lw	v1,DmaTranslationBase(t0)
	bne	v0,zero,MctadrResetError// Check LFAR reset value
	lw	v0,DmaTranslationLimit(t0)
	bne	v1,zero,MctadrResetError// Check Ttable base reset value
	lw	v1,DmaRemoteFailedAddress(t0)
	bne	v0,zero,MctadrResetError// Check TT limit reset value
	lw	v0,DmaMemoryFailedAddress(t0)
	bne	v1,zero,MctadrResetError// Check RFAR reset value
	lw	v1,DmaByteMask(t0)
	bne	v0,zero,MctadrResetError// Check MFAR reset value
	addiu	t1,t0,DmaRemoteSpeed0	  // address of REM_SPEED 0
	bne	v1,zero,MctadrResetError// Check TT_BMASK reset value
	addiu	t2,t0,DmaRemoteSpeed15	  // address of REM_SPEED 15
	lw	v0,0(t1)		// read register
	li	t3,REMSPEED_RESET	//
	addiu	t1,t1,8 		// next register address.
NextRemSpeed:
	bne	v0,t3,MctadrResetError	// Check Rem speed reg reset value
	lw	v0,0(t1)		// read next rem speed
	bne	t1,t2,NextRemSpeed
	addiu	t1,t1,8 		// next register address.
	bne	v0,t3,MctadrResetError	// Check last Rem speed reg reset value

	addiu	t1,t0,DmaChannel0Mode	// address of CHAN0MODE register
	addiu	t2,t0,DmaChannel7Address// address of DMA_CHAN7ADDR (last reg)
	lw	v0,0(t1)		// read register
	addiu	t1,t1,8 		// next register address.
NextChannelReg:
	bne	v0,zero,MctadrResetError// Check channel reg reset value
	lw	v0,0(t1)		// read next channel
	bne	t1,t2,NextChannelReg
	addiu	t1,t1,8 		// next register address.
	bne	v0,zero,MctadrResetError// Checklast  channel reg reset value
	lw	v0,DmaInterruptSource(t0)      // read DMA Channel interrupt
	lw	v1,DmaErrortype(t0)    // read eisa/ethernet error reg
	bne	v0,zero,MctadrResetError// check Intpend
	lw	v0,DmaRefreshRate(t0)
	bne	v1,zero,MctadrResetError// check Eisa error type reset value
	li	t1,REFRRATE_RESET
	bne	v0,t1,MctadrResetError	// check Refresh rate reset value
	lw	v0,DmaSystemSecurity(t0)
	li	t1,SECURITY_RESET
	bne	v0,t1,MctadrResetError	// check Security reg reset value
	lw	v0,DmaInterruptAcknowledge(t0)	// read register but don't check
//
// now perform a register test
//
	bal	PutLedDisplay
	ori	a0,zero,LED_MCTADR_REG	// Next test Led value
MctadrReg:
//
// Check the data path between R4K and Mctadr by writing to Byte mask reg.
//
	li	t0,DEVICE_VIRTUAL_BASE
	sw	zero,DmaCacheMaintenance(t0) // select cache block zero.
	li	t1,1
	sw	t1,DmaLogicalTag(t0)   // Set LFN=zero, Offset=0 , Direction=READ from memory, Valid
	li	t2,0x55555555
	bal	FlushWriteBuffer
	sw	t2,DmaByteMask(t0)	// write patten to Byte mask
	lw	v0,DmaByteMask(t0)	// read Byte mask
	sw	t1,DmaPhysicalTag(t0)	// PFN=0 and Valid
	bne	v0,t2,MctadrRegError	//
	addu	t2,t2,t2		// t1=0xAAAAAAAA
	bal	FlushWriteBuffer
	sw	t2,DmaByteMask(t0)	// write patten to Byte mask
	lw	v0,DmaByteMask(t0)	// read Byte mask
	li	t2,0xFFFFFFFF		// expected value
	bne	v0,t2,MctadrRegError	// Check byte mask
	li	a0,0xA0000000		// get memory address zero.
	bal	FlushWriteBuffer
	sw	zero,0(a0)		// write address zero -> flushes buffers
	lw	v0,DmaByteMask(t0)	// read Byte mask

//
//Initialize mem config to 64MB and Write to some registers
//
	li	t1,0x17F
	sw	t1,DmaConfiguration(t0)       // Init Global Config
	li	t2,0x1555000		//
	sw	t2,DmaTranslationBase(t0)// write to TT BASE
	li	t4,MEMORY_REFRESH_RATE
	sw	t4,DmaRefreshRate(t0)	// Initialize REFRESH RATE
	li	t3,0x5550
	bal	FlushWriteBuffer
	sw	t3,DmaTranslationLimit(t0)	 // write to TT_limit
	lw	v0,DmaConfiguration(t0)       // READ GLOBAL CONFIG
	lw	v1,DmaTranslationBase(t0)      // read TT BASE
	bne	v0,t1,MctadrRegError	// check GLOBAL CONFIG
	lw	v0,DmaTranslationLimit(t0)	 // read  TT_limit
	bne	v1,t2,MctadrRegError	// check TT-BASE
	lw	v1,DmaRefreshRate(t0)	  // Read REFRESH RATE
	bne	v0,t3,MctadrRegError	// check TT-LIMIT
	li	t1,0x2AAA000
	bne	v1,t4,MctadrRegError	// check REFRESH RATE
	li	t2,0xAAA0
	sw	t1,DmaTranslationBase(t0)      // write to TT BASE
	bal	FlushWriteBuffer
	sw	t2,DmaTranslationLimit(t0)	 // write to TT_limit
	lw	v0,DmaTranslationBase(t0)      // read TT BASE
	lw	v1,DmaTranslationLimit(t0)	 // read  TT_limit
	bne	v0,t1,MctadrRegError	// check TT-BASE
	li	t1,TT_BASE_ADDRESS     // Init translation table base address
	sw	t1,DmaTranslationBase(t0)     // Init  TT BASE
	bne	v1,t2,MctadrRegError	// check TT-LIMIT
	sw	zero,DmaTranslationLimit(t0)	 // clear TT-limit
//
// Initialize remote speed registers.
//
	addiu	t1,t0,DmaRemoteSpeed1	  // address of REM_SPEED 1
	la	a1,RomRemoteSpeedValues - LINK_ADDRESS + RESET_VECTOR //
	addiu	t2,a1,14		// addres of last value
WriteNextRemSpeed:
	lbu	v0,0(a1)		// load init value for rem speed
	addiu	a1,a1,1 		// compute next address
	sw	v0,0(t1)		// write to rem speed reg
	bne	a1,t2,WriteNextRemSpeed // check for end condition
	addiu	t1,t1,8 		// next register address
	bal	FlushWriteBuffer
	addiu	a1,t2,-14		// address of first value for rem speed register
	addiu	t1,t0,DmaRemoteSpeed1	// address of REM_SPEED 1
	lbu	v1,0(a1)		// read expected value
CheckNextRemSpeed:
	lw	v0,0(t1)		// read register
	addiu	a1,a1,1 		// address of next value
	bne	v0,v1,MctadrRegError	// check register
	addiu	t1,t1,8 		// address of next register
	bne	a1,t2,CheckNextRemSpeed // check for end condition
	lbu	v1,0(a1)		// read expected value
//
// Now test the DMA channel registers
//
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
	li	a0,0x15 		// Mode
	li	a1,0x2			// enable
	li	a2,0xAAAAA		// byte count
	li	a3,0x555555		// address
WriteNextChannel:
	sw	a0,0(t1)		// write mode
	sw	a1,0x8(t1)		// write enable
	sw	a2,0x10(t1)		// write byte count
	sw	a3,0x18(t1)		// write address
	addiu	t1,t1,DMA_CHANNEL_GAP	// compute address of next channel
	addiu	a2,a2,1 		// change addres
	bne	t1,t2,WriteNextChannel
	addiu	a3,a3,-1		// change Byte count
	bal	FlushWriteBuffer	// flush
//
// Check channel regs.
//
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
	li	a2,0xAAAAA		// byte count
	li	a3,0x555555		// address
CheckNextChannel:
	lw	t4,0x0(t1)		// read mode
	lw	t5,0x8(t1)		// read enable
	bne	t4,a0,MctadrRegError	// check mode
	lw	t4,0x10(t1)		// read byte count
	bne	t5,a1,MctadrRegError	// check enable
	lw	t5,0x18(t1)		// read address
	bne	t4,a2,MctadrRegError	// check abyte count
	addiu	a2,a2,1 		// next expected byte count
	bne	t5,a3,MctadrRegError	// check address
	addiu	t1,t1,DMA_CHANNEL_GAP	// next channel address
	bne	t1,t2,CheckNextChannel
	addiu	a3,a3,-1
//
// Now do a second test on DMA channel registers
//
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
	li	a0,0x2A 		// Mode
	li	a2,0x55555		// byte count
	li	a3,0xAAAAAA		// address
WriteNextChannel2:
	sw	a0,0(t1)		// write mode
	sw	a2,0x10(t1)		// write byte count
	sw	a3,0x18(t1)		// write address
	addiu	t1,t1,DMA_CHANNEL_GAP	// compute address of next channel
	addiu	a2,a2,1 		// change addres
	bne	t1,t2,WriteNextChannel2
	addiu	a3,a3,-1		// change Byte count
	bal	FlushWriteBuffer	// flush
//
// Check channel regs.
//
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
	li	a2,0x55555		// byte count
	li	a3,0xAAAAAA		// address
CheckNextChannel2:
	lw	t4,0x0(t1)		// read mode
	lw	t5,0x10(t1)		// read byte count
	bne	t4,a0,MctadrRegError	// check mode
	lw	t4,0x18(t1)		// read address
	bne	t5,a2,MctadrRegError	// check abyte count
	addiu	a2,a2,1 		// next expected byte count
	bne	t4,a3,MctadrRegError	// check address
	addiu	t1,t1,DMA_CHANNEL_GAP	// next channel address
	bne	t1,t2,CheckNextChannel2
	addiu	a3,a3,-1
//
// Now zero the channel registers
//
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
ZeroChannelRegs:
	addiu	t1,t1,8
	bne	t1,t2,ZeroChannelRegs
	sw	zero,-8(t1)		// clear reg
	bal	FlushWriteBuffer	// flush
	addiu	t1,t0,DmaChannel0Mode	// address of channel 0
	addiu	t2,t1,8*DMA_CHANNEL_GAP // last address of channel regs
CheckZeroedChannelRegs:
	lw	a0,0(t1)
	addiu	t1,t1,8 		// next channel
	bne	a0,zero,MctadrRegError	// check
	nop
	bne	t1,t2,CheckZeroedChannelRegs
	nop
//
// Address chip test passed if code reaches this point
// Skip over error case routines.
//
	b	MapROM			// go for the ROM Checksum

//
// Address chip error routines.
//

MctadrRegError:
        li      t0,DIAGNOSTIC_VIRTUAL_BASE  // get base address of diag register
        lb      t0,0(t0)                    // read register value.
        li      t1,LOOP_ON_ERROR_MASK       // get value to compare
        andi    t0,DIAGNOSTIC_MASK          // mask diagnostic bits.
        li      v0,PROM_ENTRY(14)           // load address of PutLedDisplay
        beq     t1,t0,10f                   // branch if loop on error.
        ori     a0,zero,LED_MCTADR_REG      // load LED display value.
        lui     t0,LED_BLINK                // get LED blink code
        jal     v0                          // Blink LED and hang.
        or      a0,a0,t0                    // pass argument in a0
10:
        lui     t0,LED_LOOP_ERROR           // get LED LOOP_ERROR code
        jal     v0                          // Set LOOP ON ERROR on LED
        or      a0,a0,t0                    // pass argument in a0
	b	MctadrReg
	nop
MctadrResetError:
        li      t0,DIAGNOSTIC_VIRTUAL_BASE  // get base address of diag register
        lb      t0,0(t0)                    // read register value.
        li      t1,LOOP_ON_ERROR_MASK       // get value to compare
        andi    t0,DIAGNOSTIC_MASK          // mask diagnostic bits.
        li      v0,PROM_ENTRY(14)           // load address of PutLedDisplay
        beq     t1,t0,10f                   // branch if loop on error.
        ori     a0,zero,LED_MCTADR_RESET    // load LED display value.
        lui     t0,LED_BLINK                // get LED blink code
        jal     v0                          // Blink LED and hang.
        or      a0,a0,t0                    // pass argument in a0
10:
        lui     t0,LED_LOOP_ERROR           // get LED LOOP_ERROR code
        jal     v0                          // Set LOOP ON ERROR on LED
        or      a0,a0,t0                    // pass argument in a0
	b	MctadrReset
	nop

//
// Map the rom into the TLB so can run from virtual address space.
//
MapROM:
	bal	PutLedDisplay
	ori	a0,zero,LED_ROM_CHECKSUM
//
// initialize the TLB to map the whole ROM. This takes 16 or 32 entries.
//
	li	t0,ROM_LO	       // entry lo
	li	t1,ROM_HI	       // entry hi
	li	t2,(1<<INDEX_INDEX)    // first index
	li	t3,ROM_TLB_ENTRIES*(1<<INDEX_INDEX)  // last  index
RomTlbLoop:
	mtc0	t2,index
	mtc0	t0,entrylo
	mtc0	t1,entryhi
	addiu	t0,PAGE_SIZE		// compute next entry lo
	addiu	t1,PAGE_SIZE		// compute next entry hi
	tlbwi				// write tlb entry
	bne	t2,t3,RomTlbLoop	// check for end of loop
	addiu	t2,(1<<INDEX_INDEX)	// compute next index

//
// now go to the virtual address instead of using the page
// 1FC00000 that is mapped by the address chip.
//
	la	t0,Virtual
	j	t0
	nop
Virtual:
//
// Perform a ROM Checksum.
//
	li	a0,PROM_VIRTUAL_BASE	// address of PROM
	li	t0,ROM_SIZE-8
	add	a1,a0,t0		// end of loop address
	move	t0,zero 		// init sum register
RomCheckSum:
	lw	t1,0(a0)		// fetch word
	lw	t2,4(a0)		// fetch second word
	addu	t0,t0,t1		// calculate checksum
	addiu	a0,a0,8 		// compute next address
	bne	a0,a1,RomCheckSum	// check end of loop condition
	addu	t0,t0,t2		// calculate checksum
	lw	t1,0(a0)		// fetch last word
	lw	t2,4(a0)		// fetch expected checksum value
	addu	t0,t0,t1		// calculate checksum
//
// if test passes, jump to next part of initialization code.
//
	beq	t2,t0,TestMemory	// Go if calculated checksum is correct
	lui	a0,LED_BLINK		// otherwise hang
	bal	PutLedDisplay		// by calling PutLedDisplay
        ori     a0,a0,LED_ROM_CHECKSUM  // blinking the test number



//
// Test the first portion of the memory. Code is fetched from the PROM
//
TestMemory:
	bal	PutLedDisplay		  // call PutLedDisplay to show that
	ori	a0,zero,LED_MEMORY_TEST_1 // Mem test is starting

//
// Call memory test routine to test small portion of memory.
// a0 is start of tested memory. a1 is length in bytes to test
//
	li	a0,KSEG1_BASE			// start of mem test test
	ori	a1,zero,MEMTEST_SIZE		// length to test in bytes
	bal	WriteNoXorAddressTest
	move	a2,zero 			// xor pattern zero
	lui	a3,LED_BLINK
	bal	CheckNoXorAddressTest
	ori	a3,LED_MEMORY_TEST_1		// set LED blink in case of Error
	nop
//
// Do the same flipping all bits
//
	bal	WriteAddressTest
	li	a2,-1				// Xor pattern = FFFFFFFF
	bal	CheckAddressTest
	nop
//
// Do the same flipping some bits to be sure parity bits are flipped in each byte
//
	lui	a2,0x0101
	bal	WriteAddressTest
	ori	a2,a2,0x0101			// Xor pattern = 01010101
	bal	CheckAddressTest
	nop
//
// Now test the tlb by writing to the tested memory
//
//
// Perform a tlb test. Entries 0-16 are used to map the LED and the ROM the
// rest are invalid.
//
	bal	PutLedDisplay		    // call PutLedDisplay to show that
	ori	a0,zero,LED_TLB_TEST	    // TLB test is starting
	li	t0,(ROM_TLB_ENTRIES+1)*(1<<INDEX_INDEX)      // index of first available entry
	li	t1,TLB_TEST_HI		    // address in user space
	li	t2,TLB_TEST_LO
	li	t3,KSEG1_BASE | TLB_TEST_PHYS// Kseg1 address of the
					    // same place as the mapped addresses
	sw	t1,0(t3)		    // store word
	li	t5,64*(1<<INDEX_INDEX)	    // last index
	mtc0	t2,entrylo
NextEntry:
	mtc0	t0,index
	mtc0	t1,entryhi
	nop
	tlbwi
TlbTestBegin:				    // Start of block where tlb misses
	nop				    // are not allowed.
	lw	t4,0(t1)		    // load address from address
	addiu	t0,(1<<INDEX_INDEX)	    // compute next index
	bne	t4,t1, TlbError 	    //
	addiu	t4,t1,PAGE_SIZE 	    // compute next virtual address
	sw	t4,0(t1)		    // write next address in address
	bne	t0,t5,NextEntry 	    // check for tlb full
	move	t1,t4			    // copy virtual address to map
TlbTestEnd:				    // End of block where tlb misses are forbiden

//
// TLB test passed. If there had been an error, there would have been
// a trap, and the trap would have jumped to the following TlbError routine.
// Test passed if code got to here. Go initialize caches.
//
	b	InitCaches
	nop

//
// TLB test failure routine.
//

TlbError:
	lui	a0,LED_BLINK		    // if error hang
	bal	PutLedDisplay		    // while displaying
	ori	a0,LED_TLB_TEST 	    // the test number



//	  .globl TlbReInit
//TlbReInit:
//	  la	  t0,TlbInit - LINK_ADDRESS + RESET_VECTOR
//	  j	  t0
//	  nop

	.globl TlbInit
TlbInit:
/*++

Routine Description:

    This routine will initialize the TLB for virtual addressing. There
    will be 6 basic mappings initially until the operating system sets up a
    full virtual address mapping. Mapped items will include and the virtual
    mapping will be: 
        main memory       A0000000 - A0800000    (uncached)
        main memory       80000000 - 80800000    (cached)
            note that these will not be actual entries because they
            automatically get mapped as kseg[1:0]
        I/O device        E0000000 - E00FFFFF
        Intr src reg      E0100000 - E0100FFF
        video cntr        E0200000 - E0203FFF
        video memory      E0800000 - E0FFFFFF
        prom space        E1000000 - E100FFFF
	eisa i/o space    E2000000 - E2FFFFFF
	eisa mem space    E3000000 - E3FFFFFF
	reserved          E4000000 - 

    All other unused TLB entries will be marked invalid using addresses
	from the reserved region.

    The general algorithm for loading a cache entry is as follows:
        Load Hi register with virtual address and protection bits.
        Load Lo[1:0] register with Physical address and protection bits.
        Load mask register with range of bits to compare with TLB tag.
        Load Index register to point to TLB entry.
        Store with a PLBWI instruction.

Note:

	This routine must be loaded in the first page of the rom.

Arguments:

    None.

Return Value:

    None.

Revision History:

    Added R3000 stuff. Use 16 entries to map 64K of rom and another
    16 entries to map I/O space. Next five entries will be used for
    video controllers, and tlb miss handler will use only entry #37.

    

--*/


//
// Prom space
//
	li	t0,ROM_LO			//entrylo
	li	t2,ROM_HI			//entryhi
	li	t4,(ROM_TLB_ENTRIES << INDEX_INDEX) // loop count
	move	t5,zero 			// first index
rom_tlb:
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t0,t0,(1 << ENTRYLO_PFN)	// increment for next page
	tlbwi					// store tlb entry
	addiu	t5,t5,(1<<INDEX_INDEX)		// next index
	bne	t4,t5,rom_tlb			// exit loop when whole rom mapped
	addiu	t2,t2,(1 << ENTRYHI_VPN)	// increment for next page
//
// I/O Device space
//
	li	t0,DEVICE_LO			//entrylo
	li	t2,DEVICE_HI			//entryhi
	li	t4,((DEVICE_TLB_ENTRIES+ROM_TLB_ENTRIES)<<INDEX_INDEX)	 // last index
device_tlb:
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t0,t0,(1 << ENTRYLO_PFN)	// increment for next page
	tlbwi					// store tlb entry
	addiu	t5,t5,(1<<INDEX_INDEX)		// next index
	bne	t4,t5,device_tlb		// exit loop when whole rom mapped
	addiu	t2,t2,(1 << ENTRYHI_VPN)	// increment for next page
//
// Interrupt source register space
//
	li	t0,PROC_LO			//entrylo
	li	t2,PROC_HI			//entryhi
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t5,t5,(1<<INDEX_INDEX)		// compute next index
	tlbwi					// store tlb entry

//
// video register space
//

	li	t0,VID_LO			//entrylo
	li	t2,VID_HI			//entryhi
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t5,t5,(1<<INDEX_INDEX)		// next index
	tlbwi					// store tlb entry
//
// cursor register space
//
	li	t0,CURSOR_LO			//entrylo
	li	t2,CURSOR_HI			//entryhi
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t5,t5,(1<<INDEX_INDEX)		// next free
	tlbwi					// store tlb entry
//
// Map the two first pages of video memory to avoid taking traps when
// displaying on the first line of the screen
//
	li	t0,VIDMEM_LO			//entrylo
	li	t2,VIDMEM_HI			//entryhi
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t0,t0,(1 << ENTRYLO_PFN)	// second page phys
	tlbwi					// store tlb entry
	addiu	t2,t2,(1 << ENTRYHI_VPN)	// second page virt
	addiu	t5,t5,(1<<INDEX_INDEX)
	mtc0	t0,entrylo
	mtc0	t2,entryhi
	mtc0	t5,index
	addiu	t5,t5,(1<<INDEX_INDEX)
	tlbwi					// store tlb entry
//
// zero the rest of the unused entries.
#define RESV		((1 << ENTRYLO_G) + \
			 (1 << ENTRYLO_N))
	li	t0,RESV					//entrylo
	li	t2,((RESV_VIRT >> 12) << ENTRYHI_VPN)	//entryhi
	li	t4,(64 << INDEX_INDEX)		// last entry
zero_tlb:
	mtc0	t0,entrylo			// store entry lo
	mtc0	t2,entryhi			// store entry high
	mtc0	t5,index			// store index
	addiu	t0,t0,(1 << ENTRYLO_PFN)	// increment for next page
	tlbwi					// store tlb entry
	addiu	t5,t5,(1<<INDEX_INDEX)
	bne	t5,t4,zero_tlb
	addiu	t2,t2,(1 << ENTRYHI_VPN)	// increment for next page
	j	ra
	nop

InitCaches:
//
// Copy routines to the tested memory at the same offset
// from the beginning of memory that they are from the beginning of ROM
// These are, Memory Tests, Zero Memory, PutLedDisplay and DataCopy
//
// calculate arguments for DataCopy call
// a0 is source of data, a1 is dest, a2 is length in bytes
//
	la	a0,MemoryRoutines		    // source
	la	a1,MemoryRoutines-LINK_ADDRESS+KSEG1_BASE // destination location
        la      t2,EndMemoryRoutines                // end
	bal	DataCopy
        sub     a2,t2,a0                            // length of code

//
// Call Cache initialization routine, run it from memory
// different routines for R4000 and R3000
//
	bal	PutLedDisplay
	ori	a0,zero,LED_CACHE_INIT
	la	s1,R3000CacheInit-LINK_ADDRESS+KSEG1_BASE
	jal	s1					// flush data cache
	li	s0, ((1 << PSR_SWC) | (1 << PSR_BEV))	// set bit to swap ic
	mtc0	s0,psr					// and bev
	nop
	jal	s1					// flush icache
	nop
//
// call routine now in non cached memory to test bigger portion of memory
//
	bal	PutLedDisplay			// display that memory test
	ori	a0,zero,LED_WRITE_MEMORY_2	// is starting
	li	a0,KSEG1_BASE+MEMTEST_SIZE	// start of memory to write non cached
	li	a1,ROM_SIZE+STACK_SIZE		// test the memory needed to copy the code
						// to memory and for the stack
	la	s1,WriteNoXorAddressTest-LINK_ADDRESS+KSEG1_BASE // address of routine in memory
	jal	s1				// Write and therefore init mem.
	move	a2,zero 			// xor pattern
	la	s2,CheckNoXorAddressTest-LINK_ADDRESS+KSEG1_BASE // address of routine in memory
	jal	s2				// Check written memory
        ori     a3,zero,LED_READ_MEMORY_2        // load LED value if memory test fails
	la	s1,WriteAddressTest-LINK_ADDRESS+KSEG1_BASE // address of routine in memory
	li	a0,KSEG0_BASE+MEMTEST_SIZE	// start of memory now cached
	li	a2,0xDFFFFFFF			// to flipp all bits
	jal	s1				// Write second time now cached.
	la	s2,CheckAddressTest-LINK_ADDRESS+KSEG1_BASE // address of routine in memory
	jal	s2				// check also cached.
	nop
	lui	a2,0x0101
	jal	s1				// Write third	time cached.
	ori	a2,a2,0x0101			// flipping some bits
	jal	s2				// check also cached.
	nop
//
// if we come back, the piece of memory is tested and therefore initialized.
// The Dcache is also tested.
// Perform the Icache test now.
//
	bal	PutLedDisplay			// display that the icache test
	ori	a0,zero,LED_ICACHE		// is starting
	la	t0,nop_opcode			// get address of nop instruction
	lw	a0,0(t0)			// fetch nop opcode
	la	t1,R3000ICacheTest-LINK_ADDRESS+KSEG1_BASE  // Address of routine in memory
	jal	t1
	lw	a1,4(t0)			// fetch j ra opcode.
//
//  Flush the Icache so that when we run the copy routine cached we
//  don't execute the nops again.
//
	li	s0, ((1 << PSR_SWC) | (1 << PSR_BEV))	// set bit to swap ic
	mtc0	s0,psr					// and bev
	la	s1,R3000CacheInit-LINK_ADDRESS+KSEG1_BASE
	jal	s1					// flush data cache
	nop
//
// Now Put the entry point of the TLBMiss exception to be able to
// access the video Memory
//
	bal	PutLedDisplay			// display that the VideoMemory
	ori	a0,zero,LED_VIDEOMEM		// is being tested.
	li	t0,EXCEPTION_JUMP_TABLE 	// base address of table
	la	t1,TLBMiss			// address of TLB Miss handler
	sw	t1,8(t0)			// use it in User TLB Miss
	sw	t1,12(t0)			// and the other TLB exception
	la	t0,TlbInit - LINK_ADDRESS + RESET_VECTOR
	jal	t0				// Init the TLB runing at the ResetVector Page
	nop
	bal	SizeMemory			// Go to size the memory
	nop					// If we return, the global config
						// is set to the proper configuration.
//
// SELFCOPY
// load addresses to copy and jump to copy in memory routine.
//
	bal	PutLedDisplay			// Display That SelfCopy Starts
	ori	a0,zero,LED_SELFCOPY		//
	la	s0,DataCopy-LINK_ADDRESS+KSEG0_BASE// address of copy routine in cached space
	la	a0,end				// end of this file = begining of
						// next.
        li      a1,RAM_TEST_DESTINATION_ADDRESS // destination is linked address.
        andi    t0,a0,0xFFFF                    // get offset of code address
        li      a2,ROM_SIZE                     // load size of prom
        subu    a2,a2,t0                        // size to copy is rest of prom.
	jal	s0				// jump to copy
        nop
        li      t0,RAM_TEST_LINK_ADDRESS        // load address of code.
//
//  Initialize the stack to the first page of memory and Call Rom tests
//  if the stack grows to much it will overwrite the MemoryTest routine
//  and PutLedDisplay...
//
	li	sp,RAM_TEST_STACK_ADDRESS-16	// init stack
        jal     t0                              // jump to code in memory
	nop
99:
	b	99b				// hang if we get here.
	nop					//
/*++
SizeMemory(
    );
Routine Description:

    This routine sizes the memory and writes the proper value into
    the GLOBAL CONFIG register.

    The way memory is sized is the following:
	The global config is ALREADY set to 64MB
	for each bank base address i.e 48,32,16,0 MB
	  ID0 is written to offset 0 from base of bank
	  ID4 is written to offseet 4MB from base of bank
	  Data is read from offset 0 then:
	    if ID4 is found the SIMMs at the current bank are 1MB SIMMs
		and 4MB wrapped to 0MB.
	    if ID0 is found at offset 0 and ID4 is found at offset 4MB,
		then SIMMs at bank are 4Mb SIMMs.
	    if data does not match or a parity exception is taken
	       then memory is not present in that bank.

Arguments:

	None.

Return Value:

	If the installed memory is inconsistent, does not return
	and the LED flashes A.E

--*/
#define MEM_ID0     0x0A0A0A0A
#define MEM_ID4     0xF5F5F5F5
    LEAF_ENTRY(SizeMemory)
	.set noat
	.set noreorder
	li	t0,EXCEPTION_JUMP_TABLE     // get base address of table
	la	t1,DBEHandler		    // get DBE handler address
	sw	t1,XCODE_DATA_BUS_ERROR(t0) // Install handler in table
	li	t0,0xA3000000		    // get address 48MB
	li	t1,MEM_ID0		    // get ID0
	li	t2,0xA3400000		    // get address 52MB
	li	t3,MEM_ID4		    // get ID4
	li	s0,3			    // counts how many banks left to check
	move	t8,zero 		    // t8 stores the present banks
	move	t9,zero 		    // t9 stores the size of the banks
SizeBank:
	move	a1,zero 		    // set current bank to 1 MB by default
	sw	t1,0x0(t0)		    // fill whole memory line at base of bank
	sw	t1,0x4(t0)
	sw	t1,0x8(t0)
	sw	t1,0xC(t0)
	sw	t3,0x0(t2)		    // fill whole memory line at base of bank + 4MB
	sw	t3,0x4(t2)
	sw	t3,0x8(t2)
	sw	t3,0xC(t2)
	//
	// Check written data
	//
	move	v1,zero 		    // init v1 to zero
	.align	4			    // align address so that Parity Handler
					    // can easily determine if it happened here
ExpectedDBE:
	lw	t4,0x0(t0)		    // read whole memory line.
	lw	t5,0x4(t0)		    // the four words must be identical
	lw	t6,0x8(t0)		    //
	lw	t7,0xC(t0)		    //
DBEReturnAddress:
	bne	v1,zero,10f		    // if v1!=0 Parity exception occurred.
	move	a0,zero 		    // tells that bank not present
        bne     t4,t5,10f                   // check for consistency
	nop
        bne     t4,t6,10f                   // check for consistency
	nop				    //
        bne     t4,t7,10f                   // check for consistency
	nop				    //
	beq	t4,t3,10f		    // If ID4 is found at PA 0
        li      a0,0x1                      // bank is present and SIMMS are 1 MB
        bne     t4,t1,10f                   // if neither ID4 nor ID0 is found
        move    a0,zero                     // no memory in bank
        li      a0,0x1                      // bank is present and SIMMS
                                            // look like they are 4 MB
	//
	// ID written at Address 0 has been correctly checked
	// Now check the ID written at address 4MB
	//
	lw	t4,0x0(t2)		    // read whole memory line.
	lw	t5,0x4(t2)		    // the four words must be identical
        bne     t3,t4,10f                   // check for consistency
	lw	t6,0x8(t2)		    //
        bne     t3,t5,10f                   // check for consistency
	lw	t7,0xC(t2)		    //
        bne     t3,t6,10f                   // check for consistency
	nop				    //
        bne     t3,t7,10f                   // check for consistency
        nop
	li	a1,0x1			    // If all matches SIMMs are 4MB
10:	//
	// a0 has the value 0 if no memory in bank 1 if memory in bank
	// a1 has the value 0 if 1MB SIMMS 1 if 4MB SIMMS
	//
	or	t8,t8,a0		    // accummulate present banks
	or	t9,t9,a1		    // accummulate size of banks
	//
	//  Check if last bank
	//
	beq	s0,zero,Done
	//
	// Now set addresses to check next bank
	//
	li	AT,0x01000000		    // load 16MB
	subu	t0,t0,AT		    // substract to base address
	subu	t2,t2,AT		    // substract to base address + 4MB
	sll	t8,t8,1 		    // make room for next bank
	sll	t9,t9,1 		    // make room for next bank
	b	SizeBank		    // go to size next memory bank
	addiu	s0,s0,-1		    // substract one to the num of banks left
Done:	//
	// t8 has the present banks in bits 3-0 for banks 3-0
	// t9 has the size of the banks in bits 3-2 and 1-0
        //
        // Check that memory is present in bank zero
        //
        andi    t0,t8,1
        beq     t0,zero,WrongMemory

	sll	t8,t8,2 		    // shift bank enable bits to bits 5-2
	andi	t9,t9,0x3		    // get rid of bits 2-3
	or	t8,t9,t8		    // or size of banks with present banks
        ori     v0,t8,0x340                 // Set Video RAM size map PROM bit and init timer.
        li      t0,DEVICE_VIRTUAL_BASE      // Get base address of MCTADR
        sw      v0,DmaConfiguration(t0)     // Store computed Config
	j	ra			    // return to caller.
	nop
WrongMemory:
	//
	// Control reaches here if the memory can't be sized.
	//
	lui	a0,LED_BLINK		    // Hang
	bal	PutLedDisplay		    // blinking the error code
	ori	a0,a0,LED_WRONG_MEMORY	    // in the LED
	.end	SizeMemory
/*++
DBEHandler();
Routine Description:

	This routine is called as a result of a DBE
	It checks if the exception occurred while sizing the memory
	if this is the case, it sets v0=1 and returns to the right
	place.

	If the exception occurred somewhere else, returns to WrongMemory
	where the error code is displayed in the LED.

Arguments:

	This routine does not preserve the contents of v1

Return Value:

	Returns to the right place to avoid taking the exception again

--*/
	LEAF_ENTRY(DBEHandler)
	li	k0,DEVICE_VIRTUAL_BASE	       // get base address of MCTADR
	lw	k1,DmaInterruptSource(k0)  // read Interrupt pending register
	li	k0,(1<<8)	    // mask to test bit 8
	and	k1,k0,k1	    // test for parity error
	beq	k1,k0,ParityError   // branch if parity error
	li	k0,DEVICE_VIRTUAL_BASE	       // get base address of MCTADR
	lw	k1,DmaInvalidAddress(k0)     // read lfar to clear error
	b	NotExpectedReturn   // return
	nop
ParityError:
	lw	k1,DmaMemoryFailedAddress(k0)	  // read MFAR to clear bit 8
	lw	k1,DmaParityDiagnosticLow(k0)  // clear error in parity diag reg.
	mfc0	k0,epc		    // get address of exception
	li	k1,0xFFFFFFF0	    // mask to align address of exception
	and	k0,k1,k0	    // align epc
	la	k1,ExpectedDBE	    // get address where exception is expected
	beq	k0,k1,ExpectedReturn// if equal return
	nop
NotExpectedReturn:
	la	k0,WrongMemory	    // set return address
	j	ra		    // return to dispatcher
	nop			    // which will restore ra and return to K0
ExpectedReturn:
	la	k0,DBEReturnAddress // set return address
	j	ra		    // return to dispatcher which will restore ra and return to K0
	addiu	v1,zero,1	    // set v1 to 1 to signal exception occurred
	.end	DBEHandler

#endif //JAZZ && R3000