"""Portable command debugger for the 65C02. 6502-python's :class:`~sixfiveohtwo.console.CommandDebugger` with this core's state (``WAI`` and ``STP`` among it) and the row's disassembly. ``execute(line)`` runs one command and returns printable lines; ``interact(input, output)`` is a line loop with no terminal dependencies. ``python -m w65c02_python`` starts one on a memory image. """ from __future__ import annotations from typing import ClassVar from sixfiveohtwo.console import CommandDebugger, CommandError, CommandResult, parse_number from w65c02_python.disasm import ByteReader, Instruction, disassemble from w65c02_python.state import CPUState _HELP = ( "help | ? Show this command summary", "registers | regs | r Show CPU registers and lifecycle state", "step [COUNT] | s Execute one or more boundaries (ignores breakpoints)", "over | o Step, running a JSR through to its return", "run STEPS [CYCLES] Run with finite step and optional cycle limits", "continue | c Run up to 1,000,000 steps, off a breakpoint if on one", "break ADDRESS | b Add an execute breakpoint", "delete ADDRESS Remove an execute breakpoint", "breakpoints List breakpoints and watchpoints", "watch ADDRESS [r|w|rw] Stop after a step that touches the byte at ADDRESS", "unwatch ADDRESS Remove a watchpoint", "disassemble [ADDRESS] [COUNT] | d Decode as this part executes, without side effects", "memory ADDRESS [LENGTH] | m Display up to 256 bytes", "history [COUNT] Show retained step records", "set REGISTER VALUE Set A X Y S P PC", "irq on | irq off Assert or release the IRQ line (a level)", "nmi | reset Latch an NMI edge, or run the reset sequence", "quit | exit | q Leave the command loop", "Numbers are decimal; 0x1234 or $1234 is hexadecimal.", ) def _flags(p: int) -> str: labels = "NV-BDIZC" return "".join(label if p & (0x80 >> index) else "-" for index, label in enumerate(labels)) class W65C02Debugger(CommandDebugger): """A :class:`~sixfiveohtwo.console.CommandDebugger` over a ``W65C02Session``.""" prompt: ClassVar[str] = "65c02> " help_lines: ClassVar[tuple[str, ...]] = _HELP def format_state(self, state: CPUState) -> tuple[str, ...]: polled = "-" if state.polled_i is None else str(int(state.polled_i)) seen = "".join( f" POLLED_{name}={int(value)}" for name, value in ( ("NMI", state.polled_non_maskable_interrupt), ("IRQ", state.polled_maskable_interrupt), ) if value is not None ) skipped = " NO_POLL" if state.skipped_poll else "" registers = ( f"PC={state.pc:04X} A={state.a:02X} X={state.x:02X} Y={state.y:02X} " f"S={state.s:02X} P={state.p:02X} {_flags(state.p)} {self.session.row.__name__}" ) lifecycle = ( f"WAI={int(state.waiting)} STP={int(state.stopped)} RESET={int(state.reset_pending)} " f"NMI={int(state.non_maskable_interrupt_pending)} " f"IRQ={int(state.maskable_interrupt_pending)} POLLED_I={polled}{seen}{skipped}" ) return registers, lifecycle def disassemble_at(self, peek: ByteReader, address: int, state: CPUState) -> Instruction: return disassemble(peek, address, self.session.row) __all__ = ["CommandError", "CommandResult", "W65C02Debugger", "parse_number"]