summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/video/vectrex.c
blob: 4630ad72c97b39e89a35d1213fb07b209b3ce3a9 (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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <math.h>
#include "emu.h"
#include "includes/vectrex.h"
#include "video/vector.h"
#include "machine/6522via.h"
#include "cpu/m6809/m6809.h"
#include "sound/ay8910.h"
#include "sound/dac.h"


#define ANALOG_DELAY 7800

#define INT_PER_CLOCK 550

#ifndef M_SQRT1_2
#define M_SQRT1_2 0.70710678118654752440
#endif

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

   Enums and typedefs

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

enum {
	PORTB = 0,
	PORTA
};

enum {
	A_X = 0,
	A_ZR,
	A_Z,
	A_AUDIO,
	A_Y,
};


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

   Prototypes

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

static WRITE8_DEVICE_HANDLER (v_via_pa_w);
static WRITE8_DEVICE_HANDLER(v_via_pb_w);
static WRITE8_DEVICE_HANDLER (v_via_ca2_w);
static WRITE8_DEVICE_HANDLER (v_via_cb2_w);


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

   Local variables

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

const via6522_interface vectrex_via6522_interface =
{
	DEVCB_HANDLER(vectrex_via_pa_r), DEVCB_HANDLER(vectrex_via_pb_r),         /* read PA/B */
	DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL,                     /* read ca1, cb1, ca2, cb2 */
	DEVCB_HANDLER(v_via_pa_w), DEVCB_HANDLER(v_via_pb_w),         /* write PA/B */
	DEVCB_NULL, DEVCB_NULL, DEVCB_HANDLER(v_via_ca2_w), DEVCB_HANDLER(v_via_cb2_w), /* write ca1, cb1, ca2, cb2 */
	DEVCB_LINE(vectrex_via_irq),                      /* IRQ */
};



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

   Lightpen

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

static TIMER_CALLBACK(lightpen_trigger)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	if (state->m_lightpen_port & 1)
	{
		via6522_device *via_0 = machine.device<via6522_device>("via6522_0");
		via_0->write_ca1(1);
		via_0->write_ca1(0);
	}

	if (state->m_lightpen_port & 2)
	{
		cputag_set_input_line(machine, "maincpu", M6809_FIRQ_LINE, PULSE_LINE);
	}
}


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

   VIA T2 configuration

   We need to snoop the frequency of VIA timer 2 here since most
   vectrex games use that timer for steady screen refresh. Even if the
   game stops T2 we continue refreshing the screen with the last known
   frequency. Note that we quickly get out of sync in this case and the
   screen will start flickering (see cut scenes in Spike).

   Note that the timer can be adjusted to the full period each time T2
   is restarted. This behaviour avoids flicker in most games. Some
   games like mine 3d don't work well with this scheme though and show
   severe jerking. So the second option is to leave the current period
   alone (if the new period isn't shorter) and change only the next
   full period.

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

READ8_HANDLER(vectrex_via_r)
{
	via6522_device *via = space->machine().device<via6522_device>("via6522_0");
	return via->read(*space, offset);
}

WRITE8_HANDLER(vectrex_via_w)
{
	vectrex_state *state = space->machine().driver_data<vectrex_state>();
	via6522_device *via = space->machine().device<via6522_device>("via6522_0");
	attotime period;

	switch (offset)
	{
	case 8:
		state->m_via_timer2 = (state->m_via_timer2 & 0xff00) | data;
		break;

	case 9:
		state->m_via_timer2 = (state->m_via_timer2 & 0x00ff) | (data << 8);

		period = (attotime::from_hz(space->machine().device("maincpu")->unscaled_clock()) * state->m_via_timer2);

		if (state->m_reset_refresh)
			state->m_refresh->adjust(period, 0, period);
		else
			state->m_refresh->adjust(
								  min(period, state->m_refresh->remaining()),
								  0,
								  period);
		break;
	}
	via->write(*space, offset, data);
}


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

   Screen refresh

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

static TIMER_CALLBACK(vectrex_refresh)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	/* Refresh only marks the range of vectors which will be drawn
     * during the next SCREEN_UPDATE. */
	state->m_display_start = state->m_display_end;
	state->m_display_end = state->m_point_index;
}


SCREEN_UPDATE(vectrex)
{
	vectrex_state *state = screen->machine().driver_data<vectrex_state>();
	int i;

	vectrex_configuration(screen->machine());

	/* start black */
	vector_add_point(screen->machine(),
					 state->m_points[state->m_display_start].x,
					 state->m_points[state->m_display_start].y,
					 state->m_points[state->m_display_start].col,
					 0);

	for (i = state->m_display_start; i != state->m_display_end; i = (i + 1) % NVECT)
	{
		vector_add_point(screen->machine(),
						 state->m_points[i].x,
						 state->m_points[i].y,
						 state->m_points[i].col,
						 state->m_points[i].intensity);
	}

	SCREEN_UPDATE_CALL(vector);
	vector_clear_list();
	return 0;
}


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

   Vector functions

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

void vectrex_add_point(running_machine &machine, int x, int y, rgb_t color, int intensity)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	vectrex_point *newpoint;

	state->m_point_index = (state->m_point_index+1) % NVECT;
	newpoint = &state->m_points[state->m_point_index];

	newpoint->x = x;
	newpoint->y = y;
	newpoint->col = color;
	newpoint->intensity = intensity;
}


void vectrex_add_point_stereo(running_machine &machine, int x, int y, rgb_t color, int intensity)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	if (state->m_imager_status == 2) /* left = 1, right = 2 */
		vectrex_add_point(machine, (int)(y * M_SQRT1_2)+ state->m_x_center,
						   (int)(((state->m_x_max - x) * M_SQRT1_2)),
						   color,
						   intensity);
	else
		vectrex_add_point(machine, (int)(y * M_SQRT1_2),
						   (int)((state->m_x_max - x) * M_SQRT1_2),
						   color,
						   intensity);
}


static TIMER_CALLBACK(vectrex_zero_integrators)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	state->m_x_int = state->m_x_center + (state->m_analog[A_ZR] * INT_PER_CLOCK);
	state->m_y_int = state->m_y_center + (state->m_analog[A_ZR] * INT_PER_CLOCK);
	(*state->vector_add_point_function)(machine, state->m_x_int, state->m_y_int, state->m_beam_color, 0);
}


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

   Delayed signals

   The RAMP signal is delayed wrt. beam blanking. Getting this right
   is important for text placement, the maze in Clean Sweep and many
   other games.

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

static TIMER_CALLBACK(update_signal)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	int length;

	if (!state->m_ramp)
	{
		length = machine.device("maincpu")->unscaled_clock() * INT_PER_CLOCK
			* (machine.time() - state->m_vector_start_time).as_double();

		state->m_x_int += length * (state->m_analog[A_X] + state->m_analog[A_ZR]);
		state->m_y_int += length * (state->m_analog[A_Y] + state->m_analog[A_ZR]);

		(*state->vector_add_point_function)(machine, state->m_x_int, state->m_y_int, state->m_beam_color, 2 * state->m_analog[A_Z] * state->m_blank);
	}
	else
	{
		if (state->m_blank)
			(*state->vector_add_point_function)(machine, state->m_x_int, state->m_y_int, state->m_beam_color, 2 * state->m_analog[A_Z]);
	}

	state->m_vector_start_time = machine.time();

	if (ptr)
		* (UINT8 *) ptr = param;
}


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

   Startup

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

VIDEO_START(vectrex)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	screen_device *screen = machine.first_screen();
	const rectangle &visarea = screen->visible_area();

	state->m_x_center=((visarea.max_x - visarea.min_x) / 2) << 16;
	state->m_y_center=((visarea.max_y - visarea.min_y) / 2) << 16;
	state->m_x_max = visarea.max_x << 16;
	state->m_y_max = visarea.max_y << 16;

	state->m_imager_freq = 1;

	state->vector_add_point_function = vectrex_add_point;
	state->m_imager_timer = machine.scheduler().timer_alloc(FUNC(vectrex_imager_eye));
	state->m_imager_timer->adjust(
						  attotime::from_hz(state->m_imager_freq),
						  2,
						  attotime::from_hz(state->m_imager_freq));

	state->m_lp_t = machine.scheduler().timer_alloc(FUNC(lightpen_trigger));

	state->m_refresh = machine.scheduler().timer_alloc(FUNC(vectrex_refresh));

	VIDEO_START_CALL(vector);
}


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

   VIA interface functions

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

static void vectrex_multiplexer(running_machine &machine, int mux)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	device_t *dac_device = machine.device("dac");

	machine.scheduler().timer_set(attotime::from_nsec(ANALOG_DELAY), FUNC(update_signal), state->m_via_out[PORTA], &state->m_analog[mux]);

	if (mux == A_AUDIO)
		dac_data_w(dac_device, state->m_via_out[PORTA]);
}


static WRITE8_DEVICE_HANDLER(v_via_pb_w)
{
	vectrex_state *state = device->machine().driver_data<vectrex_state>();
	if (!(data & 0x80))
	{
		/* RAMP is active */
		if ((state->m_ramp & 0x80))
		{
			/* RAMP was inactive before */

			if (state->m_lightpen_down)
			{
				/* Simple lin. algebra to check if pen is near
                 * the line defined by (A_X,A_Y).
                 * If that is the case, set a timer which goes
                 * off when the beam reaches the pen. Exact
                 * timing is important here.
                 *
                 *    lightpen
                 *       ^
                 *  _   /|
                 *  b  / |
                 *    /  |
                 *   /   |d
                 *  /    |
                 * /     |
                 * ------+---------> beam path
                 *    l  |    _
                 *            a
                 */
				double a2, b2, ab, d2;
				ab = (state->m_pen_x - state->m_x_int) * state->m_analog[A_X]
					+(state->m_pen_y - state->m_y_int) * state->m_analog[A_Y];
				if (ab > 0)
				{
					a2 = (double)(state->m_analog[A_X] * state->m_analog[A_X]
								  +(double)state->m_analog[A_Y] * state->m_analog[A_Y]);
					b2 = (double)(state->m_pen_x - state->m_x_int) * (state->m_pen_x - state->m_x_int)
						+(double)(state->m_pen_y - state->m_y_int) * (state->m_pen_y - state->m_y_int);
					d2 = b2 - ab * ab / a2;
					if (d2 < 2e10 && state->m_analog[A_Z] * state->m_blank > 0)
						state->m_lp_t->adjust(attotime::from_double(ab / a2 / (device->machine().device("maincpu")->unscaled_clock() * INT_PER_CLOCK)));
				}
			}
		}

		if (!(data & 0x1) && (state->m_via_out[PORTB] & 0x1))
		{
			/* MUX has been enabled */
			device->machine().scheduler().timer_set(attotime::from_nsec(ANALOG_DELAY), FUNC(update_signal));
		}
	}
	else
	{
		/* RAMP is inactive */
		if (!(state->m_ramp & 0x80))
		{
			/* Cancel running timer, line already finished */
			if (state->m_lightpen_down)
				state->m_lp_t->adjust(attotime::never);
		}
	}

	/* Sound */
	if (data & 0x10)
	{
		device_t *ay8912 = device->machine().device("ay8912");

		if (data & 0x08) /* BC1 (do we select a reg or write it ?) */
			ay8910_address_w(ay8912, 0, state->m_via_out[PORTA]);
		else
			ay8910_data_w(ay8912, 0, state->m_via_out[PORTA]);
	}

	if (!(data & 0x1) && (state->m_via_out[PORTB] & 0x1))
		vectrex_multiplexer (device->machine(), (data >> 1) & 0x3);

	state->m_via_out[PORTB] = data;
	device->machine().scheduler().timer_set(attotime::from_nsec(ANALOG_DELAY), FUNC(update_signal), data & 0x80, &state->m_ramp);
}


static WRITE8_DEVICE_HANDLER(v_via_pa_w)
{
	vectrex_state *state = device->machine().driver_data<vectrex_state>();
	/* DAC output always goes to Y integrator */
	state->m_via_out[PORTA] = data;
	device->machine().scheduler().timer_set(attotime::from_nsec(ANALOG_DELAY), FUNC(update_signal), data, &state->m_analog[A_Y]);

	if (!(state->m_via_out[PORTB] & 0x1))
		vectrex_multiplexer (device->machine(), (state->m_via_out[PORTB] >> 1) & 0x3);
}


static WRITE8_DEVICE_HANDLER(v_via_ca2_w)
{
	if (data == 0)
		device->machine().scheduler().timer_set(attotime::from_nsec(ANALOG_DELAY), FUNC(vectrex_zero_integrators));
}


static WRITE8_DEVICE_HANDLER(v_via_cb2_w)
{
	vectrex_state *state = device->machine().driver_data<vectrex_state>();
	int dx, dy;

	if (state->m_cb2 != data)
	{

		/* Check lightpen */
		if (state->m_lightpen_port != 0)
		{
			state->m_lightpen_down = input_port_read(device->machine(), "LPENCONF") & 0x10;

			if (state->m_lightpen_down)
			{
				state->m_pen_x = input_port_read(device->machine(), "LPENX") * (state->m_x_max / 0xff);
				state->m_pen_y = input_port_read(device->machine(), "LPENY") * (state->m_y_max / 0xff);

				dx = abs(state->m_pen_x - state->m_x_int);
				dy = abs(state->m_pen_y - state->m_y_int);
				if (dx < 500000 && dy < 500000 && data > 0)
					device->machine().scheduler().timer_set(attotime::zero, FUNC(lightpen_trigger));
			}
		}

		device->machine().scheduler().timer_set(attotime::zero, FUNC(update_signal), data, &state->m_blank);
		state->m_cb2 = data;
	}
}


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

   RA+A Spectrum I+

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

const via6522_interface spectrum1_via6522_interface =
{
	/*inputs : A/B,CA/B1,CA/B2 */ DEVCB_HANDLER(vectrex_via_pa_r), DEVCB_HANDLER(vectrex_s1_via_pb_r), DEVCB_NULL, DEVCB_NULL, DEVCB_NULL, DEVCB_NULL,
	/*outputs: A/B,CA/B1,CA/B2 */ DEVCB_HANDLER(v_via_pa_w), DEVCB_HANDLER(v_via_pb_w), DEVCB_NULL, DEVCB_NULL, DEVCB_HANDLER(v_via_ca2_w), DEVCB_HANDLER(v_via_cb2_w),
	/*irq                      */ DEVCB_LINE(vectrex_via_irq),
};


WRITE8_HANDLER(raaspec_led_w)
{
	logerror("Spectrum I+ LED: %i%i%i%i%i%i%i%i\n",
			 (data>>7)&0x1, (data>>6)&0x1, (data>>5)&0x1, (data>>4)&0x1,
			 (data>>3)&0x1, (data>>2)&0x1, (data>>1)&0x1, data&0x1);
}


VIDEO_START(raaspec)
{
	vectrex_state *state = machine.driver_data<vectrex_state>();
	screen_device *screen = machine.first_screen();
	const rectangle &visarea = screen->visible_area();

	state->m_x_center=((visarea.max_x - visarea.min_x) / 2) << 16;
	state->m_y_center=((visarea.max_y - visarea.min_y) / 2) << 16;
	state->m_x_max = visarea.max_x << 16;
	state->m_y_max = visarea.max_y << 16;

	state->vector_add_point_function = vectrex_add_point;
	state->m_refresh = machine.scheduler().timer_alloc(FUNC(vectrex_refresh));

	VIDEO_START_CALL(vector);
}