# SPDX-License-Identifier: MIT """Census a MAME driver family's romsets in $MAME_ROMPATH, read in place. MAME is a closed box: its machine descriptions come from `mame -listxml` (run here), never from its source. Each set's zip is opened read-only; every member is checked against the description by CRC32 and by SHA-1 (computed here). Nothing is copied. MAME_ROMPATH=... python -I scripts/census.py namco/namcos1.cpp namco/namcos1b.cpp MAME_ROMPATH=... python -I scripts/census.py --device namcoc65 --device namcoc68 Prints one line per set (present, complete, bad or missing members) and, with --firmware, the SHA-1 of every MCU or device ROM (the firmware the player supplies). """ import argparse import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile import zlib MCU_REGIONS = ("mcu", "c65mcu:mcu", "c68mcu:mcu", "c65mcu:external", "c68mcu:external") def sets_for_sources(sources): out = subprocess.run(["mame", "-listsource"], capture_output=True, text=True, check=True).stdout names = [] for line in out.splitlines(): parts = line.split() if len(parts) == 2 and parts[1] in sources: names.append(parts[0]) return names def listxml(names): out = subprocess.run(["mame", "-listxml", *names], capture_output=True, check=True).stdout return ET.fromstring(out) def rompaths(): raw = os.environ.get("MAME_ROMPATH") if not raw: sys.exit("set MAME_ROMPATH (a non-merged MAME romset directory; ';' separates several)") return [p for p in raw.split(";") if p] def find_zip(name, paths): for p in paths: z = os.path.join(p, name + ".zip") if os.path.exists(z): return z return None def members(zpath): found = {} with zipfile.ZipFile(zpath) as z: for info in z.infolist(): data = z.read(info) found[f"{zlib.crc32(data):08x}"] = (info.filename, hashlib.sha1(data).hexdigest(), len(data)) return found def main(): ap = argparse.ArgumentParser() ap.add_argument("sources", nargs="*", help="MAME source files, e.g. namco/namcos1.cpp") ap.add_argument("--device", action="append", default=[], help="a device set holding firmware (e.g. namcoc65)") ap.add_argument("--firmware", action="store_true", help="list MCU/device ROM SHA-1s") a = ap.parse_args() names = sets_for_sources(set(a.sources)) + a.device root = listxml(names) paths = rompaths() totals = {"sets": 0, "present": 0, "complete": 0} firmware = {} for m in root.iter("machine"): name = m.get("name") if name not in names: continue totals["sets"] += 1 roms = [r for r in m.findall("rom") if r.get("status") != "nodump"] zpath = find_zip(name, paths) if zpath is None: print(f"{name:12} MISSING") continue totals["present"] += 1 have = members(zpath) bad = [] for r in roms: crc = r.get("crc") hit = have.get(crc) if hit is None or hit[1] != r.get("sha1"): bad.append(r.get("name")) elif r.get("region") in MCU_REGIONS or m.get("isdevice") == "yes": firmware.setdefault((r.get("sha1"), r.get("size")), set()).add( f"{name}:{r.get('region')}:{r.get('name')}") if not bad: totals["complete"] += 1 state = "complete" if not bad else "INCOMPLETE " + ",".join(bad) print(f"{name:12} {len(roms):3} roms {state}") print("totals", totals) if a.firmware: for (sha1, size), users in sorted(firmware.items(), key=lambda kv: sorted(kv[1])[0]): print(f"firmware sha1={sha1} size={size} used by {len(users)}: " + " ".join(sorted(users))) if __name__ == "__main__": main()