summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/mc68901.c
blob: a43b2db82c0f4f0642a9e6ab8953f415d08e7614 (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
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
/**********************************************************************

    Motorola MC68901 Multi Function Peripheral emulation

    Copyright MESS Team.
    Visit http://mamedev.org for licensing and usage restrictions.

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

/*

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

    - 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"
#include "machine/devhelpr.h"



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

#define LOG 0


enum
{
	REGISTER_GPIP = 0,
	REGISTER_AER,
	REGISTER_DDR,
	REGISTER_IERA,
	REGISTER_IERB,
	REGISTER_IPRA,
	REGISTER_IPRB,
	REGISTER_ISRA,
	REGISTER_ISRB,
	REGISTER_IMRA,
	REGISTER_IMRB,
	REGISTER_VR,
	REGISTER_TACR,
	REGISTER_TBCR,
	REGISTER_TCDCR,
	REGISTER_TADR,
	REGISTER_TBDR,
	REGISTER_TCDR,
	REGISTER_TDDR,
	REGISTER_SCR,
	REGISTER_UCR,
	REGISTER_RSR,
	REGISTER_TSR,
	REGISTER_UDR
};


enum
{
	INT_GPI0 = 0,
	INT_GPI1,
	INT_GPI2,
	INT_GPI3,
	INT_TIMER_D,
	INT_TIMER_C,
	INT_GPI4,
	INT_GPI5,
	INT_TIMER_B,
	INT_XMIT_ERROR,
	INT_XMIT_BUFFER_EMPTY,
	INT_RCV_ERROR,
	INT_RCV_BUFFER_FULL,
	INT_TIMER_A,
	INT_GPI6,
	INT_GPI7
};


enum
{
	GPIP_0 = 0,
	GPIP_1,
	GPIP_2,
	GPIP_3,
	GPIP_4,
	GPIP_5,
	GPIP_6,
	GPIP_7
};


enum
{
	TIMER_A = 0,
	TIMER_B,
	TIMER_C,
	TIMER_D,
	MAX_TIMERS
};


enum
{
	SERIAL_START = 0,
	SERIAL_DATA,
	SERIAL_PARITY,
	SERIAL_STOP
};


enum
{
	XMIT_OFF = 0,
	XMIT_STARTING,
	XMIT_ON,
	XMIT_BREAK,
	XMIT_STOPPING
};


#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


static const int 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
};


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


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


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


#define TXD(_data) devcb_call_write_line(&m_out_so_func, _data);


//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

// devices
const device_type MC68901 = mc68901_device_config::static_alloc_device_config;



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

GENERIC_DEVICE_CONFIG_SETUP(mc68901, "Motorola MC68901")


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

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

	// or initialize to defaults if none provided
	else
	{
//		memset(&in_pa_func, 0, sizeof(in_pa_func));
	}
}



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

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

inline void mc68901_device::take_interrupt(UINT16 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::tx_buffer_empty()
{
	if (m_ier & IR_XMIT_BUFFER_EMPTY)
	{
		take_interrupt(IR_XMIT_BUFFER_EMPTY);
	}
}

inline void mc68901_device::tx_error()
{
	if (m_ier & IR_XMIT_ERROR)
	{
		take_interrupt(IR_XMIT_ERROR);
	}
	else
	{
		tx_buffer_empty();
	}
}

inline int mc68901_device::get_parity_bit(UINT8 b)
{
	b ^= b >> 4;
	b ^= b >> 2;
	b ^= b >> 1;
	return b & 1;
}

inline void mc68901_device::serial_receive()
{
	int rxd;

	if (!(m_rsr & RSR_RCV_ENABLE)) return;

	rxd = devcb_call_read_line(&m_in_si_func);

	switch (m_rx_state)
	{
	case SERIAL_START:
		if (!rxd)
		{
			m_rsr |= RSR_CHAR_IN_PROGRESS;
			m_rx_bits = 0;
			m_rx_buffer = 0;
			m_rx_state = SERIAL_DATA;
			m_next_rsr = RSR_BREAK;
		}
		break;

	case SERIAL_DATA:
		if ((m_next_rsr & RSR_BREAK) && (rxd == 1) && m_rsr_read)
		{
			m_next_rsr &= ~RSR_BREAK;
		}

		m_rx_buffer >>= 1;
		m_rx_buffer |= (rxd << 7);
		m_rx_bits++;

		if (m_rx_bits == m_rxtx_word)
		{
			if (m_rxtx_word < 8)
			{
				m_rx_buffer >>= (8 - m_rxtx_word);
			}

			m_rsr &= ~RSR_CHAR_IN_PROGRESS;

			if (m_ucr & UCR_PARITY_ENABLED)
			{
				m_rx_state = SERIAL_PARITY;
			}
			else
			{
				m_rx_state = SERIAL_STOP;
			}
		}
		break;

	case SERIAL_PARITY:
		m_rx_parity = rxd;

		if (m_rx_parity != (get_parity_bit(m_rx_buffer) ^ ((m_ucr & UCR_PARITY_EVEN) >> 1)))
		{
			m_next_rsr |= RSR_PARITY_ERROR;
		}

		m_rx_state = SERIAL_STOP;
		break;

	case SERIAL_STOP:
		if (rxd == 1)
		{
			if (!((m_rsr & RSR_SYNC_STRIP_ENABLE) && (m_rx_buffer == m_scr)))
			{
				if (!(m_rsr & RSR_OVERRUN_ERROR))
				{
					if (m_rsr & RSR_BUFFER_FULL)
					{
						// incoming word received but last word in receive buffer has not been read
						m_next_rsr |= RSR_OVERRUN_ERROR;
					}
					else
					{
						// incoming word received and receive buffer is empty
						m_rsr |= RSR_BUFFER_FULL;
						m_udr = m_rx_buffer;
						rx_buffer_full();
					}
				}
			}
		}
		else
		{
			if (m_rx_buffer)
			{
				// non-zero data word not followed by a stop bit
				m_next_rsr |= RSR_FRAME_ERROR;
			}
		}

		m_rx_state = SERIAL_START;
		break;
	}
}


inline void mc68901_device::tx_disabled()
{
	switch (m_tsr & TSR_OUTPUT_MASK)
	{
	case TSR_OUTPUT_HI_Z:
		/* indeterminate state */
	case TSR_OUTPUT_LOW:
		TXD(0);
		break;

	case TSR_OUTPUT_HIGH:
	case TSR_OUTPUT_LOOP:
		TXD(1);
		break;
	}
}


inline void mc68901_device::tx_starting()
{
	if (m_tsr & TSR_XMIT_ENABLE)
	{
		/* enable transmitter */
		TXD(1);
		m_xmit_state = XMIT_ON;
	}
	else
	{
		/* disable transmitter */
		m_tsr |= TSR_END_OF_XMIT;
		m_xmit_state = XMIT_OFF;
	}
}


inline void mc68901_device::tx_break()
{
	if (m_tsr & TSR_XMIT_ENABLE)
	{
		if (m_tsr & TSR_BREAK)
		{
			/* transmit break */
			TXD(1);
		}
		else
		{
			/* enable transmitter */
			m_xmit_state = XMIT_ON;
		}
	}
	else
	{
		/* disable transmitter */
		m_tsr |= TSR_END_OF_XMIT;
		m_xmit_state = XMIT_OFF;
	}
}


inline void mc68901_device::tx_enabled()
{
	switch (m_tx_state)
	{
	case SERIAL_START:
		if (m_tsr & TSR_UNDERRUN_ERROR)
		{
			/* buffer underrun condition */
			if (m_tsr & TSR_XMIT_ENABLE)
			{
				/* transmit break */
				TXD(1);
			}
			else
			{
				/* transmitter disabled */
				tx_disabled();
			}
		}
		else
		{
			if (m_tsr & TSR_BUFFER_EMPTY)
			{
				/* transmit buffer is empty, signal underrun error */
				m_tsr |= TSR_UNDERRUN_ERROR;

				if (m_tsr & TSR_XMIT_ENABLE)
				{
					/* transmit break */
					TXD(1);
				}
				else
				{
					/* transmitter disabled */
					tx_disabled();
				}
			}
			else
			{
				/* transmit start bit */
				TXD(0);

				/* load transmit buffer */
				m_tx_buffer = m_udr;
				m_tx_bits = 0;

				/* signal transmit buffer empty */
				m_tsr |= TSR_BUFFER_EMPTY;
				tx_buffer_empty();

				/* calculate parity */
				m_tx_parity = get_parity_bit(m_tx_buffer);

				/* next bit is data */
				m_tx_state = SERIAL_DATA;
			}
		}
		break;

	case SERIAL_DATA:
		/* transmit data bit */
		TXD(m_tx_buffer & 0x01);

		/* shift transmit buffer */
		m_tx_buffer >>= 1;
		m_tx_bits++;

		if (m_tx_bits == m_rxtx_word)
		{
			/* all data bits transferred */
			if (m_ucr & UCR_PARITY_ENABLED)
			{
				/* next bit is parity */
				m_tx_state = SERIAL_PARITY;
			}
			else
			{
				/* next bit is stop */
				m_tx_state = SERIAL_STOP;
			}
		}
		break;

	case SERIAL_PARITY:
		if (m_rxtx_word < 8)
		{
			/* transmit user-defined parity bit from buffer */
			TXD(m_tx_buffer & 0x01);
		}
		else
		{
			/* transmit calculated parity bit */
			TXD(m_tx_parity ^ ((m_ucr & UCR_PARITY_EVEN) >> 1));
		}

		/* next bit is stop */
		m_tx_state = SERIAL_STOP;
		break;

	case SERIAL_STOP:
		/* transmit stop bit */
		TXD(1);

		if (m_tsr & TSR_XMIT_ENABLE)
		{
			/* next bit is start */
			m_tx_state = SERIAL_START;
		}
		else
		{
			if (m_tsr & TSR_AUTO_TURNAROUND)
			{
				/* enable transmitter */
				m_tsr |= TSR_XMIT_ENABLE;

				/* next bit is start */
				m_tx_state = SERIAL_START;
			}
			else
			{
				/* disable transmitter */
				m_xmit_state = XMIT_OFF;
				m_tsr |= TSR_END_OF_XMIT;

				/* signal transmit error */
				tx_error();
			}
		}
		break;
	}
}

inline void mc68901_device::serial_transmit()
{
	switch (m_xmit_state)
	{
	case XMIT_OFF:		tx_disabled();	break;
	case XMIT_STARTING:	tx_starting();	break;
	case XMIT_BREAK:	tx_break();		break;
	case XMIT_ON:		tx_enabled();	break;
	}
}


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:	devcb_call_write_line(&m_out_tao_func, m_to[index]);	break;
		case TIMER_B:	devcb_call_write_line(&m_out_tbo_func, m_to[index]);	break;
		case TIMER_C:	devcb_call_write_line(&m_out_tco_func, m_to[index]);	break;
		case TIMER_D:	devcb_call_write_line(&m_out_tdo_func, 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 (BIT(m_gpip, bit) && !state) // if transition from 1 to 0 is detected...
	{
		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
		}
	}

	m_gpip &= ((1 << bit) ^ 0xff);
	m_gpip |= (state << bit);
}



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

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

mc68901_device::mc68901_device(running_machine &_machine, const mc68901_device_config &config)
    : device_t(_machine, config),
	  m_gpip(0),
	  m_tsr(TSR_BUFFER_EMPTY),
      m_config(config)
{
}


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

void mc68901_device::device_start()
{
	/* resolve callbacks */
	devcb_resolve_read8(&m_in_gpio_func, &m_config.in_gpio_func, this);
	devcb_resolve_write8(&m_out_gpio_func, &m_config.out_gpio_func, this);
	devcb_resolve_read_line(&m_in_si_func, &m_config.in_si_func, this);
	devcb_resolve_write_line(&m_out_so_func, &m_config.out_so_func, this);
	devcb_resolve_write_line(&m_out_tao_func, &m_config.out_tao_func, this);
	devcb_resolve_write_line(&m_out_tbo_func, &m_config.out_tbo_func, this);
	devcb_resolve_write_line(&m_out_tco_func, &m_config.out_tco_func, this);
	devcb_resolve_write_line(&m_out_tdo_func, &m_config.out_tdo_func, this);
	devcb_resolve_write_line(&m_out_irq_func, &m_config.out_irq_func, this);

	/* set initial values */
	m_timer_clock = m_config.timer_clock;

	/* 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_config.rx_clock > 0)
	{
		m_rx_timer = timer_alloc(TIMER_RX);
		m_rx_timer->adjust(attotime::zero, 0, attotime::from_hz(m_config.rx_clock));
	}

	if (m_config.tx_clock > 0)
	{
		m_tx_timer = timer_alloc(TIMER_TX);
		m_tx_timer->adjust(attotime::zero, 0, attotime::from_hz(m_config.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_udr));
	save_item(NAME(m_rx_bits));
	save_item(NAME(m_tx_bits));
	save_item(NAME(m_rx_parity));
	save_item(NAME(m_tx_parity));
	save_item(NAME(m_rx_state));
	save_item(NAME(m_tx_state));
	save_item(NAME(m_rx_buffer));
	save_item(NAME(m_tx_buffer));
	save_item(NAME(m_xmit_state));
	save_item(NAME(m_rxtx_word));
	save_item(NAME(m_rxtx_start));
	save_item(NAME(m_rxtx_stop));
	save_item(NAME(m_rsr_read));
	save_item(NAME(m_next_rsr));
}


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

void mc68901_device::device_reset()
{
	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);
}


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

void mc68901_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (id)
	{
	case TIMER_RX:
		serial_receive();
		break;

	case TIMER_TX:
		serial_transmit();
		break;

	default:
		timer_count(id);
		break;
	}
}



READ8_MEMBER( mc68901_device::read )
{
	switch (offset)
	{
	case REGISTER_GPIP:  
		m_gpip = devcb_call_read8(&m_in_gpio_func, 0);
		return m_gpip;

	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:
		m_rsr_read = 1;
		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 tsr = m_tsr;
			m_tsr &= 0xbf;

			return tsr;
		}

	case REGISTER_UDR:
		/* load RSR with latched value */
		m_rsr = (m_next_rsr & 0x7c) | (m_rsr & 0x03);
		m_next_rsr = 0;

		if (m_rsr & 0x78)
		{
			/* signal receiver error interrupt */
			rx_error();
		}

		return m_udr;

	default:					  return 0;
	}
}


#define DIVISOR PRESCALER[data & 0x07]

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

		devcb_call_write8(&m_out_gpio_func, 0, m_gpip);
		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;
		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;

			devcb_call_write_line(&m_out_tao_func, 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;

			devcb_call_write_line(&m_out_tbo_func, 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:
		if (data & UCR_PARITY_ENABLED)
		{
			if (data & UCR_PARITY_EVEN)
			{
				if (LOG) logerror("MC68901 '%s' Parity : Even\n", tag());
			}
			else
			{
				if (LOG) logerror("MC68901 '%s' Parity : Odd\n", tag());
			}
		}
		else
		{
			if (LOG) logerror("MC68901 '%s' Parity : Disabled\n", tag());
		}

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

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

		switch (data & 0x18)
		{
		case UCR_START_STOP_0_0:
			m_rxtx_start = 0;
			m_rxtx_stop = 0;
			if (LOG) logerror("MC68901 '%s' Start Bits : 0, Stop Bits : 0, Format : synchronous\n", tag());
			break;
		case UCR_START_STOP_1_1:
			m_rxtx_start = 1;
			m_rxtx_stop = 1;
			if (LOG) logerror("MC68901 '%s' Start Bits : 1, Stop Bits : 1, Format : asynchronous\n", tag());
			break;
		case UCR_START_STOP_1_15:
			m_rxtx_start = 1;
			m_rxtx_stop = 1;
			if (LOG) logerror("MC68901 '%s' Start Bits : 1, Stop Bits : 1??, Format : asynchronous\n", tag());
			break;
		case UCR_START_STOP_1_2:
			m_rxtx_start = 1;
			m_rxtx_stop = 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());
		}

		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:
		if ((data & TSR_XMIT_ENABLE) == 0)
		{
			if (LOG) logerror("MC68901 '%s' Transmitter Disabled\n", tag());

			m_tsr = data & 0x27;
		}
		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 = data & 0x2f;
			m_tsr |= TSR_BUFFER_EMPTY;  // x68000 expects the buffer to be empty, so this will do for now
		}
		break;

	case REGISTER_UDR:
		if (LOG) logerror("MC68901 '%s' UDR %x\n", tag(), data);
		m_udr = data;
		//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::rc_w )
{
	if (state)
	{
		serial_receive();
	}
}


WRITE_LINE_MEMBER( mc68901_device::tc_w )
{
	if (state)
	{
		serial_transmit();
	}
}