summaryrefslogtreecommitdiff
path: root/v4.0/src/CMD/FORMAT/FORMAT.ASM
blob: c9034d7eb67e24165584ba383da75dac5eec98e7 (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
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
page   84,132
;
;	SCCSID = @(#)format.asm 1.26 85/10/20
;	SCCSID = @(#)format.asm 1.26 85/10/20
;***************************************************************
;
;	86-DOS FORMAT DISK UTILITY
;
;	This routine formats a new disk,clears the FAT and DIRECTORY then
;	optionally copies the SYSTEM and COMMAND.COM to this new disk
;
;	SYNTAX: FORMAT	[drive][/switch1][/switch2]...[/switch16]
;
;	Regardless of the drive designator , the user will be prompted to
;	insert the diskette to be formatted.
;
;***************************************************************

;	    5/12/82 ARR Mod to ask for volume ID
;	    5/19/82 ARR Fixed rounding bug in CLUSCAL:
;   REV 1.5
;	    Added rev number message
;	    Added dir attribute to DELALL FCB
;   REV 2.00
;	    Redone for 2.0
;   REV 2.10
;	    5/1/83 ARR Re-do to transfer system on small memory systems
;   REV 2.20
;	    6/17/83 system size re-initialization bug -- mjb001
;   Rev 2.25
;	    8/31/83 16-bit fat insertion
;   Rev 2.26
;	    11/2/83 MZ fix signed compare problems for bad sectors
;   Rev 2.27
;	    11/8/83 EE current directories are always saved and restored
;   Rev 2.28
;	    11/9/83 NP Printf and changed to an .EXE file
;   Rev 2.29
;	    11/11/83 ARR Fixed ASSIGN detection to use NameTrans call to see
;			if drive letter remapped. No longer IBM only
;   Rev 2.30
;	    11/13/83 ARR SS does NOT = CS, so all use of BP needs CS override
;   Rev 2.31
;	    12/27/83 ARR REP STOSB instruction at Clean: changed to be
;			sure ES = CS.




code	segment public para 'CODE'
code	ends



data	segment public para 'DATA'
data	ends

End_Of_Memory segment public para 'BUFFERS'
End_Of_Memory ends


code	segment public	para	'CODE'

	assume	cs:code,ds:nothing,es:nothing

;-------------------------------------------------------------------------------
; Define as public for debugging

; procedures
	public	GetSize
	public	AddToSystemSize
	public	Phase1Initialisation
	public	SetStartSector
	public	SetfBigFat
	public	Phase2Initialisation
	public	DiskFormat
	public	BadSector
	public	DisplayCurrentTrack
	public	WriteFileSystem
	public	Done
	public	CurrentLogicalSector
	public	PrintErrorAbort
	public	GetDeviceParameters
	public	SetDeviceParameters
	public	Multiply_32_Bits

	public	START
	public	FatAllocated
	public	MEMERRJ
	public	MEM_OK
	public	RDFRST
	public	NEEDSYS
	public	INITCALL
	public	SWITCHCHK
	public	SYSLOOP
	public	FRMTPROB
	public	GETTRK
	public	TRKFND
	public	CLRTEST
	public	CMPTRKS
	public	BadClus
;	public	DoBig
;	public	DoSet
	public	DRTFAT
	public	CLEARED
	public	LOUSE
	public	LOUSEP
	public	FATWRT
	public	SYSOK
	public	STATUS
	public	REPORTC
	public	ONCLUS
	public	MORE
	public	FEXIT
	public	SYSPRM
	public	fexitJ
	public	DoPrompt
	public	TARGPRM
	public	IsRemovable
	public	CheckRemove
	public	IsRemove
	public	NotRemove
	public	DSKPRM
	public	GOPRNIT
	public	crlf
	public	PrintString
	public	std_printf
	public	READDOS
	public	RDFILS
	public	FILESDONE
	public	CLSALL
	public	GOTBIOS
	public	GOTDOS
	public	CLSALLJ
	public	GOTCOM
	public	WRITEDOS
	public	GOTALLBIO
	public	BIOSDONE
	public	GOTNDOS
	public	PARTDOS
	public	GOTALLDOS
	public	DOSDONE
	public	PARTCOM
	public	GOTALLCOM
	public	COMDONE
	public	MAKEFIL
	public	CheckMany
	public	CLOSETARG
	public	IOLOOP
	public	GOTTARG
	public	GSYS
	public	TESTSYS
	public	GETOFFS
;	public	TESTSYSDISK		; dcl 8/23/86
	public	SETBIOS
	public	BIOSCLS
	public	SETBIOSSIZ
	public	DOSOPNOK
	public	DOSCLS
	public	SETDOSSIZ
	public	GotComHand
	public	COMCLS
	public	SETCOMSIZ
	public	GETFSIZ
	public	READFILE
	public	WRITEFILE
	public	FILIO
	public	NORMIO
	public	IORETP
	public	IORET
	public	NORMALIZE
	public	GotDeviceParameters
	public	LoadSectorTable
	public	NotBigTotalSectors
	public	NotBig
	public	FormatLoop
	public	FormatDone
	public	ContinueFormat
	public	ReportBadTrack
	public	NoMoreTracks
	public	WriteDIRloop
	public	Main_Routine
	public	ControlC_Handler

; bytes
	public	fBigFat
	public	formatError
	public	ROOTSTR
	public	DBLFLG
	public	DRIVE
	public	FILSTAT
	public	USERDIRS
	public	VOLFCB
	public	VOLNAM
	public	TRANSRC
	public	TRANDST
	public	INBUFF
	public	driveLetter
	public	systemDriveLetter
	public	CommandFile
	public	ExitStatus
	public	VolDrive
	public	DelFCB
	public	DelDrive

; words
	public	startSector
	public	fatSpace
	public	firstHead
	public	firstCylinder
	public	tracksLeft
	public	tracksPerDisk
	public	sectorsInRootDirectory
	public	directorySector
	public	printStringPointer
	public	MSTART
	public	MSIZE
	public	TempHandle
	public	BEGSEG
	public	SWITCHMAP
	public	SWITCHCOPY
	public	FAT
	public	CLUSSIZ
	public	SECSIZ
	public	SYSTRKS
	public	SECTORS
	public	currentHead
	public	currentCylinder
	public	PercentComplete
	public	Formatted_Tracks_High
	public	Formatted_Tracks_Low

; other
	public	deviceParameters
	public	Disk_Access
	public	formatPacket
;-------------------------------------------------------------------------------

data	segment public	para	'DATA'
	extrn	msgAssignedDrive:byte
	extrn	msgBadDosVersion:byte
	extrn	msgDirectoryWriteError:byte
	extrn	msgFormatComplete:byte
	extrn	msgFormatNotSupported:byte
	extrn	msgFATwriteError:byte
	extrn	msgInvalidDeviceParameters:byte
	extrn	msgLabelPrompt:byte
	extrn	msgNeedDrive:byte
	extrn	msgNoSystemFiles:byte
	extrn	msgNetDrive:byte
	extrn	msgInsertDisk:byte
	extrn	msgHardDiskWarning:byte
	extrn	msgSystemTransfered:byte
	extrn	msgFormatAnother?:byte
	extrn	msgBadCharacters:byte
	extrn	msgBadDrive:byte
	extrn	msgInvalidParameter:byte
	extrn	msgParametersNotSupported:byte
	extrn	msgReInsertDisk:byte
	extrn	msgInsertDosDisk:byte
	extrn	msgFormatFailure:byte
	extrn	ContinueMsg:Byte
	extrn	msgNotSystemDisk:byte
	extrn	msgDiskUnusable:byte
	extrn	msgOutOfMemory:byte
	extrn	msgCurrentTrack:byte
	extrn	msgWriteProtected:byte
	extrn	msgInterrupt:byte
	extrn	msgCRLF:byte
	extrn	Fatal_Error:Byte
	extrn	Read_Write_Relative:Byte
	extrn	PSP_Segment:Word
	extrn	Parse_Error_Msg:Byte
	extrn	Extended_Error_Msg:Byte
	extrn	MsgVerify:Byte

data	ends


debug	equ	0
	.xlist
	INCLUDE VERSIONA.INC
	INCLUDE DOSMAC.INC
	INCLUDE SYSCALL.INC
	INCLUDE ERROR.INC
	INCLUDE DPB.INC
	INCLUDE CPMFCB.INC
	INCLUDE DIRENT.INC
	INCLUDE CURDIR.INC
	INCLUDE PDB.INC
	INCLUDE BPB.INC
	INCLUDE FOREQU.INC
	INCLUDE FORMACRO.INC
	INCLUDE IOCTL.INC
	INCLUDE FORSWTCH.INC
	INCLUDE SYSVAR.INC
	.list


;-------------------------------------------------------------------------------
; And this is the actual data

data	segment public	para	'DATA'
	public	deviceParameters
	public	bios
	public	dos
	public	command
	public	FAT_Flag

validSavedDeviceParameters db 0
savedDeviceParameters a_DeviceParameters <>
deviceParameters a_DeviceParameters <>

Disk_Access	A_DiskAccess_Control <> 					;an000; dms;

formatPacket a_FormatPacket <>
RWPacket     a_TrackReadWritePacket <>
RW_TRF_Area	db    512     dup(0)

startSector dw	?
fatSpace dd	?
fBigFat db	FALSE

firstHead dw	?
firstCylinder dw ?
tracksLeft dw	?
tracksPerDisk dw ?

Formatted_Tracks_Low dw 0
Formatted_Tracks_High dw 0


public	NumSectors ,TrackCnt
NumSectors dw	0FFFFh
TrackCnt dw	0FFFFh
PercentComplete dw 0FFFFh			;Init non-zero so msg will display first time

public	Old_Dir
Old_Dir db	FALSE

public	fLastChance
fLastChance db	FALSE				; Flags reinvocation from
						;   LastChanceToSaveIt. Used by DSKPRM

sectorsInRootDirectory dw ?

directorySector dd 0

formatError db	0

printStringPointer dw 0

; Exit status defines
ExitStatus		db	0
ExitOK			equ	0
ExitCtrlC		equ	3
ExitFatal		equ	4
ExitNo			equ	5
ExitDriveNotReady	equ	6						;an017; dms;drive not ready error
ExitWriteProtect	equ	7						;an017; dms;write protect error

ROOTSTR DB	?
	DB	":\",0
DBLFLG	DB	0				;Initialize flags to zero
IOCNT	DD	?
MSTART	DW	?				; Start of sys file buffer (para#)
MSIZE	DW	?				; Size of above in paragraphs
TempHandle DW	?
FILSTAT DB	?				; In memory status of files
						; XXXXXX00B BIOS not in
						; XXXXXX01B BIOS partly in
						; XXXXXX10B BIOS all in
						; XXXX00XXB DOS not in
						; XXXX01XXB DOS partly in
						; XXXX10XXB DOS all in
						; XX00XXXXB COMMAND not in
						; XX01XXXXB COMMAND partly in
						; XX10XXXXB COMMAND all in

USERDIRS DB	DIRSTRLEN+3 DUP(?)		; Storage for users current directory

Paras_Per_Fat	dw	0000h			;an000;holds fat para count
Fat_Init_Value	dw	0000h			;an000;initializes the FAT

bios	a_FileStructure <>
BiosAttributes EQU attr_hidden + attr_system + attr_read_only

dos	a_FileStructure <>
DosAttributes EQU attr_hidden + attr_system + attr_read_only

command a_FileStructure <>
CommandAttributes EQU 0
CommandFile DB	"X:\COMMAND.COM",0
CommandFile_Buffer	DB	127	dup(0)	;an000;allow room for copy

Command_Com		DB	"COMMAND.COM",0

VOLFCB	DB	-1,0,0,0,0,0,8
VOLDRIVE DB	0
VOLNAM	DB	"           "
	DB	8
	DB	26 DUP(?)

DelFCB	DB	-1,0,0,0,0,0,8
DelDRIVE DB	0
DelNAM	DB	"???????????"
	DB	8
	DB	26 DUP(?)

TRANSRC DB	"A:CON",0,0			; Device so we don't hit the drive
TRANDST DB	"A:\",0,0,0,0,0,0,0,0,0,0

BEGSEG	DW	?
SWITCHMAP DW	?
SWITCHCOPY DW	?
FAT	DW	?
	DW	?
CLUSSIZ DW	?
SECSIZ	DW	?
SYSTRKS DW	?
SECTORS DW	?
INBUFF	DB	80,0
	DB	80 DUP(?)


drive	db	0
driveLetter db	"x"
systemDriveLetter db "x"

CTRL_BREAK_VECTOR	dd	?		;ac010; dms;Holds CTRL-Break
						;	    vector

Command_Path	dd	?			;an011; dms;hold pointer to
						;	    COMMAND's path

Comspec_ID	db	"COMSPEC=",00		;an011; dms;Comspec target


Environ_Segment dw	?			;an011; dms;hold segment of
						;	    environ. vector
;======== Disk Table ========== 		;an012; dms;
;Used if NumberOfFATs in BPB
;is 0.

DiskTable	dw	0,	32680,	0803h,	512,	0
		dw	4h,	0000h,	0402h,	512,	Fbig
		dw	8h,	0000h,	0803h,	512,	Fbig
		dw	10h,	0000h,	1004h,	512,	Fbig
		dw	20h,	0000h,	2005h,	512,	Fbig

public		Org_AX				;an000; dms;make it known
Org_AX		dw	?			;an000; dms;AX on entry

Cluster_Boundary_Adj_Factor	dw	?	;an000; dms;
Cluster_Boundary_SPT_Count	dw	?	;an000; dms;
Cluster_Boundary_Flag		db	False	;an000; dms;
Cluster_Boundary_Buffer_Seg	dw	?	;an000; dms;

Relative_Sector_Low		dw	?	;an000; dms;
Relative_Sector_High		dw	?	;an000; dms;

FAT_Flag			db	?	;an000; dms;
Tracks_To_Format		dw	?	;an015; dms;
Track_Count			dw	?	;an015; dms;
Format_End			db	FALSE	;an015; dms;

public Msg_Allocation_Unit_Val

Msg_Allocation_Unit_Val 	dd	?	;an019; dms;


data	ends

;For FORPROC and FORMES modules

	public	secsiz,clussiz,inbuff

	PUBLIC	crlf,std_printf

	public	switchmap,drive,driveLetter,fatSpace
	public	fBigFat, PrintString,currentHead,currentCylinder
	extrn	CheckSwitches:near,LastChanceToSaveIt:near
	extrn	Volid:near
	extrn	WriteBootSector:near,OemDone:near
	extrn	AccessDisk:near
	extrn	Main_Init:near
	extrn	Read_Disk:near
	extrn	Write_Disk:near

data	segment public	para	'DATA'
	extrn	BiosFile:byte,DosFile:byte
data	ends

;For FORPROC module

	EXTRN	FormatAnother?:near,Yes?:near,REPORT:NEAR,USER_STRING:NEAR
data	segment public	para	'DATA'
	extrn	syssiz:dword,biosiz:dword
data	ends

DOSVER_LOW EQU	0300H+20
DOSVER_HIGH EQU 0300H+20

RECLEN	EQU	fcb_RECSIZ+7
RR	EQU	fcb_RR+7

PSP_Environ	equ	2ch			;an011; dms;location of
						;	    environ. segment
						;	    in PSP

Fbig	equ	0ffh				;an000; dms;flag for big FAT

START:
	xor	bx,bx				;				;AN000;
	push	bx				;				;AN000;
	Set_Data_Segment			;				;AC000;
	mov	Org_AX,ax			;an000; dms;save ax on entry
	jmp	Main_Init			;				;AC000;


Main_Routine:					;				;AN000;
; Set memory requirements
	mov	bx,PSP_Segment			;Shrink to free space for FAT	;AC000;
	mov	es,bx				;				;AC000;
	mov	bx,End_Of_Memory		;				;AC000;
	sub	bx,PSP_Segment			;				;AC000;
	DOS_Call Setblock			;				;AC000;

	call	Get_Disk_Access 						;an014; dms;
	cmp	Disk_Access.DAC_Access_Flag,0ffh				;an014; dms;is access already allowed?
;	$if	ne								;an014; dms;no, don't change status
	JE $$IF1
		lea	dx,Disk_Access						;an014; dms;point to parm block
		mov	Disk_Access.DAC_Access_Flag,01h 			;an014; dms;signal disk access
		call	Set_Disk_Access_On_Off					;an014;dms;allow disk access
;	$endif									;an014; dms;
$$IF1:

	CALL	Phase1Initialisation
	jnc	FatAllocated

	Message msgFormatFailure		;				;AC000;
	jmp	Fexit

MEMERR:
	mov	ax, seg data
	mov	ds, ax
	Message msgOutOfMemory			;				;AC000;
						;call	 PrintString
	JMP	FEXIT

FatAllocated:

	TEST	SWITCHMAP,SWITCH_S
	JZ	INITCALL
	MOV	BX,0FFFFH
	MOV	AH,ALLOC
	INT	21H
	OR	BX,BX
	JZ	MEMERRJ 			;No memory
	MOV	[MSIZE],BX
	MOV	AH,ALLOC
	INT	21H
	JNC	MEM_OK
MEMERRJ:
	JMP	MEMERR				;No memory

MEM_OK:
	MOV	[MSTART],AX

RDFRST:
	mov	bios.fileSizeInParagraphs,0	;mjb001 initialize file size
	mov	dos.fileSizeInParagraphs,0	;mjb001 ...
	mov	command.fileSizeInParagraphs,0	;mjb001 ...
	CALL	READDOS 			;Read BIOS and DOS
	JNC	INITCALL			;OK -- read next file
NEEDSYS:
	CALL	SYSPRM				;Prompt for system disk
	JMP	RDFRST				;Try again

INITCALL:
	CALL	Phase2Initialisation

SWITCHCHK:
	MOV	DX,SWITCHMAP
	MOV	SWITCHCOPY,DX

SYSLOOP:
	;Must intialize for each iteration

	MOV	WORD PTR SYSSIZ,0
	MOV	WORD PTR SYSSIZ+2,0
	MOV	BYTE PTR DBLFLG,0
	mov	ExitStatus, ExitOK
	MOV	DX,SWITCHCOPY
	MOV	SWITCHMAP,DX			;Restore original Switches
; DiskFormat will handle call for new disk
	CALL	DISKFORMAT			;Format the disk
	JNC	GETTRK
FRMTPROB:

	test	SwitchMap,Switch_Select 					;an017; dms;SELECT option?
;	$if	z								;an017; dms;no - display message
	JNZ $$IF3
		Message msgFormatFailure		;			;AC000;
		mov	ExitStatus, ExitFatal					;an017; dms;
;	$endif									;an017; dms;
$$IF3:
	CALL	MORE				;See if more disks to format
	JMP	SHORT SYSLOOP

;Mark any bad sectors in the FATs
;And keep track of how many bytes there are in bad sectors

GETTRK:
	CALL	BADSECTOR			;Do bad track fix-up
	JC	FRMTPROB			;Had an error in Formatting - can't recover
	CMP	AX,0				;Are we finished?
	JNZ	TRKFND				;No - check error conditions
	JMP	DRTFAT				;Yes
TRKFND:
	mov	bx,word ptr Relative_Sector_Low ;get the low word of the sector ;an000; dms;
	CMP	BX,STARTSECTOR			;Are any sectors in the system area bad?
	JAE	CLRTEST 			; MZ 2.26 unsigned compare
	Message msgDiskUnusable 		;				;AC000;
	JMP	FRMTPROB			;Bad disk -- try again
CLRTEST:
	MOV	SECTORS,AX			;Save the number of sectors on the track
	TEST	SWITCHMAP,SWITCH_S		;If system requested calculate size
	JZ	BAD100
	CMP	BYTE PTR DBLFLG,0		;Have we already calculated System space?
	JNZ	CMPTRKS 			;Yes -- all ready for the compare
	INC	BYTE PTR DBLFLG 		;No -- set the flag
	CALL	GETBIOSIZE			; Get the size of the BIOS
	MOV	DX,WORD PTR SYSSIZ+2
	MOV	AX,WORD PTR SYSSIZ
	MOV	WORD PTR BIOSIZ+2,DX
	MOV	WORD PTR BIOSIZ,AX
	CALL	GETDOSSIZE
	CALL	GETCMDSIZE
	MOV	DX,WORD PTR BIOSIZ+2
	MOV	AX,WORD PTR BIOSIZ
	DIV	deviceParameters.DP_BPB.BPB_BytesPerSector
	ADD	AX,STARTSECTOR
	MOV	SYSTRKS,AX			;Space FAT,Dir,and system files require
CMPTRKS:
	mov	bx,word ptr Relative_Sector_Low ;get the low word of the sector ;an000; dms;
	CMP	BX,SYSTRKS
	JA	BAD100				; MZ 2.26 unsigned compare
	mov	ExitStatus, ExitFatal
	Message msgNotSystemDisk		;				;AC000;
	AND	SWITCHMAP,NOT SWITCH_S		;Turn off system transfer switch
	MOV	WORD PTR SYSSIZ+2,0		;No system to transfer
	MOV	WORD PTR SYSSIZ,0		;No system to transfer
BAD100:

	CMP	deviceParameters.DP_DeviceType, DEV_HARDDISK			;an000; dms;hard disk?
;	$if	e								;an000; dms; yes
	JNE $$IF5
		call	Get_Bad_Sector_Hard					;an000; dms;see if a sector is bad
;	$else									;an000; dms;floppy disk
	JMP SHORT $$EN5
$$IF5:
		call	Get_Bad_Sector_Floppy					;an000; dms;mark entire track bad
;	$endif									;an000; dms;
$$EN5:

	JMP	GETTRK

;   Inputs:	BX = Cluster number
;   Outputs:	The given cluster is marked as invalid
;		Zero flag is set if the cluster was already marked bad
;   Registers modified: DX,SI
;   No other registers affected

;=========================================================================
; BADCLUS	:	Marks off a bad cluster in the FAT
;			If a cluster has already been marked bad it
;			will return with ZR.
;
;	Inputs	:	DX:AX - Cluster Number
;
;	Outputs :	Cluster is marked invalid
;			ZR set if cluster already marked bad
;=========================================================================

BadClus 	proc		near		;an000; mark bad clusters

	push	di				;an000; save affected regs
	push	ax
	push	bx
	push	cx
	push	dx
	push	es

	mov	es, word ptr fatSpace + 2	;an005; obtain seg of FAT
	CMP	fBigFat,TRUE			;an005; 16 bit fat?
;	$if	ne				;an005; no - 12-bit fat
	JE $$IF8
		push	ax			;an000; save ax - contains low cluster number
		mov	si,dx			;an000; pick up high word of cluster
		mov	di,ax			;an000; pick up low word of cluster
		mov	cx,2			;an000; divide by 2
		call	Divide_32_Bits		;an000; 32 bit divide

		add	ax,di			;an000; add in low word of result
		adc	dx,si			;an000; pick up low word carry
						;cluster = cluster * 1.5
		add	ax,word ptr fatspace	;an005; add 0
		adc	dx,0			;an000; pick up carry

		mov	bx,dx			;an000; get high word for adjust
		mov	cx,es			;an005; place seg in ax
		call	BadClus_Address_Adjust	;an000; adjust segment offset
		mov	es,cx			;an000; new segment
		mov	si,ax			;an000; new offset

		MOV	DX,0FF7h		;an005; bad cluster flag
		MOV	AX,0FFFh		;an005; mask value

		pop	cx			;an000; restore ax in cx - low cluster number
		test	cx,1			;an000; is old clus num odd?
;		$if	nz			;an005; yes
		JZ $$IF9
			mov	cl,4		;an005; set shift count
			SHL	AX,cl		;an005; get only 12 bits - fff0
			mov	cl,4		;an005; set shift count
			SHL	DX,cl		;an005; get 12 bits	 - ff70
;		$endif				;an005;
$$IF9:
;	$else					;an005; 16-bit fats here
	JMP SHORT $$EN8
$$IF8:
	       xor     si,si			;an005; clear si
	       mov     bx,dx			;an000; get high word for multiply
	       mov     cx,2			;an000; multiply by 2
	       call    Multiply_32_Bits 	;an000; 32 bit multiply
						;	due to 2 bytes per
						;	FAT cell.  This gives
						;	us an offset into the
						;	FAT.

	       mov     cx,es			;an005; place seg in cx
	       call    BadClus_Address_Adjust	;an000; adjust segment:offset
	       mov     es,cx			;an000; new segment
	       mov     si,ax			;an000; new offset

	       MOV     DX,0FFF7h		;an005; bad cluster value
	       MOV     AX,0FFFFh		;an005; mask value
;	$endif
$$EN8:

	MOV	CX,es:[SI]			;an005; get contents of fat cell
	AND	CX,AX				;an005; make it 12 or 16 bit
						;	depending on value in AX
	NOT	AX				;an005; set AX to 0
	AND	es:[SI],AX			;an005; clear FAT entry
	OR	es:[SI],DX			;an005; flag it a bad cluster
	CMP	DX,CX				;   return op == badval;

	pop	es
	pop	dx
	pop	cx
	pop	bx
	pop	ax
	pop	di
	return

badclus 	endp

DRTFAT:
	TEST	SWITCHMAP,SWITCH_S		;If system requested, calculate size
	JZ	CLEARED
	CMP	BYTE PTR DBLFLG,0		;Have we already calculated System space?
	JNZ	CLEARED 			;Yes
	INC	BYTE PTR DBLFLG 		;No -- set the flag
	CALL	GETSIZE 			;Calculate the system size
CLEARED:
	call	Ctrl_Break_Save 		;ac010; dms;save CTRL-Break
	call	Set_Ctrl_Break
	CALL	WriteFileSystem

	JNC	FATWRT


LOUSE:

	call	Reset_Ctrl_Break		;ac010; dms;restore CTRL-Break
	Message msgDiskUnusable 		;				;AC000;
	JMP	FRMTPROB

LOUSEP:
	POP	DS
	JMP	LOUSE

FATWRT:

	PUSH	DS
	MOV	DL,DRIVE
	INC	DL
	MOV	AH,GET_DPB
	INT	21H
	CMP	AL,-1
	JZ	LOUSEP				;Something BAD has happened
	MOV	[BX.dpb_next_free],0		; Reset allocation to start of disk
	MOV	[BX.dpb_free_cnt],-1		; Force free space to be computed
	POP	DS
	TEST	SWITCHMAP,SWITCH_S		;System desired
	JZ	STATUS
	mov	al, drive
	call	AccessDisk			; note what is current logical drive
	CALL	WRITEDOS			;Write the BIOS & DOS
	JNC	SYSOK
	Message msgNotSystemDisk		;				;AC000;
	MOV	WORD PTR SYSSIZ+2,0		;No system transfered
	MOV	WORD PTR SYSSIZ,0		;No system transfered
	JMP	SHORT STATUS

SYSOK:


	test	SwitchMap,(Switch_Select or SWITCH_AUTOTEST) ;Don't display if EXEC'd by     ;AN000;
;	$IF	Z				; Select			;AN000;
	JNZ $$IF13
	   Message msgSystemTransfered		;				;AC000;
;	$ENDIF					;AN000;
$$IF13:
STATUS:

	call	Reset_Ctrl_Break		;ac010; dms;restore CTRL-Break

	CALL	CRLF




	MOV	AH,DISK_RESET
	INT	21H
	CALL	DONE				;Final call to OEM module
	JNC	REPORTC
	JMP	FRMTPROB			;Report an error

REPORTC:

;
;TEMP FIX for /AUTOTEST
;
	test	SwitchMap,(Switch_Autotest or Switch_8)       ;TEMP
;	$IF	Z
	JNZ $$IF15
	   CALL    VOLID
;	$ENDIF
$$IF15:
	test	SwitchMap,(Switch_Select or SWITCH_AUTOTEST) ;Need to shut down the report? ;AN000;
;	$IF	Z				;If exec'd by Select, we do     ;AN000;
	JNZ $$IF17
	   CALL    REPORT			;Print report
;	$ENDIF					;				;AN000;
$$IF17:
	CALL	MORE				;See if more disks to format
	JMP	SYSLOOP 			;If we returned from MORE then continue

;******************************************
; Calculate the size in bytes of the system rounded up to sector and
;   cluster boundries, Answer in SYSSIZ

GetSize proc	near
	call	GetBioSize
	call	GetDosSize
	call	GetCmdSize
	return
GetSize endp

GetBioSize proc near
	MOV	AX,WORD PTR bios.fileSizeInBytes
	MOV	DX,WORD PTR bios.fileSizeInBytes+2
	CALL	AddToSystemSize
	return
GetBioSize endp

GetDosSize proc near
	MOV	AX,WORD PTR dos.fileSizeInBytes
	MOV	DX,WORD PTR dos.fileSizeInBytes+2
	CALL	AddToSystemSize
	return
GetDosSize endp

GetCmdSize proc near
	MOV	AX,WORD PTR command.fileSizeInBytes
	MOV	DX,WORD PTR command.fileSizeInBytes+2
	call	AddToSystemSize
	return
GetCmdSize endp

;Calculate the number of sectors used for the system
PUBLIC	AddToSystemSize
AddToSystemSize proc near
	push	bx
	DIV	deviceParameters.DP_BPB.BPB_BytesPerSector
	OR	DX,DX
	JZ	FNDSIZ0
	INC	AX				; Round up to next sector
FNDSIZ0:
	PUSH	AX
	XOR	DX,DX
	xor	bx,bx
	mov	bl, deviceParameters.DP_BPB.BPB_SectorsPerCluster
	div	bx
	POP	AX
	OR	DX,DX
	JZ	ONCLUS
	SUB	DX, bx
	NEG	DX
	ADD	AX,DX				; Round up sector count to cluster
						;	boundry
ONCLUS:
	MUL	deviceParameters.DP_BPB.BPB_BytesPerSector
	ADD	WORD PTR SYSSIZ,AX
	ADC	WORD PTR SYSSIZ+2,DX
	pop	bx
	return
AddToSystemSize endp

MORE:

	mov	Formatted_Tracks_Low,0		;Reinit the track counter	;AN000;
	mov	Formatted_Tracks_High,0 	; in case of another format	;AN000;
	test	SwitchMap,(Switch_Select or SWITCH_AUTOTEST) ;Don't display if EXEC'd  by ;AN000;
	jnz	ExitProgram			; Select			;AN000;

	CMP	deviceParameters.DP_DeviceType, DEV_HARDDISK
	je	ExitProgram
	test	SwitchMap,(SWITCH_Select or SWITCH_AUTOTEST) ;If exec'd from select, then;AN000;
	jnz	ExitProgram			; don't give user choice        ;AN000;
	CALL	FormatAnother?			;Get yes or no response
	JC	ExitProgram
	CALL	CRLF
	JMP	CRLF


FEXIT:
	Set_Data_Segment			;Make sure have addressability	;AN000;
	mov	ExitStatus,ExitFatal

ExitProgram:
	test	validSavedDeviceParameters, 0ffH
	jz	DoNotRestoreDeviceParameters
	mov	savedDeviceParameters.DP_SpecialFunctions, TRACKLAYOUT_IS_GOOD
	lea	dx, savedDeviceParameters
	call	SetDeviceParameters
DoNotRestoreDeviceParameters:

	call	Format_Access_Wrap_Up		;determine access status	;an000; dms;determine access status
	mov	al,ExitStatus			;Get Errorlevel 		;AN000;
	DOS_Call Exit				;Exit program			;AN000;
	int	20h				;If other exit fails		;AN000;

; Prompt the user for a system diskette in the default drive
SYSPRM:
	MOV	AH,GET_DEFAULT_DRIVE		;Will find out the default drive
	INT	21H				;Default now in AL
	MOV	BL,AL
	INC	BL				; A = 1
	ADD	AL,41H				;Now in Ascii
	MOV	systemDriveLetter,AL		;Text now ok
	CALL	IsRemovable
	JNC	DoPrompt
;
; Media is non-removable. Switch sys disk to drive A.  Check, though, to see
; if drive A is removable too.
;
	MOV	AL,"A"
	MOV	BYTE PTR [systemDriveLetter],AL
	MOV	[BiosFile],AL
	MOV	[DosFile],AL
	MOV	[CommandFile],AL
	MOV	BX,1
	CALL	IsRemovable
	JNC	DoPrompt
	Message msgNoSystemFiles		;				;AC000;
fexitJ:
	JMP	FEXIT

DoPrompt:
	mov	al, systemDriveLetter
	sub	al, 'A'
	call	AccessDisk
	Message msgInsertDOSDisk		;				;AC000;
	Message ContinueMsg
						;lea	 dx, ptr_msgInsertDosDisk
						;CALL	 std_printf		 ;Print first line
	CALL	USER_STRING			;Wait for a key
	CALL	CRLF
	call	crlf
	return

TARGPRM:
	mov	al, drive
	call	AccessDisk
	Message MsgInsertDisk			;				;AC000;
	Message ContinueMsg			;
						;lea	 DX, ptr_msgInsertDisk
						;CALL	 std_printf		 ;Print first line
	CALL	USER_STRING			;Wait for a key
	CALL	CRLF
	return

;
; Determine if the drive indicated in BX is removable or not.
;
;   Inputs:	BX has drive (0=def, 1=A)
;   Outputs:	Carry clear
;		    Removable
;		Carry set
;		    not removable
;   Registers modified: none

IsRemovable:
	SaveReg <AX>
	MOV	AX,(IOCTL SHL 8) OR 8		; Rem media check
	INT	21H
	JNC	CheckRemove
	MOV	AX,(IOCTL SHL 8) + 9		; Is it a NET drive?
	INT	21h
	JC	NotRemove			; Yipe, say non-removable
	TEST	DX,1000h
	JNZ	NotRemove			; Is NET drive, say non-removeable
	JMP	IsRemove			; Is local, say removable
CheckRemove:
	TEST	AX,1
	JNZ	NotRemove
IsRemove:
	CLC
	RestoreReg <AX>
	return
NotRemove:
	STC
	RestoreReg <AX>
	return


; DiSKPRoMpt:
;
;	This routine prompts for the insertion of the correct diskette
;  into the Target drive, UNLESS we are being re-entrantly invoked
;  from LastChanceToSaveIt.  If the target is a Hardisk we issue a
;  warning message.
;
;	INPUTS:
;		deviceParameters.DP_DeviceType
;		fLastChance
;
;	OUTPUTS:
;		Prompt string
;		fLastChance	:= FALSE
;
;	Registers affected:
;				Flags
;
DSKPRM:
	CMP	fLastChance,TRUE
	JE	PrmptRet

	CMP	deviceParameters.DP_DeviceType, DEV_HARDDISK
	jne	goprnit
	Message msgHardDiskWarning		;				;AC000;
						;lea	 dx, ptr_msgHardDiskWarning
						;call	 std_printf
	CALL	Yes?
	jnc	OkToFormatHardDisk
	mov	ExitStatus, ExitNo
	jmp	ExitProgram

OkToFormatHardDisk:
	CALL	CRLF
	CALL	CRLF
	return

GOPRNIT:
	mov	al, drive
	call	AccessDisk
	Message msgInsertDisk			;				;AC000;
	Message ContinueMsg			;
						;lea	 dx,ptr_msgInsertDisk
						;CALL	 std_printf
	CALL	USER_STRING			;Wait for any key
	CALL	CRLF
	CALL	CRLF

PrmptRet:
	mov	fLastChance, FALSE
	return


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

ControlC_Handler:
	mov	ax, seg data
	mov	ds, ax
	Message msgInterrupt			;				;AC000;
	mov	ExitStatus, ExitCtrlC
	jmp	ExitProgram


crlf:
						;lea	 dx, msgCRLF
	mov	dx,offset msgCRLF		;CR,LF added to message 	;AC000;
PrintString:
						;mov	 printStringPointer, dx
						;lea	 dx, PrintStringPointer

std_printf:
						;push	 dx
						;call	 printf
	call	Display_Interface		;				;AC000;
	return

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


;****************************************
;Copy IO.SYS, MSDOS.SYS and COMMAND.COM into data area.
; Carry set if problems

READDOS:
	push	ax				;save regs			;an025; dms;
	push	bx				;				;an025; dms;
	push	es				;				;an025; dms;

	mov	ah,Get_In_Vars			;Find out boot drive		;an025; dms;
	int	21h				;				;an025; dms;
	mov	al,byte ptr es:[bx].SysI_Boot_Drive ;get 1 based drive ID	;an025; dms;
	add	al,40h				;Make it ASCII			;an025; dms;
	mov	[BiosFile],al			;Stuff it in file specs.	;an025; dms;
	mov	[DosFile],al			;				;an025; dms;
	mov	[CommandFile],al		;				;an025; dms;

	pop	es				;restore regs			;an025; dms;
	pop	bx				;				;an025; dms;
	pop	ax				;				;an025; dms;

	call	Get_BIOS			; dcl 8/23/86
	JNC	RDFILS
	return

RDFILS:
	MOV	BYTE PTR [FILSTAT],0
	MOV	BX,[bios.fileHandle]
	MOV	AX,[MSTART]
	MOV	DX,AX
	ADD	DX,[MSIZE]			; CX first bad para
	MOV	[bios.fileStartSegment],AX
	MOV	CX,[bios.fileSizeInParagraphs]
	ADD	AX,CX
	CMP	AX,DX
	JBE	GOTBIOS
	MOV	BYTE PTR [FILSTAT],00000001B	; Got part of BIOS
	MOV	SI,[MSIZE]
	XOR	DI,DI
	CALL	DISIX4
	push	ds
	MOV	DS,[bios.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JC	CLSALL
	XOR	DX,DX
	MOV	CX,DX
	MOV	AX,(LSEEK SHL 8) OR 1
	INT	21H
	MOV	WORD PTR [bios.fileOffset],AX
	MOV	WORD PTR [bios.fileOffset+2],DX
FILESDONE:
	CLC
CLSALL:
	PUSHF
;	CALL	COMCLS			; dcl 8/23/86
	call	FILE_CLS			; dcl 8/23/86
	POPF
	return

GOTBIOS:
	MOV	BYTE PTR [FILSTAT],00000010B	; Got all of BIOS
	push	es
	LES	SI,[bios.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[bios.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JC	CLSALL

	push	ax				; dcl 8/23/86
	push	dx				; dcl 8/23/86
	call	File_Cls			; dcl 8/23/86
	call	Get_DOS 			; dcl 8/23/86
	pop	dx				; dcl 8/23/86
	pop	ax				; dcl 8/23/86

	JNC	Found_IBMDOS			;mt 12/8/86 P894
	return					;mt 12/8/86

Found_IBMDOS:					;mt 12/8/86

	MOV	BX,[dos.fileHandle]
	MOV	[dos.fileStartSegment],AX
	CMP	AX,DX				; No room left?
	JZ	CLSALL				; Yes
	MOV	CX,[dos.fileSizeInParagraphs]
	ADD	AX,CX
	CMP	AX,DX
	JBE	GOTDOS
	OR	BYTE PTR [FILSTAT],00000100B	; Got part of DOS
	SUB	DX,[dos.fileStartSegment]
	MOV	SI,DX
	XOR	DI,DI
	CALL	DISIX4
	push	ds
	MOV	DS,[dos.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JC	CLSALL
	XOR	DX,DX
	MOV	CX,DX
	MOV	AX,(LSEEK SHL 8) OR 1
	INT	21H
	MOV	WORD PTR [dos.fileOffset],AX
	MOV	WORD PTR [dos.fileOffset+2],DX
	JMP	FILESDONE

GOTDOS:
	OR	BYTE PTR [FILSTAT],00001000B	; Got all of DOS
	push	es
	LES	SI,[dos.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[dos.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data

CLSALLJ: JNC	NOTCLSALL			;PTM P894  mt 12/8/86
	jmp	clsall				;

NotCLSALL:
	push	ax				; dcl 8/23/86

	push	dx				; dcl 8/23/86
	call	File_cls			; dcl 8/23/86
	call	Get_Command_Path		;ac011; dms; get path of
						;	     COMMAND.COM
	call	Get_COMMAND			;ac011; dms; Point to COMMAND
						;	     and read it
	pop	dx				; dcl 8/23/86
	pop	ax				; dcl 8/23/86

	JNC	Found_COMMAND			;mt 12/8/86 P894
	return					;mt 12/8/86

Found_COMMAND:					;mt 12/8/86
	MOV	BX,[command.fileHandle]
	MOV	[command.fileStartSegment],AX
	CMP	AX,DX				; No room left?
	JZ	CLSALLJ 			; Yes
	MOV	CX,[command.fileSizeInParagraphs]
	ADD	AX,CX
	CMP	AX,DX
	JBE	GOTCOM
	OR	BYTE PTR [FILSTAT],00010000B	; Got part of COMMAND
	SUB	DX,[command.fileStartSegment]
	MOV	SI,DX
	XOR	DI,DI
	CALL	DISIX4
	push	ds
	MOV	DS,[command.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JC	CLSALLJ
	XOR	DX,DX
	MOV	CX,DX
	MOV	AX,(LSEEK SHL 8) OR 1
	INT	21H
	MOV	WORD PTR [command.fileOffset],AX
	MOV	WORD PTR [command.fileOffset+2],DX
	JMP	FILESDONE

GOTCOM:
	OR	BYTE PTR [FILSTAT],00100000B	; Got all of COMMAND
	push	es
	LES	SI,[command.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[command.fileStartSegment]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JMP	CLSALL

;**************************************************
;Write BIOS DOS COMMAND to the newly formatted disk.

ASSUME	DS:DATA
WRITEDOS:
	MOV	CX,BiosAttributes
	MOV	DX,OFFSET BiosFile
	push	es
	LES	SI,[bios.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	CALL	MAKEFIL
	retc

	MOV	[TempHandle],BX
	TEST	BYTE PTR FILSTAT,00000010B
	JNZ	GOTALLBIO
	call	Get_BIOS			; dcl 8/23/86
	jnc	Got_WBIOS			;mt 12/8/86  P894
	ret

Got_WBIOS:

	push	es
	LES	SI,[bios.fileOffset]
	MOV	DI,ES
	pop	es
	MOV	WORD PTR [IOCNT],SI
	MOV	WORD PTR [IOCNT+2],DI
	MOV	BP,OFFSET bios
	CALL	GOTTARG
	retc
	JMP	SHORT BIOSDONE

GOTALLBIO:
	push	es
	LES	SI,[bios.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[bios.fileStartSegment]
	assume	ds:nothing
	CALL	WRITEFILE
	pop	ds
	assume	ds:data
BIOSDONE:
	MOV	BX,[TempHandle]
	MOV	CX,bios.fileTime
	MOV	DX,bios.fileDate
	CALL	CLOSETARG
	MOV	CX,DosAttributes
	MOV	DX,OFFSET DosFile
	push	es
	LES	SI,[dos.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	CALL	MAKEFIL
	retc

GOTNDOS:
	MOV	[TempHandle],BX
	TEST	BYTE PTR FILSTAT,00001000B
	JNZ	GOTALLDOS
	call	Get_DOS 			; dcl 8/23/86
	jnc	Got_WDOS			;mt 12/8/86  P894
	ret

Got_WDOS:
	MOV	BP,OFFSET dos
	TEST	BYTE PTR FILSTAT,00000100B
	JNZ	PARTDOS
	MOV	WORD PTR [dos.fileOffset],0
	MOV	WORD PTR [dos.fileOffset+2],0
	CALL	GETSYS3
	retc
	JMP	SHORT DOSDONE

PARTDOS:
	push	es
	LES	SI,[dos.fileOffset]
	MOV	DI,ES
	pop	es
	MOV	WORD PTR [IOCNT],SI
	MOV	WORD PTR [IOCNT+2],DI
	CALL	GOTTARG
	retc
	JMP	SHORT DOSDONE

GOTALLDOS:
	push	es
	LES	SI,[dos.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[dos.fileStartSegment]
	assume	ds:nothing
	CALL	WRITEFILE
	pop	ds
	assume	ds:data
DOSDONE:
	MOV	BX,[TempHandle]
	MOV	CX,dos.fileTime
	MOV	DX,dos.fileDate
	CALL	CLOSETARG
	MOV	CX,CommandAttributes
	call	Command_Root			;an011; dms;adjust path for
						;COMMAND.COM creation
	MOV	DX,OFFSET CommandFile
	push	es
	LES	SI,[command.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	CALL	MAKEFIL
	retc

	MOV	[TempHandle],BX
	TEST	BYTE PTR FILSTAT,00100000B
	JNZ	GOTALLCOM
	call	Get_COMMAND			; dcl 8/23/86
	jnc	Got_WCOM			;mt 12/8/86  P894
	ret

Got_WCOM:
	MOV	BP,OFFSET command
	TEST	BYTE PTR FILSTAT,00010000B
	JNZ	PARTCOM
	MOV	WORD PTR [command.fileOffset],0
	MOV	WORD PTR [command.fileOffset+2],0
	CALL	GETSYS3
	retc
	JMP	SHORT COMDONE

PARTCOM:
	push	es
	LES	SI,[command.fileOffset]
	MOV	DI,ES
	pop	es
	MOV	WORD PTR [IOCNT],SI
	MOV	WORD PTR [IOCNT+2],DI
	CALL	GOTTARG
	retc
	JMP	SHORT COMDONE

GOTALLCOM:
	push	es
	LES	SI,[command.fileSizeInBytes]
	MOV	DI,ES
	pop	es
	push	ds
	MOV	DS,[command.fileStartSegment]
	assume	ds:nothing
	CALL	WRITEFILE
	pop	ds
	assume	ds:data
COMDONE:
	MOV	BX,[TempHandle]
	MOV	CX,command.fileTime
	MOV	DX,command.fileDate
	CALL	CLOSETARG
;****************************************************************
; I don't see the need for the following code!! - RS 3.20
;	CMP	BYTE PTR [FILSTAT],00101010B
;	JZ	NOREDOS
;RDFRST2:
;	CALL	READDOS 		; Start back with BIOS
;	JNC	NOREDOS
;	CALL	SYSPRM			;Prompt for system disk
;	JMP	RDFRST2 		;Try again
;NOREDOS:
;****************************************************************
	CLC
	return

;*********************************************
; Create a file on target disk
; CX = attributes, DX points to name
; DI:SI is size file is to have
;
;   There is a bug in DOS 2.00 and 2.01 having to do with writes
;   from the end of memory. In order to circumvent it this routine
;   must create files with the length in DI:SI
;
; On return BX is handle, carry set if problem

MAKEFIL:
	MOV	BX,DX
	PUSH	WORD PTR [BX]
	MOV	AL,DriveLetter
	MOV	[BX],AL
	MOV	AH,CREAT
	INT	21H
	POP	WORD PTR [BX]
	MOV	BX,AX
	JC	CheckMany
	MOV	CX,DI
	MOV	DX,SI
	MOV	AX,LSEEK SHL 8
	INT	21H				; Seek to eventual EOF
	XOR	CX,CX
	MOV	AH,WRITE
	INT	21H				; Set size of file to position
	XOR	CX,CX
	MOV	DX,CX
	MOV	AX,LSEEK SHL 8
	INT	21H				; Seek back to start
	return

;
; Examine error code in AX to see if it is too-many-open-files.
; If it is, we abort right here. Otherwise we return.
;
CheckMany:
	CMP	AX,error_too_many_open_files
	retnz
	Extended_Message			;				;AC006;
	JMP	FEXIT

;*********************************************
; Close a file on the target disk
; CX/DX is time/date, BX is handle

CLOSETARG:
	MOV	AX,(FILE_TIMES SHL 8) OR 1
	INT	21H
	MOV	AH,CLOSE
	INT	21H
	return

;****************************************
; Transfer system files
; BP points to data structure for file involved
; offset is set to current amount read in
; Start set to start of file in buffer
; TempHandle is handle to write to on target

IOLOOP:
	MOV	AL,[systemDriveLetter]
	CMP	AL,[DriveLetter]
	JNZ	GOTTARG
	MOV	AH,DISK_RESET
	INT	21H
	CALL	TARGPRM 			;Get target disk

GOTTARG:
ASSUME	DS:DATA
;Enter here if some of file is already in buffer, IOCNT must be set
; to size already in buffer.
	MOV	BX,[TempHandle]
	MOV	SI,WORD PTR [IOCNT]
	MOV	DI,WORD PTR [IOCNT+2]
	push	ds
	MOV	DS,ds:[BP.fileStartSegment]
	assume	ds:nothing
	CALL	WRITEFILE			; Write next part
	pop	ds
	assume	ds:data
	retc

	push	es
	LES	AX,ds:[BP.fileOffset]
	CMP	AX,WORD PTR ds:[BP.fileSizeInBytes]
	JNZ	GETSYS3
	MOV	AX,ES
	CMP	AX,WORD PTR ds:[BP.fileSizeInBytes+2]
	JNZ	GETSYS3
	pop	es
	return					; Carry clear from CMP

GETSYS3:
;Enter here if none of file is in buffer
	pop	es
	MOV	AH,DISK_RESET
	INT	21H
	MOV	AX,[MSTART]			;Furthur IO done starting here
	MOV	ds:[BP.fileStartSegment],AX	;point to start of buffer
	MOV	AL,[systemDriveLetter]		;see if we have system disk
	CMP	AL,[DriveLetter]
	JNZ	TESTSYS
GSYS:
	MOV	AH,DISK_RESET
	INT	21H
	CALL	SYSPRM				;Prompt for system disk
TESTSYS:
;	CALL	TESTSYSDISK			; dcl 8/23/86
	JC	GSYS
	MOV	BX,word ptr DS:[BP.fileHandle]	; CS over ARR 2.30
	push	es
	LES	DX,dword ptr DS:[BP.fileOffset] ; CS over ARR 2.30
	MOV	CX,ES
	pop	es
	PUSH	DX
	MOV	AX,LSEEK SHL 8
	INT	21H
	POP	DX
	push	es
	LES	SI,dword ptr DS:[BP.fileSizeInBytes] ; CS over ARR 2.30
	MOV	DI,ES				;put high word in di
	pop	es
	SUB	SI,DX				;get low word value
	SBB	DI,CX				; DI:SI is #bytes to go
	PUSH	DI
	PUSH	SI
	ADD	SI,15				;round up 1 para
	ADC	DI,0				;pick up carry
	CALL	DISID4				;div 16 to get para count
	MOV	AX,SI				;put para count in ax
	POP	SI				;restore bytes remaining
	POP	DI				;restore bytes remaining
	CMP	AX,[MSIZE]			;enough memory to read remainder?
	JBE	GOTSIZ2 			;yes
	MOV	SI,[MSIZE]
	XOR	DI,DI
	CALL	DISIX4
GOTSIZ2:
	MOV	WORD PTR [IOCNT],SI		;save byte count for read
	MOV	WORD PTR [IOCNT+2],DI
	push	ds
	MOV	DS,[MSTART]
	assume	ds:nothing
	CALL	READFILE
	pop	ds
	assume	ds:data
	JNC	GETOFFS
	CALL	CLSALL
	JMP	GSYS
GETOFFS:
	XOR	DX,DX				;clear dx
	MOV	CX,DX				;clear cx
	MOV	AX,(LSEEK SHL 8) OR 1
	INT	21H
	MOV	WORD PTR DS:[BP.fileOffset],AX	; CS over ARR 2.30
	MOV	WORD PTR DS:[BP.fileOffset+2],DX ; CS over ARR 2.30
;;;;;;	CALL	CLSALL
	JMP	IOLOOP

;*************************************************
; Test to see if correct system disk. Open handles

CRET12:
	STC
	return

;TESTSYSDISK:					; dcl 8/23/86
Get_BIOS:					; dcl 8/23/86
	MOV	AX,OPEN SHL 8
	MOV	DX,OFFSET BiosFile
	INT	21H
	JNC	SETBIOS
;	call	CheckMany			; dcl 8/23/86
	jmp	CheckMany			; dcl 8/23/86

SETBIOS:
	MOV	[Bios.fileHandle],AX
	MOV	BX,AX
	CALL	GETFSIZ
	CMP	[bios.fileSizeInParagraphs],0
	JZ	SETBIOSSIZ
	CMP	[bios.fileSizeInParagraphs],AX
	JZ	SETBIOSSIZ
BIOSCLS:
	MOV	AH,CLOSE
	MOV	BX,[Bios.fileHandle]
	INT	21H
;	JMP	CRET12		       ; dcl 8/23/86
	ret

SETBIOSSIZ:
	MOV	[bios.fileSizeInParagraphs],AX
	MOV	WORD PTR [bios.fileSizeInBytes],SI
	MOV	WORD PTR [bios.fileSizeInBytes+2],DI
	MOV	[bios.fileDate],DX
	MOV	[bios.fileTime],CX
	clc
	ret					; dcl 8/23/86

Get_DOS:					; dcl 8/23/86
	MOV	AX,OPEN SHL 8
	MOV	DX,OFFSET DosFile
	INT	21H
	JNC	DOSOPNOK
;	call	CheckMany			; dcl 8/23/86
;	JMP	BIOSCLS 			; dcl 8/23/86  Checkmany no ret.
	jmp	CheckMany			; dcl 8/23/86

DOSOPNOK:
	MOV	[dos.fileHandle],AX
	MOV	BX,AX
	CALL	GETFSIZ
	CMP	[dos.fileSizeInParagraphs],0
	JZ	SETDOSSIZ
	CMP	[dos.fileSizeInParagraphs],AX
	JZ	SETDOSSIZ

DOSCLS:
	MOV	AH,CLOSE
	MOV	BX,[dos.fileHandle]
	INT	21H
;	JMP	BIOSCLS 		; dcl 8/23/86
	ret					; dcl 8/23/86

SETDOSSIZ:
	MOV	[dos.fileSizeInParagraphs],AX
	MOV	WORD PTR [dos.fileSizeInBytes],SI
	MOV	WORD PTR [dos.fileSizeInBytes+2],DI
	MOV	[dos.fileDate],DX
	MOV	[dos.fileTime],CX
	clc
	ret					; dcl 8/23/86



Get_COMMAND:
	MOV	AX,OPEN SHL 8
	MOV	DX,OFFSET CommandFile
	INT	21H
	JNC	GotComHand
;	call	CheckMany		; dcl 8/23/86
;	JMP	DosCls			; dcl 8/23/86
	jmp	Checkmany			; dcl 8/23/86

GotComHand:
	MOV	[command.fileHandle],AX
	MOV	BX,AX
	CALL	GETFSIZ
	CMP	[command.fileSizeInParagraphs],0
	JZ	SETCOMSIZ
	CMP	[command.fileSizeInParagraphs],AX
	JZ	SETCOMSIZ
COMCLS:
	MOV	AH,CLOSE
	MOV	BX,[command.fileHandle]
	INT	21H
;	JMP	DOSCLS			; dcl 8/23/86
	ret					; dcl 8/23/86

SETCOMSIZ:
	MOV	[command.fileSizeInParagraphs],AX
	MOV	WORD PTR [command.fileSizeInBytes],SI
	MOV	WORD PTR [command.fileSizeInBytes+2],DI
	MOV	[command.fileDate],DX
	MOV	[command.fileTime],CX
	CLC
	return

FILE_CLS:					; dcl 8/23/86
	MOV	AH,CLOSE			; dcl 8/23/86
	INT	21H				; dcl 8/23/86
	ret					; dcl 8/23/86

;*******************************************
; Handle in BX, return file size in para in AX
; File size in bytes DI:SI, file date in DX, file
; time in CX.

GETFSIZ:
	MOV	AX,(LSEEK SHL 8) OR 2
	XOR	CX,CX
	MOV	DX,CX
	INT	21H
	MOV	SI,AX
	MOV	DI,DX
	ADD	AX,15				; Para round up
	ADC	DX,0
	AND	DX,0FH				; If the file is larger than this it
						; is bigger than the 8086 address
						; space!
	MOV	CL,12
	SHL	DX,CL
	MOV	CL,4
	SHR	AX,CL
	OR	AX,DX
	PUSH	AX
	MOV	AX,LSEEK SHL 8
	XOR	CX,CX
	MOV	DX,CX
	INT	21H
	MOV	AX,FILE_TIMES SHL 8
	INT	21H
	POP	AX
	return

;********************************************
; Read/Write file
;	DS:0 is Xaddr
;	DI:SI is byte count to I/O
;	BX is handle
; Carry set if screw up
;
; I/O SI bytes
; I/O 64K - 1 bytes DI times
; I/O DI bytes


READFILE:
; Must preserve AX,DX
	PUSH	AX
	PUSH	DX
	PUSH	BP
	MOV	BP,READ SHL 8
	CALL	FILIO
	POP	BP
	POP	DX
	POP	AX
	return

WRITEFILE:
	PUSH	BP
	MOV	BP,WRITE SHL 8
	CALL	FILIO
	POP	BP
	return

FILIO:
	XOR	DX,DX
	MOV	CX,SI
	JCXZ	K64IO
	MOV	AX,BP
	INT	21H
	retc
	ADD	DX,AX
	CMP	AX,CX				; If not =, AX<CX, carry set.
	retnz
	CALL	NORMALIZE
K64IO:
	CLC
	MOV	CX,DI
	JCXZ	IORET
	MOV	AX,BP
	INT	21H
	retc
	ADD	DX,AX
	CMP	AX,CX				; If not =, AX<CX, carry set.
	retnz
	CALL	NORMALIZE
	MOV	CX,DI
K64M1:
	PUSH	CX
	XOR	AX,AX
	OR	DX,DX
	JZ	NORMIO
	MOV	CX,10H
	SUB	CX,DX
	MOV	AX,BP
	INT	21H
	JC	IORETP
	ADD	DX,AX
	CMP	AX,CX				; If not =, AX<CX, carry set.
	JNZ	IORETP
	CALL	NORMALIZE
NORMIO:
	MOV	CX,0FFFFH
	SUB	CX,AX
	MOV	AX,BP
	INT	21H
	JC	IORETP
	ADD	DX,AX
	CMP	AX,CX				; If not =, AX<CX, carry set.
	JNZ	IORETP
	CALL	NORMALIZE			; Clears carry
	POP	CX
	LOOP	K64M1
	PUSH	CX
IORETP:
	POP	CX
IORET:
	return


;*********************************
; Shift DI:SI left 4 bits
DISIX4:
	MOV	CX,4
SH32:
	SHL	SI,1
	RCL	DI,1
	LOOP	SH32
	return

;*********************************
; Shift DI:SI right 4 bits
DISID4:
	MOV	CX,4
SH32B:
	SHR	DI,1
	RCR	SI,1
	LOOP	SH32B
	return

;********************************
; Normalize DS:DX

NORMALIZE:
	PUSH	DX
	PUSH	AX
	SHR	DX,1
	SHR	DX,1
	SHR	DX,1
	SHR	DX,1
	MOV	AX,DS
	ADD	AX,DX
	MOV	DS,AX
	POP	AX
	POP	DX
	AND	DX,0FH				; Clears carry
	return

;-------------------------------------------------------------------------------
; Phase1Initialisation:
;    This routine MUST set up fatSpace, and fBigFat
;    It also does most of the other initialisation
;
;    Algorithm:
;	Open a handle for accessing the drive
;	Get device parameters
;	save device parameters for exit
;	Check switches against parameters
;	Use switches to modify device parameters
;	directorySector = malloc( Bytes Per Sector )
;	fatSpace = malloc( Bytes Per Sector * Sectors Per Fat )
;	Calculate start sector (first sector not used by DOS)
;	fBigFat = (((Total Sectors - StartSector)/Sectors Per Cluster) >= 4086)
;
Phase1Initialisation proc near

; Get device parameters
	lea	dx, deviceParameters
	mov	deviceParameters.DP_SpecialFunctions, 0
	call	GetDeviceParameters
	jnc	GotDeviceParameters
	Message msgFormatNotSupported		;				;AC000;
						;lea	 dx, ptr_msgFormatNotSupported
						;call	 std_printf
	jmp	fexit
GotDeviceParameters:

; Save the device parameters for when we exit
	lea	si, deviceParameters
	lea	di, savedDeviceParameters
	mov	cx, size a_DeviceParameters
	push	ds
	pop	es
	rep	movsb

; Ensure that there is a valid number of sectors in the track table
	mov	savedDeviceParameters.DP_TrackTableEntries, 0
	mov	validSavedDeviceParameters, 1

; Initialise this to zero to know if CheckSwitches defined the track layout
	mov	deviceParameters.DP_TrackTableEntries, 0

	call	Set_BPB_Info				;an000; dms; Check to see if we are on
							; FAT system.  If not set BPB to proper
							; values for format.
SetMTsupp:

; Check switches against parameters and use switches to modify device parameters
	call	CheckSwitches
	retc

IF ShipDisk

	test	SwitchMap,Switch_Z						;an000; dms;1 sector/cluster disk?
;	$if	nz								;an000; dms;yes
	JZ $$IF19
		mov	DeviceParameters.DP_BPB.BPB_SectorsPerCluster,01h	;an000; dms;set BPB accordingly
		call	Calc_Small_Fat						;an000; dms;calc FAT size
;	$endif									;an000; dms;
$$IF19:

ENDIF


	cmp	deviceParameters.DP_TrackTableEntries, 0
	jne	TrackLayoutSet			; There is a good track layout

; Store sector table info
	mov	cx, deviceParameters.DP_BPB.BPB_SectorsPerTrack
	mov	deviceParameters.DP_TrackTableEntries, cx
	mov	ax, 1
	mov	bx, deviceParameters.DP_BPB.BPB_bytesPerSector
	lea	di, deviceParameters.DP_SectorTable
LoadSectorTable:
	stosw
	xchg	ax, bx
	stosw
	xchg	ax, bx
	inc	ax
	loop	LoadSectorTable
TrackLayoutSet:

;
; directorySector = malloc( Bytes Per Sector )
;
	mov	bx, deviceParameters.DP_BPB.BPB_BytesPerSector
	add	bx, 0fH
	shr	bx, 1
	shr	bx, 1
	shr	bx, 1
	shr	bx, 1
	mov	ah, Alloc
	int	21H
	retc
	mov	word ptr directorySector+2, ax
	xor	ax,ax
	mov	word ptr directorySector, ax

;
; fatSpace = malloc( Bytes Per Sector * Sectors Per FAT )
;
	mov	ax, deviceParameters.DP_BPB.BPB_BytesPerSector
	add	ax, 0fH
	shr	ax, 1
	shr	ax, 1
	shr	ax, 1
	shr	ax, 1
	mul	deviceParameters.DP_BPB.BPB_SectorsPerFAT
	mov	Paras_Per_Fat,ax				;AN005;128k FAT
	mov	bx,ax
	mov	ah,Alloc
	int	21H
	retc
	mov	word ptr fatSpace+2,ax
	xor	ax,ax
	mov	word ptr fatSpace,ax

	call	SetStartSector
	call	SetfBigFat

	clc
	return

Phase1Initialisation endp

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

SetStartSector proc near

; startSector = number of reserved sectors
;	 + number of FAT Sectors	( Number of FATS * Sectors Per FAT )
;	 + number of directory sectors	( 32* Root Entries / bytes Per Sector )
;					( above is rounded up )

; Calculate the number of directory sectors
	mov	ax, deviceParameters.DP_BPB.BPB_RootEntries
	mov	bx, size dir_entry
	mul	bx
	add	ax, deviceParameters.DP_BPB.BPB_bytesPerSector
	dec	ax
	xor	dx,dx
	div	deviceParameters.DP_BPB.BPB_bytesPerSector
	mov	sectorsInRootDirectory,ax
	mov	startSector, ax

; Calculate the number of FAT sectors
	mov	ax, deviceParameters.DP_BPB.BPB_SectorsPerFAT
	mul	deviceParameters.DP_BPB.BPB_numberOfFATs
; Add in the number of boot sectors
	add	ax, deviceParameters.DP_BPB.BPB_ReservedSectors
	add	startSector, ax

	return

SetStartSector endp

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

SetfBigFat proc near
;
; fBigFat = ( ( (Total Sectors - Start Sector) / Sectors Per Cluster) >= 4086 )
;
	cmp	deviceParameters.DP_BPB.BPB_BigTotalSectors+2,0 ; > 32mb part?	;AN000;
;	$IF	NE				;Yes, big FAT	;AC000;
	JE $$IF21
	   mov	   fBigFat, TRUE		;Set flag	;AN000;
;	$ELSE					;Nope, < 32,b	 ;AC000;
	JMP SHORT $$EN21
$$IF21:
	   mov	   ax,deviceParameters.DP_BPB.BPB_BigTotalSectors ;Assume this used ;AN000;
	   cmp	   ax,0 			;Was this field used?		 ;AN000;
;	   $IF	   E				;Nope, use the other sector field;AN000;
	   JNE $$IF23
	      mov     ax, deviceParameters.DP_BPB.BPB_TotalSectors ;		      ;AC000;
						;** Fix for PTM PCDOS P51
;	   $ENDIF				;				;AN000;
$$IF23:
	   sub	   ax,startSector		;Get sectors in data area
	   xor	   dx,dx
	   xor	   bx,bx
	   mov	   bl,deviceParameters.DP_BPB.BPB_sectorsPerCluster
	   div	   bx				;Get total clusters
	   cmp	   ax,BIG_FAT_THRESHOLD 	;Is clusters >= 4086?
;	   $IF	   AE
	   JNAE $$IF25
	      mov     fBigFAT,TRUE		;16 bit FAT if >=4096
						;** END fix for PTM PCDOS P51
;	   $ENDIF
$$IF25:
;	$ENDIF
$$EN21:
	return

SetfBigFat endp

;-------------------------------------------------------------------------------
;
;    Phase2Initialisation:
;	Use device parameters to build information that will be
;	required for each format
;
;    Algorithm:
;	Calculate first head/cylinder to format
;	Calculate number of tracks to format
;	Calculate the total bytes on the disk and save for later printout
;	First initialise the directory buffer
;
Phase2Initialisation proc near

; Calculate first track/head to format (round up - kludge)
	mov	ax, deviceParameters.DP_BPB.BPB_HiddenSectors
	mov	dx, deviceParameters.DP_BPB.BPB_HiddenSectors + 2
	add	ax, deviceParameters.DP_BPB.BPB_SectorsPerTrack
	adc	dx, 0
	dec	ax
	sbb	dx, 0
	div	deviceParameters.DP_BPB.BPB_SectorsPerTrack
	xor	dx,dx
	div	deviceParameters.DP_BPB.BPB_Heads
	mov	firstCylinder, ax
	mov	firstHead, dx

; Calculate the total number of tracks to be formatted (round down - kludge)
	mov	ax, deviceParameters.DP_BPB.BPB_TotalSectors
	xor	dx,dx
; if (TotalSectors == 0) then use BigTotalSectors
	or	ax,ax
	jnz	NotBigTotalSectors
	mov	ax, deviceParameters.DP_BPB.BPB_BigTotalSectors
	mov	dx, deviceParameters.DP_BPB.BPB_BigTotalSectors + 2

NotBigTotalSectors:
	div	deviceParameters.DP_BPB.BPB_SectorsPerTrack
	mov	tracksPerDisk, ax

; Initialise the directory buffer
; Clear out the Directory Sector before any information is inserted.
	mov	cx, deviceParameters.DP_BPB.BPB_BytesPerSector
	les	di, directorySector
	xor	ax,ax
	rep	stosb

	mov	ax, deviceParameters.DP_BPB.BPB_BytesPerSector
	xor	dx, dx
	mov	bx, size dir_entry
	div	bx
	mov	cx, ax

	les	bx, directorySector
; If Old_Dir = TRUE then put the first letter of each directory entry must be 0E5H
	xor	al, al
	cmp	old_Dir, TRUE
	jne	StickE5
	mov	al, 0e5H
StickE5:
	mov	es:[bx], al
	add	bx, size dir_entry
	loop	stickE5

	ret

Phase2Initialisation endp

;-------------------------------------------------------------------------------
;
;   SetDeviceParameters:
;      Set the device parameters
;
;   Input:
;      drive
;      dx - pointer to device parameters
;
SetDeviceParameters proc near

	mov	ax, (IOCTL shl 8) or GENERIC_IOCTL
	mov	bl, drive
	inc	bl
	mov	cx, (RAWIO shl 8) or SET_DEVICE_PARAMETERS
	int	21H
	return

SetDeviceParameters endp

;-------------------------------------------------------------------------------
;
;   GetDeviceParameters:
;      Get the device parameters
;
;   Input:
;      drive
;      dx - pointer to device parameters
;
GetDeviceParameters proc near

	mov	ax, (IOCTL shl 8) or GENERIC_IOCTL
	mov	bl, drive
	inc	bl
	mov	cx, (RAWIO shl 8) or GET_DEVICE_PARAMETERS
	int	21H
	return

GetDeviceParameters endp

;-------------------------------------------------------------------------------
;
;    DiskFormat:
;	Format the tracks on the disk
;	Since we do our SetDeviceParameters here, we also need to
;	detect the legality of /N /T if present and abort with errors
;	if not.
;	This routine stops as soon as it encounters a bad track
;	Then BadSector is called to report the bad track, and it continues
;	the format
;
;    Algorithm:
;	Initialise in memory FAT
;	current track = first
;	while not done
;	   if format track fails
;	      DiskFormatErrors = true
;	      return
;	   next track

DiskFormat proc near


;
; Initialise fatSpace
;


	push	es

	call	Fat_Init			;an000; initialize the FAT

	mov	di, word ptr fatspace+2 	;an000; get segment of FAT
	mov	es, di				;an000; place it in es
	mov	di, word ptr fatSpace		;Should be 0
	mov	al, deviceParameters.DP_BPB.BPB_MediaDescriptor
	mov	ah, 0ffH
	stosw
	mov	ax, 00ffH
	test	fBigFat, TRUE
	jz	NotBig
	mov	ax, 0ffffH
NotBig: stosw
	pop	es

; don't bother to do the formatting if /c was given
	test	switchmap, SWITCH_C
	jz	Keep_Going
	jmp	FormatDone			;FormatDone is to far away

Keep_Going:
foofoo	=	INSTALL_FAKE_BPB or TRACKLAYOUT_IS_GOOD
	mov	deviceParameters.DP_SpecialFunctions, foofoo
	lea	dx, deviceParameters

	call	SetDeviceParameters

	call	Cluster_Buffer_Allocate 					;an000; dms;get room for retry buffer

	call	Prompt_User_For_Disk						;an016; dms;

	test	switchmap,switch_8		; DCL 5/12/86 avoid Naples AH=18h
	jnz	stdBpB				; lackof support for 8 sectors/track

						; DCL 5/12/86
						; Always do the STATUS_FOR_FORMAT test, as we don't know if the machine
						; has this support.  For 3.2 /N: & /T: were not documented & therefore
						; not fully supported thru the ROM of Aquarius & Naples & Royal Palm

						;test	 SwitchMap, SWITCH_N or SWITCH_T	 ; IF ( /N or /T ) ;; DCL 5/12/86
						;jz	 StdBPB
						;   THEN check if
						;     supported
	mov	formatPacket.FP_SpecialFunctions, STATUS_FOR_FORMAT
	mov	ax, (IOCTL shl 8) or GENERIC_IOCTL
	mov	bl, drive
	inc	bl
	mov	cx, (RAWIO shl 8) or FORMAT_TRACK
	lea	dx, formatPacket
	int	21H
						; switch ( FormatStatusCall)

						;cmp	 FormatPacket.FP_SpecialFunctions, Format_No_ROM_Support
						;jb	 NTSupported			 ; 0 returned from IBMBIO
						;ja	 IllegalComb			 ; 2 returned - ROM Support
						;   Illegal Combination!
	cmp	FormatPacket.FP_SpecialFunctions,0
	je	NTSupported
	cmp	FormatPacket.FP_SpecialFunctions,2
;	$IF	E				;				;AC000;
	JNE $$IF28
	   Message msgInvalidParameter		;				;AC000;
	   mov	   Fatal_Error,Yes		;Indicate quittin'type err!     ;AN000;
;	$ELSE					;				;     ;
	JMP SHORT $$EN28
$$IF28:
	   cmp	   FormatPacket.FP_SpecialFunctions,3 ; 			   ;	 ;
;	   $IF	   E				;				;AC000;
	   JNE $$IF30
		   mov	   ax,Error_Not_Ready	;flag not ready 		;an000;dms;
		   call    CheckError		; set error level		;an017; dms;
		   jmp	   FrmtProb		; exit program			;an017; dms;
;	   $ELSE				; DCL No ROM support is okay	;     ;
	   JMP SHORT $$EN30
$$IF30:
						; except for /N: & /T:		;     ;
	      test    SwitchMap, SWITCH_N or SWITCH_T ; DCL 5/12/86		      ;     ;
;	      $IF     NZ			;				;AC000;
	      JZ $$IF32
		 Message msgParametersNotSupported ;				   ;AC000;
		 mov	 Fatal_Error,Yes	;Indicate quittin'type err!     ;AN000;
;	      $ENDIF				;				;AN000;
$$IF32:
;	   $ENDIF				;				;AN000;
$$EN30:
;	$ENDIF					;				;AN000;
$$EN28:
	cmp	Fatal_Error,Yes 		;				;AN000;
	jne	StdBPB				;				;AN000;
	jmp	Fexit
;
; We have the support to carry out the FORMAT
;
NTSupported:
StdBPB:
						;call	 DSKPRM 		 ; prompt user for disk ;; DCL 5/12/86
	mov	FormatPacket.FP_SpecialFunctions, 0
	mov	ax, firstHead
	mov	formatPacket.FP_Head, ax
	mov	ax, firstCylinder
	mov	formatPacket.FP_Cylinder, ax
	mov	cx, tracksPerDisk
	mov	tracksLeft, cx
	mov	Format_End,False						;an015; dms;flag not at end of format
	call	Calc_Max_Tracks_To_Format					;an015; dms;max track count for FormatTrack call
FormatLoop:
	call	Format_Loop							;an015; dms;Format until CY occurs

	cmp	Format_End,True 						;an015; dms;End of Format?
;	$if	e								;an015; dms;yes
	JNE $$IF36
		mov	FormatError,0						;an015; dms;signal good format
		clc								;an015; dms;clear CY
;	$else									;an015; dms;bad format
	JMP SHORT $$EN36
$$IF36:
		call	CheckError						;an015; dms;determine type of error
;		$if	nc							;an015; dms;
		JC $$IF38
			call	LastChanceToSaveIt				;an015; dms;acceptable error?
;			$if	c						;an015; dms;yes
			JNC $$IF39
				mov	FormatError,1				;an015; dms;signal error type
				clc						;an015; dms;clear CY
;			$else							;an015; dms;not acceptable error
			JMP SHORT $$EN39
$$IF39:
				call	SetStartSector				;an015; dms;start from scratch
				call	SetFBigFat				;an015; dms;
				push	ax					;an015; dms;
				call	Phase2Initialisation			;an015; dms;
				clc						;an015; dms;
				pop	ax					;an015; dms;
				jmp	DiskFormat				;an015; dms;try again
;			$endif							;an015; dms;
$$EN39:
;		$endif								;an015; dms;
$$IF38:
;	$endif									;an015; dms;
$$EN36:
	return

FormatDone:
	mov	FormatError,0
	clc
	return

DiskFormat endp


;-------------------------------------------------------------------------------
;
;    BadSector:
;	Reports the bad sectors.
;	Reports the track where DiskFormat stopped.
;	From then on it formats until it reaches a bad track, or end,
;	and reports that.
;
;    Output:
;	Carry: set --> fatal error
;	if Carry not set
;	   ax - The number of consecutive bad sectors encountered
;		ax == 0 --> no more bad sectors
;	   bx - The logical sector number of the first bad sector
;
;    Algorithm:
;	if DiskFormatErrors
;	   DiskFormatErrors = false
;	   return current track
;	else
;	   next track
;	   while not done
;	      if format track fails
;		 return current track
;	      next track
;	   return 0

BadSector proc	near


; don't bother to do the formatting if /c was given
	test	switchmap, SWITCH_C
	jnz	NoMoreTracks

	test	formatError, 0ffH
	jz	ContinueFormat
	mov	formatError, 0
	jmp	ReportBadTrack

ContinueFormat:
	call	Adj_Track_Count 						;an015; dms;decrease track counter
	call	NextTrack							;an015; dms;adjust head and cylinder
	cmp	Format_End,True 						;an015; dms;end of format?
;	$if	ne								;an015; dms;no
	JE $$IF44
		call	Format_Loop						;an015; dms;format until CY
		cmp	Format_End,True 					;an015; dms;end of format?
;		$if	ne							;an015; dms;no
		JE $$IF45
			call	CheckError					;an015; dms;must be error - which error?
;			$if	nc						;an015; dms;non-fatal error?
			JC $$IF46
				call	CurrentLogicalSector			;an015; dms;yes - get position
				mov	ax,DeviceParameters.DP_BPB.BPB_SectorsPerTrack	;an015; dms; set track size
				clc						;an015; dms;signal O.K. to continue
;			$endif							;an015; dms;
$$IF46:
;		$else								;an015; dms;
		JMP SHORT $$EN45
$$IF45:
			jmp	NoMoreTracks					;an015; dms;end of format
;		$endif								;an015; dms;
$$EN45:
;	$else									;an015; dms;
	JMP SHORT $$EN44
$$IF44:
		jmp	NoMoreTracks						;an015; dms;end of format
;	$endif									;an015; dms;
$$EN44:
	return									;an015; dms;

ReportBadTrack:
	call	CurrentLogicalSector
	mov	ax, deviceParameters.DP_BPB.BPB_SectorsPerTrack
	clc
	return

NoMoreTracks:
	test	SwitchMap,(Switch_Select or SWITCH_AUTOTEST) ;Don't display done msg;AN000;
;	$IF	Z				; if EXEC'd by SELECT           ;AN000;
	JNZ $$IF52
	   Message msgFormatComplete		;				;AC000;
;	$ENDIF					;				;AN000;
$$IF52:
	mov	ax, 0
	clc
	return

BadSector endp



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

data	segment public	para 'DATA'

;ptr_msgCurrentTrack dw offset msgCurrentTrack
currentHead dw	0
currentCylinder dw 0

data	ends

;=========================================================================
; Calc_Current_Head_Cyl : Obtain the current head and cylinder of the
;			  track being formatted.
;
;	Inputs: FP_Cylinder	- Cylinder of track being formatted
;		FP_Head 	- Head of track being formatted
;=========================================================================

Procedure Calc_Current_Head_Cyl 						;an000; dms;

	push	cx								;an000; dms;save cx
	mov	cx,FormatPacket.FP_Cylinder					;an000; dms;get current cylinder
	mov	CurrentCylinder,cx						;an000; dms;put into variable
	mov	cx,FormatPacket.FP_Head 					;an000; dms;get current head
	mov	CurrentHead,cx							;an000; dms;put into variable
	pop	cx								;an000; dms;restore cx
	ret									;an000; dms;

Calc_Current_Head_Cyl	endp							;an000; dms;


DisplayCurrentTrack proc near

	push	dx				;				;AN000;
	push	cx				;				;AN000;
	push	ax				;an015; dms;

	mov	ax,Tracks_To_Format		;an015; dms;get track count

	add	Formatted_Tracks_Low,ax 	;Indicate formatted a track	;AN000;
	adc	Formatted_Tracks_High,0 	;				;AN000;
	mov	ax,Formatted_Tracks_Low 	;				;AN000;
	mov	bx,Formatted_Tracks_High	;				;AN000;
	mov	cx,100				;Make integer calc for div	;AN000;
	call	Multiply_32_Bits		; BX:AX = (Cyl * Head *100)	;AN000;
	mov	dx,bx				;Set up divide			;AN000;
	div	TracksPerDisk			;% = (Cyl * Head *100)/ # tracks;AN000;
	cmp	ax,PercentComplete		;Only print message when change ;AN000;
;	$IF	NE				;To avoid excess cursor splat	;AN000;
	JE $$IF54
	   mov	   PercentComplete,ax		;Save it if changed		;AN000;
	   Message msgCurrentTrack		;				;AC000;
;	$ENDIF					;
$$IF54:
	pop	ax				;an015; dms;
	pop	cx				;Restore register		;AN000;
	pop	dx				;				;AN000;
	return

DisplayCurrentTrack endp


;-------------------------------------------------------------------------------
;    CheckError:
;	Input:
;	   ax - extended error code
;	Ouput:
;	   carry set if error is fatal
;	   Message printed if Not Ready or Write Protect
;
CheckError proc near
	cmp	ax, error_write_protect
	je	WriteProtectError
	cmp	ax, error_not_ready
	je	NotReadyError
	cmp	currentCylinder, 0
	jne	CheckRealErrors
	cmp	currentHead, 0
	je	BadTrackZero

CheckRealErrors:
	cmp	ax, error_CRC
	je	JustABadTrack
	cmp	ax, error_sector_not_found
	je	JustABadTrack
	cmp	ax, error_write_fault
	je	JustABadTrack
	cmp	ax, error_read_fault
	je	JustABadTrack
	cmp	ax, error_gen_failure
	je	JustABadTrack

	stc
	ret

JustABadTrack:
	clc
	ret

WriteProtectError:

	test	SwitchMap,Switch_SELECT 					;an017; dms;SELECT option?
;	$if	z								;an017; dms;no - display messages
	JNZ $$IF56
		Message  msgCRLF			;			;AC006;
		Message  msgCRLF			;			;AC006;
		Extended_Message			;			;AC006;
;	$else									;an017; dms;yes - set error level
	JMP SHORT $$EN56
$$IF56:
		mov	ExitStatus,ExitWriteProtect				;an017; dms;signal write protect error
;	$endif									;an017; dms;
$$EN56:

	stc									;an017; dms;signal fatal error
	ret									;an017; dms;return to caller

NotReadyError:
	test	SwitchMap,Switch_SELECT 					;an017; dms; SELECT option?
;	$if	z								;an017; dms; no - display messages
	JNZ $$IF59
		Message  msgCRLF			;			;AC006;
		Message  msgCRLF			;			;AC006;
		Extended_Message			;			;AC006;
;	$else									;an017; dms;yes - set error level
	JMP SHORT $$EN59
$$IF59:
		mov	ExitStatus,ExitDriveNotReady				;an017; dms;signal drive not ready
;	$endif									;an017; dms;
$$EN59:
	stc
	ret


BadTrackZero:
	Message msgDiskUnusable 		;				;AC000;
	stc
	ret

CheckError endp

;-------------------------------------------------------------------------------
;    WriteFileSystem:
;	Write the boot sector and FATs out to disk
;	Clear the directory sectors to zero
;

WriteFileSystem proc near


	call	WriteBootSector
	retc

	Set_Data_Segment			;Set DS,ES = DATA		;AN000;
; Write out each of the FATs
	push	ds				;ac005; dms;save ds
	xor	cx, cx
	mov	cl, es:deviceParameters.DP_BPB.BPB_numberOfFATs ;		;AC000;
	mov	dx, es:deviceParameters.DP_BPB.BPB_ReservedSectors ;		;AC000;
	mov	al, es:drive			;				;AC000;
	mov	bx,word ptr es:FatSpace+2	;Get segment of memory Fat	;AC000;
	mov	ds,bx				;				;AN000;
	mov	bx,word ptr es:FatSpace 	;				;AN000;

	mov	si,bx				;ac005; dms;set up for add. calc
	call	SEG_ADJ 			;ac005; dms;get adjusted seg:off
	mov	bx,si				;ac005; dms;get new off
	assume	ds:nothing,es:data		;				;AN000;

;	$do					;ac005; dms;while FATS > 0
$$DO62:
		cmp	cx,00			;ac005; dms;FATS remaining?
;		$leave	e			;ac005; dms;no
		JE $$EN62
		push	bx			;ac005; dms;save FAT offset
		push	ds			;ac005; dms;save FAT segment
		push	cx			;ac005; dms;save FAT count
		push	dx			;ac005; dms;reserved FAT sector
		call	WRITE_FAT		;ac005; dms;write the FAT
		pop	dx			;ac005; dms;get 1st. FAT sector
		pop	cx			;ac005; dms;get FAT count
		pop	ds			;ac005; dms;restore FAT segment
		pop	bx			;ac005; dms;restore FAT offset
;		$if	c			;ac005; dms;an error occurred
		JNC $$IF64
			Message msgFATwriteError;ac005; dms;say why failed
			jmp	FEXIT		;ac005; dms;exit format
;		$endif				;ac005; dms;
$$IF64:
		add	dx, es:deviceParameters.DP_BPB.BPB_SectorsPerFAT ;		;AC000;
		dec	cx			;ac005; dms;decrease FAT count
;	$enddo					;ac005; dms;
	JMP SHORT $$DO62
$$EN62:

	pop	ds				;ac005; dms;restore ds
	assume	ds:data 			;ac005; dms;


; Clear the directory

; Now write the initialised directory sectors out to disk
	mov	ax, es:deviceParameters.DP_BPB.BPB_SectorsPerFAT ;		;AC000;
	xor	dx,dx
	push	bx								;an000; dms;save bx
	xor	bx,bx								;an000; dms;clear bx
	mov	bl,es:DeviceParameters.DP_BPB.BPB_NumberOfFATs			;an000; dms;get FAT count
	mul	bx								;an000; dms;get total FAT sectors
	pop	bx								;an000; dms;restore bx

	mov	dx, es:deviceParameters.DP_BPB.BPB_ReservedSectors ;		;AC000;
	add	dx, ax
	mov	cx, es:sectorsInRootDirectory	;				  ;AC000;
WriteDIRloop:
	push	cx
	push	dx
	mov	al, es:drive			;				;AC000;
	mov	cx, 1
	lds	bx, es:directorySector		;				;AC000;

	assume	ds:nothing,es:data		;				;AN000;

;Assume dir is alway contined in first 32mb of partition

	mov	es:Read_Write_Relative.Start_Sector_High,0 ;			;AC000;
	Call	Write_Disk			;				;AN000;
	jnc	Dir_OK				;				;AC000;
	Message msgDirectoryWriteError		;				;AC000;
	jmp	FExit				;				;AN000;
Dir_OK: 					;				;AN000;
	pop	dx
	add	dx, 1
	pop	cx
	loop	WriteDIRLoop

	Set_Data_Segment			;Set DS to DATA segment 	;AN000;
; Ok, we can tell the device driver that we are finished formatting
	mov	savedDeviceParameters.DP_TrackTableEntries, 0
	mov	savedDeviceParameters.DP_SpecialFunctions, TRACKLAYOUT_IS_GOOD
	lea	dx, savedDeviceParameters
	call	SetDeviceParameters

	MOV	AH,DISK_RESET			; Flush any directories in
	INT	21H				; buffers

	return


WriteFileSystem endp

;=========================================================================
; WRITE_FAT	:	This routine writes the logical sector count requested.
;			It will write a maximum of 40h sectors.  If more
;			than 40h exists it will continue looping until
;			all sectors have been written.
;
;	Inputs	:	AL - Drive letter
;			DS:BX - Segment:offset of transfer address
;			CX - Sector count
;			DX - 1st. sector
;
;	Outputs :	Logical sectors written
;=========================================================================

procedure	write_fat

	mov	cx, es:deviceParameters.DP_BPB.BPB_SectorsPerFAT ;		;AC000;

;	$do					;an000;while sectors left
$$DO67:
		cmp	cx,00h			;an000;any sectors?
;		$leave	e			;an000;no
		JE $$EN67

		cmp	cx,40h
;		$if	a			;an000;yes
		JNA $$IF69
			push	cx		;an000;save cx
			mov	cx,40h
			push	ax		;an000;save ax
			call	write_disk	;an000;write it
			pop	ax		;an000;restore ax
			pop	cx		;an000;restore cx
			jc	Write_Exit	;an000;exit if fail
			mov	si,8000h
			call	seg_adj 	;an000;adjust segment
			mov	bx,si		;an000;new offset
			add	dx,40h
			sub	cx,40h
;		$else				;an000;< 64k
		JMP SHORT $$EN69
$$IF69:
			push	ax		;an000;save ax
			call	write_disk	;an000;write it
			pop	ax		;an000;restore ax
			xor	cx,cx		;an000;set cx to 0 - last read
;		$endif
$$EN69:
;	$enddo
	JMP SHORT $$DO67
$$EN67:

	Write_Exit:

	ret

write_fat	endp

;=========================================================================
; SEG_ADJ	:	This routine adjusts the segment:offset to prevent
;			address wrap.
;
;	Inputs	:	SI - Offset to adjust segment with
;			DS - Segment to be adjusted
;
;	Outputs :	SI - New offset
;			DS - Adjusted segment
;=========================================================================

procedure	seg_adj

	push	ax
	push	cx
	push	dx
	mov	ax,si				;an000;get offset
	mov	bx,0010h			;an000;16
	xor	dx,dx				;an000;clear dx
	div	bx				;an000;get para count
;	$if	c				;an000;overflow?
	JNC $$IF73
		adc	bx,0			;an000;pick it up
;	$endif					;an000;
$$IF73:
	mov	bx,ds				;an000;get seg
	add	bx,ax				;an000;adjust for paras
	mov	ds,bx				;an000;save new seg
	mov	si,dx				;an000;new offset
	pop	dx
	pop	cx
	pop	ax
	ret

seg_adj 	endp

;-------------------------------------------------------------------------------
;	format is done... so clean up the disk!
;
Done	proc	near


	call	OemDone
	return

Done	endp

;-------------------------------------------------------------------------------
;    CurrentLogicalSector:
;	Get the current logical sector number
;
;    Input:
;	current track = tracksPerDisk - tracksLeft
;	SectorsPerTrack
;
;    Output:
;	BX = logical sector number of the first sector in the track we
;	     just tried to format
;
CurrentLogicalSector proc near

	push	ax								;an000; dms;save regs
	push	bx								;an000; dms;
	push	dx								;an000; dms;

	mov	ax, tracksPerDisk
	sub	ax, tracksLeft
	xor	dx,dx								;an000; dms;clear dx
	mul	deviceParameters.DP_BPB.BPB_SectorsPerTrack
	mov	word ptr Relative_Sector_High,dx				;an000; dms;save high word of sector #
	mov	word ptr Relative_Sector_Low,ax 				;an000; dms;save low word of sector #

	pop	dx								;an000; dms;restore regs
	pop	bx								;an000; dms;
	pop	ax								;an000; dms;

	return

CurrentLogicalSector endp

;-------------------------------------------------------------------------------
;    PrintErrorAbort:
;	Print an error message and abort
;
;    Input:
;	dx - Pointer to error message string
;
PrintErrorAbort proc near

	push	dx
	call	crlf
	pop	dx
	call	PrintString

	jmp	fexit

PrintErrorAbort endp




;*****************************************************************************
;Routine name: Multiply_32_Bits
;*****************************************************************************
;
;Description: A real sleazy 32 bit x 16 bit multiply routine. Works by adding
;	      the 32 bit number to itself for each power of 2 contained in the
;	      16 bit number. Whenever a bit that is set in the multiplier (CX)
;	      gets shifted to the bit 0 spot, it means that that amount has
;	      been multiplied so far, and it should be added into the total
;	      value. Take the example CX = 12 (1100). Using the associative
;	      rule, this is the same as CX = 8+4 (1000 + 0100). The
;	      multiply is done on this principle - whenever a bit that is set
;	      is shifted down to the bit 0 location, the value in BX:AX is
;	      added to the running total in DI:SI. The multiply is continued
;	      until CX = 0. The routine will exit with CY set if overflow
;	      occurs.
;
;
;Called Procedures: None
;
;Change History: Created	7/23/87 	MT
;
;Input: BX:AX = 32 bit number to be multiplied
;	CX = 16 bit number to be multiplied. (Must be even number)
;
;Output: BX:AX = output.
;	 CY set if overflow
;
;Psuedocode
;----------
;
;	Point at ControlC_Handler routine
;	Set interrupt handler (INT 21h, AX=2523h)
;	ret
;*****************************************************************************

Public Multiply_32_Bits
Multiply_32_Bits proc				;				;AN000;

	push	di				;				;AN000;
	push	si				;				;AN000;
	xor	di,di				;Init result to zero
	xor	si,si				;
	cmp	cx,0				;Multiply by 0? 		;AN000;
;	$IF	NE				;Keep going if not		;AN000;
	JE $$IF75
;	   $DO					;This works by adding the result;AN000;
$$DO76:
	      test    cx,1			;Need to add in sum of this bit?;AN000;
;	      $IF     NZ			;Yes				;AN000;
	      JZ $$IF77
		 add	 si,ax			;Add in the total so far for	;AN000;
		 adc	 di,bx			; this bit multiplier (CY oflow);AN000;
;	      $ELSE				;Don't split multiplier         ;AN000;
	      JMP SHORT $$EN77
$$IF77:
		 clc				;Force non exit 		;AN000;
;	      $ENDIF				;				;AN000;
$$EN77:
;	   $LEAVE  C				;Leave on overflow		;AN000;
	   JC $$EN76
	      shr     cx,1			;See if need to multiply value	;AN000;
	      cmp     cx,0			;by 2				;AN000;
;	   $LEAVE  E				;Done if cx shifted down to zero;AN000;
	   JE $$EN76
	      add     ax,ax			;Each time cx is shifted, add	;AN000;
	      adc     bx,bx			;value to itself (Multiply * 2) ;AN000;
;	   $ENDDO  C				;CY set on overflow		;AN000;
	   JNC $$DO76
$$EN76:
;	   $IF	   NC				;If no overflow, add in DI:SI	;AN000;
	   JC $$IF83
	      mov     ax,si			; which contains the original	;AN000;
	      mov     bx,di			; value if odd, 0 if even. This ;AN000;
	      clc				;Set no overflow flag		;AN000;
;	   $ENDIF				;				;AN000;
$$IF83:
;	$ELSE					;
	JMP SHORT $$EN75
$$IF75:
	   xor	   ax,ax			;
	   xor	   bx,bx			;
;	$ENDIF					;Multiply by 0			;AN000;
$$EN75:
	pop	si				;				;AN000;
	pop	di				;				;AN000;
	ret					;				;AN000;

Multiply_32_Bits endp


;=========================================================================
; Divide_32_Bits	- This routine will perform 32bit division
;
;	Inputs	: SI:DI - value to be divided
;		  CX	- divisor
;
;	Outputs : SI:DI - result
;		  CX	- remainder
;=========================================================================

Procedure Divide_32_Bits							;an000; dms;

	push	ax								;an000; dms;save regs
	push	bx								;an000; dms;
	push	dx								;an000; dms;

	xor	dx,dx								;an000; dms;clear dx
	mov	ax,si								;an000; dms;get high word
	div	cx								;an000; dms;get high word result
	mov	si,ax								;an000; dms;save high word result

	mov	ax,di								;an000; dms;get low word
	div	cx								;an000; dms;get low word result
	mov	di,ax								;an000; dms;save low word result
	mov	cx,dx								;an000; dms;pick up remainder

	pop	dx								;an000; dms;restore regs
	pop	bx								;an000; dms;
	pop	ax								;an000; dms;

	ret									;an000; dms;

Divide_32_Bits	endp								;an000; dms;




;=========================================================================
; FAT_INIT:		This routine initializes the FAT based on the
;			number of paragraphs.
;
;
; input - fatspace
;	  fatspace+2
;	  paras_per_fat
;	  fat_init_value
; output - fat space is initialized
;
;=========================================================================
Public		Fat_Init
Fat_Init	proc	near

	push	es
	push	di
	push	ax
	push	bx
	push	cx
	mov	di, word ptr FatSpace+2 	;Get segment of Fat space	;AC000;
	mov	es,di				;				;AN000;
	mov	di, word ptr FatSpace		;				;AN000;
	mov	bx,Paras_Per_Fat		;an000;get number of paras
	mov	ax,fat_init_value		;an000;
	push	dx				;an000;save bx
	mov	dx,es				;an000;grab es into dx
;	$do
$$DO87:
		cmp	bx,0			;an000;do while bx not = 0
;		$leave	e			;an000;exit if 0
		JE $$EN87
		mov	cx,10h			;an000;word move of paragraph
		rep	stosb			;an000;move the data to FAT
		xor	di,di			;an000;offset always init to 0
		inc	dx			;an000;next paragraph
		mov	es,dx			;an000;put next para in es
		dec	bx			;an000;loop iteration counter
;	$enddo					;an000;
	JMP SHORT $$DO87
$$EN87:
	pop	dx				;an000;
	pop	cx				;an000;
	pop	bx				;an000;
	pop	ax				;an000;
	pop	di				;an000;
	pop	es				;an000;

	ret					;an000;

Fat_Init	endp				;an000;


;=========================================================================
; Ctrl_Break_Write	: This routine takes the control break request
;			  an returns.  In essence, it disables the CTRL-BREAK.
;			  This routine is used during the writing of the
;			  FAT, DIR, and SYSTEM.
;=========================================================================

Ctrl_Break_Write:				;ac010; dms;

	iret					;ac010; dms;return to caller


;=========================================================================
; Ctrl_Break_Save	: This routine gets the current vector of
;			  INT 23h and saves it in CTRL_BREAK_VECTOR.
;
;	Inputs	: none
;
;	Outputs : CTRL_BREAK_VECTOR - holds address of INT 23h routine
;=========================================================================

Ctrl_Break_Save 	proc	near		;ac010; dms;

	push	es				;ac010; dms;save es
	push	bx				;ac010; dms;save bx
	push	ax				;ac010; dms;save ax

	mov	ax,3523h			;ac010; dms;get CTRL-BREAK
						;	    interrupt vector
	int	21h				;ac010; dms;

	mov	word ptr Ctrl_Break_Vector,bx	;ac010; dms;get vector offset
	mov	word ptr Ctrl_Break_Vector+2,es ;ac010; dms;get vector segment

	pop	ax				;ac010; dms;restore ax
	pop	bx				;ac010; dms;restore bx
	pop	es				;ac010; dms;restore es

	ret					;ac010; dms;


Ctrl_Break_Save 	endp			;ac010; dms;


;=========================================================================
; Set_Ctrl_Break	: This routine sets the CTRL-Break vector to one
;			  defined by the user.
;
;	Inputs	: none
;
;	Outputs : CTRL_BREAK_VECTOR - holds address of INT 23h routine
;=========================================================================

Set_Ctrl_Break		proc	near		;ac010; dms;

	push	ds				;ac010; dms;save ds
	push	ax				;ac010; dms;save ax
	push	bx				;ac010; dms;save bx
	push	dx				;ac010; dms;save dx

	push	cs				;ac010; dms;swap cs with ds
	pop	ds				;an000; dms;point to code seg

	mov	dx,offset Ctrl_Break_Write	;ac010; dms;get interrupt vec.
	mov	ax,2523h			;ac010; dms;set CTRL-BREAK
						;	    interrupt vector
	int	21h				;ac010; dms;

	pop	dx				;ac010; dms;restore dx
	pop	bx				;ac010; dms;restore bx
	pop	ax				;ac010; dms;restore ax
	pop	ds				;ac010; dms;restore ds

	ret					;ac010; dms;


Set_Ctrl_Break		endp			;ac010; dms;


;=========================================================================
; Reset_Ctrl_Break	: This routine resets the CTRL-Break vector to that
;			  originally defined.
;
;	Inputs	: CTRL_BREAK_VECTOR - holds address of INT 23h routine
;
;	Outputs : none
;=========================================================================

Reset_Ctrl_Break	proc	near		;ac010; dms;

	push	ds				;ac010; dms;save ds
	push	ax				;ac010; dms;save ax
	push	bx				;ac010; dms;save bx
	push	dx				;ac010; dms;save ds

	mov	ax,word ptr Ctrl_Break_Vector+2 ;ac010; dms;get seg. of vector
	mov	bx,word ptr Ctrl_Break_Vector	;ac010; dms;get off. of vector
	mov	ds,ax				;ac010; dms;get seg.
	mov	dx,bx				;ac010; dms;get off.
	mov	ax,2523h			;ac010; dms;set CTRL-BREAK
						;	    interrupt vector
	int	21h				;ac010; dms;

	pop	dx				;ac010; dms;restore dx
	pop	bx				;ac010; dms;restore bx
	pop	ax				;ac010; dms;restore ax
	pop	ds				;ac010; dms;restore ds

	ret					;ac010; dms;


Reset_Ctrl_Break	endp			;ac010; dms;

;=========================================================================
; Get_Command_Path		: This routine finds the path where
;				  COMMAND.COM resides based on the
;				  environmental vector.  Once the
;				  path is found it is copied to
;				  CommandFile.
;
;	Inputs	: Exec_Block.Segment_Env - Segment of environmental vector
;		  Comspec_ID		 - "COMSPEC="
;
;	Outputs : CommandFile		 - Holds path to COMMAND.COM
;=========================================================================

Procedure	Get_Command_Path		;an011; dms;

	push	ds				;an011; dms;save ds
	push	es				;an011; dms;save es

	Set_Data_Segment			;an011; dms; DS,ES = Data
	call	Get_PSP_Parms			;an011; dms; gets PSP info.
	cld					;an011; dms; clear direction
	mov	ax,es:Environ_Segment		;an011; dms; get seg. of
						;	     environ. vector
	mov	ds,ax				;an011; dms; put it in DS
	assume	ds:nothing			;an011; dms;
	xor	si,si				;an011; dms; clear si
	mov	bx,si				;an011; dms; save si
	mov	di,offset Comspec_ID		;an011; dms; point to target
	mov	cx,127				;an011; dms; loop 127 times
;	$do					;an011; dms; while cx not 0
$$DO90:
						;	     and target not found
		cmp	cx,00h			;an011; dms; end of env.?
;		$leave	e			;an011; dms; yes
		JE $$EN90

		push	cx			;an011; dms; save cx
		mov	cx,0008h		;an011; dms; loop 8 times
		repe	cmpsb			;an011; dms; "COMSPEC=" ?
		pop	cx			;an011; dms; restore cx
;		$if	z			;an011; dms; yes
		JNZ $$IF92
			push	di		;an011; dms; save di
			mov	di,offset es:CommandFile   ;an011; dms
			lodsb			;an011; dms; priming read
			mov	dl,al		;an011; dms; prepare for capitalization
			call	Cap_Char	;an011; dms; capitalize character in DL
			cmp	dl,es:CommandFile  ;an011; dms;COMSPEC same as default drive?
;			$if	e		;an000; dms; yes
			JNE $$IF93
;				$do		;an011; dms; while AL not = 0
$$DO94:
				    cmp  al,00h ;an011; dms; at end?
;				    $leave  e	;an011; dms; yes
				    JE $$EN94
				    stosb	;an011; dms; save it
				    lodsb	;an011; dms; get character
;				$enddo
				JMP SHORT $$DO94
$$EN94:
;			$endif			;an011; dms;
$$IF93:
			pop	di		;an011; dms; restore di
			mov	cx,0ffffh	;an011; dms; flag target found
;		$endif				;an011; dms;
$$IF92:

		cmp	cx,0ffffh		;an011; dms; target found?
;		$leave	e			;an011; dms; yes
		JE $$EN90

		mov	di,offset Comspec_ID	;an011; dms; point to target
		mov	si,bx			;an011; dms; restore si
		inc	si			;an011; dms; point to next byte
		mov	bx,si			;an011; dms; save si

		dec	cx			;an011; dms; decrease counter
;	$enddo					;an011; dms;
	JMP SHORT $$DO90
$$EN90:

	pop	es				;an011; dms; restore es
	pop	ds				;an011; dms; restore ds

	ret					;an011; dms;

Get_Command_Path	endp			;an011; dms;


;
;****************************************************************************
; Get_PSP_Parms
;****************************************************************************
;
;
;
;

Procedure Get_PSP_Parms 			;				;AC000;

	Set_Data_Segment
	mov	ax,PSP_Segment			;Get segment of PSP		;AN000;
	mov	ds,ax				;  "  "    "  " 		;AN000;
						;     ;
	assume	ds:nothing
						;Setup segment of Environment string, get from PSP			    ;	  ;

	mov	ax,ds:PSP_Environ		;				;     ;
	mov	es:Environ_Segment,ax		;				;     ;
	Set_Data_Segment
	ret					;				;     ;


Get_PSP_Parms endp				;				;AN000;


;=========================================================================
; Command_Root	:	This routine sets up CommandFile so that the
;			COMMAND.COM will be written to the root.
;			It does this by copying at offset 3 of CommandFile
;			the literal COMMAND.COM.  This effectively
;			overrides the original path, but maintains the
;			drive letter that is to be written to.
;
;	Inputs	:	CommandFile - Holds full path to default COMMAND.COM
;	Outputs :	CommandFile - Holds modified path to new COMMAND.COM
;				      on target drive.
;=========================================================================

Procedure	Command_Root			;an011; dms;

	push	ds				;an011; dms; save ds
	push	es				;an011; dms; save es
	push	di				;an011; dms; save di
	push	si				;an011; dms; save si
	push	cx				;an011; dms; save cx
	Set_Data_Segment			;an011;

	mov	di,offset CommandFile+3 	;an011; dms; point to path
						;	     past drive spec
	mov	si,offset Command_Com		;an011; dms; holds the literal
						;	     COMMAND.COM
	mov	cx,000ch			;an011; dms; len. of literal
	rep	movsb				;an011; dms; move it

	pop	cx				;an011; dms; restore cx
	pop	si				;an011; dms; restore si
	pop	di				;an011; dms; restore di
	pop	es				;an011; dms; restore es
	pop	ds				;an011; dms; restore ds

	ret					;an011; dms;

Command_Root	endp				;an011; dms;


;=========================================================================
; Set_BPB_Info	:	When we have a FAT count of 0, we must calculate
;			certain parts of the BPB.  The following code
;			will do just that.
;
;	Inputs	: DeviceParameters
;
;	Outputs : BPB information
;=========================================================================

Procedure Set_BPB_Info								;an012; dms;calc new BPB

	Set_Data_Segment							;an012; dms;set up addressibility
	cmp	DeviceParameters.DP_BPB.BPB_NumberOfFats,00h			;an012; dms;see if we have 0 FATS specified
;	$if	e								;an012; dms;yes, 0 FATS specified
	JNE $$IF101
		call	Scan_Disk_Table 					;an012; dms;access disk table
		mov	bl,byte ptr ds:[si+8]					;an012; dms;get FAT type
		mov	cx,word ptr ds:[si+4]					;an012; dms;get sectors/cluster
		mov	dx,word ptr ds:[si+6]					;an012; dms;number of entries for the root DIR

		mov	DeviceParameters.DP_BPB.BPB_RootEntries,dx		;an012; dms;save root entries
		mov	DeviceParameters.DP_BPB.BPB_SectorsPerCluster,ch	;an012; dms;save sectors/cluster
		mov	DeviceParameters.DP_BPB.BPB_BytesPerSector,0200h	;an012; dms;save bytes/sector
		mov	DeviceParameters.DP_BPB.BPB_ReservedSectors,0001h	;an012; dms;save reserved sectors
		mov	DeviceParameters.DP_BPB.BPB_NumberOfFats,02h		;an012; dms;FAT count

		cmp	bl,FBIG 						;an012; dms;Big FAT?
;		$if	e							;an012; dms;yes
		JNE $$IF102
			call	Calc_Big_FAT					;an012; dms;calc big FAT info
;		$else								;an012; dms;
		JMP SHORT $$EN102
$$IF102:
			call	Calc_Small_FAT					;an012; dms;calc small FAT info
;		$endif								;an012; dms;
$$EN102:
;	$endif									;an012; dms;
$$IF101:

	ret									;an012; dms;

Set_BPB_Info	endp								;an012; dms;



;=========================================================================
; Scan_Disk_Table	: Scans the table containing information on
;			  the disk's attributes.  When it finds the
;			  applicable data, it returns a pointer in
;			  DS:SI for reference by the calling proc.
;
;	Inputs	: DiskTable - Contains data about disk types
;
;	Outputs : DS:SI     - Points to applicable disk data
;=========================================================================

Procedure Scan_Disk_Table							;an012; dms;

	cmp	DeviceParameters.DP_BPB.BPB_TotalSectors,00h			;an012; dms;small disk?
;	$if	ne								;an012; dms;yes
	JE $$IF106
		mov	dx,00h							;an012; dms;set high to 0
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_TotalSectors	;an012; dms;get sector count
;	$else									;an012; dms;
	JMP SHORT $$EN106
$$IF106:
		mov	dx,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+2]	;an012; dms;get high count
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+0]	;an012; dms;get low count
;	$endif									;an012; dms;
$$EN106:

	mov	si,offset DiskTable						;an012; dms;point to disk data
Scan:

	cmp	dx,word ptr ds:[si]						;an012; dms;below?
	jb	Scan_Disk_Table_Exit						;an012; dms;yes, exit
	ja	Scan_Next							;an012; dms;no, continue
	cmp	ax,word ptr ds:[si+2]						;an012; dms;below or equal?
	jbe	Scan_Disk_Table_Exit						;an012; dms;yes, exit

Scan_Next:

	add	si,5*2								;an012; dms;adjust pointer
	jmp	Scan								;an012; dms;continue scan

Scan_Disk_Table_Exit:

	ret									;an012; dms;

Scan_Disk_Table endp								;an012; dms;



;=========================================================================
; Calc_Big_FAT	:	Calculates the sectors per FAT for a 16 bit FAT.
;
;	Inputs	: DeviceParameters.DP_BPB.BPB_BigTotalSectors	or
;		  DeviceParameters.DP_BPB.BPB_TotalSectors
;
;	Outputs : DeviceParameters.DP_BPB.BPB_SectorsPerFat
;=========================================================================

Procedure Calc_Big_FAT								;an012; dms;

	cmp	DeviceParameters.DP_BPB.BPB_TotalSectors,00h			;an012; dms;small disk?
;	$if	ne								;an012; dms;yes
	JE $$IF109
		mov	dx,00h							;an012; dms;set high to 0
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_TotalSectors	;an012; dms;get sector count
;	$else									;an012; dms;
	JMP SHORT $$EN109
$$IF109:
		mov	dx,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+2]	;an012; dms;get high count
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+0]	;an012; dms;get low count
;	$endif									;an012; dms;
$$EN109:

	mov	cl,04h								;an012; dms;16 DIR entries per sector
	push	dx								;an012; dms;save total sectors (high)
	mov	dx,DeviceParameters.DP_BPB.BPB_RootEntries			;an012; dms;get root entry count
	shr	dx,cl								;an012; dms;divide by 16
	sub	ax,dx								;an012; dms;
	pop	dx								;an012; dms;restore dx
	sbb	dx,0								;an012; dms;
	sub	ax,1								;an012; dms;AX = T - R - D
	sbb	dx,0								;an012; dms;
	mov	bl,02h								;an012; dms;
	mov	bh,DeviceParameters.DP_BPB.BPB_SectorsPerCluster		;an012; dms;get sectors per cluster
	add	ax,bx								;an012; dms;AX = T-R-D+256*SPC+2
	adc	dx,0								;an012; dms;
	sub	ax,1								;an012; dms;AX = T-R-D+256*SPC+1
	sbb	dx,0								;an012; dms;
	div	bx								;an012; dms; sec/FAT = CEIL((TOTAL-DIR-RES)/
										;			    (256*SECPERCLUS+2)
	mov	word ptr DeviceParameters.DP_BPB.BPB_SectorsPerFAT,ax		;an012; dms;Sectors/cluster
	ret									;an012; dms;

Calc_Big_FAT	endp								;an012; dms;


;=========================================================================
; Calc_Small_FAT:	Calculates the sectors per FAT for a 12 bit FAT.
;
;	Inputs	: DeviceParameters.DP_BPB.BPB_BigTotalSectors	or
;		  DeviceParameters.DP_BPB.BPB_TotalSectors
;
;	Outputs : DeviceParameters.DP_BPB.BPB_SectorsPerFat
;=========================================================================

Procedure Calc_Small_FAT							;an012; dms;

	cmp	DeviceParameters.DP_BPB.BPB_TotalSectors,00h			;an012; dms;small disk?
;	$if	ne								;an012; dms;yes
	JE $$IF112
		mov	dx,00h							;an012; dms;set high to 0
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_TotalSectors	;an012; dms;get sector count
;	$else									;an012; dms;
	JMP SHORT $$EN112
$$IF112:
		mov	dx,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+2]	;an012; dms;get high count
		mov	ax,word ptr DeviceParameters.DP_BPB.BPB_BigTotalSectors[+0]	;an012; dms;get low count
;	$endif									;an012; dms;
$$EN112:

	xor	bx,bx								;an012; dms;clear bx
	mov	bl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster		;an012; dms;get sectors/cluster
	div	bx								;an012; dms;
; now multiply by 3/2
	mov	bx,3								;an012; dms;
	mul	bx								;an012; dms;div by log 2 of sectors/cluster
	mov	bx,2								;an012; dms;
	div	bx								;an012; dms;
	xor	dx,dx								;an012; dms;
; now divide by 512
	mov	bx,512								;an012; dms;
	div	bx								;an012; dms;
	inc	ax								;an012; dms;
; dx:ax contains number of FAT sectors necessary
	mov	DeviceParameters.DP_BPB.BPB_SectorsPerFAT,ax			;an012; dms;save sectors/FAT
	ret									;an012; dms;

Calc_Small_FAT	endp								;an012; dms;

;=========================================================================
; Get_Bad_Sector_Hard	: Determine the bad sector.
;
;	Inputs	: Head of failing track
;		  Cylinder of failing track
;		  Relative_Sector_Low		- 1st. sector in track
;		  Relative_Sector_High
;
;		  Cluster_Boundary_Adj_Factor	- The number of sectors
;						  that are to be read
;						  at one time.
;		  Cluster_Boundary_SPT_Count	- Used by Calc_Cluster_Boundary
;						  to track how many sectors
;						  have been read.
;		  Cluster_Boundary_Flag 	- True (Use cluster buffer)
;						- False (Use internal buffer)
;		  Cluster_Boundary_Buffer_Seg	- Segment of buffer
;
;	Outputs : Marked cluster as bad
;=========================================================================

Procedure Get_Bad_Sector_Hard							;an000; dms;

	push	cx								;an000; dms;save cx
	mov	cx,0001h							;an000; dms;set counter to start at 1
	mov	Cluster_Boundary_SPT_Count,00h					;an000; dms;clear sector counter
	mov	Cluster_Boundary_Adj_Factor,01h 				;an000; dms;default value
;	$do									;an000; dms;while sectors left
$$DO115:
		cmp	cx,DeviceParameters.DP_BPB.BPB_SectorsPerTrack		;an000; dms;at end?
;		$leave	a							;an000; dms;yes,exit
		JA $$EN115
		push	cx							;an000; dms;save cx

		cmp	Cluster_Boundary_Flag,True				;an000; dms;full buffer there?
;		$if	e							;an000; dms;yes
		JNE $$IF117
			call	Calc_Cluster_Boundary				;an000; dms;see if on boundary
			mov	ax,Cluster_Boundary_Buffer_Seg
			mov	word ptr RWPacket.TRWP_TransferAddress[0],0	;an000; dms;point to transfer area
			mov	word ptr RWPacket.TRWP_TransferAddress[2],ax	;an000; dms;
;		$else								;an000; dms;default to internal buffer
		JMP SHORT $$EN117
$$IF117:
			mov	word ptr RWPacket.TRWP_TransferAddress[0],offset RW_TRF_Area  ;an000; dms;point to transfer area
			mov	word ptr RWPacket.TRWP_TransferAddress[2],DS	;an000; dms;
;		$endif								;an000; dms;
$$EN117:

		call	Verify_Structure_Set_Up 				;an019; dms; set up verify vars

		mov	ax,(IOCTL shl 8) or GENERIC_IOCTL			;an000; dms;
		xor	bx,bx							;an000; dms;clear bx
		mov	bl,drive						;an000; dms;get drive
		inc	bl							;an000; dms;adjust it
		mov	cx,(IOC_DC shl 8) or READ_TRACK 			;an000; dms;read track
		lea	dx,RWPacket						;an000; dms;point to parms
		int	21h							;an000; dms;

		pop	cx							;an000; dms;restore cx

		push	cx							;an000; dms;save cx

;		$if	c							;an000; dms;an error occurred
		JNC $$IF120
			call	Calc_Cluster_Position				;an000; dms;determine which cluster
			call	BadClus 					;an000; dms;mark the cluster as bad
;		$endif								;an000; dms;
$$IF120:

		pop	cx							;an000; dms;restore cx

		add	cx,Cluster_Boundary_Adj_Factor				;an000; dms;adjust loop counter
		mov	ax,Cluster_Boundary_Adj_Factor				;an000; dms;get adjustment factor
		xor	dx,dx							;an000; dms;clear dx
		add	ax,Relative_Sector_Low					;an000; dms;add in low word
		adc	dx,Relative_Sector_High 				;an000; dms;pick up carry in high word
		mov	Relative_Sector_Low,ax					;an000; dms;save low word
		mov	Relative_Sector_High,dx 				;an000; dms;save high word


;	$enddo									;an000; dms;
	JMP SHORT $$DO115
$$EN115:

	pop	cx								;an000; dms;restore cx

	ret									;an000; dms;

Get_Bad_Sector_Hard	endp							;an000; dms;


;=========================================================================
; Verify_Structure_Set_Up	: Set up the fields for the Read IOCTL
;				  to verify the sectors in a failing
;				  track.  Also, it displays the
;				  message notifying the user of the
;				  sectors it is verifying.
;=========================================================================

Procedure	Verify_Structure_Set_Up 					;an019; dms;set up verify structure

	mov	RWPacket.TRWP_SpecialFunctions,00h				;an000; dms;reset special functions

	mov	ax,FormatPacket.FP_Head 					;an000; dms;get current head
	mov	RWPacket.TRWP_Head,ax						;an000; dms;get current head

	mov	ax,FormatPacket.FP_Cylinder					;an000; dms;get current cylinder
	mov	RWPacket.TRWP_Cylinder,ax					;an000; dms;get current cylinder

	dec	cx								;an000; dms;make sector 0 based
	mov	RWPacket.TRWP_FirstSector,cx					;an000; dms;get sector to read

	mov	ax,Cluster_Boundary_Adj_Factor					;an000; dms;get # of sectors to read
	mov	RWPacket.TRWP_SectorsToReadWrite,ax				;an000; dms;read only # sector(s)

	call	Calc_Cluster_Position						;an019; dms;determine cluster number
	mov	word ptr Msg_Allocation_Unit_Val[+2],dx 			;an019; dms;save high word of cluster
	mov	word ptr Msg_Allocation_Unit_Val[+0],ax 			;an019; dms;save low word of cluster
	message MsgVerify

	ret

Verify_Structure_Set_Up endp							;an019; dms;


;=========================================================================
; Get_Bad_Sector_Floppy : This routine marks an entire track as bad
;			  since it is a floppy disk.
;
;	Inputs	: Relative_Sector_Low	- first sector
;
;	Outputs : FAT marked with bad sectors
;=========================================================================

Procedure Get_Bad_Sector_Floppy 						;an000; dms;

	push	bx								;an000; dms;save regs
	push	cx								;an000; dms;

	mov	cx,DeviceParameters.DP_BPB.BPB_SectorsPerTrack			;an000; dms;get sectors/track
;	$do									;an000; dms;while sectors left
$$DO123:
		cmp	cx,00							;an000; dms;at end
;		$leave	e							;an000; dms;yes
		JE $$EN123
		push	bx							;an000; dms;save bx we destroy it
		push	cx							;an000; dms;save cx we destroy it
		call	Calc_Cluster_Position					;an000; dms;get cluster position
		call	BadClus 						;an000; dms;mark it as bad
		pop	cx							;an000; dms;restore regs
		pop	bx							;an000; dms;
		dec	cx							;an000; dms;decrease loop counter
		inc	Relative_Sector_Low					;an000; dms;next sector
;	$enddo									;an000; dms;
	JMP SHORT $$DO123
$$EN123:

	pop	cx								;an000; dms;restore regs
	pop	bx								;an000; dms;

	ret									;an000; dms;

Get_Bad_Sector_Floppy	endp							;an000; dms;


;=========================================================================
; Calc_Cluster_Position : This routine calculates which cluster the
;			  failing sector falls in.
;
;	Inputs	: Relative_Sector_High	- high word of sector position
;		  Relative_Sector_Low	- low word of sector position
;
;	Outputs : DX:AX - Cluster position
;=========================================================================
Procedure Calc_Cluster_Position 						;an000; dms;

	push	cx								;an000; dms;save regs
	push	di								;an000; dms;
	push	si								;an000; dms;

	xor	dx,dx								;an000; dms;clear high word
	mov	dx,word ptr Relative_Sector_High				;an000; dms;get the high sector word
	mov	ax,word ptr Relative_Sector_Low 				;an000; dms;get the low sector word
	sub	ax,StartSector							;an000; dms;get relative sector #
	sbb	dx,0								;an000; dms;pick up borrow

	mov	si,dx								;an000; dms;get high word
	mov	di,ax								;an000; dms;get low word
	xor	cx,cx								;an000; dms;clear cx
	mov	cl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster		;an000; dms;get sectors/cluster
	call	Divide_32_Bits							;an000; dms;32 bit division

	mov	dx,si								;an000; dms;get high word of result
	mov	ax,di								;an000; dms;get low word of result
	add	ax,2								;an000; dms;adjust for cluster bias
	adc	dx,0								;an000; dms;pick up carry

	pop	si								;an000; dms;restore regs
	pop	di								;an000; dms;
	pop	cx								;an000; dms;

	ret									;an000 ;dms;

Calc_Cluster_Position	endp							;an000; dms;


;=========================================================================
; Cap_Char	: This routine will capitalize the character passed in
;		  DL.
;
;	Inputs	: DL - Character to be capitalized
;
;	Outputs : DL - Capitalized character
;=========================================================================

Procedure Cap_Char								;an011; dms;

	push	ax								;an011; dms;save ax
	mov	ax,6520h							;an011; dms;capitalize character
	int	21h								;an011; dms;
	pop	ax								;an011; dms;restore ax
	ret									;an011; dms;

Cap_Char	endp								;an011; dms;

;=========================================================================
; Set_Disk_Access_On_Off: This routine will either turn access on or off
;			  to a disk depending on the contents of the
;			  buffer passed in DX.
;
;	Inputs	: DX - pointer to buffer
;
;=========================================================================

Procedure Set_Disk_Access_On_Off						;an014; dms;

	push	ax								;an014; dms;save regs
	push	bx								;an014; dms;
	push	cx								;an014; dms;
	push	dx								;an014; dms;

	xor	bx,bx								;an014; dms;clear bx
	mov	bl,Drive							;an014; dms;get driver number
	inc	bl								;an014; dms;make it 1 based
	call	IsRemovable							;an014; dms;see if removable media
;	$if	c								;an014; dms;not removable
	JNC $$IF126
		mov	ax,(IOCTL shl 8) or Generic_IOCTL			;an014; dms;generic ioctl
		xor	bx,bx							;an014; dms;clear bx
		mov	bl,Drive						;an014; dms;get drive letter
		inc	bl							;an014; dms;make it 1 based
		mov	cx,(RAWIO shl 8) or Set_Access_Flag			;an014; dms;allow access to disk
		int	21h							;an014; dms;
;	$endif									;an014; dms;
$$IF126:

	pop	dx								;an014; dms;restore regs
	pop	cx								;an014; dms;
	pop	bx								;an014; dms;
	pop	ax								;an014; dms;

	ret									;an014; dms;

Set_Disk_Access_On_Off	endp							;an014; dms;


;=========================================================================
; Get_Disk_Access	: This routine will determine the access state of
;			  the disk.
;
;	Inputs	: DX - pointer to buffer
;	Outputs : Disk_Access.DAC_Access_Flag - 0ffh signals access allowed
;						to the disk previously.
;
;=========================================================================

Procedure Get_Disk_Access							;an014; dms;

	push	ax								;an014; dms;save regs
	push	bx								;an014; dms;
	push	cx								;an014; dms;
	push	dx								;an014; dms;

	xor	bx,bx								;an014; dms;clear bx
	mov	bl,Drive							;an014; dms;get driver number
	inc	bl								;an014; dms;make it 1 based
	call	IsRemovable							;an014; dms;see if removable media
;	$if	c								;an014; dms;not removable
	JNC $$IF128
		mov	ax,(IOCTL shl 8) or Generic_IOCTL			;an014; dms;generic ioctl
		xor	bx,bx							;an014; dms;clear bx
		mov	bl,Drive						;an014; dms;get drive letter
		inc	bl							;an014; dms;make it 1 based
		mov	cx,(RAWIO shl 8) or Get_Access_Flag			;an014; dms;determine disk access
		lea	dx,Disk_Access						;an014; dms;point to parm list
		int	21h							;an014; dms;
		cmp	Disk_Access.DAC_Access_Flag,01h 			;an014; dms;access is currently allowed?
;		$if	e							;an014; dms;yes
		JNE $$IF129
			mov	Disk_Access.DAC_Access_Flag,0ffh		;an014; dms;signal access is currently allowed
;		$endif								;an014; dms;
$$IF129:
;	$endif									;an014; dms;
$$IF128:

	pop	dx								;an014; dms;restore regs
	pop	cx								;an014; dms;
	pop	bx								;an014; dms;
	pop	ax								;an014; dms;

	ret									;an014; dms;

Get_Disk_Access 	endp							;an014; dms;

;=========================================================================
; Calc_Cluster_Boundary : This routine will determine where, within a
;			  cluster, a sector resides.
;
;	Inputs	: Relative_Sector_Low		- Sector
;		  Relative_Sector_High
;
;	Outputs : Cluster_Boundary_Adj_Factor	- The number of sectors
;						  remaining in the cluster.
;		  Cluster_Boundary_SPT_Count	- The count of sectors
;						  having been accessed for
;						  a track.
;=========================================================================

Procedure Calc_Cluster_Boundary 						;an000; dms;

	push	ax								;an000; dms;save regs
	push	bx								;an000; dms;
	push	cx								;an000; dms;
	push	dx								;an000; dms;
	push	si								;an000; dms;
	push	di								;an000; dms;

	xor	dx,dx								;an000; dms;clear high word
	mov	dx,word ptr Relative_Sector_High				;an000; dms;get the high sector word
	mov	ax,word ptr Relative_Sector_Low 				;an000; dms;get the low sector word
	sub	ax,StartSector							;an000; dms;get relative sector #
	sbb	dx,0								;an000; dms;pick up borrow

	mov	si,dx								;an000; dms;get high word
	mov	di,ax								;an000; dms;get low word
	xor	cx,cx								;an000; dms;clear cx
	mov	cl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster		;an000; dms;get sectors/cluster
	call	Divide_32_Bits							;an000; dms;32 bit division

	or	cx,cx								;an000; dms;see if remainder exists
;	$if	nz								;an000; dms;remainder exists
	JZ $$IF132
		xor	bx,bx							;an021; dms;
		mov	bl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster	;an021; dms;get sectors/cluster
		sub	bx,cx							;an021; dms;get number of sectors to read
		mov	Cluster_Boundary_Adj_Factor,bx				;ac021; dms;remainder = sector count
;	$else									;an000; dms;no remainder
	JMP SHORT $$EN132
$$IF132:
		xor	bx,bx							;an000; dms;clear bx
		mov	bl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster	;an000; dms;get sectors/cluster
		mov	Cluster_Boundary_Adj_Factor,bx				;an000; dms;get sectors/cluster
;	$endif									;an000; dms;
$$EN132:

	mov	ax,Cluster_Boundary_SPT_Count					;an000; dms;get current sector count
	xor	dx,dx								;an000; dms;clear high word
	add	ax,Cluster_Boundary_Adj_Factor					;an000; dms;get next sector count
	cmp	ax,DeviceParameters.DP_BPB.BPB_SectorsPerTrack			;an000; dms;exceeded sectors/track?
;	$if	a								;an000; dms;yes
	JNA $$IF135
		mov	ax,DeviceParameters.DP_BPB.BPB_SectorsPerTrack		;an000; dms;only use difference
		sub	ax,Cluster_Boundary_SPT_Count				;an000; dms;get next sector count
		mov	Cluster_Boundary_Adj_Factor,ax				;an000; dms;
;	$endif									;an000; dms;
$$IF135:

	mov	ax,Cluster_Boundary_SPT_Count					;an000; dms;get sector count
	xor	dx,dx								;an000; dms;clear high word
	add	ax,Cluster_Boundary_Adj_Factor					;an000; dms;get new sector count
	mov	Cluster_Boundary_SPT_Count,ax					;an000; dms;save it

	pop	di								;an000; dms;restore regs
	pop	si								;an000; dms;
	pop	dx								;an000; dms;restore regs
	pop	cx								;an000; dms;
	pop	bx								;an000; dms;
	pop	ax								;an000; dms;

	ret									;an000; dms;

Calc_Cluster_Boundary	endp							;an000; dms;

;=========================================================================
; Cluster_Buffer_Allocate	: This routine will allocate a buffer
;				  based on a cluster's size.  If enough
;				  space does not exist, a cluster will
;				  be redefined to a smaller size for
;				  purposes of sector retries.
;
;	Inputs	: DeviceParameters.DP_BPB.BPB_BytesPerSector
;		  DeviceParameters.DP_BPB.BPB_SectorsPerCluster
;
;	Outputs : Cluster_Boundary_Flag 	- True (space available)
;						  False(not enough space)
;		  Cluster_Boundary_Buffer_Seg	- Pointer to buffer
;=========================================================================

Procedure Cluster_Buffer_Allocate						;an000; dms;

	push	ax								;an000; dms;save regs
	push	bx								;an000; dms;
	push	cx								;an000; dms;
	push	dx								;an000; dms;

	mov	ax,(Alloc shl 8)						;an000; dms;allocate memory
	mov	bx,0ffffh							;an000; dms;get available memory
	int	21h								;an000; dms;

	mov	ax,DeviceParameters.DP_BPB.BPB_BytesPerSector			;an000; dms;get bytes/sector
	xor	dx,dx								;an000; dms;clear high word
	xor	cx,cx								;an000; dms;clear cx
	mov	cl,DeviceParameters.DP_BPB.BPB_SectorsPerCluster		;an000; dms;get sector count
	mul	cx								;an000; dms;get total byte count
	mov	cl,4								;an000; dms;set up shift count
	shr	ax,cl								;an000; dms;divide by 16
	inc	ax								;an000; dms;round up

	cmp	bx,ax								;an000; dms;enough room
;	$if	a								;an000; dms;yes
	JNA $$IF137
		mov	bx,ax							;an000; dms;allocate needed memory
		mov	ax,(Alloc shl 8)					;an000; dms;
		int	21h							;an000; dms;
		mov	Cluster_Boundary_Buffer_Seg,ax				;an000; dms;save pointer to buffer
		mov	Cluster_Boundary_Flag,True				;an000; dms;signal space available
;	$else									;an000; dms;not enough room
	JMP SHORT $$EN137
$$IF137:
		mov	Cluster_Boundary_Flag,False				;an000; dms;signal not enough space
;	$endif									;an000; dms;
$$EN137:

	pop	dx								;an000; dms;restore regs
	pop	cx								;an000; dms;
	pop	bx								;an000; dms;
	pop	ax								;an000; dms;

	ret									;an000; dms;

Cluster_Buffer_Allocate endp							;an000; dms;


;=========================================================================
; Set_CDS_Off			- This routine disallows access to a
;				  disk if a format fails on a non-FAT
;				  formatted disk.
;
;=========================================================================

Procedure Set_CDS_Off								;an000; dms;

	push	ax								;an000; dms;save regs
	push	dx								;an000; dms;

	mov	ax,5f08h							;an000; dms;reset CDS
	mov	dl,Drive							;an000; dms;drive to reset
	int	21h								;an000; dms;

	pop	dx								;an000; dms;restore regs
	pop	ax								;an000; dms;

	ret									;an000; dms;

Set_CDS_Off	endp								;an000; dms;


;=========================================================================
; Format_Access_Wrap_Up 	- This routine determines whether or
;				  not access should be allowed to the
;				  disk based on the exit status of
;				  format.
;
;=========================================================================

Procedure Format_Access_Wrap_Up 						;an000; dms;

	cmp	Disk_Access.DAC_Access_Flag,0ffh				;an015; dms;access prev. allowed?
;	$if	ne								;an015; dms;no
	JE $$IF140
		cmp	ExitStatus,ExitOK					;an015; dms;good exit?
;	       $if     ne							;an015; dms;no
	       JE $$IF141
		       lea     dx,Disk_Access					;an015; dms;point to parm block
		       mov     Disk_Access.DAC_Access_Flag,00h			;an015; dms;signal no disk access
		       call    Set_Disk_Access_On_Off				;an015; dms;don't allow disk access
;	       $else								;an015; dms;bad exit
	       JMP SHORT $$EN141
$$IF141:
		       lea     dx,Disk_Access					;an015; dms;point to parm block
		       mov     Disk_Access.DAC_Access_Flag,01h			;an015; dms;signal disk access
		       call    Set_Disk_Access_On_Off				;an015; dms;allow disk access
;	       $endif								;an015; dms;
$$EN141:
;	$endif									;an015; dms;
$$IF140:

	cmp	FAT_Flag,No							;an012; dms;non-FAT format?
;	$if	e								;an012; dms;yes
	JNE $$IF145
		cmp	ExitStatus,ExitOK					;an012; dms;good exit?
;		$if	ne							;an012; dms;no
		JE $$IF146
			call	Set_CDS_Off					;an012; dms;disallow FAT access
;		$endif								;an012; dms;
$$IF146:
;	$endif									;an012; dms;
$$IF145:

	ret									;an000; dms;

Format_Access_Wrap_Up	endp							;an000; dms;

;=========================================================================
; BadClus_Address_Adjust	- This routine adjusts the segment and
;				  offset to provide addressibility into
;				  the FAT table.
;
;	Inputs	: bx	- high word to adjust segment for
;		  ax	- low word to adjust segment for
;		  cx	- segment to be adjusted
;
;	Outputs : cx	- new segment value
;		  ax	- new offset value
;=========================================================================

Procedure BadClus_Address_Adjust						;an000; dms;

	push	bx								;an000; save regs
	push	dx								;an000;
	push	di								;an000;
	push	si								;an000;

	mov	dx,cx								;an000; save segment value
	mov	si,bx								;an000; get high word for divide
	mov	di,ax								;an000; get low word for divide
	xor	cx,cx								;an000; clear cx
	mov	cl,Paragraph_Size						;an000; divide by 16
	call	Divide_32_Bits							;an000; perform division

	add	dx,di								;an000; adjust segment for result
	mov	ax,cx								;an000; pick up the remainder
	mov	cx,dx								;an000; pass back new segment

	pop	si								;an000; restore regs
	pop	di								;an000;
	pop	dx								;an000;
	pop	bx								;an000;

	ret									;an000; dms;

BadClus_Address_Adjust	endp							;an000; dms;



;=========================================================================
; NextTrack	: This routine determines the next track to be
;		  formatted.
;
;	Inputs	: TracksLeft		- # of tracks remaining
;		  Tracks_To_Format	- # of tracks to format in 1 call
;		  FP_Head		- disk head
;		  FP_Cylinder		- disk cylinder
;
;	Outputs : TracksLeft		- # of tracks remaining
;		  FP_Head		- disk head
;		  FP_Cylinder		- disk cylinder
;		  CY			- no tracks left to format
;		  NC			- tracks left to format
;=========================================================================

Procedure NextTrack								;an015; dms;


	cmp	TracksLeft,00							;an015; dms;end of format?
;	$if	e								;an015; dms;yes
	JNE $$IF149
		stc								;an015; dms;signal end of format
		mov	Format_End,True
;	$else
	JMP SHORT $$EN149
$$IF149:
		mov	cx,Tracks_To_Format					;an015; dms;get max track count for call
;		$do								;an015; dms;while tracks remain
$$DO151:
			cmp	TracksLeft,00					;an015; dms;end of format?
;			$leave	e						;an015; dms;yes
			JE $$EN151
			cmp	cx,00						;an015; dms;end of head/cyl. adjustment?
;			$leave	e						;an015; dms;yes
			JE $$EN151
			inc	FormatPacket.FP_Head				;an015; dms;next head
			mov	ax,FormatPacket.FP_Head 			;an015; dms;get head for comp
			cmp	ax,DeviceParameters.DP_BPB.BPB_Heads		;an015; dms;exceeded head count?
;			$if	e						;an015; dms;yes
			JNE $$IF154
				mov	FormatPacket.FP_Head,00 		;an015; dms;reinit. head
				inc	FormatPacket.FP_Cylinder		;an015; dms;next cylinder
;			$endif							;an015; dms;
$$IF154:

			dec	cx						;an015; dms;decrease counter
;		$enddo								;an015; dms;
		JMP SHORT $$DO151
$$EN151:

		clc								;an015; dms;clear CY
;	$endif									;an015; dms;
$$EN149:

	ret									;an015; dms;

NextTrack	endp								;an015; dms;

;=========================================================================
; Determine_Format_Type : This routine determines the type of format
;			  that is to occur based on the media type.
;
;	Inputs	: Dev_HardDisk		- Media type (harddisk)
;		  Multi_Track_Format	- EQU 02h
;		  Single_Track_Format	- EQU 00h
;
;	Outputs : FP_SpecialFunctions	- Set appropriately for single
;					  or multi track format
;=========================================================================

Procedure Determine_Format_Type 						;an015; dms;

	cmp	DeviceParameters.DP_DeviceType,Dev_HardDisk			;an015; dms;harddisk?
;	$if	e								;an015; dms;yes
	JNE $$IF158
		mov	FormatPacket.FP_SpecialFunctions,Multi_Track_Format	;an015; dms;set for multi track format
;	$else									;an015; dms;
	JMP SHORT $$EN158
$$IF158:
		mov	FormatPacket.FP_SpecialFunctions,Single_Track_Format	;an015; dms;set for single track format
;	$endif									;an015; dms;
$$EN158:
	ret									;an015; dms;

Determine_Format_Type	endp							;an015; dms;


;=========================================================================
; FormatTrack		: This routine performs multi track or single
;			  track formatting based on the state of the
;			  SpecialFunctions byte.
;
;	Inputs	: Tracks_To_Format	- # of tracks to format in 1 call
;		  FormatPacket		- Parms for IOCTL call
;
;	Outputs : NC			- formatted track(s)
;		  CY			- error in format
;		  AX			- extended error on CY
;=========================================================================

Procedure FormatTrack								;an015; dms;

	mov	ax,(IOCTL shl 8) or Generic_IOCTL				;an015; dms;Generic IOCTL
	mov	bl,drive							;an015; dms;get drive number
	inc	bl								;an015; dms;make it 1 based
	mov	cx,(RawIO shl 8) or Format_Track				;an015; dms;Format track(s)
	mov	dx,Tracks_To_Format						;an015; dms;get track count
	mov	FormatPacket.FP_TrackCount,dx					;an015; dms;put count in parms list
	lea	dx,FormatPacket 						;an015; dms;ptr to parms
	int	21h								;an015; dms;

;	$if	c								;an015; dms;error?
	JNC $$IF161
		mov	ah,59h							;an015; dms;get extended error
		xor	bx,bx							;an015; dms;clear bx
		int	21h							;an015; dms;
		stc								;an015; dms;flag an error
;	$endif									;an015; dms;
$$IF161:

	ret									;an015; dms;

FormatTrack	endp								;an015; dms;


;=========================================================================
; Determine_Track_Count 	: This routine determines the number of
;				  tracks to be formatted, based on whether
;				  or not we have a hard disk.  If we have
;				  a hard disk we can use multi-track
;				  format/verify, otherwise we use the
;				  single track format/verify.
;
;	Inputs	: Device_Type			- Media type
;
;	Outputs : Tracks_To_Format		- Max. number of tracks
;						  to be formatted in one
;						  call
;=========================================================================

Procedure Determine_Track_Count 						;an015; dms;

	cmp	DeviceParameters.DP_DeviceType,Dev_HardDisk			;an015; dms;harddisk?
;	$if	e								;an015; dms;yes
	JNE $$IF163
		call	Calc_Track_Count					;an015; dms;calc Tracks_To_Format
;	$else									;an015; dms;removable media
	JMP SHORT $$EN163
$$IF163:
		mov	Tracks_To_Format,0001h					;an015; dms;default to 1 track
;	$endif									;an015; dms;
$$EN163:

	ret									;an015; dms;

Determine_Track_Count	endp							;an015;dms;


;=========================================================================
; Calc_Track_Count	: This routine determines if we have enough tracks
;			  remaining to use the max. number of tracks
;			  in the FormatTrack call.  If the tracks remaining
;			  to be formatted is less that the max. number of
;			  allowable tracks for the call, the max. number
;			  of allowable tracks is set to the remaining track
;			  count.
;
;	Inputs	: Track_Count - Max. number of allowable tracks to be
;				formatted in 1 FormatTrack call.
;		  TracksLeft  - Track count of remaining tracks to be
;				formatted.
;
;	Outputs : Tracks_To_Format - Count of the tracks to be formatted
;				     in the next FormatTrack call.
;
;
;=========================================================================

Procedure Calc_Track_Count							;an015; dms;

	push	ax								;an015; dms;save regs
	mov	ax,Track_Count							;an015; dms;max bytes to format
	cmp	ax,TracksLeft							;an015; dms;too many tracks?
;	$if	a								;an015; dms;yes
	JNA $$IF166
		mov	ax,TracksLeft						;an015; dms;format remaining tracks
;	$endif									;an015; dms;
$$IF166:
	mov	Tracks_To_Format,ax						;an015; dms;save track count

	pop	ax								;an015; dms;

	ret									;an015; dms;

Calc_Track_Count	endp							;an015; dms;

;=========================================================================
; Calc_Max_Tracks_To_Format	: This routine determines the maximum
;				  number of tracks to format at 1 time.
;
;	Inputs	: DeviceParameters - SectorsPerTrack
;				     BytesPerSector
;
;	Outputs : Track_Count	   - Max. # of tracks to format in 1 call
;				     to FormatTrack
;=========================================================================

Procedure Calc_Max_Tracks_To_Format

	push	ax								;an015; dms;save regs
	push	bx								;an015; dms;
	push	dx								;an015; dms;

	mov	ax,DeviceParameters.DP_BPB.BPB_SectorsPerTrack			;an015; dms;get sectors per track
	mov	bx,DeviceParameters.DP_BPB.BPB_BytesPerSector			;an015; dms;get byte count
	xor	dx,dx								;an015; dms;clear dx
	mul	bx								;an015; dms;get total byte count
	mov	bx,ax								;an015; dms;put count in bx
	mov	ax,Max_Format_Size						;an015; dms;max bytes to format
	div	bx								;an015; dms;get track count
	mov	Track_Count,ax

	pop	dx								;an015; dms;
	pop	bx								;an015; dms;
	pop	ax								;an015; dms;

	ret

Calc_Max_Tracks_To_Format	endp





;=========================================================================
; Format_Track_Retry	: This routine performs the retry logic for
;			  the format multi-track.  It will retry each track
;			  until the failing track is encountered through
;			  a CY condition.
;
;	Inputs	: none
;
;	Outputs : CY - indicates either a failing track or end of format
;
;
;=========================================================================

Procedure Format_Track_Retry

	clc									;an022; dms; clear existing CY
	mov	Tracks_To_Format,1						;an015; dms; only format 1 track
;	$do									;an015; dms; while we have good tracks
$$DO168:
;		$leave	c							;an015; dms; exit on bad track
		JC $$EN168
		call	FormatTrack						;an015; dms; format the track
;		$if	nc							;an015; dms;error?
		JC $$IF170
			call	DisplayCurrentTrack				;an022; dms;adjust percent counter
			call	Adj_Track_Count
			call	NextTrack					;an015; dms;calc next track
;		$endif								;an015; dms;
$$IF170:
;	$enddo									;an015; dms;
	JMP SHORT $$DO168
$$EN168:

	ret									;an015; dms;

Format_Track_Retry	endp							;an015; dms;

;=========================================================================
; Format_Loop			: This routine provides the main template
;				  for the formatting of a disk.  A disk
;				  will be formatted as long as there are
;				  tracks remaining to be formatted.
;				  This routine can be exited on a carry
;				  condition; i.e., bad track, last track, etc.
;
;	Inputs	: none
;
;	Outputs : CY - Set on exit from this routine
;		  AX - Possible error condition code
;=========================================================================

Procedure Format_Loop								;an015; dms;

	clc									;an015; dms;initialize to NC
;	$do									;an015; dms;while NC
$$DO173:
;		$leave	c							;an015; dms;exit on CY
		JC $$EN173
		call	Calc_Current_Head_Cyl					;an015; dms;head and cylinder calc.
		call	Determine_Format_Type					;an015; dms;floppy/hard media?
		call	Determine_Track_Count					;an015; dms;how many tracks?
		call	FormatTrack						;an015; dms;format track(s)
;		$if	c							;an015; dms;formattrack failed
		JNC $$IF175
			pushf							;an015; dms;save flags
			cmp	DeviceParameters.DP_DeviceType,Dev_HardDisk	;an015; dms;harddisk?
;			$if	e						;an015; dms;yes
			JNE $$IF176
				popf						;an015; dms;restore flags
				call	Format_Track_Retry			;an015; dms;find failing track
;			$else							;an015; dms;
			JMP SHORT $$EN176
$$IF176:
				popf						;an015; dms;restore flags
;			$endif							;an015; dms;
$$EN176:
;		$endif								;an015; dms;
$$IF175:

;		$if	c							;an015; dms;format error?
		JNC $$IF180
			pushf							;an015; dms;yes - save flags
			push	ax						;an015; dms;save return code
			call	CheckRealErrors 				;an015; dms;check error type
;			$if	nc						;an015; dms;if non-fatal
			JC $$IF181
				call	DisplayCurrentTrack			;an015; dms;display % formatted
;			$endif							;an015; dms;
$$IF181:
			pop	ax						;an015; dms;restore regs
			popf							;an015; dms;
;		$endif								;an015; dms;
$$IF180:

;		$leave	c							;an015; dms;exit on CY
		JC $$EN173

		call	DisplayCurrentTrack					;an015; dms;tell how much formatted
		call	Adj_Track_Count 					;an015; dms;decrease track counter
		call	NextTrack						;an015; dms;adjust head and cylinder
;	$enddo									;an015; dms;
	JMP SHORT $$DO173
$$EN173:
	ret									;an015; dms;

Format_Loop	endp								;an015; dms;

;=========================================================================
; Adj_Track_Count	: This routine adjusts the track count by the
;			  number of tracks that have been formatted
;			  in one FormatTrack call.
;
;	Inputs	: TracksLeft	- # of tracks remaining to be formatted
;		  Tracks_To_Format - Tracks formatted in 1 call
;
;	Outputs : TracksLeft	- # of tracks remaining to be formatted
;=========================================================================

Procedure Adj_Track_Count							;an015; dms;

	push	ax								;an015; dms; save regs
	mov	ax,TracksLeft							;an015; dms; get tracks remaining
	sub	ax,Tracks_To_Format						;an015; dms; subtract amount formatted
	mov	TracksLeft,ax							;an015; dms; save new tracks remaining value
	pop	ax								;an015; dms; restore regs
	ret									;an015; dms;

Adj_Track_Count endp								;an015; dms;

;=========================================================================
; Prompt_User_For_Disk		: This routine prompts the user for the
;				  disk to be formatted.  An appropriate
;				  message is chosen based on the type
;				  of switch entered.  If the /SELECT
;				  switch is entered, the disk prompt is
;				  issued through the INT 2fh services
;				  provided by SELECT.
;
;	Inputs	: Switchmap	- Switches chosen for format
;
;	Outputs : Message printed as appropriate.
;=========================================================================

Procedure Prompt_User_For_Disk							;an016;dms;

	push	ax								;an016;dms;save ax
	test	switchmap, (SWITCH_Backup or SWITCH_Select or SWITCH_AUTOTEST) ;Suppress prompt?	       ;AC000;
;	$IF	Z				;					      ;AC000;
	JNZ $$IF186
	   call    DSKPRM			; prompt user for disk
;	$ENDIF					;					      ;AC000;
$$IF186:

	test	switchmap, (Switch_Select)					;an016;dms;/SELECT requested?
;	$if	nz								;an016;dms;yes
	JZ $$IF188
		mov	al, drive						;an016;dms;get drive to access for format
		call	AccessDisk						;an016;dms;access the disk
		mov	ax,Select_Disk_Message					;an016;dms;display disk prompt
		int	2fh							;an016;dms;  through INT 2fh services
;	$endif									;an016;dms;
$$IF188:
	pop	ax								;an016;dms;restore ax

	ret									;an016;dms;

Prompt_User_For_Disk	endp							;an016;dms;


code	ends
	END	START