summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/drivers/bfcobra.cpp
blob: ccbefc4235f0a91e7ac519a63cb92d50368b56ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
// license:BSD-3-Clause
// copyright-holders:Philip Bennett, Anonymous
/******************************************************************************

    Bell-Fruit Cobra I/II and Viper Hardware

    driver by Phil Bennett and Anonymous

    Games supported:
        * A Question of Sport [2 sets]
        * Beeline (non-working - missing disk)
        * Every Second Counts
        * Inquizitor (Viper hardware, non-working - missing disk)
        * Quizvaders
        * Treble Top

    Other games on this hardware:
        * Brain Box
        * Quintoon alt. version (Cobra II/Cyclone hardware)

    Notes:

    The hardware is based on a chipset known as 'Flare One', developed
    by Flare Technology. It consists of a 16-bit DSP (intended for sound
    synthesis and 3D maths), an 8bpp blitter and a video controller, driven
    by a Z80.

    Flare One would evolve to become 'Slipstream', used by the unreleased
    Konix Multisystem console.

    The Flare One chipset is implemented as four Texas Instruments ASICs,
    each an 84 pin PLCC package:

    CF30204, CF30205, CF30206 (DSP) and CF30207.

    The hardware using this chipset is as follows:

    Viper
    =====

    A video expansion PCB for Scorpion I?
    On some PCB revisions there is audio output circuitry connected to the DSP.
    Viper uses a WD1772 type floppy disk controller.

    Cobra I
    =======

    A combination of Viper and Scorpion I hardware on a single PCB.
    Cobra uses an NEC '765 type FDC. Later revisions have no DSP.

    Cobra II (Cyclone)
    ==================

    A compact video expansion board for Scorpion II.
    The Z80 is replaced by a Z180 and there is no Flare DSP.
    The FDC is replaced by a NCR53C80 SCSI controller.
    Cyclone was never released due to reliability issues.

    Cobra II Jamma
    ==============
    Based on Cobra II but with a JAMMA edge connector for easy use in video game cabinets.
    This is a standalone board which is not paired with any Scorpion boards.
    Break Ball is the only released game for this platform.
    The only other known software is for a functional test unit (FTU).

    Note that for Z8S180 boards the CPU has 20 address lines so can select the entire memory map
    without using the Flare chipset paging option. I believe changing the addresses using chipset_w
    have no effect on Z8S180 boards.

    To do:

    * Complete blitter emulation
    * Cobra II support.
    * Hook up additional inputs, EM meters, lamps etc
    * The prom range selected for the first 16K of Z80 memory map is not fixed so chipset_w
      function needs changing to process offset 0. Note that no released software changes this anyway.
    * Frame interrupt for Z80 boards is wrong. It isn't a frame interrupt but an interrupt which can be
      triggered on any scan line. For Z80 boards the fact that it's treated as a frame interrupt doesn't
      matter as no released software changes the position of the interrupt.  Emulation for Z8S180 boards
      is correct as Break Ball uses multiple scan line interrupts.

    Known issues:

    * All games bar qos: NVRAM not saved

    * Viper does not have a colour palette - the Flare chipset drives RGB direct.
      To fix this I set default values in the palette when the machine is initialised
    * CPU execution rate is wrong, the hardware adds 1 TCycle to each access which is unaccounted for.
    * Plane priority is probably wrong but it's only used in Treble Top.
    * Blitter loop counts and step are wrong - they are 9 bit counts, not 8.
    * Blitter emulation doesn't support hi-res mode (needed for Inquizitor)
    * Blitter emulation currently runs until blit is complete then burns the required number of Z80
      cycles to simulate it holding the bus. On the real hardware the blitter halts the CPU until the
      operation is complete or an interrupt is issued to CPU. When the interrupt line is asserted the
      CPU is allowed to run until the interrupt line is released. This allows all interrupts to run at
      the correct time. The current emulation is OK for Z80 boards and all released software for those
      boards, however, for Cobra II/JAMMA this causes problems due to the UPD7759 audio chip issuing
      dma requests which are then processed too late if the Z8S180 cycles are burnt. To get round this
      the Z8S180 blitter emulation does not burn CPU cycles.
      The blitter emulation ought to take account of irq/dma requests but I don't know how to do that!

******************************************************************************/

#include "emu.h"

#include "video/bfm_dm01.h"

#include "cpu/m6809/m6809.h"
#include "cpu/z80/z80.h"
#include "cpu/z180/z180.h"
#include "machine/6850acia.h"
#include "machine/clock.h"
#include "machine/i2cmem.h"
#include "machine/meters.h"
#include "machine/nvram.h"
#include "sound/ay8910.h"
#include "sound/upd7759.h"
#include "sound/ym2413.h"
#include "video/ramdac.h"

#include "emupal.h"
#include "screen.h"
#include "speaker.h"

#include "brkball.lh"


/*
    Defines
*/
#define Z8S180_XTAL 24000000
#define Z80_XTAL    5910000     /* Unconfirmed */
#define M6809_XTAL  4000000



/***************************************************************************

    Split into video\bfcobra.cpp !

***************************************************************************/

union ADDR_REG
{
#ifdef LSB_FIRST
	struct { uint16_t loword, hiword ; } as16bit;
	struct { uint8_t addr0, addr1, addr2; } as8bit;
#else
	struct { uint16_t hiword, loword ; } as16bit;
	struct { uint8_t addr2, addr1, addr0; } as8bit;
#endif
	uint32_t addr;
};

/* Blitter register flag bits */
#define CMD_RUN         0x01
#define CMD_COLST       0x02
#define CMD_PARRD       0x04        /* Never used? */
#define CMD_SRCUP       0x08
#define CMD_DSTUP       0x10
#define CMD_LT0         0x20
#define CMD_LT1         0x40
#define CMD_LINEDRAW    0x80


/* All unconfirmed */
//#define SRCDST_CMP    0x10
//#define SRCDST_WRAP   0x20
//#define SRCDST_SIGN   0x40
#define SRCDST_A_1      0x80        /* This might be correct for line drawing? */

/* These appear to be correct */
#define MODE_SSIGN      0x80
#define MODE_DSIGN      0x40
#define MODE_YFRAC      0x20
#define MODE_BITTOBYTE  0x04
#define MODE_PALREMAP   0x10

#define CMPFUNC_LT      0x01
#define CMPFUNC_EQ      0x02
#define CMPFUNC_GT      0x04
#define CMPFUNC_BEQ     0x08
#define CMPFUNC_LOG0    0x10
#define CMPFUNC_LOG1    0x20
#define CMPFUNC_LOG2    0x40
#define CMPFUNC_LOG3    0x80

/*
    Blitter state
*/
struct bf_blitter_t
{
	ADDR_REG    program;

	uint8_t       control;
	uint8_t       status;

	uint8_t       command;
	ADDR_REG    source;
	ADDR_REG    dest;
	uint8_t       modectl;
	uint8_t       compfunc;
	uint8_t       outercnt;

	uint8_t       innercnt;
	uint8_t       step;
	uint8_t       pattern;
};

#define LOOPTYPE ( ( blitter.command&0x60 ) >> 5 )

struct fdc_t
{
	uint8_t   MSR;

	int     side;
	int     track;
	int     sector;
	int     number;
	int     stop_track;
	int     setup_read;

	int     byte_pos;
	int     offset;

	int     phase;
	int     next_phase;
	int     cmd_len;
	int     cmd_cnt;
	int     res_len;
	int     res_cnt;
	uint8_t   cmd[10];
	uint8_t   results[8];
};


#define BLUE_0 0
#define BLUE_1 1
#define BLUE_2 2
#define BLUE_3 3
#define GREEN_0 ( 0 << 2 )
#define GREEN_1 ( 1 << 2 )
#define GREEN_2 ( 2 << 2 )
#define GREEN_3 ( 3 << 2 )
#define GREEN_4 ( 4 << 2 )
#define GREEN_5 ( 5 << 2 )
#define GREEN_6 ( 6 << 2 )
#define GREEN_7 ( 7 << 2 )
#define RED_0 ( 0 << 5 )
#define RED_1 ( 1 << 5 )
#define RED_2 ( 2 << 5 )
#define RED_3 ( 3 << 5 )
#define RED_4 ( 4 << 5 )
#define RED_5 ( 5 << 5 )
#define RED_6 ( 6 << 5 )
#define RED_7 ( 7 << 5 )

class bfcobra_state : public driver_device
{
public:
	bfcobra_state(const machine_config &mconfig, device_type type, const char *tag) :
		driver_device(mconfig, type, tag),
		m_maincpu(*this, "maincpu"),
		m_audiocpu(*this, "audiocpu"),
		m_acia6850_0(*this, "acia6850_0"),
		m_acia6850_1(*this, "acia6850_1"),
		m_acia6850_2(*this, "acia6850_2"),
		m_upd7759(*this, "upd"),
		m_palette(*this, "palette"),
		m_meters(*this, "meters")
	{
	}

	void init_bfcobra();
	void bfcobra(machine_config &config);

protected:
	uint8_t chipset_r(offs_t offset);
	void chipset_w(offs_t offset, uint8_t data);
	void rombank_w(uint8_t data);
	uint8_t fdctrl_r();
	uint8_t fddata_r();
	void fdctrl_w(uint8_t data);
	uint8_t int_latch_r();
	uint8_t meter_r();
	void meter_w(uint8_t data);
	uint8_t latch_r();
	void latch_w(offs_t offset, uint8_t data);
	uint8_t upd_r();
	void upd_w(uint8_t data);
	DECLARE_WRITE_LINE_MEMBER(z80_acia_irq);
	DECLARE_WRITE_LINE_MEMBER(m6809_data_irq);
	DECLARE_WRITE_LINE_MEMBER(data_acia_tx_w);
	DECLARE_WRITE_LINE_MEMBER(write_acia_clock);
	virtual void machine_reset() override;
	virtual void video_start() override;
	uint32_t screen_update_bfcobra(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
	INTERRUPT_GEN_MEMBER(timer_irq);
	INTERRUPT_GEN_MEMBER(vblank_gen);
	void RunBlit();
	void update_irqs();
	void reset_fdc();
	void exec_w_phase(uint8_t data);
	void init_ram();
	void command_phase(struct fdc_t &fdc, uint8_t data);
	inline uint8_t* blitter_get_addr(uint32_t addr);
	inline void z80_bank(int num, int data);

	void m6809_prog_map(address_map &map);
	void ramdac_map(address_map &map);
	void z80_io_map(address_map &map);
	void z80_prog_map(address_map &map);

private:
	uint8_t m_bank_data[4];
	std::unique_ptr<uint8_t[]> m_work_ram;
	std::unique_ptr<uint8_t[]> m_video_ram;
	uint8_t m_h_scroll;
	uint8_t m_v_scroll;
	uint8_t m_flip_8;
	uint8_t m_flip_22;
	uint8_t m_videomode;
	uint8_t m_data_r;
	uint8_t m_data_t;
	int m_irq_state;
	int m_acia_irq;
	int m_vblank_irq;
	int m_blitter_irq;
	uint8_t m_z80_int;
	uint8_t m_z80_inten;
	uint32_t m_meter_latch;
	uint32_t m_mux_input;
	uint32_t m_mux_outputlatch;
	uint8_t m_col4bit[16];
	uint8_t m_col3bit[16];
	uint8_t m_col8bit[256];
	uint8_t m_col7bit[256];
	uint8_t m_col6bit[256];
	struct bf_blitter_t m_blitter;
	struct fdc_t m_fdc;
	required_device<cpu_device> m_maincpu;
	required_device<cpu_device> m_audiocpu;
	required_device<acia6850_device> m_acia6850_0;
	required_device<acia6850_device> m_acia6850_1;
	required_device<acia6850_device> m_acia6850_2;
	required_device<upd7759_device> m_upd7759;
	required_device<palette_device> m_palette;
	required_device<meters_device> m_meters;
};


static const uint8_t col4bit_default[16]=
{
	BLUE_0 | GREEN_0 | RED_0,
	BLUE_1,
	GREEN_2,
	BLUE_1 | GREEN_2,
	RED_2,
	RED_2 | BLUE_1,
	RED_2 | GREEN_2,
	RED_2 | GREEN_2 | BLUE_1,
	BLUE_2 | GREEN_5 | RED_5,
	BLUE_3,
	GREEN_7,
	BLUE_3 | GREEN_7,
	RED_7,
	RED_7 | BLUE_3,
	RED_7 | GREEN_7,
	RED_7 | GREEN_7 | BLUE_3
};

static const uint8_t col3bit_default[16]=
{
	0,
	BLUE_3,
	GREEN_7,
	BLUE_3 | GREEN_7,
	RED_7,
	RED_7 | BLUE_3,
	RED_7 | GREEN_7,
	RED_7 | GREEN_7 | BLUE_3,
	0,
	BLUE_3,
	GREEN_7,
	BLUE_3 | GREEN_7,
	RED_7,
	RED_7 | BLUE_3,
	RED_7 | GREEN_7,
	RED_7 | GREEN_7 | BLUE_3
};

static const uint8_t col76index[] = {0, 2, 4, 7};


void bfcobra_state::video_start()
{
	int i;

	memcpy(m_col4bit, col4bit_default, sizeof(m_col4bit));
	memcpy(m_col3bit, col3bit_default, sizeof(m_col3bit));
	for (i = 0; i < 256; ++i)
	{
		uint8_t col;

		m_col8bit[i] = i;
		col = i & 0x7f;
		col = (col & 0x1f) | (col76index[ ( (col & 0x60) >> 5 ) & 3] << 5);
		m_col7bit[i] = col;

		col = (col & 3) | (col76index[( (col & 0x0c) >> 2) & 3] << 2 ) |
				(col76index[( (col & 0x30) >> 4) & 3] << 5 );
		m_col6bit[i] = col;
	}
}

uint32_t bfcobra_state::screen_update_bfcobra(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	int x, y;
	uint8_t  *src;
	uint32_t *dest;
	uint32_t offset;
	uint8_t *hirescol;
	uint8_t *lorescol;

	/* Select screen has to be programmed into two registers */
	/* No idea what happens if the registers are different */
	if (m_flip_8 & 0x40 && m_flip_22 & 0x40)
		offset = 0x10000;
	else
		offset = 0;

	if(m_videomode & 0x20)
	{
		hirescol = m_col3bit;
		lorescol = m_col7bit;
	}
	else if(m_videomode & 0x40)
	{
		hirescol = m_col4bit;
		lorescol = m_col6bit;
	}
	else
	{
		hirescol = m_col4bit;
		lorescol = m_col8bit;
	}

	for (y = cliprect.top(); y <= cliprect.bottom(); ++y)
	{
		uint16_t y_offset = (y + m_v_scroll) * 256;
		src = &m_video_ram[offset + y_offset];
		dest = &bitmap.pix32(y);

		for (x = cliprect.left(); x <= cliprect.right() / 2; ++x)
		{
			uint8_t x_offset = x + m_h_scroll;
			uint8_t pen = *(src + x_offset);

			if ( ( m_videomode & 0x81 ) == 1 || (m_videomode & 0x80 && pen & 0x80) )
			{
				*dest++ = m_palette->pen(hirescol[pen & 0x0f]);
				*dest++ = m_palette->pen(hirescol[(pen >> 4) & 0x0f]);
			}
			else
			{
				*dest++ = m_palette->pen(lorescol[pen]);
				*dest++ = m_palette->pen(lorescol[pen]);
			}
		}
	}

	return 0;
}

uint8_t* bfcobra_state::blitter_get_addr(uint32_t addr)
{
	if (addr < 0x10000)
	{
		/* Is this region fixed? */
		return (uint8_t*)(memregion("user1")->base() + addr);
	}
	else if(addr < 0x20000)
	{
		addr &= 0xffff;
		addr += (m_bank_data[0] & 1) ? 0x10000 : 0;

		return (uint8_t*)(memregion("user1")->base() + addr + ((m_bank_data[0] >> 1) * 0x20000));
	}
	else if (addr >= 0x20000 && addr < 0x40000)
	{
		return (uint8_t*)&m_video_ram[addr - 0x20000];
	}
	else
	{
		return (uint8_t*)&m_work_ram[addr - 0x40000];
	}
}


/*
    This is based this on the Slipstream technical reference manual.
    The Flare One blitter is a simpler design with slightly different parameters
    and will require hardware tests to figure everything out correctly.
*/
void bfcobra_state::RunBlit()
{
#define BLITPRG_READ(x)     blitter.x = *(blitter_get_addr(blitter.program.addr++))

	struct bf_blitter_t &blitter = m_blitter;
	int cycles_used = 0;


	do
	{
		uint8_t srcdata = 0;
		uint8_t dstdata = 0;

		/* Read the blitter command */
		BLITPRG_READ(source.as8bit.addr0);
		BLITPRG_READ(source.as8bit.addr1);
		BLITPRG_READ(source.as8bit.addr2);
		BLITPRG_READ(dest.as8bit.addr0);
		BLITPRG_READ(dest.as8bit.addr1);
		BLITPRG_READ(dest.as8bit.addr2);
		BLITPRG_READ(modectl);
		BLITPRG_READ(compfunc);
		BLITPRG_READ(outercnt);
		BLITPRG_READ(innercnt);
		BLITPRG_READ(step);
		BLITPRG_READ(pattern);

#if 0
		/* This debug is now wrong ! */
		if (DEBUG_BLITTER)
		{
			osd_printf_debug("\n%s:Blitter: Running command from 0x%.5x\n\n", device->machine().describe_context(), blitter.program.addr - 12);
			osd_printf_debug("Command Reg         %.2x",   blitter.command);
			osd_printf_debug("     %s %s %s %s %s %s %s\n",
				blitter.command & CMD_RUN ? "RUN" : "     ",
				blitter.command & CMD_COLST ? "COLST" : "     ",
				blitter.command & CMD_PARRD ? "PARRD" : "     ",
				blitter.command & CMD_SRCUP ? "SRCUP" : "     ",
				blitter.command & CMD_DSTUP ? "DSTUP" : "     ");

			osd_printf_debug("Src Address Byte 0  %.2x\n", blitter.source.as8bit.addr0);
			osd_printf_debug("Src Address Byte 1  %.2x\n", blitter.source.as8bit.addr1);
			osd_printf_debug("Src Control         %.2x\n", blitter.source.as8bit.addr2);
			osd_printf_debug("  Src Address       %.5x\n", blitter.source.addr & 0xfffff);
			osd_printf_debug("Dest Address Byte 0 %.2x\n", blitter.dest.as8bit.addr0);
			osd_printf_debug("Dest Address Byte 1 %.2x\n", blitter.dest.as8bit.addr1);
			osd_printf_debug("Dest Control        %.2x\n", blitter.dest.as8bit.addr2);
			osd_printf_debug("  Dst. Address      %.5x\n", blitter.dest.addr & 0xfffff);
			osd_printf_debug("Mode Control        %.2x",   blitter.modectl);
			osd_printf_debug("     %s\n", blitter.modectl & MODE_BITTOBYTE ? "BIT_TO_BYTE" : "");

			osd_printf_debug("Comp. and LFU       %.2x\n", blitter.compfunc);
			osd_printf_debug("Outer Loop Count    %.2x (%d)\n", blitter.outercnt, blitter.outercnt);
			osd_printf_debug("Inner Loop Count    %.2x (%d)\n", blitter.innercnt, blitter.innercnt);
			osd_printf_debug("Step Value          %.2x\n", blitter.step);
			osd_printf_debug("Pattern Byte        %.2x\n", blitter.pattern);
		}
#endif

		/* Ignore these writes */
		if (blitter.dest.addr == 0)
			return;

		/* Begin outer loop */
		for (;;)
		{
			uint8_t innercnt = blitter.innercnt;
			dstdata = blitter.pattern;

			if (blitter.command & CMD_LINEDRAW)
			{
				do
				{
					if (blitter.modectl & MODE_YFRAC)
					{
						if (blitter.modectl & MODE_SSIGN )
							blitter.dest.as8bit.addr0--;
						else
							blitter.dest.as8bit.addr0++;
					}
					else
					{
						if (blitter.modectl & MODE_DSIGN )
							blitter.dest.as8bit.addr1--;
						else
							blitter.dest.as8bit.addr1++;
					}
					if( blitter.source.as8bit.addr0 < blitter.step )
					{
						blitter.source.as8bit.addr0 -= blitter.step ;
						blitter.source.as8bit.addr0 += blitter.source.as8bit.addr1;

						if ( blitter.modectl & MODE_YFRAC )
						{
							if (blitter.modectl & MODE_DSIGN )
								blitter.dest.as8bit.addr1--;
							else
								blitter.dest.as8bit.addr1++;
						}
						else
						{
							if (blitter.modectl & MODE_SSIGN )
								blitter.dest.as8bit.addr0--;
							else
								blitter.dest.as8bit.addr0++;
						}
					}
					else
					{
						blitter.source.as8bit.addr0 -= blitter.step;
					}

					*blitter_get_addr( blitter.dest.addr) = blitter.pattern;
					cycles_used++;

				} while (--innercnt);
			}
			else do
			{
				uint8_t   inhibit = 0;

				/* TODO: Set this correctly */
				uint8_t   result = blitter.pattern;

				if (LOOPTYPE == 3 && innercnt == blitter.innercnt)
				{
					srcdata = *(blitter_get_addr( blitter.source.addr & 0xfffff));
					blitter.source.as16bit.loword++;
					cycles_used++;
				}

				/* Enable source address read and increment? */
				if (!(blitter.modectl & (MODE_BITTOBYTE | MODE_PALREMAP)))
				{
					if (LOOPTYPE == 0 || LOOPTYPE == 1)
					{
						srcdata = *(blitter_get_addr( blitter.source.addr & 0xfffff));
						cycles_used++;

						if (blitter.modectl & MODE_SSIGN)
							blitter.source.as16bit.loword-- ;
						else
							blitter.source.as16bit.loword++;

						result = srcdata;
					}
				}

				/* Read destination pixel? */
				if (LOOPTYPE == 0)
				{
					dstdata = *blitter_get_addr( blitter.dest.addr & 0xfffff);
					cycles_used++;
				}

				/* Inhibit depending on the bit selected by the inner count */

				/* Switch on comparator type? */
				if (blitter.modectl & MODE_BITTOBYTE)
				{
					inhibit = !(srcdata & (1 << (8 - innercnt)));
				}

				if (blitter.compfunc & CMPFUNC_BEQ)
				{
					if (srcdata == blitter.pattern)
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_LT)
				{
					if ((srcdata & 0xc0) > (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_EQ)
				{
					if ((srcdata & 0xc0) == (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_GT)
				{
					if ((srcdata & 0xc0) < (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}

				/* Write the data if not inhibited */
				if (!inhibit)
				{
					if (blitter.modectl == MODE_PALREMAP)
					{
						/*
						    In this mode, the source points to a 256 entry lookup table.
						    The existing destination pixel is used as a lookup
						    into the table and the colours is replaced.
						*/
						uint8_t dest = *blitter_get_addr( blitter.dest.addr);
						uint8_t newcol = *(blitter_get_addr( (blitter.source.addr + dest) & 0xfffff));

						*blitter_get_addr( blitter.dest.addr) = newcol;
						cycles_used += 3;
					}
					else
					{
						uint8_t final_result = 0;

						if (blitter.compfunc & CMPFUNC_LOG3)
							final_result |= result & dstdata;

						if (blitter.compfunc & CMPFUNC_LOG2)
							final_result |= result & ~dstdata;

						if (blitter.compfunc & CMPFUNC_LOG1)
							final_result |= ~result & dstdata;

						if (blitter.compfunc & CMPFUNC_LOG0)
							final_result |= ~result & ~dstdata;

						*blitter_get_addr( blitter.dest.addr) = final_result;
						cycles_used++;
					}
				}

				/* Update destination address */
				if (blitter.modectl & MODE_DSIGN)
					blitter.dest.as16bit.loword--;
				else
					blitter.dest.as16bit.loword++;

			} while (--innercnt);

			if (!--blitter.outercnt)
			{
				break;
			}
			else
			{
				if (blitter.command & CMD_DSTUP)
					blitter.dest.as16bit.loword += blitter.step;

				if (blitter.command & CMD_SRCUP)
					blitter.source.as16bit.loword += blitter.step;

				if (blitter.command & CMD_PARRD)
				{
					BLITPRG_READ(innercnt);
					BLITPRG_READ(step);
					BLITPRG_READ(pattern);
				}
			}
		}

		/* Read next command header */
		BLITPRG_READ(command);

	} while (blitter.command  & CMD_RUN);

	/* Burn Z80 cycles while blitter is in operation */
	m_maincpu->spin_until_time(attotime::from_nsec( (1000000000 / Z80_XTAL)*cycles_used * 2 ) );
}

/***************************************************************************

    Flare One Register Map

    01  Bank control for Z80 region 0x4000-0x7fff (16kB)    WR
    02  Bank control for Z80 region 0x8000-0xbfff (16kB)    WR
    03  Bank control for Z80 region 0xc000-0xffff (16kB)    WR

    06  Interrupt status....................................WR
    07  Interrupt ack.......................................WR
        Writing here sets the line number that vertical interrupt is generated at.
        cmd1, bit2 is the 9th bit of the line number
        ???? Written with 0x21
    08  cmd1                                                WR * bit 6 = screen select
        bit2 = 9th bit of vertical interrupt line number
        bit6 = 1 = select screen 1 else screen 0
    09  cmd2 Linked with c001...............................W * bit 0 = 1 = hires
        bit0=1=hi res else lo res (as long as bit7 is 0)
        bit5=mask msb of each pixel
        bit6=mask 2 msbits of each lores pixel
        bit7=1=variable resolution - resolution is set by bit 7 of each vram byte.  bit7=1=2 hires pixels
    0A  ???? Written with 0 and 1...........................W
        color of border

    0B  Horizontal frame buffer scroll .....................W
    0C  Vertical frame buffer scroll .......................W

    0D  Colour hold colour..................................W
    0E  Palette value for hi-res magenta....................W
    0F  Palette value for hi-res yellow?....................W
    14  ....................................................W

    18  Blitter program low byte............................WR
    19  Blitter program middle byte.........................W
    1A  Blitter program high byte...........................W
    1B  Blitter command?....................................W
    1C  Blitter status?......................................R
    20  Blitter control register............................W

    22  ???? Linked with C002...............................W
        Mask of 20

        Joystick: xxx1 11xx                                 R
        ? (polled on tight loop):  x1xx xxxx                R

    40: ROM bank select.....................................W

***************************************************************************/

void bfcobra_state::update_irqs()
{
	int newstate = m_blitter_irq || m_vblank_irq || m_acia_irq;

	if (newstate != m_irq_state)
	{
		m_irq_state = newstate;
		m_maincpu->set_input_line(0, m_irq_state ? ASSERT_LINE : CLEAR_LINE);
	}
}

uint8_t bfcobra_state::chipset_r(offs_t offset)
{
	uint8_t val = 0xff;

	switch(offset)
	{
		case 1:
		case 2:
		case 3:
		{
			val = m_bank_data[offset];
			break;
		}
		case 6:
		{
			/* TODO */
			val = m_vblank_irq << 4;
			break;
		}
		case 7:
		{
			m_vblank_irq = 0;
			val = 0x1;

			/* TODO */
			update_irqs();
			break;
		}
		case 0x1C:
		{
			/* Blitter status ? */
			val = 0;
			break;
		}
		case 0x20:
		{
			/* Seems correct - used during RLE pic decoding */
			val = m_blitter.dest.as8bit.addr0;
			break;
		}
		case 0x22:
		{
			val = 0x40 | ioport("JOYSTICK")->read();
			break;
		}
		default:
		{
			osd_printf_debug("Flare One unknown read: 0x%.2x (PC:0x%.4x)\n", offset, m_maincpu->pcbase());
		}
	}

	return val;
}

void bfcobra_state::chipset_w(offs_t offset, uint8_t data)
{
	switch (offset)
	{
		case 0x01:
		case 0x02:
		case 0x03:
		{
			if (data > 0x3f)
				popmessage("%x: Unusual bank access (%x)\n", m_maincpu->pcbase(), data);

			data &= 0x3f;
			m_bank_data[offset] = data;
			z80_bank(offset, data);
			break;
		}

		case 0x08:
		{
			m_flip_8 = data;
			break;
		}
		case 9:
			m_videomode = data;
			break;

		case 0x0B:
		{
			m_h_scroll = data;
			break;
		}
		case 0x0C:
		{
			m_v_scroll = data;
			break;
		}
		case 0x0E:
		{
			m_col4bit[5] = data;
			m_col3bit[5] = data;
			m_col3bit[5 + 8] = data;
			break;
		}
		case 0x0f:
		{
			m_col4bit[6] = data;
			m_col3bit[6] = data;
			m_col3bit[6 + 8] = data;
			break;
		}
		case 0x18:
		{
			m_blitter.program.as8bit.addr0 = data;
			break;
		}
		case 0x19:
		{
			m_blitter.program.as8bit.addr1 = data;
			break;
		}
		case 0x1A:
		{
			m_blitter.program.as8bit.addr2 = data;
			break;
		}
		case 0x20:
		{
			m_blitter.command = data;

			if (data & CMD_RUN)
				RunBlit();
			else
				osd_printf_debug("Blitter stopped by IO.\n");

			break;
		}
		case 0x22:
		{
			m_flip_22 = data;
			break;
		}
		default:
		{
			osd_printf_debug("Flare One unknown write: 0x%.2x with 0x%.2x (PC:0x%.4x)\n", offset, data, m_maincpu->pcbase());
		}
	}
}

void bfcobra_state::z80_bank(int num, int data)
{
	static const char * const bank_names[] = { "bank1", "bank2", "bank3" };

	if (data < 0x08)
	{
		uint32_t offset = ((m_bank_data[0] >> 1) * 0x20000) + ((0x4000 * data) ^ ((m_bank_data[0] & 1) ? 0 : 0x10000));

		membank(bank_names[num - 1])->set_base(memregion("user1")->base() + offset);
	}
	else if (data < 0x10)
	{
		membank(bank_names[num - 1])->set_base(&m_video_ram[(data - 0x08) * 0x4000]);
	}
	else
	{
		membank(bank_names[num - 1])->set_base(&m_work_ram[(data - 0x10) * 0x4000]);
	}
}

void bfcobra_state::rombank_w(uint8_t data)
{
	m_bank_data[0] = data;
	z80_bank(1, m_bank_data[1]);
	z80_bank(2, m_bank_data[2]);
	z80_bank(3, m_bank_data[3]);
}



/***************************************************************************

    Split into machine\bfcobra.cpp !

    Alternatively chuck it all away and borrow the MESS implementation
    because it's a million times better.

***************************************************************************/


/*
    WD37C656C-PL (or equivalent) Floppy Disk Controller
*/

enum fdc_phase
{
	COMMAND,
	EXECUTION_R,
	EXECUTION_W,
	RESULTS
};

enum command
{
	SENSE_DRIVE_STATUS = 0,
	READ_A_TRACK = 2,
	SPECIFY = 3,
	WRITE_DATA = 5,
	READ_DATA = 6,
	RECALIBRATE = 7,
	SENSE_INTERRUPT_STATUS = 8,
	WRITE_DELETED_DATA = 9,
	READ_ID = 10,
	FORMAT_TRACK = 13,
	READ_DELETED_DATA = 14,
	SEEK = 15,
	SCAN_EQUAL = 17,
	SCAN_LOW_OR_EQUAL = 25,
	SCAN_HIGH_OR_EQUAL = 29
};

void bfcobra_state::reset_fdc()
{
	memset(&m_fdc, 0, sizeof(m_fdc));

	m_fdc.MSR = 0x80;
	m_fdc.phase = COMMAND;
}

uint8_t bfcobra_state::fdctrl_r()
{
	uint8_t val = 0;

	val = m_fdc.MSR;

	return val;
}

uint8_t bfcobra_state::fddata_r()
{
	struct fdc_t &fdc = m_fdc;
	#define BPS     1024
	#define SPT     10
	#define BPT     1024*10

	uint8_t val = 0;

	if (fdc.phase == EXECUTION_R)
	{
		switch (fdc.cmd[0] & 0x1f)
		{
			/* Specify */
			case READ_DATA:
			{
				if (fdc.setup_read)
				{
					fdc.track = fdc.cmd[2];
					fdc.side = fdc.cmd[3];
					fdc.sector = fdc.cmd[4];
					fdc.number = fdc.cmd[5];
					fdc.stop_track = fdc.cmd[6];
					//int GPL = fdc.cmd[7];
					//int DTL = fdc.cmd[8];

					fdc.setup_read = 0;
					fdc.byte_pos = 0;
				}

				fdc.offset = (BPT * fdc.track*2) + (fdc.side ? BPT : 0) + (BPS * (fdc.sector-1)) + fdc.byte_pos++;
				val = *(memregion("user2")->base() + fdc.offset);

				/* Move on to next sector? */
				if (fdc.byte_pos == 1024)
				{
					fdc.byte_pos = 0;

					if (fdc.sector == fdc.stop_track || ++fdc.sector == 11)
					{
						/* End of read operation */
						fdc.MSR = 0xd0;
						fdc.phase = RESULTS;

						fdc.results[0] = 0;
						fdc.results[1] = 0;
						fdc.results[2] = 0;

						fdc.results[3] = 0;
						fdc.results[4] = 0;
						fdc.results[5] = 0;
						fdc.results[6] = 0;
					}
				}
				break;
			}
		}
	}
	else if (fdc.phase == RESULTS)
	{
		val = fdc.results[fdc.res_cnt++];

		if (fdc.res_cnt == fdc.res_len)
		{
			fdc.phase = COMMAND;
			fdc.res_cnt = 0;
			fdc.MSR &= ~0x40;
		}
	}

	return val;
}

void bfcobra_state::fdctrl_w(uint8_t data)
{
	struct fdc_t &fdc = m_fdc;
	switch (fdc.phase)
	{
		case COMMAND:
		{
			command_phase(fdc, data);
			break;
		}
		case EXECUTION_W:
		{
			exec_w_phase(data);
			break;
		}
		default:
		{
			osd_printf_debug("Unknown FDC phase?!");
		}
	}
}

void bfcobra_state::command_phase(struct fdc_t &fdc, uint8_t data)
{
	if (fdc.cmd_cnt == 0)
	{
		fdc.cmd[0] = data;

		fdc.cmd_cnt = 1;

		switch (data & 0x1f)
		{
			/* Specify */
			case READ_DATA:
			{
//              osd_printf_debug("Read data\n");
				fdc.cmd_len = 9;
				fdc.res_len = 7;
				fdc.next_phase = EXECUTION_R;
				fdc.setup_read = 1;
				break;
			}
			case SPECIFY:
			{
//              osd_printf_debug("Specify\n");
				fdc.cmd_len = 3;
				fdc.res_len = 0;
				fdc.next_phase = COMMAND;
				break;
			}
			case RECALIBRATE:
			{
//              osd_printf_debug("Recalibrate\n");
				fdc.cmd_len = 2;
				fdc.res_len = 0;
				fdc.next_phase = COMMAND;
				//fdc.MSR |= 0x40;
				break;
			}
			case SENSE_INTERRUPT_STATUS:
			{
//              osd_printf_debug("Sense interrupt status\n");
				fdc.cmd_len = 1;
				fdc.res_len = 2;
				fdc.phase = RESULTS;

				fdc.results[0] = 0;
				fdc.results[1] = 0;

				fdc.cmd_cnt = 0;
				fdc.MSR |= 0x40;
				break;
			}
			case SEEK:
			{
//              osd_printf_debug("Seek\n");
				fdc.cmd_len = 3;
				fdc.res_len = 0;
				fdc.next_phase = COMMAND;
				break;
			}
			default:
			{
//              osd_printf_debug("%x\n",data & 0x1f);
			}
		}
	}
	else
	{
		fdc.cmd[fdc.cmd_cnt++] = data;
		//osd_printf_debug(" %x\n",data);
	}

	if (fdc.cmd_cnt == fdc.cmd_len)
	{
		fdc.phase = fdc.next_phase;
		fdc.cmd_cnt = 0;

		if ((fdc.cmd[0] & 0x1f) == READ_DATA)
			fdc.MSR = 0xf0;
	}
}

#ifdef UNUSED_FUNCTION
uint8_t bfcobra_state::exec_r_phase(void)
{
	return 0;
}
#endif

void bfcobra_state::exec_w_phase(uint8_t data)
{
}

#ifdef UNUSED_FUNCTION
uint8_t bfcobra_state::results_phase(void)
{
	return 0;
}

void bfcobra_state::fd_op_w(uint8_t data)
{
}

void bfcobra_state::fd_ctrl_w(uint8_t data)
{
}
#endif

void bfcobra_state::machine_reset()
{
	unsigned int pal;

	for (pal = 0; pal < 256; ++pal)
	{
		m_palette->set_pen_color(pal, pal3bit((pal>>5)&7), pal3bit((pal>>2)&7), pal2bit(pal&3));
	}

	m_bank_data[0] = 1;
	reset_fdc();

	m_irq_state = m_blitter_irq = m_vblank_irq = m_acia_irq = 0;
}

/***************************************************************************

    Cobra I/Viper Z80 Memory Map

***************************************************************************/

void bfcobra_state::z80_prog_map(address_map &map)
{
	map(0x0000, 0x3fff).bankr("bank4");
	map(0x4000, 0x7fff).bankrw("bank1");
	map(0x8000, 0xbfff).bankrw("bank2");
	map(0xc000, 0xffff).bankrw("bank3");
}

void bfcobra_state::z80_io_map(address_map &map)
{
map.global_mask(0xff);
	map(0x00, 0x23).rw(FUNC(bfcobra_state::chipset_r), FUNC(bfcobra_state::chipset_w));
	map(0x24, 0x25).w(m_acia6850_0, FUNC(acia6850_device::write));
	map(0x26, 0x27).r(m_acia6850_0, FUNC(acia6850_device::read));
	map(0x30, 0x30).r(FUNC(bfcobra_state::fdctrl_r));
	map(0x31, 0x31).rw(FUNC(bfcobra_state::fddata_r), FUNC(bfcobra_state::fdctrl_w));
	map(0x40, 0x40).w(FUNC(bfcobra_state::rombank_w));
	map(0x50, 0x50).w("ramdac", FUNC(ramdac_device::index_w));
	map(0x51, 0x51).rw("ramdac", FUNC(ramdac_device::pal_r), FUNC(ramdac_device::pal_w));
	map(0x52, 0x52).w("ramdac", FUNC(ramdac_device::mask_w));
	map(0x53, 0x53).w("ramdac", FUNC(ramdac_device::index_r_w));
}


void bfcobra_state::ramdac_map(address_map &map)
{
	map(0x000, 0x3ff).rw("ramdac", FUNC(ramdac_device::ramdac_pal_r), FUNC(ramdac_device::ramdac_rgb666_w));
}


/***************************************************************************

    Cobra I/Viper 6809 Memory Map

    Cobra I has these components:

    EF68B50P            - For communication with Z80
    EF68B50P            - For data retrieval
    AY38912A
    UPD7759C
    BFM1090 (CF30056)   - 'TEXAS' I/O chip, lamps and misc
    BFM1095 (CF30057)   - 'TEXAS' I/O chip, handles switches
    BFM1071             - 6809 Address decoding and bus control

    /IRQ provided by EF68850P at IC38
    /FIRQ connected to meter current sensing circuitry

    TODO: Calculate watchdog timer period.

***************************************************************************/

/* TODO */
uint8_t bfcobra_state::int_latch_r()
{
	return 2 | 1;
}

/* TODO */
uint8_t bfcobra_state::meter_r()
{
	return m_meter_latch;
}

/* TODO: This is borrowed from Scorpion 1 */
void bfcobra_state::meter_w(uint8_t data)
{
	int i;
	int  changed = m_meter_latch ^ data;

	m_meter_latch = data;

	/*
	    When a meter is triggered, the current drawn is sensed. If a meter
	    is connected, the /FIRQ line will be pulsed.
	*/
	for (i = 0; i < 8; i++)
	{
		if (changed & (1 << i))
		{
			m_meters->update(i, data & (1 << i) );
			m_audiocpu->set_input_line(M6809_FIRQ_LINE, HOLD_LINE);
		}
	}
}

/* TODO */
uint8_t bfcobra_state::latch_r()
{
	return m_mux_input;
}

void bfcobra_state::latch_w(offs_t offset, uint8_t data)
{
	/* TODO: This is borrowed from Scorpion 1 */
	switch(offset)
	{
		case 0:
		{
			int changed = m_mux_outputlatch ^ data;
			static const char *const port[] = { "STROBE0", "STROBE1", "STROBE2", "STROBE3", "STROBE4", "STROBE5", "STROBE6", "STROBE7" };

			m_mux_outputlatch = data;

			/* Clock has changed */
			if (changed & 0x08)
			{
				int input_strobe = data & 0x7;

				/* Clock is low */
				if (!(data & 0x08))
					m_mux_input = ioport(port[input_strobe])->read();
			}
			break;
		}
		case 1:
		{
//          strobe_data_l = data;
			break;
		}
		case 2:
		{
//          strobe_data_h = data;
			break;
		}
	}
}

uint8_t bfcobra_state::upd_r()
{
	return 2 | m_upd7759->busy_r();
}

void bfcobra_state::upd_w(uint8_t data)
{
	m_upd7759->reset_w(BIT(data, 7));
	m_upd7759->port_w(data & 0x3f);
	m_upd7759->start_w(!BIT(data, 6));
}

void bfcobra_state::m6809_prog_map(address_map &map)
{
	map(0x0000, 0x1fff).ram().share("nvram");
	map(0x2000, 0x2000).ram();     // W 'B', 6F
	map(0x2200, 0x2200).ram();     // W 'F'
	map(0x2600, 0x2600).rw(FUNC(bfcobra_state::meter_r), FUNC(bfcobra_state::meter_w));
	map(0x2800, 0x2800).ram();     // W
	map(0x2a00, 0x2a02).rw(FUNC(bfcobra_state::latch_r), FUNC(bfcobra_state::latch_w));
	map(0x2e00, 0x2e00).r(FUNC(bfcobra_state::int_latch_r));
	map(0x3001, 0x3001).w("aysnd", FUNC(ay8910_device::data_w));
	map(0x3201, 0x3201).w("aysnd", FUNC(ay8910_device::address_w));
	map(0x3404, 0x3405).rw(m_acia6850_1, FUNC(acia6850_device::read), FUNC(acia6850_device::write));
	map(0x3406, 0x3407).rw(m_acia6850_2, FUNC(acia6850_device::read), FUNC(acia6850_device::write));
//  map(0x3408, 0x3408).noprw();
//  map(0x340a, 0x340a).noprw();
//  map(0x3600, 0x3600).noprw();
	map(0x3801, 0x3801).rw(FUNC(bfcobra_state::upd_r), FUNC(bfcobra_state::upd_w));
	map(0x8000, 0xffff).rom();
	map(0xf000, 0xf000).nopw();    /* Watchdog */
}

static INPUT_PORTS_START( bfcobra )
	PORT_START("STROBE0")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_COIN1 ) PORT_NAME("Coin: 10p")
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_COIN2 ) PORT_NAME("Coin: 20p")
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_COIN3 ) PORT_NAME("Coin: 50p")
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_COIN4 ) PORT_NAME("Coin: 1 pound")
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_SERVICE ) PORT_NAME("Green Test?") PORT_CODE(KEYCODE_F1)
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_SERVICE ) PORT_NAME("Red Test?")
	PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_UNKNOWN )

	PORT_START("STROBE1")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Pass") PORT_CODE(KEYCODE_A)
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Continue") PORT_CODE(KEYCODE_S)
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Collect") PORT_CODE(KEYCODE_D)
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_START1 ) PORT_NAME("Start")
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_OTHER ) PORT_NAME("Bonus") PORT_CODE(KEYCODE_F)
//  PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_BUTTON1 )
//  PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_BUTTON2 )
//  PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_BUTTON3 )

	PORT_START("STROBE2")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON5 ) PORT_NAME("<A")
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON6 ) PORT_NAME("<B")
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON7 ) PORT_NAME("<C")
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_BUTTON8 ) PORT_NAME("A>")
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON9 ) PORT_NAME("B>")
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_BUTTON10 ) PORT_NAME("C>")
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("STROBE3")
	PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_INTERLOCK) PORT_NAME("Cash box door") PORT_CODE(KEYCODE_Y) PORT_TOGGLE
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_SERVICE ) PORT_NAME("Front Door? (resets)") PORT_CODE(KEYCODE_T) PORT_TOGGLE
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_SERVICE ) PORT_NAME("Refill Key") PORT_CODE(KEYCODE_R) PORT_TOGGLE
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("STROBE4")
	PORT_BIT( 0xFF, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("STROBE5")
	PORT_BIT( 0xFF, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("STROBE6")
	PORT_DIPNAME( 0x01, 0x00, "DIL09" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x01, DEF_STR( On  ) )
	PORT_DIPNAME( 0x02, 0x00, "DIL10" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x02, DEF_STR( On  ) )
	PORT_DIPNAME( 0x04, 0x00, "DIL11" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x04, DEF_STR( On  ) )
	PORT_DIPNAME( 0x08, 0x00, "DIL12" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x08, DEF_STR( On  ) )
	PORT_DIPNAME( 0x10, 0x00, "DIL13" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x10, DEF_STR( On  ) )
	PORT_DIPNAME( 0x20, 0x00, "DIL14" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x20, DEF_STR( On  ) )
	PORT_DIPNAME( 0x40, 0x00, "DIL15" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x40, DEF_STR( On  ) )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("STROBE7")
	PORT_DIPNAME( 0x01, 0x00, "DIL02" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x01, DEF_STR( On  ) )
	PORT_DIPNAME( 0x02, 0x00, "DIL03" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x02, DEF_STR( On  ) )
	PORT_DIPNAME( 0x04, 0x00, "DIL04" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x04, DEF_STR( On  ) )
	PORT_DIPNAME( 0x08, 0x00, "DIL05" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x08, DEF_STR( On  ) )
	PORT_DIPNAME( 0x10, 0x00, "DIL06" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x10, DEF_STR( On  ) )
	PORT_DIPNAME( 0x20, 0x00, "DIL07" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x20, DEF_STR( On  ) )
	PORT_DIPNAME( 0x40, 0x00, "DIL08" )
	PORT_DIPSETTING(    0x00, DEF_STR( Off ) )
	PORT_DIPSETTING(    0x40, DEF_STR( On  ) )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("JOYSTICK")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_BUTTON1 )
INPUT_PORTS_END

/*
    Allocate work RAM and video RAM shared by the Z80 and chipset.
*/
void bfcobra_state::init_ram()
{
	/* 768kB work RAM */
	m_work_ram = make_unique_clear<uint8_t[]>(0xC0000);

	/* 128kB video RAM */
	m_video_ram = make_unique_clear<uint8_t[]>(0x20000);
}


WRITE_LINE_MEMBER(bfcobra_state::z80_acia_irq)
{
	m_acia_irq = state;
	update_irqs();
}


WRITE_LINE_MEMBER(bfcobra_state::m6809_data_irq)
{
	m_audiocpu->set_input_line(M6809_IRQ_LINE, state ? ASSERT_LINE : CLEAR_LINE);
}


WRITE_LINE_MEMBER(bfcobra_state::data_acia_tx_w)
{
	m_data_t = state;
}


WRITE_LINE_MEMBER(bfcobra_state::write_acia_clock)
{
	m_acia6850_0->write_txc(state);
	m_acia6850_0->write_rxc(state);
	m_acia6850_1->write_txc(state);
	m_acia6850_1->write_rxc(state);
	m_acia6850_2->write_txc(state);
	m_acia6850_2->write_rxc(state);
}


/* TODO: Driver vs Machine Init */
void bfcobra_state::init_bfcobra()
{
	/*
	    6809 ROM address and data lines are scrambled.
	    This is the same scrambling as Scorpion 2.
	*/
	static const uint8_t datalookup[] = { 1, 3, 5, 6, 4, 2, 0, 7 };
	static const uint8_t addrlookup[] = { 11, 12, 0, 2, 3, 5, 7, 9, 8, 6, 1, 4, 10, 13, 14 };

	std::vector<uint8_t> tmp(0x8000);
	uint8_t *rom = memregion("audiocpu")->base() + 0x8000;
	memcpy(&tmp[0], rom, 0x8000);

	for (uint32_t i = 0; i < 0x8000; i++)
	{
		uint8_t val = tmp[i];

		uint8_t data = 0;
		for (uint8_t x = 0; x < 8; x ++)
			data |= ((val >> x) & 1) << datalookup[x];

		uint16_t addr = 0;
		for (uint8_t x = 0; x < 15; x ++)
			addr |= ((i >> x) & 1)  << addrlookup[x];

		rom[addr] = data;
	}

	init_ram();

	m_bank_data[0] = 1;
	m_bank_data[1] = 0;
	m_bank_data[2] = 0;
	m_bank_data[3] = 0;

	/* Fixed 16kB ROM region */
	membank("bank4")->set_base(memregion("user1")->base());

	/* TODO: Properly sort out the data ACIA */
	m_acia6850_2->write_rxd(1);

	/* Finish this */
	save_item(NAME(m_data_r));
	save_item(NAME(m_data_t));
	save_item(NAME(m_h_scroll));
	save_item(NAME(m_v_scroll));
	save_item(NAME(m_flip_8));
	save_item(NAME(m_flip_22));
	save_item(NAME(m_z80_int));
	save_item(NAME(m_z80_inten));
	save_item(NAME(m_bank_data));
	save_pointer(NAME(m_work_ram), 0xc0000);
	save_pointer(NAME(m_video_ram), 0x20000);
}

/* TODO */
INTERRUPT_GEN_MEMBER(bfcobra_state::timer_irq)
{
	device.execute().set_input_line(M6809_IRQ_LINE, HOLD_LINE);
}

/* TODO */
INTERRUPT_GEN_MEMBER(bfcobra_state::vblank_gen)
{
	m_vblank_irq = 1;
	update_irqs();
}

void bfcobra_state::bfcobra(machine_config &config)
{
	Z80(config, m_maincpu, Z80_XTAL);
	m_maincpu->set_addrmap(AS_PROGRAM, &bfcobra_state::z80_prog_map);
	m_maincpu->set_addrmap(AS_IO, &bfcobra_state::z80_io_map);
	m_maincpu->set_vblank_int("screen", FUNC(bfcobra_state::vblank_gen));

	MC6809(config, m_audiocpu, M6809_XTAL); // MC6809P
	m_audiocpu->set_addrmap(AS_PROGRAM, &bfcobra_state::m6809_prog_map);
	m_audiocpu->set_periodic_int(FUNC(bfcobra_state::timer_irq), attotime::from_hz(1000));

	NVRAM(config, "nvram", nvram_device::DEFAULT_ALL_0);


	/* TODO */
	screen_device &screen(SCREEN(config, "screen", SCREEN_TYPE_RASTER));
	screen.set_refresh_hz(50);
	screen.set_vblank_time(ATTOSECONDS_IN_USEC(2500) /* not accurate */);
	screen.set_size(512, 256);
	screen.set_visarea_full();
	screen.set_screen_update(FUNC(bfcobra_state::screen_update_bfcobra));

	PALETTE(config, m_palette).set_entries(256);

	ramdac_device &ramdac(RAMDAC(config, "ramdac", 0, m_palette)); // MUSIC Semiconductor TR9C1710 RAMDAC or equivalent
	ramdac.set_addrmap(0, &bfcobra_state::ramdac_map);
	ramdac.set_split_read(1);

	SPEAKER(config, "mono").front_center();

	AY8910(config, "aysnd", M6809_XTAL / 4).add_route(ALL_OUTPUTS, "mono", 0.20);

	UPD7759(config, m_upd7759).add_route(ALL_OUTPUTS, "mono", 0.40);

	/* ACIAs */
	ACIA6850(config, m_acia6850_0, 0);
	m_acia6850_0->txd_handler().set(m_acia6850_1, FUNC(acia6850_device::write_rxd));
	m_acia6850_0->irq_handler().set(FUNC(bfcobra_state::z80_acia_irq));

	ACIA6850(config, m_acia6850_1, 0);
	m_acia6850_1->txd_handler().set(m_acia6850_0, FUNC(acia6850_device::write_rxd));

	ACIA6850(config, m_acia6850_2, 0);
	m_acia6850_2->txd_handler().set(FUNC(bfcobra_state::data_acia_tx_w));
	m_acia6850_2->irq_handler().set(FUNC(bfcobra_state::m6809_data_irq));

	clock_device &acia_clock(CLOCK(config, "acia_clock", 31250*16)); // What are the correct ACIA clocks ?
	acia_clock.signal_handler().set(FUNC(bfcobra_state::write_acia_clock));

	METERS(config, m_meters, 0).set_number(8);
}

/***************************************************************************
  Cobra II Jamma.

  This has a Z180 and no 6809

***************************************************************************/
class bfcobjam_state : public driver_device
{
public:
	bfcobjam_state(const machine_config &mconfig, device_type type, const char *tag) :
		driver_device(mconfig, type, tag),
		m_maincpu(*this, "maincpu"),
		m_acia6850_0(*this, "acia6850_0"),
		m_screen(*this, "screen"),
		m_palette(*this, "palette"),
		m_aux_upd7759(*this, "upd"),
		m_upd7759_int(*this, "updint"),
		m_ym2413(*this, "ymsnd"),
		m_i2cmem(*this, "i2cmem"),
		m_dm01(*this, "dm01"),
		m_work_ram(*this, "work_ram"),
		m_video_ram(*this, "video_ram")
	{
	}

	void init_bfcobjam();
	void bfcobjam(machine_config &config);
	void bfcobjam_with_dmd(machine_config &config);

protected:
	virtual void machine_start() override;
	virtual void machine_reset() override;
	virtual void video_start() override;

private:
	uint8_t m_bank_data[4];
	uint8_t m_rompage;
	uint8_t m_h_scroll;
	uint8_t m_v_scroll;
	uint8_t m_flip_8;
	uint8_t m_flip_22;
	uint8_t m_videomode;
	uint8_t m_data_r;
	uint8_t m_data_t;
	uint8_t m_port_0;
	int m_irq_state;
	int m_acia_irq;
	int m_scanline_irq;
	int m_blitter_irq;
	int m_global_volume;
	uint8_t dm_shift_data ;
	int dm_shift = 0 ;
	int dm_last_data = 0 ;

	uint8_t m_z8s180_int;
	uint8_t m_z8s180_inten;
	uint8_t m_col4bit[16];
	uint8_t m_col3bit[16];
	uint8_t m_col8bit[256];
	uint8_t m_col7bit[256];
	uint8_t m_col6bit[256];
	struct bf_blitter_t m_blitter;
	emu_timer *m_scanline_timer;
	uint8_t m_genio ;
	required_device<cpu_device> m_maincpu;
	required_device<acia6850_device> m_acia6850_0;
	required_device<screen_device> m_screen;
	required_device<palette_device> m_palette;
	optional_device<upd7759_device> m_aux_upd7759;
	required_device<upd7759_device> m_upd7759_int;
	required_device<ym2413_device> m_ym2413;
	required_device<i2c_24c08_device> m_i2cmem;
	optional_device<bfm_dm01_device> m_dm01;
	required_shared_ptr<uint8_t> m_work_ram;
	required_shared_ptr<uint8_t> m_video_ram;

	uint8_t chipset_r(offs_t offset);
	void chipset_w(offs_t offset, uint8_t data);
	void rombank_w(uint8_t data);
	void upd7759_w(uint8_t data);
	uint8_t aux_upd7759_r();
	void aux_upd7759_w(uint8_t data);
	void output0_w(uint8_t data);
	uint8_t input0_r();
	uint8_t input1_r();
	DECLARE_WRITE_LINE_MEMBER(z8s180_acia_irq);
	DECLARE_WRITE_LINE_MEMBER(data_acia_tx_w);
	DECLARE_WRITE_LINE_MEMBER(write_acia_clock);
	DECLARE_WRITE_LINE_MEMBER(upd7759_generate_dreq );
	uint32_t screen_update_bfcobjam(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect);
	INTERRUPT_GEN_MEMBER(timer_irq);
	TIMER_CALLBACK_MEMBER( scanline_callback ) ;
	void RunBlit();
	void update_irqs();
	void genio_w( uint8_t data ) ;
	void dotmatrix_w( uint8_t data );
	inline uint8_t* blitter_get_addr(uint32_t addr);
	inline void z8s180_bank(int num, int data);

	void ramdac_map(address_map &map);
	void z8s180_io_map(address_map &map);
	void z8s180_prog_map(address_map &map);
};

void bfcobjam_state::init_bfcobjam()
{
	m_rompage = 0 ;
	m_bank_data[0] = 0;
	m_bank_data[1] = 0;
	m_bank_data[2] = 0;
	m_bank_data[3] = 0;

	/* Fixed 16kB ROM region */
	membank("bank4")->set_base(memregion("user1")->base());
	membank("bank1")->set_base(memregion("user1")->base()+0x04000);
	membank("bank2")->set_base(memregion("user1")->base()+0x08000);
	membank("bank3")->set_base(memregion("user1")->base()+0x0c000);

	m_scanline_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(bfcobjam_state::scanline_callback),this));

	/* Finish this */
	save_item(NAME(m_data_r));
	save_item(NAME(m_data_t));
	save_item(NAME(m_h_scroll));
	save_item(NAME(m_v_scroll));
	save_item(NAME(m_flip_8));
	save_item(NAME(m_flip_22));
	save_item(NAME(m_z8s180_int));
	save_item(NAME(m_z8s180_inten));
	save_item(NAME(m_bank_data));
	save_item(NAME(m_genio));
	save_item(NAME(m_global_volume));

}

/*
    This is based this on the Slipstream technical reference manual.
    The Flare One blitter is a simpler design with slightly different parameters
    and will require hardware tests to figure everything out correctly.
*/
void bfcobjam_state::RunBlit()
{
#define BLITPRG_READ(x)     blitter.x = *(blitter_get_addr(blitter.program.addr++))

	struct bf_blitter_t &blitter = m_blitter;
	int cycles_used = 0;


	do
	{
		uint8_t srcdata = 0;
		uint8_t dstdata = 0;

		/* Read the blitter command */
		BLITPRG_READ(source.as8bit.addr0);
		BLITPRG_READ(source.as8bit.addr1);
		BLITPRG_READ(source.as8bit.addr2);
		BLITPRG_READ(dest.as8bit.addr0);
		BLITPRG_READ(dest.as8bit.addr1);
		BLITPRG_READ(dest.as8bit.addr2);
		BLITPRG_READ(modectl);
		BLITPRG_READ(compfunc);
		BLITPRG_READ(outercnt);
		BLITPRG_READ(innercnt);
		BLITPRG_READ(step);
		BLITPRG_READ(pattern);

#if 0
		/* This debug is now wrong ! */
		if (DEBUG_BLITTER)
		{
			osd_printf_debug("\n%s:Blitter: Running command from 0x%.5x\n\n", device->machine().describe_context(), blitter.program.addr - 12);
			osd_printf_debug("Command Reg         %.2x",   blitter.command);
			osd_printf_debug("     %s %s %s %s %s %s %s\n",
				blitter.command & CMD_RUN ? "RUN" : "     ",
				blitter.command & CMD_COLST ? "COLST" : "     ",
				blitter.command & CMD_PARRD ? "PARRD" : "     ",
				blitter.command & CMD_SRCUP ? "SRCUP" : "     ",
				blitter.command & CMD_DSTUP ? "DSTUP" : "     ");

			osd_printf_debug("Src Address Byte 0  %.2x\n", blitter.source.as8bit.addr0);
			osd_printf_debug("Src Address Byte 1  %.2x\n", blitter.source.as8bit.addr1);
			osd_printf_debug("Src Control         %.2x\n", blitter.source.as8bit.addr2);
			osd_printf_debug("  Src Address       %.5x\n", blitter.source.addr & 0xfffff);
			osd_printf_debug("Dest Address Byte 0 %.2x\n", blitter.dest.as8bit.addr0);
			osd_printf_debug("Dest Address Byte 1 %.2x\n", blitter.dest.as8bit.addr1);
			osd_printf_debug("Dest Control        %.2x\n", blitter.dest.as8bit.addr2);
			osd_printf_debug("  Dst. Address      %.5x\n", blitter.dest.addr & 0xfffff);
			osd_printf_debug("Mode Control        %.2x",   blitter.modectl);
			osd_printf_debug("     %s\n", blitter.modectl & MODE_BITTOBYTE ? "BIT_TO_BYTE" : "");

			osd_printf_debug("Comp. and LFU       %.2x\n", blitter.compfunc);
			osd_printf_debug("Outer Loop Count    %.2x (%d)\n", blitter.outercnt, blitter.outercnt);
			osd_printf_debug("Inner Loop Count    %.2x (%d)\n", blitter.innercnt, blitter.innercnt);
			osd_printf_debug("Step Value          %.2x\n", blitter.step);
			osd_printf_debug("Pattern Byte        %.2x\n", blitter.pattern);
		}
#endif

		/* Ignore these writes */
		if (blitter.dest.addr == 0)
			return;

		/* Begin outer loop */
		for (;;)
		{
			uint8_t innercnt = blitter.innercnt;
			dstdata = blitter.pattern;

			if (blitter.command & CMD_LINEDRAW)
			{
				do
				{
					if (blitter.modectl & MODE_YFRAC)
					{
						if (blitter.modectl & MODE_SSIGN )
							blitter.dest.as8bit.addr0--;
						else
							blitter.dest.as8bit.addr0++;
					}
					else
					{
						if (blitter.modectl & MODE_DSIGN )
							blitter.dest.as8bit.addr1--;
						else
							blitter.dest.as8bit.addr1++;
					}
					if( blitter.source.as8bit.addr0 < blitter.step )
					{
						blitter.source.as8bit.addr0 -= blitter.step ;
						blitter.source.as8bit.addr0 += blitter.source.as8bit.addr1;

						if ( blitter.modectl & MODE_YFRAC )
						{
							if (blitter.modectl & MODE_DSIGN )
								blitter.dest.as8bit.addr1--;
							else
								blitter.dest.as8bit.addr1++;
						}
						else
						{
							if (blitter.modectl & MODE_SSIGN )
								blitter.dest.as8bit.addr0--;
							else
								blitter.dest.as8bit.addr0++;
						}
					}
					else
					{
						blitter.source.as8bit.addr0 -= blitter.step;
					}

					*blitter_get_addr( blitter.dest.addr) = blitter.pattern;
					cycles_used++;

				} while (--innercnt);
			}
			else do
			{
				uint8_t   inhibit = 0;

				/* TODO: Set this correctly */
				uint8_t   result = blitter.pattern;

				if (LOOPTYPE == 3 && innercnt == blitter.innercnt)
				{
					srcdata = *(blitter_get_addr( blitter.source.addr & 0xfffff));
					blitter.source.as16bit.loword++;
					cycles_used++;
				}

				/* Enable source address read and increment? */
				if (!(blitter.modectl & (MODE_BITTOBYTE | MODE_PALREMAP)))
				{
					if (LOOPTYPE == 0 || LOOPTYPE == 1)
					{
						srcdata = *(blitter_get_addr( blitter.source.addr & 0xfffff));
						cycles_used++;

						if (blitter.modectl & MODE_SSIGN)
							blitter.source.as16bit.loword-- ;
						else
							blitter.source.as16bit.loword++;

						result = srcdata;
					}
				}

				/* Read destination pixel? */
				if (LOOPTYPE == 0)
				{
					dstdata = *blitter_get_addr( blitter.dest.addr & 0xfffff);
					cycles_used++;
				}

				/* Inhibit depending on the bit selected by the inner count */

				/* Switch on comparator type? */
				if (blitter.modectl & MODE_BITTOBYTE)
				{
					inhibit = !(srcdata & (1 << (8 - innercnt)));
				}

				if (blitter.compfunc & CMPFUNC_BEQ)
				{
					if (srcdata == blitter.pattern)
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_LT)
				{
					if ((srcdata & 0xc0) > (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_EQ)
				{
					if ((srcdata & 0xc0) == (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}
				if (blitter.compfunc & CMPFUNC_GT)
				{
					if ((srcdata & 0xc0) < (dstdata & 0xc0))
					{
						inhibit = 1;

						/* TODO: Resume from inhibit? */
						if (blitter.command & CMD_COLST)
							return;
					}
				}

				/* Write the data if not inhibited */
				if (!inhibit)
				{
					if (blitter.modectl == MODE_PALREMAP)
					{
						/*
						    In this mode, the source points to a 256 entry lookup table.
						    The existing destination pixel is used as a lookup
						    into the table and the colours is replaced.
						*/
						uint8_t dest = *blitter_get_addr( blitter.dest.addr);
						uint8_t newcol = *(blitter_get_addr( (blitter.source.addr + dest) & 0xfffff));

						*blitter_get_addr( blitter.dest.addr) = newcol;
						cycles_used += 3;
					}
					else
					{
						uint8_t final_result = 0;

						if (blitter.compfunc & CMPFUNC_LOG3)
							final_result |= result & dstdata;

						if (blitter.compfunc & CMPFUNC_LOG2)
							final_result |= result & ~dstdata;

						if (blitter.compfunc & CMPFUNC_LOG1)
							final_result |= ~result & dstdata;

						if (blitter.compfunc & CMPFUNC_LOG0)
							final_result |= ~result & ~dstdata;

						*blitter_get_addr( blitter.dest.addr) = final_result;
						cycles_used++;
					}
				}

				/* Update destination address */
				if (blitter.modectl & MODE_DSIGN)
					blitter.dest.as16bit.loword--;
				else
					blitter.dest.as16bit.loword++;

			} while (--innercnt);

			if (!--blitter.outercnt)
			{
				break;
			}
			else
			{
				if (blitter.command & CMD_DSTUP)
					blitter.dest.as16bit.loword += blitter.step;

				if (blitter.command & CMD_SRCUP)
					blitter.source.as16bit.loword += blitter.step;

				if (blitter.command & CMD_PARRD)
				{
					BLITPRG_READ(innercnt);
					BLITPRG_READ(step);
					BLITPRG_READ(pattern);
				}
			}
		}

		/* Read next command header */
		BLITPRG_READ(command);

	} while (blitter.command  & CMD_RUN);

	/* Burn Z180 cycles while blitter is in operation */
	/* Don't burn for Z8S180 */
//  m_maincpu->spin_until_time(attotime::from_nsec( (1000000000 / Z8S180_XTAL)*cycles_used * 2 ) );
}

uint8_t* bfcobjam_state::blitter_get_addr(uint32_t addr)
{
	if (addr < 0x10000)
	{
		/* Is this region fixed? */
		return (uint8_t*)(memregion("user1")->base() + addr);
	}
	else if(addr < 0x20000)
	{
		addr &= 0xffff;
		addr += m_rompage * 0x10000 ;

		return (uint8_t*)(memregion("user1")->base() + addr );
	}
	else if (addr >= 0x20000 && addr < 0x40000)
	{
		return (uint8_t*)&m_video_ram[addr - 0x20000];
	}
	else
	{
		return (uint8_t*)&m_work_ram[addr - 0x40000];
	}
}

uint8_t bfcobjam_state::chipset_r(offs_t offset)
{
	uint8_t val = 0xff;

	switch(offset)
	{
		case 0:
		case 1:
		case 2:
		case 3:
		{
			val = m_bank_data[offset];
			break;
		}
		case 6:
		{
			/* TODO */
			val = m_scanline_irq << 4;
			break;
		}
		case 7:
		{
			m_scanline_irq = 0;
			val = 0x1;

			/* TODO */
			update_irqs();
			break;
		}
		case 0x1C:
		{
			/* Blitter status ? */
			val = ioport("CJIN3")->read();
			break;
		}
		case 0x20:
		{
			/* Seems correct - used during RLE pic decoding */
			val = m_blitter.dest.as8bit.addr0;
			break;
		}
		case 0x22:
		{
			val = 0x40 | ioport("CJIN2")->read() | ( m_upd7759_int->busy_r() ? 0x20 : 0 ) ;
			break;
		}
		default:
		{
			osd_printf_debug("Flare One unknown read: 0x%.2x (PC:0x%.4x)\n", offset, m_maincpu->pcbase());
		}
	}

	return val;
}

void bfcobjam_state::chipset_w(offs_t offset, uint8_t data)
{
	switch (offset)
	{
		case 0x00:
		case 0x01:
		case 0x02:
		case 0x03:
		{
			popmessage("%x: Unusual bank access (%x)\n", m_maincpu->pcbase(), data);

			data &= 0x3f;
			m_bank_data[offset] = data;
			z8s180_bank(offset, data);
			break;
		}

		case 0x07:
			m_scanline_timer->adjust(m_screen->time_until_pos(data));
			break ;

		case 0x08:
		{
			m_flip_8 = data;
			break;
		}
		case 9:
			m_videomode = data;
			break;

		case 0x0B:
		{
			m_h_scroll = data;
			break;
		}
		case 0x0C:
		{
			m_v_scroll = data;
			break;
		}
		case 0x0E:
		{
			m_col4bit[5] = data;
			m_col3bit[5] = data;
			m_col3bit[5 + 8] = data;
			break;
		}
		case 0x0f:
		{
			m_col4bit[6] = data;
			m_col3bit[6] = data;
			m_col3bit[6 + 8] = data;
			break;
		}
		case 0x18:
		{
			m_blitter.program.as8bit.addr0 = data;
			break;
		}
		case 0x19:
		{
			m_blitter.program.as8bit.addr1 = data;
			break;
		}
		case 0x1A:
		{
			m_blitter.program.as8bit.addr2 = data;
			break;
		}
		case 0x20:
		{
			m_blitter.command = data;

			if (data & CMD_RUN)
				RunBlit();
			else
				osd_printf_debug("Blitter stopped by IO.\n");

			break;
		}
		case 0x22:
		{
			m_flip_22 = data;
			genio_w( data ) ;
			break;
		}
		default:
		{
			osd_printf_debug("Flare One unknown write: 0x%.2x with 0x%.2x (PC:0x%.4x)\n", offset, data, m_maincpu->pcbase());
		}
	}
}

uint32_t bfcobjam_state::screen_update_bfcobjam(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	int x, y;
	uint8_t  *src;
	uint32_t *dest;
	uint32_t offset;
	uint8_t *hirescol;
	uint8_t *lorescol;


	/* Select screen has to be programmed into two registers */
	/* No idea what happens if the registers are different */
	if (m_flip_8 & 0x40 && m_flip_22 & 0x40)
		offset = 0x10000;
	else
		offset = 0;

	if(m_videomode & 0x20)
	{
		hirescol = m_col3bit;
		lorescol = m_col7bit;
	}
	else if(m_videomode & 0x40)
	{
		hirescol = m_col4bit;
		lorescol = m_col6bit;
	}
	else
	{
		hirescol = m_col4bit;
		lorescol = m_col8bit;
	}

	for (y = cliprect.top(); y <= cliprect.bottom(); ++y)
	{
		uint16_t y_offset = (y + m_v_scroll) * 256;
		src = &m_video_ram[offset + y_offset];
		dest = &bitmap.pix32(y);

		for (x = cliprect.left(); x <= cliprect.right() / 2; ++x)
		{
			uint8_t x_offset = x + m_h_scroll;
			uint8_t pen = *(src + x_offset);

			if ( ( m_videomode & 0x81 ) == 1 || (m_videomode & 0x80 && pen & 0x80) )
			{
				*dest++ = m_palette->pen(hirescol[pen & 0x0f]);
				*dest++ = m_palette->pen(hirescol[(pen >> 4) & 0x0f]);
			}
			else
			{
				*dest++ = m_palette->pen(lorescol[pen]);
				*dest++ = m_palette->pen(lorescol[pen]);
			}
		}
	}

	return 0;
}

void bfcobjam_state::ramdac_map(address_map &map)
{
	map(0x000, 0x3ff).rw("ramdac", FUNC(ramdac_device::ramdac_pal_r), FUNC(ramdac_device::ramdac_rgb666_w));
}


void bfcobjam_state::z8s180_bank(int num, int data)
{
	static const char * const bank_names[] = { "bank4","bank1", "bank2", "bank3" };

	if (data < 0x04)
	{
		membank(bank_names[num])->set_base(memregion("user1")->base() + (data*0x4000));
	}
	else if (data < 0x08)
	{
		uint32_t offset ;
		offset = m_rompage * 0x10000 ;
		offset += (data-4) * 0x4000 ;

		membank(bank_names[num])->set_base(memregion("user1")->base() + offset);
	}
	else if (data < 0x10)
	{
		membank(bank_names[num])->set_base(&m_video_ram[(data - 0x08) * 0x4000]);
	}
	else
	{
		membank(bank_names[num])->set_base(&m_work_ram[(data - 0x10) * 0x4000]);
	}
}

void bfcobjam_state::update_irqs()
{
	int newstate = m_blitter_irq || m_scanline_irq || m_acia_irq;

	if (newstate != m_irq_state)
	{
		m_irq_state = newstate;
		m_maincpu->set_input_line(0, m_irq_state ? ASSERT_LINE : CLEAR_LINE);
	}
}

TIMER_CALLBACK_MEMBER( bfcobjam_state::scanline_callback )
{
	m_scanline_timer->adjust(m_screen->time_until_pos(0));
	m_scanline_irq = 1;
	update_irqs();
}

WRITE_LINE_MEMBER(bfcobjam_state::z8s180_acia_irq)
{
	m_acia_irq = state;
	update_irqs();
}


void bfcobjam_state::rombank_w(uint8_t data)
{
	m_rompage = data ;

	membank("bank5")->set_entry(m_rompage & 0x0f);
}

/*
  This parallel port is used for bit-bashing the following devices.
    1. Dot matrix display
    2. I2C 24C08
  The bit use is as follows:
    b0 =
    b1 =
    b2 = Dot matrix clock
    b3 = Dot matrix data
    b4 = Dot matrix reset
    b5 = I2C SDA
    b6 = I2C SCL
    b7 =
*/
void bfcobjam_state::output0_w(uint8_t data)
{
	uint8_t changed = m_port_0 ^ data ;
	m_port_0 = data ;

	dotmatrix_w( data ) ;

	//cobra handling of scl and sda is wrong but devices handle it ok
	//unfortunately mame emulation of i2c 24c08 doesn't :(
	//we frig the driving of scl/sda to correct this
	changed &= 0x60 ;

	if( changed == 0x60 )
	{
		if( data & 0x40 )
		{
			m_i2cmem->write_scl(!(BIT(data, 6)));
			m_i2cmem->write_sda(!(BIT(data, 5)));
		}
		else
		{
			m_i2cmem->write_sda(!(BIT(data, 5)));
			m_i2cmem->write_scl(!(BIT(data, 6)));
		}
	}
	else
	{
		m_i2cmem->write_scl(!(BIT(data, 6)));
		m_i2cmem->write_sda(!(BIT(data, 5)));
	}
}

void bfcobjam_state::dotmatrix_w( uint8_t data )
{
	if( m_dm01 )
	{
		data &= 0x1c ;

		if( data != dm_last_data )
		{
			if( data & 0x10 )
			{
			}
			else
			{
				m_dm01->reset() ;
				dm_shift=0;
				dm_shift_data=0;
			}
			if( !(data & 4 ) && ( dm_last_data & 4 ) && data & 0x10 ) // clock is low but was high and out of reset
			{
				dm_shift_data <<= 1 ;
				if( !(data & 0x08 ))
				{
					dm_shift_data |=1 ;
				}
				dm_shift++;
				if( dm_shift == 8 )
				{
					dm_shift=0;
					m_dm01->writedata( dm_shift_data ) ;
				}
			}
			dm_last_data = data ;
		}
	}
}

uint8_t bfcobjam_state::input0_r()
{
	return ioport("CJIN0")->read() | (m_i2cmem->read_sda() ? 0x80 : 0x00);
}

uint8_t bfcobjam_state::input1_r()
{
	return ioport("CJIN1")->read();
}

void bfcobjam_state::aux_upd7759_w(uint8_t data)
{
	if( m_aux_upd7759 )
	{
		m_aux_upd7759->set_rom_bank((data>>2)&3);

		m_aux_upd7759->port_w((data>>4)&0xf);

		m_aux_upd7759->md_w(1);

		m_aux_upd7759->reset_w(BIT(data, 0));

		m_aux_upd7759->start_w(!BIT(data, 1));
	}
}

uint8_t bfcobjam_state::aux_upd7759_r()
{
	if( m_aux_upd7759 )
	{
		return m_aux_upd7759->busy_r();
	}
	else
	{
		return 0 ;
	}
}

void bfcobjam_state::genio_w( uint8_t data )
{
	//bit 0 = ??
	//bit 1 = upd7759 start
	//bit 2 = upd7759 reset
	//bit 3 = ??
	//bit 4 = digital volume clock
	//bit 5 = digital volume up/down
	//bit 6 = ??
	//bit 7 = ??

	uint8_t changed = m_genio ^ data ;

	m_genio = data ;

	if ( changed & 0x10)
	{ // digital volume clock line changed
		if ( !(data & 0x10) )
		{ // changed from high to low,
			if ( data & 0x20 )
			{
				if ( m_global_volume < 31 ) m_global_volume++; //0-31 expressed as 1-32
			}
			else
			{
				if ( m_global_volume > 0  ) m_global_volume--;
			}

			{
				if (m_ym2413)
				{
					float percent = (32 - m_global_volume) / 32.0f;

					m_ym2413->set_output_gain(ALL_OUTPUTS, percent);
					m_upd7759_int->set_output_gain(ALL_OUTPUTS, percent);
					if( m_aux_upd7759 )
					{
						m_aux_upd7759->set_output_gain(ALL_OUTPUTS, percent);
					}
				}
			}
		}
	}

	//bits 1 and 2 are for upd7759
	m_upd7759_int->md_w(0);
	m_upd7759_int->reset_w(!BIT(data,2));
	m_upd7759_int->start_w(BIT(data,1));
}

void bfcobjam_state::upd7759_w(uint8_t data)
{
	m_upd7759_int->port_w(data);
}

WRITE_LINE_MEMBER( bfcobjam_state::upd7759_generate_dreq )
{
	if( state )
	{
		m_maincpu->set_input_line(Z180_INPUT_LINE_DREQ0, ASSERT_LINE );
	}
	else
	{
		m_maincpu->set_input_line(Z180_INPUT_LINE_DREQ0, CLEAR_LINE );
	}
}

void bfcobjam_state::video_start()
{
	memcpy(m_col4bit, col4bit_default, sizeof(m_col4bit));
	memcpy(m_col3bit, col3bit_default, sizeof(m_col3bit));
	for (int i = 0; i < 256; ++i)
	{
		uint8_t col;

		m_col8bit[i] = i;
		col = i & 0x7f;
		col = (col & 0x1f) | (col76index[ ( (col & 0x60) >> 5 ) & 3] << 5);
		m_col7bit[i] = col;

		col = (col & 3) | (col76index[( (col & 0x0c) >> 2) & 3] << 2 ) |
				(col76index[( (col & 0x30) >> 4) & 3] << 5 );
		m_col6bit[i] = col;
	}
}

/***************************************************************************

    Cobra II Z8S180 Memory Map

***************************************************************************/

void bfcobjam_state::z8s180_prog_map(address_map &map)
{
	map(0x00000, 0x03fff).bankrw("bank4");
	map(0x04000, 0x07fff).bankrw("bank1");
	map(0x08000, 0x0bfff).bankrw("bank2");
	map(0x0c000, 0x0ffff).bankrw("bank3");
	map(0x10000, 0x1ffff).bankrw("bank5");
	map(0x20000, 0x3ffff).ram().share(m_video_ram);
	map(0x40000, 0xfffff).ram().share(m_work_ram);
}

void bfcobjam_state::z8s180_io_map(address_map &map)
{
	map.global_mask(0xff);
	map(0x00, 0x23).rw(FUNC(bfcobjam_state::chipset_r), FUNC(bfcobjam_state::chipset_w));
	map(0x24, 0x25).w(m_acia6850_0, FUNC(acia6850_device::write));
	map(0x26, 0x27).r(m_acia6850_0, FUNC(acia6850_device::read));
	map(0x2c, 0x2c).w(FUNC(bfcobjam_state::rombank_w));
	map(0x30, 0x30).w(FUNC(bfcobjam_state::upd7759_w));
	map(0x50, 0x50).w("ramdac", FUNC(ramdac_device::index_w));
	map(0x51, 0x51).rw("ramdac", FUNC(ramdac_device::pal_r), FUNC(ramdac_device::pal_w));
	map(0x52, 0x52).w("ramdac", FUNC(ramdac_device::mask_w));
	map(0x53, 0x53).w("ramdac", FUNC(ramdac_device::index_r_w));
	map(0x80, 0x87).r(FUNC(bfcobjam_state::input1_r));
	map(0x88, 0x8f).r(FUNC(bfcobjam_state::input0_r));
	map(0x88, 0x8f).w(FUNC(bfcobjam_state::output0_w));
	map(0x90, 0x97).r(FUNC(bfcobjam_state::aux_upd7759_r));
	map(0x90, 0x97).w(m_ym2413,FUNC(ym2413_device::write));
	map(0x98, 0x9f).w(FUNC(bfcobjam_state::aux_upd7759_w));
}


static INPUT_PORTS_START( brkball )
	PORT_START("CJIN0")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON4 ) PORT_PLAYER(2) PORT_NAME("P2 FIRE C")
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_START1 )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT ) PORT_PLAYER(2)
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_COIN1 )

	PORT_START("CJIN1")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_START2 )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_NAME("P1 Left Flip")
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_BUTTON2 ) PORT_NAME("P1 Right Flip")
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )

	PORT_START("CJIN2")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P1 Left Flip")
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON2 ) PORT_PLAYER(2)  PORT_NAME("P1 Right Flip")
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_SERVICE1 )
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_COIN2 )

	PORT_START("CJIN3")
	PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_TILT ) PORT_NAME("TEST") PORT_CODE(KEYCODE_F1)
	PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_UNKNOWN )
	PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_UNKNOWN )
INPUT_PORTS_END


void bfcobjam_state::machine_start()
{
	membank("bank5")->configure_entries(0, 0x10, memregion("user1")->base(), 0x10000);
	membank("bank5")->set_entry(1);
}

void bfcobjam_state::machine_reset()
{
	for (unsigned int pal = 0; pal < 256; ++pal)
	{
		m_palette->set_pen_color(pal, pal3bit((pal>>5)&7), pal3bit((pal>>2)&7), pal2bit(pal&3));
	}

	m_rompage = 0 ;
	m_bank_data[0] = 0;
	m_bank_data[1] = 0;
	m_bank_data[2] = 0;
	m_bank_data[3] = 0;

	m_genio = 0 ;
	m_global_volume = 0;

	m_ym2413->reset();


	m_irq_state = m_blitter_irq = m_scanline_irq = m_acia_irq = 0;

	m_scanline_timer->adjust(m_screen->time_until_pos(0));

	if( m_dm01 )
		m_dm01->reset() ;
}

WRITE_LINE_MEMBER(bfcobjam_state::write_acia_clock)
{
	m_acia6850_0->write_txc(state);
	m_acia6850_0->write_rxc(state);
}

void bfcobjam_state::bfcobjam(machine_config &config)
{
	Z8S180(config, m_maincpu, Z8S180_XTAL);
	m_maincpu->set_addrmap(AS_PROGRAM, &bfcobjam_state::z8s180_prog_map);
	m_maincpu->set_addrmap(AS_IO, &bfcobjam_state::z8s180_io_map);

	/* TODO */
	SCREEN(config, m_screen, SCREEN_TYPE_RASTER);
	m_screen->set_refresh_hz(50);
	m_screen->set_vblank_time(ATTOSECONDS_IN_USEC(2500) /* not accurate */);
	m_screen->set_size(512, 256);
	m_screen->set_visarea_full();
	m_screen->set_screen_update(FUNC(bfcobjam_state::screen_update_bfcobjam));

	PALETTE(config, m_palette).set_entries(256);

	ramdac_device &ramdac(RAMDAC(config, "ramdac", 0, m_palette)); // MUSIC Semiconductor TR9C1710 RAMDAC or equivalent
	ramdac.set_addrmap(0, &bfcobjam_state::ramdac_map);
	ramdac.set_split_read(1);

	SPEAKER(config, "mono").front_center();

	UPD7759(config, m_upd7759_int);
	m_upd7759_int->add_route(ALL_OUTPUTS, "mono", 0.40);
	m_upd7759_int->drq().set(FUNC(bfcobjam_state::upd7759_generate_dreq));

	YM2413(config, m_ym2413, XTAL(3'579'545)).add_route(ALL_OUTPUTS, "mono", 1.0);


	/* ACIAs */
	ACIA6850(config, m_acia6850_0, 0);
	m_acia6850_0->irq_handler().set(FUNC(bfcobjam_state::z8s180_acia_irq));

	clock_device &acia_clock(CLOCK(config, "acia_clock", 31250*16)); // What are the correct ACIA clocks ?
	acia_clock.signal_handler().set(FUNC(bfcobjam_state::write_acia_clock));

	I2C_24C08(config, m_i2cmem);
}

void bfcobjam_state::bfcobjam_with_dmd(machine_config &config)
{
	bfcobjam( config ) ;

	UPD7759(config, m_aux_upd7759);
	m_aux_upd7759->add_route(ALL_OUTPUTS, "mono", 0.40);

	BFM_DM01(config, m_dm01, 0);
}

/***************************************************************************

  Game driver(s)

  Note: Two different versions of each 6809 ROM exist: standard and protocol
  It appears sets can be a combination of disk images, 6809 and Z80 ROMs!

***************************************************************************/

ROM_START( inquiztr )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "inq6809", 0x08000, 0x08000, CRC(ae996600) SHA1(f360399e77b81399d910770fa8106c196f04363c) )

	ROM_REGION( 0x20000, "user1", 0 )
	ROM_LOAD( "9576002.bin", 0x00000, 0x10000, CRC(5b8c8a04) SHA1(af5328fee79c370f45bff36f534aaf50964b6900) )

	ROM_REGION( 0x20000, "altuser1", 0 )
	ROM_LOAD( "9576028.bin", 0x10000, 0x10000, CRC(2d85682c) SHA1(baec47bff4b8beef5afbb737dc57b22bf93ebcf8) )

		// these look quite different.. (like they belong together) but booting with these gives a checksum error (banking?)
	ROM_LOAD( "inqvypp1", 0x00000, 0x010000, CRC(9bac8c6e) SHA1(15e24d60c2f3997e637694f60daa552b22628766) )
	ROM_LOAD( "inqvypp2", 0x10000, 0x010000, CRC(f9cd196c) SHA1(0ac31d87462cbee6f41e19aefe740d876910bdf5) )

	ROM_REGION( 0x1c2000, "user2", 0 )
	ROM_LOAD( "inqdisk.img", 0x000000, 0x1c2000, NO_DUMP )
ROM_END




ROM_START( escounts )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "esc12int", 0x08000, 0x08000, CRC(741a1fe6) SHA1(e741d0ae0d2f11036a358120381e4b0df4a560a1) )

	ROM_REGION( 0x10000, "altrevs", 0 )
	ROM_LOAD( "bfm_esc.bin", 0x08000, 0x08000, CRC(27acb5a5) SHA1(da50d650ab6456d61d0fb7f89247f2040b4bb9a8) )
	ROM_LOAD( "escint1b",    0x08000, 0x08000, CRC(fde413c9) SHA1(18724a1b771ba4c72e571c356591c5be32948d7a) )

	ROM_REGION( 0x10000, "user1", 0 )
	ROM_LOAD( "esccobpa", 0x00000, 0x10000, CRC(d8eadeb7) SHA1(9b94f1454e6a17bf8321b0ef4ddd0ed1a56150f7) )

	/* 95-100-207 */
	ROM_REGION( 0x190000, "user2", 0 )
	ROM_LOAD( "escdisk4.img", 0x000000, 0x190000, CRC(24156e0f) SHA1(e163daa8effadd93ace2bc2ba1af0f4a190abd7f) )
ROM_END

ROM_START( trebltop )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "95740078.bin", 0x08000, 0x08000, CRC(aca1980b) SHA1(3d4ed1dc545cc80f56d7daa13028fb10a12a718b) )

	ROM_REGION( 0x10000, "altrevs", 0 )
	ROM_LOAD( "ttopint", 0x08000, 0x08000, CRC(cc798f90) SHA1(c3f541ebbb3a2c29cc4f00bbd47e289bcff50ecb) )

	ROM_REGION( 0x20000, "user1", 0 )
	ROM_LOAD( "95760031.bin", 0x00000, 0x10000, CRC(8e75edb8) SHA1(0aaa3834d5ac20f92bdf1f2b8f1eb71854469cbe) )
	ROM_LOAD( "95760021.bin", 0x10000, 0x10000, CRC(f42016c0) SHA1(7067d018cb4bdcfba777267fb01cddf44e4216c3) )

	ROM_REGION( 0x1c2000, "user2", 0 )
	ROM_LOAD( "ttdisk1.img", 0x000000, 0x190000, CRC(b2003228) SHA1(5eb49f05137cdd404f22948d39aa79c1518c06eb) )

	ROM_REGION( 0x20000, "upd", 0 )
	ROM_LOAD( "95000172.bin", 0x00000, 0x10000, CRC(e85367a5) SHA1(695fd95ddeecdb16602f7b0f075cf5128a2fb808) )
	ROM_LOAD( "95000173.bin", 0x10000, 0x10000, CRC(8bda2c5e) SHA1(79aab5a2af7a5add5fe9132dc13bcc3705c6faf3) )
ROM_END

ROM_START( beeline )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "bln12int.a", 0x08000, 0x08000, CRC(cb97905e) SHA1(9725156bf64e53a56bc0f90795d4b07db41d059e) )

	ROM_REGION( 0x10000, "altrevs", 0 )
	ROM_LOAD( "beeint12", 0x8000, 0x008000, CRC(0a77d0be) SHA1(8e55e7b4eb85cc2521d8fdf7ede02131ed80372e) )


	ROM_REGION( 0x20000, "user1", 0 )
	ROM_LOAD( "blncob.pa", 0x00000, 0x10000, CRC(8abc0017) SHA1(ecf6e7a4021b35295eb9bb9aed1b88fff27ffbd1) )
	ROM_LOAD( "blncob.pb", 0x10000, 0x10000, CRC(feb121fe) SHA1(e83bfd6db00a3264e5076f257e261a1cb4605a83) )

	ROM_REGION( 0x1c2000, "user2", 0 )
	ROM_LOAD( "beedisk.img", 0x000000, 0x1c2000, NO_DUMP )
ROM_END

ROM_START( quizvadr )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "q6809.bin", 0x08000, 0x8000, CRC(a74dff10) SHA1(87578694a022dc3d7ade9cc76d387c1ae5fc74d9) )

	ROM_REGION( 0x10000, "altrevs", 0 )
	ROM_LOAD( "qvadrint", 0x08000, 0x08000, CRC(4c729943) SHA1(ab4e0fb6cfc66540ea1e9e36eebdf85f65c5fd2a) )

	ROM_REGION( 0x200000, "user1", 0 )
	ROM_LOAD( "5947011r.0", 0x000000, 0x80000, CRC(cac43c97) SHA1(3af529cd0f8ec57dd3596f5bca7b9c74cff171e4) )
	ROM_LOAD( "5947011r.1", 0x080000, 0x80000, CRC(120018dc) SHA1(cd153d2b7ed535b04dbcaf189d2fc96fe3c5b466) )
	ROM_LOAD( "5947011r.2", 0x100000, 0x80000, CRC(689b3b5a) SHA1(ea32d18acfd380de822efff4f2c95ce9873a33a2) )
	ROM_LOAD( "5947011r.3", 0x180000, 0x80000, CRC(c38dafeb) SHA1(d693387a5c3cde34c9d581f81a08a5fbc6f753f2) )

	ROM_REGION( 0x20000, "upd", 0 )
	ROM_LOAD( "185snd2.bin", 0x00000, 0x10000, CRC(e36eccc2) SHA1(cfd8ca4c71528ea4e229074016240681b6de37cd) )
	ROM_LOAD( "184snd1.bin", 0x10000, 0x10000, CRC(aac058e8) SHA1(39e59ad9524130fc3bd8d46e1aa78bc4daf04e39) )
ROM_END

ROM_START( qos )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "39360107.bin", 0x08000, 0x8000, CRC(20844655) SHA1(b67c7f7bbabf6d5139b8ad8cbb5f8cc3f28e9cc7) )

	ROM_REGION( 0x200000, "user1", 0 )
	ROM_LOAD( "95000338.rm0", 0x000000, 0x80000, CRC(96918aae) SHA1(849ce7b8eccc89c45aacc840a73935f95788a141) )
	ROM_LOAD( "95000339.rm1", 0x080000, 0x80000, CRC(b4c6dcc0) SHA1(56d8761766dfbd5b0e71f8c3ca575e88f1bc9929) )
	ROM_LOAD( "95000340.rm2", 0x100000, 0x80000, CRC(66d121fd) SHA1(ac65cc0ac6b0a41e78a3159c21ee44f765bdb5c8) )
	ROM_LOAD( "95000341.rm3", 0x180000, 0x80000, CRC(ef13658d) SHA1(240bc589900214eac79c91a531f254a9ac2f4ef6) )

	ROM_REGION( 0x20000, "upd", 0 )
	ROM_LOAD( "snd1_218.ic7", 0x00000, 0x10000, CRC(061f496d) SHA1(653d16454d909c034191813b37d14010da7258c6) )
	ROM_LOAD( "snd2_219.ic8", 0x10000, 0x10000, CRC(d7874a47) SHA1(5bbd4040c7c0299e8cc135e6c6cd05370b260e9b) )
ROM_END

ROM_START( qosa )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "qos_nondata_68f4.bin", 0x08000, 0x8000, CRC(5f40005a) SHA1(180017acf6b432bc135d1090099fdf99f1e3583a) )

	ROM_REGION( 0x200000, "user1", 0 )
	ROM_LOAD( "95000338.rm0", 0x000000, 0x80000, CRC(96918aae) SHA1(849ce7b8eccc89c45aacc840a73935f95788a141) )
	ROM_LOAD( "95000339.rm1", 0x080000, 0x80000, CRC(b4c6dcc0) SHA1(56d8761766dfbd5b0e71f8c3ca575e88f1bc9929) )
	ROM_LOAD( "95000340.rm2", 0x100000, 0x80000, CRC(66d121fd) SHA1(ac65cc0ac6b0a41e78a3159c21ee44f765bdb5c8) )
	ROM_LOAD( "95000341.rm3", 0x180000, 0x80000, CRC(ef13658d) SHA1(240bc589900214eac79c91a531f254a9ac2f4ef6) )

	ROM_REGION( 0x20000, "upd", 0 )
	ROM_LOAD( "snd1_218.ic7", 0x00000, 0x10000, CRC(061f496d) SHA1(653d16454d909c034191813b37d14010da7258c6) )
	ROM_LOAD( "snd2_219.ic8", 0x10000, 0x10000, CRC(d7874a47) SHA1(5bbd4040c7c0299e8cc135e6c6cd05370b260e9b) )
ROM_END

ROM_START( qosb )
	ROM_REGION( 0x10000, "audiocpu", 0 )
	ROM_LOAD( "95740599.bin", 0x08000, 0x8000, CRC(bf1e321f) SHA1(51f18620f22ba2a1b110954284ddf00614d51a0e) )

	ROM_REGION( 0x200000, "user1", 0 )
	ROM_LOAD( "rom0_306.bin", 0x000000, 0x80000, CRC(c26c8f83) SHA1(6949027e1fe241cbb2e1cbbce18e47bcb0d84550) )
	ROM_LOAD( "rom1_307.bin", 0x080000, 0x80000, CRC(94611c03) SHA1(81f545ff96ff3d44285315400da94d870c89f896) )
	ROM_LOAD( "rom2_308.bin", 0x100000, 0x80000, CRC(f5572726) SHA1(e109265c5571d21213a6f405a13459e7bc6699bc) )
	ROM_LOAD( "rom3_309.bin", 0x180000, 0x80000, CRC(1b5edfa8) SHA1(348488debd4aa52f064e351ed0c082274da1db2b) )

	ROM_REGION( 0x20000, "upd", 0 )
	ROM_LOAD( "snd1_218.ic7", 0x00000, 0x10000, CRC(061f496d) SHA1(653d16454d909c034191813b37d14010da7258c6) )
	ROM_LOAD( "snd2_219.ic8", 0x10000, 0x10000, CRC(d7874a47) SHA1(5bbd4040c7c0299e8cc135e6c6cd05370b260e9b) )

	/* there is a set close to this with

	    ROM_LOAD( "qosrom0", 0x0000, 0x080000, CRC(4f150634) SHA1(beb1d3c212b189f3baa08fe454e83f30108be08e) )
	    qosrom1 = rom1_307.bin          qosb       A Question of Sport (39-960-089)
	    ROM_LOAD( "qosrom2", 0x0000, 0x080000, CRC(c39737db) SHA1(ccfdb19dab3af064db44e6022248aef98749bc97) )
	    ROM_LOAD( "qosrom3", 0x0000, 0x080000, CRC(785b8ff9) SHA1(61b31e0e60c31ecb4b179bfe008a96155d939709) )
	    ROM_LOAD( "qossnd1", 0x0000, 0x010000, CRC(888a29f8) SHA1(0e5aa9db54e783708ece1e8c7bffb10d994ab384) )

	   but it simply looks like a bad dump, rom3 is an alt 'rom 2' and the others are mostly the same as qosb
	*/

ROM_END

/*  The dot matrix software "ledv1.bin" is development software and not the release version.
    Everything appears to work correctly except there are issues when the machine first starts with the
    display showing a strange animation.
*/
ROM_START( brkball )
	ROM_REGION( 0x200000, "user1", 0 )
	ROM_LOAD( "95000352.bin", 0x000000, 0x80000, CRC(8daad60a) SHA1(10ac31b7791c2215d0561972ae63e1405361b908) )
	ROM_LOAD( "95000353.bin", 0x080000, 0x80000, CRC(8e7b84ed) SHA1(b5e9ffe08eabe1446aa583fb85548451d944e811) )

	ROM_REGION( 0x80000, "upd", 0 )
	ROM_LOAD( "sounds.bin", 0x00000, 0x80000, CRC(4e84402d) SHA1(f6056669f005d8790331be5b0f34d9441190b120) )

	ROM_REGION( 0x20000, "dm01:matrix", 0 )
	ROM_LOAD("ledv1.bin",  0x00000, 0x10000, CRC(ea918cb9) SHA1(9e7047613cf1cb4b9a7fefb8a02d8479a7b09e6a))
ROM_END

GAME( 1989, inquiztr, 0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "Inquizitor",                              MACHINE_NOT_WORKING )
GAME( 1990, escounts, 0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "Every Second Counts (39-360-053)",        MACHINE_IMPERFECT_GRAPHICS )
GAME( 1991, trebltop, 0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "Treble Top (39-360-070)",                 MACHINE_IMPERFECT_GRAPHICS )
GAME( 1991, beeline,  0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "Beeline (39-360-075)",                    MACHINE_NOT_WORKING | MACHINE_IMPERFECT_GRAPHICS )
GAME( 1991, quizvadr, 0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "Quizvaders (39-360-078)",                 MACHINE_IMPERFECT_GRAPHICS )
GAME( 1992, qos,      0,   bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "A Question of Sport (set 1, 39-960-107)", MACHINE_IMPERFECT_GRAPHICS )
GAME( 1992, qosa,     qos, bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "A Question of Sport (set 2, 39-960-099)", MACHINE_IMPERFECT_GRAPHICS )
GAME( 1992, qosb,     qos, bfcobra,          bfcobra, bfcobra_state, init_bfcobra, ROT0, "BFM",      "A Question of Sport (set 3, 39-960-089)", MACHINE_IMPERFECT_GRAPHICS )
GAMEL(1994, brkball,  0,   bfcobjam_with_dmd,brkball, bfcobjam_state,init_bfcobjam,ROT0, "BFM/ATOD", "Break Ball",                              MACHINE_IMPERFECT_GRAPHICS, layout_brkball )