# SPDX-License-Identifier: MIT """Run MAME 0.285's Enterprise drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --driver ep64 --frames 300 # OUT log + PNG python -I scripts/mame_trace.py --driver ep128 --frames 100 --trace 3 # + Z80 trace Without a cartridge the EXOS ROM runs to its own screen, which is all the board's first milestones need. A write tap on the Z80's I/O space logs every OUT as ``O port value frame seconds`` (outs.log). ``--trace N`` also traces the Z80 through the debugger for the first N frames (``T pc totalcycles``, one line per instruction, in run.log). The romset comes from MAME_ROMPATH. Output goes to reference/mame// (git-ignored). MAME is only run, never read here (docs/mame-oracle.md; its Enterprise files are BSD-3-Clause, docs/sources.md). Traps (shared with the family's other MAME recipes): the tap must be held in a Lua global or it is collected; -seconds_to_run takes whole seconds, so the run is stopped from Lua. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DRIVERS = {"ep64": ":u1", "ep128": ":u1", "phc64": ":u1"} def lua(cpu_tag: str, frames: int, trace: int) -> str: lines = [ f'local cpu = manager.machine.devices["{cpu_tag}"]', 'screen = manager.machine.screens[":screen"]', 'outs = io.open("outs.log", "w")', 'tap = cpu.spaces["io"]:install_write_tap(0x0000, 0xffff, "outs", function(offset, data, mask)', ' outs:write(string.format("O %04X %02X %d %.9f\\n", offset, data,' " screen:frame_number(), manager.machine.time:as_double()))", "end)", ] if trace: lines += [ "local dbg = manager.machine.debugger", "dbg.visible_cpu = cpu", f"dbg:command('trace T.trace,{cpu_tag},noloop,{{logerror \"T %X %d\\\\n\",pc,totalcycles}}')", 'dbg:command("go")', ] lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", ] if trace: lines += [f" if count == {trace} then manager.machine.debugger:command('trace off') end"] lines += [ f" if count == {frames} then", " manager.machine.video:snapshot()", " outs: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="ep128", choices=sorted(DRIVERS)) parser.add_argument("--frames", type=int, default=300, help="stop after this many frame notifications") parser.add_argument("--trace", type=int, default=0, help="trace the Z80 for N frame notifications") 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.frames, 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.frames / 50) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(["nice", *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())