summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/toki.c
blob: 74e7eadade8fd9d4423bf5a057f83b124a9230bc (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
/***************************************************************************

  video.c

  Functions to emulate the video hardware of the machine.

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

#include "driver.h"

static tilemap *background_layer,*foreground_layer,*text_layer;

UINT16 *toki_background1_videoram16;
UINT16 *toki_background2_videoram16;
UINT16 *toki_scrollram16;

/*************************************************************************
                    RASTER EFFECTS

Xscroll can be altered per scanline to create rowscroll effect.

(The driver does not implement rowscroll on the bootleg. It seems unlikely
the bootleggers would have been able to do this on their chipset. They
remapped the scroll registers, so obviously they were using a different
chip.

Why then would the old ghost of one of the remapped registers
still cause rowscroll? Probably the bootleggers simply didn't bother to
remove all the code writing the $a0000 area.)

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

WRITE16_HANDLER( toki_control_w )
{
	COMBINE_DATA(&toki_scrollram16[offset]);

	video_screen_update_partial(0, video_screen_get_vpos(0) - 1);
}

VIDEO_EOF( toki )
{
	buffer_spriteram16_w(0,0,0);
}

VIDEO_EOF( tokib )
{
	buffer_spriteram16_w(0,0,0);
}

static TILE_GET_INFO( get_text_tile_info )
{
	int tile = videoram16[tile_index];
	int color=(tile>>12)&0xf;

	tile&=0xfff;

	SET_TILE_INFO(
			0,
			tile,
			color,
			0);
}

static TILE_GET_INFO( get_back_tile_info )
{
	int tile = toki_background1_videoram16[tile_index];
	int color=(tile>>12)&0xf;

	tile&=0xfff;

	SET_TILE_INFO(
			2,
			tile,
			color,
			0);
}

static TILE_GET_INFO( get_fore_tile_info )
{
	int tile = toki_background2_videoram16[tile_index];
	int color=(tile>>12)&0xf;

	tile&=0xfff;

	SET_TILE_INFO(
			3,
			tile,
			color,
			0);
}


/*************************************
 *
 *      Start/Stop
 *
 *************************************/

VIDEO_START( toki )
{
	text_layer       = tilemap_create(get_text_tile_info,tilemap_scan_rows,TILEMAP_TYPE_PEN,  8,8,32,32);
	background_layer = tilemap_create(get_back_tile_info,tilemap_scan_rows,TILEMAP_TYPE_PEN,16,16,32,32);
	foreground_layer = tilemap_create(get_fore_tile_info,tilemap_scan_rows,TILEMAP_TYPE_PEN,16,16,32,32);

	tilemap_set_transparent_pen(text_layer,15);
	tilemap_set_transparent_pen(background_layer,15);
	tilemap_set_transparent_pen(foreground_layer,15);
}

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

WRITE16_HANDLER( toki_foreground_videoram16_w )
{
	COMBINE_DATA(&videoram16[offset]);
	tilemap_mark_tile_dirty(text_layer,offset);
}

WRITE16_HANDLER( toki_background1_videoram16_w )
{
	COMBINE_DATA(&toki_background1_videoram16[offset]);
	tilemap_mark_tile_dirty(background_layer,offset);
}

WRITE16_HANDLER( toki_background2_videoram16_w )
{
	COMBINE_DATA(&toki_background2_videoram16[offset]);
	tilemap_mark_tile_dirty(foreground_layer,offset);
}

/***************************************************************************
                    SPRITES

    Original Spriteram
    ------------------

    It's not clear what purpose is served by marking tiles as being part of
    big sprites. (Big sprites in the attract abduction scene have all tiles
    marked as "first" unlike big sprites in-game.)

    We just ignore this top nibble (although perhaps in theory the bits
    enable X/Y offsets in the low byte).

    +0   x....... ........  sprite disable ??
      +0   .xx..... ........  tile is part of big sprite (4=first, 6=middle, 2=last)
    +0   .....x.. ........  ??? always set? (could be priority - see Bloodbro)
    +0   .......x ........  Flip x
    +0   ........ xxxx....  X offset: add (this * 16) to X coord
    +0   ........ ....xxxx  Y offset: add (this * 16) to Y coord

    +1   xxxx.... ........  Color bank
    +1   ....xxxx xxxxxxxx  Tile number (lo bits)
    +2   x....... ........  Tile number (hi bit)
    +2   .???.... ........  (set in not yet used entries)
    +2   .......x xxxxxxxx  X coordinate
    +3   .......x xxxxxxxx  Y coordinate

    f000 0000 f000 0000     entry not yet used: unless this is honored there
                            will be junk sprites in top left corner
    ffff ???? ???? ????     sprite marked as dead: unless this is honored
                            there will be junk sprites after floating monkey machine


    Bootleg Spriteram
    -----------------

    +0   .......x xxxxxxxx  Sprite Y coordinate
    +1   ...xxxxx xxxxxxxx  Sprite tile number
    +1   .x...... ........  Sprite flip x
    +2   xxxx.... ........  Sprite color bank
    +3   .......x xxxxxxxx  Sprite X coordinate

    f100 ???? ???? ????     dead / unused sprite ??
    ???? ???? 0000 ????     dead / unused sprite ??


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


static void toki_draw_sprites(running_machine *machine, mame_bitmap *bitmap,const rectangle *cliprect)
{
	int x,y,xoffs,yoffs,tile,flipx,flipy,color,offs;
	UINT16 *sprite_word;

	for (offs = (spriteram_size/2)-4;offs >= 0;offs -= 4)
	{
		sprite_word = &buffered_spriteram16[offs];

		if ((sprite_word[2] != 0xf000) && (sprite_word[0] != 0xffff))
		{
			xoffs = (sprite_word[0] &0xf0);
			x = (sprite_word[2] + xoffs) & 0x1ff;
			if (x > 256)
				x -= 512;

			yoffs = (sprite_word[0] &0xf) << 4;
			y = (sprite_word[3] + yoffs) & 0x1ff;
			if (y > 256)
				y -= 512;

			color = sprite_word[1] >> 12;
			flipx   = sprite_word[0] & 0x100;
			flipy   = 0;
			tile    = (sprite_word[1] & 0xfff) + ((sprite_word[2] & 0x8000) >> 3);

			if (flip_screen) {
				x=240-x;
				y=240-y;
				if (flipx) flipx=0; else flipx=1;
				flipy=1;
			}

			drawgfx (bitmap,machine->gfx[1],
					tile,
					color,
					flipx,flipy,
					x,y,
					cliprect,TRANSPARENCY_PEN,15);
		}
	}
}


static void tokib_draw_sprites(running_machine *machine, mame_bitmap *bitmap,const rectangle *cliprect)
{
	int x,y,tile,flipx,color,offs;
	UINT16 *sprite_word;

	for (offs = 0;offs < spriteram_size / 2;offs += 4)
	{
		sprite_word = &buffered_spriteram16[offs];

		if (sprite_word[0] == 0xf100)
			break;
		if (sprite_word[2])
		{

			x = sprite_word[3] & 0x1ff;
			if (x > 256)
				x -= 512;

			y = sprite_word[0] & 0x1ff;
			if (y > 256)
				y = (512-y)+240;
			else
				y = 240-y;

			flipx   = sprite_word[1] & 0x4000;
			tile    = sprite_word[1] & 0x1fff;
			color   = sprite_word[2] >> 12;

			drawgfx (bitmap,machine->gfx[1],
					tile,
					color,
					flipx,0,
					x,y-1,
					cliprect,TRANSPARENCY_PEN,15);
		}
	}
}

/*************************************
 *
 *      Master update function
 *
 *************************************/

VIDEO_UPDATE( toki )
{
	int background_y_scroll,foreground_y_scroll,background_x_scroll,foreground_x_scroll;

	background_x_scroll=((toki_scrollram16[0x06] &0x7f) << 1)
								 |((toki_scrollram16[0x06] &0x80) >> 7)
								 |((toki_scrollram16[0x05] &0x10) << 4);
	background_y_scroll=((toki_scrollram16[0x0d]&0x10)<<4)+((toki_scrollram16[0x0e]&0x7f)<<1)+((toki_scrollram16[0x0e]&0x80)>>7);

	tilemap_set_scrollx( background_layer, 0, background_x_scroll );
	tilemap_set_scrolly( background_layer, 0, background_y_scroll );

	foreground_x_scroll= ((toki_scrollram16[0x16] &0x7f) << 1)
								 |((toki_scrollram16[0x16] &0x80) >> 7)
								 |((toki_scrollram16[0x15] &0x10) << 4);
	foreground_y_scroll=((toki_scrollram16[0x1d]&0x10)<<4)+((toki_scrollram16[0x1e]&0x7f)<<1)+((toki_scrollram16[0x1e]&0x80)>>7);

	tilemap_set_scrollx( foreground_layer, 0, foreground_x_scroll );
	tilemap_set_scrolly( foreground_layer, 0, foreground_y_scroll );

	flip_screen_set((toki_scrollram16[0x28]&0x8000)==0);

	if (toki_scrollram16[0x28]&0x100) {
		tilemap_draw(bitmap,cliprect,background_layer,TILEMAP_DRAW_OPAQUE,0);
		tilemap_draw(bitmap,cliprect,foreground_layer,0,0);
	} else {
		tilemap_draw(bitmap,cliprect,foreground_layer,TILEMAP_DRAW_OPAQUE,0);
		tilemap_draw(bitmap,cliprect,background_layer,0,0);
	}
	toki_draw_sprites(machine, bitmap,cliprect);
	tilemap_draw(bitmap,cliprect,text_layer,0,0);
	return 0;
}

VIDEO_UPDATE( tokib )
{
	tilemap_set_scroll_rows(foreground_layer,1);
	tilemap_set_scroll_rows(background_layer,1);
	tilemap_set_scrolly( background_layer, 0, toki_scrollram16[0]+1 );
	tilemap_set_scrollx( background_layer, 0, toki_scrollram16[1]-0x103 );
	tilemap_set_scrolly( foreground_layer, 0, toki_scrollram16[2]+1 );
	tilemap_set_scrollx( foreground_layer, 0, toki_scrollram16[3]-0x101 );

	if (toki_scrollram16[3]&0x2000) {
		tilemap_draw(bitmap,cliprect,background_layer,TILEMAP_DRAW_OPAQUE,0);
		tilemap_draw(bitmap,cliprect,foreground_layer,0,0);
	} else {
		tilemap_draw(bitmap,cliprect,foreground_layer,TILEMAP_DRAW_OPAQUE,0);
		tilemap_draw(bitmap,cliprect,background_layer,0,0);
	}

	tokib_draw_sprites(machine, bitmap,cliprect);
	tilemap_draw(bitmap,cliprect,text_layer,0,0);
	return 0;
}