summaryrefslogtreecommitdiff
path: root/src/fdc.c
blob: f3b99bf321b2af56b5e9b4b615eeb76e1d857fe4 (plain)
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
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
/*
  Hatari - fdc.c

  This file is distributed under the GNU General Public License, version 2
  or at your option any later version. Read the file gpl.txt for details.

  Floppy Disk Controller(FDC) emulation.
  All commands are emulated with good timings estimation, as many programs
  (demo or cracked games) rely on accurate FDC timings and DMA transfer by blocks
  of 16 bytes.
  The behaviour of all FDC's registers matches the official docs and should not
  cause programs to fail when accessing the FDC (especially for Status Register).
  As Hatari only handles ST/MSA disk images that only support 512 bytes sectors as
  well as a fixed number of sectors per track, a few parts of the FDC emulation are
  simplified and would need to be changed to handle more complex disk images (Pasti).
*/

const char FDC_fileid[] = "Hatari fdc.c";

#include <inttypes.h>

#include "main.h"
#include "configuration.h"
#include "fdc.h"
#include "hdc.h"
#include "floppy.h"
#include "floppy_ipf.h"
#include "floppy_stx.h"
#include "ioMem.h"
#include "log.h"
#include "m68000.h"
#include "memorySnapShot.h"
#include "mfp.h"
#include "psg.h"
#include "stMemory.h"
#include "video.h"
#include "clocks_timings.h"
#include "utils.h"
#include "statusbar.h"


/*
  Floppy Disk Controller

Programmable Sound Generator (YM-2149)

  0xff8800(even byte)  - PSG Register Data (Read, used for parallel port)
            - PSG Register Select (Write)

  Write to bits 0-3 to select PSG register to use(then write data to 0xfff8802)
    Value    Register

    0000    Channel A Fine Tune
    0001    Channel A Coarse Tune
    0010    Channel B Fine Tune
    0011    Channel B Coarse Tune
    0100    Channel C Fine Tune
    0101    Channel C Coarse Tune
    0110    Noise Generator Control
    0111    Mixer Control - I/O enable
    1000    Channel A Amplitude
    1001    Channel B Amplitude
    1010    Channel C Amplitude
    1011    Envelope Period Fine Tune
    1100    Envelope Period Coarse Tune
    1101    Envelope Shape
    1110    I/O Port A Select (Write only)
    1111    I/O Port B Select

  0xfff8802(even byte)  - Bits according to 0xff8800 Register select

  1110(Register 14) - I/O Port A
    Bit 0 - Floppy side 0/1
    Bit 1 - Floppy drive 0 select
    Bit 2 - Floppy drive 1 select
    Bit 3 - RS232 Ready to send (RTS)
    Bit 4 - RS232 Data Terminal Ready (DTR)
    Bit 5 - Centronics Strobe
    Bit 6 - General Purpose Output
    Bit 7 - Reserved

ACSI DMA and Floppy Disk Controller(FDC)
  0xff8604 - information from file '1772.info.txt, by David Gahris' (register r0)
    Word access only, but only lower byte (ff8605) is used
  (write) - Disk controller
    Set DMA sector count if ff8606 bit 4 == 1
    Set FDC's internal registers depending on bit 1/2 of ff8606 if bit 4 == 0
  (read) - Disk controller status
    Bit 0 - Busy.  This bit is 1 when the 177x is busy.  This bit is 0 when the 177x is free for CPU commands.
    Bit 1 - Index / Data Request.  On Type I commands, this bit is high during the index pulse that occurs once
      per disk rotation.  This bit is low at all times other than the index pulse.  For Type II and III commands,
      Bit 1 high signals the CPU to handle the data register in order to maintain a continuous flow of data.
      Bit 1 is high when the data register is full during a read or when the data register is empty during a write.
      "Worst case service time" for Data Request is 23.5 cycles.
    Bit 2 - Track Zero / Lost Data.  After Type I commands, this bit is 0 if the mechanism is at track zero.
      This bit is 1 if the head is not at track zero.  After Type II or III commands, this bit is 1 if the
      CPU did not respond to Data Request (Status bit 1) in time for the 177x to maintain a continuous data flow.
      This bit is 0 if the CPU responded promptly to Data Request.
      NOTE : on ST, Lost Data is never set because the DMA always handles the data request signal.
    Bit 3 - CRC Error.  This bit is high if a sector CRC on disk does not match the CRC which the 177x
      computed from the data.  The CRC polynomial is x^16+x^12+x^5+1.  If the stored CRC matches the newly
      calculated CRC, the CRC Error bit is low.  If this bit and the Record Not Found bit are set, the error
      was in an ID field.  If this bit is set but Record Not Found is clear, the error was in a data field.
    Bit 4 - Record Not Found.  This bit is set if the 177x cannot find the track, sector, or side which
      the CPU requested.  Otherwise, this bit is clear.
    Bit 5 - Spin-up / Record Type.  For Type I commands, this bit is low during the 6-revolution motor
      spin-up time.  This bit is high after spin-up.  For Type II and Type III commands, Bit 5 low
      indicates a normal data mark.  Bit 5 high indicates a deleted data mark.
    Bit 6 - Write Protect.  This bit is not used during reads.  During writes, this bit is high when the disk is write protected.
      After a type I command, this bit is constantly updated an give the current value of the WPT signal.
    Bit 7 - Motor On.  This bit is high when the drive motor is on, and low when the motor is off.

  0xff8606 - DMA Status(read), DMA Mode Control(write) - NOTE bits 0,9-15 are not used
    Bit 1 - FDC Pin A0 (See below)
    Bit 2 - FDC Pin A1
    Bit 3 - FDC/HDC Register Select
    Bit 4 - FDC/Sector count select
    Bit 5 - Reserved
    Bit 6 - Enable/Disable DMA
    Bit 7 - HDC/FDC
    Bit 8 - Read/Write

    A1  A0    Read        Write(bit 8==1)
    0  0    Status        Command
    0  1    Track Register    Track Register
    1  0    Sector Register    Sector Register
    1  1    Data Register    Data Register

  0xff860e - DD/HD mode selection - NOTE bits 2-15 are not used
    This register is only available on MegaSTE, TT and Falcon
    Bit 0 - FDC Frequency  0=8 MHz  1=16 MHz
    Bit 1 - Density  0=DD  1=HD

  0xff9200 - DIP switches setting - NOTE bits 0-7 are used by joypads on STE and Falcon
    Bit 8-15 of this register are only used on MegaSTE, TT and Falcon and reflect
      the state of the 8 DIP switches (or soldering) on the motherboard
    TT and Falcon were produced with HD drives. But MegaSTE were first produced with
      DD drives then later with HD drives
    Bit 8-13 - Not used or machine dependent
    Bit 14   - Floppy Drive model    0=HD drive  1=DD drive
    Bit 15   - Disable DMA sound     0=disable   1=don't disable

  NOTE [NP] : The DMA is connected to the FDC and its Data Register, each time a DRQ
  is made by the FDC, it's handled by the DMA through its internal 16 bytes buffer.
  This means that in the case of the Atari ST the LOST_DATA bit will never be set
  in the Status Register (but data can be lost if FDC_DMA.SectorCount=0 as there
  will be no transfer between DMA and RAM).
  So, "read sector" and "write sector" type II command will never set LOST_DATA, but
  strangely on a real STF the "read track" type III command will set LOST_DATA when
  it succeeds (maybe it depends on the size of the track ?)
  (eg Super Monaco GP on Superior 65 : loader fails if LOST_DATA is set when
  there're not enough DMA sectors to transfer bytes with read sectors)

  NOTE [NP] : All commands that require to read data from the floppy (verify sequence,
  searching next sector id, ...) will not fail if the drive is OFF or empty. They will
  wait forever until the drive is enabled again or a floppy is inserted ; at this point
  the command will complete as usual, even after several seconds/minutes of delay.

  NOTE [NP] : Although the doc says a new type I/II/III command can't be started while
  the busy bit is set, it's in fact possible to do it under certain conditions. As seen
  in the loader of 'The Overdrive Demos' by Phalanx, the 'restore' command should be
  replaced by a 'seek' command when it occurs in less than 900 cycles.
  A possible explanation from this could be seen in the WD1772's documentation, where
  the specific type I command is in fact checked after the 'prepare + spinup' sequence
  in the state machine diagram.
  Similarly, we can guess that a type II command can be replaced by another type II as long
  as the 'prepare + spinup + head settle' sequence is not over (this was not tested on real HW)

  NOTE [NP] : As verified on a real STF, when reading DMA status at $ff8606 or DMA sector
  count at $ff8604, the unused bits are not set to 0, but they contain the value from the latest
  read/write made at $ff8604 when accessing FDC or HDC registers. Moreover, it's not possible to
  read DMA sector count, so we return the lowest 8 bits from the latest access at $ff8604.


  Cycles and wait states :
  ------------------------
  As verified on a real STF, reading or writing to $ff8604 or $ff8606 can add some 4 cycles
  wait state.
	lea	$ffff8604,a3
	lea	$ffff8606,a4

	move.w  (a4),d0		; dma status			motorola=8	stf=8

	move.w  #$90,(a4)	; dma ctrl sector count		motorola=12	stf=12+4
	move.w  (a3),d0		; read sector count / fdc reg	motorola=8	stf=8
	move.w  #1,(a3)		; write sector count		motorola=12	stf=12+4

	move.w  #$80,(a4)	; dma ctrl status/cmd reg	motorola=12	stf=12+4
	move.w  (a3),d0		; read fdc reg			motorola=8	stf=8+4
	move.w  #$d0,(a3)	; write fdc reg			motorola=12	stf=12+4

  -> All accesses take 4 extra cycles, except reading DMA status and reading DMA sector count
  (which can't really be read, see note above)


  Detecting disk changes :
  ------------------------
  3'1/2 floppy drives include a 'DSKCHG' signal on pin 34 to detect when a disk was changed.
  Unfortunately on ST, this signal is not connected. Nevertheless, it's possible to detect
  a disk was inserted or ejected by looking at the 'WPT' signal which tells if a disk is write
  protected or not (but this method has some limitations and doesn't work in all cases).

  The following values of the WPT signal were measured with a custom program when ejecting/inserting
  a floppy (tested on a 520 STF with a single sided drive and with a double sided drive) :
    - floppy with write protection OFF (write possible), WPT=0 :
	eject   start=0 -> end=1
	insert  start=1 -> end=0
    - floppy with write protection ON (write not possible), WPT=1 :
	eject   start=1 -> end=1
	insert  start=1 -> end=1

  As can be seen, when a disk is write protected (WPT=1), it is not possible to detect the
  transition between inserting and ejecting, WPT will always be 1.

  The TOS monitors the changes on the WPT signal to determine if a floppy was ejected or inserted.
  On TOS 1.02fr, the code is located between $fc1bc4 and $fc1ebc. Every 8 VBL, one floppy drive is checked
  to see if the WPT signal changed. When 1 drive is connected, this means a floppy change should keep the
  WPT signal during at least 8 VBLs. When 2 drive are connected, each drive is checked every 16 VBLs, so
  the WPT signal should be kept for at least 16 VBLs.

  During these transition phases between "ejected" and "inserted", we force the WPT signal to 1,
  depending on which transition we're emulating (see Floppy_DriveTransitionUpdateState()) :
    - Ejecting : WPT will be X, then 1
    - Inserting : WPT will be 1, then X

  NOTE : the TT machines have support for "real" disk change by the mean of the Disk Change (DC)
  signal as output on the pin 34 of the internal floppy drive. The DC signal is then inverted
  and connected to GPIP4 on the TT MFP. See FDC_Drive_Get_DC_signal for more details.
  The DC signal is only available for the internal floppy drive, not the external one.
  Note also that TOS 3 (for TT) doesn't use this signal, it only uses the above method
  monitoring the write protect signal.

*/

/*-----------------------------------------------------------------------*/

/* Status register */
#define	FDC_STR_BIT_BUSY			0x01
#define	FDC_STR_BIT_INDEX			0x02		/* type I */
#define	FDC_STR_BIT_DRQ				0x02		/* type II and III */
#define	FDC_STR_BIT_TR00			0x04		/* type I */
#define	FDC_STR_BIT_LOST_DATA			0x04		/* type II and III */
#define	FDC_STR_BIT_CRC_ERROR			0x08
#define	FDC_STR_BIT_RNF				0x10
#define	FDC_STR_BIT_SPIN_UP			0x20		/* type I */
#define	FDC_STR_BIT_RECORD_TYPE			0x20		/* type II and III */
#define	FDC_STR_BIT_WPRT			0x40
#define	FDC_STR_BIT_MOTOR_ON			0x80



/* FDC Emulation commands used in FDC.Command */
enum
{
	FDCEMU_CMD_NULL = 0,
	/* Type I */
	FDCEMU_CMD_RESTORE,
	FDCEMU_CMD_SEEK,
	FDCEMU_CMD_STEP,					/* Also used for STEP IN and STEP OUT */
	/* Type II */
	FDCEMU_CMD_READSECTORS,
	FDCEMU_CMD_WRITESECTORS,
	/* Type III */
	FDCEMU_CMD_READADDRESS,
	FDCEMU_CMD_READTRACK,
	FDCEMU_CMD_WRITETRACK,

	/* Other fake commands used internally */
	FDCEMU_CMD_MOTOR_STOP
};


/* FDC Emulation commands' sub-states used in FDC.CommandState */
enum
{
	FDCEMU_RUN_NULL = 0,

	/* Restore */
	FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO,
	FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_SPIN_UP,
	FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_MOTOR_ON,
	FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_LOOP,
	FDCEMU_RUN_RESTORE_VERIFY,
	FDCEMU_RUN_RESTORE_VERIFY_HEAD_OK,
	FDCEMU_RUN_RESTORE_VERIFY_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_RESTORE_VERIFY_CHECK_SECTOR_HEADER,
	FDCEMU_RUN_RESTORE_COMPLETE,
	/* Seek */
	FDCEMU_RUN_SEEK_TOTRACK,
	FDCEMU_RUN_SEEK_TOTRACK_SPIN_UP,
	FDCEMU_RUN_SEEK_TOTRACK_MOTOR_ON,
	FDCEMU_RUN_SEEK_VERIFY,
	FDCEMU_RUN_SEEK_VERIFY_HEAD_OK,
	FDCEMU_RUN_SEEK_VERIFY_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_SEEK_VERIFY_CHECK_SECTOR_HEADER,
	FDCEMU_RUN_SEEK_COMPLETE,
	/* Step / Step In / Step Out */
	FDCEMU_RUN_STEP_ONCE,
	FDCEMU_RUN_STEP_ONCE_SPIN_UP,
	FDCEMU_RUN_STEP_ONCE_MOTOR_ON,
	FDCEMU_RUN_STEP_VERIFY,
	FDCEMU_RUN_STEP_VERIFY_HEAD_OK,
	FDCEMU_RUN_STEP_VERIFY_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_STEP_VERIFY_CHECK_SECTOR_HEADER,
	FDCEMU_RUN_STEP_COMPLETE,
	/* Read Sector */
	FDCEMU_RUN_READSECTORS_READDATA,
	FDCEMU_RUN_READSECTORS_READDATA_SPIN_UP,
	FDCEMU_RUN_READSECTORS_READDATA_HEAD_LOAD,
	FDCEMU_RUN_READSECTORS_READDATA_MOTOR_ON,
	FDCEMU_RUN_READSECTORS_READDATA_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_READSECTORS_READDATA_CHECK_SECTOR_HEADER,
	FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_START,
	FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_LOOP,
	FDCEMU_RUN_READSECTORS_CRC,
	FDCEMU_RUN_READSECTORS_MULTI,
	FDCEMU_RUN_READSECTORS_RNF,
	FDCEMU_RUN_READSECTORS_COMPLETE,
	/* Write Sector */
	FDCEMU_RUN_WRITESECTORS_WRITEDATA,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_SPIN_UP,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_HEAD_LOAD,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_MOTOR_ON,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_CHECK_SECTOR_HEADER,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_START,
	FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_LOOP,
	FDCEMU_RUN_WRITESECTORS_CRC,
	FDCEMU_RUN_WRITESECTORS_MULTI,
	FDCEMU_RUN_WRITESECTORS_RNF,
	FDCEMU_RUN_WRITESECTORS_COMPLETE,
	/* Read Address */
	FDCEMU_RUN_READADDRESS,
	FDCEMU_RUN_READADDRESS_SPIN_UP,
	FDCEMU_RUN_READADDRESS_HEAD_LOAD,
	FDCEMU_RUN_READADDRESS_MOTOR_ON,
	FDCEMU_RUN_READADDRESS_NEXT_SECTOR_HEADER,
	FDCEMU_RUN_READADDRESS_TRANSFER_START,
	FDCEMU_RUN_READADDRESS_TRANSFER_LOOP,
	FDCEMU_RUN_READADDRESS_RNF,
	FDCEMU_RUN_READADDRESS_COMPLETE,
	/* Read Track */
	FDCEMU_RUN_READTRACK,
	FDCEMU_RUN_READTRACK_SPIN_UP,
	FDCEMU_RUN_READTRACK_HEAD_LOAD,
	FDCEMU_RUN_READTRACK_MOTOR_ON,
	FDCEMU_RUN_READTRACK_INDEX,
	FDCEMU_RUN_READTRACK_TRANSFER_LOOP,
	FDCEMU_RUN_READTRACK_COMPLETE,
	/* Write Track */
	FDCEMU_RUN_WRITETRACK,
	FDCEMU_RUN_WRITETRACK_SPIN_UP,
	FDCEMU_RUN_WRITETRACK_HEAD_LOAD,
	FDCEMU_RUN_WRITETRACK_MOTOR_ON,
	FDCEMU_RUN_WRITETRACK_INDEX,
	FDCEMU_RUN_WRITETRACK_TRANSFER_LOOP,
	FDCEMU_RUN_WRITETRACK_COMPLETE,

	/*  Motor Stop */
	FDCEMU_RUN_MOTOR_STOP,
	FDCEMU_RUN_MOTOR_STOP_WAIT,
	FDCEMU_RUN_MOTOR_STOP_COMPLETE
};



/*
 * Standard hardware values for the FDC. This should allow to get very good timings' emulation
 * when dealing with non protected disks that still require a correct speed (MSA or ST images)
 *
 * - WD1772's datasheet is based on a reference clock of 8 MHz, so delays expressed in milli-seconds
 *   will be slightly different for the Atari ST, whose FDC's clock is around 8.021247 MHz (but this is
 *   not really noticeable in practice, less than 0.3 %)
 * - DD MFM encoding defines a standard signal of 4 micro sec per bit (a possible variation of +/- 10 %
 *   should still be possible). This means the WD1772 will read/write at 250 kbits/sec.
 *   Taking 4 us per bit means 32 us for a full byte, and with a 8 MHz clock, 256 cycles per byte.
 * - The floppy drives used in the Atari ST are spinning at 300 RPM. Variations are possible, as long
 *   as it keeps the duration of an MFM bit in the required 4 us +/- 10 % (in practice, ST drives are often
 *   at 299-301 RPM)
 * - When FDC runs at 8 MHz, the 250 kbits/s and 300 RPM give 6250 bytes for a standard track
 * - When FDC runs at 8.021247 MHz (Atari ST), the 250.664 kbit/s and 300 RPM give 6267 bytes per track
 *
 * Notes on timings required for precise emulation :
 * For a standard floppy recorded with a constant speed, the FDC will take 32 microsec
 * to read/write 1 byte on the floppy. On STF with a 8 MHz CPU clock, this means one byte can be
 * transferred every 256 cpu cycles. So, to get some correct timings as required by some games' protection
 * we must update the emulated FDC's state every 256 cycles (it could be less frequently and still work,
 * due to the 16 bytes DMA FIFO that will transfer data only 16 bytes at a time, every 256*16=4096 cycles)
 */


#define	FDC_CLOCK_STANDARD			(8000000.L)	/* In the WD1772's datasheet, all timings are related to a reference clock of 8 MHz */
#define FDC_DELAY_CYCLE_MFM_BYTE		( 4 * 8 * 8 )	/* 4 us per bit, 8 bits per byte, 8 MHz clock -> 256 cycles */
#define	FDC_BITRATE_STANDARD			250000		/* read/write speed of the WD1772 in bits per sec */
#define	FDC_RPM_STANDARD			300		/* 300 RPM or 5 spins per sec */
//#define	FDC_TRACK_BYTES_STANDARD		( ( FDC_BITRATE_STANDARD / 8 ) / ( FDC_RPM_STANDARD / 60 ) )	/* 6250 bytes */
#define FDC_TRACK_BYTES_STANDARD	6268

#define FDC_TRANSFER_BYTES_US( n )		(  ( n ) * 8 * 1000000.L / FDC_BITRATE_STANDARD )	/* micro sec to read/write 'n' bytes in the WD1772 */

#define	FDC_DELAY_IP_SPIN_UP			6		/* 6 index pulses to reach correct speed during spin up */
#define	FDC_DELAY_IP_MOTOR_OFF			9		/* Turn off motor 9 index pulses after the last command */
#define	FDC_DELAY_IP_ADDRESS_ID			5		/* 5 index pulses max when looking for next sector's address id field */


/* Delays are in micro sec */
#define	FDC_DELAY_US_HEAD_LOAD			( 15 * 1000 )	/* Additional 15 ms delay to load the head in type II/III */

/* Index pulse signal remains high during 3.71 ms on each rotation ([NP] tested on my STF, can vary between 1.5 and 4 ms depending on the drive) */
#define	FDC_DELAY_US_INDEX_PULSE_LENGTH		( 3.71 * 1000 )


/* Internal delays to process commands are in fdc cycles for a 8 MHz clock */
#define	FDC_DELAY_CYCLE_TYPE_I_PREPARE		(90*8)		/* Types I commands take at least 0.09 ms to execute */
								/* (~740 cpu cycles @ 8 Mhz). [NP] : this was measured on a 520 STF */
								/* and avoid returning immediately when command has no effect */
#define	FDC_DELAY_CYCLE_TYPE_II_PREPARE		(1*8) // 65	/* Start Type II commands immediately */
#define	FDC_DELAY_CYCLE_TYPE_III_PREPARE	(1*8)		/* Start Type III commands immediately */
#define	FDC_DELAY_CYCLE_TYPE_IV_PREPARE		(100*8)		/* FIXME [NP] : this was not measured */
#define	FDC_DELAY_CYCLE_COMMAND_COMPLETE	(1*8)		/* Number of cycles before going to the _COMPLETE state (~8 cpu cycles) */
#define	FDC_DELAY_CYCLE_COMMAND_IMMEDIATE	(0)		/* Number of cycles to go immediately to another state */

/* When the drive is switched off or if there's no floppy, some commands will wait forever */
/* as they can't find the next index pulse. Instead of continuously testing if a valid drive */
/* or floppy becomes available (which would slow down emulation), we only test every 50000 FDC cycles, */
/* which shouldn't give any noticeable emulation error */
#define	FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY	50000

/* Update the floppy's angular position on a regular basis to detect the index pulses */
#define	FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE	500


#define	FDC_DMA_SECTOR_SIZE			512		/* Sector count at $ff8606 is for 512 bytes blocks */
#define	FDC_DMA_FIFO_SIZE			16		/* DMA transfers blocks of 16 bytes at a time */

#define	FDC_PHYSICAL_MAX_TRACK			90		/* Head can't go beyond 90 tracks */


#define	FDC_STEP_RATE				( FDC.CR & 0x03 )	/* Bits 0 and 1 of the current type I command */

int FDC_StepRate_ms[] = { 6 , 12 , 2 , 3 };			/* Controlled by bits 1 and 0 (r1/r0) in type I commands */




#define	FDC_FAST_FDC_FACTOR			10		/* Divide all delays by this value when --fastfdc is used */

/* Standard ST floppies are double density ; to simulate HD or ED floppies, we use */
/* a density factor to have x2 or x4 more bytes during 1 FDC cycle */
#define	FDC_DENSITY_FACTOR_DD			1
#define	FDC_DENSITY_FACTOR_HD			2		/* For a HD disk, we get x2 bytes than DD */
#define	FDC_DENSITY_FACTOR_ED			4		/* For a ED disk, we get x4 bytes than DD */


#define	FDC_EMULATION_MODE_INTERNAL		1		/* Use fdc.c to handle emulation (ST, MSA, DIM and STX images) */
#define	FDC_EMULATION_MODE_IPF			2		/* Use floppy_ipf.c to handle emulation (IPF, CTR images) */


typedef struct {
	/* WD1772 internal registers */
	uint8_t		DR;					/* Data Register */
	uint8_t		TR;					/* Track Register */
	uint8_t		SR;					/* Sector Register */
	uint8_t		CR;					/* Command Register */
	uint8_t		STR;					/* Status Register */
	int		StepDirection;				/* +1 (Step In) or -1 (Step Out) */

	uint8_t		SideSignal;				/* Side 0 or 1 */
	int		DriveSelSignal;				/* 0 or 1 for drive A or B ; or -1 if no drive selected */
	uint8_t		IRQ_Signal;				/* 0 if IRQ is not set, else value depends on the source of the IRQ */

	uint16_t		DensityMode;				/* bits 0 and 1 of $ff860e */
								/* 0x00 : FDC operates in DD mode */
								/* 0x03 : FDC operates in HD mode */

	/* Other variables */
	int		Command;				/* FDC emulation command currently being executed */
	int		CommandState;				/* Current state for the running command */
	uint8_t		CommandType;				/* Type of latest FDC command (1,2,3 or 4) */
	bool		ReplaceCommandPossible;			/* true if the current command can be replaced by another one (see notes) */

	uint8_t		Status_Temp;				/* Temporary content of the status register */
	bool		StatusTypeI;				/* When true, STR will report the status of a type I command */
	int		IndexPulse_Counter;			/* To count the number of rotations when motor is ON */
	uint8_t		NextSector_ID_Field_TR;			/* Track value in the next ID Field after a call to FDC_NextSectorID_FdcCycles_ST() */
	uint8_t		NextSector_ID_Field_SR;			/* Sector value in the next ID Field after a call to FDC_NextSectorID_FdcCycles_ST() */
	uint8_t		NextSector_ID_Field_LEN;		/* Sector's length in the next ID Field after a call to FDC_NextSectorID_FdcCycles_ST() */
	uint8_t		NextSector_ID_Field_CRC_OK;		/* CRC OK or not in the next ID Field after a call to FDC_NextSectorID_FdcCycles_ST() */
	uint8_t		InterruptCond;				/* For a type IV force interrupt, contains the condition on the lower 4 bits */

	int		EmulationMode;				/* FDC_EMULATION_MODE_INTERNAL or FDC_EMULATION_MODE_IPF */
} FDC_STRUCT;


typedef struct {
	/* DMA internal registers */
	uint16_t		Status;
	uint16_t		Mode;
	uint16_t		SectorCount;
	int16_t		BytesInSector;

	uint8_t		FIFO[ FDC_DMA_FIFO_SIZE ];
	int		FIFO_Size;				/* Between 0 and FDC_DMA_FIFO_SIZE */

	uint32_t		Address;

	uint16_t		ff8604_recent_val;			/* Most recent value read/written at $ff8604 in fdc/hdc mode (bit4=0 in $ff8606) */

	/* Variables to handle our DMA buffer */
	int		PosInBuffer;
	int		PosInBufferTransfer;			/* FIXME REMOVE */
	int		BytesToTransfer;
} FDC_DMA_STRUCT;


typedef struct {
	bool		Enabled;
	bool		DiskInserted;
	int		RPM;					/* Rotation Per Minutes * 1000 */
	int		FloppyDensity;				/* Density of the inserted floppy for current track/side */
								/* 1 for DD (720 kB), 2 for HD (1.4 MB), 4 for ED (2.8 MB) */
	uint8_t		HeadTrack;				/* Current position of the head */
//	uint8_t		Motor;					/* State of the drive's motor : 0=OFF 1=ON */
	uint8_t		NumberOfHeads;				/* 1 for single sided drive, 2 for double sided */
	uint8_t		DiskChange_signal;			/* 0=disk ejected 1=disk inserted and step pulse received */
								/* This signal is available on pin 34 of compatible drives */
								/* and connected to the 2nd MFP of the TT */

	uint64_t		IndexPulse_Time;			/* CyclesGlobalClockCounter value last time we had an index pulse with motor ON */
} FDC_DRIVE_STRUCT;


/**
 * Bytes to transfer with type II/III commands are stored in this buffer
 * which associates a specific delay to each byte. This allows to
 * have a common method to transfer data from ST/MSA disk images (with fixed
 * timing), as well as data from STX disk images (with possible timing variations)
 */
typedef struct {
	int		Size;
	int		PosRead;

	struct {
		uint8_t		Byte;
		uint16_t		Timing;
	} Data [ FDC_TRACK_BYTES_STANDARD*4+1000 ];
} FDC_BUFFER_STRUCT;


static FDC_STRUCT	FDC;					/* All variables related to the WD1772 emulation */
static FDC_DMA_STRUCT	FDC_DMA;				/* All variables related to the DMA transfer */
static FDC_DRIVE_STRUCT	FDC_DRIVES[ MAX_FLOPPYDRIVES ];		/* A: and B: */
static FDC_BUFFER_STRUCT	FDC_BUFFER;			/* Buffer of Timing/Byte to transfer with the FDC */

static uint8_t DMADiskWorkSpace[ FDC_TRACK_BYTES_STANDARD*4+1000 ];/* Workspace used to transfer bytes between floppy and DMA */
								/* It should be large enough to contain a whole track */
								/* We use a x4 factor when we need to simulate HD and ED too */



/*--------------------------------------------------------------*/
/* Local functions prototypes					*/
/*--------------------------------------------------------------*/

static uint32_t	FDC_DelayToFdcCycles ( uint32_t Delay_micro );
static uint32_t	FDC_FdcCyclesToCpuCycles ( uint32_t FdcCycles );
static uint32_t	FDC_CpuCyclesToFdcCycles ( uint32_t CpuCycles );
static void	FDC_StartTimer_FdcCycles ( int FdcCycles , int InternalCycleOffset );
static int	FDC_TransferByte_FdcCycles ( int NbBytes );
static void	FDC_CRC16 ( uint8_t *buf , int nb , uint16_t *pCRC );

static void	FDC_ResetDMA ( void );

static int	FDC_GetEmulationMode ( void );
static void	FDC_Drive_Connect_DC_signal_GPIP ( int Drive );
static void	FDC_Drive_Set_DC_signal ( int Drive , uint8_t val );
static int	FDC_GetSectorsPerTrack ( int Drive , int Track , int Side );
static int	FDC_GetSidesPerDisk ( int Drive , int Track );
static int	FDC_GetTracksPerDisk ( int Drive );
static int	FDC_ComputeFloppyDensity ( uint8_t Drive , uint8_t Track , uint8_t Side );
static void	FDC_UpdateFloppyDensity ( uint8_t Drive , uint8_t Track , uint8_t Side );

static uint32_t	FDC_GetCyclesPerRev_FdcCycles ( int Drive );
static void	FDC_IndexPulse_Update ( void );
static void	FDC_IndexPulse_Init ( int Drive );

static void	FDC_Update_STR ( uint8_t DisableBits , uint8_t EnableBits );
static int	FDC_CmdCompleteCommon ( bool DoInt );
static bool	FDC_VerifyTrack ( void );
static int	FDC_UpdateMotorStop ( void );
static int	FDC_UpdateRestoreCmd ( void );
static int	FDC_UpdateSeekCmd ( void );
static int	FDC_UpdateStepCmd ( void );
static int	FDC_UpdateReadSectorsCmd ( void );
static int	FDC_UpdateWriteSectorsCmd ( void );
static int	FDC_UpdateReadAddressCmd ( void );
static int	FDC_UpdateReadTrackCmd ( void );
static int	FDC_UpdateWriteTrackCmd ( void );

static bool	FDC_Set_MotorON ( uint8_t FDC_CR );
static int	FDC_TypeI_Restore ( void );
static int	FDC_TypeI_Seek ( void );
static int	FDC_TypeI_Step ( void );
static int	FDC_TypeI_StepIn ( void );
static int	FDC_TypeI_StepOut ( void );
static int	FDC_TypeII_ReadSector ( void );
static int	FDC_TypeII_WriteSector(void);
static int	FDC_TypeIII_ReadAddress ( void );
static int	FDC_TypeIII_ReadTrack ( void );
static int	FDC_TypeIII_WriteTrack ( void );
static int	FDC_TypeIV_ForceInterrupt ( void );

static int	FDC_ExecuteTypeICommands ( void );
static int	FDC_ExecuteTypeIICommands ( void );
static int	FDC_ExecuteTypeIIICommands ( void );
static int	FDC_ExecuteTypeIVCommands ( void );
static void	FDC_ExecuteCommand ( void );

static void	FDC_WriteSectorCountRegister ( void );
static void	FDC_WriteCommandRegister ( void );
static void	FDC_WriteTrackRegister ( void );
static void	FDC_WriteSectorRegister ( void );
static void	FDC_WriteDataRegister ( void );

static int	FDC_NextSectorID_FdcCycles_ST ( uint8_t Drive , uint8_t NumberOfHeads , uint8_t Track , uint8_t Side );
static uint8_t	FDC_NextSectorID_TR_ST ( void );
static uint8_t	FDC_NextSectorID_SR_ST ( void );
static uint8_t	FDC_NextSectorID_LEN_ST ( void );
static uint8_t	FDC_NextSectorID_CRC_OK_ST ( void );
static uint8_t	FDC_ReadSector_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side , int *pSectorSize );
static uint8_t	FDC_WriteSector_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side , int SectorSize );
static uint8_t	FDC_ReadAddress_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side );
static uint8_t	FDC_ReadTrack_ST ( uint8_t Drive , uint8_t Track , uint8_t Side );
static uint8_t	FDC_WriteTrack_ST ( uint8_t Drive , uint8_t Track , uint8_t Side , int TrackSize );


/*-----------------------------------------------------------------------*/
/**
 * Save/Restore snapshot of local variables('MemorySnapShot_Store' handles type)
 */
void	FDC_MemorySnapShot_Capture(bool bSave)
{
	MemorySnapShot_Store(&FDC, sizeof(FDC));
	MemorySnapShot_Store(&FDC_DMA, sizeof(FDC_DMA));
	MemorySnapShot_Store(&FDC_DRIVES, sizeof(FDC_DRIVES));
	MemorySnapShot_Store(&FDC_BUFFER, sizeof(FDC_BUFFER_STRUCT));

	MemorySnapShot_Store(DMADiskWorkSpace, sizeof(DMADiskWorkSpace));
}


/*-----------------------------------------------------------------------*/
/**
 * Change the color of the drive's led color in the statusbar, depending
 * on the state of the busy bit in STR
 */
void	FDC_Drive_Set_BusyLed ( uint8_t STR )
{
//fprintf ( stderr ,"fdc led %d %x\n" , FDC.DriveSelSignal , STR );
	if ( FDC.DriveSelSignal < 0 )
		return;						/* no drive selected */

	if ( STR & FDC_STR_BIT_BUSY )
		Statusbar_SetFloppyLed ( FDC.DriveSelSignal , LED_STATE_ON_BUSY );
	else
		Statusbar_SetFloppyLed ( FDC.DriveSelSignal , LED_STATE_ON );
}


/*-----------------------------------------------------------------------*/
/**
 * Return a small text + length with the current values of the FDC's registers
 * This text is displayed in the statusbar and it looks like :
 * CC:xx HH:TT:SS:s
 *   CC=command in 2 letters
 *   xx=command in hexa
 *   HH=physical head's position
 *   TT=track register
 *   SS=sector register
 *   s=side
 */
int	FDC_Get_Statusbar_Text ( char *text, size_t maxlen )
{
	uint8_t	Command , Head , Track , Sector , Side;
	char	CommandText[ 3 ];
	size_t	written;
	int	Drive;

	Drive = FDC.DriveSelSignal;
	if ( Drive < 0 )					/* If no drive enabled, use drive O for Head */
		Drive = 0;

	if ( FDC_GetEmulationMode() == FDC_EMULATION_MODE_INTERNAL )
	{
		Command = FDC.CR;
		Head	= FDC_DRIVES[ Drive ].HeadTrack;
		Track	= FDC.TR;
		Sector	= FDC.SR;
		Side	= FDC.SideSignal;
	}
	else							/* FDC_EMULATION_MODE_IPF */
	{
		IPF_FDC_StatusBar ( &Command , &Head , &Track , &Sector , &Side );
	}

	if      ( ( Command & 0xf0 ) == 0x00 )	strcpy ( CommandText , "RE" );		/* Restore */
	else if ( ( Command & 0xf0 ) == 0x10 )	strcpy ( CommandText , "SE" );		/* Seek */
	else if ( ( Command & 0xe0 ) == 0x20 )	strcpy ( CommandText , "ST" );		/* Step */
	else if ( ( Command & 0xe0 ) == 0x40 )	strcpy ( CommandText , "SI" );		/* Step In */
	else if ( ( Command & 0xe0 ) == 0x60 )	strcpy ( CommandText , "SO" );		/* Step Out */
	else if ( ( Command & 0xe0 ) == 0x80 )	strcpy ( CommandText , "RS" );		/* Read Sector */
	else if ( ( Command & 0xe0 ) == 0xa0 )	strcpy ( CommandText , "WS" );		/* Write Sector */
	else if ( ( Command & 0xf0 ) == 0xc0 )	strcpy ( CommandText , "RA" );		/* Read Address */
	else if ( ( Command & 0xf0 ) == 0xe0 )	strcpy ( CommandText , "RT" );		/* Read Track */
	else if ( ( Command & 0xf0 ) == 0xf0 )	strcpy ( CommandText , "WT" );		/* Write Track */
	else					strcpy ( CommandText , "FI" );		/* Force Int */

	written = snprintf ( text, maxlen, "%s:%02X %02X:%02X:%02X:%d" , CommandText , Command , Head , Track , Sector , Side );
	assert(written < maxlen); /* more space needs to be allocated */
	return written;
}


/*-----------------------------------------------------------------------*/
/**
 * Convert a delay in micro seconds to its equivalent of fdc cycles
 * (delays in the WD1772 specs are relative to a 8 MHz reference clock)
 */
static uint32_t	FDC_DelayToFdcCycles ( uint32_t Delay_micro )
{
	uint32_t	FdcCycles;

	FdcCycles = (uint32_t) ( ( (uint64_t) FDC_CLOCK_STANDARD * Delay_micro ) / 1000000 );

//fprintf ( stderr , "fdc state %d delay %d us %d fdc cycles\n" , FDC.Command , Delay_micro , FdcCycles );
	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Convert a number of fdc cycles at freq MachineClocks.FDC_Freq to a number
 * of cpu cycles at freq MachineClocks.CPU_Freq
 * TODO : we use a fixed 8 MHz clock to convert cycles for our internal timers
 * in cycInt.c. This should be replaced some days by using MachineClocks.CPU_Freq.
 * (for Falcon, we multiply cycles by 2 to simulate a freq in the 8 MHz range)
 */
static uint32_t	FDC_FdcCyclesToCpuCycles ( uint32_t FdcCycles )
{
	uint32_t	CpuCycles;

	/* Our conversion expects FDC_Freq to be nearly the same as CPU_Freq (8 Mhz) */
	/* but the Falcon uses a 16 MHz clock for the Ajax FDC */
	/* FIXME : as stated above, this should be handled better, without involving 8 MHz CPU_Freq */
	if (Config_IsMachineFalcon())
		FdcCycles *= 2;					/* correct delays for a 8 MHz FDC_Freq clock instead of 16 */

//	CpuCycles = rint ( ( (uint64_t)FdcCycles * MachineClocks.CPU_Freq ) / MachineClocks.FDC_Freq );
	CpuCycles = rint ( ( (uint64_t)FdcCycles * 8021247.L ) / MachineClocks.FDC_Freq );
	CpuCycles <<= nCpuFreqShift;				/* Convert to x1 or x2 or x4 cpu speed */

//fprintf ( stderr , "fdc command %d machine %d fdc cycles %d cpu cycles %d\n" , FDC.Command , ConfigureParams.System.nMachineType , FdcCycles , CpuCycles );
//if ( Delay==4104) Delay=4166;		// 4166 : decade demo
	return CpuCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Convert a number of cpu cycles at freq MachineClocks.CPU_Freq to a number
 * of fdc cycles at freq MachineClocks.FDC_Freq (this is the opposite
 * of FDC_FdcCyclesToCpuCycles)
 * TODO : we use a fixed 8 MHz clock to convert cycles for our internal timers
 * in cycInt.c. This should be replaced some days by using MachineClocks.CPU_Freq.
 */
static uint32_t	FDC_CpuCyclesToFdcCycles ( uint32_t CpuCycles )
{
	int	FdcCycles;


	CpuCycles >>= nCpuFreqShift;				/* Compensate for x2 or x4 cpu speed */

//	FdcCycles = rint ( ( (uint64_t)CpuCycles * MachineClocks.FDC_Freq ) / MachineClocks.CPU_Freq );
	FdcCycles = rint ( ( (uint64_t)CpuCycles * MachineClocks.FDC_Freq ) / 8021247.L );

	/* Our conversion expects FDC_Freq to be nearly the same as CPU_Freq (8 Mhz) */
	/* but the Falcon uses a 16 MHz clock for the Ajax FDC */
	/* FIXME : as stated above, this should be handled better, without involving 8 MHz CPU_Freq */
	if (Config_IsMachineFalcon())
		FdcCycles /= 2;					/* correct delays for a 8 MHz FDC_Freq clock instead of 16 */

//fprintf ( stderr , "fdc state %d delay %d cpu cycles %d fdc cycles\n" , FDC.Command , CpuCycles , FdcCycles );
	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Start an internal timer to handle the FDC's events.
 * If "fast floppy" mode is used, we speed up the timer by dividing
 * the number of cycles by a fixed number.
 */
static void	FDC_StartTimer_FdcCycles ( int FdcCycles , int InternalCycleOffset )
{
//fprintf ( stderr , "fdc start timer %d cycles\n" , FdcCycles );

	if ( ( ConfigureParams.DiskImage.FastFloppy ) && ( FdcCycles > FDC_FAST_FDC_FACTOR ) )
		FdcCycles /= FDC_FAST_FDC_FACTOR;

	CycInt_AddRelativeInterruptWithOffset ( FDC_FdcCyclesToCpuCycles ( FdcCycles ) , INT_CPU_CYCLE , INTERRUPT_FDC , InternalCycleOffset );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of FDC cycles required to read/write 'nb' bytes
 * This function will always be called when FDC.DriveSelSignal >= 0, as
 * there's no case where we transfer bytes if no drive is enabled.
 * We take "FloppyDensity" into account to simulate faster transfer for HD/ED floppies.
 *
 * 2015/10/23 [NP] As seen in the 'Bird Mad Girl Show' demo, it's possible to get
 * FDC.DriveSelSignal < 0 once a transfer was started (for example, read sector
 * will complete successfully). So we use DD by default in that case.
 */
static int	FDC_TransferByte_FdcCycles ( int NbBytes )
{
//fprintf ( stderr , "fdc state %d transfer %d bytes\n" , FDC.Command , NbBytes );
	if ( FDC.DriveSelSignal < 0 )
	{
		/* Drive was unselected during the transfer : assume DD for the rest of the bytes */
		return ( NbBytes * FDC_DELAY_CYCLE_MFM_BYTE ) / FDC_DENSITY_FACTOR_DD;
	}

	return ( NbBytes * FDC_DELAY_CYCLE_MFM_BYTE ) / FDC_GetFloppyDensity ( FDC.DriveSelSignal );
}


/*-----------------------------------------------------------------------*/
/**
 * Compute the CRC16 of 'nb' bytes stored in 'buf'.
 */
static void FDC_CRC16 ( uint8_t *buf , int nb , uint16_t *pCRC )
{
	int	i;

	crc16_reset ( pCRC );
	for ( i=0 ; i<nb ; i++ )
	{
//		fprintf ( stderr , "fdc crc16 %d 0x%x\n" , i , buf[ i ] );
		crc16_add_byte ( pCRC , buf[ i ] );
	}
//	fprintf ( stderr , "fdc crc16 0x%x 0x%x\n" , *pCRC>>8 , *pCRC & 0xff );
}


/*-----------------------------------------------------------------------*/
/**
 * Init variables used in FDC and DMA emulation
 */
void FDC_Init ( void )
{
	int	i;

        LOG_TRACE ( TRACE_FDC , "fdc init\n" );

	for ( i=0 ; i<MAX_FLOPPYDRIVES ; i++ )
	{
		FDC_DRIVES[ i ].Enabled = true;
		FDC_DRIVES[ i ].DiskInserted = false;
		FDC_DRIVES[ i ].RPM = FDC_RPM_STANDARD * 1000;
		FDC_DRIVES[ i ].FloppyDensity = FDC_DENSITY_FACTOR_DD;
		FDC_DRIVES[ i ].HeadTrack = 0;			/* Set all drives to track 0 */
		FDC_DRIVES[ i ].NumberOfHeads = 2;		/* Double sided drive */
		FDC_DRIVES[ i ].IndexPulse_Time = 0;
		FDC_Drive_Set_DC_signal ( i , 0 );
	}

	FDC_Buffer_Reset();

	FDC.EmulationMode = FDC_EMULATION_MODE_INTERNAL;
}


/*-----------------------------------------------------------------------*/
/**
 * Reset variables used in FDC and DMA emulation
 */

/* This function is called after a hardware reset of the FDC.
 * Cold reset is when the computer is turned off/on.
 * Warm reset is when the reset button is pressed or the 68000
 * RESET instruction is used.
 * On warm reset, TR and DR should not be reset.
 * STR is set to 0 and SR is set to 1 (verified on a real STF)
 */
void FDC_Reset ( bool bCold )
{
	int	i;

	LOG_TRACE ( TRACE_FDC , "fdc reset mode=%s\n" , bCold?"cold":"warm" );

	/* Clear out FDC registers */
	FDC.CR = 0;
	FDC.STR = 0;
	FDC.SR = 1;
	FDC.StatusTypeI = false;

	/* On cold reset, TR and DR should be reset */
	/* On warm reset, TR and DR value should be kept */
	if ( bCold )
	{
		FDC.TR = 0;
		FDC.DR = 0;
		FDC_DMA.ff8604_recent_val = 0;		/* Only set to 0 on cold reset */
	}
	FDC.StepDirection = 1;

	FDC.Command = FDCEMU_CMD_NULL;			/* FDC emulation command currently being executed */
	FDC.CommandState = FDCEMU_RUN_NULL;
	FDC.CommandType = 0;
	FDC.InterruptCond = 0;
	FDC.IRQ_Signal = 0;
	FDC_ClearIRQ();					/* Propagate IRQ signal to MFP GPIP5 */

	FDC.IndexPulse_Counter = 0;
	for ( i=0 ; i<MAX_FLOPPYDRIVES ; i++ )
	{
		FDC_DRIVES[ i ].IndexPulse_Time = 0;	/* Current IP's locations are lost after a reset (motor is now OFF) */
		FDC_Drive_Set_DC_signal ( i , 0 );
	}

	FDC_DMA.Status = 1;				/* no DMA error and SectorCount=0 */
	FDC_DMA.Mode = 0;

	FDC_ResetDMA();

	FDC_Buffer_Reset();

	/* Also reset IPF emulation */
	IPF_Reset( bCold );
}


/*-----------------------------------------------------------------------*/
/**
 * Reset DMA (clear internal 16 bytes buffer)
 *
 * This is done by 'toggling' bit 8 of the DMA Mode Control register
 * This will empty the FIFOs and reset Sector Count to 0
 */
static void FDC_ResetDMA ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
	LOG_TRACE(TRACE_FDC, "fdc reset dma VBL=%d video_cyc=%d %d@%d pc=%x\n",
		nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Empty FIFO */
	FDC_DMA.FIFO_Size = 0;

	/* Reset bytes count for current DMA sector */
	FDC_DMA.BytesInSector = FDC_DMA_SECTOR_SIZE;
	FDC_DMA.SectorCount = 0;			/* After a reset, sector count is 0 (verified on a real STF) */

	/* Reset internal variables used to handle DMA transfer */
	FDC_DMA.PosInBuffer = 0;
	FDC_DMA.PosInBufferTransfer = 0;
	FDC_DMA.BytesToTransfer = 0;

	/* Reset HDC command status */
	HDC_ResetCommandStatus();
}


/*-----------------------------------------------------------------------*/
/**
 * Set DMA Status at $ff8606
 *
 * Bit 0 - Error Status (0=Error 1=No error)
 * Bit 1 - Sector Count Zero Status (0=Sector Count Zero)
 * Bit 2 - Data Request signal from the FDC
 */
void FDC_SetDMAStatus ( bool bError )
{
	/* Set error bit */
	if (!bError)
		FDC_DMA.Status |= 0x1;					/* No Error, set bit 0 */
	else
		FDC_DMA.Status &= ~0x1;					/* Error, clear bit 0 */
}


/*-----------------------------------------------------------------------*/
/**
 * Return the value of bit 8 in the FDC's DMA mode control register.
 * 0=dma read  0x100=dma write
 */
int	FDC_DMA_GetModeControl_R_WR ( void )
{
	return FDC_DMA.Mode & 0x100;
}

int FDC_DMA_GetMode(void)
{
	return FDC_DMA.Mode;
}


/*-----------------------------------------------------------------------*/
/**
 * Add a byte to the DMA's FIFO buffer (read from disk).
 * If the buffer is full and DMA is ON, write the FIFO's 16 bytes to DMA's address.
 *
 * NOTE [NP] : the DMA is connected to the FDC, each time a DRQ is made by the FDC,
 * it's handled by the DMA and stored in the DMA's 16 bytes buffer. This means
 * FDC_STR_BIT_LOST_DATA will never be set (but data can be lost if FDC_DMA.SectorCount==0)
 *
 * NOTE [NP] : as seen on a real STF, the unused bits when reading DMA Status at $ff8606
 * are also changed by the DMA operations (this might not be complete, but seems quite reproducible) :
 *  - reading a byte from the FDC to the DMA will change unused bits in the lowest byte at $ff8604
 *  - transferring the 16 byte DMA buffer to RAM will change the unused bits in the 2 bytes at $ff8604
 *
 * In all cases, the byte read from the FDC is transferred to the DMA, even if DMA sector count is 0, so
 * we must always update lowest byte of ff8604_recent_val.
 * DMA FIFO is transferred only when DMA sector count is >0, so high byte of ff8604_recent_val will be
 * updated only in that case.
 */
void	FDC_DMA_FIFO_Push ( uint8_t Byte )
{
	uint32_t	Address;

//fprintf ( stderr , "dma push pos=%d byte=%x %lld\n" , FDC_DMA.FIFO_Size , Byte , CyclesGlobalClockCounter );

	/* Store the byte that was just read from FDC's Data Register */
	FDC_DMA.ff8604_recent_val = ( FDC_DMA.ff8604_recent_val & 0xff00 ) | Byte;

	if ( FDC_DMA.SectorCount == 0 )
	{
		//FDC_Update_STR ( 0 , FDC_STR_BIT_LOST_DATA );		/* If DMA is OFF, data are lost -> Not on the ST */
		FDC_SetDMAStatus ( true );				/* Set DMA error (bit 0) */
		return;
	}

	FDC_SetDMAStatus ( false );					/* No DMA error (bit 0) */

	FDC_DMA.FIFO [ FDC_DMA.FIFO_Size++ ] = Byte;

	if ( FDC_DMA.FIFO_Size < FDC_DMA_FIFO_SIZE )			/* FIFO is not full yet */
		return;

	/* FIFO full : transfer data from FIFO to RAM and update DMA address */
	Address = FDC_GetDMAAddress();
	STMemory_SafeCopy ( Address , FDC_DMA.FIFO , FDC_DMA_FIFO_SIZE , "FDC DMA push to fifo" );
	FDC_WriteDMAAddress ( Address + FDC_DMA_FIFO_SIZE );
	FDC_DMA.FIFO_Size = 0;						/* FIFO is now empty again */
	/* When the FIFO transfers data to RAM it takes 4 cycles per word and the CPU is stalled during this time */
	M68000_AddCycles_CE ( 4 * FDC_DMA_FIFO_SIZE / 2 );		/* 32 cycles */
	/* For the MegaSTE, using the FDC DMA will flush the external cache */
	if ( ConfigureParams.System.nMachineType == MACHINE_MEGA_STE )
		MegaSTE_Cache_Flush ();

	/* Store the last word that was just transferred by the DMA */
	FDC_DMA.ff8604_recent_val = ( FDC_DMA.FIFO [ FDC_DMA_FIFO_SIZE-2 ] << 8 ) | FDC_DMA.FIFO [ FDC_DMA_FIFO_SIZE-1 ];

	/* Update Sector Count */
	FDC_DMA.BytesInSector -= FDC_DMA_FIFO_SIZE;
	if ( FDC_DMA.BytesInSector <= 0 )
	{
		FDC_DMA.SectorCount--;
		FDC_DMA.BytesInSector = FDC_DMA_SECTOR_SIZE;
	}
}


/*-----------------------------------------------------------------------*/
/**
 * Get a byte from the DMA's FIFO buffer (write to disk).
 * If the buffer is empty and DMA is ON, load 16 bytes in the FIFO from DMA's address.
 *
 * NOTE [NP] : on a real ST, there're two 16 byte DMA FIFO, this allows to refill one FIFO
 * while the other FIFO is used to transfer data to the FDC. We don't emulate this at the
 * moment, as it doesn't cause any problem (when a DMA is set to write mode, we would need
 * to prefill 32 bytes instead of 16 bytes as we do now)
 *
 * NOTE [NP] : as with FDC_DMA_FIFO_Push, this also changes the unused bits at $ff8606
 */
uint8_t	FDC_DMA_FIFO_Pull ( void )
{
	uint32_t	Address;
	uint8_t	Byte;

//fprintf ( stderr , "fifo pull %d %d %d\n" , FDC_DMA.BytesToTransfer , FDC_DMA.BytesInSector , FDC_DMA.SectorCount );
	if ( FDC_DMA.SectorCount == 0 )
	{
		//FDC_Update_STR ( 0 , FDC_STR_BIT_LOST_DATA );		/* If DMA is OFF, data are lost -> Not on the ST */
		FDC_SetDMAStatus ( true );				/* Set DMA error (bit 0) */
		return 0;						/* Write a '0' byte when dma is off */
	}

	FDC_SetDMAStatus ( false );					/* No DMA error (bit 0) */

	if ( FDC_DMA.FIFO_Size > 0 )					/* FIFO is not empty yet */
		Byte = FDC_DMA.FIFO [ FDC_DMA_FIFO_SIZE - ( FDC_DMA.FIFO_Size-- ) ];	/* return byte at pos 0, 1, .., 15 */

	else
	{
		/* FIFO empty : transfer data from RAM to FIFO and update DMA address */
		Address = FDC_GetDMAAddress();
		memcpy ( FDC_DMA.FIFO , &STRam[ Address ] , FDC_DMA_FIFO_SIZE );/* TODO : check we read from a valid RAM location ? */
		FDC_WriteDMAAddress ( Address + FDC_DMA_FIFO_SIZE );
		FDC_DMA.FIFO_Size = FDC_DMA_FIFO_SIZE - 1;			/* FIFO is now full again (minus the byte we will return below) */
		/* When the FIFO reads data from RAM it takes 4 cycles per word and the CPU is stalled during this time */
		M68000_AddCycles_CE ( 4 * FDC_DMA_FIFO_SIZE / 2 );		/* 32 cycles */
		/* For the MegaSTE, using the FDC DMA will flush the external cache */
		if ( ConfigureParams.System.nMachineType == MACHINE_MEGA_STE )
			MegaSTE_Cache_Flush ();

		/* Store the last word that was just transferred by the DMA */
		FDC_DMA.ff8604_recent_val = ( FDC_DMA.FIFO [ FDC_DMA_FIFO_SIZE-2 ] << 8 ) | FDC_DMA.FIFO [ FDC_DMA_FIFO_SIZE-1 ];

		/* Update Sector Count */
		FDC_DMA.BytesInSector -= FDC_DMA_FIFO_SIZE;
		if ( FDC_DMA.BytesInSector < 0 )
		{
			FDC_DMA.SectorCount--;
			FDC_DMA.BytesInSector = FDC_DMA_SECTOR_SIZE;
		}

		Byte = FDC_DMA.FIFO [ 0 ];				/* return the 1st byte we just transferred in the FIFO */
	}

	/* Store the byte that will be written to FDC's Data Register */
	FDC_DMA.ff8604_recent_val = ( FDC_DMA.ff8604_recent_val & 0xff00 ) | Byte;

	return Byte;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the value of FDC_DMA.SectorCount (used in floppy_ipf.c)
 */
int	FDC_DMA_GetSectorCount ( void )
{
	return FDC_DMA.SectorCount;
}


/*-----------------------------------------------------------------------*/
/**
 * Reset the buffer used to transfer data between the FDC and the DMA
 */
void	FDC_Buffer_Reset ( void )
{
	FDC_BUFFER.Size = 0;
	FDC_BUFFER.PosRead = 0;	
}


/*-----------------------------------------------------------------------*/
/**
 * Add a byte to the FDC transfer buffer, using a specific timing
 */
void	FDC_Buffer_Add_Timing ( uint8_t Byte , uint16_t Timing )
{
	FDC_BUFFER.Data[ FDC_BUFFER.Size ].Byte = Byte;
	FDC_BUFFER.Data[ FDC_BUFFER.Size ].Timing = Timing;
	FDC_BUFFER.Size++;
}


/*-----------------------------------------------------------------------*/
/**
 * Add a byte to the FDC transfer buffer, using a default timing
 */
void	FDC_Buffer_Add ( uint8_t Byte )
{
	FDC_Buffer_Add_Timing ( Byte , FDC_TransferByte_FdcCycles ( 1 ) );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the timing needed to transfer the Byte at the current position
 */
uint16_t	FDC_Buffer_Read_Timing ( void )
{
//fprintf ( stderr , "read timing %d %x\n" , FDC_BUFFER.PosRead , FDC_BUFFER.Data[ FDC_BUFFER.PosRead ].Timing );
	return FDC_BUFFER.Data[ FDC_BUFFER.PosRead ].Timing;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the Byte at the current position and increment position
 */
uint8_t	FDC_Buffer_Read_Byte ( void )
{
//fprintf ( stderr , "read byte %d %x\n" , FDC_BUFFER.PosRead , FDC_BUFFER.Data[ FDC_BUFFER.PosRead ].Byte );
	return FDC_BUFFER.Data[ FDC_BUFFER.PosRead++ ].Byte;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the Byte at a given position
 */
uint8_t	FDC_Buffer_Read_Byte_pos ( int pos )
{
//fprintf ( stderr , "read byte pos %d %x\n" , pos , FDC_BUFFER.Data[ pos ].Byte );
	return FDC_BUFFER.Data[ pos ].Byte;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of bytes stored in FDC_BUFFER
 */
int	FDC_Buffer_Get_Size ( void )
{
	return FDC_BUFFER.Size;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the mode to handle a read/write in $ff86xx
 * Depending on the images inserted in each floppy drive and on the
 * selected drive, we must choose which fdc emulation should be used.
 * Possible emulation methods are 'internal' or 'ipf'.
 * To avoid mixing emulation methods on both drives when possible (which
 * could lead to inconstancies when precise timings are required), we also
 * use the IPF mode for an empty drive if the other drive contains an IPF
 * image.
 * If no drive is selected, we must use the previous mode (before drives were
 * unselected), not the internal one : in case some commands are sent when
 * drives are deselected and the drive was in IPF mode, we must send
 * the command to IPF to ensure no command are lost if the drive is selected again
 * (eg : D0 command in "Saint Dragon" IPF)
 */
static int FDC_GetEmulationMode ( void )
{
	int	Mode;

	Mode = FDC.EmulationMode;				/* Default to previous mode if no drive is selected */

	/* Check drive 1 first */
	if ( ( PSGRegisters[PSG_REG_IO_PORTA] & 0x04 ) == 0 )
	{
		if ( EmulationDrives[ 1 ].ImageType == FLOPPY_IMAGE_TYPE_IPF )
			Mode = FDC_EMULATION_MODE_IPF;
		else if ( ( EmulationDrives[ 1 ].ImageType == FLOPPY_IMAGE_TYPE_NONE )		/* Drive 1 is empty */
			&& ( EmulationDrives[ 0 ].ImageType == FLOPPY_IMAGE_TYPE_IPF ) )	/* Drive 0 is an IPF image */
			Mode = FDC_EMULATION_MODE_IPF;						/* Use IPF for drive 1 too */
		else
			Mode = FDC_EMULATION_MODE_INTERNAL;
	}

	/* If both drive 0 and drive 1 are enabled, we keep only drive 0 to choose emulation's mode */
	if ( ( PSGRegisters[PSG_REG_IO_PORTA] & 0x02 ) == 0 )
	{
		if ( EmulationDrives[ 0 ].ImageType == FLOPPY_IMAGE_TYPE_IPF )
			Mode = FDC_EMULATION_MODE_IPF;
		else if ( ( EmulationDrives[ 0 ].ImageType == FLOPPY_IMAGE_TYPE_NONE )		/* Drive 0 is empty */
			&& ( EmulationDrives[ 1 ].ImageType == FLOPPY_IMAGE_TYPE_IPF ) )	/* Drive 1 is an IPF image */
			Mode = FDC_EMULATION_MODE_IPF;						/* Use IPF for drive 0 too */
		else
			Mode = FDC_EMULATION_MODE_INTERNAL;
	}

	FDC.EmulationMode = Mode;
//fprintf ( stderr , "emul mode %x %d\n" , PSGRegisters[PSG_REG_IO_PORTA] & 0x06 , Mode );
//	return FDC_EMULATION_MODE_INTERNAL;
//	return FDC_EMULATION_MODE_IPF;
	return Mode;
}


/*-----------------------------------------------------------------------*/
/**
 * Update the FDC's internal variables on a regular basis.
 * To get correct accuracy, this should be called every 200-500 FDC cycles
 * So far, we only need to update the index position for the valid
 * drive/floppy ; updating every 500 cycles is enough for this case.
 */
static void FDC_UpdateAll(void)
{
	FDC_IndexPulse_Update ();
}


/*-----------------------------------------------------------------------*/
/**
 * This function is used to enable/disable a drive when
 * using the UI or command line parameters
 */
void	FDC_Drive_Set_Enable ( int Drive , bool value )
{
	LOG_TRACE ( TRACE_FDC , "fdc enable drive=%d %s\n" , Drive , value?"on":"off" );

	if ( ( Drive >= 0 ) && ( Drive < MAX_FLOPPYDRIVES ) )
		FDC_DRIVES[ Drive ].Enabled = value;

	/* Also forward change to IPF emulation */
	IPF_Drive_Set_Enable ( Drive , value );
}


/*-----------------------------------------------------------------------*/
/**
 * This function is used to choose single sided or double sided for a drive
 * when using the UI or command line parameters
 */
void	FDC_Drive_Set_NumberOfHeads ( int Drive , int NbrHeads )
{
	LOG_TRACE ( TRACE_FDC , "fdc set nbr heads drive=%d %d\n" , Drive , NbrHeads );

	if ( ( Drive >= 0 ) && ( Drive < MAX_FLOPPYDRIVES ) )
		FDC_DRIVES[ Drive ].NumberOfHeads = NbrHeads;

	/* Also forward change to IPF emulation */
	IPF_Drive_Set_DoubleSided ( Drive , NbrHeads==2 ? true : false );
}


/*-----------------------------------------------------------------------*/
/**
 * Get the value of the Disk Change (DC) signal available on pin 34 of some
 * floppy drives (used in TT emulation) and update the TT GPIP register.
 *
 * This signal is active low unless a disk is inserted and a STEP pulse is received
 * and the drive is selected :
 *  0 = drive is selected and a disk was ejected (ie drive is empty)
 *  1 = drive is not selected or a disk was inserted and a step pulse received
 *
 * DC signal was only available on the TT machines and in that case it's connected
 * to the TT MFP on GPIP4 (the signal is inverted before going to GPIP4)
 */
static void	FDC_Drive_Connect_DC_signal_GPIP ( int Drive )
{
	uint8_t	state;

	if ( FDC.DriveSelSignal != Drive )
		state = 1;						/* drive is not selected */
	else
		state = FDC_DRIVES[ Drive ].DiskChange_signal;

	/* DC signal is inverted before going into GPIP4 */
	if ( state == 1 )
		state = MFP_GPIP_STATE_LOW;
	else
		state = MFP_GPIP_STATE_HIGH;

	MFP_GPIP_Set_Line_Input ( pMFP_TT , MFP_TT_GPIP_LINE_DC , state );
}


/*-----------------------------------------------------------------------*/
/**
 * Update the DC signal for a drive and update GPIP for TT machines
 *  - set DC=0 when disk is ejected
 *  - set DC=1 when a step pulse (Type I command) is received and a floppy
 *    is inserted and the drive is selected
 */
static void	FDC_Drive_Set_DC_signal ( int Drive , uint8_t val )
{
	FDC_DRIVES[ Drive ].DiskChange_signal = val;

	if ( Config_IsMachineTT() && ( Drive == 0 ) )
		FDC_Drive_Connect_DC_signal_GPIP ( Drive );
}


/*-----------------------------------------------------------------------*/
/**
 * This function is called when a floppy is inserted in a drive
 * using the UI or command line parameters
 */
void	FDC_InsertFloppy ( int Drive )
{
	LOG_TRACE ( TRACE_FDC , "fdc insert drive=%d\n" , Drive );

	if ( ( Drive >= 0 ) && ( Drive < MAX_FLOPPYDRIVES ) )
	{
		FDC_DRIVES[ Drive ].DiskInserted = true;
		if ( ( FDC.STR & FDC_STR_BIT_MOTOR_ON ) != 0 )		/* If we insert a floppy while motor is already on, we must */
			FDC_IndexPulse_Init ( Drive );			/* init the index pulse's position */
		else
			FDC_DRIVES[ Drive ].IndexPulse_Time = 0;	/* Index pulse's position not known yet */

		/* Update floppy's density for this drive */
		FDC_UpdateFloppyDensity ( Drive , FDC_DRIVES[ Drive ].HeadTrack , FDC.SideSignal );
	}
}


/*-----------------------------------------------------------------------*/
/**
 * This function is called when a floppy is ejected from a drive
 * using the UI or command line parameters
 */
void	FDC_EjectFloppy ( int Drive )
{
	LOG_TRACE ( TRACE_FDC , "fdc eject drive=%d\n" , Drive );

	if ( ( Drive >= 0 ) && ( Drive < MAX_FLOPPYDRIVES ) )
	{
		FDC_DRIVES[ Drive ].DiskInserted = false;
		FDC_DRIVES[ Drive ].IndexPulse_Time = 0;		/* Stop counting index pulses on an empty drive */

		/* Set the Disk Change signal to "ejected" */
		FDC_Drive_Set_DC_signal ( Drive , FDC_DC_SIGNAL_EJECTED );
	}
}


/*-----------------------------------------------------------------------*/
/**
 * Handle a write in the IO_PORTA register $E through $ff8802. Only bits
 * 0-2 are available here, others are masked to 0.
 * bit 0 : side select
 * bit 1-2 : drive select
 *
 * - For internal FDC emulation, we init index pulse if the active drive
 *   changed
 * - We also forward the change to IPF emulation, as it doesn't have direct access
 *   to this IO_PORTA register.
 *
 * If both drives are selected, we keep only drive 0
 */
void	FDC_SetDriveSide ( uint8_t io_porta_old , uint8_t io_porta_new )
{
	int	Side;
	int	Drive;

	if ( io_porta_old == io_porta_new )
		return;							/* No change */

	Side = ( (~io_porta_new) & 0x01 );				/* Side 0 or 1 */

	Drive = -1;							/* By default, don't select any drive */

	/* Check drive 1 first */
	if ( ( io_porta_new & 0x04 ) == 0 )
		Drive = 1;						/* Select drive 1 */

	/* If both drive 0 and drive 1 are enabled, we keep only drive 0 as newdrive */
	if ( ( io_porta_new & 0x02 ) == 0 )
		Drive = 0;						/* Select drive 0 (and un-select drive 1 if set above) */

	LOG_TRACE(TRACE_FDC, "fdc change drive/side io_porta_old=0x%x io_porta_new=0x%x side %d->%d drive %d->%d VBL=%d HBL=%d\n" ,
		  io_porta_old , io_porta_new , FDC.SideSignal , Side , FDC.DriveSelSignal , Drive , nVBLs , nHBL );

	if ( FDC.DriveSelSignal != Drive )
	{
		if ( FDC.DriveSelSignal >= 0 )					/* A drive was previously enabled */
			FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time = 0;	/* Stop counting index pulses on the previous drive */

		if ( Drive >= 0 )						/* A new drive is enabled */
		{
			if ( ( FDC_DRIVES[ Drive ].DiskInserted )		/* If there's a disk in the new drive and motor is already */
			  && ( ( FDC.STR & FDC_STR_BIT_MOTOR_ON ) != 0 ) )	/* on, we must init the index pulse's position */
				FDC_IndexPulse_Init ( Drive );
			else
				FDC_DRIVES[ Drive ].IndexPulse_Time = 0;	/* Index pulse's position not known yet */
		}
	}

	FDC.SideSignal = Side;
	FDC.DriveSelSignal = Drive;

	/* Update floppy's density for this drive */
	if ( Drive >= 0 )
		FDC_UpdateFloppyDensity ( Drive , FDC_DRIVES[ Drive ].HeadTrack , FDC.SideSignal );

	/* Also forward change to IPF emulation */
	IPF_SetDriveSide ( io_porta_old , io_porta_new );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of sectors for track/side for the current floppy in a drive
 * TODO [NP] : this function calls Floppy_FindDiskDetails which handles only ST/MSA
 * disk images so far, so this implies all tracks have in fact the same number
 * of sectors (we don't use Track and Side for now)
 * Drive should be a valid drive (0 or 1)
 */
static int FDC_GetSectorsPerTrack ( int Drive , int Track , int Side )
{
	uint16_t	SectorsPerTrack;

	if (EmulationDrives[ Drive ].bDiskInserted)
	{
		Floppy_FindDiskDetails ( EmulationDrives[ Drive ].pBuffer , EmulationDrives[ Drive ].nImageBytes , &SectorsPerTrack , NULL );
		return SectorsPerTrack;
	}
	else
		return 0;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of sides for a track for the current floppy in a drive
 * Drive should be a valid drive (0 or 1)
 */
static int FDC_GetSidesPerDisk ( int Drive , int Track )
{
	uint16_t	SidesPerDisk;

	if (EmulationDrives[ Drive ].bDiskInserted)
	{
		Floppy_FindDiskDetails ( EmulationDrives[ Drive ].pBuffer , EmulationDrives[ Drive ].nImageBytes , NULL , &SidesPerDisk );
		return SidesPerDisk;					/* 1 or 2 */
	}
	else
		return 0;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of tracks for the current floppy in a drive
 * For ST/MSA, this assumes both sides have the same number of tracks
 * Drive should be a valid drive (0 or 1)
 */
static int FDC_GetTracksPerDisk ( int Drive )
{
	uint16_t	SectorsPerTrack;
	uint16_t	SidesPerDisk;

	if (EmulationDrives[ Drive ].bDiskInserted)
	{
		Floppy_FindDiskDetails ( EmulationDrives[ Drive ].pBuffer , EmulationDrives[ Drive ].nImageBytes , &SectorsPerTrack , &SidesPerDisk );
		return ( ( EmulationDrives[Drive].nImageBytes / NUMBYTESPERSECTOR ) / SectorsPerTrack ) / SidesPerDisk;
	}
	else
		return 0;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of bytes in a raw track
 * For ST/MSA disk images, we consider all the tracks have the same size.
 * To simulate HD/ED floppies, we multiply the size by a density factor (x2 or x4).
 * Drive should be a valid drive (0 or 1)
 */
int	FDC_GetBytesPerTrack ( uint8_t Drive , uint8_t Track , uint8_t Side )
{
	int	SectorsPerTrack;
	int	TrackSize;

	if ( EmulationDrives[ Drive ].bDiskInserted )
	{
		/* If the inserted disk is an STX, we use the supplied track length */
		if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_STX )
			return FDC_GetBytesPerTrack_STX ( Drive , Track , Side );

		SectorsPerTrack = FDC_GetSectorsPerTrack ( Drive , FDC_DRIVES[ Drive ].HeadTrack , FDC.SideSignal );
		if ( SectorsPerTrack >= 36 )
			TrackSize =  FDC_TRACK_BYTES_STANDARD * 4;	/* Simulate a ED disk, 36 sectors or more */
		else if ( SectorsPerTrack >= 18 )
			TrackSize = FDC_TRACK_BYTES_STANDARD * 2;	/* Simulate a HD disk, between 18 and 36 sectors */
		else
			TrackSize = FDC_TRACK_BYTES_STANDARD;		/* Normal DD disk */
	}
	else
		TrackSize = FDC_TRACK_BYTES_STANDARD;			/* No disk, default to DD disk */

	return TrackSize;
}


/*-----------------------------------------------------------------------*/
/**
 * Return a density factor for the current track/side of the floppy in a drive
 * A DD track is usually FDC_TRACK_BYTES_STANDARD bytes, a HD track is
 * twice that number and an ED track is 4 times that number.
 * As number of bytes can vary slightly depending on mastering process and RPM
 * speed, we don't use x1, x2 and x4 directly but we use a margin such as x1.5
 * to choose between DD and HD and x3 to choose between HD and ED
 *
 * We return a factor of x1, x2 or x4 for DD, HD or ED
 * Drive should be a valid drive (0 or 1)
 */
static int FDC_ComputeFloppyDensity ( uint8_t Drive , uint8_t Track , uint8_t Side )
{
	int	TrackSize;

	TrackSize = FDC_GetBytesPerTrack ( Drive , Track , Side );

	if ( TrackSize > 3 * FDC_TRACK_BYTES_STANDARD )
		return FDC_DENSITY_FACTOR_ED;				/* Simulate a ED disk */
	else if ( TrackSize > 1.5 * FDC_TRACK_BYTES_STANDARD )
		return FDC_DENSITY_FACTOR_HD;				/* Simulate a HD disk */
	else
		return FDC_DENSITY_FACTOR_DD;				/* Normal DD disk */
}


/*-----------------------------------------------------------------------*/
/**
 * Update the density value for the inserted floppy depending on the density
 * of the current track/side
 * This function should be called each time track/side are changed and before
 * reading/writing bytes.
 */
static void FDC_UpdateFloppyDensity ( uint8_t Drive , uint8_t Track , uint8_t Side )
{
	FDC_DRIVES[ Drive ].FloppyDensity = FDC_ComputeFloppyDensity ( Drive , Track , Side );
//fprintf ( stderr , "fdc update density drive=%d track=0x%x side=%d density=%d\n" , Drive , Track, Side , FDC_DRIVES[ Drive ].FloppyDensity );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the latest Density value set for a drive
 */
int	FDC_GetFloppyDensity ( uint8_t Drive )
{
	return FDC_DRIVES[ Drive ].FloppyDensity;
}


/*-----------------------------------------------------------------------*/
/**
 * Check if the emulated machine can access the floppy in Drive depending
 * on its density (DD/HD/ED)
 * - For MegaSTE, TT and Falcon, we use the content of $ff860e to compare the
 *   floppy's density with the config of the FDC. If density doesn't match,
 *   the FDC command will abort with RNF error
 * - For other machines (STF, STE) we consider as a convenience that any
 *   DD/HD/ED floppy can be accessed, even if this is not the case on real HW
 *
 * Return true if the floppy in Drive can be accessed, else false
 */
int	FDC_MachineHandleDensity ( uint8_t Drive )
{
	bool		res;

	if ( Config_IsMachineMegaSTE() || Config_IsMachineTT() || Config_IsMachineFalcon() )
	{
		if ( FDC_DRIVES[ Drive ].FloppyDensity == FDC_DENSITY_FACTOR_DD )
		{
			if ( ( FDC.DensityMode & 0x03 ) == 0x00 )	/* FDC is in DD mode ? */
				res = true;
			else
				res = false;
		}
		else							/* HD and ED */
		{
			if ( ( FDC.DensityMode & 0x03 ) == 0x03 )	/* FDC is in HD mode ? */
				res = true;
			else
				res = false;
		}
	}

	else								/* STF, STE */
		res = true;

	if ( !res )
		LOG_TRACE(TRACE_FDC, "fdc handle density failed, drive=%d drive_floppy_density=%d, fdc_mode=%d VBL=%d HBL=%d\n" ,
			Drive , FDC_DRIVES[ Drive ].FloppyDensity , FDC.DensityMode , nVBLs , nHBL );

	return res;
}


/*-----------------------------------------------------------------------*/
/**
 * Get the number of FDC cycles for one revolution of the floppy
 * RPM is already multiplied by 1000 to simulate non-integer values
 * (for Falcon, we divide cycles by 2 to simulate a FDC freq in the 8 MHz range)
 * For STX image, the number of cycles depends on drive/track/side.
 * Drive should be a valid drive (0 or 1)
 */
static uint32_t	FDC_GetCyclesPerRev_FdcCycles ( int Drive )
{
	uint32_t	FdcCyclesPerRev;

	assert(Drive == 0 || Drive == 1);

	/* If the inserted disk is an STX, we use the supplied track length to compute cycles per rev */
	if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_STX )
		return FDC_GetCyclesPerRev_FdcCycles_STX ( Drive , FDC_DRIVES[ Drive ].HeadTrack , FDC.SideSignal );

	/* Assume a standard length for all tracks for ST/MSA images */
	FdcCyclesPerRev = (uint64_t)(MachineClocks.FDC_Freq * 1000.L) / ( FDC_DRIVES[ Drive ].RPM / 60 );

	/* Our conversion expects FDC_Freq to be nearly the same as CPU_Freq (8 Mhz) */
	/* but the Falcon uses a 16 MHz clock for the Ajax FDC */
	/* FIXME : this should be handled better, without involving 8 MHz CPU_Freq */
	if (Config_IsMachineFalcon())
		FdcCyclesPerRev /= 2;					/* correct delays for a 8 MHz FDC_Freq clock instead of 16 */

	return FdcCyclesPerRev;
}



/*-----------------------------------------------------------------------*/
/**
 * If some valid drive/floppy are available and the motor signal is on,
 * update the current angular position for the drive and check if
 * a new index pulse was reached. Increase Index Pulse counter in that case.
 *
 * This function should be called at least every 500 FDC cycles when motor
 * is ON to get good accuracy.
 *
 * [NP] TODO : should we have 2 different Index Pulses for each side or do they
 * happen at the same time ?
 */
static void FDC_IndexPulse_Update(void)
{
	uint32_t	FdcCyclesPerRev;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

//fprintf ( stderr , "update index drive=%d side=%d counter=%d VBL=%d HBL=%d\n" , FDC.DriveSelSignal , FDC.SideSignal , FDC.IndexPulse_Counter , nVBLs , nHBL );

	if ( ( FDC.STR & FDC_STR_BIT_MOTOR_ON ) == 0 )
		return;							/* Motor is OFF, nothing to update */

	if ( ( FDC.DriveSelSignal < 0 ) || ( !FDC_DRIVES[ FDC.DriveSelSignal ].Enabled )
		|| ( !FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted ) )
		return;							/* No valid drive/floppy, nothing to update */

	if ( FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time == 0 )	/* No reference Index Pulse for this drive */
		FDC_IndexPulse_Init ( FDC.DriveSelSignal );		/* (could be the case after a 'reset') */

	FdcCyclesPerRev = FDC_GetCyclesPerRev_FdcCycles ( FDC.DriveSelSignal );

	if ( CyclesGlobalClockCounter - FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time >= FDC_FdcCyclesToCpuCycles ( FdcCyclesPerRev ) )
	{
		/* Store new position of the most recent Index Pulse */
		FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time += FDC_FdcCyclesToCpuCycles ( FdcCyclesPerRev );
		FDC.IndexPulse_Counter++;
		LOG_TRACE(TRACE_FDC, "fdc update index drive=%d side=%d counter=%d ip_time=%"PRIu64" VBL=%d HBL=%d\n" ,
			FDC.DriveSelSignal , FDC.SideSignal , FDC.IndexPulse_Counter ,
			FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time , nVBLs , nHBL );

		if ( FDC.InterruptCond & FDC_INTERRUPT_COND_IP )	/* Do we have a "force int on index pulse" command running ? */
		{
			LOG_TRACE(TRACE_FDC, "fdc type IV force int on index, set irq VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
				nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
			FDC_SetIRQ ( FDC_IRQ_SOURCE_INDEX );
		}
	}
}


/*-----------------------------------------------------------------------*/
/**
 * When motor is started, the position of the next index pulse will be random,
 * as we don't know how much the floppy rotated when the motor was stopped or
 * the floppy was inserted.
 * We compute a random position in the "past" (less than one revolution)
 * and use it as a reference to detect the next index pulse.
 *
 */
static void	FDC_IndexPulse_Init ( int Drive )
{
	uint32_t	FdcCyclesPerRev;
	uint64_t	IndexPulse_Time;

	FdcCyclesPerRev = FDC_GetCyclesPerRev_FdcCycles ( Drive );
	IndexPulse_Time = CyclesGlobalClockCounter - Hatari_rand() % FDC_FdcCyclesToCpuCycles ( FdcCyclesPerRev );
	if ( IndexPulse_Time <= 0 )					/* Should not happen (only if FDC_IndexPulse_Init is */
		IndexPulse_Time = 1;					/* called just after emulation starts) */
	FDC_DRIVES[ Drive ].IndexPulse_Time = IndexPulse_Time;

	LOG_TRACE(TRACE_FDC, "fdc init index drive=%d side=%d counter=%d ip_time=%"PRIu64" VBL=%d HBL=%d\n" ,
		  Drive , FDC.SideSignal , FDC.IndexPulse_Counter ,
		  FDC_DRIVES[ Drive ].IndexPulse_Time , nVBLs , nHBL );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of FDC cycles since the previous index pulse signal
 * for the current drive.
 * If there's no available drive/floppy (i.e. no index), we return -1
 */
int	FDC_IndexPulse_GetCurrentPos_FdcCycles ( uint32_t *pFdcCyclesPerRev )
{
	uint32_t	FdcCyclesPerRev;
	uint32_t	CpuCyclesSinceIndex;

	if ( ( FDC.DriveSelSignal < 0 ) || ( FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time == 0 ) )
		return -1;

	FdcCyclesPerRev = FDC_GetCyclesPerRev_FdcCycles ( FDC.DriveSelSignal );
	CpuCyclesSinceIndex = CyclesGlobalClockCounter - FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time;

	if ( pFdcCyclesPerRev )
		*pFdcCyclesPerRev = FdcCyclesPerRev;

//fprintf ( stderr , "current pos %d %lld %d %lld\n" , FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time ,
//      FdcCyclesPerRev , CyclesGlobalClockCounter - FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time );

	return FDC_CpuCyclesToFdcCycles ( CpuCyclesSinceIndex );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the current position in the track relative to the index pulse.
 * For standard floppy, this is a number of bytes in the range [0,6250[
 * If there's no available drive/floppy and no index, we return -1
 * To simulate HD/ED floppies, we multiply the number of bytes by a density factor.
 */
int	FDC_IndexPulse_GetCurrentPos_NbBytes ( void )
{
	int	FdcCyclesSinceIndex;

	FdcCyclesSinceIndex = FDC_IndexPulse_GetCurrentPos_FdcCycles ( NULL );
	if ( FdcCyclesSinceIndex < 0 )					/* No drive/floppy available at the moment */
		return -1;
//fprintf ( stderr , "fdc index current pos new=%d\n" , FdcCyclesSinceIndex / FDC_DELAY_CYCLE_MFM_BYTE );

	return FdcCyclesSinceIndex * FDC_GetFloppyDensity ( FDC.DriveSelSignal ) / FDC_DELAY_CYCLE_MFM_BYTE;
}



/*-----------------------------------------------------------------------*/
/**
 * Return the current state of the index pulse signal.
 * The signal goes to 1 when reaching the index pulse location and remain
 * to 1 during 1.5 ms (approx 46 bytes).
 * During the rest of the track, the signal will be 0 (it will also be 0
 * if the drive if OFF or empty)
 */
int	FDC_IndexPulse_GetState ( void )
{
	int	state;
	int	FdcCyclesSinceIndex;

	FdcCyclesSinceIndex = FDC_IndexPulse_GetCurrentPos_FdcCycles ( NULL );

	state = 0;
	if ( ( FdcCyclesSinceIndex >= 0 )				/* We have a valid drive/floppy */
	  && ( (uint32_t)FdcCyclesSinceIndex < FDC_DelayToFdcCycles ( FDC_DELAY_US_INDEX_PULSE_LENGTH ) ) )
		state = 1;

//fprintf ( stderr , "fdc index state 2 pos pos=%d state=%d\n" , FdcCyclesSinceIndex , state );
	return state;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of FDC cycles before reaching the next index pulse signal.
 * If there's no available drive/floppy and no index, we return -1
 */
int	FDC_NextIndexPulse_FdcCycles ( void )
{
	uint32_t	FdcCyclesPerRev;
	int	FdcCyclesSinceIndex;
	int	res;

	FdcCyclesSinceIndex = FDC_IndexPulse_GetCurrentPos_FdcCycles ( &FdcCyclesPerRev );
	if ( FdcCyclesSinceIndex < 0 )					/* No drive/floppy available at the moment */
		return -1;

	res = FdcCyclesPerRev - FdcCyclesSinceIndex;

	/* If the next IP is in 0 or 1 cycle, we consider this is a rounding error */
	/* and we wait for one full revolution (this can happen in Force Int on Index Pulse */
	/* when we call FDC_NextIndexPulse_FdcCycles in a loop) */
	if ( res <= 1 )
		res = FdcCyclesPerRev;					// TODO : 0 should be allowed
	
//fprintf ( stderr , "fdc next index current pos new=%d\n" , res );
	return res;
}


/*-----------------------------------------------------------------------*/
/**
 * Set the IRQ signal
 * This is called either on command completion, or when an index pulse
 * is received or when the "force interrupt immediate" command is used.
 * This function can also be called from the HDC emulation or from another
 * FDC emulation module (IPF for example)
 *
 * NOTE : although high/1 on the IRQ pin of the FDC means an interrupt is
 * requested, this signal is inverted before going into MFP's GPIP5.
 * So, we must set the line to low/0 to request an interrupt.
 */
void FDC_SetIRQ ( uint8_t IRQ_Source )
{
	if ( FDC.IRQ_Signal != 0 )					/* Don't set MFP's IRQ again if already set */
	{
	  LOG_TRACE(TRACE_FDC, "fdc set irq, irq 0x%x already set VBL=%d HBL=%d\n" , FDC.IRQ_Signal , nVBLs , nHBL );
	}

	else
	{
		/* Acknowledge in MFP circuit, pass bit, enable, pending */
		MFP_GPIP_Set_Line_Input ( pMFP_Main , MFP_GPIP_LINE_FDC_HDC , MFP_GPIP_STATE_LOW );
		LOG_TRACE(TRACE_FDC, "fdc set irq 0x%x source 0x%x VBL=%d HBL=%d\n" , FDC.IRQ_Signal , IRQ_Source , nVBLs , nHBL );
	}

	/* If IRQ comes from HDC or other sources (IPF), we don't need */
	/* to handle the forced IRQ case used in FDC */
	if ( IRQ_Source == FDC_IRQ_SOURCE_HDC )
		FDC.IRQ_Signal = FDC_IRQ_SOURCE_HDC;

	else if ( IRQ_Source == FDC_IRQ_SOURCE_OTHER )
		FDC.IRQ_Signal = FDC_IRQ_SOURCE_OTHER;

	else								/* IRQ comes from FDC */
	{
		FDC.IRQ_Signal &= ~( FDC_IRQ_SOURCE_HDC | FDC_IRQ_SOURCE_OTHER );
		FDC.IRQ_Signal |= IRQ_Source;
	}
}


/*-----------------------------------------------------------------------*/
/**
 * Reset the IRQ signal ; in case the source of the interrupt is also
 * a "force interrupt immediate" command, the IRQ signal should not be cleared
 * (only command 0xD0 or any new command followed by a read of status reg
 * can clear the forced IRQ)
 *
 * NOTE : although low/0 on the IRQ pin of the FDC means interrupt is
 * cleared, this signal is inverted before going into MFP's GPIP5.
 * So, we must set the line to high/1 to clear interrupt request.
 */
void FDC_ClearIRQ ( void )
{
	if ( ( FDC.IRQ_Signal & FDC_IRQ_SOURCE_FORCED ) == 0 )
	{
		FDC.IRQ_Signal = 0;
		MFP_GPIP_Set_Line_Input ( pMFP_Main , MFP_GPIP_LINE_FDC_HDC , MFP_GPIP_STATE_HIGH );
		LOG_TRACE(TRACE_FDC, "fdc clear irq VBL=%d HBL=%d\n" , nVBLs , nHBL );
	}

	else
	{
		FDC.IRQ_Signal &= FDC_IRQ_SOURCE_FORCED;		/* Clear all sources except 'forced irq' and keep IRQ set in MFP */
		LOG_TRACE(TRACE_FDC, "fdc clear irq not done, irq forced VBL=%d HBL=%d\n" , nVBLs , nHBL );
	}
}

void FDC_ClearHdcIRQ(void)
{
	FDC.IRQ_Signal &= ~FDC_IRQ_SOURCE_HDC;
	if (FDC.IRQ_Signal == 0)
	{
		MFP_GPIP_Set_Line_Input ( pMFP_Main , MFP_GPIP_LINE_FDC_HDC , MFP_GPIP_STATE_HIGH );
	}
}

/*-----------------------------------------------------------------------*/
/**
 * Handle the current FDC command.
 * We use a timer to go from one state to another to emulate the different
 * phases of an FDC command.
 * When the command completes (success or failure), FDC.Command will be
 * set to FDCEMU_CMD_NULL. Until then, this function will be called to
 * handle each state of the command and the corresponding delay in micro
 * seconds.
 * This handler is called after a first delay corresponding to the prepare
 * delay and the eventual motor on delay.
 * Once we reach this point, the current command can not be replaced by
 * another command (except 'Force Interrupt')
 */
void FDC_InterruptHandler_Update ( void )
{
	int	FdcCycles = 0;
	int	PendingCyclesOver;

	/* Number of internal cycles we went over for this timer ( <= 0 ) */
	/* Used to restart the next timer and keep a constant rate (important for DMA transfers) */
	PendingCyclesOver = -PendingInterruptCount;			/* >= 0 */

//fprintf ( stderr , "fdc int handler %lld delay %d\n" , CyclesGlobalClockCounter, PendingCyclesOver );

	CycInt_AcknowledgeInterrupt();

	do								/* We loop as long as FdcCycles == 0 (immediate change of state) */
	{
		/* Update FDC's internal variables */
		FDC_UpdateAll ();

		/* Is FDC active? */
		if (FDC.Command!=FDCEMU_CMD_NULL)
		{
			/* Which command are we running ? */
			switch(FDC.Command)
			{
			case FDCEMU_CMD_RESTORE:
				FdcCycles = FDC_UpdateRestoreCmd();
				break;
			case FDCEMU_CMD_SEEK:
				FdcCycles = FDC_UpdateSeekCmd();
				break;
			case FDCEMU_CMD_STEP:
				FdcCycles = FDC_UpdateStepCmd();
				break;

			case FDCEMU_CMD_READSECTORS:
				FdcCycles = FDC_UpdateReadSectorsCmd();
				break;
			case FDCEMU_CMD_WRITESECTORS:
				FdcCycles = FDC_UpdateWriteSectorsCmd();
				break;

			case FDCEMU_CMD_READADDRESS:
				FdcCycles = FDC_UpdateReadAddressCmd();
				break;

			case FDCEMU_CMD_READTRACK:
				FdcCycles = FDC_UpdateReadTrackCmd();
				break;

			case FDCEMU_CMD_WRITETRACK:
				FdcCycles = FDC_UpdateWriteTrackCmd();
				break;

			case FDCEMU_CMD_MOTOR_STOP:
				FdcCycles = FDC_UpdateMotorStop();
				break;
			}
		}
	}
	while ( ( FDC.Command != FDCEMU_CMD_NULL ) && ( FdcCycles == 0 ) );

	if ( FDC.Command != FDCEMU_CMD_NULL )
	{
		FDC_StartTimer_FdcCycles ( FdcCycles , -PendingCyclesOver );
	}
}


/*-----------------------------------------------------------------------*/
/**
 * Return the type of a command, based on the upper bits of CR
 */
uint8_t FDC_GetCmdType ( uint8_t CR )
{
	if ( ( CR & 0x80 ) == 0 )					/* Type I - Restore, Seek, Step, Step-In, Step-Out */
		return 1;
	else if ( ( CR & 0x40 ) == 0 )					/* Type II - Read Sector, Write Sector */
		return 2;
	else if ( ( CR & 0xf0 ) != 0xd0 )				/* Type III - Read Address, Read Track, Write Track */
		return 3;
	else								/* Type IV - Force Interrupt */
		return 4;
}


/*-----------------------------------------------------------------------*/
/**
 * Update the FDC's Status Register.
 * All bits in DisableBits are cleared in STR, then all bits in EnableBits
 * are set in STR.
 */
static void FDC_Update_STR ( uint8_t DisableBits , uint8_t EnableBits )
{
	FDC.STR &= (~DisableBits);					/* Clear bits in DisableBits */
	FDC.STR |= EnableBits;						/* Set bits in EnableBits */

	FDC_Drive_Set_BusyLed ( FDC.STR );
//fprintf ( stderr , "fdc str 0x%x\n" , FDC.STR );
}


/*-----------------------------------------------------------------------*/
/**
 * Common to all commands once they're completed :
 * - remove busy bit
 * - set interrupt if necessary
 * - stop motor after 2 sec
 */
static int FDC_CmdCompleteCommon ( bool DoInt )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
	LOG_TRACE(TRACE_FDC, "fdc complete command VBL=%d video_cyc=%d %d@%d pc=%x\n",
		nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	FDC_Update_STR ( FDC_STR_BIT_BUSY , 0 );			/* Remove busy bit */

	if ( DoInt )
		FDC_SetIRQ ( FDC_IRQ_SOURCE_COMPLETE );

	FDC.Command = FDCEMU_CMD_MOTOR_STOP;				/* Fake command to stop the motor */
	FDC.CommandState = FDCEMU_RUN_MOTOR_STOP;
	return FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
}


/*-----------------------------------------------------------------------*/
/**
 * Verify track after a type I command.
 * The FDC will read the first ID field of the current track and will
 * compare the track number in this ID field with the current Track Register.
 * If they don't match, we try again with the next ID field until we
 * reach 5 revolutions, in which case we set RNF.
 *
 * NOTE [NP] : in the case of Hatari when using ST/MSA images, the track is always the correct one,
 * so the verify will always be good (except if no disk is inserted or the physical head is
 * not on the same track as FDC.TR)
 * For STX images, verify track might fail on purpose with some protection
 */
static bool FDC_VerifyTrack ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	Next_TR;
	uint8_t	Next_CRC_OK;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Return false if no drive selected, or drive not enabled, or no disk in drive */
	if ( ( FDC.DriveSelSignal < 0 ) || ( !FDC_DRIVES[ FDC.DriveSelSignal ].Enabled )
		|| ( !FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted ) )
	{
		LOG_TRACE(TRACE_FDC, "fdc type I verify track failed disabled/empty drive=%d VBL=%d video_cyc=%d %d@%d pc=%x\n",
			FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
		return false;
	}

	/* Check if the current ID Field is the one we're looking for (same track and correct CRC) */
	if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
	{
		Next_TR = FDC_NextSectorID_TR_STX ();
		Next_CRC_OK = FDC_NextSectorID_CRC_OK_STX ();
	}
	else
	{
		Next_TR = FDC_NextSectorID_TR_ST ();
		Next_CRC_OK = FDC_NextSectorID_CRC_OK_ST ();
	}

	/* ST/MSA image will always be correct, only STX can fail depending on some protections */
	if ( ( Next_TR != FDC.TR ) || ( Next_CRC_OK == 0 ) )
	{
		LOG_TRACE(TRACE_FDC, "fdc type I verify track failed ID_TR=0x%x TR=0x%x crc_ok=%d head=0x%x drive=%d VBL=%d video_cyc=%d %d@%d pc=%x\n",
			Next_TR , FDC.TR , Next_CRC_OK , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.DriveSelSignal ,
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		return false;
	}

	/* If disk image has only one side or drive is single sided and we're trying to verify on 2nd side, then return false */
	if ( ( FDC.SideSignal == 1  )
	  && ( ( FDC_GetSidesPerDisk ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ) != 2 )
	    || ( FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads == 1 ) ) )
	{
		LOG_TRACE(TRACE_FDC, "fdc type I verify track failed TR=0x%x head=0x%x side=1 doesn't exist drive=%d VBL=%d video_cyc=%d %d@%d pc=%x\n",
			FDC.TR , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.DriveSelSignal ,
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		return false;
	}

	/* The track is the correct one */
	return true;
}


/*-----------------------------------------------------------------------*/
/**
 * Run the 'motor stop' sequence : wait for 9 revolutions (1.8 sec)
 * and stop the motor.
 * We clear motor bit, but spinup bit remains to 1 (verified on a real STF)
 */
static int FDC_UpdateMotorStop ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_MOTOR_STOP:
		FDC.IndexPulse_Counter = 0;
		FDC.CommandState = FDCEMU_RUN_MOTOR_STOP_WAIT;
		/* Fall through to next state */
	 case FDCEMU_RUN_MOTOR_STOP_WAIT:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_MOTOR_OFF )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _COMPLETE state */
	 case FDCEMU_RUN_MOTOR_STOP_COMPLETE:
		Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
		LOG_TRACE(TRACE_FDC, "fdc motor stopped VBL=%d video_cyc=%d %d@%d pc=%x\n",
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC.IndexPulse_Counter = 0;
		if ( FDC.DriveSelSignal >= 0 )                                  /* A drive was previously enabled */
			FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time = 0;   /* Stop counting index pulses on the drive */

		FDC_Update_STR ( FDC_STR_BIT_MOTOR_ON , 0 );		/* Unset motor bit and keep spin up bit */
		FDC.Command = FDCEMU_CMD_NULL;				/* Motor stopped, this is the last state */
		FdcCycles = 0;
		break;
	}
	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'RESTORE' command
 */
static int FDC_UpdateRestoreCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_MOTOR_ON;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_MOTOR_ON:
		FDC_Update_STR ( 0 , FDC_STR_BIT_SPIN_UP );		/* At this point, spin up sequence is ok */
		FDC.ReplaceCommandPossible = false;

		/* The FDC will try 255 times to reach track 0 using step out signals */
		/* If track 0 signal is not detected after 255 attempts, the command is interrupted */
		/* and FDC_STR_BIT_RNF is set in the Status Register. */
		/* This can happen if no drive is selected or if the selected drive is disabled */
		/* TR should be set to 255 once the spin-up sequence is made and the command */
		/* can't be interrupted anymore by another command (else TR value will be wrong */
		/* for other type I commands) */
		FDC.TR = 0xff;				
		FDC.CommandState = FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_LOOP;
		/* Fall through to the _LOOP state */
	 case FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO_LOOP:
		if ( FDC.TR == 0 )					/* Track 0 not reached after 255 attempts ? */
		{							/* (this can happen if the drive is disabled) */
			FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );
			FDC_Update_STR ( FDC_STR_BIT_TR00 , 0 );	/* Unset bit TR00 */
			FdcCycles = FDC_CmdCompleteCommon( true );
			break;
		}

		if ( ( FDC.DriveSelSignal < 0 ) || ( !FDC_DRIVES[ FDC.DriveSelSignal ].Enabled )
			|| ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack != 0 ) )	/* Are we at track zero ? */
		{
			FDC_Update_STR ( FDC_STR_BIT_TR00 , 0 );	/* Unset bit TR00 */
			FDC.TR--;					/* One less attempt */
			if ( ( FDC.DriveSelSignal >= 0 ) && ( FDC_DRIVES[ FDC.DriveSelSignal ].Enabled ) )
			{
				FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack--;	/* Move physical head only if an enabled drive is selected */
				FDC_UpdateFloppyDensity ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
			}
			FdcCycles = FDC_DelayToFdcCycles ( FDC_StepRate_ms[ FDC_STEP_RATE ] * 1000 );
		}
		else							/* Drive is enabled and head is at track 0 */
		{
			FDC_Update_STR ( 0 , FDC_STR_BIT_TR00 );	/* Set bit TR00 */
			FDC.TR = 0;					/* Update Track Register to 0 */
			FDC.CommandState = FDCEMU_RUN_RESTORE_VERIFY;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_RESTORE_VERIFY:
		if ( FDC.CR & FDC_COMMAND_BIT_VERIFY )
		{
			FDC.CommandState = FDCEMU_RUN_RESTORE_VERIFY_HEAD_OK;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_RESTORE_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_RESTORE_VERIFY_HEAD_OK:
		FDC.IndexPulse_Counter = 0;
		/* Head OK, fall through and look for sector header */
	 case FDCEMU_RUN_RESTORE_VERIFY_NEXT_SECTOR_HEADER:
		/* If 'verify' doesn't succeed after 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			LOG_TRACE(TRACE_FDC, "fdc type I restore track=%d drive=%d verify RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );			/* Set RNF bit */
			FDC.CommandState = FDCEMU_RUN_RESTORE_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FDC.CommandState = FDCEMU_RUN_RESTORE_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field and skip 10 more bytes to read the whole ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 10 );		/* Add delay to read 3xA1, FE, ID field */
			FDC.CommandState = FDCEMU_RUN_RESTORE_VERIFY_CHECK_SECTOR_HEADER;
		}
		break;
	 case FDCEMU_RUN_RESTORE_VERIFY_CHECK_SECTOR_HEADER:
		/* Check if the current ID Field matches the track number */
		if ( FDC_VerifyTrack () )
		{
			FDC_Update_STR ( FDC_STR_BIT_RNF , 0 );			/* Track is correct, remove RNF bit */
			FDC.CommandState = FDCEMU_RUN_RESTORE_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		else
		{
			/* Verify failed with this ID field ; check the next one */
			FDC.CommandState = FDCEMU_RUN_RESTORE_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_RESTORE_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'SEEK' command
 */
static int FDC_UpdateSeekCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_SEEK_TOTRACK:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_TOTRACK_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_TOTRACK_MOTOR_ON;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_SEEK_TOTRACK_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_SEEK_TOTRACK_MOTOR_ON:
		FDC_Update_STR ( 0 , FDC_STR_BIT_SPIN_UP );		/* At this point, spin up sequence is ok */
		FDC.ReplaceCommandPossible = false;

		if ( FDC.TR == FDC.DR )					/* Are we at the selected track ? */
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		else
		{
			if ( FDC.DR < FDC.TR )				/* Set StepDirection to the correct value */
				FDC.StepDirection = -1;
			else
				FDC.StepDirection = 1;

			/* Move head by one track depending on FDC.StepDirection and update Track Register */
			FDC.TR += FDC.StepDirection;

			FdcCycles = FDC_DelayToFdcCycles ( FDC_StepRate_ms[ FDC_STEP_RATE ] * 1000 );
			FDC_Update_STR ( FDC_STR_BIT_TR00 , 0 );	/* By default, unset bit TR00 */

			/* Check / move physical head only if an enabled drive is selected */
			if ( ( FDC.DriveSelSignal >= 0 ) && ( FDC_DRIVES[ FDC.DriveSelSignal ].Enabled ) )
			{
				if ( ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == FDC_PHYSICAL_MAX_TRACK ) && ( FDC.StepDirection == 1 ) )
				{
					FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY;
					FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;	/* No delay if trying to go after max track */
				}

				else if ( ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == 0 ) && ( FDC.StepDirection == -1 ) )
				{
					FDC.TR = 0;			/* If we reach track 0, we stop there */
					FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY;
					FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
				}

				else
				{
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack += FDC.StepDirection;	/* Move physical head */
					FDC_UpdateFloppyDensity ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
				}

				if ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == 0 )
					FDC_Update_STR ( 0 , FDC_STR_BIT_TR00 );	/* Set bit TR00 */
			}
		}

		break;
	 case FDCEMU_RUN_SEEK_VERIFY:
		if ( FDC.CR & FDC_COMMAND_BIT_VERIFY )
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY_HEAD_OK;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_SEEK_VERIFY_HEAD_OK:
		FDC.IndexPulse_Counter = 0;
		/* Head OK, fall through and look for sector header */
	 case FDCEMU_RUN_SEEK_VERIFY_NEXT_SECTOR_HEADER:
		/* If 'verify' doesn't succeed after 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			LOG_TRACE(TRACE_FDC, "fdc type I seek track=%d drive=%d verify RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );			/* Set RNF bit */
			FDC.CommandState = FDCEMU_RUN_SEEK_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field and skip 10 more bytes to read the whole ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 10 );		/* Add delay to read 3xA1, FE, ID field */
			FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY_CHECK_SECTOR_HEADER;
		}
		break;
	 case FDCEMU_RUN_SEEK_VERIFY_CHECK_SECTOR_HEADER:
		/* Check if the current ID Field matches the track number */
		if ( FDC_VerifyTrack () )
		{
			FDC_Update_STR ( FDC_STR_BIT_RNF , 0 );			/* Track is correct, remove RNF bit */
			FDC.CommandState = FDCEMU_RUN_SEEK_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		else
		{
			/* Verify failed with this ID field ; check the next one */
			FDC.CommandState = FDCEMU_RUN_SEEK_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_SEEK_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'STEP' command
 */
static int FDC_UpdateStepCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_STEP_ONCE:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_STEP_ONCE_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_STEP_ONCE_MOTOR_ON;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_STEP_ONCE_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_STEP_ONCE_MOTOR_ON:
		FDC_Update_STR ( 0 , FDC_STR_BIT_SPIN_UP );		/* At this point, spin up sequence is ok */
		FDC.ReplaceCommandPossible = false;

		/* Move head by one track depending on FDC.StepDirection */
		if ( FDC.CR & FDC_COMMAND_BIT_UPDATE_TRACK )
			FDC.TR += FDC.StepDirection;			/* Update Track Register */

		FdcCycles = FDC_DelayToFdcCycles ( FDC_StepRate_ms[ FDC_STEP_RATE ] * 1000 );
		FDC_Update_STR ( FDC_STR_BIT_TR00 , 0 );		/* By default, unset bit TR00 */

		/* Check / move physical head only if an enabled drive is selected */
		if ( ( FDC.DriveSelSignal >= 0 ) && ( FDC_DRIVES[ FDC.DriveSelSignal ].Enabled ) )
		{
			if ( ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == FDC_PHYSICAL_MAX_TRACK ) && ( FDC.StepDirection == 1 ) )
				FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;	/* No delay if trying to go after max track */

			else if ( ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == 0 ) && ( FDC.StepDirection == -1 ) )
				FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;	/* No delay if trying to go before track 0 */

			else
			{
				FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack += FDC.StepDirection;	/* Move physical head */
				FDC_UpdateFloppyDensity ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
			}

			if ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == 0 )
				FDC_Update_STR ( 0 , FDC_STR_BIT_TR00 );	/* Set bit TR00 */
		}

		FDC.CommandState = FDCEMU_RUN_STEP_VERIFY;
		break;
	 case FDCEMU_RUN_STEP_VERIFY:
		if ( FDC.CR & FDC_COMMAND_BIT_VERIFY )
		{
			FDC.CommandState = FDCEMU_RUN_STEP_VERIFY_HEAD_OK;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_STEP_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_STEP_VERIFY_HEAD_OK:
		FDC.IndexPulse_Counter = 0;
		/* Head OK, fall through and look for sector header */
	 case FDCEMU_RUN_STEP_VERIFY_NEXT_SECTOR_HEADER:
		/* If 'verify' doesn't succeed after 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			LOG_TRACE(TRACE_FDC, "fdc type I step track=%d drive=%d verify RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );			/* Set RNF bit */
			FDC.CommandState = FDCEMU_RUN_STEP_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FDC.CommandState = FDCEMU_RUN_STEP_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field and skip 10 more bytes to read the whole ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 10 );		/* Add delay to read 3xA1, FE, ID field */
			FDC.CommandState = FDCEMU_RUN_STEP_VERIFY_CHECK_SECTOR_HEADER;
		}
		break;
	 case FDCEMU_RUN_STEP_VERIFY_CHECK_SECTOR_HEADER:
		/* Check if the current ID Field matches the track number */
		if ( FDC_VerifyTrack () )
		{
			FDC_Update_STR ( FDC_STR_BIT_RNF , 0 );			/* Track is correct, remove RNF bit */
			FDC.CommandState = FDCEMU_RUN_STEP_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		else
		{
			/* Verify failed with this ID field ; check the next one */
			FDC.CommandState = FDCEMU_RUN_STEP_VERIFY_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_STEP_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'READ SECTOR/S' command
 */
static int FDC_UpdateReadSectorsCmd ( void )
{
	int	FdcCycles = 0;
	int	SectorSize;
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	Next_TR;
	uint8_t	Next_SR;
	uint8_t	Next_CRC_OK;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );


	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_READSECTORS_READDATA:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_HEAD_LOAD;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_READSECTORS_READDATA_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _HEAD_LOAD state */
	 case FDCEMU_RUN_READSECTORS_READDATA_HEAD_LOAD:
		if ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD )
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_MOTOR_ON;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
			break;
		}
		/* If there's no head settle, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_READSECTORS_READDATA_MOTOR_ON:
		FDC.ReplaceCommandPossible = false;
		FDC.IndexPulse_Counter = 0;
		FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_NEXT_SECTOR_HEADER;
		FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		break;
	 case FDCEMU_RUN_READSECTORS_READDATA_NEXT_SECTOR_HEADER:
		/* If we're looking for sector FDC.SR for more than 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_RNF;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field and skip 10 more bytes to read the whole ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 10 );		/* Add delay to read 3xA1, FE, TR, SIDE, SR, LEN, CRC1, CRC2 */
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_CHECK_SECTOR_HEADER;
		}
		break;
	 case FDCEMU_RUN_READSECTORS_READDATA_CHECK_SECTOR_HEADER:
		/* Check if the current ID Field is the one we're looking for (same track/sector and correct CRC) */
		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
		{
			Next_TR = FDC_NextSectorID_TR_STX ();
			Next_SR = FDC_NextSectorID_SR_STX ();
			Next_CRC_OK = FDC_NextSectorID_CRC_OK_STX ();
		}
		else
		{
			Next_TR = FDC_NextSectorID_TR_ST ();
			Next_SR = FDC_NextSectorID_SR_ST ();
			Next_CRC_OK = FDC_NextSectorID_CRC_OK_ST ();
		}
		if ( ( Next_TR == FDC.TR ) && ( Next_SR == FDC.SR ) && ( Next_CRC_OK ) )
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_START;
			/* Read bytes to reach the sector's data : GAP3a + GAP3b + 3xA1 + FB */
			FdcCycles = FDC_TransferByte_FdcCycles ( FDC_TRACK_LAYOUT_STANDARD_GAP3a + FDC_TRACK_LAYOUT_STANDARD_GAP3b + 3 + 1 );
		}
		else
		{
			/* This is not the ID field we're looking for ; check the next one */
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_START:
		/* Read a single sector into temporary buffer (512 bytes for ST/MSA) */
		FDC_Buffer_Reset();

		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FDC.Status_Temp = FDC_ReadSector_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SR , FDC.SideSignal , &SectorSize );
		else
			FDC.Status_Temp = FDC_ReadSector_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SR , FDC.SideSignal , &SectorSize );

		if ( FDC.Status_Temp & FDC_STR_BIT_RNF )		/* Sector FDC.SR was not found */
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_RNF;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		else
		{
			if ( FDC.Status_Temp & FDC_STR_BIT_RECORD_TYPE )
				FDC_Update_STR ( 0 , FDC_STR_BIT_RECORD_TYPE );
			else
				FDC_Update_STR ( FDC_STR_BIT_RECORD_TYPE , 0 );

			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_LOOP;
			FdcCycles = FDC_Buffer_Read_Timing ();		/* Delay to transfer the first byte */
		}
		break;
	 case FDCEMU_RUN_READSECTORS_READDATA_TRANSFER_LOOP:
		/* Transfer the sector 1 byte at a time using DMA */
		FDC_DMA_FIFO_Push ( FDC_Buffer_Read_Byte () );		/* Add 1 byte to the DMA FIFO */
		if ( FDC_BUFFER.PosRead < FDC_Buffer_Get_Size () )
		{
			FdcCycles = FDC_Buffer_Read_Timing ();		/* Delay to transfer the next byte */
		}
		else							/* Sector transferred, check the CRC */
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_CRC;
			FdcCycles = FDC_TransferByte_FdcCycles ( 2 );	/* Read 2 bytes for CRC */
		}
		break;
	 case FDCEMU_RUN_READSECTORS_CRC:
		/* Sector completely transferred, CRC is always good for ST/MSA, but not always for STX */
		if ( FDC.Status_Temp & FDC_STR_BIT_CRC_ERROR )
		{
			LOG_TRACE(TRACE_FDC, "fdc type II read sector=%d track=0x%x side=%d drive=%d CRC VBL=%d video_cyc=%d %d@%d pc=%x\n",
				  FDC.SR , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_CRC_ERROR );
			FdcCycles = FDC_CmdCompleteCommon( true );
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_MULTI;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_READSECTORS_MULTI:
		/* Check for multi bit */
		if ( FDC.CR & FDC_COMMAND_BIT_MULTIPLE_SECTOR  )
		{
			FDC.SR++;					/* Try to read next sector and set RNF if not possible */
			FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA_MOTOR_ON;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
			LOG_TRACE(TRACE_FDC, "fdc type II read sector with multi sector=0x%x track=0x%x side=%d drive=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC.SR, FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal ,
				FDC_GetDMAAddress(), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
		}
		else							/* Multi=0, stop here with no error */
		{
			FDC.CommandState = FDCEMU_RUN_READSECTORS_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_READSECTORS_RNF:
		LOG_TRACE(TRACE_FDC, "fdc type II read sector=%d track=0x%x side=%d drive=%d RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
			  FDC.SR , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	 case FDCEMU_RUN_READSECTORS_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'WRITE SECTOR/S' command
 */
static int FDC_UpdateWriteSectorsCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	Next_TR;
	uint8_t	Next_SR;
	uint8_t	Next_LEN;
	uint8_t	Next_CRC_OK;
	uint8_t	Byte;
	uint8_t	Status;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Stop now if disk is write protected */
	if ( ( FDC.DriveSelSignal >= 0 ) && ( FDC_DRIVES[ FDC.DriveSelSignal ].Enabled )
		&& ( FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted )
		&& ( Floppy_IsWriteProtected ( FDC.DriveSelSignal ) ) )
	{
		LOG_TRACE(TRACE_FDC, "fdc type II write sector=%d track=0x%x side=%d drive=%d WPRT VBL=%d video_cyc=%d %d@%d pc=%x\n",
			  FDC.SR , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC_Update_STR ( 0 , FDC_STR_BIT_WPRT );		/* Set WPRT bit */
		FdcCycles = FDC_CmdCompleteCommon( true );
	}
	else
		FDC_Update_STR ( FDC_STR_BIT_WPRT , 0 );		/* Unset WPRT bit */

	
	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_HEAD_LOAD;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _HEAD_LOAD state */
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_HEAD_LOAD:
		if ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD )
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_MOTOR_ON;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
			break;
		}
		/* If there's no head settle, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_MOTOR_ON:
		FDC.ReplaceCommandPossible = false;
		FDC.IndexPulse_Counter = 0;
		FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_NEXT_SECTOR_HEADER;
		FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		break;
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_NEXT_SECTOR_HEADER:
		/* If we're looking for sector FDC.SR for more than 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_RNF;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field and skip 10 more bytes to read the whole ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 10 );		/* Add delay to read 3xA1, FE, TR, SIDE, SR, LEN, CRC1, CRC2 */
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_CHECK_SECTOR_HEADER;
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_CHECK_SECTOR_HEADER:
		/* Check if the current ID Field is the one we're looking for (same track/sector and correct CRC) */
		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
		{
			Next_TR = FDC_NextSectorID_TR_STX ();
			Next_SR = FDC_NextSectorID_SR_STX ();
			Next_CRC_OK = FDC_NextSectorID_CRC_OK_STX ();
		}
		else
		{
			Next_TR = FDC_NextSectorID_TR_ST ();
			Next_SR = FDC_NextSectorID_SR_ST ();
			Next_CRC_OK = FDC_NextSectorID_CRC_OK_ST ();
		}
		if ( ( Next_TR == FDC.TR ) && ( Next_SR == FDC.SR ) && ( Next_CRC_OK ) )
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_START;
			/* Read bytes to reach the sector's data : GAP3a + GAP3b + 3xA1 + FB */
			FdcCycles = FDC_TransferByte_FdcCycles ( FDC_TRACK_LAYOUT_STANDARD_GAP3a + FDC_TRACK_LAYOUT_STANDARD_GAP3b + 3 + 1 );
		}
		else
		{
			/* This is not the ID field we're looking for ; check the next one */
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_NEXT_SECTOR_HEADER;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_START:
		/* Write a single sector from RAM (512 bytes for ST/MSA) */
		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			Next_LEN = FDC_NextSectorID_LEN_STX ();
		else
			Next_LEN = FDC_NextSectorID_LEN_ST ();

		FDC_Buffer_Reset();
		FDC_DMA.BytesToTransfer = 128 << ( Next_LEN & FDC_SECTOR_SIZE_MASK );

		FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_LOOP;
		FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		break;
	 case FDCEMU_RUN_WRITESECTORS_WRITEDATA_TRANSFER_LOOP:
		/* Transfer the sector 1 byte at a time using DMA */
		if ( FDC_DMA.BytesToTransfer-- > 0 )
		{
			Byte = FDC_DMA_FIFO_Pull ();			/* Get 1 byte from the DMA FIFO */
//fprintf ( stderr , "byte %d %x\n" , FDC_DMA.BytesToTransfer , Byte );
			FDC_Buffer_Add ( Byte );
			FdcCycles = FDC_TransferByte_FdcCycles ( 1 );
		}
		else							/* Sector transferred, add the CRC */
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_CRC;
			FdcCycles = FDC_TransferByte_FdcCycles ( 2 );	/* Write 2 bytes for CRC */
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_CRC:
		/* Sector completely transferred, CRC is always good for ST/MSA */
		/* This is where we save the buffer to the disk image */

		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			Status = FDC_WriteSector_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SR , FDC.SideSignal , FDC_Buffer_Get_Size () );
		else
			Status = FDC_WriteSector_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SR , FDC.SideSignal , FDC_Buffer_Get_Size () );

		if ( Status & FDC_STR_BIT_RNF )				/* Sector FDC.SR was not correctly written */
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_RNF;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_MULTI;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_MULTI:
		/* Check for multi bit */
		if ( FDC.CR & FDC_COMMAND_BIT_MULTIPLE_SECTOR  )
		{
			FDC.SR++;					/* Try to write next sector and set RNF if not possible */
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA_MOTOR_ON;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
			LOG_TRACE(TRACE_FDC, "fdc type II write sector with multi sector=0x%x track=0x%x side=%d drive=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC.SR, FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal ,
				FDC_GetDMAAddress(), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
		}
		else							/* Multi=0, stop here with no error */
		{
			FDC.CommandState = FDCEMU_RUN_WRITESECTORS_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_WRITESECTORS_RNF:
		LOG_TRACE(TRACE_FDC, "fdc type II write sector=%d track=0x%x side=%d drive=%d RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
			  FDC.SR , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	 case FDCEMU_RUN_WRITESECTORS_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'READ ADDRESS' command
 */
static int FDC_UpdateReadAddressCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_READADDRESS:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_READADDRESS_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READADDRESS_HEAD_LOAD;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_READADDRESS_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _HEAD_LOAD state */
	 case FDCEMU_RUN_READADDRESS_HEAD_LOAD:
		FDC.ReplaceCommandPossible = false;
		if ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD )
		{
			FDC.CommandState = FDCEMU_RUN_READADDRESS_MOTOR_ON;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
			break;
		}
		/* If there's no head settle, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_READADDRESS_MOTOR_ON:
		FDC.ReplaceCommandPossible = false;
		FDC.IndexPulse_Counter = 0;
		FDC.CommandState = FDCEMU_RUN_READADDRESS_NEXT_SECTOR_HEADER;
		FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		break;
	 case FDCEMU_RUN_READADDRESS_NEXT_SECTOR_HEADER:
		/* If we don't find a sector header after more than 5 revolutions, we abort with RNF */
		if ( FDC.IndexPulse_Counter >= FDC_DELAY_IP_ADDRESS_ID )
		{
			FDC.CommandState = FDCEMU_RUN_READADDRESS_RNF;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
			break;
		}

		if ( FDC.DriveSelSignal < 0 )					/* No drive selected */
			FdcCycles = -1;
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FdcCycles = FDC_NextSectorID_FdcCycles_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		else
			FdcCycles = FDC_NextSectorID_FdcCycles_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads ,
					FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		if ( FdcCycles < 0 )
		{
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			/* Read bytes to reach the next sector's ID field */
			FdcCycles += FDC_TransferByte_FdcCycles ( 4 );		/* Add delay to read 3xA1, FE */
			FDC.CommandState = FDCEMU_RUN_READADDRESS_TRANSFER_START;
		}
		break;
	 case FDCEMU_RUN_READADDRESS_TRANSFER_START:
		/* Read the ID field into buffer */
		FDC_Buffer_Reset();

		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			FDC.Status_Temp = FDC_ReadAddress_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				     FDC_NextSectorID_SR_STX () , FDC.SideSignal );
		else
			FDC.Status_Temp = FDC_ReadAddress_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				     FDC_NextSectorID_SR_ST () , FDC.SideSignal );

		FDC.SR = FDC_Buffer_Read_Byte_pos ( 0 );		/* The 1st byte of the ID field is also copied into Sector Register */

		FDC.CommandState = FDCEMU_RUN_READADDRESS_TRANSFER_LOOP;
		FdcCycles = FDC_Buffer_Read_Timing ();			/* Delay to transfer the first byte */
		break;
	 case FDCEMU_RUN_READADDRESS_TRANSFER_LOOP:
		/* Transfer the ID field 1 byte at a time using DMA */
		FDC_DMA_FIFO_Push ( FDC_Buffer_Read_Byte () );		/* Add 1 byte to the DMA FIFO */
		if ( FDC_BUFFER.PosRead < FDC_Buffer_Get_Size () )
		{
			FdcCycles = FDC_Buffer_Read_Timing ();		/* Delay to transfer the next byte */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READADDRESS_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_READADDRESS_RNF:
		LOG_TRACE(TRACE_FDC, "fdc type III read address track=0x%x side=%d drive=%d RNF VBL=%d video_cyc=%d %d@%d pc=%x\n",
			  FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , FDC.DriveSelSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC_Update_STR ( 0 , FDC_STR_BIT_RNF );
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	 case FDCEMU_RUN_READADDRESS_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'READ TRACK' command
 */
static int FDC_UpdateReadTrackCmd ( void )
{
	int	FdcCycles = 0;
	int	i;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_READTRACK:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_READTRACK_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READTRACK_HEAD_LOAD;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_READTRACK_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _HEAD_LOAD state */
	 case FDCEMU_RUN_READTRACK_HEAD_LOAD:
		FDC.ReplaceCommandPossible = false;
		if ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD )
		{
			FDC.CommandState = FDCEMU_RUN_READTRACK_MOTOR_ON;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
			break;
		}
		/* If there's no head settle, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_READTRACK_MOTOR_ON:
		FdcCycles = FDC_NextIndexPulse_FdcCycles ();		/* Wait for the next index pulse */
//fprintf ( stderr , "read tr idx=%d %d\n" , FDC_IndexPulse_GetState() , FdcCycles );
		if ( FdcCycles < 0 )
		{
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_READTRACK_INDEX;
		}
		break;
	 case FDCEMU_RUN_READTRACK_INDEX:
		/* At this point, we have a valid drive/floppy, build the track data */
		FDC_Buffer_Reset();

		if ( ( ( FDC.SideSignal == 1 )				/* Try to read side 1 on a disk that doesn't have 2 sides or drive is single sided */
			&& ( ( FDC_GetSidesPerDisk ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ) != 2 )
			  || ( FDC_DRIVES[ FDC.DriveSelSignal ].NumberOfHeads == 1 ) ) )
		    || ( FDC_MachineHandleDensity ( FDC.DriveSelSignal ) == false ) ) 	/* Can't handle the floppy's density */
		{
			LOG_TRACE(TRACE_FDC, "fdc type III read track drive=%d track=%d side=%d, side not found or wrong density VBL=%d video_cyc=%d %d@%d pc=%x\n",
				  FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal ,
				  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			for ( i=0 ; i<FDC_GetBytesPerTrack ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal ) ; i++ )
				FDC_Buffer_Add ( Hatari_rand() & 0xff ); /* Fill the track buffer with random bytes */
		}
		else if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
		{
			FDC.Status_Temp = FDC_ReadTrack_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		}
		else							/* Track/side available in the disk image */
		{
			FDC.Status_Temp = FDC_ReadTrack_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );
		}

		FDC.CommandState = FDCEMU_RUN_READTRACK_TRANSFER_LOOP;
		FdcCycles = FDC_Buffer_Read_Timing ();			/* Delay to transfer the first byte */
		break;
	 case FDCEMU_RUN_READTRACK_TRANSFER_LOOP:
		/* Transfer the track 1 byte at a time using DMA */
		FDC_DMA_FIFO_Push ( FDC_Buffer_Read_Byte () );		/* Add 1 byte to the DMA FIFO */
		if ( FDC_BUFFER.PosRead < FDC_Buffer_Get_Size () )
		{
			FdcCycles = FDC_Buffer_Read_Timing ();		/* Delay to transfer the next byte */
		}
		else							/* Track completely transferred */
		{
			FDC.CommandState = FDCEMU_RUN_READTRACK_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_COMPLETE;
		}
		break;
	 case FDCEMU_RUN_READTRACK_COMPLETE:
		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Run 'WRITE TRACK' command
 */
static int FDC_UpdateWriteTrackCmd ( void )
{
	int	FdcCycles = 0;
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	Byte;
	uint8_t	Status;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
	
	/* Which command is running? */
	switch (FDC.CommandState)
	{
	 case FDCEMU_RUN_WRITETRACK:
		if ( FDC_Set_MotorON ( FDC.CR ) )
		{
			FDC.CommandState = FDCEMU_RUN_WRITETRACK_SPIN_UP;
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Spin up needed */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_WRITETRACK_HEAD_LOAD;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;		/* No spin up needed */
		}
		break;
	 case FDCEMU_RUN_WRITETRACK_SPIN_UP:
		if ( FDC.IndexPulse_Counter < FDC_DELAY_IP_SPIN_UP )
		{
			FdcCycles = FDC_DELAY_CYCLE_REFRESH_INDEX_PULSE;	/* Wait for the correct number of IP */
			break;
		}
		/* If IndexPulse_Counter reached, we fall through directly to the _HEAD_LOAD state */
	 case FDCEMU_RUN_WRITETRACK_HEAD_LOAD:
		FDC.ReplaceCommandPossible = false;
		if ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD )
		{
			FDC.CommandState = FDCEMU_RUN_WRITETRACK_MOTOR_ON;
			FdcCycles = FDC_DelayToFdcCycles ( FDC_DELAY_US_HEAD_LOAD );	/* Head settle delay */
			break;
		}
		/* If there's no head settle, we fall through directly to the _MOTOR_ON state */
	 case FDCEMU_RUN_WRITETRACK_MOTOR_ON:
		FdcCycles = FDC_NextIndexPulse_FdcCycles ();		/* Wait for the next index pulse */
//fprintf ( stderr , "write tr idx=%d %d\n" , FDC_IndexPulse_GetState() , FdcCycles );
		if ( FdcCycles < 0 )
		{
			FdcCycles = FDC_DELAY_CYCLE_WAIT_NO_DRIVE_FLOPPY;	/* Wait for a valid drive/floppy */
		}
		else
		{
			FDC.CommandState = FDCEMU_RUN_WRITETRACK_INDEX;
		}
		break;
	 case FDCEMU_RUN_WRITETRACK_INDEX:
		if ( FDC_MachineHandleDensity ( FDC.DriveSelSignal ) == false )	/* Can't handle the floppy's density */
		{
			LOG_TRACE(TRACE_FDC, "fdc type III write track drive=%d track=0x%x side=%d wrong density VBL=%d video_cyc=%d %d@%d pc=%x\n",
				  FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_LOST_DATA );	/* Set LOST_DATA bit */
			FdcCycles = FDC_CmdCompleteCommon( true );
			break;
		}

		/* At this point, we have a valid drive/floppy, check write protection and write the track data */
		if ( Floppy_IsWriteProtected ( FDC.DriveSelSignal ) )
		{
			LOG_TRACE(TRACE_FDC, "fdc type III write track drive=%d track=0x%x side=%d WPRT VBL=%d video_cyc=%d %d@%d pc=%x\n",
				  FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal , nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

			FDC_Update_STR ( 0 , FDC_STR_BIT_WPRT );	/* Set WPRT bit */
			FdcCycles = FDC_CmdCompleteCommon( true );
			break;
		}

		FDC_Update_STR ( FDC_STR_BIT_WPRT , 0 );		/* Unset WPRT bit */

		FDC_Buffer_Reset();
		FDC_DMA.BytesToTransfer = FDC_GetBytesPerTrack ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack , FDC.SideSignal );

		FDC.CommandState = FDCEMU_RUN_WRITETRACK_TRANSFER_LOOP;
		FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		break;
	 case FDCEMU_RUN_WRITETRACK_TRANSFER_LOOP:
		/* Transfer the track 1 byte at a time using DMA */
		if ( FDC_DMA.BytesToTransfer-- > 0 )
		{
			Byte = FDC_DMA_FIFO_Pull ();			/* Get 1 byte from the DMA FIFO */
//fprintf ( stderr , "byte %d %x\n" , FDC_DMA.BytesToTransfer , Byte );
			FDC_Buffer_Add ( Byte );
			FdcCycles = FDC_TransferByte_FdcCycles ( 1 );
		}
		else							/* Track written */
		{
			FDC.CommandState = FDCEMU_RUN_WRITETRACK_COMPLETE;
			FdcCycles = FDC_DELAY_CYCLE_COMMAND_IMMEDIATE;
		}
		break;
	 case FDCEMU_RUN_WRITETRACK_COMPLETE:
		/* Track completely transferred */
		/* This is where we save the buffer to the disk image */
		if ( EmulationDrives[ FDC.DriveSelSignal ].ImageType == FLOPPY_IMAGE_TYPE_STX )
			Status = FDC_WriteTrack_STX ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SideSignal , FDC_Buffer_Get_Size () );
		else
			Status = FDC_WriteTrack_ST ( FDC.DriveSelSignal , FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack ,
				FDC.SideSignal , FDC_Buffer_Get_Size () );

		if ( Status & FDC_STR_BIT_LOST_DATA )			/* Error while writing */
			FDC_Update_STR ( 0 , FDC_STR_BIT_LOST_DATA );	/* Set LOST_DATA bit */

		FdcCycles = FDC_CmdCompleteCommon( true );
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Common to types I, II and III
 *
 * Start motor / spin up sequence if needed
 * Return true if spin up sequence is needed, else false
 */

static bool FDC_Set_MotorON ( uint8_t FDC_CR )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	bool	SpinUp;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	if ( ( ( FDC_CR & FDC_COMMAND_BIT_SPIN_UP ) == 0 )		/* Command wants motor's spin up */
	  && ( ( FDC.STR & FDC_STR_BIT_MOTOR_ON ) == 0 ) )		/* Motor on not enabled yet */
	{
		LOG_TRACE(TRACE_FDC, "fdc start motor with spinup VBL=%d video_cyc=%d %d@%d pc=%x\n",
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		FDC_Update_STR ( FDC_STR_BIT_SPIN_UP , 0 );		/* Unset spin up bit */
		FDC.IndexPulse_Counter = 0;				/* Reset counter to measure the spin up sequence */
		SpinUp = true;
	}
	else								/* No spin up : don't add delay to start the motor */
	{
		LOG_TRACE(TRACE_FDC, "fdc start motor without spinup VBL=%d video_cyc=%d %d@%d pc=%x\n",
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		SpinUp = false;
	}

	FDC_Update_STR ( 0 , FDC_STR_BIT_MOTOR_ON );			/* Start motor */

	if ( ( FDC.DriveSelSignal < 0 ) || ( !FDC_DRIVES[ FDC.DriveSelSignal ].Enabled )
		|| ( !FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted ) )
	{
		LOG_TRACE(TRACE_FDC, "fdc start motor : no disk/drive VBL=%d video_cyc=%d %d@%d pc=%x\n",
			nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
	}
	else if ( FDC_DRIVES[ FDC.DriveSelSignal ].IndexPulse_Time == 0 )
		FDC_IndexPulse_Init ( FDC.DriveSelSignal );		/* Index Pulse's position is random when motor starts */
	
	return SpinUp;
}


/*-----------------------------------------------------------------------*/
/**
 * Type I Commands
 *
 * Restore, Seek, Step, Step-In and Step-Out
 */


/*-----------------------------------------------------------------------*/
static int FDC_TypeI_Restore(void)
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type I restore spinup=%s verify=%s steprate_ms=%d drive=%d tr=0x%x head_track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_VERIFY ) ? "on" : "off" ,
		  FDC_StepRate_ms[ FDC_STEP_RATE ] ,
		  FDC.DriveSelSignal , FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal].HeadTrack : -1 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to seek to track zero */
	FDC.Command = FDCEMU_CMD_RESTORE;
	FDC.CommandState = FDCEMU_RUN_RESTORE_SEEKTOTRACKZERO;

	FDC_Update_STR ( FDC_STR_BIT_INDEX | FDC_STR_BIT_CRC_ERROR | FDC_STR_BIT_RNF , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_I_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeI_Seek ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type I seek dest_track=0x%x spinup=%s verify=%s steprate_ms=%d drive=%d tr=0x%x head_track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  FDC.DR,
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_VERIFY ) ? "on" : "off" ,
		  FDC_StepRate_ms[ FDC_STEP_RATE ] ,
		  FDC.DriveSelSignal , FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to seek to chosen track */
	FDC.Command = FDCEMU_CMD_SEEK;
	FDC.CommandState = FDCEMU_RUN_SEEK_TOTRACK;

	FDC_Update_STR ( FDC_STR_BIT_INDEX | FDC_STR_BIT_CRC_ERROR | FDC_STR_BIT_RNF , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_I_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeI_Step ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type I step %d spinup=%s verify=%s steprate_ms=%d drive=%d tr=0x%x head_track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  FDC.StepDirection,
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_VERIFY ) ? "on" : "off" ,
		  FDC_StepRate_ms[ FDC_STEP_RATE ] ,
		  FDC.DriveSelSignal , FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to step (using same direction as latest seek executed, ie 'FDC.StepDirection') */
	FDC.Command = FDCEMU_CMD_STEP;
	FDC.CommandState = FDCEMU_RUN_STEP_ONCE;

	FDC_Update_STR ( FDC_STR_BIT_INDEX | FDC_STR_BIT_CRC_ERROR | FDC_STR_BIT_RNF , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_I_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeI_StepIn(void)
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type I step in spinup=%s verify=%s steprate_ms=%d drive=%d tr=0x%x head_track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_VERIFY ) ? "on" : "off" ,
		  FDC_StepRate_ms[ FDC_STEP_RATE ] ,
		  FDC.DriveSelSignal , FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to step in (direction = +1) */
	FDC.Command = FDCEMU_CMD_STEP;
	FDC.CommandState = FDCEMU_RUN_STEP_ONCE;
	FDC.StepDirection = 1;						/* Increment track*/

	FDC_Update_STR ( FDC_STR_BIT_INDEX | FDC_STR_BIT_CRC_ERROR | FDC_STR_BIT_RNF , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_I_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeI_StepOut ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type I step out spinup=%s verify=%s steprate_ms=%d drive=%d tr=0x%x head_track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_VERIFY ) ? "on" : "off" ,
		  FDC_StepRate_ms[ FDC_STEP_RATE ] ,
		  FDC.DriveSelSignal , FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to step out (direction = -1) */
	FDC.Command = FDCEMU_CMD_STEP;
	FDC.CommandState = FDCEMU_RUN_STEP_ONCE;
	FDC.StepDirection = -1;						/* Decrement track */

	FDC_Update_STR ( FDC_STR_BIT_INDEX | FDC_STR_BIT_CRC_ERROR | FDC_STR_BIT_RNF , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_I_PREPARE;
}


/*-----------------------------------------------------------------------*/
/**
 * Type II Commands
 *
 * Read Sector, Write Sector
 */


/*-----------------------------------------------------------------------*/
static int FDC_TypeII_ReadSector ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type II read sector sector=0x%x multi=%s spinup=%s settle=%s tr=0x%x head_track=0x%x side=%d drive=%d dmasector=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  FDC.SR, ( FDC.CR & FDC_COMMAND_BIT_MULTIPLE_SECTOR ) ? "on" : "off" ,
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD ) ? "on" : "off" ,
		  FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  FDC.SideSignal , FDC.DriveSelSignal , FDC_DMA.SectorCount ,
		  FDC_GetDMAAddress(), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to read sector(s) */
	FDC.Command = FDCEMU_CMD_READSECTORS;
	FDC.CommandState = FDCEMU_RUN_READSECTORS_READDATA;

	FDC_Update_STR ( FDC_STR_BIT_DRQ | FDC_STR_BIT_LOST_DATA | FDC_STR_BIT_CRC_ERROR
		| FDC_STR_BIT_RNF | FDC_STR_BIT_RECORD_TYPE | FDC_STR_BIT_WPRT , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_II_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeII_WriteSector ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type II write sector sector=0x%x multi=%s spinup=%s settle=%s tr=0x%x head_track=0x%x side=%d drive=%d dmasector=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  FDC.SR, ( FDC.CR & FDC_COMMAND_BIT_MULTIPLE_SECTOR ) ? "on" : "off" ,
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD ) ? "on" : "off" ,
		  FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  FDC.SideSignal , FDC.DriveSelSignal , FDC_DMA.SectorCount,
		  FDC_GetDMAAddress(), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to write a sector(s) */
	FDC.Command = FDCEMU_CMD_WRITESECTORS;
	FDC.CommandState = FDCEMU_RUN_WRITESECTORS_WRITEDATA;

	FDC_Update_STR ( FDC_STR_BIT_DRQ | FDC_STR_BIT_LOST_DATA | FDC_STR_BIT_CRC_ERROR
		| FDC_STR_BIT_RNF | FDC_STR_BIT_RECORD_TYPE , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_II_PREPARE;
}


/*-----------------------------------------------------------------------*/
/**
 * Type III Commands
 *
 * Read Address, Read Track, Write Track
 */


/*-----------------------------------------------------------------------*/
static int FDC_TypeIII_ReadAddress ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type III read address spinup=%s settle=%s tr=0x%x head_track=0x%x side=%d drive=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD ) ? "on" : "off" ,
		  FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  FDC.SideSignal , FDC.DriveSelSignal , FDC_GetDMAAddress(),
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to seek to track zero */
	FDC.Command = FDCEMU_CMD_READADDRESS;
	FDC.CommandState = FDCEMU_RUN_READADDRESS;

	FDC_Update_STR ( FDC_STR_BIT_DRQ | FDC_STR_BIT_LOST_DATA | FDC_STR_BIT_CRC_ERROR
		| FDC_STR_BIT_RNF | FDC_STR_BIT_RECORD_TYPE | FDC_STR_BIT_WPRT , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_III_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeIII_ReadTrack ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type III read track spinup=%s settle=%s tr=0x%x head_track=0x%x side=%d drive=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD ) ? "on" : "off" ,
		  FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  FDC.SideSignal , FDC.DriveSelSignal , FDC_GetDMAAddress(),
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to read a single track */
	FDC.Command = FDCEMU_CMD_READTRACK;
	FDC.CommandState = FDCEMU_RUN_READTRACK;

	FDC_Update_STR ( FDC_STR_BIT_DRQ | FDC_STR_BIT_LOST_DATA | FDC_STR_BIT_CRC_ERROR
		| FDC_STR_BIT_RNF | FDC_STR_BIT_RECORD_TYPE | FDC_STR_BIT_WPRT , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_III_PREPARE;
}


/*-----------------------------------------------------------------------*/
static int FDC_TypeIII_WriteTrack ( void )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type III write track spinup=%s settle=%s tr=0x%x head_track=0x%x side=%d drive=%d addr=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  ( FDC.CR & FDC_COMMAND_BIT_SPIN_UP ) ? "off" : "on" ,
		  ( FDC.CR & FDC_COMMAND_BIT_HEAD_LOAD ) ? "on" : "off" ,
		  FDC.TR , FDC.DriveSelSignal >= 0 ? FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack : -1 ,
		  FDC.SideSignal , FDC.DriveSelSignal , FDC_GetDMAAddress(),
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* Set emulation to write a single track */
	FDC.Command = FDCEMU_CMD_WRITETRACK;
	FDC.CommandState = FDCEMU_RUN_WRITETRACK;

	FDC_Update_STR ( FDC_STR_BIT_DRQ | FDC_STR_BIT_LOST_DATA | FDC_STR_BIT_CRC_ERROR
		| FDC_STR_BIT_RNF | FDC_STR_BIT_RECORD_TYPE | FDC_STR_BIT_WPRT , FDC_STR_BIT_BUSY );

	return FDC_DELAY_CYCLE_TYPE_III_PREPARE;
}


/*-----------------------------------------------------------------------*/
/**
 * Type IV Commands
 *
 * Force Interrupt
 */


/*-----------------------------------------------------------------------*/
static int FDC_TypeIV_ForceInterrupt ( void )
{
	int	FdcCycles;
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type IV force int 0x%x irq=%d index=%d VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  FDC.CR , ( FDC.CR & 0x8 ) >> 3 , ( FDC.CR & 0x4 ) >> 2 ,
		  nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* If a command was running, just remove busy bit and keep the current content of Status reg */
	/* If FDC was idle, the content of Status reg is forced to type I */
	if ( ( FDC.STR & FDC_STR_BIT_BUSY ) == 0 )			
	{
		FDC.StatusTypeI = true;

		/* Starting a Force Int command when idle should set the motor bit and clear the spinup bit (verified on STF) */
		FDC_Update_STR ( FDC_STR_BIT_SPIN_UP , FDC_STR_BIT_MOTOR_ON );	/* Clear spinup bit and set motor bit */
	}

	/* Get the interrupt's condition and set IRQ accordingly */
	/* Most of the time a 0xD8 command is followed by a 0xD0 command and a read STR to clear the IRQ signal */
	FDC.InterruptCond = FDC.CR & 0x0f;				/* Keep the 4 lowest bits */

	if ( FDC.InterruptCond & FDC_INTERRUPT_COND_IMMEDIATE )
		FDC_SetIRQ ( FDC_IRQ_SOURCE_FORCED );
	else
		FDC_ClearIRQ ();

	/* Remove busy bit, don't change IRQ's state and stop the motor */
	FdcCycles = FDC_CmdCompleteCommon( false );

	return FDC_DELAY_CYCLE_TYPE_IV_PREPARE + FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Execute Type I commands
 */
static int FDC_ExecuteTypeICommands ( void )
{
	int	FdcCycles = 0;

	FDC.CommandType = 1;
	FDC.StatusTypeI = true;

	/* Check Type I Command */
	switch ( FDC.CR & 0xf0 )
	{
	 case 0x00:             /* Restore */
		FdcCycles = FDC_TypeI_Restore();
		break;
	 case 0x10:             /* Seek */
		FdcCycles = FDC_TypeI_Seek();
		break;
	 case 0x20:             /* Step */
	 case 0x30:
		FdcCycles = FDC_TypeI_Step();
		break;
	 case 0x40:             /* Step-In */
	 case 0x50:
		FdcCycles = FDC_TypeI_StepIn();
		break;
	 case 0x60:             /* Step-Out */
	 case 0x70:
		FdcCycles = FDC_TypeI_StepOut();
		break;
	}


	/* After a "STEP" command we set the Disk Change signal to "inserted" */
	/* if the drive is selected and a floppy is inserted */
	if ( ( FDC.DriveSelSignal >= 0 ) && ( FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted == true ) )
		FDC_Drive_Set_DC_signal ( FDC.DriveSelSignal , FDC_DC_SIGNAL_INSERTED );


	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Execute Type II commands
 */
static int FDC_ExecuteTypeIICommands ( void )
{
	int	FdcCycles = 0;

	FDC.CommandType = 2;
	FDC.StatusTypeI = false;

	/* Check Type II Command */
	switch ( FDC.CR & 0xf0 )
	{
	 case 0x80:             /* Read Sector multi=0*/
	 case 0x90:             /* Read Sectors multi=1 */
		FdcCycles = FDC_TypeII_ReadSector();
		break;
	 case 0xa0:             /* Write Sector multi=0 */
	 case 0xb0:             /* Write Sectors multi=1 */
		FdcCycles = FDC_TypeII_WriteSector();
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Execute Type III commands
 */
static int FDC_ExecuteTypeIIICommands ( void )
{
	int	FdcCycles = 0;

	FDC.CommandType = 3;
	FDC.StatusTypeI = false;

	/* Check Type III Command */
	switch ( FDC.CR & 0xf0 )
	{
	 case 0xc0:             /* Read Address */
		FdcCycles = FDC_TypeIII_ReadAddress();
		break;
	 case 0xe0:             /* Read Track */
		FdcCycles = FDC_TypeIII_ReadTrack();
		break;
	 case 0xf0:             /* Write Track */
		FdcCycles = FDC_TypeIII_WriteTrack();
		break;
	}

	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Execute Type IV commands
 */
static int FDC_ExecuteTypeIVCommands ( void )
{
	int	FdcCycles;

	FDC.CommandType = 4;

	FdcCycles = FDC_TypeIV_ForceInterrupt();
	
	return FdcCycles;
}


/*-----------------------------------------------------------------------*/
/**
 * Find FDC command type and execute
 *
 * NOTE [NP] : as verified on a real STF and contrary to what is written
 * in the WD1772 doc, any new command will reset the InterruptCond set by
 * a previous Dx command, not just a D0.
 * This means that a D8 command (force int) can be cancelled by a D0 command
 * or by any other command ; but in any case, IRQ will remain set until
 * status register is read or another new command is started.
 * -> 1st command clears force IRQ condition, 2nd command clears IRQ
 */
static void FDC_ExecuteCommand ( void )
{
	int	FdcCycles;
	uint8_t	Type;

	Type = FDC_GetCmdType ( FDC.CR );

	/* When a new command is started, FDC's IRQ is reset (except if "force interrupt immediate" is set) */

	/* If IRQ is forced but FDC_INTERRUPT_COND_IMMEDIATE is not set anymore, this means */
	/* the D8 command was stopped and we can clear the forced IRQ when starting a new command */
	if ( ( FDC.IRQ_Signal & FDC_IRQ_SOURCE_FORCED )
	  && ( ( FDC.InterruptCond & FDC_INTERRUPT_COND_IMMEDIATE ) == 0 ) )
		FDC.IRQ_Signal &= ~FDC_IRQ_SOURCE_FORCED;	/* Really stop the forced IRQ */

	/* Starting a new type I/II/III should clear the IRQ (except when IRQ is forced) */
	/* For type IV, this is handled in FDC_TypeIV_ForceInterrupt() */
	if ( Type != 4 )
		FDC_ClearIRQ ();
	
	/* When a new command is executed, we clear InterruptCond */
	/* (not just when the new command is D0) */
	/* InterruptCond is cleared here, but it might be set again just after */
	/* when we call FDC_ExecuteTypeIVCommands() */
	FDC.InterruptCond = 0;

	/* Check type of command and execute */
	if ( Type == 1 )						/* Type I - Restore, Seek, Step, Step-In, Step-Out */
		FdcCycles = FDC_ExecuteTypeICommands();
	else if ( Type == 2 )						/* Type II - Read Sector, Write Sector */
		FdcCycles = FDC_ExecuteTypeIICommands();
	else if ( Type == 3 )						/* Type III - Read Address, Read Track, Write Track */
		FdcCycles = FDC_ExecuteTypeIIICommands();
	else								/* Type IV - Force Interrupt */
		FdcCycles = FDC_ExecuteTypeIVCommands();

	FDC.ReplaceCommandPossible = true;				/* This new command can be replaced during the prepare+spinup phase */
	FDC_StartTimer_FdcCycles ( FdcCycles , 0 );
}


/*-----------------------------------------------------------------------*/
/**
 * Write to SectorCount register $ff8604
 */
static void FDC_WriteSectorCountRegister ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	FDC_DMA.SectorCount = IoMem_ReadWord(0xff8604);
	if (!Config_IsMachineFalcon())
		FDC_DMA.SectorCount &= 0xff;

	LOG_TRACE(TRACE_FDC, "fdc write 8604 dma sector count=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
	          FDC_DMA.SectorCount, nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

}


/*-----------------------------------------------------------------------*/
/**
 * Write to Command register $ff8604
 */
static void FDC_WriteCommandRegister ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;
	uint8_t Type_new;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8604 command=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		  IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	/* If fdc is busy, only 'Force Interrupt' is possible */
	/* [NP] : it's also possible to start a new command just after another command */
	/* was started and spinup phase was not completed yet (eg Overdrive Demos by Phalanx) (see notes at the top of the file)*/
	if ( FDC.STR & FDC_STR_BIT_BUSY )
	{
		Type_new = FDC_GetCmdType ( IoMem_ReadByte(0xff8605) );
		if ( Type_new == 4 )					/* 'Force Interrupt' command */
		{
			LOG_TRACE(TRACE_FDC, "fdc write 8604 while fdc busy, current command=0x%x interrupted by command=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC.CR , IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
		}

		else if ( FDC.ReplaceCommandPossible
			&& ( ( ( Type_new == 1 ) && ( FDC.CommandType == Type_new ) )		/* Replace a type I command with a type I */
			  || ( ( Type_new == 2 ) && ( FDC.CommandType == Type_new ) ) ) )	/* Replace a type II command with a type II */
		{
			LOG_TRACE(TRACE_FDC, "fdc write 8604 while fdc busy in prepare+spinup, current command=0x%x replaced by command=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
				FDC.CR , IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
		}

		else								/* Other cases : new command is ignored */
		{
			LOG_TRACE(TRACE_FDC, "fdc write 8604 fdc busy, command=0x%x ignored VBL=%d video_cyc=%d %d@%d pc=%x\n",
				IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
			return;
		}
	}

	FDC.CR = IoMem_ReadByte(0xff8605);
	FDC_ExecuteCommand();
}


/*-----------------------------------------------------------------------*/
/**
 * Write to Track register $ff8604
 */
static void FDC_WriteTrackRegister ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8604 track=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoMem_ReadByte(0xff8605) , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* [NP] Contrary to what is written in the WD1772 doc, Track Register can be changed */
	/* while the fdc is busy (the change will be ignored or not, depending on the current sub-state */
	/* in the state machine) */
	if ( FDC.STR & FDC_STR_BIT_BUSY )
	{
		LOG_TRACE(TRACE_FDC, "fdc write 8604 fdc busy, track=0x%x may be ignored VBL=%d video_cyc=%d %d@%d pc=%x\n",
			IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
//return;
	}

	FDC.TR = IoMem_ReadByte(0xff8605);
}


/*-----------------------------------------------------------------------*/
/**
 * Write to Sector register $ff8604
 */
static void FDC_WriteSectorRegister ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8604 sector=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoMem_ReadByte(0xff8605) , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* [NP] Contrary to what is written in the WD1772 doc, Sector Register can be changed */
	/* while the fdc is busy (but it will have no effect once the sector's header is found) */
	/* (fix Delirious Demo IV's loader, which is bugged and set SR after starting the Read Sector command) */
	if ( FDC.STR & FDC_STR_BIT_BUSY )
	{
		LOG_TRACE(TRACE_FDC, "fdc write 8604 fdc busy, sector=0x%x may be ignored VBL=%d video_cyc=%d %d@%d pc=%x\n",
			IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());
	}

	FDC.SR = IoMem_ReadByte(0xff8605);
}


/*-----------------------------------------------------------------------*/
/**
 * Write to Data register $ff8604
 */
static void FDC_WriteDataRegister ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8604 data=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoMem_ReadByte(0xff8605), nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	FDC.DR = IoMem_ReadByte(0xff8605);
}


/*-----------------------------------------------------------------------*/
/**
 * Store byte in FDC/HDC registers or DMA sector count, when writing to $ff8604
 * When accessing FDC/HDC registers, a copy of $ff8604 should be kept in ff8604_recent_val
 * to be used later when reading unused bits at $ff8604/$ff8606
 *
 * NOTE [NP] : add 4 cycles wait state in all cases (sector count / FDC / HDC)
 */
void FDC_DiskController_WriteWord ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;
	int EmulationMode;
	int FDC_reg;

	if ( nIoMemAccessSize == SIZE_BYTE )
	{
		/* This register does not like to be accessed in byte mode on a normal ST */
		M68000_BusError(IoAccessFullAddress, BUS_ERROR_WRITE, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
		return;
	}

	M68000_WaitState(4);

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8604 data=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoMem_ReadWord(0xff8604), nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );


	/* Are we trying to set the DMA SectorCount ? */
	if ( FDC_DMA.Mode & 0x10 )					/* Bit 4 */
	{
		FDC_WriteSectorCountRegister();
		return;
	}

	/* Store the byte that was just accessed by this write */
	FDC_DMA.ff8604_recent_val = ( FDC_DMA.ff8604_recent_val & 0xff00 ) | IoMem_ReadByte(0xff8605);
	
	if ( ( FDC_DMA.Mode & 0x0008 ) == 0x0008 )			/* Is it an ACSI (or Falcon SCSI) HDC command access ? */
	{
		/*  Handle HDC access */
		LOG_TRACE(TRACE_FDC, "fdc write 8604 hdc command addr=%x command=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n",
			  FDC_DMA.Mode & 0x7 , IoMem_ReadByte(0xff8605), nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

		HDC_WriteCommandByte(FDC_DMA.Mode & 0x7, IoMem_ReadByte(0xff8605));
		return;
	}

	else								/* It's a FDC register access */
	{
		FDC_reg = ( FDC_DMA.Mode & 0x6 ) >> 1;			/* Bits 1,2 (A0,A1) */

		EmulationMode = FDC_GetEmulationMode();
		if ( EmulationMode == FDC_EMULATION_MODE_INTERNAL )
		{
			/* Update FDC's internal variables */
			FDC_UpdateAll ();

			/* Write to FDC registers */
			switch ( FDC_reg )
			{
			 case 0x0:					/* 0 0 - Command register */
				FDC_WriteCommandRegister();
				break;
			 case 0x1:					/* 0 1 - Track register */
				FDC_WriteTrackRegister();
				break;
			 case 0x2:					/* 1 0 - Sector register */
				FDC_WriteSectorRegister();
				break;
			 case 0x3:					/* 1 1 - Data register */
				FDC_WriteDataRegister();
				break;
			}
		}
		else if ( EmulationMode == FDC_EMULATION_MODE_IPF )
		{
			IPF_FDC_WriteReg ( FDC_reg , IoMem_ReadByte(0xff8605) );
		}
	}
}


/*-----------------------------------------------------------------------*/
/**
 * Return FDC/HDC registers or DMA sector count when reading from $ff8604
 * - When accessing FDC/HDC registers, a copy of $ff8604 should be kept in ff8604_recent_val
 *   to be used later when reading unused bits at $ff8604/$ff8606
 * - DMA sector count can't be read, this will return ff8604_recent_val (verified on a real STF)
 *
 * NOTE [NP] : add 4 cycles wait state in that case, except when reading DMA sector count
 */
void FDC_DiskControllerStatus_ReadWord ( void )
{
	uint16_t DiskControllerByte = 0;					/* Used to pass back the parameter */
	int FrameCycles, HblCounterVideo, LineCycles;
	int ForceWPRT;
	int EmulationMode;
	int FDC_reg;

	if (nIoMemAccessSize == SIZE_BYTE && !Config_IsMachineFalcon())
	{
		/* This register does not like to be accessed in byte mode on a normal ST */
		M68000_BusError(IoAccessFullAddress, BUS_ERROR_READ, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
		return;
	}

	/* Are we trying to read the DMA SectorCount ? */
	if ( FDC_DMA.Mode & 0x10 )					/* Bit 4 */
	{
		DiskControllerByte = FDC_DMA.ff8604_recent_val;		/* As verified on real STF, DMA sector count can't be read back */
	}

	else if ( ( FDC_DMA.Mode & 0x0008) == 0x0008)			/* HDC status reg selected ? */
	{
		M68000_WaitState(4);					/* [NP] : possible, but not tested on real HW */

		/* Return the HDC status byte */
		DiskControllerByte = HDC_ReadCommandByte(FDC_DMA.Mode & 0x7);
	}

	else								/* It's a FDC register access */
	{
		M68000_WaitState(4);

		FDC_reg = ( FDC_DMA.Mode & 0x6 ) >> 1;			/* Bits 1,2 (A0,A1) */

		EmulationMode = FDC_GetEmulationMode();
		if ( EmulationMode == FDC_EMULATION_MODE_INTERNAL )
		{
			/* Update FDC's internal variables */
			FDC_UpdateAll ();

			/* Read FDC registers */
			switch ( FDC_reg )
			{
			 case 0x0:						/* 0 0 - Status register */
				/* If we report a type I status, some bits are updated in real time */
				/* depending on the corresponding signals. If this is not a type I, we return STR unmodified */
				/* [NP] Contrary to what is written in the WD1772 doc, the WPRT bit */
				/* is updated after a Type I command */
				/* (eg : Procopy or Terminators Copy 1.68 do a Restore/Seek to test WPRT) */
				if ( FDC.StatusTypeI )
				{
					/* If no drive available, FDC's input signals TR00, INDEX and WPRT are off */
					if ( ( FDC.DriveSelSignal < 0 ) || ( !FDC_DRIVES[ FDC.DriveSelSignal ].Enabled ) )
						FDC_Update_STR ( FDC_STR_BIT_TR00 | FDC_STR_BIT_INDEX | FDC_STR_BIT_WPRT , 0 );

					else
					{
						if ( FDC_DRIVES[ FDC.DriveSelSignal ].HeadTrack == 0 )
							FDC_Update_STR ( 0 , FDC_STR_BIT_TR00 );	/* Set TR00 bit2 */
						else
							FDC_Update_STR ( FDC_STR_BIT_TR00 , 0 );	/* Unset TR00 bit2 */

						if ( FDC_IndexPulse_GetState () )
							FDC_Update_STR ( 0 , FDC_STR_BIT_INDEX );	/* Set INDEX bit1 */
						else
							FDC_Update_STR ( FDC_STR_BIT_INDEX , 0 );	/* Unset INDEX bit1 */

						/* For Type I, always unset CRC ERROR  bit3 */
						FDC_Update_STR ( FDC_STR_BIT_CRC_ERROR , 0 );

						/* When there's no disk in drive, the floppy drive hardware can't see */
						/* the difference with an inserted disk that would be write protected */
						if ( ! FDC_DRIVES[ FDC.DriveSelSignal ].DiskInserted )
							FDC_Update_STR ( 0 , FDC_STR_BIT_WPRT );	/* Set WPRT bit6 */
						else if ( Floppy_IsWriteProtected ( FDC.DriveSelSignal ) )
							FDC_Update_STR ( 0 , FDC_STR_BIT_WPRT );	/* Set WPRT bit6 */
						else
							FDC_Update_STR ( FDC_STR_BIT_WPRT , 0 );	/* Unset WPRT bit6 */

						/* Temporarily change the WPRT bit if we're in a transition phase */
						/* regarding the disk in the drive (inserting or ejecting) */
						ForceWPRT = Floppy_DriveTransitionUpdateState ( FDC.DriveSelSignal );
						if ( ForceWPRT == 1 )
							FDC_Update_STR ( 0 , FDC_STR_BIT_WPRT );	/* Force setting WPRT */
						else if ( ForceWPRT == -1 )
							FDC_Update_STR ( FDC_STR_BIT_WPRT , 0 );	/* Force clearing WPRT */

						if ( ForceWPRT != 0 )
							LOG_TRACE(TRACE_FDC, "force wprt=%d VBL=%d drive=%d str=%x\n",
							  ForceWPRT==1?1:0, nVBLs, FDC.DriveSelSignal, FDC.STR );
					}
				}

				DiskControllerByte = FDC.STR;

				/* When Status Register is read, FDC's IRQ is reset (except if "force interrupt immediate" is set) */

				/* If IRQ is forced but FDC_INTERRUPT_COND_IMMEDIATE is not set anymore, this means */
				/* the D8 command was stopped and we can clear the forced IRQ while reading status register */
				if ( ( FDC.IRQ_Signal & FDC_IRQ_SOURCE_FORCED )
				  && ( ( FDC.InterruptCond & FDC_INTERRUPT_COND_IMMEDIATE ) == 0 ) )
					FDC.IRQ_Signal &= ~FDC_IRQ_SOURCE_FORCED;	/* Really stop the forced IRQ */

				FDC_ClearIRQ ();
				break;
			 case 0x1:						/* 0 1 - Track register */
				DiskControllerByte = FDC.TR;
				break;
			 case 0x2:						/* 1 0 - Sector register */
				DiskControllerByte = FDC.SR;
				break;
			 case 0x3:						/* 1 1 - Data register */
				DiskControllerByte = FDC.DR;
				break;
			}
		}
		else if ( EmulationMode == FDC_EMULATION_MODE_IPF )
		{
			DiskControllerByte = IPF_FDC_ReadReg ( FDC_reg );
			if ( ( FDC_reg == 0x0 ) && ( FDC.DriveSelSignal >= 0 ) )	/* 0 0 - Status register */
			{
				/* Temporarily change the WPRT bit if we're in a transition phase */
				/* regarding the disk in the drive (inserting or ejecting) */
				ForceWPRT = Floppy_DriveTransitionUpdateState ( FDC.DriveSelSignal );
				if ( ForceWPRT == 1 )
					DiskControllerByte |= FDC_STR_BIT_WPRT;		/* Force setting WPRT */
				if ( ForceWPRT == -1 )
					DiskControllerByte &= ~FDC_STR_BIT_WPRT;	/* Force clearing WPRT */

				if ( ForceWPRT != 0 )
					LOG_TRACE(TRACE_FDC, "force wprt=%d VBL=%d drive=%d str=%x\n", ForceWPRT==1?1:0, nVBLs, FDC.DriveSelSignal, DiskControllerByte );
			}
		}
	}


	/* Store the byte that was just returned by this read if we accessed fdc/hdc regs */
	if ( ( FDC_DMA.Mode & 0x10 ) == 0 )				/* Bit 4 */
		FDC_DMA.ff8604_recent_val = ( FDC_DMA.ff8604_recent_val & 0xff00 ) | ( DiskControllerByte & 0xff );

	IoMem_WriteWord(0xff8604, DiskControllerByte);

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc read 8604 ctrl status=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		DiskControllerByte , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );
}


/*-----------------------------------------------------------------------*/
/**
 * Write word to $ff8606 (DMA Mode Control)
 *
 * Eg.
 * $80 - Selects command/status register
 * $82 - Selects track register
 * $84 - Selects sector register
 * $86 - Selects data register
 * NOTE - OR above values with $100 is transfer from memory to floppy
 * Also if bit 4 is set, write to DMA sector count register
 *
 * NOTE [NP] : add 4 cycles wait state in that case
 */
void FDC_DmaModeControl_WriteWord ( void )
{
	uint16_t Mode_prev;						/* Store previous write to 0xff8606 for 'toggle' checks */
	int FrameCycles, HblCounterVideo, LineCycles;


	if (nIoMemAccessSize == SIZE_BYTE)
	{
		/* This register does not like to be accessed in byte mode on a normal ST */
		M68000_BusError(IoAccessFullAddress, BUS_ERROR_WRITE, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
		return;
	}

	M68000_WaitState(4);

	Mode_prev = FDC_DMA.Mode;					/* Store previous to check for _read/_write toggle (DMA reset) */
	FDC_DMA.Mode = IoMem_ReadWord(0xff8606);			/* Store to DMA Mode control */

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 8606 ctrl=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC_DMA.Mode , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* When write to 0xff8606, check bit '8' toggle. This causes DMA status reset */
	if ((Mode_prev ^ FDC_DMA.Mode) & 0x0100)
		FDC_ResetDMA();

	if ((Mode_prev & 0xc0) != 0 && (FDC_DMA.Mode & 0xc0) == 0)
		HDC_DmaTransfer();
}


/*-----------------------------------------------------------------------*/
/**
 * Read DMA Status at $ff8606
 *
 * Only bits 0-2 are used :
 *   Bit 0 - Error Status (0=Error)
 *   Bit 1 - Sector Count Zero Status (0=Sector Count Zero)
 *   Bit 2 - Data Request signal from the FDC
 *
 * NOTE [NP] : as verified on STF, bit 0 will be cleared (=error) if DMA sector count is 0
 * when we get some DRQ to process.
 *
 * NOTE [NP] : on the ST, the Data Register will always be read by the DMA when the FDC's DRQ
 * signal is set. This means bit 2 of DMA status will be '0' nearly all the time
 * (as verified on STF by constantly reading DMA Status, bit 2 can be '1' during
 * a few cycles, before the DMA read the Data Register, but for the emulation we
 * consider it's always '0')
 *
 * NOTE [NP] : unused bits 3-15 are the ones from the latest $ff8604 access (verified on real STF)
 *
 * NOTE [NP] : no 4 cycles wait state in that case
 */
void FDC_DmaStatus_ReadWord ( void )
{
	if (nIoMemAccessSize == SIZE_BYTE && !Config_IsMachineFalcon())
	{
		/* This register does not like to be accessed in byte mode on a normal ST */
		M68000_BusError(IoAccessFullAddress, BUS_ERROR_READ, BUS_ERROR_SIZE_BYTE, BUS_ERROR_ACCESS_DATA, 0);
		return;
	}

	/* Update Bit1 for zero sector count */
	if ( FDC_DMA.SectorCount != 0 )
		FDC_DMA.Status |= 0x02;
	else
		FDC_DMA.Status &= ~0x02;

	/* In the case of the ST, Bit2 / DRQ is always 0 because it's handled by the DMA and its 16 bytes buffer */

	/* Return Status with unused bits replaced by latest bits from $ff8604 */
	IoMem_WriteWord( 0xff8606 , FDC_DMA.Status | ( FDC_DMA.ff8604_recent_val & 0xfff8 ) );
}


/*-----------------------------------------------------------------------*/
/**
 * Read hi/med/low DMA address byte at $ff8609/0b/0d
 */
void	FDC_DmaAddress_ReadByte ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc read dma address %x val=0x%02x address=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoAccessCurrentAddress , IoMem[ IoAccessCurrentAddress ] , FDC_GetDMAAddress() ,
		nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );
}


/*-----------------------------------------------------------------------*/
/**
 * Write hi/med/low DMA address byte at $ff8609/0b/0d
 *
 * NOTE [NP] : as described by Ijor in http://atari-forum.com/viewtopic.php?f=16&t=30289
 * the STF DMA address counter uses a ripple carry adder that will increment middle byte
 * when bit 7 of lower byte goes from 1 to 0 (same for middle/high bytes)
 * To avoid possible error with this carry, DMA address bytes should be written in the order
 * low, middle, high (as specified by Atari documentations) and not high/middle/low
 */
void	FDC_DmaAddress_WriteByte ( void )
{
	uint32_t Address;
	uint32_t Address_old;
	int FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write dma address %x val=0x%02x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		IoAccessCurrentAddress , IoMem[ IoAccessCurrentAddress ] ,
		nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* Build up 24-bit address from hardware registers */
	Address = ((uint32_t)STMemory_ReadByte(0xff8609)<<16) | ((uint32_t)STMemory_ReadByte(0xff860b)<<8) | (uint32_t)STMemory_ReadByte(0xff860d);

	/* On STF, DMA address uses a "ripple carry adder" which can trigger when writing to $ff860b/0d */
	if ( Config_IsMachineST() )
	{
		Address_old = FDC_GetDMAAddress();

		if ( ( Address_old & 0x80 ) && !( Address & 0x80 ) )		/* Bit 7 goes from 1 to 0 */
		{
			Address += 0x100;					/* Increase middle byte (and high byte if needed) */
//fprintf ( stderr , "fdc write dma address detect ripple carry at $ff860d old=0x%x new=0x%x\n" , Address_old , Address );
			LOG_TRACE(TRACE_FDC, "fdc write dma address detect ripple carry at $ff860d old=0x%x new=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
				Address_old , Address ,
				nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );
		}
		else if ( ( Address_old & 0x8000 ) && !( Address & 0x8000 ) )	/* Bit 15 goes from 1 to 0 */
		{
			Address += 0x10000;					/* Increase high byte */
//fprintf ( stderr , "fdc write dma address detect ripple carry at $ff860b old=0x%x new=0x%x\n" , Address_old , Address );
			LOG_TRACE(TRACE_FDC, "fdc write dma address detect ripple carry at $ff860b old=0x%x new=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
				Address_old , Address ,
				nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );
		}
	}

	/* Store new address as DMA address and update $ff8609/0b/0d */
	FDC_WriteDMAAddress ( Address );
}


/*-----------------------------------------------------------------------*/
/**
 * Get DMA address used to transfer data between FDC/HDC and RAM
 */
uint32_t FDC_GetDMAAddress(void)
{
	return FDC_DMA.Address;
}


/*-----------------------------------------------------------------------*/
/**
 * Write a new address to the FDC/HDC DMA address registers at $ff8909/0b/0d
 * As verified on real STF, DMA address high byte written at $ff8609 is masked
 * with 0x3f :
 *	move.b #$ff,$ff8609
 *	move.b $ff8609,d0  -> d0=$3f
 * DMA address must also be word-aligned, low byte at $ff860d is masked with 0xfe
 *	move.b #$ff,$ff860d
 *	move.b $ff860d,d0  -> d0=$fe
 */
void FDC_WriteDMAAddress ( uint32_t Address )
{
	int FrameCycles, HblCounterVideo, LineCycles;
	uint32_t dma_mask;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write dma address new=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		Address , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* Mask DMA address : */
	/*  - DMA address must be word-aligned, bit 0 at $ff860d is always 0 */
	/*  - On STF/STE machines limited to 4MB of RAM, DMA address is also limited to $3fffff */
	dma_mask = 0xff00fffe | ( DMA_MaskAddressHigh() << 16 );		/* Force bit 0 to 0 */
	FDC_DMA.Address = Address & dma_mask;

	/* Store as 24-bit address */
	STMemory_WriteByte(0xff8609, FDC_DMA.Address>>16);
	STMemory_WriteByte(0xff860b, FDC_DMA.Address>>8);
	STMemory_WriteByte(0xff860d, FDC_DMA.Address);
}


/*-----------------------------------------------------------------------*/
/**
 * Return the number of FDC cycles to wait before reaching the next
 * sector's ID Field in the track ($A1 $A1 $A1 $FE TR SIDE SR LEN CRC1 CRC2)
 * If no ID Field is found before the end of the track, we use the 1st
 * ID Field of the track (which simulates a full spin of the floppy).
 * We also store the next sector's number into NextSector_ID_Field_SR,
 * the next track's number into NextSector_ID_Field_TR, the next sector's length
 * into NextSector_ID_Field_LEN and if the CRC is correct or not into
 * NextSector_ID_Field_CRC_OK.
 * This function assumes some 512 byte sectors stored in ascending
 * order (for ST/MSA)
 * If there's no available drive/floppy, we return -1
 */
static int	FDC_NextSectorID_FdcCycles_ST ( uint8_t Drive , uint8_t NumberOfHeads , uint8_t Track , uint8_t Side )
{
	int	CurrentPos;
	int	MaxSector;
	int	TrackPos;
	int	i;
	int	NextSector;
	int	NbBytes;

	CurrentPos = FDC_IndexPulse_GetCurrentPos_NbBytes ();
	if ( CurrentPos < 0 )						/* No drive/floppy available at the moment */
		return -1;

	if ( ( Side == 1 ) && ( NumberOfHeads == 1 ) )			/* Can't read side 1 on a single sided drive */
		return -1;

	if ( Track >= FDC_GetTracksPerDisk ( Drive ) )			/* Try to access a non existing track */
		return -1;

	if ( FDC_MachineHandleDensity ( Drive ) == false )		/* Can't handle the floppy's density */
		return -1;

	MaxSector = FDC_GetSectorsPerTrack ( Drive , Track , Side );
	TrackPos = FDC_TRACK_LAYOUT_STANDARD_GAP1;			/* Position of 1st raw sector */
	TrackPos += FDC_TRACK_LAYOUT_STANDARD_GAP2;			/* Position of ID Field in 1st raw sector */

	/* Compare CurrentPos with each sector's position in ascending order */
	for ( i=0 ; i<MaxSector ; i++ )
	{
		if ( CurrentPos < TrackPos )
			break;						/* We found the next sector */
		else
			TrackPos += FDC_TRACK_LAYOUT_STANDARD_RAW_SECTOR_512;
	}

	if ( i == MaxSector )						/* CurrentPos is after the last ID Field of this track */
	{
		/* Reach end of track (new index pulse), then go to sector 1 */
		NbBytes = FDC_GetBytesPerTrack ( Drive , Track , Side ) - CurrentPos + FDC_TRACK_LAYOUT_STANDARD_GAP1 + FDC_TRACK_LAYOUT_STANDARD_GAP2;
		NextSector = 1;
	}
	else								/* There's an ID Field before end of track */
	{
		NbBytes = TrackPos - CurrentPos;
		NextSector = i+1;
	}

//fprintf ( stderr , "fdc bytes next sector pos=%d trpos=%d nbbytes=%d maxsr=%d nextsr=%d\n" , CurrentPos, TrackPos, NbBytes, MaxSector, NextSector );
	FDC.NextSector_ID_Field_TR = Track;
	FDC.NextSector_ID_Field_SR = NextSector;
	FDC.NextSector_ID_Field_LEN = FDC_SECTOR_SIZE_512;		/* ST/MSA have 512 bytes per sector */
	FDC.NextSector_ID_Field_CRC_OK = 1;				/* CRC is always correct for ST/MSA */

	return FDC_TransferByte_FdcCycles ( NbBytes );
}


/*-----------------------------------------------------------------------*/
/**
 * Return the value of the track number in the next ID field set by
 * FDC_NextSectorID_FdcCycles_ST (for ST/MSA, it's always HeadTrack value)
 */
static uint8_t	FDC_NextSectorID_TR_ST ( void )
{
	return FDC.NextSector_ID_Field_TR;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the value of the sector number in the next ID field set by
 * FDC_NextSectorID_FdcCycles_ST.
 */
static uint8_t	FDC_NextSectorID_SR_ST ( void )
{
	return FDC.NextSector_ID_Field_SR;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the value of the sector's length in the next ID field set by
 * FDC_NextSectorID_FdcCycles_ST.
 * For ST/MSA, it's always 2 (512 bytes per sector)
 */
static uint8_t	FDC_NextSectorID_LEN_ST ( void )
{
	return FDC.NextSector_ID_Field_LEN;
}


/*-----------------------------------------------------------------------*/
/**
 * Return the status of the CRC in the next ID field set by
 * FDC_NextSectorID_FdcCycles_ST.
 * If '0', CRC is bad, else CRC is OK
 * For ST/MSA, CRC is always OK
 */
static uint8_t	FDC_NextSectorID_CRC_OK_ST ( void )
{
	return FDC.NextSector_ID_Field_CRC_OK;
}


/*-----------------------------------------------------------------------*/
/**
 * Read a sector from a floppy image in ST format (used in type II command)
 * Each byte of the sector is added to the FDC buffer with a default timing
 * (32 microsec)
 * Return 0 if sector was read without error, or FDC_STR_BIT_RNF if an error occurred
 * (FDC_STR_BIT_CRC_ERROR and FDC_STR_BIT_RECORD_TYPE are always set 0 for ST/MSA)
 */
static uint8_t FDC_ReadSector_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side , int *pSectorSize )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	int	i;
	uint8_t	*pSectorData;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc read sector addr=0x%x drive=%d track=%d sect=%d side=%d VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC_GetDMAAddress(), Drive, Track, Sector, Side,
		nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* Get a pointer to the sector's data and convert into bytes/timings */
	if ( Floppy_ReadSectors ( Drive, &pSectorData, Sector, Track, Side, 1, NULL, pSectorSize ) )
	{
		for ( i=0 ; i<*pSectorSize ; i++ )
			FDC_Buffer_Add ( pSectorData[ i ] );
		return 0;						/* No error */
	}

	/* Failed */
	LOG_TRACE(TRACE_FDC, "fdc read sector failed\n" );
	return FDC_STR_BIT_RNF;
}


/*-----------------------------------------------------------------------*/
/**
 * Write a sector to a floppy image in ST format (used in type II command)
 * Bytes were added to the FDC Buffer with a default timing (32 microsec) ;
 * we copy only the bytes into a temporary buffer, and write this buffer
 * to the floppy image.
 * If DMASectorsCount==0, the DMA won't transfer any byte from RAM to the FDC
 * and some '0' bytes will be written to the disk.
 * Return 0 if sector was written without error, or FDC_STR_BIT_RNF if an error occurred
 */
static uint8_t FDC_WriteSector_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side , int SectorSize )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	int	i;
	uint8_t	SectorData[ 1024 ];					/* max sector size for WD1772 */

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write sector addr=0x%x drive=%d track=%d sect=%d side=%d VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC_GetDMAAddress(), Drive, Track, Sector, Side,
		nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* Get the sector's data (ignore timings) */
	for ( i=0 ; i<SectorSize ; i++ )
		SectorData[ i ] = FDC_Buffer_Read_Byte_pos ( i );

	/* Write the sector's data */
	if ( Floppy_WriteSectors ( Drive, SectorData, Sector, Track, Side, 1, NULL, NULL ) )
		return 0;						/* No error */

	/* Failed */
	LOG_TRACE(TRACE_FDC, "fdc write sector failed\n" );
	return FDC_STR_BIT_RNF;
}


/*-----------------------------------------------------------------------*/
/**
 * Read an address field from a floppy image in ST format (used in type III command)
 * As ST images don't have address field, we compute a standard one based
 * on the current track/sector/side.
 * Each byte of the ID field is added to the FDC buffer with a default timing
 * (32 microsec)
 * Always return 0 = OK (FDC_STR_BIT_CRC_ERROR and FDC_STR_BIT_RNF are
 * always set 0 for ST/MSA)
 */
static uint8_t FDC_ReadAddress_ST ( uint8_t Drive , uint8_t Track , uint8_t Sector , uint8_t Side )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	buf_id[ 10 ];						/* 3 SYNC + IAM + TR + SIDE + SECTOR + SIZE + CRC1 + CRC2 */
	uint8_t	*p;
	uint16_t	CRC;
	int	i;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	/* If trying to access address field on a non existing track, then return RNF */
	if ( Track >= FDC_GetTracksPerDisk ( Drive ) )
	{
		fprintf ( stderr , "fdc : read address drive=%d track=%d side=%d, but maxtrack=%d, return RNF\n" ,
			Drive , Track , Side , FDC_GetTracksPerDisk ( Drive ) );
		return STX_SECTOR_FLAG_RNF;				/* Should not happen if FDC_NextSectorID_FdcCycles_ST succeeded before */
	}

	p = buf_id;

	*p++ = 0xa1;							/* SYNC bytes and IAM byte are included in the CRC */
	*p++ = 0xa1;
	*p++ = 0xa1;
	*p++ = 0xfe;
	*p++ = Track;
	*p++ = Side;
	*p++ = Sector;
	*p++ = FDC_SECTOR_SIZE_512;					/* ST/MSA images are 512 bytes per sector */

	FDC_CRC16 ( buf_id , 8 , &CRC );

	*p++ = CRC >> 8;
	*p++ = CRC & 0xff;

	/* 6 bytes per ID field,  don't return the 3 x $A1 and $FE */
	for ( i=4 ; i<10 ; i++ )
		FDC_Buffer_Add ( buf_id[ i ] );
	
	LOG_TRACE(TRACE_FDC, "fdc read address 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x VBL=%d video_cyc=%d %d@%d pc=%x\n",
		buf_id[4] , buf_id[5] , buf_id[6] , buf_id[7] , buf_id[8] , buf_id[9] ,
		nVBLs, FrameCycles, LineCycles, HblCounterVideo, M68000_GetPC());

	return 0;							/* No error */
}


/*-----------------------------------------------------------------------*/
/**
 * Read a track from a floppy image in ST format (used in type III command)
 * As ST images don't have gaps,sync,..., we compute a standard track based
 * on the current track/side.
 * Each byte of the track is added to the FDC buffer with a default timing
 * (32 microsec)
 * Always return 0 = OK (we fill the track buffer in all cases)
 */
static uint8_t FDC_ReadTrack_ST ( uint8_t Drive , uint8_t Track , uint8_t Side )
{
	int	FrameCycles, HblCounterVideo, LineCycles;
	uint8_t	buf_id[ 10 ];						/* 3 SYNC + IAM + TR + SIDE + SECTOR + SIZE + CRC1 + CRC2 */
	uint8_t	*p;
	uint16_t	CRC;
	int	Sector;
	uint8_t	*pSectorData;
	int	SectorSize;
	int	i;
	
	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc type III read track drive=%d track=%d side=%d VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		Drive, Track, Side, nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	/* If trying to access a non existing track, then return an empty / not formatted track */
	if ( Track >= FDC_GetTracksPerDisk ( Drive ) )
	{
		fprintf ( stderr , "fdc : read track drive=%d track=%d side=%d, but maxtrack=%d, building an unformatted track\n" ,
			Drive , Track , Side , FDC_GetTracksPerDisk ( Drive ) );
		for ( i=0 ; i<FDC_GetBytesPerTrack ( Drive , Track , Side ) ; i++ )
			FDC_Buffer_Add ( Hatari_rand() & 0xff );	/* Fill the track buffer with random bytes */
		return 0;
	}

	for ( i=0 ; i<FDC_TRACK_LAYOUT_STANDARD_GAP1 ; i++ )		/* GAP1 */
		FDC_Buffer_Add ( 0x4e );

	for ( Sector=1 ; Sector <= FDC_GetSectorsPerTrack ( Drive , Track , Side ) ; Sector++ )
	{
		for ( i=0 ; i<FDC_TRACK_LAYOUT_STANDARD_GAP2 ; i++ )	/* GAP2 */
			FDC_Buffer_Add ( 0x00 );

		/* Add the ID field for the sector */
		p = buf_id;
		for ( i=0 ; i<3 ; i++ )		*p++ = 0xa1;		/* SYNC (write $F5) */
		*p++ = 0xfe;						/* Index Address Mark */
		*p++ = Track;						/* Track */
		*p++ = Side;						/* Side */
		*p++ = Sector;						/* Sector */
		*p++ = FDC_SECTOR_SIZE_512;				/* 512 bytes/sector for ST/MSA */
		FDC_CRC16 ( buf_id , 8 , &CRC );
		*p++ = CRC >> 8;					/* CRC1 (write $F7) */
		*p++ = CRC & 0xff;					/* CRC2 */

		for ( i=0 ; i<10 ; i++ )				/* Add the ID field to the track data */
			FDC_Buffer_Add ( buf_id[ i ] );

		for ( i=0 ; i<FDC_TRACK_LAYOUT_STANDARD_GAP3a ; i++ )	/* GAP3a */
			FDC_Buffer_Add ( 0x4e );
		for ( i=0 ; i<FDC_TRACK_LAYOUT_STANDARD_GAP3b ; i++ )	/* GAP3b */
			FDC_Buffer_Add ( 0x00 );

		/* Add the data for the sector + build the CRC */
		crc16_reset ( &CRC );
		for ( i=0 ; i<3 ; i++ )
		{
			FDC_Buffer_Add ( 0xa1 );			/* SYNC (write $F5) */
			crc16_add_byte ( &CRC , 0xa1 );
		}

		FDC_Buffer_Add ( 0xfb );				/* Data Address Mark */
		crc16_add_byte ( &CRC , 0xfb );

		if ( Floppy_ReadSectors ( Drive, &pSectorData, Sector, Track, Side, 1, NULL, &SectorSize ) )
		{
			for ( i=0 ; i<SectorSize ; i++ )
			{
				FDC_Buffer_Add ( pSectorData[ i ] );
				crc16_add_byte ( &CRC , pSectorData[ i ] );
			}
		}
		else
		{
			/* In case of error, we put some 0x00 bytes, but this case should */
			/* not happen with ST/MSA disk images, all sectors should be present on each track */
			for ( i=0 ; i<512 ; i++ )
			{
				FDC_Buffer_Add ( 0x00 );
				crc16_add_byte ( &CRC , 0x00 );
			}
		}

		FDC_Buffer_Add ( CRC >> 8 );				/* CRC1 (write $F7) */
		FDC_Buffer_Add ( CRC & 0xff );				/* CRC2 */

		for ( i=0 ; i<FDC_TRACK_LAYOUT_STANDARD_GAP4 ; i++ )	/* GAP4 */
			FDC_Buffer_Add ( 0x4e );
	}

	while ( FDC_Buffer_Get_Size () < FDC_GetBytesPerTrack ( Drive , Track , Side ) )	/* Complete the track buffer */
	      FDC_Buffer_Add ( 0x4e );					/* GAP5 */

	return 0;							/* No error */
}


/*-----------------------------------------------------------------------*/
/**
 * Write a track to a floppy image in ST format (used in type III command)
 * Bytes were added to the FDC Buffer with a default timing (32 microsec) ;
 * we copy only the bytes into a temporary buffer, and write this buffer
 * to the floppy image.
 * If DMASectorsCount==0, the DMA won't transfer any byte from RAM to the FDC
 * and some '0' bytes will be written to the disk.
 * Return 0 if track was written without error, or FDC_STR_BIT_LOST_DATA if an error occurred
 *
 * TODO : this is not supported for ST/MSA at the moment, so we return FDC_STR_BIT_LOST_DATA
 */
static uint8_t FDC_WriteTrack_ST ( uint8_t Drive , uint8_t Track , uint8_t Side , int TrackSize )
{
	int	FrameCycles, HblCounterVideo, LineCycles;

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write track not supported addr=0x%x drive=%d track=%d side=%d VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC_GetDMAAddress(), Drive, Track, Side,
		nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	Log_Printf(LOG_TODO, "FDC type III command 'write track' is not supported with ST/MSA files\n");

	/* TODO : "Write track" should write all the sectors after extracting them from the track data ? */

	/* Failed */
	LOG_TRACE(TRACE_FDC, "fdc write track failed\n" );
	return FDC_STR_BIT_LOST_DATA;
}


/*-----------------------------------------------------------------------*/
/**
 * Write word to density mode register to choose between DD/HD behaviour for the FDC
 * Used on MegaSTE, TT and Falcon
 *
 *   ______________10  Density Control Register
 *                 ||
 *                 |+- FDC Frequency 0:8MHz 1:16MHz
 *                 +-- Density 0:DD 1:HD
 *
 * For DD disks:  0x00
 * For HD disks:  0x03
 */
void FDC_DensityMode_WriteWord ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;


	M68000_WaitState(4);			/* TODO : check on real HW */

	FDC.DensityMode = IoMem_ReadWord(0xff860e);

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc write 860e density=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC.DensityMode , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );
}


/*-----------------------------------------------------------------------*/
/**
 * Read word from density mode register to choose between DD/HD behaviour for the FDC
 * Used on MegaSTE, TT and Falcon
 *
 *   ______________10  Density Control Register
 *                 ||
 *                 |+- FDC Frequency 0:8MHz 1:16MHz
 *                 +-- Density 0:DD 1:HD
 *
 * For DD disks:  0x00
 * For HD disks:  0x03
 */
void FDC_DensityMode_ReadWord ( void )
{
	int FrameCycles, HblCounterVideo, LineCycles;


	M68000_WaitState(4);			/* TODO : check on real HW */

	Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );

	LOG_TRACE(TRACE_FDC, "fdc read 860e density=0x%x VBL=%d video_cyc=%d %d@%d pc=%x\n" ,
		FDC.DensityMode , nVBLs , FrameCycles, LineCycles, HblCounterVideo , M68000_GetPC() );

	IoMem_WriteWord( 0xff860e , FDC.DensityMode );
}