# SPDX-License-Identifier: MIT """Run MAME 0.285's NEC PC-8801 drivers headless, as a closed-box detector (docs/mame-oracle.md). MAME_ROMPATH=... python -I scripts/mame_trace.py --driver pc8801mk2sr --seconds 3 MAME_ROMPATH=... python -I scripts/mame_trace.py --driver pc8801ma --cpu sub --trace 0.05 MAME's PC-8801 driver (src/mame/nec/pc8801.cpp, pc8801.h) and its disk unit (src/devices/bus/nec_fdd/pc80s31k.cpp) are BSD-3-Clause by their ``license:`` lines, but the Phase 0 brief treats MAME's drivers as closed boxes: only the installed binary is run and nothing of the source is fetched or read. With no disk the BASIC ROM comes up in ROM BASIC. A write tap and a read tap on the chosen Z80's I/O space (``--cpu main``: the main Z80; ``--cpu sub``: the disk unit's Z80) log every OUT and IN with the emulated time (``O port value seconds`` and ``I port value seconds`` in io.log; MAME maps both Z80s' I/O spaces as 8-bit, the PC-8801's decode); 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 = ("pc8801", "pc8801mk2", "pc8801mk2sr", "pc8801mk2fr", "pc8801mk2mr", "pc8801mh", "pc8801fa", "pc8801ma", "pc8801ma2", "pc8801mc") # fmt: skip CPUS = {"main": ":z80", "sub": ":pc80s31:fdc_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(0x00, 0xff, "outs", function(offset, data, mask)', ' log:write(string.format("O %02X %02X %.9f\\n", offset, data, now()))', "end)", 'rtap = cpu.spaces["io"]:install_read_tap(0x00, 0xff, "ins", function(offset, data, mask)', ' log:write(string.format("I %02X %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="pc8801mk2sr", choices=DRIVERS) parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) 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" / f"{args.driver}-{args.cpu}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(CPUS[args.cpu], 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())