#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Census of the No-Intro Mega Duck set: sizes, SHA-1s, bank-register writes, MAME's list. Reads every zip under ``$NO_INTRO_DIR/Welback - Mega Duck`` in place (members are read, never extracted or copied) and prints one line per ROM: size, SHA-1, the first bytes at $0000 (the Mega Duck's entry point; there is no boot ROM and no header), whether a Game Boy header logo is present at $0104, and a static count of the three bank-register store forms ``LD ($nnnn),A`` (``EA lo hi``) for $0001 (MD2), $B000 (MD1) and $1000 (MD0). The count is a hint, not a decision: code may write the registers other ways (``LD (HL),A``). It then hashes the firmware MAME 0.285 names for ``mduckspa`` (the Spanish laptop) under ``$MAME_ROMPATH`` (SHA-1 only; nothing is copied), and cross-checks every ROM against MAME's CC0 software list (reference/mame-hash/megaduck.xml). NO_INTRO_DIR=... MAME_ROMPATH=... python -I scripts/census.py > tests/detectors/census-.txt """ from __future__ import annotations import hashlib import os import sys import xml.etree.ElementTree as ET import zipfile from collections import Counter from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SYSTEM = "Welback - Mega Duck" MAME_LIST = ROOT / "reference" / "mame-hash" / "megaduck.xml" #: The Game Boy header logo's first bytes ($0104); a Mega Duck image should not carry it. GB_LOGO = bytes.fromhex("CEED6666CC0D000B") STORES = {"$0001 (MD2)": b"\xea\x01\x00", "$B000 (MD1)": b"\xea\x00\xb0", "$1000 (MD0)": b"\xea\x00\x10"} FIRMWARE = {"mduckspa.zip": ["megaducksp.bin"]} def mame_list() -> dict[str, tuple[str, int, str, str]]: """SHA-1 -> (software name, size, fixedbank feature, supported) from MAME's CC0 list.""" found: dict[str, tuple[str, int, str, str]] = {} if not MAME_LIST.exists(): return found for software in ET.parse(MAME_LIST).getroot().findall("software"): fixed = "-" for feature in software.iter("feature"): if feature.get("name") == "fixedbank": fixed = "fixedbank" for rom in software.iter("rom"): if rom.get("sha1"): found[rom.get("sha1")] = ( software.get("name"), int(rom.get("size"), 0), fixed, software.get("supported", "yes")) return found def main() -> int: root = os.environ.get("NO_INTRO_DIR") if not root: print("set NO_INTRO_DIR", file=sys.stderr) return 2 folder = Path(root) / SYSTEM mame = mame_list() sizes: Counter[int] = Counter() kinds: Counter[str] = Counter() matched: set[str] = set() rows = 0 zips = sorted(folder.glob("*.zip")) for path in zips: with zipfile.ZipFile(path) as archive: for member in archive.infolist(): data = archive.read(member) sha1 = hashlib.sha1(data).hexdigest() rows += 1 sizes[len(data)] += 1 stores = {name: data.count(pattern) for name, pattern in STORES.items()} written = [f"{name} x{n}" for name, n in stores.items() if n] kind = "flat" if len(data) <= 0x8000 else ( "MD2?" if stores["$0001 (MD2)"] else "MD1?" if stores["$B000 (MD1)"] else "MD0?" if stores["$1000 (MD0)"] else "banked, register not found") kinds[kind] += 1 logo = "GB logo" if data[0x104:0x10C] == GB_LOGO else "no GB logo" listed = mame.get(sha1) if listed: matched.add(listed[0]) note = f"MAME {listed[0]} ({listed[1]} B, {listed[2]}, supported={listed[3]})" if listed else "not in MAME's list by SHA-1" print(f"{len(data):7d} {sha1} @0000 {data[:4].hex()} {logo} | {kind}: {', '.join(written) or 'no stores'} | {note} | {member.filename}") print() print(f"ROMs: {rows} in {len(zips)} zips; SHA-1 also in MAME 0.285's list: {len(matched)} of {len({v[0] for v in mame.values()})} entries") print("sizes: " + ", ".join(f"{size // 1024} KB x{count}" for size, count in sorted(sizes.items()))) print("kinds (static guess): " + ", ".join(f"{k} x{n}" for k, n in sorted(kinds.items()))) unmatched = sorted({v[0] for v in mame.values()} - matched) print("MAME entries with no No-Intro match by SHA-1: " + (", ".join(unmatched) or "none")) print() rompath = os.environ.get("MAME_ROMPATH") print("Firmware (SHA-1 only):") for archive_name, members in FIRMWARE.items(): hits = [Path(p) / archive_name for p in (rompath or "").split(os.pathsep) if p and (Path(p) / archive_name).exists()] if not hits: print(f" {archive_name}: not found under MAME_ROMPATH") continue with zipfile.ZipFile(hits[0]) as archive: for name in members: try: data = archive.read(name) except KeyError: print(f" {archive_name}/{name}: missing") continue print(f" {archive_name}/{name}: {len(data)} B SHA-1 {hashlib.sha1(data).hexdigest()}") return 0 if __name__ == "__main__": sys.exit(main())