"""Portable command debugger for the W65C816. 6502-python's :class:`~sixfiveohtwo.console.CommandDebugger` with this core's registers, 24-bit addresses, M/X-aware disassembly, ``over`` running JSR and JSL through, and ``abort``. ``execute(line)`` runs one command and returns printable lines; ``interact(input, output)`` is a line loop with no terminal dependencies. ``python -m w65c816`` starts one on a ROM. """ from __future__ import annotations from typing import ClassVar from sixfiveohtwo.console import CommandDebugger, CommandError, CommandResult, parse_number from w65c816.disasm import ByteReader, Instruction, disassemble from w65c816.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 or JSL 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 (24-bit: $008000)", "delete ADDRESS Remove an execute breakpoint", "breakpoints List breakpoints and watchpoints", "watch ADDRESS [r|w|rw] Stop after a step that reads or writes the byte", "unwatch ADDRESS Remove a watchpoint", "disassemble [ADDRESS] [COUNT] | d Decode with the current M and X flags", "memory ADDRESS [LENGTH] | m Display up to 256 bytes", "history [COUNT] Show retained step records", "set REGISTER VALUE Set A X Y S D DBR PBR PC P E", "irq on | irq off Assert or release the IRQ line (a level)", "nmi | abort | reset Latch an NMI or ABORT, or run the reset sequence", "quit | exit | q Leave the command loop", "Numbers are decimal; 0x1234 or $1234 is hexadecimal.", ) def _flags(p: int, e: int) -> str: labels = "NV1BDIZC" if e else "NVMXDIZC" return "".join(label if p & (0x80 >> index) else "-" for index, label in enumerate(labels)) class W65C816Debugger(CommandDebugger): """A :class:`~sixfiveohtwo.console.CommandDebugger` over a ``W65C816Session``.""" prompt: ClassVar[str] = "65816> " registers: ClassVar[dict[str, int]] = { "A": 0xFFFF, "X": 0xFFFF, "Y": 0xFFFF, "S": 0xFFFF, "D": 0xFFFF, "DBR": 0xFF, "PBR": 0xFF, "PC": 0xFFFF, "P": 0xFF, "E": 1, } help_lines: ClassVar[tuple[str, ...]] = _HELP call_mnemonics: ClassVar[frozenset[str]] = frozenset(("JSR", "JSL")) def format_state(self, state: CPUState) -> tuple[str, ...]: polled = "-" if state.polled_i is None else str(int(state.polled_i)) skipped = " NO_POLL" if state.skipped_poll else "" registers = ( f"PC={state.pbr:02X}:{state.pc:04X} A={state.a:04X} X={state.x:04X} " f"Y={state.y:04X} S={state.s:04X} D={state.d:04X} DB={state.dbr:02X} " f"P={state.p:02X} {_flags(state.p, state.e)} E={state.e}" ) lifecycle = ( f"WAI={int(state.waiting)} STP={int(state.stopped)} " f"RESET={int(state.reset_pending)} NMI={int(state.non_maskable_interrupt_pending)} " f"IRQ={int(state.maskable_interrupt_pending)} ABORT={int(state.abort_pending)} " f"POLLED_I={polled}{skipped}" ) return registers, lifecycle def disassemble_at(self, peek: ByteReader, address: int, state: CPUState) -> Instruction: return disassemble(peek, address, m=bool(state.p & 0x20), x=bool(state.p & 0x10)) def format_instruction(self, instruction: Instruction) -> str: encoded = " ".join(f"{value:02X}" for value in instruction.data) address = instruction.address return f"{address >> 16:02X}:{address & 0xFFFF:04X} {encoded:<11} {instruction.text}" def format_address(self, address: int) -> str: return f"{address >> 16:02X}:{address & 0xFFFF:04X}" def set_register(self, name: str, value: int) -> None: """Set a register as the chip would hold it: 8-bit index registers keep a zero high byte, and emulation mode keeps M, X and S's page.""" cpu = self.session.cpu setattr(cpu, name.lower(), value) if cpu.e: cpu.p |= 0x30 cpu.s = 0x0100 | cpu.s & 0xFF if cpu.p & 0x10: cpu.x &= 0xFF cpu.y &= 0xFF def execute(self, command: str) -> CommandResult: words = command.split() if words and words[0].lower() == "abort": self._arity("abort", words[1:], 0) self.session.cpu.request_abort() return CommandResult(("ABORT latched; taken at the next boundary",)) return super().execute(command) __all__ = ["CommandError", "CommandResult", "W65C816Debugger", "parse_number"]