#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Milestone 8's frontend judge: the pygame-ce window, headless (SDL's dummy video driver). PYPY_GC_NURSERY=1M nice -n 19 .venv-pypy/bin/python scripts/m8_frontend.py [--frames N] [--fault keys|pixels|composite] 1. Palettes: for each of cool, warm and hot the hue step measured back from the RGB table (the angle between successive hues in U/V, through an independent RGB -> YUV inversion) equals the wiki's figure within 0.2 degrees, hue 0 has no chroma and luminance rises with the low nibble. 2. The window: Joust (USA) from the NTSC BIOS to frame ``--skip``, then N frames through ``Frontend.run_frame``/``draw``; after each frame the window surface is read back (``pygame.image.tobytes``) and every pixel compared with the board's frame mapped through the palette by this script (scale 1). 3. The keys: synthetic KEYDOWN/KEYUP events posted to SDL's queue must reach the board: Right clears SWCHA D7 (port 0) while held, Enter clears SWCHB D0, Space sets both buttons of the ProLine. 4. The composite option: on a synthetic 320-mode row, alternating light and dark pixels give chroma of opposite signs for the two phases, a uniform row gives none. Seeded faults: ``keys`` (Right ignored), ``pixels`` (the wrong palette drawn), ``composite`` (the option does nothing). Each must fail. No human has looked at this window on a screen: this judge reads the surface back. """ from __future__ import annotations import argparse import math import os import sys from pathlib import Path os.environ["SDL_VIDEODRIVER"] = "dummy" os.environ["SDL_AUDIODRIVER"] = "dummy" ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / "src")) import pygame # noqa: E402 from a7800_python import frontend, romsets # noqa: E402 from a7800_python.system import System # noqa: E402 JOUST = romsets.no_intro(romsets.LICENSED_7800) / "Joust (USA).zip" def to_yuv(rgb: tuple[int, int, int]) -> tuple[float, float, float]: r, g, b = (c / 255 for c in rgb) y = 0.299 * r + 0.587 * g + 0.114 * b return y, 0.492 * (b - y), 0.877 * (r - y) def check_palettes() -> list[str]: fails = [] for kind, step in frontend.PALETTES.items(): table = frontend.palette(kind) steps = [] for hue in range(1, 15): _, u1, v1 = to_yuv(table[hue << 4 | 7]) _, u2, v2 = to_yuv(table[(hue + 1) << 4 | 7]) d = math.degrees(math.atan2(v2, u2) - math.atan2(v1, u1)) % 360 steps.append(d) mean = sum(steps) / len(steps) ok = abs(mean - step) < 0.2 greys = all(len(set(table[c])) == 1 for c in range(16)) rising = all(to_yuv(table[0x40 | lum])[0] <= to_yuv(table[0x40 | lum + 1])[0] + 1e-9 for lum in range(15)) # fmt: skip print(f"palette {kind}: hue step {mean:.2f} (wiki {step}); hue 0 grey {greys}; " f"luminance rising {rising}") # fmt: skip if not (ok and greys and rising): fails.append(f"palette {kind}") return fails def check_window(frames: int, skip: int, faults: set[str]) -> list[str]: fails = [] system = System.from_rom(JOUST) ui = frontend.Frontend(system, "warm", False, scale=1) ui.faults = faults while ui.tv.count < skip: system.step() table = [bytes(c) for c in frontend.palette("warm")] bad_frames = 0 for _ in range(frames): ui.run_frame() ui.draw() shown = pygame.image.tobytes(ui.window, "RGB") expected = b"".join(b"".join(table[c] for c in row) for row in ui.tv.frames[-1]) if shown != expected: bad_frames += 1 print(f"window: {frames} frames read back, {bad_frames} differ from the board's frame") if bad_frames: fails.append("window pixels") riot = system.board.riot tia = system.board.tia def key(name: str, down: bool) -> None: kind = pygame.KEYDOWN if down else pygame.KEYUP pygame.event.post(pygame.event.Event(kind, key=pygame.key.key_code(name))) ui.run_frame() pad = system.controllers key("right", True) right_held = pad.directions[0] == 0x08 # RIGHT (controllers.py): SWCHA D7 for port 0 key("right", False) right_free = pad.directions[0] == 0 key("return", True) reset_held = riot.input_b & 0x01 == 0 key("return", False) reset_free = riot.input_b & 0x01 != 0 key("space", True) both = pad.buttons[0] == [True, True] key("space", False) print(f"keys: Right held {right_held}, released {right_free}; Enter -> SWCHB D0 low " f"{reset_held}, released {reset_free}; Space -> both buttons {both}") # fmt: skip del tia if not (right_held and right_free and reset_held and reset_free and both): fails.append("keys") pygame.display.quit() return fails def check_composite(faults: set[str]) -> list[str]: alternating = [bytes([0x0F, 0x00] * 160)] swapped = [bytes([0x00, 0x0F] * 160)] uniform = [bytes([0x08] * 320)] use = "composite" not in faults a = frontend.render(alternating, "warm", use) s = frontend.render(swapped, "warm", use) u = frontend.render(uniform, "warm", use) ca = to_yuv(tuple(a[0:3])) cs = to_yuv(tuple(s[0:3])) cu = to_yuv(tuple(u[0:3])) chroma_a = math.hypot(ca[1], ca[2]) opposite = ca[1] * cs[1] + ca[2] * cs[2] < 0 flat = math.hypot(cu[1], cu[2]) < 1e-3 print(f"composite: alternating chroma {chroma_a:.3f}, phases opposite {opposite}, uniform flat {flat}") return [] if chroma_a > 0.02 and opposite and flat else ["composite"] def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--frames", type=int, default=20) parser.add_argument("--skip", type=int, default=200) parser.add_argument("--fault", choices=("keys", "pixels", "composite")) args = parser.parse_args() faults = {args.fault} if args.fault else set() fails = check_palettes() + check_window(args.frames, args.skip, faults) + check_composite(faults) print("PASS" if not fails else f"FAIL: {', '.join(fails)}") return 0 if not fails else 1 if __name__ == "__main__": sys.exit(main())