# SPDX-License-Identifier: MIT """Registers, the bus, addressing helpers and the shape of ``step()``. The bus contract (docs/handoff-brief.md "The bus contract"): every SPC700 cycle is one callback, in chip order, dummy reads and internal cycles included. A step starts with the opcode fetch and ends after the instruction's last cycle, the order SingleStepTests records. """ from __future__ import annotations from collections.abc import Callable from spc700_python._flags import FLAG_N, FLAG_P, FLAG_Z STACK_PAGE = 0x0100 def pack(high: int, low: int) -> int: """A 16-bit value from its two bytes.""" return high << 8 | low class CoreMixin: """State and plumbing shared by every instruction family.""" # Declared for the type checker; ``SPC700.__init__`` assigns them. a: int x: int y: int sp: int pc: int psw: int opcode: int cycle: int stopped: bool dummy: bool read_byte: Callable[[int], int] write_byte: Callable[[int, int], None] idle: Callable[[], None] _table: list[Callable[[int], None]] @property def ya(self) -> int: """Y:A, not a register of its own: the two bytes paired (fullsnes "Registers").""" return self.y << 8 | self.a @ya.setter def ya(self, value: int) -> None: self.y, self.a = value >> 8 & 0xFF, value & 0xFF # ----- the bus: one callback per cycle -------------------------------- def _read(self, address: int) -> int: """One cycle that reads.""" self.cycle += 1 return self.read_byte(address) & 0xFF def _read_dummy(self, address: int) -> None: """One cycle that reads and discards: the read still happens on the bus, so a dummy read of $FD-$FF clears a timer on the board (U1, U10). ``dummy`` is true during it.""" self.cycle += 1 self.dummy = True self.read_byte(address) self.dummy = False def _write(self, address: int, value: int) -> None: """One cycle that writes.""" self.cycle += 1 self.write_byte(address, value) def _idle(self) -> None: """One internal cycle: no address (C1); the board prices it from opcode and cycle (C3).""" self.cycle += 1 self.idle() # ----- operands ------------------------------------------------------- def _fetch(self) -> int: """Operand fetch: read at PC, which wraps $FFFF to $0000.""" value = self._read(self.pc) self.pc = (self.pc + 1) & 0xFFFF return value def _fetch_word(self) -> int: """Two operand fetches, low byte first.""" low = self._fetch() return pack(self._fetch(), low) def _dp(self, offset: int) -> int: """Direct-page address: page 0 or 1 by P, and the offset wraps within the page (U2).""" return (self.psw & FLAG_P) << 3 | (offset & 0xFF) def _load(self, offset: int) -> int: """Read a direct-page byte.""" return self._read(self._dp(offset)) def _store(self, offset: int, value: int) -> None: """Write a direct-page byte.""" self._write(self._dp(offset), value) def _load_pointer(self, offset: int) -> int: """Read a little-endian word from the direct page; the high byte wraps in the page (U2).""" low = self._load(offset) return pack(self._load(offset + 1), low) def _direct_x(self) -> int: """``d+X``: the operand, then an internal cycle for the index add; returns the offset.""" offset = self._fetch() self._idle() return (offset + self.x) & 0xFF def _absolute_indexed(self, index: int) -> int: """``!a+X`` / ``!a+Y``: the operand, an internal cycle, no page-wrap quirk (U2).""" base = self._fetch_word() self._idle() return (base + index) & 0xFFFF def _indexed_indirect(self) -> int: """``[d+X]``: the pointer at ``d+X`` in the direct page; returns the address it holds.""" return self._load_pointer(self._direct_x()) def _indirect_y_load(self) -> int: """``[d]+Y`` as the read forms do it: the internal cycle comes before the pointer reads. C4: SingleStepTests and ares; anomie group 7 ("most likely") puts it after them, as the store does. SST is followed; the count is the same either way. """ offset = self._fetch() self._idle() return (self._load_pointer(offset) + self.y) & 0xFFFF def _absolute_bit(self) -> tuple[int, int]: """``m.b``: 13 address bits and a bit number in the top three (U15).""" operand = self._fetch_word() return operand & 0x1FFF, operand >> 13 # ----- the stack: page 1, SP wraps within it ---------------------------- def _push(self, value: int) -> None: self._write(STACK_PAGE | self.sp, value) self.sp = (self.sp - 1) & 0xFF def _pull(self) -> int: self.sp = (self.sp + 1) & 0xFF return self._read(STACK_PAGE | self.sp) def _push_pc(self) -> None: """Calls push the next instruction's address, high byte first (U14).""" self._push(self.pc >> 8) self._push(self.pc & 0xFF) def _pull_pc(self) -> None: low = self._pull() self.pc = pack(self._pull(), low) # ----- flags ---------------------------------------------------------- def _set_nz(self, value: int) -> None: """N and Z from an 8-bit result.""" self.psw = self.psw & ~(FLAG_N | FLAG_Z) | (value & FLAG_N) | (FLAG_Z if value == 0 else 0) def _set_nz16(self, value: int) -> None: """N from bit 15 and Z from all sixteen bits.""" negative = FLAG_N if value & 0x8000 else 0 self.psw = self.psw & ~(FLAG_N | FLAG_Z) | negative | (FLAG_Z if value == 0 else 0) # ----- one step --------------------------------------------------------- def step(self) -> int: """Execute one instruction, or one stopped cycle. Returns the SPC700 cycles consumed, which is the number of bus callbacks made. A stopped CPU makes exactly one ``idle()`` and returns 1 (C5): the board's clock keeps running and only ``reset()`` leaves the state. ``cycle`` is the index of the current cycle, 0 = the fetch. ``opcode`` still holds the previous opcode during the fetch cycle itself; it is current from cycle 1 on. """ if self.stopped: self.cycle = 0 self.idle() return 1 self.cycle = -1 opcode = self._fetch() self.opcode = opcode self._table[opcode](opcode) return self.cycle + 1 __all__ = ["STACK_PAGE", "CoreMixin", "pack"]