"""Load a program and step through it: ``python -m sixfiveohtwo``. python -m sixfiveohtwo --load program.bin@0x0400 --pc 0x0400 python -m sixfiveohtwo --zip ROMPATH/set.zip:rom.bin@0xF800 --reset -c "run 100000" python -m sixfiveohtwo --load 6502_functional_test.bin@0 --pc 0x400 --batch -c "step 5" The host is flat 64 KiB RAM holding every image loaded, so ROM is writable and no device is mapped anywhere: good for reading and stepping through code, not for running a machine (for that, write a host; examples/minimal_host.py is the smallest). ``--reset`` starts through the reset vector at $FFFC instead of ``--pc``. Bus accesses are tracked, so ``watch`` works. Numbers are decimal; ``0x1234`` or ``$1234`` is hexadecimal. Commands given with ``-c`` run first; then the prompt reads stdin, unless ``--batch`` is given. ``help`` lists the commands. """ import argparse import sys import zipfile from pathlib import Path from sixfiveohtwo.console import CommandDebugger, CommandError, parse_number from sixfiveohtwo.cpu import MOS6502 from sixfiveohtwo.debug import DebugSession def _image(spec: str, *, zipped: bool) -> tuple[bytes, int]: source, _, where = spec.rpartition("@") if not source: raise SystemExit(f"{spec!r}: give the load address as FILE@ADDRESS") try: address = parse_number(where, "load address", maximum=0xFFFF) except CommandError as exc: raise SystemExit(f"{spec!r}: {exc}") from exc if zipped: archive, _, member = source.rpartition(":") if not archive: raise SystemExit(f"{spec!r}: give --zip as ZIPFILE:MEMBER@ADDRESS") with zipfile.ZipFile(archive) as bundle: data = bundle.read(member) else: data = Path(source).read_bytes() if address + len(data) > 0x10000: raise SystemExit(f"{spec!r}: {len(data)} bytes do not fit at 0x{address:04X}") return data, address def main(argv: list[str] | None = None) -> None: parser = argparse.ArgumentParser( prog="python -m sixfiveohtwo", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument( "--load", action="append", default=[], metavar="FILE@ADDRESS", help="load a binary file at an address", ) parser.add_argument( "--zip", action="append", default=[], metavar="ZIP:MEMBER@ADDRESS", help="load one file out of a zip (a MAME ROM set) at an address", ) parser.add_argument("--pc", default="0", help="start at this address (default 0)") parser.add_argument( "--reset", action="store_true", help="run the reset sequence first (PC from $FFFC)" ) 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) memory = bytearray(0x10000) for spec, zipped in [(s, False) for s in args.load] + [(s, True) for s in args.zip]: data, address = _image(spec, zipped=zipped) memory[address : address + len(data)] = data cpu = MOS6502(memory.__getitem__, memory.__setitem__) try: cpu.pc = parse_number(args.pc, "pc", maximum=0xFFFF) except CommandError as exc: raise SystemExit(f"--pc: {exc}") from exc session = DebugSession(cpu, peek_byte=memory.__getitem__, track_accesses=True) if args.reset: cpu.request_reset() session.step() debugger = CommandDebugger(session) for command in ["registers", "disassemble", *args.command]: print(f"6502> {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 if not args.batch: debugger.interact(sys.stdin, sys.stdout) if __name__ == "__main__": main()