Skip to content

SNES: the APU on the second core, wait-loop skip, strip rendering (Mega Man X 87% → 100%) - #51

Merged
finger563 merged 4 commits into
mainfrom
perf/snes-speed
Nov 30, 2026
Merged

finger563 merged 4 commits into
mainfrom
perf/snes-speed

Conversation

@finger563

Copy link
Copy Markdown
Contributor

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:

  1. The APU (SPC700 + DSP) on the second core.
    • The sound unit was about a third of each frame's work on core 0, while core 1 sat 83% idle.
    • The S-CPU side now turns its APU accesses into timed commands, and a worker task on core 1 runs them in order.
    • The S-CPU waits only when it reads an APU port (Mega Man X reads none during play) and, at the end of a frame, for the frame before.
    • The APU gets the same inputs at the same APU clocks, so its output doesn't change.
    • CONFIG_TAB5_EMU_SNES_APU_CORE1 (default on) can turn it off.
  2. Wait-loop skip re-enabled.
    • snes9x's idle-loop hack (CPUShutdown) was compiled out together with the old APU's catch-up loop, so every spin of a wait-for-interrupt loop was emulated.
    • The blargg APU catches up by itself, so the skip needs nothing else. It made frames about 8% faster in Mega Man X.
  3. Drawing in 16-line strips, with each strip's depth buffers in a 16 KB internal buffer.
    • Before, every layer was drawn over the whole frame: ~340 KB of buffers in PSRAM, more than the L1 and L2 caches.
    • Strips: 20.2 → 18.0 ms a drawn frame. Internal depth buffers: → 16.8 ms.
  4. Pixel-writer fixes:
    • The GFX fields are copied to locals; byte stores through uint8_t* made the compiler reload them from PSRAM for every pixel.
    • The colour-math writers are inlined.
    • tile.c and gfx.c are built at -O3. The whole core at -O3 is slower.
    • Result: 18.35 → 15.74 ms a drawn frame in the heaviest scene tested.

Results (Tab5, Mega Man X)

Before After
Boss fight (slot 1): speed / frames drawn 87% / 54% 100% / 88%
Time to draw a frame (boss fight) 23.4 ms 15.0 ms
Highway scene, every frame drawn (frames 120–1119 of a state) — 15.74 ms (60 fps)
  • Core 1: about 34% idle now, with the APU worker on it.
  • Smoke test on the Tab5: Super Mario World, Super Metroid, Star Fox and Mario Kart all run and look right.
  • Star Fox (Super FX, emulated on core 0) is unchanged, at about 40 fps.

Testing

  • The host harness: a 32-bit RISC-V build of the core under QEMU, which loads the Tab5's save states. It records every frame's video and audio hashes and counts port reads.
    • Cases: three Mega Man X states plus eight games from power-on (Mario Kart, F-Zero, Super Mario World, Zelda, Super Metroid, Donkey Kong Country, Secret of Mana, Star Fox).
    • Each case runs 1,800 frames.
  • APU queue:
    • The deferred mode (no worker; the queue runs only when something waits on it, the most lag a worker could have) matches the inline APU exactly: every frame's video and the whole audio stream.
    • A native build with real threads matches on every run from power-on, where the sound-program upload polls the ports about 1,350 times a frame.
  • Rendering:
    • Strips with internal depth buffers are pixel-identical to the one-pass renderer, audio included, in all 11 cases.
    • Guard zones around the strip buffer stay untouched.
    • The pixel-writer changes are identical too.
  • Wait-loop skip:
    • In 10 of the 11 cases it shifts audio timing by fractions of a sample.
    • The video is the same everywhere except Super Mario World's attract demo, where frames 366–373 differ and then converge again.
  • Sound on the Tab5: play-tested by ear in several games.
  • Builds: each commit builds on its own.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

finger563 and others added 4 commits November 29, 2026 23:08
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
@finger563
finger563 merged commit a5bc62e into main Nov 30, 2026
1 check passed
@finger563
finger563 deleted the perf/snes-speed branch November 30, 2026 13:05
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