# SPDX-License-Identifier: MIT """A window to play the board in (rung 7): ``python -m gba_python.window ROM [--skip]``. A host, not part of the specification (charter: hosts run a tier, they do not define behaviour). It runs one frame of 280,896 cycles, shows the finished picture, and reads the keyboard into KEYINPUT, as fast as the board goes (the Python board is slower than real time; speed is the Rust tiers' job). Tk ships with Python and PyPy, so the window needs nothing else. Keys: arrows, Z = A, X = B, Return = Start, BackSpace = Select, A = L, S = R; Escape quits. ``--png-every N`` also writes every Nth frame to the working folder. """ from __future__ import annotations import argparse import sys import time from gba_python.board import GBA 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 # KEYINPUT bits (GBATEK §GBA Keypad Input; DOC), 1 = pressed as the board takes them. KEYS = { "z": 0x001, "x": 0x002, "BackSpace": 0x004, "Return": 0x008, "Right": 0x010, "Left": 0x020, "Up": 0x040, "Down": 0x080, "s": 0x100, "a": 0x200, } # fmt: skip def ppm_bytes(picture: list[list[int]]) -> bytes: """A binary PPM (P6) of a 240 x 160 picture of 15-bit BGR colours.""" body = bytearray() for row in picture: for colour in row: body.extend(to_rgb(colour)) return b"P6 240 160 255\n" + bytes(body) def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("rom") parser.add_argument("--bios", default="free", help="free (default) or a .bin/.zip path") parser.add_argument("--skip", action="store_true", help="start as the BIOS leaves it") parser.add_argument("--scale", type=int, default=2) parser.add_argument("--png-every", type=int, default=0) args = parser.parse_args(argv) import tkinter board = GBA(GamePakROM.from_path(args.rom), bios=args.bios) if args.skip: board.skip_bios() root = tkinter.Tk() root.title("gba-python") label = tkinter.Label(root) label.pack() held = set() root.bind("", lambda e: held.add(e.keysym)) root.bind("", lambda e: held.discard(e.keysym)) root.bind("", lambda e: root.destroy()) state = {"frame": 0, "image": None, "started": time.perf_counter()} def tick() -> None: mask = 0 for name in held: mask |= KEYS.get(name, 0) board.set_keys(mask) state["frame"] += 1 board.run_until(state["frame"] * FRAME_CYCLES) picture = board.picture() image = tkinter.PhotoImage(data=ppm_bytes(picture), format="PPM") if args.scale > 1: image = image.zoom(args.scale) label.configure(image=image) state["image"] = image # Tk keeps no reference of its own if args.png_every and state["frame"] % args.png_every == 0: with open(f"frame{state['frame']:06d}.png", "wb") as out: out.write(png_bytes(picture)) rate = state["frame"] / (time.perf_counter() - state["started"]) root.title(f"gba-python frame {state['frame']} {rate:.1f} fps") root.after(1, tick) root.after(1, tick) root.mainloop() return 0 if __name__ == "__main__": sys.exit(main())