Skip to content

Dark Forces: every level fits on the box (JABSHIP loads; finishing NARSHADA no longer crashes) - #146

Merged
finger563 merged 9 commits into
mainfrom
perf/df-level-memory
Nov 22, 2026
Merged

finger563 merged 9 commits into
mainfrom
perf/df-level-memory

Conversation

@finger563

@finger563 finger563 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Bumps TheForceEngine to esp-cpp/TheForceEngine#3. With it, every Dark Forces level fits on the box, alongside the code overlay of #145:

  • 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 cut to their real size.
  • A save load frees the loaders' read buffers.

#145 and esp-cpp/TheForceEngine#3 are merged; this is now only the submodule bump.

Testing on the box (muted)

Before and after, PSRAM free:

#145 alone with this
Talay 1275 KB 1778 KB
finishing NARSHADA crash in the jabba1 cutscene JABSHIP loads (1329 KB)
JABSHIP directly crash (out of memory) 1762 KB

The whole campaign goes from SECBASE through ARC with the game's own LASKIP, then the ending and the credits. The lowest point is ARC at 787 KB free after the thirteen levels before it, and there was no crash or failed allocation. Details are in esp-cpp/TheForceEngine#3.

Saves and other cores:

  • A save made in ARC loads back in ARC (795 KB) and from the title (925 KB).
  • Dark Forces, Genesis, NES and Doom launch and quit on this build.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

finger563 and others added 8 commits November 21, 2026 16:52
…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
…g 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
…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
Base automatically changed from perf/core-overlay to main November 22, 2026 06:56
@github-actions

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

@github-actions

Copy link
Copy Markdown

ESP-IDF Size Report for 'Esp Box Emu'

Metric Base PR Delta
FLASH 4,639,452 bytes (73.74%) 4,640,332 bytes (73.76%) 🔺 +880 bytes (+0.02%)
DRAM 170,813 bytes (49.98%) 170,813 bytes (49.98%) 0 bytes (0.00%)
IRAM 0 bytes 0 bytes 0 bytes
RAM (DRAM+IRAM) 170,813 bytes 170,813 bytes 0 bytes (0.00%)

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 9908f4f into main Nov 22, 2026
3 checks passed
@finger563
finger563 deleted the perf/df-level-memory branch November 22, 2026 07:23
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