# SPDX-License-Identifier: MIT """Census of the Redump Saturn set and of the Saturn boot ROMs we hold, read in place. python -I scripts/census.py redump [--headers] # $REDUMP_DIR/"Sega - Saturn" python -I scripts/census.py bios # $MAME_ROMPATH and $SATURN_BIOS_DIR Nothing is copied or written except the report on stdout. `redump` counts the set by region and tag; with --headers it also reads each disc's first sector (the IP.BIN system ID, DISC section 4) from inside its zip without extracting it, to count the compatible area symbols and the device field. `bios` hashes the boot and CD block ROMs inside MAME's romsets and a BIOS directory, by SHA-1, and says which known image each is. Where the set lives is an environment variable, never a path in this repository: REDUMP_DIR is the directory holding "Sega - Saturn" (Redump's own folder name). """ from __future__ import annotations import argparse import collections import hashlib import os import re import sys import zipfile from pathlib import Path # The images MAME's Saturn drivers name (MAME 0.285 romsets, hashed 2026-11-25), by SHA-1. # Role, the MAME set(s) and file name that hold it, and the common emulator file name. KNOWN = { "faa8ea183a6d7bbe5d4e03bb1332519800d3fbc3": "boot ROM, Overseas v1.01a 941115 (saturn, saturneu: mpr-17933.bin)", "3bb41feb82838ab9a35601ac666de5aacfd17a58": "boot ROM, Overseas v1.00a 941115 (saturn, saturneu: sega_100a.bin)", "df94c5b4d47eb3cc404d88b33a8fda237eaf4720": "boot ROM, Japan v1.01 941228 (saturnjp, saturnkr [MAME: baddump there]: sega_101.bin)", "2b8cb4f87580683eb4d760e4ed210813d667f0a2": "boot ROM, Japan v1.00 940921 (saturnjp: sega_100.bin)", "7b23b53d62de0f29a23e423d0fe751dfb469c2fa": "boot ROM, Japan v1.003 941012 (saturnjp: sega1003.bin)", "4154e11959f3d5639b11d7902b3a393a99fb5776": "boot ROM, Victor V-Saturn (vsaturn.bin)", "49d8493008fa715ca0c94d99817a5439d6f2c796": "boot ROM, Hitachi Hi-Saturn v1.01 950130 (hisaturn.bin)", "8a22710e09ce75f39625894366cafe503ed1942d": "boot ROM, Hitachi Hi-Saturn v1.02 950519 (mpr-18100.bin)", "eb8393058f324e922c11b43709b64fc6ca94ab86": "CD block SH-1 ROM, Saturn CD Block 1.05 (all Saturn sets: cdb105.bin)", "b3c20fbe57cd2eb595e9edac86817e5948dccae4": "CD block SH-1 ROM, Saturn CD Block 1.06, MAME default (all Saturn sets: cdb106.bin)", "f4f6c2aac68c352814d396ae41f81f54ad228e68": "CD block SH-1 ROM, YGR022 315-5962 (all Saturn sets: ygr022.bin)", "a67cd4f550751f8b91de2b8b74528ab4e0c11c77": "ROM cartridge: The King of Fighters '95 (mpr-18811-mx.ic1)", "56c1b93da6b660bf393fbf48ca47569000ef4047": "ROM cartridge: Ultraman - Hikari no Kyojin Densetsu (mpr-19367-mx.ic1)", } MAME_SETS = ("saturn", "saturnjp", "saturneu", "saturnkr", "vsaturn", "hisaturn", "stvbios") SECTOR = 2352 def redump(args: argparse.Namespace) -> int: base = os.environ.get("REDUMP_DIR") if not base: print("set REDUMP_DIR to the directory holding 'Sega - Saturn'", file=sys.stderr) return 2 folder = Path(base) / "Sega - Saturn" names = sorted(p.name for p in folder.iterdir() if p.suffix == ".zip") regions = collections.Counter() tags = collections.Counter() titles = set() for name in names: groups = re.findall(r"\(([^)]*)\)", name) regions[groups[0] if groups else "?"] += 1 for group in groups[1:]: for tag in ("Disc", "Rev", "Demo", "Beta", "Proto", "Sample", "Unl", "Program"): if group.startswith(tag): tags[tag] += 1 titles.add(re.sub(r"\s*\(Disc [^)]*\)", "", name)) print(f"{len(names)} zips, {len(titles)} titles once multi-disc sets are merged") for region, count in regions.most_common(): print(f" {count:5} {region}") print("tags:", dict(tags)) if args.headers: headers(folder, names) return 0 def first_sector(archive: zipfile.ZipFile) -> tuple[bytes, str]: """The first 2352 bytes of the first data track, and the member's name.""" bins = sorted(n for n in archive.namelist() if n.lower().endswith(".bin")) track1 = next((n for n in bins if re.search(r"\(Track 0?1\)", n)), bins[0] if bins else None) if track1 is None: return b"", "" with archive.open(track1) as member: return member.read(SECTOR), track1 def headers(folder: Path, names: list[str]) -> None: areas = collections.Counter() symbols = collections.Counter() devices = collections.Counter() odd = [] for name in names: try: with zipfile.ZipFile(folder / name) as archive: raw, _ = first_sector(archive) except (zipfile.BadZipFile, OSError) as error: odd.append(f"{name}: {error}") continue # A raw Mode 1 sector: 12 sync bytes, 4 header bytes, then the user data. ip = raw[16:16 + 256] if ip[:16] != b"SEGA SEGASATURN ": odd.append(f"{name}: no system ID in the first sector") continue area = ip[0x40:0x4A].decode("ascii", "replace").strip() areas[area] += 1 for symbol in area: symbols[symbol] += 1 devices[ip[0x38:0x40].decode("ascii", "replace").strip()] += 1 print(f"system IDs read: {sum(areas.values())}; unreadable or not Saturn: {len(odd)}") print("area symbol sets:", dict(areas.most_common())) print("discs carrying each area symbol:", dict(symbols.most_common())) print("device information field:", dict(devices.most_common(10))) for line in odd: print(" odd:", line) def bios(_: argparse.Namespace) -> int: seen = {} rompath = os.environ.get("MAME_ROMPATH") if rompath: for name in MAME_SETS: path = Path(rompath) / f"{name}.zip" if not path.exists(): print(f"missing MAME set {name}.zip") continue with zipfile.ZipFile(path) as archive: for member in archive.namelist(): digest = hashlib.sha1(archive.read(member)).hexdigest() seen.setdefault(digest, []).append(f"{name}.zip:{member}") bios_dir = os.environ.get("SATURN_BIOS_DIR") if bios_dir: for path in sorted(Path(bios_dir).iterdir()): if path.is_file() and path.stat().st_size <= 4 << 20: digest = hashlib.sha1(path.read_bytes()).hexdigest() seen.setdefault(digest, []).append(f"$SATURN_BIOS_DIR/{path.name}") for digest, role in KNOWN.items(): where = ", ".join(seen.get(digest, [])) or "NOT FOUND" print(f"{digest} {role}\n {where}") return 0 def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) sub = parser.add_subparsers(dest="what", required=True) one = sub.add_parser("redump") one.add_argument("--headers", action="store_true", help="read each disc's IP.BIN system ID") sub.add_parser("bios") args = parser.parse_args() return redump(args) if args.what == "redump" else bios(args) if __name__ == "__main__": sys.exit(main())