"""Dependency-free execution control and structured debugging records. ``DebugSession`` drives an existing CPU -- any object with ``step()`` and ``capture_state()`` -- one boundary at a time, with execute breakpoints, bounded runs, a history ring and, optionally, bus-access tracking and watchpoints. It never changes what the core does. Same shape as z80-python's and m6800-python's. See docs/debug-session.md. """ from collections import deque from collections.abc import Iterator from dataclasses import dataclass from enum import Enum from typing import Protocol, runtime_checkable from i8080_python.disasm import ByteReader, Instruction, disassemble from i8080_python.state import CPUState @runtime_checkable class DebugTarget(Protocol): """Minimum processor interface consumed by :class:`DebugSession`.""" def step(self) -> int: """Advance one instruction or lifecycle boundary and return its state count.""" def capture_state(self) -> CPUState: """Capture the current CPU-owned state.""" class BoundaryKind(Enum): """Kind of work performed by one target ``step()`` call.""" INSTRUCTION = "instruction" HALT_IDLE = "halt_idle" RESET = "reset" INTERRUPT = "interrupt" class StopReason(Enum): """Why a bounded run returned control.""" BREAKPOINT = "breakpoint" # before the instruction at a breakpoint WATCHPOINT = "watchpoint" # after the step that touched a watched address HALTED = "halted" STEP_LIMIT = "step_limit" STATE_LIMIT = "state_limit" #: One bus access: ``("r" | "w", address, value)`` for memory and #: ``("in" | "out", port, value)`` for I/O, in the order the step made them. Access = tuple[str, int, int] _ACCESS_KINDS = frozenset(("r", "w", "in", "out")) def _valid_access(access: object) -> bool: if type(access) is not tuple or len(access) != 3: return False kind, address, value = access return ( kind in _ACCESS_KINDS and type(address) is int and 0 <= address <= 0xFFFF and type(value) is int and 0 <= value <= 0xFF ) @dataclass(frozen=True, slots=True) class StepRecord: """Immutable before/after evidence for one processor boundary.""" sequence: int kind: BoundaryKind before: CPUState after: CPUState states: int instruction: Instruction | None #: Every bus access the step made, in order, when the session tracks #: accesses; ``None`` when it does not. accesses: tuple[Access, ...] | None = None def __post_init__(self) -> None: if type(self.sequence) is not int or self.sequence < 0: raise ValueError("sequence must be a non-negative integer") if type(self.kind) is not BoundaryKind: raise ValueError("kind must be a BoundaryKind") if type(self.before) is not CPUState or type(self.after) is not CPUState: raise ValueError("before and after must be CPUState values") if type(self.states) is not int or self.states <= 0: raise ValueError("states must be a positive integer") if self.instruction is not None and type(self.instruction) is not Instruction: raise ValueError("instruction must be an Instruction or None") if self.kind is not BoundaryKind.INSTRUCTION and self.instruction is not None: raise ValueError("lifecycle boundaries cannot contain an instruction") if self.accesses is not None and ( type(self.accesses) is not tuple or not all(_valid_access(access) for access in self.accesses) ): raise ValueError("accesses must be a tuple of (kind, address, value) or None") @dataclass(frozen=True, slots=True) class RunResult: """Summary of one bounded run operation.""" reason: StopReason steps: int instructions: int states: int state: CPUState last_record: StepRecord | None #: For WATCHPOINT: the watched accesses that stopped the run. hits: tuple[Access, ...] = () def next_boundary(state: CPUState) -> BoundaryKind: """What the next ``step()`` will do, decided exactly as ``I8080CPU.step()`` decides it.""" if state.reset_pending: return BoundaryKind.RESET if state.interrupt_vector is not None and state.inte and state.ei_delay == 0: return BoundaryKind.INTERRUPT if state.halted: return BoundaryKind.HALT_IDLE return BoundaryKind.INSTRUCTION class DebugSession: """Control an existing CPU host without modifying its execution core. ``peek_byte`` must be side-effect-free (the host's memory, not its bus). If it is omitted, stepping and breakpoints still work but step records carry no disassembly. ``track_accesses=True`` wraps the CPU's four bus callables to record every access in :attr:`StepRecord.accesses` and to enable watchpoints; :meth:`close` puts the originals back. The target may be a whole board rather than a bare CPU: an object whose ``step()`` runs its devices around one CPU step and whose ``cpu`` attribute is the processor, whose bus is then the one tracked. """ def __init__( self, target: DebugTarget, *, peek_byte: ByteReader | None = None, history_limit: int = 256, track_accesses: bool = False, ) -> None: if not isinstance(target, DebugTarget): raise TypeError("target must provide step() and capture_state()") if peek_byte is not None and not callable(peek_byte): raise TypeError("peek_byte must be callable or None") if type(history_limit) is not int or history_limit < 0: raise ValueError("history_limit must be a non-negative integer") self.target = target #: The processor: the target itself, or the ``cpu`` a board target #: carries. Access tracking wraps its bus callables. self.cpu = getattr(target, "cpu", target) self.peek_byte = peek_byte self.history_limit = history_limit self.breakpoints: set[int] = set() #: Memory watchpoints: address -> "r", "w" or "rw". self.watchpoints: dict[int, str] = {} self.total_steps = 0 self.total_instructions = 0 self.total_states = 0 self._history: deque[StepRecord] = deque(maxlen=history_limit or 1) self._accesses: list[Access] | None = None self._originals: tuple | None = None if track_accesses: self._wrap_bus() @property def tracking(self) -> bool: """Whether this session is recording bus accesses.""" return self._originals is not None def _wrap_bus(self) -> None: cpu = self.cpu originals = (cpu.read_byte, cpu.write_byte, cpu.read_port, cpu.write_port) read_byte, write_byte, read_port, write_port = originals self._originals = originals log: list[Access] = [] self._accesses = log def tracked_read_byte(address: int) -> int: value = read_byte(address) log.append(("r", address, value)) return value def tracked_write_byte(address: int, value: int) -> None: log.append(("w", address, value)) write_byte(address, value) def tracked_read_port(port: int) -> int: value = read_port(port) log.append(("in", port, value)) return value def tracked_write_port(port: int, value: int) -> None: log.append(("out", port, value)) write_port(port, value) cpu.read_byte = tracked_read_byte cpu.write_byte = tracked_write_byte cpu.read_port = tracked_read_port cpu.write_port = tracked_write_port def close(self) -> None: """Stop tracking accesses and give the CPU its own bus callables back.""" if self._originals is not None: cpu = self.cpu cpu.read_byte, cpu.write_byte, cpu.read_port, cpu.write_port = self._originals self._originals = None self._accesses = None @property def history(self) -> tuple[StepRecord, ...]: """Bounded immutable view of the retained step records.""" return tuple(self._history) def clear_history(self) -> None: """Discard retained records without changing execution totals.""" self._history.clear() def add_breakpoint(self, address: int) -> None: """Stop before executing the instruction at a 16-bit address.""" self.breakpoints.add(self._validate_address(address)) def remove_breakpoint(self, address: int) -> None: """Remove an execute breakpoint if it exists.""" self.breakpoints.discard(self._validate_address(address)) def add_watchpoint(self, address: int, kind: str = "rw") -> None: """Stop after a step that reads (``r``), writes (``w``) or either (``rw``) a byte.""" if not self.tracking: raise ValueError("watchpoints need a session created with track_accesses=True") if kind not in ("r", "w", "rw"): raise ValueError('kind must be "r", "w" or "rw"') self.watchpoints[self._validate_address(address)] = kind def remove_watchpoint(self, address: int) -> None: """Remove a watchpoint if it exists.""" self.watchpoints.pop(self._validate_address(address), None) def step(self) -> StepRecord: """Advance exactly one boundary, ignoring execute breakpoints.""" before = self.target.capture_state() kind = next_boundary(before) instruction = None if kind is BoundaryKind.INSTRUCTION and self.peek_byte is not None: instruction = disassemble(self.peek_byte, before.pc) if self._accesses is not None: self._accesses.clear() states = self.target.step() if type(states) is not int or states <= 0: raise ValueError("target step() must return a positive integer state count") record = StepRecord( sequence=self.total_steps, kind=kind, before=before, after=self.target.capture_state(), states=states, instruction=instruction, accesses=None if self._accesses is None else tuple(self._accesses), ) self.total_steps += 1 self.total_states += states if kind is BoundaryKind.INSTRUCTION: self.total_instructions += 1 if self.history_limit: self._history.append(record) return record def run( self, *, max_steps: int, max_states: int | None = None, stop_on_halt: bool = True, ) -> RunResult: """Run until a stop condition or the mandatory finite step budget. A state limit is checked after each atomic boundary and may therefore be exceeded by that boundary's cost. Execute breakpoints stop before execution; watchpoints stop after the step that touched the byte. """ if type(max_steps) is not int or max_steps <= 0: raise ValueError("max_steps must be a positive integer") if max_states is not None and (type(max_states) is not int or max_states <= 0): raise ValueError("max_states must be a positive integer or None") steps = instructions = states = 0 last_record = None while steps < max_steps: state = self.target.capture_state() kind = next_boundary(state) if kind is BoundaryKind.INSTRUCTION and state.pc in self.breakpoints: return RunResult( StopReason.BREAKPOINT, steps, instructions, states, state, last_record ) if stop_on_halt and kind is BoundaryKind.HALT_IDLE: return RunResult(StopReason.HALTED, steps, instructions, states, state, last_record) last_record = self.step() steps += 1 states += last_record.states if last_record.kind is BoundaryKind.INSTRUCTION: instructions += 1 hits = self._watch_hits(last_record) if hits: return RunResult( StopReason.WATCHPOINT, steps, instructions, states, last_record.after, last_record, hits, ) if max_states is not None and states >= max_states: return RunResult( StopReason.STATE_LIMIT, steps, instructions, states, last_record.after, last_record, ) state = last_record.after if last_record is not None else self.target.capture_state() return RunResult(StopReason.STEP_LIMIT, steps, instructions, states, state, last_record) def iter_history(self, *, newest_first: bool = False) -> Iterator[StepRecord]: """Iterate retained records without exposing the mutable history buffer.""" return reversed(self._history) if newest_first else iter(self._history) def _watch_hits(self, record: StepRecord) -> tuple[Access, ...]: if not self.watchpoints or not record.accesses: return () return tuple( access for access in record.accesses if access[0] in ("r", "w") and access[1] in self.watchpoints and access[0] in self.watchpoints[access[1]] ) @staticmethod def _validate_address(address: int) -> int: if type(address) is not int or not 0 <= address <= 0xFFFF: raise ValueError("address must be an integer in range 0x0000..0xFFFF") return address __all__ = [ "Access", "BoundaryKind", "DebugSession", "DebugTarget", "RunResult", "StepRecord", "StopReason", "next_boundary", ]