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

    Sega "Space Tactics" Driver

    Frank Palazzolo (palazzol@home.com)


The Video system used in Space Tactics is unusual.
Here are my notes on how the video system works:

There are 4, 4K pages of Video RAM. (B,D,E & F)

The first 1K of each VRAM page contains the following:
0 0 V V V V V H H H H H   offset value for each 8x8 bitmap
     (v-tile)  (h-tile)

The offset values are used to generate an access into the
last 2K of the VRAM page:
1 D D D D D D D D V V V   here we find 8x8 character data
     (offset)    (line)

In addition, in Page B, the upper nibble of the offset is
also used to select the color palette for the tile.

Page B, D, E, and F all work similarly, except that pages
D, E, and F can be offset from Page B by a given
number of scanlines, based on the contents of the offset
RAM

The offset RAM is addressed in this format:
1 0 0 0 P P P V V V V V V V V V
        (Page)   (scanline)
Page 4=D, 5=E, 6=F

Page D, E, and F are drawn offset from the top of the screen,
starting on the first scanline which contains a 1 in the
appropriate memory location

---

The composited monitor image is seen in a mirror.  It appears
to move when the player moves the handle, due to motors which
tilt the mirror up and down, and the monitor left and right.

---

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

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



/*************************************
 *
 *  Palette
 *
 *************************************/

static PALETTE_INIT( stactics )
{
	int i;

	for (i = 0; i < 0x400; i++)
	{
		int bit0 = (color_prom[i] >> 0) & 0x01;
		int bit1 = (color_prom[i] >> 1) & 0x01;
		int bit2 = (color_prom[i] >> 2) & 0x01;
		int bit3 = (color_prom[i] >> 3) & 0x01;

		/* red component */
		int r = 0xff * bit0;

		/* green component */
		int g = 0xff * bit1 - 0xcc * bit3;

		/* blue component */
		int b = 0xff * bit2;

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



/*************************************
 *
 *  Scrolling
 *
 *************************************/

WRITE8_HANDLER( stactics_scroll_ram_w )
{
	stactics_state *state = space->machine().driver_data<stactics_state>();

	if (data & 0x01)
	{
		switch (offset >> 8)
		{
			case 4: state->m_y_scroll_d = offset & 0xff; break;
			case 5: state->m_y_scroll_e = offset & 0xff; break;
			case 6: state->m_y_scroll_f = offset & 0xff; break;
		}
	}
}



/*************************************
 *
 *  Frane counter
 *
 *************************************/

CUSTOM_INPUT( stactics_get_frame_count_d3 )
{
	stactics_state *state = field.machine().driver_data<stactics_state>();

	return (state->m_frame_count >> 3) & 0x01;
}



/*************************************
 *
 *  Beam handling
 *
 *************************************/

WRITE8_HANDLER( stactics_speed_latch_w )
{
	stactics_state *state = space->machine().driver_data<stactics_state>();

	/* This writes to a shift register which is clocked by   */
	/* a 555 oscillator.  This value determines the speed of */
	/* the LED fire beams as follows:                        */

	/*   555_freq / bits_in_SR * edges_in_SR / states_in_PR67 / frame_rate */
	/*      = num_led_states_per_frame  */
	/*   36439 / 8 * x / 32 / 60 ~= 19/8*x */

	/* Here, we will count the number of rising edges in the shift register */

	int i;
	int num_rising_edges = 0;

	for(i=0;i<8;i++)
	{
		if ( (((data>>i)&0x01) == 1) && (((data>>((i+1)%8))&0x01) == 0))
			num_rising_edges++;
	}

	state->m_beam_states_per_frame = num_rising_edges*19/8;
}


WRITE8_HANDLER( stactics_shot_trigger_w )
{
	stactics_state *state = space->machine().driver_data<stactics_state>();

	state->m_shot_standby = 0;
}


WRITE8_HANDLER( stactics_shot_flag_clear_w )
{
	stactics_state *state = space->machine().driver_data<stactics_state>();

	state->m_shot_arrive = 0;
}


CUSTOM_INPUT( stactics_get_shot_standby )
{
	stactics_state *state = field.machine().driver_data<stactics_state>();

	return state->m_shot_standby;
}


CUSTOM_INPUT( stactics_get_not_shot_arrive )
{
	stactics_state *state = field.machine().driver_data<stactics_state>();

	return !state->m_shot_arrive;
}


static void update_beam(stactics_state *state)
{
	/* first, update the firebeam state */
	state->m_old_beam_state = state->m_beam_state;
	if (state->m_shot_standby == 0)
		state->m_beam_state = state->m_beam_state + state->m_beam_states_per_frame;

	/* These are thresholds for the two shots from the LED fire ROM */
	/* (Note: There are two more for sound triggers, */
	/*        whenever that gets implemented)        */
	if ((state->m_old_beam_state < 0x8b) & (state->m_beam_state >= 0x8b))
		state->m_shot_arrive = 1;

	if ((state->m_old_beam_state < 0xca) & (state->m_beam_state >= 0xca))
		state->m_shot_arrive = 1;

	if (state->m_beam_state >= 0x100)
	{
		state->m_beam_state = 0;
		state->m_shot_standby = 1;
	}
}



/*************************************
 *
 *  Video drawing
 *
 *************************************/

INLINE int get_pixel_on_plane(UINT8 *videoram, UINT8 y, UINT8 x, UINT8 y_scroll)
{
	UINT8 code;
	UINT8 gfx;

	/* compute effective row */
	y = y - y_scroll;

	/* get the character code at the given pixel */
	code = videoram[((y >> 3) << 5) | (x >> 3)];

	/* get the gfx byte */
	gfx = videoram[0x800 | (code << 3) | (y & 0x07)];

	/* return the appropriate pixel within the byte */
	return (gfx >> (~x & 0x07)) & 0x01;
}


static void draw_background(stactics_state *state, bitmap_t *bitmap, const rectangle *cliprect)
{
	int y;

	bitmap_fill(bitmap, cliprect, 0);

	/* for every row */
	for (y = 0; y < 0x100; y++)
	{
		int x;

		/* for every pixel on the row */
		for (x = 0; x < 0x100; x++)
		{
			/* get the pixels for the four planes */
			int pixel_b = get_pixel_on_plane(state->m_videoram_b, y, x, 0);
			int pixel_d = get_pixel_on_plane(state->m_videoram_d, y, x, state->m_y_scroll_d);
			int pixel_e = get_pixel_on_plane(state->m_videoram_e, y, x, state->m_y_scroll_e);
			int pixel_f = get_pixel_on_plane(state->m_videoram_f, y, x, state->m_y_scroll_f);

			/* get the color for this pixel */
			UINT8 color = state->m_videoram_b[((y >> 3) << 5) | (x >> 3)] >> 4;

			/* assemble the pen index */
			int pen = color |
					  (pixel_b << 4) |
					  (pixel_f << 5) |
					  (pixel_e << 6) |
					  (pixel_d << 7) |
					  ((state->m_palette[0] & 0x01) << 8) |
					  ((state->m_palette[1] & 0x01) << 9);

			/* compute the effective pixel coordinate after adjusting for the
               mirror movement - this is mechanical on the real machine */
			int sy = y + state->m_vert_pos;
			int sx = x - state->m_horiz_pos;

			/* plot if visible */
			if ((sy >= 0) && (sy < 0x100) && (sx >= 0) && (sx < 0x100))
				*BITMAP_ADDR16(bitmap, sy, sx) = pen;
		}
	}
}



/*************************************
 *
 *  Non-video artwork
 *
 *************************************/

/* from 7448 datasheet */
static const int to_7seg[0x10] =
{
	0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7c, 0x07,
	0x7f, 0x67, 0x58, 0x4c, 0x62, 0x69, 0x78, 0x00
};


static void set_indicator_leds(int data, const char *output_name, int base_index)
{
	/* decode the data */
	data = to_7seg[~data & 0x0f];

	/* set the 4 LEDs */
	output_set_indexed_value(output_name, base_index + 0, (data >> 2) & 0x01);
	output_set_indexed_value(output_name, base_index + 1, (data >> 6) & 0x01);
	output_set_indexed_value(output_name, base_index + 2, (data >> 5) & 0x01);
	output_set_indexed_value(output_name, base_index + 3, (data >> 4) & 0x01);
}


static void update_artwork(running_machine &machine, stactics_state *state)
{
	int i;
	UINT8 *beam_region = machine.region("user1")->base();

	/* set the lamps first */
	output_set_indexed_value("base_lamp", 4, state->m_lamps[0] & 0x01);
	output_set_indexed_value("base_lamp", 3, state->m_lamps[1] & 0x01);
	output_set_indexed_value("base_lamp", 2, state->m_lamps[2] & 0x01);
	output_set_indexed_value("base_lamp", 1, state->m_lamps[3] & 0x01);
	output_set_indexed_value("base_lamp", 0, state->m_lamps[4] & 0x01);
	output_set_value("start_lamp",   state->m_lamps[5] & 0x01);
	output_set_value("barrier_lamp", state->m_lamps[6] & 0x01);  /* this needs to flash on/off, not implemented */

	/* laser beam - loop for each LED */
	for (i = 0; i < 0x40; i++)
	{
		offs_t beam_data_offs = ((i & 0x08) << 7) | ((i & 0x30) << 4) | state->m_beam_state;
		UINT8 beam_data = beam_region[beam_data_offs];
		int on = (beam_data >> (i & 0x07)) & 0x01;

		output_set_indexed_value("beam_led_left", i, on);
		output_set_indexed_value("beam_led_right", i, on);
	}

	/* sight LED */
	output_set_value("sight_led", *state->m_motor_on & 0x01);

	/* score display */
	for (i = 0x01; i < 0x07; i++)
		output_set_digit_value(i - 1, to_7seg[~state->m_display_buffer[i] & 0x0f]);

	/* credits indicator */
	set_indicator_leds(state->m_display_buffer[0x07], "credit_led", 0x00);
	set_indicator_leds(state->m_display_buffer[0x08], "credit_led", 0x04);

	/* barriers indicator */
	set_indicator_leds(state->m_display_buffer[0x09], "barrier_led", 0x00);
	set_indicator_leds(state->m_display_buffer[0x0a], "barrier_led", 0x04);
	set_indicator_leds(state->m_display_buffer[0x0b], "barrier_led", 0x08);

	/* rounds indicator */
	set_indicator_leds(state->m_display_buffer[0x0c], "round_led", 0x00);
	set_indicator_leds(state->m_display_buffer[0x0d], "round_led", 0x04);
	set_indicator_leds(state->m_display_buffer[0x0e], "round_led", 0x08);
	set_indicator_leds(state->m_display_buffer[0x0f], "round_led", 0x0c);
}



/*************************************
 *
 *  Start
 *
 *************************************/

static VIDEO_START( stactics )
{
	stactics_state *state = machine.driver_data<stactics_state>();

	state->m_y_scroll_d = 0;
	state->m_y_scroll_e = 0;
	state->m_y_scroll_f = 0;

	state->m_frame_count = 0;
	state->m_shot_standby = 1;
	state->m_shot_arrive = 0;
	state->m_beam_state = 0;
	state->m_old_beam_state = 0;
}



/*************************************
 *
 *  Update
 *
 *************************************/

static SCREEN_UPDATE( stactics )
{
	stactics_state *state = screen->machine().driver_data<stactics_state>();

	update_beam(state);
	draw_background(state, bitmap, cliprect);
	update_artwork(screen->machine(), state);

	state->m_frame_count = (state->m_frame_count + 1) & 0x0f;

	return 0;
}



/*************************************
 *
 *  Machine driver
 *
 *************************************/

MACHINE_CONFIG_FRAGMENT( stactics_video )

	MCFG_VIDEO_ATTRIBUTES(VIDEO_ALWAYS_UPDATE)

	MCFG_SCREEN_ADD("screen", RASTER)
	MCFG_SCREEN_REFRESH_RATE(60)
	MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(0))
	MCFG_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
	MCFG_SCREEN_SIZE(32*8, 32*8)
	MCFG_SCREEN_VISIBLE_AREA(0*8, 32*8-1, 0*8, 30*8-1)
	MCFG_SCREEN_UPDATE(stactics)

	MCFG_PALETTE_LENGTH(0x400)

	MCFG_PALETTE_INIT(stactics)
	MCFG_VIDEO_START(stactics)
MACHINE_CONFIG_END