summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/6840ptm.c
blob: 6014e681de472403a701b7871ca7cef6f93ff0df (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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/***************************************************************************

    Motorola 6840 (PTM)

    Programmable Timer Module

    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).


    2009-06 Converted to be a device

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

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


#define PTMVERBOSE 0
#define PLOG(x) do { if (PTMVERBOSE) logerror x; } while (0)

/***************************************************************************
    PARAMETERS / PROTOTYPES
***************************************************************************/

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,
};

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"
};

static void ptm6840_timeout(const device_config *device, int idx);

/***************************************************************************
	TYPE DEFINITIONS
***************************************************************************/

typedef struct _ptm6840_state ptm6840_state;
struct _ptm6840_state
{
	int internal_clock;
	int external_clock[3];
	devcb_resolved_write8 out_func[3];	// function to call when output[idx] changes
	devcb_resolved_write_line irq_func;	// function called if IRQ line changes

	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;

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

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


/***************************************************************************
	INLINE FUNCTIONS
***************************************************************************/

INLINE ptm6840_state *get_safe_token(const device_config *device)
{
	assert(device != NULL);
	assert(device->token != NULL);
	assert((device->type == PTM6840));
	return (ptm6840_state *)device->token;
}

INLINE const ptm6840_interface *get_interface(const device_config *device)
{
	assert(device != NULL);
	assert((device->type == PTM6840));
	return (const ptm6840_interface *) device->static_config;
}


/***************************************************************************
	IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
	ptm6840_get_status - Get enabled status
-------------------------------------------------*/

int ptm6840_get_status( const device_config *device, int clock )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	return ptm6840->enabled[clock - 1];
}

/*-------------------------------------------------
	ptm6840_get_irq - Get IRQ state
-------------------------------------------------*/

int ptm6840_get_irq( const device_config *device )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	return ptm6840->IRQ;
}

/*-------------------------------------------------
	subtract_from_counter - Subtract from Counter
-------------------------------------------------*/

static void subtract_from_counter( const device_config *device, int counter, int count )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int clock;
	attotime duration;

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

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

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

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

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

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

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

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

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

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

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

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

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

		timer_adjust_oneshot(ptm6840->timer[counter], duration, 0);
	}
}

/*-------------------------------------------------
	ptm_tick
-------------------------------------------------*/

static void ptm_tick( const device_config *device, int counter, int count )
{
	ptm6840_state *ptm6840 = get_safe_token(device);

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

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

/*-------------------------------------------------
	update_interrupts - Update Internal Interrupts
-------------------------------------------------*/

INLINE void update_interrupts( const device_config *device )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int new_state;

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

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

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

		devcb_call_write_line(&ptm6840->irq_func, ptm6840->IRQ);
	}
}

/*-------------------------------------------------
	compute_counter - Compute Counter
-------------------------------------------------*/

static UINT16 compute_counter( const device_config *device, int counter )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int clock;
	int remaining = 0;

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

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

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

/*-------------------------------------------------
	reload_count - Reload Counter
-------------------------------------------------*/

static void reload_count( const device_config *device, int idx )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int clock;
	int count;
	attotime duration;

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

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

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

	ptm6840->fired[idx] = 0;

	if ((ptm6840->mode[idx] == 4) || (ptm6840->mode[idx] == 6))
	{
		ptm6840->output[idx] = 1;
		if (ptm6840->out_func[idx].write != NULL)
			devcb_call_write8(&ptm6840->out_func[idx], 0, ptm6840->output[idx]);
	}

	/* Set the timer */
	PLOG(("MC6840 #%s: reload_count(%d): clock = %d  count = %d\n", device->tag, idx, clock, count));

	duration = attotime_mul(ATTOTIME_IN_HZ(clock), count);
	if (idx == 2) 
		duration = attotime_mul(duration, ptm6840->t3_divisor);

	PLOG(("MC6840 #%s: reload_count(%d): output = %lf\n", device->tag, idx, attotime_to_double(duration)));

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


/*-------------------------------------------------
	ptm6840_read - Read Timer
-------------------------------------------------*/

READ8_DEVICE_HANDLER( ptm6840_read )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int val;

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

		case PTM_6840_STATUS:
		{
			PLOG(("%s: MC6840 #%s: Status read = %04X\n", cpuexec_describe_context(device->machine), device->tag, ptm6840->status_reg));
			ptm6840->status_read_since_int |= ptm6840->status_reg & 0x07;
			val = ptm6840->status_reg;
			break;
		}

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

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

			ptm6840->lsb_buffer = result & 0xff;

			PLOG(("%s: MC6840 #%s: Counter %d read = %04X\n", cpuexec_describe_context(device->machine), device->tag, idx, result >> 8));
			val = result >> 8;
			break;
		}

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

		default:
		{
			val = 0;
			break;
		}

	}
	return val;
}

/*-------------------------------------------------
	ptm6840_write - Write Timer
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( ptm6840_write )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int idx;
	int i;
	UINT8 diffs;

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

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

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

				ptm6840->status_reg = 0;
				update_interrupts(device);

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

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

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

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

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

			PLOG(("%s:MC6840 #%s: Counter %d latch = %04X\n", cpuexec_describe_context(device->machine), device->tag, idx, ptm6840->latch[idx]));
			break;
		}
	}
}

/*-------------------------------------------------
	ptm6840_timeout - Called if timer is mature
-------------------------------------------------*/

static void ptm6840_timeout( const device_config *device, int idx )
{
	ptm6840_state *ptm6840 = get_safe_token(device);

	PLOG(("**ptm6840 %s t%d timeout**\n", device->tag, idx + 1));

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

	if ( ptm6840->control_reg[idx] & 0x80 )
	{
		if ((ptm6840->mode[idx] == 0)||(ptm6840->mode[idx] == 2))
		{
			ptm6840->output[idx] = ptm6840->output[idx] ? 0 : 1;
			PLOG(("**ptm6840 %s t%d output %d **\n", device->tag, idx + 1, ptm6840->output[idx]));

			if (ptm6840->out_func[idx].write != NULL)
				devcb_call_write8(&ptm6840->out_func[idx], 0, ptm6840->output[idx]);
		}
		if ((ptm6840->mode[idx] == 4)||(ptm6840->mode[idx] == 6))
		{
			if (!ptm6840->fired[idx])
			{
				ptm6840->output[idx] = 1;
				PLOG(("**ptm6840 %s t%d output %d **\n", device->tag, idx + 1, ptm6840->output[idx]));

				if (ptm6840->out_func[idx].write != NULL)
					devcb_call_write8(&ptm6840->out_func[idx], 0, ptm6840->output[idx]);

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

/*-------------------------------------------------
	TIMER_CALLBACKs for Timer 1, 2 & 3
-------------------------------------------------*/

static TIMER_CALLBACK( ptm6840_timer1_cb ) 
{ 
	const device_config *device = (const device_config *)ptr;
	ptm6840_timeout(device, 0); 
}

static TIMER_CALLBACK( ptm6840_timer2_cb )
{ 
	const device_config *device = (const device_config *)ptr;
	ptm6840_timeout(device, 1); 
}

static TIMER_CALLBACK( ptm6840_timer3_cb )
{ 
	const device_config *device = (const device_config *)ptr;
	ptm6840_timeout(device, 2); 
}


/*-------------------------------------------------
	ptm6840_set_gate - set gate status (0 or 1)
-------------------------------------------------*/

INLINE void ptm6840_set_gate( const device_config *device, int state, int idx )
{
	ptm6840_state *ptm6840 = get_safe_token(device);

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

/*-------------------------------------------------
	WRITE8_DEVICE_HANDLERs for Gate 1, 2 & 3
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( ptm6840_set_g1 )
{ 
	ptm6840_set_gate(device, data, 0);
}

WRITE8_DEVICE_HANDLER( ptm6840_set_g2 )
{ 
	ptm6840_set_gate(device, data, 1);
}

WRITE8_DEVICE_HANDLER( ptm6840_set_g3 )
{ 
	ptm6840_set_gate(device, data, 2);
}

/*-------------------------------------------------
	ptm6840_set_clock - set clock status (0 or 1)
-------------------------------------------------*/

INLINE void ptm6840_set_clock( const device_config *device, int state, int idx )
{
	ptm6840_state *ptm6840 = get_safe_token(device);

	ptm6840->clock[idx] = state;

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

/*-------------------------------------------------
	WRITE8_DEVICE_HANDLERs for Clock 1, 2 & 3
-------------------------------------------------*/

WRITE8_DEVICE_HANDLER( ptm6840_set_c1 )
{ 
	ptm6840_set_clock(device, data, 0); 
}

WRITE8_DEVICE_HANDLER( ptm6840_set_c2 )
{ 
	ptm6840_set_clock(device, data, 1); 
}

WRITE8_DEVICE_HANDLER( ptm6840_set_c3 )
{ 
	ptm6840_set_clock(device, data, 2); 
}


/*-------------------------------------------------
	ptm6840_get_count - get count value
-------------------------------------------------*/

UINT16 ptm6840_get_count( const device_config *device, int counter )
{
	return compute_counter(device, counter);
}

/*------------------------------------------------------------
	ptm6840_set_ext_clock - set external clock frequency
------------------------------------------------------------*/

void ptm6840_set_ext_clock( const device_config *device, int counter, int clock )
{
	ptm6840_state *ptm6840 = get_safe_token(device);

	ptm6840->external_clock[counter] = clock;

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

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

		if (ptm6840->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, ptm6840->t3_divisor);

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

/*------------------------------------------------------------
	ptm6840_get_ext_clock - get external clock frequency
------------------------------------------------------------*/

int ptm6840_get_ext_clock( const device_config *device, int counter )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	return ptm6840->external_clock[counter];
}


/*-------------------------------------------------
	DEVICE_START( ptm6840 )
-------------------------------------------------*/

static DEVICE_START( ptm6840 )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	const ptm6840_interface *intf = get_interface(device);
	int i;

	ptm6840->internal_clock = intf->internal_clock;

	/* resolve callbacks */
	for (i = 0; i < 3; i++)
	{
		devcb_resolve_write8(&ptm6840->out_func[i], &intf->out_func[i], device);
	}

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


	ptm6840->timer[0] = timer_alloc(device->machine, ptm6840_timer1_cb, (void *)device);
	ptm6840->timer[1] = timer_alloc(device->machine, ptm6840_timer2_cb, (void *)device);
	ptm6840->timer[2] = timer_alloc(device->machine, ptm6840_timer3_cb, (void *)device);

	for (i = 0; i < 3; i++)
	{
		timer_enable(ptm6840->timer[i], FALSE);
	}

	devcb_resolve_write_line(&ptm6840->irq_func, &intf->irq_func, device);

	/* register for state saving */
	state_save_register_device_item(device, 0, ptm6840->lsb_buffer);
	state_save_register_device_item(device, 0, ptm6840->msb_buffer);
	state_save_register_device_item(device, 0, ptm6840->status_read_since_int);
	state_save_register_device_item(device, 0, ptm6840->status_reg);
	state_save_register_device_item(device, 0, ptm6840->t3_divisor);
	state_save_register_device_item(device, 0, ptm6840->t3_scaler);
	state_save_register_device_item(device, 0, ptm6840->internal_clock);
	state_save_register_device_item(device, 0, ptm6840->IRQ);

	state_save_register_device_item_array(device, 0, ptm6840->control_reg);
	state_save_register_device_item_array(device, 0, ptm6840->output);
	state_save_register_device_item_array(device, 0, ptm6840->gate);
	state_save_register_device_item_array(device, 0, ptm6840->clock);
	state_save_register_device_item_array(device, 0, ptm6840->mode);
	state_save_register_device_item_array(device, 0, ptm6840->fired);
	state_save_register_device_item_array(device, 0, ptm6840->enabled);
	state_save_register_device_item_array(device, 0, ptm6840->external_clock);
	state_save_register_device_item_array(device, 0, ptm6840->counter);
	state_save_register_device_item_array(device, 0, ptm6840->latch);
}

/*-------------------------------------------------
	DEVICE_RESET( ptm6840 )
-------------------------------------------------*/

static DEVICE_RESET( ptm6840 )
{
	ptm6840_state *ptm6840 = get_safe_token(device);
	int i;

	ptm6840->control_reg[2]		 = 0;
	ptm6840->control_reg[1]		 = 0;
	ptm6840->control_reg[0]		 = 1;
	ptm6840->status_reg			 = 0;
	ptm6840->t3_divisor			 = 1;
	ptm6840->status_read_since_int = 0;
	ptm6840->IRQ                   = 0;

	for (i = 0; i < 3; i++)
	{
		ptm6840->counter[i] = 0xffff;
		ptm6840->latch[i]   = 0xffff;
		ptm6840->output[i]  = 0;
		ptm6840->fired[i]   = 0;
	}
}

/*-------------------------------------------------
    device definition
-------------------------------------------------*/

static const char *DEVTEMPLATE_SOURCE = __FILE__;

#define DEVTEMPLATE_ID(p,s)		p##ptm6840##s
#define DEVTEMPLATE_FEATURES	DT_HAS_START | DT_HAS_RESET
#define DEVTEMPLATE_NAME		"6840 PTM"
#define DEVTEMPLATE_FAMILY		"Motorola Programmable Timer Modules"
#define DEVTEMPLATE_CLASS		DEVICE_CLASS_PERIPHERAL
#include "devtempl.h"