#!/usr/bin/env python # SPDX-License-Identifier: MIT """Count the machines in MAME that instantiate a YM2151 or a YM2164. python -I scripts/mame_census.py # runs `mame -listxml`, no game is run python -I scripts/mame_census.py --mame /usr/games/mame --sets DIR [--sets DIR ...] It streams `mame -listxml` (about 300 MB of XML, never written to disk) and keeps, for every machine with a `` whose name mentions YM2151 or YM2164: the machine, its parent, its driver source file, the chip tag and its clock. The table lands in ``reference/census/`` (git-ignored); the summary it prints is what docs/users.md records. With ``--sets`` it also says how many of those machines have a zip of the same name in each ROM directory (a listing only: nothing is opened or hashed). """ from __future__ import annotations import argparse import collections import subprocess import sys import xml.etree.ElementTree as ET from pathlib import Path ROOT = Path(__file__).resolve().parent.parent OUT = ROOT / "reference" / "census" WANTED = ("YM2151", "YM2164") def machines(mame: str): process = subprocess.Popen([mame, "-listxml"], stdout=subprocess.PIPE) assert process.stdout is not None build = "?" for event, element in ET.iterparse(process.stdout, events=("start", "end")): if event == "start": if element.tag == "mame": build = element.get("build", "?") continue if element.tag != "machine": continue chips = [ (chip.get("tag", ""), chip.get("name", ""), chip.get("clock", "")) for chip in element.findall("chip") if any(word in chip.get("name", "") for word in WANTED) ] if chips: yield { "build": build, "name": element.get("name", ""), "cloneof": element.get("cloneof", ""), "source": element.get("sourcefile", ""), "runnable": element.get("runnable", "yes"), "isdevice": element.get("isdevice", "no"), "description": (element.findtext("description") or "").strip(), "year": (element.findtext("year") or "").strip(), "manufacturer": (element.findtext("manufacturer") or "").strip(), "chips": chips, } element.clear() process.wait() def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--mame", default="mame") parser.add_argument("--sets", action="append", default=[], help="a directory of .zip") args = parser.parse_args(argv) rows = list(machines(args.mame)) if not rows: print("no machine found: is this a MAME binary?") return 1 OUT.mkdir(parents=True, exist_ok=True) table = OUT / "ym2151-machines.tsv" with table.open("w", encoding="utf-8") as handle: handle.write("machine\tcloneof\tsource\tyear\tmanufacturer\tchip\ttag\tclock\tdescription\n") for row in rows: for tag, name, clock in row["chips"]: handle.write( "\t".join( [ row["name"], row["cloneof"], row["source"], row["year"], row["manufacturer"], name, tag, clock, row["description"], ] ) + "\n" ) print(f"MAME build: {rows[0]['build']}") for word in WANTED: with_chip = [row for row in rows if any(word in name for _, name, _ in row["chips"])] parents = [row for row in with_chip if not row["cloneof"]] sources = {row["source"] for row in with_chip} count = sum(sum(1 for _, name, _ in row["chips"] if word in name) for row in with_chip) print( f"{word}: {len(with_chip)} machines ({len(parents)} parents), " f"{len(sources)} driver source files, {count} chip instances" ) clocks = collections.Counter( clock for row in rows for _, name, clock in row["chips"] if "YM2151" in name ) print("YM2151 clocks (Hz: instances):") for clock, number in clocks.most_common(12): print(f" {clock}: {number}") print(f" ({len(clocks)} distinct clocks)") by_source = collections.Counter(row["source"] for row in rows) print("driver source files with the most machines:") for source, number in by_source.most_common(25): print(f" {source}: {number}") names = {row["name"] for row in rows} for directory in args.sets: present = {path.stem for path in Path(directory).glob("*.zip")} & names print(f"{directory}: {len(present)} of {len(names)} machines have a zip of that name") print(f"table: {table}") return 0 if __name__ == "__main__": sys.exit(main())