"""Execution control and structured debugging records for the W65C816. 6502-python's :class:`~sixfiveohtwo.debug.DebugSession` and :class:`~sixfiveohtwo.debug.StepRecord`, subclassed through their hooks (6502-python docs/api-stability.md, "The tooling"): 24-bit addresses, this core's :class:`~w65c816.state.CPUState`, its boundary kinds and its disassembler, and IO cycles tracked beside reads and writes. A tracked access is ``(kind, address, value)``. The kind is ``"r"``, ``"w"`` or ``"i"`` (an IO cycle, value 0), followed by the pins the chip asserted in that cycle, in this order: ``d`` VDA, ``p`` VPA, ``v`` VPB, ``l`` MLB. An opcode fetch is ``"rdp"``, an operand byte ``"rp"``, a data read ``"rd"``, a vector byte ``"rdv"``, a read-modify-write's write ``"wdl"``, an internal cycle ``"i"``. Watchpoints match the first letter, so they see IO cycles only as what they are: neither reads nor writes. """ from __future__ import annotations from dataclasses import dataclass from enum import Enum from itertools import combinations from sixfiveohtwo.debug import Access, DebugSession, RunResult, StepRecord, StopReason from w65c816._core import I from w65c816.disasm import Instruction, disassemble, disassemble_bytes from w65c816.state import CPUState class BoundaryKind(Enum): """Kind of work performed by one ``W65C816.step()`` call.""" INSTRUCTION = "instruction" WAIT_IDLE = "wait_idle" # an IO cycle while WAI waits (or the one that ends it) STOP_IDLE = "stop_idle" # an IO cycle while STP has stopped the clock RESET = "reset" ABORT = "abort" NMI = "nmi" IRQ = "irq" _PINS = "dpvl" ACCESS_KINDS = frozenset( prefix + "".join(pins) for prefix in "rwi" for size in range(len(_PINS) + 1) for pins in combinations(_PINS, size) ) def next_boundary(state: CPUState) -> BoundaryKind: """What the next ``step()`` will do, decided exactly as ``W65C816.step()`` decides.""" if state.reset_pending: return BoundaryKind.RESET if state.halted: return BoundaryKind.WAIT_IDLE if state.waiting else BoundaryKind.STOP_IDLE if state.skipped_poll: return BoundaryKind.INSTRUCTION if state.abort_pending: return BoundaryKind.ABORT nmi = state.polled_non_maskable_interrupt if state.non_maskable_interrupt_pending if nmi is None else nmi: return BoundaryKind.NMI irq = state.polled_maskable_interrupt masked = bool(state.p & I) if state.polled_i is None else state.polled_i if (state.maskable_interrupt_pending if irq is None else irq) and not masked: return BoundaryKind.IRQ return BoundaryKind.INSTRUCTION @dataclass(frozen=True, slots=True) class W65C816Record(StepRecord): """A :class:`~sixfiveohtwo.debug.StepRecord` of the W65C816.""" state_type = CPUState kind_type = BoundaryKind instruction_kind = BoundaryKind.INSTRUCTION halted_kinds = frozenset((BoundaryKind.WAIT_IDLE, BoundaryKind.STOP_IDLE)) instruction_type = Instruction access_kinds = ACCESS_KINDS address_mask = 0xFFFFFF @staticmethod def decode(data: bytes, address: int) -> Instruction: return disassemble_bytes(data, address) class W65C816Session(DebugSession): """A :class:`~sixfiveohtwo.debug.DebugSession` for a W65C816 or a board around one. Breakpoints are 24-bit addresses (PBR:PC). ``track_accesses=True`` wraps ``idle`` as well as ``read_byte`` and ``write_byte``, so a step's accesses are exactly one per cycle, and each records the cycle's pins. """ record_type = W65C816Record def boundary(self, state: CPUState) -> BoundaryKind: return next_boundary(state) def program_address(self, state: CPUState) -> int: return state.pbr << 16 | state.pc def decode(self, state: CPUState) -> Instruction: return disassemble( self.peek_byte, state.pbr << 16 | state.pc, m=bool(state.p & 0x20), x=bool(state.p & 0x10), ) def _wrap_bus(self) -> None: cpu = self.cpu originals = (cpu.read_byte, cpu.write_byte, cpu.idle) read_byte, write_byte, idle = originals self._originals = originals log: list[Access] = [] self._accesses = log def pins() -> str: return ( ("d" if cpu.vda else "") + ("p" if cpu.vpa else "") + ("v" if cpu.vpb else "") + ("l" if cpu.mlb else "") ) def tracked_read_byte(address: int) -> int: value = read_byte(address) log.append(("r" + pins(), address, value)) return value def tracked_write_byte(address: int, value: int) -> None: log.append(("w" + pins(), address, value)) write_byte(address, value) def tracked_idle(address: int) -> None: log.append(("i" + pins(), address, 0)) idle(address) cpu.read_byte = tracked_read_byte cpu.write_byte = tracked_write_byte cpu.idle = tracked_idle def _unwrap_bus(self, originals: tuple) -> None: cpu = self.cpu cpu.read_byte, cpu.write_byte, cpu.idle = originals __all__ = [ "ACCESS_KINDS", "BoundaryKind", "RunResult", "StopReason", "W65C816Record", "W65C816Session", "next_boundary", ]