summaryrefslogtreecommitdiffstatshomepage
path: root/src/mame/machine/mcr68.c
blob: d99e4039efe068b2d7e1040e51f3da2b0aa267ad (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/***************************************************************************

    Midway MCR system

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

#include "emu.h"
#include "audio/midway.h"
#include "includes/mcr.h"
#include "includes/mcr68.h"

#define VERBOSE 0
#define LOG(x) do { if (VERBOSE) logerror x; } while (0)


/*************************************
 *
 *  Function prototypes
 *
 *************************************/

static void subtract_from_counter(running_machine &machine, int counter, int count);

static TIMER_CALLBACK( mcr68_493_callback );
static TIMER_CALLBACK( zwackery_493_callback );

static DECLARE_WRITE8_DEVICE_HANDLER( zwackery_pia0_w );
static DECLARE_WRITE8_DEVICE_HANDLER( zwackery_pia1_w );
static WRITE_LINE_DEVICE_HANDLER( zwackery_ca2_w );
static WRITE_LINE_DEVICE_HANDLER( zwackery_pia_irq );

static TIMER_CALLBACK( counter_fired_callback );



/*************************************
 *
 *  6821 PIA declarations
 *
 *************************************/

static READ8_DEVICE_HANDLER( zwackery_port_1_r )
{
	UINT8 ret = space.machine().root_device().ioport("IN1")->read();

	downcast<pia6821_device *>(device)->set_port_a_z_mask(ret);

	return ret;
}


static READ8_DEVICE_HANDLER( zwackery_port_3_r )
{
	UINT8 ret = space.machine().root_device().ioport("IN3")->read();

	downcast<pia6821_device *>(device)->set_port_a_z_mask(ret);

	return ret;
}


const pia6821_interface zwackery_pia0_intf =
{
	DEVCB_NULL,		/* port A in */
	DEVCB_INPUT_PORT("IN0"),		/* port B in */
	DEVCB_NULL,		/* line CA1 in */
	DEVCB_NULL,		/* line CB1 in */
	DEVCB_NULL,		/* line CA2 in */
	DEVCB_NULL,		/* line CB2 in */
	DEVCB_HANDLER(zwackery_pia0_w),		/* port A out */
	DEVCB_NULL,		/* port B out */
	DEVCB_NULL,		/* line CA2 out */
	DEVCB_NULL,		/* port CB2 out */
	DEVCB_LINE(zwackery_pia_irq),		/* IRQA */
	DEVCB_LINE(zwackery_pia_irq)		/* IRQB */
};


const pia6821_interface zwackery_pia1_intf =
{
	DEVCB_HANDLER(zwackery_port_1_r),		/* port A in */
	DEVCB_DRIVER_MEMBER(mcr68_state, zwackery_port_2_r),		/* port B in */
	DEVCB_NULL,		/* line CA1 in */
	DEVCB_NULL,		/* line CB1 in */
	DEVCB_NULL,		/* line CA2 in */
	DEVCB_NULL,		/* line CB2 in */
	DEVCB_HANDLER(zwackery_pia1_w),		/* port A out */
	DEVCB_NULL,		/* port B out */
	DEVCB_LINE(zwackery_ca2_w),		/* line CA2 out */
	DEVCB_NULL,		/* port CB2 out */
	DEVCB_NULL,		/* IRQA */
	DEVCB_NULL		/* IRQB */
};


const pia6821_interface zwackery_pia2_intf =
{
	DEVCB_HANDLER(zwackery_port_3_r),		/* port A in */
	DEVCB_INPUT_PORT("DSW"),				/* port B in */
	DEVCB_NULL,		/* line CA1 in */
	DEVCB_NULL,		/* line CB1 in */
	DEVCB_NULL,		/* line CA2 in */
	DEVCB_NULL,		/* line CB2 in */
	DEVCB_NULL,		/* port A out */
	DEVCB_NULL,		/* port B out */
	DEVCB_NULL,		/* line CA2 out */
	DEVCB_NULL,		/* port CB2 out */
	DEVCB_NULL,		/* IRQA */
	DEVCB_NULL		/* IRQB */
};



/*************************************
 *
 *  Generic MCR/68k machine initialization
 *
 *************************************/

MACHINE_START_MEMBER(mcr68_state,mcr68)
{
	int i;

	for (i = 0; i < 3; i++)
	{
		struct counter_state *m6840 = &m_m6840_state[i];

		m6840->timer = machine().scheduler().timer_alloc(FUNC(counter_fired_callback));

		state_save_register_item(machine(), "m6840", NULL, i, m6840->control);
		state_save_register_item(machine(), "m6840", NULL, i, m6840->latch);
		state_save_register_item(machine(), "m6840", NULL, i, m6840->count);
		state_save_register_item(machine(), "m6840", NULL, i, m6840->timer_active);
	}

	state_save_register_global(machine(), m_m6840_status);
	state_save_register_global(machine(), m_m6840_status_read_since_int);
	state_save_register_global(machine(), m_m6840_msb_buffer);
	state_save_register_global(machine(), m_m6840_lsb_buffer);
	state_save_register_global(machine(), m_m6840_irq_state);
	state_save_register_global(machine(), m_v493_irq_state);
	state_save_register_global(machine(), m_zwackery_sound_data);
}


static void mcr68_common_init(running_machine &machine)
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	int i;

	/* reset the 6840's */
	state->m_m6840_counter_periods[0] = attotime::from_hz(30);			/* clocked by /VBLANK */
	state->m_m6840_counter_periods[1] = attotime::never;					/* grounded */
	state->m_m6840_counter_periods[2] = attotime::from_hz(512 * 30);	/* clocked by /HSYNC */

	state->m_m6840_status = 0x00;
	state->m_m6840_status_read_since_int = 0x00;
	state->m_m6840_msb_buffer = state->m_m6840_lsb_buffer = 0;
	for (i = 0; i < 3; i++)
	{
		struct counter_state *m6840 = &state->m_m6840_state[i];

		m6840->control = 0x00;
		m6840->latch = 0xffff;
		m6840->count = 0xffff;
		m6840->timer->enable(false);
		m6840->timer_active = 0;
		m6840->period = state->m_m6840_counter_periods[i];
	}

	/* initialize the clock */
	state->m_m6840_internal_counter_period = attotime::from_hz(machine.device("maincpu")->unscaled_clock() / 10);
}


MACHINE_RESET_MEMBER(mcr68_state,mcr68)
{
	/* for the most part all MCR/68k games are the same */
	mcr68_common_init(machine());
	m_v493_callback = mcr68_493_callback;
	m_v493_callback_name = "mcr68_493_callback";

	/* vectors are 1 and 2 */
	m_v493_irq_vector = 1;
	m_m6840_irq_vector = 2;
}


MACHINE_START_MEMBER(mcr68_state,zwackery)
{
	MACHINE_START_CALL_MEMBER(mcr68);
}


MACHINE_RESET_MEMBER(mcr68_state,zwackery)
{
	/* for the most part all MCR/68k games are the same */
	mcr68_common_init(machine());
	m_v493_callback = zwackery_493_callback;
	m_v493_callback_name = "zwackery_493_callback";

	/* vectors are 5 and 6 */
	m_v493_irq_vector = 5;
	m_m6840_irq_vector = 6;
}



/*************************************
 *
 *  Generic MCR interrupt handler
 *
 *************************************/

INTERRUPT_GEN_MEMBER(mcr68_state::mcr68_interrupt)
{
	/* update the 6840 VBLANK clock */
	if (!m_m6840_state[0].timer_active)
		subtract_from_counter(machine(), 0, 1);

	logerror("--- VBLANK ---\n");

	/* also set a timer to generate the 493 signal at a specific time before the next VBLANK */
	/* the timing of this is crucial for Blasted and Tri-Sports, which check the timing of */
	/* VBLANK and 493 using counter 2 */
	machine().scheduler().timer_set(attotime::from_hz(30) - m_timing_factor, m_v493_callback, m_v493_callback_name);
}



/*************************************
 *
 *  MCR/68k interrupt central
 *
 *************************************/

static void update_mcr68_interrupts(running_machine &machine)
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	machine.device("maincpu")->execute().set_input_line(state->m_v493_irq_vector, state->m_v493_irq_state ? ASSERT_LINE : CLEAR_LINE);
	machine.device("maincpu")->execute().set_input_line(state->m_m6840_irq_vector, state->m_m6840_irq_state ? ASSERT_LINE : CLEAR_LINE);
}


static TIMER_CALLBACK( mcr68_493_off_callback )
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	state->m_v493_irq_state = 0;
	update_mcr68_interrupts(machine);
}


static TIMER_CALLBACK( mcr68_493_callback )
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	state->m_v493_irq_state = 1;
	update_mcr68_interrupts(machine);
	machine.scheduler().timer_set(machine.primary_screen->scan_period(), FUNC(mcr68_493_off_callback));
	logerror("--- (INT1) ---\n");
}



/*************************************
 *
 *  Zwackery-specific interfaces
 *
 *************************************/

WRITE8_DEVICE_HANDLER( zwackery_pia0_w )
{
	/* bit 7 is the watchdog */
	if (!(data & 0x80)) space.machine().watchdog_reset();

	/* bits 5 and 6 control hflip/vflip */
	/* bits 3 and 4 control coin counters? */
	/* bits 0, 1 and 2 control meters? */
}


WRITE8_DEVICE_HANDLER( zwackery_pia1_w )
{
	mcr68_state *state = space.machine().driver_data<mcr68_state>();
	state->m_zwackery_sound_data = (data >> 4) & 0x0f;
}


WRITE_LINE_DEVICE_HANDLER( zwackery_ca2_w )
{
	mcr68_state *drvstate = device->machine().driver_data<mcr68_state>();
	address_space &space = device->machine().device("maincpu")->memory().space(AS_PROGRAM);
	drvstate->m_chip_squeak_deluxe->write(space, 0, (state << 4) | drvstate->m_zwackery_sound_data);
}


static WRITE_LINE_DEVICE_HANDLER( zwackery_pia_irq )
{
	mcr68_state *drvstate = device->machine().driver_data<mcr68_state>();
	pia6821_device *pia = downcast<pia6821_device *>(device);
	drvstate->m_v493_irq_state = pia->irq_a_state() | pia->irq_b_state();
	update_mcr68_interrupts(device->machine());
}


static TIMER_CALLBACK( zwackery_493_off_callback )
{
	pia6821_device *pia = machine.device<pia6821_device>("pia0");
	pia->ca1_w(0);
}


static TIMER_CALLBACK( zwackery_493_callback )
{
	pia6821_device *pia = machine.device<pia6821_device>("pia0");

	pia->ca1_w(1);
	machine.scheduler().timer_set(machine.primary_screen->scan_period(), FUNC(zwackery_493_off_callback));
}



/*************************************
 *
 *  M6840 timer utilities
 *
 *************************************/

INLINE void update_interrupts(running_machine &machine)
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	state->m_m6840_status &= ~0x80;

	if ((state->m_m6840_status & 0x01) && (state->m_m6840_state[0].control & 0x40)) state->m_m6840_status |= 0x80;
	if ((state->m_m6840_status & 0x02) && (state->m_m6840_state[1].control & 0x40)) state->m_m6840_status |= 0x80;
	if ((state->m_m6840_status & 0x04) && (state->m_m6840_state[2].control & 0x40)) state->m_m6840_status |= 0x80;

	state->m_m6840_irq_state = state->m_m6840_status >> 7;
	update_mcr68_interrupts(machine);
}


static void subtract_from_counter(running_machine &machine, int counter, int count)
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	struct counter_state *m6840 = &state->m_m6840_state[counter];

	/* dual-byte mode */
	if (m6840->control & 0x04)
	{
		int lsb = m6840->count & 0xff;
		int msb = m6840->count >> 8;

		/* count the clocks */
		lsb -= count;

		/* loop while we're less than zero */
		while (lsb < 0)
		{
			/* borrow from the MSB */
			lsb += (m6840->latch & 0xff) + 1;
			msb--;

			/* if MSB goes less than zero, we've expired */
			if (msb < 0)
			{
				state->m_m6840_status |= 1 << counter;
				state->m_m6840_status_read_since_int &= ~(1 << counter);
				update_interrupts(machine);
				msb = (m6840->latch >> 8) + 1;
				LOG(("** Counter %d fired\n", counter));
			}
		}

		/* store the result */
		m6840->count = (msb << 8) | lsb;
	}

	/* word mode */
	else
	{
		int word = m6840->count;

		/* count the clocks */
		word -= count;

		/* loop while we're less than zero */
		while (word < 0)
		{
			/* borrow from the MSB */
			word += m6840->latch + 1;

			/* we've expired */
			state->m_m6840_status |= 1 << counter;
			state->m_m6840_status_read_since_int &= ~(1 << counter);
			update_interrupts(machine);
			LOG(("** Counter %d fired\n", counter));
		}

		/* store the result */
		m6840->count = word;
	}
}


static TIMER_CALLBACK( counter_fired_callback )
{
	mcr68_state *state = machine.driver_data<mcr68_state>();
	int count = param >> 2;
	int counter = param & 3;
	struct counter_state *m6840 = &state->m_m6840_state[counter];

	/* reset the timer */
	m6840->timer_active = 0;

	/* subtract it all from the counter; this will generate an interrupt */
	subtract_from_counter(machine, counter, count);
}


void mcr68_state::reload_count(int counter)
{
	struct counter_state *m6840 = &m_m6840_state[counter];
	attotime period;
	attotime total_period;
	int count;

	/* copy the latched value in */
	m6840->count = m6840->latch;

	/* counter 0 is self-updating if clocked externally */
	if (counter == 0 && !(m6840->control & 0x02))
	{
		m6840->timer->adjust(attotime::never);
		m6840->timer_active = 0;
		return;
	}

	/* determine the clock period for this timer */
	if (m6840->control & 0x02)
		period = m_m6840_internal_counter_period;
	else
		period = m_m6840_counter_periods[counter];

	/* determine the number of clock periods before we expire */
	count = m6840->count;
	if (m6840->control & 0x04)
		count = ((count >> 8) + 1) * ((count & 0xff) + 1);
	else
		count = count + 1;

	/* set the timer */
	total_period = period * count;
LOG(("reload_count(%d): period = %f  count = %d\n", counter, period.as_double(), count));
	m6840->timer->adjust(total_period, (count << 2) + counter);
	m6840->timer_active = 1;
}


UINT16 mcr68_state::compute_counter(int counter)
{
	struct counter_state *m6840 = &m_m6840_state[counter];
	attotime period;
	int remaining;

	/* if there's no timer, return the count */
	if (!m6840->timer_active)
		return m6840->count;

	/* determine the clock period for this timer */
	if (m6840->control & 0x02)
		period = m_m6840_internal_counter_period;
	else
		period = m_m6840_counter_periods[counter];
	/* see how many are left */
	remaining = m6840->timer->remaining().as_attoseconds() / period.as_attoseconds();

	/* adjust the count for dual byte mode */
	if (m6840->control & 0x04)
	{
		int divisor = (m6840->count & 0xff) + 1;
		int msb = remaining / divisor;
		int lsb = remaining % divisor;
		remaining = (msb << 8) | lsb;
	}

	return remaining;
}



/*************************************
 *
 *  M6840 timer I/O
 *
 *************************************/

WRITE8_MEMBER(mcr68_state::mcr68_6840_w_common)
{
	int i;

	/* offsets 0 and 1 are control registers */
	if (offset < 2)
	{
		int counter = (offset == 1) ? 1 : (m_m6840_state[1].control & 0x01) ? 0 : 2;
		struct counter_state *m6840 = &m_m6840_state[counter];
		UINT8 diffs = data ^ m6840->control;

		m6840->control = data;

		/* reset? */
		if (counter == 0 && (diffs & 0x01))
		{
			/* holding reset down */
			if (data & 0x01)
			{
				for (i = 0; i < 3; i++)
				{
					m_m6840_state[i].timer->adjust(attotime::never);
					m_m6840_state[i].timer_active = 0;
				}
			}

			/* releasing reset */
			else
			{
				for (i = 0; i < 3; i++)
					reload_count(i);
			}

			m_m6840_status = 0;
			update_interrupts(machine());
		}

		/* changing the clock source? (needed for Zwackery) */
		if (diffs & 0x02)
			reload_count(counter);

		LOG(("%06X:Counter %d control = %02X\n", space.device().safe_pcbase(), counter, data));
	}

	/* offsets 2, 4, and 6 are MSB buffer registers */
	else if ((offset & 1) == 0)
	{
		LOG(("%06X:MSB = %02X\n", space.device().safe_pcbase(), data));
		m_m6840_msb_buffer = data;
	}

	/* offsets 3, 5, and 7 are Write Timer Latch commands */
	else
	{
		int counter = (offset - 2) / 2;
		struct counter_state *m6840 = &m_m6840_state[counter];
		m6840->latch = (m_m6840_msb_buffer << 8) | (data & 0xff);

		/* clear the interrupt */
		m_m6840_status &= ~(1 << counter);
		update_interrupts(machine());

		/* reload the count if in an appropriate mode */
		if (!(m6840->control & 0x10))
			reload_count(counter);

		LOG(("%06X:Counter %d latch = %04X\n", space.device().safe_pcbase(), counter, m6840->latch));
	}
}


READ16_MEMBER(mcr68_state::mcr68_6840_r_common)
{
	/* offset 0 is a no-op */
	if (offset == 0)
		return 0;

	/* offset 1 is the status register */
	else if (offset == 1)
	{
		LOG(("%06X:Status read = %04X\n", space.device().safe_pcbase(), m_m6840_status));
		m_m6840_status_read_since_int |= m_m6840_status & 0x07;
		return m_m6840_status;
	}

	/* offsets 2, 4, and 6 are Read Timer Counter commands */
	else if ((offset & 1) == 0)
	{
		int counter = (offset - 2) / 2;
		int result = compute_counter(counter);

		/* clear the interrupt if the status has been read */
		if (m_m6840_status_read_since_int & (1 << counter))
			m_m6840_status &= ~(1 << counter);
		update_interrupts(machine());

		m_m6840_lsb_buffer = result & 0xff;

		LOG(("%06X:Counter %d read = %04X\n", space.device().safe_pcbase(), counter, result));
		return result >> 8;
	}

	/* offsets 3, 5, and 7 are LSB buffer registers */
	else
		return m_m6840_lsb_buffer;
}


WRITE16_MEMBER(mcr68_state::mcr68_6840_upper_w)
{
	if (ACCESSING_BITS_8_15)
		mcr68_6840_w_common(space, offset, (data >> 8) & 0xff);
}


WRITE16_MEMBER(mcr68_state::mcr68_6840_lower_w)
{
	if (ACCESSING_BITS_0_7)
		mcr68_6840_w_common(space, offset, data & 0xff);
}


READ16_MEMBER(mcr68_state::mcr68_6840_upper_r)
{
	return (mcr68_6840_r_common(space,offset,0) << 8) | 0x00ff;
}


READ16_MEMBER(mcr68_state::mcr68_6840_lower_r)
{
	return mcr68_6840_r_common(space,offset,0) | 0xff00;
}