Repository navigation
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
Open
SNES no longer slows to a crawl after Dark Forces / Jedi Knight: game task stacks off the LP SRAM#52finger563 wants to merge 1 commit into
finger563 wants to merge 1 commit into
Conversation
… 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
There was a problem hiding this comment.
🟡 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
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
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.
CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP). The allocator hands it out once the HP SRAM has no block left that fits.Dark Forces → Super Mario Kart: 6 fps before this change, 60 fps after.
Changes
In this repo:
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.deinit());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.
activeplats/activeceilingsoutlivedZ_Close().MGS_PORT_TASK_STACK_CAPShook.Not changed:
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):
SLUS_008.84/FILE.000.Results:
🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY