summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/stfight.c
blob: 48bca0487fca6fa4ed0aeaecfdb5d8d00c378a5c (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
/***************************************************************************

  video.c

  Functions to emulate the video hardware of the machine.

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

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


/*
        Graphics ROM Format
        ===================

        Each tile is 8x8 pixels
        Each composite tile is 2x2 tiles, 16x16 pixels
        Each screen is 32x32 composite tiles, 64x64 tiles, 256x256 pixels
        Each layer is a 4-plane bitmap 8x16 screens, 2048x4096 pixels

        There are 4x256=1024 composite tiles defined for each layer

        Each layer is mapped using 2 bytes/composite tile
        - one byte for the tile
        - one byte for the tile bank, attribute
            - b7,b5     tile bank (0-3)

        Each pixel is 4 bits = 16 colours.

 */

PALETTE_INIT( stfight )
{
	int i;

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

	/* text uses colors 0xc0-0xcf */
	for (i = 0; i < 0x40; i++)
	{
		UINT8 ctabentry = (color_prom[i] & 0x0f) | 0xc0;
		colortable_entry_set_value(machine.colortable, i, ctabentry);
	}

	/* fg uses colors 0x40-0x7f */
	for (i = 0x40; i < 0x140; i++)
	{
		UINT8 ctabentry = (color_prom[i + 0x1c0] & 0x0f) | ((color_prom[i + 0x0c0] & 0x03) << 4) | 0x40;
		colortable_entry_set_value(machine.colortable, i, ctabentry);
	}

	/* bg uses colors 0-0x3f */
	for (i = 0x140; i < 0x240; i++)
	{
		UINT8 ctabentry = (color_prom[i + 0x2c0] & 0x0f) | ((color_prom[i + 0x1c0] & 0x03) << 4);
		colortable_entry_set_value(machine.colortable, i, ctabentry);
	}

	/* bg uses colors 0x80-0xbf */
	for (i = 0x240; i < 0x340; i++)
	{
		UINT8 ctabentry = (color_prom[i + 0x3c0] & 0x0f) | ((color_prom[i + 0x2c0] & 0x03) << 4) | 0x80;
		colortable_entry_set_value(machine.colortable, i, ctabentry);
	}
}


static void set_pens(running_machine &machine)
{
	int i;

	for (i = 0; i < 0x100; i++)
	{
		UINT16 data = machine.generic.paletteram.u8[i] | (machine.generic.paletteram2.u8[i] << 8);
		rgb_t color = MAKE_RGB(pal4bit(data >> 4), pal4bit(data >> 0), pal4bit(data >> 8));

		colortable_palette_set_color(machine.colortable, i, color);
	}
}


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

  Callbacks for the TileMap code

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

static TILEMAP_MAPPER( fg_scan )
{
	/* logical (col,row) -> memory offset */
	return (col & 0x0f) + ((row & 0x0f) << 4) + ((col & 0x70) << 4) + ((row & 0xf0) << 7);
}

static TILE_GET_INFO( get_fg_tile_info )
{
	UINT8   *fgMap = machine.region("gfx5")->base();
	int attr,tile_base;

	attr = fgMap[0x8000+tile_index];
	tile_base = ((attr & 0x80) << 2) | ((attr & 0x20) << 3);

	SET_TILE_INFO(
			1,
			tile_base + fgMap[tile_index],
			attr & 0x07,
			0);
}

static TILEMAP_MAPPER( bg_scan )
{
	/* logical (col,row) -> memory offset */
	return ((col & 0x0e) >> 1) + ((row & 0x0f) << 3) + ((col & 0x70) << 3) +
			((row & 0x80) << 3) + ((row & 0x10) << 7) + ((col & 0x01) << 12) +
			((row & 0x60) << 8);
}

static TILE_GET_INFO( get_bg_tile_info )
{
	UINT8   *bgMap = machine.region("gfx6")->base();
	int attr,tile_bank,tile_base;

	attr = bgMap[0x8000+tile_index];
	tile_bank = (attr & 0x20) >> 5;
	tile_base = (attr & 0x80) << 1;

	SET_TILE_INFO(
			2+tile_bank,
			tile_base + bgMap[tile_index],
			attr & 0x07,
			0);
}

static TILE_GET_INFO( get_tx_tile_info )
{
	stfight_state *state = machine.driver_data<stfight_state>();
	UINT8 attr = state->m_text_attr_ram[tile_index];
	int color = attr & 0x0f;

	tileinfo->group = color;

	SET_TILE_INFO(
			0,
			state->m_text_char_ram[tile_index] + ((attr & 0x80) << 1),
			attr & 0x0f,
			TILE_FLIPYX((attr & 0x60) >> 5));
}


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

  Start the video hardware emulation.

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

VIDEO_START( stfight )
{
	stfight_state *state = machine.driver_data<stfight_state>();
	state->m_bg_tilemap = tilemap_create(machine, get_bg_tile_info,bg_scan,     16,16,128,256);
	state->m_fg_tilemap = tilemap_create(machine, get_fg_tile_info,fg_scan,16,16,128,256);
	state->m_tx_tilemap = tilemap_create(machine, get_tx_tile_info,tilemap_scan_rows, 8,8,32,32);

	tilemap_set_transparent_pen(state->m_fg_tilemap,0x0f);
	colortable_configure_tilemap_groups(machine.colortable, state->m_tx_tilemap, machine.gfx[0], 0xcf);
}



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

  Memory handlers

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

WRITE8_HANDLER( stfight_text_char_w )
{
	stfight_state *state = space->machine().driver_data<stfight_state>();
	state->m_text_char_ram[offset] = data;
	tilemap_mark_tile_dirty(state->m_tx_tilemap,offset);
}

WRITE8_HANDLER( stfight_text_attr_w )
{
	stfight_state *state = space->machine().driver_data<stfight_state>();
	state->m_text_attr_ram[offset] = data;
	tilemap_mark_tile_dirty(state->m_tx_tilemap,offset);
}

WRITE8_HANDLER( stfight_sprite_bank_w )
{
	stfight_state *state = space->machine().driver_data<stfight_state>();
	state->m_sprite_base = ( ( data & 0x04 ) << 7 ) |
				          ( ( data & 0x01 ) << 8 );
}

WRITE8_HANDLER( stfight_vh_latch_w )
{
	stfight_state *state = space->machine().driver_data<stfight_state>();
	int scroll;


	state->m_vh_latch_ram[offset] = data;

	switch( offset )
	{
		case 0x00:
		case 0x01:
			scroll = (state->m_vh_latch_ram[1] << 8) | state->m_vh_latch_ram[0];
			tilemap_set_scrollx(state->m_fg_tilemap,0,scroll);
			break;

		case 0x02:
		case 0x03:
			scroll = (state->m_vh_latch_ram[3] << 8) | state->m_vh_latch_ram[2];
			tilemap_set_scrolly(state->m_fg_tilemap,0,scroll);
			break;

		case 0x04:
		case 0x05:
			scroll = (state->m_vh_latch_ram[5] << 8) | state->m_vh_latch_ram[4];
			tilemap_set_scrollx(state->m_bg_tilemap,0,scroll);
			break;

		case 0x06:
		case 0x08:
			scroll = (state->m_vh_latch_ram[8] << 8) | state->m_vh_latch_ram[6];
			tilemap_set_scrolly(state->m_bg_tilemap,0,scroll);
			break;

		case 0x07:
			tilemap_set_enable(state->m_tx_tilemap,data & 0x80);
			/* 0x40 = sprites */
			tilemap_set_enable(state->m_bg_tilemap,data & 0x20);
			tilemap_set_enable(state->m_fg_tilemap,data & 0x10);
			flip_screen_set(space->machine(), data & 0x01);
			break;
	}
}

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

  Display refresh

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

static void draw_sprites(running_machine &machine, bitmap_t *bitmap, const rectangle *cliprect)
{
	stfight_state *state = machine.driver_data<stfight_state>();
	int offs,sx,sy;

	for (offs = 0;offs < 4096;offs += 32)
	{
		int code;
		int attr = state->m_sprite_ram[offs+1];
		int flipx = attr & 0x10;
		int color = attr & 0x0f;
		int pri = (attr & 0x20) >> 5;

		sy = state->m_sprite_ram[offs+2];
		sx = state->m_sprite_ram[offs+3];

		// non-active sprites have zero y coordinate value
		if( sy > 0 )
		{
			// sprites which wrap onto/off the screen have
			// a sign extension bit in the sprite attribute
			if( sx >= 0xf0 )
			{
				if (attr & 0x80)
				    sx -= 0x100;
			}

			if (flip_screen_get(machine))
			{
				sx = 240 - sx;
				sy = 240 - sy;
				flipx = !flipx;
			}

			code = state->m_sprite_base + state->m_sprite_ram[offs];

			pdrawgfx_transpen(bitmap,cliprect,machine.gfx[4],
				     code,
					 color,
					 flipx,flip_screen_get(machine),
					 sx,sy,
				     machine.priority_bitmap,
					 pri ? 0x02 : 0,0x0f);
		}
	}
}


SCREEN_UPDATE( stfight )
{
	stfight_state *state = screen->machine().driver_data<stfight_state>();
	set_pens(screen->machine());

	bitmap_fill(screen->machine().priority_bitmap,cliprect,0);

	bitmap_fill(bitmap,cliprect,0);	/* in case state->m_bg_tilemap is disabled */
	tilemap_draw(bitmap,cliprect,state->m_bg_tilemap,0,0);
	tilemap_draw(bitmap,cliprect,state->m_fg_tilemap,0,1);

	/* Draw sprites (may be obscured by foreground layer) */
	if (state->m_vh_latch_ram[0x07] & 0x40)
		draw_sprites(screen->machine(), bitmap,cliprect);

	tilemap_draw(bitmap,cliprect,state->m_tx_tilemap,0,0);
	return 0;
}