# SPDX-License-Identifier: MIT """Census of the Williams 6809 romsets, read in place from $MAME_ROMPATH. MAME_ROMPATH=... python -I scripts/census.py # every set in midway/williams.cpp MAME_ROMPATH=... python -I scripts/census.py --hashes # also print every member's SHA-1 What it does: asks the installed MAME (a closed box: run, never read) for its romset descriptions (`mame -listsource`, then `mame -listxml` for the sets whose source file is midway/williams.cpp), opens each `.zip` under MAME_ROMPATH read-only, hashes every member with SHA-1 and matches the hashes against MAME's list. Nothing is copied, extracted or written except the summary on stdout. The romsets are Williams's (and bootleggers') copyright: they are identified by SHA-1 and never committed. """ from __future__ import annotations import argparse import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from collections import Counter from pathlib import Path SOURCE = "midway/williams.cpp" def mame_sets(mame: str) -> list[str]: out = subprocess.run([mame, "-listsource"], capture_output=True, text=True, check=True).stdout return sorted(line.split()[0] for line in out.splitlines() if line.split()[1:2] == [SOURCE]) def mame_xml(mame: str, sets: list[str]) -> ET.Element: out = subprocess.run([mame, "-listxml", *sets], capture_output=True, text=True, check=True) return ET.fromstring(out.stdout) def sha1_members(path: Path) -> dict[str, str]: with zipfile.ZipFile(path) as archive: return { info.filename: hashlib.sha1(archive.read(info)).hexdigest() for info in archive.infolist() if not info.is_dir() } def cpu_profile(machine: ET.Element) -> str: chips = [c for c in machine.findall("chip") if c.get("type") == "cpu"] return " + ".join(f"{c.get('name')} {int(c.get('clock')) / 1e6:.6g} MHz" for c in chips) def board_kind(machine: ET.Element) -> str: devices = {d.get("name") for d in machine.findall("device_ref")} if "williams_blitter_sc2_device" in devices: blitter = "SC2" elif "williams_blitter_sc1_device" in devices: blitter = "SC1" else: blitter = "none" extras = [name for name in ("hc55516", "ym2151", "gfxdecode") if name in devices] return f"blitter {blitter}" + ("".join(f", {e}" for e in extras)) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") parser.add_argument("--hashes", action="store_true", help="print each member's SHA-1") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH (the non-merged MAME romsets)", file=sys.stderr) return 2 rompath = Path(args.rompath) sets = mame_sets(args.mame) root = mame_xml(args.mame, sets) totals = Counter() kinds = Counter() for machine in root.findall("machine"): if machine.get("isdevice") == "yes" or machine.get("name") not in sets: continue name = machine.get("name") wanted = {r.get("sha1"): r.get("name") for r in machine.findall("rom") if r.get("sha1")} nodump = [r.get("name") for r in machine.findall("rom") if r.get("status") == "nodump"] zpath = rompath / f"{name}.zip" if zpath.exists(): have = sha1_members(zpath) found = set(have.values()) matched = sum(1 for sha in wanted if sha in found) state = "complete" if matched == len(wanted) else f"incomplete {matched}/{len(wanted)}" else: have, state = {}, "absent" totals[state.split()[0]] += 1 kind = board_kind(machine) kinds[kind] += 1 nvram = "nvram" in {d.get("name") for d in machine.findall("device_ref")} print( f"{name:12} {machine.get('cloneof') or '-':10} {machine.findtext('year'):5} " f"{state:16} roms={len(wanted):2}{' nodump=' + str(len(nodump)) if nodump else ''} " f"{kind}; {cpu_profile(machine)}{'; nvram' if nvram else ''} | {machine.findtext('description')}" ) if args.hashes: for member, sha in sorted(have.items()): tag = wanted.get(sha, "NOT IN MAME'S LIST") print(f" {sha} {member} ({tag})") print(f"\n{sum(totals.values())} sets from {SOURCE}: " + ", ".join(f"{k} {v}" for k, v in sorted(totals.items()))) print("board kinds: " + "; ".join(f"{k}: {v}" for k, v in sorted(kinds.items()))) return 0 if __name__ == "__main__": sys.exit(main())