summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/video/315_5124.cpp
blob: 4e7d04bcab6feacac50dd6f3c087a818556f5aca (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
// license:BSD-3-Clause
// copyright-holders:Wilbert Pol, Enik Land
/*********************************************************************

    sega315_5124.cpp

    Implementation of video hardware chips used by Sega System E,
    Master System, and Game Gear.

    Some specific behavior of the chip used by Sega Genesis/Mega
    Drive is also implemented for mode 4 only.

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

/*

To do:

  - Register 3 behaviour in mode 4
  - VRAM/CRAM access constraints


SMS Display Timing
------------------
    For more information, please see:
    - http://cgfm2.emuviews.com/txt/msvdp.txt
    - http://www.smspower.org/forums/viewtopic.php?p=37142
    - http://www.smspower.org/forums/viewtopic.php?p=92925

A scanline contains the following sections:
  - horizontal sync    26  E9-F5   => HSYNC low
  - left blanking       2  F6-F6   => HSYNC high
  - color burst        14  F7-FD
  - left blanking       8  FE-01
  - left border        13  02-08
  - active display    256  08-88
  - right border       15  88-8F
  - right blanking      8  90-93

  Probably the processing done for the active display occurs when HCount
  is in the 00-7F range and there is a delay until its signal is shown on
  screen, as happens on the TMS9918 chip according to this timing diagram:
      http://www.smspower.org/Development/TMS9918MasterTimingDiagram


NTSC frame timing
                       256x192         256x224        256x240 (doesn't work on real hardware)
  - vertical sync       3  D5-D7        3  E5-E7       3  ED-EF
  - top blanking       13  D8-E4       13  E8-F4      13  F0-FC
  - top border         27  E5-FF       11  F5-FF       3  FD-FF
  - active display    192  00-BF      224  00-DF     240  00-EF
  - bottom border      24  C0-D7        8  E0-E7       0  F0-F0
  - bottom blanking     3  D8-DA        3  E8-EA       3  F0-F2


PAL frame timing
                       256x192         256x224        256x240
  - vertical sync       3  BA-BC        3  CA-CC       3  D2-D4
  - top blanking       13  BD-C9       13  CD-D9      13  D5-E1
  - top border         54  CA-FF       38  DA-FF      30  E2-FF
  - active display    192  00-BF      224  00-DF     240  00-EF
  - bottom border      48  C0-EF       32  E0-FF      24  F0-07
  - bottom blanking     3  F0-F2        3  00-02       3  08-0A

*/

#include "emu.h"
#include "video/315_5124.h"


#define SEGA315_5124_PALETTE_SIZE     (64 + 16)
#define SEGA315_5377_PALETTE_SIZE     4096

#define VRAM_SIZE             0x4000

#define STATUS_VINT           0x80  /* Pending vertical interrupt flag */
#define STATUS_SPROVR         0x40  /* Sprite overflow flag */
#define STATUS_SPRCOL         0x20  /* Object collision flag */

#define PRIORITY_BIT          0x1000
#define BACKDROP_COLOR        ((m_vdp_mode == 4 ? 0x10 : 0x00) + (m_reg[0x07] & 0x0f))

#define VERTICAL_SYNC         0
#define TOP_BLANKING          1
#define TOP_BORDER            2
#define ACTIVE_DISPLAY_V      3
#define BOTTOM_BORDER         4
#define BOTTOM_BLANKING       5

static constexpr u8 ntsc_192[6] = { 3, 13, 27, 192, 24, 3 };
static constexpr u8 ntsc_224[6] = { 3, 13, 11, 224,  8, 3 };
static constexpr u8 ntsc_240[6] = { 3, 13,  3, 240,  0, 3 };
static constexpr u8 pal_192[6]  = { 3, 13, 54, 192, 48, 3 };
static constexpr u8 pal_224[6]  = { 3, 13, 38, 224, 32, 3 };
static constexpr u8 pal_240[6]  = { 3, 13, 30, 240, 24, 3 };

#define VINT_HPOS             0
#define VINT_FLAG_HPOS        1
#define HINT_HPOS             2
#define NMI_HPOS              3
#define XSCROLL_HPOS          4
#define VCOUNT_CHANGE_HPOS    5
#define SPROVR_HPOS           6
#define SPRCOL_BASEHPOS       7

static constexpr u8 line_315_5124[8] = { 24, 24, 26, 28 /* not verified */, 21, 23, 24, 59 };
static constexpr u8 line_315_5377[8] = { 26, 26, 27, 28 /* not verified */, 24, 28, 26, 62 };

#define DISPLAY_DISABLED_HPOS 24 /* not verified, works if above 18 (for 'pstrike2') and below 25 (for 'fantdizzy') */
#define DISPLAY_CB_HPOS       2  /* fixes 'roadrash' (SMS game) title scrolling, due to line counter reload timing */

#define DRAW_TIME_GG         111      /* 26 + 2 + 14 + 8 + 13 + 96/2 */
#define DRAW_TIME_SMS         63      /* 26 + 2 + 14 + 8 + 13 */


DEFINE_DEVICE_TYPE(SEGA315_5124, sega315_5124_device, "sega315_5124", "Sega 315-5124 SMS1 VDP")
DEFINE_DEVICE_TYPE(SEGA315_5246, sega315_5246_device, "sega315_5246", "Sega 315-5246 SMS2 VDP")
DEFINE_DEVICE_TYPE(SEGA315_5377, sega315_5377_device, "sega315_5377", "Sega 315-5377 Gamegear VDP")

// (reference for VDP colors : http://www.sega-16.com/forum/showthread.php?30530-SMS-VDP-output-levels)
void sega315_5124_device::sega315_5124_palette(palette_device &palette) const
{
	static const u8 level[4] = {0,90,173,255};
	for (int i = 0; i < 64; i++)
	{
		const u8 r = i & 0x03;
		const u8 g = (i & 0x0c) >> 2;
		const u8 b = (i & 0x30) >> 4;
		palette.set_pen_color(i, level[r], level[g], level[b]);
	}
	// sms and sg1000-mark3 uses a different palette for modes 0 to 3 - see http://www.smspower.org/Development/Palette
	// TMS9918 palette
	palette.set_pen_color(64+ 0, level[0], level[0], level[0]); // palette.set_pen_color(64+ 0,   0,   0,   0);
	palette.set_pen_color(64+ 1, level[0], level[0], level[0]); // palette.set_pen_color(64+ 1,   0,   0,   0);
	palette.set_pen_color(64+ 2, level[0], level[2], level[0]); // palette.set_pen_color(64+ 2,  33, 200,  66);
	palette.set_pen_color(64+ 3, level[0], level[3], level[0]); // palette.set_pen_color(64+ 3,  94, 220, 120);
	palette.set_pen_color(64+ 4, level[0], level[0], level[1]); // palette.set_pen_color(64+ 4,  84,  85, 237);
	palette.set_pen_color(64+ 5, level[0], level[0], level[3]); // palette.set_pen_color(64+ 5, 125, 118, 252);
	palette.set_pen_color(64+ 6, level[1], level[0], level[0]); // palette.set_pen_color(64+ 6, 212,  82,  77);
	palette.set_pen_color(64+ 7, level[0], level[3], level[3]); // palette.set_pen_color(64+ 7,  66, 235, 245);
	palette.set_pen_color(64+ 8, level[2], level[0], level[0]); // palette.set_pen_color(64+ 8, 252,  85,  84);
	palette.set_pen_color(64+ 9, level[3], level[0], level[0]); // palette.set_pen_color(64+ 9, 255, 121, 120);
	palette.set_pen_color(64+10, level[1], level[1], level[0]); // palette.set_pen_color(64+10, 212, 193,  84);
	palette.set_pen_color(64+11, level[3], level[3], level[0]); // palette.set_pen_color(64+11, 230, 206, 128);
	palette.set_pen_color(64+12, level[0], level[1], level[0]); // palette.set_pen_color(64+12,  33, 176,  59);
	palette.set_pen_color(64+13, level[3], level[0], level[3]); // palette.set_pen_color(64+13, 201,  91, 186);
	palette.set_pen_color(64+14, level[1], level[1], level[1]); // palette.set_pen_color(64+14, 204, 204, 204);
	palette.set_pen_color(64+15, level[3], level[3], level[3]); // palette.set_pen_color(64+15, 255, 255, 255);
}


void sega315_5246_device::sega315_5246_palette(palette_device &palette) const
{
	// bit different output level compare to 315_5124
	static const u8 level[4] = {0,89,174,255};
	for (int i = 0; i < 64; i++)
	{
		const u8 r = i & 0x03;
		const u8 g = (i & 0x0c) >> 2;
		const u8 b = (i & 0x30) >> 4;
		palette.set_pen_color(i, level[r], level[g], level[b]);
	}
	// TMS9918 palette (see sega315_5124_palette)
	palette.set_pen_color(64+ 0, level[0], level[0], level[0]); // palette.set_pen_color(64+ 0,   0,   0,   0);
	palette.set_pen_color(64+ 1, level[0], level[0], level[0]); // palette.set_pen_color(64+ 1,   0,   0,   0);
	palette.set_pen_color(64+ 2, level[0], level[2], level[0]); // palette.set_pen_color(64+ 2,  33, 200,  66);
	palette.set_pen_color(64+ 3, level[0], level[3], level[0]); // palette.set_pen_color(64+ 3,  94, 220, 120);
	palette.set_pen_color(64+ 4, level[0], level[0], level[1]); // palette.set_pen_color(64+ 4,  84,  85, 237);
	palette.set_pen_color(64+ 5, level[0], level[0], level[3]); // palette.set_pen_color(64+ 5, 125, 118, 252);
	palette.set_pen_color(64+ 6, level[1], level[0], level[0]); // palette.set_pen_color(64+ 6, 212,  82,  77);
	palette.set_pen_color(64+ 7, level[0], level[3], level[3]); // palette.set_pen_color(64+ 7,  66, 235, 245);
	palette.set_pen_color(64+ 8, level[2], level[0], level[0]); // palette.set_pen_color(64+ 8, 252,  85,  84);
	palette.set_pen_color(64+ 9, level[3], level[0], level[0]); // palette.set_pen_color(64+ 9, 255, 121, 120);
	palette.set_pen_color(64+10, level[1], level[1], level[0]); // palette.set_pen_color(64+10, 212, 193,  84);
	palette.set_pen_color(64+11, level[3], level[3], level[0]); // palette.set_pen_color(64+11, 230, 206, 128);
	palette.set_pen_color(64+12, level[0], level[1], level[0]); // palette.set_pen_color(64+12,  33, 176,  59);
	palette.set_pen_color(64+13, level[3], level[0], level[3]); // palette.set_pen_color(64+13, 201,  91, 186);
	palette.set_pen_color(64+14, level[1], level[1], level[1]); // palette.set_pen_color(64+14, 204, 204, 204);
	palette.set_pen_color(64+15, level[3], level[3], level[3]); // palette.set_pen_color(64+15, 255, 255, 255);
}


void sega315_5377_device::sega315_5377_palette(palette_device &palette) const
{
	// TODO : linear? measure this
	for (int i = 0; i < 4096; i++)
	{
		const u8 r = i & 0x000f;
		const u8 g = (i & 0x00f0) >> 4;
		const u8 b = (i & 0x0f00) >> 8;
		palette.set_pen_color(i, pal4bit(r), pal4bit(g), pal4bit(b));
	}
}


void sega315_5313_mode4_device::sega315_5313_palette(palette_device &palette) const
{
	// non-linear (reference : http://gendev.spritesmind.net/forum/viewtopic.php?f=22&t=2188)
	static const u8 level[15] = {0,29,52,70,87,101,116,130,144,158,172,187,206,228,255};
	for (int i = 0; i < 512; i++)
	{
		const u8 r = (i & 0x0007) >> 0;
		const u8 g = (i & 0x0038) >> 3;
		const u8 b = (i & 0x01c0) >> 6;
		palette.set_pen_color(i + (512 * 0), level[r << 1], level[g << 1], level[b << 1]); // normal
		palette.set_pen_color(i + (512 * 1), level[r], level[g], level[b]); // shadow
		palette.set_pen_color(i + (512 * 2), level[7 + r], level[7 + g], level[7 + b]); // hilight
	}
	// seperated SMS compatible mode color
	static const u8 sms_level[4] = {0,99,162,255};
	for (int i = 0; i < 64; i++)
	{
		const u8 r = (i & 0x0003) >> 0;
		const u8 g = (i & 0x000c) >> 2;
		const u8 b = (i & 0x0030) >> 4;
		palette.set_pen_color(i + (512 * 3), sms_level[r], sms_level[g], sms_level[b]); // normal
	}
}


// default address map
void sega315_5124_device::sega315_5124(address_map &map)
{
	if (!has_configured_map(0))
		map(0x0000, VRAM_SIZE-1).ram();
}


sega315_5124_device::sega315_5124_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: sega315_5124_device(mconfig, SEGA315_5124, tag, owner, clock, SEGA315_5124_CRAM_SIZE, 0x00, 0x0f, 4, 8, line_315_5124)
{
}


sega315_5124_device::sega315_5124_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock, u8 cram_size, u8 palette_offset, u8 reg_num_mask, int max_sprite_zoom_hcount, int max_sprite_zoom_vcount, const u8 *line_timing)
	: device_t(mconfig, type, tag, owner, clock)
	, device_memory_interface(mconfig, *this)
	, device_video_interface(mconfig, *this)
	, device_mixer_interface(mconfig, *this, 2)
	, m_hcounter_divide(1)
	, m_cram_size(cram_size)
	, m_line_timing(line_timing)
	, m_palette_offset(palette_offset)
	, m_reg_num_mask(reg_num_mask)
	, m_max_sprite_zoom_hcount(max_sprite_zoom_hcount)
	, m_max_sprite_zoom_vcount(max_sprite_zoom_vcount)
	, m_is_pal(false)
	, m_vblank_cb(*this)
	, m_n_csync_cb(*this)
	, m_n_int_cb(*this)
	, m_n_nmi_cb(*this)
	, m_space_config("videoram", ENDIANNESS_LITTLE, 8, 14, 0, address_map_constructor(FUNC(sega315_5124_device::sega315_5124), this))
	, m_palette_lut(*this, "palette_lut")
	, m_snsnd(*this, "snsnd")
{
}


sega315_5246_device::sega315_5246_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: sega315_5124_device(mconfig, SEGA315_5246, tag, owner, clock, SEGA315_5124_CRAM_SIZE, 0x00, 0x0f, 8, 8, line_315_5124)
{
}


sega315_5246_device::sega315_5246_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock, u8 cram_size, u8 palette_offset, u8 reg_num_mask, int max_sprite_zoom_hcount, int max_sprite_zoom_vcount, const u8 *line_timing)
	: sega315_5124_device(mconfig, type, tag, owner, clock, cram_size, palette_offset, reg_num_mask, max_sprite_zoom_hcount, max_sprite_zoom_vcount, line_timing)
{
}


// Embedded mode 4 support of the 315-5313 VDP (see 315_5313.cpp), used by Sega Genesis/Mega Drive
sega315_5313_mode4_device::sega315_5313_mode4_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, u32 clock, u8 cram_size, u8 palette_offset, u8 reg_num_mask, int max_sprite_zoom_hcount, int max_sprite_zoom_vcount, const u8 *line_timing)
	: sega315_5246_device(mconfig, type, tag, owner, clock, cram_size, palette_offset, reg_num_mask, max_sprite_zoom_hcount, max_sprite_zoom_vcount, line_timing)
{
}


sega315_5377_device::sega315_5377_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock)
	: sega315_5246_device(mconfig, SEGA315_5377, tag, owner, clock, SEGA315_5377_CRAM_SIZE, 0x10, 0x0f, 8, 8, line_315_5377)
{
}


device_memory_interface::space_config_vector sega315_5124_device::memory_space_config() const
{
	return space_config_vector {
		std::make_pair(0, &m_space_config)
	};
}


void sega315_5124_device::select_extended_res_mode4(bool M1, bool M2, bool M3)
{
	// no extended resolution supported.
	return;
}


void sega315_5246_device::select_extended_res_mode4(bool M1, bool M2, bool M3)
{
	if (M2)
	{
		if (M1 && !M3)
			m_y_pixels = 224;   // 224-line display
		else if (!M1 && M3)
			m_y_pixels = 240;   // 240-line display
	}
}

void sega315_5313_mode4_device::select_extended_res_mode4(bool M1, bool M2, bool M3)
{
	// no extended resolution supported.
}


void sega315_5124_device::select_display_mode()
{
	const bool M1 = BIT(m_reg[0x01], 4);
	const bool M2 = BIT(m_reg[0x00], 1);
	const bool M3 = BIT(m_reg[0x01], 3);
	const bool M4 = BIT(m_reg[0x00], 2);

	if (M4)
	{
		// mode 4
		m_vdp_mode = 4;
		select_extended_res_mode4(M1, M2, M3);
	}
	else // original TMS9918 mode
	{
		if (!M1 && !M2 && !M3) // Mode 0 (Graphics I Mode)
			m_vdp_mode = 0;
		else if (M1 && !M2 && !M3) // Mode 1 (Text Mode)
			m_vdp_mode = 1;
		else if (!M1 && M2 && !M3) // Mode 2 (Graphics II Mode)
			m_vdp_mode = 2;
		else if (!M1 && !M2 && M3) // Mode 3 (Multicolor Mode) */
			m_vdp_mode = 3;
		else
			logerror("Unknown video mode detected (M1 = %c, M2 = %c, M3 = %c, M4 = %c)\n", M1 ? '1' : '0', M2 ? '1' : '0', M3 ? '1' : '0', M4 ? '1' : '0');
	}

}


void sega315_5313_mode4_device::select_display_mode()
{
	const bool M5 = BIT(m_reg[0x01], 2);

	if (M5)
	{
		/* mode 5, not implemented */
		m_vdp_mode = 5;
		logerror("Switched to unimplemented video mode 5 !\n");
	}
	else
	{
		/* mode 4, SMS compatibility */
		m_vdp_mode = 4;
	}
}


void sega315_5124_device::set_display_settings()
{
	m_y_pixels = 192;

	select_display_mode();
	set_frame_timing();
	m_cram_dirty = true;
}


void sega315_5124_device::set_frame_timing()
{
	switch (m_y_pixels)
	{
	case 192:
		m_frame_timing = (m_is_pal) ? pal_192 : ntsc_192;
		break;

	case 224:
		m_frame_timing = (m_is_pal) ? pal_224 : ntsc_224;
		break;

	case 240:
		m_frame_timing = (m_is_pal) ? pal_240 : ntsc_240;
		break;
	}
}


u8 sega315_5124_device::vcount_read()
{
	const int active_scr_start = m_frame_timing[VERTICAL_SYNC] + m_frame_timing[TOP_BLANKING] + m_frame_timing[TOP_BORDER];
	int vpos = screen().vpos();

	if (screen_hpos() < m_line_timing[VCOUNT_CHANGE_HPOS])
	{
		vpos--;
		if (vpos < 0)
			vpos += screen().height();
	}

	return (vpos - active_scr_start) & 0xff;
}


u8 sega315_5124_device::hcount_read()
{
	m_hcounter_latched = false;
	return m_hcounter;
}


void sega315_5124_device::hcount_latch()
{
	/* The hcount value returned by the VDP seems to be based on the previous hpos */
	int hclock = screen_hpos() - 1;
	if (hclock < 0)
		hclock += WIDTH;

	m_hcounter = ((hclock - 46) >> 1) & 0xff;
	m_hcounter_latched = true;
}


void sega315_5377_device::set_sega315_5124_compatibility_mode(bool sega315_5124_compatibility_mode)
{
	m_sega315_5124_compatibility_mode = sega315_5124_compatibility_mode;
	m_cram_mask = (!m_sega315_5124_compatibility_mode) ? (SEGA315_5377_CRAM_SIZE - 1) : (SEGA315_5124_CRAM_SIZE - 1);
	m_draw_time = m_sega315_5124_compatibility_mode ? DRAW_TIME_SMS : DRAW_TIME_GG;
}


void sega315_5124_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
	case TIMER_LINE:
		process_line_timer();
		break;

	case TIMER_FLAGS:
		/* Activate flags that were pending until the end of the line. */
		check_pending_flags();
		break;

	case TIMER_DRAW:
		update_palette();
		draw_scanline(LBORDER_START + LBORDER_WIDTH, param, screen().vpos() - param);
		break;

	case TIMER_LBORDER:
		{
			rectangle rec;
			rec.min_y = rec.max_y = param;

			update_palette();

			/* Draw left border */
			rec.min_x = LBORDER_START;
			rec.max_x = LBORDER_START + LBORDER_WIDTH - 1;
			m_tmpbitmap.fill(m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]), rec);
			m_y1_bitmap.fill((m_reg[0x07] & 0x0f) ? 1 : 0, rec);
		}
		break;

	case TIMER_RBORDER:
		{
			rectangle rec;
			rec.min_y = rec.max_y = param;

			update_palette();

			/* Draw right border */
			rec.min_x = LBORDER_START + LBORDER_WIDTH + 256;
			rec.max_x = rec.min_x + RBORDER_WIDTH - 1;
			m_tmpbitmap.fill(m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]), rec);
			m_y1_bitmap.fill((m_reg[0x07] & 0x0f) ? 1 : 0, rec);
		}
		break;

	case TIMER_HINT:
		if (m_pending_hint || m_hint_occurred)
		{
			if (BIT(m_reg[0x00], 4))
			{
				m_n_int_state = 0;

				if (!m_n_int_cb.isnull())
					m_n_int_cb(ASSERT_LINE);
			}
		}
		break;

	case TIMER_VINT:
		if ((m_pending_status & STATUS_VINT) || (m_status & STATUS_VINT))
		{
			if (BIT(m_reg[0x01], 5))
			{
				m_n_int_state = 0;

				if (!m_n_int_cb.isnull())
					m_n_int_cb(ASSERT_LINE);
			}
		}
		break;

	case TIMER_NMI:
		if (!m_n_nmi_in_state)
		{
			if (m_n_nmi_state)
				m_n_nmi_cb(ASSERT_LINE);
		}
		else
		{
			if (!m_n_nmi_state)
				m_n_nmi_cb(CLEAR_LINE);
		}
		m_n_nmi_state = m_n_nmi_in_state;
		break;
	}
}


void sega315_5124_device::vblank_end(int vpos)
{
	m_nmi_timer->adjust(screen().time_until_pos(vpos, m_line_timing[NMI_HPOS]));
}


void sega315_5377_device::vblank_end(int vpos)
{
	// Assume the VBlank line is used to trigger the NMI logic performed by the 315-5378 chip.
	if (!m_vblank_cb.isnull())
		m_vblank_cb(0);
}


void sega315_5124_device::process_line_timer()
{
	const int vpos = screen().vpos();
	int vpos_limit = m_frame_timing[VERTICAL_SYNC] + m_frame_timing[TOP_BLANKING]
					+ m_frame_timing[TOP_BORDER] + m_frame_timing[ACTIVE_DISPLAY_V]
					+ m_frame_timing[BOTTOM_BORDER] + m_frame_timing[BOTTOM_BLANKING];

	/* copy current values in case they are not changed until latch time */
	m_display_disabled = !BIT(m_reg[0x01], 6);
	m_reg8copy = m_reg[0x08];

	/* Check if the /CSYNC signal must be active (low) */
	if (!m_n_csync_cb.isnull())
	{
		/* /CSYNC is signals /HSYNC and /VSYNC (both internals) ANDed together.
		   According to Charles MacDonald, /HSYNC goes low for 28 pixels on beginning
		   (before active screen) of all lines except on vertical sync area, where
		   /VSYNC goes low for 3 full lines, and except the two lines that follows,
		   because /VSYNC goes high for another line and remains high until the
		   active screen of the next line, what avoids a /HSYNC pulse there.
		*/
		if (vpos == 0 || vpos > (m_frame_timing[VERTICAL_SYNC] + 1))
		{
			m_n_csync_cb(0);
		}
	}

	vpos_limit -= m_frame_timing[BOTTOM_BLANKING];

	/* Check if we're below the bottom border */
	if (vpos >= vpos_limit)
	{
		m_line_counter = m_reg[0x0a];
		return;
	}

	vpos_limit -= m_frame_timing[BOTTOM_BORDER];

	/* Check if we're in the bottom border area */
	if (vpos >= vpos_limit)
	{
		if (vpos == vpos_limit)
		{
			if (m_line_counter == 0x00)
			{
				m_line_counter = m_reg[0x0a];
				m_hint_timer->adjust(screen().time_until_pos(vpos, m_line_timing[HINT_HPOS]));
				m_pending_hint = true;
			}
			else
			{
				m_line_counter--;
			}
		}
		else
		{
			m_line_counter = m_reg[0x0a];
		}

		// vpos_limit + 1 because VINT fires at the end of the first logical line of the bottom border.
		if (vpos == vpos_limit + 1)
		{
			m_vint_timer->adjust(screen().time_until_pos(vpos, m_line_timing[VINT_HPOS]));
			m_pending_status |= STATUS_VINT;
			if (!m_vblank_cb.isnull())
				m_vblank_cb(1);
		}

		/* Draw borders */
		m_lborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START), vpos);
		m_rborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START + LBORDER_WIDTH + 256), vpos);

		/* Draw middle of the border */
		/* We need to do this through the regular drawing function */
		/* so sprite collisions can occur on the border. */
		select_sprites(vpos - (vpos_limit - m_frame_timing[ACTIVE_DISPLAY_V]));
		m_draw_timer->adjust(screen().time_until_pos(vpos, m_draw_time ), vpos_limit - m_frame_timing[ACTIVE_DISPLAY_V]);
		return;
	}

	vpos_limit -= m_frame_timing[ACTIVE_DISPLAY_V];

	/* Check if we're in the active display area */
	if (vpos >= vpos_limit)
	{
		if (vpos == vpos_limit)
		{
			m_reg9copy = m_reg[0x09];
		}

		if (m_line_counter == 0x00)
		{
			m_line_counter = m_reg[0x0a];
			m_hint_timer->adjust(screen().time_until_pos(vpos, m_line_timing[HINT_HPOS]));
			m_pending_hint = true;
		}
		else
		{
			m_line_counter--;
		}

		/* Draw borders */
		m_lborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START), vpos);
		m_rborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START + LBORDER_WIDTH + 256), vpos);

		/* Draw active display */
		select_sprites(vpos - vpos_limit);
		m_draw_timer->adjust(screen().time_until_pos(vpos, m_draw_time), vpos_limit);
		return;
	}

	vpos_limit -= m_frame_timing[TOP_BORDER];

	/* Check if we're in the top border area */
	if (vpos >= vpos_limit)
	{
		m_line_counter = m_reg[0x0a];

		/* Check if we're on the last line of the top border */
		if (vpos == vpos_limit + m_frame_timing[TOP_BORDER] - 1)
		{
			m_hcounter_latched = false;
			vblank_end(vpos);
		}

		/* Draw borders */
		m_lborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START), vpos);
		m_rborder_timer->adjust(screen().time_until_pos(vpos, LBORDER_START + LBORDER_WIDTH + 256), vpos);

		/* Draw middle of the border */
		/* We need to do this through the regular drawing function */
		/* so sprite collisions can occur on the border. */
		select_sprites(vpos - (vpos_limit + m_frame_timing[TOP_BORDER]));
		m_draw_timer->adjust(screen().time_until_pos(vpos, m_draw_time), vpos_limit + m_frame_timing[TOP_BORDER]);
		return;
	}

	/* we're in the vertical sync or top blanking areas */
	m_line_counter = m_reg[0x0a];
}


u8 sega315_5124_device::data_read()
{
	/* Return data buffer contents */
	const u8 temp = m_buffer;

	/* Clear pending write flag */
	m_pending_control_write = false;

	if (!machine().side_effects_disabled())
	{
		/* Load data buffer */
		m_buffer = this->space().read_byte(m_addr & 0x3fff);

		/* Bump internal address register */
		m_addr += 1;
	}
	return temp;
}


void sega315_5124_device::check_pending_flags()
{
	if (!(m_pending_status & (STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL)) && !m_pending_hint)
		return;

	/* A timer ensures that this function will run at least at end of each line.
	   When this function runs through a CPU instruction executed when the timer
	   was about to fire, the time added in the CPU timeslice may make hpos()
	   return some position in the beginning of next line. To ensure the instruction
	   will get updated status, here a maximum hpos is set if the timer reports no
	   remaining time, what could also occur due to the ahead time of the timeslice. */
	int const hpos = (m_pending_flags_timer->remaining() == attotime::zero) ? (WIDTH - 1) : screen_hpos();

	if ((m_pending_hint) && hpos >= m_line_timing[HINT_HPOS])
	{
		m_pending_hint = false;
		m_hint_occurred = true;
	}
	if ((m_pending_status & STATUS_VINT) && hpos >= m_line_timing[VINT_FLAG_HPOS])
	{
		m_pending_status &= ~STATUS_VINT;
		m_status |= STATUS_VINT;
	}
	if ((m_pending_status & STATUS_SPROVR) && hpos >= m_line_timing[SPROVR_HPOS])
	{
		m_pending_status &= ~STATUS_SPROVR;
		m_status |= STATUS_SPROVR;
		// copy and reset the pending bits that were based on the number
		// of the first sprite that overflowed.
		m_status &= m_pending_status | (STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL);
		m_pending_status |= ~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL);
	}
	if ((m_pending_status & STATUS_SPRCOL) && hpos >= m_pending_sprcol_x)
	{
		m_pending_status &= ~STATUS_SPRCOL;
		m_status |= STATUS_SPRCOL;
		m_pending_sprcol_x = 0;
	}
}


u8 sega315_5124_device::control_read()
{
	u8 temp;

	check_pending_flags();
	temp = m_status;

	if (!machine().side_effects_disabled())
	{
		/* Clear pending write flag */
		m_pending_control_write = false;

		/* Clear status flags */
		m_hint_occurred = false;
		m_status = u8(~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL));

		if (m_n_int_state == 0)
		{
			m_n_int_state = 1;

			if (!m_n_int_cb.isnull())
				m_n_int_cb(CLEAR_LINE);
		}
	}

	return temp;
}


void sega315_5124_device::write_memory(u8 data)
{
	switch (m_addrmode)
	{
	case 0x00:
	case 0x01:
	case 0x02:
		this->space().write_byte(m_addr & 0x3fff, data);
		break;

	case 0x03:
		cram_write(data);
		break;
	}

	// data written to data port loads the data buffer.
	m_buffer = data;
}


void sega315_5313_mode4_device::write_memory(u8 data)
{
	switch (m_addrmode)
	{
	case 0x00:
	case 0x01:
		this->space().write_byte(m_addr & 0x3fff, data);
		break;

	case 0x02:
	case 0x03:
		cram_write(data);
		break;
	}
	// data buffer isn't loaded on data port write.
}


void sega315_5124_device::data_write(u8 data)
{
	/* Clear pending write flag */
	m_pending_control_write = false;

	write_memory(data);
	m_addr += 1;
}


void sega315_5124_device::load_vram_addr(u8 data)
{
	// Seems like the latched data is passed straight through
	// to the address register when in the middle of doing a command.
	// Cosmic Spacehead needs this, among others
	if (m_pending_control_write)
		m_addr = (m_addr & 0xff00) | data;
	else
		m_addr = (data << 8) | (m_addr & 0xff);
}


void sega315_5313_mode4_device::load_vram_addr(u8 data)
{
	if (m_pending_control_write)
		m_control_write_data_latch = data;
	else
		m_addr = (data << 8) | m_control_write_data_latch;
}


void sega315_5124_device::control_write(u8 data)
{
	if (!m_pending_control_write)
	{
		m_pending_control_write = true;
		load_vram_addr(data);
	}
	else
	{
		int reg_num;

		/* Clear pending write flag */
		m_pending_control_write = false;

		m_addrmode = (data >> 6) & 0x03;
		load_vram_addr(data);
		switch (m_addrmode)
		{
		case 0:     /* VRAM reading mode */
			m_buffer = this->space().read_byte(m_addr & 0x3fff);
			m_addr += 1;
			break;

		case 1:     /* VRAM writing mode */
			break;

		case 2:     /* VDP register write */
			reg_num = data & m_reg_num_mask;
			// for the 315-5313, the proper bit count for register
			// numbers is emulated, but because it allows for more
			// than 16 registers, what is not implemented, there is
			// a break here if the limit is surpassed.
			if (reg_num >= 16)
			{
				break;
			}
			m_reg[reg_num] = m_addr & 0xff;
			//logerror("%s: %s: setting register %x to %02x\n", machine().describe_context(), tag(), reg_num, m_addr & 0xff);

			switch (reg_num)
			{
			case 0:
				set_display_settings();
				if (BIT(m_addr, 1))
					logerror("overscan enabled.\n");
				break;
			case 1:
				set_display_settings();
				if (screen_hpos() <= DISPLAY_DISABLED_HPOS)
					m_display_disabled = !BIT(m_reg[0x01], 6);
				break;
			case 8:
				if (screen_hpos() <= m_line_timing[XSCROLL_HPOS])
					m_reg8copy = m_reg[0x08];
			}

			check_pending_flags();

			if ((reg_num == 0 && (m_hint_occurred)) || (reg_num == 1 && (m_status & STATUS_VINT)))
			{
				// For HINT disabling through register 00:
				// "Line IRQ VCount" test, of Flubba's VDPTest ROM, disables HINT to wait
				// for next VINT, but HINT occurs when the operation is about to execute.
				// So here, where the setting is done, the /INT state needs to be cleared.
				//
				// For VINT disabling through register 01:
				// When running eagles5 on the smskr driver the /INT state is 0 because of some
				// previous HINTs that occurred. eagles5 sets register 01 to 0x02 and expects
				// the irq state to be cleared after that.
				// The following bit of code takes care of that.
				//
				if ((reg_num == 0 && !BIT(m_reg[0x00], 4)) ||
						(reg_num == 1 && !BIT(m_reg[0x01], 5)))
				{
					if (m_n_int_state == 0)
					{
						m_n_int_state = 1;

						if (!m_n_int_cb.isnull())
						{
							m_n_int_cb(CLEAR_LINE);
						}
					}
				}
				else
				{
					// For register 01 and VINT enabling:
					// Assert the /INT line for the scoreboard of robocop3,
					// on the sms/smspal driver, be displayed correctly.
					//
					// Assume the same behavior for reg0+HINT.
					//
					m_n_int_state = 0;

					if (!m_n_int_cb.isnull())
						m_n_int_cb(ASSERT_LINE);
				}
			}
			m_addrmode = 0;
			break;

		case 3:     /* CRAM writing mode */
			break;
		}
	}
}


u16 sega315_5124_device::name_table_row_mode4(int row)
{
	return ((row >> 3) << 6) & (((m_reg[0x02] & 0x01) << 10) | ((m_reg[0x04] & 0x03) << 11) | 0x23ff);
}


u16 sega315_5246_device::name_table_row_mode4(int row)
{
	return (row >> 3) << 6;
}


void sega315_5124_device::draw_leftmost_pixels_mode4(int *line_buffer, int *priority_selected, int fine_x_scroll, int palette_selected, int tile_line)
{
	// To draw the leftmost pixels when they aren't part of a tile column
	// due to scrolling, Sega Master System has a weird behaviour to select
	// which palette will be used to obtain the color in entry 0, that
	// depends on the content of tile 0x100.
	// This implementation mimics the behaviour of the Emulicious emulator,
	// seen with the test ROM provided by sverx here:
	//
	// http://www.smspower.org/forums/15653-CommunityEffortRequestHelpDiscoverHowTheVDPHandlesTheLeftmostPixelsWhenScrolling
	//
	// From the tile 0x100, it uses bit 1 of the plane that would
	// select the color for the pixel 4 at the current line.

	const int pixel_x = 4;
	// The parsing seems to occur before the line counter is incremented
	// to the number of the line to be drawn.
	int parse_line = tile_line - 1;

	// Select tile 0x100.
	// Tried before with the number stored in m_sprite_tile_selected[0],
	// but with it the expected behaviour only occurs with some BIOSes.
	const int tile_selected = 0x100;

	// Load data of bit plane 1 for the selected tile.
	const int tmp_bit_plane_1 = space().read_byte((tile_selected << 5) + ((parse_line & 0x07) << 2) + 0x01);
	const u8 pen_bit_1 = BIT(tmp_bit_plane_1, 7 - pixel_x);

	for (int pixel_plot_x = 0; pixel_plot_x < fine_x_scroll; pixel_plot_x++)
	{
		line_buffer[pixel_plot_x] = m_current_palette[pen_bit_1 ? 0x10 : 0x00];
		priority_selected[pixel_plot_x] = 0;
	}
}


void sega315_5313_mode4_device::draw_leftmost_pixels_mode4(int *line_buffer, int *priority_selected, int fine_x_scroll, int palette_selected, int tile_line)
{
	// To draw the leftmost pixels when they aren't part of a tile column
	// due to scrolling, Sega Genesis/Mega Drive seems to use entry #0 of
	// the palette selected by the next background tile to be drawn on screen.

	for (int pixel_plot_x = 0; pixel_plot_x < fine_x_scroll; pixel_plot_x++)
	{
		line_buffer[pixel_plot_x] = m_current_palette[palette_selected ? 0x10 : 0x00];
		priority_selected[pixel_plot_x] = 0;
	}
}


void sega315_5124_device::draw_scanline_mode4(int *line_buffer, int *priority_selected, int line)
{
	/* if top 2 rows of screen not affected by horizontal scrolling, then x_scroll = 0 */
	/* else x_scroll = m_reg8copy                                                      */
	const int x_scroll = ((BIT(m_reg[0x00], 6) && (line < 16)) ? 0 : m_reg8copy);

	const int x_scroll_start_column = 32 - (x_scroll >> 3);             /* x starting column tile */
	const int fine_x_scroll = (x_scroll & 0x07);

	int scroll_mod;
	u16 name_table_address;
	if (m_y_pixels != 192)
	{
		name_table_address = ((m_reg[0x02] & 0x0c) << 10) | 0x0700;
		scroll_mod = 256;
	}
	else
	{
		name_table_address = (m_reg[0x02] << 10) & 0x3800;
		scroll_mod = 224;
	}

	/* Draw background layer */
	for (int tile_column = 0; tile_column < 32; tile_column++)
	{
		int tile_line;
		const int table_column = ((tile_column + x_scroll_start_column) & 0x1f) << 1;

		/* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
		/* vertical scrolling when bit 7 of reg[0x00] is set */
		const int y_scroll = (BIT(m_reg[0x00], 7) && (tile_column > 23)) ? 0 : m_reg9copy;

		const u16 tile_data = space().read_word(name_table_address + name_table_row_mode4((line + y_scroll) % scroll_mod) + table_column);

		const int tile_selected = (tile_data & 0x01ff);
		const int priority_select = tile_data & PRIORITY_BIT;
		const int palette_selected = BIT(tile_data, 11);
		const int vert_selected = BIT(tile_data, 10);
		const int horiz_selected = BIT(tile_data, 9);

		tile_line = line - ((0x07 - (y_scroll & 0x07)) + 1);
		if (vert_selected)
			tile_line = 0x07 - tile_line;

		const int bit_plane_0 = space().read_byte(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x00);
		const int bit_plane_1 = space().read_byte(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x01);
		const int bit_plane_2 = space().read_byte(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x02);
		const int bit_plane_3 = space().read_byte(((tile_selected << 5) + ((tile_line & 0x07) << 2)) + 0x03);

		/* Column 0 is the leftmost tile column that completely entered in the screen.
		   If the leftmost pixels aren't part of a complete tile, due to horizontal
		   scrolling, they are drawn only with color #0 of the selected palette. */
		if (tile_column == 0 && fine_x_scroll > 0)
			draw_leftmost_pixels_mode4(line_buffer, priority_selected, fine_x_scroll, palette_selected, tile_line);

		for (int pixel_x = 0; pixel_x < 8; pixel_x++)
		{
			const u8 pen_bit_0 = BIT(bit_plane_0, 7 - pixel_x);
			const u8 pen_bit_1 = BIT(bit_plane_1, 7 - pixel_x);
			const u8 pen_bit_2 = BIT(bit_plane_2, 7 - pixel_x);
			const u8 pen_bit_3 = BIT(bit_plane_3, 7 - pixel_x);

			u8 pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0);
			if (palette_selected)
				pen_selected |= 0x10;

			int pixel_plot_x = !horiz_selected ? pixel_x : (7 - pixel_x);

			pixel_plot_x = fine_x_scroll + (tile_column << 3) + pixel_plot_x;
			if (pixel_plot_x < 256)
			{
				line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
				priority_selected[pixel_plot_x] = priority_select | (pen_selected & 0x0f);
			}
		}
	}
}


u16 sega315_5124_device::sprite_attributes_addr_mode4(u16 base)
{
	return base & ((BIT(m_reg[0x05], 0) << 7) | 0x3f7f);
}


u16 sega315_5246_device::sprite_attributes_addr_mode4(u16 base)
{
	return base;
}


u8 sega315_5124_device::sprite_tile_mask_mode4(u8 tile_number)
{
	return tile_number & (((m_reg[0x06] & 0x03) << 6) | 0x3f);
}


u8 sega315_5246_device::sprite_tile_mask_mode4(u8 tile_number)
{
	return tile_number;
}


void sega315_5124_device::sprite_count_overflow(int line, int sprite_index)
{
	/* Overflow is flagged only on active display and when VINT isn't active */
	if (!(m_status & STATUS_VINT) && line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
	{
		u8 sprite_number_bits;
		m_pending_status |= STATUS_SPROVR;
		if (sprite_index < 14)
			sprite_number_bits = (sprite_index + 1) / 2;
		else
			sprite_number_bits = sprite_index / 2;

		m_pending_status &= sprite_number_bits | (STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL);
	}
}


void sega315_5313_mode4_device::sprite_count_overflow(int line, int sprite_index)
{
	if (!m_display_disabled)
	{
		sega315_5124_device::sprite_count_overflow(line, sprite_index);
	}
}


void sega315_5124_device::select_sprites(int line)
{
	/* Check if SI is set */
	m_sprite_height = BIT(m_reg[0x01], 1) ? 16 : 8;
	/* Check if MAG is set */
	m_sprite_zoom_scale = BIT(m_reg[0x01], 0) ? 2 : 1;

	m_sprite_count = 0;

	if (m_vdp_mode == 0 || m_vdp_mode == 2)
	{
		/* TMS9918 compatibility sprites */

		const int max_sprites = 4;

		m_sprite_base = ((m_reg[0x05] & 0x7f) << 7);

		for (int sprite_index = 0; (sprite_index < 32 * 4); sprite_index += 4)
		{
			/* At this point the VDP vcount still doesn't refer the new line,
			   because the logical start point is slightly shifted on the scanline */
			int parse_line = line - 1;

			int sprite_y = space().read_byte(m_sprite_base + sprite_index);
			if (sprite_y == 0xd0)
				break;

			if (sprite_y >= 240)
			{
				sprite_y -= 256; /* wrap from top if y position is >= 240 */
			}

			if (m_sprite_zoom_scale > 1 && m_sprite_count < m_max_sprite_zoom_vcount)
			{
				/* Divide before use the value for comparison, or else an
				   off-by-one bug could occur, as seen with Tarzan, for Game Gear */
				parse_line >>= 1;
				sprite_y >>= 1;
			}

			if ((parse_line >= sprite_y) && (parse_line < (sprite_y + m_sprite_height)))
			{
				if (m_sprite_count < max_sprites)
				{
					const int sprite_x = space().read_byte(m_sprite_base + sprite_index + 1);
					int sprite_tile_selected = space().read_byte(m_sprite_base + sprite_index + 2);
					const u8 flags = space().read_byte(m_sprite_base + sprite_index + 3);

					int sprite_line = parse_line - sprite_y;

					if (m_sprite_height == 16)
					{
						sprite_tile_selected &= 0xfc;

						if (sprite_line > 0x07)
						{
							sprite_tile_selected += 1;
							sprite_line -= 8;
						}
					}

					m_sprite_x[m_sprite_count] = sprite_x;
					m_sprite_tile_selected[m_sprite_count] = sprite_tile_selected;
					m_sprite_flags[m_sprite_count] = flags;
					m_sprite_pattern_line[m_sprite_count] = ((m_reg[0x06] & 0x07) << 11) + sprite_line;

					m_sprite_count++;
				}
				else
				{
					sprite_count_overflow(line, sprite_index);
				}
			}
		}
	}
	else
	{
		/* Regular sprites */

		const int max_sprites = 8;

		m_sprite_base = (m_reg[0x05] << 7) & 0x3f00;

		for (int sprite_index = 0; (sprite_index < 64); sprite_index++)
		{
			/* At this point the VDP vcount still doesn't refer the new line,
			   because the logical start point is slightly shifted on the scanline */
			int parse_line = line - 1;

			int sprite_y = space().read_byte(m_sprite_base + sprite_index);
			if (m_y_pixels == 192 && sprite_y == 0xd0)
				break;

			if (sprite_y >= 240)
			{
				sprite_y -= 256; /* wrap from top if y position is >= 240 */
			}

			if (m_sprite_zoom_scale > 1 && m_sprite_count < m_max_sprite_zoom_vcount)
			{
				/* Divide before use the value for comparison, or else an
				   off-by-one bug could occur, as seen with Tarzan, for Game Gear */
				parse_line >>= 1;
				sprite_y >>= 1;
			}

			if ((parse_line >= sprite_y) && (parse_line < (sprite_y + m_sprite_height)))
			{
				if (m_sprite_count < max_sprites)
				{
					const int sprite_line = parse_line - sprite_y;
					int sprite_x = space().read_byte(sprite_attributes_addr_mode4(m_sprite_base + 0x80) + (sprite_index << 1));
					int sprite_tile_selected = space().read_byte(sprite_attributes_addr_mode4(m_sprite_base + 0x81) + (sprite_index << 1));

					sprite_tile_selected = sprite_tile_mask_mode4(sprite_tile_selected);

					if (BIT(m_reg[0x00], 3))
						sprite_x -= 0x08;    /* sprite shift */

					if (BIT(m_reg[0x06], 2))
						sprite_tile_selected += 256; /* pattern table select */

					if (m_sprite_height == 16)
						sprite_tile_selected &= 0x01fe; /* force even index */

					if (sprite_line > 0x07)
						sprite_tile_selected += 1;

					m_sprite_x[m_sprite_count] = sprite_x;
					m_sprite_tile_selected[m_sprite_count] = sprite_tile_selected;
					m_sprite_pattern_line[m_sprite_count] = ((sprite_line & 0x07) << 2);

					m_sprite_count++;
				}
				else
				{
					sprite_count_overflow(line, sprite_index);
				}
			}
		}
	}
}


void sega315_5313_mode4_device::select_sprites(int line)
{
	sega315_5124_device::select_sprites(line);

	/*
	    Info from Charles MacDonald regarding real hardware behavior:
	    (http://www.smspower.org/forums/15772-Sprites8x16Question)

	    "As I recall the SMS will parses the sprite table on line N to find the
	    sprite numbers to display on line N+1, and on the next line it displays
	    those sprites. The data for the sprites is read in real-time and is not
	    buffered from any earlier time. The only thing that's buffered are the
	    eight sprite numbers.

	    On the MD it has two line buffers (RAM) that it renders sprite data into
	    on one line, and displays that buffer on the next line while preparing
	    the next one. This only reveals itself if you play with the screen
	    blanking or left-column blanking bits, as the display process erases the
	    other buffer and on a blanked line nothing gets erased.

	    So if you turn on either of those bits later they show the old sprite
	    data that hadn't been erased yet. This is how you can show a line of
	    sprite data further down on the screen than where the sprites were."
	*/

	// Runs the function that draws sprites, but only to check for
	// sprite collision, due to it be flagged on the 315-5313 VDP in
	// a point where the active screen was not drawn yet.
	if (m_sprite_count > 0 && !m_display_disabled)
	{
		int blitline_buffer[256];
		int priority_selected[256];
		draw_sprites_mode4(blitline_buffer, priority_selected, line);
	}
}


void sega315_5124_device::sprite_collision(int line, int sprite_col_x)
{
	/* SMS/GG: collisions don't occur on column 0 if it is disabled. */
	if (BIT(m_reg[0x00], 5) && sprite_col_x < 8)
		return;

	m_pending_status |= STATUS_SPRCOL;
	m_pending_sprcol_x = m_line_timing[SPRCOL_BASEHPOS] + sprite_col_x;
}


void sega315_5313_mode4_device::sprite_collision(int line, int sprite_col_x)
{
	if (line >= 0 && line < m_frame_timing[ACTIVE_DISPLAY_V])
	{
		// This function is been used to check for sprite collision of
		// the 315-5313 VDP, that occurs before the active screen is
		// drawn, so it must not flag a collision again when drawing.
		if (screen_hpos() < m_draw_time)
		{
			m_pending_status |= STATUS_SPRCOL;
			m_pending_sprcol_x = m_line_timing[SPRCOL_BASEHPOS];
		}
	}
}


void sega315_5124_device::draw_sprites_mode4(int *line_buffer, int *priority_selected, int line)
{
	if (m_display_disabled || m_sprite_count == 0)
		return;

	bool sprite_col_occurred = false;
	int sprite_col_x = 255;
	u8 collision_buffer[256] = { 0 };

	// Draw sprite layer
	for (int sprite_buffer_index = m_sprite_count - 1; sprite_buffer_index >= 0; sprite_buffer_index--)
	{
		const int sprite_x = m_sprite_x[sprite_buffer_index];
		const int sprite_tile_selected = m_sprite_tile_selected[sprite_buffer_index];
		const u16 sprite_pattern_line = m_sprite_pattern_line[sprite_buffer_index];
		const int zoom_scale = sprite_buffer_index < m_max_sprite_zoom_hcount ? m_sprite_zoom_scale : 1;

		const u8 bit_plane_0 = space().read_byte((sprite_tile_selected << 5) + sprite_pattern_line + 0x00);
		const u8 bit_plane_1 = space().read_byte((sprite_tile_selected << 5) + sprite_pattern_line + 0x01);
		const u8 bit_plane_2 = space().read_byte((sprite_tile_selected << 5) + sprite_pattern_line + 0x02);
		const u8 bit_plane_3 = space().read_byte((sprite_tile_selected << 5) + sprite_pattern_line + 0x03);

		for (int pixel_x = 0; pixel_x < 8; pixel_x++)
		{
			const u8 pen_bit_0 = BIT(bit_plane_0, 7 - pixel_x);
			const u8 pen_bit_1 = BIT(bit_plane_1, 7 - pixel_x);
			const u8 pen_bit_2 = BIT(bit_plane_2, 7 - pixel_x);
			const u8 pen_bit_3 = BIT(bit_plane_3, 7 - pixel_x);
			const u8 pen_selected = (pen_bit_3 << 3 | pen_bit_2 << 2 | pen_bit_1 << 1 | pen_bit_0) | 0x10;

			if (pen_selected == 0x10) // Transparent palette so skip draw
				continue;

			int pixel_plot_x;
			if (zoom_scale > 1)
			{
				/* sprite doubling is enabled */
				pixel_plot_x = sprite_x + (pixel_x << 1);
			}
			else
			{
				pixel_plot_x = sprite_x + pixel_x;
			}

			/* Draw at pixel position and, if zoomed, at pixel+1 */
			for (int zoom = 0; zoom < zoom_scale; zoom++)
			{
				pixel_plot_x += zoom;

				/* check to prevent going outside of active display area */
				if (pixel_plot_x < 0 || pixel_plot_x > 255)
					continue;

				/* Draw sprite pixel */
				/* Check if the background has lower priority */
				if (!(priority_selected[pixel_plot_x] & PRIORITY_BIT))
				{
					line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
					priority_selected[pixel_plot_x] = pen_selected;
				}
				else
				{
					/* Check if the higher priority background has transparent pixel */
					if (priority_selected[pixel_plot_x] == PRIORITY_BIT)
					{
						line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
						priority_selected[pixel_plot_x] = pen_selected;
					}
				}
				if (collision_buffer[pixel_plot_x] != 1)
				{
					collision_buffer[pixel_plot_x] = 1;
				}
				else
				{
					sprite_col_occurred = true;
					sprite_col_x = std::min(sprite_col_x, pixel_plot_x);
				}
			}
		}

		if (sprite_col_occurred)
			sprite_collision(line, sprite_col_x);
	}
}


void sega315_5124_device::draw_sprites_tms9918_mode(int *line_buffer, int line)
{
	if (m_display_disabled || m_sprite_count == 0)
		return;

	bool sprite_col_occurred = false;
	int sprite_col_x = 255;
	u8 collision_buffer[256] = { 0 };

	/* Draw sprite layer */
	for (int sprite_buffer_index = m_sprite_count - 1; sprite_buffer_index >= 0; sprite_buffer_index--)
	{
		int sprite_x = m_sprite_x[sprite_buffer_index];
		int sprite_tile_selected = m_sprite_tile_selected[sprite_buffer_index];
		const u16 sprite_pattern_line = m_sprite_pattern_line[sprite_buffer_index];
		const u8 flags = m_sprite_flags[sprite_buffer_index];
		const int pen_selected = m_palette_offset + (flags & 0x0f);
		const int zoom_scale = sprite_buffer_index < m_max_sprite_zoom_hcount ? m_sprite_zoom_scale : 1;

		if (BIT(flags, 7))
			sprite_x -= 32;

		for (int height = 8; height <= m_sprite_height; height += 8)
		{
			if (height == 16)
			{
				sprite_tile_selected += 2;
				sprite_x += (zoom_scale > 1 ? 16 : 8);
			}

			u8 pattern = space().read_byte(sprite_pattern_line + sprite_tile_selected * 8);

			for (int pixel_x = 0; pixel_x < 8; pixel_x++)
			{
				if (pen_selected && BIT(pattern, 7 - pixel_x))
				{
					int pixel_plot_x;
					if (zoom_scale > 1)
						pixel_plot_x = sprite_x + (pixel_x << 1);
					else
						pixel_plot_x = sprite_x + pixel_x;

					/* Draw at pixel position and, if zoomed, at pixel+1 */
					for (int zoom = 0; zoom < zoom_scale; zoom++)
					{
						pixel_plot_x += zoom;

						/* check to prevent going outside of active display area */
						if (pixel_plot_x < 0 || pixel_plot_x > 255)
							continue;

						line_buffer[pixel_plot_x] = m_current_palette[pen_selected];

						if (collision_buffer[pixel_plot_x] != 1)
						{
							collision_buffer[pixel_plot_x] = 1;
						}
						else
						{
							sprite_col_occurred = true;
							sprite_col_x = std::min(sprite_col_x, pixel_plot_x);
						}
					}
				}
			}
		}

		if (sprite_col_occurred)
			sprite_collision(line, sprite_col_x);
	}
}


// Display mode 2 (Graphics II Mode)
void sega315_5124_device::draw_scanline_mode2(int *line_buffer, int line)
{
	const u16 name_table_base = ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
	const u16 color_base = ((m_reg[0x03] & 0x80) << 6);
	const int color_mask = ((m_reg[0x03] & 0x7f) << 3) | 0x07;
	const u16 pattern_base = ((m_reg[0x04] & 0x04) << 11);
	const int pattern_mask = ((m_reg[0x04] & 0x03) << 8) | 0xff;
	const int pattern_offset = (line & 0xc0) << 2;

	/* Draw background layer */
	for (int tile_column = 0; tile_column < 32; tile_column++)
	{
		const u8 name = space().read_byte(name_table_base + tile_column);
		const u8 pattern = space().read_byte(pattern_base + (((pattern_offset + name) & pattern_mask) * 8) + (line & 0x07) );
		const u8 colors = space().read_byte(color_base + (((pattern_offset + name) & color_mask) * 8) + (line & 0x07) );

		for (int pixel_x = 0; pixel_x < 8; pixel_x++)
		{
			u8 pen_selected;
			const int pixel_plot_x = (tile_column << 3) + pixel_x;

			if (BIT(pattern, 7 - pixel_x))
			{
				pen_selected = colors >> 4;
			}
			else
			{
				pen_selected = colors & 0x0f;
			}

			if (!pen_selected)
				pen_selected = BACKDROP_COLOR;

			pen_selected += m_palette_offset;
			line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
		}
	}
}


// Display mode 0 (Graphics I Mode)
void sega315_5124_device::draw_scanline_mode0(int *line_buffer, int line)
{
	const u16 name_base = ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
	const u16 color_base = ((m_reg[0x03] << 6) & (VRAM_SIZE - 1));
	const u16 pattern_base = ((m_reg[0x04] << 11) & (VRAM_SIZE - 1));

	/* Draw background layer */
	for (int tile_column = 0; tile_column < 32; tile_column++)
	{
		const u8 name = space().read_byte(name_base + tile_column);
		const u8 pattern = space().read_byte(pattern_base + (name << 3) + (line & 0x07));
		const u8 colors = space().read_byte(color_base + (name >> 3));

		for (int pixel_x = 0; pixel_x < 8; pixel_x++)
		{
			int pen_selected;
			const int pixel_plot_x = (tile_column << 3) + pixel_x;

			if (BIT(pattern, 7 - pixel_x))
				pen_selected = colors >> 4;
			else
				pen_selected = colors & 0x0f;

			if (!pen_selected)
				pen_selected = BACKDROP_COLOR;

			pen_selected += m_palette_offset;
			line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
		}
	}
}


// Display mode 1 (Text Mode)
void sega315_5124_device::draw_scanline_mode1(int *line_buffer, int line)
{
	const u16 name_base = ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
	const u16 pattern_base = ((m_reg[0x04] << 11) & (VRAM_SIZE - 1));

	for (int pixel_plot_x = 0; pixel_plot_x < 8; pixel_plot_x++)
	{
		line_buffer[pixel_plot_x] = m_current_palette[BACKDROP_COLOR];
	}

	/* Draw background layer */
	for (int tile_column = 0; tile_column < 40; tile_column++)
	{
		const u8 name = space().read_byte(name_base + tile_column);
		const u8 pattern = space().read_byte(pattern_base + (name << 3) + (line & 0x07));

		for (int pixel_x = 0; pixel_x < 6; pixel_x++)
		{
			const int pixel_plot_x = (tile_column * 6) + pixel_x + 8;

			int pen_selected;
			if (BIT(pattern, 7 - pixel_x))
				pen_selected = m_reg[0x07] >> 4;
			else
				pen_selected = m_reg[0x07] & 0x0f;

			if (!pen_selected)
				pen_selected = BACKDROP_COLOR;

			pen_selected += m_palette_offset;
			line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
		}
	}

	for (int pixel_plot_x = 248; pixel_plot_x < 256; pixel_plot_x++)
		line_buffer[pixel_plot_x] = m_current_palette[BACKDROP_COLOR];
}


// Display mode 3 (Multicolor Mode)
void sega315_5124_device::draw_scanline_mode3(int *line_buffer, int line)
{
	const u16 name_base = ((m_reg[0x02] & 0x0f) << 10) + ((line >> 3) * 32);
	const u16 pattern_base = ((m_reg[0x04] << 11) & (VRAM_SIZE - 1));

	/* Draw background layer */
	for (int tile_column = 0; tile_column < 32; tile_column++)
	{
		const u8 name = space().read_byte(name_base + tile_column);
		const u8 pattern = space().read_byte(pattern_base + (name << 3) + (((line >> 3) & 3) << 1) + ((line & 4) >> 2));

		for (int pixel_x = 0; pixel_x < 8; pixel_x++)
		{
			const int pixel_plot_x = (tile_column << 3) + pixel_x;
			int pen_selected = (pattern >> (~pixel_x & 4)) & 0x0f;

			if (!pen_selected)
				pen_selected = BACKDROP_COLOR;

			pen_selected += m_palette_offset;
			line_buffer[pixel_plot_x] = m_current_palette[pen_selected];
		}
	}
}


void sega315_5124_device::draw_scanline(int pixel_offset_x, int pixel_plot_y, int line)
{
	int blitline_buffer[256];
	int priority_selected[256];

	/* Sprite processing is restricted because collisions on top border of extended
	   resolution break the scoreboard of Fantasy Dizzy (SMS) on smspal driver */

	if (line < m_frame_timing[ACTIVE_DISPLAY_V])
	{
		std::fill(std::begin(priority_selected), std::end(priority_selected), 1);

		switch (m_vdp_mode)
		{
		case 0:
			if (line >= 0)
				draw_scanline_mode0(blitline_buffer, line);
			if (line >= 0 || (line >= -13 && m_y_pixels == 192))
				draw_sprites_tms9918_mode(blitline_buffer, line);
			break;

		case 1:
			if (line >= 0)
				draw_scanline_mode1(blitline_buffer, line);
			// Text Mode, no sprite drawing.
			break;

		case 2:
			if (line >= 0)
				draw_scanline_mode2(blitline_buffer, line);
			if (line >= 0 || (line >= -13 && m_y_pixels == 192))
				draw_sprites_tms9918_mode(blitline_buffer, line);
			break;

		case 3:
			if (line >= 0)
				draw_scanline_mode3(blitline_buffer, line);
			if (line >= 0 || (line >= -13 && m_y_pixels == 192))
				draw_sprites_tms9918_mode(blitline_buffer, line);
			break;

		case 4:
		default:
			if (line >= 0)
				draw_scanline_mode4(blitline_buffer, priority_selected, line);
			if (line >= 0 || (line >= -13 && m_y_pixels == 192))
				draw_sprites_mode4(blitline_buffer, priority_selected, line);
			break;

		case 5:
			// Mode 5 (Mega Drive / Genesis) not implemented.
			break;
		}
	}

	/* Check if display is disabled or we're below/above active area */
	if (m_display_disabled || line < 0 || line >= m_frame_timing[ACTIVE_DISPLAY_V])
	{
		rectangle rec;
		rec.min_y = rec.max_y = pixel_plot_y + line;

		rec.min_x = pixel_offset_x;
		rec.max_x = pixel_offset_x + 255;
		m_tmpbitmap.fill(m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]), rec);
		m_y1_bitmap.fill((m_reg[0x07] & 0x0f) ? 1 : 0, rec);
	}
	else
	{
		blit_scanline(blitline_buffer, priority_selected, pixel_offset_x, pixel_plot_y, line);
	}
}


void sega315_5124_device::blit_scanline(int *line_buffer, int *priority_selected, int pixel_offset_x, int pixel_plot_y, int line)
{
	u32 *p_bitmap = &m_tmpbitmap.pix32(pixel_plot_y + line, pixel_offset_x);
	u8  *p_y1 = &m_y1_bitmap.pix8(pixel_plot_y + line, pixel_offset_x);
	int x = 0;

	if (m_vdp_mode == 4 && BIT(m_reg[0x00], 5))
	{
		/* Fill column 0 with overscan color from m_reg[0x07] */
		do
		{
			p_bitmap[x] = m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]);
			p_y1[x] = (m_reg[0x07] & 0x0f) ? 1 : 0;
		}
		while(++x < 8);
	}

	do
	{
		p_bitmap[x] = m_palette_lut->pen(line_buffer[x]);
		p_y1[x] = (priority_selected[x] & 0x0f) ? 1 : 0;
	}
	while(++x < 256);
}


void sega315_5377_device::blit_scanline(int *line_buffer, int *priority_selected, int pixel_offset_x, int pixel_plot_y, int line)
{
	if (m_sega315_5124_compatibility_mode)
	{
		sega315_5124_device::blit_scanline(line_buffer, priority_selected, pixel_offset_x, pixel_plot_y, line);
	}
	else
	{
		u32 *p_bitmap = &m_tmpbitmap.pix32(pixel_plot_y + line, pixel_offset_x);
		u8  *p_y1 = &m_y1_bitmap.pix8(pixel_plot_y + line, pixel_offset_x);
		int x = 0;

		/* border on left side of the GG active screen */
		do
		{
			p_bitmap[x] = m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]);
			p_y1[x] = (m_reg[0x07] & 0x0f) ? 1 : 0;
		}
		while (++x < 48);

		if (line >= 24 && line < 168)
		{
			do
			{
				p_bitmap[x] = m_palette_lut->pen(line_buffer[x]);
				p_y1[x] = (priority_selected[x] & 0x0f) ? 1 : 0;
			}
			while (++x < 208);
		}
		else
		{
			/* top/bottom GG border */
			do
			{
				p_bitmap[x] = m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]);
				p_y1[x] = (m_reg[0x07] & 0x0f) ? 1 : 0;
			}
			while (++x < 208);
		}

		/* border on right side of the GG active screen */
		do
		{
			p_bitmap[x] = m_palette_lut->pen(m_current_palette[BACKDROP_COLOR]);
			p_y1[x] = (m_reg[0x07] & 0x0f) ? 1 : 0;
		}
		while (++x < 256);
	}
}


void sega315_5124_device::update_palette()
{
	/* Exit if palette has no changes */
	if (!m_cram_dirty)
	{
		return;
	}
	m_cram_dirty = false;

	if (m_vdp_mode != 4)
	{
		for(int i = 0; i < 16; i++)
		{
			m_current_palette[i] = 64 + i;
		}
		return;
	}

	for (int i = 0; i < 32; i++)
	{
		m_current_palette[i] = m_CRAM[i] & 0x3f;
	}
}


void sega315_5377_device::update_palette()
{
	/* Exit if palette has no changes */
	if (!m_cram_dirty)
		return;

	m_cram_dirty = false;

	if (m_sega315_5124_compatibility_mode)
	{
		for (int i = 0; i < 32; i++)
			m_current_palette[i] = ((m_CRAM[i] & 0x30) << 6) | ((m_CRAM[i] & 0x0c ) << 4) | ((m_CRAM[i] & 0x03) << 2);
	}
	else
	{
		for (int i = 0; i < 32; i++)
			m_current_palette[i] = (m_CRAM[2*i] | (m_CRAM[2*i+1] << 8)) & 0x0fff;
	}
}


void sega315_5313_mode4_device::update_palette()
{
	/* Exit if palette has no changes */
	if (!m_cram_dirty)
	{
		return;
	}
	m_cram_dirty = false;

	if (m_vdp_mode != 4)
	{
		return;
	}

	for (int i = 0; i < 32; i++)
	{
		m_current_palette[i] = (512 * 3) + (m_CRAM[i] & 0x3f);
	}
}


void sega315_5124_device::cram_write(u8 data)
{
	u16 address = m_addr & m_cram_mask;
	if (data != m_CRAM[address])
	{
		m_CRAM[address] = data;
		m_cram_dirty = true;
	}
}


void sega315_5377_device::cram_write(u8 data)
{
	if (m_sega315_5124_compatibility_mode)
	{
		sega315_5124_device::cram_write(data);
	}
	else
	{
		if (m_addr & 1)
		{
			u16 address = (m_addr & m_cram_mask) & ~1;
			if (m_buffer != m_CRAM[address] || data != m_CRAM[address + 1])
			{
				m_CRAM[address] = m_buffer;
				m_CRAM[address + 1] = data;
				m_cram_dirty = true;
			}
		}
	}
}


u32 sega315_5124_device::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	copybitmap(bitmap, m_tmpbitmap, 0, 0, 0, 0, cliprect);
	return 0;
}


// MegaDrive/Genesis VDP (315-5313) is currently coded as superset of the 315-5124
// To support properly SMS VDP in MegaTech and MegaPlay, we start the 315-5124
// in all systems using MegaDrive/Genesis VDP, but this affects the performance
// of the emulator hence we stop it in systems that don't need it
// Proper way to handle this would be implement the 315-5124 modes in the 315-5313
// device instead of running the two chips separately...
void sega315_5313_mode4_device::stop_timers()
{
	m_display_timer->adjust(attotime::never);
	m_pending_flags_timer->adjust(attotime::never);
	m_hint_timer->adjust(attotime::never);
	m_vint_timer->adjust(attotime::never);
	m_nmi_timer->adjust(attotime::never);
	m_draw_timer->adjust(attotime::never);
	m_lborder_timer->adjust(attotime::never);
	m_rborder_timer->adjust(attotime::never);
}


/*****************************************************************************
    DEVICE INTERFACE
*****************************************************************************/

void sega315_5124_device::device_post_load()
{
	set_frame_timing();
}

void sega315_5124_device::device_start()
{
	/* Resolve callbacks */
	m_vblank_cb.resolve();
	m_n_csync_cb.resolve();
	m_n_int_cb.resolve();
	m_n_nmi_cb.resolve();

	/* Make temp bitmap for rendering */
	screen().register_screen_bitmap(m_tmpbitmap);
	screen().register_screen_bitmap(m_y1_bitmap);

	m_display_timer = timer_alloc(TIMER_LINE);
	m_display_timer->adjust(screen().time_until_pos(0, DISPLAY_CB_HPOS), 0, screen().scan_period());
	m_pending_flags_timer = timer_alloc(TIMER_FLAGS);
	m_pending_flags_timer->adjust(screen().time_until_pos(0, WIDTH - 1), 0, screen().scan_period());
	m_draw_timer = timer_alloc(TIMER_DRAW);
	m_lborder_timer = timer_alloc(TIMER_LBORDER);
	m_rborder_timer = timer_alloc(TIMER_RBORDER);
	m_hint_timer = timer_alloc(TIMER_HINT);
	m_vint_timer = timer_alloc(TIMER_VINT);
	m_nmi_timer = timer_alloc(TIMER_NMI);

	save_item(NAME(m_status));
	save_item(NAME(m_pending_status));
	save_item(NAME(m_pending_sprcol_x));
	save_item(NAME(m_reg8copy));
	save_item(NAME(m_reg9copy));
	save_item(NAME(m_addrmode));
	save_item(NAME(m_addr));
	save_item(NAME(m_cram_mask));
	save_item(NAME(m_cram_dirty));
	save_item(NAME(m_hint_occurred));
	save_item(NAME(m_pending_hint));
	save_item(NAME(m_pending_control_write));
	save_item(NAME(m_buffer));
	save_item(NAME(m_control_write_data_latch));
	save_item(NAME(m_sega315_5124_compatibility_mode));
	save_item(NAME(m_display_disabled));
	save_item(NAME(m_n_int_state));
	save_item(NAME(m_n_nmi_state));
	save_item(NAME(m_n_nmi_in_state));
	save_item(NAME(m_vdp_mode));
	save_item(NAME(m_y_pixels));
	save_item(NAME(m_line_counter));
	save_item(NAME(m_hcounter));
	save_item(NAME(m_hcounter_latched));
	save_item(NAME(m_reg));
	save_item(NAME(m_current_palette));

	// these were created with register_screen_bitmap which is dynamic, and will reallocate if the screen size changes, saving them is NOT safe with the current core.
	// The Genesis VDP (315_5313.cpp) which uses this as a base in order to support the legacy SMS operation mode can change resolutions for example.
	//save_item(NAME(m_tmpbitmap));
	//save_item(NAME(m_y1_bitmap));
	save_item(NAME(m_draw_time));
	save_item(NAME(m_sprite_base));
	save_item(NAME(m_sprite_pattern_line));
	save_item(NAME(m_sprite_tile_selected));
	save_item(NAME(m_sprite_x));
	save_item(NAME(m_sprite_flags));
	save_item(NAME(m_sprite_count));
	save_item(NAME(m_sprite_height));
	save_item(NAME(m_sprite_zoom_scale));
	save_item(NAME(m_max_sprite_zoom_hcount));
	save_item(NAME(m_max_sprite_zoom_vcount));
	save_item(NAME(m_CRAM));
}


void sega315_5124_device::device_reset()
{
	/* Most register are 0x00 at power-up */
	std::fill(std::begin(m_reg), std::end(m_reg), 0x00);

	m_reg[0x02] = 0x0e;
	m_reg[0x0a] = 0xff;

	m_status = m_pending_status = u8(~(STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL));
	m_pending_sprcol_x = 0;
	m_pending_control_write = false;
	m_pending_hint = false;
	m_hint_occurred = false;
	m_reg8copy = 0;
	m_reg9copy = 0;
	m_addrmode = 0;
	m_addr = 0;
	m_sega315_5124_compatibility_mode = false;
	m_display_disabled = false;
	m_cram_mask = m_cram_size - 1;
	m_cram_dirty = true;
	m_buffer = 0;
	m_control_write_data_latch = 0;
	m_n_int_state = 1;
	m_n_nmi_state = 1;
	m_n_nmi_in_state = 1;
	m_line_counter = 0;
	m_hcounter = 0;
	m_hcounter_latched = false;
	m_draw_time = DRAW_TIME_SMS;

	std::fill(std::begin(m_current_palette), std::end(m_current_palette), 0);

	set_display_settings();

	/* Clear RAM */
	std::fill(std::begin(m_CRAM), std::end(m_CRAM), 0);
}

//-------------------------------------------------
//  device_add_mconfig - add machine configuration
//-------------------------------------------------

void sega315_5124_device::device_add_mconfig(machine_config &config)
{
	PALETTE(config, m_palette_lut, FUNC(sega315_5124_device::sega315_5124_palette), SEGA315_5124_PALETTE_SIZE);

	SEGAPSG(config, m_snsnd, DERIVED_CLOCK(1, 3)).add_route(ALL_OUTPUTS, *this, 1.0, AUTO_ALLOC_INPUT, 0);
}

void sega315_5246_device::device_add_mconfig(machine_config &config)
{
	sega315_5124_device::device_add_mconfig(config);
	m_palette_lut->set_init(FUNC(sega315_5246_device::sega315_5246_palette));
}

void sega315_5377_device::device_reset()
{
	sega315_5124_device::device_reset();
	m_draw_time = DRAW_TIME_GG;
}

//-------------------------------------------------
//  device_add_mconfig - add machine configuration
//-------------------------------------------------

void sega315_5377_device::device_add_mconfig(machine_config &config)
{
	sega315_5246_device::device_add_mconfig(config);

	m_palette_lut->set_entries(SEGA315_5377_PALETTE_SIZE);
	m_palette_lut->set_init(FUNC(sega315_5377_device::sega315_5377_palette));

	GAMEGEAR(config.replace(), m_snsnd, DERIVED_CLOCK(1, 3));
	m_snsnd->add_route(0, *this, 1.0, AUTO_ALLOC_INPUT, 0);
	m_snsnd->add_route(1, *this, 1.0, AUTO_ALLOC_INPUT, 1);
}

//-------------------------------------------------
//  device_add_mconfig - add machine configuration
//-------------------------------------------------

void sega315_5313_mode4_device::device_add_mconfig(machine_config &config)
{
	sega315_5246_device::device_add_mconfig(config);

	m_palette_lut->set_entries((512 * 3) + 64);
	m_palette_lut->set_init(FUNC(sega315_5313_mode4_device::sega315_5313_palette));
}