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

    video.c

    Functions to emulate the video hardware of the machine.

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

#include "driver.h"

UINT8 *tp84_videoram2, *tp84_colorram2;

static int col0;

/*
sprites are multiplexed, so we have to buffer the spriteram
scanline by scanline.
*/
static UINT8 *sprite_mux_buffer;
static int scanline;

static tilemap *bg_tilemap, *fg_tilemap;

/*
-The colortable is divided in 2 part:
 -The characters colors
 -The sprites colors

-The characters colors are indexed like this:
 -2 bits from the characters
 -4 bits from the attribute in colorram
 -2 bits from col0 (d3-d4)
 -3 bits from col0 (d0-d1-d2)
-So, there is 2048 bytes for the characters

-The sprites colors are indexed like this:
 -4 bits from the sprites (16 colors)
 -4 bits from the attribute of the sprites
 -3 bits from col0 (d0-d1-d2)
-So, there is 2048 bytes for the sprites

*/
/*
     The RGB signals are generated by 3 proms 256X4 (prom 2C, 2D and 1E)
        The resistors values are:
            1K  ohm
            470 ohm
            220 ohm
            100 ohm
*/
PALETTE_INIT( tp84 )
{
	int i;
	#define TOTAL_COLORS(gfxn) (machine->gfx[gfxn]->total_colors * machine->gfx[gfxn]->color_granularity)
	#define COLOR(gfxn,offs) (colortable[machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])


	for (i = 0;i < machine->drv->total_colors;i++)
	{
		int bit0,bit1,bit2,bit3,r,g,b;

		/* red component */
		bit0 = (color_prom[0] >> 0) & 0x01;
		bit1 = (color_prom[0] >> 1) & 0x01;
		bit2 = (color_prom[0] >> 2) & 0x01;
		bit3 = (color_prom[0] >> 3) & 0x01;
		r = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
		/* green component */
		bit0 = (color_prom[machine->drv->total_colors] >> 0) & 0x01;
		bit1 = (color_prom[machine->drv->total_colors] >> 1) & 0x01;
		bit2 = (color_prom[machine->drv->total_colors] >> 2) & 0x01;
		bit3 = (color_prom[machine->drv->total_colors] >> 3) & 0x01;
		g = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;
		/* blue component */
		bit0 = (color_prom[2*machine->drv->total_colors] >> 0) & 0x01;
		bit1 = (color_prom[2*machine->drv->total_colors] >> 1) & 0x01;
		bit2 = (color_prom[2*machine->drv->total_colors] >> 2) & 0x01;
		bit3 = (color_prom[2*machine->drv->total_colors] >> 3) & 0x01;
		b = 0x0e * bit0 + 0x1f * bit1 + 0x42 * bit2 + 0x90 * bit3;

		palette_set_color(machine,i,MAKE_RGB(r,g,b));

		color_prom++;
	}

	color_prom += 2*machine->drv->total_colors;
	/* color_prom now points to the beginning of the lookup table */


	/* characters use colors 128-255 */
	for (i = 0;i < TOTAL_COLORS(0)/8;i++)
	{
		int j;


		for (j = 0;j < 8;j++)
			COLOR(0,i+256*j) = *color_prom + 128 + 16*j;

		color_prom++;
	}

	/* sprites use colors 0-127 */
	for (i = 0;i < TOTAL_COLORS(1)/8;i++)
	{
		int j;


		for (j = 0;j < 8;j++)
		{
			if (*color_prom) COLOR(1,i+256*j) = *color_prom + 16*j;
			else COLOR(1,i+256*j) = 0;	/* preserve transparency */
		}

		color_prom++;
	}
}


WRITE8_HANDLER( tp84_videoram_w )
{
	videoram[offset] = data;
	tilemap_mark_tile_dirty(bg_tilemap, offset);
}

WRITE8_HANDLER( tp84_colorram_w )
{
	colorram[offset] = data;
	tilemap_mark_tile_dirty(bg_tilemap, offset);
}

WRITE8_HANDLER( tp84_videoram2_w )
{
	tp84_videoram2[offset] = data;
	tilemap_mark_tile_dirty(fg_tilemap, offset);
}

WRITE8_HANDLER( tp84_colorram2_w )
{
	tp84_colorram2[offset] = data;
	tilemap_mark_tile_dirty(fg_tilemap, offset);
}

WRITE8_HANDLER( tp84_scroll_x_w )
{
	tilemap_set_scrollx(bg_tilemap, 0, data);
}

WRITE8_HANDLER( tp84_scroll_y_w )
{
	tilemap_set_scrolly(bg_tilemap, 0, data);
}

WRITE8_HANDLER( tp84_flipscreen_x_w )
{
	flip_screen_x_set(data & 0x01);
}

WRITE8_HANDLER( tp84_flipscreen_y_w )
{
	flip_screen_y_set(data & 0x01);
}

/*****
  col0 is a register to index the color Proms
*****/
WRITE8_HANDLER( tp84_col0_w )
{
	if (col0 != data)
	{
		col0 = data;
		tilemap_mark_all_tiles_dirty(ALL_TILEMAPS);
	}
}

/* Return the current video scan line */
READ8_HANDLER( tp84_scanline_r )
{
	return scanline;
}

static TILE_GET_INFO( get_bg_tile_info )
{
	int coloffs = ((col0 & 0x18) << 1) + ((col0 & 0x07) << 6);
	int attr = colorram[tile_index];
	int code = videoram[tile_index] + ((attr & 0x30) << 4);
	int color = (attr & 0x0f) + coloffs;
	int flags = ((attr & 0x40) ? TILE_FLIPX : 0) | ((attr & 0x80) ? TILE_FLIPY : 0);

	SET_TILE_INFO(0, code, color, flags);
}

static TILE_GET_INFO( get_fg_tile_info )
{
	int coloffs = ((col0 & 0x18) << 1) + ((col0 & 0x07) << 6);
	int attr = tp84_colorram2[tile_index];
	int code = tp84_videoram2[tile_index]+ ((attr & 0x30) << 4);
	int color = (attr & 0x0f) + coloffs;
	int flags = ((attr & 0x40) ? TILE_FLIPX : 0) | ((attr & 0x80) ? TILE_FLIPY : 0) | TILE_FORCE_LAYER0;

	SET_TILE_INFO(0, code, color, flags);
}

VIDEO_START( tp84 )
{
	bg_tilemap = tilemap_create(get_bg_tile_info, tilemap_scan_rows,
		TILEMAP_TYPE_PEN, 8, 8, 32, 32);

	fg_tilemap = tilemap_create(get_fg_tile_info, tilemap_scan_rows,
		TILEMAP_TYPE_PEN, 8, 8, 32, 32);

	tilemap_set_transparent_pen(fg_tilemap, 0);

	sprite_mux_buffer = auto_malloc(256 * spriteram_size);
}

static void draw_sprites(running_machine *machine, mame_bitmap *bitmap, const rectangle *cliprect)
{
	const gfx_element *gfx = machine->gfx[1];
	int offs;
	int line;
	int coloffset = ((col0&0x07) << 4);

	for (line = cliprect->min_y; line <= cliprect->max_y; line++)
	{
		UINT8 *sr;
		rectangle clip = *cliprect;

		sr = sprite_mux_buffer + line * spriteram_size;
		clip.min_y = clip.max_y = line;

		for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
		{
			int code,color,sx,sy,flipx,flipy;

			sx = sr[offs];
			sy = 240 - sr[offs + 3];

			if (sy > line-16 && sy <= line)
			{
				code = sr[offs + 1];
				color = (sr[offs + 2] & 0x0f) + coloffset;
				flipx = ~sr[offs + 2] & 0x40;
				flipy = sr[offs + 2] & 0x80;

				drawgfx(bitmap,gfx,
						code,
						color,
						flipx,flipy,
						sx,sy,
						&clip,TRANSPARENCY_COLOR,0);
			}
		}
	}
}

VIDEO_UPDATE( tp84 )
{
	rectangle clip;

	tilemap_draw(bitmap, cliprect, bg_tilemap, 0, 0);
	draw_sprites(machine, bitmap, cliprect);

	/* draw top fg_tilemap status layer part */
	clip.min_x = machine->screen[0].visarea.min_x;
	clip.max_x = machine->screen[0].visarea.min_x+15;
	clip.min_y = machine->screen[0].visarea.min_y;
	clip.max_y = machine->screen[0].visarea.max_y;
	tilemap_draw(bitmap, &clip, fg_tilemap, 0, 0);

	/* the middle part of fg_tilemap seems to be used as normal ram and is skipped */

	/* draw bottom fg_tilemap status layer part */
	clip.min_x = machine->screen[0].visarea.max_x-15;
	clip.max_x = machine->screen[0].visarea.max_x;
	clip.min_y = machine->screen[0].visarea.min_y;
	clip.max_y = machine->screen[0].visarea.max_y;
	tilemap_draw(bitmap, &clip, fg_tilemap, 0, 0);
	return 0;
}

INTERRUPT_GEN( tp84_6809_interrupt )
{
	scanline = 255 - cpu_getiloops();

	memcpy(sprite_mux_buffer + scanline * spriteram_size,spriteram,spriteram_size);

	if (scanline == 255)
		irq0_line_hold();
}