# SPDX-License-Identifier: MIT """The interrupt controller (IE, IF, IME), the CPU's IRQ line, HALT and STOP, POSTFLG. Registers: GBATEK §GBA Interrupt Control, §GBA System Control (DOC); docs/timing.md "Interrupts", "HALT and STOP"; open questions U-IRQ-1, U-SYS-1, U-SYS-3, U-SYS-5. """ from __future__ import annotations from gba_python.scheduler import ORDER_IRQ # IF/IE bits (GBATEK §GBA Interrupt Control; DOC). VBLANK, HBLANK, VCOUNT = 0x0001, 0x0002, 0x0004 TIMER0 = 0x0008 # timer n is TIMER0 << n SERIAL = 0x0080 DMA0 = 0x0100 # DMA n is DMA0 << n KEYPAD, GAMEPAK = 0x1000, 0x2000 # The CPU's line is IME and (IE and IF) != 0, each register's value reaching it through its # own delay: the line at cycle t is IME(t - IME_DELAY) and IE(t - IE_DELAY) and # IF(t - IF_DELAY) (U-IRQ-1; no document gives these). FITTED (docs/timing.md "Interrupts"): # the mGBA suite's "Timer IRQ" table (an overflow tick in cycle c sets IF in c + 2, timers' # IRQ_LAG, and interrupts an LDRH starting at c + 5, not c + 4), alyosha-tas irq/IF (a read # in c + 1 sees IF clear), irq/IE (an IE cleared just after the overflow still lets the IRQ # in), hw-test irq/irq-delay (92/112/120: the IRQ is taken an instruction or two after # IME = 1). IF_DELAY = 3 IE_DELAY = 4 IME_DELAY = 4 # HALT ends HALT_WAKE cycles after (IE and IF) != 0, and never sooner than HALT_WAKE cycles # after it began (FITTED: hw-test haltcnt "TIME IWRAM" 125 and "TIME ROM" 249, which halt # with the IRQ already pending, and "HALTCNT CPUSET" 4155; docs/timing.md "HALT and STOP"). HALT_WAKE = 3 HALT_LINE_DELAY = 0 NEVER = 1 << 62 class Delayed: """A register's value as seen through a fixed delay: the changes, oldest first.""" __slots__ = ("changes", "delay") def __init__(self, delay: int) -> None: self.delay = delay self.changes: list[tuple[int, int]] = [(-NEVER, 0)] def set(self, time: int, value: int) -> None: self.changes.append((time, value)) def seen(self, now: int, delay: int) -> int: changes = self.changes at = now - delay while len(changes) > 1 and changes[1][0] <= at: changes.pop(0) return changes[0][1] class Interrupts: def __init__(self, board) -> None: self.board = board self.ie = 0 self.if_ = 0 self.ime = 0 self.postflg = 0 self.halted = False self.stopped = False self.halt_time = 0 # the cycle HALTCNT was written self.seen_ie = Delayed(IE_DELAY) self.seen_if = Delayed(IF_DELAY) self.seen_ime = Delayed(IME_DELAY) # When (IE and IF) last became nonzero (for HALT, which ignores IME). self.pending_since = NEVER def update_pending(self, time: int) -> None: pending = self.ie & self.if_ if pending and self.pending_since == NEVER: self.pending_since = time elif not pending: self.pending_since = NEVER def request(self, bits: int, time: int) -> None: """A source raises its IF bits in cycle ``time``, whatever IE says (HYPOTHESIS consistent with GBATEK; U-SYS-5; judged by alyosha-tas irq/IF, IE). A request for a later cycle is scheduled for it.""" if time > self.board.bus.now: self.board.scheduler.schedule(time, ORDER_IRQ, lambda t: self.request(bits, t)) return if bits & ~self.if_: self.if_ |= bits self.seen_if.set(time, self.if_) self.update_pending(time) def line(self, now: int) -> bool: """The CPU's IRQ line at ``now`` (GBATEK §GBA Interrupt Control for the function; DOC; the delays FITTED); the core gates it with CPSR.I.""" if not (self.ime or len(self.seen_ime.changes) > 1): return False return bool( self.seen_ime.seen(now, IME_DELAY) & 1 and self.seen_ie.seen(now, IE_DELAY) & self.seen_if.seen(now, IF_DELAY) ) def wake_time(self) -> int: """The cycle HALT ends, or NEVER while (IE and IF) = 0. Halt ends when (IE and IF) != 0; IME and CPSR.I do not matter ("Halt passes through even if either one has disabled interrupts"; GBATEK §BIOS Halt Functions; DOC); timing FITTED (HALT_WAKE).""" if self.pending_since == NEVER: return NEVER return max(self.pending_since + HALT_LINE_DELAY, self.halt_time) + HALT_WAKE # ---------------------------------------------------------------------------------- # Registers (halfword offsets in I/O; ``mask`` selects the bytes written) def read16(self, offset: int) -> int | None: if offset == 0x200: return self.ie if offset == 0x202: return self.if_ if offset == 0x208: return self.ime if offset == 0x300: # POSTFLG in the low byte; HALTCNT (high byte) is write-only: reads 0 (HYPOTHESIS). return self.postflg return None def write16(self, offset: int, value: int, mask: int, time: int) -> None: if offset == 0x200: self.ie = ((self.ie & ~mask) | (value & mask)) & 0x3FFF self.seen_ie.set(time, self.ie) self.update_pending(time) elif offset == 0x202: # Writing 1 acknowledges (clears) the bit (GBATEK §GBA Interrupt Control; DOC). if self.if_ & value & mask: self.if_ &= ~(value & mask) self.seen_if.set(time, self.if_) self.update_pending(time) elif offset == 0x208: self.ime = ((self.ime & ~mask) | (value & mask)) & 1 self.seen_ime.set(time, self.ime) elif offset == 0x300: if mask & 0x00FF: # The BIOS sets POSTFLG after the first boot; a halfword write of 0 leaves it 1 # (hw-test haltcnt "POSTFLG: PASS 1"; TEST ROM). HYPOTHESIS: set-only (U-SYS-1). self.postflg |= value & 1 if mask & 0xFF00 and self.board.bus.fetch_in_bios: # HALTCNT, bit 7: 0 halt, 1 stop (GBATEK §GBA System Control; DOC). It takes # effect only while the CPU runs in the BIOS: hw-test haltcnt "HALTCNT DIRECT" # (a STRB from IWRAM) reads 12, not the ~270 a halt until timer 1's IRQ would # give, while CpuSet's write, and a DMA's write made while CpuSet runs, halt # (TEST ROM; HYPOTHESIS for the mechanism: gated like the BIOS read protection; # docs/undocumented-behavior.md U-SYS-6). if value & 0x8000: self.stopped = True else: self.halted = True self.halt_time = time