# SPDX-License-Identifier: MIT """Execution control and structured debugging records for the 65C02. 6502-python's :class:`~sixfiveohtwo.debug.DebugSession` and :class:`~sixfiveohtwo.debug.StepRecord`, subclassed through their hooks (6502-python docs/api-stability.md, "The tooling"): this core's :class:`~w65c02_python.state.CPUState`, its boundary kinds (``WAI`` and ``STP`` waiting among them), and its disassembler, which depends on the row. A record type therefore belongs to a row: :func:`record_type` gives it. A tracked access is ``(kind, address, value)``, one per cycle: ``"w"`` a write, ``"r"`` a data, stack, vector or dummy read, ``"rf"`` a read of the instruction stream at PC (``cpu.fetch``), ``"rs"`` an opcode fetch (``cpu.sync``). Watchpoints match the first letter. """ from __future__ import annotations from dataclasses import dataclass from enum import Enum from functools import cache from sixfiveohtwo.debug import Access, DebugSession, RunResult, StepRecord, StopReason from w65c02_python._core import I from w65c02_python.cpu import W65C02S from w65c02_python.disasm import Instruction, disassemble, disassemble_bytes from w65c02_python.state import CPUState class BoundaryKind(Enum): """Kind of work performed by one ``step()`` call.""" INSTRUCTION = "instruction" WAI = "wai" # a cycle spent waiting after WAI STP = "stp" # a cycle spent stopped after STP RESET = "reset" NMI = "nmi" IRQ = "irq" ACCESS_KINDS = frozenset(("r", "rf", "rs", "w")) def next_boundary(state: CPUState) -> BoundaryKind: """What the next ``step()`` will do, decided exactly as the core's ``step()`` decides.""" if state.reset_pending: return BoundaryKind.RESET if state.stopped: return BoundaryKind.STP nmi = state.non_maskable_interrupt_pending irq = state.maskable_interrupt_pending masked = bool(state.p & I) if state.waiting: # A request ends the wait, and the step that finds it is the entry or, # with IRQ and I set, the next instruction (U6). DOC (DS 3.4); the # timing of the wake is HYPOTHESIS (U6). if not (nmi or irq): return BoundaryKind.WAI else: if state.skipped_poll: return BoundaryKind.INSTRUCTION if state.polled_non_maskable_interrupt is not None: nmi = state.polled_non_maskable_interrupt if state.polled_maskable_interrupt is not None: irq = state.polled_maskable_interrupt if state.polled_i is not None: masked = state.polled_i if nmi: return BoundaryKind.NMI if irq and not masked: return BoundaryKind.IRQ return BoundaryKind.INSTRUCTION @dataclass(frozen=True, slots=True) class W65C02Record(StepRecord): """A :class:`~sixfiveohtwo.debug.StepRecord` of a 65C02; this one decodes as a W65C02S. :func:`record_type` gives the subclass for another row. """ state_type = CPUState kind_type = BoundaryKind instruction_kind = BoundaryKind.INSTRUCTION halted_kinds = frozenset((BoundaryKind.WAI, BoundaryKind.STP)) instruction_type = Instruction access_kinds = ACCESS_KINDS address_mask = 0xFFFF @staticmethod def decode(data: bytes, address: int) -> Instruction: return disassemble_bytes(data, address, W65C02S) @cache def record_type(row: type) -> type[W65C02Record]: """The record type whose instructions decode as ``row`` (a CPU class) executes them.""" if row is W65C02S: return W65C02Record @dataclass(frozen=True, slots=True) class Record(W65C02Record): @staticmethod def decode(data: bytes, address: int) -> Instruction: return disassemble_bytes(data, address, row) Record.__name__ = Record.__qualname__ = f"{row.__name__}Record" Record.__doc__ = f"A :class:`W65C02Record` that decodes as a {row.__name__}." return Record class W65C02Session(DebugSession): """A :class:`~sixfiveohtwo.debug.DebugSession` for a 65C02 or a board around one. The row is the CPU object's class. ``track_accesses=True`` records one access per cycle with the kinds above. """ record_type = W65C02Record def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) #: The CPU class: which part's opcodes the disassembly shows. self.row = type(self.cpu) self.record_type = record_type(self.row) def boundary(self, state: CPUState) -> BoundaryKind: return next_boundary(state) def decode(self, state: CPUState) -> Instruction: return disassemble(self.peek_byte, state.pc, self.row) def _wrap_bus(self) -> None: cpu = self.cpu originals = (cpu.read_byte, cpu.write_byte) read_byte, write_byte = originals self._originals = originals log: list[Access] = [] self._accesses = log def tracked_read_byte(address: int) -> int: kind = "rs" if cpu.sync else "rf" if cpu.fetch else "r" value = read_byte(address) log.append((kind, address, value)) return value def tracked_write_byte(address: int, value: int) -> None: log.append(("w", address, value)) write_byte(address, value) cpu.read_byte = tracked_read_byte cpu.write_byte = tracked_write_byte __all__ = [ "ACCESS_KINDS", "BoundaryKind", "RunResult", "StopReason", "W65C02Record", "W65C02Session", "next_boundary", "record_type", ]