summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/6840ptm.c
blob: 7669052ceb314baf8004109d5959a0be3451b7b1 (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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/**********************************************************************


    Motorola 6840 PTM interface and emulation

    This function is a simple emulation of up to 4 MC6840
    Programmable Timer Modules

    Written By J.Wallace based on previous work by Aaron Giles,
   'Re-Animator' and Mathis Rosenhauer.

    Todo:
         Confirm handling for 'Single Shot' operation.
         (Datasheet suggests that output starts high, going low
         on timeout, opposite of continuous case)
         Establish whether ptm6840_set_c? routines can replace
         hard coding of external clock frequencies.


    Operation:
    The interface is arranged as follows:

    Internal Clock frequency,
    Clock 1 frequency, Clock 2 frequency, Clock 3 frequency,
    Clock 1 output, Clock 2 output, Clock 3 output,
    IRQ function

    If the external clock frequencies are not fixed, they should be
    entered as '0', and the ptm6840_set_c?(which, state) functions
    should be used instead if necessary (This should allow the VBLANK
    clock on the MCR units to operate).

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

#include "driver.h"
#include "deprecat.h"
#include "6840ptm.h"

// Defines /////////////////////////////////////////////////////////////

#define PTMVERBOSE 0

#if PTMVERBOSE
	#define PLOG(x)	logerror x;
#else
	#define PLOG(x)
#endif

enum
{
	PTM_6840_CTRL1   = 0,
	PTM_6840_CTRL2   = 1,
	PTM_6840_STATUS  = 1,
	PTM_6840_MSBBUF1 = 2,
	PTM_6840_LSB1	 = 3,
	PTM_6840_MSBBUF2 = 4,
	PTM_6840_LSB2    = 5,
	PTM_6840_MSBBUF3 = 6,
	PTM_6840_LSB3    = 7,
};


typedef struct _ptm6840
{
	const ptm6840_interface *intf;

	UINT8 control_reg[3];
	UINT8 output[3]; /* Output states */
	UINT8 gate[3];   /* Input gate states */
	UINT8 clock[3];  /* Clock states */
	UINT8 enabled[3];
	UINT8 mode[3];
	UINT8 fired[3];
	UINT8 t3_divisor;
	UINT8 t3_scaler;
	UINT8 IRQ;
	UINT8 status_reg;
	UINT8 status_read_since_int;
	UINT8 lsb_buffer;
	UINT8 msb_buffer;

	int internal_clock;
	int external_clock[3];

	/* Each PTM has 3 timers */
	emu_timer *timer[3];

	UINT16 latch[3];
	UINT16 counter[3];
} ptm6840;

// Local prototypes ///////////////////////////////////////////////////////

static void ptm6840_timeout(int which, int idx);
static TIMER_CALLBACK( ptm6840_t1_timeout );
static TIMER_CALLBACK( ptm6840_t2_timeout );
static TIMER_CALLBACK( ptm6840_t3_timeout );

// Local vars /////////////////////////////////////////////////////////////

static ptm6840 ptm[PTM_6840_MAX];

static const char *const opmode[] =
{
	"000 continous mode",
	"001 freq comparison mode",
	"010 continous mode",
	"011 pulse width comparison mode",
	"100 single shot mode",
	"101 freq comparison mode",
	"110 single shot mode",
	"111 pulse width comparison mode"
};

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Get enabled status                                                    //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

int ptm6840_get_status(int which, int clock)
{
	ptm6840 *p = ptm + which;
	return p->enabled[clock - 1];
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_get_irq: get IRQ state                                        //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

int ptm6840_get_irq(int which)
{
	ptm6840 *p = ptm + which;
	return p->IRQ;
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Subtract from Counter                                                 //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

static void subtract_from_counter(int which, int counter, int count)
{
	int clock;
	attotime duration;

	ptm6840 *currptr = ptm + which;

	/* Determine the clock frequency for this timer */
	if (currptr->control_reg[counter] & 0x02)
		clock = currptr->internal_clock;
	else
		clock = currptr->external_clock[counter];

	/* Dual-byte mode */
	if (currptr->control_reg[counter] & 0x04)
	{
		int lsb = currptr->counter[counter] & 0xff;
		int msb = currptr->counter[counter] >> 8;

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

		/* Loop while we're less than zero */
		while (lsb < 0)
		{
			/* Borrow from the MSB */
			lsb += (currptr->latch[counter] & 0xff) + 1;
			msb--;

			/* If MSB goes less than zero, we've expired */
			if (msb < 0)
			{
				ptm6840_timeout(which, counter);
				msb = (currptr->latch[counter] >> 8) + 1;
			}
		}

		/* Store the result */
		currptr->counter[counter] = (msb << 8) | lsb;
	}

	/* Word mode */
	else
	{
		int word = currptr->counter[counter];

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

		/* loop while we're less than zero */
		while (word < 0)
		{
			/* Borrow from the MSB */
			word += currptr->latch[counter] + 1;

			/* We've expired */
			ptm6840_timeout(which, counter);
		}

		/* Store the result */
		currptr->counter[counter] = word;
	}

	if (currptr->enabled[counter])
	{
		duration = attotime_mul(ATTOTIME_IN_HZ(clock), currptr->counter[counter]);

		if (counter == 2)
			duration = attotime_mul(duration, currptr->t3_divisor);

		timer_adjust_oneshot(currptr->timer[counter], duration, which);
	}
}

static void ptm_tick(int which, int counter, int count)
{
	ptm6840 *currptr = ptm + which;

	if (counter == 2)
	{
		currptr->t3_scaler += count;

		if ( currptr->t3_scaler > currptr->t3_divisor - 1)
		{
			subtract_from_counter(which, counter, 1);
			currptr->t3_scaler = 0;
		}
	}
	else
	{
		subtract_from_counter(which, counter, count);
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Update Internal Interrupts                                            //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

INLINE void update_interrupts(int which)
{
	int new_state;
	ptm6840 *currptr = ptm + which;

	new_state = ((currptr->status_reg & 0x01) && (currptr->control_reg[0] & 0x40)) ||
				((currptr->status_reg & 0x02) && (currptr->control_reg[1] & 0x40)) ||
				((currptr->status_reg & 0x04) && (currptr->control_reg[2] & 0x40));

//  if (new_state != currptr->IRQ)
	{
		currptr->IRQ = new_state;

		if (currptr->IRQ)
			currptr->status_reg |= 0x80;
		else
			currptr->status_reg &= ~0x80;

		if (currptr->intf->irq_func)
			(currptr->intf->irq_func)(currptr->IRQ);
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Compute Counter                                                       //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

static UINT16 compute_counter(int which, int counter)
{
	ptm6840 *currptr = ptm + which;

	int clock;
	int remaining=0;

	/* If there's no timer, return the count */
	if (!currptr->enabled[counter])
	{
		PLOG(("MC6840 #%d: read counter(%d): %d\n", which, counter, currptr->counter[counter]));
		return currptr->counter[counter];
	}

	/* determine the clock frequency for this timer */
	if (currptr->control_reg[counter] & 0x02)
	{
		clock = currptr->internal_clock;
		PLOG(("MC6840 #%d: %d internal clock freq %d \n", which, counter, clock));
	}
	else
	{
		clock = currptr->external_clock[counter];
		PLOG(("MC6840 #%d: %d external clock freq %d \n", which, counter, clock));
	}
	/* See how many are left */
	remaining = attotime_to_double(attotime_mul(timer_timeleft(currptr->timer[counter]), clock));

	/* Adjust the count for dual byte mode */
	if (currptr->control_reg[counter] & 0x04)
	{
		int divisor = (currptr->counter[counter] & 0xff) + 1;
		int msb = remaining / divisor;
		int lsb = remaining % divisor;
		remaining = (msb << 8) | lsb;
	}
	PLOG(("MC6840 #%d: read counter(%d): %d\n", which, counter, remaining));
	return remaining;
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Reload Counter                                                        //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

static void reload_count(int which, int idx)
{
	int clock;
	int count;
	attotime duration;
	ptm6840 *currptr = ptm + which;

	/* Copy the latched value in */
	currptr->counter[idx] = currptr->latch[idx];

	/* Determine the clock frequency for this timer */
	if (currptr->control_reg[idx] & 0x02)
	{
		clock = currptr->internal_clock;
		PLOG(("MC6840 #%d: %d internal clock freq %d \n", which,idx, clock));
	}
	else
	{
		clock = currptr->external_clock[idx];
		PLOG(("MC6840 #%d: %d external clock freq %d \n", which,idx, clock));
	}

	/* Determine the number of clock periods before we expire */
	count = currptr->counter[idx];
	if (currptr->control_reg[idx] & 0x04)
		count = ((count >> 8) + 1) * ((count & 0xff) + 1);
	else
		count = count + 1;

	currptr->fired[idx]=0;

	if ((currptr->mode[idx] == 4)||(currptr->mode[idx] == 6))
	{
		currptr->output[idx] = 1;
		if ( currptr->intf->out_func[idx] ) currptr->intf->out_func[idx](Machine, 0, currptr->output[idx]);
	}

	/* Set the timer */
	PLOG(("MC6840 #%d: reload_count(%d): clock = %d  count = %d\n", which, idx, clock, count));

	duration = attotime_mul(ATTOTIME_IN_HZ(clock), count);
	if (idx == 2) duration = attotime_mul(duration, currptr->t3_divisor);
	PLOG(("MC6840 #%d: reload_count(%d): output = %lf\n", which, idx, attotime_to_double(duration)));

	if (!currptr->control_reg[idx] & 0x02)
	{
		if (!currptr->intf->external_clock[idx])
		{
			currptr->enabled[idx] = 0;
			timer_enable(currptr->timer[idx],FALSE);
		}
	}
	else
	{
		currptr->enabled[idx] = 1;
		timer_adjust_oneshot(currptr->timer[idx], duration, which);
		timer_enable(currptr->timer[idx],TRUE);
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Configure Timer                                                       //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

void ptm6840_config(int which, const ptm6840_interface *intf)
{
	int i;
	ptm6840 *currptr = ptm + which;

	assert_always(mame_get_phase(Machine) == MAME_PHASE_INIT, "Can only call ptm6840_config at init time!");
	assert_always((which >= 0) && (which < PTM_6840_MAX), "ptm6840_config called on an invalid PTM!");
	assert_always(intf, "ptm6840_config called with an invalid interface!");
	ptm[which].intf = intf;
	ptm[which].internal_clock = currptr->intf->internal_clock;

	for (i = 0; i < 3; i++)
	{
		if ( currptr->intf->external_clock[i] )
			ptm[which].external_clock[i] = currptr->intf->external_clock[i];
		else
			ptm[which].external_clock[i] = 1;
	}

	ptm[which].timer[0] = timer_alloc(ptm6840_t1_timeout, NULL);
	ptm[which].timer[1] = timer_alloc(ptm6840_t2_timeout, NULL);
	ptm[which].timer[2] = timer_alloc(ptm6840_t3_timeout, NULL);

	for (i = 0; i < 3; i++)
		timer_enable(ptm[which].timer[i], FALSE);

	state_save_register_item("6840ptm", which, currptr->lsb_buffer);
	state_save_register_item("6840ptm", which, currptr->msb_buffer);
	state_save_register_item("6840ptm", which, currptr->status_read_since_int);
	state_save_register_item("6840ptm", which, currptr->status_reg);
	state_save_register_item("6840ptm", which, currptr->t3_divisor);
	state_save_register_item("6840ptm", which, currptr->t3_scaler);
	state_save_register_item("6840ptm", which, currptr->internal_clock);
	state_save_register_item("6840ptm", which, currptr->IRQ);

	state_save_register_item_array("6840ptm", which, currptr->control_reg);
	state_save_register_item_array("6840ptm", which, currptr->output);
	state_save_register_item_array("6840ptm", which, currptr->gate);
	state_save_register_item_array("6840ptm", which, currptr->clock);
	state_save_register_item_array("6840ptm", which, currptr->mode);
	state_save_register_item_array("6840ptm", which, currptr->fired);
	state_save_register_item_array("6840ptm", which, currptr->enabled);
	state_save_register_item_array("6840ptm", which, currptr->external_clock);
	state_save_register_item_array("6840ptm", which, currptr->counter);
	state_save_register_item_array("6840ptm", which, currptr->latch);

	ptm6840_reset(which);
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Reset Timer                                                           //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

void ptm6840_reset(int which)
{
	int i;

	ptm[which].control_reg[2]		 = 0;
	ptm[which].control_reg[1]		 = 0;
	ptm[which].control_reg[0]		 = 1;
	ptm[which].status_reg			 = 0;
	ptm[which].t3_divisor			 = 1;
	ptm[which].status_read_since_int = 0;
	ptm[which].IRQ                   = 0;

	for (i = 0; i < 3; i++)
	{
		ptm[which].counter[i] = 0xffff;
		ptm[which].latch[i]   = 0xffff;
		ptm[which].output[i]  = 0;
		ptm[which].fired[i]   = 0;
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Read Timer                                                            //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

int ptm6840_read(int which, int offset)
{
	int val;
	ptm6840 *currptr = ptm + which;

	switch ( offset )
	{
		case PTM_6840_CTRL1:
		{
			val = 0;
			break;
		}

		case PTM_6840_STATUS:
		{
			PLOG(("%06X: MC6840 #%d: Status read = %04X\n", activecpu_get_previouspc(), which, currptr->status_reg));
			currptr->status_read_since_int |= currptr->status_reg & 0x07;
			val = currptr->status_reg;
			break;
		}

		case PTM_6840_MSBBUF1:
		case PTM_6840_MSBBUF2:
		case PTM_6840_MSBBUF3:
		{
			int idx = (offset - 2) / 2;
			int result = compute_counter(which, idx);

			/* Clear the interrupt if the status has been read */
			if (currptr->status_read_since_int & (1 << idx))
			{
				currptr->status_reg &= ~(1 << idx);
				update_interrupts(which);
			}

			currptr->lsb_buffer = result & 0xff;

			PLOG(("%06X: MC6840 #%d: Counter %d read = %04X\n", activecpu_get_previouspc(), which, idx, result >> 8));
			val = result >> 8;
			break;
		}

		case PTM_6840_LSB1:
		case PTM_6840_LSB2:
		case PTM_6840_LSB3:
		{
			val = currptr->lsb_buffer;
			break;
		}

		default:
		{
			val = 0;
			break;
		}

	}
	return val;
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// Write Timer                                                           //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

void ptm6840_write (int which, int offset, int data)
{
	ptm6840 *currptr = ptm + which;

	int idx;
	int i;
	UINT8 diffs;

	switch ( offset )
	{
		case PTM_6840_CTRL1:
		case PTM_6840_CTRL2:
		{
			idx = (offset == 1) ? 1 : (currptr->control_reg[1] & 0x01) ? 0 : 2;
			diffs = data ^ currptr->control_reg[idx];
			currptr->t3_divisor = (currptr->control_reg[2] & 0x01) ? 8 : 1;
			currptr->mode[idx] = (data >> 3) & 0x07;
			currptr->control_reg[idx] = data;

			PLOG(("MC6840 #%d : Control register %d selected\n", which, idx));
			PLOG(("operation mode   = %s\n", opmode[ currptr->mode[idx] ]));
			PLOG(("value            = %04X\n", currptr->control_reg[idx]));
			PLOG(("t3divisor        = %d\n", currptr->t3_divisor));

			if (!(currptr->control_reg[idx] & 0x80 ))
			{
				/* Output cleared */
				if ( currptr->intf )
				{
					if ( currptr->intf->out_func[idx] )
						currptr->intf->out_func[idx](Machine, 0, 0);
				}
			}
			/* Reset? */
			if (idx == 0 && (diffs & 0x01))
			{
				/* Holding reset down */
				if (data & 0x01)
				{
					PLOG(("MC6840 #%d : Timer reset\n", which));
					for (i = 0; i < 3; i++)
					{
						timer_enable(currptr->timer[i], FALSE);
						currptr->enabled[i] = 0;
					}
				}
				/* Releasing reset */
				else
				{
					for (i = 0; i < 3; i++)
						reload_count(which, i);
				}

				currptr->status_reg = 0;
				update_interrupts(which);

				/* Changing the clock source? (e.g. Zwackery) */
				if (diffs & 0x02)
					reload_count(which, idx);
			}
			break;
		}

		case PTM_6840_MSBBUF1:
		case PTM_6840_MSBBUF2:
		case PTM_6840_MSBBUF3:
		{
			PLOG(("MC6840 #%d msbbuf%d = %02X\n", which, offset / 2, data));
			currptr->msb_buffer = data;
			break;
		}

		case PTM_6840_LSB1:
		case PTM_6840_LSB2:
		case PTM_6840_LSB3:
		{
			idx = (offset - 3) / 2;
			currptr->latch[idx] = (currptr->msb_buffer << 8) | (data & 0xff);

			/* Clear the interrupt */
			currptr->status_reg &= ~(1 << idx);
			update_interrupts(which);

			/* Reload the count if in an appropriate mode */
			if (!(currptr->control_reg[idx] & 0x10))
				reload_count(which,idx);

			PLOG(("%06X:MC6840 #%d: Counter %d latch = %04X\n", activecpu_get_previouspc(), which, idx, currptr->latch[idx]));
			break;
		}
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_timeout: called if timer is mature                            //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

static void ptm6840_timeout(int which, int idx)
{
	ptm6840 *p = ptm + which;

	PLOG(("**ptm6840 %d t%d timeout**\n", which, idx + 1));

	if ( p->control_reg[idx] & 0x40 )
	{
		/* Interrupt enabled */
		p->status_reg |= (1 << idx);
		p->status_read_since_int &= ~(1 << idx);
		update_interrupts(which);
	}

	if ( p->control_reg[idx] & 0x80 )
	{
		/* Output enabled */
		if ( p->intf )
		{
			if ((p->mode[idx] == 0)||(p->mode[idx] == 2))
			{
				p->output[idx] = p->output[idx]?0:1;
				PLOG(("**ptm6840 %d t%d output %d **\n", which, idx + 1, p->output[idx]));

				if ( p->intf->out_func[idx] )
					p->intf->out_func[idx](Machine, 0, p->output[idx]);
			}
			if ((p->mode[idx] == 4)||(p->mode[idx] == 6))
			{
				if (!p->fired[idx])
				{
					p->output[idx] = 1;
					PLOG(("**ptm6840 %d t%d output %d **\n", which, idx + 1, p->output[idx]));

					if ( p->intf->out_func[idx] )
						p->intf->out_func[idx](Machine, 0, p->output[idx]);

					/* No changes in output until reinit */
					p->fired[idx] = 1;
				}
			}
		}
	}
	p->enabled[idx]= 0;
	reload_count(which, idx);
}

static TIMER_CALLBACK( ptm6840_t1_timeout ) { ptm6840_timeout(param, 0); }
static TIMER_CALLBACK( ptm6840_t2_timeout ) { ptm6840_timeout(param, 1); }
static TIMER_CALLBACK( ptm6840_t3_timeout ) { ptm6840_timeout(param, 2); }

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_set_gate: set gate status (0 or 1)                            //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

INLINE void ptm6840_set_gate(int which, int state, int idx)
{
	ptm6840 *p = ptm + which;

	if ( (p->mode[idx] == 0) || (p->mode[idx] == 2) || (p->mode[0] == 4) || (p->mode[idx] == 6) )
	{
		if (state == 0 && p->gate[idx])
			reload_count(which,idx);
	}
	p->gate[idx] = state;
}

void ptm6840_set_g1(int which, int state) { ptm6840_set_gate(which, state, 0); }
void ptm6840_set_g2(int which, int state) { ptm6840_set_gate(which, state, 1); }
void ptm6840_set_g3(int which, int state) { ptm6840_set_gate(which, state, 2); }

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_set_clock: set clock status (0 or 1)                          //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

INLINE void ptm6840_set_clock(int which, int state, int idx)
{
	ptm6840 *p = ptm + which;

	p->clock[idx] = state;

	if (!(p->control_reg[idx] & 0x02))
	{
		if (state)
			ptm_tick(which, idx, 1);
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_get_count: get count value                                    //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

UINT16 ptm6840_get_count(int which,int counter)
{
	return compute_counter(which, counter);
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_set_ext_clock: set external clock frequency                   //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

void ptm6840_set_ext_clock(int which, int counter, int clock)
{
	ptm6840 *currptr = ptm + which;
	currptr->external_clock[counter] = clock;

	if (!currptr->control_reg[counter] & 0x02)
	{
		if (!currptr->intf->external_clock[counter])
		{
			currptr->enabled[counter] = 0;
			timer_enable(currptr->timer[counter], FALSE);
		}
	}
	else
	{
		int count;
		attotime duration;

		/* Determine the number of clock periods before we expire */
		count = currptr->counter[counter];

		if (currptr->control_reg[counter] & 0x04)
			count = ((count >> 8) + 1) * ((count & 0xff) + 1);
		else
			count = count + 1;

		duration = attotime_mul(ATTOTIME_IN_HZ(clock), count);

		if (counter == 2)
			duration = attotime_mul(duration, currptr->t3_divisor);

		currptr->enabled[counter] = 1;
		timer_adjust_oneshot(currptr->timer[counter], duration, which);
		timer_enable(currptr->timer[counter], TRUE);
	}
}

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// ptm6840_get_ext_clock: get external clock frequency                   //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

int ptm6840_get_ext_clock(int which, int counter)
{
	ptm6840 *currptr = ptm + which;
	return currptr->external_clock[counter];
}

void ptm6840_set_c1(int which, int state) { ptm6840_set_clock(which, state, 0); }
void ptm6840_set_c2(int which, int state) { ptm6840_set_clock(which, state, 1); }
void ptm6840_set_c3(int which, int state) { ptm6840_set_clock(which, state, 2); }

/////////////////////////////////////////////////////////////////////////////////

READ8_HANDLER( ptm6840_0_r ) { return ptm6840_read(0, offset); }
READ8_HANDLER( ptm6840_1_r ) { return ptm6840_read(1, offset); }
READ8_HANDLER( ptm6840_2_r ) { return ptm6840_read(2, offset); }
READ8_HANDLER( ptm6840_3_r ) { return ptm6840_read(3, offset); }

WRITE8_HANDLER( ptm6840_0_w ) { ptm6840_write(0, offset, data); }
WRITE8_HANDLER( ptm6840_1_w ) { ptm6840_write(1, offset, data); }
WRITE8_HANDLER( ptm6840_2_w ) { ptm6840_write(2, offset, data); }
WRITE8_HANDLER( ptm6840_3_w ) { ptm6840_write(3, offset, data); }

READ16_HANDLER( ptm6840_0_msb_r ) { return ptm6840_read(0, offset) << 8; }
READ16_HANDLER( ptm6840_1_msb_r ) { return ptm6840_read(1, offset) << 8; }
READ16_HANDLER( ptm6840_2_msb_r ) { return ptm6840_read(2, offset) << 8; }
READ16_HANDLER( ptm6840_3_msb_r ) { return ptm6840_read(3, offset) << 8; }

WRITE16_HANDLER( ptm6840_0_msb_w ) { if (ACCESSING_BITS_8_15) ptm6840_write(0, offset, data >> 8); }
WRITE16_HANDLER( ptm6840_1_msb_w ) { if (ACCESSING_BITS_8_15) ptm6840_write(1, offset, data >> 8); }
WRITE16_HANDLER( ptm6840_2_msb_w ) { if (ACCESSING_BITS_8_15) ptm6840_write(2, offset, data >> 8); }
WRITE16_HANDLER( ptm6840_3_msb_w ) { if (ACCESSING_BITS_8_15) ptm6840_write(3, offset, data >> 8); }

READ16_HANDLER( ptm6840_0_lsb_r ) { return ptm6840_read(0, offset); }
READ16_HANDLER( ptm6840_1_lsb_r ) { return ptm6840_read(1, offset); }
READ16_HANDLER( ptm6840_2_lsb_r ) { return ptm6840_read(2, offset); }
READ16_HANDLER( ptm6840_3_lsb_r ) { return ptm6840_read(3, offset); }

WRITE16_HANDLER( ptm6840_0_lsb_w ) { if (ACCESSING_BITS_0_7) ptm6840_write(0, offset, data & 0xff); }
WRITE16_HANDLER( ptm6840_1_lsb_w ) { if (ACCESSING_BITS_0_7) ptm6840_write(1, offset, data & 0xff); }
WRITE16_HANDLER( ptm6840_2_lsb_w ) { if (ACCESSING_BITS_0_7) ptm6840_write(2, offset, data & 0xff); }
WRITE16_HANDLER( ptm6840_3_lsb_w ) { if (ACCESSING_BITS_0_7) ptm6840_write(3, offset, data & 0xff); }