# SPDX-License-Identifier: MIT """Run a memory image on a 65C02 with flat RAM, or step through it: ``python -m w65c02_python``. python -m w65c02_python test.bin --row g65sc02 --start 0x400 --max-cycles 100000000 python -m w65c02_python test.bin --row r65c02 --start 0x400 --trace out.jsonl --max-cycles 1000 python -m w65c02_python rom.bin --load 0xC000 --debug -c "break $C100" -c continue The host is ``w65c02_python.harness``: 64 KB of RAM, the image copied in at ``--load`` (default 0), no devices. Execution starts at ``--start``, or at the reset vector the image holds at ``$FFFC`` when ``--start`` is not given. ``--row`` picks the part: ``w65c02s`` (default), ``r65c02``, ``g65sc02`` or ``lynx2cell``. A run stops when the program traps on a jump or branch to itself, when ``STP`` or ``WAI`` halts the CPU, or after ``--max-cycles`` cycles, and prints why and where. ``--trace`` writes every boundary as a JSON Lines record, bus accesses included (``w65c02_python.trace``). ``--debug`` (or any ``-c``) opens the command debugger instead (``w65c02_python.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. """ from __future__ import annotations import argparse import sys from pathlib import Path from w65c02_python.console import CommandError, W65C02Debugger, parse_number from w65c02_python.debug import W65C02Session from w65c02_python.harness import ROWS, Harness from w65c02_python.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 w65c02_python", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("image", type=Path, help="a raw memory image") parser.add_argument("--row", choices=sorted(ROWS), default="w65c02s", help="the part") parser.add_argument("--load", default="0", help="where the image goes (default 0)") parser.add_argument("--start", help="where execution starts (default: the reset vector)") parser.add_argument("--max-cycles", default="10000000", help="cycles to run at most") parser.add_argument("--trace", type=Path, metavar="OUT.jsonl", help="write every boundary") 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" ) parser.add_argument("--batch", action="store_true", help="exit after the -c commands") args = parser.parse_args(argv) harness = Harness(ROWS[args.row]) try: harness.load(args.image.read_bytes(), _number(args.load, "--load")) except ValueError as exc: raise SystemExit(str(exc)) from exc harness.start(None if args.start is None else _number(args.start, "--start")) if args.debug or args.command: session = W65C02Session(harness.cpu, peek_byte=harness.peek, track_accesses=True) debugger = W65C02Debugger(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 = W65C02Session( harness.cpu, peek_byte=harness.peek, 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 {result.pc:04X}") return 0 if __name__ == "__main__": sys.exit(main())