#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 8 judge: ``lynx-play`` run headless with SDL's dummy drivers. For every frame: the window's pixels must equal the board's frame turned for the cart's rotation (lynx_python.picture.rotate, the reference) and scaled by nearest neighbour; the host's pace must equal the period the board measured. At the end: the bytes queued to the mixer must equal the board's samples, and each queued sound's raw data must come back from the mixer unchanged. ``--fault`` seeds one host fault, which the judge must catch. Needs pygame-ce (the PyPy venv has it). python -I scripts/m8_frontend.py "Klax" "Ms. Pac-Man" --frames 600 [--fault NAME] """ from __future__ import annotations import argparse import os import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) os.environ["SDL_VIDEODRIVER"] = "dummy" os.environ["SDL_AUDIODRIVER"] = "dummy" FAULTS = ("rotation-reversed", "stale-frame", "audio-dropped", "fixed-60hz", "keys-unflipped") def seed(fault: str | None) -> None: from lynx_python import play if fault == "rotation-reversed": original = play.Host.frame_surface def frame_surface(self): saved = self.rotation self.rotation = {"left": "right", "right": "left"}.get(saved, saved) try: return original(self) finally: self.rotation = saved play.Host.frame_surface = frame_surface elif fault == "stale-frame": # the window shows the frame before the one just finished from lynx_python.mikey.display import Display original_present = play.Host.present original_rgb = Display.rgb state = {"previous": None, "presenting": False} def rgb(self): if state["presenting"] and state["previous"] is not None: return state["previous"] return original_rgb(self) def present(self): current = original_rgb(self.board.mikey.display) state["presenting"] = True try: original_present(self) finally: state["presenting"] = False state["previous"] = current Display.rgb = rgb play.Host.present = present elif fault == "audio-dropped": # the first buffer of each second never queued original_queue = play.Host.queue_audio def queue_audio(self): before = len(self.queued) original_queue(self) if len(self.queued) > before and len(self.queued) % 60 == 1: self.queued.pop() play.Host.queue_audio = queue_audio elif fault == "fixed-60hz": original_step = play.Host.step def step(self, keys=0, pause=False): original_step(self, keys, pause) self.frame_interval = 1 / 60 play.Host.step = step elif fault == "keys-unflipped": play.Host._turn_keys = lambda self, keys: keys def judge(title: str, frames: int, flip: bool, presses: int) -> tuple[int, int, bool, bool, str]: import pygame from lynx_python import romsets from lynx_python.picture import rotate, scale from lynx_python.play import DOWN, LEFT, RIGHT, UP, Host cart = romsets.find_cart(title) host = Host(cart, scale=2, flip=flip) board = host.board good = bad = 0 pace_ok = True keys_ok = True for frame in range(frames): keys = (UP | RIGHT) if presses and frame % presses == 0 else 0 host.step(keys) if flip and keys: # A flipped host hands the board the opposite directions. keys_ok &= board.suzy.joystick == DOWN | LEFT rgb = board.mikey.display.rgb() turned, w, h = rotate(rgb, host.rotation) if flip: turned, w, h = rotate(turned, "right", w, h) turned, w, h = rotate(turned, "right", w, h) want = scale(turned, w, h, 2) got = pygame.image.tobytes(host.window, "RGB") if got == want: good += 1 else: bad += 1 period = board.mikey.display.frame_period if period and abs(host.frame_interval * 16_000_000 - period) > 1e-3: pace_ok = False audio_ok = b"".join(host.queued) == board.mikey.samples.tobytes() raw_ok = all(pygame.mixer.Sound(buffer=data).get_raw() == data for data in host.queued[:5]) hz = 16_000_000 / board.mikey.display.frame_period if board.mikey.display.frame_period else 0 note = (f"rotation {host.rotation}, {len(board.mikey.samples) // 2} sample pairs, " f"non-zero {sum(1 for s in board.mikey.samples if s)}, {hz:.2f} Hz") # fmt: skip pygame.mixer.quit() pygame.display.quit() return good, bad, pace_ok and keys_ok, audio_ok and raw_ok, note def main(argv: list[str]) -> int: parser = argparse.ArgumentParser() parser.add_argument("titles", nargs="+") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--flip", action="store_true") parser.add_argument("--presses", type=int, default=0, help="press up+right every N frames") parser.add_argument("--fault", choices=FAULTS) args = parser.parse_args(argv) seed(args.fault) failures = 0 for title in args.titles: good, bad, pace, audio, note = judge(title, args.frames, args.flip, args.presses) ok = bad == 0 and pace and audio failures += not ok print(f"{title}: window frames equal {good}/{good + bad}; pace and keys " f"{'ok' if pace else 'WRONG'}; queued sound {'equal' if audio else 'DIFFERS'}" f" ({note}): {'PASS' if ok else 'FAIL'}", flush=True) # fmt: skip return 1 if failures else 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))