summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/mc68901.cpp
blob: fa6256f18716056a979ea4ce7f9b6d55576a1544 (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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
// license:BSD-3-Clause
// copyright-holders:Curt Coder
/**********************************************************************

    Motorola MC68901 Multi Function Peripheral emulation

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

/*

    TODO:

    - daisy chaining
    - disable GPIO3/4 interrupts when timer A/B in pulse mode
    - spurious interrupt

        If you look at the MFP datasheet it is obvious that it can generate the conditions for a spurious interrupt.
        However the fact that they indeed happen in the ST is quite interesting.

        The MFP will generate a spurious interrupt if interrupts are disabled (by changing the IERA/IERB registers)
        at the 'precise point'. The precise point would be after the system (but not necessarily the CPU, see below)
        triggered an MFP interrupt, and before the CPU drives the interrupt acknowledge cycle.

        If the MFP was connected directly to the CPU, spurious interrupts probably couldn't happen. However in the
        ST, GLUE seats in the middle and handles all the interrupt timing. It is possible that GLUE introduces a
        delay between detecting a change in the MFP interrupt request signal and actually propagating the change to
        the CPU IPL signals (it is even possible that GLUE make some kind of latching). This would create a window
        long enough for the 'precise point' described above.

        "yes, the spurious interrupt occurs when i mask a timer. i did not notice an occurance of the SPI when changing data and control registers.
        if i kill interrupts with the status reg before masking the timer interrupt, then the SPI occurs as soon as the status register is set to re-enable interrupts."

        Well, more experiments show that it's somewhat incorrect, and
        the GLUE is essentially invisible w.r.t IPL.  The CPU and the
        MFP manage to add the delays all by themselves.

    - divide serial clock by 16
    - synchronous mode
    - 1.5/2 stop bits
    - interrupt on receiver break end
    - interrupt on character boundaries during break transmission
    - loopback mode

*/

#include "emu.h"
#include "mc68901.h"
#include "cpu/m68000/m68000.h"


// device type definition
const device_type MC68901 = &device_creator<mc68901_device>;


//**************************************************************************
//  MACROS / CONSTANTS
//**************************************************************************

#define LOG 0



#define AER_GPIP_0              0x01
#define AER_GPIP_1              0x02
#define AER_GPIP_2              0x04
#define AER_GPIP_3              0x08
#define AER_GPIP_4              0x10
#define AER_GPIP_5              0x20
#define AER_GPIP_6              0x40
#define AER_GPIP_7              0x80


#define VR_S                    0x08


#define IR_GPIP_0               0x0001
#define IR_GPIP_1               0x0002
#define IR_GPIP_2               0x0004
#define IR_GPIP_3               0x0008
#define IR_TIMER_D              0x0010
#define IR_TIMER_C              0x0020
#define IR_GPIP_4               0x0040
#define IR_GPIP_5               0x0080
#define IR_TIMER_B              0x0100
#define IR_XMIT_ERROR           0x0200
#define IR_XMIT_BUFFER_EMPTY    0x0400
#define IR_RCV_ERROR            0x0800
#define IR_RCV_BUFFER_FULL      0x1000
#define IR_TIMER_A              0x2000
#define IR_GPIP_6               0x4000
#define IR_GPIP_7               0x8000


#define TCR_TIMER_STOPPED       0x00
#define TCR_TIMER_DELAY_4       0x01
#define TCR_TIMER_DELAY_10      0x02
#define TCR_TIMER_DELAY_16      0x03
#define TCR_TIMER_DELAY_50      0x04
#define TCR_TIMER_DELAY_64      0x05
#define TCR_TIMER_DELAY_100     0x06
#define TCR_TIMER_DELAY_200     0x07
#define TCR_TIMER_EVENT         0x08
#define TCR_TIMER_PULSE_4       0x09
#define TCR_TIMER_PULSE_10      0x0a
#define TCR_TIMER_PULSE_16      0x0b
#define TCR_TIMER_PULSE_50      0x0c
#define TCR_TIMER_PULSE_64      0x0d
#define TCR_TIMER_PULSE_100     0x0e
#define TCR_TIMER_PULSE_200     0x0f
#define TCR_TIMER_RESET         0x10


#define UCR_PARITY_ENABLED      0x04
#define UCR_PARITY_EVEN         0x02
#define UCR_PARITY_ODD          0x00
#define UCR_WORD_LENGTH_8       0x00
#define UCR_WORD_LENGTH_7       0x20
#define UCR_WORD_LENGTH_6       0x40
#define UCR_WORD_LENGTH_5       0x60
#define UCR_START_STOP_0_0      0x00
#define UCR_START_STOP_1_1      0x08
#define UCR_START_STOP_1_15     0x10
#define UCR_START_STOP_1_2      0x18
#define UCR_CLOCK_DIVIDE_16     0x80
#define UCR_CLOCK_DIVIDE_1      0x00


#define RSR_RCV_ENABLE          0x01
#define RSR_SYNC_STRIP_ENABLE   0x02
#define RSR_MATCH               0x04
#define RSR_CHAR_IN_PROGRESS    0x04
#define RSR_FOUND_SEARCH        0x08
#define RSR_BREAK               0x08
#define RSR_FRAME_ERROR         0x10
#define RSR_PARITY_ERROR        0x20
#define RSR_OVERRUN_ERROR       0x40
#define RSR_BUFFER_FULL         0x80

#define TSR_XMIT_ENABLE         0x01
#define TSR_OUTPUT_HI_Z         0x00
#define TSR_OUTPUT_LOW          0x02
#define TSR_OUTPUT_HIGH         0x04
#define TSR_OUTPUT_LOOP         0x06
#define TSR_OUTPUT_MASK         0x06
#define TSR_BREAK               0x08
#define TSR_END_OF_XMIT         0x10
#define TSR_AUTO_TURNAROUND     0x20
#define TSR_UNDERRUN_ERROR      0x40
#define TSR_BUFFER_EMPTY        0x80

#define DIVISOR PRESCALER[data & 0x07]


const int mc68901_device::INT_MASK_GPIO[] =
{
	IR_GPIP_0, IR_GPIP_1, IR_GPIP_2, IR_GPIP_3,
	IR_GPIP_4, IR_GPIP_5, IR_GPIP_6, IR_GPIP_7
};


const int mc68901_device::INT_MASK_TIMER[] =
{
	IR_TIMER_A, IR_TIMER_B, IR_TIMER_C, IR_TIMER_D
};


const int mc68901_device::GPIO_TIMER[] =
{
	GPIP_4, GPIP_3
};


const int mc68901_device::PRESCALER[] = { 0, 4, 10, 16, 50, 64, 100, 200 };


//**************************************************************************
//  INLINE HELPERS
//**************************************************************************

inline void mc68901_device::check_interrupts()
{
	if (m_ipr & m_imr)
	{
		m_out_irq_cb(ASSERT_LINE);
	}
	else
	{
		m_out_irq_cb(CLEAR_LINE);
	}
}

inline void mc68901_device::take_interrupt(uint16_t mask)
{
	m_ipr |= mask;

	check_interrupts();
}

inline void mc68901_device::rx_buffer_full()
{
	if (m_ier & IR_RCV_BUFFER_FULL)
	{
		take_interrupt(IR_RCV_BUFFER_FULL);
	}
}

inline void mc68901_device::rx_error()
{
	if (m_ier & IR_RCV_ERROR)
	{
		take_interrupt(IR_RCV_ERROR);
	}
	else
	{
		rx_buffer_full();
	}
}

inline void mc68901_device::timer_count(int index)
{
	if (m_tmc[index] == 0x01)
	{
		/* toggle timer output signal */
		m_to[index] = !m_to[index];

		switch (index)
		{
		case TIMER_A:   m_out_tao_cb(m_to[index]);    break;
		case TIMER_B:   m_out_tbo_cb(m_to[index]);    break;
		case TIMER_C:   m_out_tco_cb(m_to[index]);    break;
		case TIMER_D:   m_out_tdo_cb(m_to[index]);    break;
		}

		if (m_ier & INT_MASK_TIMER[index])
		{
			/* signal timer elapsed interrupt */
			take_interrupt(INT_MASK_TIMER[index]);
		}

		/* load main counter */
		m_tmc[index] = m_tdr[index];
	}
	else
	{
		/* count down */
		m_tmc[index]--;
	}
}


inline void mc68901_device::timer_input(int index, int value)
{
	int bit = GPIO_TIMER[index];
	int aer = BIT(m_aer, bit);
	int cr = index ? m_tbcr : m_tacr;

	switch (cr & 0x0f)
	{
	case TCR_TIMER_EVENT:
		if (((m_ti[index] ^ aer) == 1) && ((value ^ aer) == 0))
		{
			timer_count(index);
		}

		m_ti[index] = value;
		break;

	case TCR_TIMER_PULSE_4:
	case TCR_TIMER_PULSE_10:
	case TCR_TIMER_PULSE_16:
	case TCR_TIMER_PULSE_50:
	case TCR_TIMER_PULSE_64:
	case TCR_TIMER_PULSE_100:
	case TCR_TIMER_PULSE_200:
		m_timer[index]->enable((value == aer));

		if (((m_ti[index] ^ aer) == 0) && ((value ^ aer) == 1))
		{
			if (m_ier & INT_MASK_GPIO[bit])
			{
				take_interrupt(INT_MASK_GPIO[bit]);
			}
		}

		m_ti[index] = value;
		break;
	}
}


inline void mc68901_device::gpio_input(int bit, int state)
{
	if (state != BIT(m_gpio_input, bit))
	{
		if (state == BIT(m_aer, bit))
		{
			if (LOG) logerror("MC68901 '%s' Edge Transition Detected on GPIO%u\n", tag(), bit);

			if (m_ier & INT_MASK_GPIO[bit]) // AND interrupt enabled bit is set...
			{
				if (LOG) logerror("MC68901 '%s' Interrupt Pending for GPIO%u\n", tag(), bit);

				take_interrupt(INT_MASK_GPIO[bit]); // set interrupt pending bit
			}
		}


		if (state)
			m_gpio_input |= (1 << bit);
		else
			m_gpio_input &= ~(1 << bit);
	}
}


void mc68901_device::gpio_output()
{
	uint8_t new_gpio_output = m_gpip & m_ddr;

	if (m_gpio_output != new_gpio_output)
	{
		m_gpio_output = new_gpio_output;
		m_out_gpio_cb((offs_t)0, m_gpio_output);
	}
}

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

//-------------------------------------------------
//  mc68901_device - constructor
//-------------------------------------------------

mc68901_device::mc68901_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, MC68901, "MC68901 MFP", tag, owner, clock, "mc68901", __FILE__),
		device_serial_interface(mconfig, *this),
		m_timer_clock(0),
		m_rx_clock(0),
		m_tx_clock(0),
		m_out_irq_cb(*this),
		m_out_gpio_cb(*this),
		m_out_tao_cb(*this),
		m_out_tbo_cb(*this),
		m_out_tco_cb(*this),
		m_out_tdo_cb(*this),
		m_out_so_cb(*this),
		//m_out_rr_cb(*this),
		//m_out_tr_cb(*this),
		m_aer(0),
		m_ier(0),
		m_gpio_input(0),
		m_gpio_output(0xff)
{
}


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

void mc68901_device::device_start()
{
	m_start_bit_hack_for_external_clocks = true;

	/* resolve callbacks */
	m_out_irq_cb.resolve_safe();
	m_out_gpio_cb.resolve_safe();
	m_out_tao_cb.resolve_safe();
	m_out_tbo_cb.resolve_safe();
	m_out_tco_cb.resolve_safe();
	m_out_tdo_cb.resolve_safe();
	m_out_so_cb.resolve_safe();
	//m_out_rr_cb.resolve_safe();
	//m_out_tr_cb.resolve_safe();

	/* create the timers */
	m_timer[TIMER_A] = timer_alloc(TIMER_A);
	m_timer[TIMER_B] = timer_alloc(TIMER_B);
	m_timer[TIMER_C] = timer_alloc(TIMER_C);
	m_timer[TIMER_D] = timer_alloc(TIMER_D);

	if (m_rx_clock > 0)
	{
		set_rcv_rate(m_rx_clock);
	}

	if (m_tx_clock > 0)
	{
		set_tra_rate(m_tx_clock);
	}

	/* register for state saving */
	save_item(NAME(m_gpip));
	save_item(NAME(m_aer));
	save_item(NAME(m_ddr));
	save_item(NAME(m_ier));
	save_item(NAME(m_ipr));
	save_item(NAME(m_isr));
	save_item(NAME(m_imr));
	save_item(NAME(m_vr));
	save_item(NAME(m_tacr));
	save_item(NAME(m_tbcr));
	save_item(NAME(m_tcdcr));
	save_item(NAME(m_tdr));
	save_item(NAME(m_tmc));
	save_item(NAME(m_to));
	save_item(NAME(m_ti));
	save_item(NAME(m_scr));
	save_item(NAME(m_ucr));
	save_item(NAME(m_rsr));
	save_item(NAME(m_tsr));
	save_item(NAME(m_transmit_buffer));
	save_item(NAME(m_transmit_pending));
	save_item(NAME(m_receive_buffer));
	save_item(NAME(m_receive_pending));
	save_item(NAME(m_gpio_input));
	save_item(NAME(m_gpio_output));
	save_item(NAME(m_rsr_read));
	save_item(NAME(m_next_rsr));
}


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

void mc68901_device::device_reset()
{
	m_tsr = 0;
	m_transmit_pending = 0;

	// Avoid read-before-write
	m_ipr = m_imr = 0;

	m_next_rsr = 0;

	memset(m_tmc, 0, sizeof(m_tmc));
	memset(m_ti, 0, sizeof(m_ti));
	memset(m_to, 0, sizeof(m_to));

	register_w(REGISTER_GPIP, 0);
	register_w(REGISTER_AER, 0);
	register_w(REGISTER_DDR, 0);
	register_w(REGISTER_IERA, 0);
	register_w(REGISTER_IERB, 0);
	register_w(REGISTER_IPRA, 0);
	register_w(REGISTER_IPRB, 0);
	register_w(REGISTER_ISRA, 0);
	register_w(REGISTER_ISRB, 0);
	register_w(REGISTER_IMRA, 0);
	register_w(REGISTER_IMRB, 0);
	register_w(REGISTER_VR, 0);
	register_w(REGISTER_TACR, 0);
	register_w(REGISTER_TBCR, 0);
	register_w(REGISTER_TCDCR, 0);
	register_w(REGISTER_SCR, 0);
	register_w(REGISTER_UCR, 0);
	register_w(REGISTER_RSR, 0);

	transmit_register_reset();
	receive_register_reset();
}


//-------------------------------------------------
//  device_timer - handler timer events
//-------------------------------------------------

void mc68901_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	if(id >= TIMER_A && id <= TIMER_D)
		timer_count(id);
	else
		device_serial_interface::device_timer(timer, id, param, ptr);
}


//-------------------------------------------------
//  tra_callback -
//-------------------------------------------------

void mc68901_device::tra_callback()
{
	m_out_so_cb(transmit_register_get_data_bit());
}


//-------------------------------------------------
//  tra_complete -
//-------------------------------------------------

void mc68901_device::tra_complete()
{
	if (m_tsr & TSR_XMIT_ENABLE)
	{
		if (m_transmit_pending)
		{
			transmit_register_setup(m_transmit_buffer);
			m_transmit_pending = 0;
			m_tsr |= TSR_BUFFER_EMPTY;

			if (m_ier & IR_XMIT_BUFFER_EMPTY)
			{
				take_interrupt(IR_XMIT_BUFFER_EMPTY);
			}
		}
		else
		{
			m_tsr |= TSR_UNDERRUN_ERROR;
			// TODO: transmit error?
		}
	}
	else
	{
		m_tsr |= TSR_END_OF_XMIT;
	}
}


//-------------------------------------------------
//  rcv_complete -
//-------------------------------------------------

void mc68901_device::rcv_complete()
{
	receive_register_extract();
	m_receive_buffer = get_received_char();
	//if (m_receive_pending) TODO: error?

	m_receive_pending = 1;
	rx_buffer_full();
}


//-------------------------------------------------
//  read -
//-------------------------------------------------

READ8_MEMBER( mc68901_device::read )
{
	switch (offset)
	{
	case REGISTER_GPIP:  return (m_gpio_input & ~m_ddr) | (m_gpip & m_ddr);

	case REGISTER_AER:   return m_aer;
	case REGISTER_DDR:   return m_ddr;

	case REGISTER_IERA:  return m_ier >> 8;
	case REGISTER_IERB:  return m_ier & 0xff;
	case REGISTER_IPRA:  return m_ipr >> 8;
	case REGISTER_IPRB:  return m_ipr & 0xff;
	case REGISTER_ISRA:  return m_isr >> 8;
	case REGISTER_ISRB:  return m_isr & 0xff;
	case REGISTER_IMRA:  return m_imr >> 8;
	case REGISTER_IMRB:  return m_imr & 0xff;
	case REGISTER_VR:    return m_vr;

	case REGISTER_TACR:  return m_tacr;
	case REGISTER_TBCR:  return m_tbcr;
	case REGISTER_TCDCR: return m_tcdcr;
	case REGISTER_TADR:  return m_tmc[TIMER_A];
	case REGISTER_TBDR:  return m_tmc[TIMER_B];
	case REGISTER_TCDR:  return m_tmc[TIMER_C];
	case REGISTER_TDDR:  return m_tmc[TIMER_D];

	case REGISTER_SCR:   return m_scr;
	case REGISTER_UCR:   return m_ucr;
	case REGISTER_RSR:   return m_rsr;

	case REGISTER_TSR:
		{
			/* clear UE bit (in reality, this won't be cleared until one full clock cycle of the transmitter has passed since the bit was set) */
			uint8_t tsr = m_tsr;
			m_tsr &= ~TSR_UNDERRUN_ERROR;

			return tsr;
		}

	case REGISTER_UDR:
		m_receive_pending = 0;
		return m_receive_buffer;

	default:                      return 0;
	}
}



//-------------------------------------------------
//  register_w -
//-------------------------------------------------

void mc68901_device::register_w(offs_t offset, uint8_t data)
{
	switch (offset)
	{
	case REGISTER_GPIP:
		if (LOG) logerror("MC68901 '%s' General Purpose I/O : %x\n", tag(), data);
		m_gpip = data;
		gpio_output();
		break;

	case REGISTER_AER:
		if (LOG) logerror("MC68901 '%s' Active Edge Register : %x\n", tag(), data);
		m_aer = data;
		break;

	case REGISTER_DDR:
		if (LOG) logerror("MC68901 '%s' Data Direction Register : %x\n", tag(), data);
		m_ddr = data;
		gpio_output();
		break;

	case REGISTER_IERA:
		if (LOG) logerror("MC68901 '%s' Interrupt Enable Register A : %x\n", tag(), data);
		m_ier = (data << 8) | (m_ier & 0xff);
		m_ipr &= m_ier;
		check_interrupts();
		break;

	case REGISTER_IERB:
		if (LOG) logerror("MC68901 '%s' Interrupt Enable Register B : %x\n", tag(), data);
		m_ier = (m_ier & 0xff00) | data;
		m_ipr &= m_ier;
		check_interrupts();
		break;

	case REGISTER_IPRA:
		if (LOG) logerror("MC68901 '%s' Interrupt Pending Register A : %x\n", tag(), data);
		m_ipr &= (data << 8) | (m_ipr & 0xff);
		check_interrupts();
		break;

	case REGISTER_IPRB:
		if (LOG) logerror("MC68901 '%s' Interrupt Pending Register B : %x\n", tag(), data);
		m_ipr &= (m_ipr & 0xff00) | data;
		check_interrupts();
		break;

	case REGISTER_ISRA:
		if (LOG) logerror("MC68901 '%s' Interrupt In-Service Register A : %x\n", tag(), data);
		m_isr &= (data << 8) | (m_isr & 0xff);
		break;

	case REGISTER_ISRB:
		if (LOG) logerror("MC68901 '%s' Interrupt In-Service Register B : %x\n", tag(), data);
		m_isr &= (m_isr & 0xff00) | data;
		break;

	case REGISTER_IMRA:
		if (LOG) logerror("MC68901 '%s' Interrupt Mask Register A : %x\n", tag(), data);
		m_imr = (data << 8) | (m_imr & 0xff);
		m_isr &= m_imr;
		check_interrupts();
		break;

	case REGISTER_IMRB:
		if (LOG) logerror("MC68901 '%s' Interrupt Mask Register B : %x\n", tag(), data);
		m_imr = (m_imr & 0xff00) | data;
		m_isr &= m_imr;
		check_interrupts();
		break;

	case REGISTER_VR:
		if (LOG) logerror("MC68901 '%s' Interrupt Vector : %x\n", tag(), data & 0xf0);

		m_vr = data & 0xf8;

		if (m_vr & VR_S)
		{
			if (LOG) logerror("MC68901 '%s' Software End-Of-Interrupt Mode\n", tag());
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Automatic End-Of-Interrupt Mode\n", tag());

			m_isr = 0;
		}
		break;

	case REGISTER_TACR:
		m_tacr = data & 0x1f;

		switch (m_tacr & 0x0f)
		{
		case TCR_TIMER_STOPPED:
			if (LOG) logerror("MC68901 '%s' Timer A Stopped\n", tag());
			m_timer[TIMER_A]->enable(false);
			break;

		case TCR_TIMER_DELAY_4:
		case TCR_TIMER_DELAY_10:
		case TCR_TIMER_DELAY_16:
		case TCR_TIMER_DELAY_50:
		case TCR_TIMER_DELAY_64:
		case TCR_TIMER_DELAY_100:
		case TCR_TIMER_DELAY_200:
			{
			int divisor = PRESCALER[m_tacr & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer A Delay Mode : %u Prescale\n", tag(), divisor);
			m_timer[TIMER_A]->adjust(attotime::from_hz(m_timer_clock / divisor), 0, attotime::from_hz(m_timer_clock / divisor));
			}
			break;

		case TCR_TIMER_EVENT:
			if (LOG) logerror("MC68901 '%s' Timer A Event Count Mode\n", tag());
			m_timer[TIMER_A]->enable(false);
			break;

		case TCR_TIMER_PULSE_4:
		case TCR_TIMER_PULSE_10:
		case TCR_TIMER_PULSE_16:
		case TCR_TIMER_PULSE_50:
		case TCR_TIMER_PULSE_64:
		case TCR_TIMER_PULSE_100:
		case TCR_TIMER_PULSE_200:
			{
			int divisor = PRESCALER[m_tacr & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer A Pulse Width Mode : %u Prescale\n", tag(), divisor);
			m_timer[TIMER_A]->adjust(attotime::never, 0, attotime::from_hz(m_timer_clock / divisor));
			m_timer[TIMER_A]->enable(false);
			}
			break;
		}

		if (m_tacr & TCR_TIMER_RESET)
		{
			if (LOG) logerror("MC68901 '%s' Timer A Reset\n", tag());

			m_to[TIMER_A] = 0;

			m_out_tao_cb(m_to[TIMER_A]);
		}
		break;

	case REGISTER_TBCR:
		m_tbcr = data & 0x1f;

		switch (m_tbcr & 0x0f)
		{
		case TCR_TIMER_STOPPED:
			if (LOG) logerror("MC68901 '%s' Timer B Stopped\n", tag());
			m_timer[TIMER_B]->enable(false);
			break;

		case TCR_TIMER_DELAY_4:
		case TCR_TIMER_DELAY_10:
		case TCR_TIMER_DELAY_16:
		case TCR_TIMER_DELAY_50:
		case TCR_TIMER_DELAY_64:
		case TCR_TIMER_DELAY_100:
		case TCR_TIMER_DELAY_200:
			{
			int divisor = PRESCALER[m_tbcr & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer B Delay Mode : %u Prescale\n", tag(), divisor);
			m_timer[TIMER_B]->adjust(attotime::from_hz(m_timer_clock / divisor), 0, attotime::from_hz(m_timer_clock / divisor));
			}
			break;

		case TCR_TIMER_EVENT:
			if (LOG) logerror("MC68901 '%s' Timer B Event Count Mode\n", tag());
			m_timer[TIMER_B]->enable(false);
			break;

		case TCR_TIMER_PULSE_4:
		case TCR_TIMER_PULSE_10:
		case TCR_TIMER_PULSE_16:
		case TCR_TIMER_PULSE_50:
		case TCR_TIMER_PULSE_64:
		case TCR_TIMER_PULSE_100:
		case TCR_TIMER_PULSE_200:
			{
			int divisor = PRESCALER[m_tbcr & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer B Pulse Width Mode : %u Prescale\n", tag(), DIVISOR);
			m_timer[TIMER_B]->adjust(attotime::never, 0, attotime::from_hz(m_timer_clock / divisor));
			m_timer[TIMER_B]->enable(false);
			}
			break;
		}

		if (m_tacr & TCR_TIMER_RESET)
		{
			if (LOG) logerror("MC68901 '%s' Timer B Reset\n", tag());

			m_to[TIMER_B] = 0;

			m_out_tbo_cb(m_to[TIMER_B]);
		}
		break;

	case REGISTER_TCDCR:
		m_tcdcr = data & 0x6f;

		switch (m_tcdcr & 0x07)
		{
		case TCR_TIMER_STOPPED:
			if (LOG) logerror("MC68901 '%s' Timer D Stopped\n", tag());
			m_timer[TIMER_D]->enable(false);
			break;

		case TCR_TIMER_DELAY_4:
		case TCR_TIMER_DELAY_10:
		case TCR_TIMER_DELAY_16:
		case TCR_TIMER_DELAY_50:
		case TCR_TIMER_DELAY_64:
		case TCR_TIMER_DELAY_100:
		case TCR_TIMER_DELAY_200:
			{
			int divisor = PRESCALER[m_tcdcr & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer D Delay Mode : %u Prescale\n", tag(), divisor);
			m_timer[TIMER_D]->adjust(attotime::from_hz(m_timer_clock / divisor), 0, attotime::from_hz(m_timer_clock / divisor));
			}
			break;
		}

		switch ((m_tcdcr >> 4) & 0x07)
		{
		case TCR_TIMER_STOPPED:
			if (LOG) logerror("MC68901 '%s' Timer C Stopped\n", tag());
			m_timer[TIMER_C]->enable(false);
			break;

		case TCR_TIMER_DELAY_4:
		case TCR_TIMER_DELAY_10:
		case TCR_TIMER_DELAY_16:
		case TCR_TIMER_DELAY_50:
		case TCR_TIMER_DELAY_64:
		case TCR_TIMER_DELAY_100:
		case TCR_TIMER_DELAY_200:
			{
			int divisor = PRESCALER[(m_tcdcr >> 4) & 0x07];
			if (LOG) logerror("MC68901 '%s' Timer C Delay Mode : %u Prescale\n", tag(), divisor);
			m_timer[TIMER_C]->adjust(attotime::from_hz(m_timer_clock / divisor), 0, attotime::from_hz(m_timer_clock / divisor));
			}
			break;
		}
		break;

	case REGISTER_TADR:
		if (LOG) logerror("MC68901 '%s' Timer A Data Register : %x\n", tag(), data);

		m_tdr[TIMER_A] = data;

		if (!m_timer[TIMER_A]->enabled())
		{
			m_tmc[TIMER_A] = data;
		}
		break;

	case REGISTER_TBDR:
		if (LOG) logerror("MC68901 '%s' Timer B Data Register : %x\n", tag(), data);

		m_tdr[TIMER_B] = data;

		if (!m_timer[TIMER_B]->enabled())
		{
			m_tmc[TIMER_B] = data;
		}
		break;

	case REGISTER_TCDR:
		if (LOG) logerror("MC68901 '%s' Timer C Data Register : %x\n", tag(), data);

		m_tdr[TIMER_C] = data;

		if (!m_timer[TIMER_C]->enabled())
		{
			m_tmc[TIMER_C] = data;
		}
		break;

	case REGISTER_TDDR:
		if (LOG) logerror("MC68901 '%s' Timer D Data Register : %x\n", tag(), data);

		m_tdr[TIMER_D] = data;

		if (!m_timer[TIMER_D]->enabled())
		{
			m_tmc[TIMER_D] = data;
		}
		break;

	case REGISTER_SCR:
		if (LOG) logerror("MC68901 '%s' Sync Character : %x\n", tag(), data);

		m_scr = data;
		break;

	case REGISTER_UCR:
		{
		int data_bit_count;

		switch (data & 0x60)
		{
		case UCR_WORD_LENGTH_8: default: data_bit_count = 8; break;
		case UCR_WORD_LENGTH_7: data_bit_count = 7; break;
		case UCR_WORD_LENGTH_6: data_bit_count = 6; break;
		case UCR_WORD_LENGTH_5: data_bit_count = 5; break;
		}

		parity_t parity;

		if (data & UCR_PARITY_ENABLED)
		{
			if (data & UCR_PARITY_EVEN)
			{
				if (LOG) logerror("MC68901 '%s' Parity : Even\n", tag());

				parity = PARITY_EVEN;
			}
			else
			{
				if (LOG) logerror("MC68901 '%s' Parity : Odd\n", tag());

				parity = PARITY_ODD;
			}
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Parity : Disabled\n", tag());

			parity = PARITY_NONE;
		}

		if (LOG) logerror("MC68901 '%s' Word Length : %u bits\n", tag(), data_bit_count);


		int start_bits;
		stop_bits_t stop_bits;

		switch (data & 0x18)
		{
		case UCR_START_STOP_0_0:
		default:
			start_bits = 0;
			stop_bits = STOP_BITS_0;
			if (LOG) logerror("MC68901 '%s' Start Bits : 0, Stop Bits : 0, Format : synchronous\n", tag());
			break;

		case UCR_START_STOP_1_1:
			start_bits = 1;
			stop_bits = STOP_BITS_1;
			if (LOG) logerror("MC68901 '%s' Start Bits : 1, Stop Bits : 1, Format : asynchronous\n", tag());
			break;

		case UCR_START_STOP_1_15:
			start_bits = 1;
			stop_bits = STOP_BITS_1_5;
			if (LOG) logerror("MC68901 '%s' Start Bits : 1, Stop Bits : 1.5, Format : asynchronous\n", tag());
			break;

		case UCR_START_STOP_1_2:
			start_bits = 1;
			stop_bits = STOP_BITS_2;
			if (LOG) logerror("MC68901 '%s' Start Bits : 1, Stop Bits : 2, Format : asynchronous\n", tag());
			break;
		}

		if (data & UCR_CLOCK_DIVIDE_16)
		{
			if (LOG) logerror("MC68901 '%s' Rx/Tx Clock Divisor : 16\n", tag());
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Rx/Tx Clock Divisor : 1\n", tag());
		}

		set_data_frame(start_bits, data_bit_count, parity, stop_bits);

		m_ucr = data;
		}
		break;

	case REGISTER_RSR:
		if ((data & RSR_RCV_ENABLE) == 0)
		{
			if (LOG) logerror("MC68901 '%s' Receiver Disabled\n", tag());
			m_rsr = 0;
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Receiver Enabled\n", tag());

			if (data & RSR_SYNC_STRIP_ENABLE)
			{
				if (LOG) logerror("MC68901 '%s' Sync Strip Enabled\n", tag());
			}
			else
			{
				if (LOG) logerror("MC68901 '%s' Sync Strip Disabled\n", tag());
			}

			if (data & RSR_FOUND_SEARCH)
				if (LOG) logerror("MC68901 '%s' Receiver Search Mode Enabled\n", tag());

			m_rsr = data & 0x0b;
		}
		break;

	case REGISTER_TSR:
		m_tsr = (m_tsr & (TSR_BUFFER_EMPTY | TSR_UNDERRUN_ERROR | TSR_END_OF_XMIT)) | (data & ~(TSR_BUFFER_EMPTY | TSR_UNDERRUN_ERROR | TSR_END_OF_XMIT));

		if ((data & TSR_XMIT_ENABLE) == 0)
		{
			if (LOG) logerror("MC68901 '%s' Transmitter Disabled\n", tag());

			m_tsr &= ~TSR_UNDERRUN_ERROR;

			if (is_transmit_register_empty())
				m_tsr |= TSR_END_OF_XMIT;
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Transmitter Enabled\n", tag());

			switch (data & 0x06)
			{
			case TSR_OUTPUT_HI_Z:
				if (LOG) logerror("MC68901 '%s' Transmitter Disabled Output State : Hi-Z\n", tag());
				break;
			case TSR_OUTPUT_LOW:
				if (LOG) logerror("MC68901 '%s' Transmitter Disabled Output State : 0\n", tag());
				break;
			case TSR_OUTPUT_HIGH:
				if (LOG) logerror("MC68901 '%s' Transmitter Disabled Output State : 1\n", tag());
				break;
			case TSR_OUTPUT_LOOP:
				if (LOG) logerror("MC68901 '%s' Transmitter Disabled Output State : Loop\n", tag());
				break;
			}

			if (data & TSR_BREAK)
			{
				if (LOG) logerror("MC68901 '%s' Transmitter Break Enabled\n", tag());
			}
			else
			{
				if (LOG) logerror("MC68901 '%s' Transmitter Break Disabled\n", tag());
			}

			if (data & TSR_AUTO_TURNAROUND)
			{
				if (LOG) logerror("MC68901 '%s' Transmitter Auto Turnaround Enabled\n", tag());
			}
			else
			{
				if (LOG) logerror("MC68901 '%s' Transmitter Auto Turnaround Disabled\n", tag());
			}

			m_tsr &= ~TSR_END_OF_XMIT;

			if (m_transmit_pending && is_transmit_register_empty())
			{
				transmit_register_setup(m_transmit_buffer);
				m_transmit_pending = 0;
				m_tsr |= TSR_BUFFER_EMPTY;
			}
		}
		break;

	case REGISTER_UDR:
		if (LOG) logerror("MC68901 '%s' UDR %x\n", tag(), data);
		m_transmit_buffer = data;
		m_transmit_pending = 1;
		m_tsr &= ~TSR_BUFFER_EMPTY;

		if ((m_tsr & TSR_XMIT_ENABLE) && is_transmit_register_empty())
		{
			transmit_register_setup(m_transmit_buffer);
			m_transmit_pending = 0;
			m_tsr |= TSR_BUFFER_EMPTY;
		}
		break;
	}
}

WRITE8_MEMBER( mc68901_device::write )
{
	register_w(offset, data);
}


int mc68901_device::get_vector()
{
	int ch;

	for (ch = 15; ch >= 0; ch--)
	{
		if (BIT(m_imr, ch) && BIT(m_ipr, ch))
		{
			if (m_vr & VR_S)
			{
				/* set interrupt-in-service bit */
				m_isr |= (1 << ch);
			}

			/* clear interrupt pending bit */
			m_ipr &= ~(1 << ch);

			check_interrupts();

			return (m_vr & 0xf0) | ch;
		}
	}

	return M68K_INT_ACK_SPURIOUS;
}

WRITE_LINE_MEMBER( mc68901_device::i0_w ) { gpio_input(0, state); }
WRITE_LINE_MEMBER( mc68901_device::i1_w ) { gpio_input(1, state); }
WRITE_LINE_MEMBER( mc68901_device::i2_w ) { gpio_input(2, state); }
WRITE_LINE_MEMBER( mc68901_device::i3_w ) { gpio_input(3, state); }
WRITE_LINE_MEMBER( mc68901_device::i4_w ) { gpio_input(4, state); }
WRITE_LINE_MEMBER( mc68901_device::i5_w ) { gpio_input(5, state); }
WRITE_LINE_MEMBER( mc68901_device::i6_w ) { gpio_input(6, state); }
WRITE_LINE_MEMBER( mc68901_device::i7_w ) { gpio_input(7, state); }


WRITE_LINE_MEMBER( mc68901_device::tai_w )
{
	timer_input(TIMER_A, state);
}


WRITE_LINE_MEMBER( mc68901_device::tbi_w )
{
	timer_input(TIMER_B, state);
}

WRITE_LINE_MEMBER(mc68901_device::write_rx)
{
	device_serial_interface::rx_w(state);
}