"""The public W65C816 CPU class. The host owns memory and every device: it passes ``read_byte(address)``, ``write_byte(address, value)`` and optionally ``idle(address)`` in, calls :meth:`W65C816.step`, and runs its devices inside the callbacks for as long as each cycle lasts on its board. See docs/handoff-brief.md, "The bus contract". """ from sixfiveohtwo.frame import CPUFrame from w65c816._alu import ALUMixin from w65c816._block import BlockMixin from w65c816._control import ABORT_LATCH, ControlMixin from w65c816._core import OPCODE, CoreMixin, Idle, ReadByte, WriteByte from w65c816._dispatch import Entry, build_table from w65c816._loads import LoadMixin from w65c816._shifts import ShiftMixin from w65c816.state import CPUState def _no_idle(address: int) -> None: """The default ``idle``: an IO cycle touches nothing.""" class W65C816( ALUMixin, LoadMixin, ShiftMixin, ControlMixin, BlockMixin, CoreMixin, CPUFrame, ): """A WDC 65C816 core whose bus is callables supplied by a host. ``read_byte(address)`` and ``write_byte(address, value)`` are the bus, with 24-bit addresses (bank << 16 | offset); ``idle(address)`` is called for each IO cycle, when the chip drives an address and asserts neither VDA nor VPA. Every cycle is exactly one callback, in the chip's order, and ``step()`` returns how many it made. Inside a callback ``vda``, ``vpa``, ``vpb`` and ``mlb`` describe the cycle, and ``e``, ``m`` and ``x_flag`` the mode the pins show. Registers are plain attributes: ``a`` (C, 16 bits), ``x``, ``y``, ``s``, ``d``, ``pc`` (16 bits), ``dbr``, ``pbr``, ``p`` (8 bits) and ``e``. A new CPU is in emulation mode; :meth:`request_reset` runs the start sequence. The interrupt inputs, their poll and ``step()``'s skeleton are 6502-python's :class:`~sixfiveohtwo.frame.CPUFrame`. """ _table: list[Entry] def __init__( self, read_byte: ReadByte, write_byte: WriteByte, idle: Idle | None = None ) -> None: super().__init__(read_byte, write_byte) self.idle = _no_idle if idle is None else idle self._init_registers() cls = type(self) if "_table" not in cls.__dict__: cls._table = build_table(cls) @property def idle(self) -> Idle: """The host's ``idle(address)``, called for every IO cycle.""" return self._idle @idle.setter def idle(self, idle: Idle) -> None: if not callable(idle): raise TypeError(f"idle must be callable, not {type(idle).__name__}") self._idle = idle def _execute(self) -> int: self._cycles = 0 pc = self.pc opcode = self._cycle_read(self.pbr << 16 | pc, OPCODE) self.pc = (pc + 1) & 0xFFFF handler, mode = self._table[opcode] if mode is None: handler(self) else: handler(self, mode) return self._cycles # -- state ----------------------------------------------------------- def capture_state(self) -> CPUState: """Return an immutable snapshot of all CPU-owned state at a boundary. No host reads, no side effects. Memory and devices are the host's and are not included. """ return CPUState( a=self.a, x=self.x, y=self.y, s=self.s, d=self.d, dbr=self.dbr, pbr=self.pbr, pc=self.pc, p=self.p, e=self.e, waiting=self.waiting, stopped=self.stopped, abort_pending=bool(self._requests & ABORT_LATCH), **self._poll_fields(), ) def restore_state(self, state: CPUState) -> None: """Restore a captured CPU state without touching the host.""" if type(state) is not CPUState: raise TypeError("state must be a w65c816 CPUState") for name in ("a", "x", "y", "s", "d", "dbr", "pbr", "pc", "p", "e"): setattr(self, name, getattr(state, name)) self.waiting = state.waiting self.stopped = state.stopped self._restore_poll(state) if state.abort_pending: self._requests |= ABORT_LATCH __all__ = ["W65C816"]