summaryrefslogtreecommitdiff
path: root/v1.25/source/IO.ASM
blob: 3b9f181f9617a449c50d58d0862ef9d8a2336c3d (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
; I/O System for 86-DOS version 1.20 and later. Revised 8-02-82.
;
; Assumes a CPU Support card at F0 hex for character I/O,
; with disk drivers for SCP, Tarbell, or Cromemco controllers.
;
; Select whether console input is interrupt-driven or polled.
INTINP:		EQU	1
;
; Select whether the auxiliary port is the Support Card parallel port
; or on channel 1 of a Multiport Serial card addressed at 10H.
PARALLELAUX:	EQU	1
SERIALAUX:	EQU	0
;
; Select whether the printer is connected to the Support card parallel
; output port (standard) or channel 0 of a Multiport Serial card
; addressed at 10H.
PARALLELPRN:	EQU	1
SERIALPRN:	EQU	0
;
; If the Multiport Serial was chosen for either the auxiliary or the
; printer, select the baud rate here. Refer to Multiport Serial manual
; page 11 to pick the correct value for a given baud rate.
PRNBAUD:EQU	7		; 1200 baud
AUXBAUD:EQU	0FH		; 19200 baud
;
; Select disk controller here.
SCP:		EQU	1
TARBELLSD:	EQU	0
TARBELLDD:	EQU	0
CROMEMCO4FDC:	EQU	0
CROMEMCO16FDC:	EQU	0
;
; Select if you want a special conversion version which can read/write
; both the new Microsoft format and the old SCP format.
; For a two drive system, drives A and B are the new Microsoft format,
; and drives C and D are the old SCP format (where C is the same physical
; drive as A, and D is the same drive as B).  CONVERT has no effect
; on 5.25-inch drives.
CONVERT:EQU	1
;
; Select disk configuration:
LARGE:	EQU	1		; Large drives.
COMBIN:	EQU	0		; Two 8-inch and one 5.25-inch.
SMALL:	EQU	0		; Three 5.25-inch drives.
CUSTOM:	EQU	0		; User defined.
;
; If 8-inch drives are PerSci, select FASTSEEK here:
; (Fastseek with Tarbell controllers doesn't work yet).
FASTSEEK:	EQU	1
;
; For double-density controllers, select double-sided operation of
; 8-inch disks in double-density mode.
LARGEDS:	EQU	0
;
; For double-density controllers, select double-sided operation of
; 5.25-inch disks in double-density mode.
SMALLDS:	EQU	0
;
; Use table below to select head step speed. Step times for 5" drives
; are double that shown in the table. Times for Fast Seek mode (using
; PerSci drives) is very small - 200-400 microseconds.
;
; Step value	1771	1793
;
;     0		 6ms	 3ms
;     1		 6ms	 6ms
;     2		10ms	10ms
;     3		20ms	15ms
;
STPSPD:	EQU	0
;
; ****** End of selections ********************************************
;
BIOSSEG:EQU	40H		; I/O system segment.
BIOSLEN:EQU	2048		; Maximum length of I/O system.
DOSLEN:	EQU	8192		; Maximum length of MS-DOS.
QSIZE:	EQU	80		; Input queue size.
PBUFSIZ:EQU	128		; Size of print buffer
BASE:	EQU	0F0H		; CPU Support card base port number.
SIOBASE:EQU	10H		; Base port number of Multiport Serial card.
STAT:	EQU	BASE+7		; Serial I/O status port.
DATA:	EQU	BASE+6		; Serial I/O data port.
DAV:	EQU	2		; Data available bit.
TBMT:	EQU	1		; Transmitter buffer empty bit.
SERIAL:	EQU	SERIALPRN+SERIALAUX
STCDATA:EQU	BASE+4		; Ports for 9513 Timer chip.
STCCOM:	EQU	BASE+5

	IF	SERIALAUX
AUXSTAT:EQU	SIOBASE+3
AUXDATA:EQU	SIOBASE+2
	ENDIF

	IF	PARALLELAUX
AUXSTAT:EQU	BASE+13
AUXDATA:EQU	BASE+12
	ENDIF

	IF	SERIALPRN
PRNSTAT:EQU	SIOBASE+1
PRNDATA:EQU	SIOBASE+0
	ENDIF

	IF	PARALLELPRN
PRNSTAT:EQU	BASE+13
PRNDATA:EQU	BASE+12
	ENDIF

	ORG	0
	PUT	100H

	JMP	INIT
	JMP	STATUS
	JMP	INP
	JMP	OUTP
	JMP	PRINT
	JMP	AUXIN
	JMP	AUXOUT
	JMP	READ
	JMP	WRITE
	JMP	DSKCHG
	JMP	SETDATE
	JMP	SETTIME
	JMP	GETTIME
	JMP	FLUSH
	JMP	MAPDEV
MAPDEV:
	RET	L

INIT:
	XOR	BP,BP		; Set up stack just below I/O system.
	MOV	SS,BP
	MOV	SP,BIOSSEG*16

	IF	INTINP-1
	MOV	AL,0FFH		; Mask all interrupts.
	OUTB	BASE+3
	ENDIF

	IF	INTINP
	DI			; Set up keyboard interrupt vector.
	MOV	[BP+64H],KBINT
	MOV	[BP+66H],CS
	EI
	ENDIF

	MOV	[BP+4*38H],PRNFCB
	MOV	[BP+4*38H+2],CS
	PUSH	CS
	POP	DS
;
; Initialize time-of-day clock.
;
	MOV	SI,STCTAB
	MOV	CX,4		;Initialize 4 registers
	UP
INITSTC:
	LODB
	OUT	STCCOM		;Select register to initialize
	LODB
	OUT	STCDATA
	LODB
	OUT	STCDATA
	LOOP	INITSTC

	IF	SERIAL
	MOV	CX,4
SERINIT:
	LODB
	OUT	SIOBASE+1
	OUT	SIOBASE+3
	LOOP	SERINIT
	LODB			;Baud rate for channel 0
	OUT	SIOBASE+8
	LODB			;Baud rate for channel 1
	OUT	SIOBASE+9
	ENDIF
;
; Move MS-DOS down to the first segment just above the I/O system.
;
	MOV	SI,BIOSLEN	; Source points to where MS-DOS currently is.
	MOV	AX,DOSSEG	; Destination is beginning of DOSSEG.
	MOV	ES,AX
	SUB	DI,DI
	MOV	CX,DOSLEN/2	; CX is number of words to move.
	REP
	MOVSW

	MOV	SI,INITTAB
	MOV	DX,1		; Do auto memory scan.
	CALL	0,DOSSEG
;
; Change disk read and write vectors (INT 37 and INT 38) to go to
; DIRECTREAD and DIRECTWRITE rather than READ and WRITE.
;
	SUB	BP,BP
	MOV	W,[BP+37*4],DIRECTREAD
	MOV	W,[BP+38*4],DIRECTWRITE

	MOV	DX,100H
	MOV	AH,26		;Set DMA address
	INT	33
	MOV	CX,[6]		;Get size of segment
	MOV	BX,DS		;Save segment for later
;
; DS must be set to CS so we can point to the FCB.
;
	MOV	AX,CS
	MOV	DS,AX
	MOV	DX,FCB		;File Control Block for COMMAND.COM
	MOV	AH,15
	INT	33		;Open COMMAND.COM
	OR	AL,AL
	JNZ	COMERR		;Error if file not found
	XOR	AX,AX
	MOV	[FCB+33],AX	; Set 4-byte Random Record field to
	MOV	[FCB+35],AX	;  beginning of file.
	INC	AX
	MOV	[FCB+14],AX	;Set record length field
	MOV	AH,39		;Block read (CX already set)
	INT	33
	JCXZ	COMERR		;Error if no records read
	TEST	AL,1
	JZ	COMERR		;Error if not end-of-file
;
; Make all segment registers the same.
;
	MOV	DS,BX
	MOV	ES,BX
	MOV	SS,BX
	MOV	SP,5CH		;Set stack to standard value
	XOR	AX,AX
	PUSH	AX		;Put zero on top of stack for return
	MOV	DX,80H
	MOV	AH,26
	INT	33		;Set default transfer address (DS:0080)
	PUSH	BX		;Put segment on stack
	MOV	AX,100H
	PUSH	AX		;Put address to execute within segment on stack
	RET	L		;Jump to COMMAND

COMERR:
	MOV	DX,BADCOM
	MOV	AH,9		;Print string
	INT	33
	EI
STALL:	JP	STALL

STCTAB:	DB	17H		;Select master mode register
	DW	84F3H		;Enable time-of-day
	DB	1		;Counter 1 mode register
	DW	0138H
	DB	2
	DW	0038H
	DB	3
	DW	0008H		;Set counter 3 to count days

	IF	SERIAL
	DB	0B7H, 77H, 4EH, 37H, PRNBAUD, AUXBAUD
	ENDIF

BADCOM:	DB	13,10,"Error in loading Command Interpreter",13,10,"$"
FCB:	DB	1,"COMMAND COM"
	DS	25
;
; ************ Time and Date ************
;
GETTIME:
	MOV	AL,0A7H		;Save counters 1,2,3
	OUT	STCCOM
	MOV	AL,0E0H		;Enable data pointer sequencing
	OUT	STCCOM
	MOV	AL,19H		;Select hold 1 / hold cycle
	OUT	STCCOM
	CALL	STCTIME		;Get seconds & 1/100's
	XCHG	AX,DX
	CALL	STCTIME		;Get hours & minutes
	XCHG	AX,CX
	IN	STCDATA
	MOV	AH,AL
	IN	STCDATA
	XCHG	AL,AH		;Count of days
	JP	POINTSTAT

STCTIME:
	CALL	STCBYTE
	MOV	CL,AH
STCBYTE:
	IN	STCDATA
	MOV	AH,AL
	SHR	AH
	SHR	AH
	SHR	AH
	SHR	AH
	AND	AL,0FH		;Unpack BCD digits
	AAD			;Convert to binary
	MOV	AH,AL
	MOV	AL,CL
	RET

SETTIME:
	PUSH	CX
	PUSH	DX
	CALL	LOAD0		;Put 0 into load registers to condition timer
	MOV	AL,43H		;Load counters 1 & 2
	OUT	STCCOM
	POP	DX
	POP	CX
	CALL	LOAD
	MOV	AL,43H
	OUT	STCCOM		;Load counters 1&2
	CALL	LOAD0
	MOV	AL,27H		;Arm counters 1,2,3
	OUT	STCCOM
	JP	POINTSTAT

LOAD0:
	XOR	CX,CX
	MOV	DX,CX
LOAD:
	MOV	AL,09		;Counter 1 load register
	CALL	OUTDX
	MOV	AL,0AH		;Counter 2 load register
	MOV	DX,CX
OUTDX:
	OUT	STCCOM		;Select a load register
	MOV	AL,DL
	CALL	OUTBCD
	MOV	AL,DH
OUTBCD:
	AAM			;Convert binary to unpacked BCD
	SHL	AH
	SHL	AH
	SHL	AH
	SHL	AH
	OR	AL,AH		;Packed BCD
	OUT	STCDATA
	RET

SETDATE:
	XCHG	AX,DX		;Put date in DX
	MOV	AL,0BH		;Select Counter 3 load register
	OUT	STCCOM
	XCHG	AX,DX
	OUT	STCDATA
	MOV	AL,AH
	OUT	STCDATA
	MOV	AL,44H		;Load counter 3
	OUT	STCCOM
POINTSTAT:
	PUSH	AX
	MOV	AL,1FH		;Point to status register
	OUT	STCCOM		;   so power-off glitches won't hurt
	POP	AX
	RET	L
;
; ************ CONSOLE INPUT ************
;

	IF	INTINP-1	; Non-interrupt driven input.
STATUS:
	IN	STAT
	AND	AL,DAV
	JZ	NOTHING		; Jump if nothing there.
	PUSHF			; Save Z flag.
	INB	DATA
	AND	AL,7FH
	SEG	CS
	MOV	[QUEUE],AL	; Put new character in buffer.
	POPF			; Return with Z flag clear.
	RET	L
NOTHING:
	SEG	CS
	MOV	AL,[QUEUE]	; See if there's anything in the buffer.
	NOT	AL		; Set up the Z flag.
	TEST	AL,80H
	PUSHF
	NOT	AL
	POPF
	RET	L

INP:
	MOV	AL,-1
	SEG	CS
	XCHG	AL,[QUEUE]	; Remove the character from the buffer.
	AND	AL,AL
	JNS	INRET		; Return if we have a character.
INLOOP:
	IN	STAT		; Wait till a character is available.
	AND	AL,DAV
	JZ	INLOOP
	IN	DATA
	AND	AL,7FH
INRET:
FLUSH:
	RET	L

QUEUE:	DB	-1		; For storing characters from STATUS to INP.
	ENDIF

	IF	INTINP		; Interrupt-driven input.
;
; Console keyboard interrupt handler.
;
KBINT:
	PUSH	AX
	PUSH	SI
	MOV	AL,20H		;End of Interrupt command
	OUT	BASE+2		;Send to slave
	IN	DATA		;Get the character
	AND	AL,7FH
	CMP	AL,"C"-"@"
	JZ	FLSH
	CMP	AL,"S"-"@"
	JZ	FLSH
	CMP	AL,"F"-"@"
	JNZ	SAVKY
FLSH:
	CALL	13*3,BIOSSEG	; Call I/O system keyboard buffer flush.
SAVKY:
	SEG	CS
	MOV	SI,[REAR]	;Pointer to rear of queue
	CALL	INCQ
	SEG	CS
	CMP	SI,[FRONT]	;Any room in queue?
	JZ	QFULL
	SEG	CS
	MOV	[SI],AL		;Put character in queue
	SEG	CS
	MOV	[REAR],SI	;Save pointer
LEAVINT:
	POP	SI
	POP	AX
	IRET
QFULL:
	MOV	AL,7		; BELL character.
	CALL	3*3,BIOSSEG	; Call I/O system console output function.
	JMPS	LEAVINT

STATUS:
	PUSH	SI
;See if printer ready
	IN	PRNSTAT
	AND	AL,TBMT
	JZ	NOPRN
	SEG	CS
	MOV	SI,[PFRONT]
	SEG	CS
	CMP	SI,[PREAR]	;Anything in print queue?
	JNZ	SENDPRN
	SEG	CS
	CMP	B,[PRNFCB],-1	;Print spooling in progress?
	JZ	NOPRN		;If not, nothing to print
;Print spooling in progress. Get next buffer
	PUSH	DS
	PUSH	CS
	POP	DS
	PUSH	AX
	PUSH	CX
	PUSH	DX
	PUSH	[STKSAV]
	PUSH	[STKSAV+2]
	PUSH	[DMAADD]
	PUSH	[DMAADD+2]
	MOV	DX,PQUEUE
	MOV	AH,26		;Set DMA address
	INT	33
	MOV	DX,PRNFCB
	MOV	CX,PBUFSIZ
	MOV	AH,39		;Read buffer
	INT	33
	OR	AL,AL
	JZ	NOTEOF
	MOV	B,[PRNFCB],-1	;Turn off print spooling at EOF
NOTEOF:
	POP	[DMAADD+2]
	POP	[DMAADD]
	POP	[STKSAV+2]
	POP	[STKSAV]
	MOV	SI,CX
	POP	DX
	POP	CX
	POP	AX
	POP	DS
	OR	SI,SI
	JZ	NOPRN
	ADD	SI,PQUEUE-1
	SEG	CS
	MOV	[PREAR],SI
	MOV	SI,ENDPQ-1
SENDPRN:
	CALL	INCPQ
	SEG	CS
	MOV	[PFRONT],SI
	SEG	CS
	LODSB			;Get character to print
	OUT	PRNDATA
NOPRN:
	DI			; Disable interrupts while checking queue.
	SEG	CS
	MOV	SI,[FRONT]
	SEG	CS
	CMP	SI,[REAR]	; Anything in queue?
	JZ	NOCHR		; Jump if nothing in queue.
	CALL	INCQ
	SEG	CS
	LODSB			;Get character (if there is one)
	OR	SI,SI		;Reset zero flag
NOCHR:
	EI
	POP	SI
	RET	L		;Zero clear if we have a character

INP:
	CALL	STATUS,BIOSSEG	; Get I/O system console input status.
	JZ	INP
	PUSH	SI
	DI			; Disable interrupts while changing queue pointers.
	SEG	CS
	MOV	SI,[FRONT]
	CALL	INCQ		; Permanently remove char from queue
	SEG	CS
	MOV	[FRONT],SI
	EI
	POP	SI
	RET	L

FLUSH:
	DI
	SEG	CS
	MOV	[REAR],QUEUE
	SEG	CS
	MOV	[FRONT],QUEUE
	EI
	RET	L

INCQ:
	INC	SI
	CMP	SI,ENDQ		;Exceeded length of queue?
	JB	RET
	MOV	SI,QUEUE
	RET

INCPQ:
	INC	SI
	CMP	SI,ENDPQ	;Exceeded length of queue?
	JB	RET
	MOV	SI,PQUEUE
	RET

FRONT:	DW	QUEUE
REAR:	DW	QUEUE
QUEUE:	DS	QSIZE
ENDQ:	EQU	$
PFRONT:	DW	PQUEUE
PREAR:	DW	PQUEUE
PQUEUE:	DS	PBUFSIZ
ENDPQ:	EQU	$
PRNFCB:	DB	-1
	DS	36
	ENDIF

;
; ************ Console and Printer Output ************
;
OUTP:
	PUSH	AX
OUTLP:
	IN	STAT
	AND	AL,TBMT
	JZ	OUTLP
	POP	AX
	OUT	DATA
	RET	L

PRINT:
	PUSH	SI
	SEG	CS
	MOV	SI,[PREAR]
	CALL	INCPQ
PRINLP:
	SEG	CS
	CMP	SI,[PFRONT]
	JNZ	PRNCHR
;Print queue is full
	PUSH	AX
	CALL	STATUS,BIOSSEG	;Poll and maybe print something
	POP	AX
	JMPS	PRINLP
PRNCHR:
	SEG	CS
	MOV	[PREAR],SI
	SEG	CS
	MOV	[SI],AL
	POP	SI
	RET	L
;
; ************ Auxiliary I/O ************
;
AUXIN:
	IN	AUXSTAT
	AND	AL,DAV
	JZ	AUXIN
	IN	AUXDATA
	RET	L

AUXOUT:
	PUSH	AX
AUXLP:
	IN	AUXSTAT
	AND	AL,TBMT
	JZ	AUXLP
	POP	AX
	OUT	AUXDATA
	RET	L
;
; ************ 1771/1793-type controller disk I/O ************
;
TARBELL:EQU	TARBELLSD+TARBELLDD
CROMEMCO:EQU	CROMEMCO4FDC+CROMEMCO16FDC

WD1791:	EQU	SCP+TARBELLDD+CROMEMCO16FDC
WD1771:	EQU	TARBELLSD+CROMEMCO4FDC

	IF	WD1791
READCOM:EQU	80H
WRITECOM:EQU	0A0H
	ENDIF

	IF	WD1771
READCOM:EQU	88H
WRITECOM:EQU	0A8H
	ENDIF

	IF	SCP
SMALLBIT:EQU	10H
BACKBIT:EQU	04H
DDENBIT:EQU	08H
DONEBIT:EQU	01H
DISK:	EQU	0E0H
	ENDIF

	IF	TARBELL
BACKBIT:EQU	40H
DDENBIT:EQU	08H
DONEBIT:EQU	80H
DISK:	EQU	78H
	ENDIF

	IF	CROMEMCO
SMALLBIT:EQU	10H
BACKBIT:EQU	0FDH		; Send this to port 4 to select back.
DDENBIT:EQU	40H
DONEBIT:EQU	01H
DISK:	EQU	30H
	ENDIF

	IF	SMALLDS-1
SMALLDDSECT:	EQU	8
	ENDIF

	IF	SMALLDS
SMALLDDSECT:	EQU	16
	ENDIF

	IF	LARGEDS-1
LARGEDDSECT:	EQU	8
	ENDIF

	IF	LARGEDS
LARGEDDSECT:	EQU	16
	ENDIF
;
; Disk change function.
; On entry:
;	AL = disk drive number.
; On exit:
;	AH = -1 (FF hex) if disk is changed.
;	AH = 0 if don't know.
;	AH = 1 if not changed.
;
;	CF clear if no disk error.
;	AL = disk I/O driver number.
;
;	CF set if disk error.
;	AL = disk error code (see disk read below).
;
	IF	WD1771
DSKCHG:
	MOV	AH,0		; AH = 0 in case we don't know.
	SEG	CS
	CMP	AL,[CURDRV]
	JNZ	RETL
	PUSH	AX		; Save drive number.

	IF	CROMEMCO
	INB	DISK+4
	ENDIF

	IF	TARBELL
	INB	DISK
	ENDIF

	AND	AL,20H		; Look at head load bit
	POP	AX
	JZ	RETL
	MOV	AH,1		; AH = 1, disk not changed.
RETL:
	CLC			; No disk error.
	RET	L
	ENDIF			; End of 1771 DSKCHG.

	IF	WD1791
DSKCHG:
	MOV	AH,0		; AH = 0 in case we don't know.
	SEG	CS
	CMP	AL,[CURDRV]
	JNZ	DENSCHK		; Check density if not same drive.
	PUSH	AX

	IF	SCP+CROMEMCO
	INB	DISK+4
	ENDIF

	IF	TARBELL
	INB	DISK
	ENDIF

	AND	AL,20H		; Look at head load bit
	POP	AX
	JZ	DENSCHK		; Check density if head not loaded.
	MOV	AH,1		; AH = 1, disk not changed.
	MOV	BX,PREVDENS
	SEG	CS
	XLAT			; Get previous density
	CLC			; No disk error.
	RET	L
DENSCHK:
	CALL	CHKNEW		; Unload head if selecting new drive.
	CBW
	XCHG	AX,SI
	ADD	SI,PREVDENS
	MOV	CX,4		; Try each density twice
	MOV	AH,0		; Disk may not have been changed.
CHKDENS:
	SEG	CS
	MOV	AL,[SI]		; Get previous disk I/O driver number.
	MOV	BX,DRVTAB
	SEG	CS
	XLAT			; Get drive select byte for previous density

	IF	CROMEMCO16FDC
	CALL	MOTOR		; Wait for motor to come up to speed.
	ENDIF

	OUT	DISK+4		; Select disk
	MOV	AL,0C4H		; READ ADDRESS command
	CALL	DCOM
	AND	AL,98H
	IN	DISK+3		; Eat last byte to reset DRQ
	JZ	HAVDENS		; Jump if no error in reading address.
	NOT	AH		; AH = -1 (disk changed) if new density works.
	SEG	CS
	XOR	B,[SI],1	; Try other density
	LOOP	CHKDENS
	MOV	AX,2		; Couldn't read disk at all, AH = 0 for don't 
	STC			;  know if disk changed, AL = error code 2 -
	RET	L		;  disk not ready, carry set to indicate error.

HAVDENS:
	SEG	CS
	LODSB			; AL = disk I/O driver number.
	CLC			; No disk error.
	RET	L

PREVDENS:DB	1,3,5,7,9,11,13	; Table of previous disk I/O driver numbers.
	ENDIF			; End of 1793 DSKCHG function.

CHKNEW:
	MOV	AH,AL		; Save disk drive number in AH.
	SEG	CS		; AL = previous disk drive number,
	XCHG	AL,[CURDRV]	;  make new drive current.
	CMP	AL,AH		; Changing drives?
	JZ	RET
;
; If changing drives, unload head so the head load delay one-shot will
; fire again. Do it by seeking to the same track with the H bit reset.
;
	IN	DISK+1		; Get current track number
	OUT	DISK+3		; Make it the track to seek to
	MOV	AL,10H		; Seek and unload head
	CALL	DCOM
	MOV	AL,AH		; Restore current drive number
	RET

	IF	CROMEMCO16FDC
MOTOR:
	PUSH	AX
	MOV	AH,AL
	IN	DISK+4		; See if the motor is on.
	TEST	AL,08H
	MOV	AL,AH
	OUTB	DISK+4		; Select drive & start motor.
	JNZ	MOTORSON	; No delay if motors already on.
	PUSH	CX
	MOV	CX,43716	; Loop count for 1 second.
MOTORDELAY:			;  (8 MHz, 16-bit memory).
	AAM			; 83 clocks.
	AAM			; 83 clocks.
	LOOP	MOTORDELAY	; 17 clocks.
	POP	CX
MOTORSON:
	POP	AX
	RET
	ENDIF
;
; Disk read function.
;
; On entry:
;	AL = Disk I/O driver number
;	BX = Disk transfer address in DS
;	CX = Number of sectors to transfer
;	DX = Logical record number of transfer
; On exit:
;	CF clear if transfer complete
;
;	CF set if hard disk error.
;	CX = number of sectors left to transfer.
;	AL = disk error code
;		0 = write protect error
;		2 = not ready error
;		4 = "data" (CRC) error
;		6 = seek error
;		8 = sector not found
;	       10 = write fault
;	       12 = "disk" (none of the above) error
;
READ:
	CALL	SEEK		;Position head
	JC	ERROR
	PUSH	ES		; Make ES same as DS.
	MOV	BX,DS
	MOV	ES,BX
RDLP:
	CALL	READSECT	;Perform sector read
	JC	POPESERROR
	INC	DH		;Next sector number
	LOOP	RDLP		;Read each sector requested
	CLC			; No errors.
	POP	ES		; Restore ES register.
	RET	L
;
; Disk write function.
; Registers same on entry and exit as read above.
;
WRITE:
	CALL	SEEK		;Position head
	JC	ERROR
WRTLP:
	CALL	WRITESECT	;Perform sector write
	JC	ERROR
	INC	DH		;Bump sector counter
	LOOP	WRTLP		;Write CX sectors
	CLC			; No errors.
WRITERET:
	RET	L

POPESERROR:
	POP	ES		; Restore ES register.
ERROR:
	MOV	BL,-1
	SEG	CS
	MOV	[DI],BL		; Indicate we don't know where head is.
	MOV	SI,ERRTAB
GETCOD:
	INC	BL		; Increment to next error code.
	SEG	CS
	LODB
	TEST	AH,AL		; See if error code matches disk status.
	JZ	GETCOD		; Try another if not.
	MOV	AL,BL		; Now we've got the code.
	SHL	AL		; Multiply by two.
	STC
	RET	L

ERRTAB:
	DB	40H		;Write protect error
	DB	80H		;Not ready error
	DB	8		;CRC error
	DB	2		;Seek error
	DB	10H		;Sector not found
	DB	20H		;Write fault
	DB	7		;"Disk" error
;
; Direct disk read and write from INT 37 and INT 38.  Subroutine GETIODRIVER
; calls DSKCHG to convert disk drive number to I/O driver number.
;
; Setting CURDRV to -1 before calling DSKCHG forces DSKCHG to check the disk's
; density before returning the I/O driver number.  This is necessary because
; programs such as FORMAT could change the density of a disk and leave the
; head loaded.  If the head is loaded DSKCHG assumes the disk hasn't been
; changed and returns the old I/O driver number which could be wrong.
;
; CURDRV is set to -1 before returning so when DSKCHG is called by the
; operating system, it will tell the operating system the disk may have
; been changed (because it may have been).
;
DIRECTREAD:

	IF	WD1791
	CALL	GETIODRIVER	; Convert drive number to I/O driver number.
	JC	DIRECTRET	; Return if DSKCHG returned error.
	ENDIF

	CALL	7*3,BIOSSEG	; Call READ.
	JMPS	DIRECTRET

DIRECTWRITE:

	IF	WD1791
	CALL	GETIODRIVER	; Convert drive number to I/O driver number.
	JC	DIRECTRET	; Return if DSKCHG returned error.
	ENDIF

	CALL	8*3,BIOSSEG	; Call WRITE.
DIRECTRET:
	SEG	CS
	MOV	B,[CURDRV],-1	; Force DSKCHG to do density check.
	RET	L

	IF	WD1791
GETIODRIVER:
	SEG	CS
	MOV	B,[CURDRV],-1	; Force DSKCHG to do density check.
	PUSH	BX
	PUSH	CX
	CALL	9*3,BIOSSEG	; Call DSKCHG.
	POP	CX
	POP	BX
	RET
	ENDIF
;
; Function:
;	Seeks to proper track.
; On entry:
;	Same as for disk read or write above.
; On exit:
;	AH = Drive select byte
;	DL = Track number
;	DH = Sector number
;	SI = Disk transfer address in DS
;	DI = pointer to drive's track counter in CS
;	CX unchanged (number of sectors)
;
SEEK:
	MOV	SI,BX		; Save transfer address
	CBW
	MOV	BX,AX		; Prepare to index on drive number

	IF	WD1791		; If two disk formats per drive.
	SHR	AL		; Convert to physical disk drive number.
	ENDIF

	CALL	CHKNEW		; Unload head if changing drives.
	SEG	CS
	MOV	AL,[BX+DRVTAB]	; Get drive-select byte.

	IF	CROMEMCO16FDC
	CALL	MOTOR		; Wait for the motors to come up to speed.
	ENDIF

	OUTB	DISK+4		; Select drive.

	IF	CROMEMCO
	OR	AL,80H		; Set auto-wait bit.
	ENDIF

	MOV	AH,AL		; Save drive-select byte in AH.
	XCHG	AX,DX		; AX = logical sector number.
	MOV	DL,26		; 26 sectors/track unless changed below

	IF	SCP
	TEST	DH,SMALLBIT	; Check if small disk.
	JZ	BIGONE		; Jump if big disk.
	MOV	DL,18		; Assume 18 sectors on small track.
	TEST	DH,DDENBIT	; Check if double-density.
	JZ	HAVSECT		; Jump if not.
	MOV	DL,SMALLDDSECT	; Number of sectors on small DD track.
	JP	HAVSECT
BIGONE:
	TEST	DH,DDENBIT	; Check if double-density.
	JZ	HAVSECT		; Jump if not.
	MOV	DL,LARGEDDSECT	; Number of sectors on big DD track.
	ENDIF

	IF	TARBELLDD	; Tarbell DD controller.
	TEST	DH,DDENBIT	; Check for double-density.
	JZ	HAVSECT
	MOV	DL,LARGEDDSECT	; Number of sectors on DD track.
	ENDIF

	IF	CROMEMCO4FDC
	TEST	DH,SMALLBIT	; Check if small disk.
	JNZ	HAVSECT		; Jump if not.
	MOV	DL,18		; 18 sectors on small disk track.
	ENDIF

	IF	CROMEMCO16FDC
	TEST	DH,SMALLBIT	; Check if small disk.
	JNZ	BIGONE		; Jump if big disk.
	MOV	DL,18		; Assume 18 sectors on small track.
	TEST	DH,DDENBIT	; Check if double-density.
	JZ	HAVSECT		; Jump if not.
	MOV	DL,SMALLDDSECT	; Number of sectors on small DD track.
	JP	HAVSECT
BIGONE:
	TEST	DH,DDENBIT	; Check if double-density.
	JZ	HAVSECT		; Jump if not.
	MOV	DL,LARGEDDSECT	; Number of sectors on big DD track.
	ENDIF

HAVSECT:
	DIV	AL,DL		; AL = track, AH = sector.
	XCHG	AX,DX		; AH has drive-select byte, DX = track & sector.
	INC	DH		; Sectors start at one, not zero.
	SEG	CS
	MOV	BL,[BX+TRKPT]	; Get this drive's displacement into track table.
	ADD	BX,TRKTAB	; BX now points to track counter for this drive.
	MOV	DI,BX
	MOV	AL,DL		; Move new track number into AL.
	SEG	CS
	XCHG	AL,[DI]		; Xchange current track with desired track
	OUT	DISK+1		; Inform controller chip of current track
	CMP	AL,DL		; See if we're at the right track.
	JZ	RET
	MOV	BH,2		; Seek retry count
	CMP	AL,-1		; Head position known?
	JNZ	NOHOME		; If not, home head
TRYSK:
	CALL	HOME
	JC	SEEKERR
NOHOME:
	MOV	AL,DL		; AL = new track number.
	OUT	DISK+3
	MOV	AL,1CH+STPSPD	; Seek command.
	CALL	MOVHEAD
	AND	AL,98H		; Accept not ready, seek, & CRC error bits.
	JZ	RET
	JS	SEEKERR		; No retries if not ready
	DEC	BH
	JNZ	TRYSK
SEEKERR:
	MOV	AH,AL		; Put status in AH.
	TEST	AL,80H		; See if it was a Not Ready error.
	STC
	JNZ	RET		; Status is OK for Not Ready error.
	MOV	AH,2		; Everything else is seek error.
	RET

SETUP:
	MOV	BL,DH		; Move sector number to BL to play with

	IF	SCP+CROMEMCO16FDC
	TEST	AH,DDENBIT	; Check for double density.
	JZ	CHECKSMALL	; Not DD, check size for SD.
	ENDIF

	IF	TARBELLDD
	TEST	AH,DDENBIT	; Check for double density.
	JZ	CHECK26		; Not DD.
	ENDIF

	IF	WD1791

	IF	(SCP+TARBELL)*LARGEDS+SCP*SMALLDS
	MOV	AL,AH		; Select front side of disk.
	OUT	DISK+4
	ENDIF

	IF	CROMEMCO*(LARGEDS+SMALLDS)
	MOV	AL,0FFH		; Select front side of disk.
	OUT	04H
	ENDIF

	CMP	BL,8		; See if legal DD sector number.
	JBE	PUTSEC		; Jump if ok.

	IF	(LARGEDS-1)*((SMALLDS*(SCP+CROMEMCO))-1)
	JP	STEP		; If only SS drives, we gotta step.
	ENDIF

	IF	SCP*LARGEDS*(SMALLDS-1)
	TEST	AH,SMALLBIT	; Check for 5.25 inch disk.
	JNZ	STEP		; Jump if small because SMALLDS is off.
	ENDIF

	IF	SCP*SMALLDS*(LARGEDS-1)
	TEST	AH,SMALLBIT	; Check for 8 inch disk.
	JZ	STEP		; Jump if large because LARGEDS is off.
	ENDIF

	IF	CROMEMCO16FDC*LARGEDS*(SMALLDS-1)
	TEST	AH,SMALLBIT	; Check for 5.25 inch disk.
	JZ	STEP		; Jump if small because SMALLDS is off.
	ENDIF

	IF	CROMEMCO16FDC*SMALLDS*(LARGEDS-1)
	TEST	AH,SMALLBIT	; Check for 8 inch disk.
	JNZ	STEP		; Jump if large because LARGEDS is off.
	ENDIF

	IF	LARGEDS+SMALLDS*(SCP+CROMEMCO)
	SUB	BL,8		; Find true sector for back side.
	CMP	BL,8		; See if ok now.
	JA	STEP		; Have to step if still too big.

	IF	SCP+TARBELLDD
	MOV	AL,AH		; Move drive select byte into AL.
	OR	AL,BACKBIT	; Select back side.
	OUT	DISK+4
	ENDIF

	IF	CROMEMCO16FDC
	MOV	AL,BACKBIT	; Select back side.
	OUT	04H
	ENDIF

	JP	PUTSEC
	ENDIF

	ENDIF

	IF	SCP
CHECKSMALL:
	TEST	AH,SMALLBIT	; See if big disk.
	JZ	CHECK26		; Jump if big.
	ENDIF

	IF	CROMEMCO
CHECKSMALL:
	TEST	AH,SMALLBIT	; See if big disk.
	JNZ	CHECK26		; Jump if big.
	ENDIF

	IF 	SCP+CROMEMCO
	CMP	BL,18		; See if legal small SD/SS sector.
	JA	STEP		; Jump if not.
	ENDIF

CHECK26:
	CMP	BL,26		; See if legal large SD/SS sector.
	JBE	PUTSEC		; Jump if ok.
STEP:
	INC	DL		; Increment track number.
	MOV	AL,58H		; Step in with update.
	CALL	DCOM
	SEG	CS
	INC	B,[DI]		; Increment the track pointer.
	MOV	DH,1		; After step, do first sector.
	MOV	BL,DH		; Fix temporary sector number also.
PUTSEC:
	MOV	AL,BL		; Output sector number to controller.
	OUT	DISK+2
	DI			; Interrupts not allowed until I/O done

	IF	SCP+CROMEMCO
	INB	DISK+4		; Get head-load bit.
	ENDIF

	IF	TARBELL
	INB	DISK
	ENDIF

	NOT	AL
	AND	AL,20H		; Check head load status
	JZ	RET
	MOV	AL,4
	RET

READSECT:
	CALL	SETUP
	MOV	BL,10		; Retry count for hard error.
	XCHG	DI,SI		; Transfer address to DI.
	PUSH	DX		; Save track & sector number.
	MOV	DL,DISK+3	; Disk controller data port.
RDAGN:
	OR	AL,READCOM
	OUT	DISK

	IF	CROMEMCO
	MOV	AL,AH		; Turn on auto-wait.
	OUT	DISK+4
	ENDIF

	MOV	BP,DI		; Save address for retry.
	JMPS	RLOOPENTRY
RLOOP:
	STOB			; Write into memory.
RLOOPENTRY:

	IF	SCP
	IN	DISK+5		; Wait for DRQ or INTRQ.
	ENDIF

	IF	TARBELL+CROMEMCO
	IN	DISK+4
	ENDIF

	IF	TARBELL
	SHL	AL
	INB	DX		; Read data from disk controller chip.
	JC	RLOOP
	ENDIF

	IF	SCP+CROMEMCO
	SHR	AL
	INB	DX		; Read data from disk controller chip.
	JNC	RLOOP
	ENDIF

	EI			; Interrupts OK now
	CALL	GETSTAT
	AND	AL,9CH
	JZ	RDPOP
	MOV	DI,BP		; Get origainal address back for retry.
	MOV	BH,AL		; Save error status for report
	MOV	AL,0
	DEC	BL
	JNZ	RDAGN
	MOV	AH,BH		; Put error status in AH.
	STC
RDPOP:
	POP	DX		; Get back track & sector number.
	XCHG	SI,DI		; Address back to SI.

	IF	TARBELL
FORCINT:
	MOV	AL,0D0H		; Tarbell controllers need this Force Interrupt
	OUT	DISK		;  so that Type I status is always available
	MOV	AL,10		;  at the 1771/1793 status port so we can find
INTDLY:				;  out if the head is loaded.  SCP and Cromemco
	DEC	AL		;  controllers have head-load status available
	JNZ	INTDLY		;  at the DISK+4 status port.
	ENDIF

	RET

WRITESECT:
	CALL	SETUP
	MOV	BL,10
	PUSH	DX		; Save track & sector number.
	MOV	DL,DISK+3	; Disk controller data port.
WRTAGN:
	OR	AL,WRITECOM
	OUT	DISK

	IF	CROMEMCO
	MOV	AL,AH		; Turn on auto-wait.
	OUT	DISK+4
	ENDIF

	MOV	BP,SI
WRLOOP:

	IF	SCP
	INB	DISK+5
	ENDIF

	IF	TARBELL+CROMEMCO
	INB	DISK+4
	ENDIF

	IF	SCP+CROMEMCO
	SHR	AL
	LODB			; Get data from memory.
	OUTB	DX		; Write to disk.
	JNC	WRLOOP
	ENDIF

	IF	TARBELL
	SHL	AL
	LODB			; Get data from memory.
	OUTB	DX		; Write to disk.
	JC	WRLOOP
	ENDIF

	EI			; Interrupts OK now.
	DEC	SI
	CALL	GETSTAT
	AND	AL,0FCH
	JZ	WRPOP
	MOV	SI,BP
	MOV	BH,AL
	MOV	AL,0
	DEC	BL
	JNZ	WRTAGN
	MOV	AH,BH		; Error status to AH.
	STC
WRPOP:
	POP	DX		; Get back track & sector number.

	IF	TARBELL
	JMPS	FORCINT
	ENDIF

	IF	SCP+CROMEMCO
	RET
	ENDIF
;
; Subroutine to restore the read/write head to track 0.
;
	IF	SCP+CROMEMCO+TARBELL*(FASTSEEK-1)
HOME:
	ENDIF

	IF	FASTSEEK*CROMEMCO
	TEST	AH,SMALLBIT	; Check for large disk.
	JNZ	RESTORE		; Big disks are fast seek PerSci.
	ENDIF

	MOV	BL,3
TRYHOM:

	IF	SCP*FASTSEEK
	MOV	AL,AH		; Turn on Restore to PerSci.
	OR	AL,80H
	OUTB	DISK+4
	ENDIF

	MOV	AL,0CH+STPSPD	; Restore with verify command.
	CALL	DCOM
	AND	AL,98H

	IF	SCP*FASTSEEK
	MOV	AL,AH		; Restore off.
	OUTB	DISK+4
	ENDIF

	JZ	RET
	JS	HOMERR		; No retries if not ready
	MOV	AL,58H+STPSPD	; Step in with update
	CALL	DCOM
	DEC	BL
	JNZ	TRYHOM
HOMERR:
	STC
	RET
;
; RESTORE for PerSci drives.
; Doesn't exist yet for Tarbell controllers.
;
	IF	FASTSEEK*TARBELL
HOME:
RESTORE:
	RET
	ENDIF

	IF	FASTSEEK*CROMEMCO4FDC
RESTORE:
	MOV	AL,0C4H		;READ ADDRESS command to keep head loaded
	OUT	DISK
	MOV	AL,77H
	OUT	4
CHKRES:
	IN	4
	AND	AL,40H
	JZ	RESDONE
	IN	DISK+4
	TEST	AL,DONEBIT
	JZ	CHKRES
	IN	DISK
	JP	RESTORE		;Reload head
RESDONE:
	MOV	AL,7FH
	OUT	4
	CALL	GETSTAT
	MOV	AL,0
	OUT	DISK+1		;Tell 1771 we're now on track 0
	RET
	ENDIF

	IF	FASTSEEK*CROMEMCO16FDC
RESTORE:
	MOV	AL,0D7H		; Turn on Drive-Select and Restore.
	OUTB	4
	PUSH	AX
	AAM			; 10 uS delay.
	POP	AX
RESWAIT:
	INB	4		; Wait till Seek Complete is active.
	TEST	AL,40H
	JNZ	RESWAIT
	MOV	AL,0FFH		; Turn off Drive-Select and Restore.
	OUTB	4
	SUB	AL,AL		; Tell 1793 we're on track 0.
	OUTB	DISK+1
	RET
	ENDIF
;
; Subroutine to move the read/write head to the desired track.
; Usually falls through to DCOM unless special handling for
; PerSci drives is required in which case go to FASTSK.
;
	IF	SCP+CROMEMCO+TARBELL*(FASTSEEK-1)
MOVHEAD:
	ENDIF

	IF	CROMEMCO*FASTSEEK
	TEST	AH,SMALLBIT	; Check for PerSci.
	JNZ	FASTSK
	ENDIF

DCOM:
	OUT	DISK
	PUSH	AX
	AAM			;Delay 10 microseconds
	POP	AX
GETSTAT:
	IN	DISK+4
	TEST	AL,DONEBIT

	IF	TARBELL
	JNZ	GETSTAT
	ENDIF

	IF	SCP+CROMEMCO
	JZ	GETSTAT
	ENDIF

	IN	DISK
	RET
;
; Fast seek code for PerSci drives.
; Tarbell not installed yet.
;
	IF	FASTSEEK*TARBELL
MOVHEAD:
FASTSK:
	RET
	ENDIF

	IF	FASTSEEK*CROMEMCO
FASTSK:
	MOV	AL,6FH
	OUT	4
	MOV	AL,18H
	CALL	DCOM
SKWAIT:
	IN	4
	TEST	AL,40H
	JNZ	SKWAIT
	MOV	AL,7FH
	OUT	4
	MOV	AL,0
	RET
	ENDIF

CURDRV:	DB	-1
;
; Explanation of tables below.
;
; DRVTAB is a table of bytes which are sent to the disk controller as drive-
; select bytes to choose which physical drive is selected for each disk I/O
; driver.  It also selects whether the disk is 5.25-inch or 8-inch, single-
; density or double-density.  Always select side 0 in the drive-select byte if
; a side-select bit is available.  There should be one entry in the DRVTAB
; table for each disk I/O driver.  Exactly which bits in the drive-select byte
; do what depends on which disk controller is used.
;
; TRKTAB is a table of bytes used to store which track the read/write
; head of each drive is on.  Each physical drive should have its own
; entry in TRKTAB.
;
; TRKPT is a table of bytes which indicates which TRKTAB entry each
; disk I/O driver should use.  Since each physical drive may be used for
; more than one disk I/O driver, more than one entry in TRKPT may point
; to the same entry in TRKTAB.  Drives such as PerSci 277s which use
; the same head positioner for more than one drive should share entrys
; in TRKTAB.
;
; INITTAB is the initialization table for 86-DOS as described in the
; 86-DOS Programer's Manual under "Customizing the I/O System."
;
	IF	SCP*COMBIN*FASTSEEK
;
; A PerSci 277 or 299 and one 5.25-inch drive.
;
DRVTAB:	DB	00H,08H,01H,09H,10H,18H,00H,08H,01H,09H
TRKPT:	DB	0,0,0,0,1,1,0,0,0,0
TRKTAB:	DB	-1,-1
INITTAB:
	IF	CONVERT-1
	DB	6		; Number of disk I/O drivers.
	ENDIF

	IF	CONVERT
	DB	10
	ENDIF

	DB	0		; Disk I/O driver 0 uses disk drive 0.
	DW	LSDRIVE		; Disk I/O driver 0 is 8-inch single-density.
	DB	0		; Disk I/O driver 1 uses disk drive 0.
	DW	LDDRIVE		; Disk I/O driver 1 is 8-inch double-density.
	DB	1		; Etc.
	DW	LSDRIVE
	DB	1
	DW	LDDRIVE
	DB	2
	DW	SSDRIVE
	DB	2
	DW	SDDRIVE

	IF	CONVERT
	DB	3
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLDDRIVE
	DB	4
	DW	OLDLSDRIVE
	DB	4
	DW	OLDLDDRIVE
	ENDIF
	ENDIF

	IF	SCP*LARGE*FASTSEEK
;
; PerSci 277 or 299.
;
DRVTAB:	DB	00H,08H,01H,09H,00H,08H,01H,09H
TRKPT:	DB	0,0,0,0,0,0,0,0
TRKTAB:	DB	-1
INITTAB:
	IF	CONVERT-1
	DB	4
	ENDIF

	IF	CONVERT
	DB	8
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	0
	DW	LDDRIVE
	DB	1
	DW	LSDRIVE
	DB	1
	DW	LDDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	2
	DW	OLDLDDRIVE
	DB	3
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLDDRIVE
	ENDIF
	ENDIF

	IF	TARBELLDD
;
; Two 8-inch Shugart-type drives.
;
DRVTAB:	DB	0,8,10H,18H,0,8,10H,18H
TRKPT:	DB	0,0,1,1,0,0,1,1
TRKTAB:	DB	-1,-1
INITTAB:

	IF	CONVERT-1
	DB	4
	ENDIF

	IF	CONVERT
	DB	8
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	0
	DW	LDDRIVE
	DB	1
	DW	LSDRIVE
	DB	1
	DW	LDDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	2
	DW	OLDLDDRIVE
	DB	3
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLDDRIVE
	ENDIF
	ENDIF

	IF	TARBELLSD
;
; Four 8-inch Shugart-type drives.
;
DRVTAB:	DB	0F2H,0E2H,0F2H,0E2H
TRKPT:	DB	0,1,0,1
TRKTAB:	DB	-1,-1
INITTAB:

	IF	CONVERT-1
	DB	2
	ENDIF

	IF	CONVERT
	DB	4
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	1
	DW	LSDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLSDRIVE
	ENDIF
	ENDIF
;
; Cromemco drive select byte is derived as follows:
;	Bit 7 = 0
;	Bit 6 = 1 if double density (if 16FDC)
;	Bit 5 = 1 (motor on)
;	Bit 4 = 0 for 5", 1 for 8" drives
;	Bit 3 = 1 for drive 3
;	Bit 2 = 1 for drive 2
;	Bit 1 = 1 for drive 1
;	Bit 0 = 1 for drive 0
;
	IF	CROMEMCO4FDC*LARGE
;
; PerSci 277 drive.
;
DRVTAB:	DB	31H,32H,31H,32H
TRKPT:	DB	0,0,0,0
TRKTAB:	DB	-1
INITTAB:

	IF	CONVERT-1
	DB	2
	ENDIF

	IF	CONVERT
	DB	4
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	1
	DW	LSDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLSDRIVE
	ENDIF
	ENDIF

	IF	CROMEMCO4FDC*COMBIN
;
; A PerSci 277 and one 5.25-inch drive.
;
DRVTAB:	DB	31H,32H,24H,31H,32H
TRKPT:	DB	0,0,1,0,0
TRKTAB:	DB	-1,-1
INITTAB:

	IF	CONVERT-1
	DB	3
	ENDIF

	IF	CONVERT
	DB	5
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	1
	DW	LSDRIVE
	DB	2
	DW	SSDRIVE

	IF	CONVERT
	DB	3
	DW	OLDLSDRIVE
	DB	4
	DW	OLDLSDRIVE
	ENDIF
	ENDIF

	IF	CROMEMCO4FDC*SMALL
;
; Three 5.25-inch drives.
;
DRVTAB:	DB	21H,22H,24H
TRKPT:	DB	0,1,2
TRKTAB:	DB	-1,-1,-1
INITTAB:DB	3
	DB	0
	DW	SSDRIVE
	DB	1
	DW	SSDRIVE
	DB	2
	DW	SSDRIVE
	ENDIF

	IF	CUSTOM
;
; Cromemco 4FDC with two 8-inch Shugart-type drives.
;
DRVTAB:	DB	31H,32H,31H,32H
TRKPT:	DB	0,1,0,1
TRKTAB:	DB	-1,-1
INITTAB:
	IF	CONVERT-1
	DB	2
	ENDIF

	IF	CONVERT
	DB	4
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	1
	DW	LSDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLSDRIVE
	ENDIF
	ENDIF

	IF	CROMEMCO16FDC*SMALL
;
; Three 5.25-inch drives.
;
DRVTAB:	DB	21H,61H,22H,62H,24H,64H
TRKPT:	DB	0,0,1,1,2,2
TRKTAB:	DB	-1,-1,-1
INITTAB:DB	6
	DB	0
	DW	SSDRIVE
	DB	0
	DW	SDDRIVE
	DB	1
	DW	SSDRIVE
	DB	1
	DW	SDDRIVE
	DB	2
	DW	SSDRIVE
	DB	2
	DW	SDDRIVE
	ENDIF

	IF	CROMEMCO16FDC*COMBIN
;
; A PerSci 277 or 299 and one 5.25-inch drive.
;
DRVTAB:	DB	31H,71H,32H,72H,24H,64H,31H,71H,32H,72H
TRKPT:	DB	0,0,0,0,1,1,0,0,0,0
TRKTAB:	DB	-1,-1
INITTAB:
	IF	CONVERT-1
	DB	6
	ENDIF

	IF	CONVERT
	DB	10
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	0
	DW	LDDRIVE
	DB	1
	DW	LSDRIVE
	DB	1
	DW	LDDRIVE
	DB	2
	DW	SSDRIVE
	DB	2
	DW	SDDRIVE

	IF	CONVERT
	DB	3
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLDDRIVE
	DB	4
	DW	OLDLSDRIVE
	DB	4
	DW	OLDLDDRIVE
	ENDIF
	ENDIF

	IF	CROMEMCO16FDC*LARGE
;
; A PerSci 277 or 299.
;
DRVTAB:	DB	31H,71H,32H,72H,31H,71H,32H,72H
TRKPT:	DB	0,0,0,0,0,0,0,0
TRKTAB:	DB	-1
INITTAB:
	IF	CONVERT-1
	DB	4
	ENDIF

	IF	CONVERT
	DB	8
	ENDIF

	DB	0
	DW	LSDRIVE
	DB	0
	DW	LDDRIVE
	DB	1
	DW	LSDRIVE
	DB	1
	DW	LDDRIVE

	IF	CONVERT
	DB	2
	DW	OLDLSDRIVE
	DB	2
	DW	OLDLDDRIVE
	DB	3
	DW	OLDLSDRIVE
	DB	3
	DW	OLDLDDRIVE
	ENDIF
	ENDIF

	IF	SMALL+COMBIN
SSDRIVE:
	DW	128		; Sector size in bytes.
	DB	2		; Sector per allocation unit.
	DW	54		; Reserved sectors.
	DB	2		; Number of allocation tables.
	DW	64		; Number of directory entrys.
	DW	720		; Number of sectors on the disk.

	IF	SMALLDS-1
SDDRIVE:			; This is the IBM Personal Computer
	DW	512		; disk format.
	DB	1
	DW	1
	DB	2
	DW	64
	DW	320
	ENDIF

	IF	SMALLDS
SDDRIVE:
	DW	512
	DB	2
	DW	1
	DB	2
	DW	112
	DW	640
	ENDIF
	ENDIF			; End of small drive DPTs.

	IF	COMBIN+LARGE
LSDRIVE:
	DW	128		; Size of sector in bytes.
	DB	4		; Sectors per allocation unit.
	DW	1		; Number of reserved sectors.
	DB	2		; Number of File Allocation Tables.
	DW	68		; Number of directory entrys.
	DW	77*26		; Number of sectors on the disk.

	IF	CONVERT
OLDLSDRIVE:
	DW	128
	DB	4
	DW	52		; Old format had two tracks reserved.
	DB	2
	DW	64		; 64 directory entrys.
	DW	77*26
	ENDIF

	IF	LARGEDS-1
OLDLDDRIVE:
LDDRIVE:
	DW	1024
	DB	1
	DW	1
	DB	2
	DW	96
	DW	77*8
	ENDIF

	IF	LARGEDS
LDDRIVE:
	DW	1024
	DB	1
	DW	1
	DB	2
	DW	192		; 192 directory entrys in new 8-inch DD/DS format.
	DW	77*8*2

	IF	CONVERT
OLDLDDRIVE:
	DW	1024
	DB	1
	DW	1
	DB	2
	DW	128		; 128 directory entrys in old 8-inch DD/DS format.
	DW	77*8*2
	ENDIF
	ENDIF

	ENDIF			; End of large drive DPTs.

DOSSEG:	EQU	($+15)/16+BIOSSEG	; Compute segment to use for 86-DOS.
DOSDIF:	EQU	16*(DOSSEG-BIOSSEG)
STKSAV:	EQU	1701H+DOSDIF
DMAADD:	EQU	15B4H+DOSDIF
	END