#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run MAME's IBM PC headless as a closed-box detector: a port-I/O log and a screenshot. For --frames frames it logs every access the 8088 makes to I/O ports 000h-3FFh (emulated time in seconds, CS:IP of the instruction, direction, port, data) through Lua taps on the CPU's I/O space, then saves a screenshot and prints the access counts per port. The BIOS comes from $MAME_ROMPATH; nothing is copied. MAME_ROMPATH=... python3 -I scripts/mame_run.py --frames 1200 --no-hdc MAME_ROMPATH=... python3 -I scripts/mame_run.py --machine ibm5160 --bios rev1 Everything lands in tests/mame_traces/-/ (git-ignored): io.log, snap//0000.png, error.log, cfg/. 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 / "tests" / "mame_traces" LUA = """ cpu = manager.machine.devices[":maincpu"] space = cpu.spaces["io"] log = io.open("io.log", "w") n = 0 function rec(dir, offset, data) n = n + 1 if n <= {limit} then log:write(string.format("%.9f %04X:%04X %s %03X %02X\\n", manager.machine.time:as_double(), cpu.state["CS"].value, cpu.state["IP"].value, dir, offset, data & 0xff)) end end wtap = space:install_write_tap(0x000, 0x3ff, "w", function(offset, data, mask) rec("W", offset, data) end) rtap = space:install_read_tap(0x000, 0x3ff, "r", function(offset, data, mask) rec("R", offset, data) 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="ibm5150") parser.add_argument("--bios", default="rev3") parser.add_argument("--ramsize", default="640K", help="must agree with MAME's default switch settings (640K)") parser.add_argument("--no-hdc", action="store_true", help="empty slot isa4 (MAME's default fits a hard disk controller)") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--limit", type=int, default=500_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 / f"{args.machine}-{args.bios}" 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, "-bios", args.bios, "-ramsize", args.ramsize, "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", "snap", "-autoboot_script", str(script), "-log", ] if args.no_hdc: command += ["-isa4", ""] 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, _ = line.split() counts[(direction, int(port, 16))] += 1 for (direction, port), count in sorted(counts.items(), key=lambda item: (item[0][1], item[0][0])): print(f"{direction} {port:03X} {count}") return 0 if __name__ == "__main__": sys.exit(main())