# SPDX-License-Identifier: MIT """Run an SPC700 program on the core in the minimal harness: ``python -m spc700_python``. python -m spc700_python tune.spc python -m spc700_python program.bin --org 0x200 --max-cycles 5000000 python -m spc700_python program.bin --trace run.jsonl --max-cycles 100000 python -m spc700_python program.bin --debug -c "break 0x300" -c "continue" --batch A file with the ``.spc`` signature loads as a snapshot; anything else as a raw program at ``--org``, entered as the IPL ROM would enter it. The host is not a SNES: 64 KiB RAM, the S-SMP I/O page, timers and a DSP register file with no audio (``spc700_python.harness``). The run ends at a branch to itself, at SLEEP or STOP, or after ``--max-cycles``, and prints every value written to port $F4. ``--trace`` writes one JSON Line per processor boundary with every bus cycle (docs/trace-schema.md). ``--debug`` opens the command debugger (``help`` lists the commands); numbers are decimal, ``0x1234`` or ``$1234`` hexadecimal. """ from __future__ import annotations import argparse import json import sys from pathlib import Path from spc700_python.console import CommandDebugger, CommandError, format_state from spc700_python.debug import DebugSession from spc700_python.harness import Harness, default_ipl from spc700_python.trace import step_record_to_dict def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="python -m spc700_python", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("program", type=Path, help="a .spc snapshot or a raw program") parser.add_argument("--org", type=lambda text: int(text, 0), default=0x0300, metavar="ADDRESS") parser.add_argument("--max-cycles", type=int, default=100_000_000, metavar="N") parser.add_argument("--trace", type=Path, metavar="FILE", help="write a JSONL trace") 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) harness = Harness(default_ipl()) harness.load_file(args.program.read_bytes(), args.org) if args.debug: session = DebugSession(harness.cpu, peek_byte=harness.peek, track_accesses=True) debugger = CommandDebugger(session) for command in ["registers", "disassemble", *args.command]: print(f"spc700> {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 trace_file = args.trace.open("w", encoding="utf-8") if args.trace else None try: step = None if trace_file is not None: session = DebugSession( harness.cpu, peek_byte=harness.peek, history_limit=0, track_accesses=True ) def step() -> int: record = session.step() encoded = json.dumps( step_record_to_dict(record), separators=(",", ":"), sort_keys=True ) trace_file.write(encoded + "\n") return record.cycles result = harness.run(max_cycles=args.max_cycles, step=step) finally: if trace_file is not None: trace_file.close() for cycle, port, value in harness.port_log: if port == 0: print(f"{cycle:>12} $F4 = ${value:02X}") print( f"[{result.reason} after {result.cycles} cycles, {result.instructions} instructions]", file=sys.stderr, ) print(*format_state(harness.cpu.capture_state()), sep="\n", file=sys.stderr) return 0 if __name__ == "__main__": raise SystemExit(main())