# SPDX-License-Identifier: MIT """Census of MAME's System 16 family romsets, read in place (docs/census.md). MAME_ROMPATH=... python -I scripts/census.py # table + summary MAME_ROMPATH=... python -I scripts/census.py --board 16b MAME 0.285 is run only as a list tool (`-listsource`, `-listxml`); its source is never read. For every machine driven by the System 16 family's driver files the script records parent, year, board, main CPU (plain 68000 or an FD1089A/FD1089B/FD1094 with its key ROM), the 8751 MCU and its ROM, the sound CPU (Z80 or the MC-8123 encrypted Z80), the sample chip, and whether `$MAME_ROMPATH/.zip` holds every dumped ROM (by CRC-32 from the zip directory: nothing is decompressed, copied or kept). Output: a table on stdout and reference/census/machines.tsv (git-ignored). Names and hashes are MAME's data (its software-description XML); no ROM content is written anywhere. """ from __future__ import annotations import argparse import collections import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] BOARDS = { "sega/segas16a.cpp": "16a", # also the Pre-System 16 sets (CMP16) "sega/segas16b.cpp": "16b", "sega/segas16b_isgsm.cpp": "16b-isgsm", "sega/segas18.cpp": "18", "sega/segas18_astormbl.cpp": "18-bootleg", "sega/segaorun.cpp": "outrun", "sega/system16.cpp": "bootleg", } MAIN_CPUS = { "Motorola MC68000": "68000", "Hitachi FD1089A Encrypted CPU": "FD1089A", "Hitachi FD1089B Encrypted CPU": "FD1089B", "Hitachi FD1094 Encrypted CPU": "FD1094", } def machines(mame: str) -> list[ET.Element]: listing = subprocess.run(["nice", mame, "-listsource"], capture_output=True, text=True, check=True).stdout # fmt: skip names = [line.split()[0] for line in listing.splitlines() if len(line.split()) == 2 and line.split()[1] in BOARDS] # fmt: skip xml = subprocess.run(["nice", mame, "-listxml", *names], capture_output=True, check=True).stdout return [m for m in ET.fromstring(xml) if m.get("isdevice") != "yes" and m.get("name") in names] def row(machine: ET.Element, rompath: Path | None) -> dict: chips = machine.findall("chip") roms = machine.findall("rom") cpus = {c.get("tag"): c.get("name") for c in chips if c.get("type") == "cpu"} audio = sorted({c.get("name") for c in chips if c.get("type") == "audio"} - {"Speaker"}) main = MAIN_CPUS.get(cpus.get("maincpu", ""), cpus.get("maincpu", "-")) def rom_in(region: str) -> str: found = [r for r in roms if r.get("region") == region] if not found: return "-" return ",".join(f"{r.get('name')}({r.get('status', 'good')})" for r in found) sound = {"Zilog Z80": "Z80", "MC-8123": "MC-8123"}.get(cpus.get("soundcpu", ""), cpus.get("soundcpu", "-")) mcu = cpus.get("mcu", "-") dumped = [r for r in roms if r.get("crc") and r.get("status") != "nodump"] have = "no zip" if rompath is not None: path = rompath / f"{machine.get('name')}.zip" if path.exists(): with zipfile.ZipFile(path) as archive: crcs = {f"{info.CRC:08x}" for info in archive.infolist()} matched = sum(r.get("crc") in crcs for r in dumped) have = "complete" if matched == len(dumped) else f"{matched}/{len(dumped)}" return { "name": machine.get("name"), "parent": machine.get("cloneof") or "-", "year": machine.findtext("year") or "?", "board": BOARDS[machine.get("sourcefile")], "main": main, "main_key": rom_in("maincpu:key"), "mcu": mcu, "mcu_rom": rom_in("mcu"), "sound": sound, "sound_key": rom_in("soundcpu:key"), "sample": "uPD7751" if "upd7751" in cpus else ("uPD7759" if "NEC uPD7759" in audio else "-"), "audio": "+".join(audio), "zip": have, "description": machine.findtext("description") or "", } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--mame", default="mame") parser.add_argument("--board", help="only this board (16a, 16b, 18, outrun, bootleg, ...)") args = parser.parse_args() env = os.environ.get("MAME_ROMPATH") rompath = Path(env) if env else None if rompath is None: print("MAME_ROMPATH is not set: listing without the zip check", file=sys.stderr) rows = [row(m, rompath) for m in machines(args.mame)] if args.board: rows = [r for r in rows if r["board"] == args.board] out = ROOT / "reference" / "census" / "machines.tsv" out.parent.mkdir(parents=True, exist_ok=True) keys = list(rows[0]) lines = ["\t".join(keys)] + ["\t".join(r[k] for k in keys) for r in rows] out.write_text("\n".join(lines) + "\n", encoding="utf-8") def tally(label: str, field: str) -> None: counts = collections.Counter((r["board"], r[field]) for r in rows) print(f"\n{label}:") for (board, value), count in sorted(counts.items()): print(f" {board:11} {value:28} {count:4}") print(f"{len(rows)} machines, {sum(r['parent'] == '-' for r in rows)} parents; " f"table in {out.relative_to(ROOT)}") # fmt: skip tally("Main CPU", "main") tally("MCU", "mcu") tally("Sound CPU", "sound") tally("Sample chip", "sample") tally("Zip in MAME_ROMPATH", "zip") return 0 if __name__ == "__main__": sys.exit(main())