Skip to content

GBA BIOS: Div's r3 patch no longer clobbers ArcTan2's r4 (FFTA's snowball fight froze) - #50

Merged
finger563 merged 1 commit into
mainfrom
fix/gba-bios-arctan2
Nov 30, 2026
Merged

finger563 merged 1 commit into
mainfrom
fix/gba-bios-arctan2

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

Final Fantasy Tactics Advance froze at the start of its first battle, the snowball fight. The screen stopped and buttons did nothing, but the music kept playing. The interpreter, the RISC-V recompiler (#49) and upstream's ARM64 recompiler all froze at the same frame. The bug comes from tab5-emu's own patch to gpSP's open BIOS, not from the recompilers.

Cause:

  • The gba: the open BIOS's Div returns |quotient| in r3 (Minish Cap's file select) #10 patch: it makes the BIOS's Div return |quotient| in r3, which Minish Cap's file select needs. It does this by storing the value into the SWI dispatcher's saved-r3 slot, [sp, #4].
  • ArcTan2 also calls Div, from inside the BIOS, after push {r3, r4, r5, lr}. There, [sp, #4] is ArcTan2's saved r4, so ArcTan2 returned with the caller's r4 replaced by |quotient|.
  • Why FFTA froze: its steering routine (0x08007AD0, the battle's opening camera move) keeps its object pointer in r4 across ArcTan2. With r4 clobbered, it read the heading from a BIOS address, never moved the camera, and ran again every frame.

Fix (components/gba/gpsp/open_gba_bios.c, four instructions):

  • Div stores into the slot only when the dispatcher called it (lr == swi_complete, 0x94).
  • The quotient negation now uses eor / sub with ip, asr #31 instead of the flags. That frees the instruction the check needs, so the routine still fits in its six words ahead of SoftReset at 0x1804.

Testing

On the host:

  • FFTA through the fight: starting from a save state just before the fight, with a scripted run through the name entry, the battle now starts in all three cores.
  • The two recompilers agree: the RISC-V recompiler (under QEMU) matches ARM64 on every frame for 6,000 frames.
  • A state saved while frozen also carries on.
  • No change elsewhere: 13 other cases give the same video and audio as before. They include Minish Cap's boot to the file select, which needs r3 from Div.
  • Recompiler matrix: the RISC-V recompiler still matches ARM64 in all 12 matrix cases.

On the Tab5:

  • Recovery from the freeze: loading the state saved while frozen continues into the battle's dialogue.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

…ball fight froze)

Final Fantasy Tactics Advance froze as its first battle, the snowball fight,
began: the screen stopped, the music played on, buttons did nothing. The
interpreter and both recompilers (ours and upstream's ARM64) froze alike.

The cause is tab5-emu's patch to the open BIOS's Div (#10), which returns
|quotient| in r3 by storing it into the SWI dispatcher's saved r3 slot,
[sp, #4]. ArcTan2 calls Div too, from inside the BIOS, after
push {r3, r4, r5, lr}: there [sp, #4] is ArcTan2's saved r4, so ArcTan2
returned with the caller's r4 replaced by |quotient|. FFTA's steering
routine (0x08007AD0, the battle's opening camera move) keeps its object
pointer in r4 across ArcTan2; it then read the heading through a BIOS
address, never moved the camera, and ran again every frame.

Div now stores into the slot only when the dispatcher called it (lr ==
swi_complete, 0x94). Its quotient negation uses eor / sub with ip, asr #31
instead of the flags, which keeps the routine in its six words ahead of
SoftReset at 0x1804.

Tested on the host from a state just before the fight (scripted input
through the name entry): the battle now starts in the interpreter, the
RISC-V recompiler (under QEMU) and upstream's ARM64 one, the RISC-V
recompiler identical to ARM64 every frame for 6000 frames; the state saved
while frozen also carries on. The 13 other host cases (Minish Cap's boot to
the file select, which needs r3, included) give the same video and audio as
before, and the recompiler still matches ARM64 in all 12 matrix cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
@finger563
finger563 merged commit 174212e into main Nov 30, 2026
1 check passed
@finger563
finger563 deleted the fix/gba-bios-arctan2 branch November 30, 2026 02:13
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