"""The public CPU classes: one instruction core, one class per part. The host owns memory and every device: it passes ``read_byte(address)`` and ``write_byte(address, value)`` in, calls ``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". The differences between makers' parts are data (docs/start-here.md, "The rows"): each class below is :class:`CMOS65C02` with its row of settings. """ from sixfiveohtwo.frame import CPUFrame from w65c02_python._alu import ALUMixin from w65c02_python._bits import BitMixin from w65c02_python._control import ControlMixin from w65c02_python._core import CoreMixin, ReadByte, WriteByte from w65c02_python._dispatch import Entry, build_table from w65c02_python._loads import LoadMixin from w65c02_python._nops import NopMixin from w65c02_python._shifts import ShiftMixin from w65c02_python.state import CPUState class CMOS65C02( ALUMixin, LoadMixin, ShiftMixin, BitMixin, ControlMixin, NopMixin, CoreMixin, CPUFrame, ): """The 65C02 instruction core; a part is a subclass that sets the row's attributes. ``read_byte(address)`` and ``write_byte(address, value)`` are the bus, with 16-bit addresses and 8-bit values. Every cycle is exactly one callback, in the chip's order, dummy reads included: a 65C02 reads or writes in every cycle, so there is no third callable. ``step()`` runs one instruction, one interrupt entry, one reset, or one cycle of ``WAI``/``STP`` waiting, and returns how many callbacks it made. The core keeps no time. Inside a callback the host may read ``sync`` (True during an opcode fetch: the SYNC pin), ``fetch`` (True for every read of the instruction stream at PC: opcode, operand bytes, and the dropped read of the next byte) and ``opcode`` (the instruction in progress). Registers are plain attributes: ``a``, ``x``, ``y``, ``s`` (8 bits), ``pc`` (16 bits) and ``p``, which keeps bit 5 set and B clear. A new CPU has zeroed registers and I set; :meth:`request_reset` runs the chip's start sequence. The interrupt inputs, their poll and ``step()``'s skeleton are 6502-python's :class:`~sixfiveohtwo.frame.CPUFrame`. The row's settings are class attributes; a board makes its own row by subclassing and assigning them (the opcode table is built once per class): - ``bit_instructions``: ``RMB SMB BBR BBS`` at ``x7`` and ``xF``, else one-byte, one-cycle NOPs; - ``wai_stp``: ``WAI`` and ``STP`` at ``$CB`` and ``$DB``, else one-byte, one-cycle NOPs; - ``reset_writes_stack``: reset's three stack cycles write PCH, PCL, P; - ``one_cycle_nops_poll``, ``nop_5c_reads``, ``nop_dc_fc_reads``: the defaults of U7, U9 and C10 (docs/undocumented-behavior.md). """ bit_instructions = True wai_stp = True _table: list[Entry] def __init__(self, read_byte: ReadByte, write_byte: WriteByte) -> None: super().__init__(read_byte, write_byte) self._init_registers() cls = type(self) if "_table" not in cls.__dict__: cls._table = build_table(cls) def _execute(self) -> int: pc = self.pc self.sync = self.fetch = True opcode = self._read(pc) self.sync = self.fetch = False self.pc = (pc + 1) & 0xFFFF self.opcode = opcode handler, mode, cycles = self._table[opcode] if mode is None: handler(self) else: handler(self, mode) extra = self._extra if extra: self._extra = 0 return cycles + extra return 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, pc=self.pc, p=self.p, waiting=self.waiting, stopped=self.stopped, **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 w65c02_python CPUState") for name in ("a", "x", "y", "s", "pc", "p"): setattr(self, name, getattr(state, name)) self.waiting = state.waiting self.stopped = state.stopped self._restore_poll(state) class W65C02S(CMOS65C02): """WDC W65C02S: the bit instructions, ``WAI`` and ``STP`` (DS Table 5-2: 212 opcodes). No capture of this part's reset was found; it is taken to read the stack (U1). Its only tier-1 evidence is Foot's data-only capture (rung 4b). """ bit_instructions = True wai_stp = True reset_writes_stack = False # U1: unknown; the default class R65C02(CMOS65C02): """Rockwell R65C02, R65C102, R65C112: the bit instructions, no ``WAI`` or ``STP`` (RDS; C5). Captured (CAP, a BBC B fitted with one): reset reads the stack. """ bit_instructions = True wai_stp = False # U3: `$CB` and `$DB` are one-byte, one-cycle NOPs reset_writes_stack = False # CAP class G65SC02(CMOS65C02): """GTE/CMD G65SC02 and G65SC12, NCR65C02, VLSI VL65NC02, and the Lynx I's cell: 178 opcodes. No bit instructions (CAP on a BBC Master's G65SC12: `x7` and `xF` take one cycle; GDS Table V, VDS Table 2, NDS), no ``WAI`` or ``STP``. Captured: reset writes PCH, PCL and P to the stack. """ bit_instructions = False # C2 wai_stp = False # U3 reset_writes_stack = True # CAP; GDS Table V class Lynx2Cell(CMOS65C02): """The 65C02 cell of the Atari Lynx II's Mikey: the `G65SC02` row with the bit instructions. Real Lynx IIs execute ``RMB SMB BBR BBS`` and Lynx Is do not (LT `cpu` test 7; C12). No part number is known for the later cell, and nothing else about it is: its reset is taken to be the Lynx I's (U2), and ``WAI`` and ``STP`` to be absent (C8). The Lynx I's cell is :class:`G65SC02`. """ bit_instructions = True # C12: LT, tier 3 with hardware-derived expectations wai_stp = False # C8 reset_writes_stack = True # U2: no evidence; as the Lynx I row __all__ = ["CMOS65C02", "G65SC02", "R65C02", "W65C02S", "Lynx2Cell"]