Skip to content

Only the running game core's code is in PSRAM (a code overlay, as on the Tab5): +1.5 MB of PSRAM - #145

Merged
finger563 merged 4 commits into
mainfrom
perf/core-overlay
Nov 22, 2026
Merged

finger563 merged 4 commits into
mainfrom
perf/core-overlay

Conversation

@finger563

@finger563 finger563 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 rename runs POST_BUILD on each core's library, through core_overlay_archive().
    • The archive's .literal* and .text* sections become .core_ovl.<core>.*, and its .ctors become the member's constructor table, which runs on the core's first load.
    • COMDAT code stays resident: that's shared inline and template code, of which the linker keeps one copy for the whole firmware. Dark Forces' allocator also stays resident, because its firmware-wide free()/realloc() wrappers are always called.
  • overlay.ld holds the OVERLAY. NOCROSSREFS makes it a link error for one core's code to reference another's.
  • tools/core_overlay.py image runs after linking.
    • It copies the linked bytes into a flash store in .flash.rodata (src/store.c, sized before the link from the archives) and marks the members NOBITS.
    • It also checks that nothing resident reaches into the overlay except the carts, the loader, and a core's own tables and code. The build fails otherwise.
  • src/core_overlay.c runs at launch.
    • It allocates a 64 KB-aligned PSRAM heap block and maps it a second time at the overlay address, executable, with esp_mmu_map_virt.
    • It copies the core's code in, writes back the data cache and invalidates the instruction cache.
    • It unmaps and frees the block once the cart is gone (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 inside sections.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 PatchZ80 stub. Both cores define one, and the linker resolved fMSX's call to the Genesis copy, so fMSX's own PatchZ80 (fMSX/Patch.c, the ED FE disk-BIOS traps) was never linked. libmsx.a's copy is now renamed fmsx_PatchZ80 (components/msx/symbol_renames.syms), as on the Tab5.

Results on the box (PSRAM free)

before after
launcher 941 KB (largest 656 KB) 2480 KB (largest 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, since the cores' code still runs from PSRAM:

  • Talay renders in 13.0 ms per frame (12.9 ms before).
  • Genesis stays at 60 fps, with 3.4–6.5 ms to spare per frame.
  • Dark Forces' music synthesis in Talay is unchanged: about 22 ms per 23.2 ms chunk.

Testing

  • Every core launches, runs and quits: NES, GBC, SMS, GG, MSX, Genesis, Doom, PICO-8 and Dark Forces. NES, SMS and MSX ran twice, which also checks that constructors run only on the first load.
  • Dark Forces, Genesis, Doom and NES also ran with sound on.
  • Loads take 2–18 ms, and the PSRAM returns after each quit.
  • The firmware image grows from 4.28 to 4.69 MB. The store is sized before the link, from archives that still contain the code the link later drops, so about 400 KB of it is unused. It could be trimmed later.

Review fixes (c881caa)

A separate review of the overlay led to three changes:

  • Unload waits 20 ms before unmapping. A core's self-deleting helper task (the Genesis sound task, Dark Forces' MIDI thread) signals its owner a few instructions before vTaskDelete(), still inside overlay code. Today a 50 ms sleep in the Genesis teardown happens to cover that; now nothing depends on it.
  • Prioritised constructor tables are named by their priority, so they run in priority order. This is latent: no core uses init_priority.
  • Archives rebuild when their tools change. Editing core_overlay.py or symbol_renames.syms used 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.obj and 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

…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
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

✅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
finger563 and others added 2 commits November 21, 2026 22:42
…, 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
@github-actions

Copy link
Copy Markdown

ESP-IDF Size Report for 'Esp Box Emu'

Metric Base PR Delta
FLASH 4,232,628 bytes (67.28%) 4,639,452 bytes (73.74%) 🔺 +406,824 bytes (+6.46%)
DRAM 170,069 bytes (49.76%) 170,813 bytes (49.98%) 🔺 +744 bytes (+0.22%)
IRAM 0 bytes 0 bytes 0 bytes
RAM (DRAM+IRAM) 170,069 bytes 170,813 bytes 🔺 +744 bytes (+0.22%)

FLASH uses app .bin size or json2 flash sum. RAM sums DRAM+IRAM via idf_size. Percentages shown when totals are available.
DRAM/IRAM usage does not include memory used by the heap allocator at runtime.
This report was generated by esp-idf-size-delta.

@finger563
finger563 merged commit 2a41a25 into main Nov 22, 2026
3 checks passed
@finger563
finger563 deleted the perf/core-overlay branch November 22, 2026 06:56
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>
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