summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/taito_f2.c
blob: 648dc34cc4d81d6f98eb95231c47b4f73dee1695 (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
#include "emu.h"
#include "video/taitoic.h"
#include "includes/taito_f2.h"

/************************************************************
                      SPRITE BANKING

  Four sprite banking methods are used for games with more
  than $2000 sprite tiles, because the sprite ram only has
  13 bits available for tile numbers.

   0 = standard (only a limited selection of sprites are
                  available for display at a given time)
   1 = use sprite extension area lo bytes for hi 6 bits
   2 = use sprite extension area hi bytes
   3 = use sprite extension area lo bytes as hi bytes
            (sprite extension areas mean all sprite
             tiles are always accessible)
************************************************************/

enum
{
	FOOTCHMP = 1
};


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

static void taitof2_core_vh_start (running_machine &machine, int sprite_type, int hide, int flip_hide )
{
	int i;
	taitof2_state *state = machine.driver_data<taitof2_state>();
	state->m_sprite_type = sprite_type;
	state->m_hide_pixels = hide;
	state->m_flip_hide_pixels = flip_hide;

	state->m_spriteram_delayed = auto_alloc_array(machine, UINT16, state->m_spriteram_size / 2);
	state->m_spriteram_buffered = auto_alloc_array(machine, UINT16, state->m_spriteram_size / 2);
	state->m_spritelist = auto_alloc_array(machine, struct f2_tempsprite, 0x400);

	for (i = 0; i < 8; i ++)
	{
		state->m_spritebank_buffered[i] = 0x400 * i;
		state->m_spritebank[i] = state->m_spritebank_buffered[i];
	}

	state->m_sprites_disabled = 1;
	state->m_sprites_active_area = 0;
	state->m_sprites_flipscreen = 0;

	state->m_sprites_master_scrollx = 0;
	state->m_sprites_master_scrolly = 0;

	state->m_spriteblendmode = 0;
	state->m_prepare_sprites = 0;

	state->m_game = 0;	/* means NOT footchmp */

	state->save_item(NAME(state->m_spritebank));
	state->save_item(NAME(state->m_spritebank_buffered));
	state->save_item(NAME(state->m_sprites_disabled));
	state->save_item(NAME(state->m_sprites_active_area));
	state->save_item(NAME(state->m_sprites_flipscreen));
	state->save_item(NAME(state->m_sprites_master_scrollx));
	state->save_item(NAME(state->m_sprites_master_scrolly));
	state->save_item(NAME(state->m_tilepri));
	state->save_item(NAME(state->m_spritepri));
	state->save_item(NAME(state->m_spriteblendmode));
	state->save_item(NAME(state->m_prepare_sprites));
	state->save_pointer(NAME(state->m_spriteram_delayed), state->m_spriteram_size / 2);
	state->save_pointer(NAME(state->m_spriteram_buffered), state->m_spriteram_size / 2);
}

/**************************************************************************************/
/*    ( spritetype, hide, hideflip, xoffs, yoffs, flipx, flipy, textflipx, textflipy) */
/**************************************************************************************/

VIDEO_START( taitof2_default )
{
	taitof2_core_vh_start(machine, 0, 0, 0);
}

VIDEO_START( taitof2_megab )   /* Megab, Liquidk */
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_quiz )   /* Quiz Crayons, Quiz Jinsei */
{
	taitof2_core_vh_start(machine, 3, 3, 3);
}

VIDEO_START( taitof2_finalb )
{
	taitof2_core_vh_start(machine, 0, 1, 1);
}

VIDEO_START( taitof2_ssi )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_growl )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_ninjak )
{
	taitof2_core_vh_start(machine, 0, 0, 0);
}

VIDEO_START( taitof2_qzchikyu )
{
	taitof2_core_vh_start(machine, 0, 0, 4);
}

VIDEO_START( taitof2_solfigtr )
{
	taitof2_core_vh_start(machine, 0, 3, -3);
}

VIDEO_START( taitof2_koshien )
{
	taitof2_core_vh_start(machine, 0, 1,  - 1);
}

VIDEO_START( taitof2_gunfront )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_thundfox )
{
	taitof2_core_vh_start(machine, 0, 3, -3);
}

VIDEO_START( taitof2_mjnquest )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	taitof2_core_vh_start(machine, 0, 0, 0);

	tc0100scn_set_bg_tilemask(state->m_tc0100scn, 0x7fff);
}

VIDEO_START( taitof2_footchmp )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	taitof2_core_vh_start(machine, 0, 3, 3);

	state->m_game = FOOTCHMP;
}

VIDEO_START( taitof2_hthero )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	taitof2_core_vh_start(machine, 0, 3, 3);

	state->m_game = FOOTCHMP;
}

VIDEO_START( taitof2_deadconx )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_deadconxj )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_metalb )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_yuyugogo )
{
	taitof2_core_vh_start(machine, 1, 3, 3);
}

VIDEO_START( taitof2_yesnoj )
{
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_dinorex )
{
	taitof2_core_vh_start(machine, 3, 3, 3);
}

VIDEO_START( taitof2_dondokod )	/* dondokod, cameltry */
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	state->m_pivot_xdisp = -16;
	state->m_pivot_ydisp = 0;
	taitof2_core_vh_start(machine, 0, 3, 3);
}

VIDEO_START( taitof2_pulirula )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	state->m_pivot_xdisp = -10;	/* alignment seems correct (see level 2, falling */
	state->m_pivot_ydisp = 16;	/* block of ice after armour man) */
	taitof2_core_vh_start(machine, 2, 3, 3);
}

VIDEO_START( taitof2_driftout )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	state->m_pivot_xdisp = -16;
	state->m_pivot_ydisp = 16;
	taitof2_core_vh_start(machine, 0, 3, 3);
}


/********************************************************
          SPRITE READ AND WRITE HANDLERS

The spritebank buffering is currently not needed.

If we wanted to buffer sprites by an extra frame, it
might be for Footchmp. That seems to be the only game
altering spritebanks of sprites while they're on screen.
********************************************************/

WRITE16_HANDLER( taitof2_sprite_extension_w )
{
	/* areas above 0x1000 cleared in some games, but not used */
	taitof2_state *state = space->machine().driver_data<taitof2_state>();

	if (offset < 0x800)
	{
		COMBINE_DATA(&state->m_sprite_extension[offset]);
	}
}


WRITE16_HANDLER( taitof2_spritebank_w )
{
	taitof2_state *state = space->machine().driver_data<taitof2_state>();
	int i = 0;
	int j = 0;

	if (offset < 2)
		return;   /* irrelevant zero writes */

	if (offset < 4)   /* special bank pairs */
	{
		j = (offset & 1) << 1;   /* either set pair 0&1 or 2&3 */
		i = data << 11;
		state->m_spritebank_buffered[j] = i;
		state->m_spritebank_buffered[j + 1] = (i + 0x400);

//logerror("bank %d, set to: %04x\n", j, i);
//logerror("bank %d, paired so: %04x\n", j + 1, i + 0x400);

	}
	else   /* last 4 are individual banks */
	{
		i = data << 10;
		state->m_spritebank_buffered[offset] = i;

//logerror("bank %d, new value: %04x\n", offset, i);
	}

}

WRITE16_HANDLER( koshien_spritebank_w )
{
	taitof2_state *state = space->machine().driver_data<taitof2_state>();
	state->m_spritebank_buffered[0] = 0x0000;   /* never changes */
	state->m_spritebank_buffered[1] = 0x0400;

	state->m_spritebank_buffered[2] =  ((data & 0x00f) + 1) * 0x800;
	state->m_spritebank_buffered[4] = (((data & 0x0f0) >> 4) + 1) * 0x800;
	state->m_spritebank_buffered[6] = (((data & 0xf00) >> 8) + 1) * 0x800;
	state->m_spritebank_buffered[3] = state->m_spritebank_buffered[2] + 0x400;
	state->m_spritebank_buffered[5] = state->m_spritebank_buffered[4] + 0x400;
	state->m_spritebank_buffered[7] = state->m_spritebank_buffered[6] + 0x400;
}

static void taito_f2_tc360_spritemixdraw( running_machine &machine, bitmap_t *dest_bmp, const rectangle *clip, const gfx_element *gfx,
		UINT32 code, UINT32 color, int flipx, int flipy, int sx, int sy, int scalex, int scaley )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	int pal_base = gfx->color_base + gfx->color_granularity * (color % gfx->total_colors);
	const UINT8 *source_base = gfx_element_get_data(gfx, code % gfx->total_elements);
	bitmap_t *priority_bitmap = gfx->machine().priority_bitmap;
	int sprite_screen_height = (scaley * gfx->height + 0x8000) >> 16;
	int sprite_screen_width = (scalex * gfx->width + 0x8000) >> 16;

	if (!scalex || !scaley)
		return;

	if (sprite_screen_width && sprite_screen_height)
	{
		/* compute sprite increment per screen pixel */
		int dx = (gfx->width << 16) / sprite_screen_width;
		int dy = (gfx->height << 16) / sprite_screen_height;

		int ex = sx + sprite_screen_width;
		int ey = sy + sprite_screen_height;

		int x_index_base;
		int y_index;

		if (flipx)
		{
			x_index_base = (sprite_screen_width - 1) * dx;
			dx = -dx;
		}
		else
		{
			x_index_base = 0;
		}

		if (flipy)
		{
			y_index = (sprite_screen_height - 1) * dy;
			dy = -dy;
		}
		else
		{
			y_index = 0;
		}

		if (clip)
		{
			if (sx < clip->min_x)
			{ /* clip left */
				int pixels = clip->min_x - sx;
				sx += pixels;
				x_index_base += pixels * dx;
			}
			if (sy < clip->min_y)
			{ /* clip top */
				int pixels = clip->min_y - sy;
				sy += pixels;
				y_index += pixels * dy;
			}
			/* NS 980211 - fixed incorrect clipping */
			if (ex > clip->max_x + 1)
			{ /* clip right */
				int pixels = ex-clip->max_x - 1;
				ex -= pixels;
			}
			if (ey > clip->max_y + 1)
			{ /* clip bottom */
				int pixels = ey-clip->max_y - 1;
				ey -= pixels;
			}
		}

		if (ex > sx)
		{
			/* skip if inner loop doesn't draw anything */
			int y;

			for (y = sy; y < ey; y++)
			{
				const UINT8 *source = source_base + (y_index >> 16) * gfx->line_modulo;
				UINT16 *dest = BITMAP_ADDR16(dest_bmp, y, 0);
				UINT8 *pri = BITMAP_ADDR8(priority_bitmap, y, 0);

				int x, x_index = x_index_base;
				for (x = sx; x < ex; x++)
				{
					int c = source[x_index >> 16];
					if (c && (pri[x] & 0x80) == 0)
					{
						UINT8 tilemap_priority = 0, sprite_priority = 0;

						// Get tilemap priority (0 - 0xf) for this destination pixel
						if (pri[x] & 0x10) tilemap_priority = state->m_tilepri[4];
						else if (pri[x] & 0x8) tilemap_priority = state->m_tilepri[3];
						else if (pri[x] & 0x4) tilemap_priority = state->m_tilepri[2];
						else if (pri[x] & 0x2) tilemap_priority = state->m_tilepri[1];
						else if (pri[x] & 0x1) tilemap_priority = state->m_tilepri[0];

						// Get sprite priority (0 - 0xf) for this source pixel
						if ((color & 0xc0) == 0xc0)
							sprite_priority = state->m_spritepri[3];
						else if ((color & 0xc0) == 0x80)
							sprite_priority = state->m_spritepri[2];
						else if ((color & 0xc0) == 0x40)
							sprite_priority = state->m_spritepri[1];
						else if ((color & 0xc0) == 0x00)
							sprite_priority = state->m_spritepri[0];

						// Blend mode 1 - Sprite under tilemap, use sprite palette with tilemap data
						if ((state->m_spriteblendmode & 0xc0) == 0xc0 && sprite_priority == (tilemap_priority - 1))
						{
							dest[x] = ((pal_base + c) & 0xfff0) | (dest[x] & 0xf);
						}
						// Blend mode 1 - Sprite over tilemap, use sprite data with tilemap palette
						else if ((state->m_spriteblendmode & 0xc0) == 0xc0 && sprite_priority == (tilemap_priority + 1))
						{
							if (dest[x] & 0xf)
								dest[x] = (dest[x] & 0xfff0) | ((pal_base + c) & 0xf);
							else
								dest[x] = pal_base + c;
						}
						// Blend mode 2 - Sprite under tilemap, use sprite data with tilemap palette
						else if ((state->m_spriteblendmode & 0xc0) == 0x80 && sprite_priority == (tilemap_priority - 1))
						{
							dest[x] = (dest[x] & 0xffef);
						}
						// Blend mode 2 - Sprite over tilemap, alternate sprite palette, confirmed in Pulirula level 2
						else if ((state->m_spriteblendmode & 0xc0) == 0x80 && sprite_priority == (tilemap_priority + 1))
						{
							dest[x] = ((pal_base + c) & 0xffef); // Pulirula level 2, Liquid Kids attract mode
						}
						// No blending
						else
						{
							if (sprite_priority > tilemap_priority) // Ninja Kids confirms tilemap takes priority in equal value case
								dest[x] = pal_base + c;
						}
						pri[x] |= 0x80;
					}

					x_index += dx;
				}

				y_index += dy;
			}
		}
	}
}

static void draw_sprites( running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect, int *primasks, int uses_tc360_mixer )
{
	/*
        Sprite format:
        0000: ---xxxxxxxxxxxxx tile code (0x0000 - 0x1fff)
        0002: xxxxxxxx-------- sprite y-zoom level
              --------xxxxxxxx sprite x-zoom level

              0x00 - non scaled = 100%
              0x80 - scaled to 50%
              0xc0 - scaled to 25%
              0xe0 - scaled to 12.5%
              0xff - scaled to zero pixels size (off)

        [this zoom scale may not be 100% correct, see Gunfront flame screen]

        0004: ----xxxxxxxxxxxx x-coordinate (-0x800 to 0x07ff)
              ---x------------ latch extra scroll
              --x------------- latch master scroll
              -x-------------- don't use extra scroll compensation
              x--------------- absolute screen coordinates (ignore all sprite scrolls)
              xxxx------------ the typical use of the above is therefore
                               1010 = set master scroll
                               0101 = set extra scroll
        0006: ----xxxxxxxxxxxx y-coordinate (-0x800 to 0x07ff)
              x--------------- marks special control commands (used in conjunction with 00a)
                               If the special command flag is set:
              ---------------x related to sprite ram bank
              ---x------------ unknown (deadconx, maybe others)
              --x------------- unknown, some games (growl, gunfront) set it to 1 when
                               screen is flipped
        0008: --------xxxxxxxx color (0x00 - 0xff)
              -------x-------- flipx
              ------x--------- flipy
              -----x---------- if set, use latched color, else use & latch specified one
              ----x----------- if set, next sprite entry is part of sequence
              ---x------------ if clear, use latched y coordinate, else use current y
              --x------------- if set, y += 16
              -x-------------- if clear, use latched x coordinate, else use current x
              x--------------- if set, x += 16
        000a: only valid when the special command bit in 006 is set
              ---------------x related to sprite ram bank. I think this is the one causing
                               the bank switch, implementing it this way all games seem
                               to properly bank switch except for footchmp which uses the
                               bit in byte 006 instead.
              ------------x--- unknown; some games toggle it before updating sprite ram.
              ------xx-------- unknown (finalb)
              -----x---------- unknown (mjnquest)
              ---x------------ disable the following sprites until another marker with
                               this bit clear is found
              --x------------- flip screen

        000b - 000f : unused

    DG comment: the sprite zoom code grafted on from Jarek's TaitoB
    may mean I have pointlessly duplicated x,y latches in the zoom &
    non zoom parts.

    */
	taitof2_state *state = machine.driver_data<taitof2_state>();
	int i, x, y, off, extoffs;
	int code, color, spritedata, spritecont, flipx, flipy;
	int xcurrent, ycurrent, big_sprite = 0;
	int y_no = 0, x_no = 0, xlatch = 0, ylatch = 0, last_continuation_tile = 0;   /* for zooms */
	UINT32 zoomword, zoomx, zoomy, zx = 0, zy = 0, zoomxlatch = 0, zoomylatch = 0;   /* for zooms */
	int scroll1x, scroll1y;
	int scrollx = 0, scrolly = 0;
	int curx, cury;
	int f2_x_offset;

	/* pdrawgfx() needs us to draw sprites front to back, so we have to build a list
       while processing sprite ram and then draw them all at the end */
	struct f2_tempsprite *sprite_ptr = state->m_spritelist;

	/* must remember enable status from last frame because driftout fails to
       reactivate them from a certain point onwards. */
	int disabled = state->m_sprites_disabled;

	/* must remember master scroll from previous frame because driftout
       sometimes doesn't set it. */
	int master_scrollx = state->m_sprites_master_scrollx;
	int master_scrolly = state->m_sprites_master_scrolly;

	/* must also remember the sprite bank from previous frame. */
	int area = state->m_sprites_active_area;

	scroll1x = 0;
	scroll1y = 0;
	x = y = 0;
	xcurrent = ycurrent = 0;
	color = 0;

	f2_x_offset = state->m_hide_pixels;   /* Get rid of 0-3 unwanted pixels on edge of screen. */
	if (state->m_sprites_flipscreen)
		f2_x_offset = -state->m_flip_hide_pixels;		// was -f2_x_offset

	/* safety check to avoid getting stuck in bank 2 for games using only one bank */
	if (area == 0x8000 && state->m_spriteram_buffered[(0x8000 + 6) / 2] == 0 && state->m_spriteram_buffered[(0x8000 + 10) / 2] == 0)
		area = 0;

	for (off = 0; off < 0x4000; off += 16)
	{
		/* sprites_active_area may change during processing */
		int offs = off + area;

		if (state->m_spriteram_buffered[(offs + 6) / 2] & 0x8000)
		{
			disabled = state->m_spriteram_buffered[(offs + 10) / 2] & 0x1000;
			state->m_sprites_flipscreen = state->m_spriteram_buffered[(offs + 10) / 2] & 0x2000;

			/* Get rid of 0-3 unwanted pixels on edge of screen. */
			f2_x_offset = state->m_hide_pixels;
			if (state->m_sprites_flipscreen)
				f2_x_offset = -state->m_flip_hide_pixels;		// was -f2_x_offset

			if (state->m_game == FOOTCHMP)
				area = 0x8000 * (state->m_spriteram_buffered[(offs + 6) / 2] & 0x0001);
			else
				area = 0x8000 * (state->m_spriteram_buffered[(offs + 10) / 2] & 0x0001);
			continue;
		}

//popmessage("%04x",area);

		/* check for extra scroll offset */
		if ((state->m_spriteram_buffered[(offs + 4) / 2] & 0xf000) == 0xa000)
		{
			master_scrollx = state->m_spriteram_buffered[(offs + 4) / 2] & 0xfff;
			if (master_scrollx >= 0x800)
				master_scrollx -= 0x1000;   /* signed value */

			master_scrolly = state->m_spriteram_buffered[(offs + 6) / 2] & 0xfff;
			if (master_scrolly >= 0x800)
				master_scrolly -= 0x1000;   /* signed value */
		}

		if ((state->m_spriteram_buffered[(offs + 4) / 2] & 0xf000) == 0x5000)
		{
			scroll1x = state->m_spriteram_buffered[(offs + 4) / 2] & 0xfff;
			if (scroll1x >= 0x800)
				scroll1x -= 0x1000;   /* signed value */

			scroll1y = state->m_spriteram_buffered[(offs + 6) / 2] & 0xfff;
			if (scroll1y >= 0x800)
				scroll1y -= 0x1000;   /* signed value */
		}

		if (disabled)
			continue;

		spritedata = state->m_spriteram_buffered[(offs + 8) / 2];

		spritecont = (spritedata & 0xff00) >> 8;

		if ((spritecont & 0x08) != 0)   /* sprite continuation flag set */
		{
			if (big_sprite == 0)   /* are we starting a big sprite ? */
			{
				xlatch = state->m_spriteram_buffered[(offs + 4) / 2] & 0xfff;
				ylatch = state->m_spriteram_buffered[(offs + 6) / 2] & 0xfff;
				x_no = 0;
				y_no = 0;
				zoomword = state->m_spriteram_buffered[(offs + 2) / 2];
				zoomylatch = (zoomword >> 8) & 0xff;
				zoomxlatch = (zoomword >> 0) & 0xff;
				big_sprite = 1;   /* we have started a new big sprite */
			}
		}
		else if (big_sprite)
		{
			last_continuation_tile = 1;   /* don't clear big_sprite until last tile done */
		}


		if ((spritecont & 0x04) == 0)
			color = spritedata & 0xff;


// The bigsprite == 0 check fixes "tied-up" little sprites in Thunderfox
// which (mostly?) have spritecont = 0x20 when they are not continuations
// of anything.
		if (big_sprite == 0 || (spritecont & 0xf0) == 0)
		{
			x = state->m_spriteram_buffered[(offs + 4) / 2];

// Some absolute x values deduced here are 1 too high (scenes when you get
// home run in Koshien, and may also relate to BG layer woods and stuff as you
// journey in MjnQuest). You will see they are 1 pixel too far to the right.
// Where is this extra pixel offset coming from??

			if (x & 0x8000)   /* absolute (koshien) */
			{
				scrollx = - f2_x_offset - 0x60;
				scrolly = 0;
			}
			else if (x & 0x4000)   /* ignore extra scroll */
			{
				scrollx = master_scrollx - f2_x_offset - 0x60;
				scrolly = master_scrolly;
			}
			else   /* all scrolls applied */
			{
				scrollx = scroll1x + master_scrollx - f2_x_offset - 0x60;
				scrolly = scroll1y + master_scrolly;
			}
			x &= 0xfff;
			y = state->m_spriteram_buffered[(offs + 6) / 2] & 0xfff;

			xcurrent = x;
			ycurrent = y;
		}
		else
		{
			if ((spritecont & 0x10) == 0)
				y = ycurrent;
			else if ((spritecont & 0x20) != 0)
			{
				y += 16;
				y_no++;   /* keep track of y tile for zooms */
			}
			if ((spritecont & 0x40) == 0)
				x = xcurrent;
			else if ((spritecont & 0x80) != 0)
			{
				x += 16;
				y_no=0;
				x_no++;   /* keep track of x tile for zooms */
			}
		}

		if (big_sprite)
		{
			zoomx = zoomxlatch;
			zoomy = zoomylatch;
			zx = 0x10;	/* default, no zoom: 16 pixels across */
			zy = 0x10;	/* default, no zoom: 16 pixels vertical */

			if (zoomx || zoomy)
			{
				/* "Zoom" zx&y is pixel size horizontally and vertically
                   of our sprite chunk. So it is difference in x and y
                   coords of our chunk and diagonally adjoining one.

                   Ideally, big_sprite should be zoomed as a whole, and not
                   into chunks. The current implementation practically only has
                   16 zoom levels instead of the 256 the hardware supports,
                   resulting in a shaky and distorted zoom.
                   (example: planet earth zooming in, in gunfront intro)
                */

				x = xlatch + (x_no * (0xff - zoomx) + 15) / 16;
				y = ylatch + (y_no * (0xff - zoomy) + 15) / 16;
				zx = xlatch + ((x_no + 1) * (0xff - zoomx) + 15) / 16 - x;
				zy = ylatch + ((y_no + 1) * (0xff - zoomy) + 15) / 16 - y;
			}
		}
		else
		{
			zoomword = state->m_spriteram_buffered[(offs + 2) / 2];
			zoomy = (zoomword >> 8) & 0xff;
			zoomx = (zoomword >> 0) & 0xff;
			zx = (0x100 - zoomx) / 16;
			zy = (0x100 - zoomy) / 16;
		}

		if (last_continuation_tile)
		{
			big_sprite=0;
			last_continuation_tile=0;
		}

		code = 0;
		extoffs = offs;
		/* spriteram[0x4000-7fff] has no corresponding extension area */
		if (extoffs >= 0x8000) extoffs -= 0x4000;

		if (state->m_sprite_type == 0)
		{
			code = state->m_spriteram_buffered[(offs) / 2] & 0x1fff;
			i = (code & 0x1c00) >> 10;
			code = state->m_spritebank[i] + (code & 0x3ff);
		}

		if (state->m_sprite_type == 1)   /* Yuyugogo */
		{
			code = state->m_spriteram_buffered[(offs) / 2] & 0x3ff;
			i = (state->m_sprite_extension[(extoffs >> 4)] & 0x3f ) << 10;
			code = (i | code);
		}

		if (state->m_sprite_type == 2)   /* Pulirula */
		{
			code = state->m_spriteram_buffered[(offs) / 2] & 0xff;
			i = (state->m_sprite_extension[(extoffs >> 4)] & 0xff00 );
			code = (i | code);
		}

		if (state->m_sprite_type == 3)   /* Dinorex and a few quizzes */
		{
			code = state->m_spriteram_buffered[(offs) / 2] & 0xff;
			i = (state->m_sprite_extension[(extoffs >> 4)] & 0xff ) << 8;
			code = (i | code);
		}

		if (code == 0) continue;

		flipx = spritecont & 0x01;
		flipy = spritecont & 0x02;

		curx = (x + scrollx) & 0xfff;
		if (curx >= 0x800)	curx -= 0x1000;   /* treat it as signed */

		cury = (y + scrolly) & 0xfff;
		if (cury >= 0x800)	cury -= 0x1000;   /* treat it as signed */

		if (state->m_sprites_flipscreen)
		{
			/* -zx/y is there to fix zoomed sprite coords in screenflip.
               drawgfxzoom does not know to draw from flip-side of sprites when
               screen is flipped; so we must correct the coords ourselves. */

			curx = 320 - curx - zx;
			cury = 256 - cury - zy;
			flipx = !flipx;
			flipy = !flipy;
		}

		{
			sprite_ptr->code = code;
			sprite_ptr->color = color;
			if (machine.gfx[0]->color_granularity == 64)	/* Final Blow is 6-bit deep */
				sprite_ptr->color /= 4;
			sprite_ptr->flipx = flipx;
			sprite_ptr->flipy = flipy;
			sprite_ptr->x = curx;
			sprite_ptr->y = cury;
			sprite_ptr->zoomx = zx << 12;
			sprite_ptr->zoomy = zy << 12;

			if (primasks || uses_tc360_mixer)
			{
				if (primasks)
					sprite_ptr->primask = primasks[(color & 0xc0) >> 6];

				sprite_ptr++;
			}
			else
			{
				drawgfxzoom_transpen(bitmap,cliprect,machine.gfx[0],
						sprite_ptr->code,
						sprite_ptr->color,
						sprite_ptr->flipx,sprite_ptr->flipy,
						sprite_ptr->x,sprite_ptr->y,
						sprite_ptr->zoomx,sprite_ptr->zoomy,0);
			}
		}
	}


	/* this happens only if primsks != NULL */
	while (sprite_ptr != state->m_spritelist)
	{
		sprite_ptr--;

		if (!uses_tc360_mixer)
			pdrawgfxzoom_transpen(bitmap,cliprect,machine.gfx[0],
					sprite_ptr->code,
					sprite_ptr->color,
					sprite_ptr->flipx,sprite_ptr->flipy,
					sprite_ptr->x,sprite_ptr->y,
					sprite_ptr->zoomx,sprite_ptr->zoomy,
					machine.priority_bitmap,sprite_ptr->primask,0);
		else
			taito_f2_tc360_spritemixdraw(machine, bitmap,cliprect,machine.gfx[0],
					sprite_ptr->code,
					sprite_ptr->color,
					sprite_ptr->flipx,sprite_ptr->flipy,
					sprite_ptr->x,sprite_ptr->y,
					sprite_ptr->zoomx,sprite_ptr->zoomy);
	}
}


static void update_spritebanks( running_machine &machine )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	int i;
#if 1
	for (i = 0; i < 8; i ++)
	{
		state->m_spritebank[i] = state->m_spritebank_buffered[i];
	}
#else
	/* this makes footchmp blobbing worse! */
	for (i = 0; i < 8; i ++)
	{
		state->m_spritebank[i] = state->m_spritebank_eof[i];
		state->m_spritebank_eof[i] = state->m_spritebank_buffered[i];
	}
#endif
}

static void taitof2_handle_sprite_buffering( running_machine &machine )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	if (state->m_prepare_sprites)	/* no buffering */
	{
		memcpy(state->m_spriteram_buffered, state->m_spriteram, state->m_spriteram_size);
		state->m_prepare_sprites = 0;
	}
}

static void taitof2_update_sprites_active_area( running_machine &machine )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	int off;

	update_spritebanks(machine);

	/* if the frame was skipped, we'll have to do the buffering now */
	taitof2_handle_sprite_buffering(machine);

	/* safety check to avoid getting stuck in bank 2 for games using only one bank */
	if (state->m_sprites_active_area == 0x8000 &&
			state->m_spriteram_buffered[(0x8000 + 6) / 2] == 0 &&
			state->m_spriteram_buffered[(0x8000 + 10) / 2] == 0)
		state->m_sprites_active_area = 0;

	for (off = 0; off < 0x4000; off += 16)
	{
		/* sprites_active_area may change during processing */
		int offs = off + state->m_sprites_active_area;

		if (state->m_spriteram_buffered[(offs + 6) / 2] & 0x8000)
		{
			state->m_sprites_disabled = state->m_spriteram_buffered[(offs + 10) / 2] & 0x1000;
			if (state->m_game == FOOTCHMP)
				state->m_sprites_active_area = 0x8000 * (state->m_spriteram_buffered[(offs + 6) / 2] & 0x0001);
			else
				state->m_sprites_active_area = 0x8000 * (state->m_spriteram_buffered[(offs + 10) / 2] & 0x0001);
			continue;
		}

		/* check for extra scroll offset */
		if ((state->m_spriteram_buffered[(offs + 4) / 2] & 0xf000) == 0xa000)
		{
			state->m_sprites_master_scrollx = state->m_spriteram_buffered[(offs + 4) / 2] & 0xfff;
			if (state->m_sprites_master_scrollx >= 0x800)
				state->m_sprites_master_scrollx -= 0x1000;   /* signed value */

			state->m_sprites_master_scrolly = state->m_spriteram_buffered[(offs + 6) / 2] & 0xfff;
			if (state->m_sprites_master_scrolly >= 0x800)
				state->m_sprites_master_scrolly -= 0x1000;   /* signed value */
		}
	}
}


SCREEN_EOF( taitof2_no_buffer )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	taitof2_update_sprites_active_area(machine);

	state->m_prepare_sprites = 1;
}

SCREEN_EOF( taitof2_full_buffer_delayed )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	UINT16 *spriteram = state->m_spriteram;
	int i;

	taitof2_update_sprites_active_area(machine);

	state->m_prepare_sprites = 0;
	memcpy(state->m_spriteram_buffered, state->m_spriteram_delayed, state->m_spriteram_size);
	for (i = 0; i < state->m_spriteram_size / 2; i++)
		state->m_spriteram_buffered[i] = spriteram[i];
	memcpy(state->m_spriteram_delayed, spriteram, state->m_spriteram_size);
}

SCREEN_EOF( taitof2_partial_buffer_delayed )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	UINT16 *spriteram = state->m_spriteram;
	int i;

	taitof2_update_sprites_active_area(machine);

	state->m_prepare_sprites = 0;
	memcpy(state->m_spriteram_buffered, state->m_spriteram_delayed, state->m_spriteram_size);
	for (i = 0;i < state->m_spriteram_size / 2; i += 4)
		state->m_spriteram_buffered[i] = spriteram[i];
	memcpy(state->m_spriteram_delayed, spriteram, state->m_spriteram_size);
}

SCREEN_EOF( taitof2_partial_buffer_delayed_thundfox )
{
	taitof2_state *state = machine.driver_data<taitof2_state>();
	UINT16 *spriteram = state->m_spriteram;
	int i;

	taitof2_update_sprites_active_area(machine);

	state->m_prepare_sprites = 0;
	memcpy(state->m_spriteram_buffered, state->m_spriteram_delayed, state->m_spriteram_size);
	for (i = 0; i < state->m_spriteram_size / 2; i += 8)
	{
		state->m_spriteram_buffered[i]     = spriteram[i];
		state->m_spriteram_buffered[i + 1] = spriteram[i + 1];
		state->m_spriteram_buffered[i + 4] = spriteram[i + 4];
	}
	memcpy(state->m_spriteram_delayed, spriteram, state->m_spriteram_size);
}

SCREEN_EOF( taitof2_partial_buffer_delayed_qzchikyu )
{
	/* spriteram[2] and [3] are 1 frame behind...
       probably thundfox_eof_callback would work fine */

	taitof2_state *state = machine.driver_data<taitof2_state>();
	UINT16 *spriteram = state->m_spriteram;
	int i;

	taitof2_update_sprites_active_area(machine);

	state->m_prepare_sprites = 0;
	memcpy(state->m_spriteram_buffered, state->m_spriteram_delayed, state->m_spriteram_size);
	for (i = 0; i < state->m_spriteram_size / 2; i += 8)
	{
		state->m_spriteram_buffered[i]     = spriteram[i];
		state->m_spriteram_buffered[i + 1] = spriteram[i + 1];
		state->m_spriteram_buffered[i + 4] = spriteram[i + 4];
		state->m_spriteram_buffered[i + 5] = spriteram[i + 5];	// not needed?
		state->m_spriteram_buffered[i + 6] = spriteram[i + 6];	// not needed?
		state->m_spriteram_buffered[i + 7] = spriteram[i + 7];	// not needed?
	}
	memcpy(state->m_spriteram_delayed, spriteram, state->m_spriteram_size);
}


/* SSI */
SCREEN_UPDATE( taitof2_ssi )
{
	taitof2_handle_sprite_buffering(screen->machine());

	/* SSI only uses sprites, the tilemap registers are not even initialized.
       (they are in Majestic 12, but the tilemaps are not used anyway) */
	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);
	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 0);
	return 0;
}


SCREEN_UPDATE( taitof2_yesnoj )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();

	taitof2_handle_sprite_buffering(screen->machine());

	tc0100scn_tilemap_update(state->m_tc0100scn);

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);	/* wrong color? */
	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 0);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, tc0100scn_bottomlayer(state->m_tc0100scn), 0, 0);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, tc0100scn_bottomlayer(state->m_tc0100scn) ^ 1, 0, 0);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, 2, 0, 0);
	return 0;
}


SCREEN_UPDATE( taitof2 )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();

	taitof2_handle_sprite_buffering(screen->machine());

	tc0100scn_tilemap_update(state->m_tc0100scn);

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);	/* wrong color? */
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, tc0100scn_bottomlayer(state->m_tc0100scn), 0, 0);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, tc0100scn_bottomlayer(state->m_tc0100scn) ^ 1, 0, 0);
	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 0);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, 2, 0, 0);
	return 0;
}


SCREEN_UPDATE( taitof2_pri )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();
	int layer[3];

	taitof2_handle_sprite_buffering(screen->machine());

	tc0100scn_tilemap_update(state->m_tc0100scn);

	layer[0] = tc0100scn_bottomlayer(state->m_tc0100scn);
	layer[1] = layer[0] ^ 1;
	layer[2] = 2;
	state->m_tilepri[layer[0]] = tc0360pri_r(state->m_tc0360pri, 5) & 0x0f;
	state->m_tilepri[layer[1]] = tc0360pri_r(state->m_tc0360pri, 5) >> 4;
	state->m_tilepri[layer[2]] = tc0360pri_r(state->m_tc0360pri, 4) >> 4;

	state->m_spritepri[0] = tc0360pri_r(state->m_tc0360pri, 6) & 0x0f;
	state->m_spritepri[1] = tc0360pri_r(state->m_tc0360pri, 6) >> 4;
	state->m_spritepri[2] = tc0360pri_r(state->m_tc0360pri, 7) & 0x0f;
	state->m_spritepri[3] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;

	state->m_spriteblendmode = tc0360pri_r(state->m_tc0360pri, 0) & 0xc0;

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);	/* wrong color? */

	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, layer[0], 0, 1);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, layer[1], 0, 2);
	tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, layer[2], 0, 4);

	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 1);
	return 0;
}



static void draw_roz_layer( running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect, UINT32 priority)
{
	taitof2_state *state = machine.driver_data<taitof2_state>();

	if (state->m_tc0280grd != NULL)
		tc0280grd_zoom_draw(state->m_tc0280grd, bitmap, cliprect, state->m_pivot_xdisp, state->m_pivot_ydisp, priority);

	if (state->m_tc0430grw != NULL)
		tc0430grw_zoom_draw(state->m_tc0430grw, bitmap, cliprect, state->m_pivot_xdisp, state->m_pivot_ydisp, priority);
}

SCREEN_UPDATE( taitof2_pri_roz )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();
	int tilepri[3];
	int rozpri;
	int layer[3];
	int drawn;
	int i,j;
	int roz_base_color = (tc0360pri_r(state->m_tc0360pri, 1) & 0x3f) << 2;

	taitof2_handle_sprite_buffering(screen->machine());

	if (state->m_tc0280grd != NULL)
		tc0280grd_tilemap_update(state->m_tc0280grd, roz_base_color);

	if (state->m_tc0430grw != NULL)
		tc0430grw_tilemap_update(state->m_tc0430grw, roz_base_color);

	tc0100scn_tilemap_update(state->m_tc0100scn);

	rozpri = (tc0360pri_r(state->m_tc0360pri, 1) & 0xc0) >> 6;
	rozpri = (tc0360pri_r(state->m_tc0360pri, 8 + rozpri / 2) >> 4 * (rozpri & 1)) & 0x0f;

	layer[0] = tc0100scn_bottomlayer(state->m_tc0100scn);
	layer[1] = layer[0] ^ 1;
	layer[2] = 2;

	tilepri[layer[0]] = tc0360pri_r(state->m_tc0360pri, 5) & 0x0f;
	tilepri[layer[1]] = tc0360pri_r(state->m_tc0360pri, 5) >> 4;
	tilepri[layer[2]] = tc0360pri_r(state->m_tc0360pri, 4) >> 4;

	state->m_spritepri[0] = tc0360pri_r(state->m_tc0360pri, 6) & 0x0f;
	state->m_spritepri[1] = tc0360pri_r(state->m_tc0360pri, 6) >> 4;
	state->m_spritepri[2] = tc0360pri_r(state->m_tc0360pri, 7) & 0x0f;
	state->m_spritepri[3] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;

	state->m_spriteblendmode = tc0360pri_r(state->m_tc0360pri, 0) & 0xc0;

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);	/* wrong color? */

	drawn = 0;
	for (i = 0; i < 16; i++)
	{
		if (rozpri == i)
		{
			draw_roz_layer(screen->machine(), bitmap, cliprect, 1 << drawn);
			state->m_tilepri[drawn] = i;
			drawn++;
		}

		for (j = 0; j < 3; j++)
		{
			if (tilepri[layer[j]] == i)
			{
				tc0100scn_tilemap_draw(state->m_tc0100scn, bitmap, cliprect, layer[j], 0, 1 << drawn);
				state->m_tilepri[drawn] = i;
				drawn++;
			}
		}
	}

	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 1);
	return 0;
}



/* Thunderfox */
SCREEN_UPDATE( taitof2_thundfox )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();
	int tilepri[2][3];
	int spritepri[4];
	int layer[2][3];
	int drawn[2];

	taitof2_handle_sprite_buffering(screen->machine());

	tc0100scn_tilemap_update(state->m_tc0100scn_1);
	tc0100scn_tilemap_update(state->m_tc0100scn_2);

	layer[0][0] = tc0100scn_bottomlayer(state->m_tc0100scn_1);
	layer[0][1] = layer[0][0] ^ 1;
	layer[0][2] = 2;
	tilepri[0][layer[0][0]] = tc0360pri_r(state->m_tc0360pri, 5) & 0x0f;
	tilepri[0][layer[0][1]] = tc0360pri_r(state->m_tc0360pri, 5) >> 4;
	tilepri[0][layer[0][2]] = tc0360pri_r(state->m_tc0360pri, 4) >> 4;

	layer[1][0] = tc0100scn_bottomlayer(state->m_tc0100scn_2);
	layer[1][1] = layer[1][0] ^ 1;
	layer[1][2] = 2;
	tilepri[1][layer[1][0]] = tc0360pri_r(state->m_tc0360pri, 9) & 0x0f;
	tilepri[1][layer[1][1]] = tc0360pri_r(state->m_tc0360pri, 9) >> 4;
	tilepri[1][layer[1][2]] = tc0360pri_r(state->m_tc0360pri, 8) >> 4;

	spritepri[0] = tc0360pri_r(state->m_tc0360pri, 6) & 0x0f;
	spritepri[1] = tc0360pri_r(state->m_tc0360pri, 6) >> 4;
	spritepri[2] = tc0360pri_r(state->m_tc0360pri, 7) & 0x0f;
	spritepri[3] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);	/* wrong color? */

	/*
    TODO: This isn't the correct way to handle the priority. At the moment of
    writing, pdrawgfx() doesn't support 6 layers, so I have to cheat, assuming
    that the two FG layers are always on top of sprites.
    */

	drawn[0] = drawn[1] = 0;
	while (drawn[0] < 2 && drawn[1] < 2)
	{
		int pick;
		device_t *tc0100scn;

		if (tilepri[0][drawn[0]] < tilepri[1][drawn[1]])
		{
			pick = 0;
			tc0100scn = state->m_tc0100scn_1;
		}
		else
		{
			pick = 1;
			tc0100scn = state->m_tc0100scn_2;
		}

		tc0100scn_tilemap_draw(tc0100scn , bitmap, cliprect, layer[pick][drawn[pick]], 0, 1 << (drawn[pick] + 2 * pick));
		drawn[pick]++;
	}
	while (drawn[0] < 2)
	{
		tc0100scn_tilemap_draw(state->m_tc0100scn_1, bitmap, cliprect, layer[0][drawn[0]], 0, 1 << drawn[0]);
		drawn[0]++;
	}
	while (drawn[1] < 2)
	{
		tc0100scn_tilemap_draw(state->m_tc0100scn_2, bitmap, cliprect, layer[1][drawn[1]], 0, 1 << (drawn[1] + 2));
		drawn[1]++;
	}

	{
		int primasks[4] = {0,0,0,0};
		int i;

		for (i = 0;i < 4;i++)
		{
			if (spritepri[i] < tilepri[0][0]) primasks[i] |= 0xaaaa;
			if (spritepri[i] < tilepri[0][1]) primasks[i] |= 0xcccc;
			if (spritepri[i] < tilepri[1][0]) primasks[i] |= 0xf0f0;
			if (spritepri[i] < tilepri[1][1]) primasks[i] |= 0xff00;
		}

		draw_sprites(screen->machine(), bitmap,cliprect,primasks,0);
	}


	/*
    TODO: This isn't the correct way to handle the priority. At the moment of
    writing, pdrawgfx() doesn't support 6 layers, so I have to cheat, assuming
    that the two FG layers are always on top of sprites.
    */

	if (tilepri[0][2] < tilepri[1][2])
	{
		tc0100scn_tilemap_draw(state->m_tc0100scn_1, bitmap, cliprect, layer[0][2], 0, 0);
		tc0100scn_tilemap_draw(state->m_tc0100scn_2, bitmap, cliprect, layer[1][2], 0, 0);
	}
	else
	{
		tc0100scn_tilemap_draw(state->m_tc0100scn_2, bitmap, cliprect, layer[1][2], 0, 0);
		tc0100scn_tilemap_draw(state->m_tc0100scn_1, bitmap, cliprect, layer[0][2], 0, 0);
	}
	return 0;
}



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

Deadconx and Footchmp use in the PRI chip
-----------------------------------------

+4  xxxx0000   BG0
    0000xxxx   BG3
+6  xxxx0000   BG2
    0000xxxx   BG1

Deadconx = 0x7db9 (bg0-3) 0x8eca (sprites)
So it has bg0 [back] / s / bg1 / s / bg2 / s / bg3 / s

Footchmp = 0x8db9 (bg0-3) 0xe5ac (sprites)
So it has s / bg0 [grass] / bg1 [crowd] / s / bg2 [goal] / s / bg3 [messages] / s [scan dots]

Metalb uses in the PRI chip
---------------------------

+4  xxxx0000   BG1
    0000xxxx   BG0
+6  xxxx0000   BG3
    0000xxxx   BG2

and it changes these (and the sprite pri settings) a lot.

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

SCREEN_UPDATE( taitof2_metalb )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();
	UINT8 layer[5], invlayer[4];
	UINT16 priority;

	taitof2_handle_sprite_buffering(screen->machine());

	tc0480scp_tilemap_update(state->m_tc0480scp);

	priority = tc0480scp_get_bg_priority(state->m_tc0480scp);

	layer[0] = (priority & 0xf000) >> 12;	/* tells us which bg layer is bottom */
	layer[1] = (priority & 0x0f00) >>  8;
	layer[2] = (priority & 0x00f0) >>  4;
	layer[3] = (priority & 0x000f) >>  0;	/* tells us which is top */
	layer[4] = 4;   /* text layer always over bg layers */

	invlayer[layer[0]] = 0;
	invlayer[layer[1]] = 1;
	invlayer[layer[2]] = 2;
	invlayer[layer[3]] = 3;

	state->m_tilepri[invlayer[0]] = tc0360pri_r(state->m_tc0360pri, 4) & 0x0f;	/* bg0 */
	state->m_tilepri[invlayer[1]] = tc0360pri_r(state->m_tc0360pri, 4) >> 4;	/* bg1 */
	state->m_tilepri[invlayer[2]] = tc0360pri_r(state->m_tc0360pri, 5) & 0x0f;	/* bg2 */
	state->m_tilepri[invlayer[3]] = tc0360pri_r(state->m_tc0360pri, 5) >> 4;	/* bg3 */
	state->m_tilepri[4] = tc0360pri_r(state->m_tc0360pri, 9) & 0x0f;			/* fg (text layer) */

	state->m_spritepri[0] = tc0360pri_r(state->m_tc0360pri, 6) & 0x0f;
	state->m_spritepri[1] = tc0360pri_r(state->m_tc0360pri, 6) >> 4;
	state->m_spritepri[2] = tc0360pri_r(state->m_tc0360pri, 7) & 0x0f;
	state->m_spritepri[3] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;

	state->m_spriteblendmode = tc0360pri_r(state->m_tc0360pri, 0) & 0xc0;

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);

	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[0], 0 ,1);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[1], 0, 2);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[2], 0, 4);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[3], 0, 8);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[4], 0, 16);

	draw_sprites(screen->machine(), bitmap, cliprect, NULL, 1);
	return 0;
}


/* Deadconx, Footchmp */
SCREEN_UPDATE( taitof2_deadconx )
{
	taitof2_state *state = screen->machine().driver_data<taitof2_state>();
	UINT8 layer[5];
	UINT8 tilepri[5];
	UINT8 spritepri[4];
	UINT16 priority;

	taitof2_handle_sprite_buffering(screen->machine());

	tc0480scp_tilemap_update(state->m_tc0480scp);

	priority = tc0480scp_get_bg_priority(state->m_tc0480scp);

	layer[0] = (priority & 0xf000) >> 12;	/* tells us which bg layer is bottom */
	layer[1] = (priority & 0x0f00) >>  8;
	layer[2] = (priority & 0x00f0) >>  4;
	layer[3] = (priority & 0x000f) >>  0;	/* tells us which is top */
	layer[4] = 4;   /* text layer always over bg layers */

	tilepri[0] = tc0360pri_r(state->m_tc0360pri, 4) >> 4;      /* bg0 */
	tilepri[1] = tc0360pri_r(state->m_tc0360pri, 5) & 0x0f;    /* bg1 */
	tilepri[2] = tc0360pri_r(state->m_tc0360pri, 5) >> 4;      /* bg2 */
	tilepri[3] = tc0360pri_r(state->m_tc0360pri, 4) & 0x0f;    /* bg3 */

/* we actually assume text layer is on top of everything anyway, but FWIW... */
	tilepri[layer[4]] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;    /* fg (text layer) */

	spritepri[0] = tc0360pri_r(state->m_tc0360pri, 6) & 0x0f;
	spritepri[1] = tc0360pri_r(state->m_tc0360pri, 6) >> 4;
	spritepri[2] = tc0360pri_r(state->m_tc0360pri, 7) & 0x0f;
	spritepri[3] = tc0360pri_r(state->m_tc0360pri, 7) >> 4;

	bitmap_fill(screen->machine().priority_bitmap, cliprect, 0);
	bitmap_fill(bitmap, cliprect, 0);

	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[0], 0 ,1);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[1], 0, 2);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[2], 0, 4);
	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[3], 0, 8);

	{
		int primasks[4] = {0,0,0,0};
		int i;

		for (i = 0;i < 4;i++)
		{
			if (spritepri[i] < tilepri[(layer[0])]) primasks[i] |= 0xaaaa;
			if (spritepri[i] < tilepri[(layer[1])]) primasks[i] |= 0xcccc;
			if (spritepri[i] < tilepri[(layer[2])]) primasks[i] |= 0xf0f0;
			if (spritepri[i] < tilepri[(layer[3])]) primasks[i] |= 0xff00;
		}

		draw_sprites(screen->machine(), bitmap, cliprect, primasks, 0);
	}

	/*
    TODO: This isn't the correct way to handle the priority. At the moment of
    writing, pdrawgfx() doesn't support 5 layers, so I have to cheat, assuming
    that the FG layer is always on top of sprites.
    */

	tc0480scp_tilemap_draw(state->m_tc0480scp, bitmap, cliprect, layer[4], 0, 0);
	return 0;
}