summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/6526cia.c
blob: 71d91b4bcb05701eae0b413e790e685f77c46172 (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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/**********************************************************************

    MOS 6526/8520 CIA interface and emulation

    This function emulates all the functionality MOS6526 or
    MOS8520 complex interface adapters.

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

#include "emu.h"
#include "6526cia.h"

/***************************************************************************
    CONSTANTS
***************************************************************************/

/* CIA registers */
#define CIA_PRA			0
#define CIA_PRB			1
#define CIA_DDRA		2
#define CIA_DDRB		3
#define CIA_TALO		4
#define CIA_TAHI		5
#define CIA_TBLO		6
#define CIA_TBHI		7
#define CIA_TOD0		8		/* 6526: 1/10 seconds   8520: bits  0- 7 */
#define CIA_TOD1		9		/* 6526: seconds        8520: bits  8-15 */
#define CIA_TOD2		10		/* 6526: minutes        8520: bits 16-23 */
#define CIA_TOD3		11		/* 6526: hours          8520: N/A */
#define CIA_SDR			12
#define CIA_ICR			13
#define CIA_CRA			14
#define CIA_CRB			15

#define CIA_CRA_START	0x01
#define	CIA_CRA_PBON	0x02
#define	CIA_CRA_OUTMODE	0x04
#define	CIA_CRA_RUNMODE	0x08
#define	CIA_CRA_LOAD	0x10
#define	CIA_CRA_INMODE	0x20
#define	CIA_CRA_SPMODE	0x40
#define	CIA_CRA_TODIN	0x80

//**************************************************************************
//  DEVICE CONFIGURATION
//**************************************************************************

//-------------------------------------------------
//  mos6526_device_config - constructor
//-------------------------------------------------

mos6526_device_config::mos6526_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
    : device_config(mconfig, static_alloc_device_config, "MOS6526", tag, owner, clock)
{
}


//-------------------------------------------------
//  static_alloc_device_config - allocate a new
//  configuration object
//-------------------------------------------------

device_config *mos6526_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
{
    return global_alloc(mos6526_device_config(mconfig, tag, owner, clock));
}


//-------------------------------------------------
//  alloc_device - allocate a new device object
//-------------------------------------------------

device_t *mos6526_device_config::alloc_device(running_machine &machine) const
{
    return auto_alloc(&machine, mos6526_device(machine, *this));
}


//-------------------------------------------------
//  device_config_complete - perform any
//  operations now that the configuration is
//  complete
//-------------------------------------------------

void mos6526_device_config::device_config_complete()
{
	// inherit a copy of the static data
	const mos6526_interface *intf = reinterpret_cast<const mos6526_interface *>(static_config());
	if (intf != NULL)
		*static_cast<mos6526_interface *>(this) = *intf;

	// or initialize to defaults if none provided
	else
	{
		m_tod_clock = 0;
    	memset(&m_out_irq_func, 0, sizeof(m_out_irq_func));
    	memset(&m_out_pc_func, 0, sizeof(m_out_pc_func));
    	memset(&m_out_cnt_func, 0, sizeof(m_out_cnt_func));
    	memset(&m_out_sp_func, 0, sizeof(m_out_sp_func));
    	memset(&m_in_pa_func, 0, sizeof(m_in_pa_func));
    	memset(&m_out_pa_func, 0, sizeof(m_out_pa_func));
    	memset(&m_in_pb_func, 0, sizeof(m_in_pb_func));
    	memset(&m_out_pb_func, 0, sizeof(m_out_pb_func));
	}
}



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

const device_type MOS6526R1 = mos6526_device_config::static_alloc_device_config;
const device_type MOS6526R2 = mos6526_device_config::static_alloc_device_config;
const device_type MOS8520 = mos6526_device_config::static_alloc_device_config;

//-------------------------------------------------
//  mos6526_device - constructor
//-------------------------------------------------

mos6526_device::mos6526_device(running_machine &_machine, const mos6526_device_config &config)
    : device_t(_machine, config),
      m_config(config)
{

}


//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void mos6526_device::device_reset()
{
	/* clear things out */
	m_port[0].m_latch = 0x00;
	m_port[0].m_in = 0x00;
	m_port[0].m_out = 0x00;
	m_port[0].m_mask_value = 0xff;
	m_port[1].m_latch = 0x00;
	m_port[1].m_in = 0x00;
	m_port[1].m_out = 0x00;
	m_port[1].m_mask_value = 0xff;
	m_tod = 0;
	m_tod_latch = 0;
	m_alarm = 0;
	m_icr = 0x00;
	m_ics = 0x00;
	m_irq = 0;
	m_shift = 0;
	m_loaded = 0;
	m_cnt = 1;
	m_sp = 0;

	/* initialize data direction registers */
	m_port[0].m_ddr = !strcmp(tag(), "cia_0") ? 0x03 : 0xff;
	m_port[1].m_ddr = !strcmp(tag(), "cia_0") ? 0x00 : 0xff;

	/* TOD running by default */
	m_tod_running = TRUE;

	/* initialize timers */
	for(int t = 0; t < 2; t++)
	{
		cia_timer *timer = &m_timer[t];
		timer->m_cia = this;
		timer->m_clock = clock();
		timer->m_latch = 0xffff;
		timer->m_count = 0x0000;
		timer->m_mode = 0x00;
	}
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void mos6526_device::device_start()
{
	/* clear out CIA structure, and copy the interface */
	devcb_resolve_write_line(&m_out_irq_func, &m_config.m_out_irq_func, this);
	devcb_resolve_write_line(&m_out_pc_func, &m_config.m_out_pc_func, this);
	devcb_resolve_write_line(&m_out_cnt_func, &m_config.m_out_cnt_func, this);
	devcb_resolve_write_line(&m_out_sp_func, &m_config.m_out_sp_func, this);
	m_flag = 1;

	/* setup ports */
	devcb_resolve_read8(&m_port[0].m_read, &m_config.m_in_pa_func, this);
	devcb_resolve_write8(&m_port[0].m_write, &m_config.m_out_pa_func, this);
	devcb_resolve_read8(&m_port[1].m_read, &m_config.m_in_pb_func, this);
	devcb_resolve_write8(&m_port[1].m_write, &m_config.m_out_pb_func, this);

	for (int p = 0; p < (sizeof(m_port) / sizeof(m_port[0])); p++)
	{
		m_port[p].m_mask_value = 0xff;
	}

	/* setup timers */
	for (int t = 0; t < (sizeof(m_timer) / sizeof(m_timer[0])); t++)
	{
		cia_timer *timer = &m_timer[t];
		timer->m_timer = timer_alloc(&m_machine, timer_proc, (void*)this);
		timer->m_cia = this;
		timer->m_irq = 0x01 << t;
	}

	/* setup TOD timer, if appropriate */
	if (m_config.m_tod_clock != 0)
	{
		timer_pulse(&m_machine, ATTOTIME_IN_HZ(m_config.m_tod_clock), (void *)this, 0, clock_tod_callback);
	}

	/* state save support */
	state_save_register_device_item(this, 0, m_port[0].m_ddr);
	state_save_register_device_item(this, 0, m_port[0].m_latch);
	state_save_register_device_item(this, 0, m_port[0].m_in);
	state_save_register_device_item(this, 0, m_port[0].m_out);
	state_save_register_device_item(this, 0, m_port[0].m_mask_value);
	state_save_register_device_item(this, 0, m_port[1].m_ddr);
	state_save_register_device_item(this, 0, m_port[1].m_latch);
	state_save_register_device_item(this, 0, m_port[1].m_in);
	state_save_register_device_item(this, 0, m_port[1].m_out);
	state_save_register_device_item(this, 0, m_port[1].m_mask_value);
	state_save_register_device_item(this, 0, m_timer[0].m_latch);
	state_save_register_device_item(this, 0, m_timer[0].m_count);
	state_save_register_device_item(this, 0, m_timer[0].m_mode);
	state_save_register_device_item(this, 0, m_timer[0].m_irq);
	state_save_register_device_item(this, 0, m_timer[1].m_latch);
	state_save_register_device_item(this, 0, m_timer[1].m_count);
	state_save_register_device_item(this, 0, m_timer[1].m_mode);
	state_save_register_device_item(this, 0, m_timer[1].m_irq);
	state_save_register_device_item(this, 0, m_tod);
	state_save_register_device_item(this, 0, m_tod_latch);
	state_save_register_device_item(this, 0, m_tod_latched);
	state_save_register_device_item(this, 0, m_tod_running);
	state_save_register_device_item(this, 0, m_alarm);
	state_save_register_device_item(this, 0, m_icr);
	state_save_register_device_item(this, 0, m_ics);
	state_save_register_device_item(this, 0, m_irq);
	state_save_register_device_item(this, 0, m_flag);
	state_save_register_device_item(this, 0, m_loaded);
	state_save_register_device_item(this, 0, m_sdr);
	state_save_register_device_item(this, 0, m_sp);
	state_save_register_device_item(this, 0, m_cnt);
	state_save_register_device_item(this, 0, m_shift);
	state_save_register_device_item(this, 0, m_serial);
}


/*-------------------------------------------------
    set_port_mask_value
-------------------------------------------------*/

void mos6526_device::set_port_mask_value(int port, int data)
{
	m_port[port].m_mask_value = data;
}

/*-------------------------------------------------
    update_pc - pulse /pc output
-------------------------------------------------*/

void mos6526_device::update_pc()
{
	/* this should really be one cycle long */
	devcb_call_write_line(&m_out_pc_func, 0);
	devcb_call_write_line(&m_out_pc_func, 1);
}

/*-------------------------------------------------
    update_interrupts
-------------------------------------------------*/

void mos6526_device::update_interrupts()
{
	UINT8 new_irq;

	/* always update the high bit of ICS */
	if (m_ics & 0x7f)
	{
		m_ics |= 0x80;
	}
	else
	{
		m_ics &= ~0x80;
	}

	/* based on what is enabled, set/clear the IRQ via the custom chip */
	new_irq = (m_ics & m_icr) ? 1 : 0;
	if (m_irq != new_irq)
	{
		m_irq = new_irq;
		devcb_call_write_line(&m_out_irq_func, m_irq);
	}
}


/*-------------------------------------------------
    timer_bump
-------------------------------------------------*/

void mos6526_device::timer_bump(int timer)
{
	m_timer[timer].update(timer, -1);

	if (m_timer[timer].m_count == 0x00)
	{
		timer_underflow(timer);
	}
	else
	{
		m_timer[timer].update(timer, m_timer[timer].m_count - 1);
	}
}

/*-------------------------------------------------
    cia_timer_underflow
-------------------------------------------------*/

void mos6526_device::timer_underflow(int timer)
{
	assert((timer == 0) || (timer == 1));

	/* set the status and update interrupts */
	m_ics |= m_timer[timer].m_irq;
	update_interrupts();

	/* if one-shot mode, turn it off */
	if (m_timer[timer].m_mode & 0x08)
	{
		m_timer[timer].m_mode &= 0xfe;
	}

	/* reload the timer */
	m_timer[timer].update(timer, m_timer[timer].m_latch);

	/* timer A has some interesting properties */
	if (timer == 0)
	{
		/* such as cascading to timer B */
		if ((m_timer[1].m_mode & 0x41) == 0x41)
		{
			if (m_cnt || !(m_timer[1].m_mode & 0x20))
			{
				timer_bump(1);
			}
		}

		/* also the serial line */
		if ((m_timer[timer].m_irq == 0x01) && (m_timer[timer].m_mode & CIA_CRA_SPMODE))
		{
			if (m_loaded || m_shift)
			{
				/* falling edge */
				if (m_cnt)
				{
					if (m_shift == 0)
					{
						/* load shift register */
						m_loaded = 0;
						m_serial = m_sdr;
					}

					/* transmit MSB */
					m_sp = BIT(m_serial, 7);
					devcb_call_write_line(&m_out_sp_func, m_sp);

					/* toggle CNT */
					m_cnt = !m_cnt;
					devcb_call_write_line(&m_out_cnt_func, m_cnt);

					/* shift data */
					m_serial <<= 1;
					m_shift++;

					if (m_shift == 8)
					{
						/* signal interrupt */
						m_ics |= 0x08;
						update_interrupts();
					}
				}
				else
				{
					/* toggle CNT */
					m_cnt = !m_cnt;
					devcb_call_write_line(&m_out_cnt_func, m_cnt);

					if (m_shift == 8)
					{
						m_shift = 0;
					}
				}
			}
		}
	}
}

/*-------------------------------------------------
    TIMER_CALLBACK( cia_timer_proc )
-------------------------------------------------*/

TIMER_CALLBACK( mos6526_device::timer_proc )
{
    mos6526_device *cia = reinterpret_cast<mos6526_device *>(ptr);

	cia->timer_underflow(param);
}

/*-------------------------------------------------
    bcd_increment
-------------------------------------------------*/

static UINT8 bcd_increment(UINT8 value)
{
	value++;
	if ((value & 0x0f) >= 0x0a)
		value += 0x10 - 0x0a;
	return value;
}

/*-------------------------------------------------
    cia6526_increment
-------------------------------------------------*/

void mos6526_device::increment()
{
	/* break down TOD value into components */
	UINT8 subsecond	= (UINT8) (m_tod >>  0);
	UINT8 second	= (UINT8) (m_tod >>  8);
	UINT8 minute	= (UINT8) (m_tod >> 16);
	UINT8 hour		= (UINT8) (m_tod >> 24);

	subsecond = bcd_increment(subsecond);
	if (subsecond >= 0x10)
	{
		subsecond = 0x00;
		second = bcd_increment(second);
		if (second >= ((m_timer[0].m_mode & 0x80) ? 0x50 : 0x60))
		{
			second = 0x00;
			minute = bcd_increment(minute);
			if (minute >= 0x60)
			{
				minute = 0x00;
				if (hour == 0x91)
					hour = 0x00;
				else if (hour == 0x89)
					hour = 0x90;
				else if (hour == 0x11)
					hour = 0x80;
				else if (hour == 0x09)
					hour = 0x10;
				else
					hour++;
			}
		}
	}

	/* update the TOD with new value */
	m_tod = (((UINT32) subsecond)	<<  0)
		  | (((UINT32) second)		<<  8)
		  | (((UINT32) minute)		<< 16)
		  | (((UINT32) hour)		<< 24);
}

/*-------------------------------------------------
    cia_clock_tod - Update TOD on CIA A
-------------------------------------------------*/

void mos6526_device::clock_tod()
{
	if (m_tod_running)
	{
		if ((type() == MOS6526R1) || (type() == MOS6526R2))
		{
			/* The 6526 split the value into hours, minutes, seconds and
             * subseconds */
			increment();
		}
		else if (type() == MOS8520)
		{
			/* the 8520 has a straight 24-bit counter */
			m_tod++;
			m_tod &= 0xffffff;
		}

		if (m_tod == m_alarm)
		{
			m_ics |= 0x04;
			update_interrupts();
		}
	}
}


/*-------------------------------------------------
    clock_tod_callback
-------------------------------------------------*/

TIMER_CALLBACK( mos6526_device::clock_tod_callback )
{
    mos6526_device *cia = reinterpret_cast<mos6526_device *>(ptr);
	cia->clock_tod();
}


/*-------------------------------------------------
	cnt_w
-------------------------------------------------*/

void mos6526_device::cnt_w(UINT8 state)
{
	/* is this a rising edge? */
	if (!m_cnt && state)
	{
		if (m_timer[0].m_mode & CIA_CRA_START)
		{
			/* does timer #0 bump on CNT? */
			if (m_timer[0].m_mode & CIA_CRA_INMODE)
			{
				timer_bump(0);
			}
		}

		/* if the serial port is set to input, the CNT will shift the port */
		if (!(m_timer[0].m_mode & CIA_CRA_SPMODE))
		{
			m_serial <<= 1;
			m_shift++;

			if (m_sp)
			{
				m_serial |= 0x01;
			}

			if (m_shift == 8)
			{
				m_sdr = m_serial;
				m_serial = 0;
				m_shift = 0;
				m_ics |= 0x08;
				update_interrupts();
			}
		}

		/* does timer #1 bump on CNT? */
		if ((m_timer[1].m_mode & 0x61) == 0x21)
		{
			timer_bump(1);
		}
	}

	m_cnt = state;
}

void mos6526_device::flag_w(UINT8 state)
{
	/* falling edge */
	if (m_flag && !state)
	{
		m_ics |= 0x10;
		update_interrupts();
	}

	m_flag = state;
}

/*-------------------------------------------------
    reg_r
-------------------------------------------------*/

UINT8 mos6526_device::reg_r(UINT8 offset)
{
	cia_timer *timer;
	cia_port *port;
	UINT8 data = 0x00;

	offset &= 0x0F;

	switch(offset)
	{
		/* port A/B data */
		case CIA_PRA:
		case CIA_PRB:
			port = &m_port[offset & 1];
			data = devcb_call_read8(&port->m_read, 0);
			data = ((data & ~port->m_ddr) | (port->m_latch & port->m_ddr)) & port->m_mask_value;

			port->m_in = data;

			if (offset == CIA_PRB)
			{
				/* timer #0 can change PB6 */
				if (m_timer[0].m_mode & 0x02)
				{
					m_timer[0].update(0, -1);
					if (m_timer[0].m_count != 0)
					{
						data |= 0x40;
					}
					else
					{
						data &= ~0x40;
					}
				}

				/* timer #1 can change PB7 */
				if (m_timer[1].m_mode & 0x02)
				{
					m_timer[1].update(1, -1);
					if (m_timer[1].m_count != 0)
					{
						data |= 0x80;
					}
					else
					{
						data &= ~0x80;
					}
				}

				/* pulse /PC following the read */
				update_pc();
			}
			break;

		/* port A/B direction */
		case CIA_DDRA:
		case CIA_DDRB:
			port = &m_port[offset & 1];
			data = port->m_ddr;
			break;

		/* timer A/B low byte */
		case CIA_TALO:
		case CIA_TBLO:
			timer = &m_timer[(offset >> 1) & 1];
			data = timer->get_count() >> 0;
			break;

		/* timer A/B high byte */
		case CIA_TAHI:
		case CIA_TBHI:
			timer = &m_timer[(offset >> 1) & 1];
			data = timer->get_count() >> 8;
			break;

		/* TOD counter */
		case CIA_TOD0:
		case CIA_TOD1:
		case CIA_TOD2:
		case CIA_TOD3:
			if (type() == MOS8520)
			{
				if (offset == CIA_TOD2)
				{
					m_tod_latch = m_tod;
					m_tod_latched = TRUE;
				}
			}
			else
			{
				if (offset == CIA_TOD3)
				{
					m_tod_latch = m_tod;
					m_tod_latched = TRUE;
				}
			}
			if (offset == CIA_TOD0)
			{
				m_tod_latched = FALSE;
			}

			if (m_tod_latched)
			{
				data = m_tod_latch >> ((offset - CIA_TOD0) * 8);
			}
			else
			{
				data = m_tod >> ((offset - CIA_TOD0) * 8);
			}
			break;

		/* serial data ready */
		case CIA_SDR:
			data = m_sdr;
			break;

		/* interrupt status/clear */
		case CIA_ICR:
			data = m_ics;
			m_ics = 0; /* clear on read */
			update_interrupts();
			break;

		/* timer A/B mode */
		case CIA_CRA:
		case CIA_CRB:
			timer = &m_timer[offset & 1];
			data = timer->m_mode;
			break;
	}

	return data;
}

/*-------------------------------------------------
    reg_w
-------------------------------------------------*/

void mos6526_device::reg_w(UINT8 offset, UINT8 data)
{
	cia_timer *timer;
	cia_port *port;
	int shift;

	offset &= 0x0F;

	switch(offset)
	{
		/* port A/B data */
		case CIA_PRA:
		case CIA_PRB:
			port = &m_port[offset & 1];
			port->m_latch = data;
			port->m_out = (data & port->m_ddr) | (port->m_in & ~port->m_ddr);
			devcb_call_write8(&port->m_write, 0, port->m_out);

			/* pulse /PC following the write */
			if (offset == CIA_PRB)
			{
				update_pc();
			}

			break;

		/* port A/B direction */
		case CIA_DDRA:
		case CIA_DDRB:
			port = &m_port[offset & 1];
			port->m_ddr = data;
			break;

		/* timer A/B latch low */
		case CIA_TALO:
		case CIA_TBLO:
			timer = &m_timer[(offset >> 1) & 1];
			timer->m_latch = (timer->m_latch & 0xff00) | (data << 0);
			break;

		/* timer A latch high */
		case CIA_TAHI:
		case CIA_TBHI:
			timer = &m_timer[(offset >> 1) & 1];
			timer->m_latch = (timer->m_latch & 0x00ff) | (data << 8);

			/* if the timer is one-shot, then force a start on it */
			if (timer->m_mode & 0x08)
			{
				timer->m_mode |= 1;
				timer->update((offset >> 1) & 1, timer->m_latch);
			}
			else
			{
				/* if the timer is off, update the count */
				if (!(timer->m_mode & 0x01))
				{
					timer->update((offset >> 1) & 1, timer->m_latch);
				}
			}
			break;

		/* time of day latches */
		case CIA_TOD0:
		case CIA_TOD1:
		case CIA_TOD2:
		case CIA_TOD3:
			shift = 8 * ((offset - CIA_TOD0));

			/* alarm setting mode? */
			if (m_timer[1].m_mode & 0x80)
			{
				m_alarm = (m_alarm & ~(0xff << shift)) | (data << shift);
			}
			/* counter setting mode */
			else
			{
				m_tod = (m_tod & ~(0xff << shift)) | (data << shift);
			}

			if (type() == MOS8520)
			{
				if (offset == CIA_TOD2)
				{
					m_tod_running = FALSE;
				}
			}
			else
			{
				if (offset == CIA_TOD3)
				{
					m_tod_running = FALSE;
				}
			}
			if (offset == CIA_TOD0)
			{
				m_tod_running = TRUE;
			}
			break;

		/* serial data ready */
		case CIA_SDR:
			m_sdr = data;
			if (m_timer[0].m_mode & 0x40)
			{
				m_loaded = 1;
			}
			break;

		/* interrupt control register */
		case CIA_ICR:
			if (data & 0x80)
			{
				m_icr |= data & 0x7f;
			}
			else
			{
				m_icr &= ~(data & 0x7f);
			}
			update_interrupts();
			break;

		/* timer A/B modes */
		case CIA_CRA:
		case CIA_CRB:
			timer = &m_timer[offset & 1];
			timer->m_mode = data & 0xef;

			/* force load? */
			if (data & 0x10)
			{
				timer->update(offset & 1, timer->m_latch);
			}
			else
			{
				timer->update(offset & 1, -1);
			}
			break;
	}
}

/*-------------------------------------------------
    is_timer_active
-------------------------------------------------*/

static int is_timer_active(emu_timer *timer)
{
	attotime t = timer_firetime(timer);
	return attotime_compare(t, attotime_never) != 0;
}

/*-------------------------------------------------
    update - updates the count and emu_timer for
    a given CIA timer
-------------------------------------------------*/

void mos6526_device::cia_timer::update(int which, INT32 new_count)
{
	/* sanity check arguments */
	assert((new_count >= -1) && (new_count <= 0xffff));

	/* update the timer count, if necessary */
	if ((new_count == -1) && is_timer_active(m_timer))
	{
		UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(m_timer), m_clock));
		m_count = m_count - MIN(m_count, current_count);
	}

	/* set the timer if we are instructed to */
	if (new_count != -1)
	{
		m_count = new_count;
	}

	/* now update the MAME timer */
	if ((m_mode & 0x01) && ((m_mode & (which ? 0x60 : 0x20)) == 0x00))
	{
		/* timer is on and is connected to clock */
		attotime period = attotime_mul(ATTOTIME_IN_HZ(m_clock), (m_count ? m_count : 0x10000));
		timer_adjust_oneshot(m_timer, period, which);
	}
	else
	{
		/* timer is off or not connected to clock */
		timer_adjust_oneshot(m_timer, attotime_never, which);
	}
}

/*-------------------------------------------------
    get_count - get the count for a given CIA
    timer
-------------------------------------------------*/

UINT16 mos6526_device::cia_timer::get_count()
{
	UINT16 count;

	if (is_timer_active(m_timer))
	{
		UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(m_timer), m_clock));
		count = m_count - MIN(m_count, current_count);
	}
	else
	{
		count = m_count;
	}

	return count;
}

/***************************************************************************
    TRAMPOLINES
***************************************************************************/

void cia_set_port_mask_value(running_device *device, int port, int data) { downcast<mos6526_device *>(device)->set_port_mask_value(port, data); }

READ8_DEVICE_HANDLER( mos6526_r ) { return downcast<mos6526_device *>(device)->reg_r(offset); }
WRITE8_DEVICE_HANDLER( mos6526_w ) { downcast<mos6526_device *>(device)->reg_w(offset, data); }

READ8_DEVICE_HANDLER( mos6526_pa_r ) { return downcast<mos6526_device *>(device)->pa_r(offset); }
READ8_DEVICE_HANDLER( mos6526_pb_r ) { return downcast<mos6526_device *>(device)->pb_r(offset); }

READ_LINE_DEVICE_HANDLER( mos6526_irq_r ) { return downcast<mos6526_device *>(device)->irq_r(); }

WRITE_LINE_DEVICE_HANDLER( mos6526_tod_w ) { downcast<mos6526_device *>(device)->tod_w(state); }

READ_LINE_DEVICE_HANDLER( mos6526_cnt_r ) { return downcast<mos6526_device *>(device)->cnt_r(); }
WRITE_LINE_DEVICE_HANDLER( mos6526_cnt_w ) { downcast<mos6526_device *>(device)->cnt_w(state); }

READ_LINE_DEVICE_HANDLER( mos6526_sp_r ) { return downcast<mos6526_device *>(device)->sp_r(); }
WRITE_LINE_DEVICE_HANDLER( mos6526_sp_w ) { downcast<mos6526_device *>(device)->sp_w(state); }

WRITE_LINE_DEVICE_HANDLER( mos6526_flag_w ) { downcast<mos6526_device *>(device)->flag_w(state); }