summaryrefslogtreecommitdiff
path: root/v4.0-ozzie/bin/DISK2/BIOS/IBMBIO.ASM
blob: 318b1b0462d4b4b4dcd9348eaf93e0363f051a91 (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
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
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
TITLE	IBMBIO	IBM BIOS FOR MS-DOS 4.0

;   Modified for 3.0, July '83, Marc McDonald
;
;   arw 02/01/84
;	split disk driver into separate file
;   arw 05/01/84
;	split out console driver

;::::::::::::::::::::::::::::::::::::::::::::::
;
;	IBM ADDRESSES FOR I/O
;
;::::::::::::::::::::::::::::::::::::::::::::::

	PAGE	,132
;DEBUGFLG =	1		; don't enable debug printfs
CONSFLAG =	0		; =1 to include console driver here

.xlist
	INCLUDE DEFDBUG.INC
.list

	BIOSEG=70H		;0070	SEGMENT FOR THE BIOS
	SYSIZE=100H		;Number of paragraphs in sysinit module
	RSINIT=0A3H		;RS232 INITIALIZATION
				;9600 BAUD:NO PARITY:1 STOP:8 BIT WORD
	LF=10			;LINE FEED
	CR=13			;CARRIAGE RETURN
	BACKSP=8		;BACKSPACE
	BRKADR=6CH		;006C  BREAK VECTOR ADDRESS
	DSKADR=1EH*4		;ADDRESS OF PTR TO DISK PARAMETERS
	SEC9=522H		;ADDRESS OF DISK PARAMETERS
	AKPORT=20H
	EOI=20H


;**	Timer and Clock Definitions

SCHEDCOUNT	EQU	50		; # msec/scheduler tick
MOTORCOUNT	EQU	55		; # msec/ROM BIOS tick

	EXTRN	CURRENT_DOS_LOCATION:WORD
	EXTRN	FINAL_DOS_LOCATION:WORD
	EXTRN	DEVICE_LIST:DWORD
	EXTRN	MEMORY_SIZE:WORD
	EXTRN	DEFAULT_DRIVE:BYTE
	EXTRN	SYSINIT:FAR

BiosSeg GROUP	Code,BiosInit
Code	SEGMENT BYTE PUBLIC	'CODE'

   ASSUME CS:BiosSeg,DS:NOTHING,ES:NOTHING

START$:
	JMP	INIT

	DB	20 DUP (0)	; IBM WANTS SOME ZEROED AREA

SUBTTL	Jump tables for devices
;--------------------------------------------------------------
;
;	COMMAND JUMP TABLES
;
;	BEWARE - These tables overlap somewhat! -c.p.
;
IF	CONSFLAG
CONTBL:
	DW	SetAddr
	DW	StatusComplete
	DW	StatusComplete
	DW	CMDERR
	DW	CON$READ
	DW	CON$RDND
	DW	StatusComplete
	DW	CON$FLSH
	DW	CON$WRIT
	DW	CON$WRIT
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
ENDIF	;CONSFLAG

AUXTBL:
	DW	StatusComplete
	DW	StatusComplete
	DW	StatusComplete
	DW	CMDERR
	DW	AUX$READ
	DW	AUX$RDND
	DW	StatusComplete
	DW	AUX$FLSH
	DW	AUX$WRIT
	DW	AUX$WRIT
	DW	AUX$WRST
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR

TIMTBL:
	DW	StatusComplete
	DW	StatusComplete
	DW	StatusComplete
	DW	CMDERR
	DW	TIM$READ
	DW	StatusDevReady
	DW	StatusComplete
	DW	StatusComplete
	DW	TIM$WRIT
	DW	TIM$WRIT

PRNTBL:
	DW	StatusComplete
	DW	StatusComplete
	DW	StatusComplete
	DW	CMDERR
	DW	StatusNoXfer		; Printer doesn't read
	DW	StatusDevReady
	DW	StatusComplete
	DW	StatusComplete
	DW	PRN$WRIT
	DW	PRN$WRIT
	DW	PRN$STAT
	DW	StatusComplete
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR
	DW	CMDERR

SUBTTL	Device entry points
;---------------------------------------------------
;
;	Device Entry point
;
CMDLEN	=	0	;LENGTH OF THIS COMMAND
UNIT	=	1	;SUB UNIT SPECIFIER
CMD	=	2	;COMMAND CODE
STATUS	=	3	;STATUS
MEDIA	=	13	;MEDIA DESCRIPTOR
TRANS	=	14	;TRANSFER ADDRESS
Dfun	=	DWORD PTR 14		; Passed addr of dos function
COUNT	=	18	;COUNT OF BLOCKS OR CHARACTERS
START	=	20	;FIRST BLOCK TO TRANSFER

AUXNUM	DB	0	;WHICH AUX DEVICE WAS REQUESTED

; Dos routine to do functions for device drivers
	PUBLIC	DosFunction
DosFunction	DD	0	; Device help function entry point
ScrnIOok	dd	0	; (char *) true if in current screen locus

TIM_DRV DB	-1		; TIME WHEN LAST DISK I/O PERFORMED
TIM_REM DW	0		; scheduler tics left until media presumed
				; changed

;**	Debugging control:
;
;	Group	Level	Controls
;	 01		General BIOS, bootstrapping and initialization
;		 01		Device initialization
;		 02		MS-DOS bootstrapping
;		 04		Driver error conditions
;		 10		Command dispatch
;	 04		Diskette driver
;		 01		Initialization
;		 02		System entrys
;		 04		Details of read/write processing
;	 08		Hard disk driver
;		 01		Initialization
;		 02		System entrys
;	 10		Console display driver


	PUBLIC	BUGBITS
BUGBITS DB  000H			; group bits
	DB  0ffH			; level bits

Strategy	PROC	FAR
	RET
Strategy	ENDP

IF	CONSFLAG
CON$IN:
	PUSH	SI
	MOV	SI,OFFSET CONTBL
	JMP	SHORT Interrupt
ENDIF	;CONSFLAG

AUX0$IN:
	PUSH	SI
	PUSH	AX
	XOR	AL,AL
	JMP	SHORT AUXENT

AUX1$IN:
	PUSH	SI
	PUSH	AX
	MOV	AL,1
AUXENT:
	MOV	SI,OFFSET AUXTBL
	JMP	SHORT entry1

PRN0$IN:
	PUSH	SI
	PUSH	AX
	XOR	AL,AL
	JMP	SHORT PRNENT

PRN1$IN:
	PUSH	SI
	PUSH	AX
	MOV	AL,1
	JMP	SHORT PRNENT

PRN2$IN:
	PUSH	SI
	PUSH	AX
	MOV	AL,2
PRNENT:
	MOV	SI,OFFSET PRNTBL
	JMP	SHORT entry1

TIM$IN:
	PUSH	SI
	MOV	SI,OFFSET TIMTBL


COMMENT *
   Interrupt is the main part of the interrupt device entry point( a
misnomer, actually is the execute function entry ) for all devices.
The various devices set up the dispatch table address and unit choice
and then jump to Interrupt which then dispatches to the appropriate
device routine.

Entry parameters:
	SI	Address of device dispatch table
	AL	Unit # for Aux/Prn( stored in AuxNum )
	ES:BX	Device request packet address

Exit parameters:	( to device routine )
	AL	Unit code from packet
	AH	Media descriptor from packet
	CX	Count from packet
	DX	Start sector from packet
	ES:DI	Buffer address from packet
	DS:BX	Packet address
	SS	TaskArea segment( from dos call )
	*

	PUBLIC	Interrupt
Interrupt	PROC	FAR
	PUSH	AX
entry1:
	PUSH	CX
	PUSH	DX
	PUSH	DI
	PUSH	BP
	PUSH	DS
	PUSH	ES
	PUSH	BX
	debug	1,10H,< INTERRUPT cmdtbl $x >,<si>
	MOV	CS:[AUXNUM],AL		; Save unit choice of AUX/PRN
	MOV	AX,ES
	MOV	DS,AX			; DS:BX also points to packet

	MOV	AL,DS:[BX].Cmd		; Get device command
	XOR	AH,AH
	ADD	SI,AX
	ADD	SI,AX			; Get Address of routine
	CMP	AL,18			; Too high a command number?
	JA	CmdErr			; Yes, error
	MOV	AL,DS:[BX].UNIT		;AL = Unit code
	MOV	AH,DS:[BX].MEDIA	;AH = Media descriptor
	MOV	CX,DS:[BX].COUNT	;CX = Count
	MOV	DX,DS:[BX].START	;DX = Start sector
	LES	DI,DS:[BX].TRANS	; ES:DI = buffer addr
	debug	1,10H,<un $x xfer $x:$x rtn $x\n>,<ax,es,di,cs:[si]>
	JMP	WORD PTR CS:[SI]	; Do request
Interrupt	ENDP

SUBTTL	Routines used by device routines
COMMENT	*
   All routines on this page are various exits for device functions.
They each return different information in the request packet for the
dos. The routines are as follows:

	StatusDevReady
		  The busy and done bits are set in the packet. This
		means that the device has input to be read or can
		do output without any waiting.

	StatusPartialXfer
		  The device was unable to do the I/O for the requested
		number of bytes/blocks. CX contains the number that are
		left to do. Fall into StatusError to set error has
		happened.

	StatusError
		  Set the error and done bits in the status.

	StatusNoXfer
		  The device couldn't do the read or write, set the
		number of bytes transferred to 0, but don't set the
		error bit.

	StatusComplete
		  The device actually completed the request and every-
		thing was just fine, so just set the done bit in the
		request status.

	StatusWait
		  The device driver is for 3.0 and saw that it would
		have to wait in a loop to do the request, so instead
		it will not set the done bit which tells the dos to
		put the task into the I/O wait queue.

Entry parameters:	( for all of the above routines )
	DS:BX	Address of device request packet
	CX	Count of bytes/blocks left to transfer if applicable
	AL	Error code if applicable

Exit parameters:
	ES:BX	Pointer to packet
	AX	Destroyed
	SI	Destroyed
	All other registers preserved
	*

	PUBLIC	StatusDevReady
StatusDevReady	PROC	NEAR		; Device has data or can send
	MOV	AH,00000011B		; Done + busy
	JMP	SHORT SetStatus
StatusDevReady	ENDP

	PUBLIC	CmdErr
CmdErr	PROC	NEAR		; Bad device command number
	debug	1,4,< CMDERR - $b $x\n>,<ax,si>
	MOV	AL,3			; Unknown command error
CmdErr	ENDP

StatusPartialXfer	PROC	NEAR
	SUB	[BX].COUNT,CX		;# of successful I/O's
StatusPartialXfer	ENDP

	PUBLIC	StatusError
StatusError	PROC	NEAR
	MOV	AH,10000001B		; Error + done
	JMP	SHORT SetStatus
StatusError	ENDP

StatusNoXfer	PROC	NEAR
	XOR	AX,AX
	MOV	[BX].COUNT,AX		; No chars read
StatusNoXfer	ENDP

	PUBLIC	StatusComplete
StatusComplete	PROC	NEAR
	MOV	AH,00000001B		; Done
	PUBLIC	SetStatus
SetStatus:
	POP	BX
	POP	ES
	MOV	ES:WORD PTR [BX].STATUS,AX     ;MARK OPERATION COMPLETE
	POP	DS
	POP	BP
	POP	DI
	POP	DX
	POP	CX
	POP	AX
	POP	SI
XXX	PROC	FAR
	RET				;RESTORE REGS AND RETURN
XXX	ENDP
StatusComplete	ENDP

StatusWait	PROC	NEAR
	MOV	AH,0			; Don't set done bit
	MOV	[BX].Count,CX		; Set number completed
	MOV	WORD PTR [BX].Trans,DI	; Set new offset
	JMP	SetStatus
StatusWait	ENDP

IF	CONSFLAG
; Set the address of the dos function routine for drivers
SetAddr PROC	NEAR
	MOV	AX,WORD PTR [BX].Dfun
	MOV	WORD PTR DosFunction,AX
	MOV	AX,WORD PTR [BX+2].Dfun
	MOV	WORD PTR (DosFunction+2),AX
	mov	ax,0
	mov	cx,1
	mov	dx,16
	call	DosFunction
	mov	word ptr ScrnIOok,ax
	mov	word ptr ScrnIOok+2,dx
	JMP	StatusComplete
SetAddr ENDP


;-------------------------------------------------------------
;
;	CHROUT - WRITE OUT CHAR IN AL USING CURRENT ATTRIBUTE
;
;	CALLED VIA INT 29H
;
CHROUT	=	29H

OUTCHR: STI
	PUSH	AX
	MOV	BX,7
	MOV	AH,14		;WRITE CHARACTER
	INT	10H		;SEND THE CHARACTER
	POP	AX
	IRET
ENDIF	;CONSFLAG

;----------------------------------------------
;
;	SET DX TO AUXNUM
;
GETDX:	MOV	DL,[AUXNUM]
	XOR	DH,DH
	RET

SUBTTL	Console driver
COMMENT	*
   This is the console( CON ) device driver. The input side is assigned
to the keyboard and the output to the video screen. The output code
remains more or less the same as in 2.0. The input side, however, is
changed for 4.0 to enter an I/O wait rather than loop waiting for a
character.
	*

IF	CONSFLAG
CONDEV	LABEL	WORD			;HEADER FOR DEVICE "CON"
	DW	AUXDEV,BIOSEG
	DW	1000000000010011B	;CON IN AND CON OUT + SPECIAL
	DW	STRATEGY
	DW	CON$IN
	DB	'CON     '
ELSE
	EXTRN	CONDEV:NEAR
	PUBLIC	AUXDEV
ENDIF	;CONSFLAG

IF	CONSFLAG
Key2ndPart	DB	0		; Leftover byte of 2 key codes

RomData SEGMENT AT 40H
	ORG	1AH
Bufferhead	DW	?
BufferTail	DW	?
KeyBuffer	LABEL	WORD
KeyBufLen	equ	32		; length of KeyBuffer
RomData ENDS

; BREAK interrupt routine
; ROM interrupt handler resets buffer pointers to beginning of buffer
; and places a 0000h dummy character into the buffer.

Break	PROC	NEAR
	mov	Key2ndPart,3		; Force next char to be ^C( stop )
	IRET
Break	ENDP

SUBTTL	Console read and subroutines
	PAGE

COMMENT *
   The console read dispatch tries to read the selected number of
characters from the keyboard. If at any point there is no key in
the queue, it returns to the dos to allow another process to run
until a key is depressed.

Entry parameters:
	ES:DI	Pointer to buffer in which to store characters
	CX	Number of characters to read
	DS:BX	Pointer to device request packet

Exit parameters:
	CX	Number of characters left to read
	DS:BX	Pointer to device request packet
	*

Con$Read	PROC	NEAR
	JCXZ	StatusComplete
CON$LOOP:
	PUSH	CX		;SAVE COUNT
	CALL	ChrIn		;GET CHAR IN AL
	POP	CX
	STOSB			;STORE CHAR AT ES:DI
	LOOP	CON$LOOP
	JMP	StatusComplete
Con$Read	ENDP

COMMENT *
    ChrIn attempts to read a character from the keyboard queue that
is maintained by the ROM BIOS. If the queue is not empty, the code
is returned. Otherwise, the packet is added to the list of keyboard
reads and carry is set to cause the driver routine to return to the
dos with the done bit not set which results in an I/O wait.

Entry parameters:
	DS:BX	Pointer to device request packet

Exit parameters:
	AL	Character from keyboard if present
*

ChrIn	PROC	NEAR
ConReadLoop:
	XOR	AX,AX
	XCHG	AL,Key2ndPart		; GET CHARACTER & ZERO Key2ndPart
	OR	AL,AL
	JNZ	KeyRet
	MOV	AH,0
	INT	16H			; Get the char
	OR	AX,AX		;Check for non-key after BREAK
	JZ	ConReadLoop
	CMP	AX,7200H	;Check for CTRL-PRTSC
	JNZ	ALT15
	MOV	AL,16
ALT15:
	OR	AL,AL		; 2 byte keycode?
	JNZ	KeyRet		; No, have whole code
	MOV	Key2ndPart,AH	; Yes, store scan code for next read
KeyRet: RET
ChrIn	ENDP


COMMENT *
   The non-destructive keyboard read routine returns the next char
in the queue if there is one.

Entry parameters:
	DS:BX	Pointer to device request packet

Exit parameters:
	DS:BX	Pointer to device request packet
	*
Con$RdNd	PROC	NEAR
	MOV	AL,[Key2ndPart]
	OR	AL,AL
	JNZ	RDexit
	mov	ah,1
	int	16h
	JZ	CONBUS
	OR	AX,AX
	JNZ	NOTBRK			;CHECK FOR NULL AFTER BREAK
	MOV	AH,0
	INT	16H			;READ THE NULL
	JMP	CON$RDND		;AND GET A REAL STATUS
NOTBRK: CMP	AX,7200H		;CHECK FOR CTRL-PRTSC
	JNZ	RDexit
	MOV	AL,16
RDexit:
	MOV	[BX].MEDIA,AL
EXVEC:	JMP	StatusComplete
CONBUS: JMP	StatusDevReady
Con$RdNd	ENDP

;--------------------------------------------------------------
;
;	KEYBOARD FLUSH ROUTINE
;
Con$Flsh	PROC	NEAR
	MOV	[Key2ndPart],0		       ;Clear out holding buffer

	CALL	Flush			; Flush the keyboard
	JMP	EXVEC
Con$Flsh	ENDP


Flush	PROC	NEAR
	PUSH	DS
	MOV	AX,RomData
	MOV	DS,AX
    ASSUME	DS:RomData

	CLI				; ** Disable interrupts
	MOV	AX,offset RomData:KeyBuffer  ; Start of Rom buffer
	MOV	BufferHead,AX
	MOV	BufferTail,AX		; Empty the queue
	STI				; ** enable interrupts
	POP	DS
    ASSUME	DS:NOTHING

	RET
Flush	ENDP

SUBTTL	Console output( video ) routines
	PAGE

;----------------------------------------------------------
;
;	CONSOLE WRITE ROUTINE
;
CON$WRIT:
	JCXZ	EXVEC
CON$LP: MOV	AL,ES:[DI]	;GET CHAR
	INC	DI
	PUSH	CX
	PUSH	DI
	INT	CHROUT		;OUTPUT CHAR
	POP	DI
	POP	CX
	LOOP	CON$LP		;REPEAT UNTIL ALL THROUGH
	JMP	EXVEC

SUBTTL	Keyboard interrupt routine
PAGE
; Replacement for ROM keyboard interrupt, tacks on the front.
; OldKeyInterrupt is set to original contents of INT 09H.
; The input character is passed to the O.S. console input filter
; to determine if any special action should be taken.  The filter
; return value indicates if the character should be saved in the
; type ahead buffer or if it should be discarded.  A keyboard
; semaphore exists to indicate if a process is waiting for input.
; If the keboard semaphore is set all of the processes sleeping on
; it are woken up.

OldKeyInterrupt DD	?
KeySem		db	0		; non-zero if someone waiting on input

KeyboardInterrupt	PROC FAR
	INT	32H			; Save regs
	MOV	AX,RomData
	MOV	DS,AX
    ASSUME	DS:RomData

	PUSHF				; Save flags to simulate INT
	CALL	CS:OldKeyInterrupt	; Now do ROM code
; Now tell scheduler keyboard had char
	cli				; interrupts off!
	mov	bx,BufferTail		; Get tail of queue
	cmp	bx,BufferHead		; Anything in keyboard queue?
	JE	NoKey			; No, don't requeue then
	dec	bx
	dec	bx
	cmp	bx,offset RomData:KeyBuffer
	jae	kbi1			; no rap around in buffer
	mov	bx,offset RomData:KeyBuffer+KeyBufLen
kbi1:
	mov	ax,[bx]			; get last queued char.
	mov	dx,5			; ConsInputFilter subfunction
	call	DosFunction
	jnz	kbi2			; key should remain in buffer
	mov	BufferTail,bx		; discard key from buffer
	jmp	SHORT NoKey
kbi2:
	cli
	CMP	KeySem,0		; Outstanding request?
	JE	NoKey			; No, may not be inited either
	push	ax
	push	bx
	push	cx
	push	dx
	mov	ax,cs
	mov	bx,OFFSET KeySem
	mov	cs:byte ptr [bx],0	; reset keyboard semaphore
	mov	dx,10			;; ProcRun
	call	[DosFunction]		; awaken anyone waiting on input
	pop	dx
	pop	cx
	pop	bx
	pop	ax
NoKey:
	IRET
KeyBoardInterrupt	ENDP

;-------------------------------------------------------------
; Keyboard INT 16 intercept routine to allow console input to sleep.
; Only console input function 1 is intercepted, all other functions
; are allowed to go directly to the ROM BIOS.  For the function 1
; the input status is checked, if a character is ready the function
; is allowed to go to the ROM BIOS.  Otherwise the keyboard semaphore
; is set and the process is put to sleep on the address of the
; semaphore.  When a key is typed the keyboard interrupt routine
; will wakeup any processes sleeping on this semaphore.
;
;	WARNING: The following routines can be entered recursively
;		 due to the fact that the ROM BIOS routines called
;		 reenable interrupts.  It's not usually a problem
;		 since interrupts will generally be processed faster
;		 than anyone can type.

OldKbdHandler	dd	?

;-------------------------------------------------------------

KeyBoardHandler proc	far
	or	ah,ah
	je	DoLocalRead
	cmp	ah,1
	je	DoLocalStat
OldKBint:
	jmp	[OldKbdHandler]

DoLocalStat:
	push	bx
	push	ds
	lds	bx,ScrnIOok
	test	byte ptr [bx],0FFh
	pop	ds
	pop	bx
	jnz	OldKBint
	xor	ax,ax
	ret	2

DoLocalRead:
	push	ax
	push	bx
	push	cx
	push	dx
DoLocalRd1:
	push	ds
	lds	bx,ScrnIOok
	mov	ax,ds
	test	byte ptr [bx],0FFh
	pop	ds
	jnz	DoLocalRd2
	xor	cx,cx
	mov	dx,9			;; ProcBlock
	call	[DosFunction]		; sleep until a char is typed
	jmp	DoLocalRd1

DoLocalRd2:
	mov	ah,1			; get console status
	pushf				; simulate INT to old handler
	cli
	call	[OldKbdHandler]
	cli				; subfunction 1 unconditionally sets IF
	jnz	LocalRead		; go read character
	mov	ax,cs
	mov	bx,OFFSET KeySem
	mov	cs:byte ptr [bx],0FFh	; set keyboard semaphore
	xor	cx,cx
	mov	dx,9			;; ProcBlock
	call	[DosFunction]		; sleep until a char is typed
	jmp	DoLocalRd1

LocalRead:
	pop	dx
	pop	cx
	pop	bx
	pop	ax
	jmp	[OldKbdHandler]		; read the character and return

KeyBoardHandler endp

ENDIF	;CONSFLAG

SUBTTL	Aux driver
;------------------------------------------------------
;
;	A U X - AUXILARY DEVICE DRIVER
;
AUXDEV	LABEL	WORD			;HEADER FOR DEVICE "AUX"
	DW	PRNDEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	AUX0$IN
	DB	'AUX     '

	PUBLIC	COM1DEV
COM1DEV LABEL	WORD
	DW	LPT1DEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	AUX0$IN
	DB	'COM1    '

COM2DEV LABEL	WORD
	DW	-1,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	AUX1$IN
	DB	'COM2    '

AUXBUF	DB	0,0
;-------------------------------------------------------
;
;	READ FROM AUXILARY DEVICE
;
AUX$READ:
	JCXZ	EXVEC2
	CALL	GETBX
	XOR	AX,AX
	XCHG	AL,[BX]		;Get character and zero buffer
	OR	AL,AL
	JNZ	AUX2
AUX1:	CALL	AUXIN
AUX2:	STOSB			;STORE CHARACTER
	LOOP	AUX1
EXVEC2: JMP	StatusComplete

AUXIN:	MOV	AH,2		;INDICATES A READ
	CALL	AUXOP		;READ THE AUXILIARY PORT
	TEST	AH,0EH		;Check framing, parity, overrun
	JZ	AROK
	POP	AX		;Clean up the stack
	MOV	AL,0BH		;READ ERROR
	JMP	StatusPartialXfer
AROK:	RET
;--------------------------------------------------------
;
;	AUX NON-DESTRUCTIVE READ, NO WAITING
;
AUX$RDND:
	CALL	GETBX
	MOV	AL,[BX]		;GET KEY AND ZERO BUFFER
	OR	AL,AL
	JNZ	AUXRDX		;KEY IN BUFFER?
	CALL	AUXSTAT
	TEST	AH,00000001B		;TEST DATA READY
	JZ	AUXBUS
	TEST	AL,00100000B		;TEST DATA SET READY
	JZ	AUXBUS
	CALL	AUXIN
	CALL	GETBX
	MOV	[BX],AL	    ;GET AND SAVE KEY
AUXRDX: JMP	StatusComplete
AUXBUS: JMP	StatusDevReady
;----------------------------------------------------------
;
;	AUX OUTPUT STATUS
;
AUX$WRST:
	CALL	AUXSTAT
	TEST	AL,00100000B	;TEST DATA SET READY
	JZ	AUXBUS
	TEST	AH,00100000B	;TEST CLEAR TO SEND
	JZ	AUXBUS
	JMP	EXVEC2

AUXSTAT:
	MOV	AH,3
AUXOP:	CALL	GETDX
	INT	14H
	RET
;---------------------------------------------------------
;
;	FLUSH AUX INPUT BUFFER
;
AUX$FLSH:
	CALL	GETBX
	MOV	BYTE PTR [BX],0
	JMP	EXVEC2
;---------------------------------------------------------
;
;	WRITE TO AUXILARY DEVICE
;
AUX$WRIT:
	JCXZ	EXVEC2
AUX$LOOP:
	MOV	AL,ES:[DI]	;GET CHAR
	INC	DI		;POINT TO NEXT ONE
	MOV	AH,1		;INDICATES A WRITE
	CALL	AUXOP		;SEND CHARACTER OVER AUX PORT
	TEST	AH,80H		;CHECK FOR ERROR
	JZ	AWOK
	MOV	AL,10		;INDICATE WRITE FAULT
	JMP	StatusPartialXfer
AWOK:	LOOP	AUX$LOOP
	JMP	EXVEC2

GETBX:	CALL	GETDX
	MOV	BX,DX
	ADD	BX,OFFSET AUXBUF
	RET
SUBTTL	Printer driver
;-------------------------------------------------------------
;
;	P R N - PRINTER DEVICE
;
PRNDEV	LABEL	WORD			;HEADER FOR DEVICE "PRN"
	DW	TIMDEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	PRN0$IN
	DB	'PRN     '

LPT1DEV LABEL	WORD
	DW	LPT2DEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	PRN0$IN
	DB	'LPT1    '

LPT2DEV LABEL	WORD
	DW	LPT3DEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	PRN1$IN
	DB	'LPT2    '

LPT3DEV LABEL	WORD
	DW	COM2DEV,BIOSEG
	DW	1000000000000000B
	DW	STRATEGY
	DW	PRN2$IN
	DB	'LPT3    '

ERRFLG	DB	0

;----------------------------------------------------------
;
;	WRITE TO PRINTER DEVICE
;
PRN$WRIT:
	JCXZ	EXVEC3
PRN$LOOP:
	MOV	AL,ES:[DI]	;GET CHAR INTO AL
	INC	DI		;POINT TO NEXT CHAR
	MOV	[ERRFLG],0	;INITIALIZE RETRY FLAG

PRETRY: XOR	AH,AH		;AH=0
	CALL	PRNOP		;TO INDICATE PRINT CHAR IN AL

ERRCHK: JZ	PROK
	XOR	[ERRFLG],1	      ;DO 1 AUTOMATIC RETRY
	JNZ	PRETRY
PMESSG: JMP	StatusPartialXfer	  ;RETURN WITH THE ERROR

PROK:	LOOP	PRN$LOOP
EXVEC3: JMP	StatusComplete
;--------------------------------------------------------
;
;	PRINTER STATUS ROUTINE
;
PRN$STAT:
	CALL	PRNSTAT		;DEVICE IN DX
	JNZ	PMESSG
	TEST	AH,10000000B
	JNZ	EXVEC3
	JMP	StatusDevReady

PRNSTAT:
	MOV	AH,2
PRNOP:	CALL	GETDX
	INT	17H

	MOV	AL,2
	TEST	AH,0001B	;TEST FOR NOT READY
	JNZ	PRNOP2

	MOV	AL,10		;WRITE FAULT CODE
	TEST	AH,1000B	;TEST FOR I/O ERROR
	JZ	PRNOP2

	TEST	AH,00100000B	;OUT-OF-PAPER?
	JZ	PRNOP1
	MOV	AL,9		;OUT OF PAPER CODE
PRNOP1: OR	AL,AL		;SET NZ FLAG
PRNOP2: RET

SUBTTL Timer (clock) driver
	PAGE

;**	Time Functions
;
;	Uses clock with 1000 ticks per second. User sees only
;	time in hours, minutes, seconds, and 1/100 second, in registers
;	CH, CL, DH, DL respectively. (Each is a binary number.)
;	      Modified for 4.0. The ROM bios timer routines are completely
;	replaced with code on this page. This provides a better time base
;	for the scheduler.


	EXTRN	Floppydevice:NEAR
TIMDEV	LABEL	WORD
	DW	Floppydevice,BIOSEG
	DW	1000000000001000B
	DW	STRATEGY
	DW	TIM$IN
	DB	'CLOCK$  '

DAYCNT	DW	0

;--------------------------------------------------------------------
;
; Settime sets the current time
;
; On entry ES:[DI] has the current time:
;
;	number of days since 1-1-80	(WORD)
;	minutes (0-59)			(BYTE)
;	hours (0-23)			(BYTE)
;	hundredths of seconds (0-99)	(BYTE)
;	seconds (0-59)			(BYTE)
;
; Each number has been checked for the correct range.

TIM$WRIT:
	PUSH	BX
	MOV	AX,ES:[DI]
	MOV	DAYCNT,AX
	MOV	CX,ES:[DI+2]
	MOV	AL,60
	MUL	CH		;Hours to minutes
	MOV	CH,0
	ADD	AX,CX		;Total minutes
	MOV	CX,60000	;60*1000
	MUL	CX		;Convert to milliseconds
	MOV	SI,AX
	MOV	BX,DX		; Save hours, min in Msecs in BX:SI
	MOV	AL,ES:[DI+5]	; Get # seconds
	MOV	CX,100
	MUL	CL		; Get seconds in 1/100s
	MOV	CL,ES:[DI+4]	; Hundredths of second
	ADD	AX,CX		; Now have seconds and 1/100ths
	MOV	CL,10
	MUL	CX		; Get DX:AX = milliseconds
	ADD	AX,SI
	MOV	CS:TickLow,AX
	ADC	BX,0
	MOV	CS:TickHigh,BX	; Set time in milliseconds
	POP	BX
	JMP	StatusComplete


;***************************************
;
; Gettime reads date and time
; and returns the following information:
;
;	ES:[DI]	 =count of days since 1-1-80
;	ES:[DI+2]=minutes
;	ES:[DI+3]=hours
;	ES:[DI+4]=hundredths of seconds
;	ES:[DI+5]=seconds
;
;***************************************
	PUBLIC	TIM$READ
TIM$READ:
	PUSH	BX
	MOV	AX,DayCnt
	STOSW			; return day
	MOV	DX,CS:TickHigh
	MOV	AX,CS:TickLow	; Get current time
	MOV	CX,60000	; # milliseconds in a minute
	DIV	CX		; Get AX= minutes, DX = seconds&msecs
	MOV	BX,DX		; Save seconds and fraction
	XOR	DX,DX
	MOV	CX,60		; # minutes/hour
	DIV	CX		; Get AX= hour, DX= minute
	xchg	ax,dx
	STOSB			; Return minutes
	xchg	ax,dx
	STOSB			; Return hours
	MOV	AX,BX
	XOR	DX,DX
	MOV	CX,1000
	DIV	CX		; Get AX= seconds, DX= milliseconds
	xchg	ax,bx		; (bx) = seconds
	MOV	AX,DX
	XOR	DX,DX
	MOV	CX,10
	DIV	CX		; Get hundredths of second
	STOSB			; Return hundredths of second
	xchg	ax,bx
	STOSB			; Return second
	POP	BX
	JMP	StatusComplete

SUBTTL	Clock interrupt and replacement for ROM code
PAGE

COMMENT *
   The clock code in the ROM is replaced with the following code which
provides the clock interface to the scheduler and a 1 ms time base. The
1AH interrupt is simulated to return the approximate time for those
routines which still call 1AH. The time period for motor start up is
preserved since that code has not yet been rewritten.
	*

RomData SEGMENT AT 040H
	ORG	03FH
Motor_Status	DB	1 DUP(?)
Motor_Count	DB	1 DUP(?)
RomData ENDS

TickLow		DW	?		; Low part of time in Msec
TickHigh	DW	?		; High part of time in msec
MotorFlag	DB	0		; There is an active motor
MotorTick	DB	MOTORCOUNT	; # ticks until 1/18.2 secs
SchedTick	DB	SCHEDCOUNT	; Scheduler countdown

; Interrupt 8H -	timer interrupt

    ASSUME	DS:NOTHING
TimerInterrupt	PROC	FAR
	PUSH	AX
	INC	TickLow
	JNZ	NoOverflow
	INC	TickHigh
NoOverFlow:
	CMP	TickHigh,1318		; Close to 24 hours?
	JNZ	NotDay			; No
	CMP	TickLow,23552		; Reach 24 hours?
	JNZ	NotDay			; No
; Have reached a day, bump day count
	INC	DayCnt
	XOR	AX,AX
	MOV	TickHigh,AX
	MOV	TickLow,AX		; Reset time of day
NotDay:
;;	DEC	MotorTick		; Time to decrement motor?
;;	JNZ	CheckSched			; No
;;	MOV	MotorTick,MOTORCOUNT	; Reset 1/18 sec worth of msecs
;;	PUSH	DS
;;	MOV	AX,RomData
;;	MOV	DS,AX
;;    ASSUME	DS:RomData

;;	TEST	MotorFlag,0FFH		; Active motor timer?
;;	JZ	CheckMotor		; No, see if new one
;;	DEC	Motor_Count
;;	JNZ	CheckMotor		; Not time to shut down
;;	AND	Motor_Status,0F0H	; Turn off motor running bits
;;	MOV	AL,0CH
;;	PUSH	DX
;;	MOV	DX,03F2H
;;	OUT	DX,AL			; Turn off the motors
;;	POP	DX
;;	MOV	MotorFlag,0		; No motor timeout running
;;	POP	DS
;;	JMP	CheckSched		; Go see if time to sched

;;CheckMotor:
;;	MOV	AL,Motor_Count
;;	POP	DS
;;	JNZ	CheckSched		; Motor already active
;;	OR	AL,AL			; Need to start countdown?
;;	JZ	CheckSched		; No
;;	MOV	MotorFlag,0FFH		; Yes, set flag
;;	MOV	MotorTick,MOTORCOUNT	; 1/18.2 sec later

; The scheduler is called every 10ms, rather than every 1 ms to see if
; it should switch tasks.
CheckSched:
; First reset the timer so will interrupt again
	MOV	AL,36H
	OUT	43H,AL
	MOV	AL,169
	OUT	40H,AL
	MOV	AL,4
	OUT	40H,AL			; Set 1 msec delay
	MOV	AL,20H
	OUT	20H,AL			; Tell 8259 interrupt done
; Now see if should call scheduler
	DEC	SchedTick		; Time to call scheduler?
	JNZ	NoSched			; No, all done
	MOV	SchedTick,SCHEDCOUNT	; Reset countdown
csch1:	POP	AX			; Stack is now clean
	INT	32H			; Save all regs
	CMP	WORD PTR (DosFunction+2),0	; Dos installed?
	JE	NoSchedActive		; Not yet, don't call sched
	sub	dx,dx			; (dx) = 0 = tic subfunction
	mov	ax,SCHEDCOUNT		; 10 milliseconds per tic
	CALL	DosFunction		; Do the tick
    EXTRN	FloppyTimer:FAR
	CALL	FloppyTimer		; timer service for floppy disk also
NoSchedActive:
	IRET				; All done

NoSched:
	POP	AX
INTRET: IRET				; All done
TimerInterrupt	ENDP

; INT 1AH -	Get/Set time based in 55msec tick

TimeOfDay	PROC	FAR
	OR	AH,AH			; Function 0: Read time?
	JZ	ReadOld			; Yes
	DEC	AH			; Function 1: Write time?
	JZ	WriteOld		; Yes
	IRET				; No, bad function code

; Read old( 1/18.2 second tick ) time
ReadOld:
	MOV	AX,TickHigh
	xor	dx,dx
	MOV	CX,MOTORCOUNT		; # milliseconds in 1/18.2 secs
	DIV	CX			; Get value in old ticks
	push	ax
	MOV	AX,TickLow
	div	cx
	pop	cx			; get high order count
	XOR	AL,AL			; Never have oveflow
	IRET				; Return the time

; Set time using old resolution
WriteOld:
	MOV	AX,DX
	push	bx
	MOV	BX,MOTORCOUNT		; Conversion factor
	MUL	BX
	MOV	TickLow,AX		; Set low part of time
	mov	ax,cx
	mov	cx,dx
	MUL	BX
	pop	bx
	ADD	AX,CX			; Combine high parts
	MOV	TickHigh,AX		; Set time
	IRET				; Return with new time set
TimeOfDay	ENDP

;++++
memsizmsg	db	13,10,'Error - Interrupt 12',13,10,0
memsizint	dd	0

memsizhandler	proc	far
	push	ax
	push	bx
	push	si
	push	ds
	push	cs
	pop	ds
	mov	SI,offset CS:memsizmsg
	call	WRMSG
	pop	ds
	pop	si
	pop	bx
	pop	ax
	jmp	memsizint
memsizhandler	endp
;++++

;----------------------------------------------
;	WRITE OUT MESSAGE POINTED TO BY [SI]
;
WRMSG:	LODSB			;GET THE NEXT CHARACTER OF THE MESSAGE
	AND	AL,7FH		;SEE IF END OF MESSAGE
	JZ	WRMRET
	MOV	BX,7
	MOV	AH,14		;WRITE CHARACTER
	INT	10H		;SEND THE CHARACTER
	JMP	SHORT WRMSG
WRMRET: RET

.xlist
	INCLUDE BUGCODE.INC
.list

Code	ENDS

SUBTTL	Diskette driver -- now used only for bootup
;-----------------------------------------------------------------
;
;	DISK INTERFACE ROUTINES
;
BiosInit	SEGMENT PARA PUBLIC	'CODE'

DRVMAX	DB	  4

    ASSUME	CS:BiosSeg

MAXERR	=	5
LSTDRV	=	504H

WRTVERIFY LABEL WORD
RFLAG	DB	2	;2 for read, 3 for write
VERIFY	DB	0	;1 if verify after write
SINGLE	DB	0	;1 if single drive system
SWPFLG	DB	0	;1 if BIOS swapped out
SECCNT	DW	0
HARDNUM DB	99	;logical drive number of first hardfile

RESSEC	=	3
DRVLIM	=	8	;Number of sectors on device
SECLIM	=	13	;MAXIMUM SECTOR
HDLIM	=	15	;MAXIMUM HEAD
HIDSEC	=	17	;NUMBER OF HIDDEN SECTORS

;WARNING - preserve order of drive and curhd! -c.p.

DRIVE	DB	0	;PHYSICAL DRIVE CODE
CURHD	DB	0	;CURRENT HEAD
CURSEC	DB	0	;CURRENT SECTOR
CURTRK	DW	0	;CURRENT TRACK


ERRIN:	;DISK ERRORS RETURNED FROM THE IBM ROM
	DB	80H		;NO RESPONSE
	DB	40H		;Seek failure
	DB	10H		;BAD CRC
	DB	8		;DMA OVERRUN
	DB	4		;SECTOR NOT FOUND
	DB	3		;WRITE ATTEMPT TO WRITE-PROTECT DISK
LSTERR	DB	0		;ALL OTHER ERRORS


ERROUT: ;RETURNED ERROR CODES CORRESPONDING TO ABOVE
	DB	2		;NO RESPONSE
	DB	6		;SEEK FAILURE
	DB	4		;BAD CRC
	DB	4		;DMA OVERRUN
	DB	8		;SECTOR NOT FOUND
	DB	0		;WRITE ATTEMPT ON WRITE-PROTECT DISK
	DB	12		;GENERAL ERROR

NUMERR= ERROUT-ERRIN
;---------------------------------------------------------------------
SPSAV	DW	0	;SAVE THE STACK POINTER
;

GETBP:	PUSH	AX
	PUSH	CX
	PUSH	DX
	PUSH	BX
	MOV	CL,AH		;SAVE MEDIA
	AND	CL,0F8H		;NORMALIZE
	CMP	CL,0F8H		;COMPARE WITH GOOD MEDIA BYTE
	JZ	GOODID
	MOV	AH,0FEH		;DEFAULT TO 8-SECTOR, SINGLE-SIDED
GOODID: MOV	DI,OFFSET CS:HDRIVE
	CMP	AL,[HARDNUM]
	JZ	GETRET
	JB	GETBP1
	MOV	DI,OFFSET CS:DRIVEX
	JMP	SHORT GETRET

GETBP1: MOV	AL,1		;SET NUMBER OF FAT SECTORS
	MOV	BX,64*256+8	;SET DIR ENTRIES AND SECTOR MAX
	MOV	CX,40*8		;SET SIZE OF DRIVE
	MOV	DX,01*256+1	;SET HEAD LIMIT AND SEC/ALL UNIT
	MOV	DI,OFFSET CS:FDRIVE
	TEST	AH,00000010B	;TEST FOR 8 OR 9 SECTOR
	JNZ	HAS8		;NZ = HAS 8 SECTORS
	INC	AL		;INC NUMBER OF FAT SECTORS
	INC	BL		;INC SECTOR MAX
	ADD	CX,40		;INCREASE SIZE
HAS8:	TEST	AH,00000001B	;TEST FOR 1 OR 2 HEADS
	JZ	HAS1		;Z = 1 HEAD
	ADD	CX,CX		;DOUBLE SIZE OF DISK
	MOV	BH,112		;INCREASE NUMBER OF DIRECTORY ENTRIES
	INC	DH		;INC SEC/ALL UNIT
	INC	DL		;INC HEAD LIMIT
HAS1:	MOV	CS:[DI].2,DH
	MOV	CS:[DI].6,BH
	MOV	CS:[DI].8,CX
	MOV	CS:[DI].10,AH
	MOV	CS:[DI].11,AL
	MOV	CS:[DI].13,BL
	MOV	CS:[DI].15,DL
GETRET: POP	BX
RET88:	POP	DX
	POP	CX
	POP	AX
	RET

;*********************************************************************
;	"FDRIVE" IS A FLOPPY DISK, VARIOUS PARAMETERS ARE PATCHED
;	BY GETBP TO REFLECT THE TYPE OF MEDIA INSERTED
FDRIVE:
	DW	512		;Physical sector size in bytes
	DB	1		;Sectors/allocation unit
	DW	1		;Reserved sectors for DOS
	DB	2		;No. allocation tables
	DW	64		;Number directory entries
	DW	9*40		;Number sectors (at 512 bytes ea.)
	DB	11111100B	;Media descriptor
	DW	2		;Number of FAT sectors
	DW	9		;Sector limit
	DW	1		;Head limit
	DW	0		;Hidden sector count

;------------------------------------------------------------
;
;	DISK I/O HANDLER
;
;	AL = DRIVE NUMBER (0-3)
;	AH = MEDIA DESCRIPTOR
;	CX = SECTOR COUNT
;	DX = FIRST SECTOR
;	ES:DI = TRANSFER ADDRESS
;	[RFLAG]=OPERATION (2=READ, 3=WRITE)
;	[VERIFY]=1 FOR VERIFY AFTER WRITE
;
;	IF SUCCESSFUL CARRY FLAG = 0
;	  ELSE CF=1 AND AL CONTAINS ERROR CODE
;
DISKRD:
	debug	1,4,<DISKRD: $x $x $x $x:$x\n>,<ax,cx,dx,es,di>
	MOV	[RFLAG],2
DISKIO:
	CLC
	JCXZ	IORET
	MOV	[TIM_DRV],AL		;SAVE DRIVE LETTER

	MOV	[SPSAV],SP		;SAVE SP
	XCHG	BX,DI			;ES:BX = TRANSFER ADDRESS
	CALL	GETBP			;CS:DI = PTR TO B.P.B
	MOV	SI,DX
	ADD	SI,CX
	ADD	DX,CS:[DI].HIDSEC	;ADD IN THE HIDDEN SECTORS
	CMP	SI,CS:[DI].DRVLIM	;COMPARE AGAINST DRIVE MAX
	JBE	INRANGE
	MOV	AL,8
STCRET: STC
IORET:	RET

INRANGE:CMP	AL,[HARDNUM]
	JB	NOTHARD
	MOV	AL,CS:[HARDDRV] ;SET DRIVE NUMBER OF HARDFILE
	JZ	RDWR
	INC	AL
	JMP	SHORT RDWR

NOTHARD:CMP	[SINGLE],1	;SINGLE FLOPPY INSTALLED?
	JNZ	RDWR
	CALL	SWPDSK		;ASK USER FOR CORRECT DISK
RDWR:
	MOV	[DRIVE],AL
	MOV	[SECCNT],CX	;SAVE SECTOR COUNT
	XCHG	AX,DX		;SETUP LOGICAL SECTOR FOR DIVIDE
	XOR	DX,DX
	DIV	WORD PTR CS:[DI].SECLIM ;DIVIDE BY SEC PER TRACK
	INC	DL
	MOV	[CURSEC],DL		  ;SAVE CURRENT SECTOR
	MOV	CX,CS:[DI].HDLIM	;GET NUMBER OF HEADS
	XOR	DX,DX		;DIVIDE TRACKS BY HEADS PER CYLINDER
	DIV	CX
	MOV	[CURHD],DL	;SAVE CURRENT HEAD
	MOV	[CURTRK],AX	;SAVE CURRENT TRACK

	debug	1,4,< Drv $b  Hd $b  Trk $x  Sec $b\n>,<<word ptr DRIVE>,<word ptr CURHD>,CURTRK,<word ptr CURSEC>>
	MOV	AX,[SECCNT]
	MOV	SI,ES		;Check for 64k boundary error
	SHL	SI,1
	SHL	SI,1
	SHL	SI,1
	SHL	SI,1		;Segment converted to absolute address
	ADD	SI,BX		;Combine with offset
	ADD	SI,511		;Add sector size and see if overflow
	JC	BUFIO		;Must handle special if so

	XCHG	BX,SI
	SHR	BH,1
	MOV	AH,128		;Max. sectors in 64K
	SUB	AH,BH		;Number of sectors left in this 64K
	XCHG	BX,SI

	CMP	AH,AL		;Does it exceed total request?
	JBE	FIRBLK
	MOV	AH,AL		;If so, limit transfer to the request
FIRBLK:
;At this point, AL=total number of sectors to be read, AH=number
;  of sectors that can be read before 64K boundary error. AH<=AL.
;    ES:BX points to load area, DS:DI points to B.P.B

	PUSH	AX
	MOV	AL,AH		;No. of sectors to read at once (<>0)
	CALL	BLOCK		;Transfer portion before boundary
	POP	AX
	SUB	AL,AH
	JZ	DONE
BUFIO:
	PUSH	AX
	PUSH	ES
	PUSH	BX		;SAVE CURRENT TRANSFER ADDRESS
	CALL	SWAP		;SWAP BUFFER WITH BIOS CODE

	ADD	BH,2		;POINT TO TEMP BUFFER
	CALL	DISK1		;Perform disk operation

	POP	BX		;RECALL TRANSFER ADDRESS
	POP	ES
	POP	AX
	CALL	SWAP		;SWAP BACK

	DEC	AL		;Dec sectors to read by 1
	ADD	BH,2		;Add 200H to transfer address
	CALL	BLOCK		;Read the remaining sectors
DONE:
	debug	1,4,< DISKIO DONE\n>
	CLC			;No error
	RET			;From subroutine DISKIO

;	Swap 512 bytes of BIOS with transfer address

SWAP:	PUSH	DI
	PUSH	BX
	PUSH	AX
	MOV	DI,BX		;ES:DI POINTS TO USER BUFFER
	ADD	BH,2
	MOV	SI,BX		;ES:SI POINTS TO TEMP BUFFER
	CLD
	debug	1,4,< SWAP $x:$x <==> $x:$x\n>,<es,di,es,si>
	MOV	CX,256			;256 WORDS TO SWAP
SWAP1:	MOV	BX,WORD PTR ES:[DI]	;GET USER WORD
	MOV	AX,WORD PTR ES:[SI]	;GET SCRATCH WORD
	MOV	WORD PTR ES:[SI],BX	;PUT SCRATCH WORD
	STOSW				;PUT BIOS WORD
	INC	SI
	INC	SI
	LOOP	SWAP1
	XOR	[SWPFLG],1		;TOGGLE SWAPPED FLAG
	POP	AX
	POP	BX
	POP	DI
RETZ:	RET

;Read the number of sectors specified in AL, handling track boundaries

BLOCK:	OR	AL,AL		;See if any sectors to read
	JZ	RETZ

	MOV	AH,CS:[DI].SECLIM	;Sectors per track
	INC	AH
	SUB	AH,[CURSEC]	;Number of sectors left on this track
	CMP	AH,AL		;Compare with no. of sectors needed
	JBE	DOIO
	MOV	AH,AL		;Limit to only those requested
DOIO:
	PUSH	AX
	MOV	AL,AH		;Put count where ROM needs it
	CALL	DISK		;Call ROM and trap errors
	POP	AX
	SUB	AL,AH		;Reduce request by amount just done
	SHL	AH,1		;AH * 2^8 = no. of bytes
	ADD	BH,AH		;Bump address pointer
	JMP	BLOCK

;Perform disk I/O with retries
; AL = number of sectors (1-8, all on one track)
; ES:BX = Transfer address (must not cross a 64K physical boundary)
; [RFLAG] = 2 if read, 3 if write
; [VERIFY] = 0 for normal, 1 for verify after write

DISK1:	MOV	AL,1		;Only one sector for buffered I/O

DISK:	MOV	SI,MAXERR
	MOV	AH,RFLAG	;Get read/write indicator

RETRY:	PUSH	AX

CALLROM:MOV	DX,[CURTRK]	;Load current cylinder
	MOV	CL,6		;move high bits of cyl to sec
	SHL	DH,CL
	OR	DH,[CURSEC]
	MOV	CX,DX
	XCHG	CH,CL			 ;CL = sector, CH = cylinder
	MOV	DX,WORD PTR [DRIVE]	 ;Load physical drive number
					 ;and current head number
;;	debug	1,4,< CALLROM $x $x $x $x  $x\n>,<ax,bx,cx,dx,es>
	INT	13H			 ;Request disk read/write
	JC	DSKERR
	POP	AX		;Restore sector count
	PUSH	AX
	CMP	WRTVERIFY,103H	;Check for write and verify
	JNZ	NOVERIFY
	MOV	AH,4		;Request verify
	INT	13H
	JC	DSKERR
NOVERIFY:
;;	debug	1,4,< back from ROM\n>,<>
	POP	AX
	AND	CL,03FH		;Eliminate cylinder bits from sector
	XOR	AH,AH
	SUB	[SECCNT],AX	;Reduce count of sectors to go
	ADD	CL,AL		;Next sector
	MOV	[CURSEC],CL
	CMP	CL,CS:[DI].SECLIM	; Reached limit?
	JBE	RET1

	MOV	[CURSEC],1	;Start with first sector of next track
	MOV	DH,[CURHD]
	INC	DH
	CMP	DH,CS:[DI].HDLIM
	JB	NOXOR
	XOR	DH,DH
	INC	[CURTRK]	;NEXT TRACK
NOXOR:	MOV	[CURHD],DH

RET1:	RET

DSKERR:
	debug	1,4,< DSKERR $x >,<ax>
	PUSH	AX		;Save error code
	MOV	AH,0		;Ask for disk reset
	INT	13H
	POP	AX		;Restore error code
	DEC	SI		;decrement retry count
	JZ	HARDERR
	CMP	AH,80H		;Timeout?
	JZ	HARDERR		;***
DSKERR1:POP	AX		;Restore sector count
	JMP	RETRY

HARDERR:
	CMP	[SWPFLG],0	;If BIOS swapped out
	JZ	HARD1
	POP	BX		;Get disk1 return address
	POP	BX		;Get low part of transfer address
	POP	ES		;Get high part of transfer address
	CALL	SWAP		;swap it back in
HARD1:	PUSH	CS
	POP	ES		;Make ES the local segment
	MOV	AL,AH		;Put error code in AL
	MOV	[LSTERR],AL	;Terminate list with error code
	MOV	CX,NUMERR	;Number of possible error conditions
	MOV	DI,OFFSET CS:ERRIN ;Point to error conditions
	REPNE	SCASB
	MOV	AL,es:NUMERR-1[DI] ;Get translation
	MOV	CX,SECCNT	;Get count of sectors to go
	MOV	SP,[SPSAV]	;Recover entry stack pointer
	STC			;Flag error condition
	RET			;and return

INITAB	DW	FDRIVE
	DW	FDRIVE
	DW	FDRIVE
	DW	FDRIVE
INITABH DW	HDRIVE
	DW	DRIVEX

RomData SEGMENT AT 040H
	ORG	03EH
Seek_Status	DB	1 DUP(?)
RomData ENDS


;-------------------------------------------------
;
;  ASK TO SWAP THE DISK IN DRIVE A:
;
SWPDSK: PUSH	DS
	XOR	SI,SI		;Select segment 0
	MOV	DS,SI
	MOV	AH,AL			;Make copy of drive number
	XCHG	AH,DS:LSTDRV		;Xchange with last drive used
	CMP	AL,AH		;See if same as last drive
	JZ	RDWR0
;Using a different drive in a one drive system so request the user change disks
	ADD	AL,"A"		;Add "A" to convert to drive letter
	MOV	CS:DRVLET,AL
	push	cs
	pop	ds
	MOV	SI,OFFSET CS:SNGMSG
	PUSH	BX
	CALL	WRMSG		;Print disk change message
	XOR	AH,AH
	INT	16H		;Wait for a keyboard character
	POP	BX
RDWR0:
	POP	DS
	XOR	AL,AL		;Always use drive 0
	RET


SNGMSG	DB	CR,LF,"Insert diskette for drive "
DRVLET	DB	"A: and strike",CR,LF,"any key when ready",CR,LF,LF,0
HNUM	DB	0		;NUMBER OF HARDFILES


;**	End of Permanently Resident BIOS


HARDDRV DB	80H	;Physical drive number of first hardfile
;**********************************************************************
;	"HDRIVE" IS A HARD DISK WITH 512 BYTE SECTORS
;*********************************************************************
HDRIVE:
	DW	512
	DB	1		;Sectors/allocation unit
	DW	1		;Reserved sectors for DOS
	DB	2		;No. of allocation tables
	DW	16		;Number of directory entries
	DW	0000		;Number of sectors (at 512 bytes each)
	DB	11111000B	;Media descriptor
	DW	1		;Number of FAT sectors
	DW	00		;Sector limit
	DW	00		;Head limit
	DW	00		;Hidden sector count
;**********************************************************************
;	"DRIVEX " IS AN EXTRA TYPE OF DRIVE USUALLY RESERVED FOR AN
;	ADDITIONAL HARD FILE
;*********************************************************************
DRIVEX:
	DW	512
	DB	00		;Sectors/allocation unit
	DW	1		;Reserved sectors for DOS
	DB	2		;No. of allocation tables
	DW	0000		;Number of directory entries
	DW	0000		;Number of sectors (at 512 bytes each)
	DB	11111000B	;Media descriptor
	DW	0000		;Number of FAT sectors
	DW	00		;Sector limit
	DW	00		;Head limit
	DW	00		;Hidden sector count

SUBTTL	Bios initialization
;*********************************************************
;	SYSTEM INITIALIZATION
;
;	THE ENTRY CONDITIONS ARE ESTABLISHED BY THE BOOTSTRAP
;	LOADER AND ARE CONSIDERED UNKNOWN. THE FOLLOWING JOBS
;	WILL BE PERFORMED BY THIS MODULE:
;
;	1.	ALL DEVICE INITIALIZATION IS PERFORMED
;	2.	A LOCAL STACK IS SET UP AND DS:SI ARE SET
;		TO POINT TO AN INITIALIZATION TABLE. THEN
;		AN INTER-SEGMENT CALL IS MADE TO THE FIRST
;		BYTE OF THE DOS
;	3.	ONCE THE DOS RETURNS FROM THIS CALL THE DS
;		REGISTER HAS BEEN SET UP TO POINT TO THE START
;		OF FREE MEMORY. THE INITIALIZATION WILL THEN
;		LOAD THE COMMAND PROGRAM INTO THIS AREA
;		BEGINNING AT 100 HEX AND TRANSFER CONTROL TO
;		THIS PROGRAM.
;
;********************************************************

DRVFAT	DW	0000		;DRIVE AND FAT ID OF DOS
BIOS$	DW	0000		;FIRST SECTOR OF DATA
DOSCNT	DW	0000		;HOW MANY SECTORS TO READ

BootBufr	EQU	17C0H	; High memory scratch area

    ASSUME	DS:NOTHING,ES:NOTHING

INIT:	mov	dx,1000h
	MOV	SS,DX
	MOV	SP,7C00h	;LOCAL STACK  1000:7C00
	STI
	PUSH	CX		;Save number of floppies
	MOV	[BIOS$],BX
	PUSH	AX		;Save Drive info
	MOV	AL,EOI
	OUT	AKPORT,AL	;TURN ON THE TIMER

	MOV	SI,OFFSET LPT3DEV
	CALL	PRINT_INIT		;INIT LPT3
	MOV	SI,OFFSET LPT2DEV
	CALL	PRINT_INIT		;INIT LPT2
	MOV	SI,OFFSET LPT1DEV
	CALL	PRINT_INIT		;INIT LPT1

	MOV	SI,OFFSET COM2DEV
	CALL	AUX_INIT		;INIT COM2
	MOV	SI,OFFSET COM1DEV
	CALL	AUX_INIT		;INIT COM1
;*	Can't do any DEBUG prints till now
	debug	1,1,<AUX and PRN devices initialized\n>,<>
	XOR	DX,DX
	MOV	DS,DX			;TO INITIALIZE PRINT SCREEN VECTOR
	MOV	ES,DX

	MOV	AX,CS					;FETCH SEGMENT

IF	CONSFLAG
	MOV	WORD PTR DS:BRKADR,OFFSET BREAK
	MOV	DS:BRKADR+2,AX			;VECTOR FOR BREAK
	MOV	WORD PTR DS:(CHROUT*4),OFFSET OUTCHR
	MOV	DS:(CHROUT*4+2),AX
ENDIF
	MOV	WORD PTR DS:DSKADR,SEC9		;DISK PARAMETERS
	MOV	DS:DSKADR+2,ES

	MOV	DI,4
	MOV	BX,OFFSET INTRET		; Rest just return
	XCHG	AX,BX
	STOSW						;Location 4
	XCHG	AX,BX
	STOSW				;INT 1		;Location 6
	ADD	DI,4
	XCHG	AX,BX
	STOSW						;Location 12
	XCHG	AX,BX
	STOSW				;INT 3		;Location 14
	XCHG	AX,BX
	STOSW						;Location 16
	XCHG	AX,BX
	STOSW				;INT 4		;Location 18
; Set up some vectors for scheduler and change rom interrupts
;
	CLI				; Disable, changing int vectors
	ADD	DI,3*4			; Move up to INT 8
	MOV	CX,OFFSET TimerInterrupt
	XCHG	AX,CX
	STOSW
	XCHG	AX,CX
	STOSW				; Set new INT 8: Timer
IF	CONSFLAG
	MOV	CX,DS:[DI]		; Save old addr to hook to
	MOV	WORD PTR OldKeyInterrupt,CX
	MOV	CX,DS:2[DI]
	MOV	WORD PTR (OldKeyInterrupt+2),CX
	MOV	CX,OFFSET KeyboardInterrupt
	XCHG	AX,CX
	STOSW
	XCHG	AX,CX
	STOSW				; Set new keyboard interrupt
ELSE
	ADD	DI,4
ENDIF
	XCHG	AX,BX
	STOSW
	XCHG	AX,BX			; INT 0A unused
	STOSW
	XCHG	AX,BX
	STOSW
	XCHG	AX,BX			; INT 0B unused
	STOSW
	add	di,4*6			; skip 0C - 11
	MOV	CX,DS:[DI]		; Save INT 12 addr to hook to
	MOV	WORD PTR memsizint,CX
	MOV	CX,DS:2[DI]
	MOV	WORD PTR (memsizint+2),CX
	mov	cx,offset memsizhandler
	xchg	ax,cx
	stosw
	xchg	ax,cx
	stosw
	add	di,4*2			; skip 13 - 14
	STI
; End of new 3.0 vectors
	XCHG	AX,BX
	STOSW				;INT 15		;Location 60
	XCHG	AX,BX
	STOSW						;Location 62
IF	CONSFLAG
	MOV	CX,DS:[DI]		; Save INT 16 addr to hook to
	MOV	WORD PTR OldKbdHandler,CX
	MOV	CX,DS:2[DI]
	MOV	WORD PTR (OldKbdHandler+2),CX
	MOV	CX,OFFSET KeyboardHandler
	XCHG	AX,CX
	STOSW
	XCHG	AX,CX			; Set new keyboard Handler
	STOSW
; Set new get/set time vector, time base changed
	ADD	DI,4*3			; skip 17 - 19
ELSE
	ADD	DI,4*4			; skip 16 - 19
ENDIF	;CONSFLAG
	MOV	CX,OFFSET TimeOfDay
	XCHG	AX,CX
	STOSW				; setup 1A to TimeofDay
	XCHG	AX,CX
	STOSW
	ADD	DI,4*23			; skip 1B - 31
	XCHG	AX,BX
	STOSW				; no-op INT 32 until Sched:SchedInit
	XCHG	AX,BX
	STOSW
	debug	1,1,<Interrupt vectors initialized\n>,<>

	MOV	DS:WORD PTR 500H,DX	;SET PRINT SCREEN & BREAK =0
	MOV	DS:WORD PTR LSTDRV,DX	;clean out last drive spec

	MOV	DI,SEC9			;location of drive table
	MOV	AX,02DFH		;Stuff the disk speedup/9 sector
	STOSW					;code
	MOV	AX,0225H
	STOSW
	MOV	AX,2A09H
	STOSW
	MOV	AX,50FFH
	STOSW
	MOV	AX,00F6H
	STOSW
	MOV	AL,2
	STOSB

	pushf				;simulate int 12h
	call	memsizint		;Get memory size--1K blocks in AX
	MOV	CL,6
	SHL	AX,CL			;Convert to 16-byte blocks(segment no.)
	POP	CX			;Recall drive info
	MOV	[DRVFAT],CX		;SAVE DRIVE TO LOAD DOS

	MOV	DX,SEG SYSINIT
	MOV	DS,DX

    ASSUME	DS:SEG SYSINIT

	MOV	MEMORY_SIZE,AX
	INC	CL
	MOV	DEFAULT_DRIVE,CL	 ;SAVE DEFAULT DRIVE SPEC

	add	dx,SYSIZE
	MOV	CURRENT_DOS_LOCATION,dx			; load address of DOS

	MOV	FINAL_DOS_LOCATION,SEG BiosInit
	debug	1,2,<DOS will load at $x, will move to $x\n>,<dx,FINAL_DOS_LOCATION>
	MOV	WORD PTR DEVICE_LIST,OFFSET CONDEV	  ;DS:SI = ptr to device list
	MOV	AX,CS
	MOV	WORD PTR DEVICE_LIST+2,AX
;**************************************************************
;	WILL INITIALIZE THE NUMBER OF DRIVES
;	AFTER THE EQUIPMENT CALL (INT 11H) BITS 6&7 WILL TELL
;	THE INDICATIONS ARE AS FOLLOWS:
;
;	BITS	7	6	DRIVES
;		0	0	1
;		0	1	2
;		1	0	3
;		1	1	4
;**************************************************************
	PUSH	CS
	PUSH	CS
	POP	DS
	POP	ES

    ASSUME	DS:BiosSeg,ES:BiosSeg

	INT	11H			;GET EQUIPMENT STATUS
	AND	AL,11000000B	;MASK DRIVE BITS
	JNZ	NOTSNGL		;Zero means single drive system
	INC	[SINGLE]	;REMEMBER THIS
NOTSNGL:
	POP	AX			;BOOT specifies number of floppies
	MOV	[HARDNUM],AL	;Remember which drive is hard disk
	MOV	[DRVMAX],AL	;And set initial number of drives
	MOV	AH,8
	MOV	DL,80H
	INT	13H		;Request number of hardfiles attached
	JC	ENDDRV		;Carry indicates old rom, so no hardfile
	MOV	[HNUM],DL
ENDDRV:
	MOV	DL,80H
	MOV	DI,OFFSET CS:HDRIVE
	CMP	[HNUM],0
	JLE	ITSOK1
	CALL	SETHRD		;SET UP FIRST HARDFILE

	MOV	DL,81H		;SET UP FOR NEXT CALL
	MOV	DI,OFFSET CS:DRIVEX
	JC	NOTOK
	CMP	[HNUM],2
	JZ	SETIT
	JMP	SHORT ITSOK

NOTOK:	MOV	[HARDDRV],DL
	MOV	DI,OFFSET CS:HDRIVE
	DEC	[HNUM]
	CMP	[HNUM],0
	JZ	ITSOK1
SETIT:	CALL	SETHRD		;SET UP SECOND HARDFILE
	JNC	ITSOK
	DEC	[HNUM]

; End of drive initialization

ITSOK:	MOV	AL,[HNUM]
	OR	AL,AL
	JZ	ITSOK1
	ADD	AL,[HARDNUM]
	MOV	[DRVMAX],AL
	MOV	AL,[HNUM]
	JMP	SHORT ITSOK2		;GO SET DESTINATION SEGMENT

ITSOK1: CMP	BYTE PTR [SINGLE],1
	JMP	SHORT GOINIT

    ASSUME	DS:BiosSeg

ITSOK2:
	DEC	AL

GOINIT:
	debug	1,1,<hardnum/hnum  $x  drvfat $x\n>,<<word ptr hnum>,drvfat>
	PUSH	CS
	POP	DS

    ASSUME	DS:BiosSeg,ES:NOTHING

	CALL	GETFAT		;READ IN THE FAT SECTOR
	XOR	DI,DI
	MOV	AL,ES:[DI]		;GET FAT ID BYTE
	MOV	BYTE PTR DRVFAT+1,AL	;SAVE FAT BYTE
	debug	1,2,< FAT ID: $b ds:$x\n>,<ax,ds>
	MOV	AX,[DRVFAT]
	CALL	GETBP			;GET DISK POINTER
	MOV	CL,[DI+2]		;GET SECTORS/CLUSTER
	MOV	AX,[DI].HIDSEC		;GET NUMBER OF HIDDEN SECTORS
	SUB	[BIOS$],AX		;SUBTRACT HIDDEN SECTOR OFFSET
	XOR	CH,CH			;CX = SECTORS/CLUSTER
	PUSH	DS
	XOR	DI,DI
	MOV	DS,DI
;
;	THE BOOT PROGRAM HAS LEFT THE DIRECTORY AT 0:500
;
	MOV	BX,DS:WORD PTR 53AH	;GET FIRST CLUSTER OF DOS
	POP	DS			;BX = FIRST CLUSTER OF DOS
LOADIT: MOV	AX,SEG SYSINIT
	add	ax,SYSIZE
	MOV	ES,AX			;ES:DI POINTS TO LOAD LOCATION
	CALL	GETCLUS			;READ IN A CLUSTER
	CMP	BX,0FFFH
	JNZ	LOADIT			;END OF FILE?

	EXTRN	Disk_Init:NEAR
	call	Disk_Init		; do some device driver initialization
	debug	1,2,<System loaded, going to sysinit\n>,<>
	JMP	SYSINIT

SUBTTL	Routines for reading in MSDOS

;
;	READ A FAT SECTOR INTO 17C0:0
;
GETFAT: debug	1,2,<GETFAT.>,<>
	XOR	DI,DI
	MOV	CX,1
	MOV	DX,CX
	MOV	AX,BootBufr
	MOV	ES,AX
	MOV	AL,BYTE PTR DRVFAT
	MOV	AH,0FCH
	JMP	DISKRD
;
;	READ A BOOT RECORD INTO 17C0:0
;
GETBOOT:debug	1,2,<GETBOOT.>,<>
	MOV	CX,1
	MOV	AX,0201H
	MOV	BX,BootBufr
	MOV	ES,BX
	XOR	BX,BX
	MOV	DH,BH
	INT	13H
	JC	SETRET
	CMP	WORD PTR ES:[1FEH],0AA55H
	JNZ	SETRET
	RET
;
;	SETUP VARIABLE SIZED HARDFILE
;	ON ENTRY DL=DRIVE NUMBER (80 OR 81)
;		 DI=PTR TO B.P.B
;
SETHRD: PUSH	DX
	MOV	AH,8		;GET DRIVE PARAMETERS
	INT	13H
	INC	DH
	MOV	[DI].HDLIM,DH
	POP	DX
	JC	SETRET
	AND	CL,3FH
	MOV	[DI].SECLIM,CL
	CALL	GETBOOT		;GET THE BOOT RECORD
	JC	SETRET
	MOV	BX,1C2H
SET1:	CMP	BYTE PTR ES:[BX],1
	JZ	SET2
	ADD	BX,16
	CMP	BX,202H
	JNZ	SET1
SETRET: STC			;NOT FOUND SO USE DEFAULTS
	RET

SET2:	MOV	AX,ES:[BX+4]
	MOV	DS:[DI].HIDSEC,AX	;SET HIDDEN SECTOR COUNT
	MOV	AX,ES:[BX+8]
	CMP	AX,64			;HAS TO BE AT LEAST 32K
	JB	SETRET
	MOV	DS:[DI].8,AX		;SAVE LOGICAL SECTOR COUNT
	MOV	CX,0100H		;SET CLUS SIZE AND SHIFT COUNT
	MOV	DX,64			;SET NUMBER OF DIR ENTRIES
	CMP	AX,512
	JBE	SET3

	ADD	CH,CH
	INC	CL
	MOV	DX,112
	CMP	AX,2048
	JBE	SET3

	ADD	CH,CH
	INC	CL
	MOV	DX,256
	CMP	AX,8192
	JBE	SET3

	ADD	CH,CH
	INC	CL
	ADD	DX,DX
	CMP	AX,32680	;NOT 32768! MAX NUMBER OF CLUSTERS=4085
	JBE	SET3

	ADD	CH,CH
	INC	CL
	ADD	DX,DX
SET3:
;
;	DX=NUMBER OF DIR ENTRIES, CH=NUMBER OF SECTORS PER CLUSTER
;				  CL=LOG BASE 2 OF CH
;
;	NOW CALCULATE SIZE OF FAT TABLE
;
	MOV	[DI].6,DX		;SAVE NUMBER OF DIR ENTRIES
	MOV	[DI].2,CH		;SAVE SECTORS PER CLUSTER
	XOR	BX,BX
	MOV	BL,CH
	DEC	BX
	ADD	BX,AX
	SHR	BX,CL			;DIVIDE BY SECTORS/CLUSTER
	INC	BX
	AND	BL,11111110B		;MAKE SURE COUNT IS EVEN
	MOV	SI,BX
	SHR	BX,1
	ADD	BX,SI			;MULTIPY BY 1.5
	ADD	BX,511
	SHR	BH,1
	MOV	[DI].11,BH		;SAVE NUMBER OF FAT SECTORS
	CLC
	RET
;
;  READ CLUSTER SPECIFIED IN BX
;  CX = SECTORS PER CLUSTER
;  DI = LOAD LOCATION
;
GETCLUS:debug	1,2,<GETCLUS bx $x cx $x es:di $x:$x  >,<bx,cx,es,di>
	PUSH	CX
	PUSH	DI
	MOV	[DOSCNT],CX	;SAVE NUMBER OF SECTORS TO READ
	MOV	AX,BX
	DEC	AX
	DEC	AX
	MUL	CX		;CONVERT TO LOGICAL SECTOR
	ADD	AX,[BIOS$]	;ADD IN FIRST DATA SECTOR
	MOV	DX,AX		;DX = FIRST SECTOR TO READ

GETCL1: CALL	UNPACK		;SI = BX, BX = NEXT ALLOCATION UNIT
	SUB	SI,BX
	CMP	SI,-1		;one apart?
	JNZ	GETCL2
	ADD	[DOSCNT],CX
	JMP	GETCL1

GETCL2: PUSH	BX
	MOV	AX,[DRVFAT]	;GET DRIVE AND FAT SPEC
	MOV	CX,[DOSCNT]
	CALL	DISKRD		;READ THE CLUSTERS
	POP	BX
	POP	DI
	MOV	AX,[DOSCNT]	;GET NUMBER OF SECTORS READ
	XCHG	AH,AL		;MULTIPLY BY 256
	SHL	AX,1		;TIMES 2 EQUAL 512
	ADD	DI,AX		;UPDATE LOAD LOCATION
	POP	CX		;RESTORE SECTORS/CLUSTER
	RET
;
;	GET THE FAT ENTRY AT BX, WHEN FINISHED SI=ENTRY BX
;
UNPACK: PUSH	DS
	PUSH	BX
	MOV	SI,BootBufr
	MOV	DS,SI
	MOV	SI,BX
	SHR	SI,1
	MOV	BX,[SI+BX]
	JNC	HAVCLUS
	SHR	BX,1
	SHR	BX,1
	SHR	BX,1
	SHR	BX,1
HAVCLUS:AND	BX,0FFFH
	POP	SI
	POP	DS
	RET
;
; SI POINTS TO DEVICE HEADER
;
PRINT_INIT:
	MOV	BH,1
	MOV	DL,17H
	JMP	SHORT DEV_INIT
AUX_INIT:
	MOV	BX,RSINIT
	MOV	DL,14H
DEV_INIT:
	MOV	CS:[INTNUM],DL
	MOV	AL,CS:[SI+13]	;GET DEVICE NUMBER FROM THE NAME
	SUB	AL,"1"
	CBW
	MOV	DX,AX
	MOV	AX,BX		;SET THE CALL
	DB	0CDH		;INT 17H
INTNUM	DB	17H
	RET

END$:

BiosInit	ENDS
	END