# SPDX-License-Identifier: MIT """Public SPC700 CPU API. The host owns memory and every device. It passes ``read_byte(address)``, ``write_byte(address, value)`` and optionally ``idle()`` in, calls :meth:`SPC700.step`, and advances its devices **inside** the callbacks: every SPC700 cycle is one callback, in chip order, dummy reads and internal cycles included, so the host never adds ``step()``'s return value on top. See docs/handoff-brief.md "The bus contract". """ from collections.abc import Callable from spc700_python._alu import ALUMixin from spc700_python._bit import BitMixin from spc700_python._control import ControlMixin from spc700_python._dispatch import HANDLER_NAMES from spc700_python._flags import ( FLAG_B, FLAG_C, FLAG_H, FLAG_I, FLAG_N, FLAG_P, FLAG_V, FLAG_Z, ) from spc700_python._moves import MoveMixin from spc700_python._word import WordMixin from spc700_python.state import CPUState ReadByte = Callable[[int], int] WriteByte = Callable[[int, int], None] Idle = Callable[[], None] RESET_VECTOR = 0xFFFE def _no_idle() -> None: """The default ``idle``: a host that ignores internal cycles still gets correct timing.""" class SPC700(ControlMixin, BitMixin, WordMixin, MoveMixin, ALUMixin): """A Sony SPC700 core whose memory is callables supplied by a host. ``read_byte(address) -> int`` is one cycle that reads, ``write_byte(address, value)`` one that writes, and ``idle()`` one internal cycle, which has no address (C1). A flat host is two arguments:: memory = bytearray(0x10000) cpu = SPC700(memory.__getitem__, memory.__setitem__) Inside any callback the host may read ``cpu.psw`` (current, with P: U9), ``cpu.opcode``, ``cpu.cycle`` (the index of this cycle within the instruction, 0 = the fetch, when ``opcode`` still holds the previous instruction), ``cpu.stopped`` and, for tooling, ``cpu.dummy`` (this read's value will be discarded). There is no interrupt input (U11). A newly constructed CPU has the state :class:`CPUState` defaults to: ares' power-on values (U12), all settable. """ def __init__( self, read_byte: ReadByte, write_byte: WriteByte, idle: Idle | None = None, ) -> None: if not callable(read_byte): raise TypeError("read_byte must be callable") if not callable(write_byte): raise TypeError("write_byte must be callable") if idle is not None and not callable(idle): raise TypeError("idle must be callable") self.read_byte = read_byte self.write_byte = write_byte self.idle = idle if idle is not None else _no_idle self.opcode = 0 self.cycle = 0 self.dummy = False self._init_alu() self._table = [getattr(self, name) for name in HANDLER_NAMES] self.restore_state(CPUState()) def reset(self) -> None: """Reset the processor: PC = the word at $FFFE, read through the bus; P = B = I = 0; running again. A, X, Y, SP and the other flags keep their values: what they hold before the IPL ROM runs is unknown and the IPL overwrites what it uses (U12).""" self.stopped = False self.cycle = -1 low = self._read(RESET_VECTOR) self.pc = self._read(RESET_VECTOR + 1) << 8 | low self.psw &= ~(FLAG_P | FLAG_B | FLAG_I) def capture_state(self) -> CPUState: """The complete CPU-owned state as an immutable value.""" return CPUState( a=self.a, x=self.x, y=self.y, sp=self.sp, pc=self.pc, psw=self.psw, stopped=self.stopped, ) def restore_state(self, state: CPUState) -> None: """Replace the CPU-owned state with a captured one.""" self.a, self.x, self.y = state.a, state.x, state.y self.sp, self.pc, self.psw = state.sp, state.pc, state.psw self.stopped = state.stopped __all__ = [ "FLAG_B", "FLAG_C", "FLAG_H", "FLAG_I", "FLAG_N", "FLAG_P", "FLAG_V", "FLAG_Z", "SPC700", "CPUState", "Idle", "ReadByte", "WriteByte", ]