Skip to content

SNES no longer slows to a crawl after Dark Forces / Jedi Knight: game task stacks off the LP SRAM - #52

Open
finger563 wants to merge 1 commit into
mainfrom
fix/game-stacks-off-lp-sram
Open

finger563 wants to merge 1 commit into
mainfrom
fix/game-stacks-off-lp-sram

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

After Dark Forces or Jedi Knight, every SNES game ran at a few fps, and the Tab5 dropped off USB.

Cause: the sound worker's stack landed in the P4's slow LP SRAM.

  • The internal heap includes the P4's 32 KB of LP SRAM (CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP). The allocator hands it out once the HP SRAM has no block left that fits.
  • Those two games leave the HP SRAM fragmented, so the SNES APU worker's 12 KB stack landed in LP SRAM.
  • The SPC700 / DSP code spills to its stack constantly and ran 8× slower there. The S-CPU then spent the whole frame waiting on it.
  • The busy worker, at priority 19 on core 1, also starved the USB link's tasks on that core.
APU worker's stack Cost per scanline of APU work 300 frames took
LP SRAM (forced, fresh boot) 59 µs 34.9 s
PSRAM 22–26 µs 5.0 s
HP SRAM (the old stack, when it fit) 26 µs 5.0 s

Dark Forces → Super Mario Kart: 6 fps before this change, 60 fps after.

Changes

In this repo:

  • SNES: the APU worker's stack goes in PSRAM. It parks when done and the joiner frees it. The strip zbuffer comes from the HP SRAM only, via MALLOC_CAP_DMA (all of the HP SRAM is DMA-capable and the LP SRAM is not); otherwise it uses the PSRAM path as before.
  • Other per-game tasks: created at launch, so they could land in LP SRAM the same way. These also get PSRAM stacks:
    • the Jedi Knight and MGS mixers;
    • TPM's video and audio helpers (parked, then freed by deinit());
    • the MGS port's vblank tick and scanout tasks, via MGS_PORT_TASK_STACK_CAPS.

Submodule bumps: each is an esp-cpp fork PR, and each changes only the P4 build; the S3 / box-emu builds are unchanged.

Not changed:

  • OpenJKDF2, psyz, tpm-decomp.
  • The MGS game threads, which use a static pool.
  • Tasks that only exist under debug Kconfigs.

Merge order: merge the fork PRs first, then I'll point the submodules at their merge commits.

Testing

On the Tab5, using a test build with a per-task CPU and stack-location monitor (not part of this PR):

  1. Fresh boot: every core once — NES, GB/GBC, GBA, SMS, GG, Genesis, SNES, MSX, PICO-8, Doom, Dark Forces, Jedi Knight, MGS and ScummVM. TPM could not run: this card lacks the extracted SLUS_008.84 / FILE.000.
  2. Fragmented heap: Jedi Knight → Dark Forces first, then every core again.
  3. Repeated launch and quit: Doom ×3, and SNES, Genesis, PICO-8, MGS and ScummVM ×2 each.

Results:

  • Stack placement: no task stack landed in LP SRAM in any pass. Every per-game task's stack is in PSRAM.
  • Frame rates: unchanged, e.g. Genesis 60.2 fps, SNES MMX 59.8, GBA 59.8, Dark Forces 50 (its cap).
  • Teardown: the free heap returned to the same level after each repeated launch (within ~100 B), so the park-and-free teardown leaks nothing. No crashes after the Doom fix.
  • One measurable cost: Dark Forces' music synthesis takes about 5% longer with a PSRAM stack (9.0–9.2 ms vs 8.3–8.8 ms per 23 ms chunk), about 2% of core 1. The game still holds its 50 fps cap.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

… task stacks off the LP SRAM

After Dark Forces or Jedi Knight, every SNES game ran at a few fps and the
Tab5 dropped off USB. The internal heap includes the P4's 32 KB of LP SRAM
(CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP), which the allocator hands
out once the HP SRAM has no block left that fits; those games leave it
fragmented, and the SNES APU worker's 12 KB stack landed there. The
SPC700 / DSP code (which spills to its stack constantly) ran 8x slower
(59 vs 26 us per scanline, same work), the S-CPU waited on it all frame,
and the worker, busy at priority 19 on core 1, starved the USB link's
tasks there. Forcing the stack into the LP SRAM on a fresh boot reproduces
it; in PSRAM it is as fast as in the HP SRAM.

- SNES: the APU worker's stack in PSRAM (parked, then freed by the
  joiner); the strip zbuffer from the HP SRAM only (MALLOC_CAP_DMA: all of
  it is, the LP SRAM is not), else the PSRAM path as before.
- The other tasks games create at launch, which could land there the same
  way, get PSRAM stacks too: Jedi Knight's and MGS's mixers, TPM's video
  and audio helpers (parked, freed by deinit), and the MGS port's vblank
  tick and scanout (MGS_PORT_TASK_STACK_CAPS).
- Submodules (esp-cpp forks; the P4 only, the S3 builds are unchanged):
  TheForceEngine (engine threads, mixer), gwenesis (sound task, hot
  buffers), femto8 (cart / sound tasks, audio buffers, Lua slabs), prboom
  (sound task; plus a second-launch crash fix), scummvm (mixer),
  mgs_reversing (MGS_PORT_TASK_STACK_CAPS).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Copilot AI balanced review requested due to automatic review settings December 2, 2026 07:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Timed-out helpers lose their handles and can leak task resources, while cleanup and relaunch may proceed before they stop, risking continued access to shared resources.

1 open issue
What changed in this PR

Moves per-game task stacks away from slow LP SRAM to prevent severe SNES slowdowns after Dark Forces or Jedi Knight.

Changes:

  • Places SNES, TPM, Jedi Knight, and MGS helper stacks in PSRAM.
  • Updates shutdown handling to free capability-allocated task stacks.
  • Restricts SNES strip depth buffers to HP SRAM, preserving the PSRAM fallback.
File Description
components/​tpm/​src/​tpm_engine.cpp Moves helper stacks to PSRAM and adds shutdown cleanup.
components/​snes/​src/​snes.cpp Moves the APU stack to PSRAM and frees it after shutdown.
components/​snes/​src/​snes_glue.c Restricts internal strip depth buffers to DMA-capable SRAM.
components/​mgs/​src/​mgs_platform.cpp Moves the mixer stack to PSRAM and updates deletion.
components/​mgs/​CMakeLists.txt Enables PSRAM stacks for MGS port tasks.
components/​jk/​src/​jk.cpp Configures the audio task stack for PSRAM.

Review effort: Balanced

Comment on lines +979 to +982
} else {
logger.warn("a helper task did not stop; leaving it");
}
*t = nullptr;

This branch has not been deployed

No deployments
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.

2 participants