#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run MAME's PC-98 headless as a closed-box detector: a port-I/O log and a screenshot. For --frames frames it logs every access the main CPU makes to I/O ports 0000h-FFFFh (emulated time, the linear PC of the instruction, direction, port, data) through Lua taps on the CPU's I/O space, then saves a screenshot and prints the access count per port. Firmware comes from $MAME_ROMPATH; nothing is copied. No disk is inserted, so a machine with its ROMs falls into N88-BASIC(86) (the ROM BASIC) after its self-test. MAME_ROMPATH=... python3 -I scripts/mame_run.py --machine pc9801vm --frames 600 MAME_ROMPATH=... python3 -I scripts/mame_run.py --machine pc9801 --frames 600 Everything lands in reference/mame// (git-ignored): io.log, snap/, error.log, cfg/. MAME 0.285 is a binary release (its source, BSD-3-Clause for the PC-98 files, is not opened). One MAME process at a time, short runs only. """ from __future__ import annotations import argparse import collections import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "reference" / "mame" LUA = """ cpu = manager.machine.devices[":maincpu"] space = cpu.spaces["io"] log = io.open("io.log", "w") n = 0 function rec(dir, offset, data, mask) n = n + 1 if n <= {limit} then log:write(string.format("%.9f %05X %s %04X %04X %04X\\n", manager.machine.time:as_double(), cpu.state["CURPC"].value, dir, offset, data & 0xffff, mask & 0xffff)) end end wtap = space:install_write_tap(0x0000, 0xffff, "w", function(offset, data, mask) rec("W", offset, data, mask) end) rtap = space:install_read_tap(0x0000, 0xffff, "r", function(offset, data, mask) rec("R", offset, data, mask) end) frames = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {frames} then log:close() manager.machine.video:snapshot() print("ACCESSES " .. n .. " TIME " .. manager.machine.time:as_double()) manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="pc9801vm") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--limit", type=int, default=400_000, help="most accesses logged") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("MAME_ROMPATH is not set", file=sys.stderr) return 2 run = RUNS / args.machine run.mkdir(parents=True, exist_ok=True) script = run / "tap.lua" script.write_text(LUA.replace("{limit}", str(args.limit)).replace("{frames}", str(args.frames))) command = [ args.mame, args.machine, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", "snap", "-autoboot_script", str(script), "-log", ] result = subprocess.run(command, cwd=run, capture_output=True, text=True, check=False) print(result.stdout.strip()) if result.returncode: print(result.stderr.strip(), file=sys.stderr) return result.returncode counts: collections.Counter[tuple[str, int]] = collections.Counter() for line in (run / "io.log").read_text().splitlines(): _, _, direction, port, _, mask = line.split() # The 16-bit bus taps report the even word address and a lane mask: an access to # the odd byte alone (mask FF00) is the odd port. counts[(direction, int(port, 16) + (1 if int(mask, 16) == 0xFF00 else 0))] += 1 for (direction, port), count in sorted(counts.items(), key=lambda item: (item[0][1], item[0][0])): print(f"{direction} {port:04X} {count}") return 0 if __name__ == "__main__": sys.exit(main())