#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census the firmware the PC machines need: which ROMs exist under MAME_ROMPATH, by SHA-1. For each MAME machine or device below, `mame -listxml` gives the ROMs it wants (name, size, CRC, SHA-1, BIOS set). The script opens `/.zip` (each directory of MAME_ROMPATH in turn, `;`-separated as MAME takes it), hashes every member in memory and reports which wanted ROMs are present. Nothing is copied or written except the report on stdout; the firmware stays where it is. MAME_ROMPATH=... python3 -I scripts/census_bios.py """ 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 5150 and 5160 with their default cards and keyboards, the PCjr and Tandy rows, and the # cards and keyboards a later row would add. SETS = [ "ibm5150", "ibm5160", "ibmpcjr", "t1000", "t1000hx", "t1000sx", "cga", "isa_ibm_mda", "ega", "isa_hdc", "kb_pc83", "kb_pcxt83", "keytronic_pc3270", ] def wanted(name: str) -> list[dict[str, str]]: xml = subprocess.run(["mame", "-listxml", name], capture_output=True, text=True, check=True).stdout root = ET.fromstring(xml) machine = next(m for m in root.iter("machine") if m.get("name") == name) biosnames = {b.get("name"): b.get("description", "") for b in machine.iter("biosset")} roms = [] for rom in machine.findall("rom"): roms.append({ "name": rom.get("name", ""), "size": rom.get("size", ""), "sha1": rom.get("sha1", ""), "status": rom.get("status", "good"), "bios": rom.get("bios", ""), "bios_desc": biosnames.get(rom.get("bios", ""), ""), "region": rom.get("region", ""), }) return roms def present(name: str, rompaths: list[Path]) -> tuple[Path | None, set[str]]: for base in rompaths: archive = base / f"{name}.zip" if archive.exists(): hashes = set() with zipfile.ZipFile(archive) as zf: for info in zf.infolist(): hashes.add(hashlib.sha1(zf.read(info)).hexdigest()) return archive, hashes return None, set() def main() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("MAME_ROMPATH is not set", file=sys.stderr) return 2 rompaths = [Path(p) for p in rompath.split(";") if p] total = found = 0 print("| Set | ROM | BIOS set | Size | SHA-1 (MAME 0.285) | Status | Present |") print("| --- | --- | --- | ---: | --- | --- | --- |") for name in SETS: archive, hashes = present(name, rompaths) for rom in wanted(name): total += 1 if rom["status"] == "nodump" or not rom["sha1"]: state = "no dump known (MAME)" elif rom["sha1"] in hashes: state = "yes" found += 1 else: state = "no" if archive else "no (set missing)" bios = f"{rom['bios']} ({rom['bios_desc']})" if rom["bios"] else "" print(f"| {name} | {rom['name']} | {bios} | {rom['size']} | `{rom['sha1'] or '-'}` | {rom['status']} | {state} |") print(f"\n{found} of {total} ROM entries present by SHA-1 (entries repeat across BIOS sets).") return 0 if __name__ == "__main__": sys.exit(main())