1 /***************************************************************************
2 
3   Video Hardware for Irem Games:
4   Battle Road, Lode Runner, Kid Niki, Spelunker
5 
6 ***************************************************************************/
7 
8 #include "driver.h"
9 #include "vidhrdw/generic.h"
10 
11 
12 
13 static int flipscreen;
14 static const unsigned char *sprite_height_prom;
15 static int kidniki_background_bank;
16 static int irem_background_hscroll;
17 static int irem_background_vscroll;
18 static int kidniki_text_vscroll;
19 static int spelunk2_palbank;
20 
21 unsigned char *irem_textram;
22 size_t irem_textram_size;
23 
24 
25 static const struct rectangle kungfum_spritevisiblearea =
26 {
27 	16*8, (64-16)*8-1,
28 	10*8, 32*8-1
29 };
30 static const struct rectangle kungfum_flipspritevisiblearea =
31 {
32 	16*8, (64-16)*8-1,
33 	0*8, 22*8-1
34 };
35 
36 /***************************************************************************
37 
38   Convert the color PROMs into a more useable format.
39 
40   Kung Fu Master has a six 256x4 palette PROMs (one per gun; three for
41   characters, three for sprites).
42   I don't know the exact values of the resistors between the RAM and the
43   RGB output. I assumed these values (the same as Commando)
44 
45   bit 3 -- 220 ohm resistor  -- RED/GREEN/BLUE
46         -- 470 ohm resistor  -- RED/GREEN/BLUE
47         -- 1  kohm resistor  -- RED/GREEN/BLUE
48   bit 0 -- 2.2kohm resistor  -- RED/GREEN/BLUE
49 
50 ***************************************************************************/
irem_vh_convert_color_prom(unsigned char * palette,unsigned short * colortable,const unsigned char * color_prom)51 void irem_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
52 {
53 	int i;
54 
55 
56 	for (i = 0;i < Machine->drv->total_colors;i++)
57 	{
58 		int bit0,bit1,bit2,bit3;
59 
60 		/* red component */
61 		bit0 = (color_prom[0] >> 0) & 0x01;
62 		bit1 = (color_prom[0] >> 1) & 0x01;
63 		bit2 = (color_prom[0] >> 2) & 0x01;
64 		bit3 = (color_prom[0] >> 3) & 0x01;
65 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
66 		/* green component */
67 		bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
68 		bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
69 		bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
70 		bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
71 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
72 		/* blue component */
73 		bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
74 		bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
75 		bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
76 		bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
77 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
78 
79 		color_prom++;
80 	}
81 
82 	color_prom += 2*Machine->drv->total_colors;
83 	/* color_prom now points to the beginning of the sprite height table */
84 
85 	sprite_height_prom = color_prom;	/* we'll need this at run time */
86 }
87 
battroad_vh_convert_color_prom(unsigned char * palette,unsigned short * colortable,const unsigned char * color_prom)88 void battroad_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
89 {
90 	int i;
91 
92 
93 	for (i = 0;i < 512;i++)
94 	{
95 		int bit0,bit1,bit2,bit3;
96 
97 		/* red component */
98 		bit0 = (color_prom[0] >> 0) & 0x01;
99 		bit1 = (color_prom[0] >> 1) & 0x01;
100 		bit2 = (color_prom[0] >> 2) & 0x01;
101 		bit3 = (color_prom[0] >> 3) & 0x01;
102 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
103 		/* green component */
104 		bit0 = (color_prom[512] >> 0) & 0x01;
105 		bit1 = (color_prom[512] >> 1) & 0x01;
106 		bit2 = (color_prom[512] >> 2) & 0x01;
107 		bit3 = (color_prom[512] >> 3) & 0x01;
108 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
109 		/* blue component */
110 		bit0 = (color_prom[2*512] >> 0) & 0x01;
111 		bit1 = (color_prom[2*512] >> 1) & 0x01;
112 		bit2 = (color_prom[2*512] >> 2) & 0x01;
113 		bit3 = (color_prom[2*512] >> 3) & 0x01;
114 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
115 
116 		color_prom++;
117 	}
118 
119 	color_prom += 2*512;
120 	/* color_prom now points to the beginning of the character color prom */
121 
122 	for (i = 0;i < 32;i++)
123 	{
124 		int bit0,bit1,bit2;
125 
126 
127 		bit0 = (color_prom[i] >> 0) & 0x01;
128 		bit1 = (color_prom[i] >> 1) & 0x01;
129 		bit2 = (color_prom[i] >> 2) & 0x01;
130 		*(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
131 		bit0 = (color_prom[i] >> 3) & 0x01;
132 		bit1 = (color_prom[i] >> 4) & 0x01;
133 		bit2 = (color_prom[i] >> 5) & 0x01;
134 		*(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
135 		bit0 = 0;
136 		bit1 = (color_prom[i] >> 6) & 0x01;
137 		bit2 = (color_prom[i] >> 7) & 0x01;
138 		*(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
139 
140 	}
141 
142 	color_prom += 32;
143 	/* color_prom now points to the beginning of the sprite height table */
144 
145 	sprite_height_prom = color_prom;	/* we'll need this at run time */
146 }
147 
spelunk2_vh_convert_color_prom(unsigned char * palette,unsigned short * colortable,const unsigned char * color_prom)148 void spelunk2_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
149 {
150 	int i;
151 
152 
153 	/* chars */
154 	for (i = 0;i < 512;i++)
155 	{
156 		int bit0,bit1,bit2,bit3;
157 
158 		/* red component */
159 		bit0 = (color_prom[0] >> 0) & 0x01;
160 		bit1 = (color_prom[0] >> 1) & 0x01;
161 		bit2 = (color_prom[0] >> 2) & 0x01;
162 		bit3 = (color_prom[0] >> 3) & 0x01;
163 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
164 		/* green component */
165 		bit0 = (color_prom[0] >> 4) & 0x01;
166 		bit1 = (color_prom[0] >> 5) & 0x01;
167 		bit2 = (color_prom[0] >> 6) & 0x01;
168 		bit3 = (color_prom[0] >> 7) & 0x01;
169 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
170 		/* blue component */
171 		bit0 = (color_prom[2*256] >> 0) & 0x01;
172 		bit1 = (color_prom[2*256] >> 1) & 0x01;
173 		bit2 = (color_prom[2*256] >> 2) & 0x01;
174 		bit3 = (color_prom[2*256] >> 3) & 0x01;
175 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
176 
177 		color_prom++;
178 	}
179 
180 	color_prom += 2*256;
181 
182 	/* sprites */
183 	for (i = 0;i < 256;i++)
184 	{
185 		int bit0,bit1,bit2,bit3;
186 
187 		/* red component */
188 		bit0 = (color_prom[0] >> 0) & 0x01;
189 		bit1 = (color_prom[0] >> 1) & 0x01;
190 		bit2 = (color_prom[0] >> 2) & 0x01;
191 		bit3 = (color_prom[0] >> 3) & 0x01;
192 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
193 		/* green component */
194 		bit0 = (color_prom[256] >> 0) & 0x01;
195 		bit1 = (color_prom[256] >> 1) & 0x01;
196 		bit2 = (color_prom[256] >> 2) & 0x01;
197 		bit3 = (color_prom[256] >> 3) & 0x01;
198 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
199 		/* blue component */
200 		bit0 = (color_prom[2*256] >> 0) & 0x01;
201 		bit1 = (color_prom[2*256] >> 1) & 0x01;
202 		bit2 = (color_prom[2*256] >> 2) & 0x01;
203 		bit3 = (color_prom[2*256] >> 3) & 0x01;
204 		*(palette++) =  0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
205 
206 		color_prom++;
207 	}
208 
209 	color_prom += 2*256;
210 
211 
212 	/* color_prom now points to the beginning of the sprite height table */
213 	sprite_height_prom = color_prom;	/* we'll need this at run time */
214 }
215 
216 
217 
ldrun_vh_start(void)218 int ldrun_vh_start(void)
219 {
220 	irem_background_hscroll = 0;
221 	irem_background_vscroll = 0;
222 	return generic_vh_start();
223 }
224 
225 
irem_vh_start(int width,int height)226 static int irem_vh_start( int width, int height )
227 {
228 	irem_background_hscroll = 0;
229 	irem_background_vscroll = 0;
230 
231 	if ((dirtybuffer = (unsigned char*)malloc(videoram_size)) == 0)
232 		return 1;
233 	memset(dirtybuffer,1,videoram_size);
234 
235 	if ((tmpbitmap = bitmap_alloc(width,height)) == 0)
236 	{
237 		free(dirtybuffer);
238 		return 1;
239 	}
240 
241 	return 0;
242 }
243 
kidniki_vh_start(void)244 int kidniki_vh_start(void)
245 {
246 	return irem_vh_start(512,256);
247 }
248 
spelunkr_vh_start(void)249 int spelunkr_vh_start(void)
250 {
251 	return irem_vh_start(512,512);
252 }
253 
254 
255 
WRITE_HANDLER(irem_flipscreen_w)256 WRITE_HANDLER( irem_flipscreen_w )
257 {
258 	/* screen flip is handled both by software and hardware */
259 	data ^= ~readinputport(4) & 1;
260 
261 	if (flipscreen != (data & 1))
262 	{
263 		flipscreen = data & 1;
264 		memset(dirtybuffer,1,videoram_size);
265 	}
266 
267 	coin_counter_w(0,data & 2);
268 	coin_counter_w(1,data & 4);
269 }
270 
271 
WRITE_HANDLER(irem_background_hscroll_w)272 WRITE_HANDLER( irem_background_hscroll_w )
273 {
274 	switch(offset)
275 	{
276 		case 0:
277 			irem_background_hscroll = (irem_background_hscroll&0xff00)|data;
278 			break;
279 
280 		case 1:
281 			irem_background_hscroll = (irem_background_hscroll&0xff)|(data<<8);
282 			break;
283 	}
284 }
285 
WRITE_HANDLER(kungfum_scroll_low_w)286 WRITE_HANDLER( kungfum_scroll_low_w )
287 {
288 	irem_background_hscroll_w(0,data);
289 }
WRITE_HANDLER(kungfum_scroll_high_w)290 WRITE_HANDLER( kungfum_scroll_high_w )
291 {
292 	irem_background_hscroll_w(1,data);
293 }
294 
WRITE_HANDLER(irem_background_vscroll_w)295 WRITE_HANDLER( irem_background_vscroll_w ){
296 	switch( offset ){
297 		case 0:
298 		irem_background_vscroll = (irem_background_vscroll&0xff00)|data;
299 		break;
300 
301 		case 1:
302 		irem_background_vscroll = (irem_background_vscroll&0xff)|(data<<8);
303 		break;
304 	}
305 }
306 
WRITE_HANDLER(battroad_scroll_w)307 WRITE_HANDLER( battroad_scroll_w ){
308 	switch( offset ){
309 		case 0:
310 		irem_background_vscroll_w(0, data);
311 		break;
312 
313 		case 1:
314 		irem_background_hscroll_w(1, data);
315 		break;
316 
317 		case 2:
318 		irem_background_hscroll_w(0, data);
319 		break;
320 	}
321 }
322 
WRITE_HANDLER(ldrun3_vscroll_w)323 WRITE_HANDLER( ldrun3_vscroll_w )
324 {
325 	irem_background_vscroll = data;
326 }
327 
WRITE_HANDLER(ldrun4_hscroll_w)328 WRITE_HANDLER( ldrun4_hscroll_w )
329 {
330 	irem_background_hscroll_w(offset ^ 1,data);
331 }
332 
WRITE_HANDLER(kidniki_text_vscroll_w)333 WRITE_HANDLER( kidniki_text_vscroll_w )
334 {
335 	switch (offset)
336 	{
337 		case 0:
338 		kidniki_text_vscroll = (kidniki_text_vscroll & 0xff00) | data;
339 		break;
340 
341 		case 1:
342 		kidniki_text_vscroll = (kidniki_text_vscroll & 0xff) | (data << 8);
343 		break;
344 	}
345 }
346 
WRITE_HANDLER(kidniki_background_bank_w)347 WRITE_HANDLER( kidniki_background_bank_w )
348 {
349 	if (kidniki_background_bank != (data & 1))
350 	{
351 		kidniki_background_bank = data & 1;
352 		memset(dirtybuffer,1,videoram_size);
353 	}
354 }
355 
WRITE_HANDLER(spelunkr_palbank_w)356 WRITE_HANDLER( spelunkr_palbank_w )
357 {
358 	if (spelunk2_palbank != (data & 0x01))
359 	{
360 		spelunk2_palbank = data & 0x01;
361 		memset(dirtybuffer,1,videoram_size);
362 	}
363 }
364 
WRITE_HANDLER(spelunk2_gfxport_w)365 WRITE_HANDLER( spelunk2_gfxport_w )
366 {
367 	switch( offset )
368 	{
369 		case 0:
370 		irem_background_vscroll_w(0,data);
371 		break;
372 
373 		case 1:
374 		irem_background_hscroll_w(0,data);
375 		break;
376 
377 		case 2:
378 		irem_background_hscroll_w(1,(data&2)>>1);
379 		irem_background_vscroll_w(1,(data&1));
380 		if (spelunk2_palbank != ((data & 0x0c) >> 2))
381 		{
382 			spelunk2_palbank = (data & 0x0c) >> 2;
383 			memset(dirtybuffer,1,videoram_size);
384 		}
385 		break;
386 	}
387 }
388 
389 
390 
391 /***************************************************************************
392 
393   Draw the game screen in the given osd_bitmap.
394   Do NOT call osd_update_display() from this function, it will be called by
395   the main emulation engine.
396 
397 ***************************************************************************/
draw_sprites(struct osd_bitmap * bitmap,const struct rectangle * spritevisiblearea,const struct rectangle * flipspritevisiblearea)398 static void draw_sprites(struct osd_bitmap *bitmap,
399 					     const struct rectangle *spritevisiblearea,
400 					     const struct rectangle *flipspritevisiblearea )
401 {
402 	int offs;
403 
404 	/* sprites must be drawn in this order to get correct priority */
405 	for (offs = 0;offs < spriteram_size;offs += 8)
406 	{
407 		int i,incr,code,col,flipx,flipy,sx,sy;
408 
409 
410 		code = spriteram[offs+4] + ((spriteram[offs+5] & 0x07) << 8);
411 		col = spriteram[offs+0] & 0x1f;
412 		sx = 256 * (spriteram[offs+7] & 1) + spriteram[offs+6],
413 		sy = 256+128-15 - (256 * (spriteram[offs+3] & 1) + spriteram[offs+2]),
414 		flipx = spriteram[offs+5] & 0x40;
415 		flipy = spriteram[offs+5] & 0x80;
416 
417 		i = sprite_height_prom[(code >> 5) & 0x1f];
418 		if (i == 1)	/* double height */
419 		{
420 			code &= ~1;
421 			sy -= 16;
422 		}
423 		else if (i == 2)	/* quadruple height */
424 		{
425 			i = 3;
426 			code &= ~3;
427 			sy -= 3*16;
428 		}
429 
430 		if (flipscreen)
431 		{
432 			sx = 496 - sx;
433 			sy = 242 - i*16 - sy;	/* sprites are slightly misplaced by the hardware */
434 			flipx = !flipx;
435 			flipy = !flipy;
436 		}
437 
438 		if (flipy)
439 		{
440 			incr = -1;
441 			code += i;
442 		}
443 		else incr = 1;
444 
445 		do
446 		{
447 			drawgfx(bitmap,Machine->gfx[1],
448 					code + i * incr,col,
449 					flipx,flipy,
450 					sx,sy + 16 * i,
451 					flipscreen ? flipspritevisiblearea : spritevisiblearea,TRANSPARENCY_PEN,0);
452 
453 			i--;
454 		} while (i >= 0);
455 	}
456 }
457 
458 
draw_priority_sprites(struct osd_bitmap * bitmap,int prioritylayer)459 static void draw_priority_sprites(struct osd_bitmap *bitmap, int prioritylayer)
460 {
461 	int offs;
462 
463 	/* sprites must be drawn in this order to get correct priority */
464 	for (offs = 0;offs < spriteram_size;offs += 8)
465 	{
466 		int i,incr,code,col,flipx,flipy,sx,sy;
467 
468 
469 		if (!prioritylayer || (prioritylayer && (spriteram[offs] & 0x10)))
470 		{
471 			code = spriteram[offs+4] + ((spriteram[offs+5] & 0x07) << 8);
472 			col = spriteram[offs+0] & 0x0f;
473 			sx = 256 * (spriteram[offs+7] & 1) + spriteram[offs+6],
474 			sy = 256+128-15 - (256 * (spriteram[offs+3] & 1) + spriteram[offs+2]),
475 			flipx = spriteram[offs+5] & 0x40;
476 			flipy = spriteram[offs+5] & 0x80;
477 
478 			i = sprite_height_prom[(code >> 5) & 0x1f];
479 			if (i == 1)	/* double height */
480 			{
481 				code &= ~1;
482 				sy -= 16;
483 			}
484 			else if (i == 2)	/* quadruple height */
485 			{
486 				i = 3;
487 				code &= ~3;
488 				sy -= 3*16;
489 			}
490 
491 			if (flipscreen)
492 			{
493 				sx = 496 - sx;
494 				sy = 242 - i*16 - sy;	/* sprites are slightly misplaced by the hardware */
495 				flipx = !flipx;
496 				flipy = !flipy;
497 			}
498 
499 			if (flipy)
500 			{
501 				incr = -1;
502 				code += i;
503 			}
504 			else incr = 1;
505 
506 			do
507 			{
508 				drawgfx(bitmap,Machine->gfx[1],
509 						code + i * incr,col,
510 						flipx,flipy,
511 						sx,sy + 16 * i,
512 						&Machine->visible_area,TRANSPARENCY_PEN,0);
513 
514 				i--;
515 			} while (i >= 0);
516 		}
517 	}
518 }
519 
520 
kungfum_draw_background(struct osd_bitmap * bitmap)521 void kungfum_draw_background(struct osd_bitmap *bitmap)
522 {
523 	int offs,i;
524 
525 
526 	/* for every character in the Video RAM, check if it has been modified */
527 	/* since last time and update it accordingly. */
528 	for (offs = videoram_size/2 - 1;offs >= 0;offs--)
529 	{
530 		if (dirtybuffer[offs] || dirtybuffer[offs+0x800])
531 		{
532 			int sx,sy,flipx,flipy;
533 
534 
535 			dirtybuffer[offs] = dirtybuffer[offs+0x800] = 0;
536 
537 			sx = offs % 64;
538 			sy = offs / 64;
539 			flipx = videoram[offs+0x800] & 0x20;
540 			flipy = 0;
541 			if (flipscreen)
542 			{
543 				sx = 63 - sx;
544 				sy = 31 - sy;
545 				flipx = !flipx;
546 				flipy = !flipy;
547 			}
548 
549 			drawgfx(tmpbitmap,Machine->gfx[0],
550 					videoram[offs] + 4 * (videoram[offs+0x800] & 0xc0),
551 					videoram[offs+0x800] & 0x1f,
552 					flipx,flipy,
553 					8*sx,8*sy,
554 					0,TRANSPARENCY_NONE,0);
555 		}
556 	}
557 
558 
559 	/* copy the temporary bitmap to the screen */
560 	{
561 		int scrollx[32];
562 
563 
564 		if (flipscreen)
565 		{
566 			for (i = 31;i > 25;i--)
567 				scrollx[i] = 0;
568 			for (i = 25;i >= 0;i--)
569 				scrollx[i] = irem_background_hscroll;
570 		}
571 		else
572 		{
573 			for (i = 0;i < 6;i++)
574 				scrollx[i] = 0;
575 			for (i = 6;i < 32;i++)
576 				scrollx[i] = -irem_background_hscroll;
577 		}
578 
579 		copyscrollbitmap(bitmap,tmpbitmap,32,scrollx,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
580 	}
581 }
582 
battroad_draw_background(struct osd_bitmap * bitmap,int prioritylayer)583 static void battroad_draw_background(struct osd_bitmap *bitmap, int prioritylayer)
584 {
585 	int offs;
586 
587 
588 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
589 	{
590 		if ((dirtybuffer[offs] || dirtybuffer[offs+1]) && !(!prioritylayer && (videoram[offs+1] & 0x04)))
591 		{
592 			int sx,sy;
593 
594 
595 			dirtybuffer[offs] = 0;
596 			dirtybuffer[offs+1] = 0;
597 
598 			sx = (offs/2) % 64;
599 			sy = (offs/2) / 64;
600 
601 			if (flipscreen)
602 			{
603 				sx = 63 - sx;
604 				sy = 31 - sy;
605 			}
606 
607 			drawgfx(tmpbitmap,Machine->gfx[0],
608 					videoram[offs] + ((videoram[offs+1] & 0x40) << 3) + ((videoram[offs + 1] & 0x10) << 4),
609 					videoram[offs+1] & 0x0f,
610 					flipscreen,flipscreen,
611 					8*sx,8*sy,
612 					0,TRANSPARENCY_NONE,0);
613 		}
614 	}
615 
616 
617 	{
618 		int scrollx, scrolly;
619 
620 		if (flipscreen)
621 		{
622 			scrollx = irem_background_hscroll;
623 			scrolly = irem_background_vscroll;
624 		}
625 		else
626 		{
627 			scrollx = -irem_background_hscroll;
628 			scrolly = -irem_background_vscroll;
629 		}
630 
631 		if (prioritylayer)
632 		{
633 			copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_PEN,Machine->pens[0]);
634 		}
635 		else
636 		{
637 			copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
638 		}
639 	}
640 }
641 
ldrun_draw_background(struct osd_bitmap * bitmap,int prioritylayer)642 void ldrun_draw_background(struct osd_bitmap *bitmap, int prioritylayer)
643 {
644 	int offs;
645 
646 
647 	/* for every character in the Video RAM, check if it has been modified */
648 	/* since last time and update it accordingly. */
649 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
650 	{
651 		if ((dirtybuffer[offs] || dirtybuffer[offs+1]) && !(!prioritylayer && (videoram[offs+1] & 0x04)))
652 		{
653 			int sx,sy,flipx;
654 
655 
656 			dirtybuffer[offs] = 0;
657 			dirtybuffer[offs+1] = 0;
658 
659 			sx = (offs/2) % 64;
660 			sy = (offs/2) / 64;
661 			flipx = videoram[offs+1] & 0x20;
662 
663 			if (flipscreen)
664 			{
665 				sx = 63 - sx;
666 				sy = 31 - sy;
667 				flipx = !flipx;
668 			}
669 
670 			drawgfx(tmpbitmap,Machine->gfx[0],
671 					videoram[offs] + ((videoram[offs+1] & 0xc0) << 2),
672 					videoram[offs+1] & 0x1f,
673 					flipx,flipscreen,
674 					8*sx,8*sy,
675 					0,TRANSPARENCY_NONE,0);
676 		}
677 	}
678 
679 
680 	{
681 		int scrolly;	/* ldrun3 only */
682 
683 		if (flipscreen)
684 			scrolly = irem_background_vscroll;
685 		else
686 			scrolly = -irem_background_vscroll;
687 
688 		if (prioritylayer)
689 		{
690 			copyscrollbitmap(bitmap,tmpbitmap,0,0,1,&scrolly,&Machine->visible_area,TRANSPARENCY_PEN,Machine->pens[0]);
691 		}
692 		else
693 		{
694 			copyscrollbitmap(bitmap,tmpbitmap,0,0,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
695 		}
696 	}
697 }
698 
699 /* almost identical but scrolling background, more characters, */
700 /* no char x flip, and more sprites */
ldrun4_draw_background(struct osd_bitmap * bitmap)701 void ldrun4_draw_background(struct osd_bitmap *bitmap)
702 {
703 	int offs;
704 
705 
706 	/* for every character in the Video RAM, check if it has been modified */
707 	/* since last time and update it accordingly. */
708 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
709 	{
710 		if (dirtybuffer[offs] || dirtybuffer[offs+1])
711 		{
712 			int sx,sy;
713 
714 
715 			dirtybuffer[offs] = 0;
716 			dirtybuffer[offs+1] = 0;
717 
718 			sx = (offs/2) % 64;
719 			sy = (offs/2) / 64;
720 
721 			if (flipscreen)
722 			{
723 				sx = 63 - sx;
724 				sy = 31 - sy;
725 			}
726 
727 			drawgfx(tmpbitmap,Machine->gfx[0],
728 					videoram[offs] + ((videoram[offs+1] & 0xc0) << 2) + ((videoram[offs+1] & 0x20) << 5),
729 					videoram[offs+1] & 0x1f,
730 					flipscreen,flipscreen,
731 					8*sx,8*sy,
732 					0,TRANSPARENCY_NONE,0);
733 		}
734 	}
735 
736 
737 	{
738 		int scrollx;
739 
740 		if (flipscreen)
741 			scrollx = irem_background_hscroll + 2;
742 		else
743 			scrollx = -irem_background_hscroll + 2;
744 
745 		copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
746 	}
747 }
748 
lotlot_draw_background(struct osd_bitmap * bitmap)749 void lotlot_draw_background(struct osd_bitmap *bitmap)
750 {
751 	int offs;
752 
753 
754 	/* for every character in the Video RAM, check if it has been modified */
755 	/* since last time and update it accordingly. */
756 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
757 	{
758 		if (dirtybuffer[offs] || dirtybuffer[offs+1])
759 		{
760 			int sx,sy,flipx;
761 
762 
763 			dirtybuffer[offs] = 0;
764 			dirtybuffer[offs+1] = 0;
765 
766 			sx = (offs/2) % 32;
767 			sy = (offs/2) / 32;
768 			flipx = videoram[offs+1] & 0x20;
769 
770 			if (flipscreen)
771 			{
772 				sx = 31 - sx;
773 				sy = 31 - sy;
774 				flipx = !flipx;
775 			}
776 
777 			drawgfx(tmpbitmap,Machine->gfx[0],
778 					videoram[offs] + ((videoram[offs+1] & 0xc0) << 2),
779 					videoram[offs+1] & 0x1f,
780 					flipx,flipscreen,
781 					12*sx + 64,10*sy - 32,
782 					0,TRANSPARENCY_NONE,0);
783 		}
784 	}
785 
786 
787 	copybitmap(bitmap,tmpbitmap,0,0,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
788 
789 
790 	for (offs = irem_textram_size - 2;offs >= 0;offs -= 2)
791 	{
792 		int sx,sy;
793 
794 
795 		sx = (offs/2) % 32;
796 		sy = (offs/2) / 32;
797 
798 		if (flipscreen)
799 		{
800 			sx = 31 - sx;
801 			sy = 31 - sy;
802 		}
803 
804 		drawgfx(bitmap,Machine->gfx[2],
805 				irem_textram[offs] + ((irem_textram[offs + 1] & 0xc0) << 2),
806 				(irem_textram[offs + 1] & 0x1f),
807 				flipscreen,flipscreen,
808 				12*sx + 64,10*sy - 32,
809 				&Machine->visible_area,TRANSPARENCY_PEN, 0);
810 	}
811 }
812 
kidniki_draw_background(struct osd_bitmap * bitmap)813 static void kidniki_draw_background(struct osd_bitmap *bitmap)
814 {
815 	int offs;
816 
817 
818 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
819 	{
820 		if (dirtybuffer[offs] || dirtybuffer[offs+1])
821 		{
822 			int sx,sy;
823 
824 
825 			dirtybuffer[offs] = 0;
826 			dirtybuffer[offs+1] = 0;
827 
828 			sx = (offs/2) % 64;
829 			sy = (offs/2) / 64;
830 
831 			if (flipscreen)
832 			{
833 				sx = 63 - sx;
834 				sy = 31 - sy;
835 			}
836 
837 			drawgfx(tmpbitmap,Machine->gfx[0],
838 					videoram[offs] + ((videoram[offs+1] & 0xe0) << 3) + (kidniki_background_bank << 11),
839 					videoram[offs+1] & 0x1f,
840 					flipscreen,flipscreen,
841 					8*sx,8*sy,
842 					0,TRANSPARENCY_NONE,0);
843 		}
844 	}
845 
846 
847 	{
848 		int scrollx;
849 
850 		if (flipscreen)
851 			scrollx = irem_background_hscroll + 2;
852 		else
853 			scrollx = -irem_background_hscroll + 2;
854 
855 		copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,0,0,&Machine->visible_area,TRANSPARENCY_NONE,0);
856 	}
857 }
858 
spelunkr_draw_background(struct osd_bitmap * bitmap)859 static void spelunkr_draw_background(struct osd_bitmap *bitmap)
860 {
861 	int offs;
862 
863 
864 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
865 	{
866 		if (dirtybuffer[offs] || dirtybuffer[offs+1])
867 		{
868 			int sx,sy;
869 
870 
871 			dirtybuffer[offs] = 0;
872 			dirtybuffer[offs+1] = 0;
873 
874 			sx = (offs/2) % 64;
875 			sy = (offs/2) / 64;
876 
877 			if (flipscreen)
878 			{
879 				sx = 63 - sx;
880 				sy = 63 - sy;
881 			}
882 
883 			drawgfx(tmpbitmap,Machine->gfx[0],
884 					videoram[offs]
885 							+ ((videoram[offs+1] & 0x10) << 4)
886 							+ ((videoram[offs+1] & 0x20) << 6)
887 							+ ((videoram[offs+1] & 0xc0) << 3),
888 					(videoram[offs+1] & 0x0f) + (spelunk2_palbank << 4),
889 					flipscreen,flipscreen,
890 					8*sx,8*sy,
891 					0,TRANSPARENCY_NONE,0);
892 		}
893 	}
894 
895 
896 	{
897 		int scrollx,scrolly;
898 
899 		if (flipscreen)
900 		{
901 			scrollx = irem_background_hscroll;
902 			scrolly = irem_background_vscroll - 128;
903 		}
904 		else
905 		{
906 			scrollx = -irem_background_hscroll;
907 			scrolly = -irem_background_vscroll - 128;
908 		}
909 
910 		copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
911 	}
912 }
913 
spelunk2_draw_background(struct osd_bitmap * bitmap)914 static void spelunk2_draw_background(struct osd_bitmap *bitmap)
915 {
916 	int offs;
917 
918 
919 	for (offs = videoram_size-2;offs >= 0;offs -= 2)
920 	{
921 		if (dirtybuffer[offs] || dirtybuffer[offs+1])
922 		{
923 			int sx,sy;
924 
925 
926 			dirtybuffer[offs] = 0;
927 			dirtybuffer[offs+1] = 0;
928 
929 			sx = (offs/2) % 64;
930 			sy = (offs/2) / 64;
931 
932 			if (flipscreen)
933 			{
934 				sx = 63 - sx;
935 				sy = 63 - sy;
936 			}
937 
938 			drawgfx(tmpbitmap,Machine->gfx[0],
939 					videoram[offs] + ((videoram[offs+1] & 0xf0) << 4),
940 					(videoram[offs+1] & 0x0f) + (spelunk2_palbank << 4),
941 					flipscreen,flipscreen,
942 					8*sx,8*sy,
943 					0,TRANSPARENCY_NONE,0);
944 		}
945 	}
946 
947 
948 	{
949 		int scrollx,scrolly;
950 
951 		if (flipscreen)
952 		{
953 			scrollx = irem_background_hscroll;
954 			scrolly = irem_background_vscroll - 128;
955 		}
956 		else
957 		{
958 			scrollx = -irem_background_hscroll;
959 			scrolly = -irem_background_vscroll - 128;
960 		}
961 
962 		copyscrollbitmap(bitmap,tmpbitmap,1,&scrollx,1,&scrolly,&Machine->visible_area,TRANSPARENCY_NONE,0);
963 	}
964 }
965 
966 
967 
968 
battroad_draw_text(struct osd_bitmap * bitmap)969 static void battroad_draw_text(struct osd_bitmap *bitmap)
970 {
971 	int offs;
972 
973 
974 	for (offs = irem_textram_size - 2;offs >= 0;offs -= 2)
975 	{
976 		int sx,sy;
977 
978 
979 		sx = (offs/2) % 32;
980 		sy = (offs/2) / 32;
981 
982 		if (flipscreen)
983 		{
984 			sx = 31 - sx;
985 			sy = 31 - sy;
986 		}
987 
988 		drawgfx(bitmap,Machine->gfx[2],
989 				irem_textram[offs] + ((irem_textram[offs + 1] & 0x40) << 3) + ((irem_textram[offs + 1] & 0x10) << 4),
990 				(irem_textram[offs + 1] & 0x0f),
991 				flipscreen,flipscreen,
992 				8*sx+128,8*sy,
993 				&Machine->visible_area,TRANSPARENCY_PEN, 0);
994 	}
995 }
996 
kidniki_draw_text(struct osd_bitmap * bitmap)997 static void kidniki_draw_text(struct osd_bitmap *bitmap)
998 {
999 	int offs;
1000 	int scrolly;
1001 
1002 
1003 	if (flipscreen)
1004 		scrolly = kidniki_text_vscroll-0x180;
1005 	else
1006 		scrolly = -kidniki_text_vscroll+0x180;
1007 
1008 
1009 	for (offs = irem_textram_size - 2;offs >= 0;offs -= 2)
1010 	{
1011 		int sx,sy;
1012 
1013 
1014 		sx = (offs/2) % 32;
1015 		sy = (offs/2) / 32;
1016 
1017 		if (flipscreen)
1018 		{
1019 			sx = 31 - sx;
1020 			sy = 31 - sy;
1021 		}
1022 
1023 		drawgfx(bitmap,Machine->gfx[2],
1024 				irem_textram[offs] + ((irem_textram[offs + 1] & 0xc0) << 2),
1025 				(irem_textram[offs + 1] & 0x1f),
1026 				flipscreen,flipscreen,
1027 				12*sx + 64,8*sy + scrolly,
1028 				&Machine->visible_area,TRANSPARENCY_PEN, 0);
1029 	}
1030 }
1031 
spelunkr_draw_text(struct osd_bitmap * bitmap)1032 static void spelunkr_draw_text(struct osd_bitmap *bitmap)
1033 {
1034 	int offs;
1035 
1036 
1037 	for (offs = irem_textram_size - 2;offs >= 0;offs -= 2)
1038 	{
1039 		int sx,sy;
1040 
1041 
1042 		sx = (offs/2) % 32;
1043 		sy = (offs/2) / 32;
1044 
1045 		if (flipscreen)
1046 		{
1047 			sx = 31 - sx;
1048 			sy = 31 - sy;
1049 		}
1050 
1051 		drawgfx(bitmap,Machine->gfx[2],
1052 				irem_textram[offs] + ((irem_textram[offs + 1] & 0x10) << 4),
1053 				(irem_textram[offs + 1] & 0x0f) + (spelunk2_palbank << 4),
1054 				flipscreen,flipscreen,
1055 				12*sx + 64,8*sy,
1056 				&Machine->visible_area,TRANSPARENCY_PEN, 0);
1057 	}
1058 }
1059 
1060 
1061 
kungfum_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1062 void kungfum_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1063 {
1064 	kungfum_draw_background(bitmap);
1065 	draw_sprites(bitmap, &kungfum_spritevisiblearea, &kungfum_flipspritevisiblearea);
1066 }
1067 
battroad_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1068 void battroad_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1069 {
1070 	battroad_draw_background(bitmap, 0);
1071 	draw_priority_sprites(bitmap, 0);
1072 	battroad_draw_background(bitmap, 1);
1073 	draw_priority_sprites(bitmap, 1);
1074 	battroad_draw_text(bitmap);
1075 }
1076 
ldrun_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1077 void ldrun_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1078 {
1079 	ldrun_draw_background(bitmap, 0);
1080 	draw_priority_sprites(bitmap, 0);
1081 	ldrun_draw_background(bitmap, 1);
1082 	draw_priority_sprites(bitmap, 1);
1083 }
1084 
ldrun4_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1085 void ldrun4_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1086 {
1087 	ldrun4_draw_background(bitmap);
1088 	draw_sprites(bitmap, &Machine->visible_area, &Machine->visible_area);
1089 }
1090 
lotlot_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1091 void lotlot_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1092 {
1093 	lotlot_draw_background(bitmap);
1094 	draw_sprites(bitmap, &Machine->visible_area, &Machine->visible_area);
1095 }
1096 
kidniki_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1097 void kidniki_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
1098 {
1099 	kidniki_draw_background(bitmap);
1100 	draw_sprites(bitmap, &Machine->visible_area, &Machine->visible_area);
1101 	kidniki_draw_text(bitmap);
1102 }
1103 
spelunkr_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1104 void spelunkr_vh_screenrefresh( struct osd_bitmap *bitmap, int full_refresh )
1105 {
1106 	spelunkr_draw_background(bitmap);
1107 	draw_sprites(bitmap, &Machine->visible_area, &Machine->visible_area);
1108 	spelunkr_draw_text(bitmap);
1109 }
1110 
spelunk2_vh_screenrefresh(struct osd_bitmap * bitmap,int full_refresh)1111 void spelunk2_vh_screenrefresh( struct osd_bitmap *bitmap, int full_refresh )
1112 {
1113 	spelunk2_draw_background(bitmap);
1114 	draw_sprites(bitmap, &Machine->visible_area, &Machine->visible_area);
1115 	spelunkr_draw_text(bitmap);
1116 }
1117