# SPDX-License-Identifier: MIT """The S-CPU side: $6000-$7FFF, the I/O block, the vectors, the register file and /IRQ. The chip decodes A0-A23 itself (pins 1-24), so the SNES-side map is the core's (handoff decision 2): the board routes banks 00-3F/80-BF $6000-$7FFF here and the core ignores the bank. The map (U11; fullsnes "I/O Map"; byuu's glue, ares ``sfc/coprocessor/hitachidsp/memory.cpp`` l.117-268, ISC): - $6000-$6BFF and its mirror $7000-$7BFF: the data RAM's 3,072 bytes. - $6C00-$6FFF and $7C00-$7FFF: the I/O block, 1 KiB decoded on A9-A0 (ares masks to 3FFh), so $6F40 is $7F40. Only $7F40-$7FAF and $7FC0-$7FEF answer; the rest read 0. - $7F40-$7F5F the registers below, $7F60-$7F7F the 32 vector bytes (U16), $7F80-$7FAF the sixteen 24-bit registers R0-R15 as three little-endian bytes each (mirror $7FC0-$7FEF). A read has no side effect: the S-CPU can poll anything. Writes to $7F47, $7F48 and $7F4F start a DMA, a cache preload or the program **when the chip is halted** (U12). """ from __future__ import annotations # The status register's bits ($7F5E; U12). STATUS_SUSPENDED = 0x01 STATUS_IRQ = 0x02 STATUS_RUNNING = 0x40 STATUS_BUSY = 0x80 class HostMixin: #: C13. "ares" (default; byuu's glue, ares ``memory.cpp`` l.164-166): $7F53-$7F5F read #: the status except $7F58 and $7F5A, which read 0. "all-observed": all of $7F53-$7F5F #: read it, as a closed-box Mesen2 run shows (OBSERVED, ``scripts/cx4_probe.py #: mirror``). The games read $7F5E only. STATUS_MIRROR = "ares" #: C9: writing $7F48 while halted sets PB from $7F4D-$7F4E and loads that page into the #: selected cache page even if it is already cached (a forced preload: byuu's glue, #: ares ``memory.cpp`` l.201-208). No switch: closed-box Mesen2 runs differ (no load #: when the page is cached) but do not show enough to model (C9, OBSERVED). #: C18. False (default; byuu's glue, ares ``memory.cpp`` l.236): writing $7F51 with #: bit 0 = 1 clears the IRQ flag and drops /IRQ. True: it *sets* the flag (status #: bit 1) while dropping /IRQ, as the closed-box Mesen2 replays show (OBSERVED, rung 4: #: the games' $7F5E polls answer 02h there). IRQ_DISABLE_SETS_FLAG = False # ----- the S-CPU's accesses --------------------------------------------------------- def read(self, address: int) -> int: """The S-CPU reads $6000-$7FFF (any bank the board routes here). No side effect.""" offset = address & 0x1FFF if offset & 0x0C00 != 0x0C00: return self.data_ram[offset & 0xFFF] return self._read_io(offset & 0x3FF) def write(self, address: int, value: int) -> None: """The S-CPU writes $6000-$7FFF.""" offset = address & 0x1FFF value &= 0xFF if offset & 0x0C00 != 0x0C00: self.data_ram[offset & 0xFFF] = value return self._write_io(offset & 0x3FF, value) @property def status(self) -> int: """$7F5E: bit 0 suspended, bit 1 the IRQ flag, bit 6 running, bit 7 busy.""" return ( (STATUS_SUSPENDED if self.suspend_enabled else 0) | (STATUS_IRQ if self.irq_flag else 0) | (STATUS_RUNNING if self.running else 0) | (STATUS_BUSY if self.owns_rom_bus else 0) ) @property def running(self) -> bool: """A cache load, DMA or bus access in flight, or the program not halted (ares ``hg51b.cpp`` l.117 ``running()``).""" return self.cache_enabled or self.dma_enabled or self.bus_pending > 0 or not self.halted @property def owns_rom_bus(self) -> bool: """The chip drives the ROM bus: a cache load, a DMA or a bus access in flight (U16; ares ``hg51b.cpp`` l.121 ``busy()``). While true the board blocks S-CPU ROM reads and serves $00:FFE0-$FFFF from :meth:`vector` (C12 is the board's switch).""" return self.cache_enabled or self.dma_enabled or self.bus_pending > 0 @property def irq(self) -> bool: """/IRQ asserted (U13): raised by HALT while $7F51 bit 0 = 0, dropped by writing $7F51 with bit 0 = 1. Writing $7F5E clears the status flag but not the line.""" return self.irq_line def vector(self, offset: int) -> int: """The byte the S-CPU reads at $00:FFE0 + ``offset`` while :attr:`owns_rom_bus`: $7F60 + offset (U16; fullsnes "Exception Vectors"; ares ``memory.cpp`` l.42-52 ``readROM``). ares also answers $00:FFC0-$FFDF from $7F40-$7F5F then; a board wanting that reads ``read(0x7F40 + n)``, which has no side effect.""" return self.vectors[offset & 0x1F] # ----- the I/O block ---------------------------------------------------------------- def _read_io(self, index: int) -> int: """``index`` = A9-A0 of the I/O block; $7C00 + index is the register.""" if index < 0x340: # $7C00-$7F3F: nothing answers return 0 if index < 0x353: return self._read_register_byte(index) if index < 0x360: # $7F53-$7F5F: the status and its mirrors (C13) if self.STATUS_MIRROR == "ares" and index in (0x358, 0x35A): return 0 return self.status if index < 0x380: return self.vectors[index & 0x1F] if index < 0x3B0 or 0x3C0 <= index < 0x3F0: index &= 0x3F return self.r[index // 3] >> (8 * (index % 3)) & 0xFF return 0 def _read_register_byte(self, index: int) -> int: """$7F40-$7F52 read back what was written (the INTERFACE REGISTER test).""" if index < 0x343: return self.dma_source >> (8 * (index - 0x340)) & 0xFF if index < 0x345: return self.dma_length >> (8 * (index - 0x343)) & 0xFF if index < 0x348: return self.dma_target >> (8 * (index - 0x345)) & 0xFF if index == 0x348: return self.cache_page if index < 0x34C: return self.cache_base >> (8 * (index - 0x349)) & 0xFF if index == 0x34C: return int(self.cache_lock[0]) | int(self.cache_lock[1]) << 1 if index == 0x34D: return self.cache_pb & 0xFF if index == 0x34E: return self.cache_pb >> 8 if index == 0x34F: return self.cache_pc if index == 0x350: return self.wait_ram | self.wait_rom << 4 if index == 0x351: return int(self.irq_disabled) return int(self.single_rom) # $7F52 def _write_io(self, index: int, value: int) -> None: if index < 0x340: return if index < 0x360: self._write_register(0x7C00 | index, value) elif index < 0x380: self.vectors[index & 0x1F] = value elif index < 0x3B0 or 0x3C0 <= index < 0x3F0: index &= 0x3F shift = 8 * (index % 3) r = self.r r[index // 3] = r[index // 3] & ~(0xFF << shift) & 0xFFFFFF | value << shift def _write_register(self, address: int, value: int) -> None: """$7F40-$7F5F (byuu's glue, ares ``memory.cpp`` ``writeIO`` l.183-268).""" if address <= 0x7F42: # DMA source shift = 8 * (address - 0x7F40) self.dma_source = self.dma_source & ~(0xFF << shift) & 0xFFFFFF | value << shift elif address <= 0x7F44: # DMA length (16 bits) shift = 8 * (address - 0x7F43) self.dma_length = self.dma_length & ~(0xFF << shift) & 0xFFFF | value << shift elif address <= 0x7F47: # DMA target; writing its top byte starts the DMA shift = 8 * (address - 0x7F45) self.dma_target = self.dma_target & ~(0xFF << shift) & 0xFFFFFF | value << shift if address == 0x7F47 and self.halted: self.dma_enabled = True self._dma_pos = 0 elif address == 0x7F48: # cache page select; preload when halted (C9) self.cache_page = value & 1 if self.halted: self.pb = self.cache_pb self.cache_preload = True self.cache_enabled = True self._cache_pos = -1 # address and page are decided at the next clock elif address <= 0x7F4B: # cache base address (bytes, 24 bits) shift = 8 * (address - 0x7F49) self.cache_base = self.cache_base & ~(0xFF << shift) & 0xFFFFFF | value << shift elif address == 0x7F4C: # cache page locks self.cache_lock[0] = bool(value & 1) self.cache_lock[1] = bool(value & 2) elif address == 0x7F4D: self.cache_pb = self.cache_pb & 0x7F00 | value elif address == 0x7F4E: # PB is 15 bits self.cache_pb = self.cache_pb & 0x00FF | (value & 0x7F) << 8 elif address == 0x7F4F: # entry PC; starts the program when halted (U12) self.cache_pc = value if self.halted: self.halted = False self.pb = self.cache_pb self.pc = value elif address == 0x7F50: # wait states: RAM bits 2-0, ROM bits 6-4 self.wait_ram = value & 7 self.wait_rom = value >> 4 & 7 elif address == 0x7F51: # bit 0: 1 = IRQ disabled; disabling drops /IRQ (U13) self.irq_disabled = bool(value & 1) if self.irq_disabled: self.irq_line = False self.irq_flag = self.IRQ_DISABLE_SETS_FLAG # C18 elif address == 0x7F52: # one ROM (1) or two (0) (C9); the games write 00h self.single_rom = bool(value & 1) elif address == 0x7F53: # stop, and leave the DMA lock (U15) self.lock = False self.halted = True elif 0x7F55 <= address <= 0x7F5C: # suspend: 0 = until $7F5D, else 32 x n clocks self.suspend_enabled = True self.suspend_duration = (address - 0x7F55) * 32 elif address == 0x7F5D: # resume self.suspend_enabled = False elif address == 0x7F5E: # clear the IRQ flag; /IRQ stays (ares memory.cpp l.260-262 comment) self.irq_flag = False __all__ = ["STATUS_BUSY", "STATUS_IRQ", "STATUS_RUNNING", "STATUS_SUSPENDED", "HostMixin"]