#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Compare the computed data ROM (``data_rom.py``, fullsnes' formulas) with a player's dump. The HG51B169's data ROM is 3,072 bytes. MAME's software list ``hash/snes.xml`` (CC0) names the dump in the Mega Man X2/X3 and Rockman X2/X3 entries as ``cx4.bin`` / ``cx4.ic2`` / ``cx4.u2`` / ``cx4.u3`` with SHA-1 ``a002f4ef...`` (:data:`DUMP_SHA1`). This script looks for a copy the player owns, never fetches one, and never writes one: - ``HG51B_DATA_ROM``: a file to use as is; - otherwise each directory of ``MAME_ROMPATH`` (``;``-separated), and its ``snes`` subdirectory: loose files with those names (or ``cx4.data.rom``, ``hg51bs169.data.rom``), and the zips of the software list entries (``megamnx2.zip`` ...), where a member of 3,072 bytes with the dump's SHA-1 counts. It prints where the copy was found, its SHA-1, and the first words that differ from the computed table (none, if fullsnes' formulas are right). Exit status 0 = equal, 1 = different, 2 = no copy found. MAME_ROMPATH="/roms/mame;/roms/extra" python -I scripts/check_data_rom.py """ from __future__ import annotations import hashlib import os import sys import zipfile from collections.abc import Iterator from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from hg51b_python.data_rom import DUMP_SHA1, data_rom, from_bytes # noqa: E402 NAMES = ("cx4.bin", "cx4.ic2", "cx4.u2", "cx4.u3", "cx4.data.rom", "hg51bs169.data.rom") #: The software list entries that carry the dump (MAME hash/snes.xml). SETS = ("megamnx2", "megamnx2u", "rockmnx2", "megamnx3", "megamnx3up", "rockmnx3") SIZE = 3072 def candidates() -> Iterator[tuple[str, bytes]]: """(where, bytes) for every copy found, in search order.""" explicit = os.environ.get("HG51B_DATA_ROM") if explicit: path = Path(explicit) if path.is_file(): yield str(path), path.read_bytes() for entry in filter(None, os.environ.get("MAME_ROMPATH", "").split(";")): for directory in (Path(entry), Path(entry) / "snes"): if not directory.is_dir(): continue for name in NAMES: path = directory / name if path.is_file() and path.stat().st_size == SIZE: yield str(path), path.read_bytes() for name in SETS: path = directory / f"{name}.zip" if not path.is_file(): continue with zipfile.ZipFile(path) as archive: for info in archive.infolist(): if info.file_size == SIZE: data = archive.read(info) if hashlib.sha1(data).hexdigest() == DUMP_SHA1: yield f"{path}:{info.filename}", data def find() -> tuple[str, bytes] | None: """The first copy whose SHA-1 is the dump's, else the first copy at all, else None.""" found = list(candidates()) for where, data in found: if hashlib.sha1(data).hexdigest() == DUMP_SHA1: return where, data return found[0] if found else None def main() -> int: hit = find() if hit is None: print("no data ROM found: set HG51B_DATA_ROM, or MAME_ROMPATH to where your sets are") return 2 where, data = hit digest = hashlib.sha1(data).hexdigest() print(f"{where}: SHA-1 {digest} ({'the dump' if digest == DUMP_SHA1 else 'NOT the dump'})") computed = data_rom() dumped = from_bytes(data) wrong = [(i, c, d) for i, (c, d) in enumerate(zip(computed, dumped, strict=True)) if c != d] for index, mine, theirs in wrong[:16]: print(f" word {index:03X}: computed {mine:06X}, dump {theirs:06X}") print(f"{1024 - len(wrong)} of 1024 words equal") return 1 if wrong else 0 if __name__ == "__main__": raise SystemExit(main())