summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/sega16sp.cpp
blob: 970f1a40b90ce00b5a5c072a62f9c4f5caf8a317 (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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    Sega 16-bit sprite hardware

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

#include "emu.h"
#include "sega16sp.h"
#include "segaic16.h"



//****************************************************************************
//  CONSTANTS
//****************************************************************************

// device type definition
DEFINE_DEVICE_TYPE(SEGA_HANGON_SPRITES,    sega_hangon_sprite_device,    "sega_hangon_sprite",    "Sega Custom Sprites (Hang On)")
DEFINE_DEVICE_TYPE(SEGA_SHARRIER_SPRITES,  sega_sharrier_sprite_device,  "sega_sharrier_sprite",  "Sega Custom Sprites (Space Harrier)")
DEFINE_DEVICE_TYPE(SEGA_OUTRUN_SPRITES,    sega_outrun_sprite_device,    "sega_outrun_sprite",    "Sega Custom Sprites (Out Run)")
DEFINE_DEVICE_TYPE(SEGA_SYS16A_SPRITES,    sega_sys16a_sprite_device,    "sega_sys16a_sprite",    "Sega System 16A Sprites")
DEFINE_DEVICE_TYPE(BOOTLEG_SYS16A_SPRITES, bootleg_sys16a_sprite_device, "bootleg_sys16a_sprite", "Sega System 16A Sprites (bootleg)")
DEFINE_DEVICE_TYPE(SEGA_SYS16B_SPRITES,    sega_sys16b_sprite_device,    "sega_sys16b_sprite",    "Sega System 16B Sprites")
DEFINE_DEVICE_TYPE(SEGA_XBOARD_SPRITES,    sega_xboard_sprite_device,    "sega_xboard_sprite",    "Sega X-Board Sprites")
DEFINE_DEVICE_TYPE(SEGA_YBOARD_SPRITES,    sega_yboard_sprite_device,    "sega_yboard_sprite",    "Sega Y-Board Sprites")



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

//-------------------------------------------------
//  sega_16bit_sprite_device -- core constructor
//-------------------------------------------------

sega_16bit_sprite_device::sega_16bit_sprite_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner)
	: sprite16_device_ind16(mconfig, type, tag, owner)
	, m_flip(false)
{
	// default to 1:1 bank mapping
	for (int bank = 0; bank < ARRAY_LENGTH(m_bank); bank++)
		m_bank[bank] = bank;
}


//-------------------------------------------------
//  device_start -- device startup
//-------------------------------------------------

void sega_16bit_sprite_device::device_start()
{
	// let the parent do its work
	sprite16_device_ind16::device_start();

	// save states
	save_item(NAME(m_flip));
	save_item(NAME(m_bank));
}


//-------------------------------------------------
//  draw_write -- trigger a buffer flip
//-------------------------------------------------

WRITE16_MEMBER( sega_16bit_sprite_device::draw_write )
{
	uint32_t *src = reinterpret_cast<uint32_t *>(spriteram());
	uint32_t *dst = reinterpret_cast<uint32_t *>(buffer());

	// swap the halves of the sprite RAM
	for (int i = 0; i < spriteram_bytes()/4; i++)
	{
		uint32_t temp = *src;
		*src++ = *dst;
		*dst++ = temp;
	}

	// hack for thunderblade
	*spriteram() = 0xffff;

	// we will render the sprites when the video update happens
}



//****************************************************************************
//  HANG ON-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_hangon_sprite_device -- constructor
//-------------------------------------------------

sega_hangon_sprite_device::sega_hangon_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, SEGA_HANGON_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(189, -1);
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_hangon_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  Hang On-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   bbbbbbbb --------  Bottom scanline of sprite - 1
	//       +0   -------- tttttttt  Top scanline of sprite - 1
	//       +2   bbbb---- --------  Sprite bank
	//       +2   -------x xxxxxxxx  X position of sprite (position $BD is screen position 0)
	//       +4   pppppppp pppppppp  Signed 16-bit pitch value between scanlines
	//       +6   -ooooooo oooooooo  Offset within selected sprite bank
	//       +6   f------- --------  Horizontal flip: read the data backwards if set
	//       +8   --cccccc --------  Sprite color palette
	//       +8   -------- zzzzzz--  Zoom factor
	//       +8   -------- ------pp  Sprite priority
	//       +E   dddddddd dddddddd  Scratch space for current address
	//
	//  Final bitmap format:
	//
	//            ----pp-- --------  Sprite priority
	//            ------cc cccc----  Sprite color palette
	//            -------- ----llll  4-bit pixel data
	//
	//  Special notes:
	//
	//      There is an interaction between the horizonal flip bit and the offset.
	//      The offset is maintained as a 16-bit value, even though only the lower
	//      15 bits are used for the address. The top bit is used to control flipping.
	//      This means that if the low 15 bits overflow during rendering, the sprite
	//      data will be read backwards after the overflow. This is important to
	//      emulate correctly as many games make use of this feature to render sprites
	//      at the beginning of a bank.
	//

	// render the sprites in order
	const uint16_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x10000;
	const uint8_t *zoom = memregion("zoom")->base();
	uint16_t *ramend = spriteram() + spriteram_elements();
	for (uint16_t *data = spriteram(); data < ramend; data += 8)
	{
		// fetch the bottom; stop when we get something out of range
		int bottom  = data[0] >> 8;
		if (bottom > 0xf0)
			break;

		// extract remaining parameters
		int top     = data[0] & 0xff;
		int bank    = m_bank[(data[1] >> 12) & 0xf];
		int xpos    = data[1] & 0x1ff;
		int pitch   = int16_t(data[2]);
		uint16_t addr = data[3];
		int colpri  = (((data[4] >> 8) & 0x3f) << 4) | (((data[4] >> 0) & 0x3) << 10);
		int vzoom   = (data[4] >> 2) & 0x3f;
		int hzoom   = vzoom << 1;

		// initialize the end address to the start address
		data[7] = addr;

		// if top greater than/equal to bottom, or invalid bank, punt
		if (top >= bottom || bank == 255)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint16_t *spritedata = spritebase + 0x8000 * bank;

		// determine the starting zoom address and mask
		int zaddr = (vzoom & 0x38) << 5;
		int zmask = 1 << (vzoom & 7);

		// loop from top to bottom
		int minx = xpos;
		int maxx = cliprect.min_x - 1;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		for (int y = top; y < bottom; y++)
		{
			// advance a row
			addr += pitch;

			// if the zoom bit says so, add pitch a second time
			if (zoom[zaddr++] & zmask)
				addr += pitch;

			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int xacc = 0x00;
				int x;

				// note that the System 16A sprites have a design flaw that allows the address
				// to carry into the flip flag, which is the topmost bit -- it is very important
				// to emulate this as the games compensate for it

				// non-flipped case
				if (!(addr & 0x8000))
				{
					// start at the word before because we preincrement below
					data[7] = addr - 1;
					for (x = xpos; x <= cliprect.max_x; )
					{
						uint16_t pixels = spritedata[++data[7] & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data[7] = addr + 1;
					for (x = xpos; x <= cliprect.max_x; )
					{
						uint16_t pixels = spritedata[--data[7] & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (y < miny) miny = y;
				maxy = y;
			}
		}

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}



//****************************************************************************
//  SPACE HARRIER-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_sharrier_sprite_device -- constructor
//-------------------------------------------------

sega_sharrier_sprite_device::sega_sharrier_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, SEGA_SHARRIER_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(189, -1);
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_sharrier_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  Space Harrier-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   bbbbbbbb --------  Bottom scanline of sprite - 1
	//       +0   -------- tttttttt  Top scanline of sprite - 1
	//       +2   bbbb---- --------  Sprite bank
	//       +2   -------x xxxxxxxx  X position of sprite (position $BD is screen position 0)
	//       +4   s------- --------  Sprite shadow disable (0=enable, 1=disable)
	//       +4   -p------ --------  Sprite priority
	//       +4   --cccccc --------  Sprite color palette
	//       +4   -------- -ppppppp  Signed 7-bit pitch value between scanlines
	//       +6   f------- --------  Horizontal flip: read the data backwards if set
	//       +6   -ooooooo oooooooo  Offset within selected sprite bank
	//       +8   --zzzzzz --------  Horizontal zoom factor
	//       +8   -------- --zzzzzz  Vertical zoom factor
	//       +E   dddddddd dddddddd  Scratch space for current address
	//
	//  Final bitmap format:
	//
	//            ----s--- --------  Sprite shadow disable
	//            -----p-- --------  Sprite priority
	//            ------cc cccc----  Sprite color palette
	//            -------- ----llll  4-bit pixel data
	//
	//  Special notes:
	//
	//      There is an interaction between the horizonal flip bit and the offset.
	//      The offset is maintained as a 16-bit value, even though only the lower
	//      15 bits are used for the address. The top bit is used to control flipping.
	//      This means that if the low 15 bits overflow during rendering, the sprite
	//      data will be read backwards after the overflow. This is important to
	//      emulate correctly as many games make use of this feature to render sprites
	//      at the beginning of a bank.
	//

	// render the sprites in order
	const uint32_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x20000;
	const uint8_t *zoom = memregion("zoom")->base();
	uint16_t *ramend = spriteram() + spriteram_elements();
	for (uint16_t *data = spriteram(); data < ramend; data += 8)
	{
		// fetch the bottom; stop when we get something out of range
		int bottom  = data[0] >> 8;
		if (bottom > 0xf0)
			break;

		// extract remaining parameters
		int top     = data[0] & 0xff;
		int bank    = m_bank[(data[1] >> 12) & 0x7];
		int xpos    = data[1] & 0x1ff;
		int colpri  = ((data[2] >> 8) & 0xff) << 4;
		int pitch   = int16_t(data[2] << 9) >> 9;
		uint16_t addr = data[3];
		int hzoom   = ((data[4] >> 8) & 0x3f) << 1;
		int vzoom   = (data[4] >> 0) & 0x3f;

		// initialize the end address to the start address
		data[7] = addr;

		// if top greater than/equal to bottom, or invalid bank, punt
		if (top >= bottom || bank == 255)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint32_t *spritedata = spritebase + 0x8000 * bank;

		// determine the starting zoom address and mask
		int zaddr = (vzoom & 0x38) << 5;
		int zmask = 1 << (vzoom & 7);

		// loop from top to bottom
		int minx = xpos;
		int maxx = cliprect.min_x - 1;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		for (int y = top; y < bottom; y++)
		{
			// advance a row
			addr += pitch;

			// if the zoom bit says so, add pitch a second time
			if (zoom[zaddr++] & zmask)
				addr += pitch;

			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int xacc = 0x00;
				int x;

				// note that the System 16A sprites have a design flaw that allows the address
				// to carry into the flip flag, which is the topmost bit -- it is very important
				// to emulate this as the games compensate for it

				// non-flipped case
				if (!(addr & 0x8000))
				{
					// start at the word before because we preincrement below
					data[7] = addr - 1;
					for (x = xpos; x <= cliprect.max_x; )
					{
						uint32_t pixels = spritedata[++data[7] & 0x7fff];

						// draw 8 pixels
						int pix;
						pix = (pixels >> 28) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 24) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 20) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 16) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data[7] = addr + 1;
					for (x = xpos; x <= cliprect.max_x; )
					{
						uint32_t pixels = spritedata[--data[7] & 0x7fff];

						// draw 8 pixels
						int pix;
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 16) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 20) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 24) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }
						pix = (pixels >> 28) & 0xf; xacc = (xacc & 0xff) + hzoom; if (xacc < 0x100) { if (x >= cliprect.min_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x++; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (y < miny) miny = y;
				maxy = y;
			}
		}

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}



//****************************************************************************
//  SYSTEM 16A-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_sys16a_sprite_device -- constructor
//-------------------------------------------------

sega_sys16a_sprite_device::sega_sys16a_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, SEGA_SYS16A_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(189, -1, -189, -1);
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_sys16a_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  System 16A-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   bbbbbbbb --------  Bottom scanline of sprite - 1
	//       +0   -------- tttttttt  Top scanline of sprite - 1
	//       +2   -------x xxxxxxxx  X position of sprite (position $BD is screen position 0)
	//       +4   pppppppp pppppppp  Signed 16-bit pitch value between scanlines
	//       +6   -ooooooo oooooooo  Offset within selected sprite bank
	//       +6   f------- --------  Horizontal flip: read the data backwards if set
	//       +8   --cccccc --------  Sprite color palette
	//       +8   -------- -bbb----  Sprite bank
	//       +8   -------- ------pp  Sprite priority
	//       +E   dddddddd dddddddd  Scratch space for current address
	//
	//  Final bitmap format:
	//
	//            ----pp-- --------  Sprite priority
	//            ------cc cccc----  Sprite color palette
	//            -------- ----llll  4-bit pixel data
	//
	//  Special notes:
	//
	//      There is an interaction between the horizonal flip bit and the offset.
	//      The offset is maintained as a 16-bit value, even though only the lower
	//      15 bits are used for the address. The top bit is used to control flipping.
	//      This means that if the low 15 bits overflow during rendering, the sprite
	//      data will be read backwards after the overflow. This is important to
	//      emulate correctly as many games make use of this feature to render sprites
	//      at the beginning of a bank.
	//

	// render the sprites in order
	const uint16_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x10000;
	uint16_t *ramend = spriteram() + spriteram_elements();
	for (uint16_t *data = spriteram(); data < ramend; data += 8)
	{
		// fetch the bottom; stop when we get something out of range
		int bottom  = data[0] >> 8;
		if (bottom > 0xf0)
			break;

		// extract remaining parameters
		int top     = data[0] & 0xff;
		int xpos    = data[1] & 0x1ff;
		int pitch   = int16_t(data[2]);
		uint16_t addr = data[3];
		int colpri  = (((data[4] >> 8) & 0x3f) << 4) | (((data[4] >> 0) & 0x3) << 10);
		int bank    = m_bank[(data[4] >> 4) & 0x7];

		// initialize the end address to the start address
		data[7] = addr;

		// if top greater than/equal to bottom, or invalid bank, punt
		if (top >= bottom || bank == 255)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint16_t *spritedata = spritebase + 0x8000 * bank;

		// adjust positions for screen flipping
		int xdelta = 1;
		if (m_flip)
		{
			int temp = top;
			top = 224 - bottom;
			bottom = 224 - temp;
			xpos = 320 - xpos;
			xdelta = -1;
			set_origin(m_xoffs_flipped, m_yoffs_flipped);
		}
		else
		{
			set_origin(m_xoffs, m_yoffs);
		}

		// loop from top to bottom
		int minx = xpos;
		int maxx = xpos;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		for (int y = top; y < bottom; y++)
		{
			// advance a row
			addr += pitch;

			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int x;

				// note that the System 16A sprites have a design flaw that allows the address
				// to carry into the flip flag, which is the topmost bit -- it is very important
				// to emulate this as the games compensate for it

				// non-flipped case
				if (!(addr & 0x8000))
				{
					// start at the word before because we preincrement below
					data[7] = addr - 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[++data[7] & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >> 12) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  8) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  4) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  0) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data[7] = addr + 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[--data[7] & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >>  0) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  4) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  8) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >> 12) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (x < minx) minx = x;
				if (y < miny) miny = y;
				maxy = y;
			}
		}

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}


//****************************************************************************
//  BOOTLEG SYSTEM 16A-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  bootleg_sys16a_sprite_device -- constructor
//-------------------------------------------------

bootleg_sys16a_sprite_device::bootleg_sys16a_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, BOOTLEG_SYS16A_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	m_addrmap[0] = 0;
	m_addrmap[1] = 1;
	m_addrmap[2] = 2;
	m_addrmap[3] = 3;
	m_addrmap[4] = 4;
	m_addrmap[5] = 5;
	m_addrmap[6] = 6;
	m_addrmap[7] = 7;
	set_local_origin(189, -1);
}


//-------------------------------------------------
//  set_remap -- configure sprite address
//  remapping
//-------------------------------------------------

void bootleg_sys16a_sprite_device::set_remap(uint8_t offs0, uint8_t offs1, uint8_t offs2, uint8_t offs3, uint8_t offs4, uint8_t offs5, uint8_t offs6, uint8_t offs7)
{
	m_addrmap[0] = offs0;
	m_addrmap[1] = offs1;
	m_addrmap[2] = offs2;
	m_addrmap[3] = offs3;
	m_addrmap[4] = offs4;
	m_addrmap[5] = offs5;
	m_addrmap[6] = offs6;
	m_addrmap[7] = offs7;
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void bootleg_sys16a_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  Bootleg System 16A-style sprites
	//
	//  These are identical to regular System 16A sprites (see above), with two exceptions:
	//
	//      1. Addresses within each sprite entry are generally shuffled relative
	//          to the original, and
	//
	//      2. The pitch increment happens at the end, not at the beginning of
	//          the loop.
	//

	// render the sprites in order
	const uint16_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x10000;
	uint16_t *ramend = spriteram() + spriteram_elements();
	for (uint16_t *data = spriteram(); data < ramend; data += 8)
	{
		// fetch the bottom; stop when we get something out of range
		int bottom  = data[m_addrmap[0]] >> 8;
		if (bottom > 0xf0)
			break;

		// extract remaining parameters
		int top     = data[m_addrmap[0]] & 0xff;
		int xpos    = data[m_addrmap[1]] & 0x1ff;
		int pitch   = int16_t(data[m_addrmap[2]]);
		uint16_t addr = data[m_addrmap[3]];
		int colpri  = (((data[m_addrmap[4]] >> 8) & 0x3f) << 4) | (((data[m_addrmap[4]] >> 0) & 0x3) << 10);
		int bank    = m_bank[(data[m_addrmap[4]] >> 4) & 0x7];

		// initialize the end address to the start address
		uint16_t &data7 = data[m_addrmap[7]];
		data7 = addr;

		// if top greater than/equal to bottom, or invalid bank, punt
		if (top >= bottom || bank == 255)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint16_t *spritedata = spritebase + 0x8000 * bank;

		// adjust positions for screen flipping
		int xdelta = 1;
		if (m_flip)
		{
			int temp = top;
			top = 224 - bottom;
			bottom = 224 - temp;
			xpos = 320 - xpos;
			xdelta = -1;
			set_origin(m_xoffs_flipped, m_yoffs_flipped);
		}
		else
		{
			set_origin(m_xoffs, m_yoffs);
		}

		// loop from top to bottom
		int minx = xpos;
		int maxx = xpos;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		for (int y = top; y < bottom; y++)
		{
			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int x;

				// note that the System 16A sprites have a design flaw that allows the address
				// to carry into the flip flag, which is the topmost bit -- it is very important
				// to emulate this as the games compensate for it

				// non-flipped case
				if (!(addr & 0x8000))
				{
					// start at the word before because we preincrement below
					data7 = addr - 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[++data7 & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >> 12) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  8) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  4) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  0) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data7 = addr + 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[--data7 & 0x7fff];

						// draw four pixels
						int pix;
						pix = (pixels >>  0) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  4) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >>  8) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;
						pix = (pixels >> 12) & 0xf; if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta;

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (x < minx) minx = x;
				if (y < miny) miny = y;
				maxy = y;
			}

			// advance a row - must be done at the end on the bootlegs!
			addr += pitch;
		}

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}



//****************************************************************************
//  SYSTEM 16B-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_sys16b_sprite_device -- constructor
//-------------------------------------------------

sega_sys16b_sprite_device::sega_sys16b_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, SEGA_SYS16B_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(184, 0x00, -184, 0);
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_sys16b_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  System 16B-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   bbbbbbbb --------  Bottom scanline of sprite - 1
	//       +0   -------- tttttttt  Top scanline of sprite - 1
	//       +2   -------x xxxxxxxx  X position of sprite (position $BD is screen position 0)
	//       +2   ---iiii- --------  Sprite/sprite priority for Y-board
	//       +4   e------- --------  Signify end of sprite list
	//       +4   -h------ --------  Hide this sprite
	//       +4   -------f --------  Horizontal flip: read the data backwards if set
	//       +4   -------- pppppppp  Signed 8-bit pitch value between scanlines
	//       +6   oooooooo oooooooo  Offset within selected sprite bank
	//       +8   ----bbbb --------  Sprite bank
	//       +8   -------- pp------  Sprite priority, relative to tilemaps
	//       +8   -------- --cccccc  Sprite color palette
	//       +A   ------vv vvv-----  Vertical zoom factor (0 = full size, 0x10 = half size)
	//       +A   -------- ---hhhhh  Horizontal zoom factor (0 = full size, 0x10 = half size)
	//       +E   dddddddd dddddddd  Scratch space for current address
	//
	//  Final bitmap format:
	//
	//            iiii---- --------  Sprite/sprite priority for Y-board
	//            ----pp-- --------  Sprite priority
	//            ------cc cccc----  Sprite color palette
	//            -------- ----llll  4-bit pixel data
	//
	//  Note that the zooming described below is 100% accurate to the real board.
	//

	// render the sprites in order
	const uint16_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x20000;
	uint16_t *ramend = spriteram() + spriteram_elements();
	for (uint16_t *data = spriteram(); data < ramend; data += 8)
	{
		// stop when we hit the end of sprite list
		if (data[2] & 0x8000)
			break;

		// extract parameters
		int bottom  = data[0] >> 8;
		int top     = data[0] & 0xff;
		int xpos    = data[1] & 0x1ff;
		int hide    = data[2] & 0x4000;
		int flip    = data[2] & 0x100;
		int pitch   = int8_t(data[2] & 0xff);
		uint16_t addr = data[3];
		int bank    = m_bank[(data[4] >> 8) & 0xf];
		int colpri  = ((data[4] & 0xff) << 4) | (((data[1] >> 9) & 0xf) << 12);
		int vzoom   = (data[5] >> 5) & 0x1f;
		int hzoom   = data[5] & 0x1f;
		const uint16_t *spritedata;

		xpos &= 0x1ff;

		// initialize the end address to the start address
		data[7] = addr;

		// if hidden, or top greater than/equal to bottom, or invalid bank, punt
		if (hide || top >= bottom || bank == 255)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		spritedata = spritebase + 0x10000 * bank;

		// reset the yzoom counter
		data[5] &= 0x03ff;

		// adjust positions for screen flipping
		int xdelta = 1;
		if (m_flip)
		{
			int temp = top;
			top = 224 - bottom;
			bottom = 224 - temp;
			xpos = 320 - xpos;
			xdelta = -1;
			set_origin(m_xoffs_flipped, m_yoffs_flipped);
		}
		else
		{
			set_origin(m_xoffs, m_yoffs);
		}

		// loop from top to bottom
		int minx = xpos;
		int maxx = xpos;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		for (int y = top; y < bottom; y++)
		{
			// advance a row
			addr += pitch;

			// accumulate zoom factors; if we carry into the high bit, skip an extra row
			data[5] += vzoom << 10;
			if (data[5] & 0x8000)
			{
				addr += pitch;
				data[5] &= ~0x8000;
			}

			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int x;

				// compute the initial X zoom accumulator; this is verified on the real PCB
				int xacc = 4 * hzoom;

				// non-flipped case
				if (!flip)
				{
					// start at the word before because we preincrement below
					data[7] = addr - 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[++data[7]];

						// draw four pixels
						int pix;
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data[7] = addr + 1;
					for (x = xpos; ((xpos - x) & 0x1ff) != 1; )
					{
						uint16_t pixels = spritedata[--data[7]];

						// draw four pixels
						int pix;
						pix = (pixels >>  0) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >>  4) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >>  8) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }
						pix = (pixels >> 12) & 0xf; xacc = (xacc & 0x3f) + hzoom; if (xacc < 0x40) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; }

						// stop if the last pixel in the group was 0xf
						if (pix == 15)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (x < minx) minx = x;
				if (y < miny) miny = y;
				maxy = y;
			}
		}

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}



//****************************************************************************
//  OUT RUN/X-BOARD-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_outrun_sprite_device -- constructor
//-------------------------------------------------

sega_outrun_sprite_device::sega_outrun_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_outrun_sprite_device(mconfig, SEGA_OUTRUN_SPRITES, tag, owner, clock, false)
{
}

sega_outrun_sprite_device::sega_outrun_sprite_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, bool xboard_variant)
	: sega_16bit_sprite_device(mconfig, type, tag, owner)
	, m_is_xboard(xboard_variant)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(xboard_variant ? 190 : 189, 0x00);
}


//-------------------------------------------------
//  sega_xboard_sprite_device -- constructor
//-------------------------------------------------

sega_xboard_sprite_device::sega_xboard_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_outrun_sprite_device(mconfig, SEGA_XBOARD_SPRITES, tag, owner, clock, true)
{
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_outrun_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  Out Run/X-Board-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   e------- --------  Signify end of sprite list
	//       +0   -h-h---- --------  Hide this sprite if either bit is set
	//       +0   ----bbb- --------  Sprite bank
	//       +0   -------t tttttttt  Top scanline of sprite + 256
	//       +2   oooooooo oooooooo  Offset within selected sprite bank
	//       +4   ppppppp- --------  Signed 7-bit pitch value between scanlines
	//       +4   -------x xxxxxxxx  X position of sprite (position $BE is screen position 0)
	//       +6   -s------ --------  Enable shadows
	//       +6   --pp---- --------  Sprite priority, relative to tilemaps
	//       +6   ------vv vvvvvvvv  Vertical zoom factor (0x200 = full size, 0x100 = half size, 0x300 = 2x size)
	//       +8   y------- --------  Render from top-to-bottom (1) or bottom-to-top (0) on screen
	//       +8   -f------ --------  Horizontal flip: read the data backwards if set
	//       +8   --x----- --------  Render from left-to-right (1) or right-to-left (0) on screen
	//       +8   ------hh hhhhhhhh  Horizontal zoom factor (0x200 = full size, 0x100 = half size, 0x300 = 2x size)
	//       +E   dddddddd dddddddd  Scratch space for current address
	//
	//  Out Run only:
	//       +A   hhhhhhhh --------  Height in scanlines - 1
	//       +A   -------- -ccccccc  Sprite color palette
	//
	//  X-Board only:
	//       +A   ----hhhh hhhhhhhh  Height in scanlines - 1
	//       +C   -------- cccccccc  Sprite color palette
	//
	//  Final bitmap format:
	//
	//            -s------ --------  Shadow control
	//            --pp---- --------  Sprite priority
	//            ----cccc cccc----  Sprite color palette
	//            -------- ----llll  4-bit pixel data
	//

	set_origin(m_xoffs, m_yoffs);

	// render the sprites in order
	const uint32_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x40000;
	uint16_t *ramend = buffer() + spriteram_elements();
	for (uint16_t *data = buffer(); data < ramend; data += 8)
	{
		// stop when we hit the end of sprite list
		if (data[0] & 0x8000)
			break;

		// extract parameters
		int hide    = (data[0] & 0x5000);
		int bank    = (data[0] >> 9) & 7;
		int top     = (data[0] & 0x1ff) - 0x100;
		uint16_t addr = data[1];
		int pitch   = int16_t((data[2] >> 1) | ((data[4] & 0x1000) << 3)) >> 8;
		int xpos    = data[2] & 0x1ff;
		int vzoom   = data[3] & 0x7ff;
		int ydelta  = (data[4] & 0x8000) ? 1 : -1;
		int flip    = (~data[4] >> 14) & 1;
		int xdelta  = (data[4] & 0x2000) ? 1 : -1;
		int hzoom   = data[4] & 0x7ff;
		int height  = (m_is_xboard ? (data[5] & 0xfff) : (data[5] >> 8)) + 1;
		int colpri  = ((m_is_xboard ? (data[6] & 0xff) : (data[5] & 0x7f)) << 4) | (((data[3] >> 12) & 7) << 12);

		// adjust X coordinate
		// note: the threshhold below is a guess. If it is too high, rachero will draw garbage
		// If it is too low, smgp won't draw the bottom part of the road
		if (xpos < 0x80 && xdelta < 0)
			xpos += 0x200;

		// initialize the end address to the start address
		data[7] = addr;

		// if hidden, punt
		if (hide)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint32_t *spritedata = spritebase + 0x10000 * bank;

		// clamp to a maximum of 8x (not 100% confirmed)
		if (vzoom < 0x40) vzoom = 0x40;
		if (hzoom < 0x40) hzoom = 0x40;

		// loop from top to bottom
		int minx = xpos;
		int maxx = xpos;
		int miny = cliprect.max_y + 1;
		int maxy = cliprect.min_y - 1;
		int yacc = 0;
		int ytarget = top + ydelta * height;
		for (int y = top; y != ytarget; y += ydelta)
		{
			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int xacc = 0;
				int x;

				// non-flipped case
				if (!flip)
				{
					// start at the word before because we preincrement below
					data[7] = addr - 1;
					for (x = xpos; (xdelta > 0 && x <= cliprect.max_x) || (xdelta < 0 && x >= cliprect.min_x); )
					{
						uint32_t pixels = spritedata[++data[7]];

						// draw four pixels
						int pix;
						pix = (pixels >> 28) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 24) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 20) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 16) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 12) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >>  8) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >>  4) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >>  0) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;

						// stop if the second-to-last pixel in the group was 0xf
						if ((pixels & 0x000000f0) == 0x000000f0)
							break;
					}
				}

				// flipped case
				else
				{
					// start at the word after because we predecrement below
					data[7] = addr + 1;
					for (x = xpos; (xdelta > 0 && x <= cliprect.max_x) || (xdelta < 0 && x >= cliprect.min_x); )
					{
						uint32_t pixels = spritedata[--data[7]];

						// draw four pixels
						int pix;
						pix = (pixels >>  0) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >>  4) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >>  8) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 12) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 16) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 20) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 24) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;
						pix = (pixels >> 28) & 0xf; while (xacc < 0x200) { if (x >= cliprect.min_x && x <= cliprect.max_x && pix != 0 && pix != 15) dest[x] = colpri | pix; x += xdelta; xacc += hzoom; } xacc -= 0x200;

						// stop if the second-to-last pixel in the group was 0xf
						if ((pixels & 0x0f000000) == 0x0f000000)
							break;
					}
				}

				// update bounds
				if (x > maxx) maxx = x;
				if (x < minx) minx = x;
				if (y < miny) miny = y;
				if (y > maxy) maxy = y;
			}

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

		// mark dirty
		if (minx <= maxx && miny <= maxy)
			mark_dirty(minx, maxx, miny, maxy);
	}
}



//****************************************************************************
//  Y BOARD-STYLE SPRITES
//****************************************************************************

//-------------------------------------------------
//  sega_yboard_sprite_device -- constructor
//-------------------------------------------------

sega_yboard_sprite_device::sega_yboard_sprite_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: sega_16bit_sprite_device(mconfig, SEGA_YBOARD_SPRITES, tag, owner)
	, m_sprite_region_ptr(*this, DEVICE_SELF)
{
	set_local_origin(0x600, 0x600);
}


//-------------------------------------------------
//  draw -- render the sprites within the cliprect
//-------------------------------------------------

void sega_yboard_sprite_device::draw(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	//
	//  Y-Board-style sprites
	//
	//      Offs  Bits               Usage
	//       +0   e------- --------  Signify end of sprite list
	//       +0   -----iii iiiiiiii  Address of indirection table (/16)
	//       +2   bbbb---- --------  Upper 4 bits of bank index
	//       +2   ----xxxx xxxxxxxx  X position of sprite (position $600 is screen position 0)
	//       +4   bbbb---- --------  Lower 4 bits of bank index
	//       +4   ----yyyy yyyyyyyy  Y position of sprite (position $600 is screen position 0)
	//       +6   oooooooo oooooooo  Offset within selected sprite bank
	//       +8   hhhhhhhh hhhhhhhh  Height of sprite
	//       +A   -y------ --------  Render from top-to-bottom (1) or bottom-to-top (0) on screen
	//       +A   --f----- --------  Horizontal flip: read the data backwards if set
	//       +A   ---x---- --------  Render from left-to-right (1) or right-to-left (0) on screen
	//       +A   -----zzz zzzzzzzz  Zoom factor
	//       +C   -ccc---- --------  Sprite color
	//       +C   ----rrrr --------  Sprite priority
	//       +C   -------- pppppppp  Signed 8-bit pitch value between scanlines
	//       +E   ----nnnn nnnnnnnn  Index of next sprite
	//
	//  Final bitmap format:
	//
	//            ccc----- --------  Sprite color
	//            ---rrrr- --------  Sprite priority
	//            -------i iiiiiiii  Indirected color data
	//
	//  In addition to these parameters, the sprite area is clipped using scanline extents
	//  stored for every pair of scanlines in the rotation RAM. It's a bit of a cheat for us
	//  to poke our nose into the rotation structure, but there are no known cases of Y-board
	//  sprites without rotation RAM.
	//

	set_origin(m_xoffs, m_yoffs);

	// clear out any scanlines we might be using
	const uint16_t *rotatebase = m_segaic16_rotate[0].buffer ? m_segaic16_rotate[0].buffer.get() : m_segaic16_rotate[0].rotateram;
	rotatebase -= yorigin();
	for (int y = cliprect.min_y; y <= cliprect.max_y; y++)
		if (!(rotatebase[y & ~1] & 0xc000))
			memset(&bitmap.pix(y, cliprect.min_x), 0xff, cliprect.width() * sizeof(uint16_t));

	// reset the visited list
	uint8_t visited[0x1000];
	memset(visited, 0, sizeof(visited));

	// render the sprites in order
	const uint64_t *spritebase = &m_sprite_region_ptr[0];
	uint8_t numbanks = m_sprite_region_ptr.bytes() / 0x80000;
	int next = 0;
	for (uint16_t *data = spriteram(); !(data[0] & 0x8000) && !visited[next]; data = spriteram() + next * 8)
	{
		int hide    = (data[0] & 0x5000);
		const uint16_t *indirect = spriteram() + ((data[0] & 0x7ff) << 4);
		int bank    = ((data[1] >> 8) & 0x10) | ((data[2] >> 12) & 0x0f);
		int xpos    = data[1] & 0xfff;
		int top     = data[2] & 0xfff;
		uint16_t addr = data[3];
		int height  = data[4];
		int ydelta  = (data[5] & 0x4000) ? 1 : -1;
		int flip    = (~data[5] >> 13) & 1;
		int xdelta  = (data[5] & 0x1000) ? 1 : -1;
		int zoom    = data[5] & 0x7ff;
		int colpri  = (data[6] << 1) & 0xfe00;
		int pitch   = int8_t(data[6]);

		// note that we've visited this entry and get the offset of the next one
		visited[next] = 1;
		next = data[7] & 0xfff;

		// if hidden, or invalid height, punt
		if (hide || height == 0)
			continue;

		// clamp to within the memory region size
		if (numbanks)
			bank %= numbanks;
		const uint64_t *spritedata = spritebase + 0x10000 * bank;

		// clamp to a maximum of 8x (not 100% confirmed)
		if (zoom == 0) zoom = 1;

		// loop from top to bottom
		int dminx = xpos;
		int dmaxx = xpos;
		int dminy = cliprect.max_y + 1;
		int dmaxy = cliprect.min_y - 1;
		int ytarget = top + ydelta * height;
		int yacc = 0;
		for (int y = top; y != ytarget; y += ydelta)
		{
			// skip drawing if not within the cliprect
			if (y >= cliprect.min_y && y <= cliprect.max_y)
			{
				uint16_t *dest = &bitmap.pix(y);
				int minx = rotatebase[y & ~1];
				int maxx = rotatebase[y |  1];
				int xacc = 0;

				// bit 0x8000 from rotate RAM means that Y is above the top of the screen
				if ((minx & 0x8000) && ydelta < 0)
					break;

				// bit 0x4000 from rotate RAM means that Y is below the bottom of the screen
				if ((minx & 0x4000) && ydelta > 0)
					break;

				// if either bit is set, skip the rest for this scanline
				if (!(minx & 0xc000))
				{
					// clamp min/max to the cliprect
					if (minx < cliprect.min_x)
						minx = cliprect.min_x;
					if (maxx > cliprect.max_x)
						maxx = cliprect.max_x;

					// non-flipped case
					int x;
					if (!flip)
					{
						// start at the word before because we preincrement below
						uint16_t offs = addr - 1;
						for (x = xpos; (xdelta > 0 && x <= maxx) || (xdelta < 0 && x >= minx); )
						{
							uint64_t pixels = spritedata[++offs];

							// draw 16 pixels
							int pix, ind;
							pix = (pixels >> 60) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 56) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 52) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 48) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 44) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 40) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 36) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 32) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 28) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 24) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 20) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 16) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 12) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >>  8) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >>  4) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >>  0) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;

							// stop if the last pixel in the group was 0xf
							if (pix == 0x0f)
								break;
						}
					}

					// flipped case
					else
					{
						// start at the word after because we predecrement below
						uint16_t offs = addr + 1;
						for (x = xpos; (xdelta > 0 && x <= maxx) || (xdelta < 0 && x >= minx); )
						{
							uint64_t pixels = spritedata[--offs];

							// draw 16 pixels
							int pix, ind;
							pix = (pixels >>  0) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >>  4) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >>  8) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 12) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 16) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 20) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 24) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 28) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 32) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 36) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 40) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 44) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 48) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 52) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 56) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;
							pix = (pixels >> 60) & 0xf; ind = indirect[pix]; while (xacc < 0x200) { if (x >= minx && x <= maxx && ind < 0x1fe) dest[x] = colpri | ind; x += xdelta; xacc += zoom; } xacc -= 0x200;

							// stop if the last pixel in the group was 0xf
							if (pix == 0x0f)
								break;
						}
					}

					// update bounds
					if (x > dmaxx) dmaxx = x;
					if (x < dminx) dminx = x;
					if (y < dminy) dminy = y;
					if (y > dmaxy) dmaxy = y;
				}
			}

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

		// mark dirty
		if (dminx <= dmaxx && dminy <= dmaxy)
			mark_dirty(dminx, dmaxx, dminy, dmaxy);
	}
}