#!/usr/bin/env python3 """Rung 2's clock check: run a Pokémon game with an RTC and log what it asks the clock. docs/validation.md rung 2 names "a Pokémon game's clock". Without a picture (rung 3) the check is what the game does on the GPIO port: it must read the control register (battery good, 24-hour mode), read the date and time, and never force-reset the clock, which is what the games do when they judge the RTC broken (the "internal battery has run dry" path). The ROM is read in place. python scripts/pokemon_clock.py "Pokemon - Emerald Version (USA, Europe).zip" --frames 900 """ from __future__ import annotations import argparse import sys from collections import Counter from datetime import datetime from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "src")) from gba_python import GBA # noqa: E402 from gba_python import bios as bios_module # noqa: E402 from gba_python.testrom import read_rom # noqa: E402 NOINTRO = Path("/data/emu/source/myrient.erista.me/files/No-Intro/Nintendo - Game Boy Advance") NAMES = {0: "force reset", 2: "date+time", 3: "force IRQ", 4: "control", 6: "time"} def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("rom") parser.add_argument("--frames", type=int, default=900) args = parser.parse_args() path = Path(args.rom) if Path(args.rom).exists() else NOINTRO / args.rom gba = GBA(bios_module.find(), read_rom(path), rtc_start=datetime(2026, 10, 22, 12, 0, 0)) rtc = gba.cartridge.gpio.device log: Counter[str] = Counter() load, store = rtc._load_register, rtc._store_register def logged_load() -> None: load() log[f"read {NAMES.get(rtc.register, rtc.register)}"] += 1 def logged_store() -> None: store() log[f"write {NAMES.get(rtc.register, rtc.register)} {rtc.buffer[0]:02x}"] += 1 rtc._load_register, rtc._store_register = logged_load, logged_store for _ in range(args.frames): gba.run_frame() print(f"BIOS {gba.bios.label}; {path.name}; {args.frames} frames; RTC accesses:") for name, count in sorted(log.items()): print(f" {name}: {count}") reset = any(name.startswith(("read force reset", "write force reset")) for name in log) good = log["read control"] and (log["read date+time"] or log["read time"]) and not reset print("clock accepted" if good else "clock NOT accepted") return 0 if good else 1 if __name__ == "__main__": sys.exit(main())