summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/segas32.c
blob: a64680bae7943340a26f1fb19ff2cf0138fc89ee (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
/*
    Open questions:

        - In f1en, the scrolling text in attract mode is very jumpy. Whatever
          double buffering they are using seems to be out of sync with the sprite
          rendering.

        - In radr, NBG1 should be opaque on select screen, and NBG3 should be
          opaque while driving. How is this controlled?

        - In radr, they use $1A0 as the X center for zooming; however, this
          contradicts the theory that bit 9 is a sign bit. For now, the code
          assumes that the X center has 10 bits of resolution.

        - In svf (the field) and radr (on the field), they use tilemap-specific
          flip in conjunction with rowscroll AND rowselect. According to Charles,
          in this case, the rowselect lookups should be done in reverse order,
          but this results in an incorrect display. For now, we assume there is
          a bug in the procedure and implement it so that it looks correct.


    Information extracted from below, and from Modeler:

    Tile format:
        Bits               Usage
        y------- --------  Tile Y flip
        -x------ --------  Tile X flip
        --?----- --------  Unknown
        ---ccccc cccc----  Tile color palette
        ---nnnnn nnnnnnnn  Tile index

    Text format:
        Bits               Usage
        ccccccc- --------  Tile color palette
        -------n nnnnnnnn  Tile index

    Text RAM:
        Offset     Bits                  Usage
         $31FF00 : w--- ---- ---- ---- : Screen width (0= 320, 1= 412)
                   ---- f--- ---- ---- : Bitmap format (1= 8bpp, 0= 4bpp)
                   ---- -t-- ---- ---- : Tile banking related
                   ---- --f- ---- ---- : 1= All layers X+Y flip
                   ---- ---- ---- 4--- : 1= X+Y flip for NBG3
                   ---- ---- ---- -2-- : 1= X+Y flip for NBG2
                   ---- ---- ---- --1- : 1= X+Y flip for NBG1
                   ---- ---- ---- ---0 : 1= X+Y flip for NBG0
         $31FF02 : x--- ---- --x- ---- : Bitmap layer enable (?)
                   ---1 ---- ---- ---- : 1= NBG1 page wrapping disable
                   ---- 0--- ---- ---- : 1= NBG0 page wrapping disable
                   ---- ---- --b- ---- : 1= Bitmap layer disable
                   ---- ---- ---t ---- : 1= Text layer disable
                   ---- ---- ---- 3--- : 1= NBG3 layer disable
                   ---- ---- ---- -2-- : 1= NBG2 layer disable
                   ---- ---- ---- --1- : 1= NBG1 layer disable
                   ---- ---- ---- ---0 : 1= NBG0 layer disable

        F02      cccccccc --------  Per-layer clipping modes (see Modeler)
                 -------- ----d---  Disable tilemap layer 3
                 -------- -----d--  Disable tilemap layer 2
                 -------- ------d-  Disable tilemap layer 1
                 -------- -------d  Disable tilemap layer 0
        F04      tttttttt --------  Rowscroll/select table page number
                 -------- ----s---  Enable rowselect for tilemap layer 3
                 -------- -----s--  Enable rowselect for tilemap layer 2
                 -------- ------c-  Enable rowscroll for tilemap layer 3
                 -------- -------c  Enable rowscroll for tilemap layer 2
        F06      cccc---- --------  Layer 3 clip select
                 ----cccc --------  Layer 2 clip select
                 -------- cccc----  Layer 1 clip select
                 -------- ----cccc  Layer 0 clip select
        F12      ------xx xxxxxxxx  Layer 0 X scroll
        F16      -------y yyyyyyyy  Layer 0 Y scroll
        F1A      ------xx xxxxxxxx  Layer 1 X scroll
        F1E      -------y yyyyyyyy  Layer 1 Y scroll
        F22      ------xx xxxxxxxx  Layer 2 X scroll
        F26      -------y yyyyyyyy  Layer 2 Y scroll
        F2A      ------xx xxxxxxxx  Layer 3 X scroll
        F2E      -------y yyyyyyyy  Layer 3 Y scroll
        F30      ------xx xxxxxxxx  Layer 0 X offset (Modeler says X center)
        F32      -------y yyyyyyyy  Layer 0 Y offset (Modeler says Y center)
        F34      ------xx xxxxxxxx  Layer 1 X offset
        F36      -------y yyyyyyyy  Layer 1 Y offset
        F38      ------xx xxxxxxxx  Layer 2 X offset
        F3A      -------y yyyyyyyy  Layer 2 Y offset
        F3C      ------xx xxxxxxxx  Layer 3 X offset
        F3E      -------y yyyyyyyy  Layer 3 Y offset
        F40      -wwwwwww --------  Layer 0 upper-right page select
                 -------- -wwwwwww  Layer 0 upper-left page select
        F42      -wwwwwww --------  Layer 0 lower-right page select
                 -------- -wwwwwww  Layer 0 lower-left page select
        F44      -wwwwwww --------  Layer 1 upper-right page select
                 -------- -wwwwwww  Layer 1 upper-left page select
        F46      -wwwwwww --------  Layer 1 lower-right page select
                 -------- -wwwwwww  Layer 2 upper-left page select
        F48      -wwwwwww --------  Layer 2 upper-right page select
                 -------- -wwwwwww  Layer 2 lower-left page select
        F4A      -wwwwwww --------  Layer 2 lower-right page select
                 -------- -wwwwwww  Layer 3 upper-left page select
                 -wwwwwww --------  Layer 3 upper-right page select
        F4E      -------- -wwwwwww  Layer 3 lower-left page select
                 -wwwwwww --------  Layer 3 lower-right page select
        F50      xxxxxxxx xxxxxxxx  Layer 0 X step increment (0x200 = 1.0)
        F52      yyyyyyyy yyyyyyyy  Layer 0 Y step increment (0x200 = 1.0)
        F54      xxxxxxxx xxxxxxxx  Layer 1 X step increment (0x200 = 1.0)
        F56      yyyyyyyy yyyyyyyy  Layer 1 Y step increment (0x200 = 1.0)
        F58      xxxxxxxx xxxxxxxx  Layer 2 X step increment (0x200 = 1.0)
        F5A      yyyyyyyy yyyyyyyy  Layer 2 Y step increment (0x200 = 1.0)
        F5C      -------- tttt----  Text layer page select (page = 64 + t*8)
                 -------- -----bbb  Text layer tile bank

         $31FF5E : e--- ---- ---- ---- : Select backdrop color per 1= line, 0= screen
                   ---x xxxx ---- ---- : Affects color in screen mode
                   ---d dddd dddd dddd : Offset in CRAM for line mode

        F60      xxxxxxxx xxxxxxxx  Clip rect 0, left
        F62      yyyyyyyy yyyyyyyy  Clip rect 0, top
        F64      xxxxxxxx xxxxxxxx  Clip rect 0, right
        F66      yyyyyyyy yyyyyyyy  Clip rect 0, bottom
        F68      xxxxxxxx xxxxxxxx  Clip rect 1, left
        F6A      yyyyyyyy yyyyyyyy  Clip rect 1, top
        F6C      xxxxxxxx xxxxxxxx  Clip rect 1, right
        F6E      yyyyyyyy yyyyyyyy  Clip rect 1, bottom
        F70      xxxxxxxx xxxxxxxx  Clip rect 2, left
        F72      yyyyyyyy yyyyyyyy  Clip rect 2, top
        F74      xxxxxxxx xxxxxxxx  Clip rect 2, right
        F76      yyyyyyyy yyyyyyyy  Clip rect 2, bottom
        F78      xxxxxxxx xxxxxxxx  Clip rect 3, left
        F7A      yyyyyyyy yyyyyyyy  Clip rect 3, top
        F7C      xxxxxxxx xxxxxxxx  Clip rect 3, right
        F7E      yyyyyyyy yyyyyyyy  Clip rect 3, bottom

         $31FF88 : ---- ---x xxxx xxxx : Bitmap X scroll
         $31FF8A : ---- ---y yyyy yyyy : Bitmap Y scroll (bit 8 ONLY available when format is 4bpp)
         $31FF8C : ---- ---b bbbb b--- : Bitmap palette base? (bit 3 ONLY available when format is 4bpp)

         $31FF8E : ---- ---- --b- ---- : 1= Bitmap layer disable
                   ---- ---- ---3 ---- : 1= NBG3 layer disable
                   ---- ---- ---- 2--- : 1= NBG2 layer disable
                   ---- ---- ---- -1-- : 1= NBG1 layer disable
                   ---- ---- ---- --0- : 1= NBG0 layer disable
                   ---- ---- ---- ---t : 1= Text layer disable
*/

#include "emu.h"
#include "profiler.h"
#include "includes/segas32.h"



/*************************************
 *
 *  Debugging
 *
 *************************************/

#define SHOW_CLIPS				0
#define QWERTY_LAYER_ENABLE		0
#define PRINTF_MIXER_DATA		0
#define SHOW_ALPHA				0
#define LOG_SPRITES				0



/*************************************
 *
 *  Constants
 *
 *************************************/

#define MIXER_LAYER_TEXT		0
#define MIXER_LAYER_NBG0		1
#define MIXER_LAYER_NBG1		2
#define MIXER_LAYER_NBG2		3
#define MIXER_LAYER_NBG3		4
#define MIXER_LAYER_BITMAP		5
#define MIXER_LAYER_SPRITES		6
#define MIXER_LAYER_BACKGROUND	7
#define MIXER_LAYER_SPRITES_2	8	/* semi-kludge to have a frame buffer for sprite backlayer */
#define MIXER_LAYER_MULTISPR	9
#define MIXER_LAYER_MULTISPR_2	10

#define TILEMAP_CACHE_SIZE		32



/*************************************
 *
 *  Helper macros
 *
 *************************************/

#define SWAP_HALVES(x)			NATIVE_ENDIAN_VALUE_LE_BE(x, ((x) >> 16) | ((x) << 16))



/*************************************
 *
 *  Type definitions
 *
 *************************************/

struct extents_list
{
	UINT8					scan_extent[256];
	UINT16					extent[32][16];
};


struct cache_entry
{
	struct cache_entry *	next;
	tilemap_t *				tmap;
	UINT8					page;
	UINT8					bank;
};



/*************************************
 *
 *  Prototypes
 *
 *************************************/

static TILE_GET_INFO( get_tile_info );
static void sprite_erase_buffer(segas32_state *state);
static void sprite_swap_buffers(segas32_state *state);
static void sprite_render_list(running_machine &machine);




/*************************************
 *
 *  Video start
 *
 *************************************/

static void common_start(running_machine &machine, int multi32)
{
	segas32_state *state = machine.driver_data<segas32_state>();
	int tmap;

	/* remember whether or not we are multi32 */
	state->m_is_multi32 = multi32;

	/* allocate a copy of spriteram in 32-bit format */
	state->m_spriteram_32bit = auto_alloc_array(machine, UINT32, 0x20000/4);

	/* allocate the tilemap cache */
	state->m_cache_head = NULL;
	for (tmap = 0; tmap < TILEMAP_CACHE_SIZE; tmap++)
	{
		struct cache_entry *entry = auto_alloc(machine, struct cache_entry);

		entry->tmap = tilemap_create(machine, get_tile_info, tilemap_scan_rows,  16,16, 32,16);
		entry->page = 0xff;
		entry->bank = 0;
		entry->next = state->m_cache_head;
		tilemap_set_user_data(entry->tmap, entry);

		state->m_cache_head = entry;
	}

	/* allocate the bitmaps (a few extra for multi32) */
	for (tmap = 0; tmap < 9 + 2 * multi32; tmap++)
	{
		state->m_layer_data[tmap].bitmap = auto_bitmap_alloc(machine, 416, 224, BITMAP_FORMAT_INDEXED16);
		state->m_layer_data[tmap].transparent = auto_alloc_array_clear(machine, UINT8, 256);
	}

	/* allocate pre-rendered solid lines of 0's and ffff's */
	state->m_solid_0000 = auto_alloc_array(machine, UINT16, 512);
	memset(state->m_solid_0000, 0x00, sizeof(state->m_solid_0000[0]) * 512);
	state->m_solid_ffff = auto_alloc_array(machine, UINT16, 512);
	memset(state->m_solid_ffff, 0xff, sizeof(state->m_solid_ffff[0]) * 512);

	/* initialize videoram */
	state->m_system32_videoram[0x1ff00/2] = 0x8000;
}


VIDEO_START( system32 )
{
	common_start(machine, 0);
}


VIDEO_START( multi32 )
{
	common_start(machine, 1);
}



/*************************************
 *
 *  Sprite management
 *
 *************************************/

static TIMER_CALLBACK( update_sprites )
{
	segas32_state *state = machine.driver_data<segas32_state>();
	/* if automatic mode is selected, do it every frame (0) or every other frame (1) */
	if (!(state->m_sprite_control[3] & 2))
	{
		/* if we count down to the start, process the automatic swapping, but only after a short delay */
		if (state->m_sprite_render_count-- == 0)
		{
			state->m_sprite_control[0] = 3;
			state->m_sprite_render_count = state->m_sprite_control[3] & 1;
		}
	}

	/* look for pending commands */
	if (state->m_sprite_control[0] & 2)
		sprite_erase_buffer(state);
	if (state->m_sprite_control[0] & 1)
	{
		sprite_swap_buffers(state);
		sprite_render_list(machine);
	}
	state->m_sprite_control[0] = 0;
}


void system32_set_vblank(running_machine &machine, int state)
{
	/* at the end of VBLANK is when automatic sprite rendering happens */
	if (!state)
		machine.scheduler().timer_set(attotime::from_usec(50), FUNC(update_sprites), 1);
}



/*************************************
 *
 *  Common palette handling
 *
 *************************************/

INLINE UINT16 xBBBBBGGGGGRRRRR_to_xBGRBBBBGGGGRRRR(UINT16 value)
{
	int r = (value >> 0) & 0x1f;
	int g = (value >> 5) & 0x1f;
	int b = (value >> 10) & 0x1f;
	value = (value & 0x8000) | ((b & 0x01) << 14) | ((g & 0x01) << 13) | ((r & 0x01) << 12);
	value |= ((b & 0x1e) << 7) | ((g & 0x1e) << 3) | ((r & 0x1e) >> 1);
	return value;
}


INLINE UINT16 xBGRBBBBGGGGRRRR_to_xBBBBBGGGGGRRRRR(UINT16 value)
{
	int r = ((value >> 12) & 0x01) | ((value << 1) & 0x1e);
	int g = ((value >> 13) & 0x01) | ((value >> 3) & 0x1e);
	int b = ((value >> 14) & 0x01) | ((value >> 7) & 0x1e);
	return (value & 0x8000) | (b << 10) | (g << 5) | (r << 0);
}


INLINE void update_color(running_machine &machine, int offset, UINT16 data)
{
	/* note that since we use this RAM directly, we don't technically need */
	/* to call palette_set_color() at all; however, it does give us that */
	/* nice display when you hit F4, which is useful for debugging */

	/* set the color */
	palette_set_color_rgb(machine, offset, pal5bit(data >> 0), pal5bit(data >> 5), pal5bit(data >> 10));
}


INLINE UINT16 common_paletteram_r(address_space *space, int which, offs_t offset)
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	int convert;

	/* the lower half of palette RAM is formatted xBBBBBGGGGGRRRRR */
	/* the upper half of palette RAM is formatted xBGRBBBBGGGGRRRR */
	/* we store everything if the first format, and convert accesses to the other format */
	/* on the fly */
	convert = (offset & 0x4000);
	offset &= 0x3fff;

	if (!convert)
		return state->m_system32_paletteram[which][offset];
	else
		return xBBBBBGGGGGRRRRR_to_xBGRBBBBGGGGRRRR(state->m_system32_paletteram[which][offset]);
}


static void common_paletteram_w(address_space *space, int which, offs_t offset, UINT16 data, UINT16 mem_mask)
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	UINT16 value;
	int convert;

	/* the lower half of palette RAM is formatted xBBBBBGGGGGRRRRR */
	/* the upper half of palette RAM is formatted xBGRBBBBGGGGRRRR */
	/* we store everything if the first format, and convert accesses to the other format */
	/* on the fly */
	convert = (offset & 0x4000);
	offset &= 0x3fff;

	/* read, modify, and write the new value, updating the palette */
	value = state->m_system32_paletteram[which][offset];
	if (convert) value = xBBBBBGGGGGRRRRR_to_xBGRBBBBGGGGRRRR(value);
	COMBINE_DATA(&value);
	if (convert) value = xBGRBBBBGGGGRRRR_to_xBBBBBGGGGGRRRRR(value);
	state->m_system32_paletteram[which][offset] = value;
	update_color(space->machine(), 0x4000*which + offset, value);

	/* if blending is enabled, writes go to both halves of palette RAM */
	if (state->m_mixer_control[which][0x4e/2] & 0x0880)
	{
		offset ^= 0x2000;

		/* read, modify, and write the new value, updating the palette */
		value = state->m_system32_paletteram[which][offset];
		if (convert) value = xBBBBBGGGGGRRRRR_to_xBGRBBBBGGGGRRRR(value);
		COMBINE_DATA(&value);
		if (convert) value = xBGRBBBBGGGGRRRR_to_xBBBBBGGGGGRRRRR(value);
		state->m_system32_paletteram[which][offset] = value;
		update_color(space->machine(), 0x4000*which + offset, value);
	}
}



/*************************************
 *
 *  Palette RAM access
 *
 *************************************/

READ16_HANDLER( system32_paletteram_r )
{
	return common_paletteram_r(space, 0, offset);
}


WRITE16_HANDLER( system32_paletteram_w )
{
	common_paletteram_w(space, 0, offset, data, mem_mask);
}


READ32_HANDLER( multi32_paletteram_0_r )
{
	return common_paletteram_r(space, 0, offset*2+0) |
	      (common_paletteram_r(space, 0, offset*2+1) << 16);
}


WRITE32_HANDLER( multi32_paletteram_0_w )
{
	if (ACCESSING_BITS_0_15)
		common_paletteram_w(space, 0, offset*2+0, data, mem_mask);
	if (ACCESSING_BITS_16_31)
		common_paletteram_w(space, 0, offset*2+1, data >> 16, mem_mask >> 16);
}


READ32_HANDLER( multi32_paletteram_1_r )
{
	return common_paletteram_r(space, 1, offset*2+0) |
	      (common_paletteram_r(space, 1, offset*2+1) << 16);
}


WRITE32_HANDLER( multi32_paletteram_1_w )
{
	if (ACCESSING_BITS_0_15)
		common_paletteram_w(space, 1, offset*2+0, data, mem_mask);
	if (ACCESSING_BITS_16_31)
		common_paletteram_w(space, 1, offset*2+1, data >> 16, mem_mask >> 16);
}



/*************************************
 *
 *  Video RAM access
 *
 *************************************/

READ16_HANDLER( system32_videoram_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	return state->m_system32_videoram[offset];
}


WRITE16_HANDLER( system32_videoram_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	COMBINE_DATA(&state->m_system32_videoram[offset]);

	/* if we are not in the control area, just update any affected tilemaps */
	if (offset < 0x1ff00/2)
	{
		struct cache_entry *entry;
		int page = offset / 0x200;
		offset %= 0x200;

		/* scan the cache for a matching pages */
		for (entry = state->m_cache_head; entry != NULL; entry = entry->next)
			if (entry->page == page)
				tilemap_mark_tile_dirty(entry->tmap, offset);
	}
}


READ32_HANDLER( multi32_videoram_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	return state->m_system32_videoram[offset*2+0] |
	      (state->m_system32_videoram[offset*2+1] << 16);
}


WRITE32_HANDLER( multi32_videoram_w )
{
	if (ACCESSING_BITS_0_15)
		system32_videoram_w(space, offset*2+0, data, mem_mask);
	if (ACCESSING_BITS_16_31)
		system32_videoram_w(space, offset*2+1, data >> 16, mem_mask >> 16);
}



/*************************************
 *
 *  Sprite control registers
 *
 *************************************/

READ16_HANDLER( system32_sprite_control_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	switch (offset)
	{
		case 0:
			/*  D1 : Seems to be '1' only during an erase in progress, this
                     occurs very briefly though.
                D0 : Selected frame buffer (0= A, 1= B) */
			return 0xfffc | (int)(state->m_layer_data[MIXER_LAYER_SPRITES].bitmap < state->m_layer_data[MIXER_LAYER_SPRITES_2].bitmap);

		case 1:
			/*  D1 : ?
                D0 : ?

                Values seem to be:

                0 = Unknown (relates to *approaching* out of time condition)
                1 = Normal status
                2 = Overdraw (rendering time is over but end-of-list command not read yet)
                3 = Never occurs

                Condition 2 can occur during rendering or list processing. */
			return 0xfffc | 1;

		case 2:
			/*  D1 : 1= Vertical flip, 0= Normal orientation
                D0 : 1= Horizontal flip, 0= Normal orientation */
			return 0xfffc | state->m_sprite_control_latched[2];

		case 3:
			/*  D1 : 1= Manual mode, 0= Automatic mode
                D0 : 1= 30 Hz update, 0= 60 Hz update (automatic mode only) */
			return 0xfffc | state->m_sprite_control_latched[3];

		case 4:
			/*  D1 : ?
                D0 : ? */
			return 0xfffc | state->m_sprite_control_latched[4];

		case 5:
			/*  D1 : ?
                D0 : ? */
			return 0xfffc | state->m_sprite_control_latched[5];

		case 6:
			/*  D0 : 1= 416 pixels
                     0= 320 pixels */
			return 0xfffc | (state->m_sprite_control_latched[6] & 1);

		case 7:
			/*  D1 : ?
                D0 : ? */
			return 0xfffc;
	}
	return 0xffff;
}


WRITE16_HANDLER( system32_sprite_control_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	if (ACCESSING_BITS_0_7)
		state->m_sprite_control[offset & 7] = data;
}


READ32_HANDLER( multi32_sprite_control_r )
{
	return system32_sprite_control_r(space, offset*2+0, mem_mask) |
	      (system32_sprite_control_r(space, offset*2+1, mem_mask >> 16) << 16);
}


WRITE32_HANDLER( multi32_sprite_control_w )
{
	if (ACCESSING_BITS_0_15)
		system32_sprite_control_w(space, offset*2+0, data, mem_mask);
	if (ACCESSING_BITS_16_31)
		system32_sprite_control_w(space, offset*2+1, data >> 16, mem_mask >> 16);
}



/*************************************
 *
 *  Sprite RAM access
 *
 *************************************/

READ16_HANDLER( system32_spriteram_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	return state->m_system32_spriteram[offset];
}


WRITE16_HANDLER( system32_spriteram_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	COMBINE_DATA(&state->m_system32_spriteram[offset]);
	state->m_spriteram_32bit[offset/2] =
		((state->m_system32_spriteram[offset |  1] >> 8 ) & 0x000000ff) |
		((state->m_system32_spriteram[offset |  1] << 8 ) & 0x0000ff00) |
		((state->m_system32_spriteram[offset & ~1] << 8 ) & 0x00ff0000) |
		((state->m_system32_spriteram[offset & ~1] << 24) & 0xff000000);
}


READ32_HANDLER( multi32_spriteram_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	return state->m_system32_spriteram[offset*2+0] |
	      (state->m_system32_spriteram[offset*2+1] << 16);
}


WRITE32_HANDLER( multi32_spriteram_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	data = SWAP_HALVES(data);
	mem_mask = SWAP_HALVES(mem_mask);
	COMBINE_DATA((UINT32 *)&state->m_system32_spriteram[offset*2]);
	state->m_spriteram_32bit[offset/2] =
		((state->m_system32_spriteram[offset |  1] >> 8 ) & 0x000000ff) |
		((state->m_system32_spriteram[offset |  1] << 8 ) & 0x0000ff00) |
		((state->m_system32_spriteram[offset & ~1] << 8 ) & 0x00ff0000) |
		((state->m_system32_spriteram[offset & ~1] << 24) & 0xff000000);
}



/*************************************
 *
 *  Mixer control registers
 *
 *************************************/

READ16_HANDLER( system32_mixer_r )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	return state->m_mixer_control[0][offset];
}

WRITE16_HANDLER( system32_mixer_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	COMBINE_DATA(&state->m_mixer_control[0][offset]);
}


WRITE32_HANDLER( multi32_mixer_0_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	data = SWAP_HALVES(data);
	mem_mask = SWAP_HALVES(mem_mask);
	COMBINE_DATA((UINT32 *)&state->m_mixer_control[0][offset*2]);
}


WRITE32_HANDLER( multi32_mixer_1_w )
{
	segas32_state *state = space->machine().driver_data<segas32_state>();
	data = SWAP_HALVES(data);
	mem_mask = SWAP_HALVES(mem_mask);
	COMBINE_DATA((UINT32 *)&state->m_mixer_control[1][offset*2]);
}



/*************************************
 *
 *  Tilemap cache
 *
 *************************************/

static tilemap_t *find_cache_entry(segas32_state *state, int page, int bank)
{
	struct cache_entry *entry, *prev;

	/* scan the list for a matching entry */
	prev = NULL;
	entry = state->m_cache_head;
	while (1)
	{
		if (entry->page == page && entry->bank == bank)
		{
			/* move us to the head before returning */
			if (prev)
			{
				prev->next = entry->next;
				entry->next = state->m_cache_head;
				state->m_cache_head = entry;
			}
			return entry->tmap;
		}

		/* stop on the last entry */
		if (entry->next == NULL)
			break;
		prev = entry;
		entry = entry->next;
	}

	/* okay, we didn't find one; take over this last entry */
	entry->page = page;
	entry->bank = bank;
	tilemap_mark_all_tiles_dirty(entry->tmap);

	/* move it to the head */
	prev->next = entry->next;
	entry->next = state->m_cache_head;
	state->m_cache_head = entry;

	return entry->tmap;
}



/*************************************
 *
 *  Tilemap callback
 *
 *************************************/

static TILE_GET_INFO( get_tile_info )
{
	segas32_state *state = machine.driver_data<segas32_state>();
	struct cache_entry *entry = (struct cache_entry *)param;
	UINT16 data = state->m_system32_videoram[(entry->page & 0x7f) * 0x200 + tile_index];
	SET_TILE_INFO(0, (entry->bank << 13) + (data & 0x1fff), (data >> 4) & 0x1ff, (data >> 14) & 3);
}



/*************************************
 *
 *  Clipping extents computation
 *
 *************************************/

static int compute_clipping_extents(screen_device &screen, int enable, int clipout, int clipmask, const rectangle &cliprect, struct extents_list *list)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	int flip = (state->m_system32_videoram[0x1ff00/2] >> 9) & 1;
	rectangle tempclip;
	rectangle clips[5];
	int sorted[5];
	int i, j, y;

	/* expand our cliprect to exclude the bottom-right */
	tempclip = cliprect;
	tempclip.max_x++;
	tempclip.max_y++;

	/* create the 0th entry */
	list->extent[0][0] = tempclip.min_x;
	list->extent[0][1] = tempclip.max_x;

	/* simple case if not enabled */
	if (!enable)
	{
		memset(&list->scan_extent[tempclip.min_y], 0, sizeof(list->scan_extent[0]) * (tempclip.max_y - tempclip.min_y));
		return 1;
	}

	/* extract the from videoram into locals, and apply the cliprect */
	for (i = 0; i < 5; i++)
	{
		if (!flip)
		{
			clips[i].min_x = state->m_system32_videoram[0x1ff60/2 + i * 4] & 0x1ff;
			clips[i].min_y = state->m_system32_videoram[0x1ff62/2 + i * 4] & 0x0ff;
			clips[i].max_x = (state->m_system32_videoram[0x1ff64/2 + i * 4] & 0x1ff) + 1;
			clips[i].max_y = (state->m_system32_videoram[0x1ff66/2 + i * 4] & 0x0ff) + 1;
		}
		else
		{
			const rectangle &visarea = screen.visible_area();

			clips[i].max_x = (visarea.max_x + 1) - (state->m_system32_videoram[0x1ff60/2 + i * 4] & 0x1ff);
			clips[i].max_y = (visarea.max_y + 1) - (state->m_system32_videoram[0x1ff62/2 + i * 4] & 0x0ff);
			clips[i].min_x = (visarea.max_x + 1) - ((state->m_system32_videoram[0x1ff64/2 + i * 4] & 0x1ff) + 1);
			clips[i].min_y = (visarea.max_y + 1) - ((state->m_system32_videoram[0x1ff66/2 + i * 4] & 0x0ff) + 1);
		}
		clips[i] &= tempclip;
		sorted[i] = i;
	}

	/* bubble sort them by min_x */
	for (i = 0; i < 5; i++)
		for (j = i + 1; j < 5; j++)
			if (clips[sorted[i]].min_x > clips[sorted[j]].min_x) { int temp = sorted[i]; sorted[i] = sorted[j]; sorted[j] = temp; }

	/* create all valid extent combinations */
	for (i = 1; i < 32; i++)
		if (i & clipmask)
		{
			UINT16 *extent = &list->extent[i][0];

			/* start off with an entry at tempclip.min_x */
			*extent++ = tempclip.min_x;

			/* loop in sorted order over extents */
			for (j = 0; j < 5; j++)
				if (i & (1 << sorted[j]))
				{
					const rectangle &cur = clips[sorted[j]];

					/* see if this intersects our last extent */
					if (extent != &list->extent[i][1] && cur.min_x <= extent[-1])
					{
						if (cur.max_x > extent[-1])
							extent[-1] = cur.max_x;
					}

					/* otherwise, just append to the list */
					else
					{
						*extent++ = cur.min_x;
						*extent++ = cur.max_x;
					}
				}

			/* append an ending entry */
			*extent++ = tempclip.max_x;
		}

	/* loop over scanlines and build extents */
	for (y = tempclip.min_y; y < tempclip.max_y; y++)
	{
		int sect = 0;

		/* figure out all the clips that intersect this scanline */
		for (i = 0; i < 5; i++)
			if ((clipmask & (1 << i)) && y >= clips[i].min_y && y < clips[i].max_y)
				sect |= 1 << i;
		list->scan_extent[y] = sect;
	}

	return clipout;
}



/*************************************
 *
 *  Zooming tilemaps (NBG0/1)
 *
 *************************************/

INLINE void get_tilemaps(segas32_state *state, int bgnum, tilemap_t **tilemaps)
{
	int tilebank, page;

	/* determine the current tilebank */
	if (state->m_is_multi32)
		tilebank = (state->m_system32_tilebank_external >> (2*bgnum)) & 3;
	else
		tilebank = ((state->m_system32_tilebank_external & 1) << 1) | ((state->m_system32_videoram[0x1ff00/2] & 0x400) >> 10);

	/* find the cache entries */
	page = (state->m_system32_videoram[0x1ff40/2 + 2 * bgnum + 0] >> 0) & 0x7f;
	tilemaps[0] = find_cache_entry(state, page, tilebank);
	page = (state->m_system32_videoram[0x1ff40/2 + 2 * bgnum + 0] >> 8) & 0x7f;
	tilemaps[1] = find_cache_entry(state, page, tilebank);
	page = (state->m_system32_videoram[0x1ff40/2 + 2 * bgnum + 1] >> 0) & 0x7f;
	tilemaps[2] = find_cache_entry(state, page, tilebank);
	page = (state->m_system32_videoram[0x1ff40/2 + 2 * bgnum + 1] >> 8) & 0x7f;
	tilemaps[3] = find_cache_entry(state, page, tilebank);
}


static void update_tilemap_zoom(screen_device &screen, struct layer_info *layer, const rectangle &cliprect, int bgnum)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	int clipenable, clipout, clips, clipdraw_start;
	bitmap_t &bitmap = *layer->bitmap;
	struct extents_list clip_extents;
	tilemap_t *tilemaps[4];
	UINT32 srcx, srcx_start, srcy;
	UINT32 srcxstep, srcystep;
	int dstxstep, dstystep;
	int flip, opaque;
	int x, y;

	/* get the tilemaps */
	get_tilemaps(state, bgnum, tilemaps);

	/* configure the layer */
	opaque = 0;
//opaque = (state->m_system32_videoram[0x1ff8e/2] >> (8 + bgnum)) & 1;
//if (screen.machine().input().code_pressed(KEYCODE_Z) && bgnum == 0) opaque = 1;
//if (screen.machine().input().code_pressed(KEYCODE_X) && bgnum == 1) opaque = 1;

	/* determine if we're flipped */
	flip = ((state->m_system32_videoram[0x1ff00/2] >> 9) ^ (state->m_system32_videoram[0x1ff00/2] >> bgnum)) & 1;

	/* determine the clipping */
	clipenable = (state->m_system32_videoram[0x1ff02/2] >> (11 + bgnum)) & 1;
	clipout = (state->m_system32_videoram[0x1ff02/2] >> (6 + bgnum)) & 1;
	clips = (state->m_system32_videoram[0x1ff06/2] >> (4 * bgnum)) & 0x0f;
	clipdraw_start = compute_clipping_extents(screen, clipenable, clipout, clips, cliprect, &clip_extents);

	/* extract the X/Y step values (these are in destination space!) */
	dstxstep = state->m_system32_videoram[0x1ff50/2 + 2 * bgnum] & 0xfff;
	if (state->m_system32_videoram[0x1ff00/2] & 0x4000)
		dstystep = state->m_system32_videoram[0x1ff52/2 + 2 * bgnum] & 0xfff;
	else
		dstystep = dstxstep;

	/* clamp the zoom factors */
	if (dstxstep < 0x80)
		dstxstep = 0x80;
	if (dstystep < 0x80)
		dstystep = 0x80;

	/* compute high-precision reciprocals (in 12.20 format) */
	srcxstep = (0x200 << 20) / dstxstep;
	srcystep = (0x200 << 20) / dstystep;

	/* start with the fractional scroll offsets, in source coordinates */
	srcx_start = (state->m_system32_videoram[0x1ff12/2 + 4 * bgnum] & 0x3ff) << 20;
	srcx_start += (state->m_system32_videoram[0x1ff10/2 + 4 * bgnum] & 0xff00) << 4;
	srcy = (state->m_system32_videoram[0x1ff16/2 + 4 * bgnum] & 0x1ff) << 20;
	srcy += (state->m_system32_videoram[0x1ff14/2 + 4 * bgnum] & 0xfe00) << 4;

	/* then account for the destination center coordinates */
	srcx_start -= ((INT16)(state->m_system32_videoram[0x1ff30/2 + 2 * bgnum] << 6) >> 6) * srcxstep;
	srcy -= ((INT16)(state->m_system32_videoram[0x1ff32/2 + 2 * bgnum] << 7) >> 7) * srcystep;

	/* finally, account for destination top,left coordinates */
	srcx_start += cliprect.min_x * srcxstep;
	srcy += cliprect.min_y * srcystep;

	/* if we're flipped, simply adjust the start/step parameters */
	if (flip)
	{
		const rectangle &visarea = screen.visible_area();

		srcx_start += (visarea.max_x - 2 * cliprect.min_x) * srcxstep;
		srcy += (visarea.max_y - 2 * cliprect.min_y) * srcystep;
		srcxstep = -srcxstep;
		srcystep = -srcystep;
	}

	/* loop over the target rows */
	for (y = cliprect.min_y; y <= cliprect.max_y; y++)
	{
		UINT16 *extents = &clip_extents.extent[clip_extents.scan_extent[y]][0];
		UINT16 *dst = &bitmap.pix16(y);
		int clipdraw = clipdraw_start;

		/* optimize for the case where we are clipped out */
		if (clipdraw || extents[1] <= cliprect.max_x)
		{
			int transparent = 0;
			UINT16 *src[2];

			/* look up the pages and get their source pixmaps */
			bitmap_t &tm0 = tilemap_get_pixmap(tilemaps[((srcy >> 27) & 2) + 0]);
			bitmap_t &tm1 = tilemap_get_pixmap(tilemaps[((srcy >> 27) & 2) + 1]);
			src[0] = &tm0.pix16((srcy >> 20) & 0xff);
			src[1] = &tm1.pix16((srcy >> 20) & 0xff);

			/* loop over extents */
			srcx = srcx_start;
			while (1)
			{
				/* if we're drawing on this extent, draw it */
				if (clipdraw)
				{
					for (x = extents[0]; x < extents[1]; x++)
					{
						UINT16 pix = src[(srcx >> 29) & 1][(srcx >> 20) & 0x1ff];
						srcx += srcxstep;
						if ((pix & 0x0f) == 0 && !opaque)
							pix = 0, transparent++;
						dst[x] = pix;
					}
				}

				/* otherwise, clear to zero */
				else
				{
					int pixels = extents[1] - extents[0];
					memset(&dst[extents[0]], 0, pixels * sizeof(dst[0]));
					srcx += srcxstep * pixels;
					transparent += pixels;
				}

				/* stop at the end */
				if (extents[1] > cliprect.max_x)
					break;

				/* swap states and advance to the next extent */
				clipdraw = !clipdraw;
				extents++;
			}

			layer->transparent[y] = (transparent == cliprect.max_x - cliprect.min_x + 1);
		}
		else
			layer->transparent[y] = 1;

		/* advance in Y */
		srcy += srcystep;
	}

	/* enable this code below to display zoom information */
#if 0
	if (dstxstep != 0x200 || dstystep != 0x200)
		popmessage("Zoom=%03X,%03X  Cent=%03X,%03X", dstxstep, dstystep,
			state->m_system32_videoram[0x1ff30/2 + 2 * bgnum],
			state->m_system32_videoram[0x1ff32/2 + 2 * bgnum]);
#endif
}



/*************************************
 *
 *  Rowscroll/select tilemaps (NBG2/3)
 *
 *************************************/

static void update_tilemap_rowscroll(screen_device &screen, struct layer_info *layer, const rectangle &cliprect, int bgnum)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	int clipenable, clipout, clips, clipdraw_start;
	bitmap_t &bitmap = *layer->bitmap;
	struct extents_list clip_extents;
	tilemap_t *tilemaps[4];
	int rowscroll, rowselect;
	int xscroll, yscroll;
	UINT16 *table;
	int srcx, srcy;
	int flip, opaque;
	int x, y;

	/* get the tilemaps */
	get_tilemaps(state, bgnum, tilemaps);

	/* configure the layer */
	opaque = 0;
//opaque = (state->m_system32_videoram[0x1ff8e/2] >> (8 + bgnum)) & 1;
//if (screen.machine().input().code_pressed(KEYCODE_C) && bgnum == 2) opaque = 1;
//if (screen.machine().input().code_pressed(KEYCODE_V) && bgnum == 3) opaque = 1;

	/* determine if we're flipped */
	flip = ((state->m_system32_videoram[0x1ff00/2] >> 9) ^ (state->m_system32_videoram[0x1ff00/2] >> bgnum)) & 1;

	/* determine the clipping */
	clipenable = (state->m_system32_videoram[0x1ff02/2] >> (11 + bgnum)) & 1;
	clipout = (state->m_system32_videoram[0x1ff02/2] >> (6 + bgnum)) & 1;
	clips = (state->m_system32_videoram[0x1ff06/2] >> (4 * bgnum)) & 0x0f;
	clipdraw_start = compute_clipping_extents(screen, clipenable, clipout, clips, cliprect, &clip_extents);

	/* determine if row scroll and/or row select is enabled */
	rowscroll = (state->m_system32_videoram[0x1ff04/2] >> (bgnum - 2)) & 1;
	rowselect = (state->m_system32_videoram[0x1ff04/2] >> bgnum) & 1;
	if ((state->m_system32_videoram[0x1ff04/2] >> (bgnum + 2)) & 1)
		rowscroll = rowselect = 0;

	/* get a pointer to the table */
	table = &state->m_system32_videoram[(state->m_system32_videoram[0x1ff04/2] >> 10) * 0x400];

	/* start with screen-wide X and Y scrolls */
	xscroll = (state->m_system32_videoram[0x1ff12/2 + 4 * bgnum] & 0x3ff) - (state->m_system32_videoram[0x1ff30/2 + 2 * bgnum] & 0x1ff);
	yscroll = (state->m_system32_videoram[0x1ff16/2 + 4 * bgnum] & 0x1ff);

	/* render the tilemap into its bitmap */
	for (y = cliprect.min_y; y <= cliprect.max_y; y++)
	{
		UINT16 *extents = &clip_extents.extent[clip_extents.scan_extent[y]][0];
		UINT16 *dst = &bitmap.pix16(y);
		int clipdraw = clipdraw_start;

		/* optimize for the case where we are clipped out */
		if (clipdraw || extents[1] <= cliprect.max_x)
		{
			int transparent = 0;
			UINT16 *src[2];
			int srcxstep;

			/* if we're not flipped, things are straightforward */
			if (!flip)
			{
				/* get starting scroll values */
				srcx = cliprect.min_x + xscroll;
				srcxstep = 1;
				srcy = yscroll + y;

				/* apply row scroll/select */
				if (rowscroll)
					srcx += table[0x000 + 0x100 * (bgnum - 2) + y] & 0x3ff;
				if (rowselect)
					srcy = (yscroll + table[0x200 + 0x100 * (bgnum - 2) + y]) & 0x1ff;
			}

			/* otherwise, we have to do some contortions */
			else
			{
				const rectangle &visarea = screen.visible_area();

				/* get starting scroll values */
				srcx = cliprect.max_x + xscroll;
				srcxstep = -1;
				srcy = yscroll + visarea.max_y - y;

				/* apply row scroll/select */
				if (rowscroll)
					srcx += table[0x000 + 0x100 * (bgnum - 2) + y] & 0x3ff;
				if (rowselect)
					srcy = (yscroll + table[0x200 + 0x100 * (bgnum - 2) + y]) & 0x1ff;
			}

			/* look up the pages and get their source pixmaps */
			bitmap_t &tm0 = tilemap_get_pixmap(tilemaps[((srcy >> 7) & 2) + 0]);
			bitmap_t &tm1 = tilemap_get_pixmap(tilemaps[((srcy >> 7) & 2) + 1]);
			src[0] = &tm0.pix16(srcy & 0xff);
			src[1] = &tm1.pix16(srcy & 0xff);

			/* loop over extents */
			while (1)
			{
				/* if we're drawing on this extent, draw it */
				if (clipdraw)
				{
					for (x = extents[0]; x < extents[1]; x++, srcx += srcxstep)
					{
						UINT16 pix = src[(srcx >> 9) & 1][srcx & 0x1ff];
						if ((pix & 0x0f) == 0 && !opaque)
							pix = 0, transparent++;
						dst[x] = pix;
					}
				}

				/* otherwise, clear to zero */
				else
				{
					int pixels = extents[1] - extents[0];
					memset(&dst[extents[0]], 0, pixels * sizeof(dst[0]));
					srcx += srcxstep * pixels;
					transparent += pixels;
				}

				/* stop at the end */
				if (extents[1] > cliprect.max_x)
					break;

				/* swap states and advance to the next extent */
				clipdraw = !clipdraw;
				extents++;
			}

			layer->transparent[y] = (transparent == cliprect.max_x - cliprect.min_x + 1);
		}
		else
			layer->transparent[y] = 1;
	}

	/* enable this code below to display scroll information */
#if 0
	if (rowscroll || rowselect)
		popmessage("Scroll=%d Select=%d  Table@%06X",
			rowscroll, rowselect, (state->m_system32_videoram[0x1ff04/2] >> 10) * 0x800);
#endif
}



/*************************************
 *
 *  Text layer
 *
 *************************************/

static void update_tilemap_text(screen_device &screen, struct layer_info *layer, const rectangle &cliprect)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	bitmap_t &bitmap = *layer->bitmap;
	UINT16 *tilebase;
	UINT16 *gfxbase;
	int startx, starty;
	int endx, endy;
	int x, y, iy;
	int flip;

	/* determine if we're flipped */
	flip = (state->m_system32_videoram[0x1ff00/2] >> 9) & 1;

	/* determine the base of the tilemap and graphics data */
	tilebase = &state->m_system32_videoram[((state->m_system32_videoram[0x1ff5c/2] >> 4) & 0x1f) * 0x800];
	gfxbase = &state->m_system32_videoram[(state->m_system32_videoram[0x1ff5c/2] & 7) * 0x2000];

	/* compute start/end tile numbers */
	startx = cliprect.min_x / 8;
	starty = cliprect.min_y / 8;
	endx = cliprect.max_x / 8;
	endy = cliprect.max_y / 8;

	/* loop over tiles */
	for (y = starty; y <= endy; y++)
		for (x = startx; x <= endx; x++)
		{
			int tile = tilebase[y * 64 + x];
			UINT16 *src = &gfxbase[(tile & 0x1ff) * 16];
			int color = (tile & 0xfe00) >> 5;

			/* non-flipped case */
			if (!flip)
			{
				UINT16 *dst = &bitmap.pix16(y * 8, x * 8);

				/* loop over rows */
				for (iy = 0; iy < 8; iy++)
				{
					int pixels = *src++;
					int pix;

					pix = (pixels >> 4) & 0x0f;
					if (pix)
						pix += color;
					dst[0] = pix;

					pix = (pixels >> 0) & 0x0f;
					if (pix)
						pix += color;
					dst[1] = pix;

					pix = (pixels >> 12) & 0x0f;
					if (pix)
						pix += color;
					dst[2] = pix;

					pix = (pixels >> 8) & 0x0f;
					if (pix)
						pix += color;
					dst[3] = pix;

					pixels = *src++;

					pix = (pixels >> 4) & 0x0f;
					if (pix)
						pix += color;
					dst[4] = pix;

					pix = (pixels >> 0) & 0x0f;
					if (pix)
						pix += color;
					dst[5] = pix;

					pix = (pixels >> 12) & 0x0f;
					if (pix)
						pix += color;
					dst[6] = pix;

					pix = (pixels >> 8) & 0x0f;
					if (pix)
						pix += color;
					dst[7] = pix;

					dst += bitmap.rowpixels();
				}
			}

			/* flipped case */
			else
			{
				const rectangle &visarea = screen.visible_area();

				int effdstx = visarea.max_x - x * 8;
				int effdsty = visarea.max_y - y * 8;
				UINT16 *dst = &bitmap.pix16(effdsty, effdstx);

				/* loop over rows */
				for (iy = 0; iy < 8; iy++)
				{
					int pixels = *src++;
					int pix;

					pix = (pixels >> 4) & 0x0f;
					if (pix)
						pix += color;
					dst[0] = pix;

					pix = (pixels >> 0) & 0x0f;
					if (pix)
						pix += color;
					dst[-1] = pix;

					pix = (pixels >> 12) & 0x0f;
					if (pix)
						pix += color;
					dst[-2] = pix;

					pix = (pixels >> 8) & 0x0f;
					if (pix)
						pix += color;
					dst[-3] = pix;

					pix = *src++;

					pix = (pixels >> 4) & 0x0f;
					if (pix)
						pix += color;
					dst[-4] = pix;

					pix = (pixels >> 0) & 0x0f;
					if (pix)
						pix += color;
					dst[-5] = pix;

					pix = (pixels >> 12) & 0x0f;
					if (pix)
						pix += color;
					dst[-6] = pix;

					pix = (pixels >> 8) & 0x0f;
					if (pix)
						pix += color;
					dst[-7] = pix;

					dst -= bitmap.rowpixels();
				}
			}
		}
}



/*************************************
 *
 *  Bitmap layer
 *
 *************************************/

static void update_bitmap(screen_device &screen, struct layer_info *layer, const rectangle &cliprect)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	int clipenable, clipout, clips, clipdraw_start;
	bitmap_t &bitmap = *layer->bitmap;
	struct extents_list clip_extents;
	int xscroll, yscroll;
	int color;
	int x, y;
	int bpp;

	/* configure the layer */
	bpp = (state->m_system32_videoram[0x1ff00/2] & 0x0800) ? 8 : 4;

	/* determine the clipping */
	clipenable = (state->m_system32_videoram[0x1ff02/2] >> 15) & 1;
	clipout = (state->m_system32_videoram[0x1ff02/2] >> 10) & 1;
	clips = 0x10;
	clipdraw_start = compute_clipping_extents(screen, clipenable, clipout, clips, cliprect, &clip_extents);

	/* determine x/y scroll */
	xscroll = state->m_system32_videoram[0x1ff88/2] & 0x1ff;
	yscroll = state->m_system32_videoram[0x1ff8a/2] & 0x1ff;
	color = (state->m_system32_videoram[0x1ff8c/2] << 4) & 0x1fff0 & ~((1 << bpp) - 1);

	/* loop over target rows */
	for (y = cliprect.min_y; y <= cliprect.max_y; y++)
	{
		UINT16 *extents = &clip_extents.extent[clip_extents.scan_extent[y]][0];
		UINT16 *dst = &bitmap.pix16(y);
		int clipdraw = clipdraw_start;

		/* optimize for the case where we are clipped out */
		if (clipdraw || extents[1] <= cliprect.max_x)
		{
			int transparent = 0;

			/* loop over extents */
			while (1)
			{
				/* if we're drawing on this extent, draw it */
				if (clipdraw)
				{
					/* 8bpp mode case */
					if (bpp == 8)
					{
						UINT8 *src = (UINT8 *)&state->m_system32_videoram[512/2 * ((y + yscroll) & 0xff)];
						for (x = extents[0]; x < extents[1]; x++)
						{
							int effx = (x + xscroll) & 0x1ff;
							int pix = src[BYTE_XOR_LE(effx)] + color;
							if ((pix & 0xff) == 0)
								pix = 0, transparent++;
							dst[x] = pix;
						}
					}

					/* 4bpp mode case */
					else
					{
						UINT16 *src = &state->m_system32_videoram[512/4 * ((y + yscroll) & 0x1ff)];
						for (x = extents[0]; x < extents[1]; x++)
						{
							int effx = (x + xscroll) & 0x1ff;
							int pix = ((src[effx / 4] >> (4 * (effx & 3))) & 0x0f) + color;
							if ((pix & 0x0f) == 0)
								pix = 0, transparent++;
							dst[x] = pix;
						}
					}
				}

				/* otherwise, clear to zero */
				else
				{
					int pixels = extents[1] - extents[0];
					memset(&dst[extents[0]], 0, pixels * sizeof(dst[0]));
					transparent += pixels;
				}

				/* stop at the end */
				if (extents[1] > cliprect.max_x)
					break;

				/* swap states and advance to the next extent */
				clipdraw = !clipdraw;
				extents++;
			}

			layer->transparent[y] = (transparent == cliprect.max_x - cliprect.min_x + 1);
		}
		else
			layer->transparent[y] = 1;
	}
}



/*************************************
 *
 *  Master tilemap chip updater
 *
 *************************************/

static void update_background(segas32_state *state, struct layer_info *layer, const rectangle &cliprect)
{
	bitmap_t &bitmap = *layer->bitmap;
	int x, y;

	for (y = cliprect.min_y; y <= cliprect.max_y; y++)
	{
		UINT16 *dst = &bitmap.pix16(y);
		int color;

		/* determine the color */
		if (state->m_system32_videoram[0x1ff5e/2] & 0x8000)
			color = (state->m_system32_videoram[0x1ff5e/2] & 0x1fff) + y;
		else
			color = state->m_system32_videoram[0x1ff5e/2] & 0x1e00;

		/* if the color doesn't match, fill */
		if (dst[cliprect.min_x] != color)
			for (x = cliprect.min_x; x <= cliprect.max_x; x++)
				dst[x] = color;
	}
}


static UINT8 update_tilemaps(screen_device &screen, const rectangle &cliprect)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	int enable0 = !(state->m_system32_videoram[0x1ff02/2] & 0x0001) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0002);
	int enable1 = !(state->m_system32_videoram[0x1ff02/2] & 0x0002) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0004);
	int enable2 = !(state->m_system32_videoram[0x1ff02/2] & 0x0004) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0008) && !(state->m_system32_videoram[0x1ff00/2] & 0x1000);
	int enable3 = !(state->m_system32_videoram[0x1ff02/2] & 0x0008) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0010) && !(state->m_system32_videoram[0x1ff00/2] & 0x2000);
	int enablet = !(state->m_system32_videoram[0x1ff02/2] & 0x0010) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0001);
	int enableb = !(state->m_system32_videoram[0x1ff02/2] & 0x0020) && !(state->m_system32_videoram[0x1ff8e/2] & 0x0020);

	/* update any tilemaps */
	if (enable0)
		update_tilemap_zoom(screen, &state->m_layer_data[MIXER_LAYER_NBG0], cliprect, 0);
	if (enable1)
		update_tilemap_zoom(screen, &state->m_layer_data[MIXER_LAYER_NBG1], cliprect, 1);
	if (enable2)
		update_tilemap_rowscroll(screen, &state->m_layer_data[MIXER_LAYER_NBG2], cliprect, 2);
	if (enable3)
		update_tilemap_rowscroll(screen, &state->m_layer_data[MIXER_LAYER_NBG3], cliprect, 3);
	if (enablet)
		update_tilemap_text(screen, &state->m_layer_data[MIXER_LAYER_TEXT], cliprect);
	if (enableb)
		update_bitmap(screen, &state->m_layer_data[MIXER_LAYER_BITMAP], cliprect);
	update_background(state, &state->m_layer_data[MIXER_LAYER_BACKGROUND], cliprect);

	return (enablet << 0) | (enable0 << 1) | (enable1 << 2) | (enable2 << 3) | (enable3 << 4) | (enableb << 5);
}



/*************************************
 *
 *  Sprite buffer management
 *
 *************************************/

static void sprite_erase_buffer(segas32_state *state)
{
	/* erase the visible sprite buffer and clear the checksums */
	state->m_layer_data[MIXER_LAYER_SPRITES].bitmap->fill(0xffff);

	/* for multi32, erase the other buffer as well */
	if (state->m_is_multi32)
		state->m_layer_data[MIXER_LAYER_MULTISPR].bitmap->fill(0xffff);
}


static void sprite_swap_buffers(segas32_state *state)
{
	/* swap between the two sprite buffers */
	struct layer_info temp;
	temp = state->m_layer_data[MIXER_LAYER_SPRITES];
	state->m_layer_data[MIXER_LAYER_SPRITES] = state->m_layer_data[MIXER_LAYER_SPRITES_2];
	state->m_layer_data[MIXER_LAYER_SPRITES_2] = temp;

	/* for multi32, swap the other buffer as well */
	if (state->m_is_multi32)
	{
		temp = state->m_layer_data[MIXER_LAYER_MULTISPR];
		state->m_layer_data[MIXER_LAYER_MULTISPR] = state->m_layer_data[MIXER_LAYER_MULTISPR_2];
		state->m_layer_data[MIXER_LAYER_MULTISPR_2] = temp;
	}

	/* latch any pending info */
	memcpy(state->m_sprite_control_latched, state->m_sprite_control, sizeof(state->m_sprite_control_latched));
}



/*************************************
 *
 *  Sprite render
 *
 *************************************/

/*******************************************************************************************
 *
 *  System 32-style sprites
 *
 *      Offs  Bits               Usage
 *       +0   cc------ --------  Command (00=sprite, 01=clip, 02=jump, 03=end)
 *       +0   --i----- --------  Indirect palette enable
 *       +0   ---l---- --------  Indirect palette is inline in spriteram
 *       +0   ----s--- --------  Shadow sprite
 *       +0   -----r-- --------  Graphics from spriteram
 *       +0   ------8- --------  8bpp sprite
 *       +0   -------o --------  Opaque (no transparency)
 *       +0   -------- y-------  Flip Y
 *       +0   -------- -x------  Flip X
 *       +0   -------- --Y-----  Apply Y offset from last jump
 *       +0   -------- ---X----  Apply X offset from last jump
 *       +0   -------- ----aa--  Y alignment (00=center, 10=start, 01=end)
 *       +0   -------- ------AA  X alignment (00=center, 10=start, 01=end)
 *       +2   hhhhhhhh --------  Source data height
 *       +2   -------- wwwwwwww  Source data width
 *       +4   rrrr---- --------  Low bits of ROM bank
 *       +4   -----hhh hhhhhhhh  Onscreen height
 *       +6   -5--4--- --------  Bits 5 + 4 of ROM bank
 *       +6   -----www wwwwwwww  Onscreen width
 *       +8   ----yyyy yyyyyyyy  Y position
 *       +A   ----xxxx xxxxxxxx  X position
 *       +C   oooooooo oooooooo  Offset within selected sprite bank
 *       +E   -----ppp pppp----  Palette
 *       +E   -------- ----rrrr  Priority?
 *
 *******************************************************************************************/

#define sprite_draw_pixel_16(trans)											\
	/* only draw if onscreen, not 0 or 15 */								\
	if (x >= clipin.min_x && x <= clipin.max_x &&							\
		(!do_clipout || x < clipout.min_x || x > clipout.max_x) &&			\
		pix != trans)														\
	{																		\
		if (!indirect)														\
		{																	\
			if (pix != 0)													\
			{																\
				if (!shadow)												\
					dest[x] = color | pix;									\
				else														\
					dest[x] &= 0x7fff;										\
			}																\
		}																	\
		else																\
		{																	\
			int indpix = indtable[pix];										\
			if ((indpix & transmask) != transmask)							\
			{																\
				if (!shadow)												\
					dest[x] = indpix;										\
				else														\
					dest[x] &= 0x7fff;										\
			}																\
		}																	\
	}

#define sprite_draw_pixel_256(trans)										\
	/* only draw if onscreen, not 0 or 15 */								\
	if (x >= clipin.min_x && x <= clipin.max_x &&							\
		(!do_clipout || x < clipout.min_x || x > clipout.max_x) &&			\
		pix != trans)														\
	{																		\
		if (!indirect)														\
		{																	\
			if (pix != 0)													\
			{																\
				if (!shadow)												\
					dest[x] = color | pix;									\
				else														\
					dest[x] &= 0x7fff;										\
			}																\
		}																	\
		else																\
		{																	\
			int indpix = (indtable[pix >> 4]) | (pix & 0x0f);				\
			if ((indpix & transmask) != transmask)							\
			{																\
				if (!shadow)												\
					dest[x] = indpix;										\
				else														\
					dest[x] &= 0x7fff;										\
			}																\
		}																	\
	}

static int draw_one_sprite(running_machine &machine, UINT16 *data, int xoffs, int yoffs, const rectangle &clipin, const rectangle &clipout)
{
	segas32_state *state = machine.driver_data<segas32_state>();
	static const int transparency_masks[4][4] =
	{
		{ 0x7fff, 0x3fff, 0x1fff, 0x0fff },
		{ 0x3fff, 0x1fff, 0x0fff, 0x07ff },
		{ 0x3fff, 0x1fff, 0x0fff, 0x07ff },
		{ 0x1fff, 0x0fff, 0x07ff, 0x03ff }
	};

	bitmap_t &bitmap = *state->m_layer_data[(!state->m_is_multi32 || !(data[3] & 0x0800)) ? MIXER_LAYER_SPRITES_2 : MIXER_LAYER_MULTISPR_2].bitmap;
	UINT8 numbanks = machine.region("gfx2")->bytes() / 0x400000;
	const UINT32 *spritebase = (const UINT32 *)machine.region("gfx2")->base();

	int indirect = data[0] & 0x2000;
	int indlocal = data[0] & 0x1000;
	int shadow   = (data[0] & 0x0800) && (state->m_sprite_control_latched[0x0a/2] & 1);
	int fromram  = data[0] & 0x0400;
	int bpp8     = data[0] & 0x0200;
	int transp   = (data[0] & 0x0100) ? 0 : (bpp8 ? 0xff : 0x0f);
	int flipy    = data[0] & 0x0080;
	int flipx    = data[0] & 0x0040;
	int offsety  = data[0] & 0x0020;
	int offsetx  = data[0] & 0x0010;
	int adjusty  = (data[0] >> 2) & 3;
	int adjustx  = (data[0] >> 0) & 3;
	int srch     = (data[1] >> 8);
	int srcw     = bpp8 ? (data[1] & 0x3f) : ((data[1] >> 1) & 0x3f);
	int bank     = state->m_is_multi32 ?
					((data[3] & 0x2000) >> 13) | ((data[3] & 0x8000) >> 14) :
					((data[3] & 0x0800) >> 11) | ((data[3] & 0x4000) >> 13);
	int dsth     = data[2] & 0x3ff;
	int dstw     = data[3] & 0x3ff;
	int ypos     = (INT16)(data[4] << 4) >> 4;
	int xpos     = (INT16)(data[5] << 4) >> 4;
	UINT32 addr  = data[6] | ((data[2] & 0xf000) << 4);
	int color    = 0x8000 | (data[7] & (bpp8 ? 0x7f00 : 0x7ff0));
	int hzoom, vzoom;
	int xdelta = 1, ydelta = 1;
	int x, y, xtarget, ytarget, yacc = 0, pix, transmask;
	const UINT32 *spritedata;
	UINT32 addrmask, curaddr;
	UINT16 indtable[16];

	/* if hidden, or top greater than/equal to bottom, or invalid bank, punt */
	if (srcw == 0 || srch == 0 || dstw == 0 || dsth == 0)
		goto bail;

	/* determine the transparency mask for pixels */
	transmask = transparency_masks[state->m_sprite_control_latched[0x08/2] & 3][state->m_sprite_control_latched[0x0a/2] & 3];
	if (bpp8)
		transmask &= 0xfff0;

	/* create the local palette for the indirect case */
	if (indirect)
	{
		UINT16 *src = indlocal ? &data[8] : &state->m_system32_spriteram[8 * (data[7] & 0x1fff)];
		for (x = 0; x < 16; x++)
			indtable[x] = (src[x] & (bpp8 ? 0xfff0 : 0xffff)) | ((state->m_sprite_control_latched[0x0a/2] & 1) ? 0x8000 : 0x0000);
	}

	/* clamp to within the memory region size */
	if (fromram)
	{
		spritedata = state->m_spriteram_32bit;
		addrmask = (0x20000 / 4) - 1;
	}
	else
	{
		if (numbanks)
			bank %= numbanks;
		spritedata = spritebase + 0x100000 * bank;
		addrmask = 0xfffff;
	}

	/* compute X/Y deltas */
	hzoom = (((bpp8 ? 4 : 8) * srcw) << 16) / dstw;
	vzoom = (srch << 16) / dsth;

	/* adjust the starting X position */
	if (offsetx)
		xpos += xoffs;
	switch (adjustx)
	{
		case 0:
		case 3:	xpos -= (dstw - 1) / 2; 	break;
		case 1: xpos -= dstw - 1;			break;
		case 2:								break;
	}

	/* adjust the starting Y position */
	if (offsety)
		ypos += yoffs;
	switch (adjusty)
	{
		case 0:
		case 3:	ypos -= (dsth - 1) / 2; 	break;
		case 1: ypos -= dsth - 1;			break;
		case 2:								break;
	}

	/* adjust for flipping */
	if (flipx)
	{
		xpos += dstw - 1;
		xdelta = -1;
	}
	if (flipy)
	{
		ypos += dsth - 1;
		ydelta = -1;
	}

	/* compute target X,Y positions for loops */
	xtarget = xpos + xdelta * dstw;
	ytarget = ypos + ydelta * dsth;

	/* adjust target x for clipping */
	if (xdelta > 0 && xtarget > clipin.max_x)
	{
		xtarget = clipin.max_x + 1;
		if (xpos >= xtarget)
			goto bail;
	}
	if (xdelta < 0 && xtarget < clipin.min_x)
	{
		xtarget = clipin.min_x - 1;
		if (xpos <= xtarget)
			goto bail;
	}

	/* loop from top to bottom */
	for (y = ypos; y != ytarget; y += ydelta)
	{
		/* skip drawing if not within the inclusive cliprect */
		if (y >= clipin.min_y && y <= clipin.max_y)
		{
			int do_clipout = (y >= clipout.min_y && y <= clipout.max_y);
			UINT16 *dest = &bitmap.pix16(y);
			int xacc = 0;

			/* 4bpp case */
			if (!bpp8)
			{
				/* start at the word before because we preincrement below */
				curaddr = addr - 1;
				for (x = xpos; x != xtarget; )
				{
					UINT32 pixels = spritedata[++curaddr & addrmask];

					/* draw four pixels */
					pix = (pixels >> 28) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(transp)  x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >> 24) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >> 20) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >> 16) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >> 12) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >>  8) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >>  4) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >>  0) & 0xf; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_16(transp); x += xdelta; xacc += hzoom; } xacc -= 0x10000;

					/* check for end code */
					if (transp != 0 && pix == 0x0f)
						break;
				}
			}

			/* 8bpp case */
			else
			{
				/* start at the word before because we preincrement below */
				curaddr = addr - 1;
				for (x = xpos; x != xtarget; )
				{
					UINT32 pixels = spritedata[++curaddr & addrmask];

					/* draw four pixels */
					pix = (pixels >> 24) & 0xff; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_256(transp); x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >> 16) & 0xff; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_256(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >>  8) & 0xff; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_256(0);      x += xdelta; xacc += hzoom; } xacc -= 0x10000;
					pix = (pixels >>  0) & 0xff; while (xacc < 0x10000 && x != xtarget) { sprite_draw_pixel_256(transp); x += xdelta; xacc += hzoom; } xacc -= 0x10000;

					/* check for end code */
					if (transp != 0 && pix == 0xff)
						break;
				}
			}
		}

		/* accumulate zoom factors; if we carry into the high bit, skip an extra row */
		yacc += vzoom;
		addr += srcw * (yacc >> 16);
		yacc &= 0xffff;
	}

	/* if we had an enabled inline indirect palette, we skip two entries */
bail:
	return (indirect && indlocal) ? 2 : 0;
}



static void sprite_render_list(running_machine &machine)
{
	segas32_state *state = machine.driver_data<segas32_state>();
	rectangle outerclip, clipin, clipout;
	int xoffs = 0, yoffs = 0;
	int numentries = 0;
	int spritenum = 0;
	UINT16 *sprite;

	g_profiler.start(PROFILER_USER2);

//  logerror("----\n");

	/* compute the outer clip */
	outerclip.min_x = outerclip.min_y = 0;
	outerclip.max_x = (state->m_sprite_control_latched[0x0c/2] & 1) ? 415 : 319;
	outerclip.max_y = 223;

	/* initialize the cliprects */
	clipin = outerclip;
	clipout.min_x = clipout.min_y = 0;
	clipout.max_x = clipout.max_y = -1;

	/* now draw */
	while (numentries++ < 0x20000/16)
	{
		/* top two bits are a command */
		sprite = &state->m_system32_spriteram[8 * (spritenum & 0x1fff)];
		switch (sprite[0] >> 14)
		{
			/* command 0 = draw sprite */
			case 0:
				spritenum += 1 + draw_one_sprite(machine, sprite, xoffs, yoffs, clipin, clipout);
				break;

			/* command 1 = set clipping */
			case 1:

				/* set the inclusive cliprect */
				if (sprite[0] & 0x1000)
				{
					clipin.min_y = (INT16)(sprite[0] << 4) >> 4;
					clipin.max_y = (INT16)(sprite[1] << 4) >> 4;
					clipin.min_x = (INT16)(sprite[2] << 4) >> 4;
					clipin.max_x = (INT16)(sprite[3] << 4) >> 4;
					clipin &= outerclip;
				}

				/* set the exclusive cliprect */
				if (sprite[0] & 0x2000)
				{
					clipout.min_y = (INT16)(sprite[4] << 4) >> 4;
					clipout.max_y = (INT16)(sprite[5] << 4) >> 4;
					clipout.min_x = (INT16)(sprite[6] << 4) >> 4;
					clipout.max_x = (INT16)(sprite[7] << 4) >> 4;
				}

				/* advance to the next entry */
				spritenum++;
				break;

			/* command 2 = jump to position, and set X offset */
			case 2:

				/* set the global offset */
				if (sprite[0] & 0x2000)
				{
					yoffs = (INT16)(sprite[1] << 4) >> 4;
					xoffs = (INT16)(sprite[2] << 4) >> 4;
				}
				spritenum = sprite[0] & 0x1fff;
				break;

			/* command 3 = done */
			case 3:
				numentries = 0x20000/16;
				break;
		}
	}

	g_profiler.stop();
}



/*************************************
 *
 *  Mixer layer render
 *
 *************************************/

INLINE UINT8 compute_color_offsets(segas32_state *state,int which, int layerbit, int layerflag)
{
	int mode = ((state->m_mixer_control[which][0x3e/2] & 0x8000) >> 14) | (layerbit & 1);

	switch (mode)
	{
		case 0:
		case 3:
		default:
			return !layerflag;

		case 1:
			/* fix me -- these are grayscale modes */
			return 2;

		case 2:
			return (!layerflag) ? 2 : 0;
	}
}

INLINE UINT16 compute_sprite_blend(UINT8 encoding)
{
	int value = encoding & 0xf;

	switch ((encoding >> 4) & 3)
	{
		/* blend if priority == value */
		case 0:		return 1 << value;

		/* blend if priority <= value */
		case 1:		return (1 << value) | ((1 << value) - 1);

		/* blend if priority >= value */
		case 2:		return ~((1 << value) - 1) & 0xffff;

		/* blend always */
		default:
		case 3:		return 0xffff;
	}
}

INLINE UINT16 *get_layer_scanline(segas32_state *state, int layer, int scanline)
{
	if (state->m_layer_data[layer].transparent[scanline])
		return (layer == MIXER_LAYER_SPRITES) ? state->m_solid_ffff : state->m_solid_0000;
	return &state->m_layer_data[layer].bitmap->pix16(scanline);
}

static void mix_all_layers(segas32_state *state, int which, int xoffs, bitmap_t &bitmap, const rectangle &cliprect, UINT8 enablemask)
{
	int blendenable = state->m_mixer_control[which][0x4e/2] & 0x0800;
	int blendfactor = (state->m_mixer_control[which][0x4e/2] >> 8) & 7;
	struct mixer_layer_info
	{
		UINT16		palbase;			/* palette base from control reg */
		UINT16		sprblendmask;		/* mask of sprite priorities this layer blends with */
		UINT8		blendmask;			/* mask of layers this layer blends with */
		UINT8		index;				/* index of this layer (MIXER_LAYER_XXX) */
		UINT8		effpri;				/* effective priority = (priority << 3) | layer_priority */
		UINT8		mixshift;			/* shift from control reg */
		UINT8		coloroffs;			/* color offset index */
	} layerorder[16][8], layersort[8];
	struct layer_info temp_sprite_save = { 0 };
	UINT8 sprgroup_shift, sprgroup_mask, sprgroup_or;
	int numlayers, laynum, groupnum;
	int rgboffs[3][3];
	int sprpixmask, sprshadowmask;
	int sprx, spry, sprx_start;
	int sprdx, sprdy;
	int sprshadow;
	int x, y, i;

	/* if we are the second monitor on multi32, swap in the proper sprite bank */
	if (which == 1)
	{
		temp_sprite_save = state->m_layer_data[MIXER_LAYER_SPRITES];
		state->m_layer_data[MIXER_LAYER_SPRITES] = state->m_layer_data[MIXER_LAYER_MULTISPR];
	}

	/* extract the RGB offsets */
	rgboffs[0][0] = (INT8)(state->m_mixer_control[which][0x40/2] << 2) >> 2;
	rgboffs[0][1] = (INT8)(state->m_mixer_control[which][0x42/2] << 2) >> 2;
	rgboffs[0][2] = (INT8)(state->m_mixer_control[which][0x44/2] << 2) >> 2;
	rgboffs[1][0] = (INT8)(state->m_mixer_control[which][0x46/2] << 2) >> 2;
	rgboffs[1][1] = (INT8)(state->m_mixer_control[which][0x48/2] << 2) >> 2;
	rgboffs[1][2] = (INT8)(state->m_mixer_control[which][0x4a/2] << 2) >> 2;
	rgboffs[2][0] = 0;
	rgboffs[2][1] = 0;
	rgboffs[2][2] = 0;

	/* determine the sprite grouping parameters first */
	switch (state->m_mixer_control[which][0x4c/2] & 0x0f)
	{
		default:
		case 0x0:	sprgroup_shift = 14;	sprgroup_mask = 0x00;	sprgroup_or = 0x01;	break;
		case 0x1:	sprgroup_shift = 14;	sprgroup_mask = 0x01;	sprgroup_or = 0x02;	break;
		case 0x2:	sprgroup_shift = 13;	sprgroup_mask = 0x03;	sprgroup_or = 0x04;	break;
		case 0x3:	sprgroup_shift = 12;	sprgroup_mask = 0x07;	sprgroup_or = 0x08;	break;

		case 0x4:	sprgroup_shift = 14;	sprgroup_mask = 0x01;	sprgroup_or = 0x00;	break;
		case 0x5:	sprgroup_shift = 13;	sprgroup_mask = 0x03;	sprgroup_or = 0x00;	break;
		case 0x6:	sprgroup_shift = 12;	sprgroup_mask = 0x07;	sprgroup_or = 0x00;	break;
		case 0x7:	sprgroup_shift = 11;	sprgroup_mask = 0x0f;	sprgroup_or = 0x00;	break;

		case 0x8:	sprgroup_shift = 14;	sprgroup_mask = 0x01;	sprgroup_or = 0x00;	break;
		case 0x9:	sprgroup_shift = 13;	sprgroup_mask = 0x03;	sprgroup_or = 0x00;	break;
		case 0xa:	sprgroup_shift = 12;	sprgroup_mask = 0x07;	sprgroup_or = 0x00;	break;
		case 0xb:	sprgroup_shift = 11;	sprgroup_mask = 0x0f;	sprgroup_or = 0x00;	break;

		case 0xc:	sprgroup_shift = 13;	sprgroup_mask = 0x01;	sprgroup_or = 0x00;	break;
		case 0xd:	sprgroup_shift = 12;	sprgroup_mask = 0x03;	sprgroup_or = 0x00;	break;
		case 0xe:	sprgroup_shift = 11;	sprgroup_mask = 0x07;	sprgroup_or = 0x00;	break;
		case 0xf:	sprgroup_shift = 10;	sprgroup_mask = 0x0f;	sprgroup_or = 0x00;	break;
	}
	sprshadowmask = (state->m_mixer_control[which][0x4c/2] & 0x04) ? 0x8000 : 0x0000;
	sprpixmask = ((1 << sprgroup_shift) - 1) & 0x3fff;
	sprshadow = 0x7ffe & sprpixmask;

	/* extract info about TEXT, NBG0-3, and BITMAP layers, which all follow the same pattern */
	numlayers = 0;
	for (laynum = MIXER_LAYER_TEXT; laynum <= MIXER_LAYER_BITMAP; laynum++)
	{
		int priority = state->m_mixer_control[which][0x20/2 + laynum] & 0x0f;
		if ((enablemask & (1 << laynum)) && priority != 0)
		{
			layersort[numlayers].index = laynum;
			layersort[numlayers].effpri = (priority << 3) | (6 - laynum);
			layersort[numlayers].palbase = (state->m_mixer_control[which][0x20/2 + laynum] & 0x00f0) << 6;
			layersort[numlayers].mixshift = (state->m_mixer_control[which][0x20/2 + laynum] >> 8) & 3;
			layersort[numlayers].blendmask = blendenable ? ((state->m_mixer_control[which][0x30/2 + laynum] >> 6) & 0xff) : 0;
			layersort[numlayers].sprblendmask = compute_sprite_blend(state->m_mixer_control[which][0x30/2 + laynum] & 0x3f);
			layersort[numlayers].coloroffs = compute_color_offsets(state, which, (state->m_mixer_control[which][0x3e/2] >> laynum) & 1, (state->m_mixer_control[which][0x30/2 + laynum] >> 14) & 1);
			numlayers++;
		}
	}

	/* extract info about the BACKGROUND layer */
	layersort[numlayers].index = MIXER_LAYER_BACKGROUND;
	layersort[numlayers].effpri = (1 << 3) | 0;
	layersort[numlayers].palbase = (state->m_mixer_control[which][0x2c/2] & 0x00f0) << 6;
	layersort[numlayers].mixshift = (state->m_mixer_control[which][0x2c/2] >> 8) & 3;
	layersort[numlayers].blendmask = 0;
	layersort[numlayers].sprblendmask = 0;
	layersort[numlayers].coloroffs = compute_color_offsets(state, which, (state->m_mixer_control[which][0x3e/2] >> 8) & 1, (state->m_mixer_control[which][0x3e/2] >> 14) & 1);
	numlayers++;

	/* now bubble sort the list by effective priority */
	for (laynum = 0; laynum < numlayers; laynum++)
		for (i = laynum + 1; i < numlayers; i++)
			if (layersort[i].effpri > layersort[laynum].effpri)
			{
				struct mixer_layer_info temp = layersort[i];
				layersort[i] = layersort[laynum];
				layersort[laynum] = temp;
			}

	/* for each possible sprite group, insert the sprites into the list at the appropriate point */
	for (groupnum = 0; groupnum <= sprgroup_mask; groupnum++)
	{
		int effgroup = sprgroup_or | groupnum;
		int priority = state->m_mixer_control[which][0x00/2 + effgroup] & 0x0f;
		int effpri = (priority << 3) | 7;
		int sprindex = numlayers;
		int dstnum = 0;

		/* make a copy of the sorted list, finding a location for the sprite entry */
		for (laynum = 0; laynum < numlayers; laynum++)
		{
			if (effpri > layersort[laynum].effpri && sprindex == numlayers)
				sprindex = dstnum++;
			layerorder[groupnum][dstnum++] = layersort[laynum];
		}

		/* build the sprite entry */
		layerorder[groupnum][sprindex].index = MIXER_LAYER_SPRITES;
		layerorder[groupnum][sprindex].effpri = effpri;
		if ((state->m_mixer_control[which][0x4c/2] & 3) != 3)
			layerorder[groupnum][sprindex].palbase = (state->m_mixer_control[which][0x00/2 + effgroup] & 0x00f0) << 6;
		else
			layerorder[groupnum][sprindex].palbase = (state->m_mixer_control[which][0x4c/2] & 0x00f0) << 6;
		layerorder[groupnum][sprindex].mixshift = (state->m_mixer_control[which][0x00/2 + effgroup] >> 8) & 3;
		layerorder[groupnum][sprindex].blendmask = 0;
		layerorder[groupnum][sprindex].sprblendmask = 0;
		layerorder[groupnum][sprindex].coloroffs = compute_color_offsets(state, which, (state->m_mixer_control[which][0x3e/2] >> 6) & 1, (state->m_mixer_control[which][0x4c/2] >> 15) & 1);
	}
/*
{
    static const char *const layname[] = { "TEXT", "NBG0", "NBG1", "NBG2", "NBG3", "BITM", "SPRI", "LINE" };
    for (groupnum = 0; groupnum <= sprgroup_mask; groupnum++)
    {
        mame_printf_debug("%X: ", groupnum);
        for (i = 0; i <= numlayers; i++)
            mame_printf_debug("%s(%02X) ", layname[layerorder[groupnum][i].index], layerorder[groupnum][i].effpri);
        mame_printf_debug("\n");
    }
}*/

	/* based on the sprite controller flip bits, the data is scanned to us in different */
	/* directions; account for this */
	if (state->m_sprite_control_latched[0x04/2] & 1)
	{
		sprx_start = cliprect.max_x;
		sprdx = -1;
	}
	else
	{
		sprx_start = cliprect.min_x;
		sprdx = 1;
	}

	if (state->m_sprite_control_latched[0x04/2] & 2)
	{
		spry = cliprect.max_y;
		sprdy = -1;
	}
	else
	{
		spry = cliprect.min_y;
		sprdy = 1;
	}

	/* loop over rows */
	for (y = cliprect.min_y; y <= cliprect.max_y; y++, spry += sprdy)
	{
		UINT32 *dest = &bitmap.pix32(y, xoffs);
		UINT16 *layerbase[8];

		/* get the starting address for each layer */
		layerbase[MIXER_LAYER_TEXT] = get_layer_scanline(state, MIXER_LAYER_TEXT, y);
		layerbase[MIXER_LAYER_NBG0] = get_layer_scanline(state, MIXER_LAYER_NBG0, y);
		layerbase[MIXER_LAYER_NBG1] = get_layer_scanline(state, MIXER_LAYER_NBG1, y);
		layerbase[MIXER_LAYER_NBG2] = get_layer_scanline(state, MIXER_LAYER_NBG2, y);
		layerbase[MIXER_LAYER_NBG3] = get_layer_scanline(state, MIXER_LAYER_NBG3, y);
		layerbase[MIXER_LAYER_BITMAP] = get_layer_scanline(state, MIXER_LAYER_BITMAP, y);
		layerbase[MIXER_LAYER_SPRITES] = get_layer_scanline(state, MIXER_LAYER_SPRITES, spry);
		layerbase[MIXER_LAYER_BACKGROUND] = get_layer_scanline(state, MIXER_LAYER_BACKGROUND, y);

		/* loop over columns */
		for (x = cliprect.min_x, sprx = sprx_start; x <= cliprect.max_x; x++, sprx += sprdx)
		{
			struct mixer_layer_info *first;
			int *rgbdelta;
			int firstpix;
			int sprpix, sprgroup;
			int r, g, b;
			int shadow = 0;

			/* first grab the current sprite pixel and determine the group */
			sprpix = layerbase[MIXER_LAYER_SPRITES][sprx];
			sprgroup = (sprpix >> sprgroup_shift) & sprgroup_mask;

			/* now scan the layers to find the topmost non-transparent pixel */
			for (first = &layerorder[sprgroup][0]; ; first++)
			{
				laynum = first->index;

				/* non-sprite layers are treated similarly */
				if (laynum != MIXER_LAYER_SPRITES)
				{
					firstpix = layerbase[laynum][x] & 0x1fff;
					if (firstpix != 0 || laynum == MIXER_LAYER_BACKGROUND)
						break;
				}

				/* sprite layers are special */
				else
				{
					firstpix = sprpix;
					shadow = ~firstpix & sprshadowmask;
					if ((firstpix & 0x7fff) != 0x7fff)
					{
						firstpix &= sprpixmask;
						if ((firstpix & 0x7ffe) != sprshadow)
							break;
						shadow = 1;
					}
				}
			}

			/* adjust the first pixel */
			firstpix = state->m_system32_paletteram[which][(first->palbase + ((firstpix >> first->mixshift) & 0xfff0) + (firstpix & 0x0f)) & 0x3fff];

			/* compute R, G, B */
			rgbdelta = &rgboffs[first->coloroffs][0];
			r = ((firstpix >>  0) & 0x1f) + rgbdelta[0];
			g = ((firstpix >>  5) & 0x1f) + rgbdelta[1];
			b = ((firstpix >> 10) & 0x1f) + rgbdelta[2];

			/* if there are potential blends, keep looking */
			if (first->blendmask != 0)
			{
				struct mixer_layer_info *second;
				int secondpix;

				/* now scan the layers to find the topmost non-transparent pixel */
				for (second = first + 1; ; second++)
				{
					laynum = second->index;

					/* non-sprite layers are treated similarly */
					if (laynum != MIXER_LAYER_SPRITES)
					{
						secondpix = layerbase[laynum][x] & 0x1fff;
						if (secondpix != 0 || laynum == MIXER_LAYER_BACKGROUND)
							break;
					}

					/* sprite layers are special */
					else
					{
						secondpix = sprpix;
						shadow = ~secondpix & sprshadowmask;
						if ((secondpix & 0x7fff) != 0x7fff)
						{
							secondpix &= sprpixmask;
							if ((secondpix & 0x7ffe) != sprshadow)
								break;
							shadow = 1;
						}
					}
				}

				/* are we blending with that layer? */
				if ((first->blendmask & (1 << laynum)) &&
					(laynum != MIXER_LAYER_SPRITES || (first->sprblendmask & (1 << sprgroup))))
				{
					/* adjust the second pixel */
					secondpix = state->m_system32_paletteram[which][(second->palbase + ((secondpix >> second->mixshift) & 0xfff0) + (secondpix & 0x0f)) & 0x3fff];

					/* compute first RGB */
					r *= 7 - blendfactor;
					g *= 7 - blendfactor;
					b *= 7 - blendfactor;

					/* add in second RGB */
					rgbdelta = &rgboffs[second->coloroffs][0];
					r += (((secondpix >>  0) & 0x1f) + rgbdelta[0]) * (blendfactor + 1);
					g += (((secondpix >>  5) & 0x1f) + rgbdelta[1]) * (blendfactor + 1);
					b += (((secondpix >> 10) & 0x1f) + rgbdelta[2]) * (blendfactor + 1);

					/* shift off the extra bits */
					r >>= 3;
					g >>= 3;
					b >>= 3;
				}
			}

			/* apply shadow/hilight */
			if (shadow)
			{
				r >>= 1;
				g >>= 1;
				b >>= 1;
			}

			/* clamp and combine */
			if (r > 31)
				firstpix = 31 << (16+3);
			else if (r > 0)
				firstpix = r << (16+3);
			else
				firstpix = 0;

			if (g > 31)
				firstpix |= 31 << (8+3);
			else if (g > 0)
				firstpix |= g << (8+3);

			if (b > 31)
				firstpix |= 31 << (0+3);
			else if (b > 0)
				firstpix |= b << (0+3);
			dest[x] = firstpix;
		}
	}

	/* if we are the second monitor on multi32, swap back the sprite layer */
	if (which == 1)
		state->m_layer_data[MIXER_LAYER_SPRITES] = temp_sprite_save;
}



/*************************************
 *
 *  Master update routine
 *
 *************************************/

static void print_mixer_data(segas32_state *state, int which)
{
	if (++state->m_print_count > 60 * 5)
	{
		mame_printf_debug("\n");
		mame_printf_debug("OP: %04X\n", state->m_system32_videoram[0x1ff8e/2]);
		mame_printf_debug("SC: %04X %04X %04X %04X - %04X %04X %04X %04X\n",
			state->m_sprite_control_latched[0x00],
			state->m_sprite_control_latched[0x01],
			state->m_sprite_control_latched[0x02],
			state->m_sprite_control_latched[0x03],
			state->m_sprite_control_latched[0x04],
			state->m_sprite_control_latched[0x05],
			state->m_sprite_control_latched[0x06],
			state->m_sprite_control_latched[0x07]);
		mame_printf_debug("00: %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X\n",
			state->m_mixer_control[which][0x00],
			state->m_mixer_control[which][0x01],
			state->m_mixer_control[which][0x02],
			state->m_mixer_control[which][0x03],
			state->m_mixer_control[which][0x04],
			state->m_mixer_control[which][0x05],
			state->m_mixer_control[which][0x06],
			state->m_mixer_control[which][0x07],
			state->m_mixer_control[which][0x08],
			state->m_mixer_control[which][0x09],
			state->m_mixer_control[which][0x0a],
			state->m_mixer_control[which][0x0b],
			state->m_mixer_control[which][0x0c],
			state->m_mixer_control[which][0x0d],
			state->m_mixer_control[which][0x0e],
			state->m_mixer_control[which][0x0f]);
		mame_printf_debug("20: %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X\n",
			state->m_mixer_control[which][0x10],
			state->m_mixer_control[which][0x11],
			state->m_mixer_control[which][0x12],
			state->m_mixer_control[which][0x13],
			state->m_mixer_control[which][0x14],
			state->m_mixer_control[which][0x15],
			state->m_mixer_control[which][0x16],
			state->m_mixer_control[which][0x17],
			state->m_mixer_control[which][0x18],
			state->m_mixer_control[which][0x19],
			state->m_mixer_control[which][0x1a],
			state->m_mixer_control[which][0x1b],
			state->m_mixer_control[which][0x1c],
			state->m_mixer_control[which][0x1d],
			state->m_mixer_control[which][0x1e],
			state->m_mixer_control[which][0x1f]);
		mame_printf_debug("40: %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X - %04X %04X %04X %04X\n",
			state->m_mixer_control[which][0x20],
			state->m_mixer_control[which][0x21],
			state->m_mixer_control[which][0x22],
			state->m_mixer_control[which][0x23],
			state->m_mixer_control[which][0x24],
			state->m_mixer_control[which][0x25],
			state->m_mixer_control[which][0x26],
			state->m_mixer_control[which][0x27],
			state->m_mixer_control[which][0x28],
			state->m_mixer_control[which][0x29],
			state->m_mixer_control[which][0x2a],
			state->m_mixer_control[which][0x2b],
			state->m_mixer_control[which][0x2c],
			state->m_mixer_control[which][0x2d],
			state->m_mixer_control[which][0x2e],
			state->m_mixer_control[which][0x2f]);
		state->m_print_count = 0;
	}
}

SCREEN_UPDATE( system32 )
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	UINT8 enablemask;

	/* update the visible area */
	if (state->m_system32_videoram[0x1ff00/2] & 0x8000)
		screen.set_visible_area(0, 52*8-1, 0, 28*8-1);
	else
		screen.set_visible_area(0, 40*8-1, 0, 28*8-1);

	/* if the display is off, punt */
	if (!state->m_system32_displayenable[0])
	{
		bitmap.fill(get_black_pen(screen.machine()), cliprect);
		return 0;
	}

	/* update the tilemaps */
	g_profiler.start(PROFILER_USER1);
	enablemask = update_tilemaps(screen, cliprect);
	g_profiler.stop();

	/* debugging */
#if QWERTY_LAYER_ENABLE
	if (screen.machine().input().code_pressed(KEYCODE_Q)) enablemask = 0x01;
	if (screen.machine().input().code_pressed(KEYCODE_W)) enablemask = 0x02;
	if (screen.machine().input().code_pressed(KEYCODE_E)) enablemask = 0x04;
	if (screen.machine().input().code_pressed(KEYCODE_R)) enablemask = 0x08;
	if (screen.machine().input().code_pressed(KEYCODE_T)) enablemask = 0x10;
	if (screen.machine().input().code_pressed(KEYCODE_Y)) enablemask = 0x20;
#endif

	/* do the mixing */
	g_profiler.start(PROFILER_USER3);
	mix_all_layers(state, 0, 0, bitmap, cliprect, enablemask);
	g_profiler.stop();

	if (LOG_SPRITES && screen.machine().input().code_pressed(KEYCODE_L))
	{
		const rectangle &visarea = screen.visible_area();
		FILE *f = fopen("sprite.txt", "w");
		int x, y;

		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_SPRITES, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);

		f = fopen("nbg0.txt", "w");
		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_NBG0, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);

		f = fopen("nbg1.txt", "w");
		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_NBG1, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);

		f = fopen("nbg2.txt", "w");
		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_NBG2, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);

		f = fopen("nbg3.txt", "w");
		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_NBG3, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);
	}

#if SHOW_ALPHA
{
	static const char *const layername[] = { "TEXT ", "NBG0 ", "NBG1 ", "NBG2 ", "NBG3 ", "BITMAP " };
	char temp[100];
	int count = 0, i;
	sprintf(temp, "ALPHA(%d):", (state->m_mixer_control[which][0x4e/2] >> 8) & 7);
	for (i = 0; i < 6; i++)
		if (enablemask & (1 << i))
			if ((state->m_mixer_control[which][0x30/2 + i] & 0x1010) == 0x1010)
			{
				count++;
				strcat(temp, layername[i]);
			}
	if (count)
		popmessage("%s", temp);
}
#endif

#if SHOW_CLIPS
{
	int showclip = -1;

//  if (screen.machine().input().code_pressed(KEYCODE_V))
//      showclip = 0;
//  if (screen.machine().input().code_pressed(KEYCODE_B))
//      showclip = 1;
//  if (screen.machine().input().code_pressed(KEYCODE_N))
//      showclip = 2;
//  if (screen.machine().input().code_pressed(KEYCODE_M))
//      showclip = 3;
//  if (showclip != -1)
for (showclip = 0; showclip < 4; showclip++)
	{
		int flip = (state->m_system32_videoram[0x1ff00/2] >> 9) & 1;
		int clips = (state->m_system32_videoram[0x1ff06/2] >> (4 * showclip)) & 0x0f;
		if (((state->m_system32_videoram[0x1ff02/2] >> (11 + showclip)) & 1) && clips)
		{
			int i, x, y;
			for (i = 0; i < 4; i++)
				if (clips & (1 << i))
				{
					const rectangle &visarea = screen.visible_area();

					rectangle rect;
					pen_t white = get_white_pen(screen.machine());
					if (!flip)
					{
						rect.min_x = state->m_system32_videoram[0x1ff60/2 + i * 4] & 0x1ff;
						rect.min_y = state->m_system32_videoram[0x1ff62/2 + i * 4] & 0x0ff;
						rect.max_x = (state->m_system32_videoram[0x1ff64/2 + i * 4] & 0x1ff) + 1;
						rect.max_y = (state->m_system32_videoram[0x1ff66/2 + i * 4] & 0x0ff) + 1;
					}
					else
					{
						rect.max_x = (visarea.max_x + 1) - (state->m_system32_videoram[0x1ff60/2 + i * 4] & 0x1ff);
						rect.max_y = (visarea.max_y + 1) - (state->m_system32_videoram[0x1ff62/2 + i * 4] & 0x0ff);
						rect.min_x = (visarea.max_x + 1) - ((state->m_system32_videoram[0x1ff64/2 + i * 4] & 0x1ff) + 1);
						rect.min_y = (visarea.max_y + 1) - ((state->m_system32_videoram[0x1ff66/2 + i * 4] & 0x0ff) + 1);
					}
					sect_rect(&rect, &screen.visible_area());

					if (rect.min_y <= rect.max_y && rect.min_x <= rect.max_x)
					{
						for (y = rect.min_y; y <= rect.max_y; y++)
						{
							bitmap.plot(bitmap, rect.min_x, y, white);
							bitmap.plot(bitmap, rect.max_x, y, white);
						}
						for (x = rect.min_x; x <= rect.max_x; x++)
						{
							bitmap.plot(bitmap, x, rect.min_y, white);
							bitmap.plot(bitmap, x, rect.max_y, white);
						}
					}
				}
		}
	}
}
#endif

	if (PRINTF_MIXER_DATA) print_mixer_data(state, 0);
	return 0;
}


static UINT32 multi32_update(screen_device &screen, bitmap_t &bitmap, const rectangle &cliprect, int index)
{
	segas32_state *state = screen.machine().driver_data<segas32_state>();
	UINT8 enablemask;

	/* update the visible area */
	if (state->m_system32_videoram[0x1ff00/2] & 0x8000)
		screen.set_visible_area(0, 52*8-1, 0, 28*8-1);
	else
		screen.set_visible_area(0, 40*8-1, 0, 28*8-1);

	/* if the display is off, punt */
	if (!state->m_system32_displayenable[index])
	{
		bitmap.fill(get_black_pen(screen.machine()), cliprect);
		return 0;
	}

	/* update the tilemaps */
	g_profiler.start(PROFILER_USER1);
	enablemask = update_tilemaps(screen, cliprect);
	g_profiler.stop();

	/* debugging */
#if QWERTY_LAYER_ENABLE
	if (screen.machine().input().code_pressed(KEYCODE_Q)) enablemask = 0x01;
	if (screen.machine().input().code_pressed(KEYCODE_W)) enablemask = 0x02;
	if (screen.machine().input().code_pressed(KEYCODE_E)) enablemask = 0x04;
	if (screen.machine().input().code_pressed(KEYCODE_R)) enablemask = 0x08;
	if (screen.machine().input().code_pressed(KEYCODE_T)) enablemask = 0x10;
	if (screen.machine().input().code_pressed(KEYCODE_Y)) enablemask = 0x20;
#endif

	/* do the mixing */
	g_profiler.start(PROFILER_USER3);
	mix_all_layers(state, index, 0, bitmap, cliprect, enablemask);
	g_profiler.stop();

if (PRINTF_MIXER_DATA)
{
	if (!screen.machine().input().code_pressed(KEYCODE_M)) print_mixer_data(state, 0);
	else print_mixer_data(state, 1);
}
	if (LOG_SPRITES && screen.machine().input().code_pressed(KEYCODE_L))
	{
		const rectangle &visarea = screen.visible_area();
		FILE *f = fopen("sprite.txt", "w");
		int x, y;

		for (y = visarea.min_y; y <= visarea.max_y; y++)
		{
			UINT16 *src = get_layer_scanline(state, MIXER_LAYER_SPRITES, y);
			for (x = visarea.min_x; x <= visarea.max_x; x++)
				fprintf(f, "%04X ", *src++);
			fprintf(f, "\n");
		}
		fclose(f);
	}

	return 0;
}

SCREEN_UPDATE( multi32_left ) { return multi32_update(screen, bitmap, cliprect, 0); }
SCREEN_UPDATE( multi32_right ) { return multi32_update(screen, bitmap, cliprect, 1); }

/*

Blending registers:

?--- ---- ---- ----  Unknown
-c-- ---- ---- ----  Color selection
--l- ---- ---- ----  1= blend with line color
---s ---- ---- ----  1= blend with sprites, under certain conditions
---- b--- ---- ----  1= blend with bitmap
---- -3-- ---- ----  1= blend with NBG3
---- --2- ---- ----  1= blend with NBG2
---- ---1 ---- ----  1= blend with NBG1
---- ---- 0--- ----  1= blend with NBG0
---- ---- -t-- ----  1= blend with text
---- ---- --mm ----  sprite priority comparison (see below)
---- ---- ---- vvvv  sprite priority comparison value

If sprite blending is enabled, blending is only performed if the
underlying sprite pixel's group priority matches certain criteria.
These criteria are specified in the low 6 bits of the register.
If SPGP refers to the sprite pixel group prioity, then:

   if (mm == 00) blending is performed only if (vvvv == SPGP)
   if (mm == 01) blending is performed only if (vvvv >= SPGP)
   if (mm == 10) blending is performed only if (vvvv <= SPGP)
   if (mm == 11) blending is performed regardless of SPGP



    equal priority order =

        sprite
        text
        nbg0
        nbg1
        nbg2
        nbg3
        bitmap
        line



arescue:
SC: 0003 0000 0000 0002 - 0002 0003 0000 0000
00: 0011 0014 0018 001F - 0014 0015 0016 0017 - 0018 0019 001A 001B - 001C 001D 001E 001F
20: 000E 0141 0142 014E - 0146 000F 0000 0000 - 4000 4000 5008 5008 - 4000 4000 4000 4000
40: 0040 0040 0040 0000 - 0000 0000 BE4D 0C00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

alien3:
SC: 0000 0000 0000 0000 - 0001 0003 0000 0000
00: 0011 0017 001B 001F - 0014 0015 0016 0017 - 0018 0019 001A 001B - 001C 001D 001E 001F
20: 000E 014C 014A 0147 - 0144 000B 0000 0000 - 0000 0000 1008 103C - 0000 4000 0000 0000
40: FFEB FFEB FFEB 0000 - 0000 0000 BE4D 0C00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

arabfgt:
SC: 0000 0000 0000 0000 - 0000 0000 0000 0000
00: 000E 000A 0008 0006 - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 036D 036F 0366 - 0367 004F 0051 0050 - 0000 4000 57B0 4000 - 4000 0000 0000 4000
40: 0000 0000 0000 0000 - 0000 0000 8049 0F00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

brival:
SC: 0000 0000 0000 0000 - 0000 0000 0001 0000
00: 000E 000A 0008 0006 - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 036D 036F 036C - 0367 004F 0051 0050 - 0000 4000 7FF0 4000 - 4000 0000 0000 4000
40: 0000 0000 0000 0000 - 0000 0000 8049 0B00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

darkedge:
SC: 0000 0000 0000 0000 - 0001 0003 0000 0000
00: 000E 000C 0008 000E - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 025D 025B 0259 - 0257 007E 0071 0070 - 4000 0000 0000 0000 - 0000 0000 0000 0000
40: 0020 0020 0020 0020 - 0020 0020 3E4D 0C00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

dbzvrvs:
SC: 0000 0000 0000 0000 - 0001 0000 0000 0000
00: 000D 000B 0009 000F - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007E 036C 036C 036A - 036A 007C 0073 0072 - 4000 0000 0000 0000 - 0000 0000 0000 0000
40: 0000 0000 0000 0000 - 0000 0000 3E01 0300 - 0000 0000 0000 0000 - 0000 0000 0000 0000

f1en:
SC: 0000 0000 0000 0000 - 0000 0000 0000 0000
00: 000E 000A 0008 0006 - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 0361 0361 0361 - 036F 0041 0051 0050 - 4000 4000 4300 571B - 5216 0000 0000 0000
40: 0000 0000 0000 0000 - 0000 0000 3E49 0021 - 0000 0000 0000 0000 - 0000 0000 0000 0000

ga2j: - 8 priorities + shadow
SC: 0000 0000 0000 0000 - 0002 0003 0000 0000
00: 000F 000D 000B 0009 - 0007 0007 0005 0003 - 000F 000D 000B 0009 - 0007 0007 0005 0003
20: 003E 014E 0142 014C - 0148 003E 0030 0030 - 4000 5119 4099 4000 - 4000 4000 4000 C000
40: 0000 0000 0000 0000 - 0000 0000 924E 0B00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

harddunk:
SC: 0003 0000 0000 0000 - 0001 0001 0000 0000
00: 000A 0006 000E 000A - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 036D 0000 036C - 0000 0074 0072 0070 - 0000 4000 4000 4000 - 4000 0000 0000 4000
40: 0000 0000 0000 0000 - 0000 0000 9E05 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000

holo:
SC: 0003 0000 0000 0000 - 0002 0003 0000 0000
00: 0011 0014 0018 001F - 0014 0015 0016 0017 - 0018 0019 001A 001B - 001C 001D 001E 001F
20: 000F 0146 0143 014E - 014E 000E 0000 0000 - 4000 4000 5008 5008 - 4000 4000 4000 C000
40: FF00 FF00 FF00 0000 - 0000 0000 BE4D 0C00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

jpark:
SC: 0000 0000 0000 0000 - 0000 0003 0000 0000
00: 000A 000D 0007 0006 - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 0368 036B 036E - 0369 007F 0077 0076 - 0000 4100 4200 15B0 - 0100 0000 0000 8010
40: 0000 0000 0000 0000 - 0000 0000 9E4C 0B00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

orunners:
SC: 0003 0000 0000 0000 - 0001 0003 0000 0000
00: 000E 000A 0006 0002 - 000E 000A 0006 0002 - 000E 000A 0006 0002 - 000E 000A 0006 0002
20: 003C 0141 0000 0142 - 0000 0038 0030 0030 - 5030 4000 4000 4000 - 4000 4000 4000 C000
40: 0000 0000 0000 0000 - 0000 0000 BE4D 0A00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

radm: - 8 priorities + shadow
SC: 0000 0000 0000 0000 - 0001 0002 0000 0000
00: 000E 000E 000A 0008 - 0006 0004 0002 0000 - 000E 000C 000A 0008 - 0006 0004 0002 0000
20: 007F 0367 0369 036B - 036D 0071 0071 0070 - 4000 100E 100E 100E - 100E 0000 0000 0000
40: 0000 0000 0000 0000 - 0000 0000 3E49 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000

radr: - 4 priorities
SC: 0000 0000 0000 0000 - 0001 0002 0000 0000
00: 000E 000A 0008 0006 - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 036E 036C 036D - 0367 0078 0071 0070 - 4000 4000 4300 571B - 5216 0000 0000 0000
40: 0000 0000 0000 0000 - 0000 0000 3E49 0B00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

scross:
SC: 0003 0000 0000 0002 - 0001 0001 0000 0000
00: 001B 0017 001D 001F - 0010 0010 0010 0010 - 0010 0010 0010 0010 - 0010 0010 0010 0010
20: 000E 015A 0000 0156 - 0000 0000 0000 0000 - 4000 4000 4000 4000 - 4000 4000 4000 C000
40: 0000 0000 0000 0000 - 0000 0000 BE1D 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000

slipstrm:
SC: 0003 0000 00FC 0002 - 0001 0003 0001 0000
00: 004D 004D 004D 004A - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 0108 010E 010B - 0109 0000 0000 0000 - 4030 4030 4030 7C90 - 4030 4000 0000 C040
40: 0000 0000 0000 001F - 001F 001F 7F0D 0C00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

sonic: - 4 priorities
SC: 0000 0000 0000 0000 - 0001 0002 0000 0000
00: 003E 003B 003C 0036 - 0030 0030 0030 0030 - 0030 0030 0030 0030 - 0030 0030 0030 0030
20: 002F 020E 020B 0208 - 0207 0021 0021 0020 - 0000 400B 4300 471B - 4216 0000 0000 0000
40: 0000 0000 0000 0000 - 0000 0000 3E49 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000

spidman: - 8 priorities + shadow
SC: 0000 0000 0000 0000 - 0002 0003 0000 0000
00: 000E 000C 000A 0008 - 0006 0004 0002 0000 - 000E 000C 000A 0008 - 0006 0004 0002 0000
20: 003F 0149 0149 014C - 0140 003E 0030 0030 - 4000 4000 4000 4000 - 4000 4000 4000 C000
40: 0000 0000 0000 0000 - 0000 0000 BE4E 0F00 - 0000 0000 0000 0000 - 0000 0000 0000 0000

svf: - 4 priorities
SC: 0003 0000 0000 0002 - 0002 0003 0000 0000
00: 0014 0012 0018 001E - 0014 0015 0016 0017 - 0018 0019 001A 001B - 001C 001D 001E 001E
20: 000F 0143 0141 0142 - 0142 000E 0000 0000 - 4000 4000 5008 5008 - 5008 4000 4000 C001
40: 00FF 00FF 00FF 0000 - 0000 0000 BE4D 0900 - 0000 0000 0000 0000 - 0000 0000 0000 0000

titlef:
SC: 0003 0000 0000 0000 - 0001 0001 0000 0000
00: 000E 0002 0005 000F - 0000 0000 0000 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000
20: 007F 0366 0000 0364 - 0000 0070 0071 0070 - 0000 4000 4000 4000 - 4000 0000 0000 4000
40: 0000 0000 0000 0000 - 0000 0000 9E05 0000 - 0000 0000 0000 0000 - 0000 0000 0000 0000

*/