"""Run a SNES ROM in the minimal harness, or step through it: ``python -m w65c816``. python -m w65c816 CPUADC.sfc --screen 0xF800 python -m w65c816 cputest-full.sfc --max-cycles 20000000 --trace full.jsonl python -m w65c816 game.zip --debug -c "break $008000" -c continue The host is ``w65c816.harness``: a LoROM or HiROM cartridge, WRAM, instant DMA, a VRAM port and the few registers CPU test ROMs use; not a SNES (that is snes-python). A run stops when the ROM spins on a jump to itself, at STP, or after ``--max-cycles`` CPU cycles, and prints why and where; ``--screen`` prints the text of the 32-column tilemap at that VRAM word address. ``--trace`` writes every boundary as a JSON Lines record, bus accesses included (``w65c816.trace``). ``--debug`` (or any ``-c``) opens the command debugger instead (``w65c816.console``); ``-c`` commands run first, then the prompt reads stdin unless ``--batch`` is given. ``help`` lists the commands. Numbers are decimal; ``0x1234`` or ``$1234`` is hexadecimal. ``.zip`` and ``.7z`` take the first ``.sfc``/``.smc`` inside. """ from __future__ import annotations import argparse import sys from pathlib import Path from w65c816.console import CommandError, W65C816Debugger, parse_number from w65c816.debug import W65C816Session from w65c816.harness import Harness, load_rom from w65c816.trace import write_trace def _number(text: str, name: str) -> int: try: return parse_number(text, name) except CommandError as exc: raise SystemExit(f"{name}: {exc}") from exc def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="python -m w65c816", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("rom", type=Path, help=".sfc, .smc, or a .zip/.7z holding one") parser.add_argument("--max-cycles", default="10000000", help="CPU cycles to run at most") parser.add_argument("--trace", type=Path, metavar="OUT.jsonl", help="write every boundary") parser.add_argument("--screen", metavar="WORD", help="print the tilemap text at this word") parser.add_argument("--mapping", choices=("lorom", "hirom"), help="default: from the header") parser.add_argument("--debug", action="store_true", help="open the command debugger") parser.add_argument( "-c", "--command", action="append", default=[], help="a debugger command to run first (repeatable)", ) parser.add_argument("--batch", action="store_true", help="exit after the -c commands") args = parser.parse_args(argv) harness = Harness(load_rom(args.rom), args.mapping) if args.debug or args.command: session = W65C816Session(harness.cpu, peek_byte=harness.inspect, track_accesses=True) session.step() # the reset sequence debugger = W65C816Debugger(session) for command in ["registers", "disassemble", *args.command]: print(f"{debugger.prompt}{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 args.batch: debugger.interact(sys.stdin, sys.stdout) return 0 max_cycles = _number(args.max_cycles, "--max-cycles") if args.trace is None: result = harness.run(max_cycles) else: session = W65C816Session( harness.cpu, peek_byte=harness.inspect, history_limit=0, track_accesses=True ) with args.trace.open("w", encoding="utf-8") as out: result = harness.run(max_cycles, step=lambda: write_trace((session.step(),), out)) session.close() print( f"{result.reason} after {result.cycles:,} cycles at " f"{result.pc >> 16:02X}:{result.pc & 0xFFFF:04X} ({harness.mapping})" ) if args.screen is not None: for line in harness.screen(_number(args.screen, "--screen")): if line.strip(): print(line) return 0 if __name__ == "__main__": sys.exit(main())