summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/starshp1.c
blob: 3cc16ad43d10717ad8ef9c7ff12ad53fcadb5282 (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/***************************************************************************

Atari Starship 1 video emulation

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

#include "driver.h"
#include "deprecat.h"
#include "includes/starshp1.h"

UINT8 *starshp1_playfield_ram;
UINT8 *starshp1_hpos_ram;
UINT8 *starshp1_vpos_ram;
UINT8 *starshp1_obj_ram;

int starshp1_ship_explode;
int starshp1_ship_picture;
int starshp1_ship_hoffset;
int starshp1_ship_voffset;
int starshp1_ship_size;

int starshp1_circle_kill;
int starshp1_circle_mod;
int starshp1_circle_hpos;
int starshp1_circle_vpos;
int starshp1_circle_size;

int starshp1_starfield_kill;
int starshp1_phasor;
int starshp1_collision_latch;
int starshp1_mux;
int starshp1_inverse;

static UINT16 *LSFR;

static mame_bitmap *helper;

static tilemap *bg_tilemap;


static void set_pens(colortable_t *colortable)
{
	colortable_palette_set_color(colortable, starshp1_inverse ? 7 : 0, MAKE_RGB(0x00, 0x00, 0x00));
	colortable_palette_set_color(colortable, starshp1_inverse ? 6 : 1, MAKE_RGB(0x1e, 0x1e, 0x1e));
	colortable_palette_set_color(colortable, starshp1_inverse ? 5 : 2, MAKE_RGB(0x4e, 0x4e, 0x4e));
	colortable_palette_set_color(colortable, starshp1_inverse ? 4 : 3, MAKE_RGB(0x6c, 0x6c, 0x6c));
	colortable_palette_set_color(colortable, starshp1_inverse ? 3 : 4, MAKE_RGB(0x93, 0x93, 0x93));
	colortable_palette_set_color(colortable, starshp1_inverse ? 2 : 5, MAKE_RGB(0xb1, 0xb1, 0xb1));
	colortable_palette_set_color(colortable, starshp1_inverse ? 1 : 6, MAKE_RGB(0xe1, 0xe1, 0xe1));
	colortable_palette_set_color(colortable, starshp1_inverse ? 0 : 7, MAKE_RGB(0xff, 0xff, 0xff));
}


PALETTE_INIT( starshp1 )
{
	int i;

	static const UINT16 colortable_source[] =
	{
		0, 3,       /* 0x00 - 0x01 - alpha numerics */
		0, 2,       /* 0x02 - 0x03 - sprites (Z=0) */
		0, 5,       /* 0x04 - 0x05 - sprites (Z=1) */
		0, 2, 4, 6, /* 0x06 - 0x09 - spaceship (EXPLODE=0) */
		0, 6, 6, 7, /* 0x0a - 0x0d - spaceship (EXPLODE=1) */
		5, 2,		/* 0x0e - 0x0f - star field */
		7,			/* 0x10        - phasor */
		5, 7		/* 0x11        - circle */
	};

	/* allocate the colortable */
	machine->colortable = colortable_alloc(machine, 8);

	for (i = 0; i < sizeof(colortable_source) / sizeof(colortable_source[0]); i++)
		colortable_entry_set_value(machine->colortable, i, colortable_source[i]);
}


static TILE_GET_INFO( get_tile_info )
{
	UINT8 code = starshp1_playfield_ram[tile_index];

	SET_TILE_INFO(0, code & 0x3f, 0, 0);
}


VIDEO_START( starshp1 )
{
	UINT16 val = 0;

	int i;

	bg_tilemap = tilemap_create(get_tile_info, tilemap_scan_rows,  16, 8, 32, 32);

	tilemap_set_transparent_pen(bg_tilemap, 0);

	tilemap_set_scrollx(bg_tilemap, 0, -8);

	LSFR = auto_malloc(0x20000);

	for (i = 0; i < 0x10000; i++)
	{
		int bit = (val >> 0xf) ^
				  (val >> 0xc) ^
				  (val >> 0x7) ^
				  (val >> 0x1) ^ 1;

		LSFR[i] = val;

		val = (val << 1) | (bit & 1);
	}

	helper = auto_bitmap_alloc(machine->screen[0].width, machine->screen[0].height, machine->screen[0].format);
}


READ8_HANDLER( starshp1_rng_r )
{
	int x = video_screen_get_hpos(0);
	int y = video_screen_get_vpos(0);

	/* the LFSR is only running in the non-blank region
       of the screen, so this is not quite right */
	if (x > Machine->screen[0].width - 1)
		x = Machine->screen[0].width - 1;
	if (y > Machine->screen[0].height - 1)
		y = Machine->screen[0].height - 1;

	return LSFR[x + (UINT16) (512 * y)];
}


WRITE8_HANDLER( starshp1_ssadd_w )
{
	/*
     * The range of sprite position values doesn't suffice to
     * move the zoomed spaceship sprite over the top and left
     * edges of the screen. These additional values are used
     * to compensate for this. Technically, they cut off the
     * first columns and rows of the spaceship sprite, but in
     * practice they work like offsets in zoomed pixels.
     */

	starshp1_ship_voffset = ((offset & 0xf0) >> 4);
	starshp1_ship_hoffset = ((offset & 0x0f) << 2) | (data & 3);
}


WRITE8_HANDLER( starshp1_sspic_w )
{
	/*
     * Some mysterious game code at address $2CCE is causing
     * erratic images in the target explosion sequence. The
     * following condition is a hack to filter these images.
     */

	if (data != 0x87)
		starshp1_ship_picture = data;
}


WRITE8_HANDLER( starshp1_playfield_w )
{
	if (starshp1_mux != 0)
	{
		offset ^= 0x1f;
		starshp1_playfield_ram[offset] = data;
		tilemap_mark_tile_dirty(bg_tilemap, offset);
	}
}


static void draw_starfield(mame_bitmap* bitmap)
{
	/*
     * The LSFR is reset once per frame at the position of
     * sprite 15. This behavior is quite pointless and not
     * really needed by the game. Not emulated.
     */

	int x;
	int y;

	for (y = 0; y < bitmap->height; y++)
	{
		const UINT16* p = LSFR + (UINT16) (512 * y);

		UINT16* pLine = BITMAP_ADDR16(bitmap, y, 0);

		for (x = 0; x < bitmap->width; x++)
			if ((p[x] & 0x5b56) == 0x5b44)
				pLine[x] = (p[x] & 0x0400) ? 0x0e : 0x0f;
	}
}


static int get_sprite_hpos(int i)
{
	return 2 * (starshp1_hpos_ram[i] ^ 0xff);
}
static int get_sprite_vpos(int i)
{
	return 1 * (starshp1_vpos_ram[i] - 0x07);
}


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

	for (i = 0; i < 14; i++)
	{
		int code = (starshp1_obj_ram[i] & 0xf) ^ 0xf;

		drawgfx(bitmap, machine->gfx[1],
			code % 8,
			code / 8,
			0, 0,
			get_sprite_hpos(i),
			get_sprite_vpos(i),
			cliprect, TRANSPARENCY_PEN, 0);
	}
}


static void draw_spaceship(running_machine *machine, mame_bitmap* bitmap, const rectangle* cliprect)
{
	double scaler = -5 * log(1 - starshp1_ship_size / 256.0); /* ? */

	unsigned xzoom = 2 * 0x10000 * scaler;
	unsigned yzoom = 1 * 0x10000 * scaler;

	int x = get_sprite_hpos(14);
	int y = get_sprite_vpos(14);

	if (x <= 0)
		x -= (xzoom * starshp1_ship_hoffset) >> 16;

	if (y <= 0)
		y -= (yzoom * starshp1_ship_voffset) >> 16;

	drawgfxzoom(bitmap, machine->gfx[2],
		starshp1_ship_picture & 0x03,
		starshp1_ship_explode,
		starshp1_ship_picture & 0x80, 0,
		x, y,
		cliprect, TRANSPARENCY_PEN, 0,
		xzoom, yzoom);
}


static void draw_phasor(mame_bitmap* bitmap)
{
	int i;

	for (i = 128; i < 240; i++)
		if (i >= get_sprite_vpos(13))
		{
			*BITMAP_ADDR16(bitmap, i, 2 * i + 0) = 0x10;
			*BITMAP_ADDR16(bitmap, i, 2 * i + 1) = 0x10;
			*BITMAP_ADDR16(bitmap, i, 2 * (255 - i) + 0) = 0x10;
			*BITMAP_ADDR16(bitmap, i, 2 * (255 - i) + 1) = 0x10;
		}
}


static int get_radius(void)
{
	return 6 * sqrt(starshp1_circle_size);  /* size calibrated by hand */
}
static int get_circle_hpos(void)
{
	return 2 * (3 * starshp1_circle_hpos / 2 - 64);
}
static int get_circle_vpos(void)
{
	return 1 * (3 * starshp1_circle_vpos / 2 - 64);
}


static void draw_circle_line(mame_bitmap *bitmap, int x, int y, int l)
{
	if (y >= 0 && y <= bitmap->height - 1)
	{
		const UINT16* p = LSFR + (UINT16) (512 * y);

		UINT16* pLine = BITMAP_ADDR16(bitmap, y, 0);

		int h1 = x - 2 * l;
		int h2 = x + 2 * l;

		if (h1 < 0)
			h1 = 0;
		if (h2 > bitmap->width - 1)
			h2 = bitmap->width - 1;

		for (x = h1; x <= h2; x++)
			if (starshp1_circle_mod)
			{
				if (p[x] & 1)
					pLine[x] = 0x11;
			}
			else
				pLine[x] = 0x12;
	}
}


static void draw_circle(mame_bitmap* bitmap)
{
	int cx = get_circle_hpos();
	int cy = get_circle_vpos();

	int x = 0;
	int y = get_radius();

	/* Bresenham's circle algorithm */

	int d = 3 - 2 * get_radius();

	while (x <= y)
	{
		draw_circle_line(bitmap, cx, cy - x, y);
		draw_circle_line(bitmap, cx, cy + x, y);
		draw_circle_line(bitmap, cx, cy - y, x);
		draw_circle_line(bitmap, cx, cy + y, x);

		x++;

		if (d < 0)
			d += 4 * x + 6;
		else
			d += 4 * (x - y--) + 10;
	}
}


static int spaceship_collision(mame_bitmap *bitmap, rectangle *rect)
{
	int x;
	int y;

	for (y = rect->min_y; y <= rect->max_y; y++)
	{
		const UINT16* pLine = BITMAP_ADDR16(helper, y, 0);

		for (x = rect->min_x; x <= rect->max_x; x++)
			if (pLine[x] != 0)
				return 1;
	}

	return 0;
}


static int point_in_circle(int x, int y, int center_x, int center_y, int r)
{
	int dx = abs(x - center_x) / 2;
	int dy = abs(y - center_y) / 1;

	return dx * dx + dy * dy < r * r;
}


static int circle_collision(rectangle *rect)
{
	int center_x = get_circle_hpos();
	int center_y = get_circle_vpos();

	int r = get_radius();

	return point_in_circle(rect->min_x, rect->min_y, center_x, center_y, r) ||
		   point_in_circle(rect->min_x, rect->max_y, center_x, center_y, r) ||
		   point_in_circle(rect->max_x, rect->min_y, center_x, center_y, r) ||
		   point_in_circle(rect->max_x, rect->max_y, center_x, center_y, r);
}


VIDEO_UPDATE( starshp1 )
{
	set_pens(machine->colortable);

	fillbitmap(bitmap, machine->pens[0], cliprect);

	if (starshp1_starfield_kill == 0)
		draw_starfield(bitmap);

	draw_sprites(machine, bitmap, cliprect);

	if (starshp1_circle_kill == 0 && starshp1_circle_mod != 0)
		draw_circle(bitmap);

	if (starshp1_attract == 0)
		draw_spaceship(machine, bitmap, cliprect);

	if (starshp1_circle_kill == 0 && starshp1_circle_mod == 0)
		draw_circle(bitmap);

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

	if (starshp1_phasor != 0)
		draw_phasor(bitmap);

	return 0;
}


VIDEO_EOF( starshp1 )
{
	rectangle rect;

	rect.min_x = get_sprite_hpos(13);
	rect.min_y = get_sprite_vpos(13);
	rect.max_x = rect.min_x + machine->gfx[1]->width - 1;
	rect.max_y = rect.min_y + machine->gfx[1]->height - 1;

	if (rect.min_x < 0)
		rect.min_x = 0;
	if (rect.min_y < 0)
		rect.min_y = 0;
	if (rect.max_x > helper->width - 1)
		rect.max_x = helper->width - 1;
	if (rect.max_y > helper->height - 1)
		rect.max_y = helper->height - 1;

	fillbitmap(helper, machine->pens[0], &machine->screen[0].visarea);

	if (starshp1_attract == 0)
		draw_spaceship(machine, helper, &machine->screen[0].visarea);

	if (circle_collision(&machine->screen[0].visarea))
		starshp1_collision_latch |= 1;

	if (circle_collision(&rect))
		starshp1_collision_latch |= 2;

	if (spaceship_collision(helper, &rect))
		starshp1_collision_latch |= 4;

	if (spaceship_collision(helper, &machine->screen[0].visarea))
		starshp1_collision_latch |= 8;
}