#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the PC-98 firmware and software the player might bring, read in place. MAME_ROMPATH=... python3 -I scripts/census.py firmware # MAME's PC-98 sets by SHA-1 NO_INTRO_DIR=... REDUMP_DIR=... python3 -I scripts/census.py software ``firmware`` asks the installed MAME (-listxml, a closed box run as a program) which ROMs each PC-98 machine and card expects, then hashes the members of the matching zips under $MAME_ROMPATH and reports, per ROM: name, size, SHA-1, MAME's dump status and whether the mirror's copy matches. Nothing is copied; only names, sizes and hashes are printed. ``software`` counts the No-Intro "NEC - PC-98" and Redump "NEC - PC-98 series" sets by image format. Directory names are given relative to the variables; no machine path is printed or written. """ 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 # The machines this board's first milestones target, then the later rows recorded only. MACHINES = [ "pc9801", "pc9801f", "pc9801m2", "pc9801vm", "pc9801ux", "pc9801vx", "pc9801uv2", "pc9801rs", "pc9801rx", "pc9801dx", "pc9801fs", "pc9801us", "pc9801bx2", "pc9821", ] # pc80s31: the PC-8801's intelligent 2D drive (Z80 + uPD765 + 8255), the kind of drive the # original's 320 KB interface talks to (docs/fdc.md); MAME's pc9801 set has no drive ROM. CARDS = ["pc9801_26", "pc9801_86", "pc98_fdd_2hd", "pc98_fdd_2dd", "pc80s31"] def env_dir(name: str) -> Path: value = os.environ.get(name) if not value: sys.exit(f"{name} is not set (source the family's profile)") return Path(value) def listxml(names: list[str], mame: str) -> ET.Element: result = subprocess.run([mame, "-listxml", *names], capture_output=True, text=True, check=False) return ET.fromstring(result.stdout) def zip_hashes(path: Path) -> dict[str, tuple[int, str]]: found: dict[str, tuple[int, str]] = {} if not path.exists(): return found with zipfile.ZipFile(path) as archive: for info in archive.infolist(): if info.is_dir(): continue found[info.filename] = (info.file_size, hashlib.sha1(archive.read(info)).hexdigest()) return found def firmware(mame: str) -> int: rompath = env_dir("MAME_ROMPATH") root = listxml(MACHINES + CARDS, mame) by_name = {m.get("name"): m for m in root.findall("machine")} missing = 0 print("| set | MAME status | ROM | size | SHA-1 | dump | on the mirror |") print("| --- | --- | --- | --- | --- | --- | --- |") for name in MACHINES + CARDS: machine = by_name.get(name) if machine is None: print(f"| {name} | not in this MAME | | | | | |") continue driver = machine.find("driver") status = driver.get("emulation") if driver is not None else "device" # A ROM shared with the parent lives in the parent's zip on a non-merged set too, # but a non-merged set repeats it; check the set's own zip first. have = zip_hashes(rompath / f"{name}.zip") sha_have = {sha for _, sha in have.values()} for rom in machine.findall("rom"): sha = rom.get("sha1") or "" dump = rom.get("status") or "good" if not sha: state = "undumped" elif sha in sha_have: state = "yes" else: state = "no" missing += 1 print(f"| {name} | {status} | {rom.get('name')} | {rom.get('size')} | `{sha}` | {dump} | {state} |") print(f"\n{missing} dumped ROM(s) not found on the mirror") return 0 def software() -> int: sets = [("NO_INTRO_DIR", "NEC - PC-98"), ("REDUMP_DIR", "NEC - PC-98 series")] for variable, folder in sets: base = env_dir(variable) / folder zips = sorted(base.glob("*.zip")) formats: collections.Counter[str] = collections.Counter() size = 0 for path in zips: size += path.stat().st_size with zipfile.ZipFile(path) as archive: for member in archive.namelist(): formats[member.rsplit(".", 1)[-1].lower()] += 1 print(f"${variable}/{folder}: {len(zips)} zips, {size / 2**30:.2f} GiB, members by type {dict(formats)}") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("what", choices=["firmware", "software"]) parser.add_argument("--mame", default="mame") args = parser.parse_args() return firmware(args.mame) if args.what == "firmware" else software() if __name__ == "__main__": sys.exit(main())