# Debugging and the console Three layers, each usable alone, none of which changes what the core does: - `arm7tdmi_python.disasm`: text for ARM words and Thumb halfwords (see [disassembly.md](disassembly.md)). - `arm7tdmi_python.debug`: `DebugSession`, execution control over any CPU host, with records of every step and, optionally, every bus cycle. - `arm7tdmi_python.console`: `CommandDebugger`, a line-oriented command set over a session, which `python -m arm7tdmi_python ROM --debug` opens on a program in the minimal harness. The shape is the family's: z80-python's `DebugSession` and `CommandDebugger`, and sm83-python's bus records that include internal cycles. ## DebugSession ```python from arm7tdmi_python.debug import DebugSession, StopReason from arm7tdmi_python.harness import Harness harness = Harness(rom) session = DebugSession(harness.cpu, peek_byte=harness.peek, track_accesses=True) session.add_breakpoint(0x080001A4) result = session.run(max_steps=1_000_000) # result.reason is StopReason.BREAKPOINT record = session.step() record.instruction.text # what the core ran [cycle.text for cycle in record.accesses] # 'S read 080001AC 32-bit E3A00001 code', ... ``` - **The target** is anything with `step()` (returning the bus cycles it made) and `capture_state()`. The session finds the processor as `target.cpu`, falling back to the target itself. Pass the core to step instructions; pass a board to step whatever the board calls one boundary. - **Step records** (`StepRecord`) hold: - the boundary kind (`INSTRUCTION`, `IRQ` or `FIQ`; an interrupt entry is a step of its own); - the instruction's address; - the state before and after; - the cycle count; - the instruction decoded from the core's **pipeline**: what it actually ran, whatever memory holds now; - when tracking, every bus cycle. - **Bus cycles** (`BusCycle`) hold: - `kind`: `read`, `write`, `internal` or `coprocessor`; - `address`, as the core drove it; - `width`, `access` (SEQ/CODE/LOCK) and `value`; - `type`: `N`, `S`, `I` or `C` (TRM 3.3). These are the pins as the core reports them. The fetch after an I cycle is S on the pins (merged IS); the GBA times it as N, which the board applies with `gba_access`. - **Breakpoints** stop before the instruction at an address. An interrupt entry is not an instruction, so a breakpoint never stops before one. - **Watchpoints** (`add_watchpoint(address, "rwi", length)`) stop after the step whose cycles touched the watched bytes: - a read or write touches the bytes its width covers at the aligned address (what memory sees); - an internal or coprocessor cycle touches the one address the core drove (timing.md gives them). - **`run(max_steps, max_cycles=, until=, stop_when=)`**: the step budget is mandatory. `until` stops before the instruction at that address, and `stop_when()` is checked before each step. - **`step_over()`** steps one instruction. If it is a call (BL, a Thumb BL pair from its first half, either BL half, or SWI), it runs on until the instruction after it, with SP and the mode back where they were. So a recursive call that passes the return address does not stop it. A breakpoint or watchpoint stops it first. - **Interrupt lines**: `cpu.irq` and `cpu.fiq`. `cpu.pending_interrupt()` says what the next step takes, decided exactly as `step()` decides it, with the masks of the instruction that just ended and the current ones (U14). - **Bus tracking** works by wrapping the core's `read`, `write` and `idle` callbacks, the same objects the core calls. `close()` puts the host's back. ### From a board (gba-python) The session wraps the callbacks the board gave the core, so tracking and watchpoints see each cycle as the core makes it, before the board adds wait states or DMA. A board debugger makes a small target whose `step()` is one board boundary and passes `peek_byte` as a side-effect-free read of the board's memory: ```python class BoardTarget: def __init__(self, gba): self.gba, self.cpu = gba, gba.cpu def step(self) -> int: start = self.gba.scheduler.now if self.gba.halted: self.gba.run(1) # the board runs its devices until the CPU wakes else: self.cpu.step() return self.gba.scheduler.now - start def capture_state(self): return self.cpu.capture_state() session = DebugSession(BoardTarget(gba), peek_byte=gba_peek, track_accesses=True) ``` This exact target was run (2026-11-04) against gba-python `a432e93`, with this core put ahead of the board's pinned one on `sys.path`. The run used jsmolka's `thumb.gba` with the BIOS skipped and drove `CommandDebugger` over it. Registers, stepping, `cycles`, a breakpoint, step-over across a BL, a write watchpoint and Thumb listings all worked, and the board needed no change. The cycle counts there are system clocks, wait states included: `B 0x080000C0` costs 20, where the minimal harness reports 3 bus cycles. gba-python's console (`python -m gba_python ROM --debug`) uses this target, a peek over its memory and `commands` for its own views. ## The console `CommandDebugger(session, stop_when=None, stop_label=..., loader=None, commands=None)` runs one command per call (`execute(line)` returns lines) or a loop (`interact(input, output)`). `stop_label` names what `stop_when` checks in the stop line (a string, or a function called when it fires). A board adds its own commands with `commands`, a mapping from a tuple of names to `(handler, help_lines)`: `handler(args)` returns a `CommandResult`, the help lines join `help`, and a name that matches a built-in one replaces it. gba-python's console is built this way. `help` lists the commands: | Command | Does | | --- | --- | | `registers` / `r` | R0–R15, CPSR, the current mode's SPSR, the next instruction | | `banks` | every mode's banked registers and SPSR | | `step [N]` / `s` | one or more boundaries (ignores breakpoints) | | `over` / `o` | step-over as above | | `until ADDRESS` / `u` | run to an address | | `run STEPS [CYCLES]`, `continue` / `c` | bounded runs; `continue` steps off a breakpoint first | | `break`, `delete`, `breakpoints` | execute breakpoints | | `watch ADDRESS [r\|w\|i…] [LENGTH]`, `unwatch` | bus watchpoints | | `cycles [STEPS]` | the bus cycles of the last steps, typed | | `disassemble [ADDR] [N] [arm\|thumb]` / `d` | listing from memory (default: the next instruction, current state) | | `memory ADDRESS [LENGTH] [b\|h\|w]` / `m` | bytes, halfwords or words | | `history [N]` | retained step records | | `set REGISTER VALUE` | R0–R15, SP, LR, PC, CPSR, SPSR | | `irq on\|off`, `fiq on\|off` | the interrupt lines | | `load FILE [ADDRESS]` | a raw binary at an address, or an ELF's segments | | `quit` | leave | Some commands change state the way the hardware would: - `set PC` refills the pipeline (two fetches, as a branch would). - `set CPSR` swaps banks as an MSR would, but changing T does not refill: set PC after it. - If the host raises while running, the session reports it as lines and keeps going. In the minimal harness that is `EnteredBIOS` for an SWI. The instruction it stopped may be half done. Numbers are decimal; `0x…` and `$…` are hexadecimal. ## The command line ```sh python -m arm7tdmi_python ROM.gba # run to a branch to itself, print R12 python -m arm7tdmi_python ROM.gba --trace run.jsonl # every bus cycle as JSON Lines python -m arm7tdmi_python ROM.gba --debug # the console python -m arm7tdmi_python ROM.gba --debug -c "break 0x080001A4" -c c --batch python -m arm7tdmi_python multiboot.bin --at 0x02000000 # a raw image elsewhere python -m arm7tdmi_python program.elf --debug # an ELF: segments and entry point ``` The host is the minimal harness: memory only, DISPSTAT's VBlank bit toggling, and no BIOS. In the console, `continue`, `run` and `until` also stop at a branch to itself, which is how jsmolka's and alyosha-tas's tests end. A session on jsmolka's `thumb.gba`, abridged: ```text arm7> registers R0=00000000 R1=00000000 R2=00000000 R3=00000000 ... R12=00000000 SP=03007F00 LR=00000000 PC=08000008 CPSR=0000001F nzcv ift SYS SPSR=-------- next 08000000 EA00002E B 0x080000C0 arm7> step 3 #0 08000000 B 0x080000C0 3 cycles -> 080000C0 #1 080000C0 BL 0x08000AB0 3 cycles -> 08000AB0 #2 08000AB0 STMDB SP!, {R0, R1, LR} 4 cycles -> 08000AB4 arm7> cycles 1 #2 08000AB0 STMDB SP!, {R0, R1, LR} 4 cycles -> 08000AB4 S read 08000AB8 32-bit E3800B01 code N write 03007EF4 32-bit 00000000 S write 03007EF8 32-bit 00000000 S write 03007EFC 32-bit 080000C4 arm7> until 0x08000100 arm7> d 08000100 D102 BNE 0x08000108 08000102 2001 MOV R0, #0x1 ... 0800010A F000 FC11 BL 0x08000930 arm7> watch 0x03007F00 w 64 arm7> c watchpoint: 624 steps, 898 cycles; N write 03007F04 32-bit FFFFFFFF ... next 080007E4 9901 LDR R1, [SP, #0x4] arm7> unwatch 0x03007F00 arm7> c stopped: the host's stop condition (branch to itself): 6209 steps, 8800 cycles ... R12=00000000 SP=03007F00 LR=08000B38 PC=08000AB4 ``` ## Traces `--trace FILE` writes one JSON object per bus cycle in chip order. This is the family schema of sm83-python, with ARM's `width` and `access` added (`arm7tdmi_python.trace`). The console's `cycles` command shows the same cycles for the steps it has kept.