Repository navigation
GBA BIOS: Div's r3 patch no longer clobbers ArcTan2's r4 (FFTA's snowball fight froze) - #50
Merged
Merged
Conversation
…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
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
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:
[sp, #4].push {r3, r4, r5, lr}. There,[sp, #4]is ArcTan2's saved r4, so ArcTan2 returned with the caller's r4 replaced by |quotient|.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):lr == swi_complete, 0x94).eor/subwithip, asr #31instead 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:
On the Tab5:
🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY