summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/fmtowns.cpp
blob: dcb776954ac1519f8bbb916ede15bba3e19d51af (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
// license:BSD-3-Clause
// copyright-holders:Barry Rodewald

/*
 * FM Towns video hardware
 *
 * Resolution: from 320x200 to 768x512
 *
 * Up to two graphics layers
 *
 * Sprites
 *
 * CRTC registers (16-bit):
 *
 *  0:  HSync width 1
 *  1:  HSync width 2
 *  4:  HSync total
 *  5:  VSync period 1
 *  6:  VSync period 2
 *  7:  Equalising pulse accountable time (what?)
 *  8:  VSync total
 *
 *  9:
 *  10: Graphic layer 0 horizontal start/end
 *  11:
 *  12: Graphic layer 1 horizontal start/end
 *
 *  13:
 *  14: Graphic layer 0 vertical start/end
 *  15:
 *  16: Graphic layer 1 vertical start/end
 *
 *  17: Graphic layer 0 frame start address
 *  18: Graphic layer 0 horizontal adjust
 *  19: Graphic layer 0 field indirect address offset
 *  20: Graphic layer 0 line indirect address offset
 *
 *  21-24: As above, but for Graphic layer 1
 *
 *  27: Layer zoom.     bits 0-3 = horizontal zoom layer 0
 *  (0 = x1, 1 = x2,    bits 4-7 = vertical zoom layer 0
 *    2 = x3...)        bits 8-11 = horizontal zoom layer 1
 *                      bits 12-15 = vertical zoom layer 1
 *
 *  28: Control register 0
 *      VSync enable (bit 15) (blank display?)
 *      Scroll type (layer 0 = bit 4, layer 1 = bit 5)
 *          0 = spherical scroll, 1 = cylindrical scroll
 *
 *  29: Control register 1
 *      Dot clock (bits 1 and 0)
 *      0x00 = 28.6363MHz
 *      0x01 = 24.5454MHz
 *      0x02 = 25.175MHz
 *      0x03 = 21.0525MHz (default?)
 *
 *  30: Dummy register
 *
 *  31: Control register 2
 *
 *  Video registers:
 *
 *  0:  Control register:
 *      bit 4 = PMODE - enable 2 layers
 *      bits 2-3 = layer 1 mode
 *      bits 0-1 = layer 0 mode
 *          mode: 1 = 16 colours (PMODE=1 only), 2 = 256 colours (PMODE=0 only),
 *                3 = highcolour (16-bit), 0 = display off
 *
 *  1:  Priority register (bit 0)
 *      bit 0: PR1 - layer priority (0 = layer 0 before)
 *      bit 2: YM - screen brightness (0 = high brightness)
 *      bit 3: YS - 0 = valid.  Used for 256 colour external sync
 *      bits 4-5: Palette select
 *        0 = layer 0, 16 colours, 1 or 3 = 256 colours
 *        2 = layer 1, 16 colours
 *
 *
 *  Sprite registers:
 *
 *  0,1:    Maximum sprite (last one to render?) (10-bit)
 *
 *  1 (bit 7):  Enable sprite display
 *
 *  2,3:    X offset (9-bit)
 *
 *  4,5:    Y offset (9-bit)
 *
 *  6 (bit 4):  VRAM location (0=0x40000,1=0x60000)
 *
 */

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

#include "machine/pic8259.h"
#include "machine/ram.h"
#include "screen.h"


//#define CRTC_REG_DISP 1
//#define SPR_DEBUG 1
#define LAYER_DISABLE 0  // for debugging, allow the Q and W keys to be used for disabling graphic layers
#define LOG_VID 0

//static uint32_t pshift;  // for debugging

void towns_state::towns_crtc_refresh_mode()
{
	rectangle scr(0, m_video.towns_crtc_reg[4] - m_video.towns_crtc_reg[0], 0, m_video.towns_crtc_reg[8] / 2);

	// layer 0
	m_video.towns_crtc_layerscr[0].min_x = m_video.towns_crtc_reg[9] - m_video.towns_crtc_reg[0];
	m_video.towns_crtc_layerscr[0].min_y = (m_video.towns_crtc_reg[13] - m_video.towns_crtc_reg[6]) / 2;
	m_video.towns_crtc_layerscr[0].max_x = m_video.towns_crtc_reg[10] - m_video.towns_crtc_reg[0];
	m_video.towns_crtc_layerscr[0].max_y = ((m_video.towns_crtc_reg[14] - m_video.towns_crtc_reg[6]) / 2) - 1;

	// layer 1
	m_video.towns_crtc_layerscr[1].min_x = m_video.towns_crtc_reg[11] - m_video.towns_crtc_reg[0];
	m_video.towns_crtc_layerscr[1].min_y = (m_video.towns_crtc_reg[15] - m_video.towns_crtc_reg[6]) / 2;
	m_video.towns_crtc_layerscr[1].max_x = m_video.towns_crtc_reg[12] - m_video.towns_crtc_reg[0];
	m_video.towns_crtc_layerscr[1].max_y = ((m_video.towns_crtc_reg[16] - m_video.towns_crtc_reg[6]) / 2) - 1;

	// sanity checks
	if(scr.max_x == 0 || scr.max_y == 0)
		return;
	if(scr.max_x <= scr.min_x || scr.max_y <= scr.min_y)
		return;

	m_screen->configure(scr.max_x+1,scr.max_y+1,scr,HZ_TO_ATTOSECONDS(60));
}

READ8_MEMBER( towns_state::towns_gfx_high_r )
{
	return m_towns_gfxvram[offset];
}

WRITE8_MEMBER( towns_state::towns_gfx_high_w )
{
	u8 mask = m_vram_mask[offset & 3];
	u8 mem = m_towns_gfxvram[offset];
	m_towns_gfxvram[offset] = (mem & ~mask) | (data & mask);
}

READ8_MEMBER( towns_state::towns_gfx_packed_r )
{
	return m_towns_gfxvram[bitswap<19>(offset,2,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,1,0)];
}

WRITE8_MEMBER( towns_state::towns_gfx_packed_w )
{
	u8 mask = m_vram_mask[offset & 3];
	offset = bitswap<19>(offset,2,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,1,0);
	u8 mem = m_towns_gfxvram[offset];
	m_towns_gfxvram[offset] = (mem & ~mask) | (data & mask);
}


READ8_MEMBER( towns_state::towns_gfx_r )
{
	uint8_t ret = 0;

	if(m_towns_mainmem_enable != 0)
		return m_ram->pointer()[offset+0xc0000];

	offset = offset << 2;

	if(m_video.towns_vram_page_sel != 0)
		offset += 0x20000;

	ret = (((m_towns_gfxvram[offset] >> m_video.towns_vram_rplane) << 7) & 0x80)
		| (((m_towns_gfxvram[offset] >> m_video.towns_vram_rplane) << 2) & 0x40)
		| (((m_towns_gfxvram[offset+1] >> m_video.towns_vram_rplane) << 5) & 0x20)
		| (((m_towns_gfxvram[offset+1] >> m_video.towns_vram_rplane)) & 0x10)
		| (((m_towns_gfxvram[offset+2] >> m_video.towns_vram_rplane) << 3) & 0x08)
		| (((m_towns_gfxvram[offset+2] >> m_video.towns_vram_rplane) >> 2) & 0x04)
		| (((m_towns_gfxvram[offset+3] >> m_video.towns_vram_rplane) << 1) & 0x02)
		| (((m_towns_gfxvram[offset+3] >> m_video.towns_vram_rplane) >> 4) & 0x01);

	return ret;
}

WRITE8_MEMBER( towns_state::towns_gfx_w )
{
	if(m_towns_mainmem_enable != 0)
	{
		m_ram->pointer()[offset+0xc0000] = data;
		return;
	}
	offset = offset << 2;
	if(m_video.towns_vram_page_sel != 0)
		offset += 0x20000;
	if(m_video.towns_vram_wplane & 0x08)
	{
		m_towns_gfxvram[offset] &= ~0x88;
		m_towns_gfxvram[offset] |= ((data & 0x80) >> 4) | ((data & 0x40) << 1);
		m_towns_gfxvram[offset + 1] &= ~0x88;
		m_towns_gfxvram[offset + 1] |= ((data & 0x20) >> 2) | ((data & 0x10) << 3);
		m_towns_gfxvram[offset + 2] &= ~0x88;
		m_towns_gfxvram[offset + 2] |= ((data & 0x08)) | ((data & 0x04) << 5);
		m_towns_gfxvram[offset + 3] &= ~0x88;
		m_towns_gfxvram[offset + 3] |= ((data & 0x02) << 2) | ((data & 0x01) << 7);
	}
	if(m_video.towns_vram_wplane & 0x04)
	{
		m_towns_gfxvram[offset] &= ~0x44;
		m_towns_gfxvram[offset] |= ((data & 0x80) >> 5) | ((data & 0x40));
		m_towns_gfxvram[offset + 1] &= ~0x44;
		m_towns_gfxvram[offset + 1] |= ((data & 0x20) >> 3) | ((data & 0x10) << 2);
		m_towns_gfxvram[offset + 2] &= ~0x44;
		m_towns_gfxvram[offset + 2] |= ((data & 0x08) >> 1) | ((data & 0x04) << 4);
		m_towns_gfxvram[offset + 3] &= ~0x44;
		m_towns_gfxvram[offset + 3] |= ((data & 0x02) << 1) | ((data & 0x01) << 6);
	}
	if(m_video.towns_vram_wplane & 0x02)
	{
		m_towns_gfxvram[offset] &= ~0x22;
		m_towns_gfxvram[offset] |= ((data & 0x80) >> 6) | ((data & 0x40) >> 1);
		m_towns_gfxvram[offset + 1] &= ~0x22;
		m_towns_gfxvram[offset + 1] |= ((data & 0x20) >> 4) | ((data & 0x10) << 1);
		m_towns_gfxvram[offset + 2] &= ~0x22;
		m_towns_gfxvram[offset + 2] |= ((data & 0x08) >> 2) | ((data & 0x04) << 3);
		m_towns_gfxvram[offset + 3] &= ~0x22;
		m_towns_gfxvram[offset + 3] |= ((data & 0x02)) | ((data & 0x01) << 5);
	}
	if(m_video.towns_vram_wplane & 0x01)
	{
		m_towns_gfxvram[offset] &= ~0x11;
		m_towns_gfxvram[offset] |= ((data & 0x80) >> 7) | ((data & 0x40) >> 2);
		m_towns_gfxvram[offset + 1] &= ~0x11;
		m_towns_gfxvram[offset + 1] |= ((data & 0x20) >> 5) | ((data & 0x10));
		m_towns_gfxvram[offset + 2] &= ~0x11;
		m_towns_gfxvram[offset + 2] |= ((data & 0x08) >> 3) | ((data & 0x04) << 2);
		m_towns_gfxvram[offset + 3] &= ~0x11;
		m_towns_gfxvram[offset + 3] |= ((data & 0x02) >> 1) | ((data & 0x01) << 4);
	}
}

void towns_state::towns_update_kanji_offset()
{
	// this is a little over the top...
	if(m_video.towns_kanji_code_h < 0x30)
	{
		m_video.towns_kanji_offset = ((m_video.towns_kanji_code_l & 0x1f) << 4)
							| (((m_video.towns_kanji_code_l - 0x20) & 0x20) << 8)
							| (((m_video.towns_kanji_code_l - 0x20) & 0x40) << 6)
							| ((m_video.towns_kanji_code_h & 0x07) << 9);
	}
	else if(m_video.towns_kanji_code_h < 0x70)
	{
		m_video.towns_kanji_offset = (((m_video.towns_kanji_code_l & 0x1f) << 5)
							+ (((m_video.towns_kanji_code_l - 0x20) & 0x60) << 9)
							+ ((m_video.towns_kanji_code_h & 0x0f) << 10)
							+ (((m_video.towns_kanji_code_h - 0x30) & 0x70) * 0xc00)
							+ 0x8000) >> 1;
	}
	else
	{
		m_video.towns_kanji_offset = ((m_video.towns_kanji_code_l & 0x1f) << 4)
							| (((m_video.towns_kanji_code_l - 0x20) & 0x20) << 8)
							| (((m_video.towns_kanji_code_l - 0x20) & 0x40) << 6)
							| ((m_video.towns_kanji_code_h & 0x07) << 9)
							| 0x38000;
	}
}


READ8_MEMBER( towns_state::towns_video_cff80_r )
{
	uint8_t const* const ROM = m_user->base();

	switch(offset)
	{
		case 0x00:  // mix register
			return m_video.towns_crtc_mix;
		case 0x01:  // read/write plane select (bit 0-3 write, bit 6-7 read)
			return ((m_video.towns_vram_rplane << 6) & 0xc0) | m_video.towns_vram_wplane;
		case 0x02:  // display planes (bits 0-2,5), display page select (bit 4)
			return m_video.towns_display_plane | m_video.towns_display_page_sel;
		case 0x03:  // VRAM page select (bit 5)
			if(m_video.towns_vram_page_sel != 0)
				return 0x10;
			else
				return 0x00;
		case 0x06:
			{
				uint8_t ret = 0x10;
				uint16_t const xpos = m_screen->hpos();
				if(xpos < m_video.towns_crtc_layerscr[0].max_x && xpos > m_video.towns_crtc_layerscr[0].min_x)
					ret |= 0x80;
				if(m_video.towns_vblank_flag != 0)
					ret |= 0x04;
				return ret;
			}
		case 0x16:  // Kanji character data
			return ROM[(m_video.towns_kanji_offset << 1) + 0x180000];
		case 0x17:  // Kanji character data
			return ROM[(m_video.towns_kanji_offset++ << 1) + 0x180001];
		case 0x19:  // ANK CG ROM
			if(m_towns_ankcg_enable != 0)
				return 0x01;
			else
				return 0x00;
		default:
			logerror("VGA: read from invalid or unimplemented memory-mapped port %05x\n",0xcff80+offset*4);
	}

	return 0;
}

WRITE8_MEMBER( towns_state::towns_video_cff80_w )
{
	switch(offset)
	{
		case 0x00:  // mix register
			m_video.towns_crtc_mix = data;
			break;
		case 0x01:  // read/write plane select (bit 0-3 write, bit 6-7 read)
			m_video.towns_vram_wplane = data & 0x0f;
			m_video.towns_vram_rplane = (data & 0xc0) >> 6;
			towns_update_video_banks(space);
			//logerror("VGA: VRAM wplane select = 0x%02x\n",towns_vram_wplane);
			break;
		case 0x02:  // display plane (bits 0-2), display page select (bit 4)
			m_video.towns_display_plane = data & 0x27;
			m_video.towns_display_page_sel = data & 0x10;
			break;
		case 0x03:  // VRAM page select (bit 4)
			m_video.towns_vram_page_sel = data & 0x10;
			break;
		case 0x14:  // Kanji offset (high)
			m_video.towns_kanji_code_h = data & 0x7f;
			towns_update_kanji_offset();
			//if(LOG_VID) logerror("VID: Kanji code set (high) = %02x %02x\n",towns_kanji_code_h,towns_kanji_code_l);
			break;
		case 0x15:  // Kanji offset (low)
			m_video.towns_kanji_code_l = data & 0x7f;
			towns_update_kanji_offset();
			//if(LOG_VID) logerror("VID: Kanji code set (low) = %02x %02x\n",towns_kanji_code_h,towns_kanji_code_l);
			break;
		case 0x19:  // ANK CG ROM
			m_towns_ankcg_enable = data & 0x01;
			towns_update_video_banks(space);
			break;
		default:
			logerror("VID: write %08x to invalid or unimplemented memory-mapped port %05x\n",data,0xcff80+offset);
	}
}

READ8_MEMBER( towns_state::towns_video_cff80_mem_r )
{
	if(m_towns_mainmem_enable != 0)
		return m_ram->pointer()[offset+0xcff80];

	return towns_video_cff80_r(space,offset);
}

WRITE8_MEMBER( towns_state::towns_video_cff80_mem_w )
{
	if(m_towns_mainmem_enable != 0)
	{
		m_ram->pointer()[offset+0xcff80] = data;
		return;
	}
	towns_video_cff80_w(space,offset,data);
}

/*
 *  port 0x440-0x443 - CRTC
 *      0x440 = register select
 *      0x442/3 = register data (16-bit)
 *      0x448 = shifter register select
 *      0x44a = shifter register data (8-bit)
 *
 */
READ8_MEMBER(towns_state::towns_video_440_r)
{
	uint8_t ret = 0;
	uint16_t xpos,ypos;

	switch(offset)
	{
		case 0x00:
			return m_video.towns_crtc_sel;
		case 0x02:
//          logerror("CRTC: reading register %i (0x442) [%04x]\n",towns_crtc_sel,towns_crtc_reg[towns_crtc_sel]);
			if(m_video.towns_crtc_sel == 30)
					return 0x00;
			return m_video.towns_crtc_reg[m_video.towns_crtc_sel] & 0x00ff;
		case 0x03:
//          logerror("CRTC: reading register %i (0x443) [%04x]\n",towns_crtc_sel,towns_crtc_reg[towns_crtc_sel]);
			if(m_video.towns_crtc_sel == 30)
			{
				// check video position
				xpos = m_screen->hpos();
				ypos = m_screen->vpos();

				if(xpos < (m_video.towns_crtc_reg[0] & 0xfe))
					ret |= 0x02;
				if(ypos < (m_video.towns_crtc_reg[6] & 0x1f))
					ret |= 0x04;
				if(xpos < m_video.towns_crtc_layerscr[0].max_x && xpos > m_video.towns_crtc_layerscr[0].min_x)
					ret |= 0x10;
				if(xpos < m_video.towns_crtc_layerscr[1].max_x && xpos > m_video.towns_crtc_layerscr[1].min_x)
					ret |= 0x20;
				if(ypos < m_video.towns_crtc_layerscr[0].max_y && ypos > m_video.towns_crtc_layerscr[0].min_y)
					ret |= 0x40;
				if(ypos < m_video.towns_crtc_layerscr[1].max_y && ypos > m_video.towns_crtc_layerscr[1].min_y)
					ret |= 0x80;

				return ret;
			}
			return (m_video.towns_crtc_reg[m_video.towns_crtc_sel] & 0xff00) >> 8;
		case 0x08:
			return m_video.towns_video_sel;
		case 0x0a:
			if(LOG_VID) logerror("Video: reading register %i (0x44a) [%02x]\n",m_video.towns_video_sel,m_video.towns_video_reg[m_video.towns_video_sel]);
			return m_video.towns_video_reg[m_video.towns_video_sel];
		case 0x0c:
			if(m_video.towns_dpmd_flag != 0)
			{
				m_video.towns_dpmd_flag = 0;
				ret |= 0x80;
			}
			ret |= (m_video.towns_sprite_flag ? 0x02 : 0x00);  // Sprite drawing flag
			ret |= m_video.towns_sprite_page & 0x01;
			return ret;
		case 0x10:
			return m_video.towns_sprite_sel;
		case 0x12:
			if(LOG_VID) logerror("SPR: reading register %i (0x452) [%02x]\n",m_video.towns_sprite_sel,m_video.towns_sprite_reg[m_video.towns_sprite_sel]);
			return m_video.towns_sprite_reg[m_video.towns_sprite_sel];
		case 0x18:
			return m_vram_mask_addr;
		case 0x1a:
		case 0x1b:
		{
			int idx = (m_vram_mask_addr << 1) + offset - 0x1a;
			return m_vram_mask[idx];
		}
		//default:
			//if(LOG_VID) logerror("VID: read port %04x\n",offset+0x440);
	}
	return 0x00;
}

WRITE8_MEMBER(towns_state::towns_video_440_w)
{
	switch(offset)
	{
		case 0x00:
			m_video.towns_crtc_sel = data;
			break;
		case 0x02:
//          logerror("CRTC: writing register %i (0x442) [%02x]\n",towns_crtc_sel,data);
			m_video.towns_crtc_reg[m_video.towns_crtc_sel] =
				(m_video.towns_crtc_reg[m_video.towns_crtc_sel] & 0xff00) | data;
			towns_crtc_refresh_mode();
			break;
		case 0x03:
//          logerror("CRTC: writing register %i (0x443) [%02x]\n",towns_crtc_sel,data);
			m_video.towns_crtc_reg[m_video.towns_crtc_sel] =
				(m_video.towns_crtc_reg[m_video.towns_crtc_sel] & 0x00ff) | (data << 8);
			towns_crtc_refresh_mode();
			break;
		case 0x08:
			m_video.towns_video_sel = data & 0x01;
			break;
		case 0x0a:
			logerror("Video: writing register %i (0x44a) [%02x]\n",m_video.towns_video_sel,data);
			m_video.towns_video_reg[m_video.towns_video_sel] = data;
			break;
		case 0x10:
			m_video.towns_sprite_sel = data & 0x07;
			break;
		case 0x12:
			logerror("SPR: writing register %i (0x452) [%02x]\n",m_video.towns_sprite_sel,data);
			m_video.towns_sprite_reg[m_video.towns_sprite_sel] = data;
			break;
		case 0x18:
			m_vram_mask_addr = data & 1;
			break;
		case 0x1a:
		case 0x1b:
		{
			int idx = (m_vram_mask_addr << 1) + offset - 0x1a;
			m_vram_mask[idx] = data;
			break;
		}
		default:
			if(LOG_VID) logerror("VID: wrote 0x%02x to port %04x\n",data,offset+0x440);
	}
}

READ8_MEMBER(towns_state::towns_video_5c8_r)
{
	//if(LOG_VID) logerror("VID: read port %04x\n",offset+0x5c8);
	switch(offset)
	{
		case 0x00:  // 0x5c8 - disable TVRAM?
		if(m_video.towns_tvram_enable != 0)
		{
			m_video.towns_tvram_enable = 0;
			return 0x80;
		}
		else
			return 0x00;
	}
	return 0x00;
}

WRITE8_MEMBER(towns_state::towns_video_5c8_w)
{
	switch(offset)
	{
		case 0x02:  // 0x5ca - VSync clear?
			m_pic_slave->ir3_w(0);
			if(IRQ_LOG) logerror("PIC: IRQ11 (VSync) set low\n");
			//towns_vblank_flag = 0;
			break;
	}
	if(LOG_VID) logerror("VID: wrote 0x%02x to port %04x\n",data,offset+0x5c8);
}

void towns_state::towns_update_palette()
{
	uint8_t entry = m_video.towns_palette_select;
	uint8_t r = m_video.towns_palette_r[entry];
	uint8_t g = m_video.towns_palette_g[entry];
	uint8_t b = m_video.towns_palette_b[entry];
	switch(m_video.towns_video_reg[1] & 0x30)  // Palette select
	{
		case 0x00:
		case 0x20:
			m_palette16[(m_video.towns_video_reg[1] & 0x20) >> 5]->set_pen_color(entry & 0x0f, r, g, b);
			break;
		case 0x10:
		case 0x30:
			m_palette->set_pen_color(entry, r, g, b);
			break;
	}
	// chasehq wants the + 8, the real hardware appears to be less consistent, revisit if other software doesn't like it
	if(!m_screen->vblank())
		m_screen->update_partial(m_screen->vpos() + 8);
}

/* Video/CRTC
 *
 * 0xfd90 - palette colour select
 * 0xfd92/4/6 - BRG value
 * 0xfd98-9f  - Digital palette registers (FMR-50 compatibility)
 */
READ8_MEMBER(towns_state::towns_video_fd90_r)
{
	uint8_t ret = 0;
	uint16_t xpos;
	palette_device* pal;

	if(m_video.towns_video_reg[1] & 0x10)
		pal = m_palette;
	else
		pal = m_palette16[(m_video.towns_video_reg[1] & 0x20) >> 5];
//    if(LOG_VID) logerror("VID: read port %04x\n",offset+0xfd90);
	switch(offset)
	{
		case 0x00:
			return m_video.towns_palette_select;
		case 0x02:
			return pal->pen_color(m_video.towns_palette_select).b();
		case 0x04:
			return pal->pen_color(m_video.towns_palette_select).r();
		case 0x06:
			return pal->pen_color(m_video.towns_palette_select).g();
		case 0x08:
		case 0x09:
		case 0x0a:
		case 0x0b:
		case 0x0c:
		case 0x0d:
		case 0x0e:
		case 0x0f:
			return m_video.towns_degipal[offset-0x08];
		case 0x10:  // "sub status register"
			// check video position
			xpos = m_screen->hpos();

			if(xpos < m_video.towns_crtc_layerscr[0].max_x && xpos > m_video.towns_crtc_layerscr[0].min_x)
				ret |= 0x02;
			if(m_video.towns_vblank_flag)
				ret |= 0x01;
			return ret;
	}
	return 0x00;
}

WRITE8_MEMBER(towns_state::towns_video_fd90_w)
{
	switch(offset)
	{
		case 0x00:
			m_video.towns_palette_select = data;
			break;
		case 0x02:
			m_video.towns_palette_b[m_video.towns_palette_select] = data;
			towns_update_palette();
			break;
		case 0x04:
			m_video.towns_palette_r[m_video.towns_palette_select] = data;
			towns_update_palette();
			break;
		case 0x06:
			m_video.towns_palette_g[m_video.towns_palette_select] = data;
			towns_update_palette();
			break;
		case 0x08:
		case 0x09:
		case 0x0a:
		case 0x0b:
		case 0x0c:
		case 0x0d:
		case 0x0e:
		case 0x0f:
			m_video.towns_degipal[offset-0x08] = data;
			m_video.towns_dpmd_flag = 1;
			break;
		case 0x10:
			m_video.towns_layer_ctrl = data;
			break;
	}
	if(LOG_VID) logerror("VID: wrote 0x%02x to port %04x\n",data,offset+0xfd90);
}

READ8_MEMBER(towns_state::towns_video_ff81_r)
{
	return ((m_video.towns_vram_rplane << 6) & 0xc0) | m_video.towns_vram_wplane;
}

WRITE8_MEMBER(towns_state::towns_video_ff81_w)
{
	m_video.towns_vram_wplane = data & 0x0f;
	m_video.towns_vram_rplane = (data & 0xc0) >> 6;
	towns_update_video_banks(space);
	logerror("VID: VRAM wplane select (I/O) = 0x%02x\n",m_video.towns_vram_wplane);
}

READ8_MEMBER(towns_state::towns_video_unknown_r)
{
	return 0x00;
}

/*
 *  Sprite RAM, low memory
 *  Writing to 0xc8xxx or 0xcaxxx activates TVRAM
 *  Writing to I/O port 0x5c8 disables TVRAM
 *     (bit 7 returns high if TVRAM was previously active)
 *
 *  In TVRAM mode:
 *    0xc8000-0xc8fff: ASCII text (2 bytes each: ISO646 code, then attribute)
 *    0xca000-0xcafff: JIS code
 */
READ8_MEMBER(towns_state::towns_spriteram_low_r)
{
	uint8_t* RAM = m_ram->pointer();
	uint8_t* ROM = m_user->base();

	if(offset < 0x1000)
	{  // 0xc8000-0xc8fff
		if(m_towns_mainmem_enable == 0)
		{
//          if(towns_tvram_enable == 0)
//              return towns_sprram[offset];
//          else
				return m_towns_txtvram[offset];
		}
		else
			return RAM[offset + 0xc8000];
	}
	if(offset >= 0x1000 && offset < 0x2000)
	{  // 0xc9000-0xc9fff
		return RAM[offset + 0xc9000];
	}
	if(offset >= 0x2000 && offset < 0x3000)
	{  // 0xca000-0xcafff
		if(m_towns_mainmem_enable == 0)
		{
			if(m_towns_ankcg_enable != 0 && offset < 0x2800)
				return ROM[0x180000 + 0x3d000 + (offset-0x2000)];
//          if(towns_tvram_enable == 0)
//              return m_towns_sprram[offset];
//          else
				return m_towns_txtvram[offset];
		}
		else
			return RAM[offset + 0xca000];
	}
	return 0x00;
}

WRITE8_MEMBER(towns_state::towns_spriteram_low_w)
{
	uint8_t* RAM = m_ram->pointer();

	if(offset < 0x1000)
	{  // 0xc8000-0xc8fff
		m_video.towns_tvram_enable = 1;
		if(m_towns_mainmem_enable == 0)
			m_towns_txtvram[offset] = data;
		else
			RAM[offset + 0xc8000] = data;
	}
	if(offset >= 0x1000 && offset < 0x2000)
	{
		RAM[offset + 0xc9000] = data;
	}
	if(offset >= 0x2000 && offset < 0x3000)
	{  // 0xca000-0xcafff
		m_video.towns_tvram_enable = 1;
		if(m_towns_mainmem_enable == 0)
			m_towns_txtvram[offset] = data;
		else
			RAM[offset + 0xca000] = data;
	}
}

READ8_MEMBER( towns_state::towns_spriteram_r )
{
	return m_towns_txtvram[offset];
}

WRITE8_MEMBER( towns_state::towns_spriteram_w )
{
	m_towns_txtvram[offset] = data;
}

/*
 *  Sprites
 *
 *  Max. 1024, 16x16, 16 colours per sprite
 *  128kB Sprite RAM (8kB attributes, 120kB pattern/colour data)
 *  Sprites are rendered directly to VRAM layer 1 (VRAM offset 0x40000 or 0x60000)
 *
 *  Sprite RAM format:
 *      4 words per sprite
 *      +0: X position (10-bit)
 *      +2: Y position (10-bit)
 *      +4: Sprite Attribute
 *          bit 15: enforce offsets (regs 2-5)
 *          bit 12,13,14: flip / rotate sprite
 *              When the rotate bit (14) is set, the X and Y coordinates are swapped when rendering the sprite to VRAM.
 *              By combining it with the flip bits, the sprite can be rotated in 90 degree increments, both mirrored and unmirrored.
 *          bits 10,11: half-size
 *          bits 9-0: Sprite RAM offset containing sprite pattern
 *      +6: Sprite Colour
 *          bit 15: use colour data in located in sprite RAM offset in bits 11-0 (x32)
 */
void towns_state::render_sprite_4(uint32_t poffset, uint32_t coffset, uint16_t x, uint16_t y, bool xflip, bool yflip, bool xhalfsize, bool yhalfsize, bool rotation, const rectangle* rect)
{
	uint16_t xpos,ypos;
	uint16_t col,pixel;
	uint32_t vbase = m_video.towns_sprite_page ? 0x20000 : 0, voffset;
	uint16_t xstart,xend,ystart,yend;
	int linesize = m_video.towns_crtc_reg[24] * 4;
	int xdir,ydir;
	int width = (m_video.towns_crtc_reg[12] - m_video.towns_crtc_reg[11]) / (((m_video.towns_crtc_reg[27] & 0x0f00) >> 8)+1);
	int height = (m_video.towns_crtc_reg[16] - m_video.towns_crtc_reg[15]) / (((m_video.towns_crtc_reg[27] & 0xf000) >> 12)+2);

	if (rotation)
	{
		std::swap (x,y);
		std::swap (xflip,yflip);
		std::swap (width,height);
	}

	if(xflip)
	{
		if (xhalfsize)
		{
			xstart = x+6;
			xdir = -1;
		}
		else
		{
			xstart = x+14;
			xdir = -2;
		}
		xend = x-2;
	}
	else
	{
		xstart = x+1;
		if (xhalfsize)
		{
			xend = x+9;
			xdir = 1;
		}
		else
		{
			xend = x+17;
			xdir = 2;
		}
	}
	if(yflip)
	{
		if (yhalfsize)
			ystart = y+7;
		else
			ystart = y+15;
		yend = y-1;
		ydir = -1;
	}
	else
	{
		ystart = y;
		if (yhalfsize)
			yend = y+8;
		else
			yend = y+16;
		ydir = 1;
	}
	xstart &= 0x1ff;
	xend &= 0x1ff;
	ystart &= 0x1ff;
	yend &= 0x1ff;
	poffset &= 0x1ffff;

	for(ypos=ystart;ypos!=yend;ypos+=ydir,ypos&=0x1ff)
	{
		for(xpos=xstart;xpos!=xend;xpos+=xdir,xpos&=0x1ff)
		{

			voffset = 0;
			pixel = (m_towns_txtvram[poffset] & 0xf0) >> 4;
			col = m_towns_txtvram[coffset+(pixel*2)] | (m_towns_txtvram[coffset+(pixel*2)+1] << 8);
			if (rotation)
			{
				voffset += linesize * (xpos & 0x1ff);  // scanline size in bytes * y pos
				voffset += (ypos & 0x1ff) * 2;
			}
			else
			{
				voffset += linesize * (ypos & 0x1ff);  // scanline size in bytes * y pos
				voffset += (xpos & 0x1ff) * 2;
			}
			if(voffset < 0x20000 && xpos < width && ypos < height && pixel != 0 && voffset > linesize)
			{
				m_towns_gfxvram[0x40000+voffset+vbase+1] = (col & 0xff00) >> 8;
				m_towns_gfxvram[0x40000+voffset+vbase] = col & 0x00ff;
			}

			if (!xhalfsize)
			{
				if(xflip)
					if (rotation)
						voffset+=(m_video.towns_crtc_reg[24] * 4);
					else
						voffset+=2;
				else
					if (rotation)
						voffset-=(m_video.towns_crtc_reg[24] * 4);
					else
						voffset-=2;

				pixel = m_towns_txtvram[poffset] & 0x0f;
				col = m_towns_txtvram[coffset+(pixel*2)] | (m_towns_txtvram[coffset+(pixel*2)+1] << 8);
				if(voffset < 0x20000 && xpos < width && ypos < height && pixel != 0 && voffset > linesize)
				{
					m_towns_gfxvram[0x40000+voffset+vbase+1] = (col & 0xff00) >> 8;
					m_towns_gfxvram[0x40000+voffset+vbase] = col & 0x00ff;
				}
			}

			poffset++;
			poffset &= 0x1ffff;
		}
		if (yhalfsize)
		{
			poffset+=8;
		}
	}
}

void towns_state::render_sprite_16(uint32_t poffset, uint16_t x, uint16_t y, bool xflip, bool yflip, bool xhalfsize, bool yhalfsize, bool rotation, const rectangle* rect)
{
	uint16_t xpos,ypos;
	uint16_t col;
	uint32_t vbase = m_video.towns_sprite_page ? 0x20000 : 0, voffset;
	uint16_t xstart,ystart,xend,yend;
	int linesize = m_video.towns_crtc_reg[24] * 4;
	int xdir,ydir;
	int width = (m_video.towns_crtc_reg[12] - m_video.towns_crtc_reg[11]) / (((m_video.towns_crtc_reg[27] & 0x0f00) >> 8)+1);
	int height = (m_video.towns_crtc_reg[16] - m_video.towns_crtc_reg[15]) / (((m_video.towns_crtc_reg[27] & 0xf000) >> 12)+2);

	if (rotation)
	{
		std::swap (x,y);
		std::swap (xflip,yflip);
		std::swap (width,height);
	}

	if(xflip)
	{
		if (xhalfsize)
			xstart = x+8;
		else
			xstart = x+16;
		xend = x;
		xdir = -1;
	}
	else
	{
		xstart = x+1;
		if (xhalfsize)
			xend = x+9;
		else
			xend = x+17;
		xdir = 1;
	}
	if(yflip)
	{
		if (yhalfsize)
			ystart = y+7;
		else
			ystart = y+15;
		yend = y-1;
		ydir = -1;
	}
	else
	{
		ystart = y;
		if (yhalfsize)
			yend = y+8;
		else
			yend = y+16;
		ydir = 1;
	}
	xstart &= 0x1ff;
	xend &= 0x1ff;
	ystart &= 0x1ff;
	yend &= 0x1ff;
	poffset &= 0x1ffff;

	for(ypos=ystart;ypos!=yend;ypos+=ydir,ypos&=0x1ff)
	{
		for(xpos=xstart;xpos!=xend;xpos+=xdir,xpos&=0x1ff)
		{
			voffset = 0;
			col = m_towns_txtvram[poffset] | (m_towns_txtvram[poffset+1] << 8);
			if (rotation)
			{
				voffset += linesize * (xpos & 0x1ff);  // scanline size in bytes * y pos
				voffset += (ypos & 0x1ff) * 2;
			}
			else
			{
				voffset += linesize * (ypos & 0x1ff);  // scanline size in bytes * y pos
				voffset += (xpos & 0x1ff) * 2;
			}
			if(voffset < 0x20000 && xpos < width && ypos < height && col< 0x8000 && voffset > linesize)
			{
				m_towns_gfxvram[0x40000+vbase+voffset+1] = (col & 0xff00) >> 8;
				m_towns_gfxvram[0x40000+vbase+voffset] = col & 0x00ff;
			}
			if (xhalfsize)
				poffset+=4;
			else
				poffset+=2;
			poffset &= 0x1ffff;
		}

		if (yhalfsize)
			poffset+=16;
	}
}

void towns_state::draw_sprites(const rectangle* rect)
{
	uint16_t sprite_limit = (m_video.towns_sprite_reg[0] | (m_video.towns_sprite_reg[1] << 8)) & 0x3ff;
	int n;
	uint16_t x,y,attr,colour;
	uint16_t xoff = (m_video.towns_sprite_reg[2] | (m_video.towns_sprite_reg[3] << 8)) & 0x1ff;
	uint16_t yoff = (m_video.towns_sprite_reg[4] | (m_video.towns_sprite_reg[5] << 8)) & 0x1ff;
	uint32_t poffset,coffset;
	int linesize = m_video.towns_crtc_reg[24] * 4;
	bool xflip, yflip, xhalfsize, yhalfsize, rotation;

	if(!(m_video.towns_sprite_reg[1] & 0x80))
		return;

	// TODO: I'm not confident about this but based on the behavior of aburner and rbisland, it's probably in the ballpark
	// aburner writes the backgound color from 0 to 0x400 in both pages while rbisland from 0 to 0x800 (What's the difference?)
	// it's only written when the color changes so the sprite engine has to be prevented from writing there
	uint8_t *vram;
	if(m_video.towns_sprite_page == 0)
		vram = m_towns_gfxvram.get() + 0x40000;
	else
		vram = m_towns_gfxvram.get() + 0x60000;

	for(int i = linesize; i < 0x20000; i += linesize)
		memcpy(vram + i, vram, linesize);

	for(n=sprite_limit;n<1024;n++)
	{
		x = m_towns_txtvram[8*n] | (m_towns_txtvram[8*n+1] << 8);
		y = m_towns_txtvram[8*n+2] | (m_towns_txtvram[8*n+3] << 8);
		attr = m_towns_txtvram[8*n+4] | (m_towns_txtvram[8*n+5] << 8);
		colour = m_towns_txtvram[8*n+6] | (m_towns_txtvram[8*n+7] << 8);
		xflip = (attr & 0x2000) >> 13;
		yflip = (attr & 0x1000) >> 12;
		rotation = (attr & 0x4000) >> 14;
		xhalfsize = (attr & 0x400) >> 10;
		yhalfsize = (attr & 0x800) >> 11;

		if(attr & 0x8000)
		{
			x += xoff;
			y += yoff;
		}
		x &= 0x1ff;
		y &= 0x1ff;

		if(colour & 0x8000)
		{
			poffset = (attr & 0x3ff) << 7;
			coffset = (colour & 0xfff) << 5;
#ifdef SPR_DEBUG
			printf("Sprite4 #%i, X %i Y %i Attr %04x Col %04x Poff %08x Coff %08x\n",
				n,x,y,attr,colour,poffset,coffset);
#endif
			if(!(colour & 0x2000))
				render_sprite_4((poffset)&0x1ffff,coffset,x,y,xflip,yflip,xhalfsize,yhalfsize,rotation,rect);
		}
		else
		{
			poffset = (attr & 0x3ff) << 7;
#ifdef SPR_DEBUG
			printf("Sprite16 #%i, X %i Y %i Attr %04x Col %04x Poff %08x",
				n,x,y,attr,colour,poffset);
#endif
			if(!(colour & 0x2000))
				render_sprite_16((poffset)&0x1ffff,x,y,xflip,yflip,xhalfsize,yhalfsize,rotation,rect);
		}
	}

	if(m_video.towns_sprite_page == 0)  // flip VRAM page
		m_video.towns_sprite_page = 1;
	else
		m_video.towns_sprite_page = 0;

	m_video.towns_sprite_flag = 1;  // we are now drawing
	m_video.sprite_timer->adjust(m_maincpu->cycles_to_attotime(128 * (1025-sprite_limit)));
}

void towns_state::towns_crtc_draw_scan_layer_hicolour(bitmap_rgb32 &bitmap,const rectangle* rect,int layer,int line,int scanline)
{
	uint32_t off = 0;
	int x;
	uint16_t colour;
	int hzoom = 1;
	int linesize;
	uint32_t scroll;
	int pixel;
	int page = 0;
	bool sphscroll = !(m_video.towns_crtc_reg[28] & (layer ? 0x20 : 0x10));

	if(m_video.towns_video_reg[0] & 0x10)
	{
		if(layer == 0)
			linesize = m_video.towns_crtc_reg[20] * 4;
		else
			linesize = m_video.towns_crtc_reg[24] * 4;
	}
	else
		linesize = m_video.towns_crtc_reg[20] * 8;

	if(m_video.towns_display_page_sel != 0)
	{
		off = 0x20000;
		page = 1;
	}

//  if((layer == 1) && (m_video.towns_sprite_reg[1] & 0x80) && (m_video.towns_sprite_page == 1))
//      off = 0x20000;

	if(layer != 0)
	{
		if(!(m_video.towns_video_reg[0] & 0x10))
			return;
		if(!sphscroll)
			off += (m_video.towns_crtc_reg[21]) << 2;  // initial offset
		else
		{
			scroll = ((m_video.towns_crtc_reg[21] & 0xfc00) << 2) | (((m_video.towns_crtc_reg[21] & 0x3ff) << 2));
			off += scroll;
		}
		hzoom = ((m_video.towns_crtc_reg[27] & 0x0f00) >> 8) + 1;
		off += (m_video.towns_crtc_reg[11] - m_video.towns_crtc_reg[22]) * (2 >> (hzoom - 1));
	}
	else
	{
		if(!sphscroll)
			off += (m_video.towns_crtc_reg[17]) << 2;  // initial offset
		else
		{
			scroll = ((m_video.towns_crtc_reg[17] & 0xfc00) << 2) | (((m_video.towns_crtc_reg[17] & 0x3ff) << 2));
			off += scroll;
		}
		hzoom = (m_video.towns_crtc_reg[27] & 0x000f) + 1;
		off += (m_video.towns_crtc_reg[9] - m_video.towns_crtc_reg[18]) * (2 >> (hzoom - 1));
	}

	off += line * linesize;
	off &= ~0x01;

	for(x=rect->min_x;x<rect->max_x;x+=hzoom)
	{
		if(m_video.towns_video_reg[0] & 0x10)
			off &= 0x3ffff;  // 2 layers
		else
			off &= 0x7ffff;  // 1 layer
		colour = (m_towns_gfxvram[off+(layer*0x40000)+1] << 8) | m_towns_gfxvram[off+(layer*0x40000)];
		if(colour < 0x8000 || !(m_video.towns_video_reg[0] & 0x10))
		{
			for (pixel = 0; pixel < hzoom; pixel++)
				bitmap.pix32(scanline, x+pixel) =
					((colour & 0x001f) << 3)
					| ((colour & 0x7c00) << 1)
					| ((colour & 0x03e0) << 14);
		}
		off+=2;
		if ((off - (page * 0x20000)) % linesize == 0 && sphscroll)
			off -= linesize;
	}
}

void towns_state::towns_crtc_draw_scan_layer_256(bitmap_rgb32 &bitmap,const rectangle* rect,int line,int scanline)
{
	int off = 0;
	int x;
	uint8_t colour;
	int hzoom = 1;
	int linesize;
	uint32_t scroll;
	int pixel;
	int page = 0;
	bool sphscroll = !(m_video.towns_crtc_reg[28] & 0x10);

	if(m_video.towns_display_page_sel != 0)
	{
		off = 0x20000;
		page = 1;
	}

	linesize = m_video.towns_crtc_reg[20] * 4;

	if(!sphscroll)
		off += m_video.towns_crtc_reg[17] << 2;  // initial offset
	else
	{
		scroll = ((m_video.towns_crtc_reg[17] & 0xfc00) << 2) | (((m_video.towns_crtc_reg[17] & 0x3ff) << 2));
		off += scroll;
	}
	hzoom = (m_video.towns_crtc_reg[27] & 0x000f) + 1;
	int subpix = (m_video.towns_crtc_reg[9] - m_video.towns_crtc_reg[18]) / hzoom;

	off += line * linesize;
	off += (subpix >> 1) & ~3;
	subpix = subpix & 7;

	for(x=rect->min_x;x<rect->max_x;x+=hzoom)
	{
		off &= 0x3ffff;
		colour = m_towns_gfxvram[off+(subpix >= 4 ? (subpix & 3)+0x40000 : subpix)];
		for (pixel = 0; pixel < hzoom; pixel++)
			bitmap.pix32(scanline, x+pixel) = m_palette->pen(colour);
		subpix++;
		if(subpix == 8)
		{
			off += 4;
			subpix = 0;
		}
		if ((off - (page * 0x20000)) % linesize == 0 && subpix == 0 && sphscroll)
			off -= linesize;
	}
}

void towns_state::towns_crtc_draw_scan_layer_16(bitmap_rgb32 &bitmap,const rectangle* rect,int layer,int line,int scanline)
{
	int off = 0;
	int x;
	uint8_t colour;
	int hzoom = 1;
	int linesize;
	uint32_t scroll;
	int pixel;
	int page = 0;
	palette_device* pal = m_palette16[layer];
	bool sphscroll = !(m_video.towns_crtc_reg[28] & (layer ? 0x20 : 0x10));

	if(m_video.towns_display_page_sel != 0)
	{
		off = 0x20000;
		page = 1;
	}

	bool bottom_layer = (m_video.towns_video_reg[1] & 0x01) != layer;

//  if((layer == 1) && (m_video.towns_sprite_reg[1] & 0x80) && (m_video.towns_sprite_page == 1))
//      off = 0x20000;

	if(layer == 0)
		linesize = m_video.towns_crtc_reg[20] * 4;
	else
		linesize = m_video.towns_crtc_reg[24] * 4;

	if(layer != 0)
	{
		if(!(m_video.towns_video_reg[0] & 0x10))
			return;
		if(!sphscroll)
			off += m_video.towns_crtc_reg[21] << 2;  // initial offset
		else
		{
			scroll = ((m_video.towns_crtc_reg[21] & 0xfc00)<<2) | (((m_video.towns_crtc_reg[21] & 0x3ff)<<2));
			off += scroll;
		}
		hzoom = ((m_video.towns_crtc_reg[27] & 0x0f00) >> 8) + 1;
		off += (m_video.towns_crtc_reg[11] - m_video.towns_crtc_reg[22]) / (hzoom * 2);
	}
	else
	{
		if(!sphscroll)
			off += m_video.towns_crtc_reg[17] << 2;  // initial offset
		else
		{
			scroll = ((m_video.towns_crtc_reg[17] & 0xfc00)<<2) | (((m_video.towns_crtc_reg[17] & 0x3ff)<<2));
			off += scroll;
		}
		hzoom = (m_video.towns_crtc_reg[27] & 0x000f) + 1;
		off += (m_video.towns_crtc_reg[9] - m_video.towns_crtc_reg[18]) / (hzoom * 2);
	}

	off += line * linesize;

	for(x=rect->min_x;x<rect->max_x;x+=hzoom*2)
	{
		if(m_video.towns_video_reg[0] & 0x10)
			off &= 0x3ffff;  // 2 layers
		else
			off &= 0x7ffff;  // 1 layer
		colour = m_towns_gfxvram[off+(layer*0x40000)] >> 4;
		if(colour != 0 || bottom_layer)
		{
			for (pixel = 0; pixel < hzoom; pixel++)
				bitmap.pix32(scanline, x+hzoom+pixel) = pal->pen(colour);
		}
		colour = m_towns_gfxvram[off+(layer*0x40000)] & 0x0f;
		if(colour != 0 || bottom_layer)
		{
			for (pixel = 0; pixel < hzoom; pixel++)
				bitmap.pix32(scanline, x+pixel) = pal->pen(colour);
		}
		off++;
		if ((off - (page * 0x20000)) % linesize == 0 && sphscroll)
			off -= linesize;
	}
}

void towns_state::towns_crtc_draw_layer(bitmap_rgb32 &bitmap,const rectangle* rect,int layer)
{
	int line;
	int scanline;
	int bottom;
	int top;
	uint8_t zoom;
	uint8_t count;

	if(layer == 0)
	{
		scanline = rect->min_y;
		top = (scanline - m_video.towns_crtc_layerscr[0].min_y);
		bottom = (rect->max_y - rect->min_y) + top;
		zoom = ((m_video.towns_crtc_reg[27] & 0x00f0) >> 4) + 1;
		count = top % zoom;
		bottom /= zoom;
		top /= zoom;
		switch(m_video.towns_video_reg[0] & 0x03)
		{
			case 0x01:
				for(line=top;line<=bottom;line++)
				{
					do
					{
						towns_crtc_draw_scan_layer_16(bitmap,rect,layer,line,scanline);
						scanline++;
						count++;
					} while(count < zoom);
					count = 0;
				}
				break;
			case 0x02:
				for(line=top;line<=bottom;line++)
				{
					do
					{
						towns_crtc_draw_scan_layer_256(bitmap,rect,line,scanline);
						scanline++;
						count++;
					} while(count < zoom);
					count = 0;
				}
				break;
			case 0x03:
				for(line=top;line<=bottom;line++)
				{
					do
					{
						towns_crtc_draw_scan_layer_hicolour(bitmap,rect,layer,line,scanline);
						scanline++;
						count++;
					} while(count < zoom);
					count = 0;
				}
				break;
		}
	}
	else
	{
		scanline = rect->min_y;
		top = (scanline - m_video.towns_crtc_layerscr[1].min_y);
		bottom = (rect->max_y - rect->min_y) + top;
		zoom = ((m_video.towns_crtc_reg[27] & 0xf000) >> 12) + 1;
		count = top % zoom;
		bottom /= zoom;
		top /= zoom;
		switch(m_video.towns_video_reg[0] & 0x0c)
		{
			case 0x04:
				for(line=top;line<=bottom;line++)
				{
					do
					{
						towns_crtc_draw_scan_layer_16(bitmap,rect,layer,line,scanline);
						scanline++;
						count++;
					} while(count < zoom);
					count = 0;
				}
				break;
			case 0x0c:
				for(line=top;line<=bottom;line++)
				{
					do
					{
						towns_crtc_draw_scan_layer_hicolour(bitmap,rect,layer,line,scanline);
						scanline++;
						count++;
					} while(count < zoom);
					count = 0;
				}
				break;
		}
	}
}

void towns_state::render_text_char(uint8_t x, uint8_t y, uint8_t ascii, uint16_t jis, uint8_t attr)
{
	uint32_t rom_addr;
	uint32_t vram_addr;
	uint16_t linesize = m_video.towns_crtc_reg[24] * 4;
	uint8_t code_h,code_l;
	uint8_t colour;
	uint8_t data;
	uint8_t temp;
	uint8_t* font_rom = m_user->base();
	int a,b;

	// all characters are 16 pixels high
	vram_addr = (x * 4) + (y * (linesize * 16));

	if((attr & 0xc0) == 0)
		rom_addr = 0x3d800 + (ascii * 128);
	else
	{
		code_h = (jis & 0xff00) >> 8;
		code_l = jis & 0x00ff;
		if(code_h < 0x30)
		{
			rom_addr = ((code_l & 0x1f) << 4)
								| (((code_l - 0x20) & 0x20) << 8)
								| (((code_l - 0x20) & 0x40) << 6)
								| ((code_h & 0x07) << 9);
		}
		else if(code_h < 0x70)
		{
			rom_addr = ((code_l & 0x1f) << 4)
								+ (((code_l - 0x20) & 0x60) << 8)
								+ ((code_h & 0x0f) << 9)
								+ (((code_h - 0x30) & 0x70) * 0xc00)
								+ 0x8000;
		}
		else
		{
			rom_addr = ((code_l & 0x1f) << 4)
								| (((code_l - 0x20) & 0x20) << 8)
								| (((code_l - 0x20) & 0x40) << 6)
								| ((code_h & 0x07) << 9)
								| 0x38000;
		}
	}
	colour = attr & 0x07;
	if(attr & 0x20)
		colour |= 0x08;

	for(a=0;a<16;a++)  // for each scanline
	{
		if((attr & 0xc0) == 0)
			data = font_rom[0x180000 + rom_addr + a];
		else
		{
			if((attr & 0xc0) == 0x80)
				data = font_rom[0x180000 + rom_addr + (a*2)];
			else
				data = font_rom[0x180000 + rom_addr + (a*2) + 1];
		}

		if(attr & 0x08)
			data = ~data;  // inverse

		// and finally, put the data in VRAM
		for(b=0;b<8;b+=2)
		{
			temp = 0;
			if(data & (1<<b))
				temp |= ((colour & 0x0f) << 4);
			if(data & (1<<(b+1)))
				temp |= (colour & 0x0f);
			//m_towns_gfxvram[0x40000+vram_addr+(b/2)] = temp;
		}

		vram_addr += linesize;
		vram_addr &= 0x3ffff;
	}
}

void towns_state::draw_text_layer()
{
/*
 *  Text format
 *  2 bytes per character at both 0xc8000 and 0xca000
 *  0xc8xxx: Byte 1: ASCII character
 *           Byte 2: Attributes
 *             bits 2-0: GRB (or is it BRG?)
 *             bit 3: Inverse
 *             bit 4: Blink
 *             bit 5: high brightness
 *             bits 7-6: Kanji high/low
 *
 *  If either bits 6 or 7 are high, then a fullwidth Kanji character is displayed
 *  at this location.  The character displayed is represented by a 2-byte
 *  JIS code at the same offset at 0xca000.
 *
 *  The video hardware renders text to VRAM layer 1, there is no separate text layer
 */
	int x,y,c = 0;

	for(y=0;y<40;y++)
	{
		for(x=0;x<80;x++)
		{
			render_text_char(x,y,m_towns_txtvram[c],((m_towns_txtvram[c+0x2000] << 8)|(m_towns_txtvram[c+0x2001])),m_towns_txtvram[c+1]);
			c+=2;
		}
	}
}

TIMER_CALLBACK_MEMBER(towns_state::towns_sprite_done)
{
	// sprite drawing is complete, lower flag
	m_video.towns_sprite_flag = 0;
	if(m_video.towns_sprite_page != 0)
		m_video.towns_crtc_reg[21] |= 0x8000;
	else
		m_video.towns_crtc_reg[21] &= ~0x8000;
}

TIMER_CALLBACK_MEMBER(towns_state::towns_vblank_end)
{
	// here we'll clear the vsync signal, I presume it goes low on it's own eventually
	m_pic_slave->ir3_w(0);  // IRQ11 = VSync
	if(IRQ_LOG) logerror("PIC: IRQ11 (VSync) set low\n");
	m_video.towns_vblank_flag = 0;
}

INTERRUPT_GEN_MEMBER(towns_state::towns_vsync_irq)
{
	m_pic_slave->ir3_w(1);  // IRQ11 = VSync
	if(IRQ_LOG) logerror("PIC: IRQ11 (VSync) set high\n");
	m_video.towns_vblank_flag = 1;
	machine().scheduler().timer_set(m_screen->time_until_vblank_end(), timer_expired_delegate(FUNC(towns_state::towns_vblank_end),this), 0, (void*)m_pic_slave);
	if(m_video.towns_tvram_enable)
		draw_text_layer();
	if(m_video.towns_sprite_reg[1] & 0x80)
		draw_sprites(&m_video.towns_crtc_layerscr[1]);
}

void towns_state::video_start()
{
	m_video.towns_vram_wplane = 0x00;
	m_video.towns_sprite_page = 0;
	m_video.sprite_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(towns_state::towns_sprite_done),this));
}

uint32_t towns_state::screen_update(screen_device &screen, bitmap_rgb32 &bitmap, const rectangle &cliprect)
{
	bool layer1_en = true, layer2_en = true;
	bitmap.fill(0x00000000, cliprect);

	if(LAYER_DISABLE)
	{
		if(machine().input().code_pressed(KEYCODE_Q))
			layer1_en = false;
		if(machine().input().code_pressed(KEYCODE_W))
			layer2_en = false;
	}

	rectangle cliplayer0 = m_video.towns_crtc_layerscr[0];
	cliplayer0 &= cliprect;
	rectangle cliplayer1 = m_video.towns_crtc_layerscr[1];
	cliplayer1 &= cliprect;

	if(!(m_video.towns_video_reg[1] & 0x01))
	{
		if((m_video.towns_layer_ctrl & 0x03) != 0 && layer1_en)
			towns_crtc_draw_layer(bitmap,&cliplayer1,1);
		if((m_video.towns_layer_ctrl & 0x0c) != 0 && layer2_en)
			towns_crtc_draw_layer(bitmap,&cliplayer0,0);
	}
	else
	{
		if((m_video.towns_layer_ctrl & 0x0c) != 0 && layer1_en)
			towns_crtc_draw_layer(bitmap,&cliplayer0,0);
		if((m_video.towns_layer_ctrl & 0x03) != 0 && layer2_en)
			towns_crtc_draw_layer(bitmap,&cliplayer1,1);
	}

#if 0
#ifdef SPR_DEBUG
	if(machine().input().code_pressed(KEYCODE_O))
		pshift+=0x80;
	if(machine().input().code_pressed(KEYCODE_I))
		pshift-=0x80;
	popmessage("Pixel shift = %08x",pshift);
#endif
#endif

#ifdef CRTC_REG_DISP
	popmessage("CRTC: %i %i %i %i %i %i %i %i %i\n%i %i %i %i | %i %i %i %i\n%04x %i %i %i | %04x %i %i %i\nZOOM: %04x\nVideo: %02x %02x\nText=%i Spr=%02x\nReg28=%04x",
		m_video.towns_crtc_reg[0],m_video.towns_crtc_reg[1],m_video.towns_crtc_reg[2],m_video.towns_crtc_reg[3],
		m_video.towns_crtc_reg[4],m_video.towns_crtc_reg[5],m_video.towns_crtc_reg[6],m_video.towns_crtc_reg[7],
		m_video.towns_crtc_reg[8],
		m_video.towns_crtc_reg[9],m_video.towns_crtc_reg[10],m_video.towns_crtc_reg[11],m_video.towns_crtc_reg[12],
		m_video.towns_crtc_reg[13],m_video.towns_crtc_reg[14],m_video.towns_crtc_reg[15],m_video.towns_crtc_reg[16],
		m_video.towns_crtc_reg[17],m_video.towns_crtc_reg[18],m_video.towns_crtc_reg[19],m_video.towns_crtc_reg[20],
		m_video.towns_crtc_reg[21],m_video.towns_crtc_reg[22],m_video.towns_crtc_reg[23],m_video.towns_crtc_reg[24],
		m_video.towns_crtc_reg[27],m_video.towns_video_reg[0],m_video.towns_video_reg[1],m_video.towns_tvram_enable,m_video.towns_sprite_reg[1] & 0x80,
		m_video.towns_crtc_reg[28]);
#endif

	return 0;
}