#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 1 gate, second half: host decryption against the real boot ROM. For every distinct encrypted loader frame among the carts under ``$NO_INTRO_DIR`` (read in place), run the real boot ROM on this board until the CPU reaches ``$0200`` and compare RAM from ``$0200`` with :func:`lynx_python.cart.decrypt.decrypt`. python -I scripts/m1_frames.py [--jobs 3] [--limit N] [--sets LNX,LYX] ``--sets`` keeps only the named sets (labels ``No-Intro ``: LNX, LYX, "LYX Aftermarket", "LYX Private"); the default is every set but BLL. Private sets never go into a report. """ from __future__ import annotations import argparse import hashlib import sys from concurrent.futures import ProcessPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from lynx_python import bootrom, romsets from lynx_python.board import Lynx from lynx_python.cart import load from lynx_python.cart.decrypt import BLOCK, DecryptError, decrypt, frame_blocks MAX_INSTRUCTIONS = 6_000_000 def frames(sets: list[str] | None = None) -> dict[bytes, tuple[str, bytes, int]]: found: dict[bytes, tuple[str, bytes, int]] = {} for label, directory in romsets.no_intro_sets().items(): if "BLL" in label or not directory.is_dir(): continue if sets is not None and label.removeprefix("No-Intro ") not in sets: continue for path in sorted(directory.iterdir()): try: description = load(path) count = frame_blocks(description.payload) except (ValueError, DecryptError, OSError): continue frame = description.payload[: 1 + count * BLOCK] found.setdefault(frame, (path.name, description.payload, description.bank0_block)) return found def check(item: tuple[str, bytes, int]) -> tuple[str, str]: name, payload, block = item board = Lynx(payload, block, boot_rom=bootrom.find()) board.reset() for _ in range(MAX_INSTRUCTIONS): if board.cpu.pc == 0x0200: break board.step() else: return name, "never reached $0200" expected = decrypt(payload) got = bytes(board.ram[0x200 : 0x200 + len(expected)]) if got == expected: return name, "same" first = next(i for i in range(len(expected)) if got[i] != expected[i]) return name, f"differs from byte {first} of {len(expected)}" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--jobs", type=int, default=3) parser.add_argument("--limit", type=int) parser.add_argument("--offset", type=int, default=0, help="skip the first N frames") parser.add_argument("--sets", help="comma-separated set names (LNX, LYX, ...)") args = parser.parse_args() sets = args.sets.split(",") if args.sets else None items = list(frames(sets).items())[args.offset :][: args.limit] print(f"{len(items)} distinct frames") tally: dict[str, int] = {} with ProcessPoolExecutor(args.jobs) as pool: for (frame, _item), (name, verdict) in zip( items, pool.map(check, [item for _f, item in items]), strict=True ): key = "same" if verdict == "same" else "differs" tally[key] = tally.get(key, 0) + 1 if verdict != "same": print(f"{hashlib.sha1(frame).hexdigest()[:12]} {name}: {verdict}") print(", ".join(f"{k} {v}" for k, v in sorted(tally.items()))) return 0 if set(tally) == {"same"} else 1 if __name__ == "__main__": sys.exit(main())