summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/digdug.c
blob: ca9654a6adae9efd66dd9ebfb2d6c8bcb874e130 (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
#include "driver.h"
#include "includes/galaga.h"


UINT8 *digdug_videoram,*digdug_objram, *digdug_posram, *digdug_flpram;

static tilemap *bg_tilemap,*tx_tilemap;


static INT32 bg_select, tx_color_mode, bg_disable, bg_color_bank;

static colortable *digdug_colortable;


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

  Convert the color PROMs.

  digdug has one 32x8 palette PROM and two 256x4 color lookup table PROMs
  (one for characters, one for sprites).
  The palette PROM is connected to the RGB output this way:

  bit 7 -- 220 ohm resistor  -- BLUE
        -- 470 ohm resistor  -- BLUE
        -- 220 ohm resistor  -- GREEN
        -- 470 ohm resistor  -- GREEN
        -- 1  kohm resistor  -- GREEN
        -- 220 ohm resistor  -- RED
        -- 470 ohm resistor  -- RED
  bit 0 -- 1  kohm resistor  -- RED

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

PALETTE_INIT( digdug )
{
	int i;

	digdug_colortable = colortable_alloc(machine, 32);

	for (i = 0;i < 32;i++)
	{
		int bit0,bit1,bit2,r,g,b;

		bit0 = (*color_prom >> 0) & 0x01;
		bit1 = (*color_prom >> 1) & 0x01;
		bit2 = (*color_prom >> 2) & 0x01;
		r = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		bit0 = (*color_prom >> 3) & 0x01;
		bit1 = (*color_prom >> 4) & 0x01;
		bit2 = (*color_prom >> 5) & 0x01;
		g = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		bit0 = 0;
		bit1 = (*color_prom >> 6) & 0x01;
		bit2 = (*color_prom >> 7) & 0x01;
		b = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		colortable_palette_set_color(digdug_colortable,i,MAKE_RGB(r,g,b));
		color_prom++;
	}

	/* characters - direct mapping */
	for (i = 0; i < 16; i++)
	{
		colortable_entry_set_value(digdug_colortable, i*2+0, 0);
		colortable_entry_set_value(digdug_colortable, i*2+1, i);
	}

	/* sprites */
	for (i = 0;i < 0x100;i++)
		colortable_entry_set_value(digdug_colortable, 16*2+i, (*color_prom++ & 0x0f) + 0x10);

	/* bg_select */
	for (i = 0;i < 0x100;i++)
		colortable_entry_set_value(digdug_colortable, 16*2+256+i, *color_prom++ & 0x0f);
}



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

  Callbacks for the TileMap code

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

/* convert from 32x32 to 36x28 */
static TILEMAP_MAPPER( tilemap_scan )
{
	int offs;

	row += 2;
	col -= 2;
	if (col & 0x20)
		offs = row + ((col & 0x1f) << 5);
	else
		offs = col + (row << 5);

	return offs;
}


static TILE_GET_INFO( bg_get_tile_info )
{
	UINT8 *rom = memory_region(REGION_GFX4);
	int code = rom[tile_index | (bg_select << 10)];
	/* when the background is "disabled", it is actually still drawn, but using
       a color code that makes all pixels black. There are pullups setting the
       code to 0xf, but also solder pads that optionally connect the lines with
       tilemap RAM, therefore allowing to pick some bits of the color code from
       the top 4 bits of alpha code. This feature is not used by Dig Dug. */
	int color = bg_disable ? 0xf : (code >> 4);
	SET_TILE_INFO(
			2,
			code,
			color | bg_color_bank,
			0);
}

static TILE_GET_INFO( tx_get_tile_info )
{
	UINT8 code = digdug_videoram[tile_index];
	int color;

	/* the hardware has two ways to pick the color, either straight from the
       bottom 4 bits of the character code, or from the top 4 bits through a
       formula. The former method isnot used by Dig Dug and seems kind of
       useless (I don't know what use they were thinking of when they added
       it), anyway here it is reproduced faithfully. */
	if (tx_color_mode)
		color = code & 0x0f;
	else
		color = ((code >> 4) & 0x0e) | ((code >> 3) & 2);

	/* the hardware has two character sets, one normal and one x-flipped. When
       screen is flipped, character y flip is done by the hardware inverting the
       timing signals, while x flip is done by selecting the 2nd character set.
       We reproduce this here, but since the tilemap system automatically flips
       characters when screen is flipped, we have to flip them back. */
	SET_TILE_INFO(
			0,
			(code & 0x7f) | (flip_screen ? 0x80 : 0),
			color,
			flip_screen ? TILE_FLIPX : 0);
}



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

  Start the video hardware emulation.

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

VIDEO_START( digdug )
{
	bg_tilemap = tilemap_create(bg_get_tile_info,tilemap_scan,TILEMAP_TYPE_PEN,     8,8,36,28);
	tx_tilemap = tilemap_create(tx_get_tile_info,tilemap_scan,TILEMAP_TYPE_PEN,8,8,36,28);

	tilemap_set_transparent_pen(tx_tilemap, 0);

	spriteram   = digdug_objram + 0x380;
	spriteram_2 = digdug_posram + 0x380;
	spriteram_3 = digdug_flpram + 0x380;

	state_save_register_global(bg_select);
	state_save_register_global(tx_color_mode);
	state_save_register_global(bg_disable);
	state_save_register_global(bg_color_bank);
}



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

  Memory handlers

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

READ8_HANDLER( digdug_videoram_r )
{
	return digdug_videoram[offset];
}

WRITE8_HANDLER( digdug_videoram_w )
{
	digdug_videoram[offset] = data;
	tilemap_mark_tile_dirty(tx_tilemap,offset & 0x3ff);
}

WRITE8_HANDLER( digdug_PORT_w )
{
	switch (offset)
	{
		case 0:	/* select background picture */
		case 1:
			{
				int shift = offset;
				int mask = 1 << shift;

				if ((bg_select & mask) != ((data & 1) << shift))
				{
					bg_select = (bg_select & ~mask) | ((data & 1) << shift);
					tilemap_mark_all_tiles_dirty(bg_tilemap);
				}
			}
			break;

		case 2:	/* select alpha layer color mode (see tx_get_tile_info) */
			if (tx_color_mode != (data & 1))
			{
				tx_color_mode = data & 1;
				tilemap_mark_all_tiles_dirty(tx_tilemap);
			}
			break;

		case 3:	/* "disable" background (see bg_get_tile_info) */
			if (bg_disable != (data & 1))
			{
				bg_disable = data & 1;
				tilemap_mark_all_tiles_dirty(bg_tilemap);
			}
			break;

		case 4:	/* background color bank */
		case 5:
			{
				int shift = offset;
				int mask = 1 << shift;

				if ((bg_color_bank & mask) != ((data & 1) << shift))
				{
					bg_color_bank = (bg_color_bank & ~mask) | ((data & 1) << shift);
					tilemap_mark_all_tiles_dirty(bg_tilemap);
				}
			}
			break;

		case 6:	/* n.c. */
			break;

		case 7:	/* FLIP */
			flip_screen_set(data & 1);
			break;
	}
}



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

  Display refresh

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

static rectangle spritevisiblearea =
{
	2*8, 34*8-1,
	0*8, 28*8-1
};

static void draw_sprites(running_machine* machine, mame_bitmap *bitmap, const rectangle *cliprect )
{
	int offs;

	for (offs = 0;offs < 0x80;offs += 2)
	{
		static int gfx_offs[2][2] =
		{
			{ 0, 1 },
			{ 2, 3 }
		};
		int sprite = spriteram[offs];
		int color = spriteram[offs+1] & 0x3f;
		int sx = spriteram_2[offs+1] - 40+1;
		int sy = 256 - spriteram_2[offs] + 1;	// sprites are buffered and delayed by one scanline
		int flipx = (spriteram_3[offs] & 0x01);
		int flipy = (spriteram_3[offs] & 0x02) >> 1;
		int size  = (sprite & 0x80) >> 7;
		int x,y;

		if (size)
			sprite = (sprite & 0xc0) | ((sprite & ~0xc0) << 2);

		if (flip_screen)
		{
			flipx ^= 1;
			flipy ^= 1;
		}

		sy -= 16 * size;
		sy = (sy & 0xff) - 32;	// fix wraparound

		for (y = 0;y <= size;y++)
		{
			for (x = 0;x <= size;x++)
			{
				UINT32 transmask = colortable_get_transpen_mask(digdug_colortable, machine->gfx[1], color, 0x1f);
				drawgfx(bitmap,machine->gfx[1],
					sprite + gfx_offs[y ^ (size * flipy)][x ^ (size * flipx)],
					color,
					flipx,flipy,
					((sx + 16*x) & 0xff), sy + 16*y,
					&spritevisiblearea,TRANSPARENCY_PENS,transmask);
				/* wraparound */
				drawgfx(bitmap,machine->gfx[1],
					sprite + gfx_offs[y ^ (size * flipy)][x ^ (size * flipx)],
					color,
					flipx,flipy,
					((sx + 16*x) & 0xff) + 0x100, sy + 16*y,
					&spritevisiblearea,TRANSPARENCY_PENS,transmask);
			}
		}
	}
}


VIDEO_UPDATE( digdug )
{
	tilemap_draw(bitmap,cliprect,bg_tilemap,0,0);
	tilemap_draw(bitmap,cliprect,tx_tilemap,0,0);

	draw_sprites(machine,bitmap,cliprect);
	return 0;
}