Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ set(EMULATOR_COMPONENTS

set(
COMPONENTS
"main esptool_py esp_lcd esp_psram task format display display_drivers monitor timer ${EMULATOR_COMPONENTS} box-emu gui menu settings emulink"
"main esptool_py esp_lcd esp_psram task format display display_drivers monitor timer ${EMULATOR_COMPONENTS} core_overlay box-emu gui menu settings emulink"
CACHE STRING
"List of components to include"
FORCE
Expand All @@ -111,6 +111,16 @@ project(esp-box-emu)

set(CMAKE_CXX_STANDARD 20)

# The game cores' code overlay (components/core_overlay): after linking, the
# cores' relocated code goes into its flash store (before the .bin is made from
# the ELF), and nothing resident may reach into the overlay
idf_build_get_property(python PYTHON)
add_custom_command(TARGET ${CMAKE_PROJECT_NAME}.elf POST_BUILD
COMMAND ${python} ${CMAKE_CURRENT_LIST_DIR}/components/core_overlay/tools/core_overlay.py image ${CMAKE_OBJCOPY}
$<TARGET_FILE:${CMAKE_PROJECT_NAME}.elf> ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map
COMMENT "Storing the game cores' code overlay in flash"
VERBATIM)

# The updater app (updater/) belongs in ota_1 (see partitions.csv): build it
# first (idf.py -C updater build) and `idf.py flash`, flash_args and
# flasher_args.json (the release package) put it there with the emulator.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ captured play session doubles as a profile:
block above a threshold (labelled with the task whose stack it is), each
registered heap region's used/free/largest, and every task's stack high-water
mark (`stack_free_min`, repeated at quit after a full play session).
- `core_overlay: CORE: N KB of code loaded in N ms (N KB of PSRAM, N constructors)`
/ `core_overlay: CORE: unloaded` — the game cores' code overlay
(`components/core_overlay`): every core's code is linked at one address and
only the running core's is in PSRAM (the rest of the firmware's code is
copied into PSRAM at boot), so the other cores' ~1.5 MB of code stays in
flash. The build prints the store and each core's code size
(`core_overlay: N KB of core code stored in flash ...`) and fails if
resident code or data reaches into the overlay.
- `[genesis mem] NAME: N bytes -> INTERNAL|PSRAM` — where each big emulator
buffer landed (the Genesis gets noticeably slower when its hot buffers fall
back to PSRAM).
Expand Down
32 changes: 32 additions & 0 deletions components/core_overlay/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# The game cores' code overlay (overlay.ld, include/core_overlay.h): the code
# of the cores that are not running stays in flash; the one a game needs is
# copied into PSRAM when it launches.
idf_component_register(
SRCS "src/core_overlay.c" "src/store.c"
INCLUDE_DIRS "include"
PRIV_REQUIRES esp_mm esp_hw_support hal spi_flash heap log esp_timer freertos)

# The flash store's size (src/store.c) from the cores' archives, after their
# code was renamed into the overlay (core_overlay_archive(), POST_BUILD)
set(cores nes gbc sms msx genesis doom darkforces pico8)
idf_build_get_property(python PYTHON)
idf_build_get_property(build_components BUILD_COMPONENTS)
set(libs "")
set(archives "")
foreach(core ${cores})
if(core IN_LIST build_components)
idf_component_get_property(lib ${core} COMPONENT_LIB)
list(APPEND libs ${lib})
list(APPEND archives "$<TARGET_FILE:${lib}>")
endif()
endforeach()
set(size_h "${CMAKE_CURRENT_BINARY_DIR}/core_ovl_store_size.h")
add_custom_command(OUTPUT "${size_h}"
COMMAND ${python} "${CMAKE_CURRENT_LIST_DIR}/tools/core_overlay.py" size ${CMAKE_OBJCOPY} "${size_h}" ${archives}
DEPENDS ${libs} "${CMAKE_CURRENT_LIST_DIR}/tools/core_overlay.py"
COMMENT "Sizing the core code overlay's flash store"
VERBATIM)
target_sources(${COMPONENT_LIB} PRIVATE "${size_h}")
target_include_directories(${COMPONENT_LIB} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")

target_linker_script(${COMPONENT_LIB} INTERFACE "${CMAKE_CURRENT_LIST_DIR}/overlay.ld")
34 changes: 34 additions & 0 deletions components/core_overlay/include/core_overlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/// The game cores' code overlay (overlay.ld): every core's code is linked at
/// one instruction-bus address and the core a game needs is copied there,
/// into PSRAM, when it launches; the other cores' code stays in flash.
///
/// Make a game core's code runnable: maps a PSRAM block the size of the
/// core's code at the overlay address, copies the core's code into it from
/// flash and, on the core's first load, runs its static constructors. Call it
/// before any of the core's code runs (before its cart is made) and only
/// while no other core's code runs; a core that is loaded already stays (any
/// other is unloaded first).
///
/// `core` is the overlay member's name: nes, gbc, sms, msx, genesis, doom,
/// darkforces or pico8. Returns false for any other name, or when the PSRAM
/// for the code cannot be had.
bool core_overlay_load(const char *core);

/// Unmap the loaded core's code and free its PSRAM (after its cart is gone:
/// nothing of the core may run any more, no task, callback or interrupt).
void core_overlay_unload(void);

/// The core whose code is loaded ("" for none).
const char *core_overlay_loaded(void);

#ifdef __cplusplus
}
#endif
110 changes: 110 additions & 0 deletions components/core_overlay/overlay.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* The game cores' code overlay (include/core_overlay.h): only one game runs at
* a time, so every core's code is linked at the same instruction-bus address
* and the one a game needs is copied into PSRAM there when it launches
* (src/core_overlay.c). The rest of the firmware's code is copied into PSRAM
* at boot as before (CONFIG_SPIRAM_FETCH_INSTRUCTIONS); the cores that are not
* running are in flash only.
*
* tools/core_overlay.py rename (POST_BUILD of each core's library) renames a
* core archive's code to .core_ovl.<core>.* and its constructor tables to
* .core_ovl_ctors_<core>*. A member holds its constructor table first (run on
* the core's first load), then its code; literals stay in front of the code
* that loads them (each object's .literal.* precede its .text.*).
*
* Linked bytes: the bootloader only loads an image's flash and internal RAM
* segments, so after linking tools/core_overlay.py image copies each member's
* relocated bytes into the flash store (src/store.c, in .flash.rodata), marks
* the member NOBITS, and checks that nothing resident reaches into the
* overlay. NOCROSSREFS: a link error if one core's code references another's.
*
* 0x43800000 (MMU linear page 384) is far above everything IDF maps at boot
* (flash .text and .rodata, then all of the PSRAM) and anything mapped later
* (src/core_overlay.c map_window()); its data-bus alias (0x3D800000) is never
* used. */
MEMORY
{
core_ovl_seg (RX) : org = 0x43800000, len = 0x800000
}

SECTIONS
{
OVERLAY : NOCROSSREFS
{
.core_ovl.nes
{
_core_ovl_ctors_nes_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_nes.*))) KEEP(*(.core_ovl_ctors_nes))
_core_ovl_ctors_nes_end = ABSOLUTE(.);
*(.core_ovl.nes.*)
. = ALIGN(16);
}
.core_ovl.gbc
{
_core_ovl_ctors_gbc_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_gbc.*))) KEEP(*(.core_ovl_ctors_gbc))
_core_ovl_ctors_gbc_end = ABSOLUTE(.);
*(.core_ovl.gbc.*)
. = ALIGN(16);
}
.core_ovl.sms
{
_core_ovl_ctors_sms_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_sms.*))) KEEP(*(.core_ovl_ctors_sms))
_core_ovl_ctors_sms_end = ABSOLUTE(.);
*(.core_ovl.sms.*)
. = ALIGN(16);
}
.core_ovl.msx
{
_core_ovl_ctors_msx_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_msx.*))) KEEP(*(.core_ovl_ctors_msx))
_core_ovl_ctors_msx_end = ABSOLUTE(.);
*(.core_ovl.msx.*)
. = ALIGN(16);
}
.core_ovl.genesis
{
_core_ovl_ctors_genesis_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_genesis.*))) KEEP(*(.core_ovl_ctors_genesis))
_core_ovl_ctors_genesis_end = ABSOLUTE(.);
*(.core_ovl.genesis.*)
. = ALIGN(16);
}
.core_ovl.doom
{
_core_ovl_ctors_doom_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_doom.*))) KEEP(*(.core_ovl_ctors_doom))
_core_ovl_ctors_doom_end = ABSOLUTE(.);
*(.core_ovl.doom.*)
. = ALIGN(16);
}
.core_ovl.darkforces
{
_core_ovl_ctors_darkforces_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_darkforces.*))) KEEP(*(.core_ovl_ctors_darkforces))
_core_ovl_ctors_darkforces_end = ABSOLUTE(.);
*(.core_ovl.darkforces.*)
. = ALIGN(16);
}
.core_ovl.pico8
{
_core_ovl_ctors_pico8_start = ABSOLUTE(.);
KEEP(*(SORT(.core_ovl_ctors_pico8.*))) KEEP(*(.core_ovl_ctors_pico8))
_core_ovl_ctors_pico8_end = ABSOLUTE(.);
*(.core_ovl.pico8.*)
. = ALIGN(16);
}
} > core_ovl_seg
_core_ovl_start = ADDR(.core_ovl.nes);
/* after an OVERLAY, . is its start plus its largest member */
_core_ovl_end = ALIGN(., 16);
}
_core_ovl_nes_size = SIZEOF(.core_ovl.nes);
_core_ovl_gbc_size = SIZEOF(.core_ovl.gbc);
_core_ovl_sms_size = SIZEOF(.core_ovl.sms);
_core_ovl_msx_size = SIZEOF(.core_ovl.msx);
_core_ovl_genesis_size = SIZEOF(.core_ovl.genesis);
_core_ovl_doom_size = SIZEOF(.core_ovl.doom);
_core_ovl_darkforces_size = SIZEOF(.core_ovl.darkforces);
_core_ovl_pico8_size = SIZEOF(.core_ovl.pico8);
ASSERT(_core_ovl_start % 0x10000 == 0, "the core overlay must start on an MMU page")
28 changes: 28 additions & 0 deletions components/core_overlay/project_include.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# core_overlay_archive(<core> [keep-regex ...]): in a game core's component
# CMakeLists.txt, after idf_component_register(): put the core's code into the
# code overlay (overlay.ld; tools/core_overlay.py rename, POST_BUILD of the
# component's library). keep-regex: code that stays resident although it is in
# the core's archive (matched against the function name; see the script).
set(CORE_OVERLAY_TOOL "${CMAKE_CURRENT_LIST_DIR}/tools/core_overlay.py")

function(core_overlay_archive core)
idf_build_get_property(python PYTHON)
set(keep "")
foreach(k ${ARGN})
list(APPEND keep "--keep=${k}")
endforeach()
add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD
COMMAND ${python} ${CORE_OVERLAY_TOOL} rename ${CMAKE_OBJCOPY} $<TARGET_FILE:${COMPONENT_LIB}> ${core} ${keep}
COMMENT "Moving the ${core} core's code into the code overlay"
VERBATIM)
core_overlay_rebuild_on(${CORE_OVERLAY_TOOL})
endfunction()

# core_overlay_rebuild_on(<file>): a POST_BUILD step of a library has no inputs (and
# LINK_DEPENDS does not reach a static library's archive step): rebuild the calling
# component's library when <file> changes, by making one of its objects depend on it
function(core_overlay_rebuild_on file)
get_target_property(sources ${COMPONENT_LIB} SOURCES)
list(GET sources 0 first)
set_property(SOURCE ${first} APPEND PROPERTY OBJECT_DEPENDS ${file})
endfunction()
Loading
Loading