#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census the CoCo and Dragon firmware: MAME's expected ROMs against the player's sets. For each machine (and the cartridge devices that carry firmware) this asks the installed MAME for its ROM list (``mame -listxml``: names, sizes, SHA-1s), then opens the set's zip in ``$MAME_ROMPATH`` and hashes each member in place. Nothing is copied or written except the report on standard output. Firmware is identified by SHA-1 only. MAME_ROMPATH=... python -I scripts/census_firmware.py > tests/detectors/census-firmware-.txt """ from __future__ import annotations import hashlib import os import subprocess import sys import xml.etree.ElementTree as ET import zipfile from pathlib import Path # The rows this board proposes (docs/machines.md) and the firmware-bearing devices it needs. MACHINES = [ "coco", "coco2b", "cp400", "mx1600", "lzcolor64", "cd6809", "dragon32", "dragon64", "tanodr64", "dragon200", "dragon200e", "d64plus", "coco3", "coco3p", "coco_fdc", "dragon_fdc", "cd6809_fdc", "deluxecoco", ] def listxml(names: list[str]) -> ET.Element: out = subprocess.run( ["mame", "-listxml", *names], capture_output=True, check=True, text=True ).stdout return ET.fromstring(out) def zip_hashes(path: Path) -> dict[str, tuple[int, str]]: found = {} with zipfile.ZipFile(path) as archive: for info in archive.infolist(): data = archive.read(info) found[info.filename.lower()] = (len(data), hashlib.sha1(data).hexdigest()) return found def main() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("MAME_ROMPATH is not set", file=sys.stderr) return 2 roots = [Path(p) for p in rompath.split(";") if p] version = subprocess.run(["mame", "-version"], capture_output=True, text=True).stdout.strip() print(f"# firmware census, MAME {version}; sets hashed in place from $MAME_ROMPATH") print("# machine | rom | size | sha1 (MAME) | status") tree = listxml(MACHINES) missing = matched = 0 wanted = set(MACHINES) for machine in tree.findall("machine"): name = machine.get("name") if name not in wanted: continue # slot devices -listxml pulls in (printers, other buses) seen = set() roms = [] for rom in machine.findall("rom"): key = (rom.get("name"), rom.get("sha1")) if rom.get("status") != "nodump" and key not in seen: seen.add(key) roms.append(rom) if not roms: continue print(f"\n## {name}: {machine.findtext('description')} " f"(romof={machine.get('romof', '-')}, device={machine.get('isdevice', 'no')})") archive = next((r / f"{name}.zip" for r in roots if (r / f"{name}.zip").exists()), None) have = zip_hashes(archive) if archive else {} by_sha = {sha: member for member, (_, sha) in have.items()} for rom in roms: sha = rom.get("sha1", "") if sha in by_sha: status = f"present ({by_sha[sha]})" matched += 1 elif archive is None: status = "MISSING (no set)" missing += 1 else: status = "MISSING in set" missing += 1 bios = rom.get("bios") label = f"{rom.get('name')} (bios {bios})" if bios else rom.get("name") print(f"{name} | {label} | {rom.get('size')} | {sha} | {status}") print(f"\n# {matched} present, {missing} missing") return 0 if __name__ == "__main__": sys.exit(main())