1 /***************************************************************************
2 
3   Functions to emulate the video hardware of some Toaplan games,
4   which use the BCU-2 tile controller, and the FCU-2 Sprite controller -
5   and SCU Sprite controller (Only Rally Bike uses the SCU controller).
6 
7 
8   There are 4 scrolling layers of graphics, stored in planes of 64x64 tiles.
9   Each tile in each plane is assigned a priority between 1 and 15, higher
10   numbers have greater priority.
11 
12  BCU controller. Each tile takes up 32 bits - the format is:
13 
14   0         1         2         3
15   ---- ---- ---- ---- -ttt tttt tttt tttt = Tile number (0 - $7fff)
16   ---- ---- ---- ---- h--- ---- ---- ---- = Hidden
17   ---- ---- --cc cccc ---- ---- ---- ---- = Color (0 - $3f)
18   pppp ---- ---- ---- ---- ---- ---- ---- = Priority (0-$f)
19   ---- ???? ??-- ---- ---- ---- ---- ---- = Unknown / Unused
20 
21   Scroll Reg
22 
23   0         1         2         3
24   xxxx xxxx x--- ---- ---- ---- ---- ---- = X position
25   ---- ---- ---- ---- yyyy yyyy y--- ---- = Y position
26   ---- ---- -??? ???? ---- ---- -??? ???? = Unknown / Unused
27 
28 
29 
30  FCU controller. Sprite RAM format  (except Rally Bike)
31 
32   0         1         2         3
33   -sss ssss ssss ssss ---- ---- ---- ---- = Sprite number (0 - $7fff)
34   h--- ---- ---- ---- ---- ---- ---- ---- = Hidden
35   ---- ---- ---- ---- ---- ---- --cc cccc = Color (0 - $3f)
36   ---- ---- ---- ---- ---- dddd dd-- ---- = Dimension (pointer to Size RAM)
37   ---- ---- ---- ---- pppp ---- ---- ---- = Priority (0-$f)
38 
39   4         5         6         7
40   ---- ---- ---- ---- xxxx xxxx x--- ---- = X position
41   yyyy yyyy y--- ---- ---- ---- ---- ---- = Y position
42   ---- ---- -??? ???? ---- ---- -??? ???? = Unknown
43 
44 
45 
46  SCU controller. Sprite RAM format  (Rally Bike)
47 
48   0         1         2         3
49   ---- -sss ssss ssss ---- ---- ---- ---- = Sprite number (0 - $7FF)
50   ---- ---- ---- ---- ---- ---- --cc cccc = Color (0 - $3F)
51   ---- ---- ---- ---- ---- ---x ---- ---- = Flip X
52   ---- ---- ---- ---- ---- --y- ---- ---- = Flip Y
53   ---- ---- ---- ---- ---- pp-- ---- ---- = Priority (0h,4h,8h,Ch (shifted < 2 places))
54   ???? ?--- ---- ---- ???? ---- ??-- ---- = Unknown / Unused
55 
56   4         5         6         7
57   xxxx xxxx x--- ---- ---- ---- ---- ---- = X position
58   ---- ---- ---- ---- yyyy yyyy y--- ---- = Y position
59   ---- ---- -??? ???? ---- ---- -??? ???? = Unknown
60 
61 
62 
63   The tiles use a palette of 1024 colors, the sprites use a different palette
64   of 1024 colors.
65 
66 
67            BCU Controller writes                Tile Offsets
68  Game      reg0  reg1  reg2  reg3         X     Y     flip-X  flip-Y
69 RallyBik   41e0  2e1e  148c  0f09        01e6  00fc     <- same --
70 Truxton    41e0  2717  0e86  0c06        01b7  00f2     0188  01fd
71 HellFire   41e0  2717  0e86  0c06        01b7  0102     0188  000d
72 ZeroWing   41e0  2717  0e86  0c06        01b7  0102     0188  000d
73 DemonWld   41e0  2e1e  148c  0f09        01a9  00fc     0196  0013
74 FireShrk   41e0  2717  0e86  0c06        01b7  00f2     0188  01fd
75 Out-Zone   41e0  2e1e  148c  0f09        01a9  00ec     0196  0003
76 Vimana     41e0  2717  0e86  0c06        01b7  00f2     0188  01fd
77 
78 
79 Sprites are of varying sizes between 8x8 and 128x128 with any variation
80 in between, in multiples of 8 either way.
81 Here we draw the first 8x8 part of the sprite, then by using the sprite
82 dimensions, we draw the rest of the 8x8 parts to produce the complete
83 sprite.
84 
85 
86 Abnormalities:
87  How/when do priority 0 Tile layers really get displayed ?
88 
89  What are the video PROMs for ? Priority maybe ?
90 
91 ***************************************************************************/
92 
93 
94 #include "driver.h"
95 #include "state.h"
96 #include "toaplan1.h"
97 #include "tilemap.h"
98 #include "palette.h"
99 #include "vidhrdw/generic.h"
100 #include "cpu/m68000/m68000.h"
101 
102 #define TOAPLAN1_TILEVRAM_SIZE       0x4000	/* each tile layer ram (word size) */
103 #define TOAPLAN1_SPRITERAM_SIZE      0x800	/* sprite ram (word size) */
104 #define TOAPLAN1_SPRITESIZERAM_SIZE  0x80	/* sprite size ram (word size) */
105 
106 #define TOAPLAN1_RENDER_TYPE_ZEROWING	0
107 #define TOAPLAN1_RENDER_TYPE_DEMONWLD	1
108 
109 static data16_t *toaplan1_tileram16;
110 static data16_t *toaplan1_spritesizeram16;
111 static data16_t *toaplan1_buffered_spritesizeram16;
112 
113 size_t toaplan1_colorram1_size;
114 size_t toaplan1_colorram2_size;
115 data16_t *toaplan1_colorram1;
116 data16_t *toaplan1_colorram2;
117 
118 static int bcu_flipscreen;		/* Tile   controller flip flag */
119 static int fcu_flipscreen;		/* Sprite controller flip flag */
120 
121 static unsigned int tileram_offs;
122 static unsigned int spriteram_offs;
123 
124 static unsigned int scrollregs[8];
125 static unsigned int num_tiles;
126 
127 static int layer_scrollx[4];
128 static int layer_scrolly[4];
129 static int layer_offsetx[4];
130 static int layer_offsety[4];
131 
132 static int scrollx_offs1;
133 static int scrollx_offs2;
134 static int scrollx_offs3;
135 static int scrollx_offs4;
136 static int scrolly_offs;
137 
138 static int flip_y_offs;
139 
140 static int tiles_offsetx;
141 static int tiles_offsety;
142 
143 static int toaplan1_reset;		/* Hack! See toaplan1_bcu_control below */
144 
145 
146 typedef struct
147 	{
148 	UINT16 tile_num;
149 	UINT16 color;
150 	char priority;
151 	int xpos;
152 	int ypos;
153 	} tile_struct;
154 
155 tile_struct *bg_list[4];
156 
157 tile_struct *tile_list[32];
158 tile_struct *temp_list;
159 static int max_list_size[32];
160 static int tile_count[32];
161 
162 		struct	mame_bitmap *tmpbitmap1;
163 static	struct	mame_bitmap *tmpbitmap2;
164 static	struct	mame_bitmap *tmpbitmap3;
165 
166 
167 #undef BGDBG
168 
169 #ifdef BGDBG
170 int	toaplan_dbg_sprite_only = 0;
171 int	toaplan_dbg_priority = 0;
172 int	toaplan_dbg_layer[4] = {1,1,1,1};
173 #endif
174 
toaplan1_tile_buffers_alloc(void)175 static int toaplan1_tile_buffers_alloc(void)
176 {
177 	int i;
178 
179 	if ( (toaplan1_tileram16 = (data16_t *)auto_malloc(TOAPLAN1_TILEVRAM_SIZE * 4)) == 0){
180 		return 1;
181 	}
182 	memset(toaplan1_tileram16,0,TOAPLAN1_TILEVRAM_SIZE * 4);
183 
184 	log_cb(RETRO_LOG_DEBUG, LOGPRE "colorram_size: %08x\n", toaplan1_colorram1_size + toaplan1_colorram2_size);
185 	if ( (paletteram16 = (data16_t *)auto_malloc(toaplan1_colorram1_size + toaplan1_colorram2_size)) == 0){
186 		return 1;
187 	}
188 	memset(paletteram16,0,toaplan1_colorram1_size + toaplan1_colorram2_size);
189 
190 	for (i=0; i<4; i++)
191 	{
192 		if ((bg_list[i]=(tile_struct *)auto_malloc( 33 * 44 * sizeof(tile_struct))) == 0){
193 			return 1;
194 		}
195 		memset(bg_list[i], 0, 33 * 44 * sizeof(tile_struct));
196 	}
197 
198 	for (i=0; i<16; i++)
199 	{
200 		max_list_size[i] = 8192;
201 		if ((tile_list[i]=(tile_struct *)auto_malloc(max_list_size[i]*sizeof(tile_struct))) == 0){
202 			return 1;
203 		}
204 		memset(tile_list[i],0,max_list_size[i]*sizeof(tile_struct));
205 	}
206 
207 	max_list_size[16] = 65536;
208 	if ((tile_list[16]=(tile_struct *)auto_malloc(max_list_size[16]*sizeof(tile_struct))) == 0){
209 		return 1;
210 	}
211 	memset(tile_list[16],0,max_list_size[16]*sizeof(tile_struct));
212 
213 	return 0;
214 }
215 
216 
217 
VIDEO_START(rallybik)218 VIDEO_START( rallybik )
219 {
220 
221 	if( toaplan1_tile_buffers_alloc() ){
222 		return 1;
223 	}
224 
225 	num_tiles = (Machine->drv->screen_width/8+1)*(Machine->drv->screen_height/8);
226 
227 	spriteram_offs = tileram_offs = 0;
228 
229 	scrollx_offs1 = 0x0d + 6;
230 	scrollx_offs2 = 0x0d + 4;
231 	scrollx_offs3 = 0x0d + 2;
232 	scrollx_offs4 = 0x0d + 0;
233 	scrolly_offs  = 0x111;
234 
235 	bcu_flipscreen = 0;
236 	toaplan1_reset = 0;
237 
238 	return 0;
239 
240 }
241 
VIDEO_START(toaplan1)242 VIDEO_START( toaplan1 )
243 {
244 	tmpbitmap1 = auto_bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height);
245 	tmpbitmap2 = auto_bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height);
246 	tmpbitmap3 = auto_bitmap_alloc(Machine->drv->screen_width,Machine->drv->screen_height);
247 
248 	if ( (spriteram16 = (data16_t *)auto_malloc(TOAPLAN1_SPRITERAM_SIZE)) == 0){
249 		return 1;
250 	}
251 	memset(spriteram16,0,TOAPLAN1_SPRITERAM_SIZE);
252 
253 	if ( (buffered_spriteram16 = (data16_t *)auto_malloc(TOAPLAN1_SPRITERAM_SIZE)) == 0){
254 		return 1;
255 	}
256 	memset(buffered_spriteram16,0,TOAPLAN1_SPRITERAM_SIZE);
257 
258 	if ( (toaplan1_spritesizeram16 = (data16_t *)auto_malloc(TOAPLAN1_SPRITESIZERAM_SIZE)) == 0){
259 		return 1;
260 	}
261 	memset(toaplan1_spritesizeram16,0,TOAPLAN1_SPRITESIZERAM_SIZE);
262 
263 	if ( (toaplan1_buffered_spritesizeram16 = (data16_t *)auto_malloc(TOAPLAN1_SPRITESIZERAM_SIZE)) == 0){
264 		return 1;
265 	}
266 	memset(toaplan1_buffered_spritesizeram16,0,TOAPLAN1_SPRITESIZERAM_SIZE);
267 
268 	if( toaplan1_tile_buffers_alloc() ){
269 		return 1;
270 	}
271 
272 	num_tiles = (Machine->drv->screen_width/8+1)*(Machine->drv->screen_height/8);
273 
274 	spriteram_offs = tileram_offs = 0;
275 
276 	scrollx_offs1 = 0x1ef + 6;
277 	scrollx_offs2 = 0x1ef + 4;
278 	scrollx_offs3 = 0x1ef + 2;
279 	scrollx_offs4 = 0x1ef + 0;
280 	scrolly_offs  = 0x101;
281 
282 	bcu_flipscreen = 0;
283 	fcu_flipscreen = 0;
284 	toaplan1_reset = 1;
285 
286 	return 0;
287 }
288 
289 
290 
toaplan1_set_scrolls(void)291 void toaplan1_set_scrolls(void)
292 {
293 
294 	layer_scrollx[0] = (((scrollregs[0]) >> 7) + (scrollx_offs1 - tiles_offsetx)) & 0x1ff;
295 	layer_scrollx[1] = (((scrollregs[2]) >> 7) + (scrollx_offs2 - tiles_offsetx)) & 0x1ff;
296 	layer_scrollx[2] = (((scrollregs[4]) >> 7) + (scrollx_offs3 - tiles_offsetx)) & 0x1ff;
297 	layer_scrollx[3] = (((scrollregs[6]) >> 7) + (scrollx_offs4 - tiles_offsetx)) & 0x1ff;
298 
299 	layer_scrolly[0] = (((scrollregs[1]) >> 7) + scrolly_offs - tiles_offsety) & 0x1ff;
300 	layer_scrolly[1] = (((scrollregs[3]) >> 7) + scrolly_offs - tiles_offsety) & 0x1ff;
301 	layer_scrolly[2] = (((scrollregs[5]) >> 7) + scrolly_offs - tiles_offsety) & 0x1ff;
302 	layer_scrolly[3] = (((scrollregs[7]) >> 7) + scrolly_offs - tiles_offsety) & 0x1ff;
303 
304 }
305 
306 /***************************************************************************
307 
308   Video I/O port hardware.
309 
310 ***************************************************************************/
311 
READ16_HANDLER(toaplan1_frame_done_r)312 READ16_HANDLER( toaplan1_frame_done_r )
313 {
314 	return cpu_getvblank();
315 }
316 
WRITE16_HANDLER(toaplan1_tile_offsets_w)317 WRITE16_HANDLER( toaplan1_tile_offsets_w )
318 {
319 	if ( offset == 0 )
320 	{
321 		COMBINE_DATA(&tiles_offsetx);
322 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Tiles_offsetx now = %08x\n",tiles_offsetx);
323 	}
324 	else
325 	{
326 		COMBINE_DATA(&tiles_offsety);
327 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Tiles_offsety now = %08x\n",tiles_offsety);
328 	}
329 	toaplan1_reset = 1;
330 	toaplan1_set_scrolls();
331 }
332 
333 
WRITE16_HANDLER(rallybik_bcu_flipscreen_w)334 WRITE16_HANDLER( rallybik_bcu_flipscreen_w )
335 {
336 	if (ACCESSING_LSB)
337 	{
338 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Setting BCU controller flipscreen port to %04x\n",data);
339 		bcu_flipscreen = data & 0x01;		/* 0x0001 = flip, 0x0000 = no flip */
340 		if (bcu_flipscreen)
341 		{
342 			scrollx_offs1 = 0x080 - 6;
343 			scrollx_offs2 = 0x080 - 4;
344 			scrollx_offs3 = 0x080 - 2;
345 			scrollx_offs4 = 0x080 - 0;
346 			scrolly_offs  = 0x1f8;
347 		}
348 		else
349 		{
350 			scrollx_offs1 = 0x0d + 6;
351 			scrollx_offs2 = 0x0d + 4;
352 			scrollx_offs3 = 0x0d + 2;
353 			scrollx_offs4 = 0x0d + 0;
354 			scrolly_offs  = 0x111;
355 		}
356 		toaplan1_set_scrolls();
357 	}
358 }
359 
WRITE16_HANDLER(toaplan1_bcu_flipscreen_w)360 WRITE16_HANDLER( toaplan1_bcu_flipscreen_w )
361 {
362 	if (ACCESSING_LSB)
363 	{
364 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Setting BCU controller flipscreen port to %04x\n",data);
365 		bcu_flipscreen = data & 0x01;		/* 0x0001 = flip, 0x0000 = no flip */
366 		if (bcu_flipscreen)
367 		{
368 			scrollx_offs1 = 0x011 - 6;
369 			scrollx_offs2 = 0x011 - 4;
370 			scrollx_offs3 = 0x011 - 2;
371 			scrollx_offs4 = 0x011 - 0;
372 			scrolly_offs  = 0xff;
373 			if (1)
374 			{
375 				scrolly_offs  += 16;
376 				flip_y_offs = -16;
377 			}
378 		}
379 		else
380 		{
381 			scrollx_offs1 = 0x1ef + 6;
382 			scrollx_offs2 = 0x1ef + 4;
383 			scrollx_offs3 = 0x1ef + 2;
384 			scrollx_offs4 = 0x1ef + 0;
385 			scrolly_offs  = 0x101;
386 			flip_y_offs = 0;
387 		}
388 		toaplan1_set_scrolls();
389 	}
390 }
391 
WRITE16_HANDLER(toaplan1_fcu_flipscreen_w)392 WRITE16_HANDLER( toaplan1_fcu_flipscreen_w )
393 {
394 	if (ACCESSING_MSB)
395 	{
396 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Setting FCU controller flipscreen port to %04x\n",data);
397 		fcu_flipscreen = data & 0x8000;	/* 0x8000 = flip, 0x0000 = no flip */
398 	}
399 }
400 
WRITE16_HANDLER(toaplan1_bcu_control_w)401 WRITE16_HANDLER( toaplan1_bcu_control_w )
402 {
403 	log_cb(RETRO_LOG_DEBUG, LOGPRE "BCU tile controller register:%02x now = %04x\n",offset,data);
404 
405 	/*** Hack for Zero Wing and OutZone, to reset the sound system on */
406 	/*** soft resets. These two games don't have a sound reset port,  */
407 	/*** unlike the other games */
408 
409 	if (toaplan1_unk_reset_port && toaplan1_reset)
410 	{
411 		toaplan1_reset = 0;
412 		toaplan1_reset_sound(0,0,0);
413 	}
414 
415 }
416 
417 
418 
READ16_HANDLER(toaplan1_spriteram_offs_r)419 READ16_HANDLER( toaplan1_spriteram_offs_r )
420 {
421 	return spriteram_offs;
422 }
423 
WRITE16_HANDLER(toaplan1_spriteram_offs_w)424 WRITE16_HANDLER( toaplan1_spriteram_offs_w )
425 {
426 	COMBINE_DATA(&spriteram_offs);
427 }
428 
429 
430 /* tile palette */
READ16_HANDLER(toaplan1_colorram1_r)431 READ16_HANDLER( toaplan1_colorram1_r )
432 {
433 	return toaplan1_colorram1[offset];
434 }
435 
WRITE16_HANDLER(toaplan1_colorram1_w)436 WRITE16_HANDLER( toaplan1_colorram1_w )
437 {
438 	COMBINE_DATA(&toaplan1_colorram1[offset]);
439 	paletteram16_xBBBBBGGGGGRRRRR_word_w(offset, data, 0);
440 }
441 
442 /* sprite palette */
READ16_HANDLER(toaplan1_colorram2_r)443 READ16_HANDLER( toaplan1_colorram2_r )
444 {
445 	return toaplan1_colorram2[offset];
446 }
447 
WRITE16_HANDLER(toaplan1_colorram2_w)448 WRITE16_HANDLER( toaplan1_colorram2_w )
449 {
450 	COMBINE_DATA(&toaplan1_colorram2[offset]);
451 	paletteram16_xBBBBBGGGGGRRRRR_word_w(offset+(toaplan1_colorram1_size/2), data, 0);
452 }
453 
READ16_HANDLER(toaplan1_spriteram16_r)454 READ16_HANDLER( toaplan1_spriteram16_r )
455 {
456 	return spriteram16[spriteram_offs & ((TOAPLAN1_SPRITERAM_SIZE/2)-1)];
457 }
458 
WRITE16_HANDLER(toaplan1_spriteram16_w)459 WRITE16_HANDLER( toaplan1_spriteram16_w )
460 {
461 	COMBINE_DATA(&spriteram16[spriteram_offs & ((TOAPLAN1_SPRITERAM_SIZE/2)-1)]);
462 
463 #ifdef MAME_DEBUG
464 	if (spriteram_offs >= (TOAPLAN1_SPRITERAM_SIZE/2))
465 	{
466 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Sprite_RAM_word_w, %08x out of range !\n", spriteram_offs);
467 		return;
468 	}
469 #endif
470 
471 	spriteram_offs++;
472 }
473 
READ16_HANDLER(toaplan1_spritesizeram16_r)474 READ16_HANDLER( toaplan1_spritesizeram16_r )
475 {
476 	return toaplan1_spritesizeram16[spriteram_offs & ((TOAPLAN1_SPRITESIZERAM_SIZE/2)-1)];
477 }
478 
WRITE16_HANDLER(toaplan1_spritesizeram16_w)479 WRITE16_HANDLER( toaplan1_spritesizeram16_w )
480 {
481 	COMBINE_DATA(&toaplan1_spritesizeram16[spriteram_offs & ((TOAPLAN1_SPRITESIZERAM_SIZE/2)-1)]);
482 
483 #ifdef MAME_DEBUG
484 	if (spriteram_offs >= (TOAPLAN1_SPRITESIZERAM_SIZE/2))
485 	{
486 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Sprite_Size_RAM_word_w, %08x out of range !\n", spriteram_offs);
487 		return;
488 	}
489 #endif
490 
491 	spriteram_offs++;	/* really ? shouldn't happen on the sizeram */
492 }
493 
READ16_HANDLER(toaplan1_tileram_offs_r)494 READ16_HANDLER( toaplan1_tileram_offs_r )
495 {
496 	return tileram_offs;
497 }
498 
WRITE16_HANDLER(toaplan1_tileram_offs_w)499 WRITE16_HANDLER( toaplan1_tileram_offs_w )
500 {
501 	COMBINE_DATA(&tileram_offs);
502 	toaplan1_reset = 1;
503 }
504 
READ16_HANDLER(rallybik_tileram16_r)505 READ16_HANDLER( rallybik_tileram16_r )
506 {
507 	data16_t data = toaplan1_tileram16[((tileram_offs * 2) + offset) & (((TOAPLAN1_TILEVRAM_SIZE*4)/2)-1)];
508 
509 	if (offset == 0)	/* some bit lines may be stuck to others */
510 	{
511 		data |= ((data & 0xf000) >> 4);
512 		data |= ((data & 0x0030) << 2);
513 	}
514 	return data;
515 }
516 
READ16_HANDLER(toaplan1_tileram16_r)517 READ16_HANDLER( toaplan1_tileram16_r )
518 {
519 	data16_t data = toaplan1_tileram16[((tileram_offs * 2) + offset) & (((TOAPLAN1_TILEVRAM_SIZE*4)/2)-1)];
520 
521 	return data;
522 }
523 
WRITE16_HANDLER(toaplan1_tileram16_w)524 WRITE16_HANDLER( toaplan1_tileram16_w )
525 {
526 	COMBINE_DATA(&toaplan1_tileram16[((tileram_offs * 2) + offset) & (((TOAPLAN1_TILEVRAM_SIZE*4)/2)-1)]);
527 
528 #ifdef MAME_DEBUG
529 	if ( ((tileram_offs * 2) + offset) >= ((TOAPLAN1_TILEVRAM_SIZE*4)/2) )
530 	{
531 		log_cb(RETRO_LOG_DEBUG, LOGPRE "Tile_RAM_w, %08x out of range !\n", tileram_offs);
532 		return;
533 	}
534 #endif
535 
536 	if ( offset == 1 ) tileram_offs++;
537 }
538 
READ16_HANDLER(toaplan1_scroll_regs_r)539 READ16_HANDLER( toaplan1_scroll_regs_r )
540 {
541 	offset = offset & 7;
542 	return scrollregs[offset];
543 }
544 
WRITE16_HANDLER(toaplan1_scroll_regs_w)545 WRITE16_HANDLER( toaplan1_scroll_regs_w )
546 {
547 	offset = offset & 7;
548 	COMBINE_DATA(&scrollregs[offset]);
549 	toaplan1_set_scrolls();
550 }
551 
552 /***************************************************************************
553 
554   Draw the game screen in the given mame_bitmap.
555 
556 ***************************************************************************/
557 
558 
toaplan1_find_tiles(void)559 static void toaplan1_find_tiles( void )
560 {
561 	int priority;
562 	int layer;
563 	tile_struct *tinfo;
564 	data16_t *t_info;
565 
566 	for ( priority = 0; priority < 16; priority++ )
567 	{
568 		tile_count[priority] = 0;
569 	}
570 
571 	for ( layer = 3; layer >= 0; layer-- )
572 	{
573 		int scrolly,scrollx,offsetx,offsety;
574 		int sx,sy,tattr;
575 		int i;
576 
577 #ifdef BGDBG
578 		if( toaplan_dbg_layer[layer] == 1 ){
579 #endif
580 
581 		t_info = toaplan1_tileram16 + (layer * (TOAPLAN1_TILEVRAM_SIZE/2));
582 		scrollx = layer_scrollx[layer];
583 		offsetx = scrollx / 8;
584 		scrolly = layer_scrolly[layer];
585 		offsety = scrolly / 8;
586 		layer_offsetx[layer] = scrollx & 0x7;
587 		layer_offsety[layer] = scrolly & 0x7;
588 
589 		for ( sy = 0; sy < 33; sy++ )
590 	{
591 			for ( sx = 0; sx <= 40; sx++ )
592 			{
593 				i = ((sy+offsety)&0x3f)*128 + ((sx+offsetx)&0x3f)*2;
594 				tattr = t_info[i];
595 				priority = (tattr >> 12);
596 
597 				tinfo = (tile_struct *)&(bg_list[layer][sy*41+sx]);
598 				tinfo->tile_num = t_info[i+1];
599 				tinfo->priority = priority;
600 				tinfo->color = tattr & 0x3f;
601 				tinfo->color |= layer<<8;
602 				tinfo->xpos = (sx*8)-(scrollx&0x7);
603 				tinfo->ypos = (sy*8)-(scrolly&0x7);
604 
605 				if ( (priority) || (layer == 0) )	/* if priority 0 draw layer 0 only */
606 				{
607 					tinfo = (tile_struct *)&(tile_list[priority][tile_count[priority]]);
608 					tinfo->tile_num = t_info[i+1];
609 					if ( !((priority) && (tinfo->tile_num & 0x8000)) )
610 /*					if ( (tinfo->tile_num & 0x8000) == 0 ) */
611 					{
612 						tinfo->priority = priority;
613 						tinfo->color = tattr & 0x3f;
614 						tinfo->color |= layer<<8;
615 						tinfo->xpos = (sx*8)-(scrollx&0x7);
616 						tinfo->ypos = (sy*8)-(scrolly&0x7);
617 						tile_count[priority]++;
618 						if(tile_count[priority]==max_list_size[priority]){
619 							log_cb(RETRO_LOG_DEBUG, LOGPRE " Tile buffer over flow !! %08x\n",priority);
620 						}
621 					}
622 				}
623 			}
624 		}
625 #ifdef BGDBG
626 		}
627 #endif
628 	}
629 }
630 
631 
toaplan1_find_sprites(void)632 static void toaplan1_find_sprites (void)
633 {
634 	int priority;
635 	int sprite;
636 	data16_t *s_info;
637 	data16_t *s_size;
638 
639 
640 	tile_count[16] = 0;
641 
642 	s_size = toaplan1_buffered_spritesizeram16;	/* sprite block size */
643 	s_info = buffered_spriteram16;				/* start of sprite ram */
644 
645 	for ( sprite = 0; sprite < 256; sprite++ )
646 	{
647 		int tattr,tchar;
648 
649 		tchar = s_info[0];
650 		tattr = s_info[1];
651 
652 		if ( (tchar & 0x8000) == 0 )
653 		{
654 			int sx,sy,dx,dy,s_sizex,s_sizey;
655 			int sprite_size_ptr;
656 
657 			sx=s_info[2];
658 			sx >>= 7;
659 			if ( sx > 416 ) sx -= 512;
660 
661 			sy=s_info[3];
662 			sy >>= 7;
663 			if ( sy > 416 ) sy -= 512;
664 
665 			priority = (tattr >> 12);
666 
667 			sprite_size_ptr = (tattr>>6)&0x3f;
668 			s_sizey = (s_size[sprite_size_ptr]>>4)&0xf;
669 			s_sizex =  s_size[sprite_size_ptr]    &0xf;
670 
671 			for ( dy = s_sizey; dy > 0; dy-- )
672 			{
673 				for ( dx = s_sizex; dx > 0; dx-- )
674 	{
675 					tile_struct *tinfo;
676 
677 					tinfo = (tile_struct *)&(tile_list[16][tile_count[16]]);
678 					tinfo->priority = priority;
679 					tinfo->tile_num = tchar;
680 					tinfo->color = 0x80 | (tattr & 0x3f);
681 					tinfo->xpos = sx-dx*8+s_sizex*8;
682 					tinfo->ypos = sy-dy*8+s_sizey*8 + flip_y_offs;
683 					tile_count[16]++;
684 					if(tile_count[16]==max_list_size[16]){
685 						log_cb(RETRO_LOG_DEBUG, LOGPRE " Tile buffer over flow !! %08x\n",priority);
686 					}
687 					tchar++;
688 				}
689 			}
690 		}
691 		s_info += 4;
692 	}
693 }
694 
rallybik_find_sprites(void)695 static void rallybik_find_sprites (void)
696 {
697 	int offs;
698 	int tattr;
699 	int sx,sy,tchar;
700 	int priority;
701 	tile_struct *tinfo;
702 
703 	for (offs = 0;offs < (spriteram_size/2);offs += 4)
704 	{
705 		tattr = buffered_spriteram16[offs+1];
706 		if ( tattr )	/* no need to render hidden sprites */
707 		{
708 			sx=buffered_spriteram16[offs+2];
709 			sx >>= 7;
710 			sx &= 0x1ff;
711 			if ( sx > 416 ) sx -= 512;
712 
713 			sy=buffered_spriteram16[offs+3];
714 			sy >>= 7;
715 			sy &= 0x1ff;
716 			if ( sy > 416 ) sy -= 512;
717 
718 			priority = (tattr>>8) & 0xc;
719 			tchar = buffered_spriteram16[offs];
720 			tinfo = (tile_struct *)&(tile_list[priority][tile_count[priority]]);
721 			tinfo->tile_num = tchar & 0x7ff;
722 			tinfo->color = 0x80 | (tattr&0x3f);
723 			tinfo->color |= (tattr & 0x0100);
724 			tinfo->color |= (tattr & 0x0200);
725 			if (tinfo->color & 0x0100) sx -= 15;
726 
727 			tinfo->xpos = sx-31;
728 			tinfo->ypos = sy-16;
729 			tile_count[priority]++;
730 			if(tile_count[priority]==max_list_size[priority]){
731 				log_cb(RETRO_LOG_DEBUG, LOGPRE " Tile buffer over flow !! %08x\n",priority);
732 	}
733 		}  /* if tattr */
734 	}  /* for sprite */
735 }
736 
737 
738 
toaplan1_sprite_mask(struct mame_bitmap * dest_bmp,struct mame_bitmap * src_bmp,const struct rectangle * clip)739 void toaplan1_sprite_mask
740 	(
741 	struct mame_bitmap *dest_bmp,
742 	struct mame_bitmap *src_bmp,
743 	const struct rectangle *clip
744 	)
745 {
746 	struct rectangle myclip;
747 	int sx=0;
748 	int sy=0;
749 	int transparent_color;
750 
751 	transparent_color = Machine->pens[0];
752 
753 	if (0)
754 	{
755 		int temp;
756 
757 		/* clip and myclip might be the same, so we need a temporary storage */
758 		temp = clip->min_x;
759 		myclip.min_x = clip->min_y;
760 		myclip.min_y = temp;
761 		temp = clip->max_x;
762 		myclip.max_x = clip->max_y;
763 		myclip.max_y = temp;
764 		clip = &myclip;
765 	}
766 	if (0)
767 		{
768 		int temp;
769 
770 		sx = -sx;
771 
772 		/* clip and myclip might be the same, so we need a temporary storage */
773 		temp = clip->min_x;
774 		myclip.min_x = dest_bmp->width-1 - clip->max_x;
775 		myclip.max_x = dest_bmp->width-1 - temp;
776 		myclip.min_y = clip->min_y;
777 		myclip.max_y = clip->max_y;
778 		clip = &myclip;
779 	}
780 	if (0)
781 			{
782 		int temp;
783 
784 		sy = -sy;
785 
786 		myclip.min_x = clip->min_x;
787 		myclip.max_x = clip->max_x;
788 		/* clip and myclip might be the same, so we need a temporary storage */
789 		temp = clip->min_y;
790 		myclip.min_y = dest_bmp->height-1 - clip->max_y;
791 		myclip.max_y = dest_bmp->height-1 - temp;
792 		clip = &myclip;
793 			}
794 
795 	{
796 		int ex = sx+src_bmp->width;
797 		int ey = sy+src_bmp->height;
798 
799 		if( sx < clip->min_x)
800 		{ /* clip left */
801 			sx = clip->min_x;
802 		}
803 		if( sy < clip->min_y )
804 		{ /* clip top */
805 			sy = clip->min_y;
806 		}
807 		if( ex > clip->max_x+1 )
808 		{ /* clip right */
809 			ex = clip->max_x + 1;
810 		}
811 		if( ey > clip->max_y+1 )
812 		{ /* clip bottom */
813 			ey = clip->max_y + 1;
814 		}
815 
816 		if( ex>sx )
817 		{ /* skip if inner loop doesn't draw anything */
818 			int y;
819 
820 			for( y=sy; y<ey; y++ )
821 		{
822 				unsigned short *dest = (unsigned short *)dest_bmp->line[y];
823 				unsigned short *source = (unsigned short *)src_bmp->line[y];
824 				int x;
825 
826 				for( x=sx; x<ex; x++ )
827 			{
828 					int c = source[x];
829 					if( c != transparent_color )
830 						dest[x] = transparent_color;
831 				}
832 			}
833 		}
834 	}
835 }
836 
837 
838 
toaplan1_sprite_copy(struct mame_bitmap * dest_bmp,struct mame_bitmap * src_bmp,struct mame_bitmap * look_bmp,const struct rectangle * clip)839 void toaplan1_sprite_copy
840 	(
841 	struct mame_bitmap *dest_bmp,
842 	struct mame_bitmap *src_bmp,
843 	struct mame_bitmap *look_bmp,
844 	const struct rectangle *clip
845 	)
846 {
847 	struct rectangle myclip;
848 	int sx=0;
849 	int sy=0;
850 	int transparent_color;
851 
852 	transparent_color = Machine->pens[0];
853 
854 	if (0)
855 	{
856 		int temp;
857 
858 		/* clip and myclip might be the same, so we need a temporary storage */
859 		temp = clip->min_x;
860 		myclip.min_x = clip->min_y;
861 		myclip.min_y = temp;
862 		temp = clip->max_x;
863 		myclip.max_x = clip->max_y;
864 		myclip.max_y = temp;
865 		clip = &myclip;
866 	}
867 	if (0)
868 	{
869 		int temp;
870 
871 		sx = -sx;
872 
873 		/* clip and myclip might be the same, so we need a temporary storage */
874 		temp = clip->min_x;
875 		myclip.min_x = dest_bmp->width-1 - clip->max_x;
876 		myclip.max_x = dest_bmp->width-1 - temp;
877 		myclip.min_y = clip->min_y;
878 		myclip.max_y = clip->max_y;
879 		clip = &myclip;
880 	}
881 	if (0)
882 		{
883 		int temp;
884 
885 		sy = -sy;
886 
887 		myclip.min_x = clip->min_x;
888 		myclip.max_x = clip->max_x;
889 		/* clip and myclip might be the same, so we need a temporary storage */
890 		temp = clip->min_y;
891 		myclip.min_y = dest_bmp->height-1 - clip->max_y;
892 		myclip.max_y = dest_bmp->height-1 - temp;
893 		clip = &myclip;
894 	}
895 
896 			{
897 		int ex = sx+src_bmp->width;
898 		int ey = sy+src_bmp->height;
899 
900 		if( sx < clip->min_x)
901 		{ /* clip left */
902 			sx = clip->min_x;
903 		}
904 		if( sy < clip->min_y )
905 		{ /* clip top */
906 			sy = clip->min_y;
907 			}
908 		if( ex > clip->max_x+1 )
909 		{ /* clip right */
910 			ex = clip->max_x + 1;
911 		}
912 		if( ey > clip->max_y+1 )
913 		{ /* clip bottom */
914 			ey = clip->max_y + 1;
915 	}
916 
917 		if( ex>sx )
918 		{ /* skip if inner loop doesn't draw anything */
919 			int y;
920 
921 			for( y=sy; y<ey; y++ )
922 	{
923 				unsigned short *dest = (unsigned short *)dest_bmp->line[y];
924 				unsigned short *source = (unsigned short *)src_bmp->line[y];
925 				unsigned short *look = (unsigned short *)look_bmp->line[y];
926 				int x;
927 
928 				for( x=sx; x<ex; x++ )
929 		{
930 					if( look[x] != transparent_color )
931 						dest[x] = source[x];
932 		}
933 	}
934 		}
935 	}
936 }
937 
938 
939 
toaplan1_sprite_0_copy(struct mame_bitmap * dest_bmp,struct mame_bitmap * look_bmp,const struct rectangle * clip)940 void toaplan1_sprite_0_copy
941 	(
942 	struct mame_bitmap *dest_bmp,
943 	struct mame_bitmap *look_bmp,
944 	const struct rectangle *clip
945 	)
946 {
947 	struct rectangle myclip;
948 	int sx=0;
949 	int sy=0;
950 	int transparent_color;
951 
952 	transparent_color = Machine->pens[0];
953 
954 	if (0)
955 	{
956 		int temp;
957 
958 		/* clip and myclip might be the same, so we need a temporary storage */
959 		temp = clip->min_x;
960 		myclip.min_x = clip->min_y;
961 		myclip.min_y = temp;
962 		temp = clip->max_x;
963 		myclip.max_x = clip->max_y;
964 		myclip.max_y = temp;
965 		clip = &myclip;
966 	}
967 	if (0)
968 	{
969 		int temp;
970 
971 		sx = -sx;
972 
973 		/* clip and myclip might be the same, so we need a temporary storage */
974 		temp = clip->min_x;
975 		myclip.min_x = dest_bmp->width-1 - clip->max_x;
976 		myclip.max_x = dest_bmp->width-1 - temp;
977 		myclip.min_y = clip->min_y;
978 		myclip.max_y = clip->max_y;
979 		clip = &myclip;
980 	}
981 	if (0)
982 	{
983 		int temp;
984 
985 		sy = -sy;
986 
987 		myclip.min_x = clip->min_x;
988 		myclip.max_x = clip->max_x;
989 		/* clip and myclip might be the same, so we need a temporary storage */
990 		temp = clip->min_y;
991 		myclip.min_y = dest_bmp->height-1 - clip->max_y;
992 		myclip.max_y = dest_bmp->height-1 - temp;
993 		clip = &myclip;
994 	}
995 
996 	{
997 		int ex = sx+look_bmp->width;
998 		int ey = sy+look_bmp->height;
999 
1000 		if( sx < clip->min_x)
1001 		{ /* clip left */
1002 			sx = clip->min_x;
1003 	}
1004 		if( sy < clip->min_y )
1005 		{ /* clip top */
1006 			sy = clip->min_y;
1007 	}
1008 		if( ex > clip->max_x+1 )
1009 		{ /* clip right */
1010 			ex = clip->max_x + 1;
1011 	}
1012 		if( ey > clip->max_y+1 )
1013 		{ /* clip bottom */
1014 			ey = clip->max_y + 1;
1015 	}
1016 
1017 		if( ex>sx )
1018 		{ /* skip if inner loop doesn't draw anything */
1019 			int y;
1020 
1021 			for( y=sy; y<ey; y++ )
1022 	{
1023 				unsigned short *dest = (unsigned short *)dest_bmp->line[y];
1024 				unsigned short *look = (unsigned short *)look_bmp->line[y];
1025 				int x;
1026 
1027 				for( x=sx; x<ex; x++ )
1028 	{
1029 					if( look[x] != transparent_color )
1030 						dest[x] = transparent_color;
1031 	}
1032 	}
1033 	}
1034 	}
1035 }
1036 
1037 
1038 
toaplan1_render(struct mame_bitmap * bitmap)1039 static void toaplan1_render (struct mame_bitmap *bitmap)
1040 {
1041 	int i;
1042 	int priority,pen;
1043 	int flip;
1044 	tile_struct *tinfo;
1045 
1046 	fillbitmap (bitmap, Machine->pens[0x120], &Machine->visible_area);
1047 
1048 #ifdef BGDBG
1049 
1050 if (keyboard_pressed(KEYCODE_Q)) { toaplan_dbg_priority = 0; }
1051 if (keyboard_pressed(KEYCODE_W)) { toaplan_dbg_priority = 1; }
1052 if (keyboard_pressed_memory(KEYCODE_E)) { toaplan_dbg_priority = 2; }
1053 if (keyboard_pressed_memory(KEYCODE_R)) { toaplan_dbg_priority = 3; }
1054 if (keyboard_pressed_memory(KEYCODE_T)) { toaplan_dbg_priority = 4; }
1055 if (keyboard_pressed_memory(KEYCODE_Y)) { toaplan_dbg_priority = 5; }
1056 if (keyboard_pressed_memory(KEYCODE_U)) { toaplan_dbg_priority = 6; }
1057 if (keyboard_pressed_memory(KEYCODE_I)) { toaplan_dbg_priority = 7; }
1058 if (keyboard_pressed_memory(KEYCODE_A)) { toaplan_dbg_priority = 8; }
1059 if (keyboard_pressed_memory(KEYCODE_S)) { toaplan_dbg_priority = 9; }
1060 if (keyboard_pressed_memory(KEYCODE_D)) { toaplan_dbg_priority = 10; }
1061 if (keyboard_pressed_memory(KEYCODE_F)) { toaplan_dbg_priority = 11; }
1062 if (keyboard_pressed_memory(KEYCODE_G)) { toaplan_dbg_priority = 12; }
1063 if (keyboard_pressed_memory(KEYCODE_H)) { toaplan_dbg_priority = 13; }
1064 if (keyboard_pressed_memory(KEYCODE_J)) { toaplan_dbg_priority = 14; }
1065 if (keyboard_pressed_memory(KEYCODE_K)) { toaplan_dbg_priority = 15; }
1066 
1067 if (keyboard_pressed_memory(KEYCODE_Z)) { toaplan_dbg_sprite_only ^= 1; }
1068 if (keyboard_pressed_memory(KEYCODE_X)) { toaplan_dbg_layer[0] ^= 1; }
1069 if (keyboard_pressed_memory(KEYCODE_C)) { toaplan_dbg_layer[1] ^= 1; }
1070 if (keyboard_pressed_memory(KEYCODE_V)) { toaplan_dbg_layer[2] ^= 1; }
1071 if (keyboard_pressed_memory(KEYCODE_B)) { toaplan_dbg_layer[3] ^= 1; }
1072 
1073 if( toaplan_dbg_priority != 0 ){
1074 
1075 	priority = toaplan_dbg_priority;
1076 	{
1077 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1078 		pen = TRANSPARENCY_NONE;
1079 		for ( i = 0; i < tile_count[priority]; i++ ) /* draw only tiles in list */
1080 		{
1081 			drawgfx(bitmap,Machine->gfx[0],
1082 				tinfo->tile_num,
1083 				(tinfo->color&0x3f),
1084 				0,0,						/* flipx,flipy */
1085 				tinfo->xpos,tinfo->ypos,
1086 				&Machine->visible_area,pen,0);
1087 			tinfo++;
1088 		}
1089 	}
1090 
1091 }else{
1092 
1093 #endif
1094 
1095 	if (bcu_flipscreen)
1096 		flip = 1;
1097 	else
1098 		flip = 0;
1099 
1100 
1101 #ifdef BGDBG
1102 if ( toaplan_dbg_sprite_only == 0 ){
1103 #endif
1104 
1105 	priority = 0;
1106 	while ( priority < 16 )			/* draw priority layers in order */
1107 	{
1108 		int layer;
1109 
1110 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1111 		layer = (tinfo->color >> 8);
1112 
1113 		if ( (layer == 0) && (priority >= 8 ) )
1114 	{
1115 			pen = TRANSPARENCY_NONE;
1116 		}
1117 		else{
1118 			pen = TRANSPARENCY_PEN;
1119 		}
1120 
1121 		for ( i = 0; i < tile_count[priority]; i++ )	/* draw only tiles in list */
1122 		{
1123 			int xpos,ypos;
1124 
1125 			if ( flip ){
1126 				xpos = (512 - tinfo->xpos) - 8 - (512 - Machine->drv->screen_width);
1127 				ypos = (512 - tinfo->ypos) - 8 - (512 - Machine->drv->screen_height);
1128 		}
1129 			else{
1130 				xpos = tinfo->xpos;
1131 				ypos = tinfo->ypos;
1132 	}
1133 
1134 			drawgfx(bitmap,Machine->gfx[0],
1135 				tinfo->tile_num,
1136 				(tinfo->color&0x3f),
1137 				flip,flip,							/* flipx,flipy */
1138 				xpos,ypos,
1139 				&Machine->visible_area,pen,0);
1140 			tinfo++;
1141 		}
1142 		priority++;
1143 	}
1144 #ifdef BGDBG
1145 }
1146 #endif
1147 
1148 #ifdef BGDBG
1149 }
1150 #endif
1151 
1152 }
1153 
1154 
1155 
zerowing_render(struct mame_bitmap * bitmap)1156 static void zerowing_render (struct mame_bitmap *bitmap)
1157 {
1158 	int i;
1159 	int priority,pen;
1160 	int flip;
1161 	tile_struct *tinfo;
1162 
1163 	fillbitmap (bitmap, Machine->pens[0x120], &Machine->visible_area);
1164 
1165 	if (bcu_flipscreen)
1166 		flip = 1;
1167 	else
1168 		flip = 0;
1169 
1170 	priority = 0;
1171 	while ( priority < 16 )			/* draw priority layers in order */
1172 	{
1173 		int layer;
1174 
1175 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1176 		layer = (tinfo->color >> 8);
1177 		pen = TRANSPARENCY_PEN;
1178 
1179 		for ( i = 0; i < tile_count[priority]; i++ ) /* draw only tiles in list */
1180 		{
1181 			int xpos,ypos;
1182 
1183 			if ( flip ){
1184 				xpos = (512 - tinfo->xpos) - 8 - (512 - Machine->drv->screen_width);
1185 				ypos = (512 - tinfo->ypos) - 8 - (512 - Machine->drv->screen_height);
1186 			}
1187 			else{
1188 				xpos = tinfo->xpos;
1189 				ypos = tinfo->ypos;
1190 			}
1191 
1192 			drawgfx(bitmap,Machine->gfx[0],
1193 				tinfo->tile_num,
1194 				(tinfo->color&0x3f),
1195 				flip,flip,							/* flipx,flipy */
1196 				xpos,ypos,
1197 				&Machine->visible_area,pen,0);
1198 			tinfo++;
1199 		}
1200 		priority++;
1201 	}
1202 }
1203 
1204 
demonwld_render(struct mame_bitmap * bitmap)1205 static void demonwld_render (struct mame_bitmap *bitmap)
1206 {
1207 	int i;
1208 	int priority,pen;
1209 	int flip;
1210 	tile_struct *tinfo;
1211 
1212 	fillbitmap (bitmap, Machine->pens[0], &Machine->visible_area);
1213 
1214 #ifdef BGDBG
1215 
1216 if (keyboard_pressed_memory(KEYCODE_Q)) { toaplan_dbg_priority = 0; }
1217 if (keyboard_pressed_memory(KEYCODE_W)) { toaplan_dbg_priority = 1; }
1218 if (keyboard_pressed_memory(KEYCODE_E)) { toaplan_dbg_priority = 2; }
1219 if (keyboard_pressed_memory(KEYCODE_R)) { toaplan_dbg_priority = 3; }
1220 if (keyboard_pressed_memory(KEYCODE_T)) { toaplan_dbg_priority = 4; }
1221 if (keyboard_pressed_memory(KEYCODE_Y)) { toaplan_dbg_priority = 5; }
1222 if (keyboard_pressed_memory(KEYCODE_U)) { toaplan_dbg_priority = 6; }
1223 if (keyboard_pressed_memory(KEYCODE_I)) { toaplan_dbg_priority = 7; }
1224 if (keyboard_pressed_memory(KEYCODE_O)) { toaplan_dbg_priority = 8; }
1225 if (keyboard_pressed_memory(KEYCODE_A)) { toaplan_dbg_priority = 9; }
1226 if (keyboard_pressed_memory(KEYCODE_S)) { toaplan_dbg_priority = 10; }
1227 if (keyboard_pressed_memory(KEYCODE_D)) { toaplan_dbg_priority = 11; }
1228 if (keyboard_pressed_memory(KEYCODE_F)) { toaplan_dbg_priority = 12; }
1229 if (keyboard_pressed_memory(KEYCODE_G)) { toaplan_dbg_priority = 13; }
1230 if (keyboard_pressed_memory(KEYCODE_H)) { toaplan_dbg_priority = 14; }
1231 if (keyboard_pressed_memory(KEYCODE_J)) { toaplan_dbg_priority = 15; }
1232 
1233 if (keyboard_pressed_memory(KEYCODE_Z)) { toaplan_dbg_sprite_only ^= 1; }
1234 if (keyboard_pressed_memory(KEYCODE_X)) { toaplan_dbg_layer[0] ^= 1; }
1235 if (keyboard_pressed_memory(KEYCODE_C)) { toaplan_dbg_layer[1] ^= 1; }
1236 if (keyboard_pressed_memory(KEYCODE_V)) { toaplan_dbg_layer[2] ^= 1; }
1237 if (keyboard_pressed_memory(KEYCODE_B)) { toaplan_dbg_layer[3] ^= 1; }
1238 
1239 if( toaplan_dbg_priority != 0 ){
1240 
1241 	palette_set_color(0x120,0xf,0xf,0xf);
1242 	fillbitmap (bitmap, Machine->pens[0x120], &Machine->visible_area);
1243 	priority = toaplan_dbg_priority - 1;
1244 				{
1245 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1246 		/* hack to fix black blobs in Demon's World sky */
1247 		pen = TRANSPARENCY_NONE;
1248 		for ( i = 0; i < tile_count[priority]; i++ ) /* draw only tiles in list */
1249 				{
1250 			drawgfx(bitmap,Machine->gfx[0],
1251 				tinfo->tile_num,
1252 				(tinfo->color&0x3f),
1253 				0,0,						/* flipx,flipy */
1254 				tinfo->xpos,tinfo->ypos,
1255 				&Machine->visible_area,pen,0);
1256 			tinfo++;
1257 				}
1258 			}
1259 
1260 }else{
1261 
1262 	if ( toaplan_dbg_sprite_only == 0 ){
1263 
1264 #endif
1265 
1266 	if (bcu_flipscreen)
1267 		flip = 1;
1268 	else
1269 		flip = 0;
1270 
1271 	priority = 0;
1272 	while ( priority < 16 )			/* draw priority layers in order */
1273 				{
1274 		int layer;
1275 
1276 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1277 		layer = (tinfo->color >> 8);
1278 
1279 		if ( priority == 1 )
1280 			pen = TRANSPARENCY_NONE;
1281 		else
1282 			pen = TRANSPARENCY_PEN;
1283 
1284 		for ( i = 0; i < tile_count[priority]; i++ ) /* draw only tiles in list */
1285 		{
1286 			int xpos,ypos;
1287 
1288 			if ( flip ){
1289 				xpos = (512 - tinfo->xpos) - 8 - (512 - Machine->drv->screen_width);
1290 				ypos = (512 - tinfo->ypos) - 8 - (512 - Machine->drv->screen_height);
1291 			}
1292 			else{
1293 				xpos = tinfo->xpos;
1294 				ypos = tinfo->ypos;
1295 				}
1296 
1297 			drawgfx(bitmap,Machine->gfx[0],
1298 				tinfo->tile_num,
1299 				(tinfo->color&0x3f),
1300 				flip,flip,							/* flipx,flipy */
1301 				xpos,ypos,
1302 				&Machine->visible_area,pen,0);
1303 			tinfo++;
1304 			}
1305 		priority++;
1306 		}
1307 
1308 #ifdef BGDBG
1309 	}
1310 }
1311 #endif
1312 
1313 }
1314 
1315 
rallybik_render(struct mame_bitmap * bitmap)1316 static void rallybik_render (struct mame_bitmap *bitmap)
1317 {
1318 	int i;
1319 	int priority,pen;
1320 	int flip;
1321 	tile_struct *tinfo;
1322 
1323 	fillbitmap (bitmap, Machine->pens[0], &Machine->visible_area);
1324 
1325 	if (bcu_flipscreen)
1326 		flip = 1;
1327 	else
1328 		flip = 0;
1329 
1330 	priority = 0;
1331 	while ( priority < 16 )			/* draw priority layers in order */
1332 	{
1333 		tinfo = (tile_struct *)&(tile_list[priority][0]);
1334 
1335 		if ( priority <= 1 )
1336 			pen = TRANSPARENCY_NONE;
1337 		else
1338 			pen = TRANSPARENCY_PEN;
1339 
1340 		for ( i = 0; i < tile_count[priority]; i++ )	/* draw only tiles in list */
1341 		{
1342 			int xpos,ypos;
1343 
1344 			if( tinfo->color & 0x80 ){
1345 				/* sprite */
1346 				drawgfx(bitmap,Machine->gfx[1],
1347 					tinfo->tile_num,
1348 					(tinfo->color&0x3f), 				/* bit 7 not for colour */
1349 					(tinfo->color & 0x0100),(tinfo->color & 0x0200),	/* flipx,flipy */
1350 					tinfo->xpos,tinfo->ypos,
1351 					&Machine->visible_area,pen,0);
1352 			}else{
1353 				/* BG */
1354 				if ( flip ){
1355 					xpos = (512 - tinfo->xpos) - 8 - (512 - Machine->drv->screen_width);
1356 					ypos = (512 - tinfo->ypos) - 8 - (512 - Machine->drv->screen_height);
1357 				}
1358 				else{
1359 					xpos = tinfo->xpos;
1360 					ypos = tinfo->ypos;
1361 				}
1362 				drawgfx(bitmap,Machine->gfx[0],
1363 					tinfo->tile_num,
1364 					(tinfo->color&0x3f),
1365 					flip,flip,							/* flipx,flipy */
1366 					xpos,ypos,
1367 					&Machine->visible_area,pen,0);
1368 			}
1369 			tinfo++;
1370 		}
1371 		priority++;
1372 	}
1373 }
1374 
1375 
1376 
toaplan1_sprite_render(struct mame_bitmap * bitmap)1377 static void toaplan1_sprite_render (struct mame_bitmap *bitmap)
1378 {
1379 	int i;
1380 	int j;
1381 	int priority;
1382 	int flip;
1383 	tile_struct *tinfo;
1384 	tile_struct *tinfo_sp;
1385 	struct rectangle sp_rect;
1386 
1387 	fillbitmap (tmpbitmap1, Machine->pens[0],&Machine->visible_area);
1388 
1389 	flip = 0;
1390 
1391 	tinfo_sp = (tile_struct *)&(tile_list[16][0]);
1392 	for ( i = 0; i < tile_count[16]; i++ )	/* draw sprite No. in order */
1393 	{
1394 		int	flipx,flipy;
1395 
1396 		sp_rect.min_x = tinfo_sp->xpos;
1397 		sp_rect.min_y = tinfo_sp->ypos;
1398 		sp_rect.max_x = tinfo_sp->xpos + 7;
1399 		sp_rect.max_y = tinfo_sp->ypos + 7;
1400 
1401 
1402 		flipx = (tinfo_sp->color & 0x0100);
1403 		flipy = (tinfo_sp->color & 0x0200);
1404 
1405 		fillbitmap (tmpbitmap2, Machine->pens[0], &sp_rect);
1406 
1407 		drawgfx(tmpbitmap2,Machine->gfx[1],
1408 			tinfo_sp->tile_num,
1409 			(tinfo_sp->color&0x3f),			/* bit 7 not for colour */
1410 			flipx,flipy,					/* flipx,flipy */
1411 			tinfo_sp->xpos,tinfo_sp->ypos,
1412 			&Machine->visible_area,TRANSPARENCY_PEN,0
1413 		);
1414 
1415 		fillbitmap (tmpbitmap3, Machine->pens[0], &sp_rect);
1416 
1417 		priority = tinfo_sp->priority;
1418 		{
1419 		int ix0,ix1,ix2,ix3;
1420 		int dirty;
1421 
1422 		dirty = 0;
1423 		for ( j = 0; j < 4; j++ )
1424 		{
1425 			int x,y;
1426 
1427 			y = tinfo_sp->ypos+layer_offsety[j];
1428 			x = tinfo_sp->xpos+layer_offsetx[j];
1429 			ix0 = ( y   /8) * 41 +  x   /8;
1430 			ix1 = ( y   /8) * 41 + (x+7)/8;
1431 			ix2 = ((y+7)/8) * 41 +  x   /8;
1432 			ix3 = ((y+7)/8) * 41 + (x+7)/8;
1433 
1434 			if(	(ix0 >= 0) && (ix0 < 32*41) ){
1435 				tinfo = (tile_struct *)&(bg_list[j][ix0]);
1436 				if( tinfo->priority >= priority ){
1437 					drawgfx(tmpbitmap3,Machine->gfx[0],
1438 						tinfo->tile_num,
1439 						(tinfo->color&0x3f),
1440 						flip,flip,
1441 						tinfo->xpos,tinfo->ypos,
1442 						&Machine->visible_area,TRANSPARENCY_PEN,0
1443 					);
1444 					dirty=1;
1445 				}
1446 			}
1447 			if(	(ix0 != ix1) && (ix1 >= 0) && (ix1 < 32*41) ){
1448 				tinfo = (tile_struct *)&(bg_list[j][ix1]);
1449 				if( tinfo->priority >= priority ){
1450 					drawgfx(tmpbitmap3,Machine->gfx[0],
1451 						tinfo->tile_num,
1452 						(tinfo->color&0x3f),
1453 						flip,flip,
1454 						tinfo->xpos,tinfo->ypos,
1455 						&Machine->visible_area,TRANSPARENCY_PEN,0
1456 					);
1457 					dirty=1;
1458 				}
1459 			}
1460 			if(	(ix1 != ix2) && (ix2 >= 0) && (ix2 < 32*41) ){
1461 				tinfo = (tile_struct *)&(bg_list[j][ix2]);
1462 				if( tinfo->priority >= priority ){
1463 					drawgfx(tmpbitmap3,Machine->gfx[0],
1464 						tinfo->tile_num,
1465 						(tinfo->color&0x3f),
1466 						flip,flip,
1467 						tinfo->xpos,tinfo->ypos,
1468 						&Machine->visible_area,TRANSPARENCY_PEN,0
1469 					);
1470 					dirty=1;
1471 				}
1472 			}
1473 			if( (ix2 != ix3) && (ix3 >= 0) && (ix3 < 32*41) ){
1474 				tinfo = (tile_struct *)&(bg_list[j][ix3]);
1475 				if( tinfo->priority >= priority ){
1476 					drawgfx(tmpbitmap3,Machine->gfx[0],
1477 						tinfo->tile_num,
1478 						(tinfo->color&0x3f),
1479 						flip,flip,
1480 						tinfo->xpos,tinfo->ypos,
1481 						&Machine->visible_area,TRANSPARENCY_PEN,0
1482 					);
1483 					dirty=1;
1484 				}
1485 			}
1486 		}
1487 		if( dirty != 0 )
1488 		{
1489 			toaplan1_sprite_mask(
1490 				tmpbitmap2,
1491 				tmpbitmap3,
1492 				&sp_rect
1493 			);
1494 			fillbitmap (tmpbitmap3, Machine->pens[0], &sp_rect);
1495 			drawgfx(tmpbitmap3,Machine->gfx[1],
1496 				tinfo_sp->tile_num,
1497 				(tinfo_sp->color&0x3f),
1498 				flipx,flipy,
1499 				tinfo_sp->xpos,tinfo_sp->ypos,
1500 				&Machine->visible_area,TRANSPARENCY_PEN,0
1501 			);
1502 			if ( priority == 0 ){			/* demonwld : sprite mask effect in BOSS dying */
1503 				toaplan1_sprite_0_copy(
1504 					tmpbitmap1,
1505 					tmpbitmap3,
1506 					&sp_rect
1507 				);
1508 			}else{
1509 				toaplan1_sprite_copy(
1510 					tmpbitmap1,
1511 					tmpbitmap2,
1512 					tmpbitmap3,
1513 					&sp_rect
1514 				);
1515 			}
1516 
1517 		}else{
1518 			drawgfx(tmpbitmap1,Machine->gfx[1],
1519 				tinfo_sp->tile_num,
1520 				(tinfo_sp->color&0x3f),
1521 				flipx,flipy,
1522 				tinfo_sp->xpos,tinfo_sp->ypos,
1523 				&Machine->visible_area,TRANSPARENCY_PEN,0
1524 			);
1525 		}
1526 		}
1527 		tinfo_sp++;
1528 	}
1529 
1530 	copybitmap(bitmap, tmpbitmap1,
1531 		fcu_flipscreen, fcu_flipscreen,
1532 		0, 0,
1533 		&Machine->visible_area, TRANSPARENCY_PEN, 0
1534 	);
1535 
1536 }
1537 
1538 
1539 
1540 /* void toaplan1_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh) */
VIDEO_UPDATE(toaplan1)1541 VIDEO_UPDATE( toaplan1 )
1542 {
1543 
1544 	memcpy(buffered_spriteram16, spriteram16, TOAPLAN1_SPRITERAM_SIZE);	/* ghetto fix 4x */
1545 	memcpy(toaplan1_buffered_spritesizeram16, toaplan1_spritesizeram16, TOAPLAN1_SPRITESIZERAM_SIZE);
1546 	/* discover what data will be drawn */
1547 	toaplan1_find_sprites();
1548 	toaplan1_find_tiles();
1549 
1550 	toaplan1_render(bitmap);
1551 	toaplan1_sprite_render(bitmap);
1552 }
1553 
1554 /* void zerowing_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh) */
VIDEO_UPDATE(zerowing)1555 VIDEO_UPDATE( zerowing )
1556 {
1557 	memcpy(buffered_spriteram16, spriteram16, TOAPLAN1_SPRITERAM_SIZE);
1558 	memcpy(toaplan1_buffered_spritesizeram16, toaplan1_spritesizeram16, TOAPLAN1_SPRITESIZERAM_SIZE);
1559 	/* discover what data will be drawn */
1560 	toaplan1_find_sprites();
1561 	toaplan1_find_tiles();
1562 
1563 	zerowing_render(bitmap);
1564 	toaplan1_sprite_render(bitmap);
1565 }
1566 
1567 /* void demonwld_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh) */
VIDEO_UPDATE(demonwld)1568 VIDEO_UPDATE( demonwld )
1569 {
1570 	memcpy(buffered_spriteram16, spriteram16, TOAPLAN1_SPRITERAM_SIZE);
1571 	memcpy(toaplan1_buffered_spritesizeram16, toaplan1_spritesizeram16, TOAPLAN1_SPRITESIZERAM_SIZE);
1572 	/* discover what data will be drawn */
1573 	toaplan1_find_sprites();
1574 	toaplan1_find_tiles();
1575 
1576 	demonwld_render(bitmap);
1577 	toaplan1_sprite_render(bitmap);
1578 }
1579 
1580 /* void rallybik_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh) */
VIDEO_UPDATE(rallybik)1581 VIDEO_UPDATE( rallybik )
1582 {
1583 	buffer_spriteram16_w(0, 0, 0);
1584 	/* discover what data will be drawn */
1585 	toaplan1_find_tiles();
1586 	rallybik_find_sprites();
1587 
1588 	rallybik_render(bitmap);
1589 }
1590 
1591 /****************************************************************************
1592     Spriteram is always 1 frame ahead, suggesting spriteram buffering.
1593     There are no CPU output registers that control this so we
1594     assume it happens automatically every frame, at the end of vblank
1595 ****************************************************************************/
1596 
1597 /* void toaplan1_eof_callback(void) */
VIDEO_EOF(toaplan1)1598 VIDEO_EOF( toaplan1 )
1599 {
1600 	memcpy(buffered_spriteram16, spriteram16, TOAPLAN1_SPRITERAM_SIZE);
1601 	memcpy(toaplan1_buffered_spritesizeram16, toaplan1_spritesizeram16, TOAPLAN1_SPRITESIZERAM_SIZE);
1602 }
1603 
1604 /* void rallybik_eof_callback(void) */
VIDEO_EOF(rallybik)1605 VIDEO_EOF( rallybik )
1606 {
1607 	buffer_spriteram16_w(0, 0, 0);
1608 }
1609 
1610 /* void samesame_eof_callback(void) */
VIDEO_EOF(samesame)1611 VIDEO_EOF( samesame )
1612 {
1613 	memcpy(buffered_spriteram16, spriteram16, TOAPLAN1_SPRITERAM_SIZE);
1614 	memcpy(toaplan1_buffered_spritesizeram16, toaplan1_spritesizeram16, TOAPLAN1_SPRITESIZERAM_SIZE);
1615 	cpu_set_irq_line(0, MC68000_IRQ_2, HOLD_LINE);	/* Frame done */
1616 }
1617