Repository navigation
GBA: a RISC-V recompiler for gpSP (Minish Cap in Hyrule Town 75% → 100% speed) - #49
Merged
Merged
Conversation
…om VRAM) Brings the bundled gpSP from libretro/gpsp 8d268a6 to 5819380, merged three-way into the tab5-emu copy: - 340c262: I/O reads that differ from the stored value (write-only fields of the video, sound and DMA registers, read masks, and addresses past the 1 KB register block, which are open bus rather than mirrors) go through io_read_requires_handler() and the new I/O read handlers. - 5819380: SOUNDCNT_X keeps the PSG channel-active bits readable. - 7c64ebf (#289): code that runs from VRAM is never translated. The recompilers call execute_arm_vram(), which interprets until execution leaves VRAM (execute_arm() becomes execute_arm_internal(cycles, vram_only)). - b5774e6, fa2b04b: ARM32 / ARM64 recompiler fixes only. Only the interpreter is built here, so the arm/, mips/ and x86/ parts of those commits are left out, as before. tab5-emu's lazy flags stay: the VRAM exit check collapses them before it returns. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…ch for AL About 6% fewer host instructions per guest instruction (Minish Cap in Hyrule Town: ~60 per guest instruction before), 2-3% faster; the output is unchanged (bit-identical video, audio and state over 3000 frames). - The fetch reads reg[REG_PC] once: reg[] is a global the compiler must reload after every store, and the cheat hook test, the page check and the fetch each read it. - The cheat hook and idle loop PCs are copied to locals per call (they only change between frames). - AL, nearly every ARM instruction, skips the condition switch and its indirect jump. - The VRAM exit check (upstream: before every instruction) moves into the fetch's page change: leaving VRAM always changes the 32 KB page. These matter when the interpreter runs the game (the recompiler off, or code running from VRAM). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…75% -> 100% speed) gpSP's recompiler had backends for ARM32, ARM64, MIPS and x86 only, so the Tab5 ran GBA games in the interpreter: 72-78% speed in Minish Cap's Hyrule Town (save slot 2), about 60 host instructions per guest instruction. components/gba/gpsp/riscv is an RV32IM backend, ported from the MIPS one (no condition flags there either): - riscv_codegen.h: the instruction encoders (base ISA and M only: the P4 has no compressed or Zba/Zbb instructions in this code). - riscv_emit.h: the ARM / Thumb translation. Flags in s3-s6, every guest register in a host register; branches that get patched reserve two instructions (+-4 KB conditional branches, +-1 MB jal). The memory handlers are emitted once per access type and region at init and follow the ARM64 backend's rules exactly (which I/O reads go direct, BIOS read protection, self-modifying code checks on RAM stores). - riscv_stub.S: the runtime stubs (entry and exit, cycle updates, indirect branches, CPSR / SPSR, SWI, the VRAM interpreter bridge). The front end is upstream's: cpu_threaded.c includes riscv_emit.h for RISCV_ARCH, and platform_cache_sync() calls a weak gpsp_riscv_cache_sync() (fence.i). memmap.c gets the translation caches from a weak gpsp_jit_block_alloc() (malloc), and cpu.cc keeps the CPU state arrays in C for this backend. Tab5 integration (CONFIG_TAB5_EMU_GBA_DYNAREC, on by default; off builds the interpreter as before): - 2 MB ROM and 384 KB RAM translation caches in PSRAM (executable: the PMP is off), allocated per session before the ROM cache; a 64 KB branch hash. - Cache sync as the overlay loader does it: esp_cache_msync (write back, invalidate) then fence.i. - gpsp_drc "enabled". Results on the Tab5: - Minish Cap slot 2: 100% speed, every frame rendered, 10.7-13.2 ms a frame (16.7 ms budget), with the core about 20% idle. - Metroid Fusion, Mario Kart, A Link to the Past, Fire Red, Leaf Green, Super Mario Advance 4, FF Tactics Advance, Mario Golf, Mario Tennis: 100%, 6.8-11.3 ms a frame. - About 3.7 MB of PSRAM stays free while a game runs. - Play-tested in Minish Cap: touch input, save and load states. Validation: on the host, the RV32 build under qemu-system-riscv32 gives the same video and audio, every frame, as upstream's ARM64 recompiler in 12 cases (8 games, cold boots and save states, 1800 frames each). (Both differ from the interpreter: recompilers count cycles per block.) tests/riscvgen.py checks the 812 encodings against the GNU assembler. Like upstream's recompilers, VRAM / OAM byte stores write the byte to both halves of the halfword, and OAM byte stores (ignored by the hardware) are stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
GBA games ran in gpSP's interpreter, because gpSP's recompiler had no RISC-V backend. Minish Cap in Hyrule Town (save slot 2) ran at 72–78% speed.
This adds a RISC-V backend, so the game's ARM / Thumb code is translated to native RV32 code. That scene now runs at 100% speed, rendering every frame, with the core about 20% idle.
Commits
components/gba/gpsp/riscv/: a port of upstream's MIPS backend (MIPS has no condition flags either). It has three parts:riscv_codegen.h: the RV32IM encoders;riscv_emit.h: the translation and the per-region memory handlers;riscv_stub.S: the runtime stubs.CONFIG_TAB5_EMU_GBA_DYNAREC(on by default; off builds the interpreter as before).esp_cache_msync(write back + invalidate) thenfence.i.components/gba/gpsp/tests/riscvgen.py: an encoder test, like upstream'stests/mipsgen.c/arm64gen.c.Results (Tab5)
Testing
qemu-system-riscv32, at the Tab5's cache sizes.mainover 3,000 frames of Minish Cap slot 2.riscvgen.pypasses (812 encodings against the GNU assembler). A deliberately broken encoder makes it fail.To run the encoder test (needs a host C compiler and the IDF toolchain on the PATH):
Known limitations
🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY