Repository navigation
Only the running game core's code is in PSRAM (a code overlay, as on the Tab5): +1.5 MB of PSRAM - #145
Merged
Conversation
…the Tab5) All of the firmware's code (2.4 MB, 39 MMU pages) was copied into PSRAM at boot (CONFIG_SPIRAM_FETCH_INSTRUCTIONS), every emulator core's included, although only one runs at a time. Now every core's code is linked at one instruction-bus address (0x43800000) and only the running core's is copied into PSRAM, mapped there when its game launches (components/core_overlay): - tools/core_overlay.py rename (POST_BUILD of each core's library, core_overlay_archive()): the archive's .literal* / .text* become .core_ovl.<core>.*, its .ctors the member's constructor table (run on the core's first load). COMDAT code (shared inline / template code the linker keeps one copy of) stays resident, and so does Dark Forces' allocator (its firmware-wide free()/realloc() wrappers). - overlay.ld: the OVERLAY (NOCROSSREFS: a link error if one core's code references another's). The linked bytes go into a flash store in .flash.rodata (src/store.c, sized before the link from the archives) after linking (tools/core_overlay.py image), which also checks that nothing resident reaches into the overlay except the carts, the loader and a core's own tables and code. - src/core_overlay.c: at launch a 64 KB-aligned PSRAM heap block is mapped a second time at the overlay address (esp_mmu_map_virt, executable), the core's code is copied in, the data cache written back and the instruction cache invalidated; it is unmapped and freed when the cart is gone. The rest of the firmware's code stays copied into PSRAM (the S3 has no L2 cache to hide flash's slower fetches): code in PSRAM at boot 2.4 MB -> 949 KB (15 pages); a launch maps 64 KB (GBC) to 384 KB (Dark Forces). On the box (PSRAM free; before -> after): - launcher 941 KB -> 2480 KB (largest block 656 KB -> 2432 KB) - Dark Forces: title ~705 KB -> 1841 KB; Talay 123-176 KB -> 1275 KB; a save loaded inside Talay 32 KB -> 1184 KB Speed is unchanged (the cores' code still runs from PSRAM): Talay renders in 13.0 ms (12.9 before), Genesis stays at 60 fps with 3.4-6.5 ms to spare per frame. Every core launches, runs and quits (NES, GBC, SMS, GG, MSX, Genesis, Doom, PICO-8, Dark Forces; NES, SMS and MSX twice), with and without sound; loads take 2-18 ms. The overlay's link check found a latent bug: fMSX's Z80 called the Genesis core's PatchZ80 stub (both define one; the linker resolved fMSX's call to the Genesis one), so fMSX's own (fMSX/Patch.c, the ED FE disk BIOS traps) was never linked. libmsx.a's is renamed fmsx_PatchZ80 (symbol_renames.syms, as on the Tab5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
|
✅Static analysis result - no issues found! ✅ |
…d constructors in order; archives rebuild when their tools change From a review of the overlay: - core_overlay_unload() waits 20 ms before unmapping: a core's helper task that deletes itself (the Genesis sound task, Dark Forces' MIDI thread) signals its owner just before vTaskDelete(), so its last instructions still run from the overlay. (Today a 50 ms sleep in the Genesis teardown happens to cover it; nothing guaranteed it.) - A prioritised constructor table .ctors.NNNNN (init_priority 65535 - NNNNN) is renamed by its priority, so overlay.ld's SORT by name runs it in priority order (none of the cores uses init_priority today). - A library's POST_BUILD steps (the overlay rename, fMSX's symbol renames) have no inputs, so editing core_overlay.py or symbol_renames.syms left the archives as they were: core_overlay_rebuild_on() makes one of the library's objects depend on the file (LINK_DEPENDS does not reach a static library's archive step). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…, as CI does) esp_mmu_map_virt() is new in IDF 6.1; CI builds with 6.0.1. The window is now mapped the way IDF's MMU driver does it internally: with the caches and the other CPU stopped (spi_flash_disable_interrupts_caches_and_other_cpu), from internal RAM, mmu_hal_map_region() / mmu_hal_unmap_region() and the cache bus enabled; the instruction cache is invalidated with esp_cache_msync() as before. One path for every IDF version (CI's release builds run the same code). The driver does not track the window; it lies far above everything the firmware maps. Also: pointer arithmetic on the linker symbols through uintptr_t (cppcheck subtractPointers). On the box: all eight cores launch, run and quit (NES twice), Dark Forces loads Talay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
…y.map (idf.py size parses the map)
In the map the overlay members are output sections at one address with load
addresses of their own (".core_ovl.gbc 0x43800000 0xb5e0 load address
0x43810020"), which esp-idf-size cannot parse: idf.py size, and CI's size
delta step, failed. The image step moves them (and the overlay's symbol
assignments) into <app>.core_overlay.map; the code they hold is counted where
it is in flash, in the store in .flash.rodata. idf.py size now reports flash
code 972 KB (was 2.4 MB) and flash data 3.6 MB with the store.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
ESP-IDF Size Report for 'Esp Box Emu'
FLASH uses app .bin size or json2 flash sum. RAM sums DRAM+IRAM via idf_size. Percentages shown when totals are available. |
finger563
added a commit
that referenced
this pull request
Nov 22, 2026
…RSHADA no longer crashes) (#146) * Only the running game core's code is in PSRAM (a code overlay, as on the Tab5) All of the firmware's code (2.4 MB, 39 MMU pages) was copied into PSRAM at boot (CONFIG_SPIRAM_FETCH_INSTRUCTIONS), every emulator core's included, although only one runs at a time. Now every core's code is linked at one instruction-bus address (0x43800000) and only the running core's is copied into PSRAM, mapped there when its game launches (components/core_overlay): - tools/core_overlay.py rename (POST_BUILD of each core's library, core_overlay_archive()): the archive's .literal* / .text* become .core_ovl.<core>.*, its .ctors the member's constructor table (run on the core's first load). COMDAT code (shared inline / template code the linker keeps one copy of) stays resident, and so does Dark Forces' allocator (its firmware-wide free()/realloc() wrappers). - overlay.ld: the OVERLAY (NOCROSSREFS: a link error if one core's code references another's). The linked bytes go into a flash store in .flash.rodata (src/store.c, sized before the link from the archives) after linking (tools/core_overlay.py image), which also checks that nothing resident reaches into the overlay except the carts, the loader and a core's own tables and code. - src/core_overlay.c: at launch a 64 KB-aligned PSRAM heap block is mapped a second time at the overlay address (esp_mmu_map_virt, executable), the core's code is copied in, the data cache written back and the instruction cache invalidated; it is unmapped and freed when the cart is gone. The rest of the firmware's code stays copied into PSRAM (the S3 has no L2 cache to hide flash's slower fetches): code in PSRAM at boot 2.4 MB -> 949 KB (15 pages); a launch maps 64 KB (GBC) to 384 KB (Dark Forces). On the box (PSRAM free; before -> after): - launcher 941 KB -> 2480 KB (largest block 656 KB -> 2432 KB) - Dark Forces: title ~705 KB -> 1841 KB; Talay 123-176 KB -> 1275 KB; a save loaded inside Talay 32 KB -> 1184 KB Speed is unchanged (the cores' code still runs from PSRAM): Talay renders in 13.0 ms (12.9 before), Genesis stays at 60 fps with 3.4-6.5 ms to spare per frame. Every core launches, runs and quits (NES, GBC, SMS, GG, MSX, Genesis, Doom, PICO-8, Dark Forces; NES, SMS and MSX twice), with and without sound; loads take 2-18 ms. The overlay's link check found a latent bug: fMSX's Z80 called the Genesis core's PatchZ80 stub (both define one; the linker resolved fMSX's call to the Genesis one), so fMSX's own (fMSX/Patch.c, the ED FE disk BIOS traps) was never linked. libmsx.a's is renamed fmsx_PatchZ80 (symbol_renames.syms, as on the Tab5). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY * Dark Forces: the later levels fit on the box (JABSHIP loads; finishing NARSHADA no longer crashes) TheForceEngine perf/df-level-memory 5f5b0da: only the textures a level uses are loaded (JABSHIP: 51 of 359, -1.1 MB), the level is freed before the next mission's cutscene starts, the renderer's adjoin edge list (-253 KB) and the task stacks (-96 KB per 16 tasks) are their real size, and a save load frees the loaders' read buffers. Tested with the code overlay (#145), PSRAM free: Talay 1778 KB (1275 before); NARSHADA 1431 KB, and finishing it (LASKIP) plays jabba1 and loads JABSHIP (crashed before); JABSHIP directly 1762 KB (could not load before); ARC 937 KB, a save made in ARC loads back in it (795 KB) and from the title. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY * core_overlay: unload waits for self-deleting helper tasks; prioritised constructors in order; archives rebuild when their tools change From a review of the overlay: - core_overlay_unload() waits 20 ms before unmapping: a core's helper task that deletes itself (the Genesis sound task, Dark Forces' MIDI thread) signals its owner just before vTaskDelete(), so its last instructions still run from the overlay. (Today a 50 ms sleep in the Genesis teardown happens to cover it; nothing guaranteed it.) - A prioritised constructor table .ctors.NNNNN (init_priority 65535 - NNNNN) is renamed by its priority, so overlay.ld's SORT by name runs it in priority order (none of the cores uses init_priority today). - A library's POST_BUILD steps (the overlay rename, fMSX's symbol renames) have no inputs, so editing core_overlay.py or symbol_renames.syms left the archives as they were: core_overlay_rebuild_on() makes one of the library's objects depend on the file (LINK_DEPENDS does not reach a static library's archive step). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY * core_overlay: map the window through the MMU HAL (builds with IDF 6.0, as CI does) esp_mmu_map_virt() is new in IDF 6.1; CI builds with 6.0.1. The window is now mapped the way IDF's MMU driver does it internally: with the caches and the other CPU stopped (spi_flash_disable_interrupts_caches_and_other_cpu), from internal RAM, mmu_hal_map_region() / mmu_hal_unmap_region() and the cache bus enabled; the instruction cache is invalidated with esp_cache_msync() as before. One path for every IDF version (CI's release builds run the same code). The driver does not track the window; it lies far above everything the firmware maps. Also: pointer arithmetic on the linker symbols through uintptr_t (cppcheck subtractPointers). On the box: all eight cores launch, run and quit (NES twice), Dark Forces loads Talay. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY * core_overlay: the overlay's part of the map goes to <app>.core_overlay.map (idf.py size parses the map) In the map the overlay members are output sections at one address with load addresses of their own (".core_ovl.gbc 0x43800000 0xb5e0 load address 0x43810020"), which esp-idf-size cannot parse: idf.py size, and CI's size delta step, failed. The image step moves them (and the overlay's symbol assignments) into <app>.core_overlay.map; the code they hold is counted where it is in flash, in the store in .flash.rodata. idf.py size now reports flash code 972 KB (was 2.4 MB) and flash data 3.6 MB with the store. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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
Only the running game core's code is in PSRAM now, the way the Tab5 does it. All of the firmware's code (2.4 MB, 39 MMU pages) used to be copied into PSRAM at boot (
CONFIG_SPIRAM_FETCH_INSTRUCTIONS), every emulator core's included, although only one runs at a time.Now every core's code is linked at one instruction-bus address (0x43800000), and only the running core's code is copied into PSRAM, mapped there when its game launches. The new component is
components/core_overlay:tools/core_overlay.py renameruns POST_BUILD on each core's library, throughcore_overlay_archive()..literal*and.text*sections become.core_ovl.<core>.*, and its.ctorsbecome the member's constructor table, which runs on the core's first load.free()/realloc()wrappers are always called.overlay.ldholds theOVERLAY.NOCROSSREFSmakes it a link error for one core's code to reference another's.tools/core_overlay.py imageruns after linking..flash.rodata(src/store.c, sized before the link from the archives) and marks the members NOBITS.src/core_overlay.cruns at launch.esp_mmu_map_virt.main.cpp).The rest of the firmware's code stays copied into PSRAM, because the S3 has no L2 cache to hide flash's slower fetches. That copy goes from 2.4 MB to 949 KB (15 pages). A launch maps 64 KB (GBC) up to 384 KB (Dark Forces).
The S3 differs from the Tab5 here: PSRAM data (
.ext_ram.dummy) is placed after_rodata_reserved_end, which IDF assigns insidesections.ld. So the flash store is a pre-sized array inside.flash.rodata, and the linker script doesn't override that symbol.A latent MSX bug the overlay's link check found
fMSX's Z80 called the Genesis core's
PatchZ80stub. Both cores define one, and the linker resolved fMSX's call to the Genesis copy, so fMSX's ownPatchZ80(fMSX/Patch.c, the ED FE disk-BIOS traps) was never linked.libmsx.a's copy is now renamedfmsx_PatchZ80(components/msx/symbol_renames.syms), as on the Tab5.Results on the box (PSRAM free)
Speed is unchanged, since the cores' code still runs from PSRAM:
Testing
Review fixes (c881caa)
A separate review of the overlay led to three changes:
vTaskDelete(), still inside overlay code. Today a 50 ms sleep in the Genesis teardown happens to cover that; now nothing depends on it.init_priority.core_overlay.pyorsymbol_renames.symsused to leave the archives as they were, because a POST_BUILD step has no inputs. Now one object of each library depends on those files.After these, Dark Forces, Genesis, NES and Doom launch and quit cleanly on the box.
The review also noted that the resident-reference check could be stricter. It accepts all of
main.cpp.objand any local function. That could be tightened later, but the review found nothing slipping through today.🤖 Generated with Claude Code
https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY