# SPDX-License-Identifier: MIT """Compare the Midnight Resistance PAL fuse maps pinned from jtcores with MAME's dumps. MAME_ROMPATH=... python -I scripts/pal_compare.py Two independent-looking captures of the TB board's six PALs exist: the 2011 PG4UW reads in jotego's jtcores (`reference/jtcores/midres-pal/*.jed`, pinned) and the `plds` region of MAME's `midres` set (`tb-*.??h`, 260 bytes each: a 4-byte big-endian fuse count, then the fuses packed LSB first; read in place from the player's zip, never copied). The script compares the 2048 fuses of each AND array and prints counts only: no fuse data is written anywhere. Neither capture is a judge of the other (docs/validation.md); a difference is a contradiction to settle (handoff brief C2). """ from __future__ import annotations import os import re import sys import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PALS = ROOT / "reference" / "jtcores" / "midres-pal" def jed_fuses(text: str) -> dict[int, int]: fuses: dict[int, int] = {} for match in re.finditer(r"L(\d+)\s+([01\s]+)\*", text): start, bits = int(match.group(1)), re.sub(r"\s", "", match.group(2)) fuses.update((start + i, int(b)) for i, b in enumerate(bits)) return fuses def mame_fuses(data: bytes) -> list[int]: count = int.from_bytes(data[:4], "big") return [(data[4 + i // 8] >> (i % 8)) & 1 for i in range(count)] def main() -> int: rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (it must hold midres.zip)", file=sys.stderr) return 2 with zipfile.ZipFile(Path(rompath) / "midres.zip") as archive: dumps = {n: archive.read(n) for n in archive.namelist() if re.match(r"tb-\d", n)} for name, data in sorted(dumps.items()): tag = name.split(".")[0][3:] # "1-1", "2", ... "7" (tb-7 is the PROM) jed = [p for p in PALS.glob("*.jed") if p.name.startswith(f"MidRes-TB-{tag}-")] if not jed: print(f"{name:12} no jtcores fuse map (a PROM or another part)") continue theirs = jed_fuses(jed[0].read_text(encoding="latin-1")) ours = mame_fuses(data) common = [i for i in range(len(ours)) if i in theirs] differ = sum(ours[i] != theirs[i] for i in common) print(f"{name:12} {jed[0].name:32} {len(common)} fuses compared, {differ} differ") return 0 if __name__ == "__main__": sys.exit(main())