# SPDX-License-Identifier: MIT """Run a program on the core in the minimal harness: ``python -m arm7tdmi_python``. python -m arm7tdmi_python jsmolka-gba-tests/arm/arm.gba python -m arm7tdmi_python rom.gba --max-cycles 1000000 --trace run.jsonl python -m arm7tdmi_python rom.gba --debug -c "break 0x080001A4" -c "continue" --batch python -m arm7tdmi_python multiboot.bin --at 0x02000000 --debug python -m arm7tdmi_python program.elf --debug -c "disassemble" --batch The host is not a GBA: memory only, with DISPSTAT's VBlank bit toggling and no BIOS (``arm7tdmi_python.harness``). A raw image loads at ``--at`` (the cartridge, ``0x08000000``, by default) and starts there; an ELF file loads its segments and starts at its entry point (Thumb if bit 0 is set). Without ``--debug`` the program runs until it branches to itself and R12 is printed: jsmolka's and alyosha-tas's tests leave 0 when every test passed, so the exit status is 0 then and 1 otherwise. ``--trace`` writes every bus cycle as JSON Lines (``arm7tdmi_python.trace``). ``--debug`` opens the command debugger (``help`` lists the commands): commands given with ``-c`` run first, then the prompt reads stdin unless ``--batch`` is given. Numbers are decimal; ``0x1234`` or ``$1234`` is hexadecimal. """ from __future__ import annotations import argparse import sys from pathlib import Path from arm7tdmi_python.console import CommandDebugger, CommandError, parse_number from arm7tdmi_python.debug import DebugSession from arm7tdmi_python.harness import EnteredBIOS, Harness, elf_image from arm7tdmi_python.trace import TraceWriter def _harness(path: Path, at: int, thumb: bool, observer: TraceWriter | None) -> Harness: """The harness with the program loaded and started; ``cycles`` and the trace start with the program's own two refill fetches.""" data = path.read_bytes() if data[:4] != b"\x7fELF" and at == 0x08000000 and not thumb: return Harness(data, observer=observer) # the cartridge, as the BIOS would leave it harness = Harness(b"") if data[:4] == b"\x7fELF": segments, entry = elf_image(data) for address, chunk in segments: harness.load(address, chunk) at, thumb = entry & ~1, bool(entry & 1) else: harness.load(at, data) harness.observer, harness.cycles = observer, 0 harness.start(at, thumb=thumb) return harness def _debug(harness: Harness, commands: list[str], batch: bool) -> int: session = DebugSession(harness.cpu, peek_byte=harness.peek, track_accesses=True) debugger = CommandDebugger(session, stop_when=harness.at_branch_to_self, loader=harness.load) for command in ["registers", *commands]: print(f"arm7> {command}") try: result = debugger.execute(command) except CommandError as exc: print(f"error: {exc}") continue for line in result.lines: print(line) if result.quit: return 0 if not batch: debugger.interact(sys.stdin, sys.stdout) return 0 def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="python -m arm7tdmi_python", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("rom", type=Path, help="a GBA ROM, a raw binary or an ARM ELF file") parser.add_argument("--at", default="0x08000000", metavar="ADDRESS", help="load address") parser.add_argument("--thumb", action="store_true", help="start a raw image in Thumb state") parser.add_argument("--max-cycles", type=int, default=50_000_000, metavar="N") parser.add_argument("--trace", type=Path, metavar="FILE", help="write every bus cycle here") parser.add_argument("--debug", action="store_true", help="open the command debugger") parser.add_argument("-c", "--command", action="append", default=[], help="debugger command") parser.add_argument("--batch", action="store_true", help="exit after the -c commands") args = parser.parse_args(argv) try: at = parse_number(args.at, "--at") except CommandError as exc: parser.error(str(exc)) stream = args.trace.open("w", encoding="utf-8") if args.trace else None try: harness = _harness(args.rom, at, args.thumb, TraceWriter(stream) if stream else None) if args.debug: return _debug(harness, args.command, args.batch) r12 = harness.run(args.max_cycles) except EnteredBIOS as error: print(f"{args.rom.name}: r12 = {error.r12}; then {error}", file=sys.stderr) return 1 except TimeoutError as error: print(f"{args.rom.name}: {error}", file=sys.stderr) return 2 finally: if stream: stream.close() print(f"{args.rom.name}: r12 = {r12} after {harness.cycles:,} cycles") return 0 if r12 == 0 else 1 if __name__ == "__main__": sys.exit(main())