diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e7efbee..7dd1cae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,7 +22,10 @@ jobs: uses: actions/checkout@v4 - name: Checkout the game submodules (without their desktop-only nested submodules) - run: git submodule update --init components/jk/OpenJKDF2 components/mgs/mgs_reversing components/mgs/psyz components/tpm/tpm-decomp components/genesis/gwenesis components/pico8/femto8 + run: | + git submodule update --init components/jk/OpenJKDF2 components/mgs/mgs_reversing components/mgs/psyz components/tpm/tpm-decomp components/genesis/gwenesis components/pico8/femto8 components/nes/nofrendo components/gbc/gnuboy components/msx/fmsx components/sms/smsplus components/doom/prboom components/darkforces/TheForceEngine + # ScummVM's history is large: only the pinned commit + git submodule update --init --depth 1 components/scummvm/scummvm - name: Build uses: espressif/esp-idf-ci-action@v1 diff --git a/.gitmodules b/.gitmodules index 460165d..27fa31b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -21,3 +21,31 @@ path = components/pico8/femto8 url = https://github.com/esp-cpp/femto8.git branch = esp32 +[submodule "components/nes/nofrendo"] + path = components/nes/nofrendo + url = https://github.com/esp-cpp/nofrendo.git + branch = main +[submodule "components/gbc/gnuboy"] + path = components/gbc/gnuboy + url = https://github.com/esp-cpp/gnuboy.git + branch = main +[submodule "components/msx/fmsx"] + path = components/msx/fmsx + url = https://github.com/esp-cpp/fmsx.git + branch = main +[submodule "components/sms/smsplus"] + path = components/sms/smsplus + url = https://github.com/esp-cpp/smsplus.git + branch = main +[submodule "components/doom/prboom"] + path = components/doom/prboom + url = https://github.com/esp-cpp/prboom.git + branch = main +[submodule "components/darkforces/TheForceEngine"] + path = components/darkforces/TheForceEngine + url = https://github.com/esp-cpp/TheForceEngine.git + branch = main +[submodule "components/scummvm/scummvm"] + path = components/scummvm/scummvm + url = https://github.com/esp-cpp/scummvm.git + branch = esp32p4 diff --git a/CMakeLists.txt b/CMakeLists.txt index e50c398..653aa19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,21 @@ set(TPM_COMPONENTS tpm) ### PICO-8 (femto8, shared with esp-box-emu) ### set(PICO8_COMPONENTS pico8) -### Dark Forces (The Force Engine port, shared with esp-box-emu) ### -# set(DARK_FORCES_COMPONENTS "dark_forces") +### Cores shared with esp-box-emu (each core + its port/ glue a submodule) ### +### NES (nofrendo) ### +set(NES_COMPONENTS nes) +### Game Boy / Color (gnuboy) ### +set(GBC_COMPONENTS gbc) +### Master System / Game Gear (SMS Plus) ### +set(SMS_COMPONENTS sms) +### MSX (fMSX) ### +set(MSX_COMPONENTS msx) +### Doom (PrBoom) ### +set(DOOM_COMPONENTS doom) +### Dark Forces (The Force Engine) ### +set(DARK_FORCES_COMPONENTS darkforces) +### ScummVM (esp-cpp/scummvm: Star Trek: 25th Anniversary / Judgment Rites) ### +set(SCUMMVM_COMPONENTS scummvm) if(JK_COMPONENTS) add_compile_definitions(ENABLE_JK) @@ -55,8 +68,26 @@ if(PICO8_COMPONENTS) add_compile_definitions(ENABLE_PICO8) endif() +if(NES_COMPONENTS) + add_compile_definitions(ENABLE_NES) +endif() +if(GBC_COMPONENTS) + add_compile_definitions(ENABLE_GBC) +endif() +if(SMS_COMPONENTS) + add_compile_definitions(ENABLE_SMS) +endif() +if(MSX_COMPONENTS) + add_compile_definitions(ENABLE_MSX) +endif() +if(DOOM_COMPONENTS) + add_compile_definitions(ENABLE_DOOM) +endif() if(DARK_FORCES_COMPONENTS) - add_compile_definitions(ENABLE_DARK_FORCES) + add_compile_definitions(ENABLE_DARKFORCES) +endif() +if(SCUMMVM_COMPONENTS) + add_compile_definitions(ENABLE_SCUMMVM) endif() set(GAME_COMPONENTS @@ -67,13 +98,19 @@ set(GAME_COMPONENTS ${MGS_COMPONENTS} ${TPM_COMPONENTS} ${PICO8_COMPONENTS} + ${NES_COMPONENTS} + ${GBC_COMPONENTS} + ${SMS_COMPONENTS} + ${MSX_COMPONENTS} + ${DOOM_COMPONENTS} ${DARK_FORCES_COMPONENTS} + ${SCUMMVM_COMPONENTS} ) set( COMPONENTS main esptool_py esp_lcd esp_psram task format display display_drivers monitor timer - ${GAME_COMPONENTS} tab5-emu gui menu rom_info statistics settings devlink + ${GAME_COMPONENTS} tab5-emu pool_allocator gui menu rom_info statistics settings devlink CACHE STRING "List of components to include" FORCE diff --git a/README.md b/README.md index dea863f..3170799 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,16 @@ plan and its history are in [docs/PLAN.md](docs/PLAN.md). | Genesis / Mega Drive | gwenesis (shared with esp-box-emu) | full speed | | SNES | snes9x2005 | full speed | | Game Boy Advance | gpSP | close to full speed; heavy scenes drop a few frames | +| NES | nofrendo (shared with esp-box-emu) | full speed (~3 ms a frame) | +| Game Boy / Color | gnuboy (shared with esp-box-emu) | full speed | +| Master System / Game Gear | SMS Plus (shared with esp-box-emu) | full speed | +| MSX | fMSX (shared with esp-box-emu) | runs (Metal Gear) | +| Doom | PrBoom (shared with esp-box-emu) | ~35 fps (shareware demo) | +| Dark Forces | The Force Engine (shared with esp-box-emu) | ~50 fps in the first level | + +The cores shared with esp-box-emu are submodules (the core plus its `port/` +glue, written against `BoxEmu`, which the Tab5 aliases to `Tab5Emu`), so a +fix in one firmware reaches the other. https://github.com/user-attachments/assets/641a64b0-b3af-429b-8582-a38fc9a5cdad @@ -175,6 +185,9 @@ components/ mgs/ Metal Gear Solid: mgs_reversing + psyz submodules + the Tab5 platform layer tpm/ The Phantom Menace: tpm-decomp submodule (static recompiler + runtime) + glue gba/ genesis/ snes/ the emulator cores and their glue + nes/ gbc/ sms/ msx/ doom/ darkforces/ pico8/ + the cores shared with esp-box-emu (submodules: core + port/ glue) + pool_allocator/ the allocator MSX, Doom and Dark Forces run in their 4 MB block docs/PLAN.md the Jedi Knight plan and history ``` @@ -197,6 +210,9 @@ docs/PLAN.md the Jedi Knight plan and history mgs/ # Metal Gear Solid, extracted from your own US disc STAGE.DIR RADIO.DAT FACE.DAT ZMOVIE.STR VOX.DAT DEMO.DAT BRF.DAT # (components/mgs/mgs_reversing/port/extract_disc.py) + doom/ # doom1.wad, doom.wad, doom2.wad, ... (the IWADs) + darkforces/ # your Dark Forces install: DARK.GOB, SOUNDS.GOB, TEXTURES.GOB, SPRITES.GOB, LFDs + nes/ gbc/ sms/ gg/ msx/ ... # cartridge ROMs (any folder: metadata.csv has the paths) tpm/ # The Phantom Menace, USA Rev 1: the .bin of a bin/cue rip Star Wars - Episode I - The Phantom Menace (USA) (Rev 1).bin saves/ # save states and their screenshots; the PlayStation @@ -206,8 +222,10 @@ docs/PLAN.md the Jedi Knight plan and history Example `metadata.csv` (one line per game: rom path, boxart, name; the platform comes from the path: `.md`/`.gen` Genesis, `.sfc`/`.smc` SNES, -`.gba` Game Boy Advance, `jk.cd` Jedi Knight, `STAGE.DIR` Metal Gear Solid, -`SLUS_008.84` The Phantom Menace): +`.gba` Game Boy Advance, `.nes` NES, `.gb`/`.gbc` Game Boy / Color, `.sms` +Master System, `.gg` Game Gear, `.rom` MSX, `.wad` Doom, `DARK.GOB` Dark +Forces, `.p8`/`.p8.png` PICO-8, `jk.cd` Jedi Knight, `STAGE.DIR` Metal Gear +Solid, `SLUS_008.84` The Phantom Menace): ``` jk/jk.cd, boxart/jk.jpg, Star Wars: Jedi Knight - Dark Forces II @@ -277,8 +295,8 @@ console's games. Tapping the console above the boxart (or X on a gamepad, F on a keyboard) opens the console picker, which shows only one console's games, or all of them again; an outline on the console means the list is filtered, and left / right then move the filter to the next console. -`emulink filter ` (snes, gba, genesis, playstation, pc, pico8 or -all) does the same over USB. +`emulink filter ` (nes, snes, gb, gbc, gba, sms, gg, genesis, msx, +playstation, pc, pico8 or all) does the same over USB. Jedi Knight gets the full keyboard and mouse look; on a gamepad the left stick strafes, the right stick looks, R = fire, L = secondary fire, A = diff --git a/components/darkforces/CMakeLists.txt b/components/darkforces/CMakeLists.txt new file mode 100644 index 0000000..ae56e68 --- /dev/null +++ b/components/darkforces/CMakeLists.txt @@ -0,0 +1,111 @@ +# Dark Forces for esp-box-emu, built on The Force Engine (TFE). +# +# From the esp-cpp/TheForceEngine submodule: TheForceEngine/tfe is a subset of +# BSzili's Amiga branch of TFE (based on TFE v1.09.410) with the __AMIGA__ +# platform guards renamed to TFE_ESPBOX (see TheForceEngine/VENDOR.md); +# TheForceEngine/port/platform provides the ESP32 implementations of the +# platform interfaces (system timers, render backend, audio, threads, memory +# regions, stubs). +set(TFE_SRC_DIRS + "TheForceEngine/tfe/TFE_System" + "TheForceEngine/tfe/TFE_Memory" + "TheForceEngine/tfe/TFE_FileSystem" + "TheForceEngine/tfe/TFE_Archive" + "TheForceEngine/tfe/TFE_Asset" + "TheForceEngine/tfe/TFE_Audio" + "TheForceEngine/tfe/TFE_Audio/MidiSynth" + "TheForceEngine/tfe/TFE_Input" + "TheForceEngine/tfe/TFE_Settings" + "TheForceEngine/tfe/TFE_Game" + "TheForceEngine/tfe/TFE_Outlaws" + "TheForceEngine/tfe/TFE_Jedi/Collision" + "TheForceEngine/tfe/TFE_Jedi/IMuse" + "TheForceEngine/tfe/TFE_Jedi/InfSystem" + "TheForceEngine/tfe/TFE_Jedi/Level" + "TheForceEngine/tfe/TFE_Jedi/Math" + "TheForceEngine/tfe/TFE_Jedi/Memory" + "TheForceEngine/tfe/TFE_Jedi/Serialization" + "TheForceEngine/tfe/TFE_Jedi/Task" + "TheForceEngine/tfe/TFE_Jedi/Renderer" + "TheForceEngine/tfe/TFE_Jedi/Renderer/RClassic_Fixed" + "TheForceEngine/tfe/TFE_Jedi/Renderer/RClassic_Fixed/robj3d_fixed" + "TheForceEngine/tfe/TFE_DarkForces" + "TheForceEngine/tfe/TFE_DarkForces/Actor" + "TheForceEngine/tfe/TFE_DarkForces/GameUI" + "TheForceEngine/tfe/TFE_DarkForces/Landru" + ) + +idf_component_register( + INCLUDE_DIRS "TheForceEngine/port/include" + SRC_DIRS "TheForceEngine/port" "TheForceEngine/port/platform" ${TFE_SRC_DIRS} + PRIV_INCLUDE_DIRS "TheForceEngine/tfe" "TheForceEngine/port/platform" + REQUIRES tab5-emu statistics pool_allocator esp_timer pthread heap esp_system + ) + +target_compile_definitions(${COMPONENT_LIB} PRIVATE + TFE_ESPBOX=1 + TFE_ESPBOX_BASE_DIR="/sdcard/darkforces/" + ) + +target_compile_options(${COMPONENT_LIB} PRIVATE + -Os + -Wno-error + -Wno-type-limits + -Wno-ignored-qualifiers + -Wno-unused-variable + -Wno-unused-but-set-variable + -Wno-unused-function + -Wno-switch + -Wno-sign-compare + -Wno-missing-field-initializers + -Wno-format + -Wno-format-truncation + -Wno-error=maybe-uninitialized + -Wno-error=array-bounds + -Wno-error=stringop-overflow + -Wno-error=stringop-truncation + -Wno-error=deprecated-declarations + -Wno-error=narrowing + -Wno-error=address + -Wno-error=class-memaccess + -Wno-error=reorder + -Wno-error=parentheses + -Wno-error=misleading-indentation + -Wno-error=int-in-bool-context + -Wno-error=sequence-point + -Wno-error=strict-aliasing + -Wno-error=char-subscripts + -Wno-error=unused-value + -Wno-error=implicit-fallthrough + $<$:-Wno-error=incompatible-pointer-types> + $<$:-Wno-error=discarded-qualifiers> + ) + +# The renderer and the OPL3 synthesizer are the hot paths: optimize those for speed. +file(GLOB_RECURSE DF_HOT_SOURCES + "${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/tfe/TFE_Jedi/Renderer/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/tfe/TFE_Jedi/Math/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/tfe/TFE_Audio/MidiSynth/*.c" + "${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/tfe/TFE_Audio/MidiSynth/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/tfe/TFE_Jedi/IMuse/imDigitalSound.cpp" + ) +set_source_files_properties(${DF_HOT_SOURCES} PROPERTIES COMPILE_OPTIONS "-O2") + +# Everything Dark Forces allocates lives in BoxEmu's 4MB ROM block while it runs +# (TheForceEngine/port/platform/esp_alloc.cpp): rename the allocation functions referenced by +# this library to the df_* replacements, and route free()/realloc() of block +# pointers made anywhere in the firmware back to the block. +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --redefine-syms=${CMAKE_CURRENT_SOURCE_DIR}/TheForceEngine/port/alloc_redirect.syms $ + COMMENT "Redirecting Dark Forces allocations to the ROM block" + VERBATIM) +target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=free" "-Wl,--wrap=realloc") +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the darkforces core's .bss and .data to PSRAM" + VERBATIM) diff --git a/components/darkforces/TheForceEngine b/components/darkforces/TheForceEngine new file mode 160000 index 0000000..03417ff --- /dev/null +++ b/components/darkforces/TheForceEngine @@ -0,0 +1 @@ +Subproject commit 03417ff1cf08860082f52ff4a51c1da72d48f0ac diff --git a/components/doom/CMakeLists.txt b/components/doom/CMakeLists.txt new file mode 100644 index 0000000..75d3fe3 --- /dev/null +++ b/components/doom/CMakeLists.txt @@ -0,0 +1,31 @@ +# Doom: PrBoom, from the esp-cpp/prboom submodule (the engine in +# prboom/prboom, its glue in prboom/port) +idf_component_register( + INCLUDE_DIRS "prboom/port/include" + SRC_DIRS "prboom/port" "prboom/prboom" + PRIV_INCLUDE_DIRS "prboom" "prboom/prboom" + REQUIRES tab5-emu shared_memory + ) +target_compile_options(${COMPONENT_LIB} PRIVATE + -Wno-error=address + -Wno-error=sizeof-pointer-memaccess + -Wno-misleading-indentation + -Wno-format-overflow + -Wno-char-subscripts + $<$:-std=gnu17> + $<$:-Wno-discarded-qualifiers> + $<$:-Wno-error=incompatible-pointer-types> + $<$:-Wno-error=compare-distinct-pointer-types> + -Wno-missing-field-initializers + -DHAVE_CONFIG_H + -O2 +) +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the doom core's .bss and .data to PSRAM" + VERBATIM) diff --git a/components/doom/prboom b/components/doom/prboom new file mode 160000 index 0000000..b18131f --- /dev/null +++ b/components/doom/prboom @@ -0,0 +1 @@ +Subproject commit b18131f69c42c55164c2cec1073157629a1a3a79 diff --git a/components/gbc/CMakeLists.txt b/components/gbc/CMakeLists.txt new file mode 100644 index 0000000..6bb8737 --- /dev/null +++ b/components/gbc/CMakeLists.txt @@ -0,0 +1,26 @@ +# Game Boy / Color: gnuboy, from the esp-cpp/gnuboy submodule (the core in +# gnuboy/gnuboy, its glue in gnuboy/port) +idf_component_register( + INCLUDE_DIRS "gnuboy/port/include" + SRC_DIRS "gnuboy/port" "gnuboy/gnuboy/src" + PRIV_INCLUDE_DIRS "gnuboy/gnuboy/include" + REQUIRES "tab5-emu" "statistics" "shared_memory" + ) + +target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-misleading-indentation -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable $<$:-Wno-discarded-qualifiers>) +target_compile_definitions(${COMPONENT_LIB} PRIVATE GNUBOY_NO_MINIZIP GNUBOY_NO_SCREENSHOT IS_LITTLE_ENDIAN) +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the gbc core's .bss and .data to PSRAM" + VERBATIM) +# globals other cores in this firmware define too (the box doesn't build +# those): renamed in this library, its glue included +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --redefine-syms=${CMAKE_CURRENT_LIST_DIR}/symbol_renames.syms $ + COMMENT "Renaming the globals another core also defines" + VERBATIM) diff --git a/components/gbc/gnuboy b/components/gbc/gnuboy new file mode 160000 index 0000000..4585d0a --- /dev/null +++ b/components/gbc/gnuboy @@ -0,0 +1 @@ +Subproject commit 4585d0a31dea75b9e196055e30de380381e9aa40 diff --git a/components/gbc/symbol_renames.syms b/components/gbc/symbol_renames.syms new file mode 100644 index 0000000..325b788 --- /dev/null +++ b/components/gbc/symbol_renames.syms @@ -0,0 +1,3 @@ +hw_reset gnuboy_hw_reset +rtc gnuboy_rtc +sound_off gnuboy_sound_off diff --git a/components/jk/OpenJKDF2 b/components/jk/OpenJKDF2 index 5b5c037..fd47e34 160000 --- a/components/jk/OpenJKDF2 +++ b/components/jk/OpenJKDF2 @@ -1 +1 @@ -Subproject commit 5b5c0375cfe6cc15c1be4b23445e24a7cad3616a +Subproject commit fd47e3425aa22f5c2b983ac19e6d8894090aff51 diff --git a/components/jk/src/jk.cpp b/components/jk/src/jk.cpp index 73dd0a5..274b43c 100644 --- a/components/jk/src/jk.cpp +++ b/components/jk/src/jk.cpp @@ -563,7 +563,7 @@ size_t g_pool_allocs = 0, g_pool_fallbacks = 0; inline bool pool_owns(const void *p) { return g_pool_base && p >= g_pool_base && p < g_pool_base + POOL_BYTES; } -void *pool_alloc(size_t size) { +void *jk_pool_alloc(size_t size) { size_t c = 0; while (c < POOL_NUM_CLASSES && POOL_CLASSES[c] < size) c++; if (c == POOL_NUM_CLASSES) return nullptr; @@ -582,11 +582,11 @@ void *pool_alloc(size_t size) { portEXIT_CRITICAL(&g_pool_lock); return blk; } -size_t pool_block_size(const void *p) { +size_t jk_pool_block_size(const void *p) { const uint32_t *hdr = static_cast(p) - 2; return hdr[1] == POOL_MAGIC && hdr[0] < POOL_NUM_CLASSES ? POOL_CLASSES[hdr[0]] : 0; } -void pool_free(void *p) { +void jk_pool_free(void *p) { const uint32_t *hdr = static_cast(p) - 2; if (hdr[1] != POOL_MAGIC || hdr[0] >= POOL_NUM_CLASSES) return; // not ours (corrupt header) portENTER_CRITICAL(&g_pool_lock); @@ -707,7 +707,7 @@ size_t arena_largest_free() { // evicting every frame. Whatever else in the firmware allocates while the // engine runs must do so up front (the frame buffers are) or cope with NULL. void *jk_esp_malloc_site(size_t size, const void *site) { - void *p = size <= POOL_CLASSES[POOL_NUM_CLASSES - 1] ? pool_alloc(size) : nullptr; + void *p = size <= POOL_CLASSES[POOL_NUM_CLASSES - 1] ? jk_pool_alloc(size) : nullptr; if (!p) p = arena_alloc(size); if (!p) p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); if (p) memset(p, 0, size); @@ -725,14 +725,14 @@ void *jk_esp_realloc_site(void *ptr, size_t size, const void *site) { #endif void *p; if (ptr && pool_owns(ptr)) { - const size_t old = pool_block_size(ptr); + const size_t old = jk_pool_block_size(ptr); if (size <= old) { p = ptr; // still fits its class } else { p = jk_esp_malloc_site(size, site); if (p) { memcpy(p, ptr, old); - pool_free(ptr); + jk_pool_free(ptr); } #if defined(JK_ESP_FS_DEBUG) return p; // already tracked by jk_esp_malloc_site @@ -782,7 +782,7 @@ void jk_esp_free(void *ptr) { alloc_untrack(ptr); #endif if (pool_owns(ptr)) { - pool_free(ptr); + jk_pool_free(ptr); } else if (arena_owns(ptr)) { arena_free(ptr); } else { @@ -1133,22 +1133,21 @@ void deinit() { g_audio_task.reset(); #endif Tab5Emu::get().wait_frame(); - // the presentation buffers belong to this engine run + // the presentation buffers belong to this engine run (~1.8 MB). Swapped + // with empty vectors: built without exceptions, libstdc++'s shrink_to_fit() + // does nothing, and these stayed allocated after the game (the SNES core + // then had too little PSRAM left) for (auto &b : g_frame8) { - b.clear(); - b.shrink_to_fit(); + std::vector().swap(b); } for (auto &b : g_overlay8) { - b.clear(); - b.shrink_to_fit(); + std::vector().swap(b); } - g_last_rgb565.clear(); - g_last_rgb565.shrink_to_fit(); + std::vector().swap(g_last_rgb565); g_last_frame8 = nullptr; g_last_w = g_last_h = 0; g_overlay_w = g_overlay_h = 0; - g_pattern.clear(); - g_pattern.shrink_to_fit(); + std::vector().swap(g_pattern); #if CONFIG_JK_ENGINE arena_destroy(); #endif diff --git a/components/msx/CMakeLists.txt b/components/msx/CMakeLists.txt new file mode 100644 index 0000000..530e974 --- /dev/null +++ b/components/msx/CMakeLists.txt @@ -0,0 +1,38 @@ +# MSX: fMSX, from the esp-cpp/fmsx submodule (the core in fmsx/fmsx, its +# glue in fmsx/port) +set(FM "fmsx/fmsx") +idf_component_register( + INCLUDE_DIRS "fmsx/port/include" + SRC_DIRS "fmsx/port" "${FM}" "${FM}/src/EMULib" "${FM}/src/fMSX" "${FM}/src/Z80" + PRIV_INCLUDE_DIRS "${FM}" "${FM}/src/EMULib" "${FM}/src/fMSX" "${FM}/src/Z80" + REQUIRES tab5-emu statistics shared_memory + ) +# target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-char-subscripts -Wno-attributes -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable -Wno-discarded-qualifiers) +target_compile_options(${COMPONENT_LIB} PRIVATE + -Wno-error=stringop-truncation + -Wno-error=format-overflow + -Wno-implicit-fallthrough + -Wno-format-overflow + -Wno-stringop-truncation + -Wno-unused-but-set-variable + -Wno-unused-variable + $<$:-Wno-register> + -DBPS16 -DUNIX -DLSB_FIRST -DNARROW -O2 +) +file(GLOB_RECURSE c_sources "${CMAKE_CURRENT_SOURCE_DIR}/${FM}/src/*.c") +set_source_files_properties(${c_sources} PROPERTIES COMPILE_FLAGS "-include msxfix.h") +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the msx core's .bss and .data to PSRAM" + VERBATIM) +# globals other cores in this firmware define too (the box doesn't build +# those): renamed in this library, its glue included +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --redefine-syms=${CMAKE_CURRENT_LIST_DIR}/symbol_renames.syms $ + COMMENT "Renaming the globals another core also defines" + VERBATIM) diff --git a/components/msx/fmsx b/components/msx/fmsx new file mode 160000 index 0000000..66b85a3 --- /dev/null +++ b/components/msx/fmsx @@ -0,0 +1 @@ +Subproject commit 66b85a3883d5407300dc9c93604a242f2c936b47 diff --git a/components/msx/symbol_renames.syms b/components/msx/symbol_renames.syms new file mode 100644 index 0000000..24850e3 --- /dev/null +++ b/components/msx/symbol_renames.syms @@ -0,0 +1,4 @@ +ClearImage fmsx_ClearImage +CPU fmsx_CPU +LoadROM fmsx_LoadROM +PatchZ80 fmsx_PatchZ80 diff --git a/components/nes/CMakeLists.txt b/components/nes/CMakeLists.txt new file mode 100644 index 0000000..0bfaa8e --- /dev/null +++ b/components/nes/CMakeLists.txt @@ -0,0 +1,19 @@ +# NES: nofrendo, from the esp-cpp/nofrendo submodule (the core in +# nofrendo/nofrendo, its glue in nofrendo/port) +set(NF "nofrendo/nofrendo") +idf_component_register( + INCLUDE_DIRS "nofrendo/port/include" + SRC_DIRS "nofrendo/port" "${NF}/cpu" "${NF}/libsnss" "${NF}/nes" "${NF}/sndhrdw" "${NF}/mappers" "${NF}" + PRIV_INCLUDE_DIRS "${NF}/cpu" "${NF}/libsnss" "${NF}/nes" "${NF}/sndhrdw" "${NF}" + REQUIRES "tab5-emu" "statistics" "shared_memory" +) +target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-char-subscripts -Wno-attributes -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable $<$:-std=gnu17> $<$:-Wno-discarded-qualifiers>) +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the nes core's .bss and .data to PSRAM" + VERBATIM) diff --git a/components/nes/nofrendo b/components/nes/nofrendo new file mode 160000 index 0000000..332fb5b --- /dev/null +++ b/components/nes/nofrendo @@ -0,0 +1 @@ +Subproject commit 332fb5bd1a1e0519e20c65a66074434a4a92a87a diff --git a/components/pool_allocator/CMakeLists.txt b/components/pool_allocator/CMakeLists.txt new file mode 100644 index 0000000..247af0b --- /dev/null +++ b/components/pool_allocator/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + INCLUDE_DIRS "include" + SRC_DIRS "src" + REQUIRES "" +) diff --git a/components/pool_allocator/include/pool_allocator.h b/components/pool_allocator/include/pool_allocator.h new file mode 100644 index 0000000..d7ffb0f --- /dev/null +++ b/components/pool_allocator/include/pool_allocator.h @@ -0,0 +1,31 @@ +#ifndef POOL_ALLOCATOR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include +#include + +// This file contains the declaration of a simple memory pool allocator. This +// memory pool is actually designed to be given a single (large) block of memory +// which it will then manage allocations/deallocations within + +void pool_create(void* region, size_t size); +void pool_destroy(); +int pool_contains(const void* ptr); +void* pool_alloc(size_t size); +void pool_free(void* ptr); +// Usable size of an allocated block (0 if ptr is not in the pool). +size_t pool_block_size(const void* ptr); +// Visit every block (allocated or free) in address order. +void pool_walk(void (*callback)(void* ptr, size_t size, int used, void* user), void* user); + +#ifdef __cplusplus +} +#endif + +#endif // POOL_ALLOCATOR_H diff --git a/components/pool_allocator/src/pool_allocator.c b/components/pool_allocator/src/pool_allocator.c new file mode 100644 index 0000000..c97ce28 --- /dev/null +++ b/components/pool_allocator/src/pool_allocator.c @@ -0,0 +1,123 @@ +#include "pool_allocator.h" + +#include +#include +#include + +#define ALIGN4(size) (((size) + 3) & ~3) + +typedef struct BlockHeader { + size_t size; + int free; + struct BlockHeader* next; +} BlockHeader; + +#define BLOCK_HEADER_SIZE sizeof(BlockHeader) + +// Blocks are kept in address order and tile the whole pool. +static uint8_t* memory_pool = NULL; +static size_t memory_pool_size = 0; +static BlockHeader* free_list = NULL; +// Lower bound for the first free block: every block before it is in use, so +// allocations start scanning here. NULL means there is no free block at all. +static BlockHeader* first_free = NULL; + +void pool_create(void* mem, size_t size) { + memory_pool = (uint8_t*)mem; + memory_pool_size = size; + + free_list = (BlockHeader*)memory_pool; + free_list->size = size - BLOCK_HEADER_SIZE; + free_list->free = 1; + free_list->next = NULL; + first_free = free_list; +} + +int pool_contains(const void* ptr) { + return (ptr >= (const void*)memory_pool && + ptr < (const void*)(memory_pool + memory_pool_size)); +} + +void* pool_alloc(size_t size) { + size = ALIGN4(size); + BlockHeader* curr = first_free; + BlockHeader* first_seen = NULL; // first free block found by this scan + + while (curr) { + if (curr->free) { + // Free blocks are coalesced lazily: merge the run of free blocks that follows. + while (curr->next && curr->next->free) { + curr->size += BLOCK_HEADER_SIZE + curr->next->size; + curr->next = curr->next->next; + } + + if (curr->size >= size) { + if (curr->size >= size + BLOCK_HEADER_SIZE + 4) { + // Split block + BlockHeader* new_block = (BlockHeader*)((uint8_t*)curr + BLOCK_HEADER_SIZE + size); + new_block->size = curr->size - size - BLOCK_HEADER_SIZE; + new_block->free = 1; + new_block->next = curr->next; + + curr->size = size; + curr->next = new_block; + } + + curr->free = 0; + first_free = first_seen ? first_seen : curr->next; + return (void*)((uint8_t*)curr + BLOCK_HEADER_SIZE); + } + + if (!first_seen) { + first_seen = curr; + } + } + + curr = curr->next; + } + + first_free = first_seen; + return NULL; // Out of memory +} + +void pool_free(void* ptr) { + if (!ptr) return; + + if (!pool_contains(ptr)) { + // Pointer is not in the memory pool + return; + } + + BlockHeader* block = (BlockHeader*)((uint8_t*)ptr - BLOCK_HEADER_SIZE); + block->free = 1; + + // Merge with the following block now; runs of free blocks further along are + // merged by pool_alloc() as it scans past them. + if (block->next && block->next->free) { + block->size += BLOCK_HEADER_SIZE + block->next->size; + block->next = block->next->next; + } + + if (!first_free || block < first_free) { + first_free = block; + } +} + +size_t pool_block_size(const void* ptr) { + if (!ptr || !pool_contains(ptr)) return 0; + const BlockHeader* block = (const BlockHeader*)((const uint8_t*)ptr - BLOCK_HEADER_SIZE); + return block->size; +} + +void pool_walk(void (*callback)(void* ptr, size_t size, int used, void* user), void* user) { + for (BlockHeader* curr = free_list; curr; curr = curr->next) { + callback((uint8_t*)curr + BLOCK_HEADER_SIZE, curr->size, !curr->free, user); + } +} + +void pool_destroy() { + memory_pool = NULL; + memory_pool_size = 0; + free_list = NULL; + first_free = NULL; +} diff --git a/components/rom_info/include/rom_info.hpp b/components/rom_info/include/rom_info.hpp index 8b74c86..8988164 100644 --- a/components/rom_info/include/rom_info.hpp +++ b/components/rom_info/include/rom_info.hpp @@ -8,7 +8,10 @@ #include "format.hpp" -enum class Emulator { UNKNOWN, JEDI_KNIGHT, DARK_FORCES, SEGA_GENESIS, SEGA_MEGA_DRIVE, SNES, GBA, MGS, TPM, PICO8 }; +enum class Emulator { + UNKNOWN, JEDI_KNIGHT, DARK_FORCES, SEGA_GENESIS, SEGA_MEGA_DRIVE, SNES, GBA, MGS, TPM, PICO8, + NES, GAMEBOY, GAMEBOY_COLOR, SEGA_MASTER_SYSTEM, SEGA_GAME_GEAR, MSX, DOOM, SCUMMVM +}; struct RomInfo { std::string name; @@ -21,7 +24,10 @@ struct RomInfo { /// The console a game is played as, in the order the game list shows them /// (several emulators can share one, e.g. Genesis / Mega Drive; the PC and /// PlayStation ports count as the console the game came out on). -enum class Console : uint8_t { SNES, GAME_BOY_ADVANCE, GENESIS, PLAYSTATION, PC, PICO8, UNKNOWN }; +enum class Console : uint8_t { + NES, SNES, GAME_BOY, GAME_BOY_COLOR, GAME_BOY_ADVANCE, MASTER_SYSTEM, GAME_GEAR, GENESIS, MSX, PLAYSTATION, PC, PICO8, + UNKNOWN +}; /// The shape the firmware draws for a console that has no art on the SD card. enum class ConsoleShape : uint8_t { GAMEPAD, HANDHELD_TALL, HANDHELD_WIDE, DISC, COMPUTER, FANTASY }; @@ -44,7 +50,16 @@ const ConsoleInfo &console_info(Console console); /// file name: /// - "*.jk" / "jk.cd" / "JK1.gob" : Jedi Knight (the directory containing /// the file is the game install dir) -/// - "dark.gob" / "*.df" : Dark Forces +/// - "dark.gob" / "*.df" : Dark Forces (the directory containing +/// the file holds the game's GOBs) +/// - "*.wad" : Doom (likewise, for the IWAD) +/// - "*.nes" : NES +/// - "*.gb" / "*.gbc" : Game Boy / Game Boy Color +/// - "*.sms" / "*.gg" : Master System / Game Gear +/// - "*.rom" : MSX +/// - "*.scummvm" : a ScummVM game (a marker file: the +/// directory containing it holds the +/// game's files; ScummVM detects which) /// - "STAGE.DIR" / "*.mgs" : Metal Gear Solid (the directory containing /// the file holds the extracted disc files) /// - "*.p8" / "*.p8.png" : PICO-8 cart @@ -72,6 +87,22 @@ template <> struct fmt::formatter { return fmt::format_to(ctx.out(), "TPM"); case Emulator::PICO8: return fmt::format_to(ctx.out(), "PICO8"); + case Emulator::NES: + return fmt::format_to(ctx.out(), "NES"); + case Emulator::GAMEBOY: + return fmt::format_to(ctx.out(), "GAMEBOY"); + case Emulator::GAMEBOY_COLOR: + return fmt::format_to(ctx.out(), "GAMEBOY_COLOR"); + case Emulator::SEGA_MASTER_SYSTEM: + return fmt::format_to(ctx.out(), "SEGA_MASTER_SYSTEM"); + case Emulator::SEGA_GAME_GEAR: + return fmt::format_to(ctx.out(), "SEGA_GAME_GEAR"); + case Emulator::MSX: + return fmt::format_to(ctx.out(), "MSX"); + case Emulator::DOOM: + return fmt::format_to(ctx.out(), "DOOM"); + case Emulator::SCUMMVM: + return fmt::format_to(ctx.out(), "SCUMMVM"); default: return fmt::format_to(ctx.out(), "UNKNOWN"); } diff --git a/components/rom_info/src/rom_info.cpp b/components/rom_info/src/rom_info.cpp index 67f6e12..d55a688 100644 --- a/components/rom_info/src/rom_info.cpp +++ b/components/rom_info/src/rom_info.cpp @@ -52,7 +52,7 @@ std::vector parse_metadata(const std::string &metadata_path) { platform = Emulator::JEDI_KNIGHT; #endif } else if (endsWith(lp, ".df") || endsWith(lp, "dark.gob")) { -#if defined(ENABLE_DARK_FORCES) +#if defined(ENABLE_DARKFORCES) platform = Emulator::DARK_FORCES; #endif } else if (endsWith(lp, ".gen")) { // sega genesis @@ -78,6 +78,38 @@ std::vector parse_metadata(const std::string &metadata_path) { } else if (endsWith(lp, ".p8") || endsWith(lp, ".p8.png")) { // pico-8 #if defined(ENABLE_PICO8) platform = Emulator::PICO8; +#endif + } else if (endsWith(lp, ".wad")) { // doom +#if defined(ENABLE_DOOM) + platform = Emulator::DOOM; +#endif + } else if (endsWith(lp, ".nes")) { // nes +#if defined(ENABLE_NES) + platform = Emulator::NES; +#endif + } else if (endsWith(lp, ".gb")) { // game boy +#if defined(ENABLE_GBC) + platform = Emulator::GAMEBOY; +#endif + } else if (endsWith(lp, ".gbc")) { // game boy color +#if defined(ENABLE_GBC) + platform = Emulator::GAMEBOY_COLOR; +#endif + } else if (endsWith(lp, ".sms")) { // sega master system +#if defined(ENABLE_SMS) + platform = Emulator::SEGA_MASTER_SYSTEM; +#endif + } else if (endsWith(lp, ".gg")) { // sega game gear +#if defined(ENABLE_SMS) + platform = Emulator::SEGA_GAME_GEAR; +#endif + } else if (endsWith(lp, ".rom")) { // msx +#if defined(ENABLE_MSX) + platform = Emulator::MSX; +#endif + } else if (endsWith(lp, ".scummvm")) { // scummvm (star trek: 25th anniversary, ...) +#if defined(ENABLE_SCUMMVM) + platform = Emulator::SCUMMVM; #endif } else if (endsWith(lp, "slus_008.84")) { // star wars episode i: the phantom menace #if defined(ENABLE_TPM) @@ -95,8 +127,20 @@ std::vector parse_metadata(const std::string &metadata_path) { Console console_of(Emulator platform) { switch (platform) { + case Emulator::NES: + return Console::NES; case Emulator::SNES: return Console::SNES; + case Emulator::GAMEBOY: + return Console::GAME_BOY; + case Emulator::GAMEBOY_COLOR: + return Console::GAME_BOY_COLOR; + case Emulator::SEGA_MASTER_SYSTEM: + return Console::MASTER_SYSTEM; + case Emulator::SEGA_GAME_GEAR: + return Console::GAME_GEAR; + case Emulator::MSX: + return Console::MSX; case Emulator::GBA: return Console::GAME_BOY_ADVANCE; case Emulator::SEGA_GENESIS: @@ -107,6 +151,8 @@ Console console_of(Emulator platform) { return Console::PLAYSTATION; case Emulator::JEDI_KNIGHT: case Emulator::DARK_FORCES: + case Emulator::DOOM: + case Emulator::SCUMMVM: return Console::PC; case Emulator::PICO8: return Console::PICO8; @@ -117,9 +163,15 @@ Console console_of(Emulator platform) { const ConsoleInfo &console_info(Console console) { static const ConsoleInfo infos[] = { + {"NES", "NES", "nes", 0xB22222, ConsoleShape::GAMEPAD}, {"Super Nintendo", "SNES", "snes", 0x6A5ACD, ConsoleShape::GAMEPAD}, + {"Game Boy", "Game Boy", "gb", 0x5B7F3A, ConsoleShape::HANDHELD_TALL}, + {"Game Boy Color", "GB Color", "gbc", 0x8E44AD, ConsoleShape::HANDHELD_TALL}, {"Game Boy Advance", "GBA", "gba", 0x4B3FA8, ConsoleShape::HANDHELD_WIDE}, + {"Master System", "SMS", "sms", 0x1F5FAF, ConsoleShape::GAMEPAD}, + {"Game Gear", "Game Gear", "gg", 0x4F6D8F, ConsoleShape::HANDHELD_WIDE}, {"Sega Genesis", "Genesis", "genesis", 0xC8102E, ConsoleShape::GAMEPAD}, + {"MSX", "MSX", "msx", 0x3A7D7D, ConsoleShape::COMPUTER}, {"PlayStation", "PlayStation", "playstation", 0x2E6DB4, ConsoleShape::DISC}, {"PC", "PC", "pc", 0x5A6B7B, ConsoleShape::COMPUTER}, {"PICO-8", "PICO-8", "pico8", 0xFF004D, ConsoleShape::FANTASY}, diff --git a/components/scummvm/CMakeLists.txt b/components/scummvm/CMakeLists.txt new file mode 100644 index 0000000..327a13d --- /dev/null +++ b/components/scummvm/CMakeLists.txt @@ -0,0 +1,40 @@ +# ScummVM (esp-cpp/scummvm, branch esp32p4: the backend is in +# scummvm/backends/platform/esp32), built with the Star Trek engine (25th +# Anniversary / Judgment Rites) +set(SVM "${CMAKE_CURRENT_LIST_DIR}/scummvm") +include(${SVM}/backends/platform/esp32/sources.cmake) +list(TRANSFORM SCUMMVM_SOURCES PREPEND "scummvm/") + +idf_component_register( + SRCS ${SCUMMVM_SOURCES} + INCLUDE_DIRS "scummvm/backends/platform/esp32/include" + PRIV_INCLUDE_DIRS "scummvm/backends/platform/esp32/generated" "scummvm" "scummvm/engines" + REQUIRES tab5-emu esp_timer + ) + +target_compile_definitions(${COMPONENT_LIB} PRIVATE + HAVE_CONFIG_H POSIX RELEASE_BUILD NONSTANDARD_SAVE_PATH + ENABLE_STARTREK=STATIC_PLUGIN + "DATA_PATH=\"/sdcard/scummvm\"" + ) +target_compile_options(${COMPONENT_LIB} PRIVATE + -Os -fno-exceptions -Wno-error -w + -msmall-data-limit=0 + ) +# static state in PSRAM like the other cores (see tools/bss_to_psram.py) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving ScummVM's .bss and .data to PSRAM" + VERBATIM) +# ScummVM's allocations: PSRAM, and a game's own arena (backends/platform/esp32/esp32-alloc.cpp) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${CMAKE_OBJCOPY} --redefine-syms=${SVM}/backends/platform/esp32/psram_alloc.syms $ + COMMENT "Sending ScummVM's allocations to PSRAM" + VERBATIM) +# Allocation tracking (idf.py -DSCUMMVM_TRACK_ALLOCS=1 build; =0 to turn it +# off again): each game run's leftovers go to /sdcard/saves/scummvm_leaks.txt +# (see backends/platform/esp32/esp32-alloc.cpp) +if(SCUMMVM_TRACK_ALLOCS) + target_compile_definitions(${COMPONENT_LIB} PRIVATE SCUMMVM_ESP32_TRACK_ALLOCS) +endif() diff --git a/components/scummvm/scummvm b/components/scummvm/scummvm new file mode 160000 index 0000000..a1c9a4e --- /dev/null +++ b/components/scummvm/scummvm @@ -0,0 +1 @@ +Subproject commit a1c9a4e034230155f56465dc43d28e24df0d8e5f diff --git a/components/sms/CMakeLists.txt b/components/sms/CMakeLists.txt new file mode 100644 index 0000000..9dd1c2b --- /dev/null +++ b/components/sms/CMakeLists.txt @@ -0,0 +1,21 @@ +# Master System / Game Gear: SMS Plus, from the esp-cpp/smsplus submodule +# (the core in smsplus/smsplus, its glue in smsplus/port) +set(SP "smsplus/smsplus") +idf_component_register( + INCLUDE_DIRS "smsplus/port/include" + SRC_DIRS "smsplus/port" "${SP}" "${SP}/cpu" "${SP}/sound" + PRIV_INCLUDE_DIRS "smsplus" "${SP}" "${SP}/cpu" "${SP}/sound" + REQUIRES tab5-emu statistics shared_memory + ) +# target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-char-subscripts -Wno-attributes -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable -Wno-discarded-qualifiers) +target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable -Wno-error=sizeof-pointer-memaccess $<$:-std=gnu17> $<$:-Wno-discarded-qualifiers> $<$:-Wno-error=address-of-packed-member>) +target_compile_definitions(${COMPONENT_LIB} PRIVATE LSB_FIRST=1) +# the core's static state (.bss and .data) lives in PSRAM (see the post-build step; the P4 +# linker script has no ldgen placeholder for external RAM); no gp-relative +# small data then +target_compile_options(${COMPONENT_LIB} PRIVATE -msmall-data-limit=0) +add_custom_command(TARGET ${COMPONENT_LIB} POST_BUILD + COMMAND ${PYTHON} ${CMAKE_CURRENT_LIST_DIR}/../../tools/bss_to_psram.py + ${CMAKE_OBJDUMP} ${CMAKE_OBJCOPY} $ --data + COMMENT "Moving the sms core's .bss and .data to PSRAM" + VERBATIM) diff --git a/components/sms/smsplus b/components/sms/smsplus new file mode 160000 index 0000000..762b9a6 --- /dev/null +++ b/components/sms/smsplus @@ -0,0 +1 @@ +Subproject commit 762b9a6b8dd42fbca951af8e3c89217797c10ea7 diff --git a/components/tab5-emu/CMakeLists.txt b/components/tab5-emu/CMakeLists.txt index 167da43..e398d50 100644 --- a/components/tab5-emu/CMakeLists.txt +++ b/components/tab5-emu/CMakeLists.txt @@ -18,6 +18,7 @@ idf_component_register( "input_drivers" "gt911" "event_manager" + "pool_allocator" "task" "timer" "m5stack-tab5" diff --git a/components/tab5-emu/include/box-emu.hpp b/components/tab5-emu/include/box-emu.hpp index f78a457..fc00279 100644 --- a/components/tab5-emu/include/box-emu.hpp +++ b/components/tab5-emu/include/box-emu.hpp @@ -2,4 +2,8 @@ // Compatibility shim: emulator glue shared with esp-box-emu is written against // BoxEmu; Tab5Emu mirrors that API. #include "tab5-emu.hpp" +// (box-emu.hpp brings these in for the glue too) +#include "make_color.h" +#include "pool_allocator.h" + using BoxEmu = Tab5Emu; diff --git a/components/tab5-emu/include/make_color.h b/components/tab5-emu/include/make_color.h new file mode 100644 index 0000000..245c54e --- /dev/null +++ b/components/tab5-emu/include/make_color.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +uint16_t make_color(uint8_t r, uint8_t g, uint8_t b); + +#ifdef __cplusplus +} +#endif diff --git a/components/tab5-emu/include/tab5-emu.hpp b/components/tab5-emu/include/tab5-emu.hpp index 7da209e..fccc284 100644 --- a/components/tab5-emu/include/tab5-emu.hpp +++ b/components/tab5-emu/include/tab5-emu.hpp @@ -80,6 +80,11 @@ class Tab5Emu : public espp::BaseComponent { size_t copy_file_to_romdata(const std::string &filename); uint8_t *romdata() const { return romdata_; } void free_romdata(); + /// Make romdata() an empty block of `bytes` (zeroed) instead of a copied + /// file: the cores shared with esp-box-emu that run their own allocator in + /// the box's fixed 4 MB ROM block (MSX, Doom, Dark Forces) use it that way. + /// Freed with the cart like a ROM. Returns false if there is no room. + bool reserve_romdata(size_t bytes); /// A permanent PSRAM block reserved at boot for cores that need one big /// contiguous ROM buffer (the SNES core wants 6 MB); the general heap gets /// too fragmented by GUI allocations to guarantee that later. Returns @@ -105,6 +110,9 @@ class Tab5Emu : public espp::BaseComponent { bool initialize_audio(); bool is_muted() const { return Bsp::get().is_muted(); } + /// The Tab5 has no haptic motor; for the glue shared with esp-box-emu + void play_haptic_effect() {} + void play_haptic_effect(int) {} void mute(bool v); void volume(float volume); float volume() const { return Bsp::get().volume(); } @@ -282,6 +290,8 @@ class Tab5Emu : public espp::BaseComponent { /// Set the on-screen size (in logical landscape pixels) the native frame is /// scaled to. It is centered on the screen. void display_size(size_t width, size_t height); + /// The size the frame is shown at (centered on the display) + std::pair display_size() const { return {display_width_, display_height_}; } /// Set the native (game) frame size; pitch is in pixels (defaults to width) void native_size(size_t width, size_t height, int pitch = -1); const uint16_t *palette() const { return palette_; } diff --git a/components/tab5-emu/src/make_color.cpp b/components/tab5-emu/src/make_color.cpp new file mode 100644 index 0000000..c2d23e2 --- /dev/null +++ b/components/tab5-emu/src/make_color.cpp @@ -0,0 +1,7 @@ +#include "make_color.h" + +// RGB565, the format of the frames the cores push (for the glue shared with +// esp-box-emu) +extern "C" uint16_t make_color(uint8_t r, uint8_t g, uint8_t b) { + return static_cast(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)); +} diff --git a/components/tab5-emu/src/tab5-emu.cpp b/components/tab5-emu/src/tab5-emu.cpp index b8ad249..7bc06ac 100644 --- a/components/tab5-emu/src/tab5-emu.cpp +++ b/components/tab5-emu/src/tab5-emu.cpp @@ -122,6 +122,9 @@ sdmmc_card_t *Tab5Emu::sdcard() const { return sdcard_; } // Memory ///////////////////////////////////////////////////////////////////////////// +// the ROM the cart copied in, for nofrendo (shared with esp-box-emu) +extern "C" uint8_t *osd_getromdata() { return Tab5Emu::get().romdata(); } + size_t Tab5Emu::copy_file_to_romdata(const std::string &filename) { std::ifstream file(filename, std::ios::binary | std::ios::ate); if (!file.is_open()) { @@ -148,6 +151,23 @@ size_t Tab5Emu::copy_file_to_romdata(const std::string &filename) { return size; } +bool Tab5Emu::reserve_romdata(size_t bytes) { + free_romdata(); + romdata_ = static_cast(heap_caps_calloc(1, bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)); + if (!romdata_ && rom_arena_ && bytes <= ROM_ARENA_BYTES) { + logger_.warn("No contiguous PSRAM block of {} bytes; using the ROM arena", bytes); + romdata_ = rom_arena_; + romdata_in_arena_ = true; + memset(romdata_, 0, bytes); + } + if (!romdata_) { + logger_.error("Could not allocate a {} byte block", bytes); + return false; + } + romdata_size_ = bytes; + return true; +} + void Tab5Emu::free_romdata() { if (romdata_ && romdata_in_arena_) { romdata_in_arena_ = false; @@ -260,8 +280,10 @@ size_t Tab5Emu::play_audio(const uint8_t *data, size_t size) { audio_bytes_dropped_ += out_size - sent; const auto now = esp_timer_get_time(); if (now - audio_last_report_us_ > 30'000'000) { + // (debug only: this runs on the game's audio task, whose stack can be + // small -- Doom's is 2 KB -- and formatting the line overflowed it) if (audio_last_report_us_ != 0) { - logger_.info("audio: sent {} KB, dropped {} B, waited {} times ({} -> {} Hz)", audio_bytes_sent_ / 1024, + logger_.debug("audio: sent {} KB, dropped {} B, waited {} times ({} -> {} Hz)", audio_bytes_sent_ / 1024, audio_bytes_dropped_, audio_waits_, audio_source_rate_, hw); } audio_bytes_sent_ = audio_bytes_dropped_ = audio_waits_ = 0; diff --git a/components/tpm/src/tpm_engine.cpp b/components/tpm/src/tpm_engine.cpp index dcf4cff..06604f1 100644 --- a/components/tpm/src/tpm_engine.cpp +++ b/components/tpm/src/tpm_engine.cpp @@ -980,8 +980,8 @@ void deinit() { // the runtime's buffers are the arena's, kept for the next launch (see // release_arena); the pool holds only what did not fit in it shared_mem_clear(); - g_exe.clear(); - g_exe.shrink_to_fit(); + // (swapped out: built without exceptions, shrink_to_fit() does nothing) + std::vector().swap(g_exe); } bool running() { return g_game_task && !g_game_done.load(); } diff --git a/main/carts.hpp b/main/carts.hpp index d9fd8bc..a313f1f 100644 --- a/main/carts.hpp +++ b/main/carts.hpp @@ -2,11 +2,18 @@ #include +#include "darkforces_cart.hpp" +#include "doom_cart.hpp" #include "gba_cart.hpp" +#include "gbc_cart.hpp" #include "genesis_cart.hpp" #include "jk_cart.hpp" #include "mgs_cart.hpp" +#include "msx_cart.hpp" +#include "nes_cart.hpp" #include "pico8_cart.hpp" +#include "scummvm_cart.hpp" +#include "sms_cart.hpp" #include "snes_cart.hpp" #include "tpm_cart.hpp" @@ -50,6 +57,29 @@ inline std::unique_ptr make_cart(const RomInfo &info) { case Emulator::PICO8: return std::make_unique(Cart::Config{ .info = info, .copy_romdata = true, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::NES: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = true, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::GAMEBOY: + case Emulator::GAMEBOY_COLOR: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = true, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::SEGA_MASTER_SYSTEM: + case Emulator::SEGA_GAME_GEAR: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = true, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::MSX: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = false, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::DOOM: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = false, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::DARK_FORCES: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = false, .verbosity = espp::Logger::Verbosity::INFO}); + case Emulator::SCUMMVM: + return std::make_unique(Cart::Config{ + .info = info, .copy_romdata = false, .verbosity = espp::Logger::Verbosity::INFO}); default: return nullptr; } diff --git a/main/darkforces_cart.hpp b/main/darkforces_cart.hpp new file mode 100644 index 0000000..c41b00e --- /dev/null +++ b/main/darkforces_cart.hpp @@ -0,0 +1,145 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_DARKFORCES) +#include "darkforces.hpp" +#endif + +class DarkForcesCart : public Cart { +public: + explicit DarkForcesCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + // the core runs its allocator in the box's 4 MB "ROM" block + Tab5Emu::get().reserve_romdata(4 * 1024 * 1024); + init(); + } + + virtual ~DarkForcesCart() override { + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_DARKFORCES) + reset_darkforces(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { + Cart::load(); +#if defined(ENABLE_DARKFORCES) + load_darkforces(get_save_path(), get_selected_save_slot()); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { + Cart::save(); +#if defined(ENABLE_DARKFORCES) + save_darkforces(get_save_path(true), get_selected_save_slot()); +#endif + } + + void init() { +#if defined(ENABLE_DARKFORCES) + init_darkforces(get_rom_filename(), romdata_, rom_size_bytes_); +#endif + } + + void deinit() { +#if defined(ENABLE_DARKFORCES) + deinit_darkforces(); + // the block goes back to the heap with the cart (~Cart): the game's + // free() must stop claiming pointers into it, or the board's other games + // leak everything they free there + darkforces_forget_block(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_DARKFORCES) + run_darkforces_rom(); + // quitting from the game's own menu returns to the emulator menu + if (darkforces_quit_requested()) { + running_ = false; + return false; + } +#endif + return Cart::run(); + } + +protected: + // Dark Forces renders at the DOS resolution of 320x200. + static constexpr size_t DF_WIDTH = 320; + static constexpr size_t DF_HEIGHT = 200; + + // (the GOBs and LFDs are read as the game goes) + bool uses_rom_folder() const override { return true; } + + void capture_hold(bool hold) override { +#if defined(ENABLE_DARKFORCES) + if (hold) { + pause_darkforces_tasks(); + } else { + resume_darkforces_tasks(); + } +#endif + } + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_DARKFORCES) + logger_.info("darkforces::pre_menu()"); + pause_darkforces_tasks(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_DARKFORCES) + logger_.info("darkforces::post_menu()"); + resume_darkforces_tasks(); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_DARKFORCES) + display_original(DF_WIDTH, DF_HEIGHT); +#endif + } + + virtual std::pair get_video_size() const override { + return std::make_pair(DF_WIDTH, DF_HEIGHT); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_DARKFORCES) + return get_darkforces_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_DARKFORCES) + display_fit(DF_WIDTH, DF_WIDTH * 3 / 4); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_DARKFORCES) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + return "_darkforces.sav"; + } +}; diff --git a/main/doom_cart.hpp b/main/doom_cart.hpp new file mode 100644 index 0000000..d4ea919 --- /dev/null +++ b/main/doom_cart.hpp @@ -0,0 +1,136 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_DOOM) +#include "doom.hpp" +#endif + +class DoomCart : public Cart { +public: + explicit DoomCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + // the core runs its allocator in the box's 4 MB "ROM" block + Tab5Emu::get().reserve_romdata(4 * 1024 * 1024); + init(); + } + + virtual ~DoomCart() override { + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_DOOM) + reset_doom(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { + Cart::load(); +#if defined(ENABLE_DOOM) + load_doom(get_save_path(), get_selected_save_slot()); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { + Cart::save(); +#if defined(ENABLE_DOOM) + save_doom(get_save_path(true), get_selected_save_slot()); +#endif + } + + void init() { +#if defined(ENABLE_DOOM) + init_doom(get_rom_filename(), romdata_, rom_size_bytes_); +#endif + } + + void deinit() { +#if defined(ENABLE_DOOM) + deinit_doom(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_DOOM) + run_doom_rom(); +#endif + return Cart::run(); + } + +protected: + // DOOM + static constexpr size_t DOOM_WIDTH = 320; + static constexpr size_t DOOM_HEIGHT = 240; + + // (the WAD is read as the game goes) + bool uses_rom_folder() const override { return true; } + + void capture_hold(bool hold) override { +#if defined(ENABLE_DOOM) + if (hold) { + pause_doom_tasks(); + } else { + resume_doom_tasks(); + } +#endif + } + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_DOOM) + logger_.info("doom::pre_menu()"); + pause_doom_tasks(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_DOOM) + logger_.info("doom::post_menu()"); + resume_doom_tasks(); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_DOOM) + display_original(DOOM_WIDTH, DOOM_HEIGHT); +#endif + } + + virtual std::pair get_video_size() const override { + return std::make_pair(DOOM_WIDTH, DOOM_HEIGHT); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_DOOM) + return get_doom_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_DOOM) + display_fit(DOOM_WIDTH, DOOM_HEIGHT); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_DOOM) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + return "_doom.sav"; + } +}; diff --git a/main/gbc_cart.hpp b/main/gbc_cart.hpp new file mode 100644 index 0000000..e595226 --- /dev/null +++ b/main/gbc_cart.hpp @@ -0,0 +1,129 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_GBC) +#include "gameboy.hpp" +#endif + +class GbcCart : public Cart { +public: + // an emulator core: a state loads right after the game starts + virtual bool supports_resume() const override { return true; } + + explicit GbcCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + init(); + } + + virtual ~GbcCart() override { + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_GBC) + reset_gameboy(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { + Cart::load(); +#if defined(ENABLE_GBC) + load_gameboy(get_save_path()); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { + Cart::save(); +#if defined(ENABLE_GBC) + save_gameboy(get_save_path(true)); +#endif + } + + void init() { +#if defined(ENABLE_GBC) + init_gameboy(get_rom_filename(), romdata_, rom_size_bytes_); +#endif + } + + void deinit() { +#if defined(ENABLE_GBC) + deinit_gameboy(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_GBC) + run_gameboy_rom(); +#endif + return Cart::run(); + } + +protected: + // GB + static constexpr size_t GAMEBOY_WIDTH = 160; + static constexpr size_t GAMEBOY_HEIGHT = 144; + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_GBC) + logger_.info("gbc::pre_menu()"); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_GBC) + logger_.info("gbc::post_menu()"); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_GBC) + display_original(GAMEBOY_WIDTH, GAMEBOY_HEIGHT); +#endif + } + + virtual std::pair get_video_size() const override { + return std::make_pair(GAMEBOY_WIDTH, GAMEBOY_HEIGHT); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_GBC) + return get_gameboy_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_GBC) + display_fit(GAMEBOY_WIDTH, GAMEBOY_HEIGHT); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_GBC) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + switch (info_.platform) { + case Emulator::GAMEBOY: + return "_gb.sav"; + case Emulator::GAMEBOY_COLOR: + return "_gbc.sav"; + default: + return Cart::get_save_extension(); + } + } +}; diff --git a/main/main.cpp b/main/main.cpp index 9751cb2..1abfa64 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,5 +1,9 @@ #include +#if defined(ENABLE_SCUMMVM) +#include "scummvm_port.hpp" +#endif + #include #include #include @@ -106,6 +110,12 @@ extern "C" void app_main(void) { } internal_free_log("initialize_sdcard"); +#if defined(ENABLE_SCUMMVM) + // now, while the PSRAM heap is empty: its long-lived allocations sit at + // the start instead of between the big blocks the games want + scummvm_prepare(); +#endif + if (!emu.initialize_audio()) { logger.warn("Failed to initialize audio!"); } diff --git a/main/msx_cart.hpp b/main/msx_cart.hpp new file mode 100644 index 0000000..34ca4af --- /dev/null +++ b/main/msx_cart.hpp @@ -0,0 +1,130 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_MSX) +#include "msx.hpp" +#endif + +class MsxCart : public Cart { +public: + // an emulator core: a state loads right after the game starts + virtual bool supports_resume() const override { return true; } + + explicit MsxCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + // the core runs its allocator in the box's 4 MB "ROM" block + Tab5Emu::get().reserve_romdata(4 * 1024 * 1024); + init(); + } + + virtual ~MsxCart() override { + logger_.info("~MsxCart()"); + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_MSX) + reset_msx(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { + Cart::load(); +#if defined(ENABLE_MSX) + load_msx(get_save_path()); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { + Cart::save(); +#if defined(ENABLE_MSX) + save_msx(get_save_path(true)); +#endif + } + + void init() { +#if defined(ENABLE_MSX) + init_msx(get_rom_filename(), romdata_, rom_size_bytes_); +#endif + } + + void deinit() { +#if defined(ENABLE_MSX) + deinit_msx(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_MSX) + run_msx_rom(); +#endif + return Cart::run(); + } + +protected: + // MSX + static constexpr size_t MSX_WIDTH = 256; + static constexpr size_t MSX_HEIGHT = 228; + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_MSX) + logger_.info("msx::pre_menu()"); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_MSX) + logger_.info("msx::post_menu()"); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_MSX) + display_original(MSX_WIDTH, MSX_HEIGHT); +#endif + } + + virtual std::pair get_video_size() const override { + return std::make_pair(MSX_WIDTH, MSX_HEIGHT); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_MSX) + return get_msx_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_MSX) + display_fit(MSX_WIDTH, MSX_HEIGHT); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_MSX) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + switch (info_.platform) { + case Emulator::MSX: + return "_msx.sav"; + default: + return Cart::get_save_extension(); + } + } +}; diff --git a/main/nes_cart.hpp b/main/nes_cart.hpp new file mode 100644 index 0000000..fa22ca8 --- /dev/null +++ b/main/nes_cart.hpp @@ -0,0 +1,123 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_NES) +#include "nes.hpp" +#endif + +class NesCart : public Cart { +public: + // an emulator core: a state loads right after the game starts + virtual bool supports_resume() const override { return true; } + + explicit NesCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + init(); + } + + virtual ~NesCart() override { + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_NES) + reset_nes(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { +#if defined(ENABLE_NES) + // (only a state the core took counts as loaded) + if (load_nes(get_save_path())) Cart::load(); +#else + Cart::load(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { + Cart::save(); +#if defined(ENABLE_NES) + save_nes(get_save_path(true)); +#endif + } + + void init() { +#if defined(ENABLE_NES) + init_nes(get_rom_filename(), romdata_, rom_size_bytes_); +#endif + } + + void deinit() { +#if defined(ENABLE_NES) + deinit_nes(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_NES) + run_nes_rom(); +#endif + return Cart::run(); + } + +protected: + static constexpr size_t NES_WIDTH = 256; + static constexpr size_t NES_HEIGHT = 240; + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_NES) + logger_.info("nes::pre_menu()"); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_NES) + logger_.info("nes::post_menu()"); +#endif + } + + virtual std::pair get_video_size() const override { + return std::make_pair(NES_WIDTH, NES_HEIGHT); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_NES) + return get_nes_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_NES) + display_original(NES_WIDTH, NES_HEIGHT); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_NES) + display_fit(NES_WIDTH, NES_HEIGHT); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_NES) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + return "_nes.sav"; + } +}; diff --git a/main/scummvm_cart.hpp b/main/scummvm_cart.hpp new file mode 100644 index 0000000..25d8453 --- /dev/null +++ b/main/scummvm_cart.hpp @@ -0,0 +1,138 @@ +#pragma once + +#include + +#include "cart.hpp" +#if defined(ENABLE_SCUMMVM) +#include "scummvm_port.hpp" +#endif + +/// A game ScummVM runs (Star Trek: 25th Anniversary, ...): the ROM is a marker +/// file (*.scummvm) in the game's folder, and ScummVM detects the game there. +/// The engine runs in its own task; the pause menu parks it. +class ScummvmCart : public Cart { +public: + explicit ScummvmCart(const Cart::Config &config) + : Cart(config) { + handle_video_setting(); +#if defined(ENABLE_SCUMMVM) + const auto dir = std::filesystem::path(get_rom_filename()).parent_path().string(); + if (!scummvm_start(dir)) { + logger_.error("ScummVM could not start {}", dir); + running_ = false; + } +#endif + } + + ~ScummvmCart() override { +#if defined(ENABLE_SCUMMVM) + scummvm_stop(); +#endif + } + + void load() override { +#if defined(ENABLE_SCUMMVM) + const auto path = get_save_path(); + if (path.empty() || !scummvm_load(path, get_selected_save_slot())) { + logger_.warn("ScummVM: no state loaded from slot {}", get_selected_save_slot()); + return; + } +#endif + Cart::load(); + } + + void save() override { +#if defined(ENABLE_SCUMMVM) + if (!scummvm_save(get_save_path(true), get_selected_save_slot())) { + logger_.warn("ScummVM: the game can't save now"); + return; + } +#endif + Cart::save(); + } + + bool run() override { +#if defined(ENABLE_SCUMMVM) + // the game quit (from its own menu) or failed: back to the launcher + if (!scummvm_running()) { + const auto status = scummvm_status(); + if (!status.empty()) + logger_.error("ScummVM: {}", status); + running_ = false; + return false; + } +#endif + // the screen's size is only known once the engine has set it up (and a + // game can change it): the video setting follows it + if (get_video_size() != video_size_) + handle_video_setting(); + std::this_thread::sleep_for(std::chrono::milliseconds(16)); + return Cart::run(); + } + +protected: + bool uses_rom_folder() const override { return true; } + + void capture_hold(bool hold) override { +#if defined(ENABLE_SCUMMVM) + if (hold) + scummvm_pause(); + else + scummvm_resume(); +#endif + } + + void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_SCUMMVM) + scummvm_pause(); +#endif + } + + void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_SCUMMVM) + scummvm_resume(); +#endif + } + + std::pair get_video_size() const override { +#if defined(ENABLE_SCUMMVM) + const auto [w, h] = scummvm_video_size(); + return {static_cast(w), static_cast(h)}; +#else + return {320, 200}; +#endif + } + + std::span get_video_buffer() const override { +#if defined(ENABLE_SCUMMVM) + return scummvm_video_buffer(); +#else + return {}; +#endif + } + + void handle_video_setting() override { + video_size_ = get_video_size(); + // (not set up yet: 0x0) + if (video_size_.first == 0 || video_size_.second == 0) + return; + Cart::handle_video_setting(); + } + + // the DOS games' 320x200 was drawn for a 4:3 screen + void set_original_video_setting() override { + const auto [w, h] = get_video_size(); + display_original(w, h * 6 / 5); + } + void set_fit_video_setting() override { + const auto [w, h] = get_video_size(); + display_fit(w, h * 6 / 5); + } + void set_fill_video_setting() override { display_fill(); } + + std::string get_save_extension() const override { return "_scummvm.sav"; } + + std::pair video_size_{0, 0}; +}; diff --git a/main/sms_cart.hpp b/main/sms_cart.hpp new file mode 100644 index 0000000..e4be39c --- /dev/null +++ b/main/sms_cart.hpp @@ -0,0 +1,156 @@ +#pragma once + +#include "cart.hpp" +#if defined(ENABLE_SMS) +#include "sms.hpp" +#endif + +class SmsCart : public Cart { +public: + // an emulator core: a state loads right after the game starts + virtual bool supports_resume() const override { return true; } + + explicit SmsCart(const Cart::Config& config) + : Cart(config) { + handle_video_setting(); + init(); + } + + virtual ~SmsCart() override { + logger_.info("~SmsCart()"); + deinit(); + } + + // cppcheck-suppress uselessOverride + virtual void reset() override { + Cart::reset(); +#if defined(ENABLE_SMS) + reset_sms(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void load() override { +#if defined(ENABLE_SMS) + // (only a state the core took counts as loaded) + if (load_sms(get_save_path())) Cart::load(); +#else + Cart::load(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void save() override { +#if defined(ENABLE_SMS) + // (only a state that reached the card counts as saved) + if (save_sms(get_save_path(true))) Cart::save(); +#else + Cart::save(); +#endif + } + + void init() { +#if defined(ENABLE_SMS) + switch (info_.platform) { + case Emulator::SEGA_MASTER_SYSTEM: + logger_.info("sms::init()"); + init_sms(romdata_, rom_size_bytes_); + break; + case Emulator::SEGA_GAME_GEAR: + logger_.info("gg::init()"); + init_gg(romdata_, rom_size_bytes_); + break; + default: + logger_.error("unknown platform"); + return; + } +#endif + } + + void deinit() { +#if defined(ENABLE_SMS) + deinit_sms(); +#endif + } + + // cppcheck-suppress uselessOverride + virtual bool run() override { +#if defined(ENABLE_SMS) + run_sms_rom(); +#endif + return Cart::run(); + } + +protected: + // SMS + static constexpr size_t SMS_WIDTH = 256; + static constexpr size_t SMS_HEIGHT = 192; + static constexpr size_t GG_WIDTH = 160; + static constexpr size_t GG_HEIGHT = 144; + + // cppcheck-suppress uselessOverride + virtual void pre_menu() override { + Cart::pre_menu(); +#if defined(ENABLE_SMS) + logger_.info("sms::pre_menu()"); +#endif + } + + // cppcheck-suppress uselessOverride + virtual void post_menu() override { + Cart::post_menu(); +#if defined(ENABLE_SMS) + logger_.info("sms::post_menu()"); +#endif + } + + virtual void set_original_video_setting() override { +#if defined(ENABLE_SMS) + const bool sms = info_.platform == Emulator::SEGA_MASTER_SYSTEM; + const size_t width = sms ? SMS_WIDTH : GG_WIDTH; + const size_t height = sms ? SMS_HEIGHT : GG_HEIGHT; + display_original(width, height); +#endif + } + + virtual std::pair get_video_size() const override { + auto height = info_.platform == Emulator::SEGA_MASTER_SYSTEM ? SMS_HEIGHT : GG_HEIGHT; + auto width = info_.platform == Emulator::SEGA_MASTER_SYSTEM ? SMS_WIDTH : GG_WIDTH; + return std::make_pair(width, height); + } + + // cppcheck-suppress uselessOverride + virtual std::span get_video_buffer() const override { +#if defined(ENABLE_SMS) + return get_sms_video_buffer(); +#else + return std::span(); +#endif + } + + virtual void set_fit_video_setting() override { +#if defined(ENABLE_SMS) + const bool sms = info_.platform == Emulator::SEGA_MASTER_SYSTEM; + const size_t width = sms ? SMS_WIDTH : GG_WIDTH; + const size_t height = sms ? SMS_HEIGHT : GG_HEIGHT; + display_fit(width, height); +#endif + } + + virtual void set_fill_video_setting() override { +#if defined(ENABLE_SMS) + display_fill(); +#endif + } + + virtual std::string get_save_extension() const override { + switch (info_.platform) { + case Emulator::SEGA_MASTER_SYSTEM: + return "_sms.sav"; + case Emulator::SEGA_GAME_GEAR: + return "_gg.sav"; + default: + return Cart::get_save_extension(); + } + } +};