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


 NES/Famicom cartridge emulation for Multigame Carts PCBs


 Here we emulate several PCBs used in multigame pirate carts (not MMC-3 based)

 TODO: Investigate further Gunsmoke on mc_8et40 and mc_2gn91. Both exhibit the
 same bug where enemies/barrels don't appear until they are halfway down the
 screen. They are both almost the same minor hack of the US release.

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


#include "emu.h"
#include "multigame.h"


#ifdef NES_PCB_DEBUG
#define VERBOSE 1
#else
#define VERBOSE 0
#endif

#define LOG_MMC(x) do { if (VERBOSE) logerror x; } while (0)


//-------------------------------------------------
//  constructor
//-------------------------------------------------

DEFINE_DEVICE_TYPE(NES_ACTION52,       nes_action52_device,       "nes_action52",       "NES Cart Action 52 PCB")
DEFINE_DEVICE_TYPE(NES_CALTRON6IN1,    nes_caltron6in1_device,    "nes_caltron6in1",    "NES Cart Caltron 6 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_CALTRON9IN1,    nes_caltron9in1_device,    "nes_caltron9in1",    "NES Cart Caltron 9 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_RUMBLESTATION,  nes_rumblestat_device,     "nes_rumblestat",     "NES Cart Rumblestation PCB")
DEFINE_DEVICE_TYPE(NES_SVISION16,      nes_svision16_device,      "nes_svision16",      "NES Cart Supervision 16 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_FARID_UNROM,    nes_farid_unrom_device,    "nes_farid_unrom",    "NES Cart Farid UNROM 8 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_KN42,           nes_kn42_device,           "nes_kn42",           "NES Cart KN-42 PCB")
DEFINE_DEVICE_TYPE(NES_N625092,        nes_n625092_device,        "nes_n625092",        "NES Cart N625092 PCB")
DEFINE_DEVICE_TYPE(NES_A65AS,          nes_a65as_device,          "nes_a65as",          "NES Cart A65AS PCB")
DEFINE_DEVICE_TYPE(NES_T262,           nes_t262_device,           "nes_t262",           "NES Cart T-262 PCB")
DEFINE_DEVICE_TYPE(NES_STUDYNGAME,     nes_studyngame_device,     "nes_studyngame",     "NES Cart Study n Game PCB")
DEFINE_DEVICE_TYPE(NES_SUPERGUN20IN1,  nes_sgun20in1_device,      "nes_sgun20in1",      "NES Cart Supergun 20 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_VT5201,         nes_vt5201_device,         "nes_vt5201",         "NES Cart VT5201 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_60311C,     nes_bmc_60311c_device,     "nes_bmc_60311c",     "NES Cart BMC 60311C PCB")
DEFINE_DEVICE_TYPE(NES_BMC_80013B,     nes_bmc_80013b_device,     "nes_bmc_80013b",     "NES Cart BMC 80013-B PCB")
DEFINE_DEVICE_TYPE(NES_BMC_810544C,    nes_bmc_810544c_device,    "nes_bmc_810544c",    "NES Cart BMC 810544-C-A1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_830425C,    nes_bmc_830425c_device,    "nes_bmc_830425c",    "NES Cart BMC 830425C-4391T PCB")
DEFINE_DEVICE_TYPE(NES_BMC_830928C,    nes_bmc_830928c_device,    "nes_bmc_830928c",    "NES Cart BMC 830928C PCB")
DEFINE_DEVICE_TYPE(NES_BMC_850437C,    nes_bmc_850437c_device,    "nes_bmc_850437c",    "NES Cart BMC 850437C PCB")
DEFINE_DEVICE_TYPE(NES_BMC_891227,     nes_bmc_891227_device,     "nes_bmc_891227",     "NES Cart BMC 891227 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_970630C,    nes_bmc_970630c_device,    "nes_bmc_970630c",    "NES Cart BMC 970630C PCB")
DEFINE_DEVICE_TYPE(NES_NTD03,          nes_ntd03_device,          "nes_ntd03",          "NES Cart NTD-03 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_CTC09,      nes_bmc_ctc09_device,      "nes_bmc_ctc09",      "NES Cart BMC CTC-09 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_CTC12IN1,   nes_bmc_ctc12in1_device,   "nes_bmc_ctc12in1",   "NES Cart BMC CTC-12IN1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_FAM250,     nes_bmc_fam250_device,     "nes_bmc_fam250",     "NES Cart BMC FAM250 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GKA,        nes_bmc_gka_device,        "nes_bmc_gka",        "NES Cart BMC GK-A PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GKB,        nes_bmc_gkb_device,        "nes_bmc_gkb",        "NES Cart BMC GK-B PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GKCXIN1,    nes_bmc_gkcxin1_device,    "nes_bmc_gkcxin1",    "NES Cart BMC GKCXIN1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GN91B,      nes_bmc_gn91b_device,      "nes_bmc_gn91b",      "NES Cart BMC GN-91B PCB")
DEFINE_DEVICE_TYPE(NES_BMC_HP898F,     nes_bmc_hp898f_device,     "nes_bmc_hp898f",     "NES Cart BMC HP-898F PCB")
DEFINE_DEVICE_TYPE(NES_BMC_K1029,      nes_bmc_k1029_device,      "nes_bmc_k1029",      "NES Cart BMC K-1029 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_K3036,      nes_bmc_k3036_device,      "nes_bmc_k3036",      "NES Cart BMC K-3036 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_K3046,      nes_bmc_k3046_device,      "nes_bmc_k3046",      "NES Cart BMC K-3046 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_SA005A,     nes_bmc_sa005a_device,     "nes_bmc_sa005a",     "NES Cart BMC SA005-A PCB")
DEFINE_DEVICE_TYPE(NES_BMC_TF2740,     nes_bmc_tf2740_device,     "nes_bmc_tf2740",     "NES Cart BMC TF2740 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_TJ03,       nes_bmc_tj03_device,       "nes_bmc_tj03",       "NES Cart BMC TJ-03 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_WS,         nes_bmc_ws_device,         "nes_bmc_ws",         "NES Cart BMC WS PCB")
DEFINE_DEVICE_TYPE(NES_BMC_11160,      nes_bmc_11160_device,      "nes_bmc_1160",       "NES Cart BMC-1160 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_G146,       nes_bmc_g146_device,       "nes_bmc_g146",       "NES Cart BMC-G-146 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_2751,       nes_bmc_2751_device,       "nes_bmc_2751",       "NES Cart BMC-2751 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_8157,       nes_bmc_8157_device,       "nes_bmc_8157",       "NES Cart BMC-8157 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_HIK300,     nes_bmc_hik300_device,     "nes_bmc_hik300",     "NES Cart BMC HIK 300 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_S700,       nes_bmc_s700_device,       "nes_bmc_s700",       "NES Cart BMC Super 700 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_BALL11,     nes_bmc_ball11_device,     "nes_bmc_ball11",     "NES Cart BMC Ball 11 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_22GAMES,    nes_bmc_22games_device,    "nes_bmc_22games",    "NES Cart BMC 22 Games PCB")
DEFINE_DEVICE_TYPE(NES_BMC_64Y2K,      nes_bmc_64y2k_device,      "nes_bmc_64y2k",      "NES Cart BMC Y2K 64 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_420Y2K,     nes_bmc_420y2k_device,     "nes_bmc_420y2k",     "NES Cart BMC Y2K 420 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_12IN1,      nes_bmc_12in1_device,      "nes_bmc_12in1",      "NES Cart BMC 12 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_20IN1,      nes_bmc_20in1_device,      "nes_bmc_20in1",      "NES Cart BMC 20 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_21IN1,      nes_bmc_21in1_device,      "nes_bmc_21in1",      "NES Cart BMC 21 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_31IN1,      nes_bmc_31in1_device,      "nes_bmc_31in1",      "NES Cart BMC 31 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_35IN1,      nes_bmc_35in1_device,      "nes_bmc_35in1",      "NES Cart BMC 35 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_36IN1,      nes_bmc_36in1_device,      "nes_bmc_36in1",      "NES Cart BMC 36 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_64IN1,      nes_bmc_64in1_device,      "nes_bmc_64in1",      "NES Cart BMC 64 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_70IN1,      nes_bmc_70in1_device,      "nes_bmc_70in1",      "NES Cart BMC 70 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_72IN1,      nes_bmc_72in1_device,      "nes_bmc_72in1",      "NES Cart BMC 72 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_76IN1,      nes_bmc_76in1_device,      "nes_bmc_76in1",      "NES Cart BMC 76 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_150IN1,     nes_bmc_150in1_device,     "nes_bmc_150in1",     "NES Cart BMC 150 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_190IN1,     nes_bmc_190in1_device,     "nes_bmc_190in1",     "NES Cart BMC 190 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_500IN1,     nes_bmc_500in1_device,     "nes_bmc_500in1",     "NES Cart BMC 500 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_800IN1,     nes_bmc_800in1_device,     "nes_bmc_800in1",     "NES Cart BMC 800 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_1200IN1,    nes_bmc_1200in1_device,    "nes_bmc_1200in1",    "NES Cart BMC 1200 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GOLD150,    nes_bmc_gold150_device,    "nes_bmc_gold150",    "NES Cart BMC Golden 150 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_GOLD260,    nes_bmc_gold260_device,    "nes_bmc_gold260",    "NES Cart BMC Golden 260 in 1 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_TH22913,    nes_bmc_th22913_device,    "nes_bmc_th22913",    "NES Cart BMC TH2291-3 PCB")
DEFINE_DEVICE_TYPE(NES_BMC_82AB,       nes_bmc_82ab_device,       "nes_bmc_82ab",       "NES Cart BMC 82AB PCB")
DEFINE_DEVICE_TYPE(NES_BMC_4IN1RESET,  nes_bmc_4in1reset_device,  "nes_bmc_4in1reset",  "NES Cart BMC 4 in 1 (Reset Based) PCB")
DEFINE_DEVICE_TYPE(NES_BMC_42IN1RESET, nes_bmc_42in1reset_device, "nes_bmc_42in1reset", "NES Cart BMC 42 in 1 (Reset Based) PCB")
DEFINE_DEVICE_TYPE(NES_BMC_NC20MB,     nes_bmc_nc20mb_device,     "nes_bmc_nc20mb",     "NES Cart BMC NC-20MB PCB")
DEFINE_DEVICE_TYPE(NES_BMC_LC160,      nes_bmc_lc160_device,      "nes_bmc_lc160",      "NES Cart BMC Little Com 160 PCB")


INPUT_PORTS_START( bmc_8157 )
	PORT_START("JUMPER")
	PORT_CONFNAME( 0x01, 0x01, "Menu Type" )
	PORT_CONFSETTING(    0x00, "20-in-1" )
	PORT_CONFSETTING(    0x01, "4-in-1" )
INPUT_PORTS_END


//-------------------------------------------------
//  input_ports - device-specific input ports
//-------------------------------------------------

ioport_constructor nes_bmc_8157_device::device_input_ports() const
{
	return INPUT_PORTS_NAME( bmc_8157 );
}



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

nes_action52_device::nes_action52_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_ACTION52, tag, owner, clock)
{
}

nes_caltron6in1_device::nes_caltron6in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_CALTRON6IN1, tag, owner, clock), m_latch(0), m_reg(0)
{
}

nes_caltron9in1_device::nes_caltron9in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_CALTRON9IN1, tag, owner, clock)
{
}

nes_rumblestat_device::nes_rumblestat_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_RUMBLESTATION, tag, owner, clock), m_prg(0), m_chr(0)
{
}

nes_svision16_device::nes_svision16_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_SVISION16, tag, owner, clock), m_latch1(0), m_latch2(0)
{
}

nes_farid_unrom_device::nes_farid_unrom_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_FARID_UNROM, tag, owner, clock), m_reg(0)
{
}

nes_kn42_device::nes_kn42_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_KN42, tag, owner, clock), m_latch(0)
{
}

nes_a65as_device::nes_a65as_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_A65AS, tag, owner, clock)
{
}

nes_t262_device::nes_t262_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_T262, tag, owner, clock), m_latch(0)
{
}

nes_studyngame_device::nes_studyngame_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_STUDYNGAME, tag, owner, clock)
{
}

nes_sgun20in1_device::nes_sgun20in1_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, type, tag, owner, clock)
{
}

nes_sgun20in1_device::nes_sgun20in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_sgun20in1_device(mconfig, NES_SUPERGUN20IN1, tag, owner, clock)
{
}

nes_bmc_190in1_device::nes_bmc_190in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_sgun20in1_device(mconfig, NES_BMC_190IN1, tag, owner, clock)
{
}

nes_vt5201_device::nes_vt5201_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_VT5201, tag, owner, clock), m_latch(0), m_jumper(0)
{
}

nes_bmc_80013b_device::nes_bmc_80013b_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_80013B, tag, owner, clock), m_latch(0)
{
}

nes_bmc_810544c_device::nes_bmc_810544c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_810544C, tag, owner, clock)
{
}

nes_bmc_830425c_device::nes_bmc_830425c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_830425C, tag, owner, clock), m_latch(0)
{
}

nes_bmc_830928c_device::nes_bmc_830928c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_830928C, tag, owner, clock), m_latch(0)
{
}

nes_bmc_850437c_device::nes_bmc_850437c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_850437C, tag, owner, clock)
{
}

nes_bmc_970630c_device::nes_bmc_970630c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_970630C, tag, owner, clock), m_latch(0)
{
}

nes_ntd03_device::nes_ntd03_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_NTD03, tag, owner, clock)
{
}

nes_bmc_ctc09_device::nes_bmc_ctc09_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_CTC09, tag, owner, clock)
{
}

nes_bmc_gka_device::nes_bmc_gka_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_GKA, tag, owner, clock)
{
}

nes_bmc_gkb_device::nes_bmc_gkb_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_GKB, tag, owner, clock)
{
}

nes_bmc_gkcxin1_device::nes_bmc_gkcxin1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_GKCXIN1, tag, owner, clock)
{
}

nes_bmc_gn91b_device::nes_bmc_gn91b_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_GN91B, tag, owner, clock), m_latch(0)
{
}

nes_bmc_hp898f_device::nes_bmc_hp898f_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_HP898F, tag, owner, clock)
{
}

nes_bmc_k3036_device::nes_bmc_k3036_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_K3036, tag, owner, clock)
{
}

nes_bmc_k3046_device::nes_bmc_k3046_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_K3046, tag, owner, clock)
{
}

nes_bmc_sa005a_device::nes_bmc_sa005a_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_SA005A, tag, owner, clock)
{
}

nes_bmc_tf2740_device::nes_bmc_tf2740_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_TF2740, tag, owner, clock), m_jumper(0)
{
}

nes_bmc_tj03_device::nes_bmc_tj03_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_TJ03, tag, owner, clock)
{
}

nes_bmc_ws_device::nes_bmc_ws_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_WS, tag, owner, clock), m_latch(0)
{
}

nes_bmc_11160_device::nes_bmc_11160_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_11160, tag, owner, clock)
{
}

nes_bmc_g146_device::nes_bmc_g146_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_G146, tag, owner, clock)
{
}

nes_bmc_2751_device::nes_bmc_2751_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_2751, tag, owner, clock)
{
}

nes_bmc_8157_device::nes_bmc_8157_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_8157, tag, owner, clock)
	, m_jumper(*this, "JUMPER")
	, m_latch(0)
{
}

nes_bmc_hik300_device::nes_bmc_hik300_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_HIK300, tag, owner, clock)
{
}

nes_bmc_s700_device::nes_bmc_s700_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_S700, tag, owner, clock)
{
}

nes_bmc_ball11_device::nes_bmc_ball11_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_BALL11, tag, owner, clock)
{
}

nes_bmc_22games_device::nes_bmc_22games_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_22GAMES, tag, owner, clock), m_latch(0), m_reset(0)
{
}

nes_bmc_64y2k_device::nes_bmc_64y2k_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_64Y2K, tag, owner, clock)
{
}

nes_bmc_420y2k_device::nes_bmc_420y2k_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_420Y2K, tag, owner, clock), m_latch(0), m_reg(0)
{
}

nes_bmc_12in1_device::nes_bmc_12in1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_12IN1, tag, owner, clock)
{
}

nes_bmc_20in1_device::nes_bmc_20in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_20IN1, tag, owner, clock)
{
}

nes_bmc_21in1_device::nes_bmc_21in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_21IN1, tag, owner, clock)
{
}

nes_bmc_31in1_device::nes_bmc_31in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_31IN1, tag, owner, clock)
{
}

nes_bmc_35in1_device::nes_bmc_35in1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_35IN1, tag, owner, clock)
{
}

nes_bmc_36in1_device::nes_bmc_36in1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_36IN1, tag, owner, clock)
{
}

nes_bmc_64in1_device::nes_bmc_64in1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_64IN1, tag, owner, clock)
{
}

nes_bmc_70in1_device::nes_bmc_70in1_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, type, tag, owner, clock), m_jumper(type == NES_BMC_70IN1 ? 0x0d : 0x06)
{
}

nes_bmc_70in1_device::nes_bmc_70in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_70in1_device(mconfig, NES_BMC_70IN1, tag, owner, clock)
{
}

nes_bmc_800in1_device::nes_bmc_800in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_70in1_device(mconfig, NES_BMC_800IN1, tag, owner, clock)
{
}

nes_bmc_72in1_device::nes_bmc_72in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_72IN1, tag, owner, clock)
{
}

nes_bmc_76in1_device::nes_bmc_76in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_76IN1, tag, owner, clock)
{
}

nes_bmc_150in1_device::nes_bmc_150in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_150IN1, tag, owner, clock)
{
}

nes_bmc_500in1_device::nes_bmc_500in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_500IN1, tag, owner, clock)
{
}

nes_bmc_1200in1_device::nes_bmc_1200in1_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_1200IN1, tag, owner, clock), m_vram_protect(0)
{
}

nes_bmc_gold150_device::nes_bmc_gold150_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_GOLD150, tag, owner, clock), m_latch(0)
{
}

nes_bmc_gold260_device::nes_bmc_gold260_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: nes_nrom_device(mconfig, NES_BMC_GOLD260, tag, owner, clock)
{
}

nes_bmc_4in1reset_device::nes_bmc_4in1reset_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_4IN1RESET, tag, owner, clock), m_latch(0)
{
}

nes_bmc_42in1reset_device::nes_bmc_42in1reset_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, type, tag, owner, clock), m_latch(0), m_mirror_flip(type == NES_BMC_NC20MB)
{
}

nes_bmc_42in1reset_device::nes_bmc_42in1reset_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_42in1reset_device(mconfig, NES_BMC_42IN1RESET, tag, owner, clock)
{
}

nes_bmc_nc20mb_device::nes_bmc_nc20mb_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_42in1reset_device(mconfig, NES_BMC_NC20MB, tag, owner, clock)
{
}

nes_bmc_lc160_device::nes_bmc_lc160_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_nrom_device(mconfig, NES_BMC_LC160, tag, owner, clock)
{
}

nes_vram_protect_device::nes_vram_protect_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	 : nes_nrom_device(mconfig, type, tag, owner, clock), m_vram_protect(false)
{
}

nes_bmc_60311c_device::nes_bmc_60311c_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_vram_protect_device(mconfig, NES_BMC_60311C, tag, owner, clock)
{
}

nes_bmc_ctc12in1_device::nes_bmc_ctc12in1_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_vram_protect_device(mconfig, type, tag, owner, clock)
{
}

nes_bmc_ctc12in1_device::nes_bmc_ctc12in1_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_ctc12in1_device(mconfig, NES_BMC_CTC12IN1, tag, owner, clock)
{
}

nes_bmc_891227_device::nes_bmc_891227_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_ctc12in1_device(mconfig, NES_BMC_891227, tag, owner, clock)
{
}

nes_bmc_k1029_device::nes_bmc_k1029_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_vram_protect_device(mconfig, type, tag, owner, clock)
{
}

nes_bmc_k1029_device::nes_bmc_k1029_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_k1029_device(mconfig, NES_BMC_K1029, tag, owner, clock)
{
}

nes_bmc_fam250_device::nes_bmc_fam250_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_k1029_device(mconfig, NES_BMC_FAM250, tag, owner, clock), m_latch(0), m_reg(0)
{
}

nes_n625092_device::nes_n625092_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_vram_protect_device(mconfig, NES_N625092, tag, owner, clock)
{
}

nes_bmc_th22913_device::nes_bmc_th22913_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock)
	: nes_vram_protect_device(mconfig, type, tag, owner, clock), m_vram_prot_bit(type == NES_BMC_TH22913 ? 10 : 9)
{
}

nes_bmc_th22913_device::nes_bmc_th22913_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_th22913_device(mconfig, NES_BMC_TH22913, tag, owner, clock)
{
}

nes_bmc_82ab_device::nes_bmc_82ab_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: nes_bmc_th22913_device(mconfig, NES_BMC_82AB, tag, owner, clock)
{
}




void nes_action52_device::device_start()
{
	common_start();
}

void nes_action52_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);
}

void nes_caltron6in1_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reg));
}

void nes_caltron6in1_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRROM);

	m_latch = 0;
	m_reg = 0;
}

void nes_caltron9in1_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_caltron9in1_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRROM);

	m_latch[0] = m_latch[1] = m_latch[2] = 0;
}

void nes_rumblestat_device::device_start()
{
	common_start();
	save_item(NAME(m_prg));
	save_item(NAME(m_chr));
}

void nes_rumblestat_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);

	m_prg = 0;
	m_chr = 0;
}

void nes_svision16_device::device_start()
{
	common_start();
	save_item(NAME(m_latch1));
	save_item(NAME(m_latch2));
}

void nes_svision16_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRRAM);

	m_latch1 = 0;
	m_latch2 = 0;
}

void nes_farid_unrom_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_farid_unrom_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_reg &= 0x87;    // only middle four bits cleared on soft reset
}

void nes_kn42_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_kn42_device::pcb_reset()
{
	m_latch ^= 0x10;
	prg16_89ab(m_latch);
	prg16_cdef(m_latch | 0x0f);    // fixed to last bank for either game
	chr8(0, CHRRAM);
}

void nes_a65as_device::device_start()
{
	common_start();
}

void nes_a65as_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(7);
	set_nt_mirroring(PPU_MIRROR_VERT);
	chr8(0, m_chr_source);
}

void nes_t262_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_t262_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_latch = 0;
}

void nes_studyngame_device::device_start()
{
	common_start();
}

void nes_studyngame_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);
}

void nes_sgun20in1_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
	set_nt_mirroring(PPU_MIRROR_VERT);
}

void nes_vt5201_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_jumper));
}

void nes_vt5201_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRROM);

	m_latch = 0;
	m_jumper = 0;
}

void nes_bmc_80013b_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reg));
}

void nes_bmc_80013b_device::pcb_reset()
{
	chr8(0, CHRRAM);

	m_latch = 0x80;
	m_reg[0] = m_reg[1] = 0;
	update_prg();
}

void nes_bmc_810544c_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
}

void nes_bmc_830425c_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_830425c_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0x0f);
	chr8(0, CHRRAM);
	set_nt_mirroring(PPU_MIRROR_VERT);

	m_latch = 0;
}

void nes_bmc_830928c_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_830928c_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_latch = 0;
}

void nes_bmc_850437c_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_850437c_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_reg[0] = m_reg[1] = 0;
}

void nes_bmc_970630c_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_970630c_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_latch = 0;
}

void nes_ntd03_device::device_start()
{
	common_start();
}

void nes_ntd03_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);
}

void nes_bmc_ctc09_device::pcb_reset()
{
// nes_slot's pcb_start sets us up in the main menu. Soft reset is empty so
// that games reset to their own title screens. This seems to be this cart's
// intended behavior as trying to reset to the menu here crashes (due to RAM
// contents?). Soft reset can similarly crash the main menu (BTANB?).
}

void nes_bmc_gka_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_gka_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);

	m_reg[0] = m_reg[1] = 0;
}

void nes_bmc_gn91b_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_gn91b_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_latch = 0;
}

void nes_bmc_k3036_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);
}

void nes_bmc_k3046_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);
}

void nes_bmc_sa005a_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
}

void nes_bmc_tf2740_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
	save_item(NAME(m_jumper));
}

void nes_bmc_tf2740_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);

	m_reg[0] = m_reg[1] = m_reg[2] = 0;
}

void nes_bmc_ws_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_ws_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);

	m_latch = 0;
}

void nes_bmc_2751_device::pcb_start(running_machine &machine, u8 *ciram_ptr, bool cart_mounted)
{
	device_nes_cart_interface::pcb_start(machine, ciram_ptr, cart_mounted);
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
}

void nes_bmc_2751_device::pcb_reset()
{
	// this board does not reset to menu on soft reset
}

void nes_bmc_8157_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_8157_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRRAM);

	m_latch = 0;
}

void nes_bmc_hik300_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
}

void nes_bmc_ball11_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_ball11_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRRAM);

	m_reg[0] = 2;
	m_reg[1] = 0;
}

void nes_bmc_22games_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reset));
}

void nes_bmc_22games_device::pcb_reset()
{
	if (m_reset)
	{
		prg32(4);
		m_latch = 1;
	}
	else
	{
		prg16_89ab(0);
		prg16_cdef(7);
		set_nt_mirroring(PPU_MIRROR_VERT);
		m_reset = 1;
	}
	chr8(0, CHRRAM);
}

void nes_bmc_64y2k_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_64y2k_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	chr8(0, m_chr_source);

	m_reg[0] = 0x80;
	m_reg[1] = 0x43;
	m_reg[2] = m_reg[3] = 0;
	set_prg();
	set_nt_mirroring(PPU_MIRROR_VERT);
}

void nes_bmc_420y2k_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reg));
}

void nes_bmc_420y2k_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(7);
	chr8(0, CHRRAM);

	m_latch = 0;
	m_reg = 0;
}

void nes_bmc_12in1_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_12in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);
	m_reg[0] = 0;
	m_reg[1] = 0;
	m_reg[2] = 0;
	update_banks();
}

void nes_bmc_35in1_device::device_start()
{
	common_start();
}

void nes_bmc_35in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, m_chr_source);
}

void nes_bmc_36in1_device::device_start()
{
	common_start();
}

void nes_bmc_36in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(m_prg_chunks - 1);
	prg16_cdef(m_prg_chunks - 1);
	chr8(0, m_chr_source);
}

void nes_bmc_64in1_device::device_start()
{
	common_start();
}

void nes_bmc_64in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, m_chr_source);
}

void nes_bmc_70in1_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_70in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	chr8(0, m_chr_source);

	m_latch[0] = m_latch[1] = 0;
	update_banks();
}

void nes_bmc_72in1_device::device_start()
{
	common_start();
	save_item(NAME(m_extra_ram));
}

void nes_bmc_76in1_device::device_start()
{
	common_start();
	save_item(NAME(m_reg));
}

void nes_bmc_76in1_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRRAM);

	m_reg[0] = m_reg[1] = 0;
}

void nes_bmc_150in1_device::pcb_reset()
{
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, CHRROM);
}

void nes_bmc_1200in1_device::device_start()
{
	common_start();
	save_item(NAME(m_vram_protect));
}

void nes_bmc_1200in1_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg16_89ab(0);
	prg16_cdef(0);
	chr8(0, m_chr_source);
	m_vram_protect = 0;
}

void nes_bmc_gold150_device::device_start()
{
	common_start();
	save_item(NAME(m_latch));
}

void nes_bmc_gold150_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);

	m_latch = 0;
}

void nes_bmc_gold260_device::device_start()
{
	common_start();
}

void nes_bmc_gold260_device::pcb_reset()
{
	m_chr_source = m_vrom_chunks ? CHRROM : CHRRAM;
	prg32(0);
	chr8(0, m_chr_source);
}

// This PCB is fully emulated here :)
void nes_bmc_4in1reset_device::device_start()
{
	common_start();
	m_latch = 3;
	save_item(NAME(m_latch));
}

void nes_bmc_4in1reset_device::pcb_reset()
{
	m_latch = (m_latch + 1) & 3;
	prg16_89ab(m_latch);
	prg16_cdef(m_latch);
	chr8(m_latch, CHRROM);
}


void nes_bmc_42in1reset_device::device_start()
{
	common_start();
	m_latch = 0x20;
	save_item(NAME(m_latch));
}

void nes_bmc_42in1reset_device::pcb_reset()
{
	m_latch ^= 0x20;
	prg32(m_latch >> 1);
	chr8(0, CHRRAM);
}

void nes_vram_protect_device::device_start()
{
	common_start();
	save_item(NAME(m_vram_protect));
}

void nes_vram_protect_device::pcb_reset()
{
	prg32(0);
	chr8(0, CHRRAM);
	m_vram_protect = false;
}

void nes_bmc_60311c_device::device_start()
{
	nes_vram_protect_device::device_start();
	save_item(NAME(m_reg));
}

void nes_bmc_60311c_device::pcb_reset()
{
	nes_vram_protect_device::pcb_reset();

	m_reg[0] = m_reg[1] = m_reg[2] = 0;
	update_banks();
}

void nes_bmc_ctc12in1_device::device_start()
{
	nes_vram_protect_device::device_start();
	save_item(NAME(m_reg));
}

void nes_bmc_ctc12in1_device::pcb_reset()
{
	nes_vram_protect_device::pcb_reset();
	prg16_89ab(0);
	prg16_cdef(0);

	m_reg[0] = m_reg[1] = 0;
}

void nes_bmc_fam250_device::device_start()
{
	nes_bmc_k1029_device::device_start();
	save_item(NAME(m_latch));
	save_item(NAME(m_reg));
}

void nes_bmc_fam250_device::pcb_reset()
{
	nes_bmc_k1029_device::pcb_reset();

	m_latch = 0;
	m_reg = 0;
}

void nes_n625092_device::device_start()
{
	nes_vram_protect_device::device_start();
	save_item(NAME(m_latch));
}

void nes_n625092_device::pcb_reset()
{
	nes_vram_protect_device::pcb_reset();
	prg16_89ab(0);
	prg16_cdef(0);

	m_latch[0] = m_latch[1] = 0;
}

void nes_bmc_th22913_device::pcb_reset()
{
	nes_vram_protect_device::pcb_reset();
	prg16_89ab(0);
	prg16_cdef(0);
}




/*-------------------------------------------------
 mapper specific handlers
 -------------------------------------------------*/

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

 Active Entertainment Action 52 board emulation

 iNES: mapper 228

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

void nes_action52_device::write_h(offs_t offset, uint8_t data)
{
	uint8_t pmode = offset & 0x20;
	int pbank = (offset & 0x1fc0) >> 6;
	int cbank = (data & 0x03) | ((offset & 0x0f) << 2);
	LOG_MMC(("ae_act52_w, offset: %04x, data: %02x\n", offset, data));

	set_nt_mirroring(BIT(offset, 13) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	chr8(cbank, CHRROM);

	if (pmode)
	{
		prg16_89ab(pbank);
		prg16_cdef(pbank);
	}
	else
		prg32(pbank >> 1);
}

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

 Caltron 6 in 1 Board

 Games: 6 in 1 by Caltron

 iNES: mapper 41

 In MAME: Supported.

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

void nes_caltron6in1_device::update_chr()
{
	chr8(((m_latch >> 1) & 0x0c) | m_reg, CHRROM);
}

void nes_caltron6in1_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("caltron6in1 write_m, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x1800)
	{
		case 0x0000:
			m_latch = offset & 0x3f;
			prg32(offset & 0x07);
			update_chr();
			set_nt_mirroring(BIT(offset, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
			break;
	}
}

void nes_caltron6in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("caltron6in1 write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	if (BIT(m_latch, 2))
	{
		m_reg = data & 0x03;
		update_chr();
	}
}

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

 Caltron 9 in 1 Board

 Games: 9 in 1 by Caltron

 NES 2.0: mapper 389

 In MAME: Supported.

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

void nes_caltron9in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("caltron9in1 write_h, offset: %04x, data: %02x\n", offset, data));
	int nibble = (offset >> 12) & 0x07;
	m_latch[std::min(nibble, 2)] = offset & 0x7f;

	if (BIT(m_latch[1], 1))
	{
		u8 outer = (m_latch[0] >> 2) & ~0x03;
		u8 inner = (m_latch[2] >> 2) & 0x03;
		prg16_89ab(outer | inner);
		prg16_cdef(outer | 0x03);
	}
	else
		prg32(m_latch[0] >> 3);

	if (nibble)
		chr8(((m_latch[1] >> 1) & 0x1c) | (m_latch[2] & 0x03), CHRROM);
	else
		set_nt_mirroring(BIT(m_latch[0], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

}

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

 Rumblestation Board

 Games: Rumblestation 15 in 1

 iNES: mapper 46

 In MESS: Supported.

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

void nes_rumblestat_device::write_m(offs_t offset, uint8_t data)
{
	LOG_MMC(("rumblestation write_m, offset: %04x, data: %02x\n", offset, data));

	m_prg = (m_prg & 0x01) | ((data & 0x0f) << 1);
	m_chr = (m_chr & 0x07) | ((data & 0xf0) >> 1);
	prg32(m_prg);
	chr8(m_chr, CHRROM);
}

void nes_rumblestat_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("rumblestation write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	m_prg = (m_prg & ~0x01) | (data & 0x01);
	m_chr = (m_chr & ~0x07) | ((data & 0x70) >> 4);
	prg32(m_prg);
	chr8(m_chr, CHRROM);
}

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

 Supervision 16 in 1 Board

 Games: Supervision 16 in 1

 This cart contains a 32KB boot menu that selects
 games from a separate 2MB of ROM. The common iNES
 file arbitrarily puts this first before the 2MB ROM.
 A simpler alternate approach would be to place the
 32KB menu at the end of contiguous PRG. In that case
 pcb_reset should be changed to point to the last
 part of PRG and the +2s and +4 below can be removed.

 iNES: mapper 53

 In MAME: Supported.

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

void nes_svision16_device::update_prg()
{
	if (BIT(m_latch1, 4))
	{
		u8 bank = (m_latch1 & 0x0f) << 3 | (m_latch2 & 0x07);
		prg16_89ab(bank + 2);             // +2 due to the 32KB menu
		prg16_cdef((bank | 0x07) + 2);    // +2 due to the 32KB menu
	}
}

void nes_svision16_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("svision16 write_m, offset: %04x, data: %02x\n", offset, data));

	if (!BIT(m_latch1, 4))
	{
		m_latch1 = data;
		update_prg();
		set_nt_mirroring(BIT(m_latch1, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}
}

void nes_svision16_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("svision16 write_h, offset: %04x, data: %02x\n", offset, data));
	m_latch2 = data;
	update_prg();
}

u8 nes_svision16_device::read_m(offs_t offset)
{
	LOG_MMC(("svision16 read_m, offset: %04x\n", offset));

	u8 bank = m_latch1 << 4 | 0x0f;
	return m_prg[((bank + 4) * 0x2000 + offset) % m_prg_size];    // +4 due to the 32KB menu
}

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

 FARID_UNROM_8-IN-1

 Games: 8 in 1

 NES 2.0: mapper 324

 In MAME: Supported.

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

void nes_farid_unrom_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("farid_unrom write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	if (BIT(data, 7) && !(m_reg & 0x88))
		m_reg = data;
	else
		m_reg = (m_reg & 0x78) | (data & 0x87);

	u8 bank = (m_reg & 0x70) >> 1 | (m_reg & 0x07);
	prg16_89ab(bank);
	prg16_cdef(bank | 0x07);
}

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

 Bootleg Board KN-42

 Games: 2 in 1 - Big Nose & Big Nose Freaks Out

 NES 2.0: mapper 381

 In MAME: Supported.

 TODO: Big Nose Freaks Out has timing issues like
 many Camerica games. It happens with the singleton
 dump and is unrelated to the bootleg board here.

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

void nes_kn42_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("kn42 write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	prg16_89ab(m_latch | (data & 0x07) << 1 | BIT(data, 4));
}

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

 Board BMC-A65AS

 Games: 3-in-1 (N068)

 In MESS: Supported

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

void nes_a65as_device::write_h(offs_t offset, uint8_t data)
{
	uint8_t helper = (data & 0x30) >> 1;
	LOG_MMC(("a65as write_h, offset: %04x, data: %02x\n", offset, data));

	if (data & 0x80)
		set_nt_mirroring(BIT(data, 5) ? PPU_MIRROR_HIGH : PPU_MIRROR_LOW);
	else
		set_nt_mirroring(BIT(data, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	if (data & 0x40)
		prg32(data >> 1);
	else
	{
		prg16_89ab(helper | (data & 0x07));
		prg16_cdef(helper | 0x07);
	}
}

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

 Board BMC-T-262

 Games: 4-in-1 (D-010), 8-in-1 (A-020), and others

 NES 2.0: mapper 265

 In MAME: Supported.

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

void nes_t262_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("t262 write_h, offset: %04x, data: %02x\n", offset, data));

	if (!BIT(m_latch, 13))
	{
		m_latch = offset;
		set_nt_mirroring(BIT(m_latch, 1) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}

	u8 bank = (m_latch & 0x300) >> 3 | (m_latch & 0x60) >> 2 | (data & 0x07);    // NesDev shows the high bit here, but is it correct? So far no cart is large enough to use this.
	u8 mode = BIT(m_latch, 0);
	if (BIT(m_latch, 7))    // NROM mode
	{
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
	}
	else                    // UNROM mode
	{
		prg16_89ab(bank);
		prg16_cdef(bank | 0x07);
	}
}

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

 Board UNL-STUDYNGAME

 Games: Study n Game 32 in 1

 iNES: mapper 39

 In MESS: Partially Supported (problems with PRG bankswitch,
 only keyboard exercise work).

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

void nes_studyngame_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("studyngame write_h, offset: %04x, data: %02x\n", offset, data));
	prg32(data);
}

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

 Boards BMC-SUPERGUN-20IN1, BMC-190IN1

 Unknown Bootleg Multigame Boards
 Games: Super Gun 20 in 1, Golden 190 in 1

 iNES: mapper 214
 NES 2.0: mapper 300

 In MAME: Supported.

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

void nes_sgun20in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("supergun20in1 write_h, offset: %04x, data: %02x\n", offset, data));

// Hogan's Alley in 20-in-1 will occasionally sweep through 0x66xx-0x68xx which
// causes an abrupt goto Bomberman title screen. This mask is a best guess.
	switch (offset & 0x7000)
	{
		case 0x0000:
		case 0x7000:
			offset = (offset >> 2) & (m_prg_mask >> 1);
			prg16_89ab(offset);
			prg16_cdef(offset);
			chr8(offset, CHRROM);
	}
}

void nes_bmc_190in1_device::write_h(offs_t offset, u8 data)
{
	nes_sgun20in1_device::write_h(offset, data);
	set_nt_mirroring(BIT(data, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-VT5201, BMC-T3H53, BMC-D1038

 Games: Super 35 in 1, TN 95 in 1, 46 in 1, 65 in 1,
 74 in 1, 77 in 1

 iNES: mapper 59

 In MAME: Supported.

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

void nes_vt5201_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("vt5201 write_h, offset: %04x, data: %02x\n", offset, data));

	if (!BIT(m_latch, 1))    // lock bit
	{
		m_latch = offset >> 8;

		u8 bank = (offset >> 4) & 0x07;
		u8 mode = !BIT(offset, 7);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);

		chr8(offset & 0x07, CHRROM);
		set_nt_mirroring(BIT(offset, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}
}

u8 nes_vt5201_device::read_h(offs_t offset)
{
	LOG_MMC(("bmc_vt5201 read_h, offset: %04x\n", offset));

	if (BIT(m_latch, 0))
		return (get_open_bus() & ~0x03) | m_jumper;    // TODO: add jumper settings, m_jumper is 0 for now
	else
		return hi_access_rom(offset);
}

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

 BMC-80013-B

 Games: Cartridge Story I, II, and III

 NES 2.0: mapper 274

 In MAME: Supported.

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

void nes_bmc_80013b_device::update_prg()
{
	prg16_89ab(m_latch | (m_reg[1] & 0x70) | m_reg[0]);
	prg16_cdef(m_reg[1]);
}

void nes_bmc_80013b_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_80013b write_h, offset: %04x, data: %02x\n", offset, data));
	if (offset & 0x6000)
	{
		m_reg[1] = data & 0x7f;
		m_latch = !BIT(offset, 14) << 7;
	}
	else
	{
		m_reg[0] = data & 0x0f;
		set_nt_mirroring(BIT(data, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}
	update_prg();
}

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

 BMC-810544-C-A1, NTDEC 2746

 Games: 200-in-1 Elfland, 14 in 1

 NES 2.0: mapper 261

 In MAME: Supported.

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

void nes_bmc_810544c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_810544c write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = bitswap<4>(offset, 9, 8, 7, 5);
	u8 mode = BIT(offset, 6);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8(offset & 0x0f, CHRROM);
	set_nt_mirroring(BIT(offset, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-830425C-4391T

 Games: Super HiK 6-in-1 (A-030)

 NES 2.0: mapper 320

 In MAME: Supported.

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

void nes_bmc_830425c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_830425c write_h, offset: %04x, data: %02x\n", offset, data));

	if ((offset & 0x7fe0) == 0x70e0)
		m_latch = offset & 0x1f;

	u8 outer = (m_latch & 0x0f) << 3;
	u8 mode = !BIT(m_latch, 4) << 3 | 0x07;
	prg16_89ab(outer | (data & mode));
	prg16_cdef(outer | mode);
}

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

 BMC-830928C

 Games: 9 in 1

 NES 2.0: mapper 382

 In MAME: Supported.

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

void nes_bmc_830928c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_830928c write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	if (!BIT(m_latch, 5))
	{
		m_latch = offset & 0x3f;
		set_nt_mirroring(BIT(m_latch, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}

	u8 outer = (m_latch & 0x07) << 3;
	if (BIT(m_latch, 3))    // BNROM mode
		prg32(outer >> 1 | (data & 0x03));
	else                    // UNROM mode
	{
		prg16_89ab(outer | (data & 0x07));
		prg16_cdef(outer | 7);
	}
}

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

 BMC-850437C

 Games: Super 8-in-1 (JY-050 rev0, JY-085, JY-086)

 NES 2.0: mapper 396

 In MAME: Supported.

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

void nes_bmc_850437c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_850437c write_h, offset: %04x, data: %02x\n", offset, data));

	m_reg[(offset & 0x6000) == 0x2000] = data;    // outer banking is always at 0xa000, mask is a guess

	u8 bank = (m_reg[1] & 0x07) << 3 | (m_reg[0] & 0x07);
	prg16_89ab(bank);
	prg16_cdef(bank | 0x07);

	set_nt_mirroring(BIT(m_reg[1], 6) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-970630C

 Games: 2 in 1 (NT-811), 4 in 1 1999

 NES 2.0: mapper 380

 In MAME: Supported.

 TODO: Contra has the same graphics glitches as in
 BMC-8157. These are marked as partially supported
 in the softlist, but maybe this is just a BTANB?

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

void nes_bmc_970630c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_970630c write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = (offset >> 2) & 0x1f;
	if (BIT(offset, 9))    // NROM mode
	{
		u8 mode = !BIT(offset, 0);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
	}
	else                   // UNROM-esque mode
	{
		prg16_89ab(bank);
		prg16_cdef(bank | 0x07);
	}

	set_nt_mirroring(BIT(offset, 1) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_latch = BIT(offset, 8);
}

u8 nes_bmc_970630c_device::read_h(offs_t offset)
{
//  LOG_MMC(("bmc_970630c read_h, offset: %04x\n", offset));

	if (m_latch)
		return 0;    // TODO: menu supposedly varies by solder pad value returned here, but it doesn't seem to work...
	else
		return hi_access_rom(offset);
}

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

 BMC-NTD-03

 Games: ASDER 20 in 1

 NES 2.0: mapper 290

 In MAME: Supported.

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

void nes_ntd03_device::write_h(offs_t offset, uint8_t data)
{
	uint8_t pbank = (offset >> 10) & 0x1e;
	uint8_t cbank = ((offset & 0x300) >> 5) | (offset & 0x07);
	LOG_MMC(("ntd03 write_h, offset: %04x, data: %02x\n", offset, data));

	if (BIT(offset, 7))
	{
		prg16_89ab(pbank | BIT(offset, 6));
		prg16_cdef(pbank | BIT(offset, 6));
	}
	else
		prg32(pbank >> 1);

	set_nt_mirroring(BIT(offset, 10) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	chr8(cbank, CHRROM);
}

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

 BMC-CTC-09

 Games: 10 in 1

 NES 2.0: mapper 335

 In MAME: Supported.

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

void nes_bmc_ctc09_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_ctc09 write_h, offset: %04x, data: %02x\n", offset, data));

	if (BIT(offset, 14))
	{
		if (BIT(data, 4))
		{
			u8 bank = ((data & 0x07) << 1) | BIT(data, 3);
			prg16_89ab(bank);
			prg16_cdef(bank);
		}
		else
			prg32(data & 0x07);
		set_nt_mirroring(BIT(data, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}
	else
		chr8(data & 0x0f, CHRROM);
}

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

 Board BMC-GKA

 Unknown Bootleg Multigame Board
 Games: 6 in 1, 54 in 1, 106 in 1

 iNES: mapper 57

 In MAME: Supported.

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

u8 nes_bmc_gka_device::read_m(offs_t offset)
{
	LOG_MMC(("bmc_gka read_m, offset: %04x\n", offset));
	return 0; // TODO: menus differ by jumper/DIP settings readable at 0x6000.
}

void nes_bmc_gka_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_gka write_h, offset: %04x, data: %02x\n", offset, data));

	m_reg[BIT(offset, 11)] = data;

	u8 bank = m_reg[1] >> 5;
	u8 mode = BIT(m_reg[1], 4);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8((m_reg[0] & 0x40) >> 3 | (m_reg[1] & 0x04) | (m_reg[BIT(m_reg[0], 7)] & 0x03), CHRROM);
	set_nt_mirroring(BIT(m_reg[1], 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 Board BMC-GKB

 Unknown Bootleg Multigame Board
 Games: 68 in 1, 73 in 1, 98 in 1, and many others

 iNES: mapper 58

 In MAME: Supported.

 TODO: Investigate why Jewel on mc_wq55 crashes after
 its title screen. It further invokes write_h, causing
 bank switching (and these games are all NROM/CNROM so
 they should never switch PRG once the menu loads them),
 but ignoring those writes doesn't fix the problem. Is
 the game non-working on hardware? A bad dump?

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

void nes_bmc_gkb_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_gkb write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = offset & 0x07;
	u8 mode = !BIT(offset, 6);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8((offset >> 3) & 0x07, CHRROM);
	set_nt_mirroring(BIT(offset, 7) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 Board BMC-GKCXIN1

 Unknown Bootleg Multigame Board
 Games: 21 in 1, 64 in 1

 NES 2.0: mapper 288

 In MAME: Supported.

 TODO: This has some sort of solder pad settings.

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

void nes_bmc_gkcxin1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_gkcxin1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg32((offset >> 3) & 0x03);
	chr8(offset & 0x07, CHRROM);
}

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

 Board BMC-GN-91B

 Unknown Bootleg Multigame Board
 Games: 2 in 1

 NES 2.0: mapper 431

 In MAME: Supported.

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

void nes_bmc_gn91b_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_gn91b write_h, offset: %04x, data: %02x\n", offset, data));

	if (offset < 0x4000)
	{
		m_latch = (offset & 0x10) >> 1;
		prg16_cdef(m_latch | 0x07);
		set_nt_mirroring(m_latch ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	}
	prg16_89ab(m_latch | (data & 0x07));
}

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

 Board HP-898F

 Games: Prima soft 9999999 in 1 and others

 NES 2.0: mapper 319

 In MAME: Supported.

 TODO: Mirroring is clearly incorrect for Ninja-kun
 on 4 in 1 (0207). Is this an error on the original
 cart or should there really be a variant device?

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

u8 nes_bmc_hp898f_device::read_l(offs_t offset)
{
	LOG_MMC(("bmc_hp898f read_l, offset: %04x\n", offset));

	offset += 0x100;
	if (offset & 0x1000)
		return 0;    // FIXME: some carts read jumpers that change menu

	return get_open_bus();
}

void nes_bmc_hp898f_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_hp898f write_m, offset: %04x, data: %02x\n", offset, data));

	if (offset & 0x04)
	{
		u8 bank = bitswap<3>(data, 4, 3, 5);
		u8 mode = BIT(data, 6);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
		set_nt_mirroring(BIT(data, 7) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
	}
	else
		chr8(data >> 4, CHRROM);
}

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

 BMC-K-3036

 Games: 35 in 1

 NES 2.0: mapper 340

 In MAME: Supported.

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

void nes_bmc_k3036_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_k3036 write_h, offset: %04x, data: %02x\n", offset, data));
	u8 bank = offset & 0x1f;
	prg16_89ab(bank);
	prg16_cdef(bank | (BIT(offset, 5) ? 0 : 7));
	set_nt_mirroring((offset & 0x25) == 0x25 ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-K-3046

 Games: 11 in 1

 NES 2.0: mapper 336

 In MAME: Supported.

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

void nes_bmc_k3046_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_k3046 write_h, offset: %04x, data: %02x\n", offset, data));

	// this pcb is subject to bus conflict
	data = account_bus_conflict(offset, data);

	data &= 0x1f;
	prg16_89ab(data);
	prg16_cdef(data | 0x07);
}

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

 BMC-SA005-A

 Games: 16 in 1

 NES 2.0: mapper 338

 In MAME: Supported.

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

void nes_bmc_sa005a_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_sa005a write_h, offset: %04x, data: %02x\n", offset, data));
	u8 bank = offset & 0x0f;
	prg16_89ab(bank);
	prg16_cdef(bank);
	chr8(bank, CHRROM);
	set_nt_mirroring(BIT(offset, 3) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
}

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

 BMC-TF2740

 Games: 14 in 1, 40 in 1, 158 in 1, 9999 in 1,
 10000000 in 1

 NES 2.0: mapper 428

 In MAME: Supported.

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

void nes_bmc_tf2740_device::update_chr()
{
	chr8(m_reg[2] & 0xc0 ? (m_reg[1] & 0x04) | (m_reg[0] & 0x03) : m_reg[1] & 0x07, CHRROM);
}

void nes_bmc_tf2740_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_tf2740 write_m, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x03)
	{
		case 0x01:
		{
			u8 bank = data >> 5;
			u8 mode = BIT(data, 4);
			prg16_89ab(bank & ~mode);
			prg16_cdef(bank | mode);

			set_nt_mirroring(BIT(data, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
			[[fallthrough]];
		}
		case 0x02:
			m_reg[offset & 0x03] = data;
			update_chr();
			break;
	}
}

void nes_bmc_tf2740_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_tf2740 write_h, offset: %04x, data: %02x\n", offset, data));
	m_reg[0] = data;
	update_chr();
}

u8 nes_bmc_tf2740_device::read_m(offs_t offset)
{
	LOG_MMC(("bmc_tf2740 read_m, offset: %04x\n", offset));
	return (get_open_bus() & ~0x03) | m_jumper;    // TODO: add jumper settings, m_jumper is 0 for now
}

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

 BMC-TJ-03

 Games: 4 in 1

 NES 2.0: mapper 341

 In MAME: Supported.

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

void nes_bmc_tj03_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_tj03 write_h, offset: %04x, data: %02x\n", offset, data));
	u8 bank = (offset >> 8) & 0x03;
	prg32(bank);
	chr8(bank, CHRROM);
	set_nt_mirroring(BIT(offset, 9) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 Board BMC-WS

 Games: Super 40-in-1

 In MESS: Partially Supported (some games, like Galaxian, have
 issues)

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

void nes_bmc_ws_device::write_m(offs_t offset, uint8_t data)
{
	uint8_t mmc_helper;
	LOG_MMC(("bmc_ws write_m, offset: %04x, data: %02x\n", offset, data));

	if (offset < 0x1000)
	{
		switch (offset & 0x01)
		{
			case 0:
				if (!m_latch)
				{
					m_latch = data & 0x20;
					set_nt_mirroring(BIT(data, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
					mmc_helper = (~data & 0x08) >> 3;
					prg16_89ab(data & ~mmc_helper);
					prg16_cdef(data |  mmc_helper);
				}
				break;
			case 1:
				if (!m_latch)
					chr8(data, CHRROM);
				break;
		}
	}
}

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

 Board BMC-11160 by TXC

 Games: 6 in 1 (MGC-023)

 NES 2.0: mapper 299

 In MAME: Partially supported. (Light gun hit detection is broken)

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

void nes_bmc_11160_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_11160 write_h, offset: %04x, data: %02x\n", offset, data));

	prg32((data & 0x30) >> 4);
	chr8(((data & 0x30) >> 2) | (data & 0x03), CHRROM);
	set_nt_mirroring(BIT(data, 7) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
}

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

 Board BMC-G-146

 Games: 1994 Super HIK 14 in 1 (G-136)

 NES 2.0: mapper 349

 In MAME: Supported.

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

void nes_bmc_g146_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_g146 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 prg_lo = offset & 0x1f;
	u8 prg_hi = prg_lo;         // default: NROM-128 mode
	if (BIT(offset, 11))
		prg_hi |= 0x07;     // UNROM mode
	else if (!BIT(offset, 6))
	{
		prg_lo &= ~0x01;    // NROM-256 mode
		prg_hi |= 0x01;
	}
	prg16_89ab(prg_lo);
	prg16_cdef(prg_hi);

	set_nt_mirroring(BIT(offset, 7) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 Board BMC-2751

 Games: 5 in 1

 iNES: mapper 174

 In MAME: Supported.

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

void nes_bmc_2751_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_2751 write_h, offset: %04x, data: %02x\n", offset, data));
	u8 bank = (offset >> 4) & 0x07;
	u8 mode = BIT(offset, 7);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);
	chr8((offset >> 1) & 0x07, CHRROM);
	set_nt_mirroring(BIT(offset, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 Board BMC-8157

 Games: 4 in 1 1993 (CK-001)

 NES 2.0: mapper 301

 In MAME: Preliminary supported.

 TODO: Determine the cause of Contra graphics glitches.
 Is NesDev description of board wrong? It seems b9 = 1
 and b7 = 0 always? Also couldn't see evidence of the
 mirroring bit being used.

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

void nes_bmc_8157_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_8157 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = (offset >> 2) & 0x1f;
	prg16_89ab(bank);
	if (BIT(offset, 9))
		bank |= 0x07;
	else if (BIT(offset, 7))
		bank &= ~0x07;
	prg16_cdef(bank);

	m_latch = BIT(offset, 8);
	set_nt_mirroring(BIT(offset, 1) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

u8 nes_bmc_8157_device::read_h(offs_t offset)
{
	LOG_MMC(("bmc_8157 read_h, offset: %04x\n", offset));
	if (m_latch)
		offset = (offset & ~0x01) | m_jumper->read();
	return hi_access_rom(offset);
}

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

 BMC-SUPERHIK_300IN1

 Unknown Bootleg Multigame Board
 Games: 100000 in 1, Super HIK 300 in 1, 1997 in 1

 iNES: mapper 212

 In MAME: Supported.

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

u8 nes_bmc_hik300_device::read_m(offs_t offset)
{
	LOG_MMC(("bmc_hik300 read_m, offset: %04x, data: %02x\n", offset));
	return get_open_bus() | (!BIT(offset, 4) << 7);    // some games have a protection MSB
}

void nes_bmc_hik300_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_hik300 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = offset & 0x07;
	u8 mode = BIT(offset, 14);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8(bank, CHRROM);
	set_nt_mirroring(BIT(offset, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-SUPER-700IN1

 Unknown Bootleg Multigame Board
 Games: Super 700 in 1, Super 190 in 1

 iNES: mapper 62

 In MAME: Supported.

 TODO:
 - Investigate why Gradius menu items 643-649 crash
   700-in-1 after pressing start at title screen.
   Gradius works from other menu numbers fine.
 - Slalom and Star Force have the wrong mirroring
   on 190-in-1. Bug on cart, bad dump, or is there
   another bit that affects mirroring?

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

void nes_bmc_s700_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_s700 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = (offset & 0x40) | ((offset >> 8) & 0x3f);
	u8 mode = !BIT(offset, 5);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8((offset & 0x1f) << 2 | (data & 0x03), CHRROM);
	set_nt_mirroring(BIT(offset, 7) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-BALLGAMES-11IN1

 Known Boards: Unknown Multigame Bootleg Board
 Games: 11 in 1 Ball Series

 iNES: mapper 51

 In MAME: Supported.

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

u8 nes_bmc_ball11_device::read_m(offs_t offset)
{
	LOG_MMC(("bmc_ball11 read_m, offset: %04x, data: %02x\n", offset));

	u8 bank = m_reg[1] << 2 | (m_reg[0] ? 0x23 : 0x2f);
	return m_prg[(bank * 0x2000 + offset) & (m_prg_size - 1)];
}

void nes_bmc_ball11_device::update_prg()
{
	if (m_reg[0])
		prg32(m_reg[1]);
	else
	{
		prg16_89ab(m_reg[1] << 1 | BIT(m_reg[1], 4));
		prg16_cdef(m_reg[1] << 1 | 0x07);
	}
}

void nes_bmc_ball11_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_ball11 write_m, offset: %04x, data: %02x\n", offset, data));
	set_nt_mirroring(BIT(data, 4) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_reg[0] = data & 0x02;
	update_prg();
}

void nes_bmc_ball11_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_ball11 write_h, offset: %04x, data: %02x\n", offset, data));
	m_reg[1] = data & 0x1f;
	update_prg();
}

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

 BMC-22GAMES

 Unknown Bootleg Multigame Board
 Games: Contra/22 in 1 combo

 iNES: mapper 230

 In MAME: Supported.

 TODO: Determine whether reset works as written or
 whether it toggles between Contra and the game menu.
 Reset to Contra from the menu or other games often
 fails due to RAM contents it seems.

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

void nes_bmc_22games_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_22games write_h, offset: %04x, data: %02x\n", offset, data));

	if (m_latch)
	{
		u8 bank = (data & 0x1f) + 8;
		u8 mode = !BIT(data, 5);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
		set_nt_mirroring(BIT(data, 6) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
	}
	else
		prg16_89ab(data & 0x07);
}

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

 Board BMC-64IN1NOREPEAT

 Games: 64-in-1 Y2K

 In MESS: Supported

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

void nes_bmc_64y2k_device::set_prg()
{
	uint8_t helper1 = (m_reg[1] & 0x1f);
	uint8_t helper2 = (helper1 << 1) | ((m_reg[1] & 0x40) >> 6);

	if (m_reg[0] & 0x80)
	{
		if (m_reg[1] & 0x80)
			prg32(helper1);
		else
		{
			prg16_89ab(helper2);
			prg16_cdef(helper2);
		}
	}
	else
		prg16_cdef(helper2);
}

void nes_bmc_64y2k_device::write_l(offs_t offset, uint8_t data)
{
	LOG_MMC(("bmc64y2k write_l, offset: %04x, data: %02x\n", offset, data));
	offset += 0x100;

	switch (offset)
	{
		case 0x1000:
		case 0x1001:
		case 0x1002:
		case 0x1003:
			m_reg[offset & 0x03] = data;
			set_prg();
			chr8(((m_reg[0] >> 1) & 0x03) | (m_reg[2] << 2), CHRROM);
			break;
	}
	if (offset == 0x1000)   /* write to reg[0] also sets mirroring */
		set_nt_mirroring(BIT(data, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

void nes_bmc_64y2k_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("bmc64y2k write_h, offset: %04x, data: %02x\n", offset, data));

	m_reg[3] = data;    // reg[3] is currently unused?!?
}

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

 Board BMC-TELETUBBIES
 (name assigned by BootGod who said the board has
 no markings, a glop top, and an 8K VRAM chip)

 Games: Y2K 420 in 1

 iNES: mapper 237

 In MAME: Supported.

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

void nes_bmc_420y2k_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_420y2k write_h, offset: %04x, data: %02x\n", offset, data));

	if (BIT(m_latch, 1))    // lock bit
		m_reg = (m_reg & ~0x07) | (data & 0x07);
	else
	{
		m_latch = offset;
		m_reg = data;
	}

	u8 bank = BIT(m_latch, 2) << 5 | (m_reg & 0x1f);
	u8 mode = BIT(m_reg, 6);
	prg16_89ab(bank & ~mode);    // strangely for UNROM games it DOESN'T ignore the NROM mode bit
	prg16_cdef(bank | (BIT(m_reg, 7) ? mode : 0x07));

	set_nt_mirroring(BIT(m_reg, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

u8 nes_bmc_420y2k_device::read_h(offs_t offset)
{
	LOG_MMC(("bmc_420y2k read_h, offset: %04x\n", offset));
	// latch bit 0 is only used to determine the menu, and the behavior of
	// this cart seems hardwired to OR $02 (ORing $00-$03 allows four menus)
	return hi_access_rom(offset | (m_latch & 1) << 1);
}

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

 BMC-12IN1

 Unknown Bootleg Multigame Board
 Games:

 iNES:

 In MESS: Supported.

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

void nes_bmc_12in1_device::update_banks()
{
	int bank = (m_reg[2] & 3) << 3;

	chr4_0((m_reg[0] >> 3) | (bank << 2), m_chr_source);
	chr4_4((m_reg[1] >> 3) | (bank << 2), m_chr_source);

	if (m_reg[2] & 8)
		prg32(((m_reg[0] & 7) >> 1) | bank);
	else
	{
		prg16_89ab((m_reg[0] & 7) | bank);
		prg16_cdef(7 | bank);
	}

	set_nt_mirroring(BIT(m_reg[2], 2) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

void nes_bmc_12in1_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("bmc_12in1 write_h, offset: %04x, data: %02x\n", offset, data));

	switch (offset & 0x6000)
	{
		case 0x2000: m_reg[0] = data; break;
		case 0x4000: m_reg[1] = data; break;
		case 0x6000: m_reg[2] = data; break;
	}
	update_banks();
}

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

 BMC-20IN1

 Unknown Bootleg Multigame Board
 Games: 20 in 1

 iNES: mapper 231

 In MAME: Supported.

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

void nes_bmc_20in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_20in1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg16_89ab(offset & 0x1e);
	prg16_cdef((offset & 0x1e) | BIT(offset, 5));
	set_nt_mirroring(BIT(offset, 7) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-21IN1, BMC-NOVELDIAMOND

 Unknown Bootleg Multigame Board
 Games: 8 in 1, 21 in 1, 9999999 in 1

 iNES: mapper 201

 In MAME: Supported.

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

void nes_bmc_21in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_21in1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg32(offset & 0x03);
	chr8(offset & 0x07, CHRROM);
}

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

 BMC-31IN1

 Unknown Bootleg Multigame Board
 Games: 31 in 1

 iNES: mapper 229

 In MAME: Supported.

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

void nes_bmc_31in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_31in1 write_h, offset: %04x, data: %02x\n", offset, data));
	set_nt_mirroring(BIT(offset, 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	offset &= 0x1f;
	if (offset & 0x1e)
	{
		prg16_89ab(offset);
		prg16_cdef(offset);
	}
	else
		prg32(0);

	chr8(offset, CHRROM);
}

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

 BMC-35IN1

 Unknown Bootleg Multigame Board
 Games: 35 in 1

 iNES: mapper 203

 In MESS: Supported.

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

void nes_bmc_35in1_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("bmc_35in1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg16_89ab((data >> 2) & 0x03);
	prg16_cdef((data >> 2) & 0x03);
	chr8(data & 0x03, CHRROM);
}

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

 BMC-36IN1

 Unknown Bootleg Multigame Board
 Games: 36 in 1, 1200 in 1

 iNES: mapper 200

 In MESS: Supported.

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

void nes_bmc_36in1_device::write_h(offs_t offset, uint8_t data)
{
	LOG_MMC(("bmc_36in1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg16_89ab(offset & 0x07);
	prg16_cdef(offset & 0x07);
	chr8(offset & 0x07, CHRROM);

	set_nt_mirroring(BIT(data, 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-64IN1

 Unknown Bootleg Multigame Board
 Games: 64 in 1

 iNES: mapper 204

 In MESS: Supported.

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

void nes_bmc_64in1_device::write_h(offs_t offset, uint8_t data)
{
	int bank = (offset >> 1) & (offset >> 2) & 0x01;

	LOG_MMC(("bmc_64in1 write_h, offset: %04x, data: %02x\n", offset, data));

	prg16_89ab(offset & ~bank);
	prg16_cdef(offset | bank);
	chr8(offset & ~bank, CHRROM);

	set_nt_mirroring(BIT(data, 4) ? PPU_MIRROR_HORZ: PPU_MIRROR_VERT);
}

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

 BMC-70IN1 (FIXME: according to NesDev the boards for this
 and BMC-800IN1 are Realtec 8031 and 8155. Which is which?)

 Games: 35 in 1, 68 in 1, 70 in 1

 iNES: mapper 236

 This is the same hardware as BMC-800IN1 below, but
 these carts have CHRROM and lack the extended PRG
 bank switching. Solder pads/jumpers not emulated yet.

 In MAME: Supported.

 TODO: Determine if Excitebike on 68 in 1 is actually
 bugged on the real cartridge. It sets mirroring bit
 to horizontal when it should be vertical.

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

void nes_bmc_70in1_device::update_banks()
{
	update_prg(m_latch[1] & 0x07);
	chr8(m_latch[0] & 0x07, CHRROM);
}

void nes_bmc_70in1_device::update_prg(u8 bank)
{
	if (BIT(m_latch[1], 5))    // NROM mode
	{
		u8 mode = !BIT(m_latch[1], 4);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
	}
	else                       // UNROM mode
	{
		prg16_89ab(bank);
		prg16_cdef(bank | 0x07);
	}
}

void nes_bmc_70in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc70in1 write_h, offset: %04x, data: %02x\n", offset, data));
	m_latch[BIT(offset, 14)] = offset;
	update_banks();
	set_nt_mirroring(BIT(m_latch[0], 5) ? PPU_MIRROR_HORZ: PPU_MIRROR_VERT);
}

uint8_t nes_bmc_70in1_device::read_h(offs_t offset)
{
	LOG_MMC(("bmc70in1 read_h, offset: %04x\n", offset));

	if ((m_latch[1] & 0x30) == 0x10)
		offset = (offset & ~0x0f) | m_jumper;    // TODO: make jumper selectable

	return hi_access_rom(offset);
}

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

 BMC-800IN1

 Games: 800 in 1

 iNES: mapper 236

 This is the same hardware as BMC-70IN1, but this
 cart supports larger PRG and has unbanked CHRRAM.

 In MAME: Supported.

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

void nes_bmc_800in1_device::update_banks()
{
	update_prg((m_latch[0] & 0x07) << 3 | (m_latch[1] & 0x07));
}

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

 BMC-72IN1, BMC-110IN1

 Unknown Bootleg Multigame Board
 Games: 72 in 1, 115 in 1 and other multigame carts

 iNES: mappers 225, 255

 In MAME: Supported.

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

u8 nes_bmc_72in1_device::read_l(offs_t offset)
{
	LOG_MMC(("bmc_72in1 read_l, offset: %04x\n", offset));

	offset += 0x100;
	if (offset >= 0x1800)    // 4 nibbles of RAM mirrored above 0x5800
		return m_extra_ram[offset & 0x03];

	return get_open_bus();
}

void nes_bmc_72in1_device::write_l(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_72in1 write_l, offset: %04x, data: %02x\n", offset, data));

	offset += 0x100;
	if (offset >= 0x1800)    // 4 nibbles of RAM mirrored above 0x5800
		m_extra_ram[offset & 0x03] = data & 0x0f;
}

void nes_bmc_72in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_72in1 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 high = (offset >> 8) & 0x40;
	u8 bank = high | ((offset >> 6) & 0x3f);
	u8 mode = !BIT(offset, 12);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8(high | (offset & 0x3f), CHRROM);
	set_nt_mirroring(BIT(offset, 13) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-76IN1, BMC-SUPER42IN1, BMC-GHOSTBUSTERS63IN1

 Unknown Bootleg Multigame Board
 Games: 76 in 1, Super 42 in 1, Ghostbusters 63 in 1

 iNES: mapper 226

 In MAME: Supported.

 TODO: Investigate nature of GB63's PCB. It likely has
 three 512KB chips and it seems the two high-order bits
 here select between them. However, 0,1 select the first
 chip and 2,3 correspond to the other two respectively.
 Does the PCB have an empty socket for a fourth chip in
 position 1? We mirror the first chip by reloading in
 the softlist, but should this really be open bus?

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

void nes_bmc_76in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_76in1 write_h, offset: %04x, data: %02x\n", offset, data));
	m_reg[offset & 0x01] = data;

	u8 bank = m_reg[1] << 6 | (m_reg[0] & 0x80) >> 2 | (m_reg[0] & 0x1f);
	u8 mode = !BIT(m_reg[0], 5);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	set_nt_mirroring(BIT(m_reg[0], 6) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
}

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

 BMC-150IN1

 Unknown Bootleg Multigame Board
 Games: 150 in 1

 iNES: mapper 202

 In MAME: Supported.

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

void nes_bmc_150in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_150in1 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = (offset >> 1) & 0x07;
	u8 mode = (bank & 0x06) == 0x06;
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	chr8(bank, CHRROM);
	set_nt_mirroring(BIT(offset, 0) ? PPU_MIRROR_HORZ: PPU_MIRROR_VERT);
}

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

 BMC-500IN1

 Unknown Bootleg Multigame Board
 Games: 500 in 1, 2000 in 1 Unchained Melody, etc

 iNES: mapper 217

 In MAME: Supported.

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

void nes_bmc_500in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc500in1 write_h, offset: %04x, data: %02x\n", offset, data));
	prg32((offset >> 2) & 0x07);
	chr8(offset & 0x07, CHRROM);
}

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

 BMC-1200IN1

 Unknown Bootleg Multigame Board
 Games: 1200 in 1, 295 in 1, 76 in 1

 iNES: mapper 227

 In MESS: Preliminary Supported.

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

void nes_bmc_1200in1_device::chr_w(offs_t offset, uint8_t data)
{
	int bank = offset >> 10;

	if (!m_vram_protect)
		m_chr_access[bank][offset & 0x3ff] = data;
}


void nes_bmc_1200in1_device::write_h(offs_t offset, uint8_t data)
{
	int bank = ((offset >> 2) & 0x1f) |  ((offset & 0x0100) >> 3);

	LOG_MMC(("bmc_1200in1 write_h, offset: %04x, data: %02x\n", offset, data));

	if (offset & 0x80)
	{
//      m_vram_protect = 1;
		prg16_89ab(bank);
		prg16_cdef(bank + (offset & 1));
	}
	else
	{
		int low_mask = (offset & 1) ? 0x3e : 0xff;

//      m_vram_protect = 0;
		if (!BIT(offset, 9))
		{
			prg16_89ab(bank & low_mask);
			prg16_cdef(bank & 0x38);
		}
		else
		{
			prg16_89ab(bank & low_mask);
			prg16_cdef(bank | 0x07);
		}
	}

	set_nt_mirroring(BIT(offset, 1) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}


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

 BMC-GOLDEN260IN1

 Unknown Bootleg Multigame Board
 Games:

 iNES: mapper 235

 In MESS: Preliminary Supported.

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

void nes_bmc_gold260_device::write_h(offs_t offset, uint8_t data)
{
	int bank = (offset & 0x1f) |  ((offset & 0x0300) >> 3);
	LOG_MMC(("bmc_gold260 write_h, offset: %04x, data: %02x\n", offset, data));

	if (offset & 0x400)
		set_nt_mirroring(PPU_MIRROR_LOW);
	else
		set_nt_mirroring(BIT(offset, 13) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	if (offset & 0x800)
	{
		bank = (bank << 1) | BIT(offset, 12);
		prg16_89ab(bank);
		prg16_cdef(bank);
	}
	else
		prg32(bank);
}


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

 BMC-GOLDEN150IN1

 Unknown Bootleg Multigame Board
 Games:

 iNES: mapper 235

 Same as the above + open bus in 0x8000-0xffff when
 enabled

 In MESS: Preliminary Supported.

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


void nes_bmc_gold150_device::write_h(offs_t offset, uint8_t data)
{
	int bank = (offset & 0x1f) |  ((offset & 0x0200) >> 4);
	LOG_MMC(("bmc_gold150 write_h, offset: %04x, data: %02x\n", offset, data));

	m_latch = (offset & 0x0100);

	if (offset & 0x400)
		set_nt_mirroring(PPU_MIRROR_LOW);
	else
		set_nt_mirroring(BIT(offset, 13) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);

	if (offset & 0x800)
	{
		bank = (bank << 1) | BIT(offset, 12);
		prg16_89ab(bank);
		prg16_cdef(bank);
	}
	else
		prg32(bank);
}

uint8_t nes_bmc_gold150_device::read_h(offs_t offset)
{
	LOG_MMC(("bmc_gold150 read_h, offset: %04x\n", offset));

	if (m_latch)    // open bus
		return get_open_bus();
	else
		return hi_access_rom(offset);
}

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

 BMC-RESETBASED4IN1

 Unknown Bootleg Multigame Board

 Games: 4 in 1 (several of them)

 iNES: mapper 60

 No need to use handlers. At reset the banks change
 and so does the game.

 In MAME: Supported.

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

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

 BMC-42IN1RESETBASED, BMC-NC-20MB

 Games: 42 (22 + 20) in 1, 20 in 1 (CA-006)

 Here we implement two nearly identical mappers. The
 NC-20MB board does not have the reset latch and its
 mirroring bit is inverted from the 42 in 1.

 There is an identical dump of the 42 in 1 identified
 as SUPER22GAMES that supposedly selected between the
 two menus via dip switch. It's not clear which, if
 any, variants of this cart exist, nor if this is
 really reset-based.

 iNES: mapper 233
 NES 2.0: mapper 433

 In MAME: Supported.

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

void nes_bmc_42in1reset_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_42in1reset write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = m_latch | (data & 0x1f);
	u8 mode = !BIT(data, 5);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	// some sources say there are 1-screen modes for 42-in-1, but this works as is
	set_nt_mirroring(BIT(data, 6) ^ m_mirror_flip ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
}

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

 BMC-LITTLECOM-160

 Unknown Bootleg Multigame Board

 Games: Little Com 160

 NES 2.0: mapper 541

 In MAME: Supported.

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

void nes_bmc_lc160_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_lc160 write_h, offset: %04x, data: %02x\n", offset, data));
	if (offset >= 0x4000)
	{
		u8 bank = (offset >> 2) & 0x3f;
		u8 mode = !BIT(offset, 1);
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
		set_nt_mirroring(BIT(offset, 0) ? PPU_MIRROR_VERT : PPU_MIRROR_HORZ);
	}
}

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

 BMC-60311C

 Games: 17 in 1

 NES 2.0: mapper 289

 In MAME: Preliminary supported.

 TODO: All the Capcom games and Konami's Hyper Soccer
 sound horrible here. BTANB? Also Journey to the West
 doesn't work as an individual rom (jwest) or here.

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

void nes_bmc_60311c_device::update_banks()
{
	if (BIT(m_reg[0], 1))    // UNROM mode
	{
		prg16_89ab((m_reg[1] & ~0x07) | (BIT(m_reg[0], 0) ? 0x07 : m_reg[2]));
		prg16_cdef(m_reg[1] | 0x07);
	}
	else                     // NROM mode
	{
		prg16_89ab(m_reg[1] & ~BIT(m_reg[0], 0));
		prg16_cdef(m_reg[1] | BIT(m_reg[0], 0));
	}

	set_nt_mirroring(BIT(m_reg[0], 3) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_vram_protect = BIT(m_reg[0], 2);
}

void nes_bmc_60311c_device::write_m(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_60311c write_m, offset: %04x, data: %02x\n", offset, data));
	m_reg[offset & 1] = data & 0x7f;
	update_banks();
}

void nes_bmc_60311c_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_60311c write_h, offset: %04x, data: %02x\n", offset, data));
	m_reg[2] = data & 0x07;
	update_banks();
}

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

 BMC-CTC-12IN1

 Games: 12 in 1 Game Card

 NES 2.0: mapper 337

 In MAME: Supported.

 TODO: Investigate why Legend of Kage has a corrupt
 title screen (inverting mirroring bit fixes it but
 breaks the rest of the game). Also why are walls in
 Pacman glitched?

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

u8 nes_bmc_ctc12in1_device::read_m(offs_t offset)
{
	LOG_MMC(("bmc_ctc12in1 read_m, offset: %04x, data: %02x\n", offset));
	return m_prg[0x01 * 0x2000 + offset];    // fixed to bank 1
}

void nes_bmc_ctc12in1_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_ctc12in1 write_h, offset: %04x, data: %02x\n", offset, data));
	m_reg[BIT(offset, 14)] = data;

	u8 bank = (m_reg[0] & 0x18) | (m_reg[1] & 0x07);
	switch (m_reg[0] >> 6)
	{
		case 0:
			prg16_89ab(bank);
			prg16_cdef(bank);
			break;
		case 1:
			prg32(bank >> 1);
			break;
		case 2:
		case 3:
			prg16_89ab(bank);
			prg16_cdef(bank | 0x07);
			break;
	}

	m_vram_protect == !BIT(m_reg[0], 7) || (offset & 0x6000) == 0x6000;
	set_nt_mirroring(BIT(m_reg[0], 5) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
}

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

 BMC-891227

 Games: Super 15 in 1 Game Card

 NES 2.0: mapper 350

 This board is similar to CTC-12IN1 above but with
 the mirroring and banking mode bits moved and with
 support for Contra on a separate 128K chip.

 In MAME: Supported.

 TODO: Contra bugs: Without input character sprite
 is always in the UP position. Cannot pickup weapons.

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

void nes_bmc_891227_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_891227 write_h, offset: %04x, data: %02x\n", offset, data));

	if (offset < 0x4000)
	     data = (data & 0x80) >> 2 | (data & 0x60) << 1 | (data & 0x1f);

	nes_bmc_ctc12in1_device::write_h(offset, data);

	if ((m_reg[0] & 0xc0) == 0xc0)  // Contra only mode
	{
		prg16_89ab(0x20 | (m_reg[1] & 0x07));
		prg16_cdef(0x27);
	}
}

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

 BMC-FAM250

 Games: 250 in 1

 This board is very close to the K-1029 board of the
 famous Contra 100 and 168 multicarts. It sports an
 extra mode to support a Bubble Bobble FDS bootleg.

 NES 2.0: mapper 354

 In MAME: Supported.

 TODO: Circus Charlie's mirroring is incorrectly set
 to horizontal. Is this a BTANB?

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

u8 nes_bmc_fam250_device::read_m(offs_t offset)
{
// LOG_MMC(("fam250 read_m, offset: %04x\n", offset));

	if (m_latch == 5)    // only the Bubble Bobble FDS bootleg uses this
		return m_prg[(((m_reg & 0x1f) << 1 | BIT(m_reg, 7)) * 0x2000 + offset) & (m_prg_size - 1)];
	else
		return get_open_bus();
}

void nes_bmc_fam250_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("fam250 write_h, offset: %04x, data: %02x\n", offset, data));

	if (offset >= 0x7000)
	{
		nes_bmc_k1029_device::write_h(offset, data);

		m_latch = offset & 0x07;
		m_reg = data;
		if (m_latch == 5)
			prg32((m_reg & 0x18) >> 1 | 0x03);

		m_vram_protect = BIT(offset, 3);
	}
}

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

 BMC-K-1029

 Games: 100 in 1 Contra Function 16, 168 in 1

 iNES: mapper 15

 In MAME: Supported.

 TODO: Contra games in 100-in-1 endlessly loop the
 second level played regardless of starting level.
 BTANB? Bad dump? FCEUX exhibits the same behavior.

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

void nes_bmc_k1029_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_k1029 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = data & 0x3f;
	switch (offset & 3)
	{
		case 0:
			prg32(bank >> 1);
			break;
		case 1:
			prg16_89ab(bank);
			prg16_cdef(bank | 0x07);
			break;
		case 2:
			bank = bank << 1 | BIT(data, 7);
			prg8_89(bank);
			prg8_ab(bank);
			prg8_cd(bank);
			prg8_ef(bank);
			break;
		case 3:
			prg16_89ab(bank);
			prg16_cdef(bank);
			break;
	}

	set_nt_mirroring(BIT(data, 6) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_vram_protect = BIT(offset, 0) == BIT(offset, 1);
}

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

 Bootleg Board N625092

 Games: 320 in 1, 400 in 1, 700 in 1, 800 in 1,
 1000 in 1, 2000 in 1, 5000000 in 1

 iNES: mapper 221

 In MAME: Supported.

 TODO: Several games have incorrect mirroring bits
 on all carts they appear on. It's subtle so it's
 likely a BTANB? Noticeable in Flappy, Pacman, and
 Warpman title scrolls and at bottom of screen in
 Zippy Race in-game.

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

void nes_n625092_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("n625092 write_h, offset: %04x, data: %02x\n", offset, data));

	m_latch[BIT(offset, 14)] = offset;

	u8 bank = (m_latch[0] & 0x200) >> 3 | (m_latch[0] & 0xe0) >> 2 | (m_latch[1] & 0x07);
	u8 mode = BIT(m_latch[0], 1);
	if (mode && BIT(m_latch[0], 8))    // UNROM mode
	{
		prg16_89ab(bank);
		prg16_cdef(bank | 7);
	}
	else                               // NROM mode
	{
		prg16_89ab(bank & ~mode);
		prg16_cdef(bank | mode);
	}

	set_nt_mirroring(BIT(m_latch[0], 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_vram_protect = BIT(m_latch[1], 3);
}

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

 BMC-TH2291-3, BMC-CH-011, BMC-82AB

 Games: Powerful 250 in 1, Powerful 255 in 1, 82 in 1

 iNES: mapper 63

 In MAME: Supported.

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

void nes_bmc_th22913_device::write_h(offs_t offset, u8 data)
{
	LOG_MMC(("bmc_th22913 write_h, offset: %04x, data: %02x\n", offset, data));

	u8 bank = offset >> 2;
	u8 mode = BIT(offset, 1);
	prg16_89ab(bank & ~mode);
	prg16_cdef(bank | mode);

	set_nt_mirroring(BIT(offset, 0) ? PPU_MIRROR_HORZ : PPU_MIRROR_VERT);
	m_vram_protect = BIT(offset, m_vram_prot_bit);
}