summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/galivan.c
blob: 4c9bf898d958271698dad2652afba23906dfdb0e (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
/***************************************************************************

  video.c

  Functions to emulate the video hardware of the machine.

d800-dbff   foreground:         char low bits (1 screen * 1024 chars/screen)
dc00-dfff   foreground attribute:   7       ?
                         6543       color
                             21 ?
                               0    char hi bit


e000-e0ff   spriteram:  64 sprites (4 bytes/sprite)
        offset :    0       1       2       3
        meaning:    ypos(lo)    sprite(lo)  attribute   xpos(lo)
                                7   flipy
                                6   flipx
                                5-2 color
                                1   sprite(hi)
                                0   xpos(hi)


background: 0x4000 bytes of ROM:    76543210    tile code low bits
        0x4000 bytes of ROM:    7       ?
                         6543       color
                             2  ?
                              10    tile code high bits

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

#include "emu.h"
#include "includes/galivan.h"

/* Layers has only bits 5-6 active.
   6 selects background off/on
   5 controls sprite priority (active only on title screen,
     not for scores or push start nor game)
*/


/* Notes:
     write_layers and layers are used in galivan/dangar but not ninjemak
     ninjemak_dispdisable is used in ninjemak but not galivan/dangar
*/



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

  Convert the color PROMs into a more useable format.

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

void galivan_state::palette_init()
{
	const UINT8 *color_prom = machine().root_device().memregion("proms")->base();
	int i;

	/* allocate the colortable */
	machine().colortable = colortable_alloc(machine(), 0x100);

	/* create a lookup table for the palette */
	for (i = 0; i < 0x100; i++)
	{
		int r = pal4bit(color_prom[i + 0x000]);
		int g = pal4bit(color_prom[i + 0x100]);
		int b = pal4bit(color_prom[i + 0x200]);

		colortable_palette_set_color(machine().colortable, i, MAKE_RGB(r, g, b));
	}

	/* color_prom now points to the beginning of the lookup table */
	color_prom += 0x300;

	/* characters use colors 0-0x7f */
	for (i = 0; i < 0x80; i++)
		colortable_entry_set_value(machine().colortable, i, i);

	/* I think that */
	/* background tiles use colors 0xc0-0xff in four banks */
	/* the bottom two bits of the color code select the palette bank for */
	/* pens 0-7; the top two bits for pens 8-15. */
	for (i = 0; i < 0x100; i++)
	{
		UINT8 ctabentry;

		if (i & 0x08)
			ctabentry = 0xc0 | (i & 0x0f) | ((i & 0xc0) >> 2);
		else
			ctabentry = 0xc0 | (i & 0x0f) | ((i & 0x30) >> 0);

		colortable_entry_set_value(machine().colortable, 0x80 + i, ctabentry);
	}

	/* sprites use colors 0x80-0xbf in four banks */
	/* The lookup table tells which colors to pick from the selected bank */
	/* the bank is selected by another PROM and depends on the top 7 bits of */
	/* the sprite code. The PROM selects the bank *separately* for pens 0-7 and */
	/* 8-15 (like for tiles). */
	for (i = 0; i < 0x1000; i++)
	{
		UINT8 ctabentry;
		int i_swapped = ((i & 0x0f) << 8) | ((i & 0xff0) >> 4);

		if (i & 0x80)
			ctabentry = 0x80 | ((i & 0x0c) << 2) | (color_prom[i >> 4] & 0x0f);
		else
			ctabentry = 0x80 | ((i & 0x03) << 4) | (color_prom[i >> 4] & 0x0f);

		colortable_entry_set_value(machine().colortable, 0x180 + i_swapped, ctabentry);
	}
}



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

  Callbacks for the TileMap code

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

TILE_GET_INFO_MEMBER(galivan_state::get_bg_tile_info)
{
	UINT8 *BGROM = machine().root_device().memregion("gfx4")->base();
	int attr = BGROM[tile_index + 0x4000];
	int code = BGROM[tile_index] | ((attr & 0x03) << 8);
	SET_TILE_INFO_MEMBER(
			1,
			code,
			(attr & 0x78) >> 3,     /* seems correct */
			0);
}

TILE_GET_INFO_MEMBER(galivan_state::get_tx_tile_info)
{
	int attr = m_videoram[tile_index + 0x400];
	int code = m_videoram[tile_index] | ((attr & 0x01) << 8);
	SET_TILE_INFO_MEMBER(
			0,
			code,
			(attr & 0xe0) >> 5,     /* not sure */
			0);
	tileinfo.category = attr & 8 ? 0 : 1;   /* seems correct */
}

TILE_GET_INFO_MEMBER(galivan_state::ninjemak_get_bg_tile_info)
{
	UINT8 *BGROM = machine().root_device().memregion("gfx4")->base();
	int attr = BGROM[tile_index + 0x4000];
	int code = BGROM[tile_index] | ((attr & 0x03) << 8);
	SET_TILE_INFO_MEMBER(
			1,
			code,
			((attr & 0x60) >> 3) | ((attr & 0x0c) >> 2),    /* seems correct */
			0);
}

TILE_GET_INFO_MEMBER(galivan_state::ninjemak_get_tx_tile_info)
{
	int attr = m_videoram[tile_index + 0x400];
	int code = m_videoram[tile_index] | ((attr & 0x03) << 8);

	if(tile_index < 0x12) /* don't draw the NB1414M4 params! TODO: could be a better fix */
		code = attr = 0x01;

	SET_TILE_INFO_MEMBER(
			0,
			code,
			(attr & 0x1c) >> 2,     /* seems correct ? */
			0);
}



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

  Start the video hardware emulation.

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

VIDEO_START_MEMBER(galivan_state,galivan)
{
	m_bg_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(galivan_state::get_bg_tile_info),this), TILEMAP_SCAN_ROWS, 16, 16, 128, 128);
	m_tx_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(galivan_state::get_tx_tile_info),this), TILEMAP_SCAN_COLS, 8, 8, 32, 32);

	m_tx_tilemap->set_transparent_pen(15);
}

VIDEO_START_MEMBER(galivan_state,ninjemak)
{
	m_bg_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(galivan_state::ninjemak_get_bg_tile_info),this), TILEMAP_SCAN_COLS, 16, 16, 512, 32);
	m_tx_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(galivan_state::ninjemak_get_tx_tile_info),this), TILEMAP_SCAN_COLS, 8, 8, 32, 32);

	m_tx_tilemap->set_transparent_pen(15);
}



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

  Memory handlers

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

WRITE8_MEMBER(galivan_state::galivan_videoram_w)
{
	m_videoram[offset] = data;
	m_tx_tilemap->mark_tile_dirty(offset & 0x3ff);
}

/* Written through port 40 */
WRITE8_MEMBER(galivan_state::galivan_gfxbank_w)
{
	/* bits 0 and 1 coin counters */
	coin_counter_w(machine(), 0,data & 1);
	coin_counter_w(machine(), 1,data & 2);

	/* bit 2 flip screen */
	m_flipscreen = data & 0x04;
	m_bg_tilemap->set_flip(m_flipscreen ? TILEMAP_FLIPX | TILEMAP_FLIPY : 0);
	m_tx_tilemap->set_flip(m_flipscreen ? TILEMAP_FLIPX | TILEMAP_FLIPY : 0);

	/* bit 7 selects one of two ROM banks for c000-dfff */
	membank("bank1")->set_entry((data & 0x80) >> 7);

	/*  logerror("Address: %04X - port 40 = %02x\n", space.device().safe_pc(), data); */
}

WRITE8_MEMBER(galivan_state::ninjemak_gfxbank_w)
{
	/* bits 0 and 1 coin counters */
	coin_counter_w(machine(), 0,data & 1);
	coin_counter_w(machine(), 1,data & 2);

	/* bit 2 flip screen */
	m_flipscreen = data & 0x04;
	m_bg_tilemap->set_flip(m_flipscreen ? TILEMAP_FLIPX | TILEMAP_FLIPY : 0);
	m_tx_tilemap->set_flip(m_flipscreen ? TILEMAP_FLIPX | TILEMAP_FLIPY : 0);

	/* bit 3 unknown */

	/* bit 4 background disable flag */
	m_ninjemak_dispdisable = data & 0x10;

	/* bit 5 sprite flag ??? */

	/* bit 6, 7 ROM bank select */
	membank("bank1")->set_entry((data & 0xc0) >> 6);

#if 0
	{
		char mess[80];
		int btz[8];
		int offs;

		for (offs = 0; offs < 8; offs++) btz[offs] = (((data >> offs) & 0x01) ? 1 : 0);

		sprintf(mess, "BK:%01X%01X S:%01X B:%01X T:%01X FF:%01X C2:%01X C1:%01X", btz[7], btz[6], btz[5], btz[4], btz[3], btz[2], btz[1], btz[0]);
		popmessage(mess);
	}
#endif
}



/* Written through port 41-42 */
WRITE8_MEMBER(galivan_state::galivan_scrollx_w)
{
	if (offset == 1)
	{
		if (data & 0x80)
			m_write_layers = 1;
		else if (m_write_layers)
		{
			m_layers = data & 0x60;
			m_write_layers = 0;
		}
	}
	m_galivan_scrollx[offset] = data;
}

/* Written through port 43-44 */
WRITE8_MEMBER(galivan_state::galivan_scrolly_w)
{
	m_galivan_scrolly[offset] = data;
}



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

  Display refresh

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

static void draw_sprites( running_machine &machine, bitmap_ind16 &bitmap, const rectangle &cliprect )
{
	galivan_state *state = machine.driver_data<galivan_state>();
	const UINT8 *spritepalettebank = state->memregion("user1")->base();
	UINT8 *spriteram = state->m_spriteram;
	int offs;

	/* draw the sprites */
	for (offs = 0; offs < state->m_spriteram.bytes(); offs += 4)
	{
		int code;
		int attr = spriteram[offs + 2];
		int color = (attr & 0x3c) >> 2;
		int flipx = attr & 0x40;
		int flipy = attr & 0x80;
		int sx, sy;

		sx = (spriteram[offs + 3] - 0x80) + 256 * (attr & 0x01);
		sy = 240 - spriteram[offs];
		if (state->m_flipscreen)
		{
			sx = 240 - sx;
			sy = 240 - sy;
			flipx = !flipx;
			flipy = !flipy;
		}

//      code = spriteram[offs + 1] + ((attr & 0x02) << 7);
		code = spriteram[offs + 1] + ((attr & 0x06) << 7);  // for ninjemak, not sure ?

		drawgfx_transpen(bitmap,cliprect,machine.gfx[2],
				code,
				color + 16 * (spritepalettebank[code >> 2] & 0x0f),
				flipx,flipy,
				sx,sy,15);
	}
}


UINT32 galivan_state::screen_update_galivan(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	m_bg_tilemap->set_scrollx(0, m_galivan_scrollx[0] + 256 * (m_galivan_scrollx[1] & 0x07));
	m_bg_tilemap->set_scrolly(0, m_galivan_scrolly[0] + 256 * (m_galivan_scrolly[1] & 0x07));

	if (m_layers & 0x40)
		bitmap.fill(0, cliprect);
	else
		m_bg_tilemap->draw(bitmap, cliprect, 0, 0);

	if (m_layers & 0x20)
	{
		m_tx_tilemap->draw(bitmap, cliprect, 0, 0);
		m_tx_tilemap->draw(bitmap, cliprect, 1, 0);
		draw_sprites(machine(), bitmap, cliprect);
	}
	else
	{
		draw_sprites(machine(), bitmap, cliprect);
		m_tx_tilemap->draw(bitmap, cliprect, 0, 0);
		m_tx_tilemap->draw(bitmap, cliprect, 1, 0);
	}

	return 0;
}

UINT32 galivan_state::screen_update_ninjemak(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	/* (scrollx[1] & 0x40) does something */
	m_bg_tilemap->set_scrollx(0, m_scrollx);
	m_bg_tilemap->set_scrolly(0, m_scrolly);

	if (m_ninjemak_dispdisable)
		bitmap.fill(0, cliprect);
	else
		m_bg_tilemap->draw(bitmap, cliprect, 0, 0);

	draw_sprites(machine(), bitmap, cliprect);
	m_tx_tilemap->draw(bitmap, cliprect, 0, 0);
	return 0;
}