Repository navigation
SNES: the APU on the second core, wait-loop skip, strip rendering (Mega Man X 87% → 100%) - #51
Merged
Merged
Conversation
The SNES sound unit was about a third of each frame's work on core 0, while
core 1 sat 83% idle (it runs the video and audio-out tasks).
The S-CPU side now turns its APU accesses into timed commands in a ring
(apu_blargg.c), and a worker task on core 1 ("snes_apu", priority 19, below
video and audio out) runs them in order:
- a port write is queued with its APU clock; the S-CPU does not wait;
- a port read queues "run to this clock" and waits for the worker to get
there (Mega Man X reads none during play; uploads poll heavily);
- each scanline's S9xAPUExecute queues spc_end_frame and the samples
callback; the clock bookkeeping (reference_time, spc_remainder) stays on
the S-CPU side, which alone uses it;
- the frame-end audio pull is queued too, and a frame waits only for the
one before it, so the APU's tail of a frame overlaps the next;
- reset, save / load state and the timing setters drain the ring first.
The APU sees the same inputs at the same APU clocks as before, so its output
is unchanged: on the host (an RV32 build of the core under QEMU, with the
Tab5's save states, and a native build with real threads from power-on),
every frame's video and the whole audio stream match the inline APU in 11
cases (Mega Man X states and eight other games, Mode 7, hi-res and Super FX
included), and the threaded runs match every time.
CONFIG_TAB5_EMU_SNES_APU_CORE1 (default y); off, or if the task cannot be
created, the APU runs inline as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
snes9x's idle-loop hack (CPUShutdown, and ForceShutdown for the STP / WDM speed hacks) jumps CPU.Cycles to the next event once a branch keeps going back to an instruction that read RAM. It is on (Settings.Shutdown), but the jump sat inside #ifndef USE_BLARGG_APU together with the old APU's catch-up loop, so with the blargg APU every spin of a wait-for-interrupt loop was emulated. The blargg APU runs to CPU.Cycles at its next port access or scanline end, so the jump needs nothing else. Mega Man X: ~8% faster frames on the Tab5 (its main loop spins on a RAM flag until the next NMI). Landing exactly on the event instead of a few cycles past it shifts the APU's timing by fractions of a sample: on the host the audio differs from the first seconds in 10 of 11 cases; the video is the same in all but Super Mario World's attract demo, where frames 366-373 differ and then converge again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
S9xUpdateScreen drew every layer over the whole range of lines since the last update (usually the frame): screen, sub-screen and two depth buffers, ~340 KB in PSRAM, more than the ESP32-P4's 64 KB L1 and 256 KB L2, streamed once per layer. It now draws the range in strips of 16 lines, every layer of one strip before the next, and a strip's two depth buffers live in a 16 KB internal buffer (the glue allocates it; without it they stay in the PSRAM buffers). An update clears the depth lines of its range before drawing them and uses only those, so they need not persist between updates. The pixels are the same: an update draws any range (mid-frame register writes split frames anyway), and the range's sprite-overflow flags still come from its last line only (PPU.RangeTimeOver is put back after the inner strips). Hi-res and interlaced frames keep one pass. Tab5, Mega Man X with every frame drawn: one pass 20.2 ms a frame, 16-line strips 18.0 ms (8: 18.7, 32: 18.2), and with the depth strips internal 16.8 ms (19.2 in PSRAM, same frames interleaved). Host: every frame's video and audio identical to one pass in 11 cases, and nothing outside a strip's depth lines is touched (guard zones around the buffer stay intact). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…h, renderer at -O3 - The tile pixel writers stored depth bytes through uint8_t pointers, which may alias anything, so GFX.Z1 / Z2 / FixedColour / Delta (GFX lives in PSRAM) were reloaded for every pixel: they are copied to locals first. - The colour-math writers (add / subtract, half and fixed-colour variants) were a function call per 4 pixels; they are always_inline now. - tile.c and gfx.c are built at -O3 (the rest of the core stays at -O2: all of it at -O3 is slower, the larger code misses in the instruction cache). Tab5, Mega Man X highway scene, frames 120-1119 of a state, every frame drawn: 18.35 ms -> 17.81 (locals) -> 16.57 (inline) -> 15.74 ms (-O3). Host: every frame identical in 11 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
Mega Man X ran a little slow on the Tab5. In the intro stage's boss fight it managed 87% speed while drawing only every other frame, at 23.4 ms a drawn frame. It now holds 100% speed with 88% of frames drawn, at 15.0 ms each.
The work is split across the two cores. Four commits:
CONFIG_TAB5_EMU_SNES_APU_CORE1(default on) can turn it off.CPUShutdown) was compiled out together with the old APU's catch-up loop, so every spin of a wait-for-interrupt loop was emulated.GFXfields are copied to locals; byte stores throughuint8_t*made the compiler reload them from PSRAM for every pixel.tile.candgfx.care built at-O3. The whole core at-O3is slower.Results (Tab5, Mega Man X)
Testing
🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY