#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 7 judge: carts that keep an EEPROM save across runs. Each title runs twice on fresh consoles sharing one save directory (a temporary ``XDG_DATA_HOME`` unless ``--keep``): run 1 starts from an erased chip, run 2 from what run 1 left. A title that saves shows writes in run 1, reads in run 2 that return run 1's cells, and (a program that only fills an empty chip) no rewrite in run 2. The chip is given by ``--eeprom`` (TYPE[:ORG]) where the cart table has none. python -I scripts/m7_eeprom.py "Circuit Dude" "Klondike" --frames 900 \ [--eeprom 93C46:16] [--fault NAME] """ from __future__ import annotations import argparse import os import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) FAULTS = ("no-persist", "do-inverted") # JOYSTICK bits as stored (Suzy swaps the directions when the game runs with Lefthand # clear, so "R" here moves left on screen in such a game): U D L R, A, B. KEYS = {"U": 0x80, "D": 0x40, "L": 0x20, "R": 0x10, "A": 0x01, "B": 0x02} def play(board, script: str, run_frames) -> None: """``W`` waits n frames; ```` presses a key n times (4 frames on, 14 off).""" for token in script.split(): if token[0] == "W": run_frames(board, int(token[1:])) continue for _ in range(int(token[1:] or 1)): board.suzy.joystick = KEYS[token[0]] run_frames(board, 4) board.suzy.joystick = 0 run_frames(board, 14) def run_once(cart: Path, frames: int, eeprom: str, fault: str | None, script: str = ""): from lynx_python.__main__ import boot, run_frames from lynx_python.cart import load from lynx_python.picture import save description = load(cart, overrides={"eeprom": eeprom}) board = boot(cart, description=description) chip = board.port.eeprom if fault == "no-persist": chip.path = None # the save is never written if fault == "do-inverted": # the chip's DO read inverted (this process only) from lynx_python.cart.port import CartPort original = CartPort.audin_pin if not hasattr(CartPort, "_original_audin_pin"): CartPort._original_audin_pin = original CartPort.audin_pin = lambda self: original(self) ^ (1 if chip.cs else 0) if script: play(board, script, run_frames) else: run_frames(board, frames) if PICTURES is not None: name = f"{cart.stem.split(' (')[0].replace(' ', '_')}-{'1' if script else '2'}.png" save(PICTURES / name, board.mikey.display.rgb(), description.rotation) return chip, chip.image() PICTURES: Path | None = None def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("titles", nargs="+") parser.add_argument("--frames", type=int, default=900) parser.add_argument("--eeprom", default="93C46:16") parser.add_argument("--fault", choices=FAULTS) parser.add_argument("--keep", action="store_true", help="use the real save directory") parser.add_argument("--script", default="", help="run 1's input (see play()); run 2 " "runs --frames with no input") # fmt: skip parser.add_argument("--pictures", type=Path, help="save each run's last frame here") args = parser.parse_args(argv) global PICTURES PICTURES = args.pictures from lynx_python import romsets ok = 0 for title in args.titles: cart = romsets.find_cart(title) if cart is None: print(f"{title}: not found under $NO_INTRO_DIR") continue with tempfile.TemporaryDirectory(prefix="lynx-eeprom-") as home: if not args.keep: os.environ["XDG_DATA_HOME"] = home first, image1 = run_once(cart, args.frames, args.eeprom, args.fault, args.script) second, image2 = run_once(cart, args.frames, args.eeprom, args.fault) written = sum(1 for b in image1 if b != 0xFF) kept = image2 == image1 saves = first.writes > 0 and second.reads > 0 and kept and second.writes == 0 ok += saves print( f"{cart.name}: run 1 writes {first.writes} reads {first.reads} " f"({written} bytes not erased); run 2 writes {second.writes} reads " f"{second.reads}; chip after run 2 {'equals' if kept else 'differs from'} run 1" f": {'SAVES ACROSS RUNS' if saves else 'no'}", flush=True, ) print(f"{ok}/{len(args.titles)} keep their save across runs") return 0 if ok == len(args.titles) else 1 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))