# SPDX-License-Identifier: MIT """Run MAME 0.285's Amstrad CPC drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --driver cpc6128 --seconds 3 # I/O log + PNG python -I scripts/mame_trace.py --driver cpc464 --seconds 1 --trace 0.05 # + Z80 trace MAME's CPC driver (``src/mame/amstrad/amstrad*.cpp``) is GPL-2.0+ by its ``license:`` lines: only the installed binary is run, its source is never fetched or read. With no tape, disc or cartridge the firmware runs to the BASIC prompt, which is what the board's first milestones need. A write tap and a read tap on the Z80's I/O space log every OUT and IN with the emulated time in seconds (``O port value seconds`` / ``I port value seconds`` in io.log); the taps are held in Lua globals so they are not collected. ``--trace S`` also traces the Z80 for the first S emulated seconds through the debugger (``T pc totalcycles``, one line per instruction, in run.log), which is what scripts/mame_summary.py reads to see how MAME times instructions. The run stops from Lua when the emulated time passes ``--seconds``. The romsets come from MAME_ROMPATH; output goes to reference/mame// (git-ignored). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DRIVERS = ("cpc464", "cpc664", "cpc6128", "cpc6128f", "cpc6128s", "cpc6128sp", "cpc464p", "cpc6128p", "gx4000", "kccomp") # fmt: skip def lua(seconds: float, trace: float) -> str: lines = [ 'cpu = manager.machine.devices[":maincpu"]', 'log = io.open("io.log", "w")', "local function now() return manager.machine.time:as_double() end", 'wtap = cpu.spaces["io"]:install_write_tap(0x0000, 0xffff, "outs", function(offset, data, mask)', ' log:write(string.format("O %04X %02X %.9f\\n", offset, data, now()))', "end)", 'rtap = cpu.spaces["io"]:install_read_tap(0x0000, 0xffff, "ins", function(offset, data, mask)', ' log:write(string.format("I %04X %02X %.9f\\n", offset, data, now()))', "end)", ] if trace: lines += [ "dbg = manager.machine.debugger", "dbg.visible_cpu = cpu", "dbg:command('trace T.trace,:maincpu,noloop,{logerror \"T %X %d\\\\n\",pc,totalcycles}')", 'dbg:command("go")', "tracing = true", ] lines += [ "notifier = emu.add_machine_frame_notifier(function()", " local t = now()", ] if trace: lines += [ f" if tracing and t >= {trace} then", " manager.machine.debugger:command('trace off')", " tracing = false", " end", ] lines += [ f" if t >= {seconds} then", " manager.machine.video:snapshot()", " log:close()", " manager.machine:exit()", " end", "end)", ] return "\n".join(lines) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--driver", default="cpc6128", choices=DRIVERS) parser.add_argument("--seconds", type=float, default=3.0, help="emulated seconds to run") parser.add_argument("--trace", type=float, default=0.0, help="trace the Z80 for S seconds") parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") args = parser.parse_args() if not args.rompath: print(f"set MAME_ROMPATH (it must hold {args.driver}.zip)", file=sys.stderr) return 2 out = ROOT / "reference" / "mame" / args.driver out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.seconds, args.trace), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(args.seconds) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.trace: command += ["-debug", "-debugger", "none"] # Polite to the other agents' gates: low priority, CPUs 24-31, one process. result = subprocess.run(["nice", "taskset", "-c", "24-31", *command], cwd=out, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # fmt: skip (out / "stdout.txt").write_text(result.stdout, encoding="utf-8") log = out / "error.log" if log.exists(): log.replace(out / "run.log") (out / "T.trace").unlink(missing_ok=True) # the disassembly; run.log has the state print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())