# SPDX-License-Identifier: MIT AND ISC """The S-CPU board: the A bus, one master-clock timeline, CPU I/O and DMA. The 65816 core (65816-python, package ``w65c816``) makes one callback per CPU cycle; inside it the board charges the cycle's length (6, 8 or 12 master clocks by address and MEMSEL), advances every unit on the timeline, performs the access, and runs DMA between cycles (docs/timing.md "The design"). Sources. DOC: anomie timing (``reference/docs/anomie_timing.txt``), fullsnes ("SNES Memory Map", "SNES I/O Map", "CPU Clock Cycles", "4200h-421Fh"), the SNESdev wiki (Timing, CPU registers). Event positions inside the line, the order of the polls and the DMA alignment follow ares 9408cb4 ``sfc/cpu`` (``timing.cpp``, ``irq.cpp``, ``memory.cpp``, ``io.cpp``, ``cpu.cpp``) and ``sfc/ppu/counter`` (ISC, notice below and in NOTICE); each such rule is tagged OBSERVED (ares) and names the function. MEASURED rules come from this project's console (tests/console, docs/probe-suite/) and overrule ares where they differ. ares notice (ISC): Copyright (c) 2004-2025 ares team, Near et al. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. """ from __future__ import annotations from w65c816 import W65C816 from snes_python.apu import APU, PortStub from snes_python.cartridge import Cartridge from snes_python.controllers import Gamepad from snes_python.dma import DMAController from snes_python.machines import NTSC, Machine from snes_python.ppu import PPU class CPUIO: """$4200-$421F state and the multiplier/divider (DOC: fullsnes).""" __slots__ = ( "auto_joypad", "divctr", "hirq_enable", "htime", "irq_enable", "joy", "mpyctr", "nmi_enable", "rddiv", "rdmpy", "rom_speed", "shift", "virq_enable", "vtime", "wrdiva", "wrdivb", "wrmpya", "wrmpyb", ) def __init__(self) -> None: self.nmi_enable = False self.hirq_enable = False self.virq_enable = False self.irq_enable = False self.auto_joypad = False # DOC: fullsnes "4202h-4206h": $FF after power-on. self.wrmpya = 0xFF self.wrmpyb = 0xFF self.wrdiva = 0xFFFF self.wrdivb = 0xFF # HTIME/VTIME $1FF after power-on (DOC: fullsnes "4207h-420Ah"). # HTIME is kept as ares keeps it, (H + 1) * 4, the H counter value # (10 clocks back) it is compared with (OBSERVED: ares io.cpp). self.htime = (0x1FF + 1) << 2 self.vtime = 0x1FF # MEMSEL 0: slow ROM, 8 clocks (DOC: fullsnes "420Dh - MEMSEL"). self.rom_speed = 8 self.rddiv = 0 self.rdmpy = 0 self.joy = [0, 0, 0, 0] self.mpyctr = 0 self.divctr = 0 self.shift = 0 class SNES: """A Super Nintendo: S-CPU, S-WRAM, the PPU registers, a cartridge.""" # FITTED: polls (4 clocks each) between /IRQ rising and the core seeing # it, beyond ares's one. MEASURED on the console (probe suite HVLT, IRQN, # RFSH, 2026-10-25): with ares's timing the board latched H one dot early # in every row; one more poll puts it on the console's dot. Tried: 0 # (ares). Which side of the line it belongs to (the timer or the CPU's # response) is open: a probe that reads TIMEUP in a tight loop would say. IRQ_EXTRA_POLLS = 1 def __init__( self, cartridge: Cartridge, machine: Machine = NTSC, apu: PortStub | APU | None = None, ipl: bytes | None = None, ) -> None: self.machine = machine self.cart = cartridge # HYPOTHESIS: WRAM powers on as zeros (real DRAM holds a pattern that # varies by unit; ares randomises it). self.wram = bytearray(0x20000) self.ppu = PPU(self, machine.ppu1_version, machine.ppu2_version) self.dma = DMAController(self) # Milestone 3: with an IPL ROM (bring your own: SNES_IPL, or MAME's # snes.zip) the S-SMP and S-DSP run; without one, the port stub. if apu is None: apu = APU(ipl, machine.master_hz, machine.apu_hz) if ipl is not None else PortStub() self.apu = apu self.ports = [Gamepad(), Gamepad()] self.io = CPUIO() self.cpu = W65C816(self._read, self._write, self._idle) self._power() # -- power ------------------------------------------------------------------- def _power(self) -> None: self.mdr = 0 self.wram_address = 0 self.wrio = 0xFF # DOC: fullsnes "4201h - WRIO": $FF after power-on. # The timeline (OBSERVED: ares PPUcounter::reset, CPU::power). self.clock = 0 self.h = 0 self.v = 0 self.field = 0 self.interlace = False self.hperiod = 1364 self.vperiod = 312 if self.machine.pal else 262 self.last_hperiod = 1364 self.last_vperiod = 0 self.frame = 0 self.clock_count = 0 # DOC: anomie timing l.68-72: refresh "begins at 538 cycles into the # first scanline of the first frame"; OBSERVED (ares CPU::power): 530 # on CPU version 1. self.refresh_pos = 530 if self.machine.cpu_version == 1 else 538 self.refreshed = False self.hdma_setup_pos = self._hdma_setup_position() self.hdma_setup_triggered = False self.hdma_pos = 1104 self.hdma_triggered = False self.nmi_valid = False self.nmi_line = False self.nmi_hold = False self.irq_valid = False self.irq_line = False self.irq_hold = False self.irq_wait = 0 self.core_irq = False self.in_nmitimen = False self.deferred_nmi = False self.deferred_irq = False self.dma_active = False self.dma_pending = False self.hdma_pending = False self.hdma_mode = 0 self.dma_counter = 0 self.auto_joypad_counter = 33 self.auto_joypad_port1 = 0 self.auto_joypad_port2 = 0 self.cpu_latch = False self.auto_joypad_latch = False # OBSERVED (ares CPU::main): reset waits 132 clocks before the vector # sequence (the CPU starts the program at H=186 in ares; probe suite # RSET measures it on the console: docs/probe-suite/timing-alu-ppu.md). self.step(132) self.cpu.request_reset() def _hdma_setup_position(self) -> int: # OBSERVED (ares CPU::scanline): the frame's HDMA init at H 12 + 8 - # phase (version 1) or 12 + phase (version 2), phase = the DMA divider. phase = self.clock & 7 return 12 + 8 - phase if self.machine.cpu_version == 1 else 12 + phase # -- the counters -------------------------------------------------------- def hdot(self) -> int: """The H counter in dots (DOC: anomie timing "S-PPU Rendering": dots 323 and 327 are 6 clocks, except on the short line; ares hdot()).""" h = self.h if self.hperiod == 1360: return h >> 2 return (h - ((h > 1292) << 1) - ((h > 1310) << 1)) >> 2 def vcounter_ago(self, offset: int) -> int: """V as it was ``offset`` clocks ago (OBSERVED: ares vcounter(n)).""" if offset <= self.h: return self.v if self.v > 0: return self.v - 1 return self.last_vperiod - 1 def hcounter_ago(self, offset: int) -> int: if offset <= self.h: return self.h - offset return self.h + self.last_hperiod - offset # -- the timeline ---------------------------------------------------------- def step(self, clocks: int) -> None: """Advance ``clocks`` master clocks (even), with every event on them. OBSERVED (ares CPU::step): time moves 2 clocks at a time; NMI and IRQ are polled when H is 2 mod 4; the auto-joypad unit every 128 clocks; the refresh pause and HDMA triggers are checked after the advance. """ io = self.io ticks = clocks >> 1 while ticks: ticks -= 1 self.clock += 2 h = self.h + 2 if h >= self.hperiod: self.last_hperiod = self.hperiod self.h = h = 0 self._scanline() else: self.h = h if h & 2: self._nmi_poll() if io.irq_enable or self.irq_hold or self.irq_valid: self._irq_poll() if not self.clock & 127: self._joypad_edge() if not self.refreshed and self.h >= self.refresh_pos: # DOC: anomie timing l.68-72: 40 clocks of DRAM refresh a line; # OBSERVED (ares CPU::step): as five 8-clock blocks, one ALU step # after each. self.refreshed = True for _ in range(5): self.step(8) self._alu_edge() if not self.hdma_setup_triggered and self.h >= self.hdma_setup_pos: self.hdma_setup_triggered = True self.dma.hdma_reset() if self.dma.hdma_enabled(): self.hdma_pending = True self.hdma_mode = 0 if not self.hdma_triggered and self.h >= self.hdma_pos: self.hdma_triggered = True if self.dma.hdma_any_active(): self.hdma_pending = True self.hdma_mode = 1 def dma_step(self, clocks: int) -> None: self.dma_counter += clocks self.step(clocks) def _scanline(self) -> None: """A new line starts (OBSERVED: ares tickScanline and CPU::scanline).""" # The PPU finishes the line that just ended (h has wrapped to 0). self.ppu.end_line() v = self.v + 1 pal = self.machine.pal if v == 128: # The interlace bit is sampled once a frame, mid-frame. self.interlace = self.ppu.interlace if self.interlace and not self.field: self.vperiod += 1 if v >= self.vperiod: self.last_vperiod = self.vperiod self.vperiod = 312 if pal else 262 v = 0 self.field ^= 1 self.frame += 1 self.v = v # DOC: anomie timing l.55-66, wiki/Timing "Short and Long Scanlines": # NTSC non-interlace field 1 line 240 is 1360 clocks; PAL interlace # field 1 line 311 is 1368. hperiod = 1364 if not pal and not self.interlace and self.field == 1 and v == 240: hperiod = 1360 elif pal and self.interlace and self.field == 1 and v == 311: hperiod = 1368 self.hperiod = hperiod if v == 0: self.hdma_setup_pos = self._hdma_setup_position() self.hdma_setup_triggered = False self.auto_joypad_counter = 33 if self.machine.cpu_version == 2: # OBSERVED (ares CPU::scanline): 530 + 8 - phase each line. self.refresh_pos = 530 + 8 - (self.clock & 7) self.refreshed = False if v < self.ppu.vdisp: self.hdma_pos = 1104 self.hdma_triggered = False self.ppu.scanline(v) # -- interrupts -------------------------------------------------------------- def _nmi_poll(self) -> None: """OBSERVED (ares nmiPoll): /NMI is held 4 clocks after VBlank starts, then the transition is raised if NMI is enabled.""" if self.nmi_hold: self.nmi_hold = False if self.io.nmi_enable: self._raise_nmi() valid = self.vcounter_ago(2) >= self.ppu.vdisp if valid != self.nmi_valid: self.nmi_valid = valid self.nmi_line = valid if valid: self.nmi_hold = True def _irq_poll(self) -> None: """OBSERVED (ares irqPoll): compare the counters as they were 10 clocks ago; no IRQ on the last dot of a field (V=0 and H=0, 6 back). DOC: anomie timing l.113-122: the IRQ fires 14 + 4H clocks into the line (here (H + 1) * 4 + 10).""" io = self.io self.irq_hold = False if self.irq_line and io.irq_enable: if self.irq_wait: self.irq_wait -= 1 else: self._assert_irq() valid = ( io.irq_enable and (not io.virq_enable or self.vcounter_ago(10) == io.vtime) and (not io.hirq_enable or self.hcounter_ago(10) == io.htime) and bool(self.vcounter_ago(6) or self.hcounter_ago(6)) ) if valid and not self.irq_valid: self.irq_line = True self.irq_hold = True self.irq_wait = self.IRQ_EXTRA_POLLS self.irq_valid = valid def _raise_nmi(self) -> None: if self.in_nmitimen: self.deferred_nmi = True else: self.cpu.request_non_maskable_interrupt() def _assert_irq(self) -> None: if self.core_irq: return if self.in_nmitimen: self.deferred_irq = True return self.core_irq = True self.cpu.request_maskable_interrupt() def _deassert_irq(self) -> None: self.deferred_irq = False if self.core_irq: self.core_irq = False self.cpu.clear_maskable_interrupt() def _flush_deferred(self) -> None: """A transition a $4200 write raised reaches the core one cycle later. OBSERVED (ares nmitimenUpdate sets irqLock; ares lastCycle skips the interrupt test while it is set): the core polls at the end of the second-to-last cycle, so a request made at the start of the next cycle is taken one instruction later, as ares's lock does. """ if self.deferred_nmi: self.deferred_nmi = False self.cpu.request_non_maskable_interrupt() if self.deferred_irq: self.deferred_irq = False if self.irq_line and self.io.irq_enable and not self.core_irq: self.core_irq = True self.cpu.request_maskable_interrupt() def _nmitimen(self, value: int) -> None: """$4200 (DOC: fullsnes "4200h - NMITIMEN"; OBSERVED ares nmitimenUpdate for the edges).""" io = self.io io.auto_joypad = bool(value & 1) if self.auto_joypad_counter == 0: self.auto_joypad_latch = io.auto_joypad self._latch_ports() elif not io.auto_joypad and self.auto_joypad_counter >= 2: self.auto_joypad_counter = 33 self.in_nmitimen = True io.hirq_enable = bool(value & 0x10) io.virq_enable = bool(value & 0x20) io.irq_enable = io.hirq_enable or io.virq_enable if io.virq_enable and not io.hirq_enable and self.irq_line: self._assert_irq() elif not io.irq_enable: self.irq_line = False self._deassert_irq() nmi = bool(value & 0x80) if nmi and not io.nmi_enable and self.nmi_line: self._raise_nmi() io.nmi_enable = nmi self.in_nmitimen = False def _rdnmi(self) -> int: # DOC: fullsnes "4210h - RDNMI": bit 7 cleared by the read; OBSERVED # (ares rdnmi): not while /NMI is held. result = self.nmi_line if not self.nmi_hold: self.nmi_line = False return result def _timeup(self) -> int: result = self.irq_line if not self.irq_hold: self.irq_line = False self._deassert_irq() return result # -- the multiplier and divider ---------------------------------------------- def _alu_edge(self) -> None: """One step of the 8-step multiply or 16-step divide (DOC: fullsnes "4203h/4206h": 8 and 16 CPU cycles; MEASURED: probe suite MULF, one step per CPU cycle at either speed; the shift-add and restoring division follow ares aluEdge, OBSERVED, which DIV0 on the console agrees with).""" io = self.io if io.mpyctr: io.mpyctr -= 1 if io.rddiv & 1: io.rdmpy = (io.rdmpy + io.shift) & 0xFFFF io.rddiv >>= 1 io.shift <<= 1 if io.divctr: io.divctr -= 1 io.rddiv = (io.rddiv << 1) & 0xFFFF io.shift >>= 1 if io.rdmpy >= io.shift: io.rdmpy -= io.shift io.rddiv |= 1 # -- auto-joypad --------------------------------------------------------------- def _latch_ports(self) -> None: level = self.auto_joypad_latch or self.cpu_latch self.ports[0].latch(level) self.ports[1].latch(level) def _joypad_edge(self) -> None: """Every 128 clocks (DOC: anomie timing "Auto Joypad Read": starts on the first VBlank line between dots 32.5 and 95.5 on a 256-clock grid and takes 4224 clocks; the 33 steps OBSERVED from ares joypadEdge).""" io = self.io if ( self.v == self.ppu.vdisp and not self.clock & 255 and 130 <= self.h <= 384 ): self.auto_joypad_counter = 0 else: if self.auto_joypad_counter >= 33: return self.auto_joypad_counter += 1 counter = self.auto_joypad_counter if counter == 0: self.auto_joypad_latch = io.auto_joypad self._latch_ports() if io.auto_joypad: io.joy = [0, 0, 0, 0] if counter == 1: self.auto_joypad_latch = False self._latch_ports() if counter != 1 and not io.auto_joypad: self.auto_joypad_counter = 33 return if counter >= 2: if not counter & 1: self.auto_joypad_port1 = self.ports[0].data() self.auto_joypad_port2 = self.ports[1].data() else: joy = io.joy p1 = self.auto_joypad_port1 p2 = self.auto_joypad_port2 joy[0] = (joy[0] << 1 | p1 & 1) & 0xFFFF joy[1] = (joy[1] << 1 | p2 & 1) & 0xFFFF joy[2] = (joy[2] << 1 | (p1 >> 1) & 1) & 0xFFFF joy[3] = (joy[3] << 1 | (p2 >> 1) & 1) & 0xFFFF # -- DMA between CPU cycles ---------------------------------------------------- def dma_edge(self) -> None: """Run pending DMA/HDMA before a CPU cycle (DOC: anomie timing "DMA": align to the 8-clock grid before, to the CPU cycle after; the order of the checks OBSERVED from ares dmaEdge).""" dma = self.dma if self.dma_active: if self.hdma_pending: self.hdma_pending = False if dma.hdma_enabled(): if not dma.dma_enabled(): self.dma_counter = 8 - (self.clock & 7) self.step(self.dma_counter) if self.hdma_mode == 0: dma.hdma_setup() else: dma.hdma_run() if not dma.dma_enabled(): count = self.clock_count self.step(count - self.dma_counter % count) self.dma_active = False if self.dma_pending: self.dma_pending = False if dma.dma_enabled(): self.dma_counter = 8 - (self.clock & 7) self.step(self.dma_counter) dma.run_dma() count = self.clock_count self.step(count - self.dma_counter % count) self.dma_active = False if not self.dma_active and (self.dma_pending or self.hdma_pending): self.dma_active = True # -- the core's callbacks -------------------------------------------------------- def _speed(self, address: int) -> int: """Master clocks for an access (DOC: fullsnes "CPU Clock Cycles"; start-here.md's table; ares wait() is the same test).""" if address & 0x408000: return self.io.rom_speed if address & 0x800000 else 8 if (address + 0x6000) & 0x4000: return 8 if (address - 0x4000) & 0x7E00: return 6 return 12 def _read(self, address: int) -> int: if self.deferred_nmi or self.deferred_irq: self._flush_deferred() count = self._speed(address) self.clock_count = count if self.dma_active or self.dma_pending or self.hdma_pending: self.dma_edge() # DOC/OBSERVED: the data is sampled 4 clocks before the cycle ends # (ares CPU::read). self.step(count - 4) data = self.bus_read(address, self.mdr) self.step(4) if self.io.mpyctr or self.io.divctr: self._alu_edge() # DOC: fullsnes "Unused region (open bus)": reads of $4000-$43FF are # internal to the S-CPU and leave the MDR alone. if (address & 0x40FC00) != 0x4000: self.mdr = data return data def _write(self, address: int, value: int) -> None: if self.deferred_nmi or self.deferred_irq: self._flush_deferred() if self.io.mpyctr or self.io.divctr: self._alu_edge() count = self._speed(address) self.clock_count = count if self.dma_active or self.dma_pending or self.hdma_pending: self.dma_edge() self.step(count) self.mdr = value self.bus_write(address, value) def _idle(self, address: int) -> None: if self.deferred_nmi or self.deferred_irq: self._flush_deferred() self.clock_count = 6 if self.dma_active or self.dma_pending or self.hdma_pending: self.dma_edge() self.step(6) if self.io.mpyctr or self.io.divctr: self._alu_edge() # -- the A bus ---------------------------------------------------------------- def bus_read(self, address: int, mdr: int) -> int: """One A-bus read (DOC: fullsnes "SNES Memory Map", "SNES I/O Map").""" bank = address >> 16 if not bank & 0x40: offset = address & 0xFFFF if offset < 0x2000: return self.wram[offset] if offset < 0x4000: if offset & 0xFF00 == 0x2100: r = offset & 0xFF if r < 0x40: self.ppu.sync() return self.ppu.read(r, mdr) if r < 0x80: return self.apu.cpu_read(r & 3, self.clock) if r == 0x80: # DOC: fullsnes "2180h - WMDATA": read, then increment. value = self.wram[self.wram_address] self.wram_address = (self.wram_address + 1) & 0x1FFFF return value return mdr return self.cart.read(address, mdr) if offset < 0x4400: if offset == 0x4016: # DOC: fullsnes "4016h/4017h - JOYSER0/1": bits 0-1 data # lines; $4017 bits 2-4 read 1 (grounded inputs, inverted). return mdr & 0xFC | self.ports[0].data() & 3 if offset == 0x4017: return mdr & 0xE0 | 0x1C | self.ports[1].data() & 3 if 0x4200 <= offset < 0x4220: return self._read_io(offset, mdr) if 0x4300 <= offset < 0x4380: return self.dma.channels[(offset >> 4) & 7].read(offset & 0xF, mdr) return mdr return self.cart.read(address, mdr) if bank & 0xFE == 0x7E: return self.wram[address & 0x1FFFF] return self.cart.read(address, mdr) def bus_write(self, address: int, value: int) -> None: bank = address >> 16 if not bank & 0x40: offset = address & 0xFFFF if offset < 0x2000: self.wram[offset] = value return if offset < 0x4000: if offset & 0xFF00 == 0x2100: r = offset & 0xFF if r < 0x40: self.ppu.sync() self.ppu.write(r, value) elif r < 0x80: self.apu.cpu_write(r & 3, value, self.clock) elif r == 0x80: self.wram[self.wram_address] = value self.wram_address = (self.wram_address + 1) & 0x1FFFF elif r == 0x81: self.wram_address = self.wram_address & 0x1FF00 | value elif r == 0x82: self.wram_address = self.wram_address & 0x100FF | value << 8 elif r == 0x83: self.wram_address = (value & 1) << 16 | self.wram_address & 0xFFFF return self.cart.write(address, value) return if offset < 0x4400: if offset == 0x4016: # DOC: fullsnes: bit 0 is the strobe of both ports. self.cpu_latch = bool(value & 1) self._latch_ports() elif 0x4200 <= offset < 0x4220: self._write_io(offset, value) elif 0x4300 <= offset < 0x4380: self.dma.channels[(offset >> 4) & 7].write(offset & 0xF, value) return self.cart.write(address, value) return if bank & 0xFE == 0x7E: self.wram[address & 0x1FFFF] = value return self.cart.write(address, value) def _read_io(self, offset: int, mdr: int) -> int: io = self.io if offset == 0x4210: # DOC: fullsnes: bit 7 NMI flag, bits 3-0 CPU version, 6-4 open bus. return mdr & 0x70 | self._rdnmi() << 7 | self.machine.cpu_version & 0x0F if offset == 0x4211: return mdr & 0x7F | self._timeup() << 7 if offset == 0x4212: # DOC: fullsnes "4212h - HVBJOY"; HBlank from H=1096 to H=2 and # the auto-read busy bit as in ares readCPU (OBSERVED). value = mdr & 0x3E if io.auto_joypad and self.auto_joypad_counter < 33: value |= 1 if self.h <= 2 or self.h >= 1096: value |= 0x40 if self.v >= self.ppu.vdisp: value |= 0x80 return value if offset == 0x4213: return self.wrio if offset == 0x4214: return io.rddiv & 0xFF if offset == 0x4215: return io.rddiv >> 8 if offset == 0x4216: return io.rdmpy & 0xFF if offset == 0x4217: return io.rdmpy >> 8 if offset >= 0x4218: word = io.joy[(offset - 0x4218) >> 1] return word >> 8 if offset & 1 else word & 0xFF return mdr def _write_io(self, offset: int, value: int) -> None: io = self.io if offset == 0x4200: self._nmitimen(value) elif offset == 0x4201: # DOC: anomie timing l.21: a 1-to-0 edge on bit 7 latches the H/V # counters; MEASURED (probe HVLT): one dot after a $2137 read. if self.wrio & 0x80 and not value & 0x80: self.ppu.latch_hv() self.wrio = value elif offset == 0x4202: io.wrmpya = value elif offset == 0x4203: # DOC: fullsnes "4203h - WRMPYB": starts an 8-cycle multiply. # OBSERVED (ares writeCPU): a write while busy is ignored, after # clearing RDMPY. MEASURED (probe suite MREP): a second write 7 # cycles after the first reads $0000, not ares's A x B2: open item. io.rdmpy = 0 if io.mpyctr or io.divctr: return io.wrmpyb = value io.rddiv = value << 8 | io.wrmpya io.mpyctr = 8 io.shift = value elif offset == 0x4204: io.wrdiva = io.wrdiva & 0xFF00 | value elif offset == 0x4205: io.wrdiva = value << 8 | io.wrdiva & 0xFF elif offset == 0x4206: io.rdmpy = io.wrdiva if io.mpyctr or io.divctr: return io.wrdivb = value io.divctr = 16 io.shift = value << 16 elif offset == 0x4207 or offset == 0x4208: # Recover H from the compared value (0 is H=0's, below). h = (io.htime >> 2) - 1 if io.htime else 0 if offset == 0x4207: h = h & 0x100 | value else: h = (value & 1) << 8 | h & 0xFF # OBSERVED (ares io.cpp): compared value (H + 1) * 4, i.e. the # IRQ at 14 + 4H clocks (anomie). DOC: anomie timing l.113-122: # H=0 fires 1374 clocks after the previous line's dot 0, i.e. 10 # into the line, 4 clocks before the 14 + 4H rule; the console # agrees (probe suite IRQN, HTIME 0 against 1, 2026-10-25). io.htime = ((h + 1) & 0xFFF) << 2 & 0xFFF if h else 0 self._irq_poll() elif offset == 0x4209: io.vtime = io.vtime & 0x100 | value self._irq_poll() elif offset == 0x420A: io.vtime = (value & 1) << 8 | io.vtime & 0xFF self._irq_poll() elif offset == 0x420B: for n, channel in enumerate(self.dma.channels): channel.dma_enable = bool(value >> n & 1) if value: self.dma_pending = True elif offset == 0x420C: for n, channel in enumerate(self.dma.channels): channel.hdma_enable = bool(value >> n & 1) elif offset == 0x420D: io.rom_speed = 6 if value & 1 else 8 # -- running ------------------------------------------------------------------- def run_frame(self) -> None: """Run the CPU until the next frame starts (V wraps to 0).""" frame = self.frame cpu = self.cpu while self.frame == frame: cpu.step() self.apu.catch_up(self.clock) def run_frames(self, frames: int) -> None: for _ in range(frames): self.run_frame() __all__ = ["CPUIO", "SNES"]