download BOOT.ASM
Language: Assembler
License: GPL
Copyright: (C) 2000, Leif Ekblad
LOC: 1427
Project Info
RDOS operating system(rdos)
Server: SourceForge
Type: cvs
SourceForge\r\rdos\rdos\bur\
   ad.cpp
   ad.def
   BOOT.ASM
   bur2bin.asm
   burload.cpp
   burload.def
   CFG2ROM.ASM
   CLEAN.BAT
   DEMO.CFG
   FLASH.ASM
   LCD.ASM
   MK.BAT
   SDRAM.ASM
   SHUTDOWN.ASM
   TEST.ASM
   TEST.DEF
   YMODEM.CPP
   YMODEM.H
   ZFX86.ASM

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
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; RDOS operating system
; Copyright (C) 2000, Leif Ekblad
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version. The only exception to this rule
; is for commercial usage in embedded systems. For information on
; usage in commercial embedded systems, contact embedded@rdos.net
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;
; The author of this program may be contacted at leif@rdos.net
;
; BOOT.ASM
; Second stage boot-loader for DOS
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
						
		NAME  boot

;;;;;;;;; INTERNAL PROCEDURES ;;;;;;;;;;;

.model Small

GateSize = 16

INCLUDE ..\os\system.def
INCLUDE ..\os\port.def
INCLUDE ..\os\protseg.def
INCLUDE ..\os\system.inc

StartSound    Macro
    mov al,0B4h
    out 43h,al
    jmp short $+2
    mov al,10h
    out 42h,al
    jmp short $+2
    out 42h,al
    jmp short $+2
    mov al,3
    out 61h,al
                Endm    

StopSound   Macro
    mov al,0
    out 61h,al
            Endm

WriteZflByte    Macro index, data
    mov al,index
    mov dx,218h
    out dx,al
    mov al,data
    inc dx
    out dx,al
                Endm

ReadZflByte Macro index
    mov al,index
    mov dx,218h
    out dx,al
    inc dx
    in al,dx
            Endm

WriteZflWord    Macro index, data
    mov al,index
    mov dx,218h
    out dx,al
    mov ax,data
    mov dx,21Ah
    out dx,ax
                Endm

ReadZflWord Macro index
    mov al,index
    mov dx,218h
    out dx,al
    mov dx,21Ah
    in ax,dx
            Endm

WriteZflDword   Macro index, data
    mov al,index
    mov dx,218h
    out dx,al
    mov eax,data
    mov dx,21Ah
    out dx,eax
                Endm

ReadZflDword    Macro index
    mov al,index
    mov dx,218h
    out dx,al
    mov dx,21Ah
    in eax,dx
                Endm  
                  
WriteCCR    Macro index, data
    mov al,index
    out 22h,al
    mov al,data
    out 23h,al
            Endm

ReadCCR     Macro index
    mov al,index
    out 22h,al
    in al,23h
            Endm

WriteNB Macro index, data
    mov ax,index
    out 24h,ax
    mov ax,data
    out 26h,ax
        Endm

ReadNB  Macro index
    mov ax,index
    out 24h,ax
    in ax,26h
        ENDM

WriteSIO    Macro index, data
    mov al,index
    out 2Eh,al
    mov al,data
    out 2Fh,al
        ENDM

ReadSIO    Macro index
    mov al,index
    out 2Eh,al
    in al,2Fh
        ENDM

ReadPciDword Macro bus, device, function, index
    mov eax,80000000h OR (bus SHL 16) OR (device SHL 11) OR (function SHL 8) OR (index AND 0FFFCh)
	mov dx,0CF8h
	out dx,eax
	mov dx,0CFCh
	in eax,dx
        ENDM


WritePciDword Macro bus, device, function, index, data
    mov eax,80000000h OR (bus SHL 16) OR (device SHL 11) OR (function SHL 8) OR (index AND 0FFFCh)
	mov dx,0CF8h
	out dx,eax
    mov eax,data
	mov dx,0CFCh
    out dx,eax
	    ENDM

DefaultIdtEntry		MACRO
	dw OFFSET DefaultInt
	dw 8
	dw 8E00h
	dw 0
					ENDM

BootIdtEntry		MACRO Offs
	dw OFFSET Offs
	dw 8
	dw 8E00h
	dw 0
					ENDM

BootExceptionOnePar	MACRO Entry
	push bp
	mov bp,sp
	sti
	push eax
	push ebx
	push ds
	mov al,Entry
	ShutDownPreTask
				ENDM

BootExceptionNoPar	MACRO Entry
	push dword ptr 0
	push bp
	mov bp,sp
	sti
	push eax
	push ebx
	push ds
	mov al,Entry
	ShutDownPreTask
				ENDM

FindRam     MACRO
    Local RamLoop
    Local RamNext

RamNext:
    add esi,1000h
    
RamLoop:
    mov eax,AllocMemSign
    mov [esi],eax
    cmp eax,[esi]
    jne RamNext
            ENDM

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			SetupSerial
;
;		DESCRIPTION:
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SetupSerial    MACRO
    WriteSIO 7, 3
    WriteSIO 30h, 1
    WriteSIO 60h, 3
    WriteSIO 61h, 0F8h
;
;    WriteZflWord 18h, 3F8h
;    WriteZflByte 1Ah, 17h
;
	mov al,83h
	mov dx,3F8h + 3
	out dx,al				; set line control to divisor access
;
	mov dx,3F8h
	mov al,1
	out dx,al				; output LSB divisor latch
;
	mov dx,3F8h + 1
	mov al,0
	out dx,al				; output MSB divisor latch
;
    mov dx,3F8h + 2
    mov al,1
    out dx,al               ; enable FIFOs if present
;
	mov al,3
	mov dx,3F8h + 3
	out dx,al				; set line control 
;
	mov dx,3F8h + 1
	mov al,0
	out dx,al				; disable rx ints, disable tx, line and modem ints
;
	mov dx,3F8h + 4
	mov al,0Bh
	out dx,al				; modem control, DTR = high, RTS = high
        ENDM

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			SendChar
;
;		DESCRIPTION:    Send a single char
;
;       PARAMETERS:     AL      char to send
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SendChar    Macro
    local WaitLoop

    mov ah,al
    
WaitLoop:
    mov dx,3F8h+5
    in al,dx
    test al,40h
    jz WaitLoop
;
    mov dx,3F8h
    mov al,ah
    out dx,al
            Endm

.code

.386p

code_base	DD ?
code_size	DD ?

NoBootText DB 'No kernel to boot up',0

KernelLowError	DB 'Low Kernel Overwrite',0
KernelMidError	DB 'Mid Kernel Overwrite',0
KernelHighError	DB 'High Kernel Overwrite',0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			InitSerial
;
;		DESCRIPTION:
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

InitSerial	Proc near
    push ax
    push dx
    SetupSerial
    pop dx
    pop ax
    ret
InitSerial   Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteChar
;
;		DESCRIPTION:
;
;		PARAMETERS:		AL		CHAR
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteChar	Proc near
    push ax
    push dx
    SendChar
    pop dx
    pop ax
    ret
WriteChar   Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteCRLF
;
;		DESCRIPTION:
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteCRLF	Proc near
    push ax
    push dx
    mov al,0Dh
    SendChar
    mov al,0Ah
    SendChar
    pop dx
    pop ax
    ret
WriteCRLF   Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			SingleHex
;
;		DESCRIPTION:
;
;		PARAMETERS:		AL		Value in
;						AX		Value out
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SingleHex	Proc near
	mov ah,al
	and al,0F0h
	rol al,4
	cmp al,0Ah
	jb ok_low1
	add al,7
ok_low1:
	add al,30h
	and ah,0Fh
	cmp ah,0Ah
	jb ok_high1
	add ah,7
ok_high1:
	add ah,30h
	ret
SingleHex   Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteHexByte
;
;		DESCRIPTION:
;
;		PARAMETERS:		AL		HEX DATA IN
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteHexByte    Proc near
    push ax
	call SingleHex
	call WriteChar
	xchg al,ah
    call WriteChar
    pop ax
    ret
WriteHexByte    Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteHexWord
;
;		DESCRIPTION:
;
;		PARAMETERS:		AX		HEX DATA IN
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteHexWord	Proc near
    xchg al,ah
    call WriteHexByte
    xchg al,ah
    call WriteHexByte
    ret
WriteHexWord    Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteHexDword
;
;		DESCRIPTION:
;
;		PARAMETERS:		EAX		HEX DATA IN
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteHexDword	Proc near
    push eax
    shr eax,16
    call WriteHexWord
    pop eax
    call WriteHexWord
    ret
WriteHexDword   Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			WriteRomString
;
;		DESCRIPTION:
;
;		PARAMETERS:     SI      OFFSET TO STRING
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WriteRomString	Proc near

WriteRomStringLoop:
    mov al,cs:[si]
    or al,al
    jz WriteRomStringDone
    call WriteChar
    inc si
    jmp WriteRomStringLoop
WriteRomStringDone:
    ret
WriteRomString  Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			CalcCrc
;
;		DESCRIPTION:	Calculate CRC for a byte
;
;       PARAMETERS:     AL      CRC
;                       DS:ESI  Data
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

CalcCrc Proc near
    push cx
    mov cl,[esi]
    inc esi
	xor ah,cl
;
	mov cx,1021h
	shl ax,1
	jnc no_xor0
	xor ax,cx
no_xor0:
	shl ax,1
	jnc no_xor1
	xor ax,cx
no_xor1:
	shl ax,1
	jnc no_xor2
	xor ax,cx
no_xor2:
	shl ax,1
	jnc no_xor3
	xor ax,cx
no_xor3:
	shl ax,1
	jnc no_xor4
	xor ax,cx
no_xor4:
	shl ax,1
	jnc no_xor5
	xor ax,cx
no_xor5:
	shl ax,1
	jnc no_xor6
	xor ax,cx
no_xor6:
	shl ax,1
	jnc no_xor7
	xor ax,cx
no_xor7:
    pop cx
    ret
CalcCrc Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			GetAdapter
;
;		DESCRIPTION:
;
;		PARAMETERS:     ESI     Adress to start search at
;						EDI		Max address to search at
;
;       RETURNS:        ESI     Adapter base
;                       ECX     Size of adapter
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SignError	DB 'Rdos Signature Not Found ',0
SizeError	DB 'To Large Adapter ',0

GetAdapter  Proc near
	push ds
	mov ax,flat_sel
	mov ds,ax
GetAdapterSearch:
	mov eax,[esi]
	cmp eax,RdosSign
	je GetAdapterFound

GetAdapterNext:
	add esi,1000h
	jc GetAdapterDone
;
	cmp esi,edi
	jb GetAdapterSearch
	stc
	jmp GetAdapterDone

GetAdapterCorrupt:
	pop esi
	jmp GetAdapterNext

GetAdapterFound:
	push esi
	xor ecx,ecx

GetAdapterNextDriver:
	cmp [esi].sign,RdosSign
	je GetAdapterSignOk
;
    call InitSerial
   	mov si,OFFSET SignError
    call WriteRomString
	jmp GetAdapterCorrupt
	
GetAdapterSignOk:
	cmp [esi].typ,RdosEnd
	je GetAdapterOk
;
	mov edx,[esi].len
	add ecx,edx
	cmp ecx,1000000h
	jc GetAdapterSizeOk
;
    call InitSerial
	mov si,OFFSET SizeError
    call WriteRomString
	jmp GetAdapterCorrupt
	
GetAdapterSizeOk:
	push ecx
	push esi
	mov ecx,edx
	add esi,SIZE rdos_header
	sub ecx,SIZE rdos_header
	jz GetAdapterCrcDone
;
	xor ax,ax

GetAdapterCrcLoop:
	call CalcCrc
	sub ecx,1
	jnz GetAdapterCrcLoop

GetAdapterCrcDone:
	pop esi
	pop ecx
	cmp ax,[esi].crc
	je GetAdapterCrcOk
;
	jmp GetAdapterCorrupt

GetAdapterCrcOk:
	add esi,edx
	jmp GetAdapterNextDriver

GetAdapterOk:
	pop esi
	clc
GetAdapterDone:
	pop ds
	ret
GetAdapter  Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			AddAdapter
;
;		DESCRIPTION:
;
;		PARAMETERS:     ESI     Adapter base
;						ECX     Size of adapter
;						BX		Current Adapter record
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

AddAdapter	Proc near
	push dx
	push di
;
	add ecx,SIZE rdos_header
    inc ds:rom_modules
    mov [bx].adapter_base,esi
    mov [bx].adapter_size,ecx
    mov [bx].adapter_crc,0
    add bx,SIZE adapter_typ
;
	pop di
	pop dx
	ret
AddAdapter	Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			GetAllAdapters
;
;		DESCRIPTION:
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GetAllAdapters  Proc near
    mov ax,system_data_sel
    mov ds,ax
	mov esi,ds:rom1_base
	mov edi,ds:rom1_size
	add edi,esi
    mov ds:rom_modules,0
    mov bx,OFFSET rom_adapters

get_adapters_loop:
    call GetAdapter
    jc get_adapters_done
;
	call AddAdapter
    add esi,ecx
    dec esi
    and si,0F000h
    add esi,1000h
    jmp get_adapters_loop

get_adapters_done:
    ret
GetAllAdapters  Endp

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			StartShutdownDevice
;
;		DESCRIPTION:	Starts shutdown-device
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

StartShutDownDevice	Proc near
    mov ax,system_data_sel
    mov ds,ax
	mov ax,flat_sel
	mov es,ax
	mov cx,ds:rom_modules
	mov bx,OFFSET rom_adapters
	or cx,cx
	jz StartShutDeviceEnd

StartShutAdapterLoop:
	push bx
	push cx
	mov esi,[bx].adapter_base

StartShutDeviceLoop:
	cmp es:[esi].typ,RdosShutDown
	je StartShutDeviceDo
;
	cmp es:[esi].typ,RdosEnd
	je StartShutNextAdapter
;
	add esi,es:[esi].len
	jmp StartShutDeviceLoop

StartShutNextAdapter:
	pop cx
	pop bx
	add bx,SIZE adapter_typ
	loop StartShutAdapterLoop
	jmp StartShutDeviceEnd

StartShutDeviceDo:
	pop cx
	pop bx
;
	push ds
	push es
	pushad
	push cs
	push OFFSET StartShutDeviceInitied
	mov ax,flat_sel
	mov ds,ax
	mov bx,shutdown_code_sel
	push bx
	mov ecx,[esi].len
	add esi,SIZE rdos_header
	push word ptr [esi].init_ip
	add esi,SIZE device_header
	mov ax,gdt_sel
	mov ds,ax
	dec cx
	mov [bx],cx
	mov [bx+2],esi
	mov ah,9Ah
	xchg ah,[bx+5]
	xor al,al
	mov [bx+6],ax
	retf

StartShutDeviceInitied:
	mov ax,gdt_sel
	mov ds,ax
	mov bx,idt_sel
	mov ax,10h*8-1
	mov [bx],ax
	mov eax,0FFFFFFF0h
	sub eax,OFFSET boot_vect
	add eax,OFFSET boot_idt
	mov [bx+2],eax
	db 66h
	lidt [bx]
;
	popad
	pop es
	pop ds

StartShutDeviceEnd:	
	ret
StartShutDownDevice	Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			GetBootDevice
;
;		DESCRIPTION:	Get header of boot-device
;
;		RETURNS:		NC
;						ESI			Boot device base
;						CY			No boot device
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

GetBootDevice	Proc near
    mov ax,system_data_sel
    mov ds,ax
	mov ax,flat_sel
	mov es,ax
	mov cx,ds:rom_modules
	mov bx,OFFSET rom_adapters
	or cx,cx
	jz GetBootDeviceFail
GetBootAdapterLoop:
	push bx
	push cx
	mov esi,[bx].adapter_base
GetBootDeviceLoop:
	cmp es:[esi].typ,RdosKernel
	je GetBootDeviceOk
	cmp es:[esi].typ,RdosEnd
	je GetBootNextAdapter
	add esi,es:[esi].len
	jmp GetBootDeviceLoop
GetBootNextAdapter:
	pop cx
	pop bx
	add bx,SIZE adapter_typ
	loop GetBootAdapterLoop
	jmp GetBootDeviceFail
GetBootDeviceOk:
	pop cx
	pop bx
	clc
	jmp GetBootDeviceEnd
GetBootDeviceFail:	
	stc
GetBootDeviceEnd:
	ret
GetBootDevice	Endp

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			Boot idt
;
;		DESCRIPTION:    exception handlers
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

BootExceptionOnePar	MACRO Entry
	push bp
	mov bp,sp
	push eax
	push ebx
	push ds
	mov al,Entry
	ShutDownPreTask
				ENDM

BootExceptionNoPar	MACRO Entry
	push dword ptr 0
	push bp
	mov bp,sp
	push eax
	push ebx
	push ds
	mov al,Entry
	ShutDownPreTask
				ENDM

Boot0:
	BootExceptionNoPar 0

Boot1:
	BootExceptionNoPar 1

Boot2:
	BootExceptionNoPar 2

Boot3:
	BootExceptionNoPar 3

Boot4:
	BootExceptionNoPar 4

Boot5:
	BootExceptionNoPar 5

Boot6:
	BootExceptionNoPar 6

Boot7:
	BootExceptionNoPar 7

Boot8:
	BootExceptionNoPar 8

Boot9:
	BootExceptionNoPar 9

BootA:
	BootExceptionNoPar 0Ah

BootB:
	BootExceptionOnePar 0Bh

BootC:
	BootExceptionOnePar 0Ch

BootD:
	BootExceptionOnePar 0Dh

BootE:
	BootExceptionNoPar 0Eh

BootF:
	BootExceptionNoPar 0Fh

BootIdtEntry		MACRO Offs
	dw OFFSET Offs
	dw device_code_sel
	dw 8E00h
	dw 0
					ENDM

boot_idt:
	BootIdtEntry Boot0
	BootIdtEntry Boot1
	BootIdtEntry Boot2
	BootIdtEntry Boot3
	BootIdtEntry Boot4
	BootIdtEntry Boot5
	BootIdtEntry Boot6
	BootIdtEntry Boot7
	BootIdtEntry Boot8
	BootIdtEntry Boot9
	BootIdtEntry BootA
	BootIdtEntry BootB
	BootIdtEntry BootC
	BootIdtEntry BootD
	BootIdtEntry BootE
	BootIdtEntry BootF

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			InitRdos
;
;		DESCRIPTION:    Init RDOS
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                
InitRdos:
    xor esi,esi
    FindRam
    mov edx,esi
    mov edi,esi
    mov ax,cs
    mov ds,ax
    mov esi,OFFSET gdt_cs_sel
    add edi,device_code_sel
    mov ecx,2*2
    rep movs dword ptr es:[edi],[esi]
    mov ax,10h
    mov ds,ax
    mov esi,edx
    mov ebx,gdt_sel
    add ebx,esi
    mov word ptr [ebx],0FFFh
    mov [ebx+2],esi
    mov byte ptr [ebx+5],92h
    mov word ptr [ebx+6],0
    lgdt [esi+gdt_sel]
	db 0EAh
	dw OFFSET rdos_startup
	dw device_code_sel

rdos_startup:
    FindRam
    mov ax,gdt_sel
    mov ds,ax
    mov bx,system_data_sel
    mov word ptr [bx],0FFFh
    mov [bx+2],esi
    mov byte ptr [bx+5],92h
    mov word ptr [bx+6],0
;
    mov ax,system_data_sel
    mov ds,ax
    mov ds:alloc_base,esi
    mov ax,gdt_sel
    mov ss,ax
    mov sp,1000h
;
	add esi,1000h
	push esi
	mov ax,cs
	mov ds,ax
	mov ax,flat_sel
	mov es,ax
	mov edi,esi
	xor esi,esi
	mov ecx,OFFSET boot_vect + 10h
	push cx
	shr ecx,2
	rep movs dword ptr es:[edi],[esi]
	pop cx
	and ecx,3
	rep movs byte ptr es:[edi],[esi]
	pop esi
;
	mov ax,gdt_sel
	mov ds,ax
	mov bx,device_code_sel
	mov al,[bx+5]
	mov [bx+2],esi
	mov [bx+5],al
	mov word ptr [bx+6],0
	db 0EAh
	dw OFFSET ram_startup
	dw device_code_sel

ram_startup:
    mov ax,system_data_sel
    mov ds,ax
	mov ax,flat_sel
	mov es,ax
;
	xor edi,edi
	ReadNB 20Fh
;
	mov bx,ax
	test bx,101h
	jz size_dram_bank0_done
;
	ReadNB 202h
	shr ax,9
	and al,7
	mov	cl,al
	mov eax,200000h
	shl eax,cl
	add edi,eax
	
size_dram_bank0_done:
	test bx,202h
	jz size_dram_bank1_done
;
	ReadNB 205h
	shr ax,9
	and al,7
	mov	cl,al
	mov eax,200000h
	shl eax,cl
	add edi,eax
	
size_dram_bank1_done:
	test bx,404h
	jz size_dram_bank2_done
;
	ReadNB 208h
	shr ax,9
	and al,7
	mov	cl,al
	mov eax,200000h
	shl eax,cl
	add edi,eax
	
size_dram_bank2_done:
	test bx,808h
	jz size_dram_bank3_done
;
	ReadNB 20Bh
	shr ax,9
	and al,7
	mov	cl,al
	mov eax,200000h
	shl eax,cl
	add edi,eax
	
size_dram_bank3_done:
	add edi,edi
	mov ds:ram1_size,edi
	mov ds:ram2_size,0
;
	WriteZflDword 2Ah, 0FFFFFFh
	WriteZflDword 26h, 0
;
	mov esi,0FFFFFFF0h
	mov eax,es:[esi]
	mov ebx,es:[esi+4]
	mov ecx,es:[esi+8]
	mov edx,es:[esi+12]
	mov edi,1000h

size_flash_loop:
	mov esi,0FFFFFFF0h
	sub esi,edi	
	cmp eax,es:[esi]
	jne size_flash_next
;
	cmp ebx,es:[esi+4]
	jne size_flash_next
;
	cmp ecx,es:[esi+8]
	jne size_flash_next
;
	cmp edx,es:[esi+12]
	je size_flash_found

size_flash_next:
	shl edi,1
	cmp edi,1000000h
	jne size_flash_loop	

size_flash_found:
	mov esi,1000000h
	sub esi,edi
	dec edi
;
;	WriteZflDword 2Ah, edi 
;	WriteZflDword 26h, esi
;
	inc edi
	or esi,0FF000000h
	mov eax,dword ptr cs:adapter_start
	mov ds:rom1_base,eax
	neg eax
	sub eax,OFFSET boot_vect + 10h
	mov ds:rom1_size,eax
;
	mov ds:rom2_size,0
	mov ds:rom_shadow,1
;
	mov eax,cr0
	and eax,NOT 60000000h
	mov cr0,eax
;
	xor eax,eax
	mov cr3,eax
;
    call GetAllAdapters
	call StartShutDownDevice
	call GetBootDevice
;
	mov ax,flat_sel
	mov ds,ax
	push kernel_code
	mov ecx,[esi].len
	add esi,SIZE rdos_header
	push [esi].init_ip
	add esi,SIZE device_header
	mov ax,gdt_sel
	mov ds,ax
	dec cx
	mov bx,kernel_code
	mov [bx],cx
	mov [bx+2],esi
	mov ah,9Ah
	xchg ah,[bx+5]
	xor al,al
	mov [bx+6],ax
	retf

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			FatalError
;
;		DESCRIPTION:	Fatal error dump to COM1
;
;       PARAMETERS:     DI      Offset of error text in code segment
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

FatalError:
    SetupSerial

fatal_char_loop:
    mov al,cs:[di]
    or al,al
    jz fatal_stop_loop
;
    SendChar
    inc di
    jmp fatal_char_loop

fatal_stop_loop:
	jmp fatal_stop_loop	

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			InitSdram
;
;		DESCRIPTION:    Detect & init SDRAM
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SaveAx  Macro
    mov bp,ax
    shl eax,16
    mov ax,bp
        Endm

SaveBx  Macro
    mov bp,bx
    shl ebx,16
    mov bx,bp
        Endm

SaveCx  Macro
    mov bp,cx
    shl ecx,16
    mov cx,bp
        Endm

SaveDx  Macro
    mov bp,dx
    shl edx,16
    mov dx,bp
        Endm

RestoreAx   Macro
    shr eax,16
        Endm

RestoreBx   Macro
    shr ebx,16
        Endm

RestoreCx   Macro
    shr ecx,16
        Endm

RestoreDx   Macro
    shr edx,16
        Endm

dram_failed     DB 'SDRAM not found',0 
        
InitDram:
    WriteNB 4, 2
    WriteNB 239h, 1249h
    WriteNB 11Ch, 0000h
;
    ReadNB 11Ah
    mov bx,ax
    or bx,7
    WriteNB 11Ah, bx
;
    xor bx,bx
    mov cx,4

check_bank_loop:
    ReadNB 119h
    mov di,ax
    and di,0FFEh
    WriteNB 119h, di
;
    WriteNB 20Fh, 0000h
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    mov bx,80h
    shl bx,cl
;
    ReadNB 20Fh
    or bx,ax
    WriteNB 20Fh, bx
;
    WriteNB 214h, 0000h
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;    
    SaveAx
    SaveCx
    xor ch,ch
    dec cl
    mov ax,3
    mul cx
    xchg ax,dx
    add dx,202h
    RestoreAx
    RestoreCx
    WriteNB dx, 0000h
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    dec cx
    mov ax,3
    mul cx
;
    xchg ax,bx
    add bx,204h
    WriteNB bx, 0FC71h        
;
    mov bx,cx
    shl bx,1
    or bx,221h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,229h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,221h
;
    mov cx,8

check_mode_loop:
    and bx,0FFE7h
    WriteNB 213h, bx
;
    or bx,18h
    WriteNB 213h, bx
    loop check_mode_loop
;
    and bx,6
    or bx,231h    
    WriteNB 213h, bx
;
    and bx,6
    or bx,220h
    WriteNB 213h, bx
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;    
    mov esi,00800000h    
    mov dword ptr [esi],5A5A5A5Ah
    mov dword ptr [esi+100h],0
    mov eax,[esi]
    cmp eax,5A5A5A5Ah
    jz init_dram_cont
;
    WriteNB 20Fh, 0
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    mov bx,8000h
    rol bx,cl
    ReadNB 20Fh
    or bx,ax
    WriteNB 20Fh, bx
;
    WriteNB 214h, 1
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;
    SaveAx
    SaveCx
    xor ch,ch
    dec cl
    mov ax,3
    mul cx
    xchg ax,dx
    add dx,202h
    RestoreAx
    RestoreCx
;    
    WriteNB dx, 0008
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    dec cx
    mov ax,3
    mul cx
    xchg ax,bx
    add bx,204h
    WriteNB bx, 0FC71h
;
    mov bx,cx
    shl bx,1
    or bx,221h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,229h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,221h
;
    mov cx,8

l3bd:
    and bx,0FFE7h
    WriteNB 213h, bx
;
    or bx,18h
    WriteNB 213h, bx
    loop l3bd 
;
    and bx,6
    or bx,231h
    WriteNB 213h, bx
;
    and bx,6
    or bx,220h
    WriteNB 213h, bx
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;
    mov esi,00800000h    
    mov dword ptr [esi],5A5A5A5Ah
    mov dword ptr [esi+100h],0
    mov eax,[esi]
    cmp eax,5A5A5A5Ah
    jz init_dram_cont
;
    dec cx
    jnz check_bank_loop       
;   
    jmp check_bank_done

init_dram_cont:
    ReadNB 119h
    or ax,1
    mov di,ax
    WriteNB 119h, di
;
    ReadNB 20Fh
    or bx,ax
;
    SaveAx
    SaveCx
    xor ch,ch
    dec cl
    mov ax,3
    mul cx
    xchg ax,dx
    add dx,202h
    RestoreAx
    RestoreCx
;
    ReadNB dx
    or ax,2A00h
    mov di,ax
;
    mov esi,4
        
check_bank_col_loop:
    WriteNB dx, di

check_bank_mem_loop:
    mov ebp,00800000h    
    mov dword ptr ds:[ebp],1A56E2C6h
    mov dword ptr ds:[esi+ebp],5A5A5A5Ah
    mov eax,ds:[ebp]
    cmp eax,1A56E2C6h
    jnz check_bank_first_failed
;
    mov eax,ds:[esi+ebp]
    cmp eax,5A5A5A5Ah
    jnz check_bank_next
;
    shl esi,1
    cmp esi,ebp
    jnz check_bank_mem_loop
;
    shr esi,1
    jmp check_bank_next

check_bank_first_failed:
    mov eax,esi
    cmp eax,1000000h
    ja check_bank_next
;
    shl eax,1    
    test bx,0FFh
    jz check_bank_col_ok
;
    shl eax,1

check_bank_col_ok:
    and ax,3000h
    and di,0CFFFh
    or di,ax
    mov esi,4
    jmp check_bank_col_loop

check_bank_next:
    shr esi,17
    bsf eax,esi
    dec ax
    shl ax,9
    and di,0F1FFh
    or di,ax
;
    WriteNB 20Fh, 0
;
    and si,0FFh
    or di,si
    WriteNB dx, di
;
    dec cl
    jnz check_bank_loop

check_bank_done:
    ReadNB 119h
    or ax,1
    mov di,ax
    WriteNB 119h, di
;
    WriteNB 20Fh, 0000h
;
    mov cx,1
    mov di,1
    xor dx,dx
    mov bp,dx
    shl ebp,16

setup_bank_loop:
    mov ch,cl
    test bx,cx
    jz l622
;
    xchg cx,di
    SaveAx
    SaveCx
    xor ch,ch
    dec cl
    mov ax,3
    mul cx
    xchg ax,dx
    add dx,202h
    RestoreAx
    RestoreCx 
    xchg cx,di
    ReadNB dx
;           
    mov si,ax
    and si,000FFh
    and ax,0FF00h
    shr ebp,16
    mov dx,bp
    shl ebp,16
    add ax,dx
    add dx,si
    mov bp,dx
    shl ebp,16
    mov si,ax
;
    xchg cx,di
    SaveAx
    SaveCx
    xor ch,ch
    dec cl
    mov ax,3
    mul cx
    xchg ax,dx
    add dx,202h
    RestoreAx
    RestoreCx
    xchg cx,di
    WriteNB dx, si

l622:
    cmp di,4
    jz l62d
;
    shl cl,1
    inc di
    jmp setup_bank_loop

l62d:
    mov cx,1
    mov di,1

l633:
    test bl,cl
    jz l7b2
;
    xchg cx,di
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    mov bx,8000h
    rol bx,cl
;
    ReadNB 20Fh
    or bx,ax
    WriteNB 20Fh, bx
;
    WriteNB 214h, 0001h
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    dec cx
    mov ax,3
    mul cx
    xchg ax,bx
    add bx,204h
    WriteNB bx, 0FC71h
;
    mov bx,cx
    shl bx,1
    or bx,221h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,229h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,221h
;
    mov cx,8

l763:
    and bx,0FFE7h
    WriteNB 213h, bx
; 
    or bx,18h
    WriteNB 213h, bx
    loop l763
;
    and bx,6
    or bx,231h
    WriteNB 213h, bx
;
    and bx,6
    or bx,220h
    WriteNB 213h, bx
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
    xchg cx,di
    jmp l92e

l7b2:
    test cl,bh
    jz l92e
;
    xchg cx,di
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    mov bx,80h
    rol bx,cl
;
    ReadNB 20Fh
    or bx,ax
    WriteNB 20Fh, bx
;
    WriteNB 214h, 0000h
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
;
    SaveAx
    SaveBx
    SaveCx
    SaveDx
;
    dec cx
    mov ax,3
    mul cx
    xchg ax,bx
    add bx,204h
    WriteNB bx, 0FC71h
;
    mov bx,cx
    shl bx,1
    or bx,221h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,229h
    WriteNB 213h, bx
;
    mov bx,cx
    shl bx,1
    or bx,221h
;
    mov cx,8

l8e2:
    and bx,0FFE7h
    WriteNB 213h, bx
; 
    or bx,18h
    WriteNB 213h, bx
    loop l8e2
;
    and bx,6
    or bx,231h
    WriteNB 213h, bx
;
    and bx,6
    or bx,220h
    WriteNB 213h, bx
;
    RestoreAx
    RestoreBx
    RestoreCx
    RestoreDx
    xchg cx,di

l92e:
    cmp di,4
    jz l939
;
    shl cl,1
    inc di
    jmp l633

l939:
    SaveAx
    SaveCx
;
    WriteNB 211h, 0000h
;
    mov cx,-1
    
init_dram_delay:   
    loop init_dram_delay
;
    RestoreAx
    RestoreCx
;
    ReadNB 21Ah
    and ax,0FFF8h
    mov di,ax
    WriteNB 21Ah, di
;
    ReadNB 20Fh
    or al,al
    jz init_dram_failed
;
    xor esi,esi
    mov eax,2AFC478Ah
    mov [esi],eax
    cmp eax,[esi]
    je InitRdos
    
init_dram_failed:
    mov di,OFFSET dram_failed
    jmp FatalError

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			prot_init
;
;		DESCRIPTION:	Basic ZFX86 chip-set initialization
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

prot_init:
	mov ax,10h
	mov ds,ax
	mov es,ax
	mov fs,ax
	mov gs,ax
;
    WriteCCR 0C2h, 0E2h
;
    ReadCCR 0C3h
    or al,8
    mov ah,al
    WriteCCR 0C3h, ah
;     
    WriteNB 20Fh, 0
    WriteNB 11Bh, 210h
    WriteNB 11Dh, 3D0h
    WriteNB 110h, 0
    WriteNB 111h, 0
    WriteNB 112h, 0
    WriteNB 113h, 0
    WriteNB 114h, 0
    WriteNB 115h, 0
    WriteNB 117h, 0
    WriteNB 118h, 0
    WriteNB 119h, 6Bh
    WriteNB 11Ah, 220h
    WriteNB 11Eh, 0
    WriteNB 11Fh, 0
    WriteNB 120h, 0
    WriteNB 200h, 0
    WriteNB 201h, 0
    WriteNB 202h, 0
    WriteNB 204h, 0FFFFh
    WriteNB 205h, 0
    WriteNB 207h, 0FFFFh
    WriteNB 208h, 0
    WriteNB 20Ah, 0FFFFh
    WriteNB 20Bh, 0
    WriteNB 20Dh, 0FFFFh
    WriteNB 20Eh, 0
    WriteNB 20Fh, 0
    WriteNB 213h, 321h
    WriteNB 214h, 0    
;
    WritePciDword 0, 12h, 0, 4, 0280000Fh
;
    ReadPciDword 0, 12h, 0, 0Ch
    and ax,0FF00h
    mov edi,eax
    WritePciDword 0, 12h, 0, 4, edi
;
    ReadPciDword 0, 12h, 0, 40h
    and eax,0FF0000h
    or eax,02000019h
    mov edi,eax
    WritePciDword 0, 12h, 0, 40h, edi
;    
    WritePciDword 0, 12h, 0, 10h, 00008101h
    WritePciDword 0, 12h, 0, 50h, 029A557Ch  ; ISA bus speed
;
    ReadPciDword 0, 12h, 0, 58h
    and eax,0FFFFh
    or eax,28CF0000h
    mov edi,eax
    WritePciDword 0, 12h, 0, 58h, edi
;
    ReadPciDword 0, 12h, 0, 5Ch
    xor ax,ax
    mov edi,eax
    WritePciDword 0, 12h, 0, 5Ch, edi
;    
    ReadPciDword 0, 12h, 0, 80h
    mov al,1
    mov edi,eax
    WritePciDword 0, 12h, 0, 80h, edi
;    
    ReadPciDword 0, 12h, 1, 4
    xor al,al
    mov edi,eax
    WritePciDword 0, 12h, 1, 4, edi
;
    ReadPciDword 0, 12h, 2, 4
    mov al,5
    mov edi,eax
    WritePciDword 0, 12h, 2, 4, edi
;
    ReadPciDword 0, 12h, 3, 4
    mov al,3
    mov edi,eax
    WritePciDword 0, 12h, 3, 4, edi
;
    WritePciDword 0, 12h, 3, 40h, 0FFFFFFC1h
    WritePciDword 0, 12h, 3, 10h, 00008201h
;
    ReadPciDword 0, 13h, 0, 4
    mov al,7
    mov edi,eax
    WritePciDword 0, 13h, 0, 4, edi
;
    ReadPciDword 0, 13h, 0, 0Ch
    xor ax,ax
    mov edi,eax
    WritePciDword 0, 13h, 0, 0Ch, edi     
;
    WritePciDword 0, 12h, 0, 44h, 02FE0000h
;
    mov dx,8200h
    mov eax,05000000h
    out dx,eax
;
    mov dx,8204h
    mov eax,00000030h
    out dx,eax
;
    mov dx,8208h
    mov eax,00009200h
    out dx,eax
;
    WriteSIO 21h, 1
    WriteSIO 22h, 0
;
    WriteSIO 7, 0Ah
    WriteSIO 30h, 1
;
    WriteCCR 0C1h, 0
    jmp InitDram

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			startup
;
;		DESCRIPTION:	Boot-up code
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; beyond this point offset-addresses must not be used!!

startup:
    mov bx,0FFD8h
	db 66h
	lgdt fword ptr cs:[bx]
;
	mov bx,0FFD0h
	db 66h
	lidt fword ptr cs:[bx]
;
	xor ax,ax
	mov ss,ax
;
	mov eax,cr0
	or al,1
	mov cr0,eax
	db 0EAh
	dw OFFSET prot_init
	dw 8

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			DefaultInt
;
;		DESCRIPTION:	Boot-time exception handlers
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ExceptionText DB 'Exception Fault in Boot ',0

DefaultInt:
	mov di,OFFSET ExceptionText
	jmp FatalError

; this must always reside at FFFFFF50, and should always be 128 bytes long!!

rom_idt:
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry
	DefaultIdtEntry

; this must always reside at FFFFFFD0, and should always be 32 bytes long!!

; boot idt address

	DW 7Fh
	DD 0FFFFFF50h
	DW 0

; null sel, and gdt base address

	DW 17h
	DD 0FFFFFFD8h
	DW 0

gdt_cs_sel:

    DW OFFSET boot_vect + 0Fh
    DW OFFSET boot_vect      ; must be patched to FFF0 - OFFSET boot_vect
    DW 9BFFh
    DW 0FF00h

; flat sel

	DW 0FFFFh
	DD 92000000h
	DW 008Fh

; this must reside at FFFFFFF0, and should always be 16 bytes long!!

boot_vect:
    jmp startup

    DB 'RDOS boot'

adapter_start:
	DD 0					; must be patched to start of adapters

	END boot_vect

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us