#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Judge the board's pictures against hw-test's expected pictures (hw-test, BSD-3-Clause, fleroviux and contributors: the ROMs and their expected.png are the evidence; nothing of their source is copied here). hw-test's README calls them "GBA hardware tests"; docs/validation.md "Rung 3" says what each expected picture is (bgpd and greenswap are camera photographs of consoles; bgx and dispcnt-latch are clean captures whose origin the repository does not state; sprite-hmosaic's is a NanoBoyAdvance 1.8.2 screenshot and is judged as a detector picture in scripts/board_pictures.py instead). Each check reads a feature that survives the picture's scaling and colour, measured as described beside it: - ``bgx``: line 0 is white only at dot 0 and every later line only at dot 129 (expected.png, 1083x724: a 1-pixel frame and 4.5125 image pixels per dot; the white column is image columns 583-586 = dot 129, the top-left block image 1-5 = dot 0, line 0). - ``bgpd``: the picture holds exactly two bright-to-dark band edges and the first is at line 75 or 76 (the photograph's screen spans image rows 381-2662; the first edge is at row 1462, line 75.8 on a linear scale, 75.0 corrected for perspective from the screen's width). The second edge reads 145.7-145.9 in the photograph by either scale; it is reported, not judged, because the photograph's geometry leaves a few lines' doubt (docs/ validation.md). - ``dispcnt-latch``: every one of lines 0-81 falls in the same class as the expected picture's line: forced-blank white, text, empty, and whether the OBJ shows at the left (expected.png, 602x402: a 1-pixel frame and 2.5 image pixels per dot, sampled at each dot's centre). Lines 82-159 hold only more text rows and are not read. - ``greenswap`` (rung 4): on lines 1-159 the green swap ends between dots 76 and 84. The photograph (4000x3000, a camera on a GBA SP) shows the swapped columns (red and green dots merged to yellow) ending at image column 1534-1580 of a screen spanning about 504-3676: dots 78-81 by where the screen's edges are taken; the band allows the photograph's geometry a few dots more. Line 0 has no H-blank DMA before it and is not read. - ``mode3``, ``mode4`` (rung 4; hw-test archive/ppu, "replaced or deemed unfit" by its author, so a weaker judge, and photographed on a DS-family console in GBA mode): an H-blank DMA writes one VRAM halfword every 2 cycles through the next line, and every dot shows VRAM's first one. The photographs show white, red, then yellow (mode 3) or green (mode 4) from dot 120 and green or blue from dot 180 (measured 120.4/181.3 and 119.4/180.3 on 4000x3000 photographs, screens 1505 and 1507 pixels wide), with a white dot at the left edge, on every other line (the DMA takes the whole line, so the next line's start is missed). In the mode3 photograph (zoomed) the patterned lines alternate between two DMA start times: the left white stub is one dot or two to three, and the band edge moves right by about 1.5 dots with it. Judged: at least 60 patterned lines; on the earliest lines the second colour starts at dot 120 or 121 and the third 60 dots later (180-182); no line is more than 2 dots later than those. - ``window_midframe`` (rung 4; destoer gba_tests, MIT): every dot equals destoer's 240x160 expected.png at 5 bits a channel. That picture is "the expected screenshot" credited to Powerlated in the ROM's source; its origin (console capture or emulator) is not stated, so it is class H? (docs/validation.md), and NanoBoyAdvance draws the same picture (OBSERVED). python scripts/hw_pictures.py # the free BIOS, 300 frames python scripts/hw_pictures.py --bios "$NO_INTRO_DIR/.../[BIOS] ... (World).zip" """ from __future__ import annotations import argparse import json import os import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "scripts")) sys.path.insert(0, os.environ.get("GBA_PYTHON_SRC", str(ROOT / "src"))) import referee_run from board_pictures import read_png from gba_python.board import GBA from gba_python.cartridge.rom import GamePakROM PPU = ROOT / "reference" / "src" / "hw-test" / "ppu" WHITE = 0x7FFF def run(name: str, bios, frames: int) -> list[list[int]]: folder = ARCHIVE if name in ("mode3", "mode4") else PPU if name == "window_midframe": folder = DESTOER board = GBA(GamePakROM.from_path(str(folder / name / f"{name}.gba")), bios=bios) board.run_frames(frames) return board.picture() def check_bgx(picture) -> tuple[bool, str]: bad = [] for y, row in enumerate(picture): whites = [x for x, c in enumerate(row) if c == WHITE] want = [0] if y == 0 else [129] if whites != want: bad.append((y, whites[:4])) return not bad, f"{160 - len(bad)}/160 lines" + (f"; first wrong {bad[0]}" if bad else "") def brightness(color: int) -> int: return (color & 31) + ((color >> 5) & 31) + ((color >> 10) & 31) def check_bgpd(picture) -> tuple[bool, str]: column = [brightness(row[100]) for row in picture] edges = [y for y in range(1, 160) if column[y] < column[y - 1] - 40] ok = len(edges) == 2 and edges[0] in (75, 76) return ok, f"band edges at lines {edges} (photograph: 75-76, then about 146)" def dispcnt_classes(get) -> list[tuple[str, bool]]: classes = [] for y in range(82): whites = sum(1 for x in range(16, 240) if min(get(x, y)) > 150) sprite = sum(1 for x in range(16) if get(x, y)[0] > 100 and get(x, y)[2] < 120) >= 3 kind = "blank" if whites >= 220 else ("text" if whites > 20 else "empty") classes.append((kind, sprite)) return classes def check_dispcnt(picture) -> tuple[bool, str]: expected = read_png(PPU / "dispcnt-latch" / "expected.png") def get_expected(x, y): return expected[int(1 + y * 2.5 + 1.25)][int(1 + x * 2.5 + 1.25)] def get_board(x, y): c = picture[y][x] return ((c & 31) << 3, ((c >> 5) & 31) << 3, ((c >> 10) & 31) << 3) want = dispcnt_classes(get_expected) got = dispcnt_classes(get_board) wrong = [y for y in range(82) if want[y] != got[y]] return not wrong, f"{82 - len(wrong)}/82 lines" + (f"; wrong at {wrong[:8]}" if wrong else "") def check_greenswap(picture) -> tuple[bool, str]: ends = {} for y in range(1, 160): row = picture[y] plain = [(0x001F, 0x03E0, 0x7C00)[(y * 240 + x) % 3] for x in range(240)] swapped = [x for x in range(200) if row[x] != plain[x]] end = swapped[-1] + 1 if swapped else 0 ends[end] = ends.get(end, 0) + 1 good = sum(n for end, n in ends.items() if 76 <= end <= 84) return good == 159, f"swap ends at dots {dict(sorted(ends.items()))} (photograph: 78-81)" def bands(picture) -> tuple[int, int, dict]: edges: dict = {} patterned = good = 0 for row in picture: runs = [0] + [x for x in range(1, 240) if row[x] != row[x - 1]] if len(runs) < 4 or row[0] != WHITE: continue patterned += 1 key = (runs[-2], runs[-1]) edges[key] = edges.get(key, 0) + 1 good += runs[-2] in (120, 121) and 180 <= runs[-1] <= 182 return patterned, good, edges def check_bands(picture) -> tuple[bool, str]: patterned, _good, edges = bands(picture) if not edges: return False, "no patterned line" first = min(edges) ok = ( patterned >= 60 and first[0] in (120, 121) and 180 <= first[1] <= 182 and all(a - first[0] <= 2 and b - a == first[1] - first[0] for a, b in edges) ) top = dict(sorted(edges.items(), key=lambda kv: -kv[1])[:4]) return ok, f"{patterned} patterned lines, edges {top} (photographs: 120-121, 180-181)" def check_window_midframe(picture) -> tuple[bool, str]: expected = read_png(DESTOER / "window_midframe" / "expected.png") wrong = 0 lines = set() for y in range(160): for x in range(240): c = picture[y][x] got = (c & 31, (c >> 5) & 31, (c >> 10) & 31) want = tuple(v >> 3 for v in expected[y][x][:3]) if got != want: wrong += 1 lines.add(y) detail = f"{38400 - wrong}/38400 dots" + (f"; lines {sorted(lines)[:6]}" if wrong else "") return not wrong, detail ARCHIVE = ROOT / "reference" / "src" / "hw-test" / "archive" / "ppu" DESTOER = ROOT / "reference" / "src" / "gba_tests-destoer" CHECKS = { "bgx": check_bgx, "bgpd": check_bgpd, "dispcnt-latch": check_dispcnt, "greenswap": check_greenswap, "mode3": check_bands, "mode4": check_bands, "window_midframe": check_window_midframe, } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("names", nargs="*", default=list(CHECKS)) parser.add_argument("--bios", default="free") parser.add_argument("--frames", type=int, help="default 300 (free BIOS), 600 (a dump)") parser.add_argument("--json", type=Path) args = parser.parse_args() frames = args.frames or (300 if args.bios == "free" else 600) passed = 0 results = [] with tempfile.TemporaryDirectory() as scratch: bios_args = referee_run.bios_args(args.bios, Path(scratch)) bios = bios_args[1] if bios_args else "free" for name in args.names: ok, detail = CHECKS[name](run(name, bios, frames)) passed += ok results.append({"name": name, "status": "pass" if ok else "fail", "detail": detail}) print(f"{'pass' if ok else 'FAIL':5} {name:14} {detail}", flush=True) print(f"{passed}/{len(args.names)} hw-test pictures match") if args.json: args.json.write_text(json.dumps(results, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())