#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the PC-FX firmware and the Redump PC-FX set, read in place (nothing copied). python -I scripts/census.py bios # $MAME_ROMPATH (pcfx.zip, pcfxga.zip) and $PCFX_BIOS_DIR python -I scripts/census.py redump # $REDUMP_DIR/"NEC - PC-FX & PC-FXGA" bios: SHA-1 of every member of MAME's pcfx/pcfxga sets and of every file in $PCFX_BIOS_DIR (a loose folder, ':'-separated for several; files whose names hold "fx"), named against MAME 0.285's rows (KNOWN below, from `mame -listxml pcfx pcfxga`, 2026-11-26). redump: one line per zip (--rows) or a summary: tracks and their modes from the cue sheet, and the first data track whose first sector after the pregap carries the PC-FX boot signature "PC-FX:Hu_CD-ROM" at user-data offset 0, searched track by track (Redump's data track files start with INDEX 00, a 150- or 225-sector pregap, so the first 300 sectors are streamed out of the zip and searched; nothing is written). """ from __future__ import annotations import argparse import collections import hashlib import os import re import sys import zipfile from pathlib import Path KNOWN = { "1a77fd83e337f906aecab27a1604db064cf10074": "PC-FX BIOS v1.00 (2 Sep 1994), MAME pcfx:pcfxbios.bin", "8b662f7548078be52a871565e19511ccca28c5c8": "PC-FX BIOS v1.01 (5 Dec 1994), MAME pcfx:pcfxv101.bin", "65482a23ac5c10a6095aee1db5824cca54ead6e5": "FX-SCSI ROM, MAME pcfx:fx-scsi.rom (512 KiB)", "a9372202a5db302064c994fcda9b24d29bb1b41c": "PC-FXGA BIOS, MAME pcfxga:pcfxga.rom", } SET = "NEC - PC-FX & PC-FXGA" SIGNATURE = b"PC-FX:Hu_CD-ROM" def sha1(data: bytes) -> str: return hashlib.sha1(data).hexdigest() def bios() -> int: seen: dict[str, list[str]] = collections.defaultdict(list) for root in filter(None, os.environ.get("MAME_ROMPATH", "").split(";")): for name in ("pcfx.zip", "pcfxga.zip"): path = Path(root) / name if path.exists(): with zipfile.ZipFile(path) as archive: for member in archive.infolist(): seen[sha1(archive.read(member))].append(f"$MAME_ROMPATH/{name}:{member.filename}") for root in filter(None, os.environ.get("PCFX_BIOS_DIR", "").split(":")): for path in sorted(Path(root).iterdir()): # Loose BIOS packs hold every system's files: look only at names with "fx" in them. if path.is_file() and "fx" in path.name.lower() and path.stat().st_size <= 4 << 20: seen[sha1(path.read_bytes())].append(f"$PCFX_BIOS_DIR/{path.name} ({path.stat().st_size} bytes)") for digest, where in sorted(seen.items(), key=lambda item: KNOWN.get(item[0], "~")): print(f"{digest} {KNOWN.get(digest, 'not a MAME row')}") for place in where: print(f" {place}") missing = [name for digest, name in KNOWN.items() if digest not in seen] print(f"{len(KNOWN) - len(missing)} of {len(KNOWN)} MAME images found" + (f"; missing: {missing}" if missing else "")) return 0 def cue_tracks(text: str) -> list[tuple[str, str, str]]: tracks, current = [], None for line in text.splitlines(): words = line.split() if not words: continue if words[0] == "FILE": current = line.split('"')[1] if '"' in line else words[1] elif words[0] == "TRACK": tracks.append((current or "", words[1], words[2])) return tracks def redump(rows: bool) -> int: base = os.environ.get("REDUMP_DIR") if not base: print("set REDUMP_DIR", file=sys.stderr) return 2 zips = sorted((Path(base) / SET).glob("*.zip")) modes: collections.Counter[str] = collections.Counter() counts: collections.Counter[int] = collections.Counter() tags: collections.Counter[str] = collections.Counter() regions: collections.Counter[str] = collections.Counter() signed = unsigned = incomplete = 0 first_signed: collections.Counter[bool] = collections.Counter() titles = set() for path in zips: titles.add(re.sub(r" \((Disc|Track) [^)]*\)", "", path.stem)) first = re.search(r"\(([^)]*)\)", path.stem) regions[first.group(1) if first else "?"] += 1 for tag in ("Disc", "Rev", "Demo", "Sample", "Proto", "PC-FXGA", "DOS-V", "Beta"): if tag in path.stem: tags[tag] += 1 with zipfile.ZipFile(path) as archive: names = {m.filename for m in archive.infolist()} cue = next((n for n in names if n.lower().endswith(".cue")), None) tracks = cue_tracks(archive.read(cue).decode("latin-1")) if cue else [] counts[len(tracks)] += 1 data = [t for t in tracks if t[2].startswith("MODE")] for track in tracks: modes[track[2]] += 1 missing = [t[0] for t in tracks if t[0] not in names] mark = "?" if missing: incomplete += 1 mark = "INCOMPLETE" elif data: found = None for track in data: with archive.open(track[0]) as handle: head = handle.read(300 * 2352) if SIGNATURE in head: found = track[1] break if found is None: unsigned += 1 mark = "no signature" else: signed += 1 first_signed[found == data[0][1]] += 1 mark = f"signature in {found}" if rows: print(f" {len(tracks):3d} tracks {','.join(sorted({t[2] for t in tracks}))[:24]:24} {mark:16} {path.stem}") print(f"{len(zips)} zips, {len(titles)} titles once '(Disc n)' is merged") print("first parenthesis: " + ", ".join(f"{r} x{n}" for r, n in regions.most_common())) print("tags: " + ", ".join(f"{t} x{n}" for t, n in tags.most_common())) print("tracks per disc: " + ", ".join(f"{k} x{n}" for k, n in sorted(counts.items()))) print("track modes: " + ", ".join(f"{m} x{n}" for m, n in modes.most_common())) print(f"discs with the PC-FX signature in the first 300 sectors of a data track: {signed} " f"(in the first data track: {first_signed[True]}, a later one: {first_signed[False]}); " f"without: {unsigned}; incomplete zips: {incomplete}") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("what", choices=["bios", "redump"]) parser.add_argument("--rows", action="store_true") args = parser.parse_args() return bios() if args.what == "bios" else redump(args.rows) if __name__ == "__main__": sys.exit(main())