# SPDX-License-Identifier: MIT """Census of MAME's Irem M72/M81/M82/M84/M85, M90, M92, M107 (and M62) sets. MAME_ROMPATH=... python -I scripts/census.py # markdown to stdout MAME_ROMPATH=... python -I scripts/census.py --sha1 # also hash every ROM The machine list comes from the installed MAME binary (`-listsource`, `-listxml`), never from its source. Each set's zip is looked up in MAME_ROMPATH (non-merged sets, read in place, nothing copied); a ROM counts as present when a zip member has its CRC, and with --sha1 its SHA-1 is checked too. Nothing is written into the repository except what the caller redirects; the XML is cached under reference/mame/ (git-ignored). """ from __future__ import annotations import argparse import collections import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CACHE = ROOT / "reference" / "mame" SOURCES = ("irem/m72.cpp", "irem/m90.cpp", "irem/m92.cpp", "irem/m107.cpp", "irem/m62.cpp") def listxml(mame: str) -> ET.Element: CACHE.mkdir(parents=True, exist_ok=True) cached = CACHE / "listxml.xml" if not cached.exists(): listing = subprocess.run([mame, "-listsource"], capture_output=True, text=True, check=True).stdout # fmt: skip sets = [line.split()[0] for line in listing.splitlines() if len(line.split()) == 2 and line.split()[1] in SOURCES] # fmt: skip xml = subprocess.run([mame, "-listxml", *sets], capture_output=True, text=True, check=True).stdout # fmt: skip cached.write_text(xml, encoding="utf-8") return ET.parse(cached).getroot() def board(machine: ET.Element) -> str: """The board family by source file and main CPU (MAME groups M72-M85 in m72.cpp).""" source = machine.get("sourcefile", "").split("/")[-1].removesuffix(".cpp") cpus = {c.get("tag"): c.get("name") for c in machine.findall("chip") if c.get("type") == "cpu"} main = cpus.get("maincpu", "") if source == "m72" and main == "NEC V35": return "m72-v35" return source def chips(machine: ET.Element) -> dict[str, str]: out = {} for c in machine.findall("chip"): clock = c.get("clock") mhz = f" @ {int(clock) / 1e6:.3f}" if clock else "" out[c.get("tag")] = f"{c.get('name')}{mhz}" return out def zip_index(path: Path, want_sha1: bool) -> dict[int, str | None]: """CRC -> SHA-1 (or None) of every member of one zip.""" index: dict[int, str | None] = {} with zipfile.ZipFile(path) as archive: for info in archive.infolist(): digest = None if want_sha1: digest = hashlib.sha1(archive.read(info)).hexdigest() index[info.CRC] = digest return index def check_set(machine: ET.Element, rompath: Path, want_sha1: bool) -> tuple[str, list[str]]: roms = [r for r in machine.findall("rom") if r.get("crc") and r.get("status") != "nodump"] path = rompath / f"{machine.get('name')}.zip" if not path.exists(): return "absent", [r.get("name") for r in roms] try: index = zip_index(path, want_sha1) except zipfile.BadZipFile: return "bad zip", [] missing = [] for rom in roms: crc = int(rom.get("crc"), 16) if crc not in index: missing.append(rom.get("name")) elif want_sha1 and index[crc] != rom.get("sha1"): missing.append(rom.get("name") + " (SHA-1 differs)") return ("complete" if not missing else "incomplete"), missing def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--sha1", action="store_true", help="hash every ROM (slower)") parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH (non-merged MAME sets)", file=sys.stderr) return 2 root = listxml(args.mame) rompath = Path(args.rompath) machines = [m for m in root.findall("machine") if m.get("isdevice") != "yes"] rows, tally = [], collections.Counter() for m in machines: state, missing = check_set(m, rompath, args.sha1) tally[(board(m), state)] += 1 c = chips(m) mcu = c.get("mcu", "") if mcu: dumped = [r for r in m.findall("rom") if r.get("region") == "mcu"] mcu += " (dumped)" if dumped and all(r.get("status") != "nodump" for r in dumped) \ else " (NOT dumped: MAME simulates it)" # fmt: skip driver = m.find("driver") rows.append([ board(m), m.get("name"), m.get("cloneof") or "", m.findtext("year", ""), m.findtext("description", ""), c.get("maincpu", ""), c.get("soundcpu", "") or c.get("irem_audio:iremsound", ""), mcu, ", ".join(sorted({v.split(" @")[0] for k, v in c.items() if k in ("ymsnd", "irem", "dac", "oki")})), driver.get("status") if driver is not None else "", state + (f" (missing {len(missing)})" if missing else ""), ]) # fmt: skip rows.sort(key=lambda r: (r[0], r[2] or r[1], r[2] != "", r[1])) head = ["board", "set", "parent", "year", "title", "main CPU", "sound CPU", "MCU", "sound chips", "MAME status", "in MAME_ROMPATH"] # fmt: skip print("| " + " | ".join(head) + " |") print("|" + " --- |" * len(head)) for row in rows: print("| " + " | ".join(cell.replace("|", "/") for cell in row) + " |") print() print("| board | sets | complete | incomplete | absent |") print("| --- | --- | --- | --- | --- |") for name in sorted({b for b, _ in tally}): n = {s: tally[(name, s)] for s in ("complete", "incomplete", "absent")} print(f"| {name} | {sum(tally[(name, s)] for b, s in tally if b == name)} | " f"{n['complete']} | {n['incomplete']} | {n['absent']} |") # fmt: skip print(f"\n{len(machines)} sets; SHA-1 checked: {'yes' if args.sha1 else 'no (CRC only)'}") return 0 if __name__ == "__main__": sys.exit(main())