summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/model2.cpp
blob: e84d78bb020c96c8773e831cae1bb52b17e3f67a (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
// license:BSD-3-Clause
// copyright-holders:R. Belmont, Olivier Galibert, ElSemi, Angelo Salese
/*********************************************************************************************************************************

    Sega Model 2 Geometry Engine and 3D Rasterizer Emulation

    General Notes:

    - The 3D rendering system has 3 main parts:
        1) The Geometry Engine: Model 2B and 2C hardware upload the geometry code to a DSP. The original and 2A hardware
        have the geometry code in an internal ROM inside the DSP itself. The simulation written here for the geometry engine
        is based off the disassembly of the code uploaded to a 2B game.
        2) The Z-Sort and Clip Hardware: Not much information about this hardware. There are a couple of variables that
        can be passed to this stage, like a master Z-Clip enable register, and a Z-Sort mode variable.
        3) The Hardware Renderer: Not much information about the inner workings of this hardware. Based on the available
        tidbits from the 2B manual, it is a double-buffered device (can be toggled between page flipping and single
        buffer mode), and can also be switched to generate output at 60Hz or 30Hz.

    - Most of the information used to write this code come from 3 main sources:
        1) The Geometry Engine code disassembly from a 2B game.
        2) The Model 2B-CRX Manual.
        3) ElSemi's Direct3D implementation of the geometrizer engine.

    - The emulation strategy used here is to decouple the geometrizer code from the Z-Sort/Clip/Renderer code, so that for
    original and 2A games, the HLE version of the geometrizer can be used, while for 2B and 2C games, the original geometrizer
    DSP can be emulated, and data can be pushed to the Z-Sort/Clip/Renderer code.



    Geometry Engine Notes and Known Bugs:

    - Top Skater seem to use a slightly different geometry code that has a secondary transformation matrix. Once we implement
    the real DSP code upload and emulation for 2B and 2C games, this should not be an issue.



    Z-Sort Notes and Known Bugs:

    - The Z-Sort algorithm is not entirely understood. The manual states that the Z-Sort works internally in 1.4.8 bit format,
    but then it states that the Z-Sort Mode register value is added to the floating point z value, and converted to a 16 bit
    format (most likely 4.12).
    - The system allows for Z-Sort override, by means of specifying whether a polygon will use the same Z value ordinal as the
    previous polygon, or the calculated minimum or maximum from it's points. This allows for a full object to be in front of
    another, even though the first object might have some z coordinates that are bigger than the second object's z coordinates.
    - The current implementation takes the effective computed z value for the polygon and converts it into a 4.12 fixed point
    representation, used as an index into an array of linked polygons. Every polygon with the same z value is linked with any
    previous polygon that had that same z value:

        z-value index   linked list of polygons
            0000        triangle3->triangle2->triangle1
            0001        triangle22->triangle8->triangle7
            ....
            FFFF        triangle33->triangle11->triangle9

    As we add polygons to the array, we also keep track of the minimum and maximum z-value indexes seen this frame.
    When it's time to render, we start and the max z-value seen, and iterate through the array going down to the minimum
    z-value seen, clipping and rendering the linked polygons as we go.
    Unfortunately, it seems there's something not quite right, as there are some visible Z-Sort problems in various games.
    Perhaps the linked list of polygons need to be presorted by a unknown factor before rendering.



    Clip Notes and Known Bugs:

    - The Z-Clip happens at z=0. Since dividing by 0 during projection would give bogus values, we scale by a distance of
    1, thus our projection equation turns into vd.x = (d*vs.x) / (d+vs.z) where d = 1, since we know we won't have polygons
    with a z < 0. This, however, poses the problem that for objects originally between 0.0 and 1.0 are not going to be
    scaled properly. We still need to find a solution for this.
    - A small offset need to be added horizontally and vertically to the viewport and center variables for certain games (like
    the original Model 2 games). The coordinate system has been worked out from the 2B specifications, but the older games
    need a slight adjustment.



    Hardware Renderer Notes and Known Bugs:

    - Texturing code could use a real good speed optimization.
    - The U and V coordinates provided by the game are in 13.3 fixed point format.
    - The luma/texel combination algorithm is not known. There are currently some small color glitches here and
    there, and this might be the culprit.
    - The log tables and distance coefficients are used to calculate the number of texels per world unit that need to
    be used to render a texture. Textures can also be provided with smaller levels of details and a LOD bit selector
    in the texture header tells the rasterizer which texture map to use. The rasterizer then can average two texture
    maps to do mip mapping. More information can be found on the 2B manual, on the 'Texturing' and 'Data Format' chapters.
    This is currently unemulated. We always use the texture data from the bigger texture map.
    - The rasterizer supports up to 128x128 'microtex' textures, which are supposed to be higher resolution textures used
    to display more detail when a texture is real close to the viewer. This is currently unemulated.

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

#include "emu.h"
#include "includes/model2.h"

#include <cmath>

#define pz      p[0]
#define pu      p[1]
#define pv      p[2]



/*******************************************
 *
 *  Hardware 3D Rasterizer Internal State
 *
 *******************************************/

#define MAX_TRIANGLES       32768

struct raster_state
{
//  uint32_t mode;                      /* bit 0 = Test Mode, bit 2 = Switch 60Hz(1)/30Hz(0) operation */
	uint16_t *texture_rom;              /* Texture ROM pointer */
	uint32_t texture_rom_mask;          /* Texture ROM mask */
	int16_t viewport[4];                /* View port (startx,starty,endx,endy) */
	int16_t center[4][2];               /* Centers (eye 0[x,y],1[x,y],2[x,y],3[x,y]) */
	uint16_t center_sel;                /* Selected center */
	uint32_t reverse;                   /* Left/Right Reverse */
	float z_adjust;                     /* ZSort Mode */
	float triangle_z;                   /* Current Triangle z value */
	uint8_t master_z_clip;              /* Master Z-Clip value */
	uint32_t cur_command;               /* Current command */
	uint32_t command_buffer[32];        /* Command buffer */
	uint32_t command_index;             /* Command buffer index */
	triangle tri_list[MAX_TRIANGLES];   /* Triangle list */
	uint32_t tri_list_index;            /* Triangle list index */
	triangle *tri_sorted_list[0x10000]; /* Sorted Triangle list */
	uint16_t min_z;                     /* Minimum sortable Z value */
	uint16_t max_z;                     /* Maximum sortable Z value */
	uint16_t texture_ram[0x10000];      /* Texture RAM pointer */
	uint8_t log_ram[0x40000];           /* Log RAM pointer */
};

/*******************************************
 *
 *  Geometry Engine Internal State
 *
 *******************************************/

struct geo_state
{
	raster_state *          raster;
	uint32_t              mode;                   /* bit 0 = Enable Specular, bit 1 = Calculate Normals */
	uint32_t *          polygon_rom;            /* Polygon ROM pointer */
	uint32_t            polygon_rom_mask;       /* Polygon ROM mask */
	float               matrix[12];             /* Current Transformation Matrix */
	poly_vertex         focus;                  /* Focus (x,y) */
	poly_vertex         light;                  /* Light Vector */
	float               lod;                    /* LOD */
	float               coef_table[32];         /* Distane Coefficient table */
	texture_parameter   texture_parameters[32]; /* Texture parameters */
	uint32_t          polygon_ram0[0x8000];           /* Fast Polygon RAM pointer */
	uint32_t          polygon_ram1[0x8000];           /* Slow Polygon RAM pointer */
	model2_state    *state;
};


/*******************************************
 *
 *  Generic 3D Math Functions
 *
 *******************************************/

static inline void transform_point( poly_vertex *point, float *matrix )
{
	float tx = (point->x * matrix[0]) + (point->y * matrix[3]) + (point->pz * matrix[6]) + (matrix[9]);
	float ty = (point->x * matrix[1]) + (point->y * matrix[4]) + (point->pz * matrix[7]) + (matrix[10]);
	float tz = (point->x * matrix[2]) + (point->y * matrix[5]) + (point->pz * matrix[8]) + (matrix[11]);

	point->x = tx;
	point->y = ty;
	point->pz = tz;
}

static inline void transform_vector( poly_vertex *vector, float *matrix )
{
	float tx = (vector->x * matrix[0]) + (vector->y * matrix[3]) + (vector->pz * matrix[6]);
	float ty = (vector->x * matrix[1]) + (vector->y * matrix[4]) + (vector->pz * matrix[7]);
	float tz = (vector->x * matrix[2]) + (vector->y * matrix[5]) + (vector->pz * matrix[8]);

	vector->x = tx;
	vector->y = ty;
	vector->pz = tz;
}

static inline void normalize_vector( poly_vertex *vector )
{
	float n = sqrt( (vector->x * vector->x) + (vector->y * vector->y) + (vector->pz * vector->pz) );

	if ( n )
	{
		float oon = 1.0f / n;
		vector->x *= oon;
		vector->y *= oon;
		vector->pz *= oon;
	}
}

static inline float dot_product( poly_vertex *v1, poly_vertex *v2 )
{
	return (v1->x * v2->x) + (v1->y * v2->y) + (v1->pz * v2->pz);
}

static inline void vector_cross3( poly_vertex *dst, poly_vertex *v0, poly_vertex *v1, poly_vertex *v2 )
{
	poly_vertex p1, p2;

	p1.x = v1->x - v0->x;   p1.y = v1->y - v0->y;   p1.pz = v1->pz - v0->pz;
	p2.x = v2->x - v0->x;   p2.y = v2->y - v0->y;   p2.pz = v2->pz - v0->pz;

	dst->x = (p1.y * p2.pz) - (p1.pz * p2.y);
	dst->y = (p1.pz * p2.x) - (p1.x * p2.pz);
	dst->pz = (p1.x * p2.y) - (p1.y * p2.x);
}

static inline void apply_focus( geo_state *geo, poly_vertex *p0)
{
	p0->x *= geo->focus.x;
	p0->y *= geo->focus.y;
}

/* 1.8.23 float to 4.12 float converter, courtesy of Aaron Giles */
inline uint16_t model2_state::float_to_zval( float floatval )
{
	int32_t fpint = f2u(floatval);
	int32_t exponent = ((fpint >> 23) & 0xff) - 127;
	uint32_t mantissa = fpint & 0x7fffff;

	/* round the low bits and reduce to 12 */
	mantissa += 0x400;
	if (mantissa > 0x7fffff) { exponent++; mantissa = (mantissa & 0x7fffff) >> 1; }
	mantissa >>= 11;

	/* if negative, clamp to 0 */
	if (fpint < 0)
		return 0x0000;

	/* the rest depends on the exponent */
	/* less than -12 is too small, return 0 */
	if ( exponent < -12 )
		return 0x0000;

	/* between -12 and 0 create a denormal with exponent of 0 */
	if ( exponent < 0 )
		return (mantissa | 0x1000) >> -exponent;

	/* between 0 and 14 create a FP value with exponent + 1 */
	if ( exponent < 15 )
		return (( exponent + 1 ) << 12) | mantissa;

	/* above 14 is too large */
	return 0xffff;
}

static int32_t clip_polygon(poly_vertex *v, int32_t num_vertices, poly_vertex *vout)
{
	poly_vertex *cur, *out;
	float   curdot, nextdot, scale;
	int32_t   i, curin, nextin, nextvert, outcount;
	plane clip_plane;

	clip_plane.normal.x = 0.0f;
	clip_plane.normal.y = 0.0f;
	clip_plane.normal.pz = 1.0f;
	clip_plane.distance = 0.0f;

	outcount = 0;

	cur = v;
	out = vout;

	curdot = dot_product( cur, &clip_plane.normal );
	curin = (curdot >= clip_plane.distance) ? 1 : 0;

	for( i = 0; i < num_vertices; i++ )
	{
		nextvert = (i + 1) % num_vertices;

		/* if the current point is inside the plane, add it */
		if ( curin ) memcpy( &out[outcount++], cur, sizeof( poly_vertex ) );

		nextdot = dot_product( &v[nextvert], &clip_plane.normal );
		nextin = (nextdot >= clip_plane.distance) ? 1 : 0;

		/* Add a clipped vertex if one end of the current edge is inside the plane and the other is outside */
		// TODO: displaying Honey in Fighting Vipers and Bean in Sonic the Fighters somehow causes a NaN dot product here,
		//       causing MAME to hardlock in the renderer routine. They are also causing lots of invalid polygon renders
		//       which might be related.
		if ( curin != nextin && std::isnan(curdot) == false && std::isnan(nextdot) == false )
		{
			scale = (clip_plane.distance - curdot) / (nextdot - curdot);

			out[outcount].x = cur->x + ((v[nextvert].x - cur->x) * scale);
			out[outcount].y = cur->y + ((v[nextvert].y - cur->y) * scale);
			out[outcount].pz = cur->pz + ((v[nextvert].pz - cur->pz) * scale);
			out[outcount].pu = (uint16_t)((float)cur->pu + (((float)v[nextvert].pu - (float)cur->pu) * scale));
			out[outcount].pv = (uint16_t)((float)cur->pv + (((float)v[nextvert].pv - (float)cur->pv) * scale));
			outcount++;
		}

		curdot = nextdot;
		curin = nextin;
		cur++;
	}

	return outcount;
}

inline bool model2_state::check_culling( raster_state *raster, uint32_t attr, float min_z, float max_z )
{
	/* if doubleside is disabled */
	if ( ((attr >> 17) & 1) == 0 )
	{
		/* if it's the backface, cull it */
		if ( raster->command_buffer[9] & 0x00800000 )
			return true;
	}

	/* if the linktype is 0, then we can also cull it */
	if ( ((attr >> 8) & 3) == 0 )
		return true;

	/* if the minimum z value is bigger than the master z clip value, don't render */
	if ( (int32_t)(1.0/min_z) > raster->master_z_clip )
		return true;

	/* if the maximum z value is < 0 then we can safely clip the entire polygon */
	if ( max_z < 0 )
		return true;

	return false;
}

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

/*******************************************
 *
 *  Hardware 3D Rasterizer Initialization
 *
 *******************************************/

void model2_state::raster_init( memory_region *texture_rom )
{
	m_raster = auto_alloc_clear(machine(), <raster_state>());

	m_raster->texture_rom = (uint16_t *)texture_rom->base();
	m_raster->texture_rom_mask = (texture_rom->bytes() / 2) - 1;

	save_item(NAME(m_raster->min_z));
	save_item(NAME(m_raster->max_z));
//  save_item(NAME(m_raster->tri_list));
	save_item(NAME(m_raster->tri_list_index));
	save_item(NAME(m_raster->command_buffer));
	save_item(NAME(m_raster->command_index));
	save_item(NAME(m_raster->cur_command));
	save_item(NAME(m_raster->master_z_clip));
	save_item(NAME(m_raster->triangle_z));
	save_item(NAME(m_raster->z_adjust));
	save_item(NAME(m_raster->reverse));
	save_item(NAME(m_raster->viewport));
	save_item(NAME(m_raster->center));
	save_item(NAME(m_raster->center_sel));
	save_item(NAME(m_raster->texture_ram));
	save_item(NAME(m_raster->log_ram));
}

/*******************************************
 *
 *  Hardware 3D Rasterizer Z-Clip selection
 *
 *******************************************/

WRITE32_MEMBER(model2_state::model2_3d_zclip_w)
{
	m_raster->master_z_clip = data;
}

// TODO: only Sky Target seems to use this for unknown purpose
READ32_MEMBER(model2_state::polygon_count_r)
{
//  printf("%08x\n",m_raster->tri_list_index);

	return m_raster->tri_list_index;
}

/*******************************************
 *
 *  Hardware 3D Rasterizer Processing
 *
 *******************************************/

void model2_state::model2_3d_process_quad( raster_state *raster, uint32_t attr )
{
	quad_m2 object;
	uint16_t *th, *tp;
	int32_t tho;
	uint32_t i;
	bool cull;
	float zvalue;
	float min_z, max_z;

	/* extract P0(n-1) */
	object.v[1].x = u2f( raster->command_buffer[2] << 8 );
	object.v[1].y = u2f( raster->command_buffer[3] << 8 );
	object.v[1].pz = u2f( raster->command_buffer[4] << 8 );

	/* extract P1(n-1) */
	object.v[0].x = u2f( raster->command_buffer[5] << 8 );
	object.v[0].y = u2f( raster->command_buffer[6] << 8 );
	object.v[0].pz = u2f( raster->command_buffer[7] << 8 );

	/* extract P0(n) */
	object.v[2].x = u2f( raster->command_buffer[11] << 8 );
	object.v[2].y = u2f( raster->command_buffer[12] << 8 );
	object.v[2].pz = u2f( raster->command_buffer[13] << 8 );

	/* extract P1(n) */
	object.v[3].x = u2f( raster->command_buffer[14] << 8 );
	object.v[3].y = u2f( raster->command_buffer[15] << 8 );
	object.v[3].pz = u2f( raster->command_buffer[16] << 8 );

	/* always calculate the min z and max z value */
	min_z = object.v[0].pz;
	if ( object.v[1].pz < min_z ) min_z = object.v[1].pz;
	if ( object.v[2].pz < min_z ) min_z = object.v[2].pz;
	if ( object.v[3].pz < min_z ) min_z = object.v[3].pz;

	max_z = object.v[0].pz;
	if ( object.v[1].pz > max_z ) max_z = object.v[1].pz;
	if ( object.v[2].pz > max_z ) max_z = object.v[2].pz;
	if ( object.v[3].pz > max_z ) max_z = object.v[3].pz;

	/* read in the texture information */

	/* texture point data */
	if ( raster->command_buffer[0] & 0x800000 )
		tp = &raster->texture_ram[raster->command_buffer[0] & 0xFFFF];
	else
		tp = &raster->texture_rom[raster->command_buffer[0] & raster->texture_rom_mask];

	object.v[0].pv = *tp++;
	object.v[0].pu = *tp++;
	object.v[1].pv = *tp++;
	object.v[1].pu = *tp++;
	object.v[2].pv = *tp++;
	object.v[2].pu = *tp++;
	object.v[3].pv = *tp++;
	object.v[3].pu = *tp++;

	/* update the address */
	raster->command_buffer[0] += 8;

	/* texture header data */
	if ( raster->command_buffer[1] & 0x800000 )
		th = &raster->texture_ram[raster->command_buffer[1] & 0xFFFF];
	else
		th = &raster->texture_rom[raster->command_buffer[1] & raster->texture_rom_mask];

	object.texheader[0] = *th++;
	object.texheader[1] = *th++;
	object.texheader[2] = *th++;
	object.texheader[3] = *th++;

	/* extract the texture header offset */
	tho = (attr >> 12) & 0x1F;

	/* adjust for sign */
	if ( tho & 0x10 )
		tho |= -16;

	/* update the address */
	raster->command_buffer[1] += tho * 4;

	/* set the luma value of this quad */
	object.luma = (raster->command_buffer[9] >> 15) & 0xFF;

	/* determine whether we can cull this quad */
	cull = check_culling(raster,attr,min_z,max_z);

	/* set the object's z value */
	switch((attr >> 10) & 3)
	{
		case 0: // old value
			zvalue = raster->triangle_z;
			break;
		case 1: // min z
			zvalue = min_z;
			break;
		case 2: // max z
			zvalue = max_z;
			break;
		case 3: // error
		default:
			zvalue = 1e10;
			break;
	}

	raster->triangle_z = zvalue;

	if ( cull == false )
	{
		int32_t clipped_verts;
		poly_vertex verts[10];

		/* do near z clipping */
		clipped_verts = clip_polygon( object.v, 4, verts);

		if ( clipped_verts > 2 )
		{
			triangle *ztri;

			/* adjust and set the object z-sort value */
			object.z = float_to_zval( zvalue + raster->z_adjust );

			/* get our list read to add the triangles */
			ztri = raster->tri_sorted_list[object.z];

			if ( ztri != nullptr )
			{
				while( ztri->next != nullptr )
					ztri = (triangle *)ztri->next;
			}

			/* go through the clipped vertex list, adding triangles */
			for( i = 2; i < clipped_verts; i++ )
			{
				triangle    *tri;

				tri = &raster->tri_list[raster->tri_list_index++];

				if ( raster->tri_list_index >= MAX_TRIANGLES )
				{
					fatalerror( "SEGA 3D: Max triangle limit exceeded\n" );
				}

				/* copy the object information */
				tri->z = object.z;
				tri->texheader[0] = object.texheader[0];
				tri->texheader[1] = object.texheader[1];
				tri->texheader[2] = object.texheader[2];
				tri->texheader[3] = object.texheader[3];
				tri->luma = object.luma;

				/* set the viewport */
				tri->viewport[0] = raster->viewport[0];
				tri->viewport[1] = raster->viewport[1];
				tri->viewport[2] = raster->viewport[2];
				tri->viewport[3] = raster->viewport[3];

				/* set the center */
				tri->center[0] = raster->center[raster->center_sel][0];
				tri->center[1] = raster->center[raster->center_sel][1];

				memcpy( &tri->v[0], &verts[0], sizeof( poly_vertex ) );
				memcpy( &tri->v[1], &verts[i-1], sizeof( poly_vertex ) );
				memcpy( &tri->v[2], &verts[i], sizeof( poly_vertex ) );

				/* add to our sorted list */
				tri->next = nullptr;

				if ( ztri == nullptr )
				{
					raster->tri_sorted_list[object.z] = tri;
				}
				else
				{
					ztri->next = tri;
				}

				ztri = tri;
			}

			/* keep around the min and max z values for this frame */
			if ( object.z < raster->min_z ) raster->min_z = object.z;
			if ( object.z > raster->max_z ) raster->max_z = object.z;
		}
	}

	/* update linking */
	switch( ((attr >> 8) & 3) )
	{
		case 0:
		case 2:
		{
			/* reuse P0(n) and P1(n) */
			for( i = 0; i < 6; i++ )                                        /* P0(n) -> P0(n-1) */
				raster->command_buffer[2+i] = raster->command_buffer[11+i]; /* P1(n) -> P1(n-1) */
		}
		break;

		case 1:
		{
			/* reuse P0(n-1) and P0(n) */
			for( i = 0; i < 3; i++ )
				raster->command_buffer[5+i] = raster->command_buffer[11+i]; /* P0(n) -> P1(n-1) */
		}
		break;

		case 3:
		{
			/* reuse P1(n-1) and P1(n) */
			for( i = 0; i < 3; i++ )
				raster->command_buffer[2+i] = raster->command_buffer[14+i]; /* P1(n) -> P1(n-1) */
		}
		break;
	}
}

void model2_state::model2_3d_process_triangle( raster_state *raster, uint32_t attr )
{
	triangle object;
	uint16_t *th, *tp;
	int32_t tho;
	uint32_t i;
	bool cull;
	float zvalue;
	float min_z, max_z;

	/* extract P0(n-1) */
	object.v[1].x = u2f( raster->command_buffer[2] << 8 );
	object.v[1].y = u2f( raster->command_buffer[3] << 8 );
	object.v[1].pz = u2f( raster->command_buffer[4] << 8 );

	/* extract P1(n-1) */
	object.v[0].x = u2f( raster->command_buffer[5] << 8 );
	object.v[0].y = u2f( raster->command_buffer[6] << 8 );
	object.v[0].pz = u2f( raster->command_buffer[7] << 8 );

	/* extract P0(n) */
	object.v[2].x = u2f( raster->command_buffer[11] << 8 );
	object.v[2].y = u2f( raster->command_buffer[12] << 8 );
	object.v[2].pz = u2f( raster->command_buffer[13] << 8 );

	/* for triangles, the rope of P1(n) is achieved by P0(n-1) (linktype 3) */
	raster->command_buffer[14] = raster->command_buffer[11];
	raster->command_buffer[15] = raster->command_buffer[12];
	raster->command_buffer[16] = raster->command_buffer[13];

	/* always calculate the min z and max z values */
	min_z = object.v[0].pz;
	if ( object.v[1].pz < min_z ) min_z = object.v[1].pz;
	if ( object.v[2].pz < min_z ) min_z = object.v[2].pz;

	max_z = object.v[0].pz;
	if ( object.v[1].pz > max_z ) max_z = object.v[1].pz;
	if ( object.v[2].pz > max_z ) max_z = object.v[2].pz;

	/* read in the texture information */

	/* texture point data */
	if ( raster->command_buffer[0] & 0x800000 )
		tp = &raster->texture_ram[raster->command_buffer[0] & 0xFFFF];
	else
		tp = &raster->texture_rom[raster->command_buffer[0] & raster->texture_rom_mask];

	object.v[0].pv = *tp++;
	object.v[0].pu = *tp++;
	object.v[1].pv = *tp++;
	object.v[1].pu = *tp++;
	object.v[2].pv = *tp++;
	object.v[2].pu = *tp++;

	/* update the address */
	raster->command_buffer[0] += 6;

	/* texture header data */
	if ( raster->command_buffer[1] & 0x800000 )
		th = &raster->texture_ram[raster->command_buffer[1] & 0xFFFF];
	else
		th = &raster->texture_rom[raster->command_buffer[1] & raster->texture_rom_mask];

	object.texheader[0] = *th++;
	object.texheader[1] = *th++;
	object.texheader[2] = *th++;
	object.texheader[3] = *th++;

	/* extract the texture header offset */
	tho = (attr >> 12) & 0x1F;

	/* adjust for sign */
	if ( tho & 0x10 )
		tho |= -16;

	/* update the address */
	raster->command_buffer[1] += tho * 4;

	/* set the luma value of this quad */
	object.luma = (raster->command_buffer[9] >> 15) & 0xFF;

	/* determine whether we can cull this quad */
	cull = check_culling(raster,attr,min_z,max_z);

	/* set the object's z value */
	switch((attr >> 10) & 3)
	{
		case 0: // old value
			zvalue = raster->triangle_z;
			break;
		case 1: // min z
			zvalue = min_z;
			break;
		case 2: // max z
			zvalue = max_z;
			break;
		case 3: // error
		default:
			zvalue = 1e10;
			break;
	}

	raster->triangle_z = zvalue;

	/* if we're not culling, do z-clip and add to out triangle list */
	if ( cull == false )
	{
		int32_t       clipped_verts;
		poly_vertex verts[10];

		/* do near z clipping */
		clipped_verts = clip_polygon( object.v, 3, verts);

		if ( clipped_verts > 2 )
		{
			triangle *ztri;

			/* adjust and set the object z-sort value */
			object.z = float_to_zval( zvalue + raster->z_adjust );

			/* get our list read to add the triangles */
			ztri = raster->tri_sorted_list[object.z];

			if ( ztri != nullptr )
			{
				while( ztri->next != nullptr )
					ztri = (triangle *)ztri->next;
			}

			/* go through the clipped vertex list, adding triangles */
			for( i = 2; i < clipped_verts; i++ )
			{
				triangle    *tri;

				tri = &raster->tri_list[raster->tri_list_index++];

				if ( raster->tri_list_index >= MAX_TRIANGLES )
				{
					fatalerror( "SEGA 3D: Max triangle limit exceeded\n" );
				}

				/* copy the object information */
				tri->z = object.z;
				tri->texheader[0] = object.texheader[0];
				tri->texheader[1] = object.texheader[1];
				tri->texheader[2] = object.texheader[2];
				tri->texheader[3] = object.texheader[3];
				tri->luma = object.luma;

				/* set the viewport */
				tri->viewport[0] = raster->viewport[0];
				tri->viewport[1] = raster->viewport[1];
				tri->viewport[2] = raster->viewport[2];
				tri->viewport[3] = raster->viewport[3];

				/* set the center */
				tri->center[0] = raster->center[raster->center_sel][0];
				tri->center[1] = raster->center[raster->center_sel][1];

				memcpy( &tri->v[0], &verts[0], sizeof( poly_vertex ) );
				memcpy( &tri->v[1], &verts[i-1], sizeof( poly_vertex ) );
				memcpy( &tri->v[2], &verts[i], sizeof( poly_vertex ) );

				/* add to our sorted list */
				tri->next = nullptr;

				if ( ztri == nullptr )
				{
					raster->tri_sorted_list[object.z] = tri;
				}
				else
				{
					ztri->next = tri;
				}

				ztri = tri;
			}

			/* keep around the min and max z values for this frame */
			if ( object.z < raster->min_z ) raster->min_z = object.z;
			if ( object.z > raster->max_z ) raster->max_z = object.z;
		}
	}

	/* update linking */
	switch( ((attr >> 8) & 3) )
	{
		case 0:
		case 2:
		{
			/* reuse P0(n) and P1(n) */
			for( i = 0; i < 6; i++ )                                        /* P0(n) -> P0(n-1) */
				raster->command_buffer[2+i] = raster->command_buffer[11+i]; /* P1(n) -> P1(n-1) */
		}
		break;

		case 1:
		{
			/* reuse P0(n-1) and P0(n) */
			for( i = 0; i < 3; i++ )
				raster->command_buffer[5+i] = raster->command_buffer[11+i]; /* P0(n) -> P1(n-1) */
		}
		break;

		case 3:
		{
			/* reuse P1(n-1) and P1(n) */
			for( i = 0; i < 3; i++ )
				raster->command_buffer[2+i] = raster->command_buffer[14+i]; /* P1(n) -> P1(n-1) */
		}
		break;
	}
}

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

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

void model2_renderer::model2_3d_render(triangle *tri, const rectangle &cliprect)
{
	model2_renderer *poly = m_state.m_poly;
	m2_poly_extra_data& extra = poly->object_data_alloc();
	uint8_t renderer;

	/* select renderer based on attributes (bit15 = checker, bit14 = textured, bit13 = transparent */
	renderer = (tri->texheader[0] >> 13) & 7;

	/* calculate and clip to viewport */
	rectangle vp(tri->viewport[0] + m_xoffs, tri->viewport[2] + m_xoffs, (384-tri->viewport[3]) + m_yoffs, (384-tri->viewport[1]) + m_yoffs);
	vp &= cliprect;

	extra.state = &m_state;
	extra.lumabase = ((tri->texheader[1] & 0xFF) << 7) + ((tri->luma >> 5) ^ 0x7);
	extra.colorbase = (tri->texheader[3] >> 6) & 0x3FF;

	if (renderer & 2)
	{
		extra.texwidth = 32 << ((tri->texheader[0] >> 0) & 0x7);
		extra.texheight = 32 << ((tri->texheader[0] >> 3) & 0x7);
		extra.texx = 32 * ((tri->texheader[2] >> 0) & 0x1f);
		extra.texy = 32 * (((tri->texheader[2] >> 6) & 0x1f) + ( tri->texheader[2] & 0x20 ));
		/* TODO: Virtua Striker contradicts with this. */
		extra.texmirrorx = 0;//(tri->texheader[0] >> 9) & 1;
		extra.texmirrory = 0;//(tri->texheader[0] >> 8) & 1;
		extra.texsheet = (tri->texheader[2] & 0x1000) ? m_state.m_textureram1 : m_state.m_textureram0;

		tri->v[0].pz = 1.0f / (1.0f + tri->v[0].pz);
		tri->v[0].pu = tri->v[0].pu * tri->v[0].pz * (1.0f / 8.0f);
		tri->v[0].pv = tri->v[0].pv * tri->v[0].pz * (1.0f / 8.0f);
		tri->v[1].pz = 1.0f / (1.0f + tri->v[1].pz);
		tri->v[1].pu = tri->v[1].pu * tri->v[1].pz * (1.0f / 8.0f);
		tri->v[1].pv = tri->v[1].pv * tri->v[1].pz * (1.0f / 8.0f);
		tri->v[2].pz = 1.0f / (1.0f + tri->v[2].pz);
		tri->v[2].pu = tri->v[2].pu * tri->v[2].pz * (1.0f / 8.0f);
		tri->v[2].pv = tri->v[2].pv * tri->v[2].pz * (1.0f / 8.0f);

		// Note : The class model2_renderer has an array of function pointers in it named m_renderfuncs, in theory this simply
		//        needs to be passed into the render_triangle function as such model2_renderer::m_renderfuncs[renderer], but
		//        I was unable to make it work when converting to the new polygon rasterizer interface.
		switch (renderer)
		{
		case 0: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_0, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 1: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_1, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 2: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_2, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 3: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_3, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 4: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_4, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 5: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_5, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 6: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_6, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		case 7: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_7, this), 3, tri->v[0], tri->v[1], tri->v[2]); break;
		}
	}
	else
	{
		switch (renderer)
		{
		case 0: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_0, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 1: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_1, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 2: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_2, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 3: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_3, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 4: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_4, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 5: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_5, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 6: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_6, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		case 7: render_triangle(vp, render_delegate(&model2_renderer::model2_3d_render_7, this), 0, tri->v[0], tri->v[1], tri->v[2]); break;
		}
	}
}

/*
    Projection:

    According to the 2B Manual the screen coordinates are:

    (8,474)                         (504,474)
       +--------------------------------+
       |                                |
       |                                |
       |                                |
       |                                |
       |                                |
       |                                |
       |                                |
       |                                |
       +--------------------------------+
    (8,90)                          (504,90)
*/

WRITE16_MEMBER(model2_state::horizontal_sync_w)
{
	m_crtc_xoffset = 84 + (int16_t)data;
//  printf("H %04x %d %d\n",data,(int16_t)data,m_crtc_xoffset);
	m_poly->set_xoffset(m_crtc_xoffset);
}

WRITE16_MEMBER(model2_state::vertical_sync_w)
{
	m_crtc_yoffset = 130 + (int16_t)data;
//  printf("V %04x %d %d\n",data,(int16_t)data,m_crtc_yoffset);
	m_poly->set_yoffset(m_crtc_yoffset);
}

/* 3D Rasterizer projection: projects a triangle into screen coordinates */
inline void model2_state::model2_3d_project( triangle *tri )
{
	uint16_t  i;

	for( i = 0; i < 3; i++ )
	{
		/* project the vertices */
		tri->v[i].x = m_crtc_xoffset + tri->center[0] + (tri->v[i].x / (1.0f+tri->v[i].pz));
		tri->v[i].y = ((384 - tri->center[1])+m_crtc_yoffset) - (tri->v[i].y / (1.0f+tri->v[i].pz));
	}
}

/* 3D Rasterizer frame start: Resets frame variables */
void model2_state::model2_3d_frame_start( void )
{
	raster_state *raster = m_raster;

	/* reset the triangle list index */
	raster->tri_list_index = 0;

	/* reset the sorted z list */
	memset( raster->tri_sorted_list, 0, 0x10000 * sizeof( triangle * ) );

	/* reset the min-max sortable Z values */
	raster->min_z = 0xFFFF;
	raster->max_z = 0;
}

void model2_state::model2_3d_frame_end( bitmap_rgb32 &bitmap, const rectangle &cliprect )
{
	raster_state *raster = m_raster;
	int32_t z;

	/* if we have nothing to render, bail */
	if ( raster->tri_list_index == 0 )
		return;

	m_poly->destmap().fill(0x00000000, cliprect);

	/* go through the Z levels, and render each bucket */
	for( z = raster->max_z; z >= raster->min_z; z-- )
	{
		/* see if we have items at this z level */
		if ( raster->tri_sorted_list[z] != nullptr )
		{
			/* get a pointer to the first triangle */
			triangle *tri = raster->tri_sorted_list[z];

			/* and loop clipping and rendering each triangle */
			while( tri != nullptr )
			{
				/* project and render */
				model2_3d_project( tri );
				m_poly->model2_3d_render(tri, cliprect);

				tri = (triangle *)tri->next;
			}
		}
	}
	m_poly->wait("End of frame");

	copybitmap_trans(bitmap, m_poly->destmap(), 0, 0, 0, 0, cliprect, 0x00000000);
}

// direct framebuffer drawing (enabled with render test mode, Last Bronx title screen)
// pretty sure test mode cuts off DSP framebuffer drawing/clear, according to the manual description too
void model2_state::draw_framebuffer( bitmap_rgb32 &bitmap, const rectangle &cliprect )
{
	uint16_t *fbvram = &(m_screen->frame_number() & 1 ? m_fbvramB[0] : m_fbvramA[0]);
	// TODO: halved crtc values?
	int xoffs = (-m_crtc_xoffset)/2;
	int yoffs = m_crtc_yoffset/2;

	for (int y = cliprect.min_y; y <= cliprect.max_y; ++y)
	{
		for (int x = cliprect.min_x; x <= cliprect.max_x; x++)
		{
			int r,g,b;
			int offset = (x + xoffs) + (y + yoffs)*512;
			b = (fbvram[offset] >> 0) & 0x1f;
			r = (fbvram[offset] >> 5) & 0x1f;
			g = (fbvram[offset] >> 10) & 0x1f;
			r = pal5bit(r);
			g = pal5bit(g);
			b = pal5bit(b);
			bitmap.pix32(y, x) = r << 16 | g << 8 | b;
		}
	}
}

/* 3D Rasterizer main data input port */
void model2_state::model2_3d_push( raster_state *raster, uint32_t input )
{
	/* see if we have a command in progress */
	if ( raster->cur_command != 0 )
	{
		raster->command_buffer[raster->command_index++] = input;

		switch( raster->cur_command )
		{
			case 0x00:  /* NOP */
			break;

			case 0x01:  /* Polygon Data */
			{
				uint32_t  attr;

				/* start by looking if we have the basic input data */
				if ( raster->command_index < 9 )
					return;

				/* get the attributes */
				attr = raster->command_buffer[8];

				/* see if we're done */
				if ( (attr & 3) == 0 )
				{
					raster->cur_command = 0;
					return;
				}

				/* see if it's a quad or a triangle */
				if ( attr & 1 )
				{
					/* it's a quad, wait for the rest of the points */
					if ( raster->command_index < 17 )
						return;

					/* we have a full quad info, fill up our quad structure */
					model2_3d_process_quad( raster, attr );

					/* back up and wait for more data */
					raster->command_index = 8;
				}
				else
				{
					/* it's a triangle, wait for the rest of the point */
					if ( raster->command_index < 14 )
						return;

					/* we have a full quad info, fill up our quad structure */
					model2_3d_process_triangle( raster, attr );

					/* back up and wait for more data */
					raster->command_index = 8;
				}
			}
			break;

			case 0x03:  /* Window Data */
			{
				uint32_t  i;

				/* make sure we have all the data */
				if ( raster->command_index < 6 )
					return;

				/* coordinates are 12 bit signed */

				/* extract the viewport start x */
				raster->viewport[0] = (raster->command_buffer[0] >> 12) & 0xFFF;

				if ( raster->viewport[0] & 0x800 )
					raster->viewport[0] = -( 0x800 - (raster->viewport[0] & 0x7FF) );

				/* extract the viewport start y */
				raster->viewport[1] = raster->command_buffer[0] & 0xFFF;

				if ( raster->viewport[1] & 0x800 )
					raster->viewport[1] = -( 0x800 - (raster->viewport[1] & 0x7FF) );

				/* extract the viewport end x */
				raster->viewport[2] = (raster->command_buffer[1] >> 12) & 0xFFF;

				if ( raster->viewport[2] & 0x800 )
					raster->viewport[2] = -( 0x800 - (raster->viewport[2] & 0x7FF) );

				/* extract the viewport end y */
				raster->viewport[3] = raster->command_buffer[1] & 0xFFF;

				if ( raster->viewport[3] & 0x800 )
					raster->viewport[3] = -( 0x800 - (raster->viewport[3] & 0x7FF) );

				/* extract the centers */
				for( i = 0; i < 4; i++ )
				{
					/* center x */
					raster->center[i][0] = (raster->command_buffer[2+i] >> 12) & 0xFFF;

					if ( raster->center[i][0] & 0x800 )
						raster->center[i][0] = -( 0x800 - (raster->center[i][0] & 0x7FF) );

					/* center y */
					raster->center[i][1] = raster->command_buffer[2+i] & 0xFFF;

					if ( raster->center[i][1] & 0x800 )
						raster->center[i][1] = -( 0x800 - (raster->center[i][1] & 0x7FF) );
				}

				/* done with this command */
				raster->cur_command = 0;
			}
			break;

			case 0x04:  /* Texture/Log Data write */
			{
				/* make sure we have enough data */
				if ( raster->command_index < 2 )
					return;

				/* see if the count is non-zero */
				if ( raster->command_buffer[1] > 0 )
				{
					/* see if we have data available */
					if ( raster->command_index >= 3 )
					{
						/* get the address */
						uint32_t  address = raster->command_buffer[0];

						/* do the write */
						if ( address & 0x800000 )
							raster->texture_ram[address&0xFFFF] = raster->command_buffer[2];
						else
							raster->log_ram[address&0xFFFF] = raster->command_buffer[2];

						/* increment the address and decrease the count */
						raster->command_buffer[0]++;
						raster->command_buffer[1]--;

						/* decrease the index, so we keep placing data in the same slot */
						raster->command_index--;
					}
				}

				/* see if we're done with this command */
				if ( raster->command_buffer[1] == 0 )
					raster->cur_command = 0;
			}
			break;

			case 0x08:  /* ZSort mode */
			{
				/* save the zsort mode value */
				raster->z_adjust = u2f( raster->command_buffer[0] << 8 );

				/* done with this command */
				raster->cur_command = 0;
			}
			break;

			default:
			{
				fatalerror( "SEGA 3D: Unknown rasterizer command %08x\n", raster->cur_command );
			}
		}
	}
	else
	{
		/* new command */
		raster->cur_command = input & 0x0F;
		raster->command_index = 0;

		/* see if it's object data */
		if ( raster->cur_command == 1 )
		{
			/* extract reverse bit */
			raster->reverse = (input >> 4) & 1;

			/* extract center select */
			raster->center_sel = ( input >> 6 ) & 3;

			/* reset the triangle z value */
			// Zero Gunner sets backgrounds with "previous z value" mode at the start of the display list,
			// needs this to be this big in order to work properly
			raster->triangle_z = 1e10;
		}
	}
}

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

/*******************************************
 *
 *  Geometry Engine Initialization
 *
 *******************************************/

void model2_state::geo_init(memory_region *polygon_rom)
{
	m_geo = auto_alloc_clear(machine(), <geo_state>());
	m_geo->state = this;

	m_geo->raster = m_raster;
	m_geo->polygon_rom = (uint32_t *)polygon_rom->base();
	m_geo->polygon_rom_mask = (polygon_rom->bytes() / 4) - 1;

	save_item(NAME(m_geo->mode));
	save_item(NAME(m_geo->matrix));
	save_item(NAME(m_geo->lod));
	save_item(NAME(m_geo->coef_table));
	save_item(NAME(m_geo->polygon_ram0));
	save_item(NAME(m_geo->polygon_ram1));
}

/*******************************************
 *
 *  Geometry Engine Polygon Parsers
 *
 *******************************************/

/* Parse Polygons: Normals Present, No Specular case */
void model2_state::geo_parse_np_ns( geo_state *geo, uint32_t *input, uint32_t count )
{
	raster_state *raster = geo->raster;
	poly_vertex point, normal;
	uint32_t  attr, i;

	/* read the 1st point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* read the 2nd point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* loop through the following links */
	for( i = 0; i < count; i++ )
	{
		/* read in the attributes */
		attr = *input++;

		/* push to the 3d rasterizer */
		model2_3d_push( raster, attr & 0x0003FFFF );

		/* read in the normal */
		normal.x = u2f(*input++);
		normal.y = u2f(*input++);
		normal.pz = u2f(*input++);

		/* transform with the current matrix */
		transform_vector( &normal, geo->matrix );

		if ( (attr & 3) != 0 ) /* quad or triangle */
		{
			float               dotl, dotp, luminance, distance;
			float               coef, face;
			int32_t               luma;
			texture_parameter * texparam;

			/* read in the next point */
			point.x = u2f( *input++ );
			point.y = u2f( *input++ );
			point.pz = u2f( *input++ );

			/* transform with the current matrix */
			transform_point( &point, geo->matrix );

			/* calculate the dot product of the normal and the light vector */
			dotl = dot_product( &normal, &geo->light );

			/* calculate the dot product of the normal and the point */
			dotp = dot_product( &normal, &point );

			/* apply focus */
			apply_focus( geo, &point );

			/* determine whether this is the front or the back of the polygon */
			face = 0x100; /* rear */
			if ( dotp >= 0 ) face = 0; /* front */

			/* get the texture parameters */
			texparam = &geo->texture_parameters[(attr>>18) & 0x1f];

			/* calculate luminance */
			if ( (dotl * dotp) < 0 ) luminance = 0;
			else luminance = fabs( dotl );

			luminance = (luminance * texparam->diffuse) + texparam->ambient;
			luma = (int32_t)luminance;

			if ( luma > 255 ) luma = 255;
			if ( luma < 0 ) luma = 0;

			/* add the face bit to the luma */
			luma += face;

			/* extract distance coefficient */
			coef = geo->coef_table[attr>>27];

			/* calculate texture level of detail */
			distance = coef * fabs( dotp ) * geo->lod;

			/* push to the 3d rasterizer */
			model2_3d_push( raster, luma << 15 );
			model2_3d_push( raster, f2u(distance) >> 8 );
			model2_3d_push( raster, f2u(point.x) >> 8 );
			model2_3d_push( raster, f2u(point.y) >> 8 );
			model2_3d_push( raster, f2u(point.pz) >> 8 );

			/* if it's a quad, push one more point */
			if ( attr & 1 )
			{
				/* read in the next point */
				point.x = u2f( *input++ );
				point.y = u2f( *input++ );
				point.pz = u2f( *input++ );

				/* transform with the current matrix */
				transform_point( &point, geo->matrix );

				/* apply focus */
				apply_focus( geo, &point );

				/* push to the 3d rasterizer */
				model2_3d_push( raster, f2u(point.x) >> 8 );
				model2_3d_push( raster, f2u(point.y) >> 8 );
				model2_3d_push( raster, f2u(point.pz) >> 8 );
			}
			else /* triangle */
			{
				/* skip the next 3 points */
				input += 3;
			}
		}
		else /* we're done */
		{
			break;
		}
	}

	/* notify the 3d rasterizer we're done */
	model2_3d_push( raster, 0 );
}

/* Parse Polygons: Normals Present, Specular case */
void model2_state::geo_parse_np_s( geo_state *geo, uint32_t *input, uint32_t count )
{
	raster_state *raster = geo->raster;
	poly_vertex point, normal;
	uint32_t  attr, i;

	/* read the 1st point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* read the 2nd point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* loop through the following links */
	for( i = 0; i < count; i++ )
	{
		/* read in the attributes */
		attr = *input++;

		/* push to the 3d rasterizer */
		model2_3d_push( raster, attr & 0x0003FFFF );

		/* read in the normal */
		normal.x = u2f(*input++);
		normal.y = u2f(*input++);
		normal.pz = u2f(*input++);

		/* transform with the current matrix */
		transform_vector( &normal, geo->matrix );

		if ( (attr & 3) != 0 ) /* quad or triangle */
		{
			float               dotl, dotp, luminance, distance, specular;
			float               coef, face;
			int32_t               luma;
			texture_parameter * texparam;

			/* read in the next point */
			point.x = u2f( *input++ );
			point.y = u2f( *input++ );
			point.pz = u2f( *input++ );

			/* transform with the current matrix */
			transform_point( &point, geo->matrix );

			/* calculate the dot product of the normal and the light vector */
			dotl = dot_product( &normal, &geo->light );

			/* calculate the dot product of the normal and the point */
			dotp = dot_product( &normal, &point );

			/* apply focus */
			apply_focus( geo, &point );

			/* determine whether this is the front or the back of the polygon */
			face = 0x100; /* rear */
			if ( dotp >= 0 ) face = 0; /* front */

			/* get the texture parameters */
			texparam = &geo->texture_parameters[(attr>>18) & 0x1f];

			/* calculate luminance and specular */
			if ( (dotl * dotp) < 0 ) luminance = 0;
			else luminance = fabs( dotl );

			specular = ((2*dotl) * normal.pz) - geo->light.pz;
			if ( specular < 0 ) specular = 0;
			if ( texparam->specular_control == 0 ) specular = 0;
			if ( (texparam->specular_control >> 1) != 0 ) specular *= specular;
			if ( (texparam->specular_control >> 2) != 0 ) specular *= specular;
			if ( ((texparam->specular_control+1) >> 3) != 0 ) specular *= specular;

			specular *= texparam->specular_scale;

			luminance = (luminance * texparam->diffuse) + texparam->ambient + specular;
			luma = (int32_t)luminance;

			if ( luma > 255 ) luma = 255;
			if ( luma < 0 ) luma = 0;

			/* add the face bit to the luma */
			luma += face;

			/* extract distance coefficient */
			coef = geo->coef_table[attr>>27];

			/* calculate texture level of detail */
			distance = coef * fabs( dotp ) * geo->lod;

			/* push to the 3d rasterizer */
			model2_3d_push( raster, luma << 15 );
			model2_3d_push( raster, f2u(distance) >> 8 );
			model2_3d_push( raster, f2u(point.x) >> 8 );
			model2_3d_push( raster, f2u(point.y) >> 8 );
			model2_3d_push( raster, f2u(point.pz) >> 8 );

			/* if it's a quad, push one more point */
			if ( attr & 1 )
			{
				/* read in the next point */
				point.x = u2f( *input++ );
				point.y = u2f( *input++ );
				point.pz = u2f( *input++ );

				/* transform with the current matrix */
				transform_point( &point, geo->matrix );

				/* apply focus */
				apply_focus( geo, &point );

				/* push to the 3d rasterizer */
				model2_3d_push( raster, f2u(point.x) >> 8 );
				model2_3d_push( raster, f2u(point.y) >> 8 );
				model2_3d_push( raster, f2u(point.pz) >> 8 );
			}
			else /* triangle */
			{
				/* skip the next 3 points */
				input += 3;
			}
		}
		else /* we're done */
		{
			break;
		}
	}

	/* notify the 3d rasterizer we're done */
	model2_3d_push( raster, 0 );
}

/* Parse Polygons: No Normals, No Specular case */
void model2_state::geo_parse_nn_ns( geo_state *geo, uint32_t *input, uint32_t count )
{
	raster_state *raster = geo->raster;
	poly_vertex point, normal, p0, p1, p2, p3;
	uint32_t  attr, i;

	/* read the 1st point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* save for normal calculation */
	p0.x = point.x; p0.y = point.y; p0.pz = point.pz;

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* read the 2nd point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* save for normal calculation */
	p1.x = point.x; p1.y = point.y; p1.pz = point.pz;

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* loop through the following links */
	for( i = 0; i < count; i++ )
	{
		/* read in the attributes */
		attr = *input++;

		/* push to the 3d rasterizer */
		model2_3d_push( raster, attr & 0x0003FFFF );

		if ( (attr & 3) != 0 ) /* quad or triangle */
		{
			float               dotl, dotp, luminance, distance;
			float               coef, face;
			int32_t               luma;
			texture_parameter * texparam;

			/* Skip normal */
			input += 3;

			/* read in the next point */
			point.x = u2f( *input++ );
			point.y = u2f( *input++ );
			point.pz = u2f( *input++ );

			/* transform with the current matrix */
			transform_point( &point, geo->matrix );

			/* save for normal calculation */
			p2.x = point.x; p2.y = point.y; p2.pz = point.pz;

			/* compute the normal */
			vector_cross3( &normal, &p0, &p1, &p2 );

			/* normalize it */
			normalize_vector( &normal );

			/* calculate the dot product of the normal and the light vector */
			dotl = dot_product( &normal, &geo->light );

			/* calculate the dot product of the normal and the point */
			dotp = dot_product( &normal, &point );

			/* apply focus */
			apply_focus( geo, &point );

			/* determine whether this is the front or the back of the polygon */
			face = 0x100; /* rear */
			if ( dotp >= 0 ) face = 0; /* front */

			/* get the texture parameters */
			texparam = &geo->texture_parameters[(attr>>18) & 0x1f];

			/* calculate luminance */
			if ( (dotl * dotp) < 0 ) luminance = 0;
			else luminance = fabs( dotl );

			luminance = (luminance * texparam->diffuse) + texparam->ambient;
			luma = (int32_t)luminance;

			if ( luma > 255 ) luma = 255;
			if ( luma < 0 ) luma = 0;

			/* add the face bit to the luma */
			luma += face;

			/* extract distance coefficient */
			coef = geo->coef_table[attr>>27];

			/* calculate texture level of detail */
			distance = coef * fabs( dotp ) * geo->lod;

			/* push to the 3d rasterizer */
			model2_3d_push( raster, luma << 15 );
			model2_3d_push( raster, f2u(distance) >> 8 );
			model2_3d_push( raster, f2u(point.x) >> 8 );
			model2_3d_push( raster, f2u(point.y) >> 8 );
			model2_3d_push( raster, f2u(point.pz) >> 8 );

			/* if it's a quad, push one more point */
			if ( attr & 1 )
			{
				/* read in the next point */
				point.x = u2f( *input++ );
				point.y = u2f( *input++ );
				point.pz = u2f( *input++ );

				/* transform with the current matrix */
				transform_point( &point, geo->matrix );

				/* save for normal calculation */
				p3.x = point.x; p3.y = point.y; p3.pz = point.pz;

				/* apply focus */
				apply_focus( geo, &point );

				/* push to the 3d rasterizer */
				model2_3d_push( raster, f2u(point.x) >> 8 );
				model2_3d_push( raster, f2u(point.y) >> 8 );
				model2_3d_push( raster, f2u(point.pz) >> 8 );
			}
			else
			{
				/* skip the next 3 points */
				input += 3;

				/* for triangles, the rope of P1(n) is achieved by P0(n-1) (linktype 3) */
				p3.x = p2.x; p3.y = p2.y; p3.pz = p2.pz;
			}
		}
		else /* we're done */
		{
			break;
		}

		/* link type */
		switch( (attr>>8) & 3 )
		{
			case 0:
			case 2:
			{
				/* reuse P0(n) and P1(n) */
				p0.x = p2.x; p0.y = p2.y; p0.pz = p2.pz;
				p1.x = p3.x; p1.y = p3.y; p1.pz = p3.pz;
			}
			break;

			case 1:
			{
				/* reuse P0(n-1) and P0(n) */
				p1.x = p2.x; p1.y = p2.y; p1.pz = p2.pz;
			}
			break;

			case 3:
			{
				/* reuse P1(n-1) and P1(n) */
				p0.x = p3.x; p0.y = p3.y; p0.pz = p3.pz;
			}
			break;
		}
	}

	/* notify the 3d rasterizer we're done */
	model2_3d_push( raster, 0 );
}

/* Parse Polygons: No Normals, Specular case */
void model2_state::geo_parse_nn_s( geo_state *geo, uint32_t *input, uint32_t count )
{
	raster_state *raster = geo->raster;
	poly_vertex point, normal, p0, p1, p2, p3;
	uint32_t  attr, i;

	/* read the 1st point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* save for normal calculation */
	p0.x = point.x; p0.y = point.y; p0.pz = point.pz;

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* read the 2nd point */
	point.x = u2f( *input++ );
	point.y = u2f( *input++ );
	point.pz = u2f( *input++ );

	/* transform with the current matrix */
	transform_point( &point, geo->matrix );

	/* save for normal calculation */
	p1.x = point.x; p1.y = point.y; p1.pz = point.pz;

	/* apply focus */
	apply_focus( geo, &point );

	/* push it to the 3d rasterizer */
	model2_3d_push( raster, f2u(point.x) >> 8 );
	model2_3d_push( raster, f2u(point.y) >> 8 );
	model2_3d_push( raster, f2u(point.pz) >> 8 );

	/* loop through the following links */
	for( i = 0; i < count; i++ )
	{
		/* read in the attributes */
		attr = *input++;

		/* push to the 3d rasterizer */
		model2_3d_push( raster, attr & 0x0003FFFF );

		if ( (attr & 3) != 0 ) /* quad or triangle */
		{
			float               dotl, dotp, luminance, distance, specular;
			float               coef, face;
			int32_t               luma;
			texture_parameter * texparam;

			/* Skip normal */
			input += 3;

			/* read in the next point */
			point.x = u2f( *input++ );
			point.y = u2f( *input++ );
			point.pz = u2f( *input++ );

			/* transform with the current matrix */
			transform_point( &point, geo->matrix );

			/* save for normal calculation */
			p2.x = point.x; p2.y = point.y; p2.pz = point.pz;

			/* compute the normal */
			vector_cross3( &normal, &p0, &p1, &p2 );

			/* normalize it */
			normalize_vector( &normal );

			/* calculate the dot product of the normal and the light vector */
			dotl = dot_product( &normal, &geo->light );

			/* calculate the dot product of the normal and the point */
			dotp = dot_product( &normal, &point );

			/* apply focus */
			apply_focus( geo, &point );

			/* determine whether this is the front or the back of the polygon */
			face = 0x100; /* rear */
			if ( dotp >= 0 ) face = 0; /* front */

			/* get the texture parameters */
			texparam = &geo->texture_parameters[(attr>>18) & 0x1f];

			/* calculate luminance and specular */
			if ( (dotl * dotp) < 0 ) luminance = 0;
			else luminance = fabs( dotl );

			specular = ((2*dotl) * normal.pz) - geo->light.pz;
			if ( specular < 0 ) specular = 0;
			if ( texparam->specular_control == 0 ) specular = 0;
			if ( (texparam->specular_control >> 1) != 0 ) specular *= specular;
			if ( (texparam->specular_control >> 2) != 0 ) specular *= specular;
			if ( ((texparam->specular_control+1) >> 3) != 0 ) specular *= specular;

			specular *= texparam->specular_scale;

			luminance = (luminance * texparam->diffuse) + texparam->ambient + specular;
			luma = (int32_t)luminance;

			if ( luma > 255 ) luma = 255;
			if ( luma < 0 ) luma = 0;

			/* add the face bit to the luma */
			luma += face;

			/* extract distance coefficient */
			coef = geo->coef_table[attr>>27];

			/* calculate texture level of detail */
			distance = coef * fabs( dotp ) * geo->lod;

			/* push to the 3d rasterizer */
			model2_3d_push( raster, luma << 15 );
			model2_3d_push( raster, f2u(distance) >> 8 );
			model2_3d_push( raster, f2u(point.x) >> 8 );
			model2_3d_push( raster, f2u(point.y) >> 8 );
			model2_3d_push( raster, f2u(point.pz) >> 8 );

			/* if it's a quad, push one more point */
			if ( attr & 1 )
			{
				/* read in the next point */
				point.x = u2f( *input++ );
				point.y = u2f( *input++ );
				point.pz = u2f( *input++ );

				/* transform with the current matrix */
				transform_point( &point, geo->matrix );

				/* save for normal calculation */
				p3.x = point.x; p3.y = point.y; p3.pz = point.pz;

				/* apply focus */
				apply_focus( geo, &point );

				/* push to the 3d rasterizer */
				model2_3d_push( raster, f2u(point.x) >> 8 );
				model2_3d_push( raster, f2u(point.y) >> 8 );
				model2_3d_push( raster, f2u(point.pz) >> 8 );
			}
			else
			{
				/* skip the next 3 points */
				input += 3;

				/* for triangles, the rope of P1(n) is achieved by P0(n-1) (linktype 3) */
				p3.x = p2.x; p3.y = p2.y; p3.pz = p2.pz;
			}
		}
		else /* we're done */
		{
			break;
		}

		/* link type */
		switch( (attr>>8) & 3 )
		{
			case 0:
			case 2:
			{
				/* reuse P0(n) and P1(n) */
				p0.x = p2.x; p0.y = p2.y; p0.pz = p2.pz;
				p1.x = p3.x; p1.y = p3.y; p1.pz = p3.pz;
			}
			break;

			case 1:
			{
				/* reuse P0(n-1) and P0(n) */
				p1.x = p2.x; p1.y = p2.y; p1.pz = p2.pz;
			}
			break;

			case 3:
			{
				/* reuse P1(n-1) and P1(n) */
				p0.x = p3.x; p0.y = p3.y; p0.pz = p3.pz;
			}
			break;
		}
	}

	/* notify the 3d rasterizer we're done */
	model2_3d_push( raster, 0 );
}

/*******************************************
 *
 *  Geometry Engine Commands
 *
 *******************************************/

/* Command 00: NOP */
uint32_t *model2_state::geo_nop( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;

	/* push the opcode to the 3d rasterizer */
	model2_3d_push( raster, opcode >> 23 );

	return input;
}

/* Command 01: Object Data */
uint32_t *model2_state::geo_object_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;
	uint32_t  tpa = *input++;     /* Texture Point Address */
	uint32_t  tha = *input++;     /* Texture Header Address */
	uint32_t  oba = *input++;     /* Object Address */
	uint32_t  obc = *input++;     /* Object Count */

	uint32_t *obp;                /* Object Pointer */

	/* push the initial set of data to the 3d rasterizer */
	model2_3d_push( raster, opcode >> 23 );
	model2_3d_push( raster, tpa );
	model2_3d_push( raster, tha );

	/* select where we're reading polygon information from */
	if ( oba & 0x01000000 )
	{
		/* Fast polygon RAM */
		obp = &geo->polygon_ram1[oba & 0x7FFF];
	}
	else if ( oba & 0x00800000 )
	{
		/* Polygon ROM */
		obp = &geo->polygon_rom[oba & geo->polygon_rom_mask];
	}
	else
	{
		/* Slow Polygon RAM */
		obp = &geo->polygon_ram0[oba & 0x7FFF];
	}

	// if count == 0 then rolls over to max size
	// Virtual On & Gunblade NY
	if(obc == 0)
		obc = 0xfffff;

	switch( geo->mode & 3 )
	{
		/* Normals present, No Specular */
		case 0: geo_parse_np_ns( geo, obp, obc ); break;

		/* Normals present, Specular */
		case 1: geo_parse_np_s( geo, obp, obc ); break;

		/* No Normals present, No Specular */
		case 2: geo_parse_nn_ns( geo, obp, obc ); break;

		/* No Normals present, Specular */
		case 3: geo_parse_nn_s( geo, obp, obc ); break;
	}

	/* move by 4 parameters */
	return input;
}

/* Command 02: Direct Data */
uint32_t *model2_state::geo_direct_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;
	uint32_t  tpa = *input++;     /* Texture Point Address */
	uint32_t  tha = *input++;     /* Texture Header Address */
	uint32_t  attr;

	/* push the initial set of data to the 3d rasterizer */
	model2_3d_push( raster, (opcode >> 23) - 1 );
	model2_3d_push( raster, tpa );
	model2_3d_push( raster, tha );

	/* push the initial points */
	model2_3d_push( raster, (*input++) >> 8 ); /* x */
	model2_3d_push( raster, (*input++) >> 8 ); /* y */
	model2_3d_push( raster, (*input++) >> 8 ); /* z */

	model2_3d_push( raster, (*input++) >> 8 ); /* x */
	model2_3d_push( raster, (*input++) >> 8 ); /* y */
	model2_3d_push( raster, (*input++) >> 8 ); /* z */

	do
	{
		/* read in the attributes */
		attr = *input++;

		if ( (attr & 3) == 0 )
			break;

		/* push attributes */
		model2_3d_push( raster, attr & 0x00FFFFFF );

		/* push luma */
		model2_3d_push( raster, (*input++) >> 8 );

		/* push distance */
		model2_3d_push( raster, (*input++) >> 8 );

		/* push the next point */
		model2_3d_push( raster, (*input++) >> 8 ); /* x */
		model2_3d_push( raster, (*input++) >> 8 ); /* y */
		model2_3d_push( raster, (*input++) >> 8 ); /* z */

		/* if it's a quad, output another point */
		if ( attr & 1 )
		{
			model2_3d_push( raster, (*input++) >> 8 ); /* x */
			model2_3d_push( raster, (*input++) >> 8 ); /* y */
			model2_3d_push( raster, (*input++) >> 8 ); /* z */
		}
	} while( 1 );

	/* we're done */
	model2_3d_push( raster, 0 );

	return input;
}

/* Command 03: Window Data */
uint32_t *model2_state::geo_window_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;
	uint32_t  x, y, i;

	/* start by pushing the opcode */
	model2_3d_push( raster, opcode >> 23 );

	/*
	    we're going to move 6 coordinates to the 3d rasterizer:
	    - starting coordinate
	    - completion coordinate
	    - vanishing point 0 (eye mode 0)
	    - vanishing point 1 (eye mode 1)
	    - vanishing point 2 (eye mode 2)
	    - vanishing point 3 (eye mode 3)
	*/

	for( i = 0; i < 6; i++ )
	{
		/* read in the coordinate */
		y = *input++;

		/* convert to the 3d rasterizer format (00XXXYYY) */
		x = ( y & 0x0FFF0000 ) >> 4 ;
		y &= 0xFFF;

		/* push it */
		model2_3d_push( raster, x | y );
	}

	return input;
}

/* Command 04: Texture Data Write */
uint32_t *model2_state::geo_texture_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;
	uint32_t  i, count;

	/* start by pushing the opcode */
	model2_3d_push( raster, opcode >> 23 );

	/* push the starting address/dsp id */
	model2_3d_push( raster, *input++ );

	/* get the count */
	count = *input++;

	/* push the count */
	model2_3d_push( raster, count );

	/* loop and send the data */
	for( i = 0; i < count; i++ )
		model2_3d_push( raster, *input++ );

	return input;
}

/* Command 05: Polygon Data */
uint32_t *model2_state::geo_polygon_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  address, count, i;
	uint32_t *p;

	(void)opcode;

	/* read in the address */
	address = *input++;

	/* prepare the pointer */
	if ( address & 0x01000000 )
	{
		/* Fast polygon RAM */
		p = &geo->polygon_ram1[address & 0x7FFF];
	}
	else
	{
		/* Slow Polygon RAM */
		p = &geo->polygon_ram0[address & 0x7FFF];
	}

	/* read the count */
	count = *input++;

	/* move the data */
	for( i = 0; i < count; i++ )
		*p++ = *input++;

	return input;
}

/* Command 06: Texture Parameters */
uint32_t *model2_state::geo_texture_parameters( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  index, count, i, param;

	(void)opcode;

	/* read in the index */
	index = *input++;

	/* read in the conut */
	count = *input++;

	for( i = 0; i < count; i++ )
	{
		/* read in the texture parameters */
		param = *input++;

		geo->texture_parameters[index].diffuse = (float)( param & 0xFF );
		geo->texture_parameters[index].ambient = (float)( (param >> 8) & 0xFF );
		geo->texture_parameters[index].specular_control = (param >> 24) & 0xFF;
		geo->texture_parameters[index].specular_scale = (float)( (param >> 16) & 0xFF );

		/* read in the distance coefficient */
		geo->coef_table[index] = u2f(*input++);

		index = (index + 1) & 0x1F;
	}

	return input;
}

/* Command 07: Geo Mode */
uint32_t *model2_state::geo_mode( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	(void)opcode;

	/* read in the mode */
	geo->mode = *input++;

	return input;
}

/* Command 08: ZSort Mode */
uint32_t *model2_state::geo_zsort_mode( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;

	/* push the opcode */
	model2_3d_push( raster, opcode >> 23 );

	/* push the mode */
	model2_3d_push( raster, (*input++) >> 8 );

	return input;
}

/* Command 09: Focal Distance */
uint32_t *model2_state::geo_focal_distance( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	(void)opcode;

	/* read the x focus value */
	geo->focus.x = u2f( *input++ );

	/* read the y focus value */
	geo->focus.y = u2f( *input++ );

	return input;
}

/* Command 0A: Light Source Vector Write */
uint32_t *model2_state::geo_light_source( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	(void)opcode;

	/* read the x light value */
	geo->light.x = u2f( *input++ );

	/* read the y light value */
	geo->light.y = u2f( *input++ );

	/* read the z light value */
	geo->light.pz = u2f( *input++ );

	return input;
}

/* Command 0B: Transformation Matrix Write */
uint32_t *model2_state::geo_matrix_write( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  i;

	(void)opcode;

	/* read in the transformation matrix */
	for( i = 0; i < 12; i++ )
		geo->matrix[i] = u2f( *input++ );

	return input;
}

/* Command 0C: Parallel Transfer Vector Write */
uint32_t *model2_state::geo_translate_write( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  i;

	(void)opcode;

	/* read in the translation vector */
	for( i = 0; i < 3; i++ )
		geo->matrix[i+9] = u2f( *input++ );

	return input;
}

/* Command 0D: Geo Data Memory Push (undocumented, unsupported) */
uint32_t *model2_state::geo_data_mem_push( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  address, count, i;

	/*
	    This command pushes data stored in the Geometry DSP's RAM
	    to the hardware 3D rasterizer. Since we don't emulate the
	    DSP, we don't know what the RAM contents are.

	    Eventually, we could check for the address, and if it
	    happens to point to a polygon ROM, we could potentially
	    emulate it partially.

	    No games are known to use this command yet.
	*/


	(void)opcode;

	/* read in the address */
	address = *input++;

	/* read in the count */
	count = *input++;

	logerror( "SEGA GEO: Executing unsupported geo_data_mem_push (address = %08x, count = %08x)\n", address, count );

	(void)i;
/*
    for( i = 0; i < count; i++ )
        model2_3d_push( 0 );
*/

	return input;
}

/* Command 0E: Geo Test */
uint32_t *model2_state::geo_test( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t      data, blocks, address, count, checksum, i;

	(void)opcode;

	/* fifo test */
	data = 1;

	for( i = 0; i < 32; i++ )
	{
		if ( *input++ != data )
		{
			/* TODO: Set Red LED on */
			logerror( "SEGA GEO: FIFO Test failed\n" );
		}

		data <<= 1;
	}

	/* get the number of checksums we have to run */
	blocks = *input++;

	for( i = 0; i < blocks; i++ )
	{
		uint32_t  sum_even, sum_odd, j;

		/* read in the address */
		address = (*input++) & 0x7FFFFF;

		/* read in the count */
		count = *input++;

		/* read in the checksum */
		checksum = *input++;

		/* reset the checksum counters */
		sum_even = 0;
		sum_odd = 0;

		for( j = 0; j < count; j++ )
		{
			data = geo->polygon_rom[address++];

			address &= geo->polygon_rom_mask;

			sum_even += data >> 16;
			sum_even &= 0xFFFF;

			sum_odd += data & 0xFFFF;
			sum_odd &= 0xFFFF;
		}

		sum_even += checksum >> 16;
		sum_even &= 0xFFFF;

		sum_odd += checksum & 0xFFFF;
		sum_odd &= 0xFFFF;

		if ( sum_even != 0 || sum_odd != 0 )
		{
			/* TODO: Set Green LED on */
			logerror( "SEGA GEO: Polygon ROM Test failed\n" );
		}
	}

	return input;
}

/* Command 0F: End */
uint32_t *model2_state::geo_end( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;

	(void)opcode;

	/* signal the end of this data block the rasterizer */
	model2_3d_push( raster, 0xFF000000 );

	/* signal end by returning nullptr */
	return nullptr;
}

/* Command 10: Dummy */
uint32_t *model2_state::geo_dummy( geo_state *geo, uint32_t opcode, uint32_t *input )
{
//  uint32_t  data;
	(void)opcode;

	/* do the dummy read cycle */
//  data = *input++;
	input++;

	return input;
}

/* Command 14: Log Data Write */
uint32_t *model2_state::geo_log_data( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	raster_state *raster = geo->raster;
	uint32_t  i, count;

	/* start by pushing the opcode */
	model2_3d_push( raster, opcode >> 23 );

	/* push the starting address/dsp id */
	model2_3d_push( raster, *input++ );

	/* get the count */
	count = *input++;

	/* push the count */
	model2_3d_push( raster, count << 2 );

	/* loop and send the data */
	for( i = 0; i < count; i++ )
	{
		uint32_t  data = *input++;

		model2_3d_push( raster, data & 0xff );
		model2_3d_push( raster, (data >> 8) & 0xff );
		model2_3d_push( raster, (data >> 16) & 0xff );
		model2_3d_push( raster, (data >> 24) & 0xff );
	}

	return input;
}

/* Command 16: LOD */
uint32_t *model2_state::geo_lod( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	(void)opcode;

	/* read in the LOD */
	geo->lod = u2f(*input++);

	return input;
}

/* Command 1D: Code Upload  (undocumented, unsupported) */
uint32_t *model2_state::geo_code_upload( geo_state *geo, uint32_t opcode, uint32_t *input )
{
	uint32_t  count, i;

	/*
	    This command uploads code to program memory and
	    optionally runs it. Probably used for debugging.

	    No games are known to use this command yet.
	*/

	logerror( "SEGA GEO: Uploading debug code (unimplemented)\n" );

	(void)opcode;

	/* read in the flags */
//  flags = *input++;
	input++;

	/* read in the count */
	count = *input++;

	for( i = 0; i < count; i++ )
	{
		uint64_t  code;

		/* read the top part of the opcode */
		code = *input++;

		code <<= 32;

		/* the bottom part comes in two pieces */
		code |= *input++;
		code |= (*input++) << 16;
	}

	/*
	    Bit 10 of flags indicate whether to run iummediately after upload
	*/

/*
    if ( flags & 0x400 )
        code_jump();
*/

	return input;
}

/* Command 1E: Code Jump (undocumented, unsupported) */
uint32_t *model2_state::geo_code_jump( geo_state *geo, uint32_t opcode, uint32_t *input )
{
//  uint32_t  address;

	/*
	    This command jumps to a specified address in program
	    memory. Code can be uploaded with function 1D.
	    Probably used for debugging.

	    No games are known to use this command yet.
	*/

	logerror( "SEGA GEO: Jumping to debug code (unimplemented)\n" );

	(void)opcode;

//  address = *input++ & 0x3FF;
	input++;

/*
    code_jump( address )
*/
	return input;
}

uint32_t *model2_state::geo_process_command( geo_state *geo, uint32_t opcode, uint32_t *input, bool *end_code )
{
	switch( (opcode >> 23) & 0x1f )
	{
		case 0x00: input = geo_nop( geo, opcode, input );                   break;
		case 0x01: input = geo_object_data( geo, opcode, input );           break;
		case 0x02: input = geo_direct_data( geo, opcode, input );           break;
		case 0x03: input = geo_window_data( geo, opcode, input );           break;
		case 0x04: input = geo_texture_data( geo, opcode, input );          break;
		case 0x05: input = geo_polygon_data( geo, opcode, input );          break;
		case 0x06: input = geo_texture_parameters( geo, opcode, input );    break;
		case 0x07: input = geo_mode( geo, opcode, input );                  break;
		case 0x08: input = geo_zsort_mode( geo, opcode, input );            break;
		case 0x09: input = geo_focal_distance( geo, opcode, input );        break;
		case 0x0A: input = geo_light_source( geo, opcode, input );          break;
		case 0x0B: input = geo_matrix_write( geo, opcode, input );          break;
		case 0x0C: input = geo_translate_write( geo, opcode, input );       break;
		case 0x0D: input = geo_data_mem_push( geo, opcode, input );         break;
		case 0x0E: input = geo_test( geo, opcode, input );                  break;
		case 0x0F: input = geo_end( geo, opcode, input ); *end_code = true; break;
		case 0x10: input = geo_dummy( geo, opcode, input );                 break;
		case 0x11: input = geo_object_data( geo, opcode, input );           break;
		case 0x12: input = geo_direct_data( geo, opcode, input );           break;
		case 0x13: input = geo_window_data( geo, opcode, input );           break;
		case 0x14: input = geo_log_data( geo, opcode, input );              break;
		case 0x15: input = geo_polygon_data( geo, opcode, input );          break;
		case 0x16: input = geo_lod( geo, opcode, input );                   break;
		case 0x17: input = geo_mode( geo, opcode, input );                  break;
		case 0x18: input = geo_zsort_mode( geo, opcode, input );            break;
		case 0x19: input = geo_focal_distance( geo, opcode, input );        break;
		case 0x1A: input = geo_light_source( geo, opcode, input );          break;
		case 0x1B: input = geo_matrix_write( geo, opcode, input );          break;
		case 0x1C: input = geo_translate_write( geo, opcode, input );       break;
		case 0x1D: input = geo_code_upload( geo, opcode, input );           break;
		case 0x1E: input = geo_code_jump( geo, opcode, input );             break;
		case 0x1F: input = geo_end( geo, opcode, input ); *end_code = true; break;
	}

	return input;
}

void model2_state::geo_parse( void )
{
	uint32_t  address = (m_geo_read_start_address & 0x1ffff)/4;
	uint32_t *input = &m_bufferram[address];
	uint32_t  opcode;
	bool end_code = false;

	while( end_code == false && (input - m_bufferram) < 0x20000/4  )
	{
		/* read in the opcode */
		opcode = *input++;

		/* if it's a jump opcode, do the jump */
		if ( opcode & 0x80000000 )
		{
			// TODO: daytona with master network enabled hardlocks by trying a jump with 0xffff0080 as opcode
			//       bad timings for geo_parse?
			if(opcode & 0x078000000 )
				return;

			/* get the address */
			address = (opcode & 0x1FFFF) / 4;

			/* update our pointer */
			input = &m_bufferram[address];

			/* go again */
			continue;
		}

		/* process it */
		input = geo_process_command( m_geo, opcode, input, &end_code );
	}
}

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


void model2_state::video_start()
{
	const rectangle &visarea = m_screen->visible_area();
	int width = visarea.width();
	int height = visarea.height();

	m_sys24_bitmap.allocate(width, height+4);

	m_poly = auto_alloc(machine(), model2_renderer(*this));

	/* initialize the hardware rasterizer */
	raster_init( memregion("textures") );

	/* initialize the geometry engine */
	geo_init( memregion("polygons") );

	/* init various video-related pointers */
	m_palram = make_unique_clear<uint16_t[]>(0x4000/2);
	m_colorxlat = make_unique_clear<uint16_t[]>(0xc000/2);
	m_lumaram = make_unique_clear<uint16_t[]>(0x10000/2);
	m_fbvramA = make_unique_clear<uint16_t[]>(0x80000/2);
	m_fbvramB = make_unique_clear<uint16_t[]>(0x80000/2);

	// convert (supposedly) 3d sRGB color space into linear
	// TODO: might be slightly different algorithm (Daytona USA road/cars, VF2 character skins)
	for(int i=0;i<256;i++)
	{
		double raw_value;
		raw_value = 255.0 * pow((double)(i) / 255.0,2.2);
		m_gamma_table[i] = (uint8_t)raw_value;
//      printf("%02x: %02x %lf\n",i,m_gamma_table[i],raw_value);
	}

	save_item(NAME(m_render_test_mode));
	save_item(NAME(m_render_unk));
	save_item(NAME(m_render_mode));
	save_pointer(NAME(m_palram), 0x4000/2);
	save_pointer(NAME(m_colorxlat), 0xc000/2);
	save_pointer(NAME(m_lumaram), 0x10000/2);
	save_pointer(NAME(m_gamma_table), 256);
}

uint32_t model2_state::screen_update_model2(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	//logerror("--- frame ---\n");
	bitmap.fill(m_palette->pen(0), cliprect);
	m_sys24_bitmap.fill(0, cliprect);

	for(int layer = 3; layer >= 0; layer--)
		m_tiles->draw(screen, m_sys24_bitmap, cliprect, layer<<1, 0, 0);

	copybitmap_trans(bitmap, m_sys24_bitmap, 0, 0, 0, 0, cliprect, 0);

	/* tell the rasterizer we're starting a frame */
	if(m_render_test_mode == true)
		draw_framebuffer( bitmap, cliprect );
	else
	{
		model2_3d_frame_start();

		/* let the geometry engine do it's thing */
		// TODO: move it from here
		geo_parse();

		/* have the rasterizer output the frame */
		model2_3d_frame_end( bitmap, cliprect );
	}

	m_sys24_bitmap.fill(0, cliprect);

	for (int layer = 3; layer >= 0; layer--)
		m_tiles->draw(screen, m_sys24_bitmap, cliprect, (layer<<1) | 1, 0, 0);

	copybitmap_trans(bitmap, m_sys24_bitmap, 0, 0, 0, 0, cliprect, 0);

	return 0;
}

// called from machine/model2.cpp trilist command
// TODO: fix forward declaration mess and move this function there instead
void model2_state::tri_list_dump(FILE *dst)
{
	uint32_t  i;

	for( i = 0; i < m_raster->tri_list_index; i++ )
	{
		fprintf( dst, "index: %d\n", i );
		fprintf( dst, "v0.x = %f, v0.y = %f, v0.z = %f\n", m_raster->tri_list[i].v[0].x, m_raster->tri_list[i].v[0].y, m_raster->tri_list[i].v[0].pz );
		fprintf( dst, "v1.x = %f, v1.y = %f, v1.z = %f\n", m_raster->tri_list[i].v[1].x, m_raster->tri_list[i].v[1].y, m_raster->tri_list[i].v[1].pz );
		fprintf( dst, "v2.x = %f, v2.y = %f, v2.z = %f\n", m_raster->tri_list[i].v[2].x, m_raster->tri_list[i].v[2].y, m_raster->tri_list[i].v[2].pz );

		fprintf( dst, "tri z: %04x\n", m_raster->tri_list[i].z );
		fprintf( dst, "texheader - 0: %04x\n", m_raster->tri_list[i].texheader[0] );
		fprintf( dst, "texheader - 1: %04x\n", m_raster->tri_list[i].texheader[1] );
		fprintf( dst, "texheader - 2: %04x\n", m_raster->tri_list[i].texheader[2] );
		fprintf( dst, "texheader - 3: %04x\n", m_raster->tri_list[i].texheader[3] );
		fprintf( dst, "luma: %02x\n", m_raster->tri_list[i].luma );
		fprintf( dst, "vp.sx: %04x\n", m_raster->tri_list[i].viewport[0] );
		fprintf( dst, "vp.sy: %04x\n", m_raster->tri_list[i].viewport[1] );
		fprintf( dst, "vp.ex: %04x\n", m_raster->tri_list[i].viewport[2] );
		fprintf( dst, "vp.ey: %04x\n", m_raster->tri_list[i].viewport[3] );
		fprintf( dst, "vp.swx: %04x\n", m_raster->tri_list[i].center[0] );
		fprintf( dst, "vp.swy: %04x\n", m_raster->tri_list[i].center[1] );
		fprintf( dst, "\n---\n\n" );
	}

	fprintf( dst, "min_z = %04x, max_z = %04x\n", m_raster->min_z, m_raster->max_z );

	fclose( dst );

}