# SPDX-License-Identifier: MIT """The I/O register table: which device answers each halfword of 04000000-040003FE. GBATEK §GBA I/O Map (DOC); docs/memory-map.md "I/O map". Each device's ``read16`` returns the readable bits of a halfword, or None when the halfword is write-only or unused; the bus then applies GBATEK's open-bus rule for I/O (gba_python.bus.Bus.read_io). Writes arrive as a halfword with a byte mask (0x00FF, 0xFF00 or 0xFFFF); a 32-bit write is the low halfword then the high one, at the same cycle (which gives GBATEK's "single 32bit I/O operation" timer rule). """ from __future__ import annotations class IO: def __init__(self, board) -> None: self.board = board self.ppu = board.ppu self.sound = board.sound self.dma = board.dma self.timers = board.timers self.serial = board.serial self.keypad = board.keypad self.irq = board.irq self.bus = board.bus self.reg_410 = 0 # the BIOS writes FF; purpose unknown (GBATEK §GBA I/O Map; DOC) def device(self, offset: int): if offset < 0x60: return self.ppu if offset < 0xB0: return self.sound if offset < 0xE0: return self.dma if 0x100 <= offset < 0x110: return self.timers if 0x120 <= offset < 0x130 or 0x134 <= offset < 0x160: return self.serial if 0x130 <= offset < 0x134: return self.keypad if offset in (0x200, 0x202, 0x208, 0x300): return self.irq return None def read16(self, offset: int) -> int | None: if offset == 0x204: return self.bus.waitcnt if offset in (0x206, 0x20A, 0x302): return 0 # unused halves beside readable ones (mGBA suite io-read.c; TEST ROM) device = self.device(offset) if device is None: return None return device.read16(offset) def write16(self, offset: int, value: int, mask: int, time: int) -> None: if offset == 0x204: self.bus.write_waitcnt((self.bus.waitcnt & ~mask) | (value & mask)) return if offset == 0x410: self.reg_410 = (self.reg_410 & ~mask) | (value & mask) return device = self.device(offset) if device is not None: device.write16(offset, value, mask, time)