summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/h83002/h8speriph.c
blob: 916fa0b6d7138eb0294060b518f34e7d027f8d35 (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
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
/*

    Hitachi H8S/2xxx MCU peripherals

    (c) 2001-2007 Tim Schuerewegen

    H8S/2241
    H8S/2246
    H8S/2323
    H8S/2394
*/

#include "emu.h"
#include "debugger.h"
#include "h8.h"
#include "h8priv.h"

#define VERBOSE_LEVEL ( 0 )

INLINE void ATTR_PRINTF(3,4) verboselog( running_machine &machine, int n_level, const char *s_fmt, ...)
{
	if (VERBOSE_LEVEL >= n_level)
	{
		va_list v;
		char buf[32768];
		va_start( v, s_fmt);
		vsprintf( buf, s_fmt, v);
		va_end( v);
		logerror( "%s: %s", machine.describe_context( ), buf);
	}
}

const UINT8 H8S_RESET_H8S_IO_224x[0x1C0] = // values from "e602100_h8s2245.pdf"
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE40
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE50
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE60
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE70
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE80
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE90
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEA0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // FEB0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEC0
	0xFF, 0xFF, 0xFF, 0xFF, 0xD0, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FED0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEE0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEF0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF00
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF10
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF20
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, // FF30
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF40
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF50
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF60
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF70
	0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF80
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF90
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFA0
	0x00, 0x00, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFB0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFC0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FFD0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFE0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFF0
};

const UINT8 H8S_RESET_H8S_IO_2323[0x1C0] = // values from "rej09b0220_h8s2329.pdf"
{
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE40
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE50
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE60
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FE70
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FE80
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FE90
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FEA0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, // FEB0
	0x00, 0x00, 0x00, 0x00, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x00, // FEC0
	0xFF, 0xFF, 0xFF, 0xFF, 0xD0, 0x3C, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FED0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEE0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FEF0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF00
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF10
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF20
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x3F, 0xFF, 0x00, 0x00, // FF30
	0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF40
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF50
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF60
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF70
	0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, 0x00, 0xFF, 0x00, 0xFF, 0x84, 0x00, 0xF2, 0x00, // FF80
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FF90
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, // FFA0
	0x00, 0x00, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFB0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // FFC0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, // FFD0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFE0
	0x00, 0xC0, 0x00, 0x00, 0x40, 0xC0, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // FFF0
};

#define SCR_TIE		0x80
#define SCR_RIE		0x40
#define SCR_TE		0x20
#define SCR_RE		0x10

#define SSR_TDRE  0x80
#define SSR_RDRF  0x40
#define SSR_ORER  0x20
#define SSR_FER   0x10
#define SSR_PER   0x08
#define SSR_TEND  0x04

#define SMR_CM    0x80
#define SMR_CHR   0x40
#define SMR_PE    0x20
#define SMR_PM    0x10
#define SMR_STOP  0x08
#define SMR_MP    0x04
#define SMR_CKS1  0x02
#define SMR_CKS0  0x01

#define TCSR_CMFB  0x80
#define TCSR_CMFA  0x40
#define TCSR_OVF   0x20

#define TCR_CMIEB  0x80
#define TCR_CMIEA  0x40
#define TCR_OVIE   0x20

#define TSR_TCFD  0x80
#define TSR_TCFU  0x20
#define TSR_TCFV  0x10
#define TSR_TGFD  0x08
#define TSR_TGFC  0x04
#define TSR_TGFB  0x02
#define TSR_TGFA  0x01

#define TIER_TCIEU  0x20
#define TIER_TCIEV  0x10
#define TIER_TGIED  0x08
#define TIER_TGIEC  0x04
#define TIER_TGIEB  0x02
#define TIER_TGIEA  0x01

#define DTC_MRA_MD_NORMAL  0
#define DTC_MRA_MD_REPEAT  1
#define DTC_MRA_MD_BLOCK   2

#define DTC_MRA_SM1  0x80
#define DTC_MRA_SM0  0x40
#define DTC_MRA_DM1  0x20
#define DTC_MRA_DM0  0x10
#define DTC_MRA_DTS  0x02

#define DTC_MRB_CHNE   0x80
#define DTC_MRB_DISEL  0x40
#define DTC_MRB_CHNS   0x20

#define DMABCRL_DTME1   0x80
#define DMABCRL_DTE1    0x40
#define DMABCRL_DTME0   0x20
#define DMABCRL_DTE0    0x10
#define DMABCRL_DTIE1B  0x08
#define DMABCRL_DTIE1A  0x04
#define DMABCRL_DTIE0B  0x02
#define DMABCRL_DTIE0A  0x01

#define ADCSR_ADF   0x80
#define ADCSR_ADIE  0x40
#define ADCSR_ADST  0x20
#define ADCSR_SCAN  0x10

enum
{
	PIN_P10, PIN_P11, PIN_P12, PIN_P13, PIN_P14, PIN_P15, PIN_P16, PIN_P17,
	PIN_P20, PIN_P21, PIN_P22, PIN_P23, PIN_P24, PIN_P25, PIN_P26, PIN_P27,
	PIN_P30, PIN_P31, PIN_P32, PIN_P33, PIN_P34, PIN_P35,
	PIN_P40, PIN_P41, PIN_P42, PIN_P43, PIN_P44, PIN_P45, PIN_P46, PIN_P47,
	PIN_P50, PIN_P51, PIN_P52, PIN_P53,
	PIN_P60, PIN_P61, PIN_P62, PIN_P63, PIN_P64, PIN_P65, PIN_P66, PIN_P67,
	PIN_PA0, PIN_PA1, PIN_PA2, PIN_PA3, PIN_PA4, PIN_PA5, PIN_PA6, PIN_PA7,
	PIN_PB0, PIN_PB1, PIN_PB2, PIN_PB3, PIN_PB4, PIN_PB5, PIN_PB6, PIN_PB7,
	PIN_PC0, PIN_PC1, PIN_PC2, PIN_PC3, PIN_PC4, PIN_PC5, PIN_PC6, PIN_PC7,
	PIN_PD0, PIN_PD1, PIN_PD2, PIN_PD3, PIN_PD4, PIN_PD5, PIN_PD6, PIN_PD7,
	PIN_PE0, PIN_PE1, PIN_PE2, PIN_PE3, PIN_PE4, PIN_PE5, PIN_PE6, PIN_PE7,
	PIN_PF0, PIN_PF1, PIN_PF2, PIN_PF3, PIN_PF4, PIN_PF5, PIN_PF6, PIN_PF7,
	PIN_PG0, PIN_PG1, PIN_PG2, PIN_PG3, PIN_PG4
};

#define PIN_TIOCA0	PIN_P10
#define PIN_TIOCB0	PIN_P11
#define PIN_TIOCC0	PIN_P12
#define PIN_TIOCD0	PIN_P13
#define PIN_TIOCA1	PIN_P14
#define PIN_TIOCB1	PIN_P15
#define PIN_TIOCA2	PIN_P16
#define PIN_TIOCB2	PIN_P17

#define PIN_TCLKA	PIN_P12
#define PIN_TCLKB	PIN_P13
#define PIN_TCLKC	PIN_P15
#define PIN_TCLKD	PIN_P17

#define PIN_TXD0	PIN_P30
#define PIN_TXD1	PIN_P31
#define PIN_RXD0	PIN_P32
#define PIN_RXD1	PIN_P33
#define PIN_SCK0	PIN_P34
#define PIN_SCK1	PIN_P35

#define PIN_TIOCA3	PIN_P20
#define PIN_TIOCB3	PIN_P21
#define PIN_TIOCC3	PIN_P22
#define PIN_TIOCD3	PIN_P23
#define PIN_TIOCA4	PIN_P24
#define PIN_TIOCB4	PIN_P25
#define PIN_TIOCA5	PIN_P26
#define PIN_TIOCB5	PIN_P27

#define PIN_TMRI0	PIN_P22
#define PIN_TMCI0	PIN_P23
#define PIN_TMRI1	PIN_P24
#define PIN_TMCI1	PIN_P25
#define PIN_TMO0	PIN_P26
#define PIN_TMO1	PIN_P27

const int TPU_0_DIV[] = { 1, 4, 16, 64, 0, 0, 0,0 };
const int TPU_1_DIV[] = { 1, 4, 16, 64, 0, 0, 256, 0 };
const int TPU_2_DIV[] = { 1, 4, 16, 64, 0, 0, 0, 1024 };
const int TPU_3_DIV[] = { 1, 4, 16, 64, 0, 1024, 256, 4096 };
const int TPU_4_DIV[] = { 1, 4, 16, 64, 0, 0, 1024, 0 };
const int TPU_5_DIV[] = { 1, 4, 16, 64, 0, 0, 256, 0 };

const int H8S_IO_SMR[] = { H8S_IO_SMR0, H8S_IO_SMR1, H8S_IO_SMR2 };
const int H8S_IO_BRR[] = { H8S_IO_BRR0, H8S_IO_BRR1, H8S_IO_BRR2 };
const int H8S_IO_SCR[] = { H8S_IO_SCR0, H8S_IO_SCR1, H8S_IO_SCR2 };
const int H8S_IO_TDR[] = { H8S_IO_TDR0, H8S_IO_TDR1, H8S_IO_TDR2 };
const int H8S_IO_SSR[] = { H8S_IO_SSR0, H8S_IO_SSR1, H8S_IO_SSR2 };
const int H8S_IO_RDR[] = { H8S_IO_RDR0, H8S_IO_RDR1, H8S_IO_RDR2 };

const int H8S_INT_TXI[] = { H8S_INT_TXI0, H8S_INT_TXI1, H8S_INT_TXI2 };
const int H8S_INT_RXI[] = { H8S_INT_RXI0, H8S_INT_RXI1, H8S_INT_RXI2 };

const int H8S_IO_TCORA[] = { H8S_IO_TCORA0, H8S_IO_TCORA1 };
const int H8S_IO_TCORB[] = { H8S_IO_TCORB0, H8S_IO_TCORB1 };
const int H8S_IO_TCR[]   = { H8S_IO_TCR0,   H8S_IO_TCR1   };
const int H8S_IO_TCNT[]  = { H8S_IO_TCNT0,  H8S_IO_TCNT1  };
const int H8S_IO_TCSR[]  = { H8S_IO_TCSR0,  H8S_IO_TCSR1  };

const int H8S_INT_OVI[]  = { H8S_INT_OVI0,  H8S_INT_OVI1  };
const int H8S_INT_CMIA[] = { H8S_INT_CMIA0, H8S_INT_CMIA1 };
const int H8S_INT_CMIB[] = { H8S_INT_CMIB0, H8S_INT_CMIB1 };

const int H8S_IO_TGRA_H[] = { H8S_IO_TGR0A_H, H8S_IO_TGR1A_H, H8S_IO_TGR2A_H, H8S_IO_TGR3A_H, H8S_IO_TGR4A_H, H8S_IO_TGR5A_H };
const int H8S_IO_TGRA_L[] = { H8S_IO_TGR0A_L, H8S_IO_TGR1A_L, H8S_IO_TGR2A_L, H8S_IO_TGR3A_L, H8S_IO_TGR4A_L, H8S_IO_TGR5A_L };
const int H8S_IO_TGRB_H[] = { H8S_IO_TGR0B_H, H8S_IO_TGR1B_H, H8S_IO_TGR2B_H, H8S_IO_TGR3B_H, H8S_IO_TGR4B_H, H8S_IO_TGR5B_H };
const int H8S_IO_TGRB_L[] = { H8S_IO_TGR0B_L, H8S_IO_TGR1B_L, H8S_IO_TGR2B_L, H8S_IO_TGR3B_L, H8S_IO_TGR4B_L, H8S_IO_TGR5B_L };
const int H8S_IO_TGRC_H[] = { H8S_IO_TGR0C_H, 0, 0, H8S_IO_TGR3C_H, 0, 0 };
const int H8S_IO_TGRC_L[] = { H8S_IO_TGR0C_L, 0, 0, H8S_IO_TGR3C_L, 0, 0 };
const int H8S_IO_TGRD_H[] = { H8S_IO_TGR0D_H, 0, 0, H8S_IO_TGR3D_H, 0, 0 };
const int H8S_IO_TGRD_L[] = { H8S_IO_TGR0D_L, 0, 0, H8S_IO_TGR3D_L, 0, 0 };
const int H8S_IO_TCNT_H[] = { H8S_IO_TCNT0_H, H8S_IO_TCNT1_H, H8S_IO_TCNT2_H, H8S_IO_TCNT3_H, H8S_IO_TCNT4_H, H8S_IO_TCNT5_H };
const int H8S_IO_TCNT_L[] = { H8S_IO_TCNT0_L, H8S_IO_TCNT1_L, H8S_IO_TCNT2_L, H8S_IO_TCNT3_L, H8S_IO_TCNT4_L, H8S_IO_TCNT5_L };
const int H8S_IO_TIOR_H[] = { H8S_IO_TIOR0_H, H8S_IO_TIOR1, H8S_IO_TIOR2, H8S_IO_TIOR3_H, H8S_IO_TIOR4, H8S_IO_TIOR5 };
const int H8S_IO_TIOR_L[] = { H8S_IO_TIOR0_L, 0, 0, H8S_IO_TIOR3_L, 0, 0 };
const int H8S_IO_TTCR[]   = { H8S_IO_TTCR0, H8S_IO_TTCR1, H8S_IO_TTCR2, H8S_IO_TTCR3, H8S_IO_TTCR4, H8S_IO_TTCR5 };
const int H8S_IO_TIER[]   = { H8S_IO_TIER0, H8S_IO_TIER1, H8S_IO_TIER2, H8S_IO_TIER3, H8S_IO_TIER4, H8S_IO_TIER5 };
const int H8S_IO_TSR[]    = { H8S_IO_TSR0, H8S_IO_TSR1, H8S_IO_TSR2, H8S_IO_TSR3, H8S_IO_TSR4, H8S_IO_TSR5 };

const int H8S_INT_TCIV[] = { H8S_INT_TCI0V, H8S_INT_TCI1V, H8S_INT_TCI2V, H8S_INT_TCI3V, H8S_INT_TCI4V, H8S_INT_TCI5V };
const int H8S_INT_TCIU[] = { 0, H8S_INT_TCI1U, H8S_INT_TCI2U, 0, H8S_INT_TCI4U, H8S_INT_TCI5U };
const int H8S_INT_TGIA[] = { H8S_INT_TGI0A, H8S_INT_TGI1A, H8S_INT_TGI2A, H8S_INT_TGI3A, H8S_INT_TGI4A, H8S_INT_TGI5A };
const int H8S_INT_TGIB[] = { H8S_INT_TGI0B, H8S_INT_TGI1B, H8S_INT_TGI2B, H8S_INT_TGI3B, H8S_INT_TGI4B, H8S_INT_TGI5B };
const int H8S_INT_TGIC[] = { H8S_INT_TGI0C, 0, 0, H8S_INT_TGI3C, 0, 0 };
const int H8S_INT_TGID[] = { H8S_INT_TGI0D, 0, 0, H8S_INT_TGI3D, 0, 0 };

const int PIN_TIOCA[] = { PIN_TIOCA0, PIN_TIOCA1, PIN_TIOCA2, PIN_TIOCA3, PIN_TIOCA4, PIN_TIOCA5 };
const int PIN_TIOCB[] = { PIN_TIOCB0, PIN_TIOCB1, PIN_TIOCB2, PIN_TIOCB3, PIN_TIOCB4, PIN_TIOCB5 };
const int PIN_TIOCC[] = { PIN_TIOCC0, 0, 0, PIN_TIOCC3, 0, 0 };
const int PIN_TIOCD[] = { PIN_TIOCD0, 0, 0, PIN_TIOCD3, 0, 0 };

const int TCR_CCLR_AND[] = { 7, 3, 3, 7, 3, 3 };

// prototypes

void h8s_tmr_x_reset( h83xx_state *h8, int x);
void h8s_tpu_x_reset( h83xx_state *h8, int x);
void h8s_tpu_x_tick( h83xx_state *h8, int x);

////////////
// DTVECR //
////////////

#define DTVECR_ADDR(x) (0x400 + (x << 1))

///////////////
// MEMCONV.H //
///////////////

static TIMER_CALLBACK( h8s_tmr_callback );
static TIMER_CALLBACK( h8s_tpu_callback );
static TIMER_CALLBACK( h8s_sci_callback );

/////////////
// TIMER.C //
/////////////

UINT16 ptr_read_16( void *ptr)
{
	return (*(((UINT8*)ptr) + 0) << 8) | (*(((UINT8*)ptr) + 1) << 0);
}

void ptr_write_16( void *ptr, UINT16 data)
{
	*(((UINT8*)ptr) + 0) = (data >> 8) & 0xFF;
	*(((UINT8*)ptr) + 1) = (data >> 0) & 0xFF;
}

int device_icount_min_timer_icount1( h83xx_state *h8, emu_timer *timer)
{
	return (timer->elapsed() * h8->device->unscaled_clock()).as_double();
}

void h8s2xxx_interrupt_request(h83xx_state *h8, UINT32 vecnum)
{
	UINT8 idx, bit;
	idx = vecnum >> 5;
	bit = vecnum & 0x1F;
	h8->irq_req[idx] |= (1 << bit);
}

void h8s_dtce_execute(h83xx_state *h8, UINT32 addr_dtce, UINT8 mask_dtce, UINT32 addr_dtvecr)
{
	UINT32 data[3], dtc_vect, dtc_sar, dtc_dar, cnt, i;
	UINT8 dtc_mra, dtc_mrb, sz;
	UINT16 dtc_cra, dtc_crb;
	// get dtc info
	dtc_vect  = 0xFF0000 | h8->program->read_word(addr_dtvecr);
	data[0]   = h8->program->read_dword( dtc_vect + 0);
	data[1]   = h8->program->read_dword( dtc_vect + 4);
	data[2]   = h8->program->read_dword( dtc_vect + 8);
	dtc_mra   = (data[0] >> 24) & 0xFF;
	dtc_sar   = (data[0] >>  0) & 0xFFFFFF;
	dtc_mrb   = (data[1] >> 24) & 0xFF;
	dtc_dar   = (data[1] >>  0) & 0xFFFFFF;
	dtc_cra   = (data[2] >> 16) & 0xFFFF;
	dtc_crb   = (data[2] >>  0) & 0xFFFF;
	verboselog( h8->device->machine(), 3, "dtc : vect %08X mra %02X sar %08X mrb %02X dar %08X cra %04X crb %04X\n", dtc_vect, dtc_mra, dtc_sar, dtc_mrb, dtc_dar, dtc_cra, dtc_crb);
	// execute
	if ((dtc_mra & 0x0E) != 0x00) fatalerror("H8S: dtc unsupported MRA %x\n", dtc_mra&0x0e);
	sz = 1 << (dtc_mra & 0x01);
	cnt = dtc_cra;
	for (i=0;i<cnt;i++)
	{
		if (dtc_sar == H8S_IO_ADDR( H8S_IO_RDR1)) h8->program->write_byte( H8S_IO_ADDR( H8S_IO_SSR1), h8->program->read_byte( H8S_IO_ADDR( H8S_IO_SSR1)) & (~H8S_SSR_TDRE));
		if (dtc_mra & 0x01) h8->program->write_word( dtc_dar, h8->program->read_word( dtc_sar)); else h8->program->write_byte( dtc_dar, h8->program->read_byte( dtc_sar));
		if (dtc_dar == H8S_IO_ADDR( H8S_IO_TDR0)) h8->program->write_byte( H8S_IO_ADDR( H8S_IO_SSR0), h8->program->read_byte( H8S_IO_ADDR( H8S_IO_SSR0)) & (~H8S_SSR_TDRE));
		if (dtc_mra & 0x80) { if (dtc_mra & 0x40) dtc_sar -= sz; else dtc_sar += sz; }
		if (dtc_mra & 0x20) { if (dtc_mra & 0x10) dtc_dar -= sz; else dtc_dar += sz; }
	}
	h8->program->write_byte( addr_dtce, h8->program->read_byte( addr_dtce) & (~mask_dtce));
}

void h8s_dtce_check(h83xx_state *h8,  int vecnum)
{
	UINT32 dtce = 0;
	int bit = 0;
	// get dtce info
	switch (vecnum)
	{
		// DTCEA
		case H8S_INT_IRQ0  : dtce = H8S_IO_DTCEA; bit = 7; break;
		case H8S_INT_IRQ1  : dtce = H8S_IO_DTCEA; bit = 6; break;
		case H8S_INT_IRQ2  : dtce = H8S_IO_DTCEA; bit = 5; break;
		case H8S_INT_IRQ3  : dtce = H8S_IO_DTCEA; bit = 4; break;
		case H8S_INT_IRQ4  : dtce = H8S_IO_DTCEA; bit = 3; break;
		case H8S_INT_IRQ5  : dtce = H8S_IO_DTCEA; bit = 2; break;
		case H8S_INT_IRQ6  : dtce = H8S_IO_DTCEA; bit = 1; break;
		case H8S_INT_IRQ7  : dtce = H8S_IO_DTCEA; bit = 0; break;
		// DTCEB
		case H8S_INT_ADI   : dtce = H8S_IO_DTCEB; bit = 6; break;
		case H8S_INT_TGI0A : dtce = H8S_IO_DTCEB; bit = 5; break;
		case H8S_INT_TGI0B : dtce = H8S_IO_DTCEB; bit = 4; break;
		case H8S_INT_TGI0C : dtce = H8S_IO_DTCEB; bit = 3; break;
		case H8S_INT_TGI0D : dtce = H8S_IO_DTCEB; bit = 2; break;
		case H8S_INT_TGI1A : dtce = H8S_IO_DTCEB; bit = 1; break;
		case H8S_INT_TGI1B : dtce = H8S_IO_DTCEB; bit = 0; break;
		// DTCEC
		case H8S_INT_TGI2A : dtce = H8S_IO_DTCEC; bit = 7; break;
		case H8S_INT_TGI2B : dtce = H8S_IO_DTCEC; bit = 6; break;
		// DTCED
		case H8S_INT_CMIA0 : dtce = H8S_IO_DTCED; bit = 3; break;
		case H8S_INT_CMIB0 : dtce = H8S_IO_DTCED; bit = 2; break;
		case H8S_INT_CMIA1 : dtce = H8S_IO_DTCED; bit = 1; break;
		case H8S_INT_CMIB1 : dtce = H8S_IO_DTCED; bit = 0; break;
		// DTCEE
		case H8S_INT_RXI0  : dtce = H8S_IO_DTCEE; bit = 3; break;
		case H8S_INT_TXI0  : dtce = H8S_IO_DTCEE; bit = 2; break;
		case H8S_INT_RXI1  : dtce = H8S_IO_DTCEE; bit = 1; break;
		case H8S_INT_TXI1  : dtce = H8S_IO_DTCEE; bit = 0; break;
		// DTCEF
		case H8S_INT_RXI2  : dtce = H8S_IO_DTCEF; bit = 7; break;
		case H8S_INT_TXI2  : dtce = H8S_IO_DTCEF; bit = 6; break;
	}
	// execute
	if ((dtce != 0) && (h8->per_regs[dtce] & (1 << bit))) h8s_dtce_execute(h8, H8S_IO_ADDR( dtce), (1 << bit), DTVECR_ADDR( vecnum));
}

void h8s_periph_reset(h83xx_state *h8)
{
	const int tpu_max = ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) ? 6 : 3;
	if ((h8->device->type() == H8S2241) || (h8->device->type() == H8S2246))
	{
		memcpy( h8->per_regs, H8S_RESET_H8S_IO_224x, sizeof( h8->per_regs));
	}
	else if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394))
	{
		memcpy( h8->per_regs, H8S_RESET_H8S_IO_2323, sizeof( h8->per_regs));
	}
	for (int i = 0; i < 2; i++) h8s_tmr_x_reset( h8, i);
	for (int i = 0; i < tpu_max; i++) h8s_tpu_x_reset( h8, i);
}

/////////////////
// TIMER 8-BIT //
/////////////////

void h8s_tmr_init(h83xx_state *h8)
{
	for (int i = 0; i < 2; i++)
	{
		h8->tmr[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tmr_callback), h8);
		h8->tmr[i].timer->adjust(attotime::never, i);
	}
}

void h8s_tmr_x_reset( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	tmr->cycles_per_tick = 0;
	tmr->timer->adjust(attotime::never, x);
}

UINT64 h8s_tmr_x_calculate_ticks( h83xx_state *h8, int x, UINT8 tcnt)
{
	UINT64 cycles1, cycles2;
	UINT8 tcor;
	// overflow
	if (tcnt == 0) cycles1 = 0xFF + 1; else cycles1 = (UINT8)(0xFF - tcnt + 1);
	// tcora
	tcor = h8->per_regs[H8S_IO_TCORA[x]];
	if (tcnt == tcor) cycles2 = 0xFF + 1; else cycles2 = (UINT8)(tcor - tcnt);
	if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	// tcorb
	tcor = h8->per_regs[H8S_IO_TCORB[x]];
	if (tcnt == tcor) cycles2 = 0xFF + 1; else cycles2 = (UINT8)(tcor - tcnt);
	if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	// done
	return cycles1;
}

void h8s_tmr_x_update( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	UINT64 cycles;
	int div = 0;
	UINT8 tcr, tcnt;
	tcr = h8->per_regs[H8S_IO_TCR[x]];
	switch ((tcr >> 0) & 7)
	{
		case 1  : div = 8; break;
		case 2  : div = 64; break;
		case 3  : div = 8192; break;
//      default : OS_MessageBox( NULL, "tmr_x_update", "error", 0); break;
	}
	tmr->cycles_per_tick = div;
	tcnt = h8->per_regs[H8S_IO_TCNT[x]];
	verboselog( h8->device->machine(), 5, "TMR %d - update (div %d tcnt %02X)\n", x, div, tcnt);
	// update timer
	cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt) * tmr->cycles_per_tick;
	tmr->timer_cycles = cycles;
	tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
}

UINT8 h8s_tmr_x_calculate_tcnt( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	UINT8 tcnt;
	tcnt = h8->per_regs[H8S_IO_TCNT[x]];
	if (tmr->timer->enabled())
	{
		tcnt = (UINT8)(tcnt + ((device_icount_min_timer_icount1( h8, tmr->timer)) / tmr->cycles_per_tick));
	}
	return tcnt;
}

void h8s_tmr_x_start( h83xx_state *h8, int x)
{
	verboselog( h8->device->machine(), 5, "TMR %d - start\n", x);
	// ...
	h8s_tmr_x_update( h8, x);
}

void h8s_tmr_x_stop( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	UINT8 tcnt;
	// ...
	verboselog( h8->device->machine(), 5, "TMR %d - stop\n", x);
	// calculate tcnt
	tcnt = h8s_tmr_x_calculate_tcnt( h8, x);
	// update tcnt
	h8->per_regs[H8S_IO_TCNT[x]] = tcnt;
	// disable timer
	tmr->timer->reset();
}

UINT8 h8s_tmr_x_tcnt_change( h83xx_state *h8, int x, UINT8 tcnt)
{
	UINT8 tcr, tcor;
	int clear = 0;
	// ...
	verboselog( h8->device->machine(), 9, "TMR %d - tmr_x_tcnt_change (%02X)\n", x, tcnt);
	// ...
	tcr = h8->per_regs[H8S_IO_TCR[x]];
	// overflow
	if (tcnt == 0)
	{
		verboselog( h8->device->machine(), 9, "TMR %d - overflow match\n", x);
		h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_OVF;
		if ((tcr & TCR_OVIE) != 0)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_OVI[x]);
		}
	}
	// tcora
	tcor = h8->per_regs[H8S_IO_TCORA[x]];
	if (tcnt == tcor)
	{
		verboselog( h8->device->machine(), 9, "TMR %d - tcora match\n", x);
		h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_CMFA;
		if ((tcr & TCR_CMIEA) != 0)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_CMIA[x]);
		}
		if (((tcr >> 3) & 3) == 1) // CCLR 1 -> "clear by compare match a"
		{
			clear = 1;
		}
	}
	// tcorb
	tcor = h8->per_regs[H8S_IO_TCORB[x]];
	if (tcnt == tcor)
	{
		verboselog( h8->device->machine(), 9, "TMR %d - tcorb match\n", x);
		h8->per_regs[H8S_IO_TCSR[x]] |= TCSR_CMFB;
		if ((tcr & TCR_CMIEB) != 0)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_CMIB[x]);
		}
		if (((tcr >> 3) & 3) == 2) // CCLR 2 -> "clear by compare match b"
		{
			clear = 1;
		}
	}
	// ...
	if (clear)
	{
		verboselog( h8->device->machine(), 5, "TMR %d - tcnt clear\n", x);
		tcnt = 0;
	}
	// ...
	return tcnt;
}

void h8s_tmr_x_callback( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	UINT64 cycles;
	UINT8 tcnt;
	// calculate tcnt
	tcnt = h8->per_regs[H8S_IO_TCNT[x]];
	tcnt = (UINT8)(tcnt + tmr->timer_cycles / tmr->cycles_per_tick);
	// ...
	tcnt = h8s_tmr_x_tcnt_change( h8, x, tcnt);
	// update tcnt
	h8->per_regs[H8S_IO_TCNT[x]] = tcnt;
	// update timer
	cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt) * tmr->cycles_per_tick;
	tmr->timer_cycles = cycles;
	tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
}

void h8s_tmr_x_write_tcr( h83xx_state *h8, int x, UINT8 data)
{
	UINT8 old_data = h8->per_regs[H8S_IO_TCR[x]];
	h8->per_regs[H8S_IO_TCR[x]] = data;
	if ((data & 0x07) != (old_data & 0x07))
	{
		if ((data & 0x07) != 0)
		{
			h8s_tmr_x_start( h8, x);
		}
		else
		{
			h8s_tmr_x_stop( h8, x);
		}
	}
}

void h8s_tmr_x_write_tcnt( h83xx_state *h8, int x, UINT8 data)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	h8->per_regs[H8S_IO_TCNT[x]] = data;
	if (tmr->timer->enabled())
	{
		h8s_tmr_x_update( h8, x);
	}
}

void h8s_tmr_x_write_tcor( h83xx_state *h8, int x)
{
	H8S2XXX_TMR *tmr = &h8->tmr[x];
	if ((tmr->timer->enabled()) && (tmr->cycles_per_tick != 0))
	{
		UINT8 tcnt1, tcnt2;
		UINT64 cycles;
		tcnt1 = h8->per_regs[H8S_IO_TCNT[x]];
		tcnt2 = (UINT8)(tcnt1 + ((device_icount_min_timer_icount1( h8, tmr->timer)) / tmr->cycles_per_tick));
		cycles = h8s_tmr_x_calculate_ticks( h8, x, tcnt2) * tmr->cycles_per_tick;
		cycles = cycles + (tcnt2 - tcnt1) * tmr->cycles_per_tick;
		tmr->timer_cycles = cycles;
		tmr->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
	}
}

UINT8 h8s_tmr_x_read_tcnt( h83xx_state *h8, int x)
{
	return h8s_tmr_x_calculate_tcnt( h8, x);
}

void h8s_tmr_x_write_tcora( h83xx_state *h8, int x, UINT8 data)
{
	h8->per_regs[H8S_IO_TCORA[x]] = data;
	h8s_tmr_x_write_tcor( h8, x);
}

void h8s_tmr_x_write_tcorb( h83xx_state *h8, int x, UINT8 data)
{
	h8->per_regs[H8S_IO_TCORB[x]] = data;
	h8s_tmr_x_write_tcor( h8, x);
}

static TIMER_CALLBACK( h8s_tmr_callback)
{
	h83xx_state *h8 = (h83xx_state *)ptr;
	h8s_tmr_x_callback( h8, param);
}

//////////////////
// TIMER 16-BIT //
//////////////////

void h8s_tpu_init(h83xx_state *h8)
{
	const int tpu_max = ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) ? 6 : 3;
	for (int i = 0; i < tpu_max; i++)
	{
		h8->tpu[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_tpu_callback), h8);
		h8->tpu[i].timer->adjust(attotime::never, i);
	}
}

void h8s_tpu_x_reset( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	tpu->cycles_per_tick = 0;
	tpu->timer->reset();
}

UINT16 h8s_tpu_x_calculate_tcnt( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	UINT16 tcnt;
	tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
	if ((tpu->timer->enabled()) && (tpu->cycles_per_tick > 0))
	{
		tcnt = (UINT16)(tcnt + ((device_icount_min_timer_icount1( h8, tpu->timer)) / tpu->cycles_per_tick));
	}
	return tcnt;
}

UINT64 h8s_tpu_x_calculate_ticks( h83xx_state *h8, int x, UINT16 tcnt)
{
	UINT64 cycles1, cycles2;
	UINT16 tgr;
	// overflow
	if (tcnt == 0) cycles1 = 0xFFFF + 1; else cycles1 = (UINT16)(0xFFFF - tcnt + 1);
	// tgra
	tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRA_H[x]);
	if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
	if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	// tgrb
	tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRB_H[x]);
	if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
	if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	// tgrc
	if ((x == 0) || (x == 3))
	{
		tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRC_H[x]);
		if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
		if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	}
	// tgrd
	if ((x == 0) || (x == 3))
	{
		tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRD_H[x]);
		if (tcnt == tgr) cycles2 = 0xFFFF + 1; else cycles2 = (UINT16)(tgr - tcnt);
		if ((cycles2 > 0) && (cycles2 < cycles1)) cycles1 = cycles2;
	}
	// done
	return cycles1;
}

void h8s_pin_write( h83xx_state *h8, int pin, int data)
{
	switch (pin)
	{
		case PIN_TIOCB1 :
		{
			UINT8 p1dr;
			p1dr = h8->per_regs[H8S_IO_P1DR];
			if (data) p1dr |= H8S_P1_TIOCB1; else p1dr &= ~H8S_P1_TIOCB1;
			h8->io->write_byte(H8S_IO_ADDR(H8S_IO_P1DR), p1dr);
			h8->per_regs[H8S_IO_P1DR] = p1dr;
		}
		break;
	}
}

int h8s_pin_read( h83xx_state *h8, int pin)
{
	switch (pin)
	{
		case PIN_TIOCB1 : return (h8->per_regs[H8S_IO_P1DR] & H8S_P1_TIOCB1) ? 1 : 0;
	}
	return 0;
}

void h8s_tioc_pin_update( h83xx_state *h8, int io, int pin)
{
	switch (io)
	{
		case 1 : h8s_pin_write( h8, pin, 0); break;
		case 2 : h8s_pin_write( h8, pin, 1); break;
		case 3 : h8s_pin_write( h8, pin, h8s_pin_read( h8, pin) ? 0 : 1); break;
		case 5 : h8s_pin_write( h8, pin, 0); break;
		case 6 : h8s_pin_write( h8, pin, 1); break;
		case 7 : h8s_pin_write( h8, pin, h8s_pin_read( h8, pin) ? 0 : 1); break;
	}
}

void h8s_tioc_pin_reset( h83xx_state *h8, int io, int pin)
{
	switch (io)
	{
		case 1 : h8s_pin_write( h8, pin, 0); break;
		case 2 : h8s_pin_write( h8, pin, 0); break;
		case 3 : h8s_pin_write( h8, pin, 0); break;
		case 5 : h8s_pin_write( h8, pin, 1); break;
		case 6 : h8s_pin_write( h8, pin, 1); break;
		case 7 : h8s_pin_write( h8, pin, 1); break;
	}
}

void h8s_tpu_x_tioc_reset( h83xx_state *h8, int x)
{
	UINT8 tior;
	int cclr;
	// ...
	cclr = (h8->per_regs[H8S_IO_TTCR[x]] >> 5) & TCR_CCLR_AND[x];
	// tioca & tiocb
	tior = h8->per_regs[H8S_IO_TIOR_H[x]];
	if (cclr != 1) h8s_tioc_pin_reset( h8, (tior >> 0) & 0x0F, PIN_TIOCA[x]);
	if (cclr != 2) h8s_tioc_pin_reset( h8, (tior >> 4) & 0x0F, PIN_TIOCB[x]);
	// tiocc & tiocd
	if ((x == 0) || (x == 3))
	{
		tior = h8->per_regs[H8S_IO_TIOR_L[x]];
		if (cclr != 5) h8s_tioc_pin_reset( h8, (tior >> 0) & 0x0F, PIN_TIOCC[x]);
		if (cclr != 6) h8s_tioc_pin_reset( h8, (tior >> 4) & 0x0F, PIN_TIOCD[x]);
	}
}

void h8s_tpu_x_overflow( h83xx_state *h8, int x)
{
	if (x == 2)
	{
		UINT8 tcr = h8->per_regs[H8S_IO_TTCR[1]];
		if (((tcr >> 0) & 7) == 7)
		{
			h8s_tpu_x_tick( h8, 1);
		}
	}
	if (x == 5)
	{
		UINT8 tcr = h8->per_regs[H8S_IO_TTCR[4]];
		if (((tcr >> 0) & 7) == 7)
		{
			h8s_tpu_x_tick( h8, 4);
		}
	}
}

UINT16 h8s_tpu_x_tcnt_change( h83xx_state *h8, int x, UINT16 tcnt)
{
	UINT16 tgr;
	UINT8 tcr, tier, tior;
	int clear = 0;
	// ...
	verboselog( h8->device->machine(), 9, "TPU %d - tpu_x_tcnt_change (%04X)\n", x, tcnt);
	// ...
	tcr = h8->per_regs[H8S_IO_TTCR[x]];
	tier = h8->per_regs[H8S_IO_TIER[x]];
	// overflow
	if (tcnt == 0)
	{
		verboselog( h8->device->machine(), 9, "TPU %d - overflow match\n", x);
		h8->per_regs[H8S_IO_TSR[x]] |= TSR_TCFV;
		if (tier & TIER_TCIEV)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_TCIV[x]);
		}
		h8s_tpu_x_overflow( h8, x);
	}
	// tgra
	tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRA_H[x]);
	if (tcnt == tgr)
	{
		verboselog( h8->device->machine(), 9, "TPU %d - tgra match\n", x);
		h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFA;
		if (tier & TIER_TGIEA)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_TGIA[x]);
		}
		if (((tcr >> 5) & TCR_CCLR_AND[x]) == 1) // CCLR 1 -> "TCNT cleared by TGRA compare match/input capture"
		{
			clear = 1;
		}
		else
		{
			tior = h8->per_regs[H8S_IO_TIOR_H[x]];
			h8s_tioc_pin_update( h8, (tior >> 0) & 0x0F, PIN_TIOCA[x]);
		}
	}
	// tgrb
	tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRB_H[x]);
	if (tcnt == tgr)
	{
		verboselog( h8->device->machine(), 9, "TPU %d - tgrb match\n", x);
		h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFB;
		if (tier & TIER_TGIEB)
		{
			h8s2xxx_interrupt_request( h8, H8S_INT_TGIB[x]);
		}
		if (((tcr >> 5) & TCR_CCLR_AND[x]) == 2) // CCLR 2 -> "TCNT cleared by TGRB compare match/input capture"
		{
			clear = 1;
		}
		else
		{
			tior = h8->per_regs[H8S_IO_TIOR_H[x]];
			h8s_tioc_pin_update( h8, (tior >> 4) & 0x0F, PIN_TIOCB[x]);
		}
	}
	// tgrc
	if ((x == 0) || (x == 3))
	{
		tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRC_H[x]);
		if (tcnt == tgr)
		{
			verboselog( h8->device->machine(), 9, "TPU %d - tgrc match\n", x);
			h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFC;
			if (tier & TIER_TGIEC)
			{
				h8s2xxx_interrupt_request( h8, H8S_INT_TGIC[x]);
			}
			if (((tcr >> 5) & TCR_CCLR_AND[x]) == 5) // CCLR 5 -> "TCNT cleared by TGRC compare match/input capture"
			{
				clear = 1;
			}
			else
			{
				tior = h8->per_regs[H8S_IO_TIOR_L[x]];
				h8s_tioc_pin_update( h8, (tior >> 0) & 0x0F, PIN_TIOCC[x]);
			}
		}
	}
	// tgrd
	if ((x == 0) || (x == 3))
	{
		tgr = ptr_read_16( h8->per_regs + H8S_IO_TGRD_H[x]);
		if (tcnt == tgr)
		{
			verboselog( h8->device->machine(), 9, "TPU %d - tgrd match\n", x);
			h8->per_regs[H8S_IO_TSR[x]] |= TSR_TGFD;
			if (tier & TIER_TGIED)
			{
				h8s2xxx_interrupt_request( h8, H8S_INT_TGID[x]);
			}
			if (((tcr >> 5) & TCR_CCLR_AND[x]) == 6) // CCLR 6 -> "TCNT cleared by TGRD compare match/input capture"
			{
				clear = 1;
			}
			else
			{
				tior = h8->per_regs[H8S_IO_TIOR_L[x]];
				h8s_tioc_pin_update( h8, (tior >> 4) & 0x0F, PIN_TIOCD[x]);
			}
		}
	}
	// ...
	if (clear)
	{
		verboselog( h8->device->machine(), 9, "TPU %d - tcnt clear\n", x);
		h8s_tpu_x_tioc_reset( h8, x);
		tcnt = 0;
	}
	// ...
	return tcnt;
}

void h8s_tpu_x_callback( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	UINT64 cycles;
	UINT16 tcnt;
	// calculate tcnt
	tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
	tcnt = (UINT16)(tcnt + tpu->timer_cycles / tpu->cycles_per_tick);
	// ...
	tcnt = h8s_tpu_x_tcnt_change( h8, x, tcnt);
	// update tcnt
	ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
	// update timer
	cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt) * tpu->cycles_per_tick;
	tpu->timer_cycles = cycles;
	tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
}

void h8s_tpu_x_tick( h83xx_state *h8, int x)
{
	UINT16 tcnt;
	// calculate tcnt
	tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
	tcnt = (UINT16)(tcnt + 1);
	// ...
	tcnt = h8s_tpu_x_tcnt_change( h8, x, tcnt);
	// update tcnt
	ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
}

int h8s_tpu_x_get_div( h83xx_state *h8, int x)
{
	UINT8 tcr;
	int tpsc;
	const int *TPU_X_DIV[] = { TPU_0_DIV, TPU_1_DIV, TPU_2_DIV, TPU_3_DIV, TPU_4_DIV, TPU_5_DIV };
	tcr = h8->per_regs[H8S_IO_TTCR[x]];
	tpsc = (tcr >> 0) & 7;
	return TPU_X_DIV[x][tpsc];
}

void h8s_tpu_x_update( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	// ...
	tpu->cycles_per_tick = h8s_tpu_x_get_div( h8, x);
	// update timer
	if (tpu->cycles_per_tick != 0)
	{
		UINT16 tcnt;
		UINT64 cycles;
		tcnt = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
		cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt) * tpu->cycles_per_tick;
		tpu->timer_cycles = cycles;
		tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
	}
	else
	{
		tpu->timer->reset();
	}
}

void h8s_tpu_x_start( h83xx_state *h8, int x)
{
	verboselog( h8->device->machine(), 5, "TPU %d - start\n", x);
	// ...
	h8s_tpu_x_update( h8, x);
}

void h8s_tpu_x_stop( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	UINT16 tcnt;
	// ...
	verboselog( h8->device->machine(), 5, "TPU %d - stop\n", x);
	// calculate tcnt
	tcnt = h8s_tpu_x_calculate_tcnt( h8, x);
	// update tcnt
	ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], tcnt);
	// disable timer
	tpu->timer->reset();
}

UINT16 h8s_tpu_x_read_tcnt( h83xx_state *h8, int x)
{
	return h8s_tpu_x_calculate_tcnt( h8, x);
}

void h8s_tpu_x_write_tcnt( h83xx_state *h8, int x, UINT16 data)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	verboselog( h8->device->machine(), 9, "TPU %d - write tcnt (%04X)\n", x, data);
	ptr_write_16( h8->per_regs + H8S_IO_TCNT_H[x], data);
	if (tpu->timer->enabled())
	{
		h8s_tpu_x_update( h8, x);
	}
}

void h8s_tpu_x_write_tgr( h83xx_state *h8, int x)
{
	H8S2XXX_TPU *tpu = &h8->tpu[x];
	if ((tpu->timer->enabled()) && (tpu->cycles_per_tick != 0))
	{
		UINT16 tcnt1, tcnt2;
		UINT64 cycles;
		tcnt1 = ptr_read_16( h8->per_regs + H8S_IO_TCNT_H[x]);
		tcnt2 = (UINT16)(tcnt1 + ((device_icount_min_timer_icount1( h8, tpu->timer)) / tpu->cycles_per_tick));
		cycles = h8s_tpu_x_calculate_ticks( h8, x, tcnt2) * tpu->cycles_per_tick;
		cycles = cycles + (tcnt2 - tcnt1) * tpu->cycles_per_tick;
		tpu->timer_cycles = cycles;
		tpu->timer->adjust(attotime::from_hz(h8->device->unscaled_clock() / cycles), x);
	}
}

void h8s_tpu_x_write_tgra( h83xx_state *h8, int x, UINT16 data)
{
	verboselog( h8->device->machine(), 9, "TPU %d - write tgra (%04X)\n", x, data);
	ptr_write_16( h8->per_regs + H8S_IO_TGRA_H[x], data);
	h8s_tpu_x_write_tgr( h8, x);
}

void h8s_tpu_x_write_tgrb( h83xx_state *h8, int x, UINT16 data)
{
	verboselog( h8->device->machine(), 9, "TPU %d - write tgrb (%04X)\n", x, data);
	ptr_write_16( h8->per_regs + H8S_IO_TGRB_H[x], data);
	h8s_tpu_x_write_tgr( h8, x);
}

void h8s_tpu_x_write_tgrc( h83xx_state *h8, int x, UINT16 data)
{
	verboselog( h8->device->machine(), 9, "TPU %d - write tgrc (%04X)\n", x, data);
	ptr_write_16( h8->per_regs + H8S_IO_TGRC_H[x], data);
	h8s_tpu_x_write_tgr( h8, x);
}

void h8s_tpu_x_write_tgrd( h83xx_state *h8, int x, UINT16 data)
{
	verboselog( h8->device->machine(), 9, "TPU %d - write tgrd (%04X)\n", x, data);
	ptr_write_16( h8->per_regs + H8S_IO_TGRD_H[x], data);
	h8s_tpu_x_write_tgr( h8, x);
}

void h8s_tpu_x_write_tior( h83xx_state *h8, int x)
{
	UINT8 tstr;
	tstr = h8->per_regs[H8S_IO_TSTR];
	if ((tstr & (1 << x)) == 0)
	{
		h8s_tpu_x_tioc_reset( h8, x);
	}
}

void h8s_tpu_0_write_tior_h( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR0_H] = data;
	h8s_tpu_x_write_tior( h8, 0);
}

void h8s_tpu_0_write_tior_l( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR0_L] = data;
	h8s_tpu_x_write_tior( h8, 0);
}

void h8s_tpu_1_write_tior( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR1] = data;
	h8s_tpu_x_write_tior( h8, 1);
}

void h8s_tpu_2_write_tior( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR2] = data;
	h8s_tpu_x_write_tior( h8, 2);
}

void h8s_tpu_3_write_tior_h( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR3_H] = data;
	h8s_tpu_x_write_tior( h8, 3);
}

void h8s_tpu_3_write_tior_l( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR3_L] = data;
	h8s_tpu_x_write_tior( h8, 3);
}

void h8s_tpu_4_write_tior( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR4] = data;
	h8s_tpu_x_write_tior( h8, 4);
}

void h8s_tpu_5_write_tior( h83xx_state *h8, UINT8 data)
{
	h8->per_regs[H8S_IO_TIOR5] = data;
	h8s_tpu_x_write_tior( h8, 5);
}

void h8s_tpu_write_tstr( h83xx_state *h8, UINT8 data)
{
	const int tpu_max = ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) ? 6 : 3;
	UINT8 old_data = h8->per_regs[H8S_IO_TSTR];
	int i;
	h8->per_regs[H8S_IO_TSTR] = data;
	for (i = 0; i < tpu_max; i++)
	{
		if ((data & (1 << i)) != (old_data & (1 << i)))
		{
			if ((data & (1 << i)) != 0)
			{
				h8s_tpu_x_start( h8, i);
			}
			else
			{
				h8s_tpu_x_stop( h8, i);
			}
		}
	}
}

static TIMER_CALLBACK( h8s_tpu_callback)
{
	h83xx_state *h8 = (h83xx_state *)ptr;
	h8s_tpu_x_callback( h8, param);
}

/////////////////////////////////
// SERIAL CONTROLLER INTERFACE //
/////////////////////////////////

struct H8S_SCI_ENTRY
{
	UINT32 reg_smr, reg_brr, reg_scr, reg_tdr, reg_ssr, reg_rdr;
	UINT32 reg_pdr, reg_port;
	UINT8 port_mask_sck, port_mask_txd, port_mask_rxd;
	UINT8 int_tx, int_rx;
};

const H8S_SCI_ENTRY H8S_SCI_TABLE[] =
{
	// SCI 0
	{
		H8S_IO_SMR0, H8S_IO_BRR0, H8S_IO_SCR0, H8S_IO_TDR0, H8S_IO_SSR0, H8S_IO_RDR0,
		H8S_IO_P3DR, H8S_IO_PORT3,
		H8S_P3_SCK0, H8S_P3_TXD0, H8S_P3_RXD0,
		H8S_INT_TXI0, H8S_INT_RXI0
	},
	// SCI 1
	{
		H8S_IO_SMR1, H8S_IO_BRR1, H8S_IO_SCR1, H8S_IO_TDR1, H8S_IO_SSR1, H8S_IO_RDR1,
		H8S_IO_P3DR, H8S_IO_PORT3,
		H8S_P3_SCK1, H8S_P3_TXD1, H8S_P3_RXD1,
		H8S_INT_TXI1, H8S_INT_RXI1
	},
	// SCI 2
	{
		H8S_IO_SMR2, H8S_IO_BRR2, H8S_IO_SCR2, H8S_IO_TDR2, H8S_IO_SSR2, H8S_IO_RDR2,
		H8S_IO_P5DR, H8S_IO_PORT5,
		H8S_P5_SCK2, H8S_P5_TXD2, H8S_P5_RXD2,
		H8S_INT_TXI2, H8S_INT_RXI2
	}
};

const H8S_SCI_ENTRY *h8s_sci_entry( int num)
{
	return &H8S_SCI_TABLE[num];
}

void h8s_sci_init(h83xx_state *h8)
{
	int i;
	for (i=0;i<3;i++)
	{
		h8->sci[i].timer = h8->device->machine().scheduler().timer_alloc(FUNC(h8s_sci_callback), h8);
		h8->sci[i].timer->adjust(attotime::never, i);
	}
}

static TIMER_CALLBACK(h8s_sci_callback)
{
	h83xx_state *h8 = (h83xx_state *)ptr;
	verboselog( h8->device->machine(), 7, "h8s_sci_callback (%d)\n", param);
}

void h8s_sci_start(h83xx_state *h8, int num)
{
	#if 0
	h8->sci[num].timer->adjust(h8->sci[num].bitrate, num);
	#endif
}

void h8s_sci_stop(h83xx_state *h8, int num)
{
	h8->sci[num].timer->adjust(attotime::never, num);
}

void h8s_sci_execute(h83xx_state *h8, int num)
{
	UINT8 scr, tdr, ssr, rdr, tsr, rsr, pdr, port;
	int i;
	const H8S_SCI_ENTRY *info = h8s_sci_entry( num);
	verboselog( h8->device->machine(), 5, "h8s_sci_execute(%d)\n", num);
	// load regs
	scr = h8->per_regs[info->reg_scr];
	tdr = h8->per_regs[info->reg_tdr];
	ssr = h8->per_regs[info->reg_ssr];
	rdr = h8->per_regs[info->reg_rdr];
	tsr = 0;
	rsr = 0;
	pdr = h8->per_regs[info->reg_pdr] & (~info->port_mask_sck);
	// move byte from TDR to TSR
	if (scr & H8S_SCR_TE)
	{
		tsr = tdr;
		ssr |= H8S_SSR_TDRE;
	}
	// generate transmit data empty interrupt
	if ((scr & H8S_SCR_TIE) && (ssr & H8S_SSR_TDRE)) h8s2xxx_interrupt_request(h8, info->int_tx);
	// transmit/receive bits
	for (i=0;i<8;i++)
	{
		// write bit
		if (scr & H8S_SCR_TE)
		{
			if (tsr & (1 << i)) pdr = pdr | info->port_mask_txd; else pdr = pdr & (~info->port_mask_txd);
			h8->io->write_byte( H8S_IO_ADDR( info->reg_pdr), pdr);
		}
		// clock high to low
		h8->io->write_byte( H8S_IO_ADDR( info->reg_pdr), pdr | info->port_mask_sck);
		h8->io->write_byte( H8S_IO_ADDR( info->reg_pdr), pdr);
		// read bit
		if (scr & H8S_SCR_RE)
		{
			port = h8->io->read_byte(H8S_IO_ADDR( info->reg_port));
			if (port & info->port_mask_rxd) rsr = rsr | (1 << i);
		}
	}
	// move byte from RSR to RDR
	if (scr & H8S_SCR_RE)
	{
		rdr = rsr;
		//ssr |= H8S_SSR_RDRF;
	}
	// generate receive data full interrupt
	if ((scr & H8S_SCR_RIE) && (ssr & H8S_SSR_RDRF)) h8s2xxx_interrupt_request(h8, info->int_rx);
	// save regs
	h8->per_regs[info->reg_scr] = scr;
	h8->per_regs[info->reg_tdr] = tdr;
	h8->per_regs[info->reg_ssr] = ssr;
	h8->per_regs[info->reg_rdr] = rdr;
}

const double SCR_CKE[] = { 0.5, 2, 8, 32 }; // = 2 ^ ((2 * cke) - 1)
const int SMR_MODE[] = { 64, 8 };

void h8s_sci_rate(h83xx_state *h8, int num)
{
	UINT8 brr, scr, smr, cke, mode;
	UINT32 bitrate;
	const H8S_SCI_ENTRY *info = h8s_sci_entry( num);
	// read regs
	brr = h8->per_regs[info->reg_brr];
	scr = h8->per_regs[info->reg_scr];
	smr = h8->per_regs[info->reg_smr];
	verboselog( h8->device->machine(), 2, "BRR %02X SCR %02X SMR %02X\n", brr, scr, smr);
	// calculate bitrate
	cke = (scr >> 0) & 3;
	mode = (smr >> 7) & 1;
	bitrate = (UINT32)((h8->device->unscaled_clock() / (brr + 1)) / (SMR_MODE[mode] * SCR_CKE[cke]));
	verboselog( h8->device->machine(), 2, "SCI%d bitrate %d\n", num, bitrate);
	// store bitrate
	h8->sci[num].bitrate = bitrate;
}

////////////////////////////
// INTERNAL I/O REGISTERS //
////////////////////////////

void h8s_onchip_reg_write_8(h83xx_state *h8, int offset, UINT8 data)
{
	verboselog( h8->device->machine(), 9, "%08X | %08X <- %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	switch (offset)
	{
		// SCI 0
		case H8S_IO_SSR0  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 0); break;
		case H8S_IO_SCR0  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8, h8s_sci_entry(0)->int_tx); break;
		case H8S_IO_BRR0  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 0); h8s_sci_start(h8, 0); break;
		// SCI 1
		case H8S_IO_SSR1  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 1); break;
		case H8S_IO_SCR1  :	h8->per_regs[offset] = data; if (data & H8S_SCR_RIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(1)->int_rx); break;
		case H8S_IO_BRR1  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 1); h8s_sci_start(h8, 1); break;
		// SCI 2
		case H8S_IO_SSR2  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 2); break;
		case H8S_IO_SCR2  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(2)->int_tx); break;
		case H8S_IO_BRR2  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 2); h8s_sci_start(h8, 2); break;
		// TMR 0
		case H8S_IO_TCR0   : h8s_tmr_x_write_tcr( h8, 0, data); break;
		case H8S_IO_TCNT0  : h8s_tmr_x_write_tcnt( h8, 0, data); break;
		case H8S_IO_TCORA0 : h8s_tmr_x_write_tcora( h8, 0, data); break;
		case H8S_IO_TCORB0 : h8s_tmr_x_write_tcorb( h8, 0, data); break;
		// TMR 1
		case H8S_IO_TCR1   : h8s_tmr_x_write_tcr( h8, 1, data); break;
		case H8S_IO_TCNT1  : h8s_tmr_x_write_tcnt( h8, 1, data); break;
		case H8S_IO_TCORA1 : h8s_tmr_x_write_tcora( h8, 1, data); break;
		case H8S_IO_TCORB1 : h8s_tmr_x_write_tcorb( h8, 1, data); break;
		// ports
		case H8S_IO_P1DR : case H8S_IO_P2DR : case H8S_IO_P3DR : case H8S_IO_P4DR : case H8S_IO_P5DR :
		case H8S_IO_PADR : case H8S_IO_PBDR : case H8S_IO_PCDR : case H8S_IO_PDDR : case H8S_IO_PEDR :
		case H8S_IO_PFDR : case H8S_IO_PGDR : h8->per_regs[offset] = data; h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
		// ...
		case H8S_IO_PFDDR : h8->per_regs[offset] = data; h8->io->write_byte( H8S_IO_ADDR( offset), data); break;
		// TPU
		case H8S_IO_TSTR  : h8s_tpu_write_tstr( h8, data); break;
		// DMA
		case H8S_IO_DMABCRL :
		{
			h8->per_regs[offset] = data;
			if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394))
			{
				if ((data & 0x40) && (data & 0x80))
				{
					UINT32 i, dma_src, dma_dst;
					UINT16 dma_cnt, dma_con;
					int sz;
					dma_src = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1AH));
					dma_dst = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1BH));
					dma_cnt = h8->program->read_word( H8S_IO_ADDR(H8S_IO_ETCR1A));
					dma_con = h8->program->read_word( H8S_IO_ADDR(H8S_IO_DMACR1A));
					sz = (dma_con & 0x8000) ? 2 : 1;
					for (i=0;i<dma_cnt;i++)
					{
						if (dma_con & 0x8000) h8->program->write_word( dma_dst, h8->program->read_word( dma_src)); else h8->program->write_byte( dma_dst, h8->program->read_byte( dma_src));
						if (dma_con & 0x2000) { if (dma_con & 0x4000) dma_src -= sz; else dma_src += sz; }
						if (dma_con & 0x0020) { if (dma_con & 0x0040) dma_dst -= sz; else dma_dst += sz; }
					}
					h8->per_regs[H8S_IO_DMABCRL] &= ~0x40;
				}
			}
		}
		break;
		// ...
		default :
		{
			h8->per_regs[offset] = data;
		}
		break;
	}
}

void h8s_onchip_reg_write_8_ddr(h83xx_state *h8, int offset, UINT8 data)
{
	verboselog( h8->device->machine(), 9, "%08X | %08X <- %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	switch (offset)
	{
        #if 0
		// SCI 0
		case H8S_IO_SSR0  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 0); break;
		case H8S_IO_SCR0  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8, h8s_sci_entry(0)->int_tx); break;
		case H8S_IO_BRR0  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 0); h8s_sci_start(h8, 0); break;
		// SCI 1
		case H8S_IO_SSR1  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 1); break;
		case H8S_IO_SCR1  :	h8->per_regs[offset] = data; if (data & H8S_SCR_RIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(1)->int_rx); break;
		case H8S_IO_BRR1  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 1); h8s_sci_start(h8, 1); break;
		// SCI 2
		case H8S_IO_SSR2  : h8->per_regs[offset] = data; if ((data & H8S_SSR_TDRE) == 0) h8s_sci_execute(h8, 2); break;
		case H8S_IO_SCR2  : h8->per_regs[offset] = data; if (data & H8S_SCR_TIE) h8s2xxx_interrupt_request(h8,  h8s_sci_entry(2)->int_tx); break;
		case H8S_IO_BRR2  : h8->per_regs[offset] = data; h8s_sci_rate(h8, 2); h8s_sci_start(h8, 2); break;
		// TMR 0
		case H8S_IO_TCR0   : h8s_tmr_x_write_tcr( h8, 0, data); break;
		case H8S_IO_TCNT0  : h8s_tmr_x_write_tcnt( h8, 0, data); break;
		case H8S_IO_TCORA0 : h8s_tmr_x_write_tcora( h8, 0, data); break;
		case H8S_IO_TCORB0 : h8s_tmr_x_write_tcorb( h8, 0, data); break;
		// TMR 1
		case H8S_IO_TCR1   : h8s_tmr_x_write_tcr( h8, 1, data); break;
		case H8S_IO_TCNT1  : h8s_tmr_x_write_tcnt( h8, 1, data); break;
		case H8S_IO_TCORA1 : h8s_tmr_x_write_tcora( h8, 1, data); break;
		case H8S_IO_TCORB1 : h8s_tmr_x_write_tcorb( h8, 1, data); break;
        #endif

        case H8S_IO_IFR:
			h8->per_regs[offset] = data;
            break;

        case H8S_IO_P1DDR : case H8S_IO_P2DDR : case H8S_IO_P3DDR : case H8S_IO_P5DDR : case H8S_IO_P6DDR :
        case H8S_IO_PADDR : case H8S_IO_PBDDR : case H8S_IO_PCDDR : case H8S_IO_PDDDR : case H8S_IO_PEDDR :
        case H8S_IO_PFDDR : case H8S_IO_PGDDR :
            h8->ddrs[offset - H8S_IO_P1DDR] = data;
            break;

		case H8S_IO_P1DR : case H8S_IO_P2DR : case H8S_IO_P3DR : case H8S_IO_P5DR : case H8S_IO_P6DR :
		case H8S_IO_PADR : case H8S_IO_PBDR : case H8S_IO_PCDR : case H8S_IO_PDDR : case H8S_IO_PEDR :
        case H8S_IO_PFDR : case H8S_IO_PGDR :
            {
                int port = (offset - H8S_IO_P1DR);
                h8->drs[port] = data;
                h8->io->write_byte(H8_PORT_1 + port, data & h8->ddrs[port]);
            }
            break;

		// ...
        #if 0
        // TPU
		case H8S_IO_TSTR  : h8s_tpu_write_tstr( h8, data); break;
		// DMA
		case H8S_IO_DMABCRL :
		{
			h8->per_regs[offset] = data;
			if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394))
			{
				if ((data & 0x40) && (data & 0x80))
				{
					UINT32 i, dma_src, dma_dst;
					UINT16 dma_cnt, dma_con;
					int sz;
					dma_src = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1AH));
					dma_dst = h8->program->read_dword( H8S_IO_ADDR(H8S_IO_MAR1BH));
					dma_cnt = h8->program->read_word( H8S_IO_ADDR(H8S_IO_ETCR1A));
					dma_con = h8->program->read_word( H8S_IO_ADDR(H8S_IO_DMACR1A));
					sz = (dma_con & 0x8000) ? 2 : 1;
					for (i=0;i<dma_cnt;i++)
					{
						if (dma_con & 0x8000) h8->program->write_word( dma_dst, h8->program->read_word( dma_src)); else h8->program->write_byte( dma_dst, h8->program->read_byte( dma_src));
						if (dma_con & 0x2000) { if (dma_con & 0x4000) dma_src -= sz; else dma_src += sz; }
						if (dma_con & 0x0020) { if (dma_con & 0x0040) dma_dst -= sz; else dma_dst += sz; }
					}
					h8->per_regs[H8S_IO_DMABCRL] &= ~0x40;
				}
			}
		}
		break;
        #endif
		// ...
		default :
		{
            logerror("H8S: Unknown write %02x to I/O %x\n", data, offset);
			h8->per_regs[offset] = data;
		}
		break;
	}
}

void h8s_onchip_reg_write_16(h83xx_state *h8, int offset, UINT16 data)
{
	verboselog( h8->device->machine(), 9, "%08X | %08X <- %04X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	switch (offset)
	{
		// TPU 0
		case H8S_IO_TCNT0_H : h8s_tpu_x_write_tcnt( h8, 0, data); break;
		case H8S_IO_TGR0A_H : h8s_tpu_x_write_tgra( h8, 0, data); break;
		case H8S_IO_TGR0B_H : h8s_tpu_x_write_tgrb( h8, 0, data); break;
		case H8S_IO_TGR0C_H : h8s_tpu_x_write_tgrc( h8, 0, data); break;
		case H8S_IO_TGR0D_H : h8s_tpu_x_write_tgrd( h8, 0, data); break;
		// TPU 1
		case H8S_IO_TCNT1_H : h8s_tpu_x_write_tcnt( h8, 1, data); break;
		case H8S_IO_TGR1A_H : h8s_tpu_x_write_tgra( h8, 1, data); break;
		case H8S_IO_TGR1B_H : h8s_tpu_x_write_tgrb( h8, 1, data); break;
		// TPU 2
		case H8S_IO_TCNT2_H : h8s_tpu_x_write_tcnt( h8, 2, data); break;
		case H8S_IO_TGR2A_H : h8s_tpu_x_write_tgra( h8, 2, data); break;
		case H8S_IO_TGR2B_H : h8s_tpu_x_write_tgrb( h8, 2, data); break;
		// TPU 3
		case H8S_IO_TCNT3_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tcnt( h8, 3, data); break;
		case H8S_IO_TGR3A_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgra( h8, 3, data); break;
		case H8S_IO_TGR3B_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgrb( h8, 3, data); break;
		case H8S_IO_TGR3C_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgrc( h8, 3, data); break;
		case H8S_IO_TGR3D_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgrd( h8, 3, data); break;
		// TPU 4
		case H8S_IO_TCNT4_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tcnt( h8, 4, data); break;
		case H8S_IO_TGR4A_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgra( h8, 4, data); break;
		case H8S_IO_TGR4B_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgrb( h8, 4, data); break;
		// TPU 5
		case H8S_IO_TCNT5_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tcnt( h8, 5, data); break;
		case H8S_IO_TGR5A_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgra( h8, 5, data); break;
		case H8S_IO_TGR5B_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) h8s_tpu_x_write_tgrb( h8, 5, data); break;
		// ...
		default :
		{
			h8s_onchip_reg_write_8( h8, offset + 0, (data >> 8) & 0xFF);
			h8s_onchip_reg_write_8( h8, offset + 1, (data >> 0) & 0xFF);
		}
		break;
	}
}

UINT8 h8s_onchip_reg_read_8(h83xx_state *h8, int offset)
{
	UINT8 data;
	switch (offset)
	{
		// SCI 0
		case H8S_IO_SSR0  : data = H8S_SSR_TDRE | H8S_SSR_TEND; break;
		case H8S_IO_RDR0  : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
		// SCI 1
		case H8S_IO_SSR1  : data = H8S_SSR_TDRE | H8S_SSR_TEND; break;
		// SCI 2
		case H8S_IO_SSR2 :
		{
			data = h8->per_regs[offset];
			if (!(h8->per_regs[H8S_IO_SCR2] & H8S_SCR_TE)) data |= H8S_SSR_TDRE;
		}
		break;
		// ports
		case H8S_IO_PORT1 : case H8S_IO_PORT2 : case H8S_IO_PORT3 : case H8S_IO_PORT4 : case H8S_IO_PORT5 :
		case H8S_IO_PORTA : case H8S_IO_PORTB : case H8S_IO_PORTC : case H8S_IO_PORTD : case H8S_IO_PORTE :
		case H8S_IO_PORTF : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
		// ...
        case H8S_IO_P1DR : case H8S_IO_P2DR : case H8S_IO_P3DR : case H8S_IO_P4DR : case H8S_IO_P5DR :
        case H8S_IO_PADR : case H8S_IO_PBDR : case H8S_IO_PCDR : case H8S_IO_PDDR : case H8S_IO_PEDR :
        case H8S_IO_PFDR : case H8S_IO_PGDR : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
		// TMR 0
		case H8S_IO_TCNT0 : data = h8s_tmr_x_read_tcnt( h8, 0); break;
		// TMR 1
		case H8S_IO_TCNT1 : data = h8s_tmr_x_read_tcnt( h8, 1); break;
		// ...
		// default
		default : data = h8->per_regs[offset]; break;
	}
	verboselog( h8->device->machine(), 9, "%08X | %08X -> %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	return data;
}

UINT8 h8s_onchip_reg_read_8_ddr(h83xx_state *h8, int offset)
{
	UINT8 data;
	switch (offset)
	{
        #if 0
		// SCI 0
		case H8S_IO_SSR0  : data = H8S_SSR_TDRE | H8S_SSR_TEND; break;
		case H8S_IO_RDR0  : data = h8->io->read_byte( H8S_IO_ADDR( offset)); break;
		// SCI 1
		case H8S_IO_SSR1  : data = H8S_SSR_TDRE | H8S_SSR_TEND; break;
		// SCI 2
		case H8S_IO_SSR2 :
		{
			data = h8->per_regs[offset];
			if (!(h8->per_regs[H8S_IO_SCR2] & H8S_SCR_TE)) data |= H8S_SSR_TDRE;
		}
		break;
        #endif

        case H8S_IO_IFR:
            data = h8->per_regs[offset];
            break;

    // ports
        case H8S_IO_PORT1 : case H8S_IO_PORT2 : case H8S_IO_PORT3 : case H8S_IO_PORT4 : case H8S_IO_PORT5 : case H8S_IO_PORT6 :
        case H8S_IO_PORTA : case H8S_IO_PORTB : case H8S_IO_PORTC : case H8S_IO_PORTD : case H8S_IO_PORTE : case H8S_IO_PORTF :
        case H8S_IO_PORTG :
            {
                int port = (offset - H8S_IO_PORT1);
                data = h8->drs[port] & h8->ddrs[port];   // result = data register for DDR "1" bits, live read for DDR "0" bits
                data |= (h8->io->read_byte(H8_PORT_1 + port) & (h8->ddrs[port] ^ 0xff));
            }
            break;

		// the manual is ambivalent, but the invqix code very much implies that reading DR also reads the pin states
        case H8S_IO_P1DR : case H8S_IO_P2DR : case H8S_IO_P3DR : case H8S_IO_P5DR : case H8S_IO_P6DR :
        case H8S_IO_PADR : case H8S_IO_PBDR : case H8S_IO_PCDR : case H8S_IO_PDDR : case H8S_IO_PEDR :
        case H8S_IO_PFDR : case H8S_IO_PGDR :
            {
                int port = (offset - H8S_IO_P1DR);
                data = h8->drs[port] & h8->ddrs[port];
                data |= (h8->io->read_byte(H8_PORT_1 + port) & (h8->ddrs[port] ^ 0xff));
            }
            break;

        #if 0
        // TMR 0
		case H8S_IO_TCNT0 : data = h8s_tmr_x_read_tcnt( h8, 0); break;
		// TMR 1
		case H8S_IO_TCNT1 : data = h8s_tmr_x_read_tcnt( h8, 1); break;
        #endif
		// ...
		// default
		default : data = h8->per_regs[offset]; logerror("H8S: unhandled I/O read at %x\n", offset); break;
	}
	verboselog( h8->device->machine(), 9, "%08X | %08X -> %02X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	return data;
}

UINT16 h8s_onchip_reg_read_16(h83xx_state *h8, int offset)
{
	UINT16 data = 0;
	switch (offset)
	{
		case H8S_IO_TCNT0_H : data = h8s_tpu_x_read_tcnt( h8, 0); break;
		case H8S_IO_TCNT1_H : data = h8s_tpu_x_read_tcnt( h8, 1); break;
		case H8S_IO_TCNT2_H : data = h8s_tpu_x_read_tcnt( h8, 2); break;
		case H8S_IO_TCNT3_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) data = h8s_tpu_x_read_tcnt( h8, 3); break;
		case H8S_IO_TCNT4_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) data = h8s_tpu_x_read_tcnt( h8, 4); break;
		case H8S_IO_TCNT5_H : if ((h8->device->type() == H8S2323) || (h8->device->type() == H8S2394)) data = h8s_tpu_x_read_tcnt( h8, 5); break;
		default :
		{
			UINT8 b[2];
			b[0] = h8s_onchip_reg_read_8( h8, offset + 0);
			b[1] = h8s_onchip_reg_read_8( h8, offset + 1);
			data = (b[0] << 8) | (b[1] << 0);
		}
		break;
	}
	verboselog( h8->device->machine(), 9, "%08X | %08X -> %04X\n", h8->ppc, H8S_IO_ADDR(offset), data);
	return data;
}

void h8s2241_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
{
	h8s_onchip_reg_write_8(h8, offset, data);
}

UINT8 h8s2241_per_regs_read_8(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_8(h8, offset);
}

void h8s2241_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
{
	h8s_onchip_reg_write_16(h8, offset, data);
}

UINT16 h8s2241_per_regs_read_16(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_16(h8, offset);
}

void h8s2246_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
{
	h8s_onchip_reg_write_8(h8, offset, data);
}

UINT8 h8s2246_per_regs_read_8(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_8(h8, offset);
}

void h8s2246_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
{
	h8s_onchip_reg_write_16(h8, offset, data);
}

UINT16 h8s2246_per_regs_read_16(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_16(h8, offset);
}

void h8s2323_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
{
	h8s_onchip_reg_write_8(h8, offset, data);
}

UINT8 h8s2323_per_regs_read_8(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_8(h8, offset);
}

void h8s2323_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
{
	h8s_onchip_reg_write_16(h8, offset, data);
}

UINT16 h8s2323_per_regs_read_16(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_16(h8, offset);
}

void h8s2394_per_regs_write_8(h83xx_state *h8, int offset, UINT8 data)
{
	h8s_onchip_reg_write_8_ddr(h8, offset, data);
}

UINT8 h8s2394_per_regs_read_8(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_8_ddr(h8, offset);
}

void h8s2394_per_regs_write_16(h83xx_state *h8, int offset, UINT16 data)
{
	h8s_onchip_reg_write_16(h8, offset, data);
}

UINT16 h8s2394_per_regs_read_16(h83xx_state *h8, int offset)
{
	return h8s_onchip_reg_read_16(h8, offset);
}