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

    Irem M57 hardware

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

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


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

  Convert the color PROMs into a more useable format.

  Tropical Angel has two 256x4 character palette PROMs, one 32x8 sprite
  palette PROM, and one 256x4 sprite color lookup table PROM.

  I don't know for sure how the palette PROMs are connected to the RGB
  output, but it's probably something like this; note that RED and BLUE
  are swapped wrt the usual configuration.

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

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

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

	machine().colortable = colortable_alloc(machine(), 32 * 8 + 16);

	/* character palette */
	for (i = 0; i < 256; i++)
	{
		int bit0, bit1, bit2, r, g, b;

		/* red component */
		bit0 = 0;
		bit1 = (color_prom[256] >> 2) & 0x01;
		bit2 = (color_prom[256] >> 3) & 0x01;
		r = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		/* green component */
		bit0 = (color_prom[0] >> 3) & 0x01;
		bit1 = (color_prom[256] >> 0) & 0x01;
		bit2 = (color_prom[256] >> 1) & 0x01;
		g = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		/* blue component */
		bit0 = (color_prom[0] >> 0) & 0x01;
		bit1 = (color_prom[0] >> 1) & 0x01;
		bit2 = (color_prom[0] >> 2) & 0x01;
		b = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;

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

	color_prom += 256;
	/* color_prom now points to the beginning of the sprite palette */

	/* sprite palette */
	for (i = 0; i < 16; i++)
	{
		int bit0, bit1, bit2, r, g, b;

		/* red component */
		bit0 = 0;
		bit1 = (*color_prom >> 6) & 0x01;
		bit2 = (*color_prom >> 7) & 0x01;
		r = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		/* green component */
		bit0 = (*color_prom >> 3) & 0x01;
		bit1 = (*color_prom >> 4) & 0x01;
		bit2 = (*color_prom >> 5) & 0x01;
		g = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
		/* blue component */
		bit0 = (*color_prom >> 0) & 0x01;
		bit1 = (*color_prom >> 1) & 0x01;
		bit2 = (*color_prom >> 2) & 0x01;
		b = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;

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

	color_prom += 16;
	/* color_prom now points to the beginning of the sprite lookup table */


	/* sprite lookup table */
	for (i = 0; i < 32 * 8; i++)
	{
		colortable_entry_set_value(machine().colortable, i + 32 * 8, 256 + (~*color_prom & 0x0f));
		color_prom++;
	}
}


/*************************************
 *
 *  Tilemap info callback
 *
 *************************************/

TILE_GET_INFO_MEMBER(m57_state::get_tile_info)
{
	UINT8 attr = m_videoram[tile_index * 2 + 0];
	UINT16 code = m_videoram[tile_index * 2 + 1] | ((attr & 0xc0) << 2);

	SET_TILE_INFO_MEMBER(m_gfxdecode, 0, code, attr & 0x0f, TILE_FLIPXY(attr >> 4));
}


/*************************************
 *
 *  Video RAM access
 *
 *************************************/

WRITE8_MEMBER(m57_state::m57_videoram_w)
{
	m_videoram[offset] = data;
	m_bg_tilemap->mark_tile_dirty(offset / 2);
}


/*************************************
 *
 *  Video startup
 *
 *************************************/

void m57_state::video_start()
{
	m_bg_tilemap = &machine().tilemap().create(tilemap_get_info_delegate(FUNC(m57_state::get_tile_info),this), TILEMAP_SCAN_ROWS,  8, 8, 32, 32);
	m_bg_tilemap->set_scroll_rows(256);

	save_item(NAME(m_flipscreen));
}


/*************************************
 *
 *  Outputs
 *
 *************************************/

WRITE8_MEMBER(m57_state::m57_flipscreen_w)
{
	/* screen flip is handled both by software and hardware */
	m_flipscreen = (data & 0x01) ^ (~ioport("DSW2")->read() & 0x01);
	m_bg_tilemap->set_flip(m_flipscreen ? (TILEMAP_FLIPY | TILEMAP_FLIPX) : 0);

	coin_counter_w(machine(), 0,data & 0x02);
	coin_counter_w(machine(), 1,data & 0x20);
}


/*************************************
 *
 *  Background rendering
 *
 *************************************/

void m57_state::draw_background(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	int y,x;
	INT16 scrolly;

	// from 64 to 127: not wrapped
	for (y = 64; y < 128; y++)
		m_bg_tilemap->set_scrollx(y, m_scrollram[0x40]);

	m_bg_tilemap->draw(screen, bitmap, cliprect, 0, 0);

	// from 128 to 255: wrapped
	for (y = 128; y <= cliprect.max_y; y++)
	{
		scrolly = m_scrollram[y] + (m_scrollram[y + 0x100] << 8);

		if (scrolly >= 0)
		{
			for (x = cliprect.min_x; x <= cliprect.max_x; x++)
			{
				if ((x + scrolly) <= cliprect.max_x)
					bitmap.pix16(y, x) = bitmap.pix16(y, x + scrolly);
				else
					bitmap.pix16(y, x) = bitmap.pix16(y, cliprect.max_x);
			}
		} else {
			for (x = cliprect.max_x; x >= cliprect.min_x; x--)
			{
				if ((x + scrolly) >= cliprect.min_x)
					bitmap.pix16(y, x) = bitmap.pix16(y, x + scrolly);
				else
					bitmap.pix16(y, x) = bitmap.pix16(y, cliprect.min_x);
			}
		}
	}
}

/*************************************
 *
 *  Sprite rendering
 *
 *************************************/

void m57_state::draw_sprites(bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	int offs;

	for (offs = m_spriteram.bytes() - 4; offs >= 0; offs -= 4)
	{
		UINT8 attributes = m_spriteram[offs + 1];
		int sx = m_spriteram[offs + 3];
		int sy = ((224 - m_spriteram[offs + 0] - 32) & 0xff) + 32;
		int code = m_spriteram[offs + 2];
		int color = attributes & 0x1f;
		int flipy = attributes & 0x80;
		int flipx = attributes & 0x40;

		int tile_number = code & 0x3f;

		int bank = 0;
		if (code & 0x80) bank += 1;
		if (attributes & 0x20) bank += 2;

		if (m_flipscreen)
		{
			sx = 240 - sx;
			sy = 224 - sy;
			flipx = !flipx;
			flipy = !flipy;
		}

		 m_gfxdecode->gfx(1 + bank)->transmask(bitmap,cliprect,
			tile_number,
			color,
			flipx, flipy,
			sx, sy,
			colortable_get_transpen_mask(machine().colortable, m_gfxdecode->gfx(1), color, 256 + 15));
	}
}



/*************************************
 *
 *  Video update
 *
 *************************************/

UINT32 m57_state::screen_update_m57(screen_device &screen, bitmap_ind16 &bitmap, const rectangle &cliprect)
{
	draw_background(screen, bitmap, cliprect);
	draw_sprites(bitmap, cliprect);
	return 0;
}