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
|
/*
Hatari - ikbd.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.
The keyboard processor(6301) handles any joystick/mouse/keyboard task
and sends bytes to the ACIA(6850).
The IKBD has a small ROM which is used to process various commands send
by the main CPU to the THE IKBD.
Due to lack of real HD6301 emulation, those commands are handled by
functionally equivalent code that tries to be as close as possible
to a real HD6301.
For program using their own HD6301 code, we also use some custom
handlers to emulate the expected result.
*/
const char IKBD_fileid[] = "Hatari ikbd.c";
/* 2007/09/29 [NP] Use the new int.c to add interrupts with INT_CPU_CYCLE / INT_MFP_CYCLE. */
/* 2007/12/09 [NP] If reset is written to ACIA control register, we must call ACIA_Reset to reset */
/* RX/TX status. Reading the control register fffc00 just after a reset should */
/* return the value 0x02 (used in Transbeauce 2 demo loader). */
/* 2008/07/06 [NP] Add support for executing 8 bit code sent to the 6301 processor. */
/* Instead of implementing a full 6301 emulator, we compute a checksum for each */
/* program sent to the 6301 RAM. If the checksum is recognized, we call some */
/* functions to emulate the behaviour of the 6301 in that case. */
/* When the 6301 is in 'Execute' mode (command 0x22), we must stop the normal */
/* reporting of key/mouse/joystick and use our custom handlers for each read or */
/* write to $fffc02. */
/* After a reset command, returns $F1 after $F0 (needed by Dragonnels Demo). */
/* This fixes the Transbeauce 2 demo menu, the Dragonnels demo menu and the */
/* Froggies Over The Fence demo menu (yeah ! enjoy this master piece of demo !). */
/* 2011/05/11 [NP] Add proper support for emulating TX buffer empty/full in status register bit 1 */
/* when writing to $fffc02 (using an internal timer). */
/* 2011/07/14 [NP] Don't clear bytes in transit when ACIA_Reset is called ; if a byte is sent to */
/* the ikbd it should not be cancelled ? FIXME : this would need more tests on a */
/* real ST (fix Froggies Over The Fence's menu when selecting a demo). */
/* 2011/07/31 [NP] Don't clear bytes in transit in the ACIA when the IKBD is reset (fix Overdrive */
/* by Phalanx). */
/* 2011/12/27 [NP] When sending new bytes while a byte is already in transfer from ACIA to IKBD, */
/* don't restart the internal TX timer (fix 'Pandemonium Demos' Intro). */
/* eg : .loop : move.b d0,$fc02.w btst #1,$fc00.w beq.s .loop */
/* 2012/01/22 [NP] Enable both mouse and joystick reporting when commands 0x12 and 0x14 are */
/* received during the IKBD reset. */
/* 2012/02/26 [NP] Handle TX interrupt in the ACIA (eg by sending 0xb6 instead of 0x96 after */
/* resetting the ACIA) (fix the game 'Hades Nebula'). */
/* 2012/10/10 [NP] Use the new ACIA emulation in acia.c ; add support for the IKBD's SCI, which */
/* is similar to the ACIA, with fixed 8 data bits, 1 stop bit and no parity bit. */
/* 2012/12/23 [NP] Fix timings for the commands $16, $1C, $87-$9A. The first byte is returned */
/* between 'min' and 'max' cycles after receiving the full command. The delay */
/* is not fixed to simulate the slight variations measured on a real ST. */
/* 2012/12/24 [NP] Rewrite SetClock and ReadClock commands to behave like the real IKBD. */
/* Instead of using time()/localtime() to handle the clock, we now increment it */
/* on each VBL, taking care of the BCD data (overflows and such) like in the IKBD. */
/* (this new code is based on the HD6301 disassembly of the IKBD's ROM) */
/* 2013/01/02 [NP] - Use IKBD_OutputBuffer_CheckFreeCount to ensure there's enough room in the */
/* output buffer before sending an IKBD packet. If there's not enough bytes to */
/* transfer the whole packet, then the packet must be discarded. */
/* - Don't ignore a new RDR byte if the output buffer is not empty yet. The IKBD */
/* can handle new bytes asynchronously using some interrupt while still processing */
/* another command. New RDR is discarded only if the input buffer is full. */
/* 2013/01/13 [NP] For hardware and software reset, share the common code in IKBD_Boot_ROM(). */
/* 2014/07/06 [NP] Ignore command 0x13 IKBD_Cmd_StopKeyboardTransfer during ikbd's reset. This is */
/* required for the loader of 'Just Bugging' by ACF which sends 0x11 and 0x13 just */
/* after 0x80 0x01 (temporary fix, would need to be measured on a real STF to see */
/* if it's always ignored or just during a specific delay) */
/* 2015/10/10 [NP] When IKBD_Reset / IKBD_Boot_ROM are called, we should not restart the autosend */
/* handler INTERRUPT_IKBD_AUTOSEND if it's already set, else we can loose keyboard */
/* input if IKBD_Reset is called in a loop before any event could be processed */
/* (this could happen if a program called the 'RESET' instruction in a loop, then */
/* we lost the F12 key for example) (fix endless RESET when doing a warm reset */
/* (alt+r) during the 'Vodka Demo' by 'Equinox', only solution was to kill Hatari) */
/* 2017/10/27 [TS] Add Audio Sculpture 6301 program checksum and emulation */
#include <inttypes.h>
#include "main.h"
#include "configuration.h"
#include "ikbd.h"
#include "cycles.h"
#include "cycInt.h"
#include "ioMem.h"
#include "joy.h"
#include "m68000.h"
#include "memorySnapShot.h"
#include "mfp.h"
#include "video.h"
#include "utils.h"
#include "acia.h"
#include "clocks_timings.h"
#define DBL_CLICK_HISTORY 0x07 /* Number of frames since last click to see if need to send one or two clicks */
#define ACIA_CYCLES 7200 /* Cycles (Multiple of 4) between sent to ACIA from keyboard along serial line - 500Hz/64, (approx' 6920-7200cycles from test program) */
#define ABS_X_ONRESET 0 /* Initial XY for absolute mouse position after RESET command */
#define ABS_Y_ONRESET 0
#define ABS_MAX_X_ONRESET 320 /* Initial absolute mouse limits after RESET command */
#define ABS_MAY_Y_ONRESET 200 /* These values are never actually used as user MUST call 'IKBD_Cmd_AbsMouseMode' before ever using them */
#define ABS_PREVBUTTONS (0x02|0x8) /* Don't report any buttons up on first call to 'IKBD_Cmd_ReadAbsMousePos' */
#define IKBD_RESET_CYCLES 502000 /* Number of cycles (for a 68000 at 8 MHz) between sending the reset command and receiving $F1 */
#define IKBD_ROM_VERSION 0xF1 /* On reset, the IKBD will return either 0xF0 or 0xF1, depending on the IKBD's ROM */
/* version. Only very early ST returned 0xF0, so we use 0xF1 which is the most common case.*/
/* Beside, some programs explicitly wait for 0xF1 after a reset (Dragonnels demo) */
/* Keyboard state */
KEYBOARD Keyboard;
/* Keyboard processor */
KEYBOARD_PROCESSOR KeyboardProcessor; /* Keyboard processor details */
/* Pattern of mouse button up/down in ST frames (run off a double-click message) */
static const uint8_t DoubleClickPattern[] =
{
BUTTON_MOUSE,BUTTON_MOUSE,BUTTON_MOUSE,BUTTON_MOUSE,
0,0,0,0,BUTTON_MOUSE,BUTTON_MOUSE,BUTTON_MOUSE,BUTTON_MOUSE
};
static bool bMouseDisabled, bJoystickDisabled;
static bool bDuringResetCriticalTime, bBothMouseAndJoy;
static bool bMouseEnabledDuringReset;
/*
HD6301 processor by Hitachi
References :
- HD6301V1, HD63A01V1, HD63B01V1 CMOS MCU datasheet by Hitachi
The HD6301 is connected to the ACIA through TX and RX pins.
Serial transfers are made with 8 bit word, 1 stop bit, no parity and 7812.5 baud
The IKBD's ROM is using 2 buffers to handle input/output on the serial line
in an asynchronous way, by using the SCI's interrupt at address $FEE2. This means
the IKBD can execute a new command as soon as the current one is completed, as it is
the interrupt function that will handle sending bytes to the ACIA.
Input buffer : 8 bytes, located at $CD-$D4 in the IKBD's RAM.
New bytes received in RDR are added to this buffer, until we have
enough bytes to obtain a valid command (with its potential parameters)
If the buffer already contains 8 bytes, new bytes are ignored (lost).
This buffer is emptied if a valid command was processed or if the first
byte in the buffer is not a valid command.
Output buffer : 20 bytes as a ring buffer, located at $D9-$ED in the IKBD's RAM.
When the IKBD automatically reports events or when a command returns some bytes,
those 'n' bytes are added to the ring buffer.
If the ring buffer doesn't have enough space to store 'n' new bytes, the 'n' bytes
are ignored (lost).
Each time a byte is correctly sent in TDR, a new byte is processed, until the ring
buffer becomes empty.
Special behaviours during the IKBD reset :
If the following commands are received during the reset of the IKBD,
the IKBD will go in a special mode and report both mouse and joystick at the same time :
0x08 0x14 relative mouse on , joysticks auto
0x08 0x0b 0x14 relative mouse on , mouse threshold , joysticks auto (eg Barbarian 1 by Psygnosis)
0x12 0x14 disable mouse , joysticks auto (eg Hammerfist)
0x12 0x1a disable mouse , disable joysticks
In that case mouse and joystick buttons will be reported in a "mouse report" packet
and joystick actions (except buttons) will be reported in a "joystick report" packet.
*/
static void IKBD_RunKeyboardCommand(uint8_t aciabyte);
/* List of possible keyboard commands, others are seen as NOPs by keyboard processor */
static void IKBD_Cmd_Reset(void);
static void IKBD_Cmd_MouseAction(void);
static void IKBD_Cmd_RelMouseMode(void);
static void IKBD_Cmd_AbsMouseMode(void);
static void IKBD_Cmd_MouseCursorKeycodes(void);
static void IKBD_Cmd_SetMouseThreshold(void);
static void IKBD_Cmd_SetMouseScale(void);
static void IKBD_Cmd_ReadAbsMousePos(void);
static void IKBD_Cmd_SetInternalMousePos(void);
static void IKBD_Cmd_SetYAxisDown(void);
static void IKBD_Cmd_SetYAxisUp(void);
static void IKBD_Cmd_StartKeyboardTransfer(void);
static void IKBD_Cmd_TurnMouseOff(void);
static void IKBD_Cmd_StopKeyboardTransfer(void);
static void IKBD_Cmd_ReturnJoystickAuto(void);
static void IKBD_Cmd_StopJoystick(void);
static void IKBD_Cmd_ReturnJoystick(void);
static void IKBD_Cmd_SetJoystickMonitoring(void);
static void IKBD_Cmd_SetJoystickFireDuration(void);
static void IKBD_Cmd_SetCursorForJoystick(void);
static void IKBD_Cmd_DisableJoysticks(void);
static void IKBD_Cmd_SetClock(void);
static void IKBD_Cmd_ReadClock(void);
static void IKBD_Cmd_LoadMemory(void);
static void IKBD_Cmd_ReadMemory(void);
static void IKBD_Cmd_Execute(void);
static void IKBD_Cmd_ReportMouseAction(void);
static void IKBD_Cmd_ReportMouseMode(void);
static void IKBD_Cmd_ReportMouseThreshold(void);
static void IKBD_Cmd_ReportMouseScale(void);
static void IKBD_Cmd_ReportMouseVertical(void);
static void IKBD_Cmd_ReportMouseAvailability(void);
static void IKBD_Cmd_ReportJoystickMode(void);
static void IKBD_Cmd_ReportJoystickAvailability(void);
/* Keyboard Command */
static const struct {
uint8_t Command;
uint8_t NumParameters;
void (*pCallFunction)(void);
} KeyboardCommands[] =
{
/* Known messages, counts include command byte */
{ 0x80,2, IKBD_Cmd_Reset },
{ 0x07,2, IKBD_Cmd_MouseAction },
{ 0x08,1, IKBD_Cmd_RelMouseMode },
{ 0x09,5, IKBD_Cmd_AbsMouseMode },
{ 0x0A,3, IKBD_Cmd_MouseCursorKeycodes },
{ 0x0B,3, IKBD_Cmd_SetMouseThreshold },
{ 0x0C,3, IKBD_Cmd_SetMouseScale },
{ 0x0D,1, IKBD_Cmd_ReadAbsMousePos },
{ 0x0E,6, IKBD_Cmd_SetInternalMousePos },
{ 0x0F,1, IKBD_Cmd_SetYAxisDown },
{ 0x10,1, IKBD_Cmd_SetYAxisUp },
{ 0x11,1, IKBD_Cmd_StartKeyboardTransfer },
{ 0x12,1, IKBD_Cmd_TurnMouseOff },
{ 0x13,1, IKBD_Cmd_StopKeyboardTransfer },
{ 0x14,1, IKBD_Cmd_ReturnJoystickAuto },
{ 0x15,1, IKBD_Cmd_StopJoystick },
{ 0x16,1, IKBD_Cmd_ReturnJoystick },
{ 0x17,2, IKBD_Cmd_SetJoystickMonitoring },
{ 0x18,1, IKBD_Cmd_SetJoystickFireDuration },
{ 0x19,7, IKBD_Cmd_SetCursorForJoystick },
{ 0x1A,1, IKBD_Cmd_DisableJoysticks },
{ 0x1B,7, IKBD_Cmd_SetClock },
{ 0x1C,1, IKBD_Cmd_ReadClock },
{ 0x20,4, IKBD_Cmd_LoadMemory },
{ 0x21,3, IKBD_Cmd_ReadMemory },
{ 0x22,3, IKBD_Cmd_Execute },
/* Report message (top bit set) */
{ 0x87,1, IKBD_Cmd_ReportMouseAction },
{ 0x88,1, IKBD_Cmd_ReportMouseMode },
{ 0x89,1, IKBD_Cmd_ReportMouseMode },
{ 0x8A,1, IKBD_Cmd_ReportMouseMode },
{ 0x8B,1, IKBD_Cmd_ReportMouseThreshold },
{ 0x8C,1, IKBD_Cmd_ReportMouseScale },
{ 0x8F,1, IKBD_Cmd_ReportMouseVertical },
{ 0x90,1, IKBD_Cmd_ReportMouseVertical },
{ 0x92,1, IKBD_Cmd_ReportMouseAvailability },
{ 0x94,1, IKBD_Cmd_ReportJoystickMode },
{ 0x95,1, IKBD_Cmd_ReportJoystickMode },
{ 0x99,1, IKBD_Cmd_ReportJoystickMode },
{ 0x9A,1, IKBD_Cmd_ReportJoystickAvailability },
{ 0xFF,0, NULL } /* Term */
};
/*----------------------------------------------------------------------*/
/* Variables/defines/functions used to transfer data between the */
/* IKBD's SCI and the ACIA. */
/*----------------------------------------------------------------------*/
#define IKBD_TRCSR_BIT_WU 0x01 /* Wake Up */
#define IKBD_TRCSR_BIT_TE 0x02 /* Transmit Enable */
#define IKBD_TRCSR_BIT_TIE 0x04 /* Transmit Interrupt Enable */
#define IKBD_TRCSR_BIT_RE 0x08 /* Receive Enable */
#define IKBD_TRCSR_BIT_RIE 0x10 /* Receive Interrupt Enable */
#define IKBD_TRCSR_BIT_TDRE 0x20 /* Transmit Data Register Empty */
#define IKBD_TRCSR_BIT_ORFE 0x40 /* Over Run Framing Error */
#define IKBD_TRCSR_BIT_RDRF 0x80 /* Receive Data Register Full */
/* Possible states when handling TX/RX in the IKBD's Serial Communication Interface */
enum
{
IKBD_SCI_STATE_IDLE = 0,
IKBD_SCI_STATE_DATA_BIT,
IKBD_SCI_STATE_STOP_BIT
};
typedef struct {
/* IKBD's SCI internal registers */
uint8_t RMCR; /* reg 0x10 : Rate and Mode Control Register */
uint8_t TRCSR; /* reg 0x11 : Transmit/Receive Control and Status Register */
uint8_t TDR; /* reg 0x12 : Transmit Data Register */
uint8_t RDR; /* reg 0x13 : Receive Data Register */
int SCI_TX_State;
uint8_t TSR; /* Transmit Shift Register */
uint8_t SCI_TX_Size; /* How many data bits left to transmit in TSR (8 .. 0) */
int SCI_TX_Delay; /* If >0, wait SCI_TX_Delay calls of IKBD_SCI_Set_Line_TX before */
/* transferring a new byte in TDR (to simulate the time needed by */
/* the IKBD to process a command and return the result) */
int SCI_RX_State;
uint8_t RSR; /* Receive Shift Register */
uint8_t SCI_RX_Size; /* How many bits left to receive in RSR (8 .. 0) */
/* Date/Time is stored in the IKBD using 6 bytes in BCD format */
/* Clock is cleared on cold reset, but keeps its values on warm reset */
/* Original RAM location : $82=year $83=month $84=day $85=hour $86=minute $87=second */
uint8_t Clock[ 6 ];
int64_t Clock_micro; /* Incremented every VBL to update Clock[] every second */
} IKBD_STRUCT;
static IKBD_STRUCT IKBD;
static IKBD_STRUCT *pIKBD = &IKBD;
static void IKBD_Init_Pointers ( ACIA_STRUCT *pACIA_IKBD );
static void IKBD_Boot_ROM ( bool ClearAllRAM );
static void IKBD_SCI_Get_Line_RX ( int rx_bit );
static uint8_t IKBD_SCI_Set_Line_TX ( void );
static void IKBD_Process_RDR ( uint8_t RDR );
static void IKBD_Check_New_TDR ( void );
static bool IKBD_OutputBuffer_CheckFreeCount ( int Nb );
static int IKBD_Delay_Random ( int min , int max );
static void IKBD_Cmd_Return_Byte ( uint8_t Data );
static void IKBD_Cmd_Return_Byte_Delay ( uint8_t Data , int Delay_Cycles );
static void IKBD_Send_Byte_Delay ( uint8_t Data , int Delay_Cycles );
static bool IKBD_BCD_Check ( uint8_t val );
static uint8_t IKBD_BCD_Adjust ( uint8_t val );
/*-----------------------------------------------------------------------*/
/* Belows part is used to emulate the behaviour of custom 6301 programs */
/* sent to the IKBD's RAM. */
/*-----------------------------------------------------------------------*/
static void IKBD_LoadMemoryByte ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_CommonBoot ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_FroggiesMenu_Read ( void );
static void IKBD_CustomCodeHandler_FroggiesMenu_Write ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_Transbeauce2Menu_Read ( void );
static void IKBD_CustomCodeHandler_Transbeauce2Menu_Write ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_DragonnelsMenu_Read ( void );
static void IKBD_CustomCodeHandler_DragonnelsMenu_Write ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_ChaosAD_Read ( void );
static void IKBD_CustomCodeHandler_ChaosAD_Write ( uint8_t aciabyte );
static void IKBD_CustomCodeHandler_AudioSculpture_Color_Read ( void );
static void IKBD_CustomCodeHandler_AudioSculpture_Mono_Read ( void );
static void IKBD_CustomCodeHandler_AudioSculpture_Read ( bool ColorMode );
static void IKBD_CustomCodeHandler_AudioSculpture_Write ( uint8_t aciabyte );
static int MemoryLoadNbBytesTotal = 0; /* total number of bytes to send with the command 0x20 */
static int MemoryLoadNbBytesLeft = 0; /* number of bytes that remain to be sent */
static uint32_t MemoryLoadCrc = 0xffffffff; /* CRC of the bytes sent to the IKBD */
static int MemoryExeNbBytes = 0; /* current number of bytes sent to the IKBD when IKBD_ExeMode is true */
static void (*pIKBD_CustomCodeHandler_Read) ( void );
static void (*pIKBD_CustomCodeHandler_Write) ( uint8_t );
static bool IKBD_ExeMode = false;
static uint8_t ScanCodeState[ 128 ]; /* state of each key : 0=released 1=pressed */
/* This array contains all known custom 6301 programs, with their CRC */
static const struct
{
uint32_t LoadMemCrc; /* CRC of the bytes sent using the command 0x20 */
void (*ExeBootHandler) ( uint8_t ); /* function handling write to $fffc02 during the 'boot' mode */
int MainProgNbBytes; /* number of bytes of the main 6301 program */
uint32_t MainProgCrc; /* CRC of the main 6301 program */
void (*ExeMainHandler_Read) ( void );/* function handling read to $fffc02 in the main 6301 program */
void (*ExeMainHandler_Write) ( uint8_t ); /* function handling write to $fffc02 in the main 6301 program */
const char *Name;
}
CustomCodeDefinitions[] =
{
{
0x2efb11b1 ,
IKBD_CustomCodeHandler_CommonBoot ,
167,
0xe7110b6d ,
IKBD_CustomCodeHandler_FroggiesMenu_Read ,
IKBD_CustomCodeHandler_FroggiesMenu_Write ,
"Froggies Over The Fence Main Menu"
} ,
{
0xadb6b503 ,
IKBD_CustomCodeHandler_CommonBoot ,
165,
0x5617c33c ,
IKBD_CustomCodeHandler_Transbeauce2Menu_Read ,
IKBD_CustomCodeHandler_Transbeauce2Menu_Write ,
"Transbeauce 2 Main Menu"
} ,
{
0x33c23cdf ,
IKBD_CustomCodeHandler_CommonBoot ,
83 ,
0xdf3e5a88 ,
IKBD_CustomCodeHandler_DragonnelsMenu_Read ,
IKBD_CustomCodeHandler_DragonnelsMenu_Write ,
"Dragonnels Main Menu"
},
{
0x9ad7fcdf ,
IKBD_CustomCodeHandler_CommonBoot ,
109 ,
0xa11d8be5 ,
IKBD_CustomCodeHandler_ChaosAD_Read ,
IKBD_CustomCodeHandler_ChaosAD_Write ,
"Chaos A.D."
},
{
0xbc0c206d,
IKBD_CustomCodeHandler_CommonBoot ,
91 ,
0x119b26ed ,
IKBD_CustomCodeHandler_AudioSculpture_Color_Read ,
IKBD_CustomCodeHandler_AudioSculpture_Write ,
"Audio Sculpture Color"
},
{
0xbc0c206d ,
IKBD_CustomCodeHandler_CommonBoot ,
91 ,
0x63b5f4df ,
IKBD_CustomCodeHandler_AudioSculpture_Mono_Read ,
IKBD_CustomCodeHandler_AudioSculpture_Write ,
"Audio Sculpture Mono"
}
};
/*-----------------------------------------------------------------------*/
/**
* Init the IKBD processor.
* Connect the IKBD RX/TX callback functions to the ACIA.
* This is called only once, when the emulator starts.
*/
void IKBD_Init ( void )
{
LOG_TRACE ( TRACE_IKBD_ALL, "ikbd init\n" );
/* Set the callback functions for RX/TX line */
IKBD_Init_Pointers ( pACIA_IKBD );
}
/*-----------------------------------------------------------------------*/
/**
* Init some functions/memory pointers for the IKBD.
* This is called at Init and when restoring a memory snapshot.
*/
static void IKBD_Init_Pointers ( ACIA_STRUCT *pACIA_IKBD )
{
pACIA_IKBD->Get_Line_RX = IKBD_SCI_Set_Line_TX; /* Connect ACIA's RX to IKBD SCI's TX */
pACIA_IKBD->Set_Line_TX = IKBD_SCI_Get_Line_RX; /* Connect ACIA's TX to IKBD SCI's RX */
}
/*-----------------------------------------------------------------------*/
/**
* Reset the IKBD processor
*/
/* This function is called after a hardware reset of the IKBD.
* 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.
* We clear the serial interface and we execute the function
* that emulates booting the ROM at 0xF000.
*/
void IKBD_Reset ( bool bCold )
{
LOG_TRACE ( TRACE_IKBD_ALL , "ikbd reset mode=%s\n" , bCold?"cold":"warm" );
/* Reset the SCI */
pIKBD->TRCSR = IKBD_TRCSR_BIT_TDRE;
pIKBD->SCI_TX_State = IKBD_SCI_STATE_IDLE;
pIKBD->TSR = 0;
pIKBD->SCI_TX_Size = 0;
pIKBD->SCI_TX_Delay = 0;
pIKBD->SCI_RX_State = IKBD_SCI_STATE_IDLE;
pIKBD->RSR = 0;
pIKBD->SCI_RX_Size = 0;
/* On cold reset, clear the whole RAM (including clock data) */
/* On warm reset, the clock data should be kept */
if ( bCold )
IKBD_Boot_ROM ( true );
else
IKBD_Boot_ROM ( false );
}
/* This function emulates the boot code stored in the ROM at address $F000.
* This boot code is called either after a hardware reset, or when the
* reset command ($80 $01) is received.
* Depending on the conditions, we should clear the clock data or not (the
* real IKBD will test+clear RAM either in range $80-$FF or in range $89-$FF)
*/
static void IKBD_Boot_ROM ( bool ClearAllRAM )
{
int i;
LOG_TRACE ( TRACE_IKBD_ALL , "ikbd boot rom clear_all=%s\n" , ClearAllRAM?"yes":"no" );
/* Clear clock data when the 128 bytes of RAM are cleared */
if ( ClearAllRAM )
{
/* Clear clock data on cold reset */
for ( i=0 ; i<6 ; i++ )
pIKBD->Clock[ i ] = 0;
pIKBD->Clock_micro = 0;
}
// pIKBD->Clock[ 0 ] = 0x99;
// pIKBD->Clock[ 1 ] = 0x12;
// pIKBD->Clock[ 2 ] = 0x31;
// pIKBD->Clock[ 3 ] = 0x23;
// pIKBD->Clock[ 4 ] = 0x59;
// pIKBD->Clock[ 5 ] = 0x57;
/* Set default reporting mode for mouse/joysticks */
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEREL;
KeyboardProcessor.JoystickMode = AUTOMODE_JOYSTICK;
KeyboardProcessor.Abs.X = ABS_X_ONRESET;
KeyboardProcessor.Abs.Y = ABS_Y_ONRESET;
KeyboardProcessor.Abs.MaxX = ABS_MAX_X_ONRESET;
KeyboardProcessor.Abs.MaxY = ABS_MAY_Y_ONRESET;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons = ABS_PREVBUTTONS;
KeyboardProcessor.Mouse.DeltaX = KeyboardProcessor.Mouse.DeltaY = 0;
KeyboardProcessor.Mouse.XScale = KeyboardProcessor.Mouse.YScale = 0;
KeyboardProcessor.Mouse.XThreshold = KeyboardProcessor.Mouse.YThreshold = 1;
KeyboardProcessor.Mouse.KeyCodeDeltaX = KeyboardProcessor.Mouse.KeyCodeDeltaY = 1;
KeyboardProcessor.Mouse.YAxis = 1; /* Y origin at top */
KeyboardProcessor.Mouse.Action = 0;
KeyboardProcessor.Joy.PrevJoyData[0] = KeyboardProcessor.Joy.PrevJoyData[1] = 0;
for ( i=0 ; i<128 ; i++ )
ScanCodeState[ i ] = 0; /* key is released */
/* Reset our keyboard states and clear key state table */
Keyboard.BufferHead = Keyboard.BufferTail = 0;
Keyboard.NbBytesInOutputBuffer = 0;
Keyboard.nBytesInInputBuffer = 0;
Keyboard.PauseOutput = false;
memset(Keyboard.KeyStates, 0, sizeof(Keyboard.KeyStates));
Keyboard.bLButtonDown = BUTTON_NULL;
Keyboard.bRButtonDown = BUTTON_NULL;
Keyboard.bOldLButtonDown = Keyboard.bOldRButtonDown = BUTTON_NULL;
Keyboard.LButtonDblClk = Keyboard.RButtonDblClk = 0;
Keyboard.LButtonHistory = Keyboard.RButtonHistory = 0;
/* Store bool for when disable mouse or joystick */
bMouseDisabled = bJoystickDisabled = false;
/* do emulate hardware 'quirk' where if disable both with 'x' time
* of a RESET command they are ignored! */
bDuringResetCriticalTime = true;
bBothMouseAndJoy = false;
bMouseEnabledDuringReset = false;
/* Remove any custom handlers used to emulate code loaded to the 6301's RAM */
if ( ( MemoryLoadNbBytesLeft != 0 ) || ( IKBD_ExeMode == true ) )
{
LOG_TRACE ( TRACE_IKBD_ALL , "ikbd stop memory load and turn off custom exe\n" );
MemoryLoadNbBytesLeft = 0;
pIKBD_CustomCodeHandler_Read = NULL;
pIKBD_CustomCodeHandler_Write = NULL;
IKBD_ExeMode = false;
}
/* During the boot, the IKBD will test all the keys to ensure no key */
/* is stuck. We use a timer to emulate the time needed for this part */
/* (eg Lotus Turbo Esprit 2 requires at least a delay of 50000 cycles */
/* or it will crash during start up) */
CycInt_AddRelativeInterrupt( IKBD_RESET_CYCLES , INT_CPU8_CYCLE , INTERRUPT_IKBD_RESETTIMER );
/* Add auto-update function to the queue */
/* We add it only if it was not active, else this can lead to unresponsive keyboard/input */
/* when RESET instruction is called in a loop in less than 150000 cycles */
Keyboard.AutoSendCycles = 150000; /* approx every VBL */
if ( CycInt_InterruptActive ( INTERRUPT_IKBD_AUTOSEND ) == false )
CycInt_AddRelativeInterrupt ( Keyboard.AutoSendCycles, INT_CPU8_CYCLE, INTERRUPT_IKBD_AUTOSEND );
LOG_TRACE ( TRACE_IKBD_ALL , "ikbd reset done, starting reset timer\n" );
}
/*-----------------------------------------------------------------------*/
/**
* This timer is started by IKBD_Boot_ROM to emulate the time needed
* to setup the IKBD in its default state after a reset.
* If some IKBD commands are received during the boot phase they may be ignored.
*/
void IKBD_InterruptHandler_ResetTimer(void)
{
LOG_TRACE(TRACE_IKBD_ALL, "ikbd reset timer completed, resuming ikbd processing VBLs=%i framecyc=%i\n",
nVBLs, Video_GetCyclesSinceVbl());
/* Remove this interrupt from list and re-order */
CycInt_AcknowledgeInterrupt();
/* Reset timer is over */
bDuringResetCriticalTime = false;
bMouseEnabledDuringReset = false;
/* Return $F1 when IKBD's boot is complete */
IKBD_Cmd_Return_Byte_Delay ( IKBD_ROM_VERSION , IKBD_Delay_Random ( 0 , 3000 ) );
}
/*-----------------------------------------------------------------------*/
/**
* Save/Restore snapshot of local variables
* ('MemorySnapShot_Store' handles type)
*/
void IKBD_MemorySnapShot_Capture(bool bSave)
{
unsigned int i;
/* Save/Restore details */
MemorySnapShot_Store(&Keyboard, sizeof(Keyboard));
MemorySnapShot_Store(&KeyboardProcessor, sizeof(KeyboardProcessor));
MemorySnapShot_Store(&bMouseDisabled, sizeof(bMouseDisabled));
MemorySnapShot_Store(&bJoystickDisabled, sizeof(bJoystickDisabled));
MemorySnapShot_Store(&bDuringResetCriticalTime, sizeof(bDuringResetCriticalTime));
MemorySnapShot_Store(&bBothMouseAndJoy, sizeof(bBothMouseAndJoy));
MemorySnapShot_Store(&bMouseEnabledDuringReset, sizeof(bMouseEnabledDuringReset));
/* restore custom 6301 program if needed */
MemorySnapShot_Store(&IKBD_ExeMode, sizeof(IKBD_ExeMode));
MemorySnapShot_Store(&MemoryLoadCrc, sizeof(MemoryLoadCrc));
if ((bSave == false) && (IKBD_ExeMode == true)) /* restoring a snapshot with active 6301 emulation */
{
for ( i = 0 ; i < sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ); i++ )
if ( CustomCodeDefinitions[ i ].MainProgCrc == MemoryLoadCrc )
{
pIKBD_CustomCodeHandler_Read = CustomCodeDefinitions[ i ].ExeMainHandler_Read;
pIKBD_CustomCodeHandler_Write = CustomCodeDefinitions[ i ].ExeMainHandler_Write;
Keyboard.BufferHead = Keyboard.BufferTail = 0; /* flush all queued bytes that would be read in $fffc02 */
Keyboard.NbBytesInOutputBuffer = 0;
break;
}
if ( i >= sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ) ) /* not found (should not happen) */
IKBD_ExeMode = false; /* turn off exe mode */
}
/* Save the IKBD's SCI part and restore the callback functions for RX/TX lines with the ACIA */
MemorySnapShot_Store(&IKBD, sizeof(IKBD));
if (!bSave) /* Restoring a snapshot */
{
IKBD_Init_Pointers ( pACIA_IKBD );
}
}
/************************************************************************/
/* This part emulates the IKBD's Serial Communication Interface. */
/* This is a simplified implementation that ignores the RMCR content, */
/* as we assume the IKBD and the ACIA will be using the same baud rate. */
/* The TX/RX baud rate is chosen at the ACIA level, and the IKBD will */
/* use the same rate. */
/* The SCI only supports 8 bits of data, with 1 start bit, 1 stop bit */
/* and no parity bit. */
/************************************************************************/
/*-----------------------------------------------------------------------*/
/**
* Prepare a new transfer. Copy TDR to TSR and initialize data size.
* Transfer will then start at the next call of IKBD_SCI_Set_Line_TX.
*/
static void IKBD_SCI_Prepare_TX ( IKBD_STRUCT *pIKBD )
{
pIKBD->TSR = pIKBD->TDR;
pIKBD->SCI_TX_Size = 8;
pIKBD->TRCSR |= IKBD_TRCSR_BIT_TDRE; /* TDR was copied to TSR. TDR is now empty */
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia prepare tx tsr=0x%02x size=%d VBL=%d HBL=%d\n" , pIKBD->TSR , pIKBD->SCI_TX_Size , nVBLs , nHBL );
}
/*-----------------------------------------------------------------------*/
/**
* Prepare a new reception. Initialize RSR and data size.
*/
static void IKBD_SCI_Prepare_RX ( IKBD_STRUCT *pIKBD )
{
pIKBD->RSR = 0;
pIKBD->SCI_RX_Size = 8;
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia prepare rx size=%d VBL=%d HBL=%d\n" , pIKBD->SCI_RX_Size , nVBLs , nHBL );
}
/*-----------------------------------------------------------------------*/
/**
* Receive a bit on the IKBD SCI's RX line (this is connected to the ACIA's TX)
* This will fill RDR with bits received from the serial line, using RSR.
* Incoming bits are stored in bit 7 of RSR, then RSR is shifted to the right.
* This is similar to the ACIA's RX, but with fixed parameters : 8 data bits,
* no parity bit and 1 stop bit.
*/
static void IKBD_SCI_Get_Line_RX ( int rx_bit )
{
int StateNext;
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia rx_state=%d bit=%d VBL=%d HBL=%d\n" , pIKBD->SCI_RX_State , rx_bit , nVBLs , nHBL );
StateNext = -1;
switch ( pIKBD->SCI_RX_State )
{
case IKBD_SCI_STATE_IDLE :
if ( rx_bit == 0 ) /* Receive one "0" start bit */
{
IKBD_SCI_Prepare_RX ( pIKBD );
StateNext = IKBD_SCI_STATE_DATA_BIT;
}
break; /* If no start bit, we stay in idle state */
case IKBD_SCI_STATE_DATA_BIT :
if ( rx_bit )
pIKBD->RSR |= 0x80;
pIKBD->SCI_RX_Size--;
if ( pIKBD->SCI_RX_Size > 0 ) /* All bits were not received yet */
pIKBD->RSR >>= 1;
else
StateNext = IKBD_SCI_STATE_STOP_BIT;
break;
case IKBD_SCI_STATE_STOP_BIT :
if ( rx_bit == 1 ) /* Wait for one "1" stop bit */
{
pIKBD->TRCSR &= ~IKBD_TRCSR_BIT_ORFE;
if ( ( pIKBD->TRCSR & IKBD_TRCSR_BIT_RDRF ) == 0 )
{
pIKBD->RDR = pIKBD->RSR;
pIKBD->TRCSR |= IKBD_TRCSR_BIT_RDRF;
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia get_rx received rsr=0x%02x VBL=%d HBL=%d\n" ,
pIKBD->RDR , nVBLs , nHBL );
IKBD_Process_RDR ( pIKBD->RDR ); /* Process this new byte */
}
else
{
pIKBD->TRCSR |= IKBD_TRCSR_BIT_ORFE; /* Overrun Error */
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia get_rx received rsr=0x%02x : ignored, rdr=0x%02x and rdrf already set VBL=%d HBL=%d\n" ,
pIKBD->RSR , pIKBD->RDR , nVBLs , nHBL );
IKBD_Process_RDR ( pIKBD->RDR ); /* RSR is lost, try to process the current RDR which was not read yet */
}
StateNext = IKBD_SCI_STATE_IDLE; /* Go to idle state and wait for start bit */
}
else /* Not a valid stop bit */
{
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia get_rx framing error VBL=%d HBL=%d\n" , nVBLs , nHBL );
pIKBD->TRCSR |= IKBD_TRCSR_BIT_ORFE; /* Framing Error */
StateNext = IKBD_SCI_STATE_IDLE; /* Go to idle state and wait for start bit */
}
break;
}
if ( StateNext >= 0 )
pIKBD->SCI_RX_State = StateNext; /* Go to a new state */
}
/*-----------------------------------------------------------------------*/
/**
* Send a bit on the IKBD SCI's TX line (this is connected to the ACIA's RX)
* When the SCI is idle, we send '1' stop bits.
*/
static uint8_t IKBD_SCI_Set_Line_TX ( void )
{
int StateNext;
uint8_t tx_bit = 1;
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia tx_state=%d tx_delay=%d VBL=%d HBL=%d\n" , pIKBD->SCI_TX_State , pIKBD->SCI_TX_Delay ,
nVBLs , nHBL );
StateNext = -1;
switch ( pIKBD->SCI_TX_State )
{
case IKBD_SCI_STATE_IDLE :
tx_bit = 1; /* In idle state, default is to send '1' stop bits */
if ( pIKBD->SCI_TX_Delay > 0 ) /* Should we delay the next TDR ? */
{
pIKBD->SCI_TX_Delay--; /* Don't do anything for now, send a stop bit */
break;
}
IKBD_Check_New_TDR (); /* Do we have a byte to load in TDR ? */
if ( ( pIKBD->TRCSR & IKBD_TRCSR_BIT_TDRE ) == 0 ) /* We have a new byte in TDR */
{
IKBD_SCI_Prepare_TX ( pIKBD );
tx_bit = 0; /* Send one '0' start bit */
StateNext = IKBD_SCI_STATE_DATA_BIT;
}
break;
case IKBD_SCI_STATE_DATA_BIT :
tx_bit = pIKBD->TSR & 1; /* New bit to send */
pIKBD->TSR >>= 1;
pIKBD->SCI_TX_Size--;
if ( pIKBD->SCI_TX_Size == 0 )
StateNext = IKBD_SCI_STATE_STOP_BIT;
break;
case IKBD_SCI_STATE_STOP_BIT :
tx_bit = 1; /* Send 1 stop bit */
StateNext = IKBD_SCI_STATE_IDLE; /* Go to idle state to see if a new TDR need to be sent */
break;
}
if ( StateNext >= 0 )
pIKBD->SCI_TX_State = StateNext; /* Go to a new state */
return tx_bit;
}
/*-----------------------------------------------------------------------*/
/**
* Handle the byte that was received in the RDR from the ACIA.
* Depending on the IKBD's emulation mode, we either pass it to the standard
* ROM's emulation layer, or we pass it to the custom handlers.
*/
static void IKBD_Process_RDR ( uint8_t RDR )
{
pIKBD->TRCSR &= ~IKBD_TRCSR_BIT_RDRF; /* RDR was read */
/* If IKBD is executing custom code, send the byte to the function handling this code */
if ( IKBD_ExeMode && pIKBD_CustomCodeHandler_Write )
{
(*pIKBD_CustomCodeHandler_Write) ( RDR );
return;
}
if ( MemoryLoadNbBytesLeft == 0 ) /* No pending MemoryLoad command */
IKBD_RunKeyboardCommand ( RDR ); /* Check for known commands */
else /* MemoryLoad command is not finished yet */
IKBD_LoadMemoryByte ( RDR ); /* Process bytes sent to the IKBD's RAM */
}
/*-----------------------------------------------------------------------*/
/**
* Check if we have a byte to copy to the IKBD's TDR, to send it to the ACIA.
* We get new bytes from the buffer filled by IKBD_Send_Byte_Delay
*/
static void IKBD_Check_New_TDR ( void )
{
// fprintf(stderr , "check new tdr %d %d\n", Keyboard.BufferHead , Keyboard.BufferTail );
if ( ( Keyboard.NbBytesInOutputBuffer > 0 )
&& ( Keyboard.PauseOutput == false ) )
{
pIKBD->TDR = Keyboard.Buffer[ Keyboard.BufferHead++ ];
Keyboard.BufferHead &= KEYBOARD_BUFFER_MASK;
Keyboard.NbBytesInOutputBuffer--;
pIKBD->TRCSR &= ~IKBD_TRCSR_BIT_TDRE;
}
}
/*-----------------------------------------------------------------------*/
/**
* Return true if the output buffer can store 'Nb' new bytes,
* else return false.
* Some games like 'Downfall' or 'Fokker' are continually issuing the same
* IKBD_Cmd_ReturnJoystick command without waiting for the returned bytes,
* which will fill the output buffer faster than the CPU can empty it.
* In that case, new messages must be discarded until the buffer has some room
* again for a whole packet.
*/
static bool IKBD_OutputBuffer_CheckFreeCount ( int Nb )
{
// fprintf ( stderr , "check %d %d head %d tail %d\n" , Nb , SIZE_KEYBOARD_BUFFER - Keyboard.NbBytesInOutputBuffer ,
// Keyboard.BufferHead , Keyboard.BufferTail );
if ( SIZE_KEYBOARD_BUFFER - Keyboard.NbBytesInOutputBuffer >= Nb )
return true;
else
{
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia output buffer is full, can't send %d bytes VBL=%d HBL=%d\n" ,
Nb, nVBLs , nHBL );
return false;
}
}
/*-----------------------------------------------------------------------*/
/**
* Return a random number between 'min' and 'max'.
* This is used when the IKBD send bytes to the ACIA, to add some
* randomness to the delay (on real hardware, the delay is not constant
* when a command return some bytes).
*/
static int IKBD_Delay_Random ( int min , int max )
{
return min + Hatari_rand() % ( max - min );
}
/*-----------------------------------------------------------------------*/
/**
* This function will buffer all the bytes returned by a specific
* IKBD_Cmd_xxx command. If we're using a custom handler, we should filter
* these bytes (keyboard, mouse, joystick) as they don't come from the custom handler.
*/
static void IKBD_Cmd_Return_Byte ( uint8_t Data )
{
if ( IKBD_ExeMode ) /* If IKBD is executing custom code, don't add */
return; /* anything to the buffer that comes from an IKBD's command */
IKBD_Send_Byte_Delay ( Data , 0 );
}
/**
* Same as IKBD_Cmd_Return_Byte, but with a delay before transmitting
* the byte.
*/
static void IKBD_Cmd_Return_Byte_Delay ( uint8_t Data , int Delay_Cycles )
{
if ( IKBD_ExeMode ) /* If IKBD is executing custom code, don't add */
return; /* anything to the buffer that comes from an IKBD's command */
IKBD_Send_Byte_Delay ( Data , Delay_Cycles );
}
/*-----------------------------------------------------------------------*/
/**
* Send bytes from the IKBD to the ACIA. We store the bytes in a buffer
* and we pull a new byte each time TDR needs to be re-filled.
*
* A possible delay can be specified to simulate the fact that some IKBD's
* commands don't return immediately the first byte. This delay is given
* in 68000 cycles at 8 MHz and should be converted to a number of bits
* at the chosen baud rate.
*/
static void IKBD_Send_Byte_Delay ( uint8_t Data , int Delay_Cycles )
{
//fprintf ( stderr , "send byte=0x%02x delay=%d\n" , Data , Delay_Cycles );
/* Is keyboard initialised yet ? Ignore any bytes until it is */
if ( bDuringResetCriticalTime )
{
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd is resetting, can't send byte=0x%02x VBL=%d HBL=%d\n" , Data, nVBLs , nHBL );
return;
}
/* Is ACIA's serial line initialised yet ? Ignore any bytes until it is */
if ( pACIA_IKBD->Clock_Divider == 0 )
{
LOG_TRACE ( TRACE_IKBD_ACIA, "ikbd acia not initialized, can't send byte=0x%02x VBL=%d HBL=%d\n" , Data, nVBLs , nHBL );
return;
}
if ( Delay_Cycles > 0 )
pIKBD->SCI_TX_Delay = Delay_Cycles / 1024; /* 1 bit at 7812.5 baud = 1024 cpu cycles at 8 MHz */
/* Check we have space to add one byte */
if ( IKBD_OutputBuffer_CheckFreeCount ( 1 ) )
{
/* Add byte to our buffer */
Keyboard.Buffer[Keyboard.BufferTail++] = Data;
Keyboard.BufferTail &= KEYBOARD_BUFFER_MASK;
Keyboard.NbBytesInOutputBuffer++;
}
else
{
Log_Printf(LOG_ERROR, "IKBD buffer is full, can't send 0x%02x!\n" , Data );
}
}
/************************************************************************/
/* End of the Serial Communication Interface */
/************************************************************************/
/**
* Check that the value is a correctly encoded BCD number
*/
static bool IKBD_BCD_Check ( uint8_t val )
{
if ( ( ( val & 0x0f ) > 0x09 )
|| ( ( val & 0xf0 ) > 0x90 ) )
return false;
return true;
}
/**
* After adding an integer number to a BCD number, the result is no more
* in BCD format. This function adjusts the value to be a valid BCD number again.
* In the HD6301, this is done using the 'DAA' instruction (Decimal Adjust)
* to "propagate" values 10-15 to the next 4 bits and keep each nibble
* in the 0-9 range.
*/
static uint8_t IKBD_BCD_Adjust ( uint8_t val )
{
if ( ( val & 0x0f ) > 0x09 ) /* low nibble no more in BCD */
val += 0x06; /* clear bit 4 and add 1 to high nibble */
if ( ( val & 0xf0 ) > 0x90 ) /* high nibble no more in BCD */
val += 0x60; /* propagate carry (but bits>7 will be lost) */
return val;
}
/**
* Update the IKBD's internal clock.
*
* This function is called on every VBL and we add the number of microseconds
* per VBL. When we reach 1000000 microseconds (1 sec), we update the Clock[]
* array by incrementing the 'second' byte.
*
* This code uses the same logic as the ROM version in the IKBD,
* don't try to optimise/rewrite it in a different way, as the TOS
* expects data to be handled this way.
* This works directly with BCD numbers and propagates the increment
* to the next byte each time the current byte reaches its maximum
* value.
* - when SetClock is used, the IKBD doesn't check the range of each byte,
* just that it's BCD encoded. So it's possible to set month/day/... to
* invalid values beyond the maximum allowed. These values will not correctly
* propagate to the next byte until they reach 0x99 and start again at 0x00.
* - check leap year for the number of days in february if ( year & 3 == 0 )
* - there's no explicit max for year : if year is 99 and increments,
* next year will be 00 (due to the BCD overflow)
* (used in the game 'Captain Blood' which sets clock to "99 12 31 00 00 00"
* and ends the game when clock reaches "00 01 01 00 00 00")
*/
void IKBD_UpdateClockOnVBL ( void )
{
int64_t FrameDuration_micro;
int i;
uint8_t val;
uint8_t max;
uint8_t year;
uint8_t month;
/* Max value for year/month/day/hour/minute/second */
uint8_t val_max[ 6 ] = { 0xFF , 0x13 , 0x00 , 0x24 , 0x60 , 0x60 };
/* Max number of days per month ; 18 entries, because the index for this array is a BCD coded month */
uint8_t day_max[ 18 ] = { 0x32, 0x29, 0x32, 0x31, 0x32, 0x31, 0x32, 0x32, 0x31, 0,0,0,0,0,0, 0x32, 0x31, 0x32 };
/* Check if more than 1 second passed since last increment of date/time */
FrameDuration_micro = ClocksTimings_GetVBLDuration_micro ( ConfigureParams.System.nMachineType , nScreenRefreshRate );
pIKBD->Clock_micro += FrameDuration_micro;
if ( pIKBD->Clock_micro < 1000000 )
return; /* Less than 1 second, don't increment date/time yet */
pIKBD->Clock_micro -= 1000000;
/* 1 second passed, we can increment the clock data */
// LOG_TRACE(TRACE_IKBD_CMDS,
// "IKBD_UpdateClock: %02x %02x %02x %02x %02x %02x -> ", pIKBD->Clock[ 0 ] ,pIKBD->Clock[ 1 ] , pIKBD->Clock[ 2 ] ,
// pIKBD->Clock[ 3 ] , pIKBD->Clock[ 4 ] , pIKBD->Clock[ 5 ] );
for ( i=5 ; i>=0 ; i-- )
{
val = pIKBD->Clock[ i ] + 1; /* Increment current value */
val = IKBD_BCD_Adjust ( val ); /* Convert to BCD */
if ( i != 2 )
max = val_max[ i ];
else /* Special case for days per month */
{
/* WARNING : it's possible to set the IKBD with month > 0x12, but in that case */
/* we would access day_max[] out of range. So, if month > 0x12, we limit to 31 days */
/* (this test is not done in the IKBD, but results would not be correct anyway) */
month = pIKBD->Clock[ 1 ];
if ( month > 0x12 ) /* Hatari specific, check range */
month = 0x12;
max = day_max[ month - 1 ]; /* Number of days for current month */
if ( pIKBD->Clock[ 1 ] == 2 ) /* For february, check leap year */
{
year = pIKBD->Clock[ 0 ];
/* Leap year test comes from the IKBD's ROM */
if ( year & 0x10 )
year += 0x0a;
if ( ( year & 0x03 ) == 0 )
max = 0x30; /* This is a leap year, 29 days */
}
}
if ( val != max )
{
pIKBD->Clock[ i ] = val; /* Max not reached, stop here */
break;
}
else if ( ( i == 1 ) || ( i == 2 ) )
pIKBD->Clock[ i ] = 1; /* day/month start at 1 */
else
pIKBD->Clock[ i ] = 0; /* hour/minute/second start at 0 */
}
// LOG_TRACE(TRACE_IKBD_CMDS,
// "%02x %02x %02x %02x %02x %02x\n", pIKBD->Clock[ 0 ] ,pIKBD->Clock[ 1 ] , pIKBD->Clock[ 2 ] ,
// pIKBD->Clock[ 3 ] , pIKBD->Clock[ 4 ] , pIKBD->Clock[ 5 ] );
}
/*-----------------------------------------------------------------------*/
/**
* Calculate out 'delta' that mouse has moved by each frame, and add this to our internal keyboard position
*/
static void IKBD_UpdateInternalMousePosition(void)
{
KeyboardProcessor.Mouse.DeltaX = KeyboardProcessor.Mouse.dx;
KeyboardProcessor.Mouse.DeltaY = KeyboardProcessor.Mouse.dy;
KeyboardProcessor.Mouse.dx = 0;
KeyboardProcessor.Mouse.dy = 0;
/* Update internal mouse coords - Y axis moves according to YAxis setting(up/down) */
/* Limit to Max X/Y(inclusive) */
if ( KeyboardProcessor.Mouse.XScale > 1 )
KeyboardProcessor.Abs.X += KeyboardProcessor.Mouse.DeltaX * KeyboardProcessor.Mouse.XScale;
else
KeyboardProcessor.Abs.X += KeyboardProcessor.Mouse.DeltaX;
if (KeyboardProcessor.Abs.X < 0)
KeyboardProcessor.Abs.X = 0;
if (KeyboardProcessor.Abs.X > KeyboardProcessor.Abs.MaxX)
KeyboardProcessor.Abs.X = KeyboardProcessor.Abs.MaxX;
if ( KeyboardProcessor.Mouse.YScale > 1 )
KeyboardProcessor.Abs.Y += KeyboardProcessor.Mouse.DeltaY*KeyboardProcessor.Mouse.YAxis * KeyboardProcessor.Mouse.YScale;
else
KeyboardProcessor.Abs.Y += KeyboardProcessor.Mouse.DeltaY*KeyboardProcessor.Mouse.YAxis;
if (KeyboardProcessor.Abs.Y < 0)
KeyboardProcessor.Abs.Y = 0;
if (KeyboardProcessor.Abs.Y > KeyboardProcessor.Abs.MaxY)
KeyboardProcessor.Abs.Y = KeyboardProcessor.Abs.MaxY;
}
/*-----------------------------------------------------------------------*/
/**
* When running in maximum speed the emulation will not see 'double-clicks'
* of the mouse as it is running so fast. In this case, we check for a
* double-click and pass the 'up'/'down' messages in emulation time to
* simulate the double-click effect!
*/
static void IKBD_CheckForDoubleClicks(void)
{
/*
Things get a little complicated when running max speed as a normal
double-click is a load of 1's, followed by 0's, 1's and 0's - But the
ST does not see this as a double click as the space in 'ST' time
between changes is so great.
Now, when we see a real double-click in max speed we actually send
the down/up/down/up in ST time. To get this correct (and not send
three clicks) we look in a history buffer and start at an index which
gives the correct number of clicks! Phew!
*/
/* Handle double clicks!!! */
if (Keyboard.LButtonDblClk)
{
if (Keyboard.LButtonDblClk == 1) /* First pressed! */
{
if ((Keyboard.LButtonHistory&0x3f) == 0) /* If not pressed button in long time do full dbl-click pattern */
Keyboard.LButtonDblClk = 1;
else
{
Keyboard.LButtonDblClk = 4; /* Otherwise, check where to begin to give 1111000011110000 pattern */
if ((Keyboard.LButtonHistory&0x7) == 0)
Keyboard.LButtonDblClk = 8;
else if ((Keyboard.LButtonHistory&0x3) == 0)
Keyboard.LButtonDblClk = 7;
else if ((Keyboard.LButtonHistory&0x1) == 0)
Keyboard.LButtonDblClk = 6;
}
}
Keyboard.bLButtonDown = DoubleClickPattern[Keyboard.LButtonDblClk];
Keyboard.LButtonDblClk++;
if (Keyboard.LButtonDblClk >= ARRAY_SIZE(DoubleClickPattern))
{
Keyboard.LButtonDblClk = 0;
Keyboard.bLButtonDown = false;
}
}
if (Keyboard.RButtonDblClk)
{
if (Keyboard.RButtonDblClk == 1) /* First pressed! */
{
if ((Keyboard.RButtonHistory&0x3f) == 0) /* If not pressed button in long time do full dbl-click pattern */
Keyboard.RButtonDblClk = 1;
else
{
Keyboard.RButtonDblClk = 4; /* Otherwise, check where to begin to give 1111000011110000 pattern */
if ((Keyboard.RButtonHistory&0x7) == 0)
Keyboard.RButtonDblClk = 8;
else if ((Keyboard.RButtonHistory&0x3) == 0)
Keyboard.RButtonDblClk = 7;
else if ((Keyboard.RButtonHistory&0x1) == 0)
Keyboard.RButtonDblClk = 6;
}
}
Keyboard.bRButtonDown = DoubleClickPattern[Keyboard.RButtonDblClk];
Keyboard.RButtonDblClk++;
if (Keyboard.RButtonDblClk >= ARRAY_SIZE(DoubleClickPattern))
{
Keyboard.RButtonDblClk = 0;
Keyboard.bRButtonDown = false;
}
}
/* Store presses into history */
Keyboard.LButtonHistory = (Keyboard.LButtonHistory<<1);
if (Keyboard.bLButtonDown)
Keyboard.LButtonHistory |= 0x1;
Keyboard.RButtonHistory = (Keyboard.RButtonHistory<<1);
if (Keyboard.bRButtonDown)
Keyboard.RButtonHistory |= 0x1;
}
/*-----------------------------------------------------------------------*/
/**
* Convert button to bool value
*/
static bool IKBD_ButtonBool(int Button)
{
/* Button pressed? */
if (Button)
return true;
return false;
}
/*-----------------------------------------------------------------------*/
/**
* Return true if buttons match, use this as buttons are a mask and not boolean
*/
static bool IKBD_ButtonsEqual(int Button1,int Button2)
{
/* Return bool compare */
return (IKBD_ButtonBool(Button1) == IKBD_ButtonBool(Button2));
}
/*-----------------------------------------------------------------------*/
/**
* According to if the mouse is enabled or not the joystick 1 fire
* button/right mouse button will become the same button. That means
* pressing one will also press the other and vice-versa.
* If both mouse and joystick are enabled, report it as a mouse button
* (needed by the game Big Run for example).
*/
static void IKBD_DuplicateMouseFireButtons(void)
{
/* If mouse is off then joystick fire button goes to joystick */
if (KeyboardProcessor.MouseMode == AUTOMODE_OFF)
{
/* If pressed right mouse button, should go to joystick 1 */
if (Keyboard.bRButtonDown&BUTTON_MOUSE)
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1] |= ATARIJOY_BITMASK_FIRE;
/* And left mouse button, should go to joystick 0 */
if (Keyboard.bLButtonDown&BUTTON_MOUSE)
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0] |= ATARIJOY_BITMASK_FIRE;
}
/* If mouse is on, joystick 1 fire button goes to the mouse instead */
else
{
/* Is fire button pressed? */
if (KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1]&ATARIJOY_BITMASK_FIRE)
{
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1] &= ~ATARIJOY_BITMASK_FIRE; /* Clear fire button bit */
Keyboard.bRButtonDown |= BUTTON_JOYSTICK; /* Mimic right mouse button */
}
else
Keyboard.bRButtonDown &= ~BUTTON_JOYSTICK;
}
}
/*-----------------------------------------------------------------------*/
/**
* Send 'relative' mouse position
* In case DeltaX or DeltaY are more than 127 units, we send the position
* using several packets (with a while loop)
*/
static void IKBD_SendRelMousePacket(void)
{
int ByteRelX,ByteRelY;
uint8_t Header;
while ( true )
{
ByteRelX = KeyboardProcessor.Mouse.DeltaX;
if ( ByteRelX > 127 ) ByteRelX = 127;
if ( ByteRelX < -128 ) ByteRelX = -128;
ByteRelY = KeyboardProcessor.Mouse.DeltaY;
if ( ByteRelY > 127 ) ByteRelY = 127;
if ( ByteRelY < -128 ) ByteRelY = -128;
if ( ( ( ByteRelX < 0 ) && ( ByteRelX <= -KeyboardProcessor.Mouse.XThreshold ) )
|| ( ( ByteRelX > 0 ) && ( ByteRelX >= KeyboardProcessor.Mouse.XThreshold ) )
|| ( ( ByteRelY < 0 ) && ( ByteRelY <= -KeyboardProcessor.Mouse.YThreshold ) )
|| ( ( ByteRelY > 0 ) && ( ByteRelY >= KeyboardProcessor.Mouse.YThreshold ) )
|| ( !IKBD_ButtonsEqual(Keyboard.bOldLButtonDown,Keyboard.bLButtonDown ) )
|| ( !IKBD_ButtonsEqual(Keyboard.bOldRButtonDown,Keyboard.bRButtonDown ) ) )
{
Header = 0xf8;
if (Keyboard.bLButtonDown)
Header |= 0x02;
if (Keyboard.bRButtonDown)
Header |= 0x01;
if ( IKBD_OutputBuffer_CheckFreeCount ( 3 ) )
{
IKBD_Cmd_Return_Byte (Header);
IKBD_Cmd_Return_Byte (ByteRelX);
IKBD_Cmd_Return_Byte (ByteRelY*KeyboardProcessor.Mouse.YAxis);
}
KeyboardProcessor.Mouse.DeltaX -= ByteRelX;
KeyboardProcessor.Mouse.DeltaY -= ByteRelY;
/* Store buttons for next time around */
Keyboard.bOldLButtonDown = Keyboard.bLButtonDown;
Keyboard.bOldRButtonDown = Keyboard.bRButtonDown;
}
else
break; /* exit the while loop */
}
}
/**
* Get joystick data
*/
static void IKBD_GetJoystickData(void)
{
/* Joystick 1 */
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1] = Joy_GetStickData(JOYID_JOYSTICK1);
/* If mouse is on, joystick 0 is not connected */
if (KeyboardProcessor.MouseMode==AUTOMODE_OFF
|| (bBothMouseAndJoy && KeyboardProcessor.MouseMode==AUTOMODE_MOUSEREL))
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0] = Joy_GetStickData(JOYID_JOYSTICK0);
else
KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0] = 0x00;
}
/*-----------------------------------------------------------------------*/
/**
* Send 'joysticks' bit masks
*/
static void IKBD_SendAutoJoysticks(void)
{
uint8_t JoyData;
/* Did joystick 0/mouse change? */
JoyData = KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0];
if (JoyData!=KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK0])
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (0xFE); /* Joystick 0 / Mouse */
IKBD_Cmd_Return_Byte (JoyData);
}
KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK0] = JoyData;
}
/* Did joystick 1(default) change? */
JoyData = KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1];
if (JoyData!=KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK1])
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (0xFF); /* Joystick 1 */
IKBD_Cmd_Return_Byte (JoyData);
}
KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK1] = JoyData;
}
}
/*-----------------------------------------------------------------------*/
/**
* Send 'joysticks' bit masks when in monitoring mode
* %000000xy ; where y is JOYSTICK1 Fire button
* ; and x is JOYSTICK0 Fire button
* %nnnnmmmm ; where m is JOYSTICK1 state
* ; and n is JOYSTICK0 state
*/
static void IKBD_SendAutoJoysticksMonitoring(void)
{
uint8_t Byte1;
uint8_t Byte2;
Byte1 = ( ( KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0] & ATARIJOY_BITMASK_FIRE ) >> 6 )
| ( ( KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1] & ATARIJOY_BITMASK_FIRE ) >> 7 );
Byte2 = ( ( KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK0] & 0x0f ) << 4 )
| ( KeyboardProcessor.Joy.JoyData[JOYID_JOYSTICK1] & 0x0f );
IKBD_Cmd_Return_Byte (Byte1);
IKBD_Cmd_Return_Byte (Byte2);
//fprintf ( stderr , "joystick monitoring %x %x VBL=%d HBL=%d\n" , Byte1 , Byte2 , nVBLs , nHBL );
}
/*-----------------------------------------------------------------------*/
/**
* Send packets which are generated from the mouse action settings
* If relative mode is on, still generate these packets
*/
static void IKBD_SendOnMouseAction(void)
{
bool bReportPosition = false;
/* Report buttons as keys? Do in relative/absolute mode */
if (KeyboardProcessor.Mouse.Action&0x4)
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
/* Left button? */
if ( (IKBD_ButtonBool(Keyboard.bLButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldLButtonDown))) )
IKBD_Cmd_Return_Byte (0x74); /* Left */
else if ( (IKBD_ButtonBool(Keyboard.bOldLButtonDown) && (!IKBD_ButtonBool(Keyboard.bLButtonDown))) )
IKBD_Cmd_Return_Byte (0x74|0x80);
/* Right button? */
if ( (IKBD_ButtonBool(Keyboard.bRButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldRButtonDown))) )
IKBD_Cmd_Return_Byte (0x75); /* Right */
else if ( (IKBD_ButtonBool(Keyboard.bOldRButtonDown) && (!IKBD_ButtonBool(Keyboard.bRButtonDown))) )
IKBD_Cmd_Return_Byte (0x75|0x80);
}
/* Ignore bottom two bits, so return now */
return;
}
/* Check MouseAction - report position on press/release */
/* MUST do this before update relative positions as buttons get reset */
if (KeyboardProcessor.Mouse.Action&0x3)
{
/* Check for 'press'? */
if (KeyboardProcessor.Mouse.Action&0x1)
{
/* Did 'press' mouse buttons? */
if ( (IKBD_ButtonBool(Keyboard.bLButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldLButtonDown))) )
{
bReportPosition = true;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons &= ~0x04;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons |= 0x02;
}
if ( (IKBD_ButtonBool(Keyboard.bRButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldRButtonDown))) )
{
bReportPosition = true;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons &= ~0x01;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons |= 0x08;
}
}
/* Check for 'release'? */
if (KeyboardProcessor.Mouse.Action&0x2)
{
/* Did 'release' mouse buttons? */
if ( (IKBD_ButtonBool(Keyboard.bOldLButtonDown) && (!IKBD_ButtonBool(Keyboard.bLButtonDown))) )
{
bReportPosition = true;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons &= ~0x08;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons |= 0x01;
}
if ( (IKBD_ButtonBool(Keyboard.bOldRButtonDown) && (!IKBD_ButtonBool(Keyboard.bRButtonDown))) )
{
bReportPosition = true;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons &= ~0x02;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons |= 0x04;
}
}
/* Do need to report? */
if (bReportPosition)
{
/* Only report if mouse in absolute mode */
if (KeyboardProcessor.MouseMode==AUTOMODE_MOUSEABS)
{
LOG_TRACE(TRACE_IKBD_CMDS, "Report ABS on MouseAction\n");
IKBD_Cmd_ReadAbsMousePos();
}
}
}
}
/*-----------------------------------------------------------------------*/
/**
* Send mouse movements as cursor keys
*/
static void IKBD_SendCursorMousePacket(void)
{
int i=0;
/* Run each 'Delta' as cursor presses */
/* Limit to '10' loops as host mouse cursor might have a VERY poor quality. */
/* Eg, a single mouse movement on and ST gives delta's of '1', mostly, */
/* but host mouse might go as high as 20+! */
//fprintf ( stderr , "key %d %d %d %d %d %d\n" , KeyboardProcessor.Mouse.DeltaX , KeyboardProcessor.Mouse.DeltaY, Keyboard.bOldLButtonDown,Keyboard.bLButtonDown, Keyboard.bOldRButtonDown,Keyboard.bRButtonDown );
while ( (i<10) && ((KeyboardProcessor.Mouse.DeltaX!=0) || (KeyboardProcessor.Mouse.DeltaY!=0)
|| (!IKBD_ButtonsEqual(Keyboard.bOldLButtonDown,Keyboard.bLButtonDown)) || (!IKBD_ButtonsEqual(Keyboard.bOldRButtonDown,Keyboard.bRButtonDown))) )
{
if ( KeyboardProcessor.Mouse.DeltaX != 0 )
{
/* Left? */
if (KeyboardProcessor.Mouse.DeltaX <= -KeyboardProcessor.Mouse.KeyCodeDeltaX)
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (75); /* Left cursor */
IKBD_Cmd_Return_Byte (75|0x80);
}
KeyboardProcessor.Mouse.DeltaX += KeyboardProcessor.Mouse.KeyCodeDeltaX;
}
/* Right? */
if (KeyboardProcessor.Mouse.DeltaX >= KeyboardProcessor.Mouse.KeyCodeDeltaX)
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (77); /* Right cursor */
IKBD_Cmd_Return_Byte (77|0x80);
}
KeyboardProcessor.Mouse.DeltaX -= KeyboardProcessor.Mouse.KeyCodeDeltaX;
}
}
if ( KeyboardProcessor.Mouse.DeltaY != 0 )
{
/* Up? */
if (KeyboardProcessor.Mouse.DeltaY <= -KeyboardProcessor.Mouse.KeyCodeDeltaY)
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (72); /* Up cursor */
IKBD_Cmd_Return_Byte (72|0x80);
}
KeyboardProcessor.Mouse.DeltaY += KeyboardProcessor.Mouse.KeyCodeDeltaY;
}
/* Down? */
if (KeyboardProcessor.Mouse.DeltaY >= KeyboardProcessor.Mouse.KeyCodeDeltaY)
{
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
IKBD_Cmd_Return_Byte (80); /* Down cursor */
IKBD_Cmd_Return_Byte (80|0x80);
}
KeyboardProcessor.Mouse.DeltaY -= KeyboardProcessor.Mouse.KeyCodeDeltaY;
}
}
if ( IKBD_OutputBuffer_CheckFreeCount ( 2 ) )
{
/* Left button? */
if ( (IKBD_ButtonBool(Keyboard.bLButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldLButtonDown))) )
IKBD_Cmd_Return_Byte (0x74); /* Left */
else if ( (IKBD_ButtonBool(Keyboard.bOldLButtonDown) && (!IKBD_ButtonBool(Keyboard.bLButtonDown))) )
IKBD_Cmd_Return_Byte (0x74|0x80);
/* Right button? */
if ( (IKBD_ButtonBool(Keyboard.bRButtonDown) && (!IKBD_ButtonBool(Keyboard.bOldRButtonDown))) )
IKBD_Cmd_Return_Byte (0x75); /* Right */
else if ( (IKBD_ButtonBool(Keyboard.bOldRButtonDown) && (!IKBD_ButtonBool(Keyboard.bRButtonDown))) )
IKBD_Cmd_Return_Byte (0x75|0x80);
}
Keyboard.bOldLButtonDown = Keyboard.bLButtonDown;
Keyboard.bOldRButtonDown = Keyboard.bRButtonDown;
/* Count, so exit if try too many times! */
i++;
}
}
/*-----------------------------------------------------------------------*/
/**
* Return packets from keyboard for auto, rel mouse, joystick etc...
*/
static void IKBD_SendAutoKeyboardCommands(void)
{
/* Don't do anything until processor is first reset */
if ( bDuringResetCriticalTime )
return;
/* Read joysticks for this frame */
IKBD_GetJoystickData();
/* Check for double-clicks in maximum speed mode */
IKBD_CheckForDoubleClicks();
/* Handle Joystick/Mouse fire buttons */
IKBD_DuplicateMouseFireButtons();
/* Send any packets which are to be reported by mouse action */
IKBD_SendOnMouseAction();
/* Update internal mouse absolute position by find 'delta' of mouse movement */
IKBD_UpdateInternalMousePosition();
/* If IKBD is monitoring only joysticks, don't report other events */
if ( KeyboardProcessor.JoystickMode == AUTOMODE_JOYSTICK_MONITORING )
{
IKBD_SendAutoJoysticksMonitoring();
return;
}
/* Send automatic joystick packets */
if (KeyboardProcessor.JoystickMode==AUTOMODE_JOYSTICK)
IKBD_SendAutoJoysticks();
/* Send automatic relative mouse positions(absolute are not send automatically) */
if (KeyboardProcessor.MouseMode==AUTOMODE_MOUSEREL)
IKBD_SendRelMousePacket();
/* Send cursor key directions for movements */
else if (KeyboardProcessor.MouseMode==AUTOMODE_MOUSECURSOR)
IKBD_SendCursorMousePacket();
/* Store buttons for next time around */
Keyboard.bOldLButtonDown = Keyboard.bLButtonDown;
Keyboard.bOldRButtonDown = Keyboard.bRButtonDown;
/* Send joystick button '2' as 'Space bar' key - MUST do here so does not get mixed up in middle of joystick packets! */
if (JoystickSpaceBar==JOYSTICK_SPACE_DOWN)
{
IKBD_PressSTKey(57, true); /* Press */
JoystickSpaceBar = JOYSTICK_SPACE_DOWNED; /* Pressed */
}
else if (JoystickSpaceBar==JOYSTICK_SPACE_UP)
{
IKBD_PressSTKey(57, false); /* Release */
JoystickSpaceBar = JOYSTICK_SPACE_NULL; /* Complete */
}
/* If we're executing a custom IKBD program, call it to process the key/mouse/joystick event */
if ( IKBD_ExeMode && pIKBD_CustomCodeHandler_Read )
(*pIKBD_CustomCodeHandler_Read) ();
}
/*-----------------------------------------------------------------------*/
/**
* When press/release key under host OS, execute this function.
*/
void IKBD_PressSTKey(uint8_t ScanCode, bool bPress)
{
/* If IKBD is monitoring only joysticks, don't report key */
if ( KeyboardProcessor.JoystickMode == AUTOMODE_JOYSTICK_MONITORING )
return;
/* Store the state of each ST scancode : 1=pressed 0=released */
if ( bPress ) ScanCodeState[ ScanCode & 0x7f ] = 1;
else ScanCodeState[ ScanCode & 0x7f ] = 0;
if (!bPress)
ScanCode |= 0x80; /* Set top bit if released key */
if ( IKBD_OutputBuffer_CheckFreeCount ( 1 ) )
{
IKBD_Cmd_Return_Byte (ScanCode); /* Add to the IKBD's output buffer */
}
/* If we're executing a custom IKBD program, call it to process the key event */
if ( IKBD_ExeMode && pIKBD_CustomCodeHandler_Read )
(*pIKBD_CustomCodeHandler_Read) ();
}
/*-----------------------------------------------------------------------*/
/**
* Check if a key is pressed in the ScanCodeState array
* Return the scancode >= 0 for the first key we find, else return -1
* if no key is pressed
*/
static int IKBD_CheckPressedKey(void)
{
unsigned int i;
for (i=0 ; i<sizeof(ScanCodeState) ; i++ )
if ( ScanCodeState[ i ] )
return i;
return -1;
}
/*-----------------------------------------------------------------------*/
/**
* This function is called regularly to automatically send keyboard, mouse
* and joystick updates.
*/
void IKBD_InterruptHandler_AutoSend(void)
{
/* Handle user events and other messages, (like quit message) */
Main_EventHandler();
/* Remove this interrupt from list and re-order.
* (needs to be done after UI event handling so
* that snapshots saved from UI and restored from
* command line don't miss the AUTOSEND interrupt)
*/
CycInt_AcknowledgeInterrupt();
/* Did user try to quit? */
if (bQuitProgram)
{
/* Assure that CPU core shuts down */
M68000_SetSpecial(SPCFLAG_BRK);
return;
}
/* Trigger this auto-update function again after a while */
CycInt_AddRelativeInterrupt(Keyboard.AutoSendCycles, INT_CPU8_CYCLE, INTERRUPT_IKBD_AUTOSEND);
/* We don't send keyboard data automatically within the first few
* VBLs to avoid that TOS gets confused during its boot time */
if (nVBLs > 20)
{
/* Send automatic keyboard packets for mouse, joysticks etc... */
IKBD_SendAutoKeyboardCommands();
}
}
/*-----------------------------------------------------------------------*/
/**
* On ST if disable Mouse AND Joystick with a set time of a RESET command they are
* actually turned back on! (A number of games do this so can get mouse and joystick
* packets at the same time)
*/
static void IKBD_CheckResetDisableBug(void)
{
/* Have disabled BOTH mouse and joystick? */
if (bMouseDisabled && bJoystickDisabled)
{
/* And in critical time? */
if (bDuringResetCriticalTime)
{
/* Emulate relative mouse and joystick reports being turned back on */
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEREL;
KeyboardProcessor.JoystickMode = AUTOMODE_JOYSTICK;
bBothMouseAndJoy = true;
LOG_TRACE(TRACE_IKBD_ALL, "ikbd cancel commands 0x12 and 0x1a received during reset,"
" enabling joystick and mouse reporting at the same time\n" );
}
}
}
/*-----------------------------------------------------------------------*/
/**
* When a byte is received by the IKBD, it is added to a small 8 byte buffer.
* - If the first byte is a valid command, we wait for additional bytes if needed
* and then we execute the command's handler.
* - If the first byte is not a valid command or after a successful command, we
* empty the input buffer (extra bytes, if any, are lost)
* - If the input buffer is full when a new byte is received, the new byte is lost.
* - In case the first byte read is not a valid command then IKBD does nothing
* (it doesn't return any byte to indicate the command was not recognized)
*/
static void IKBD_RunKeyboardCommand(uint8_t aciabyte)
{
int i=0;
/* Write into our keyboard input buffer if it's not full yet */
if ( Keyboard.nBytesInInputBuffer < SIZE_KEYBOARDINPUT_BUFFER )
Keyboard.InputBuffer[Keyboard.nBytesInInputBuffer++] = aciabyte;
/* Now check bytes to see if we have a valid/in-valid command string set */
while (KeyboardCommands[i].Command!=0xff)
{
/* Found command? */
if (KeyboardCommands[i].Command==Keyboard.InputBuffer[0])
{
/* If the command is complete (with its possible parameters) we can execute it */
/* Else, we wait for the next bytes until the command is complete */
if (KeyboardCommands[i].NumParameters==Keyboard.nBytesInInputBuffer)
{
/* Any new valid command will unpause the output (if command 0x13 was used) */
Keyboard.PauseOutput = false;
CALL_VAR(KeyboardCommands[i].pCallFunction);
Keyboard.nBytesInInputBuffer = 0; /* Clear input buffer after processing a command */
}
return;
}
i++;
}
/* Command not known, reset buffer(IKBD assumes a NOP) */
Keyboard.nBytesInInputBuffer = 0;
}
/************************************************************************/
/* List of keyboard commands handled by the standard IKBD's ROM. */
/* Each IKBD's command is emulated to get the same result as if we were */
/* running a full HD6301 emulation. */
/************************************************************************/
/*-----------------------------------------------------------------------*/
/**
* RESET
*
* 0x80
* 0x01
*
* Performs self test and checks for stuck (closed) keys, if OK returns
* IKBD_ROM_VERSION (0xF1). Otherwise returns break codes for keys (not emulated).
*/
static void IKBD_Cmd_Reset(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_Reset VBLs=%i framecyc=%i\n",
nVBLs, Video_GetCyclesSinceVbl());
/* Check that 0x01 was received after 0x80 */
if (Keyboard.InputBuffer[1] == 0x01)
{
IKBD_Boot_ROM ( false );
}
/* else if not 0x80,0x01 just ignore */
}
/*-----------------------------------------------------------------------*/
/**
* SET MOUSE BUTTON ACTION
*
* 0x07
* %00000mss ; mouse button action
* ; (m is presumed =1 when in MOUSE KEYCODE mode)
* ; mss=0xy, mouse button press or release causes mouse
* ; position report
* ; where y=1, mouse key press causes absolute report
* ; and x=1, mouse key release causes absolute report
* ; mss=100, mouse buttons act like keys
*/
static void IKBD_Cmd_MouseAction(void)
{
KeyboardProcessor.Mouse.Action = Keyboard.InputBuffer[1];
KeyboardProcessor.Abs.PrevReadAbsMouseButtons = ABS_PREVBUTTONS;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_MouseAction %d\n",
(unsigned int)KeyboardProcessor.Mouse.Action);
}
/*-----------------------------------------------------------------------*/
/**
* SET RELATIVE MOUSE POSITION REPORTING
*
* 0x08
*/
static void IKBD_Cmd_RelMouseMode(void)
{
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEREL;
/* Some games (like Barbarian by Psygnosis) enable both, mouse and
* joystick directly after a reset. This causes the IKBD to send both
* type of packets. To emulate this feature, we've got to remember
* that the mouse has been enabled during reset. */
if (bDuringResetCriticalTime)
bMouseEnabledDuringReset = true;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_RelMouseMode\n");
}
/*-----------------------------------------------------------------------*/
/**
* SET ABSOLUTE MOUSE POSITIONING
*
* 0x09
* XMSB ;X maximum (in scaled mouse clicks)
* XLSB
* YMSB ;Y maximum (in scaled mouse clicks)
* YLSB
*/
static void IKBD_Cmd_AbsMouseMode(void)
{
/* These maximums are 'inclusive' */
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEABS;
KeyboardProcessor.Abs.MaxX = (((unsigned int)Keyboard.InputBuffer[1])<<8) | (unsigned int)Keyboard.InputBuffer[2];
KeyboardProcessor.Abs.MaxY = (((unsigned int)Keyboard.InputBuffer[3])<<8) | (unsigned int)Keyboard.InputBuffer[4];
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_AbsMouseMode %d,%d\n",
KeyboardProcessor.Abs.MaxX, KeyboardProcessor.Abs.MaxY);
}
/*-----------------------------------------------------------------------*/
/**
* SET MOUSE KEYCODE MODE
*
* 0x0A
* deltax ; distance in X clicks to return (LEFT) or (RIGHT)
* deltay ; distance in Y clicks to return (UP) or (DOWN)
*/
static void IKBD_Cmd_MouseCursorKeycodes(void)
{
KeyboardProcessor.MouseMode = AUTOMODE_MOUSECURSOR;
KeyboardProcessor.Mouse.KeyCodeDeltaX = Keyboard.InputBuffer[1];
KeyboardProcessor.Mouse.KeyCodeDeltaY = Keyboard.InputBuffer[2];
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_MouseCursorKeycodes %d,%d\n",
(int)KeyboardProcessor.Mouse.KeyCodeDeltaX,
(int)KeyboardProcessor.Mouse.KeyCodeDeltaY);
}
/*-----------------------------------------------------------------------*/
/**
* SET MOUSE THRESHOLD
*
* 0x0B
* X ; x threshold in mouse ticks (positive integers)
* Y ; y threshold in mouse ticks (positive integers)
*/
static void IKBD_Cmd_SetMouseThreshold(void)
{
KeyboardProcessor.Mouse.XThreshold = (unsigned int)Keyboard.InputBuffer[1];
KeyboardProcessor.Mouse.YThreshold = (unsigned int)Keyboard.InputBuffer[2];
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetMouseThreshold %d,%d\n",
KeyboardProcessor.Mouse.XThreshold, KeyboardProcessor.Mouse.YThreshold);
}
/*-----------------------------------------------------------------------*/
/**
* SET MOUSE SCALE
*
* 0x0C
* X ; horizontal mouse ticks per internal X
* Y ; vertical mouse ticks per internal Y
*/
static void IKBD_Cmd_SetMouseScale(void)
{
KeyboardProcessor.Mouse.XScale = (unsigned int)Keyboard.InputBuffer[1];
KeyboardProcessor.Mouse.YScale = (unsigned int)Keyboard.InputBuffer[2];
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetMouseScale %d,%d\n",
KeyboardProcessor.Mouse.XScale, KeyboardProcessor.Mouse.YScale);
}
/*-----------------------------------------------------------------------*/
/**
* INTERROGATE MOUSE POSITION
*
* 0x0D
* Returns: 0xF7 ; absolute mouse position header
* BUTTONS
* 0000dcba
* where a is right button down since last interrogation
* b is right button up since last
* c is left button down since last
* d is left button up since last
* XMSB ; X coordinate
* XLSB
* YMSB ; Y coordinate
* YLSB
*/
static void IKBD_Cmd_ReadAbsMousePos(void)
{
uint8_t Buttons,PrevButtons;
/* Test buttons */
Buttons = 0;
/* Set buttons to show if up/down */
if (Keyboard.bRButtonDown)
Buttons |= 0x01;
else
Buttons |= 0x02;
if (Keyboard.bLButtonDown)
Buttons |= 0x04;
else
Buttons |= 0x08;
/* Mask off it didn't send last time */
PrevButtons = KeyboardProcessor.Abs.PrevReadAbsMouseButtons;
KeyboardProcessor.Abs.PrevReadAbsMouseButtons = Buttons;
Buttons &= ~PrevButtons;
/* And send packet */
if ( IKBD_OutputBuffer_CheckFreeCount ( 6 ) )
{
IKBD_Cmd_Return_Byte_Delay (0xf7, 18000-ACIA_CYCLES);
IKBD_Cmd_Return_Byte (Buttons);
IKBD_Cmd_Return_Byte ((unsigned int)KeyboardProcessor.Abs.X>>8);
IKBD_Cmd_Return_Byte ((unsigned int)KeyboardProcessor.Abs.X&0xff);
IKBD_Cmd_Return_Byte ((unsigned int)KeyboardProcessor.Abs.Y>>8);
IKBD_Cmd_Return_Byte ((unsigned int)KeyboardProcessor.Abs.Y&0xff);
}
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReadAbsMousePos %d,%d 0x%X\n",
KeyboardProcessor.Abs.X, KeyboardProcessor.Abs.Y, Buttons);
}
/*-----------------------------------------------------------------------*/
/**
* LOAD MOUSE POSITION
*
* 0x0E
* 0x00 ; filler
* XMSB ; X coordinate
* XLSB ; (in scaled coordinate system)
* YMSB ; Y coordinate
* YLSB
*/
static void IKBD_Cmd_SetInternalMousePos(void)
{
/* Setting these do not clip internal position(this happens on next update) */
KeyboardProcessor.Abs.X = (((unsigned int)Keyboard.InputBuffer[2])<<8) | (unsigned int)Keyboard.InputBuffer[3];
KeyboardProcessor.Abs.Y = (((unsigned int)Keyboard.InputBuffer[4])<<8) | (unsigned int)Keyboard.InputBuffer[5];
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetInternalMousePos %d,%d\n",
KeyboardProcessor.Abs.X, KeyboardProcessor.Abs.Y);
}
/*-----------------------------------------------------------------------*/
/**
* SET Y=0 AT BOTTOM
*
* 0x0F
*/
static void IKBD_Cmd_SetYAxisDown(void)
{
KeyboardProcessor.Mouse.YAxis = -1;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetYAxisDown\n");
}
/*-----------------------------------------------------------------------*/
/**
* SET Y=0 AT TOP
*
* 0x10
*/
static void IKBD_Cmd_SetYAxisUp(void)
{
KeyboardProcessor.Mouse.YAxis = 1;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetYAxisUp\n");
}
/*-----------------------------------------------------------------------*/
/**
* RESUME
*
* Any command received by the IKBD will also resume the output if it was
* paused by command 0x13, so this command is redundant.
*
* 0x11
*/
static void IKBD_Cmd_StartKeyboardTransfer(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_StartKeyboardTransfer\n");
Keyboard.PauseOutput = false;
}
/*-----------------------------------------------------------------------*/
/**
* DISABLE MOUSE
*
* 0x12
*/
static void IKBD_Cmd_TurnMouseOff(void)
{
KeyboardProcessor.MouseMode = AUTOMODE_OFF;
bMouseDisabled = true;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_TurnMouseOff\n");
IKBD_CheckResetDisableBug();
}
/*-----------------------------------------------------------------------*/
/**
* PAUSE OUTPUT
*
* 0x13
*/
static void IKBD_Cmd_StopKeyboardTransfer(void)
{
if (bDuringResetCriticalTime)
{
/* Required for the loader of 'Just Bugging' by ACF */
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_StopKeyboardTransfer ignored during ikbd reset\n");
return;
}
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_StopKeyboardTransfer\n");
Keyboard.PauseOutput = true;
}
/*-----------------------------------------------------------------------*/
/**
* SET JOYSTICK EVENT REPORTING
*
* 0x14
*/
static void IKBD_Cmd_ReturnJoystickAuto(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReturnJoystickAuto\n");
KeyboardProcessor.JoystickMode = AUTOMODE_JOYSTICK;
KeyboardProcessor.MouseMode = AUTOMODE_OFF;
/* If mouse was also enabled within time of a reset (0x08 command) it isn't disabled now!
* (used by the game Barbarian 1 by Psygnosis for example) */
if ( bDuringResetCriticalTime && bMouseEnabledDuringReset )
{
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEREL;
bBothMouseAndJoy = true;
LOG_TRACE(TRACE_IKBD_ALL, "ikbd commands 0x08 and 0x14 received during reset,"
" enabling joystick and mouse reporting at the same time\n" );
}
/* If mouse was disabled during the reset (0x12 command) it is enabled again
* (used by the game Hammerfist for example) */
else if ( bDuringResetCriticalTime && bMouseDisabled )
{
KeyboardProcessor.MouseMode = AUTOMODE_MOUSEREL;
bBothMouseAndJoy = true;
LOG_TRACE(TRACE_IKBD_ALL, "ikbd commands 0x12 and 0x14 received during reset,"
" enabling joystick and mouse reporting at the same time\n" );
}
/* This command resets the internally previously stored joystick states */
KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK0] = KeyboardProcessor.Joy.PrevJoyData[JOYID_JOYSTICK1] = 0;
/* This is a hack for the STE Utopos (=> v1.50) and Falcon Double Bubble
* 2000 games. They expect the joystick data to be sent within a certain
* amount of time after this command, without checking the ACIA control
* register first.
*/
IKBD_GetJoystickData();
IKBD_SendAutoJoysticks();
}
/*-----------------------------------------------------------------------*/
/**
* SET JOYSTICK INTERROGATION MODE
*
* 0x15
*/
static void IKBD_Cmd_StopJoystick(void)
{
KeyboardProcessor.JoystickMode = AUTOMODE_OFF;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_StopJoystick\n");
}
/*-----------------------------------------------------------------------*/
/**
* JOYSTICK INTERROGATE
*
* 0x16
*/
static void IKBD_Cmd_ReturnJoystick(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReturnJoystick\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 3 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xFD , IKBD_Delay_Random ( 7500 , 10000 ) );
IKBD_Cmd_Return_Byte (Joy_GetStickData(JOYID_JOYSTICK0));
IKBD_Cmd_Return_Byte (Joy_GetStickData(JOYID_JOYSTICK1));
}
}
/*-----------------------------------------------------------------------*/
/**
* SET JOYSTICK MONITORING
*
* 0x17
* rate ; time between samples in hundredths of a second
* Returns: (in packets of two as long as in mode)
* %000000xy where y is JOYSTICK1 Fire button
* and x is JOYSTICK0 Fire button
* %nnnnmmmm where m is JOYSTICK1 state
* and n is JOYSTICK0 state
*
* TODO : we use a fixed 8 MHz clock to convert rate in 1/100th of sec into cycles.
* This should be replaced by using MachineClocks.CPU_Freq.
*/
static void IKBD_Cmd_SetJoystickMonitoring(void)
{
int Rate;
int Cycles;
Rate = (unsigned int)Keyboard.InputBuffer[1];
KeyboardProcessor.JoystickMode = AUTOMODE_JOYSTICK_MONITORING;
KeyboardProcessor.MouseMode = AUTOMODE_OFF;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetJoystickMonitoring %d\n" , Rate );
if ( Rate == 0 )
Rate = 1;
Cycles = 8021247 * Rate / 100;
CycInt_AddRelativeInterrupt ( Cycles, INT_CPU8_CYCLE, INTERRUPT_IKBD_AUTOSEND );
Keyboard.AutoSendCycles = Cycles;
}
/*-----------------------------------------------------------------------*/
/**
* SET FIRE BUTTON MONITORING
*
* 0x18
* Returns: (as long as in mode)
* %bbbbbbbb ; state of the JOYSTICK1 fire button packed
* ; 8 bits per byte, the first sample if the MSB
*/
static void IKBD_Cmd_SetJoystickFireDuration(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetJoystickFireDuration (not implemented)\n");
}
/*-----------------------------------------------------------------------*/
/**
* SET JOYSTICK KEYCODE MODE
*
* 0x19
* RX ; length of time (in tenths of seconds) until
* ; horizontal velocity breakpoint is reached
* RY ; length of time (in tenths of seconds) until
* ; vertical velocity breakpoint is reached
* TX ; length (in tenths of seconds) of joystick closure
* ; until horizontal cursor key is generated before RX
* ; has elapsed
* TY ; length (in tenths of seconds) of joystick closure
* ; until vertical cursor key is generated before RY
* ; has elapsed
* VX ; length (in tenths of seconds) of joystick closure
* ; until horizontal cursor keystrokes are generated after RX
* ; has elapsed
* VY ; length (in tenths of seconds) of joystick closure
* ; until vertical cursor keystrokes are generated after RY
* ; has elapsed
*/
static void IKBD_Cmd_SetCursorForJoystick(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_SetCursorForJoystick (not implemented)\n");
}
/*-----------------------------------------------------------------------*/
/**
* DISABLE JOYSTICKS
*
* 0x1A
*/
static void IKBD_Cmd_DisableJoysticks(void)
{
KeyboardProcessor.JoystickMode = AUTOMODE_OFF;
bJoystickDisabled = true;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_DisableJoysticks\n");
IKBD_CheckResetDisableBug();
}
/*-----------------------------------------------------------------------*/
/**
* TIME-OF-DAY CLOCK SET
*
* 0x1B
* YY ; year (2 least significant digits)
* MM ; month
* DD ; day
* hh ; hour
* mm ; minute
* ss ; second
*
* All bytes are stored in BCD format. If a byte is not in BCD, we ignore it
* but we process the rest of the bytes.
* Note that the IKBD doesn't check that month/day/hour/second/minute are in
* their correct range, just that they're BCD encoded (so you can store 0x30 in hour
* for example, see IKBD_UpdateClockOnVBL())
*/
static void IKBD_Cmd_SetClock(void)
{
int i;
uint8_t val;
LOG_TRACE(TRACE_IKBD_CMDS,
"IKBD_Cmd_SetClock: %02x %02x %02x %02x %02x %02x\n",
Keyboard.InputBuffer[1], Keyboard.InputBuffer[2],
Keyboard.InputBuffer[3], Keyboard.InputBuffer[4],
Keyboard.InputBuffer[5], Keyboard.InputBuffer[6]);
for ( i=1 ; i<=6 ; i++ )
{
val = Keyboard.InputBuffer[ i ];
if ( IKBD_BCD_Check ( val ) ) /* Check if valid BCD, else ignore */
pIKBD->Clock[ i-1 ] = val; /* Store new value */
}
}
/*-----------------------------------------------------------------------*/
/**
* INTERROGATE TIME-OF-DAY CLOCK
*
* 0x1C
* Returns:
* 0xFC ; time-of-day event header
* YY ; year (2 least significant digits)
* MM ; month
* DD ; day
* hh ; hour
* mm ; minute
* ss ; second
*
* All bytes are stored/returned in BCD format.
* Date/Time is updated in IKBD_UpdateClockOnVBL()
*/
static void IKBD_Cmd_ReadClock(void)
{
int i;
LOG_TRACE(TRACE_IKBD_CMDS,
"IKBD_Cmd_ReadClock: %02x %02x %02x %02x %02x %02x\n",
pIKBD->Clock[ 0 ] ,pIKBD->Clock[ 1 ] , pIKBD->Clock[ 2 ] ,
pIKBD->Clock[ 3 ] , pIKBD->Clock[ 4 ] , pIKBD->Clock[ 5 ] );
/* Return packet header */
if ( IKBD_OutputBuffer_CheckFreeCount ( 7 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xFC , IKBD_Delay_Random ( 7000 , 7500 ) );
/* Return the 6 clock bytes */
for ( i=0 ; i<6 ; i++ )
IKBD_Cmd_Return_Byte ( pIKBD->Clock[ i ] );
}
}
/*-----------------------------------------------------------------------*/
/**
* MEMORY LOAD
*
* 0x20
* ADRMSB ; address in controller
* ADRLSB ; memory to be loaded
* NUM ; number of bytes (0-128)
* { data }
*/
static void IKBD_Cmd_LoadMemory(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_LoadMemory addr 0x%x count %d\n",
(Keyboard.InputBuffer[1] << 8) + Keyboard.InputBuffer[2], Keyboard.InputBuffer[3]);
MemoryLoadNbBytesTotal = Keyboard.InputBuffer[3];
MemoryLoadNbBytesLeft = MemoryLoadNbBytesTotal;
crc32_reset ( &MemoryLoadCrc );
}
/*-----------------------------------------------------------------------*/
/**
* MEMORY READ
*
* 0x21
* ADRMSB ; address in controller
* ADRLSB ; memory to be read
* Returns:
* 0xF6 ; status header
* 0x20 ; memory access
* { data } ; 6 data bytes starting at ADR
*
* NOTE : This function requires to handle the IKBD's RAM, which is only
* possible when emulating a real HD6301 CPU. For now, we only return
* the correct header and 6 empty bytes.
*/
static void IKBD_Cmd_ReadMemory(void)
{
int i;
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReadMemory (not implemented)\n");
/* Return packet header */
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
IKBD_Cmd_Return_Byte ( 0x20 );
/* Return 6 empty bytes */
for ( i=0 ; i<6 ; i++ )
IKBD_Cmd_Return_Byte ( 0x00 );
}
}
/*-----------------------------------------------------------------------*/
/**
* CONTROLLER EXECUTE
*
* 0x22
* ADRMSB ; address of subroutine in
* ADRLSB ; controller memory to be called
*/
static void IKBD_Cmd_Execute(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_Execute addr 0x%x\n",
(Keyboard.InputBuffer[1] << 8) + Keyboard.InputBuffer[2]);
if ( pIKBD_CustomCodeHandler_Write )
{
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd execute addr 0x%x using custom handler\n",
(Keyboard.InputBuffer[1] << 8) + Keyboard.InputBuffer[2]);
IKBD_ExeMode = true; /* turn 6301's custom mode ON */
}
else /* unknown code uploaded to ikbd RAM */
{
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd execute addr 0x%x ignored, no custom handler found\n",
(Keyboard.InputBuffer[1] << 8) + Keyboard.InputBuffer[2]);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE BUTTON ACTION
*
* 0x87
*/
static void IKBD_Cmd_ReportMouseAction(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseAction\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
IKBD_Cmd_Return_Byte (7);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.Action);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE MODE
*
* 0x88 or 0x89 or 0x8A
*/
static void IKBD_Cmd_ReportMouseMode(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseMode\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
switch (KeyboardProcessor.MouseMode)
{
case AUTOMODE_MOUSEREL:
IKBD_Cmd_Return_Byte (8);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
break;
case AUTOMODE_MOUSEABS:
IKBD_Cmd_Return_Byte (9);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Abs.MaxX >> 8);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Abs.MaxX);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Abs.MaxY >> 8);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Abs.MaxY);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
break;
case AUTOMODE_MOUSECURSOR:
IKBD_Cmd_Return_Byte (10);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.KeyCodeDeltaX);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.KeyCodeDeltaY);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
break;
}
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE THRESHOLD
*
* 0x8B
*/
static void IKBD_Cmd_ReportMouseThreshold(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseThreshold\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
IKBD_Cmd_Return_Byte (0x0B);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.XThreshold);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.YThreshold);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE SCALE
*
* 0x8C
*/
static void IKBD_Cmd_ReportMouseScale(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseScale\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
IKBD_Cmd_Return_Byte (0x0C);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.XScale);
IKBD_Cmd_Return_Byte (KeyboardProcessor.Mouse.YScale);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE VERTICAL COORDINATES
*
* 0x8F and 0x90
*/
static void IKBD_Cmd_ReportMouseVertical(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseVertical\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
if (KeyboardProcessor.Mouse.YAxis == -1)
IKBD_Cmd_Return_Byte (0x0F);
else
IKBD_Cmd_Return_Byte (0x10);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT MOUSE AVAILABILITY
*
* 0x92
*/
static void IKBD_Cmd_ReportMouseAvailability(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportMouseAvailability\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
if (KeyboardProcessor.MouseMode == AUTOMODE_OFF)
IKBD_Cmd_Return_Byte (0x12);
else
IKBD_Cmd_Return_Byte (0x00);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT JOYSTICK MODE
*
* 0x94 or 0x95 or 0x99
*/
static void IKBD_Cmd_ReportJoystickMode(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportJoystickMode\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
switch (KeyboardProcessor.JoystickMode)
{
case AUTOMODE_JOYSTICK:
IKBD_Cmd_Return_Byte (0x14);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
break;
default: /* TODO: Joystick keycodes mode not supported yet! */
IKBD_Cmd_Return_Byte (0x15);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
break;
}
}
}
/*-----------------------------------------------------------------------*/
/**
* REPORT JOYSTICK AVAILABILITY
*
* 0x9A
*/
static void IKBD_Cmd_ReportJoystickAvailability(void)
{
LOG_TRACE(TRACE_IKBD_CMDS, "IKBD_Cmd_ReportJoystickAvailability\n");
if ( IKBD_OutputBuffer_CheckFreeCount ( 8 ) )
{
IKBD_Cmd_Return_Byte_Delay ( 0xF6 , IKBD_Delay_Random ( 7000 , 7500 ) );
if (KeyboardProcessor.JoystickMode == AUTOMODE_OFF)
IKBD_Cmd_Return_Byte (0x1A);
else
IKBD_Cmd_Return_Byte (0x00);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
IKBD_Cmd_Return_Byte (0);
}
}
/************************************************************************/
/* End of the IKBD's commands emulation. */
/************************************************************************/
/*************************************************************************/
/**
* Below part is for emulating custom 6301 program sent to the IKBD's RAM
* Specific read/write functions for each demo/game should be added here,
* after being defined in the CustomCodeDefinitions[] array.
*
* The 6301 has 256 bytes of RAM, but only 128 bytes are available to
* put a program (from $80 to $ff).
*
* Executing a program in the 6301 is a 2 steps process :
* 1) a very small program is sent to the RAM using the 0x20 command.
* This is often loaded at address $b0.
* This program will stop interruptions in the 6301 and will accept
* a second small program that will relocate itself to $80.
* 2) the relocated program at address $80 will accept a third (main)
* program and will execute it once reception is complete.
*
* Writes during step 1 are handled with the ExeBootHandler matching the
* LoadMemory CRC.
* ExeBootHandler will compute a 2nd CRC for the writes corresponding to
* the 2nd and 3rd programs sent to the 6301's RAM.
*
* If a match is found for this 2nd CRC, we will override default IKBD's behaviour
* for reading/writing to $fffc02 with ExeMainHandler_Read / ExeMainHandler_Write
* (once the Execute command 0x22 is received).
*
* When using custom program (ExeMode==true), we must ignore all keyboard/mouse/joystick
* events sent to IKBD_Cmd_Return_Byte . Only our functions can add bytes
* to the keyboard buffer.
*
* To exit 6301's execution mode, we can use the 68000 'reset' instruction.
* Some 6301's programs also handle a write to $fffc02 as an exit signal.
*/
/*-----------------------------------------------------------------------*/
/**
* Handle writes to $fffc02 when loading bytes in the IKBD's RAM.
* We compute a CRC of the bytes that are sent until MemoryLoadNbBytesLeft
* reaches 0.
* When all bytes are loaded, we look for a matching CRC ; if found, we
* use the ExeBootHandler defined for this CRC to process the next writes
* that will occur in $fffc02.
* LoadMemory is often used to load a small boot code into the 6301's RAM.
* This small program will be executed later using the command 0x22.
*/
static void IKBD_LoadMemoryByte ( uint8_t aciabyte )
{
unsigned int i;
/* Write received bytes to a file for debug */
// FILE *f = fopen ( "/tmp/ikbd_loadmemory.dump" , "ab" ) ; fprintf ( f , "%c" , aciabyte ) ; fclose ( f );
crc32_add_byte ( &MemoryLoadCrc , aciabyte );
MemoryLoadNbBytesLeft--;
if ( MemoryLoadNbBytesLeft == 0 ) /* all bytes were received */
{
/* Search for a match amongst the known custom routines */
for ( i = 0 ; i < sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ) ; i++ )
if ( CustomCodeDefinitions[ i ].LoadMemCrc == MemoryLoadCrc )
break;
if ( i < sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ) ) /* found */
{
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd loadmemory %d bytes crc=0x%x matches <%s>\n",
MemoryLoadNbBytesTotal, MemoryLoadCrc, CustomCodeDefinitions[ i ].Name);
crc32_reset ( &MemoryLoadCrc );
MemoryExeNbBytes = 0;
pIKBD_CustomCodeHandler_Read = NULL;
pIKBD_CustomCodeHandler_Write = CustomCodeDefinitions[ i ].ExeBootHandler;
}
else /* unknown code uploaded to IKBD's RAM */
{
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd loadmemory %d bytes crc=0x%x : unknown code\n",
MemoryLoadNbBytesTotal, MemoryLoadCrc);
pIKBD_CustomCodeHandler_Read = NULL;
pIKBD_CustomCodeHandler_Write = NULL;
}
}
}
/*-----------------------------------------------------------------------*/
/**
* Handle writes to $fffc02 when executing custom code in the IKBD's RAM.
* This is used to send the small IKBD program that will handle
* keyboard/mouse/joystick input.
* We compute a CRC of the bytes that are sent until we found a match
* with a known custom IKBD program.
*/
static void IKBD_CustomCodeHandler_CommonBoot ( uint8_t aciabyte )
{
unsigned int i;
/* Write received bytes to a file for debug */
// FILE *f = fopen ( "/tmp/ikbd_custom_program.dump" , "ab" ) ; fprintf ( f , "%c" , aciabyte ) ; fclose ( f );
crc32_add_byte ( &MemoryLoadCrc , aciabyte );
MemoryExeNbBytes++;
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd custom exe common boot write 0x%02x count %d crc=0x%x\n",
aciabyte, MemoryExeNbBytes, MemoryLoadCrc);
/* Search for a match amongst the known custom routines */
for ( i = 0 ; i < sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ) ; i++ )
if ( ( CustomCodeDefinitions[ i ].MainProgNbBytes == MemoryExeNbBytes )
&& ( CustomCodeDefinitions[ i ].MainProgCrc == MemoryLoadCrc ) )
break;
if ( i < sizeof ( CustomCodeDefinitions ) / sizeof ( CustomCodeDefinitions[0] ) ) /* found */
{
LOG_TRACE(TRACE_IKBD_EXEC, "ikbd custom exe common boot, uploaded code matches <%s>\n",
CustomCodeDefinitions[i].Name);
pIKBD_CustomCodeHandler_Read = CustomCodeDefinitions[ i ].ExeMainHandler_Read;
pIKBD_CustomCodeHandler_Write = CustomCodeDefinitions[ i ].ExeMainHandler_Write;
Keyboard.BufferHead = Keyboard.BufferTail = 0; /* flush all queued bytes that would be read in $fffc02 */
Keyboard.NbBytesInOutputBuffer = 0;
}
/* If not found, we keep on accumulating bytes until we find a matching crc */
}
/*----------------------------------------------------------------------*/
/* Froggies Over The Fence menu. */
/* Returns 'n' bytes with the mouse position, keyboard can be used too. */
/* Writing a <0 byte to $fffc02 will cause the 6301 to exit custom exe */
/* mode (jmp $f000). */
/* When writing byte 'n' >0 to $fffc02, the 6301 will return the content*/
/* of RAM $7f+n to $7f+1. */
/* $80/$81 contains deltaY/deltaX + left mouse button in bit 7, $82 */
/* contains LMB in bit 7 and $83 contains a fixed value 0xfc. */
/* On each VBL, the demo will ask for 1 byte, then for 4 bytes ; only */
/* the last 2 bytes ($81/$80) will be used, $83/$82 are ignored. */
/* IKBD's $81 will be stored in $600 (CPU RAM), and $80 in $601. */
/* */
/* TODO : an extra delay of 7000 cycles is necessary to have $81 and $80*/
/* received after the overrun condition was cleared at the 68000 level. */
/* Does it mean some timings are wrong with acia/ikbd ? */
/*----------------------------------------------------------------------*/
static void IKBD_CustomCodeHandler_FroggiesMenu_Read ( void )
{
/* Ignore read */
}
static void IKBD_CustomCodeHandler_FroggiesMenu_Write ( uint8_t aciabyte )
{
uint8_t res80 = 0;
uint8_t res81 = 0;
uint8_t res82 = 0;
uint8_t res83 = 0xfc; /* fixed value, not used */
/* When writing a <0 byte to $fffc02, Froggies ikbd's program will terminate itself */
/* and leave Execution mode (jmp $f000) */
if ( aciabyte & 0x80 )
{
IKBD_Boot_ROM ( false );
return;
}
if ( KeyboardProcessor.Mouse.DeltaY < 0 ) res80 = 0x7a; /* mouse up */
if ( KeyboardProcessor.Mouse.DeltaY > 0 ) res80 = 0x06; /* mouse down */
if ( KeyboardProcessor.Mouse.DeltaX < 0 ) res81 = 0x7a; /* mouse left */
if ( KeyboardProcessor.Mouse.DeltaX > 0 ) res81 = 0x06; /* mouse right */
if ( Keyboard.bLButtonDown & BUTTON_MOUSE ) res82 |= 0x80; /* left mouse button */
if ( ScanCodeState[ 0x48 ] ) res80 |= 0x7a; /* up */
if ( ScanCodeState[ 0x50 ] ) res80 |= 0x06; /* down */
if ( ScanCodeState[ 0x4b ] ) res81 |= 0x7a; /* left */
if ( ScanCodeState[ 0x4d ] ) res81 |= 0x06; /* right */
if ( ScanCodeState[ 0x70 ] ) res82 |= 0x80; /* keypad 0 */
res80 |= res82; /* bit 7 is left mouse button */
res81 |= res82;
// res80 = 0x10 ; res81 = 0x11 ; res82 = 0x12 ; res83 = 0x13 ; /* force some discernible values to debug */
if ( aciabyte == 1 ) /* Send 1 byte */
IKBD_Send_Byte_Delay ( res80 , 0 ); /* $80 in IKBD's RAM */
else if ( aciabyte == 4 ) /* Send 4 bytes */
{
IKBD_Send_Byte_Delay ( res83 , 7000 ); /* $83 in IKBD's RAM */
IKBD_Send_Byte_Delay ( res82 , 0 ); /* $82 in IKBD's RAM */
IKBD_Send_Byte_Delay ( res81 , 0 ); /* $81 in IKBD's RAM */
IKBD_Send_Byte_Delay ( res80 , 0 ); /* $80 in IKBD's RAM */
}
}
/*----------------------------------------------------------------------*/
/* Transbeauce II menu. */
/* Returns 1 byte with the joystick position, keyboard can be used too. */
/*----------------------------------------------------------------------*/
static void IKBD_CustomCodeHandler_Transbeauce2Menu_Read ( void )
{
uint8_t res = 0;
/* keyboard emulation */
if ( ScanCodeState[ 0x48 ] ) res |= 0x01; /* up */
if ( ScanCodeState[ 0x50 ] ) res |= 0x02; /* down */
if ( ScanCodeState[ 0x4b ] ) res |= 0x04; /* left */
if ( ScanCodeState[ 0x4d ] ) res |= 0x08; /* right */
if ( ScanCodeState[ 0x62 ] ) res |= 0x40; /* help */
if ( ScanCodeState[ 0x39 ] ) res |= 0x80; /* space */
/* joystick emulation (bit mapping is same as cursor above, with bit 7 = fire button */
res |= ( Joy_GetStickData(JOYID_JOYSTICK1) & 0x8f ) ; /* keep bits 0-3 and 7 */
IKBD_Send_Byte_Delay ( res , 0 );
}
static void IKBD_CustomCodeHandler_Transbeauce2Menu_Write ( uint8_t aciabyte )
{
/* Ignore write */
}
/*----------------------------------------------------------------------*/
/* Dragonnels demo menu. */
/* When any byte is written in $fffc02, returns one byte with the */
/* Y position of the mouse and the state of the left button. */
/*----------------------------------------------------------------------*/
static void IKBD_CustomCodeHandler_DragonnelsMenu_Read ( void )
{
/* Ignore read */
}
static void IKBD_CustomCodeHandler_DragonnelsMenu_Write ( uint8_t aciabyte )
{
uint8_t res = 0;
if ( KeyboardProcessor.Mouse.DeltaY < 0 ) res = 0xfc; /* mouse up */
if ( KeyboardProcessor.Mouse.DeltaY > 0 ) res = 0x04; /* mouse down */
if ( Keyboard.bLButtonDown & BUTTON_MOUSE ) res = 0x80; /* left mouse button */
IKBD_Send_Byte_Delay ( res , 0 );
}
/*----------------------------------------------------------------------*/
/* Chaos A.D. protection's decoder */
/* This custom program reads bytes, decode them and send back the result*/
/* to the 68000. */
/* The program first returns $fe to indicate it's ready to receive the */
/* encoded bytes. */
/* The program then receives the 8 bytes used to decode the data and */
/* store them in $f0 - $f7 (KeyBuffer is already initialized, so we */
/* ignore those 8 bytes). */
/* Then for any received byte a XOR is made with one of the byte in the */
/* 8 bytes buffer, by incrementing an index in this buffer. */
/* The decoded byte is written to addr $13 (TDR) to be received by ACIA */
/*----------------------------------------------------------------------*/
static void IKBD_CustomCodeHandler_ChaosAD_Read ( void )
{
static bool FirstCall = true;
if ( FirstCall == true )
IKBD_Send_Byte_Delay ( 0xfe , 0 );
FirstCall = false;
}
static void IKBD_CustomCodeHandler_ChaosAD_Write ( uint8_t aciabyte )
{
static int IgnoreNb = 8;
uint8_t KeyBuffer[] = { 0xca , 0x0a , 0xbc , 0x00 , 0xde , 0xde , 0xfe , 0xca };
static int Index = 0;
static int Count = 0;
/* We ignore the first 8 bytes we received (they're already in KeyBuffer) */
if ( IgnoreNb > 0 )
{
IgnoreNb--;
return;
}
if ( Count <= 6080 ) /* there're 6081 bytes to decode */
{
Count++;
aciabyte ^= KeyBuffer[ Index ];
Index++;
Index &= 0x07;
IKBD_Send_Byte_Delay ( aciabyte , 0 );
}
else
{
/* When all bytes were decoded if 0x08 is written to $fffc02 */
/* the program will terminate itself and leave Execution mode */
if ( aciabyte == 0x08 )
IKBD_Boot_ROM ( false );
}
}
/*----------------------------------------------------------------------*/
/* Audio Sculpture decryption support */
/* The main executable is decrypted with a key extracted from a */
/* previously uploaded program in the 6301. When the magic value 0x42 */
/* is sent to fffc02 it will output the two bytes 0x4b and 0x13 */
/* and exit the custom handler again */
/* [NP] The custom program has 2 parts : */
/* - 1st part is used during the intro and wait for key 'space' in */
/* color mode or any key in mono mode (but intro screen in mono */
/* exits automatically without testing a key !) */
/* - 2nd part wait to receive $42 from the ACIA, then send $4b and $13 */
/*----------------------------------------------------------------------*/
static bool ASmagic = false;
static void IKBD_CustomCodeHandler_AudioSculpture_Color_Read ( void )
{
IKBD_CustomCodeHandler_AudioSculpture_Read ( true );
}
static void IKBD_CustomCodeHandler_AudioSculpture_Mono_Read ( void )
{
IKBD_CustomCodeHandler_AudioSculpture_Read ( false );
}
static void IKBD_CustomCodeHandler_AudioSculpture_Read ( bool ColorMode )
{
uint8_t res = 0;
static int ReadCount = 0;
if ( ASmagic )
{
ReadCount++;
if ( ReadCount == 2 ) /* We're done reading out the 2 bytes, exit the custom handler */
{
IKBD_Boot_ROM ( false );
ASmagic = false;
ReadCount = 0;
}
}
else if ( ( ( ColorMode == false ) && ( IKBD_CheckPressedKey() >= 0 ) ) /* wait for any key in mono mode */
|| ScanCodeState[ 0x39 ] ) /* wait for 'space' key in color mode */
{
res = 0x39; /* send scancode for 'space' */
IKBD_Send_Byte_Delay ( res , 0 );
}
}
static void IKBD_CustomCodeHandler_AudioSculpture_Write ( uint8_t aciabyte )
{
uint8_t Magic = 0x42;
uint8_t Key[] = { 0x4b , 0x13 };
if ( aciabyte == Magic )
{
ASmagic = true;
IKBD_Send_Byte_Delay ( Key[0] , 0 );
IKBD_Send_Byte_Delay ( Key[1] , 0 );
}
}
void IKBD_Info(FILE *fp, uint32_t dummy)
{
int i;
fprintf(fp, "Transmit/Receive Control+Status: 0x%02x\n", pIKBD->TRCSR);
fprintf(fp, "Rate + Mode Control: 0x%02x\n", pIKBD->RMCR);
fprintf(fp, "Transmit: Receive:\n");
fprintf(fp, "- Data: 0x%02x 0x%02x\n", pIKBD->TDR, pIKBD->RDR);
fprintf(fp, "- Shift: 0x%02x 0x%02x\n", pIKBD->TSR, pIKBD->RSR);
fprintf(fp, "- State: %4d %4d\n",
pIKBD->SCI_TX_State, pIKBD->SCI_RX_State);
fprintf(fp, "- #Bits: %4d %4d\n",
pIKBD->SCI_TX_Size, pIKBD->SCI_RX_Size);
fprintf(fp, "- Delay: %4d\n", pIKBD->SCI_TX_Delay);
fprintf(fp, "Clock:");
for (i = 0; i < ARRAY_SIZE(pIKBD->Clock); i++)
fprintf(fp, " %02x", pIKBD->Clock[i]);
fprintf(fp, " (+%" PRId64 ")\n", pIKBD->Clock_micro);
}
|