# SPDX-License-Identifier: MIT """Census of the Redump Dreamcast set and of the boot ROMs and flash images we hold, read in place. python -I scripts/census.py redump [--headers] # $REDUMP_DIR/"Sega - Dreamcast" python -I scripts/census.py bios # $MAME_ROMPATH and $DREAMCAST_BIOS_DIR Nothing is copied or written except the report on stdout. `redump` counts the set by region and tag and classifies each disc from its cue sheet: a GD-ROM (Redump writes "REM HIGH-DENSITY AREA" before the high-density tracks), a MIL-CD (a multi-session CD: "REM SESSION 02"), or neither. With --headers it reads the IP.BIN meta information from inside the zip without extracting it: the first sector of the first data track of the high-density area for a GD-ROM, of the last session for a MIL-CD (Comstedt, ip.bin.html: "sector 0-15 of the last data track"). Fields by Comstedt's ip0000.bin.html (tier 3). `bios` hashes the boot ROMs, flash images and VMU firmware inside MAME's romsets and a loose 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 - Dreamcast" (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 0.285 names for its Dreamcast and VMU drivers (`mame -listxml dc dcjp # dceu dcdev dctream svmu`, 2026-11-26), by SHA-1: role, MAME's description, set and file. KNOWN = { "8951d1bb219ab2ff8583033d2119c899cc81f18c": "boot ROM v1.01d (World), mpr-21931.ic501 (dc, dcjp, dceu; dctream)", "ba8bbb90fdb29525f24f17055dc2c7b2d7674437": "boot ROM v1.022 (World), mpr-23588.ic501 (dc, dcjp, dceu)", "1ede8d5be49116a4c6f3fe0961175469537a0434": "boot ROM v1.01c (World), mpr-21871.ic501 (dc, dcjp, dceu)", "1ea132c0fbbf07ef76789eadc07908045c089bd6": "boot ROM v1.004 (Japan), mpr-21068.ic501 (dcjp)", "edc5d3d70a93c935703d26119b37731fd317d2bf": "boot ROM v1.01d (Chinese hack), dc101d_ch.bin (dc, dcjp, dceu): not Sega's", "1efa00ab9d8357a9f91e5be931a3efd6236f2b79": "flash, USA NTSC, dcus_ntsc.bin (dc)", "5fb66adb6d1b54a552fe9c2bb736e4c6960e447d": "flash, Japan NTSC, dcjp_ntsc.bin (dcjp)", "13e444e613dffe0a8bce073a01efa9a1d4626ba7": "flash, Europe PAL, dceu_pal.bin (dceu)", "ca5fd977bbf8f48c28c1027a023b038123d57d39": "flash, Europe PAL (alternate), dceu_pala.bin (dceu)", "78a86fd4e8b58fc9d3535eef6591178f1b97ecf9": "flash, Treamcast, dc_flash.bin (dctream)", "6bd18fb83f8fdb56f1941e079580e5dd672a6dad": "dev box boot ROM Katana Set5 v1.011 (World), set5v1.011.ic507 (dcdev)", "ea7a3ae1de73683008dd795c252941a4fc81b42e": "dev box boot ROM Katana Set5 v1.001 (Japan), set5v1.001.ic507 (dcdev)", "c88b4b6704811e3a428ee225727e4f7df467a3b5": "dev box boot ROM Katana Set5 v0.976 (Japan), set5v0.976.ic507 (dcdev)", "08df891f02cf959189bc9b7c4ac1a4e6a4475b50": "dev box boot ROM Katana Set5 v0.972 (Japan), set5v0.972.ic507 (dcdev)", "28aec4a01419d2d2a664c540bef30ea289ca0644": "dev box Katana Set5 Checker v0.71, set5v0.71.bin (dcdev)", "dc1af1f1248ffa87d57bc5ef2ea41aac95ecfc5e": "dev box Katana Set5 Checker v0.41, set5v0.41.bin (dcdev)", "6242320d705c156f8369969d6caa8c737f01e4f3": "VMU firmware, English 1.005 1999-10-26 (svmu, MAME default)", "4a7bfd1b8eb599d87883312df0bb48e0edd13034": "VMU firmware, English 1.005 1999-04-28 (svmu)", "688b2e1ff8c60bde6e8b07a2d2695cdacc07bd0c": "VMU firmware, Japanese 1.001 1998-05-28 (svmu)", "9ee7c87d7b033235e0b315a0b421e70deb547c7a": "VMU firmware, Japanese 1.002 1998-06-04 (svmu)", "dc2fa2963138a1049a43f7f36439ad0a416ee8b4": "VMU firmware, Japanese 1.004 1998-09-30 (svmu)", "fca1aceff8a2f8c6826f3a865f4d5ef88dfd9ed1": "VMU firmware, Japanese 1.005 1998-12-09 (svmu)", "6cc2fbf4a67770988922117c300d006aa20899ac": "VMU firmware, Japanese 1.005 1999-10-26 (svmu)", "37dea034322b5b80b35b2de784298d32c71ba7a3": "VMU firmware, Japanese development 1.004 1998-09-30 (svmu)", } MAME_SETS = ("dc", "dcjp", "dceu", "dcdev", "dctream", "svmu") SECTOR = 2352 TAGS = ("Disc", "Rev", "Demo", "Beta", "Proto", "Sample", "Unl", "Program", "Kiosk", "Pirate") def parse_cue(text: str) -> list[dict]: """Tracks in order: file name, number, mode, and the area or session it is in.""" tracks, area, session, current_file = [], "", 1, None for raw in text.splitlines(): line = raw.strip() if line.startswith("REM HIGH-DENSITY AREA"): area = "HD" elif line.startswith("REM SINGLE-DENSITY AREA"): area = "SD" elif m := re.match(r"REM SESSION (\d+)", line): session = int(m.group(1)) elif m := re.match(r'FILE "(.*)" BINARY', line): current_file = m.group(1) elif m := re.match(r"TRACK (\d+) (\S+)", line): tracks.append({"file": current_file, "number": int(m.group(1)), "mode": m.group(2), "area": area, "session": session}) # fmt: skip return tracks def classify(tracks: list[dict]) -> str: if any(t["area"] == "HD" for t in tracks): return "GD-ROM" if max((t["session"] for t in tracks), default=1) > 1: return "MIL-CD" return "other CD" def boot_track(tracks: list[dict], kind: str) -> dict | None: data = [t for t in tracks if t["mode"] != "AUDIO"] if kind == "GD-ROM": hd = [t for t in data if t["area"] == "HD"] return hd[0] if hd else None if kind == "MIL-CD": last = max(t["session"] for t in tracks) tail = [t for t in data if t["session"] == last] return tail[0] if tail else None return data[0] if data else None def redump(args: argparse.Namespace) -> int: base = os.environ.get("REDUMP_DIR") if not base: print("set REDUMP_DIR to the directory holding 'Sega - Dreamcast'", file=sys.stderr) return 2 folder = Path(base) / "Sega - Dreamcast" names = sorted(p.name for p in folder.iterdir() if p.suffix == ".zip") regions, tags, kinds, titles = collections.Counter(), collections.Counter(), collections.Counter(), set() tracks_hist = collections.Counter() headers = collections.defaultdict(collections.Counter) odd = [] for name in names: groups = re.findall(r"\(([^)]*)\)", name) regions[groups[0] if groups else "?"] += 1 for group in groups[1:]: for tag in TAGS: if group.startswith(tag): tags[tag] += 1 titles.add(re.sub(r"\s*\(Disc [^)]*\)", "", name)) try: with zipfile.ZipFile(folder / name) as archive: cues = [n for n in archive.namelist() if n.lower().endswith(".cue")] if not cues: odd.append(f"{name}: no cue sheet") continue tracks = parse_cue(archive.read(cues[0]).decode("utf-8", "replace")) kind = classify(tracks) kinds[kind] += 1 tracks_hist[len(tracks)] += 1 if args.headers: read_header(archive, tracks, kind, name, headers, odd) except (zipfile.BadZipFile, OSError, KeyError) as error: odd.append(f"{name}: {error}") 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)) print("disc kinds (from the cue sheets):", dict(kinds)) print("tracks per disc:", dict(sorted(tracks_hist.items()))) for field, counter in headers.items(): print(f"{field}:", dict(counter.most_common(16))) for line in odd: print(" odd:", line) return 0 def read_header(archive: zipfile.ZipFile, tracks: list[dict], kind: str, name: str, headers: dict, odd: list) -> None: # fmt: skip track = boot_track(tracks, kind) if track is None: odd.append(f"{name}: no data track") return members = {Path(n).name: n for n in archive.namelist()} member = members.get(track["file"]) if member is None: odd.append(f"{name}: {track['file']} named by the cue is missing from the zip") return with archive.open(member) as handle: raw = handle.read(SECTOR) # Raw 2352-byte sectors: Mode 1 user data at 16, Mode 2 (XA form 1) at 24. offset = 24 if track["mode"].startswith("MODE2") else 16 ip = raw[offset:offset + 256] hardware = ip[0:16].decode("ascii", "replace") headers["hardware ID"][hardware.strip()] += 1 if hardware != "SEGA SEGAKATANA ": odd.append(f"{name}: hardware ID {hardware!r} in {kind} track {track['number']}") return device = ip[0x20:0x30].decode("ascii", "replace") headers["device field (after the CRC)"][device[4:].strip()] += 1 headers["area symbols"][ip[0x30:0x38].decode("ascii", "replace").rstrip()] += 1 peripherals = ip[0x38:0x40].decode("ascii", "replace").strip() try: bits = int(peripherals[:7], 16) headers["peripherals bit 0 (Windows CE, Comstedt)"][bits & 1] += 1 headers["peripherals bit 4 (VGA box, Comstedt)"][(bits >> 4) & 1] += 1 except ValueError: headers["peripherals unreadable"][peripherals] += 1 headers["boot file name"][ip[0x60:0x70].decode("ascii", "replace").strip()] += 1 headers["boot track kind"][f"{kind} {track['mode']}"] += 1 def bios(_: argparse.Namespace) -> int: seen: dict[str, list[str]] = {} 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("DREAMCAST_BIOS_DIR") if bios_dir: for path in sorted(Path(bios_dir).rglob("*")): if path.is_file() and path.stat().st_size <= 4 << 20 and path.suffix.lower() != ".zip": digest = hashlib.sha1(path.read_bytes()).hexdigest() rel = path.relative_to(bios_dir) seen.setdefault(digest, []).append(f"$DREAMCAST_BIOS_DIR/{rel}") for digest, role in KNOWN.items(): where = ", ".join(seen.get(digest, [])) or "NOT FOUND" print(f"{digest} {role}\n {where}") unknown = {d: w for d, w in seen.items() if d not in KNOWN and any("$DREAMCAST" in x for x in w)} for digest, where in unknown.items(): print(f"{digest} not in MAME's lists\n {', '.join(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 meta information") sub.add_parser("bios") args = parser.parse_args() return redump(args) if args.what == "redump" else bios(args) if __name__ == "__main__": sys.exit(main())