summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/cpu/i86/i186.cpp
blob: c8bd902c2014f1a210b203d37f49be254462008b (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
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
// license:BSD-3-Clause
// copyright-holders:Carl
// Peripheral code from rmnimbus driver by Phill Harvey-Smith which is
// based on the Leland sound driver by Aaron Giles and Paul Leaman

// Note: the X1 input (typically an XTAL) is divided by 2 internally.
// The device clock should therefore be twice the desired operating
// frequency (and twice the speed rating suffixed to the part number).
// Some high-performance AMD versions, however, generate the system
// clock with a PLL and only support XTAL dividers other than 1 for
// compatibility and power-saving modes.

#include "emu.h"
#include "i186.h"
#include "i86inline.h"

#define LOG_PORTS           (1U << 1)
#define LOG_INTERRUPTS      (1U << 2)
#define LOG_INTERRUPTS_EXT  (1U << 3)
#define LOG_TIMER           (1U << 4)
#define LOG_DMA             (1U << 5)
#define LOG_DMA_HIFREQ      (1U << 6)

#define VERBOSE (0)
#include "logmacro.h"

/* external int priority masks */

#define EXTINT_CTRL_PRI_MASK    0x07
#define EXTINT_CTRL_MSK         0x08
#define EXTINT_CTRL_LTM         0x10
#define EXTINT_CTRL_CASCADE     0x20
#define EXTINT_CTRL_SFNM        0x40

/* DMA control register */

#define DEST_MIO                0x8000
#define DEST_DECREMENT          0x4000
#define DEST_INCREMENT          0x2000
#define DEST_NO_CHANGE          (DEST_DECREMENT | DEST_INCREMENT)
#define DEST_INCDEC_MASK        (DEST_DECREMENT | DEST_INCREMENT)
#define SRC_MIO                 0x1000
#define SRC_DECREMENT           0x0800
#define SRC_INCREMENT           0x0400
#define SRC_NO_CHANGE           (SRC_DECREMENT | SRC_INCREMENT)
#define SRC_INCDEC_MASK         (SRC_DECREMENT | SRC_INCREMENT)
#define TERMINATE_ON_ZERO       0x0200
#define INTERRUPT_ON_ZERO       0x0100
#define SYNC_MASK               0x00C0
#define SYNC_SOURCE             0x0040
#define SYNC_DEST               0x0080
#define CHANNEL_PRIORITY        0x0020
#define TIMER_DRQ               0x0010
#define CHG_NOCHG               0x0004
#define ST_STOP                 0x0002
#define BYTE_WORD               0x0001

/* these come from the Intel 80186 datasheet */
const uint8_t i80186_cpu_device::m_i80186_timing[] =
{
	45,28,          /* exception, IRET */
		0, 2, 4, 3, /* INTs */
		2,              /* segment overrides */
		2, 2, 3,        /* flag operations */
		8, 7,19,15, /* arithmetic adjusts */
		4, 4,           /* decimal adjusts */
		2, 4,           /* sign extension */
		2,18, 6, 2, 6,11,   /* misc */

	14,14,14,       /* direct JMPs */
	11,17,26,       /* indirect JMPs */
	15,23,          /* direct CALLs */
	13,19,38,       /* indirect CALLs */
	16,22,18,25,    /* returns */
		4,13, 5,15, /* conditional JMPs */
		6,16, 6,16, /* loops */

	10,10, 8, 8,    /* port reads */
		9, 9, 7, 7, /* port writes */

		2, 9,12,        /* move, 8-bit */
		3,12,           /* move, 8-bit immediate */
		2, 9,12,        /* move, 16-bit */
		4,13,           /* move, 16-bit immediate */
		8, 8, 9, 9, /* move, AL/AX memory */
		2,11, 2,11, /* move, segment registers */
		4,17,           /* exchange, 8-bit */
		4,17, 3,        /* exchange, 16-bit */

	10,16, 9, 9,    /* pushes */
	10,20, 8, 8,    /* pops */

		3,10,10,        /* ALU ops, 8-bit */
		4,16,10,        /* ALU ops, 8-bit immediate */
		3,10,10,        /* ALU ops, 16-bit */
		4,16,10,        /* ALU ops, 16-bit immediate */
		4,16,10,        /* ALU ops, 16-bit w/8-bit immediate */
	26,35,32,41,    /* MUL */
	25,34,31,40,    /* IMUL */
	29,38,35,44,    /* DIV */
	44,53,50,59,    /* IDIV */
		3, 3,15,15, /* INC/DEC */
		3, 3,10,10, /* NEG/NOT */

		2, 5, 1,        /* reg shift/rotate */
	15,17, 1,       /* m8 shift/rotate */
	15,17, 1,       /* m16 shift/rotate */

	22, 5,22,       /* CMPS 8-bit */
	22, 5,22,       /* CMPS 16-bit */
	15, 5,15,       /* SCAS 8-bit */
	15, 5,15,       /* SCAS 16-bit */
	12, 6,11,       /* LODS 8-bit */
	12, 6,11,       /* LODS 16-bit */
	10, 6, 9,       /* STOS 8-bit */
	10, 6, 9,       /* STOS 16-bit */
	14, 8, 8,       /* MOVS 8-bit */
	14, 8, 8,       /* MOVS 16-bit */

	14, 8, 8,       /* (80186) INS 8-bit */
	14, 8, 8,       /* (80186) INS 16-bit */
	14, 8, 8,       /* (80186) OUTS 8-bit */
	14, 8, 8,       /* (80186) OUTS 16-bit */
	14,68,83,       /* (80186) PUSH immediate, PUSHA/POPA */
	22,29,          /* (80186) IMUL immediate 8-bit */
	25,32,          /* (80186) IMUL immediate 16-bit */
	15,25,4,16, 8,  /* (80186) ENTER/LEAVE */
	33,             /* (80186) BOUND */
};

DEFINE_DEVICE_TYPE(I80186, i80186_cpu_device, "i80186", "Intel 80186")
DEFINE_DEVICE_TYPE(I80188, i80188_cpu_device, "i80188", "Intel 80188")
DEFINE_DEVICE_TYPE(AM186EM, am186em_device, "am186em", "AMD Am186EM")
DEFINE_DEVICE_TYPE(AM188EM, am188em_device, "am188em", "AMD Am188EM")

i80186_cpu_device::i80186_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i80186_cpu_device(mconfig, I80186, tag, owner, clock, 16)
{
}

i80188_cpu_device::i80188_cpu_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i80186_cpu_device(mconfig, I80188, tag, owner, clock, 8)
{
}

am186em_device::am186em_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i80186_cpu_device(mconfig, AM186EM, tag, owner, clock, 16)
{
}

am188em_device::am188em_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: i80186_cpu_device(mconfig, AM188EM, tag, owner, clock, 8)
{
}

i80186_cpu_device::i80186_cpu_device(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, uint32_t clock, int data_bus_size)
	: i8086_common_cpu_device(mconfig, type, tag, owner, clock)
	, m_program_config("program", ENDIANNESS_LITTLE, data_bus_size, 20, 0)
	, m_opcodes_config("opcodes", ENDIANNESS_LITTLE, data_bus_size, 20, 0)
	, m_io_config("io", ENDIANNESS_LITTLE, data_bus_size, 16, 0)
	, m_read_slave_ack_func(*this, 0)
	, m_out_chip_select_func(*this)
	, m_out_tmrout0_func(*this)
	, m_out_tmrout1_func(*this)
	, m_irmx_irq_cb(*this)
	, m_irqa_cb(*this)
	, m_irmx_irq_ack(*this)
{
	memcpy(m_timing, m_i80186_timing, sizeof(m_i80186_timing));
	set_irq_acknowledge_callback(*this, FUNC(i80186_cpu_device::inta_callback));
}

device_memory_interface::space_config_vector i80186_cpu_device::memory_space_config() const
{
	if (has_configured_map(AS_OPCODES))
		return space_config_vector {
			std::make_pair(AS_PROGRAM, &m_program_config),
			std::make_pair(AS_OPCODES, &m_opcodes_config),
			std::make_pair(AS_IO,      &m_io_config)
		};
	else
		return space_config_vector {
			std::make_pair(AS_PROGRAM, &m_program_config),
			std::make_pair(AS_IO,      &m_io_config)
		};
}

uint8_t i80186_cpu_device::fetch()
{
	uint8_t data = m_or8(update_pc());
	m_ip++;
	return data;
}

void i80186_cpu_device::execute_run()
{
	while (m_icount > 0)
	{
		if ((m_dma[0].drq_state && (m_dma[0].control & ST_STOP)) || (m_dma[1].drq_state && (m_dma[1].control & ST_STOP)))
		{
			int channel = m_last_dma ? 0 : 1;
			m_last_dma = !m_last_dma;
			if (!(m_dma[1].drq_state && (m_dma[1].control & ST_STOP)))
				channel = 0;
			else if (!(m_dma[0].drq_state && (m_dma[0].control & ST_STOP)))
				channel = 1;
			else if ((m_dma[0].control & CHANNEL_PRIORITY) && !(m_dma[1].control & CHANNEL_PRIORITY))
				channel = 0;
			else if ((m_dma[1].control & CHANNEL_PRIORITY) && !(m_dma[0].control & CHANNEL_PRIORITY))
				channel = 1;
			m_icount--;
			drq_callback(channel);
			continue;
		}
		if (m_seg_prefix_next)
		{
			m_seg_prefix = true;
			m_seg_prefix_next = false;
		}
		else
		{
			m_prev_ip = m_ip;
			m_seg_prefix = false;

			/* Dispatch IRQ */
			if (m_pending_irq && m_no_interrupt == 0)
			{
				if (m_pending_irq & NMI_IRQ)
				{
					interrupt(2);
					m_pending_irq &= ~NMI_IRQ;
					m_halt = false;
				}
				else if (m_IF)
				{
					/* the actual vector is retrieved after pushing flags */
					/* and clearing the IF */
					interrupt(-1);
					m_halt = false;
				}
			}

			if (m_halt)
			{
				debugger_wait_hook();
				m_icount = 0;
				return;
			}

			/* No interrupt allowed between last instruction and this one */
			if (m_no_interrupt)
			{
				m_no_interrupt--;
			}

			/* trap should allow one instruction to be executed */
			if (m_fire_trap)
			{
				if (m_fire_trap >= 2)
				{
					interrupt(1);
					m_fire_trap = 0;
				}
				else
				{
					m_fire_trap++;
				}
			}
		}

		debugger_instruction_hook( update_pc() );

		uint8_t op = fetch_op();

		switch(op)
		{
			case 0x60: // i_pusha
				{
					uint32_t tmp = m_regs.w[SP];

					PUSH(m_regs.w[AX]);
					PUSH(m_regs.w[CX]);
					PUSH(m_regs.w[DX]);
					PUSH(m_regs.w[BX]);
					PUSH(tmp);
					PUSH(m_regs.w[BP]);
					PUSH(m_regs.w[SI]);
					PUSH(m_regs.w[DI]);
					CLK(PUSHA);
				}
				break;

			case 0x61: // i_popa
				m_regs.w[DI] = POP();
				m_regs.w[SI] = POP();
				m_regs.w[BP] = POP();
				POP();
				m_regs.w[BX] = POP();
				m_regs.w[DX] = POP();
				m_regs.w[CX] = POP();
				m_regs.w[AX] = POP();
				CLK(POPA);
				break;

			case 0x62: // i_bound
				{
					m_modrm = fetch();
					uint32_t low = GetRMWord();
					uint32_t high = GetnextRMWord();
					uint32_t tmp = RegWord();
					if (tmp < low || tmp > high)
						interrupt(5);
					CLK(BOUND);
					logerror("%06x: bound %04x high %04x low %04x tmp\n", m_pc, high, low, tmp);
				}
				break;

			case 0x68: // i_push_d16
				PUSH(fetch_word());
				CLK(PUSH_IMM);
				break;

			case 0x69: // i_imul_d16
				{
					DEF_r16w();
					uint32_t tmp = fetch_word();
					m_dst = (int32_t)((int16_t)m_src) * (int32_t)((int16_t)tmp);
					m_CarryVal = m_OverVal = (((int32_t)m_dst) >> 15 != 0) && (((int32_t)m_dst) >> 15 != -1);
					RegWord(m_dst);
					CLKM(IMUL_RRI16, IMUL_RMI16);
				}
				break;

			case 0x6a: // i_push_d8
				PUSH((uint16_t)((int16_t)((int8_t)fetch())));
				CLK(PUSH_IMM);
				break;

			case 0x6b: // i_imul_d8
				{
					DEF_r16w();
					uint32_t src2 = (uint16_t)(int8_t)fetch();
					m_dst = (int32_t)((int16_t)m_src) * (int32_t)((int16_t)src2);
					m_CarryVal = m_OverVal = (((int32_t)m_dst) >> 15 != 0) && (((int32_t)m_dst) >> 15 != -1);
					RegWord(m_dst);
					CLKM(IMUL_RRI8, IMUL_RMI8);
				}
				break;

			case 0x6c: // i_insb
				i_insb();
				break;

			case 0x6d: // i_insw
				i_insw();
				break;

			case 0x6e: // i_outsb
				i_outsb();
				break;

			case 0x6f: // i_outsw
				i_outsw();
				break;

			case 0x8e: // i_mov_sregw
				m_modrm = fetch();
				m_src = GetRMWord();
				CLKM(MOV_SR, MOV_SM);
				switch (m_modrm & 0x38)
				{
				case 0x00:  /* mov es,ew */
					m_sregs[ES] = m_src;
					break;
				case 0x10:  /* mov ss,ew */
					m_sregs[SS] = m_src;
					m_no_interrupt = 1;
					break;
				case 0x18:  /* mov ds,ew */
					m_sregs[DS] = m_src;
					break;
				default:
					logerror("%06x: Mov Sreg - Invalid register\n", m_pc);
					m_ip = m_prev_ip;
					interrupt(6);
					break;
				}
				break;

			case 0xc0: // i_rotshft_bd8
				{
					m_modrm = fetch();
					m_src = GetRMByte();
					m_dst = m_src;
					uint8_t c = fetch() & 0x1f;
					CLKM(ROT_REG_BASE, ROT_M8_BASE);
					m_icount -= m_timing[ROT_REG_BIT] * c;
					if (c)
					{
						switch (m_modrm & 0x38)
						{
						case 0x00: do { ROL_BYTE();  c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x08: do { ROR_BYTE();  c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x10: do { ROLC_BYTE(); c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x18: do { RORC_BYTE(); c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x30:
						case 0x20: SHL_BYTE(c); break;
						case 0x28: SHR_BYTE(c); break;
						case 0x38: SHRA_BYTE(c); break;
						}
					}
				}
				break;

			case 0xc1: // i_rotshft_wd8
				{
					m_modrm = fetch();
					m_src = GetRMWord();
					m_dst = m_src;
					uint8_t c = fetch() & 0x1f;
					CLKM(ROT_REG_BASE, ROT_M16_BASE);
					m_icount -= m_timing[ROT_REG_BIT] * c;
					if (c)
					{
						switch (m_modrm & 0x38)
						{
						case 0x00: do { ROL_WORD();  c--; } while (c > 0); PutbackRMWord(m_dst); break;
						case 0x08: do { ROR_WORD();  c--; } while (c > 0); PutbackRMWord(m_dst); break;
						case 0x10: do { ROLC_WORD(); c--; } while (c > 0); PutbackRMWord(m_dst); break;
						case 0x18: do { RORC_WORD(); c--; } while (c > 0); PutbackRMWord(m_dst); break;
						case 0x30:
						case 0x20: SHL_WORD(c); break;
						case 0x28: SHR_WORD(c); break;
						case 0x38: SHRA_WORD(c); break;
						}
					}
				}
				break;

			case 0xc8: // i_enter
				{
					uint16_t nb = fetch();
					nb |= fetch() << 8;

					uint32_t level = fetch();
					CLK(!level ? ENTER0 : (level == 1) ? ENTER1 : ENTER_BASE);
					if (level > 1)
						m_icount -= level * m_timing[ENTER_COUNT];
					PUSH(m_regs.w[BP]);
					m_regs.w[BP] = m_regs.w[SP];
					m_regs.w[SP] -= nb;
					for (int i = 1; i < level; i++)
					{
						PUSH(GetMemW(SS, m_regs.w[BP] - i * 2));
					}
					if (level)
					{
						PUSH(m_regs.w[BP]);
					}
				}
				break;

			case 0xc9: // i_leave
				m_regs.w[SP] = m_regs.w[BP];
				m_regs.w[BP] = POP();
				CLK(LEAVE);
				break;

			case 0xd2: // i_rotshft_bcl
				{
					m_modrm = fetch();
					m_src = GetRMByte();
					m_dst = m_src;
					uint8_t c = m_regs.b[CL] & 0x1f;
					CLKM(ROT_REG_BASE, ROT_M16_BASE);
					m_icount -= m_timing[ROT_REG_BIT] * c;
					if (c)
					{
						switch (m_modrm & 0x38)
						{
						case 0x00: do { ROL_BYTE();  c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x08: do { ROR_BYTE();  c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x10: do { ROLC_BYTE(); c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x18: do { RORC_BYTE(); c--; } while (c > 0); PutbackRMByte(m_dst); break;
						case 0x30:
						case 0x20: SHL_BYTE(c); break;
						case 0x28: SHR_BYTE(c); break;
						case 0x38: SHRA_BYTE(c); break;
						}
					}
				}
				break;

			case 0xd3: // i_rotshft_wcl
				{
					m_modrm = fetch();
					m_src = GetRMWord();
					m_dst = m_src;
					uint8_t c = m_regs.b[CL] & 0x1f;
					CLKM(ROT_REG_BASE, ROT_M16_BASE);
					m_icount -= m_timing[ROT_REG_BIT] * c;
					if (c)
					{
						switch (m_modrm & 0x38)
						{
							case 0x00: do { ROL_WORD();  c--; } while (c > 0); PutbackRMWord(m_dst); break;
							case 0x08: do { ROR_WORD();  c--; } while (c > 0); PutbackRMWord(m_dst); break;
							case 0x10: do { ROLC_WORD(); c--; } while (c > 0); PutbackRMWord(m_dst); break;
							case 0x18: do { RORC_WORD(); c--; } while (c > 0); PutbackRMWord(m_dst); break;
							case 0x30:
							case 0x20: SHL_WORD(c); break;
							case 0x28: SHR_WORD(c); break;
							case 0x38: SHRA_WORD(c); break;
						}
					}
				}
				break;

			case 0xd8: // i_esc
			case 0xd9:
			case 0xda:
			case 0xdb:
			case 0xdc:
			case 0xdd:
			case 0xde:
			case 0xdf:
				if (m_reloc & 0x8000)
				{
					m_ip = m_prev_ip;
					interrupt(7);
					break;
				}
				m_modrm = fetch();
				GetRMByte();
				CLK(NOP);
				// The 80187 has the FSTSW AX instruction
				if ((m_modrm == 0xe0) && (op == 0xdf))
					m_regs.w[AX] = 0xffff;  // FPU not present
				break;

			case 0xe6: // i_outal
				write_port_byte_al(fetch());
				CLK(OUT_IMM8);
				break;

			case 0xee: // i_outdxal
				write_port_byte_al(m_regs.w[DX]);
				CLK(OUT_DX8);
				break;

			case 0xf2: // i_repne
			case 0xf3:
				{
					bool pass = false;
					uint8_t next = repx_op();
					uint16_t c = m_regs.w[CX];

					switch (next)
					{
					case 0x6c:
						CLK(OVERRIDE);
						if (c)
						{
							do
							{
								i_insb();
								c--;
							} while (c > 0 && m_icount > 0);
						}
						m_regs.w[CX] = c;
						m_seg_prefix = false;
						m_seg_prefix_next = false;
						break;
					case 0x6d:
						CLK(OVERRIDE);
						if (c)
						{
							do
							{
								i_insw();
								c--;
							} while (c > 0 && m_icount > 0);
						}
						m_regs.w[CX] = c;
						m_seg_prefix = false;
						m_seg_prefix_next = false;
						break;
					case 0x6e:
						CLK(OVERRIDE);
						if (c)
						{
							do
							{
								i_outsb();
								c--;
							} while (c > 0 && m_icount > 0);
						}
						m_regs.w[CX] = c;
						m_seg_prefix = false;
						m_seg_prefix_next = false;
						break;
					case 0x6f:
						CLK(OVERRIDE);
						if (c)
						{
							do
							{
								i_outsw();
								c--;
							} while (c > 0 && m_icount > 0);
						}
						m_regs.w[CX] = c;
						m_seg_prefix = false;
						m_seg_prefix_next = false;
						break;
					default:
						// Decrement IP and pass on
						m_ip -= 1 + (m_seg_prefix_next ? 1 : 0);
						pass = true;
						break;
					}
					if (!pass)
					{
						if (c)
							m_ip = m_prev_ip;
						break;
					}
				}
				[[fallthrough]];
			default:
				if (!common_op(op))
				{
					m_icount -= 10; // UD fault timing?
					logerror("%06x: Invalid Opcode %02x\n", m_pc, op);
					m_ip = m_prev_ip;
					interrupt(6); // 80186 has #UD
					break;
				}
		}
	}
}

void i80186_cpu_device::device_start()
{
	i8086_common_cpu_device::device_start();
	state_add( I8086_ES, "ES", m_sregs[ES] ).formatstr("%04X");
	state_add( I8086_CS, "CS", m_sregs[CS] ).callimport().formatstr("%04X");
	state_add( I8086_SS, "SS", m_sregs[SS] ).formatstr("%04X");
	state_add( I8086_DS, "DS", m_sregs[DS] ).formatstr("%04X");
	state_add( I8086_VECTOR, "V", m_int_vector).formatstr("%02X");

	state_add( I8086_PC, "PC", m_pc ).callimport().formatstr("%05X");
	state_add<uint32_t>( STATE_GENPCBASE, "CURPC", [this] { return (m_sregs[CS] << 4) + m_prev_ip; }).mask(0xfffff).noshow();
	state_add( I8086_HALT, "HALT", m_halt ).mask(1);

	// Most of these mnemonics are borrowed from the Intel 80C186EA/80C188EA User's Manual.
	// (The 80C186EA/80C188EA's peripheral block is mapped incompatibly but mostly functionally analogous.)
	state_add( I80186_RELREG, "RELREG", m_reloc ).formatstr("%04X");
	state_add( I80186_UMCS, "UMCS", m_mem.upper ).formatstr("%04X");
	state_add( I80186_LMCS, "LMCS", m_mem.lower ).formatstr("%04X");
	state_add( I80186_PACS, "PACS", m_mem.peripheral ).formatstr("%04X");
	state_add( I80186_MMCS, "MMCS", m_mem.middle ).formatstr("%04X");
	state_add( I80186_MPCS, "MPCS", m_mem.middle_size ).formatstr("%04X");
	state_add( I80186_DxSRC + 0, "D0SRC", m_dma[0].source ).formatstr("%05X").mask(0xfffff);
	state_add( I80186_DxDST + 0, "D0DST", m_dma[0].dest ).formatstr("%05X").mask(0xfffff);
	state_add( I80186_DxTC + 0, "D0TC", m_dma[0].count ).formatstr("%04X");
	state_add( I80186_DxCON + 0, "D0CON", m_dma[0].control ).formatstr("%04X");
	state_add( I80186_DxSRC + 1, "D1SRC", m_dma[1].source ).formatstr("%05X").mask(0xfffff);
	state_add( I80186_DxDST + 1, "D1DST", m_dma[1].dest ).formatstr("%05X").mask(0xfffff);
	state_add( I80186_DxTC + 1, "D1TC", m_dma[1].count ).formatstr("%04X");
	state_add( I80186_DxCON + 1, "D1CON", m_dma[1].control ).formatstr("%04X");
	state_add( I80186_TxCNT + 0, "T0CNT", m_timer[0].count ).formatstr("%04X");
	state_add( I80186_TxCMPA + 0, "T0CMPA", m_timer[0].maxA ).formatstr("%04X");
	state_add( I80186_TxCMPB + 0, "T0CMPB", m_timer[0].maxB ).formatstr("%04X");
	state_add( I80186_TxCON + 0, "T0CON", m_timer[0].control ).formatstr("%04X");
	state_add( I80186_TxCNT + 1, "T1CNT", m_timer[1].count ).formatstr("%04X");
	state_add( I80186_TxCMPA + 1, "T1CMPA", m_timer[1].maxA ).formatstr("%04X");
	state_add( I80186_TxCMPB + 1, "T1CMPB", m_timer[1].maxB ).formatstr("%04X");
	state_add( I80186_TxCON + 1, "T1CON", m_timer[1].control ).formatstr("%04X");
	state_add( I80186_TxCNT + 2, "T2CNT", m_timer[2].count ).formatstr("%04X");
	state_add( I80186_TxCMPA + 2, "T2CMPA", m_timer[2].maxA ).formatstr("%04X");
	state_add( I80186_TxCON + 2, "T2CON", m_timer[2].control ).formatstr("%04X");
	state_add( I80186_INSERV, "INSERV", m_intr.in_service ).formatstr("%04X");
	state_add( I80186_REQST, "REQST", m_intr.request ).formatstr("%04X");
	state_add( I80186_PRIMSK, "PRIMSK", m_intr.priority_mask ).formatstr("%04X");
	state_add( I80186_INTSTS, "INTSTS", m_intr.status ).formatstr("%04X");
	state_add( I80186_TCUCON, "TCUCON", m_intr.timer[0] ).formatstr("%04X");
	state_add( I80186_DMA0CON, "DMA0CON", m_intr.dma[0] ).formatstr("%04X");
	state_add( I80186_DMA1CON, "DMA1CON", m_intr.dma[1] ).formatstr("%04X");
	state_add( I80186_I0CON, "I0CON", m_intr.ext[0] ).formatstr("%04X");
	state_add( I80186_I1CON, "I1CON", m_intr.ext[1] ).formatstr("%04X");
	state_add( I80186_I2CON, "I2CON", m_intr.ext[2] ).formatstr("%04X");
	state_add( I80186_I3CON, "I3CON", m_intr.ext[3] ).formatstr("%04X");
	state_add( I80186_POLLSTS, "POLLSTS", m_intr.poll_status ).formatstr("%04X");

	// register for savestates
	save_item(STRUCT_MEMBER(m_timer, control));
	save_item(STRUCT_MEMBER(m_timer, maxA));
	save_item(STRUCT_MEMBER(m_timer, maxB));
	save_item(STRUCT_MEMBER(m_timer, count));
	save_item(STRUCT_MEMBER(m_dma, source));
	save_item(STRUCT_MEMBER(m_dma, dest));
	save_item(STRUCT_MEMBER(m_dma, count));
	save_item(STRUCT_MEMBER(m_dma, control));
	save_item(NAME(m_intr.vector));
	save_item(NAME(m_intr.pending));
	save_item(NAME(m_intr.ack_mask));
	save_item(NAME(m_intr.priority_mask));
	save_item(NAME(m_intr.in_service));
	save_item(NAME(m_intr.request));
	save_item(NAME(m_intr.status));
	save_item(NAME(m_intr.poll_status));
	save_item(NAME(m_intr.timer));
	save_item(NAME(m_intr.dma));
	save_item(NAME(m_intr.ext));
	save_item(NAME(m_intr.ext_state));
	save_item(NAME(m_mem.lower));
	save_item(NAME(m_mem.upper));
	save_item(NAME(m_mem.middle));
	save_item(NAME(m_mem.middle_size));
	save_item(NAME(m_mem.peripheral));
	save_item(NAME(m_reloc));
	save_item(NAME(m_last_dma));

	// zerofill
	memset(m_timer, 0, sizeof(m_timer));
	memset(m_dma, 0, sizeof(m_dma));
	memset(&m_intr, 0, sizeof(intr_state));
	memset(&m_mem, 0, sizeof(mem_state));
	m_reloc = 0;
	m_last_dma = 0;

	m_timer[0].int_timer = timer_alloc(FUNC(i80186_cpu_device::timer_elapsed), this);
	m_timer[1].int_timer = timer_alloc(FUNC(i80186_cpu_device::timer_elapsed), this);
	m_timer[2].int_timer = timer_alloc(FUNC(i80186_cpu_device::timer_elapsed), this);

	m_irmx_irq_ack.resolve_safe(0);
}

void i80186_cpu_device::device_reset()
{
	i8086_common_cpu_device::device_reset();
	/* reset the interrupt state */
	m_intr.priority_mask    = 0x0007;
	m_intr.timer[0]         = 0x000f;
	m_intr.timer[1]         = 0x000f;
	m_intr.timer[2]         = 0x000f;
	m_intr.dma[0]           = 0x000f;
	m_intr.dma[1]           = 0x000f;
	m_intr.ext[0]           = 0x000f;
	m_intr.ext[1]           = 0x000f;
	m_intr.ext[2]           = 0x000f;
	m_intr.ext[3]           = 0x000f;
	m_intr.in_service       = 0x0000;

	m_intr.pending          = 0x0000;
	m_intr.ack_mask         = 0x0000;
	m_intr.request          = 0x0000;
	m_intr.status           = 0x0000;
	m_intr.poll_status      = 0x0000;
	m_intr.ext_state        = 0x00;

	m_reloc = 0x20ff;
	m_mem.upper = 0xfffb;

	for (dma_state &elem : m_dma)
	{
		elem.drq_state = false;
		elem.control = 0;
	}

	for (timer_state &elem : m_timer)
	{
		elem.control = 0;
		elem.maxA = 0;
		elem.maxB = 0;
		elem.count = 0;
	}
}

uint8_t i80186_cpu_device::read_port_byte(uint16_t port)
{
	if (!(m_reloc & 0x1000) && (port >> 8) == (m_reloc & 0xff))
	{
		if (port & 1)
			return internal_port_r((port >> 1) & 0x7f, 0xff00) >> 8;
		else
			return internal_port_r((port >> 1) & 0x7f, 0x00ff) & 0xff;
	}
	return m_io->read_byte(port);
}

uint16_t i80186_cpu_device::read_port_word(uint16_t port)
{
	if (!(m_reloc & 0x1000) && (port >> 8) == (m_reloc & 0xff))
	{
		// Unaligned reads from the internal bus are swapped rather than split
		if (port & 1)
			return swapendian_int16(internal_port_r((port >> 1) & 0x7f));
		else
			return internal_port_r((port >> 1) & 0x7f);
	}
	return m_io->read_word_unaligned(port);
}

void i80186_cpu_device::write_port_byte(uint16_t port, uint8_t data)
{
	if (!(m_reloc & 0x1000) && (port >> 8) == (m_reloc & 0xff))
	{
		if (port & 1)
			internal_port_w((port >> 1) & 0x7f, data << 8);
		else
			internal_port_w((port >> 1) & 0x7f, data);
	}
	else
		m_io->write_byte(port, data);
}

void i80186_cpu_device::write_port_byte_al(uint16_t port)
{
	if (!(m_reloc & 0x1000) && (port >> 8) == (m_reloc & 0xff))
	{
		// Both AH and AL are written onto the internal bus
		if (port & 1)
			internal_port_w((port >> 1) & 0x7f, swapendian_int16(m_regs.w[AX]));
		else
			internal_port_w((port >> 1) & 0x7f, m_regs.w[AX]);
	}
	else
		m_io->write_byte(port, m_regs.w[AL]);
}

void i80186_cpu_device::write_port_word(uint16_t port, uint16_t data)
{
	if (!(m_reloc & 0x1000) && (port >> 8) == (m_reloc & 0xff))
	{
		// Unaligned writes to the internal bus are swapped rather than split
		if (port & 1)
			internal_port_w((port >> 1) & 0x7f, swapendian_int16(data));
		else
			internal_port_w((port >> 1) & 0x7f, data);
	}
	else
		m_io->write_word_unaligned(port, data);
}

uint8_t i80186_cpu_device::read_byte(uint32_t addr)
{
	if ((m_reloc & 0x1000) && (addr >> 8) == (m_reloc & 0xfff))
	{
		uint16_t ret = internal_port_r((addr >> 1) & 0x7f, (addr & 1) ? 0xff00 : 0x00ff);
		return (addr & 1) ? (ret >> 8) : (ret & 0xff);
	}
	return m_program->read_byte(addr);
}

uint16_t i80186_cpu_device::read_word(uint32_t addr)
{
	if ((m_reloc & 0x1000) && (addr >> 8) == (m_reloc & 0xfff))
	{
		// Unaligned reads from the internal bus are swapped rather than split
		if (addr & 1)
			return swapendian_int16(internal_port_r((addr >> 1) & 0x7f));
		else
			return internal_port_r((addr >> 1) & 0x7f);
	}
	return m_program->read_word_unaligned(addr);
}

void i80186_cpu_device::write_byte(uint32_t addr, uint8_t data)
{
	if ((m_reloc & 0x1000) && (addr >> 8) == (m_reloc & 0xfff))
		internal_port_w((addr >> 1) & 0x7f, (addr & 1) ? (data << 8) : data);
	else
		m_program->write_byte(addr, data);
}

void i80186_cpu_device::write_word(uint32_t addr, uint16_t data)
{
	if ((m_reloc & 0x1000) && (addr >> 8) == (m_reloc & 0xfff))
	{
		// Unaligned writes from the internal bus are swapped rather than split
		if (addr & 1)
			internal_port_w((addr >> 1) & 0x7f, swapendian_int16(data));
		else
			internal_port_w((addr >> 1) & 0x7f, data);
	}
	else
		m_program->write_word_unaligned(addr, data);
}

/*************************************
 *
 *  80186 interrupt controller
 *
 *************************************/
IRQ_CALLBACK_MEMBER(i80186_cpu_device::inta_callback)
{
	// s-state 0 is irqack
	m_irqa_cb(ASSERT_LINE);
	m_irqa_cb(CLEAR_LINE);
	if (BIT(m_reloc, 14))
		return m_irmx_irq_ack(device, irqline);
	return int_callback(device, irqline);
}

IRQ_CALLBACK_MEMBER(i80186_cpu_device::int_callback)
{
	LOGMASKED(LOG_INTERRUPTS, "(%f) **** Acknowledged interrupt vector %02X\n", machine().time().as_double(), m_intr.poll_status & 0x1f);

	/* clear the interrupt */
	set_input_line(0, CLEAR_LINE);
	m_intr.pending = 0;

	uint16_t oldreq = m_intr.request;

	/* clear the request and set the in-service bit */
	if (m_intr.ack_mask & 0xf0)
	{
		int i;
		for (i = 0; i < 4; i++)
			if (BIT(m_intr.ack_mask, i + 4))
				break;
		if (!(m_intr.ext[i] & EXTINT_CTRL_LTM))
			m_intr.request &= ~m_intr.ack_mask;
	}
	else
		m_intr.request &= ~m_intr.ack_mask;

	if (m_intr.request != oldreq)
		LOGMASKED(LOG_INTERRUPTS, "intr.request changed from %02X to %02X\n", oldreq, m_intr.request);

	uint16_t old = m_intr.in_service;

	m_intr.in_service |= m_intr.ack_mask;

	if (m_intr.in_service != old)
		LOGMASKED(LOG_INTERRUPTS, "intr.in_service changed from %02X to %02X\n",old,m_intr.in_service);

	uint8_t vector;
	if (!BIT(m_reloc, 14))
	{
		if (m_intr.ack_mask == 0x0001)
		{
			switch (m_intr.poll_status & 0x1f)
			{
				case 0x08:  m_intr.status &= ~0x01; break;
				case 0x12:  m_intr.status &= ~0x02; break;
				case 0x13:  m_intr.status &= ~0x04; break;
			}
		}

		/* return the vector */
		switch(m_intr.poll_status & 0x1F)
		{
			case 0x0C: vector = (m_intr.ext[0] & EXTINT_CTRL_CASCADE) ? m_read_slave_ack_func(0) : (m_intr.poll_status & 0x1f); break;
			case 0x0D: vector = (m_intr.ext[1] & EXTINT_CTRL_CASCADE) ? m_read_slave_ack_func(1) : (m_intr.poll_status & 0x1f); break;
			default:
				vector = m_intr.poll_status & 0x1f; break;
		}
	}
	else
	{
		if (m_intr.ack_mask & 0x31)
		{
			if (m_intr.ack_mask & 1)
				m_intr.status &= ~0x01;
			else if (m_intr.ack_mask & 0x10)
				m_intr.status &= ~0x02;
			else if (m_intr.ack_mask & 0x20)
				m_intr.status &= ~0x04;
		}
		vector = m_intr.poll_status & 0xff;
	}
	m_intr.ack_mask = 0;

	/* a request no longer pending */
	m_intr.poll_status &= ~0x8000;

	LOGMASKED(LOG_INTERRUPTS, "intr.ext[0]=%04X intr.ext[1]=%04X\n", m_intr.ext[0], m_intr.ext[1]);
	LOGMASKED(LOG_INTERRUPTS, "Int %02X Calling vector %02X\n", m_intr.poll_status, vector);

	return vector;
}


void i80186_cpu_device::update_interrupt_state()
{
	int new_vector = 0;

	LOGMASKED(LOG_INTERRUPTS, "update_interrupt_status: req=%04X stat=%04X serv=%04X priority_mask=%4X\n", m_intr.request, m_intr.status, m_intr.in_service, m_intr.priority_mask);

	/* loop over priorities */
	for (int priority = 0; priority <= m_intr.priority_mask; priority++)
	{
		/* note: by checking 4 bits, we also verify that the mask is off */
		if (BIT(m_reloc, 14))
		{
			for (int int_num = 0; int_num < 3; int_num++)
			{
				if ((m_intr.timer[int_num] & 0x0f) == priority)
				{
					int irq = (1 << int_num);
					/* if we're already servicing something at this level, don't generate anything new */
					if (m_intr.in_service & 0x01)
						return;

					/* if there's something pending, generate an interrupt */
					if (m_intr.status & irq)
					{
						new_vector = m_intr.vector | priority;
						/* set the clear mask and generate the int */
						m_intr.ack_mask = int_num ? (8 << int_num) : 1;
						goto generate_int;
					}
				}
			}
		}
		else
		{
			if ((m_intr.timer[0] & 0x0f) == priority)
			{
				/* if we're already servicing something at this level, don't generate anything new */
				if (m_intr.in_service & 0x01)
					return;

				/* if there's something pending, generate an interrupt */
				if (m_intr.status & 0x07)
				{
					if (m_intr.status & 1)
						new_vector = 0x08;
					else if (m_intr.status & 2)
						new_vector = 0x12;
					else if (m_intr.status & 4)
						new_vector = 0x13;
					else
						logerror("Invalid timer interrupt!\n");

					/* set the clear mask and generate the int */
					m_intr.ack_mask = 0x0001;
					goto generate_int;
				}
			}
		}

		/* check DMA interrupts */
		for (int int_num = 0; int_num < 2; int_num++)
			if ((m_intr.dma[int_num] & 0x0F) == priority)
			{
				/* if we're already servicing something at this level, don't generate anything new */
				if (m_intr.in_service & (0x04 << int_num))
					return;

				/* if there's something pending, generate an interrupt */
				if (m_intr.request & (0x04 << int_num))
				{
					if (BIT(m_reloc, 14))
						new_vector = m_intr.vector | priority;
					else
						new_vector = 0x0a + int_num;

					/* set the clear mask and generate the int */
					m_intr.ack_mask = 0x0004 << int_num;
					goto generate_int;
				}
			}

		if (BIT(m_reloc, 14))
			continue;

		/* check external interrupts */
		for (int int_num = 0; int_num < 4; int_num++)
		{
			if ((m_intr.ext[int_num] & 0x0f) == priority)
			{
				LOGMASKED(LOG_INTERRUPTS, "Int%d priority=%d\n", int_num, priority);

				/* if we're already servicing something at this level, don't generate anything new */
				if ((m_intr.in_service & (0x10 << int_num)) && !(m_intr.ext[int_num] & EXTINT_CTRL_SFNM))
					return;

				/* if there's something pending, generate an interrupt */
				if (m_intr.request & (0x10 << int_num))
				{
					if((int_num >= 2) && (m_intr.ext[int_num - 2] & EXTINT_CTRL_CASCADE))
					{
						logerror("i186: %06x: irq %d use when set for cascade mode\n", m_pc, int_num);
						m_intr.request &= ~(0x10 << int_num);
						continue;
					}
					/* otherwise, generate an interrupt for this request */
					new_vector = 0x0c + int_num;

					/* set the clear mask and generate the int */
					m_intr.ack_mask = 0x0010 << int_num;
					goto generate_int;
				}
				else if ((m_intr.in_service & (0x10 << int_num)) && (m_intr.ext[int_num] & EXTINT_CTRL_SFNM))
					return; // if an irq is in service and sfnm is enabled, stop here
			}
		}
	}
	m_intr.pending = 0;
	if (!BIT(m_reloc, 14))
		set_input_line(0, CLEAR_LINE);
	else
		m_irmx_irq_cb(CLEAR_LINE);
	return;

generate_int:
	/* generate the appropriate interrupt */
	m_intr.poll_status = 0x8000 | new_vector;
	if (!m_intr.pending)
	{
		if (!BIT(m_reloc, 14))
			set_input_line(0, ASSERT_LINE);
		else
			m_irmx_irq_cb(ASSERT_LINE);
	}
	m_intr.pending = 1;
	LOGMASKED(LOG_INTERRUPTS, "(%f) **** Requesting interrupt vector %02X\n", machine().time().as_double(), new_vector);
}


void i80186_cpu_device::handle_eoi(int data)
{
	bool handled = false;

	/* specific case */
	if (!(data & 0x8000))
	{
		/* turn off the appropriate in-service bit */
		switch (data & 0x1f)
		{
			case 0x08:  m_intr.in_service &= ~0x01; break;
			case 0x12:  m_intr.in_service &= ~0x01; break;
			case 0x13:  m_intr.in_service &= ~0x01; break;
			case 0x0a:  m_intr.in_service &= ~0x04; break;
			case 0x0b:  m_intr.in_service &= ~0x08; break;
			case 0x0c:  m_intr.in_service &= ~0x10; break;
			case 0x0d:  m_intr.in_service &= ~0x20; break;
			case 0x0e:  m_intr.in_service &= ~0x40; break;
			case 0x0f:  m_intr.in_service &= ~0x80; break;
			default:    logerror("%05X:ERROR - 80186 EOI with unknown vector %02X\n", m_pc, data & 0x1f);
		}
		LOGMASKED(LOG_INTERRUPTS, "(%f) **** Got EOI for vector %02X\n", machine().time().as_double(), data & 0x1f);
	}
	/* non-specific case */
	else
	{
		/* loop over priorities */
		for (int priority = 0; priority <= 7 && !handled; priority++)
		{
			/* check for in-service timers */
			if (BIT(m_reloc, 14))
			{
				for (int int_num = 0; int_num < 2 && !handled; int_num++)
				{
					int mask = int_num ? (8 << int_num) : 1;
					if ((m_intr.timer[int_num] & 0x07) == priority && (m_intr.in_service & mask))
					{
						m_intr.in_service &= ~mask;
						LOGMASKED(LOG_INTERRUPTS, "(%f) **** Got EOI for timer%d\n", machine().time().as_double(), int_num);
						handled = true;
					}
				}
			}
			else
			{
				if ((m_intr.timer[0] & 0x07) == priority && (m_intr.in_service & 0x01))
				{
					m_intr.in_service &= ~0x01;
					LOGMASKED(LOG_INTERRUPTS, "(%f) **** Got EOI for timer\n", machine().time().as_double());
					handled = true;
				}
			}

			/* check for in-service DMA interrupts */
			for (int int_num = 0; int_num < 2 && !handled; int_num++)
				if ((m_intr.dma[int_num] & 0x07) == priority && (m_intr.in_service & (0x04 << int_num)))
				{
					m_intr.in_service &= ~(0x04 << int_num);
					LOGMASKED(LOG_INTERRUPTS, "(%f) **** Got EOI for DMA%d\n", machine().time().as_double(), int_num);
					handled = true;
				}

			if (BIT(m_reloc, 14))
				continue;

			/* check external interrupts */
			for (int int_num = 0; int_num < 4 && !handled; int_num++)
				if ((m_intr.ext[int_num] & 0x07) == priority && (m_intr.in_service & (0x10 << int_num)))
				{
					m_intr.in_service &= ~(0x10 << int_num);
					LOGMASKED(LOG_INTERRUPTS, "(%f) **** Got EOI for INT%d\n", machine().time().as_double(), int_num);
					handled = true;
				}
		}
	}
	update_interrupt_state();
}

/* Trigger an external interrupt, optionally supplying the vector to take */
void i80186_cpu_device::external_int(uint16_t int_num, int state)
{
	if (BIT(m_reloc, 14))
	{
		if (!int_num)
		{
			set_input_line(0, state);
			return;
		}
		logerror("irq to line %d in irmx mode\n", int_num);
		return;
	}

	if (!(m_intr.ext_state & (1 << int_num)) == !state)
		return;

	LOGMASKED(LOG_INTERRUPTS_EXT, "generating external int %02X\n", int_num);

	if (!state)
	{
		m_intr.request &= ~(0x10 << int_num);
		m_intr.ack_mask &= ~(0x10 << int_num);
		m_intr.ext_state &= ~(1 << int_num);
	}
	else // Turn on the requested request bit and handle interrupt
	{
		m_intr.request |= 0x10 << int_num;
		m_intr.ext_state |= 1 << int_num;
	}
	update_interrupt_state();
}

/*************************************
 *
 *  80186 internal timers
 *
 *************************************/

TIMER_CALLBACK_MEMBER(i80186_cpu_device::timer_elapsed)
{
	int which = param;
	timer_state *t = &m_timer[which];

	LOGMASKED(LOG_TIMER, "Hit interrupt callback for timer %d\n", which);

	/* set the max count bit */
	t->control |= 0x0020;

	/* request an interrupt */
	if (t->control & 0x2000)
	{
		m_intr.status |= 0x01 << which;
		update_interrupt_state();
		LOGMASKED(LOG_TIMER, "  Generating timer interrupt\n");
	}

	if (which == 2)
	{
		if ((m_dma[0].control & (TIMER_DRQ | ST_STOP)) == (TIMER_DRQ | ST_STOP))
			drq_callback(0);
		if ((m_dma[1].control & (TIMER_DRQ | ST_STOP)) == (TIMER_DRQ | ST_STOP))
			drq_callback(1);
		if ((m_timer[0].control & 0x800c) == 0x8008)
			inc_timer(0);
		if ((m_timer[1].control & 0x800c) == 0x8008)
			inc_timer(1);
	}
	else
	{
		if (!(t->control & 2))
		{
			if (which)
				m_out_tmrout1_func(1);
			else
				m_out_tmrout0_func(1);
		}
		else
		{
			if (which)
				m_out_tmrout1_func((t->control & 0x1000) ? 1 : 0);
			else
				m_out_tmrout0_func((t->control & 0x1000) ? 1 : 0);
		}
	}

	/* if we're continuous or altcounting, reset */
	if ((t->control & 1) || ((t->control & 2) && !(t->control & 0x1000)))
	{
		if ((t->control & 2) && !(t->control & 0x1000))
			t->control |= 0x1000;
		else
			t->control &= ~0x1000;

		restart_timer(which);
		LOGMASKED(LOG_TIMER, "  Repriming interrupt\n");
	}
	else
	{
		t->int_timer->adjust(attotime::never, which);
		t->control &= ~0x9000;
	}
	t->count = 0;
}


void i80186_cpu_device::restart_timer(int which)
{
	timer_state *t = &m_timer[which];
	/* Only run timer 0,1 when not incremented via timer 2 pre-scaler */
	if (which != 2 && (t->control & 0x800c) == 0x8008)
		return;

	int count = (t->control & 0x1000) ? t->maxB : t->maxA;
	if (!(t->control & 4))
		t->int_timer->adjust(cycles_to_attotime(4 * (count ? count : 0x10000)), which);
}

void i80186_cpu_device::internal_timer_sync(int which)
{
	timer_state *t = &m_timer[which];

	/* if we have a timing timer running, adjust the count */
	if ((t->control & 0x8000) && !(t->control & 0x0c) && t->int_timer->enabled())
		t->count = ((t->control & 0x1000) ? t->maxB : t->maxA) - attotime_to_cycles(t->int_timer->remaining()) / 4;
}

void i80186_cpu_device::inc_timer(int which)
{
	timer_state *t = &m_timer[which];

	t->count++;
	if (t->control & 2)
	{
		if (t->count == ((t->control & 0x1000) ? t->maxB : t->maxA))
			timer_elapsed(which);
	}
	else if (t->count == t->maxA)
		timer_elapsed(which);
}

void i80186_cpu_device::internal_timer_update(int which, int new_count, int new_maxA, int new_maxB, int new_control)
{
	timer_state *t = &m_timer[which];
	bool update_int_timer = false;

	LOGMASKED(LOG_TIMER, "internal_timer_update: %d, new_count=%d, new_maxA=%d, new_maxB=%d, new_control=%d\n", which, new_count, new_maxA, new_maxB, new_control);

	/* if we have a new count and we're on, update things */
	if (new_count != -1)
	{
		if (t->control & 0x8000)
		{
			internal_timer_sync(which);
			update_int_timer = true;
		}
		t->count = new_count;
	}

	/* if we have a new max and we're on, update things */
	if (new_maxA != -1 && new_maxA != t->maxA)
	{
		if (t->control & 0x8000)
		{
			internal_timer_sync(which);
			update_int_timer = true;
		}

		t->maxA = new_maxA;

		if (new_maxA == 0)
		{
			new_maxA = 0x10000;
		}
	}

	/* if we have a new max and we're on, update things */
	if (new_maxB != -1 && new_maxB != t->maxB)
	{
		if (t->control & 0x8000)
		{
			internal_timer_sync(which);
			update_int_timer = true;
		}

		t->maxB = new_maxB;

		if (new_maxB == 0)
		{
			new_maxB = 0x10000;
		}
	}

	/* handle control changes */
	if (new_control != -1)
	{
		uint16_t resbits = (which == 2) ? 0x1fde : 0x1fc0;

		/* merge back in the bits we don't modify */
		new_control = (new_control & ~resbits) | (t->control & resbits);

		/* handle the /INH bit */
		if (!(new_control & 0x4000))
			new_control = (new_control & ~0x8000) | (t->control & 0x8000);
		new_control &= ~0x4000;

		/* if we have real changes, update things */
		uint16_t diff = new_control ^ t->control;
		if (diff != 0)
		{
			/* if we're going off, make sure our timers are gone */
			if ((diff & 0x8000) && !(new_control & 0x8000))
			{
				/* compute the final count */
				internal_timer_sync(which);
				update_int_timer = true;
			}

			/* if we're going on, start the timers running except with external clock or prescale */
			else if ((diff & 0x8000) && (new_control & 0x8000) && !(new_control & 0xc))
			{
				update_int_timer = true;
			}

			/* if something about the interrupt timer changed, force an update */
			if (!(diff & 0x8000) && (diff & 0x2000))
			{
				internal_timer_sync(which);
				update_int_timer = true;
			}
		}

		/* RIU is cleared whenever ALT = 0 */
		if (!(new_control & 0x0002))
			new_control &= ~0x1000;

		/* set the new control register */
		t->control = new_control;
	}

	/* update the interrupt timer */
	if (update_int_timer)
	{
		if ((t->control & 0x8004) == 0x8000 && (!(t->control & 0x10) || t->int_timer->enabled()))
		{
			int diff = ((t->control & 0x1000) ? t->maxB : t->maxA) - t->count;
			if (diff <= 0)
				diff += 0x10000;
			t->int_timer->adjust(cycles_to_attotime(4 * diff), which);
			LOGMASKED(LOG_TIMER, "Set interrupt timer for %d\n", which);
		}
		else
		{
			t->int_timer->adjust(attotime::never, which);
		}
	}
}

void i80186_cpu_device::external_tmrin(int which, int state)
{
	// TODO: make this an actual edge trigger
	if (state)
	{
		if ((m_timer[which].control & 0x8004) == 0x8004)
			inc_timer(which);
		else if ((m_timer[which].control & 0x8014) == 0x8010)
		{
			m_timer[which].count = 0;
			restart_timer(which);
			LOGMASKED(LOG_TIMER, "Retriggered timer %d\n", which);
		}
	}
}



/*************************************
 *
 *  80186 internal DMA
 *
 *************************************/

void i80186_cpu_device::update_dma_control(int which, int new_control)
{
	dma_state *d = &m_dma[which];

	/* handle the CHG bit */
	if (!(new_control & CHG_NOCHG))
		new_control = (new_control & ~ST_STOP) | (d->control & ST_STOP);
	new_control &= ~CHG_NOCHG;

	LOGMASKED(LOG_DMA, "Initiated DMA %d - count = %04X, source = %04X, dest = %04X\n", which, d->count, d->source, d->dest);

	/* set the new control register */
	d->control = new_control;
}

void i80186_cpu_device::drq_callback(int which)
{
	dma_state *dma = &m_dma[which];

	uint16_t  dma_word;
	uint8_t   dma_byte;
	uint8_t   incdec_size;

	LOGMASKED(LOG_DMA_HIFREQ, "Control=%04X, src=%05X, dest=%05X, count=%04X\n", dma->control, dma->source, dma->dest, dma->count);

	if (!(dma->control & ST_STOP))
	{
		LOGMASKED(LOG_DMA, "%05X:ERROR! - drq%d with dma channel stopped\n", m_pc, which);
		return;
	}

	address_space *dest_space = (dma->control & DEST_MIO) ? m_program : m_io;
	address_space *src_space = (dma->control & SRC_MIO) ? m_program : m_io;

	// Do the transfer, 80188 is incapable of word transfers
	if ((dma->control & BYTE_WORD) && (m_program->data_width() == 16))
	{
		dma_word = src_space->read_word_unaligned(dma->source);
		dest_space->write_word_unaligned(dma->dest, dma_word);
		incdec_size = 2;
	}
	else
	{
		dma_byte = src_space->read_byte(dma->source);
		dest_space->write_byte(dma->dest, dma_byte);
		incdec_size = 1;
	}

	// Increment or Decrement destination and source pointers as needed
	switch (dma->control & DEST_INCDEC_MASK)
	{
		case DEST_DECREMENT:
			dma->dest -= incdec_size;
			break;
		case DEST_INCREMENT:
			dma->dest += incdec_size;
			break;
	}

	switch (dma->control & SRC_INCDEC_MASK)
	{
		case SRC_DECREMENT:
			dma->source -= incdec_size;
			break;
		case SRC_INCREMENT:
			dma->source += incdec_size;
			break;
	}

	// decrement count
	dma->count -= 1;

	// Terminate if count is zero, and terminate flag set
	if (((dma->control & TERMINATE_ON_ZERO) || !(dma->control & SYNC_MASK)) && dma->count == 0)
	{
		dma->control &= ~ST_STOP;
		LOGMASKED(LOG_DMA, "DMA terminated\n");
	}

	// Interrupt if count is zero, and interrupt flag set
	if ((dma->control & INTERRUPT_ON_ZERO) && dma->count == 0)
	{
		LOGMASKED(LOG_DMA_HIFREQ, "DMA%d - requesting interrupt: count = %04X, source = %04X\n", which, dma->count, dma->source);
		m_intr.request |= 0x04 << which;
		update_interrupt_state();
	}
}

uint16_t i80186_cpu_device::internal_port_r(offs_t offset, uint16_t mem_mask)
{
	int temp, which;

	switch (offset)
	{
		case 0x10:
			LOGMASKED(LOG_PORTS, "%05X:read interrupt vector\n", m_pc);
			return m_intr.vector;

		case 0x11:
			LOGMASKED(LOG_PORTS, "%05X:ERROR - read from 80186 EOI\n", m_pc);
			break;

		case 0x12:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt poll\n", m_pc);
			if (m_intr.poll_status & 0x8000)
				inta_callback(*this, 0);
			return m_intr.poll_status;

		case 0x13:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt poll status\n", m_pc);
			return m_intr.poll_status;

		case 0x14:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt mask\n", m_pc);
			temp  = (m_intr.timer[0]  >> 3) & 0x01;
			temp |= (m_intr.dma[0] >> 1) & 0x04;
			temp |= (m_intr.dma[1] >> 0) & 0x08;
			if (BIT(m_reloc, 14))
			{
				temp |= (m_intr.timer[1] << 1) & 0x10;
				temp |= (m_intr.timer[2] << 2) & 0x20;
			}
			else
			{
				temp |= (m_intr.ext[0] << 1) & 0x10;
				temp |= (m_intr.ext[1] << 2) & 0x20;
				temp |= (m_intr.ext[2] << 3) & 0x40;
				temp |= (m_intr.ext[3] << 4) & 0x80;
			}
			return temp;

		case 0x15:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt priority mask\n", m_pc);
			return m_intr.priority_mask;

		case 0x16:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt in-service\n", m_pc);
			return m_intr.in_service;

		case 0x17:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt request\n", m_pc);
			temp = m_intr.request & ~0x0001;
			if (m_intr.status & 0x0007)
				temp |= 1;
			return temp;

		case 0x18:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 interrupt status\n", m_pc);
			return m_intr.status;

		case 0x19:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 timer interrupt control\n", m_pc);
			return m_intr.timer[0];

		case 0x1a:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA 0 interrupt control\n", m_pc);
			return m_intr.dma[0];

		case 0x1b:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA 1 interrupt control\n", m_pc);
			return m_intr.dma[1];

		case 0x1c:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 INT 0 interrupt control\n", m_pc);
			if (BIT(m_reloc, 14))
				return m_intr.timer[1];
			else
				return m_intr.ext[0];

		case 0x1d:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 INT 1 interrupt control\n", m_pc);
			if (BIT(m_reloc, 14))
				return m_intr.timer[2];
			else
				return m_intr.ext[1];

		case 0x1e:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 INT 2 interrupt control\n", m_pc);
			if (BIT(m_reloc, 14))
				return 0;
			else
				return m_intr.ext[2];

		case 0x1f:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 INT 3 interrupt control\n", m_pc);
			if (BIT(m_reloc, 14))
				return 0;
			else
				return m_intr.ext[3];

		case 0x28:
		case 0x2c:
		case 0x30:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 Timer %d count\n", m_pc, (offset - 0x28) / 4);
			which = (offset - 0x28) / 4;
			if (ACCESSING_BITS_0_7)
				internal_timer_sync(which);
			return m_timer[which].count;

		case 0x29:
		case 0x2d:
		case 0x31:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 Timer %d max A\n", m_pc, (offset - 0x29) / 4);
			which = (offset - 0x29) / 4;
			return m_timer[which].maxA;

		case 0x2a:
		case 0x2e:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 Timer %d max B\n", m_pc, (offset - 0x2a) / 4);
			which = (offset - 0x2a) / 4;
			return m_timer[which].maxB;

		case 0x2b:
		case 0x2f:
		case 0x33:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 Timer %d control\n", m_pc, (offset - 0x2b) / 4);
			which = (offset - 0x2b) / 4;
			return m_timer[which].control;

		case 0x50:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 upper chip select\n", m_pc);
			return m_mem.upper;

		case 0x51:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 lower chip select\n", m_pc);
			return m_mem.lower;

		case 0x52:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 peripheral chip select\n", m_pc);
			return m_mem.peripheral;

		case 0x53:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 middle chip select\n", m_pc);
			return m_mem.middle;

		case 0x54:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 middle P chip select\n", m_pc);
			return m_mem.middle_size;

		case 0x60:
		case 0x68:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d lower source address\n", m_pc, (offset - 0x60) / 8);
			which = (offset - 0x60) / 8;
			return m_dma[which].source;

		case 0x61:
		case 0x69:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d upper source address\n", m_pc, (offset - 0x61) / 8);
			which = (offset - 0x61) / 8;
			return m_dma[which].source >> 16;

		case 0x62:
		case 0x6a:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d lower dest address\n", m_pc, (offset - 0x62) / 8);
			which = (offset - 0x62) / 8;
			return m_dma[which].dest;

		case 0x63:
		case 0x6b:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d upper dest address\n", m_pc, (offset - 0x63) / 8);
			which = (offset - 0x63) / 8;
			return m_dma[which].dest >> 16;

		case 0x64:
		case 0x6c:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d transfer count\n", m_pc, (offset - 0x64) / 8);
			which = (offset - 0x64) / 8;
			return m_dma[which].count;

		case 0x65:
		case 0x6d:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 DMA%d control\n", m_pc, (offset - 0x65) / 8);
			which = (offset - 0x65) / 8;
			return m_dma[which].control;

		case 0x7f:
			return m_reloc;

		default:
			LOGMASKED(LOG_PORTS, "%05X:read 80186 port %02X\n", m_pc, offset);
			break;
	}

	return 0x0000;
}

/*************************************
 *
 *  80186 internal I/O writes
 *
 *************************************/

void i80186_cpu_device::internal_port_w(offs_t offset, uint16_t data)
{
	int which;

	switch (offset)
	{
		case 0x10:
			LOGMASKED(LOG_PORTS, "%05X:write interrupt vector = %04X\n", m_pc, data);
			m_intr.vector = data & 0xf8;
			break;

		case 0x11:
			LOGMASKED(LOG_PORTS, "%05X:80186 EOI = %04X\n", m_pc, data);
			handle_eoi(data);
			update_interrupt_state();
			break;

		case 0x12:
			LOGMASKED(LOG_PORTS, "%05X:ERROR - write to 80186 interrupt poll = %04X\n", m_pc, data);
			break;

		case 0x13:
			LOGMASKED(LOG_PORTS, "%05X:ERROR - write to 80186 interrupt poll status = %04X\n", m_pc, data);
			break;

		case 0x14:
			LOGMASKED(LOG_PORTS, "%05X:80186 interrupt mask = %04X\n", m_pc, data);
			m_intr.timer[0]  = (m_intr.timer[0]  & ~0x08) | ((data << 3) & 0x08);
			m_intr.dma[0] = (m_intr.dma[0] & ~0x08) | ((data << 1) & 0x08);
			m_intr.dma[1] = (m_intr.dma[1] & ~0x08) | ((data << 0) & 0x08);
			if (BIT(m_reloc, 14))
			{
				m_intr.timer[1] = (m_intr.timer[1] & ~0x08) | ((data >> 1) & 0x08);
				m_intr.timer[2] = (m_intr.timer[2] & ~0x08) | ((data >> 2) & 0x08);
			}
			else
			{
				m_intr.ext[0] = (m_intr.ext[0] & ~0x08) | ((data >> 1) & 0x08);
				m_intr.ext[1] = (m_intr.ext[1] & ~0x08) | ((data >> 2) & 0x08);
				m_intr.ext[2] = (m_intr.ext[2] & ~0x08) | ((data >> 3) & 0x08);
				m_intr.ext[3] = (m_intr.ext[3] & ~0x08) | ((data >> 4) & 0x08);
			}
			update_interrupt_state();
			break;

		case 0x15:
			LOGMASKED(LOG_PORTS, "%05X:80186 interrupt priority mask = %04X\n", m_pc, data);
			m_intr.priority_mask = data & 0x0007;
			update_interrupt_state();
			break;

		case 0x16:
			LOGMASKED(LOG_PORTS, "%05X:80186 interrupt in-service = %04X\n", m_pc, data);
			m_intr.in_service = data & 0x00ff;
			update_interrupt_state();
			break;

		case 0x17:
			LOGMASKED(LOG_PORTS, "%05X:80186 interrupt request = %04X\n", m_pc, data);
			m_intr.request = (m_intr.request & ~0x000c) | (data & 0x000c);
			update_interrupt_state();
			break;

		case 0x18:
			LOGMASKED(LOG_PORTS, "%05X:WARNING - wrote to 80186 interrupt status = %04X\n", m_pc, data);
			m_intr.status = (m_intr.status & ~0x8007) | (data & 0x8007);
			update_interrupt_state();
			break;

		case 0x19:
			LOGMASKED(LOG_PORTS, "%05X:80186 timer interrupt contol = %04X\n", m_pc, data);
			m_intr.timer[0] = data & 0x000f;
			update_interrupt_state();
			break;

		case 0x1a:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA 0 interrupt control = %04X\n", m_pc, data);
			m_intr.dma[0] = data & 0x000f;
			update_interrupt_state();
			break;

		case 0x1b:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA 1 interrupt control = %04X\n", m_pc, data);
			m_intr.dma[1] = data & 0x000f;
			update_interrupt_state();
			break;

		case 0x1c:
			LOGMASKED(LOG_PORTS, "%05X:80186 INT 0 interrupt control = %04X\n", m_pc, data);
			if (BIT(m_reloc, 14))
				m_intr.timer[1] = data & 0x000f;
			else
				m_intr.ext[0] = data & 0x007f;
			update_interrupt_state();
			break;

		case 0x1d:
			LOGMASKED(LOG_PORTS, "%05X:80186 INT 1 interrupt control = %04X\n", m_pc, data);
			if (BIT(m_reloc, 14))
				m_intr.timer[2] = data & 0x000f;
			else
				m_intr.ext[1] = data & 0x007f;
			update_interrupt_state();
			break;

		case 0x1e:
			LOGMASKED(LOG_PORTS, "%05X:80186 INT 2 interrupt control = %04X\n", m_pc, data);
			if (!BIT(m_reloc, 14))
				m_intr.ext[2] = data & 0x001f;
			update_interrupt_state();
			break;

		case 0x1f:
			LOGMASKED(LOG_PORTS, "%05X:80186 INT 3 interrupt control = %04X\n", m_pc, data);
			if (!BIT(m_reloc, 14))
				m_intr.ext[3] = data & 0x001f;
			update_interrupt_state();
			break;

		case 0x28:
		case 0x2c:
		case 0x30:
			LOGMASKED(LOG_PORTS, "%05X:80186 Timer %d count = %04X\n", m_pc, (offset - 0x28) / 4, data);
			which = (offset - 0x28) / 4;
			internal_timer_update(which, data, -1, -1, -1);
			break;

		case 0x29:
		case 0x2d:
		case 0x31:
			LOGMASKED(LOG_PORTS, "%05X:80186 Timer %d max A = %04X\n", m_pc, (offset - 0x29) / 4, data);
			which = (offset - 0x29) / 4;
			internal_timer_update(which, -1, data, -1, -1);
			break;

		case 0x2a:
		case 0x2e:
			LOGMASKED(LOG_PORTS, "%05X:80186 Timer %d max B = %04X\n", m_pc, (offset - 0x2a) / 4, data);
			which = (offset - 0x2a) / 4;
			internal_timer_update(which, -1, -1, data, -1);
			break;

		case 0x2b:
		case 0x2f:
		case 0x33:
			LOGMASKED(LOG_PORTS, "%05X:80186 Timer %d control = %04X\n", m_pc, (offset - 0x2b) / 4, data);
			which = (offset - 0x2b) / 4;
			internal_timer_update(which, -1, -1, -1, data);
			break;

		case 0x50:
			LOGMASKED(LOG_PORTS, "%05X:80186 upper chip select = %04X\n", m_pc, data);
			m_mem.upper = data | 0xc038;
			m_out_chip_select_func(0, m_mem.upper, 0xffff);
			break;

		case 0x51:
			LOGMASKED(LOG_PORTS, "%05X:80186 lower chip select = %04X\n", m_pc, data);
			m_mem.lower = (data & 0x3fff) | 0x0038;
			m_out_chip_select_func(1, m_mem.lower, 0xffff);
			break;

		case 0x52:
			LOGMASKED(LOG_PORTS, "%05X:80186 peripheral chip select = %04X\n", m_pc, data);
			m_mem.peripheral = data | 0x0038;
			m_out_chip_select_func(2, m_mem.peripheral, 0xffff);
			break;

		case 0x53:
			LOGMASKED(LOG_PORTS, "%05X:80186 middle chip select = %04X\n", m_pc, data);
			m_mem.middle = data | 0x01f8;
			m_out_chip_select_func(3, m_mem.middle, 0xffff);
			break;

		case 0x54:
			LOGMASKED(LOG_PORTS, "%05X:80186 middle P chip select = %04X\n", m_pc, data);
			m_mem.middle_size = data | 0x8038;
			m_out_chip_select_func(4, m_mem.middle_size, 0xffff);
			break;

		case 0x60:
		case 0x68:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d lower source address = %04X\n", m_pc, (offset - 0x60) / 8, data);
			which = (offset - 0x60) / 8;
			m_dma[which].source = (m_dma[which].source & ~0x0ffff) | (data & 0x0ffff);
			break;

		case 0x61:
		case 0x69:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d upper source address = %04X\n", m_pc, (offset - 0x61) / 8, data);
			which = (offset - 0x61) / 8;
			m_dma[which].source = (m_dma[which].source & ~0xf0000) | ((data << 16) & 0xf0000);
			break;

		case 0x62:
		case 0x6a:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d lower dest address = %04X\n", m_pc, (offset - 0x62) / 8, data);
			which = (offset - 0x62) / 8;
			m_dma[which].dest = (m_dma[which].dest & ~0x0ffff) | (data & 0x0ffff);
			break;

		case 0x63:
		case 0x6b:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d upper dest address = %04X\n", m_pc, (offset - 0x63) / 8, data);
			which = (offset - 0x63) / 8;
			m_dma[which].dest = (m_dma[which].dest & ~0xf0000) | ((data << 16) & 0xf0000);
			break;

		case 0x64:
		case 0x6c:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d transfer count = %04X\n", m_pc, (offset - 0x64) / 8, data);
			which = (offset - 0x64) / 8;
			m_dma[which].count = data;
			break;

		case 0x65:
		case 0x6d:
			LOGMASKED(LOG_PORTS, "%05X:80186 DMA%d control = %04X\n", m_pc, (offset - 0x65) / 8, data);
			which = (offset - 0x65) / 8;
			update_dma_control(which, data);
			if((m_dma[which].control & (SYNC_MASK | ST_STOP | TIMER_DRQ)) == ST_STOP)
			{
				// TODO: don't do this
				while(m_dma[which].control & ST_STOP)
					drq_callback(which);
			}
			break;

		case 0x7f:
			// 80188 byte output to this port is *not* masked!
			LOGMASKED(LOG_PORTS, "%05X:80186 relocation register = %04X\n", m_pc, data);
			m_reloc = data;
			break;

		default:
			LOGMASKED(LOG_PORTS, "%05X:80186 port %02X = %04X\n", m_pc, offset, data);
			break;
	}
}