# SPDX-License-Identifier: MIT """Run a DSP program in the minimal harness: ``python -m upd7725_python``. python -m upd7725_python sns_dsp1leg.zip --command 2F/0=1 python -m upd7725_python sns_dsp1leg.zip --command 1F/0=1024 --trace run.jsonl python -m upd7725_python sns_dsp4leg.zip --word --command 14=1 python -m upd7725_python sns_seta11leg.zip --bytes --command F1=1 python -m upd7725_python sns_dsp1leg.zip --debug -c "break 0x4EF" -c continue --batch IMAGE is a MAME device-set ``.zip`` or any of the three image formats (``images.py``); a ``.program.rom`` takes its ``.data.rom`` with ``--data``. The core resets, runs to its ``JRQM $`` wait, then performs each ``--command CODE[/P1,P2,...][=RESULTS]`` (hexadecimal code and parameters, decimal result count): the code as a byte (``--word``: a 16-bit word, as the DSP-4 takes it), each parameter as a 16-bit word, then RESULTS words read back (``--bytes``: single bytes, as the ST011 answers) and printed. The host is not a SNES: it waits for RQM and leaves ``host_delay`` instructions between accesses (harness.py). ``--trace`` writes one JSON Line per instruction and per host access (trace.py). """ from __future__ import annotations import argparse import sys from pathlib import Path from upd7725_python.console import CommandDebugger, CommandError, format_state from upd7725_python.cpu import UPD7725 from upd7725_python.debug import DebugSession from upd7725_python.harness import Harness, HostTimeout from upd7725_python.images import load_image, model_for from upd7725_python.model import MODELS from upd7725_python.trace import encode def parse_command(text: str) -> tuple[int, list[int], int]: """``CODE[/P1,P2...][=RESULTS]`` -> (code, parameters, result count).""" head, _, results = text.partition("=") code, _, parameters = head.partition("/") values = [int(value, 16) for value in parameters.split(",") if value] return int(code, 16), values, int(results) if results else 1 def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser( prog="python -m upd7725_python", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, ) parser.add_argument("image", type=Path) parser.add_argument("--data", type=Path, help="the .data.rom of a .program.rom") parser.add_argument("--model", choices=sorted(MODELS), help="default: from the image size") parser.add_argument("--command", action="append", default=[], metavar="CODE[/P,...][=N]") parser.add_argument("--word", action="store_true", help="send command codes as 16-bit words") parser.add_argument("--bytes", action="store_true", help="read results as single bytes") parser.add_argument("--max-instructions", type=int, default=50_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", dest="commands", action="append", default=[], help="debugger command") parser.add_argument("--batch", action="store_true", help="exit after the -c commands") args = parser.parse_args(argv) program, data_rom = load_image(args.image, args.data) model = MODELS[args.model] if args.model else model_for(program) harness = Harness(program, data_rom, dsp=UPD7725(program, data_rom, model=model)) if args.debug: debugger = CommandDebugger(DebugSession(harness)) for command in ["registers", "disassemble", *args.commands]: print(f"upd7725> {command}") try: result = debugger.execute(command) except (CommandError, ValueError) as exc: print(f"error: {exc}") continue print(*result.lines, sep="\n") if result.quit: return 0 if not args.batch: debugger.interact(sys.stdin, sys.stdout) return 0 trace = args.trace.open("w", encoding="utf-8") if args.trace else None try: if trace is not None: session = DebugSession(harness, history_limit=0) flushed = 0 def step() -> int: nonlocal flushed for access in session.host_log[flushed:]: trace.write(encode(access) + "\n") flushed = len(session.host_log) trace.write(encode(session.step()) + "\n") return 1 harness.step = step budget = args.max_instructions harness.run_to_idle(budget) for text in args.command: code, parameters, count = parse_command(text) if args.bytes: harness.write_byte(code, budget) for value in parameters: harness.write_word(value, budget) results = [harness.read_byte(budget) for _ in range(count)] width = 2 else: results = harness.command( code, parameters, count, command_word=args.word, budget=budget ) width = 4 print(f"{code:02X}: " + " ".join(f"{value:0{width}X}" for value in results)) harness.run_to_idle(budget) except HostTimeout as exc: print(f"timeout: {exc}", file=sys.stderr) return 1 finally: if trace is not None: trace.close() print(f"[{harness.instructions} instructions]", file=sys.stderr) print(*format_state(harness.dsp.capture_state(ram=False)), sep="\n", file=sys.stderr) return 0 if __name__ == "__main__": raise SystemExit(main())