#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Rung 6: Nintendo's aging cartridges on the board, judged by their own screens. The cartridges (No-Intro "AGS Aging Cartridge" v7.0, v7.1, v9.0, v10.0 and "AGB Aging Cartridge") are read in place from ``$NO_INTRO_DIR`` and run, never read (restart.md; docs/validation.md class R). Each AGS cartridge gets three power cycles, sharing one battery file in the output folder: 1. A blank EEPROM: the cartridge must say "CHECK SUM ERROR! DEFAULT IS SAVED." (blinking, from about frame 300 with Nintendo's BIOS). 2. L and R held at power-on open its menu; CONFIGURATION, down twice to AGING TIME, right once (01:00:00 becomes 01:30:00), A saves it to EEPROM. 3. A plain power-on: the automatic tests run with no checksum message (the settings were read back) and print PASS or FAIL per group; START then begins the aging run, whose clock shows the target time read from EEPROM (01:30:00 when the setting was kept). The AGB cartridge opens on a "SELECT TARGET" menu; AGING CARTRIDGE (down twice, A) is run from a blank EEPROM and judged the same way. Each screen is written as a PNG for reading by eye; the PASS/FAIL words are also read from the picture (green or red letters in the right-hand column of each result line), and the script prints them, with whether cycle 1 showed the checksum message (red letters). The aging clock (cycle 3's last picture) is read by eye. ``--json`` writes the verdicts and the count of PASS lines (scripts/seeded_faults.py's ``aging`` judge). python scripts/board_aging.py --bios "$BIOS" --out /tmp/aging """ from __future__ import annotations import argparse import json import os import sys from concurrent.futures import ProcessPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, os.environ.get("GBA_PYTHON_SRC", str(ROOT / "src"))) from gba_python.board import GBA from gba_python.cartridge.battery import read_save, write_battery from gba_python.cartridge.rom import GamePakROM from gba_python.ppu.timing import FRAME_CYCLES from gba_python.ppu.view import png_bytes, to_rgb A, B, START, RIGHT, DOWN, R, L = 0x001, 0x002, 0x008, 0x010, 0x080, 0x100, 0x200 # The result lines: (group, text row of 8 pixels) on the AGS and AGB result screens. GROUPS = tuple( zip(("MEMORY", "LCD", "TIMER", "DMA", "COM", "KEY INPUT", "INTERRUPT"), range(2, 16, 2)) ) AGB_GROUPS = tuple( zip(("MEMORY", "LCD", "TIMER", "DMA", "KEY INPUT", "INTERRUPT"), range(3, 15, 2)) ) SET = "Nintendo - Game Boy Advance" def press(frame: int, mask: int) -> list[tuple[int, int]]: return [(frame, mask), (frame + 5, 0)] # (name, key script, frames, shots) for each power cycle. AGS_CYCLES = ( ("1-blank", [], 400, tuple(range(250, 401, 10))), # the message blinks ( "2-config", [(0, L | R), (350, 0), *press(400, A), *press(500, DOWN), *press(550, DOWN)] + press(600, RIGHT) + press(650, A), 700, (640, 700), ), ("3-auto", press(700, START), 820, (690, 820)), ) AGB_CYCLES = ( ( "1-auto", [*press(400, DOWN), *press(450, DOWN), *press(500, A)], 1700, (390, 520, 1690), ), ) def run_cycle(rom: Path, bios: str, save: Path, keys, frames: int, shots, prefix: Path): board = GBA( GamePakROM.from_path(str(rom)), bios=bios, save_data=read_save(save) if save.exists() else None, ) script = dict(keys) pictures = {} for frame in range(frames): if frame in script: board.set_keys(script[frame]) board.run_until((frame + 1) * FRAME_CYCLES) if frame + 1 in shots: pictures[frame + 1] = picture = board.picture() Path(f"{prefix}-{frame + 1}.png").write_bytes(png_bytes(picture)) write_battery(board, save) return pictures def read_results(picture, groups=GROUPS) -> dict[str, str]: """PASS (green letters) or FAIL (red letters) at x 192-239 of each result line; "-" for a line with neither (a group whose only tests need an operator or a partner).""" verdicts = {} for name, row in groups: y0 = 8 * row green = red = 0 for y in range(y0, y0 + 8): for x in range(192, 240): r, g, b = to_rgb(picture[y][x]) if g > 100 and r < 80 and b < 80: green += 1 elif r > 150 and g < 80 and b < 80: red += 1 verdicts[name] = "PASS" if green > red and green else ("FAIL" if red else "-") return verdicts def red_text(picture) -> bool: """The checksum message: red letters in lines 64-99.""" count = 0 for y in range(64, 100): for x in range(240): r, g, b = to_rgb(picture[y][x]) count += r > 150 and g < 80 and b < 80 return count > 50 def judge(rom: Path, bios: str, out: Path) -> tuple[str, dict[str, str]]: stem = "AGB" if rom.name.startswith("AGB") else rom.name.split("(")[-2].rstrip(") ") folder = out / stem.replace(" ", "_") folder.mkdir(parents=True, exist_ok=True) save = folder / "battery.sav" if save.exists(): save.rename(folder / "battery.sav.old") cycles = AGB_CYCLES if stem == "AGB" else AGS_CYCLES results: dict[str, str] = {} for name, keys, frames, shots in cycles: pictures = run_cycle(rom, bios, save, keys, frames, shots, folder / name) if name == "1-blank": shown = any(red_text(pictures[shot]) for shot in shots) results["BLANK EEPROM"] = "message" if shown else "none" if name.endswith("auto"): # The result screen: AGS before START, AGB at its last shot. if stem == "AGB": results.update(read_results(pictures[shots[-1]], AGB_GROUPS)) else: results.update(read_results(pictures[shots[0]])) return stem, results def judge_job(job): return judge(*job) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("roms", nargs="*", type=Path) parser.add_argument("--bios", default="free") parser.add_argument("--out", type=Path, required=True) parser.add_argument("--jobs", type=int, default=5) parser.add_argument("--json", type=Path, help="also write the results here") args = parser.parse_args() if args.bios == "free": # from this checkout, also when GBA_PYTHON_SRC is a copy args.bios = str(ROOT / "reference" / "src" / "bios-cult-of-gba" / "bios.bin") base = Path(os.environ["NO_INTRO_DIR"]) / SET # A bare name is looked up in the No-Intro set. roms = [rom if rom.exists() else base / rom for rom in args.roms] if not roms: roms = sorted(base.glob("AGS Aging Cartridge*.zip")) + sorted( base.glob("AGB Aging Cartridge*.zip") ) report = [] with ProcessPoolExecutor(max_workers=args.jobs) as pool: jobs = [(rom, args.bios, args.out) for rom in roms] for stem, results in pool.map(judge_job, jobs): shown = " ".join(f"{k}={v}" for k, v in results.items()) print(f"{stem:8} {shown}", flush=True) passes = sum(v == "PASS" for v in results.values()) report.append({"rom": stem, "results": results, "passes": passes}) if args.json: args.json.write_text(json.dumps(report, indent=1)) return 0 if __name__ == "__main__": sys.exit(main())