# SPDX-License-Identifier: MIT """Run MAME 0.285's Sharp X1 drivers headless, as a closed-box detector (docs/mame-oracle.md). MAME_ROMPATH=... python -I scripts/mame_trace.py --driver x1 --seconds 3 MAME_ROMPATH=... python -I scripts/mame_trace.py --driver x1turbo --seconds 2 --trace 0.05 MAME's X1 driver (src/mame/sharp/x1*.cpp, x1.h) is **LGPL-2.1+** by its ``license:`` lines: only the installed binary is run, its source is never fetched or read. With no disk or tape the IPL ROM runs to its "no system disk" wait, which is what the first milestones need. A write tap and a read tap on the main Z80's I/O space (16-bit port numbers: the X1 decodes all of A0-A15) log every OUT and IN with the emulated time (``O port value seconds`` and ``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 (``T pc totalcycles``, one line per instruction, in run.log). The run stops from Lua once the emulated time passes ``--seconds`` and saves one snapshot. 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 = {"x1": ":x1_cpu", "x1turbo": ":x1_cpu", "x1turbo40": ":x1_cpu", "x1twin": ":x1_cpu"} def lua(cpu: str, seconds: float, trace: float) -> str: lines = [ f'cpu = manager.machine.devices["{cpu}"]', '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", f"dbg:command('trace T.trace,{cpu},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="x1", choices=sorted(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(DRIVERS[args.driver], 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 at a time. 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 keeps pc and cycles print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())