summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/express.h
Commit message (Expand)AuthorAgeFilesLines
* debug/express.cpp: Fix parsing for preincrement & predecrement operators; mak... AJR2021-01-241-5/+4
* debug/express.h: Remove leftover declaration AJR2020-09-191-2/+0
* express.cpp: Replace simple_list with std::list in parsing AJR2020-09-031-6/+10
* Debugger expression and memory access overhaul AJR2020-05-251-8/+18
* Make expressions octal for applicable address spaces in disassembly view AJR2020-04-131-18/+19
* misc cleanup: Vas Crabb2019-11-181-26/+2
* debug/express.cpp: Use std::deque for token stack (nw) AJR2018-07-261-4/+2
* Grammar police (nw) Olivier Galibert2018-02-241-2/+2
* (nw) misc cleanup: start replacing auto_alloc_* with smart pointers, get Vas Crabb2017-12-011-7/+6
* general cleanup: Vas Crabb2017-05-231-9/+5
* disable: reverse polarity (nw) Olivier Galibert2017-03-261-6/+6
* debugger_access: Refactor [O. Galibert] Olivier Galibert2017-03-021-7/+11
* Fix OS X build (nw) AJR2017-02-111-0/+2
* Remove emu.h from headers (nw) Olivier Galibert2017-02-111-1/+0
* srcclean (nw) Vas Crabb2017-01-221-1/+1
* Created a new debugger command 'tracesym' Nathan Woods2017-01-151-2/+4
* Introduce u8/u16/u32/u64/s8/s16/s32/s64 Vas Crabb2016-11-191-28/+28
* NOTICE (TYPE NAME CONSOLIDATION) Miodrag Milanovic2016-10-221-28/+28
* std::min and std:max instead of MIN and MAX, also some more macros converted ... Miodrag Milanovic2016-07-311-1/+1
* tagged_list to unordered_map for debugger (nw) Miodrag Milanovic2016-06-181-4/+4
* Major refactoring of debugger core [Ryan Holtz] therealmogminer@gmail.com2016-06-081-6/+8
* Various cleanups suggested by static analyzer (nw) Miodrag Milanovic2016-04-241-1/+1
* Avoid auto return types; silence some unused variable warnings (nw) AJR2016-03-311-1/+1
* Iterate over core classes C++11 style AJR2016-03-311-1/+1
* clang-modernize part 1 (nw) Miodrag Milanovic2015-12-031-5/+5
* moved all to std::string (nw) Miodrag Milanovic2015-04-221-3/+3
* There is no implicit conversion to char* in std::string (nw) Miodrag Milanovic2015-04-121-4/+4
* Bulk convert files that already had standard BSD license in my name Aaron Giles2013-10-161-31/+2
* Cleanups and version bumpmame0148 Miodrag Milanovic2013-01-111-43/+43
* Created new enum type address_spacenum for specifying an address Aaron Giles2011-03-271-7/+7
* Move generic templates from emucore.h to emutempl.h. Aaron Giles2011-02-071-1/+1
* Cleanup & version bump.mame0140u1 Aaron Giles2010-11-081-19/+19
* Fix issues with setting condition expressions on break/watchpoints. Aaron Giles2010-11-081-0/+1
* Converted the expression engine to C++, did the usual cleanup. Aaron Giles2010-11-011-150/+369
* First round of an attempted cleanup of header files in the system. Aaron Giles2010-01-101-0/+1
* NOTE: This change requires two new osd functions: osd_malloc() and Aaron Giles2010-01-081-1/+1
* Exposed an address space for EEPROM devices. This has several Aaron Giles2009-12-281-2/+1
* Results of running the latest srcclean. Aaron Giles2009-12-281-5/+5
* Memory references can now explicitly specify logical or physical access Aaron Giles2009-10-011-7/+12
* Further debugger cleanup. Symbol tables now have a global ref Aaron Giles2008-11-221-10/+12
* Debugger interfaces cleanup. Still more to do but this compiles and Aaron Giles2008-11-211-5/+5
* Added expression validation callback to verify names for CPUs and Aaron Giles2008-08-071-4/+15
* Changed symbol table reference parameters to void * from UINT32. Aaron Giles2008-07-311-6/+6
* Region classes go bye-bye. Aaron Giles2008-07-281-4/+1
* Changed the way memory regions are referenced. Instead of a single Aaron Giles2008-07-281-2/+2
* Changed direct access EEPROM interface to return the "bus width" of the Aaron Giles2008-07-171-21/+39
* Copyright cleanup: Aaron Giles2008-01-061-1/+1
* Initial checkin of MAME 0.121.mame0121 Aaron Giles2007-12-171-0/+160
a> 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
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
/******************************************************************************


    SCC68070 SoC peripheral emulation
    -------------------

    written by Ryan Holtz


*******************************************************************************

STATUS:

- Skeleton.  Just enough for the CD-i and Magicard to run.

TODO:

- Proper handling of the 68070's internal devices (UART, DMA, Timers, etc.)
- I2C could do with re-visiting.

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

#include "emu.h"
#include "scc68070.h"

#define LOG_I2C         (1U << 1)
#define LOG_UART        (1U << 2)
#define LOG_TIMERS      (1U << 3)
#define LOG_TIMERS_HF   (1U << 4)
#define LOG_DMA         (1U << 5)
#define LOG_MMU         (1U << 6)
#define LOG_IRQS        (1U << 7)
#define LOG_UNKNOWN     (1U << 8)
#define LOG_MORE_UART   (1U << 9)
#define LOG_ALL         (LOG_I2C | LOG_UART | LOG_TIMERS | LOG_DMA | LOG_MMU | LOG_IRQS | LOG_UNKNOWN)

#define VERBOSE         (0)

#include "logmacro.h"

#define ENABLE_UART_PRINTING (0)

//**************************************************************************
// Register defines
//**************************************************************************
enum isr_bits
{
	ISR_MST      =  0x80,                        // Master
	ISR_TRX      =  0x40,                        // Transmitter
	ISR_BB       =  0x20,                        // Busy
	ISR_PIN      =  0x10,                        // No Pending Interrupt
	ISR_AL       =  0x08,                        // Arbitration Lost
	ISR_AAS      =  0x04,                        // Addressed As Slave
	ISR_AD0      =  0x02,                        // Address Zero
	ISR_LRB      =  0x01,                        // Last Received Bit
	ISR_SSR_MASK =  (ISR_MST | ISR_TRX | ISR_BB),// Mask for detecting start/stop/restart
	ISR_START    =  (ISR_MST | ISR_TRX | ISR_BB),// Start bit request
	ISR_STOP     =  (ISR_MST | ISR_TRX)         // Stop bit request
};

enum umr_bits
{
	UMR_OM          = 0xc0,
	UMR_OM_NORMAL   = 0x00,
	UMR_OM_ECHO     = 0x40,
	UMR_OM_LOOPBACK = 0x80,
	UMR_OM_RLOOP    = 0xc0,
	UMR_TXC         = 0x10,
	UMR_PC          = 0x08,
	UMR_P           = 0x04,
	UMR_SB          = 0x02,
	UMR_CL          = 0x01
};

enum usr_bits
{
	USR_RB          = 0x80,
	USR_FE          = 0x40,
	USR_PE          = 0x20,
	USR_OE          = 0x10,
	USR_TXEMT       = 0x08,
	USR_TXRDY       = 0x04,
	USR_RXRDY       = 0x01
};

enum tsr_bits
{
	TSR_OV0         = 0x80,
	TSR_MA1         = 0x40,
	TSR_CAP1        = 0x20,
	TSR_OV1         = 0x10,
	TSR_MA2         = 0x08,
	TSR_CAP2        = 0x04,
	TSR_OV2         = 0x02
};

enum tcr_bits
{
	TCR_E1          = 0xc0,
	TCR_E1_NONE     = 0x00,
	TCR_E1_RISING   = 0x40,
	TCR_E1_FALLING  = 0x80,
	TCR_E1_BOTH     = 0xc0,
	TCR_M1          = 0x30,
	TCR_M1_NONE     = 0x00,
	TCR_M1_MATCH    = 0x10,
	TCR_M1_CAPTURE  = 0x20,
	TCR_M1_COUNT    = 0x30,
	TCR_E2          = 0x0c,
	TCR_E2_NONE     = 0x00,
	TCR_E2_RISING   = 0x04,
	TCR_E2_FALLING  = 0x08,
	TCR_E2_BOTH     = 0x0c,
	TCR_M2          = 0x03,
	TCR_M2_NONE     = 0x00,
	TCR_M2_MATCH    = 0x01,
	TCR_M2_CAPTURE  = 0x02,
	TCR_M2_COUNT    = 0x03
};

enum csr_bits
{
	CSR_COC         = 0x80,
	CSR_NDT         = 0x20,
	CSR_ERR         = 0x10,
	CSR_CA          = 0x08
};

enum cer_bits
{
	CER_EC          = 0x1f,
	CER_NONE        = 0x00,
	CER_TIMING      = 0x02,
	CER_BUSERR_MEM  = 0x09,
	CER_BUSERR_DEV  = 0x0a,
	CER_SOFT_ABORT  = 0x11
};

enum dcr1_bits
{
	DCR1_ERM        = 0x80,
	DCR1_DT         = 0x30
};

enum dcr2_bits
{
	DCR2_ERM        = 0x80,
	DCR2_DT         = 0x30,
	DCR2_DS         = 0x08
};


enum scr2_bits
{
	SCR2_MAC        = 0x0c,
	SCR2_MAC_NONE   = 0x00,
	SCR2_MAC_INC    = 0x04,
	SCR2_DAC        = 0x03,
	SCR2_DAC_NONE   = 0x00,
	SCR2_DAC_INC    = 0x01
};

enum ccr_bits
{
	CCR_SO          = 0x80,
	CCR_SA          = 0x10,
	CCR_INE         = 0x08,
	CCR_IPL         = 0x07
};

enum icr_bits
{
	ICR_SEL = 0x40,
	ICR_ESO = 0x08,
	ICR_ACK = 0x04
};

enum i2c_states
{
	I2C_IDLE = 0,
	I2C_TX_IN_PROGRESS,
	I2C_RX_IN_PROGRESS,
	I2C_RX_COMPLETE,
	I2C_GET_ACK,
	I2C_SEND_ACK,
	I2C_SEND_ACK_AND_RX,
	I2C_SEND_ACK_AND_STOP,
	I2C_SEND_STOP,
	I2C_CHANGED_TO_RX,
	I2C_SEND_RESTART
};

enum i2c_clock_states
{
	I2C_SCL_IDLE = 0,
	I2C_SCL_SET_0,
	I2C_SCL_SET_1,
	I2C_SCL_WAIT_1,
};


// device type definition
DEFINE_DEVICE_TYPE(SCC68070, scc68070_device, "scc68070", "Philips SCC68070")

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

void scc68070_device::internal_map(address_map &map)
{
	map(0x80001001, 0x80001001).rw(FUNC(scc68070_device::lir_r), FUNC(scc68070_device::lir_w));
	map(0x80002001, 0x80002001).rw(FUNC(scc68070_device::idr_r), FUNC(scc68070_device::idr_w));
	map(0x80002003, 0x80002003).rw(FUNC(scc68070_device::iar_r), FUNC(scc68070_device::iar_w));
	map(0x80002005, 0x80002005).rw(FUNC(scc68070_device::isr_r), FUNC(scc68070_device::isr_w));
	map(0x80002007, 0x80002007).rw(FUNC(scc68070_device::icr_r), FUNC(scc68070_device::icr_w));
	map(0x80002009, 0x80002009).rw(FUNC(scc68070_device::iccr_r), FUNC(scc68070_device::iccr_w));
	map(0x80002011, 0x80002011).rw(FUNC(scc68070_device::umr_r), FUNC(scc68070_device::umr_w));
	map(0x80002013, 0x80002013).r(FUNC(scc68070_device::usr_r));
	map(0x80002015, 0x80002015).rw(FUNC(scc68070_device::ucsr_r), FUNC(scc68070_device::ucsr_w));
	map(0x80002017, 0x80002017).rw(FUNC(scc68070_device::ucr_r), FUNC(scc68070_device::ucr_w));
	map(0x80002019, 0x80002019).rw(FUNC(scc68070_device::uth_r), FUNC(scc68070_device::uth_w));
	map(0x8000201b, 0x8000201b).r(FUNC(scc68070_device::urh_r));
	map(0x80002020, 0x80002029).rw(FUNC(scc68070_device::timer_r), FUNC(scc68070_device::timer_w));
	map(0x80002045, 0x80002045).rw(FUNC(scc68070_device::picr1_r), FUNC(scc68070_device::picr1_w));
	map(0x80002047, 0x80002047).rw(FUNC(scc68070_device::picr2_r), FUNC(scc68070_device::picr2_w));
	map(0x80004000, 0x8000406d).rw(FUNC(scc68070_device::dma_r), FUNC(scc68070_device::dma_w));
	map(0x80008000, 0x8000807f).rw(FUNC(scc68070_device::mmu_r), FUNC(scc68070_device::mmu_w));
}

void scc68070_device::cpu_space_map(address_map &map)
{
	map(0xfffffff0, 0xffffffff).r(FUNC(scc68070_device::iack_r)).umask16(0x00ff);
}

//-------------------------------------------------
//  scc68070_device - constructor
//-------------------------------------------------

scc68070_device::scc68070_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: scc68070_base_device(mconfig, tag, owner, clock, SCC68070, address_map_constructor(FUNC(scc68070_device::internal_map), this))
	, m_iack2_callback(*this, autovector(2))
	, m_iack4_callback(*this, autovector(4))
	, m_iack5_callback(*this, autovector(5))
	, m_iack7_callback(*this, autovector(7))
	, m_uart_tx_callback(*this)
	, m_uart_rtsn_callback(*this)
	, m_i2c_scl_callback(*this)
	, m_i2c_sdaw_callback(*this)
	, m_i2c_sdar_callback(*this, 0)
	, m_ipl(0)
	, m_in2_line(CLEAR_LINE)
	, m_in4_line(CLEAR_LINE)
	, m_in5_line(CLEAR_LINE)
	, m_nmi_line(CLEAR_LINE)
	, m_int1_line(CLEAR_LINE)
	, m_int2_line(CLEAR_LINE)
{
	m_cpu_space_config.m_internal_map = address_map_constructor(FUNC(scc68070_device::cpu_space_map), this);
}

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

void scc68070_device::device_start()
{
	scc68070_base_device::device_start();

	save_item(NAME(m_ipl));

	save_item(NAME(m_in2_line));
	save_item(NAME(m_in4_line));
	save_item(NAME(m_in5_line));
	save_item(NAME(m_nmi_line));

	save_item(NAME(m_int1_line));
	save_item(NAME(m_int2_line));
	save_item(NAME(m_lir));

	save_item(NAME(m_picr1));
	save_item(NAME(m_picr2));
	save_item(NAME(m_timer_int));
	save_item(NAME(m_i2c_int));
	save_item(NAME(m_uart_rx_int));
	save_item(NAME(m_uart_tx_int));

	save_item(NAME(m_i2c.data_register));
	save_item(NAME(m_i2c.address_register));
	save_item(NAME(m_i2c.status_register));
	save_item(NAME(m_i2c.control_register));
	save_item(NAME(m_i2c.clock_control_register));
	save_item(NAME(m_i2c.scl_out_state));
	save_item(NAME(m_i2c.scl_in_state));
	save_item(NAME(m_i2c.sda_out_state));
	save_item(NAME(m_i2c.sda_in_state));
	save_item(NAME(m_i2c.state));
	save_item(NAME(m_i2c.counter));
	save_item(NAME(m_i2c.clock_change_state));
	save_item(NAME(m_i2c.clocks));
	save_item(NAME(m_i2c.first_byte));
	save_item(NAME(m_i2c.ack_or_nak_sent));

	save_item(NAME(m_uart.mode_register));
	save_item(NAME(m_uart.status_register));
	save_item(NAME(m_uart.clock_select));
	save_item(NAME(m_uart.command_register));
	save_item(NAME(m_uart.receive_holding_register));
	save_item(NAME(m_uart.receive_pointer));
	save_item(NAME(m_uart.receive_buffer));
	save_item(NAME(m_uart.transmit_holding_register));
	save_item(NAME(m_uart.transmit_pointer));
	save_item(NAME(m_uart.transmit_buffer));
	save_item(NAME(m_uart.transmit_ctsn));

	save_item(NAME(m_timers.timer_status_register));
	save_item(NAME(m_timers.timer_control_register));
	save_item(NAME(m_timers.reload_register));
	save_item(NAME(m_timers.timer0));
	save_item(NAME(m_timers.timer1));
	save_item(NAME(m_timers.timer2));

	save_item(STRUCT_MEMBER(m_dma.channel, channel_status));
	save_item(STRUCT_MEMBER(m_dma.channel, channel_error));
	save_item(STRUCT_MEMBER(m_dma.channel, device_control));
	save_item(STRUCT_MEMBER(m_dma.channel, operation_control));
	save_item(STRUCT_MEMBER(m_dma.channel, sequence_control));
	save_item(STRUCT_MEMBER(m_dma.channel, channel_control));
	save_item(STRUCT_MEMBER(m_dma.channel, transfer_counter));
	save_item(STRUCT_MEMBER(m_dma.channel, memory_address_counter));
	save_item(STRUCT_MEMBER(m_dma.channel, device_address_counter));

	save_item(NAME(m_mmu.status));
	save_item(NAME(m_mmu.control));
	save_item(STRUCT_MEMBER(m_mmu.desc, attr));
	save_item(STRUCT_MEMBER(m_mmu.desc, length));
	save_item(STRUCT_MEMBER(m_mmu.desc, segment));
	save_item(STRUCT_MEMBER(m_mmu.desc, base));

	m_timers.timer0_timer = timer_alloc(FUNC(scc68070_device::timer0_callback), this);
	m_timers.timer0_timer->adjust(attotime::never);

	m_uart.rx_timer = timer_alloc(FUNC(scc68070_device::rx_callback), this);
	m_uart.rx_timer->adjust(attotime::never);

	m_uart.tx_timer = timer_alloc(FUNC(scc68070_device::tx_callback), this);
	m_uart.tx_timer->adjust(attotime::never);

	m_i2c.timer = timer_alloc(FUNC(scc68070_device::i2c_callback), this);
	m_i2c.timer->adjust(attotime::never);
}

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

void scc68070_device::device_reset()
{
	scc68070_base_device::device_reset();

	m_lir = 0;

	m_picr1 = 0;
	m_picr2 = 0;
	m_timer_int = false;
	m_i2c_int = false;
	m_uart_rx_int = false;
	m_uart_tx_int = false;

	m_i2c.data_register = 0;
	m_i2c.address_register = 0;
	m_i2c.status_register = ISR_PIN;
	m_i2c.control_register = 0;
	m_i2c.clock_control_register = 0;
	m_i2c.scl_out_state = true;
	m_i2c.scl_in_state = true;
	m_i2c.sda_out_state = true;
	m_i2c.state = I2C_IDLE;
	m_i2c.clock_change_state = I2C_SCL_IDLE;
	m_i2c.clocks = 0;

	m_uart.mode_register = 0;
	m_uart.status_register = USR_TXRDY;
	m_uart.clock_select = 0;
	m_uart.command_register = 0;
	m_uart.transmit_holding_register = 0;
	m_uart.receive_holding_register = 0;
	m_uart.receive_pointer = -1;
	m_uart.transmit_pointer = -1;
	m_uart.transmit_ctsn = true;

	m_timers.timer_status_register = 0;
	m_timers.timer_control_register = 0;
	m_timers.reload_register = 0;
	m_timers.timer0 = 0;
	m_timers.timer1 = 0;
	m_timers.timer2 = 0;

	for(int index = 0; index < 2; index++)
	{
		m_dma.channel[index].channel_status = 0;
		m_dma.channel[index].channel_error = 0;
		m_dma.channel[index].device_control = 0;
		m_dma.channel[index].operation_control = 0;
		m_dma.channel[index].sequence_control = 0;
		m_dma.channel[index].channel_control = 0;
		m_dma.channel[index].transfer_counter = 0;
		m_dma.channel[index].memory_address_counter = 0;
		m_dma.channel[index].device_address_counter = 0;
	}

	m_mmu.status = 0;
	m_mmu.control = 0;
	for(int index = 0; index < 8; index++)
	{
		m_mmu.desc[index].attr = 0;
		m_mmu.desc[index].length = 0;
		m_mmu.desc[index].segment = 0;
		m_mmu.desc[index].base = 0;
	}

	update_ipl();

	m_uart.rx_timer->adjust(attotime::never);
	m_uart.tx_timer->adjust(attotime::never);
	set_timer_callback(0);
}


void scc68070_device::device_config_complete()
{
	scc68070_base_device::device_config_complete();

	reset_cb().append(*this, FUNC(scc68070_device::reset_peripherals));
}


void scc68070_device::reset_peripherals(int state)
{
	if (state)
	{
		m_lir = 0;

		m_picr1 = 0;
		m_picr2 = 0;
		m_timer_int = false;
		m_i2c_int = false;
		m_uart_rx_int = false;
		m_uart_tx_int = false;

		m_i2c.status_register = ISR_PIN;
		m_i2c.control_register = 0;
		m_i2c.clock_control_register = 0;
		m_i2c.scl_out_state = true;
		m_i2c.scl_in_state = true;
		m_i2c.sda_out_state = true;
		m_i2c.state = I2C_IDLE;
		m_i2c.clock_change_state = I2C_SCL_IDLE;
		m_i2c.clocks = 0;
		m_uart.command_register = 0;
		m_uart.receive_pointer = -1;
		m_uart.transmit_pointer = -1;

		m_uart.mode_register = 0;
		m_uart.status_register = USR_TXRDY;
		m_uart.clock_select = 0;

		m_timers.timer_status_register = 0;
		m_timers.timer_control_register = 0;

		m_uart.rx_timer->adjust(attotime::never);
		m_uart.tx_timer->adjust(attotime::never);
		m_timers.timer0_timer->adjust(attotime::never);
		m_i2c.timer->adjust(attotime::never);

		update_ipl();
	}
}

void scc68070_device::update_ipl()
{
	const uint8_t external_level = (m_nmi_line == ASSERT_LINE) ? 7
		: (m_in5_line == ASSERT_LINE) ? 5
		: (m_in4_line == ASSERT_LINE) ? 4
		: (m_in2_line == ASSERT_LINE) ? 2 : 0;
	const uint8_t int1_level = BIT(m_lir, 7) ? (m_lir >> 4) & 7 : 0;
	const uint8_t int2_level = BIT(m_lir, 3) ? m_lir & 7 : 0;
	const uint8_t timer_level = m_timer_int ? m_picr1 & 7 : 0;
	const uint8_t uart_rx_level = m_uart_rx_int ? (m_picr2 >> 4) & 7 : 0;
	const uint8_t uart_tx_level = m_uart_tx_int ? m_picr2 & 7 : 0;
	const uint8_t i2c_level = m_i2c_int ? (m_picr1 >> 4) & 7 : 0;
	const uint8_t dma_ch1_level = (m_dma.channel[0].channel_status & CSR_COC) && (m_dma.channel[0].channel_control & CCR_INE) ? m_dma.channel[0].channel_control & CCR_IPL : 0;
	const uint8_t dma_ch2_level = (m_dma.channel[1].channel_status & CSR_COC) && (m_dma.channel[1].channel_control & CCR_INE) ? m_dma.channel[1].channel_control & CCR_IPL : 0;

	const uint8_t new_ipl = std::max({external_level, int1_level, int2_level, timer_level, uart_rx_level, uart_tx_level, i2c_level, dma_ch1_level, dma_ch2_level});

	if (m_ipl != new_ipl)
	{
		if (m_ipl != 0)
			set_input_line(m_ipl, CLEAR_LINE);
		if (new_ipl != 0)
			set_input_line(new_ipl, ASSERT_LINE);
		m_ipl = new_ipl;
	}
}

void scc68070_device::in2_w(int state)
{
	m_in2_line = state;
	update_ipl();
}

void scc68070_device::in4_w(int state)
{
	m_in4_line = state;
	update_ipl();
}

void scc68070_device::in5_w(int state)
{
	m_in5_line = state;
	update_ipl();
}

void scc68070_device::nmi_w(int state)
{
	m_nmi_line = state;
	update_ipl();
}

void scc68070_device::int1_w(int state)
{
	if (m_int1_line != state)
	{
		if (state == ASSERT_LINE && !BIT(m_lir, 7))
		{
			m_lir |= 0x80;
			update_ipl();
		}

		m_int1_line = state;
	}
}

void scc68070_device::int2_w(int state)
{
	if (m_int2_line != state)
	{
		if (state == ASSERT_LINE && !BIT(m_lir, 3))
		{
			m_lir |= 0x08;
			update_ipl();
		}

		m_int2_line = state;
	}
}

uint8_t scc68070_device::iack_r(offs_t offset)
{
	switch (offset)
	{
	case 2:
		if (m_in2_line == ASSERT_LINE)
			return m_iack2_callback();
		break;

	case 4:
		if (m_in4_line == ASSERT_LINE)
			return m_iack4_callback();
		break;

	case 5:
		if (m_in5_line == ASSERT_LINE)
			return m_iack5_callback();
		break;

	case 7:
		if (m_nmi_line == ASSERT_LINE)
			return m_iack7_callback();
		break;
	}

	if (!machine().side_effects_disabled())
	{
		if (BIT(m_lir, 7) && offset == ((m_lir >> 4) & 7))
		{
			m_lir &= 0x7f;
			update_ipl();
		}
		else if (BIT(m_lir, 3) && offset == (m_lir & 7))
		{
			m_lir &= 0xf7;
			update_ipl();
		}
		else if (m_timer_int && offset == (m_picr1 & 7))
		{
			m_timer_int = false;
			update_ipl();
		}
		else if (m_uart_rx_int && offset == ((m_picr2 >> 4) & 7))
		{
			m_uart_rx_int = false;
			update_ipl();
		}
		else if (m_uart_tx_int && offset == (m_picr2 & 7))
		{
			m_uart_tx_int = false;
			update_ipl();
		}
		else if (m_i2c_int && offset == ((m_picr2 >> 4) & 7))
		{
			m_i2c_int = false;
			update_ipl();
		}
	}

	return 0x38 + offset;
}

void scc68070_device::set_timer_callback(int channel)
{
	switch (channel)
	{
		case 0:
		{
			// Timer clock period is 96/CLKOUT
			uint32_t compare = 0x10000 - m_timers.timer0;
			attotime period = cycles_to_attotime(96 * compare);
			m_timers.timer0_timer->adjust(period);
			break;
		}
		default:
		{
			fatalerror( "Unsupported timer channel to set_timer_callback!\n" );
		}
	}
}

TIMER_CALLBACK_MEMBER(scc68070_device::timer0_callback)
{
	m_timers.timer0 = m_timers.reload_register;
	m_timers.timer_status_register |= TSR_OV0;
	if (!m_timer_int)
	{
		m_timer_int = true;
		update_ipl();
	}

	set_timer_callback(0);
}

void scc68070_device::uart_ctsn(int state)
{
	m_uart.transmit_ctsn = state ? true : false;
}

void scc68070_device::uart_rx(uint8_t data)
{
	m_uart.receive_pointer++;
	m_uart.receive_buffer[m_uart.receive_pointer] = data;
}

void scc68070_device::uart_tx(uint8_t data)
{
	m_uart.transmit_pointer++;
	m_uart.transmit_buffer[m_uart.transmit_pointer] = data;
	m_uart.status_register &= ~USR_TXEMT;
}

TIMER_CALLBACK_MEMBER(scc68070_device::rx_callback)
{
	if ((m_uart.command_register & 3) == 1)
	{
		if (m_uart.receive_pointer >= 0)
		{
			m_uart.status_register |= USR_RXRDY;
		}
		else
		{
			m_uart.status_register &= ~USR_RXRDY;
		}

		m_uart.receive_holding_register = m_uart.receive_buffer[0];

		if (m_uart.receive_pointer > -1)
		{
			LOGMASKED(LOG_UART, "scc68070_rx_callback: Receiving %02x\n", m_uart.receive_holding_register);

			m_uart_rx_int = true;
			update_ipl();

			m_uart.status_register |= USR_RXRDY;
		}
		else
		{
			m_uart.status_register &= ~USR_RXRDY;
		}
	}
	else
	{
		m_uart.status_register &= ~USR_RXRDY;
	}
}

TIMER_CALLBACK_MEMBER(scc68070_device::tx_callback)
{
	if (((m_uart.command_register >> 2) & 3) == 1)
	{
		m_uart.status_register |= USR_TXRDY;

		m_uart_tx_int = true;
		update_ipl();

		if (m_uart.transmit_pointer > -1)
		{
			if (m_uart.transmit_ctsn && BIT(m_uart.mode_register, 4))
			{
				return;
			}

			m_uart.transmit_holding_register = m_uart.transmit_buffer[0];
			m_uart_tx_callback(m_uart.transmit_holding_register);

			LOGMASKED(LOG_MORE_UART, "tx_callback: Transmitting %02x\n", m_uart.transmit_holding_register);
			for(int index = 0; index < m_uart.transmit_pointer; index++)
			{
				m_uart.transmit_buffer[index] = m_uart.transmit_buffer[index+1];
			}
			m_uart.transmit_pointer--;
		}

		if (m_uart.transmit_pointer < 0)
		{
			m_uart.status_register |= USR_TXEMT;
		}
	}
}

uint8_t scc68070_device::lir_r()
{
	// LIR priority level: 80001001
	return m_lir & 0x77;
}

void scc68070_device::lir_w(uint8_t data)
{
	LOGMASKED(LOG_IRQS, "%s: LIR Write: %02x\n", machine().describe_context(), data);

	switch (data & 0x88)
	{
	case 0x08:
		if (m_lir & 0x08)
		{
			m_lir &= 0xf7;
			update_ipl();
		}
		break;

	case 0x80:
		if (data & 0x80)
		{
			m_lir &= 0x7f;
			update_ipl();
		}
		break;

	case 0x88:
		if (data & 0x88)
		{
			m_lir &= 0x77;
			update_ipl();
		}
		break;
	}

	m_lir = (m_lir & 0x88) | (data & 0x77);
}

uint8_t scc68070_device::picr1_r()
{
	// PICR1: 80002045
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_IRQS, "%s: Peripheral Interrupt Control Register 1 Read: %02x\n", machine().describe_context(), m_picr1);
	return m_picr1 & 0x77;
}

void scc68070_device::picr1_w(uint8_t data)
{
	LOGMASKED(LOG_IRQS, "%s: Peripheral Interrupt Control Register 1 Write: %02x\n", machine().describe_context(), data);
	m_picr1 = data & 0x77;
	switch (data & 0x88)
	{
	case 0x08:
		if (m_timer_int)
		{
			m_timer_int = false;
			update_ipl();
		}
		break;

	case 0x80:
		if (m_i2c_int)
		{
			m_i2c_int = false;
			update_ipl();
		}
		break;

	case 0x88:
		if (m_timer_int || m_i2c_int)
		{
			m_timer_int = false;
			m_i2c_int = false;
			update_ipl();
		}
		break;
	}
}

uint8_t scc68070_device::picr2_r()
{
	// PICR2: 80002047
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_IRQS, "%s: Peripheral Interrupt Control Register 2 Read: %02x\n", machine().describe_context(), m_picr2);
	return m_picr2 & 0x77;
}

void scc68070_device::picr2_w(uint8_t data)
{
	LOGMASKED(LOG_IRQS, "%s: Peripheral Interrupt Control Register 2 Write: %02x\n", machine().describe_context(), data);
	m_picr2 = data & 0x77;
	switch (data & 0x88)
	{
	case 0x08:
		if (m_uart_tx_int)
		{
			m_uart_tx_int = false;
			update_ipl();
		}
		break;

	case 0x80:
		if (m_uart_rx_int)
		{
			m_uart_rx_int = false;
			update_ipl();
		}
		break;

	case 0x88:
		if (m_uart_tx_int || m_uart_rx_int)
		{
			m_uart_tx_int = false;
			m_uart_rx_int = false;
			update_ipl();
		}
		break;
	}
}

uint8_t scc68070_device::idr_r()
{
	// I2C data register: 80002001
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_I2C, "%s: I2C Data Register Read: %02x\n", machine().describe_context(), m_i2c.data_register);

	m_i2c.counter = 0;
	m_i2c.status_register |= ISR_PIN;
	m_i2c_int = false;
	update_ipl();

	if (m_i2c.state != I2C_RX_COMPLETE)
	{
	}
	else
	{
		m_i2c.sda_out_state = (m_i2c.control_register & ICR_ACK) ? false : true;
		m_i2c_sdaw_callback(m_i2c.sda_out_state);

		if (m_i2c.control_register & ICR_ACK)
		{
			m_i2c.state = I2C_SEND_ACK_AND_RX;
			m_i2c.clocks = 9;
		}
		else
		{
			m_i2c.state = I2C_SEND_ACK;
			m_i2c.clocks = 1;
		}
		m_i2c.ack_or_nak_sent = true;
		m_i2c.clock_change_state = I2C_SCL_SET_1;
		set_i2c_timer();

	}
	return m_i2c.data_register;
}

void scc68070_device::idr_w(uint8_t data)
{
	LOGMASKED(LOG_I2C, "%s: I2C Data Register Write: %02x\n", machine().describe_context(), data);
	m_i2c.data_register = data;
	if (m_i2c.status_register & ISR_MST && m_i2c.status_register & ISR_TRX && m_i2c.status_register & ISR_BB)
	{
		m_i2c.status_register |= ISR_PIN;
		m_i2c_int = false;
		update_ipl();
		m_i2c.counter = 0;
		m_i2c.state = I2C_TX_IN_PROGRESS;
		m_i2c.clocks = 9;
		i2c_process_falling_scl();
		m_i2c.clock_change_state = I2C_SCL_SET_1;
		set_i2c_timer();
	}
}

uint8_t scc68070_device::iar_r()
{
	// I2C address register: 80002003
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_I2C, "%s: I2C Address Register Read: %02x\n", machine().describe_context(), m_i2c.address_register);
	return m_i2c.address_register;
}

void scc68070_device::iar_w(uint8_t data)
{
	LOGMASKED(LOG_I2C, "%s: I2C Address Register Write: %02x\n", machine().describe_context(), data);
	m_i2c.address_register = data;
}

uint8_t scc68070_device::isr_r()
{
	// I2C status register: 80002005
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_I2C, "%s: I2C Status Register Read: %02x\n", machine().describe_context(), m_i2c.status_register);
	return m_i2c.status_register;
}

void scc68070_device::isr_w(uint8_t data)
{
	LOGMASKED(LOG_I2C, "%s: I2C Status Register Write: %02x\n", machine().describe_context(), data);
	if (data & ISR_MST)
	{
		if ((data & ISR_SSR_MASK) == ISR_START)
		{
			if ((m_i2c.status_register & ISR_SSR_MASK) == ISR_STOP || (m_i2c.status_register & ISR_SSR_MASK) == 0)
			{
				if (m_i2c_sdar_callback() && m_i2c.state == I2C_IDLE)
				{
					m_i2c.status_register = data;
					if (data & ISR_PIN)
					{
						m_i2c_int = false;
						update_ipl();
					}
					m_i2c.sda_out_state = false;
					m_i2c_sdaw_callback(false);
					m_i2c.clock_change_state = I2C_SCL_SET_0;
					m_i2c.clocks = 10;
					m_i2c.state = I2C_TX_IN_PROGRESS;
					m_i2c.first_byte = true;
					m_i2c.ack_or_nak_sent = false;
					set_i2c_timer();
					m_i2c.counter = 0;
				}
				else
				{
					m_i2c.status_register |= ISR_AL;
					m_i2c.status_register &= ~ISR_PIN;
					m_i2c_int = true;
					update_ipl();
				}
			}
			else if ((m_i2c.status_register & ISR_SSR_MASK) == ISR_MST)
			{
				m_i2c.status_register = data;
				if (data & ISR_PIN)
				{
					m_i2c_int = false;
					update_ipl();
				}
				m_i2c.sda_out_state = true;
				m_i2c_sdaw_callback(true);
				m_i2c.clock_change_state = I2C_SCL_SET_1;
				m_i2c.clocks = 10;
				m_i2c.state = I2C_SEND_RESTART;
				m_i2c.first_byte = true;
				m_i2c.ack_or_nak_sent = false;
				set_i2c_timer();
				m_i2c.counter = 0;
			}
		}
		else if ((data & ISR_SSR_MASK) == ISR_STOP && m_i2c.status_register & ISR_BB)
		{
			// we should send STOP here, however, unkte06 in magicard appears to expect
			// NAK followed by STOP when in read mode.

			if (data & ISR_PIN)
			{
				m_i2c_int = false;
				update_ipl();
			}

			if (m_i2c.ack_or_nak_sent || (m_i2c.status_register & ISR_TRX))
			{
				m_i2c.state = I2C_SEND_STOP;
				m_i2c.sda_out_state = false;
				m_i2c_sdaw_callback(false);
			}
			else
			{
				m_i2c.ack_or_nak_sent = true;
				m_i2c.sda_out_state = (m_i2c.control_register&ICR_ACK) ? false : true;
				m_i2c_sdaw_callback(m_i2c.sda_out_state);
				m_i2c.state = I2C_SEND_ACK_AND_STOP;
				m_i2c.clocks = 2;
			}
			m_i2c.status_register = data | ISR_BB;
			m_i2c.clock_change_state = I2C_SCL_SET_1;
			set_i2c_timer();
		}
		else if ((data & ISR_SSR_MASK) == ISR_MST)
		{
			m_i2c.status_register = data;
			if (data & ISR_PIN)
			{
				m_i2c_int = false;
				update_ipl();
			}
		}
		else
		{
			if (data & ISR_PIN && !(m_i2c.status_register & ISR_PIN))
			{
				if (m_i2c.state == I2C_CHANGED_TO_RX)
				{
					m_i2c.state = I2C_RX_IN_PROGRESS;
					m_i2c.clock_change_state = I2C_SCL_SET_1;
					m_i2c.status_register = data;
					m_i2c_int = false;
					update_ipl();
					m_i2c.counter = 0;
					m_i2c.clocks = 8;
					set_i2c_timer();
				}
				else
				{
					m_i2c.ack_or_nak_sent = true;
					m_i2c.sda_out_state = (m_i2c.control_register&ICR_ACK) ? false : true;
					m_i2c_sdaw_callback(m_i2c.sda_out_state);
					m_i2c.status_register = data;
					m_i2c_int = false;
					update_ipl();
					m_i2c.state = I2C_SEND_ACK;
					m_i2c.clock_change_state = I2C_SCL_SET_1;
					m_i2c.clocks = 1;
					set_i2c_timer();
				}
			}
			else
			{
				m_i2c.status_register = data;
				if (data & ISR_PIN)
				{
					m_i2c_int = false;
					update_ipl();
				}
			}
		}
	}
	else
	{
		m_i2c.status_register = data;
		m_i2c_int = false;
		update_ipl();
		m_i2c.timer->adjust(attotime::never);
		m_i2c_scl_callback(1);
		m_i2c_sdaw_callback(1);
		m_i2c.scl_out_state = true;
		m_i2c.scl_in_state = true;
		m_i2c.sda_out_state = true;
		m_i2c.state = I2C_IDLE;
	}
}

uint8_t scc68070_device::icr_r()
{
	// I2C control register: 80002007
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_I2C, "%s: I2C Control Register Read: %02x\n", machine().describe_context(), m_i2c.control_register);
	return m_i2c.control_register;
}

void scc68070_device::icr_w(uint8_t data)
{
	LOGMASKED(LOG_I2C, "%s: I2C Control Register Write: %02x\n", machine().describe_context(), data);
	m_i2c.control_register = data;

	if (!(data & ICR_ESO))
	{
		m_i2c.timer->adjust(attotime::never);
		m_i2c_scl_callback(1);
		m_i2c_sdaw_callback(1);
		m_i2c.scl_out_state = true;
		m_i2c.scl_in_state = true;
		m_i2c.sda_out_state = true;
		m_i2c.state = I2C_IDLE;
	}
}

uint8_t scc68070_device::iccr_r()
{
	// I2C clock control register: 80002009
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_I2C, "%s: I2C Clock Control Register Read: %02x\n", machine().describe_context(), m_i2c.clock_control_register);
	return m_i2c.clock_control_register | 0xe0;
}

void scc68070_device::iccr_w(uint8_t data)
{
	LOGMASKED(LOG_I2C, "%s: I2C Clock Control Register Write: %02x\n", machine().describe_context(), data);
	m_i2c.clock_control_register = data & 0x1f;
}


void scc68070_device::i2c_process_falling_scl()
{
	switch (m_i2c.state)
	{
		case I2C_TX_IN_PROGRESS:
			if (m_i2c.counter<8)
			{
				m_i2c.sda_out_state = BIT(m_i2c.data_register, 7 - m_i2c.counter);
				m_i2c_sdaw_callback(m_i2c.sda_out_state);
				m_i2c.counter++;
			}
			else
			{
				m_i2c.sda_out_state = true;
				m_i2c_sdaw_callback(true);
				m_i2c.state = I2C_GET_ACK;
			}
			break;

		case I2C_GET_ACK:
			m_i2c.status_register &= ~ISR_PIN;
			m_i2c_int = true;
			update_ipl();
			m_i2c.state = I2C_IDLE;
			if (m_i2c.first_byte)
			{
				m_i2c.first_byte = false;
				if (BIT(m_i2c.data_register, 0))
				{
					m_i2c.status_register &= ~ISR_TRX;
					if (!(m_i2c.status_register & ISR_LRB))
					{
						m_i2c.state = I2C_CHANGED_TO_RX;
					}
				}
			}
			break;

		case I2C_RX_IN_PROGRESS:
			if (m_i2c.counter >= 8)
			{
				m_i2c.status_register &= ~ISR_PIN;
				m_i2c_int = true;
				update_ipl();
				m_i2c.state = I2C_RX_COMPLETE;
			}
			break;

		case I2C_SEND_ACK_AND_RX:
			m_i2c.sda_out_state = true;
			m_i2c_sdaw_callback(true);
			m_i2c.state = I2C_RX_IN_PROGRESS;
			m_i2c.counter = 0;
			break;

		case I2C_SEND_ACK_AND_STOP:
			m_i2c.sda_out_state = false;
			m_i2c_sdaw_callback(false);
			m_i2c.state = I2C_SEND_STOP;
			break;

		case I2C_SEND_ACK:
			m_i2c.state = I2C_IDLE;
			m_i2c.status_register &= ~ISR_PIN;
			m_i2c_int = true;
			update_ipl();
			break;
	}
}

void scc68070_device::i2c_process_rising_scl()
{
	switch (m_i2c.state)
	{
		case I2C_GET_ACK:
			if (m_i2c_sdar_callback())
			{
				m_i2c.status_register |= ISR_LRB;
			}
			else
			{
				m_i2c.status_register &= ~ISR_LRB;
			}
			break;

		case I2C_SEND_STOP:
		case I2C_SEND_RESTART:
			m_i2c.timer->adjust(attotime::from_nsec(5000));
			break;

		case I2C_RX_IN_PROGRESS:
			if (m_i2c.counter < 8)
			{
				m_i2c.data_register <<= 1;
				m_i2c.data_register |= m_i2c_sdar_callback();
				m_i2c.counter++;
			}
			break;
	}
}

void scc68070_device::write_scl(int state)
{
	if (m_i2c.status_register & ISR_MST)
	{
		if (m_i2c.scl_in_state != state && state)
		{
			i2c_process_rising_scl();
			i2c_next_state();
		}
	}
	m_i2c.scl_in_state = state;
}

TIMER_CALLBACK_MEMBER(scc68070_device::i2c_callback)
{
	i2c_next_state();
}

void scc68070_device::i2c_next_state()
{
	switch (m_i2c.clock_change_state)
	{
		case I2C_SCL_SET_0:
			if (m_i2c.state == I2C_SEND_STOP)
			{
				if (!m_i2c.sda_out_state)
				{
					m_i2c.sda_out_state = true;
					m_i2c_sdaw_callback(true);
					set_i2c_timer();
				}
				else
				{
					m_i2c.state = I2C_IDLE;
					m_i2c.status_register &= ~(ISR_PIN | ISR_BB);
					m_i2c_int = true;
					update_ipl();
					m_i2c.clock_change_state = I2C_SCL_IDLE;
				}
			}
			else if (m_i2c.state == I2C_SEND_RESTART)
			{
				m_i2c.sda_out_state = false;
				m_i2c_sdaw_callback(false);
				set_i2c_timer();
				m_i2c.clock_change_state = I2C_SCL_SET_0;
				m_i2c.state = I2C_TX_IN_PROGRESS;
			}
			else
			{
				m_i2c.scl_out_state = false;
				m_i2c_scl_callback(false);
				if (m_i2c.clocks)
				{
					m_i2c.clocks--;
				}
				if (m_i2c.clocks == 0)
				{
					m_i2c.clock_change_state = I2C_SCL_IDLE;
				}
				else
				{
					set_i2c_timer();
					m_i2c.clock_change_state = I2C_SCL_SET_1;
				}
				i2c_process_falling_scl();
			}
			break;

		case I2C_SCL_SET_1:
			m_i2c.clock_change_state = I2C_SCL_WAIT_1;
			m_i2c.scl_out_state = true;
			m_i2c_scl_callback(true);
			break;

		case I2C_SCL_WAIT_1:
			set_i2c_timer();
			m_i2c.clock_change_state = I2C_SCL_SET_0;
			break;
	}
}

void scc68070_device::set_i2c_timer()
{
	// divider offset 0 entry is illegal
	static constexpr int divider[]={    1,   78,   90,  102,  126,  150,  174,    198,
									  246,  294,  342,  390,  486,  582,  678,    774,
									  996, 1158, 1350, 1542, 1926, 2310, 2694,   3078,
									 3846, 4614, 5382, 6150, 7686, 9222, 10758, 12294 };
	m_i2c.timer->adjust(cycles_to_attotime(divider[m_i2c.clock_control_register]));
}

uint8_t scc68070_device::umr_r()
{
	// UART mode register: 80002011
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_MORE_UART, "%s: UART Mode Register Read: %02x\n", machine().describe_context(), m_uart.mode_register);
	return m_uart.mode_register | 0x20;
}

void scc68070_device::umr_w(uint8_t data)
{
	LOGMASKED(LOG_MORE_UART, "%s: UART Mode Register Write: %02x\n", machine().describe_context(), data);
	m_uart.mode_register = data;
}

uint8_t scc68070_device::usr_r()
{
	// UART status register: 80002013
	if (!machine().side_effects_disabled())
	{
		m_uart.status_register |= (1 << 1);
		LOGMASKED(LOG_MORE_UART, "%s: UART Status Register Read: %02x\n", machine().describe_context(), m_uart.status_register);
	}
	return m_uart.status_register | 0x08; // hack for magicard
}

uint8_t scc68070_device::ucsr_r()
{
	// UART clock select register: 80002015
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_UART, "%s: UART Clock Select Read: %02x\n", machine().describe_context(), m_uart.clock_select);
	return m_uart.clock_select | 0x08;
}

void scc68070_device::ucsr_w(uint8_t data)
{
	LOGMASKED(LOG_UART, "%s: UART Clock Select Write: %02x\n", machine().describe_context(), data);
	m_uart.clock_select = data;

	static const uint32_t s_baud_divisors[8] = { 65536, 32768, 16384, 4096, 2048, 1024, 512, 256 };

	attotime rx_rate = attotime::from_ticks(s_baud_divisors[(data >> 4) & 7] * 10, 49152000);
	attotime tx_rate = attotime::from_ticks(s_baud_divisors[data & 7] * 10, 49152000);
	m_uart.rx_timer->adjust(rx_rate, 0, rx_rate);
	m_uart.tx_timer->adjust(tx_rate, 0, tx_rate);
}

uint8_t scc68070_device::ucr_r()
{
	// UART command register: 80002017
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_UART, "%s: UART Command Register Read: %02x\n", machine().describe_context(), m_uart.command_register);
	return m_uart.command_register | 0x80;
}

void scc68070_device::ucr_w(uint8_t data)
{
	LOGMASKED(LOG_MORE_UART, "%s: UART Command Register Write: %02x\n", machine().describe_context(), data);
	m_uart.command_register = data;
	const uint8_t misc_command = (data & 0x70) >> 4;
	switch (misc_command)
	{
	case 0x2: // Reset receiver
		LOGMASKED(LOG_MORE_UART, "%s: Reset receiver\n", machine().describe_context());
		m_uart.receive_pointer = -1;
		m_uart.command_register &= 0xf0;
		m_uart.receive_holding_register = 0x00;
		break;
	case 0x3: // Reset transmitter
		LOGMASKED(LOG_MORE_UART, "%s: Reset transmitter\n", machine().describe_context());
		m_uart.transmit_pointer = -1;
		m_uart.status_register |= USR_TXEMT;
		m_uart.command_register &= 0xf0;
		m_uart.transmit_holding_register = 0x00;
		break;
	case 0x4: // Reset error status
		LOGMASKED(LOG_MORE_UART, "%s: Reset error status\n", machine().describe_context());
		m_uart.status_register &= 0x87; // Clear error bits in USR
		m_uart.command_register &= 0xf0;
		break;
	case 0x6: // Start break
		LOGMASKED(LOG_MORE_UART, "%s: Start break (not yet implemented)\n", machine().describe_context());
		break;
	case 0x7: // Stop break
		LOGMASKED(LOG_MORE_UART, "%s: Stop break (not yet implemented)\n", machine().describe_context());
		break;
	}
}

uint8_t scc68070_device::uth_r()
{
	// UART transmit holding register: 80002019
	if (!machine().side_effects_disabled())
		LOGMASKED(LOG_UART, "%s: UART Transmit Holding Register Read: %02x\n", machine().describe_context(), m_uart.transmit_holding_register);
	return m_uart.transmit_holding_register;
}

void scc68070_device::uth_w(uint8_t data)
{
	LOGMASKED(LOG_MORE_UART, "%s: UART Transmit Holding Register Write: %02x ('%c')\n", machine().describe_context(), data, (data >= 0x20 && data < 0x7f) ? data : ' ');
	uart_tx(data);
	m_uart.transmit_holding_register = data;
}

uint8_t scc68070_device::urh_r()
{
	// UART receive holding register: 8000201b
	if (!machine().side_effects_disabled())
	{
		LOGMASKED(LOG_UART, "%s: UART Receive Holding Register Read: %02x\n", machine().describe_context(), m_uart.receive_holding_register);

		if (m_uart_rx_int)
		{
			m_uart_rx_int = false;
			update_ipl();
		}

		m_uart.receive_holding_register = m_uart.receive_buffer[0];
		if (m_uart.receive_pointer >= 0)
		{
			for(int index = 0; index < m_uart.receive_pointer; index++)
			{
				m_uart.receive_buffer[index] = m_uart.receive_buffer[index + 1];
			}
			m_uart.receive_pointer--;
		}
	}
	return m_uart.receive_holding_register;
}

uint16_t scc68070_device::timer_r(offs_t offset, uint16_t mem_mask)
{
	switch (offset)
	{
	// Timers: 80002020 to 80002029
	case 0x0/2:
		if (ACCESSING_BITS_0_7 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_TIMERS, "%s: Timer Control Register Read: %02x & %04x\n", machine().describe_context(), m_timers.timer_control_register, mem_mask);
		}
		if (ACCESSING_BITS_8_15 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_TIMERS_HF, "%s: Timer Status Register Read: %02x & %04x\n", machine().describe_context(), m_timers.timer_status_register, mem_mask);
		}
		return (m_timers.timer_status_register << 8) | m_timers.timer_control_register;
	case 0x2/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_TIMERS, "%s: Timer Reload Register Read: %04x & %04x\n", machine().describe_context(), m_timers.reload_register, mem_mask);
		return m_timers.reload_register;
	case 0x4/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_TIMERS, "%s: Timer 0 Read: %04x & %04x\n", machine().describe_context(), m_timers.timer0, mem_mask);
		return 0x10000 - (attotime_to_cycles(m_timers.timer0_timer->remaining()) / 96);

	case 0x6/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_TIMERS, "%s: Timer 1 Read: %04x & %04x\n", machine().describe_context(), m_timers.timer1, mem_mask);
		return m_timers.timer1;
	case 0x8/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_TIMERS, "%s: Timer 2 Read: %04x & %04x\n", machine().describe_context(), m_timers.timer2, mem_mask);
		return m_timers.timer2;
	default:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_TIMERS | LOG_UNKNOWN, "%s: Timer Unknown Register Read: %04x & %04x\n", machine().describe_context(), offset * 2, mem_mask);
		break;
	}

	return 0;
}

void scc68070_device::timer_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
	// Timers: 80002020 to 80002029
	case 0x0/2:
		if (ACCESSING_BITS_0_7)
		{
			LOGMASKED(LOG_TIMERS, "%s: Timer Control Register Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
			m_timers.timer_control_register = data & 0x00ff;
		}
		if (ACCESSING_BITS_8_15)
		{
			LOGMASKED(LOG_TIMERS_HF, "%s: Timer Status Register Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
			m_timers.timer_status_register &= ~(data >> 8);
		}
		break;
	case 0x2/2:
		LOGMASKED(LOG_TIMERS, "%s: Timer Reload Register Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
		COMBINE_DATA(&m_timers.reload_register);
		set_timer_callback(0);
		break;
	case 0x4/2:
		LOGMASKED(LOG_TIMERS, "%s: Timer 0 Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
		COMBINE_DATA(&m_timers.timer0);
		set_timer_callback(0);
		break;
	case 0x6/2:
		LOGMASKED(LOG_TIMERS, "%s: Timer 1 Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
		COMBINE_DATA(&m_timers.timer1);
		break;
	case 0x8/2:
		LOGMASKED(LOG_TIMERS, "%s: Timer 2 Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
		COMBINE_DATA(&m_timers.timer2);
		break;
	default:
		LOGMASKED(LOG_TIMERS | LOG_UNKNOWN, "%s: Timer Unknown Register Write: %04x = %04x & %04x\n", machine().describe_context(), offset * 2, data, mem_mask);
		break;
	}
}

uint16_t scc68070_device::dma_r(offs_t offset, uint16_t mem_mask)
{
	switch (offset)
	{
	// DMA controller: 80004000 to 8000406d
	case 0x00/2:
	case 0x40/2:
		if (ACCESSING_BITS_0_7 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Error Register Read: %04x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].channel_error, mem_mask);
		}
		if (ACCESSING_BITS_8_15 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Status Register Read: %04x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].channel_status, mem_mask);
		}
		return (m_dma.channel[offset / 32].channel_status << 8) | m_dma.channel[offset / 32].channel_error;
	case 0x04/2:
	case 0x44/2:
		if (ACCESSING_BITS_0_7 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Operation Control Register Read: %02x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].operation_control, mem_mask);
		}
		if (ACCESSING_BITS_8_15 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Control Register Read: %02x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].device_control, mem_mask);
		}
		return (m_dma.channel[offset / 32].device_control << 8) | m_dma.channel[offset / 32].operation_control;
	case 0x06/2:
	case 0x46/2:
		if (ACCESSING_BITS_0_7 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Channel Control Register Read: %02x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].channel_control, mem_mask);
		}
		if (ACCESSING_BITS_8_15 && !machine().side_effects_disabled())
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Sequence Control Register Read: %02x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].sequence_control, mem_mask);
		}
		return (m_dma.channel[offset / 32].sequence_control << 8) | m_dma.channel[offset / 32].channel_control;
	case 0x0a/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Transfer Counter Read: %04x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].transfer_counter, mem_mask);
		return m_dma.channel[offset / 32].transfer_counter;
	case 0x0c/2:
	case 0x4c/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Address Counter (High Word) Read: %04x & %04x\n", machine().describe_context(), offset / 32, (m_dma.channel[offset / 32].memory_address_counter >> 16), mem_mask);
		return (m_dma.channel[offset / 32].memory_address_counter >> 16);
	case 0x0e/2:
	case 0x4e/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Address Counter (Low Word) Read: %04x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].memory_address_counter, mem_mask);
		return m_dma.channel[offset / 32].memory_address_counter;
	case 0x14/2:
	case 0x54/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Address Counter (High Word) Read: %04x & %04x\n", machine().describe_context(), offset / 32, (m_dma.channel[offset / 32].device_address_counter >> 16), mem_mask);
		return (m_dma.channel[offset / 32].device_address_counter >> 16);
	case 0x16/2:
	case 0x56/2:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Address Counter (Low Word) Read: %04x & %04x\n", machine().describe_context(), offset / 32, m_dma.channel[offset / 32].device_address_counter, mem_mask);
		return m_dma.channel[offset / 32].device_address_counter;

	default:
		LOGMASKED(LOG_DMA | LOG_UNKNOWN, "%s: DMA Unknown Register Read: %04x & %04x\n", machine().describe_context(), offset * 2, mem_mask);
		break;
	}

	return 0;
}

void scc68070_device::dma_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
	// DMA controller: 80004000 to 8000406d
	case 0x00/2:
	case 0x40/2:
		if (ACCESSING_BITS_0_7)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Error (invalid) Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		}
		if (ACCESSING_BITS_8_15)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Status Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
			m_dma.channel[offset / 32].channel_status &= ~((data >> 8) & 0xb0);
			update_ipl();
		}
		break;
	case 0x04/2:
	case 0x44/2:
		if (ACCESSING_BITS_0_7)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Operation Control Register Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
			m_dma.channel[offset / 32].operation_control = data & 0x00ff;
		}
		if (ACCESSING_BITS_8_15)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Control Register Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
			m_dma.channel[offset / 32].device_control = data >> 8;
		}
		break;
	case 0x06/2:
	case 0x46/2:
		if (ACCESSING_BITS_0_7)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Channel Control Register Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
			m_dma.channel[offset / 32].channel_control = data & 0x007f;
			if (data & CCR_SO)
			{
				m_dma.channel[offset / 32].channel_status |= CSR_COC;
			}
			update_ipl();
		}
		if (ACCESSING_BITS_8_15)
		{
			LOGMASKED(LOG_DMA, "%s: DMA(%d) Sequence Control Register Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
			m_dma.channel[offset / 32].sequence_control = data >> 8;
		}
		break;
	case 0x0a/2:
		LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Transfer Counter Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		COMBINE_DATA(&m_dma.channel[offset / 32].transfer_counter);
		break;
	case 0x0c/2:
	case 0x4c/2:
		LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Address Counter (High Word) Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		m_dma.channel[offset / 32].memory_address_counter &= ~(mem_mask << 16);
		m_dma.channel[offset / 32].memory_address_counter |= data << 16;
		break;
	case 0x0e/2:
	case 0x4e/2:
		LOGMASKED(LOG_DMA, "%s: DMA(%d) Memory Address Counter (Low Word) Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		m_dma.channel[offset / 32].memory_address_counter &= ~mem_mask;
		m_dma.channel[offset / 32].memory_address_counter |= data;
		break;
	case 0x14/2:
	case 0x54/2:
		LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Address Counter (High Word) Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		m_dma.channel[offset / 32].device_address_counter &= ~(mem_mask << 16);
		m_dma.channel[offset / 32].device_address_counter |= data << 16;
		break;
	case 0x16/2:
	case 0x56/2:
		LOGMASKED(LOG_DMA, "%s: DMA(%d) Device Address Counter (Low Word) Write: %04x & %04x\n", machine().describe_context(), offset / 32, data, mem_mask);
		m_dma.channel[offset / 32].device_address_counter &= ~mem_mask;
		m_dma.channel[offset / 32].device_address_counter |= data;
		break;
	default:
		LOGMASKED(LOG_DMA | LOG_UNKNOWN, "%s: DMA Unknown Register Write: %04x = %04x & %04x\n", machine().describe_context(), offset * 2, data, mem_mask);
		break;
	}
}

uint16_t scc68070_device::mmu_r(offs_t offset, uint16_t mem_mask)
{
	switch (offset)
	{
	// MMU: 80008000 to 8000807f
	case 0x00/2:  // Status / Control register
		if (ACCESSING_BITS_0_7)
		{   // Control
			if (!machine().side_effects_disabled())
				LOGMASKED(LOG_MMU, "%s: MMU Control Read: %02x & %04x\n", machine().describe_context(), m_mmu.control, mem_mask);
			return m_mmu.control;
		}   // Status
		else
		{
			if (!machine().side_effects_disabled())
				LOGMASKED(LOG_MMU, "%s: MMU Status Read: %02x & %04x\n", machine().describe_context(), m_mmu.status, mem_mask);
			return m_mmu.status;
		}
	case 0x40/2:
	case 0x48/2:
	case 0x50/2:
	case 0x58/2:
	case 0x60/2:
	case 0x68/2:
	case 0x70/2:
	case 0x78/2:  // Attributes (SD0-7)
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_MMU, "%s: MMU descriptor %d attributes Read: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, m_mmu.desc[(offset - 0x20) / 4].attr, mem_mask);
		return m_mmu.desc[(offset - 0x20) / 4].attr;
	case 0x42/2:
	case 0x4a/2:
	case 0x52/2:
	case 0x5a/2:
	case 0x62/2:
	case 0x6a/2:
	case 0x72/2:
	case 0x7a/2:  // Segment Length (SD0-7)
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_MMU, "%s: MMU descriptor %d length Read: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, m_mmu.desc[(offset - 0x20) / 4].length, mem_mask);
		return m_mmu.desc[(offset - 0x20) / 4].length;
	case 0x44/2:
	case 0x4c/2:
	case 0x54/2:
	case 0x5c/2:
	case 0x64/2:
	case 0x6c/2:
	case 0x74/2:
	case 0x7c/2:  // Segment Number (SD0-7, A0=1 only)
		if (ACCESSING_BITS_0_7)
		{
			if (!machine().side_effects_disabled())
				LOGMASKED(LOG_MMU, "%s: MMU descriptor %d segment Read: %02x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, m_mmu.desc[(offset - 0x20) / 4].segment, mem_mask);
			return m_mmu.desc[(offset - 0x20) / 4].segment;
		}
		break;
	case 0x46/2:
	case 0x4e/2:
	case 0x56/2:
	case 0x5e/2:
	case 0x66/2:
	case 0x6e/2:
	case 0x76/2:
	case 0x7e/2:  // Base Address (SD0-7)
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_MMU, "%s: MMU descriptor %d base Read: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, m_mmu.desc[(offset - 0x20) / 4].base, mem_mask);
		return m_mmu.desc[(offset - 0x20) / 4].base;
	default:
		if (!machine().side_effects_disabled())
			LOGMASKED(LOG_MMU | LOG_UNKNOWN, "%s: MMU Unknown Register Read: %04x & %04x\n", machine().describe_context(), offset * 2, mem_mask);
		break;
	}

	return 0;
}

void scc68070_device::mmu_w(offs_t offset, uint16_t data, uint16_t mem_mask)
{
	switch (offset)
	{
	// MMU: 80008000 to 8000807f
	case 0x00/2:  // Status / Control register
		if (ACCESSING_BITS_0_7)
		{   // Control
			LOGMASKED(LOG_MMU, "%s: MMU Control Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
			m_mmu.control = data & 0x00ff;
		}   // Status
		else
		{
			LOGMASKED(LOG_MMU, "%s: MMU Status (invalid) Write: %04x & %04x\n", machine().describe_context(), data, mem_mask);
		}
		break;
	case 0x40/2:
	case 0x48/2:
	case 0x50/2:
	case 0x58/2:
	case 0x60/2:
	case 0x68/2:
	case 0x70/2:
	case 0x78/2:  // Attributes (SD0-7)
		LOGMASKED(LOG_MMU, "%s: MMU descriptor %d attributes Write: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, data, mem_mask);
		COMBINE_DATA(&m_mmu.desc[(offset - 0x20) / 4].attr);
		break;
	case 0x42/2:
	case 0x4a/2:
	case 0x52/2:
	case 0x5a/2:
	case 0x62/2:
	case 0x6a/2:
	case 0x72/2:
	case 0x7a/2:  // Segment Length (SD0-7)
		LOGMASKED(LOG_MMU, "%s: MMU descriptor %d length Write: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, data, mem_mask);
		COMBINE_DATA(&m_mmu.desc[(offset - 0x20) / 4].length);
		break;
	case 0x44/2:
	case 0x4c/2:
	case 0x54/2:
	case 0x5c/2:
	case 0x64/2:
	case 0x6c/2:
	case 0x74/2:
	case 0x7c/2:  // Segment Number (SD0-7, A0=1 only)
		if (ACCESSING_BITS_0_7)
		{
			LOGMASKED(LOG_MMU, "%s: MMU descriptor %d segment Write: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, data, mem_mask);
			m_mmu.desc[(offset - 0x20) / 4].segment = data & 0x00ff;
		}
		break;
	case 0x46/2:
	case 0x4e/2:
	case 0x56/2:
	case 0x5e/2:
	case 0x66/2:
	case 0x6e/2:
	case 0x76/2:
	case 0x7e/2:  // Base Address (SD0-7)
		LOGMASKED(LOG_MMU, "%s: MMU descriptor %d base Write: %04x & %04x\n", machine().describe_context(), (offset - 0x20) / 4, data, mem_mask);
		COMBINE_DATA(&m_mmu.desc[(offset - 0x20) / 4].base);
		break;
	default:
		LOGMASKED(LOG_MMU | LOG_UNKNOWN, "%s: Unknown Register Write: %04x = %04x & %04x\n", machine().describe_context(), offset * 2, data, mem_mask);
		break;
	}
}

#if ENABLE_UART_PRINTING
uint16_t scc68070_device::uart_loopback_enable()
{
	return 0x1234;
}
#endif