"""The frontend (frontend.py): its conversions without pygame, and a window driven headless (SDL's dummy drivers) on a synthetic cartridge, with a save written beside the image.""" from __future__ import annotations import zipfile from array import array from pathlib import Path import pytest from gba_python import keypad from gba_python.apu import SAMPLE_RATE from gba_python.frontend import WORDS, Console, frame_image, save_path from gba_python.testrom import screen_rgb BRANCH_TO_SELF = (0xEAFFFFFE).to_bytes(4, "little") def cartridge(folder: Path, zipped: bool = False) -> tuple[Path, Path]: """A BIOS and an SRAM cartridge that both idle on a branch to themselves.""" bios = folder / "bios.bin" bios.write_bytes(BRANCH_TO_SELF * 0x1000) image = BRANCH_TO_SELF * 0x40 + b"SRAM_V113" + bytes(0x100) rom = folder / ("game.zip" if zipped else "game.gba") if zipped: with zipfile.ZipFile(rom, "w") as archive: archive.writestr("game.gba", image) else: rom.write_bytes(image) return bios, rom def test_words_widen_five_bits_as_the_png_writer_does() -> None: assert WORDS[0] == 0xFF000000 assert WORDS[0x7FFF] == 0xFFFFFFFF assert WORDS[31] == 0xFFFF0000 # red is the low five bits assert WORDS[16 << 10] == 0xFF000084 # 16 << 3 | 16 >> 2 def test_frame_image_is_the_png_writers_picture(tmp_path: Path) -> None: bios, rom = cartridge(tmp_path) console = Console(rom, bios, skip_bios=True) screen = console.gba.ppu.screen screen[0], screen[1], screen[239] = 0x001F, 0x7FFF, 0x8000 | 0x03E0 # bit 15 is ignored words = array("I", frame_image(screen)) assert len(words) == 240 * 160 assert words[:2].tolist() == [0xFFFF0000, 0xFFFFFFFF] and words[239] == 0xFF00FF00 rgb = bytes(c for w in words for c in (w >> 16 & 255, w >> 8 & 255, w & 255)) assert rgb == screen_rgb(console.gba) def test_saves_live_beside_the_image_or_in_a_folder(tmp_path: Path) -> None: rom = tmp_path / "Some Game (USA).zip" assert save_path(rom) == tmp_path / "Some Game (USA).sav" assert save_path(rom, tmp_path / "saves", ".state") == tmp_path / "saves/Some Game (USA).state" def test_the_console_runs_frames_sounds_and_takes_buttons(tmp_path: Path) -> None: bios, rom = cartridge(tmp_path, zipped=True) console = Console(rom, bios, skip_bios=True) sound = b"".join(console.run_frame() for _ in range(10)) now = console.gba.scheduler.now assert 9 * 280_896 < now <= 10 * 280_896 # the first frame ends at the first VBlank assert abs(len(sound) // 4 - now * SAMPLE_RATE // 16_777_216) <= 1 console.press(keypad.A | keypad.LEFT) assert console.gba.keypad.input == keypad.ALL & ~(keypad.A | keypad.LEFT) console.press(keypad.L) assert console.gba.keypad.input == keypad.ALL & ~keypad.L def test_the_save_chip_and_the_state_come_back(tmp_path: Path) -> None: bios, rom = cartridge(tmp_path) saves = tmp_path / "saves" console = Console(rom, bios, skip_bios=True, save_dir=saves) console.gba.bus_write(0x0E000123, 8, 0x5A, 0) assert console.save() == saves / "game.sav" again = Console(rom, bios, skip_bios=True, save_dir=saves) # the next power-on reads it assert again.gba.bus_read(0x0E000123, 8, 0) == 0x5A assert not again.load_state() # none yet again.run_frame() again.gba.bus_write(0x02000000, 8, 0x77, 0) again.save_state() again.gba.bus_write(0x02000000, 8, 0x11, 0) assert again.load_state() assert again.gba.bus_read(0x02000000, 8, 0) == 0x77 assert len(again.run_frame()) > 0 # sound is still captured after a load def test_a_headless_window_plays_and_saves(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: pygame = pytest.importorskip("pygame") controller = pytest.importorskip("pygame._sdl2.controller") monkeypatch.setenv("SDL_VIDEODRIVER", "dummy") monkeypatch.setenv("SDL_AUDIODRIVER", "dummy") bios, rom = cartridge(tmp_path) from gba_python.frontend import Frontend pygame.display.init() controller.init() try: frontend = Frontend(pygame, rom, str(bios), True, None, 1, True, False) frontend.console.gba.bus_write(0x0E000000, 8, 0x12, 0) frontend.console.gba.ppu.screen[0] = 0x001F seen = [] frontend.run(max_frames=5, on_frame=lambda f: seen.append(f.window.get_at((0, 0))[:3])) frontend.console.save() finally: pygame.quit() assert len(seen) == 5 assert (tmp_path / "game.sav").read_bytes()[0] == 0x12