# SPDX-License-Identifier: MIT """Compare two iNES dumps bank by bank and list the patched bytes. python tools/diff_dumps.py ORIGINAL.nes|.zip PIRATE.nes|.zip [--bank-kib 8] [--context 3] For each PRG bank of the second dump: the closest bank of the first and how many bytes differ. Then every run of changed bytes in the banks that line up, with the original and patched bytes, the CPU address assuming the last bank sits at $E000, and a count of absolute stores turned into BIT (opcode $2C), which is how conversions silence writes their new board does not want. CHR is compared in 1 KiB banks. ROMs are read from wherever the caller points (NO_INTRO_DIR); nothing is written. """ from __future__ import annotations import argparse import collections import zipfile from pathlib import Path STORES = {0x8C: "STY", 0x8D: "STA", 0x8E: "STX"} def load(path: Path) -> tuple[bytes, bytes]: if path.suffix.lower() == ".zip": with zipfile.ZipFile(path) as z: name = next(n for n in z.namelist() if n.lower().endswith(".nes")) data = z.read(name) else: data = path.read_bytes() if data[:4] != b"NES\x1a": raise SystemExit(f"{path.name}: not an iNES image") prg = data[4] * 0x4000 chr_ = data[5] * 0x2000 trainer = 512 if data[6] & 4 else 0 body = data[16 + trainer:] return body[:prg], body[prg:prg + chr_] def runs(a: bytes, b: bytes) -> list[tuple[int, int]]: """Changed spans; changes one equal byte apart are joined.""" out = [] i = 0 while i < len(a): if a[i] != b[i]: j = i while j < len(a) and (a[j] != b[j] or (j + 1 < len(a) and a[j + 1] != b[j + 1])): j += 1 out.append((i, j)) i = j else: i += 1 return out def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("original", type=Path) ap.add_argument("pirate", type=Path) ap.add_argument("--bank-kib", type=int, default=8) ap.add_argument("--show", type=int, default=24, help="bytes shown per run") a = ap.parse_args() prg_o, chr_o = load(a.original) prg_p, chr_p = load(a.pirate) size = a.bank_kib * 1024 banks_o = [prg_o[i:i + size] for i in range(0, len(prg_o), size)] banks_p = [prg_p[i:i + size] for i in range(0, len(prg_p), size)] def diff(x: bytes, y: bytes) -> int: return sum(p != q for p, q in zip(x, y)) + abs(len(x) - len(y)) print(f"PRG {len(prg_o) // 1024} / {len(prg_p) // 1024} KiB, CHR {len(chr_o) // 1024} / {len(chr_p) // 1024} KiB") pairs = [] for i, bank in enumerate(banks_p): best = min(range(len(banks_o)), key=lambda j: diff(bank, banks_o[j])) pairs.append((i, best, diff(bank, banks_o[best]))) print("pirate bank -> closest original bank (bytes differing):") print(" " + " ".join(f"{i}->{j}({d})" for i, j, d in pairs)) silenced = collections.Counter() last = len(banks_p) - 1 for i, j, d in pairs: if not d: continue x, y = banks_o[j], banks_p[i] for k in range(len(x) - 2): if x[k] in STORES and y[k] == 0x2C: silenced[f"{STORES[x[k]]} ${x[k + 1] | x[k + 2] << 8:04X}"] += 1 spans = runs(x, y) print(f"\nbank {i} (original {j}): {len(spans)} changed spans") for s, e in spans: where = f"${0x10000 - size + s:04X}" if i == last else f"+{s:04X}" print(f" {where} len {e - s}: orig {x[s:e][:a.show].hex()} new {y[s:e][:a.show].hex()}") if silenced: print("\nstores turned into BIT:", ", ".join(f"{k} x{v}" for k, v in sorted(silenced.items()))) chr_banks = min(len(chr_o), len(chr_p)) // 0x400 same = sum(chr_o[k * 0x400:(k + 1) * 0x400] == chr_p[k * 0x400:(k + 1) * 0x400] for k in range(chr_banks)) print(f"CHR: {same} of {chr_banks} 1 KiB banks identical in place") if __name__ == "__main__": main()