Skip to content

GBA: a RISC-V recompiler for gpSP (Minish Cap in Hyrule Town 75% → 100% speed) - #49

Merged
finger563 merged 3 commits into
mainfrom
perf/gba-speed
Nov 30, 2026
Merged

finger563 merged 3 commits into
mainfrom
perf/gba-speed

Conversation

@finger563

Copy link
Copy Markdown
Contributor

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

  1. gpSP: sync with upstream 5819380. The bundled gpSP is brought up to upstream's latest (I/O read semantics, code running from VRAM) by a three-way merge. It's kept separate so it can be reviewed against upstream.
  2. Interpreter tweaks. The fetch reads the PC once, AL instructions skip the condition switch, and the cheat-hook and idle-loop PCs are copied into locals. That's about 6% fewer host instructions and 2–3% faster, with exactly the same output. It still matters with the recompiler off and for code running from VRAM.
  3. The RISC-V recompiler and Tab5 integration:
    • 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.
    • The memory handlers follow upstream's ARM64 backend rules exactly.
    • CONFIG_TAB5_EMU_GBA_DYNAREC (on by default; off builds the interpreter as before).
    • Translation caches in PSRAM: 2 MB for ROM code and 384 KB for RAM code, allocated per GBA session before the ROM cache. This is about 2.4 MB in all, freed when the game exits.
    • Cache sync: done the way the overlay loader does it, with esp_cache_msync (write back + invalidate) then fence.i.
    • components/gba/gpsp/tests/riscvgen.py: an encoder test, like upstream's tests/mipsgen.c / arm64gen.c.

Results (Tab5)

Game Before After
Minish Cap, slot 2 (Hyrule Town) 72–78%, every other frame rendered at ~24 ms 100%, every frame, 10.7–13.2 ms
Metroid Fusion, Mario Kart, ALttP, Fire Red, Leaf Green, SMA4, FF Tactics Advance, Mario Golf, Mario Tennis — 100%, every frame, 6.8–11.3 ms
  • The frame budget is 16.7 ms.
  • The other games were measured about 15 s after launch, mostly on title and menu screens.
  • About 3.7 MB of PSRAM stays free while a game runs.

Testing

  • Play-tested on the Tab5: Minish Cap around Hyrule Town with touch input; save and load states.
  • Bit-exact against upstream's ARM64 recompiler:
    • The RV32 build of the committed tree runs bare-metal under qemu-system-riscv32, at the Tab5's cache sizes.
    • It gives the same video and audio hashes, every frame, as upstream's ARM64 recompiler running natively, in all 12 cases: 8 games, cold boots and save states, 1,800 frames each.
    • Both recompilers differ from the interpreter, because they count cycles per block, so ARM64 is the reference.
  • Interpreter unchanged: on the host, commits 1, 2 and 3 (recompiler off) all produce the same per-frame video, audio and final state as main over 3,000 frames of Minish Cap slot 2.
  • Encoder test: riscvgen.py passes (812 encodings against the GNU assembler). A deliberately broken encoder makes it fail.
  • Builds: each commit builds for the Tab5 on its own.

To run the encoder test (needs a host C compiler and the IDF toolchain on the PATH):

. $IDF_PATH/export.sh && python3 components/gba/gpsp/tests/riscvgen.py

Known limitations

  • Inherited from upstream: like upstream's recompilers, VRAM / OAM byte stores write the byte to both halves of the halfword, and OAM byte stores (which the hardware ignores) are stored.
  • Cache flushes: when a translation cache fills, it is flushed and rebuilt, which may cause a brief hitch. After a minute of play the largest games had used 200–924 KB of the 2 MB ROM cache.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

finger563 and others added 3 commits November 29, 2026 16:19
…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
@finger563
finger563 merged commit 1ed398f into main Nov 30, 2026
1 check passed
@finger563
finger563 deleted the perf/gba-speed branch November 30, 2026 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant