# SPDX-License-Identifier: MIT """Census of MAME's Sega System 24 and System 32 romsets, read in place (docs/census.md). MAME_ROMPATH=... python -I scripts/census.py # both boards MAME_ROMPATH=... python -I scripts/census.py --board s24 MAME 0.285 is run only as a list tool (`-listsource`, `-listxml`); its source is never read. For every machine MAME drives from `sega/segas24.cpp` or `sega/segas32.cpp` the script records parent, year, the CPUs (plain 68000 or FD1094 with its key ROM on System 24; V60 or V70, the V25 protection MCU and the uPD7725 DSP on System 32), whether the game comes on a floppy (with the image's size and bytes per track if it splits into 160 tracks) or on a ROM board, the sound chips, the firmware ROMs shared by many sets (named with SHA-1, never copied), 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. """ 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/segas24.cpp": "s24", "sega/segas32.cpp": "s32"} SHORT = { "Motorola MC68000": "68000", "Hitachi FD1094 Encrypted CPU": "FD1094", "NEC V60": "V60", "NEC V70": "V70", "NEC V25": "V25", "NEC uPD7725": "uPD7725", "Zilog Z80": "Z80", } 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").split(":")[-1], SHORT.get(c.get("name"), c.get("name")), c.get("clock")) for c in chips if c.get("type") == "cpu"] # fmt: skip pcbs = {c.get("tag").split(":")[0] for c in chips if ":" in c.get("tag", "")[1:]} audio = sorted({c.get("name") for c in chips if c.get("type") == "audio"} - {"Speaker"}) floppy = [r for r in roms if r.get("region") == "floppy"] keys = [r for r in roms if r.get("region", "").endswith(":key")] size = sum(int(r.get("size", "0")) for r in floppy) media = "rom board" if floppy: media = f"floppy {size}" + (f" ({size // 160}/track)" if size % 160 == 0 else "") 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)}" first = {} # dual-PCB machines repeat each tag once per board: list each CPU once for tag, kind, clock in cpus: first.setdefault(tag, f"{tag}={kind}@{int(clock) / 1e6:g}") return { "name": machine.get("name"), "parent": machine.get("cloneof") or "-", "year": machine.findtext("year") or "?", "board": BOARDS[machine.get("sourcefile")] + ("-dual" if len(pcbs) > 1 else ""), "cpus": " ".join(first.values()), "key": ",".join(r.get("name") for r in keys) or "-", "media": media, "main_roms": ",".join(f"{r.get('name')}:{r.get('sha1', '?')[:12]}" for r in roms if r.get("region", "").endswith("maincpu")), # fmt: skip "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", choices=["s24", "s32"], help="only this board") 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"].startswith(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, cut: int = 60) -> None: counts = collections.Counter((r["board"], r[field][:cut]) for r in rows) print(f"\n{label}:") for (board, value), count in sorted(counts.items()): print(f" {board:7} {value:60} {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("CPUs", "cpus", 90) tally("Media", "media") tally("Sound", "audio") tally("Zip in MAME_ROMPATH", "zip") return 0 if __name__ == "__main__": sys.exit(main())