diff --git a/src/AI/sithAI.c b/src/AI/sithAI.c index 6cb920ab9..ed70e90f9 100644 --- a/src/AI/sithAI.c +++ b/src/AI/sithAI.c @@ -52,6 +52,10 @@ sithAIAlign sithAI_aAlignments[10] = {0}; // MoTS Added // Added: 84KB of .bss -> extram. Allocated once at sithAI_Startup; SithAIControlBlock is // all word-width fields (audited) and every clear below is word-safe. SithAIControlBlock* sithAI_actors = NULL; +#elif defined(PLATFORM_SESSION_ARRAYS) +// Added: SITHAI_MAX_ACTORS entries the platform allocates (zeroed) before the engine +// starts and frees after it has shut down (Platform/ESP32/jk_esp_arrays.c) +SithAIControlBlock* sithAI_actors = NULL; #else SithAIControlBlock sithAI_actors[SITHAI_MAX_ACTORS] = {0}; #endif diff --git a/src/AI/sithAI.h b/src/AI/sithAI.h index 94a9ad5ca..0167c1f27 100644 --- a/src/AI/sithAI.h +++ b/src/AI/sithAI.h @@ -44,8 +44,8 @@ #define sithAI_CanDetectSightThing_ADDR (0x004EC140) extern int sithAI_bOpened; -#ifdef TARGET_TWL -extern SithAIControlBlock* sithAI_actors; // Added: heap'd (extram); see sithAI.c +#if defined(TARGET_TWL) || defined(PLATFORM_SESSION_ARRAYS) +extern SithAIControlBlock* sithAI_actors; // Added: heap'd (extram, or per session); see sithAI.c #else extern SithAIControlBlock sithAI_actors[SITHAI_MAX_ACTORS]; #endif diff --git a/src/Cog/lex.yy.c b/src/Cog/lex.yy.c index bafb1d566..8a0428611 100644 --- a/src/Cog/lex.yy.c +++ b/src/Cog/lex.yy.c @@ -1174,6 +1174,11 @@ FILE *input_file; #endif { +#ifdef TARGET_ESP32 + /* (as newer flex does: sithCogParse_ESP32_ReleaseLexer may have freed it) */ + if ( ! yy_current_buffer ) + yy_current_buffer = yy_create_buffer( (FILE*)yyin, YY_BUF_SIZE ); +#endif yy_init_buffer( yy_current_buffer, input_file ); yy_load_buffer_state(); } @@ -1303,3 +1308,16 @@ FILE *file; } # line 89 "cog.l" +#ifdef TARGET_ESP32 +/* ESP32: the lexer's input buffer (YY_BUF_SIZE) is made by the first parse + * and kept for the process. The port frees it when the engine has stopped: + * made during the first level load, it otherwise stays in the middle of the + * PSRAM heap after the game and splits its largest free block. The next parse + * makes a new one (yyrestart / yylex). */ +void sithCogParse_ESP32_ReleaseLexer(void) +{ + if ( yy_current_buffer ) + yy_delete_buffer( yy_current_buffer ); + yy_init = 1; +} +#endif diff --git a/src/Devices/sithSound.c b/src/Devices/sithSound.c index cbe8521d9..27589c54b 100644 --- a/src/Devices/sithSound.c +++ b/src/Devices/sithSound.c @@ -233,10 +233,12 @@ sithSound* sithSound_GetFromIdx(int idx) } -#ifdef TARGET_RETRO_HOMEBREW +#if defined(TARGET_RETRO_HOMEBREW) && !defined(TARGET_ESP32) // Added: fileRead byte-writes into the destination, but long-sound PCM may live // in word-addressable-only memory (DC VRAM arena). Bounce those through a chunk -// buffer with word-safe copies; everything else reads direct. +// buffer with word-safe copies; everything else reads direct. (Not on ESP32: all +// of its memory takes byte stores, stdPlatform_IsWordAddressableOnly() is always +// 0 there, and the 16 KB bounce buffer was never used.) static int sithSound_ReadIntoBuffer(int fd, void* pDst, int len) { if (!stdPlatform_IsWordAddressableOnly(pDst)) diff --git a/src/Engine/rdActive.c b/src/Engine/rdActive.c index ff68e8a3e..44370761b 100644 --- a/src/Engine/rdActive.c +++ b/src/Engine/rdActive.c @@ -6,16 +6,25 @@ #include "jk.h" #ifdef RDRASTER_SOFTWARE_RENDERER -// Software-rasterizer working pools. numActiveFaces caps at 0x3ff and numActiveSpans at -// 0x7ff, so the pools hold 1024 faces (~512 KB) and 2048 spans (~176 KB). These are -// present only when the software path is compiled in. +// Software-rasterizer working pools. numActiveFaces caps at RDACTIVE_MAX_FACES-1 and +// numActiveSpans at RDACTIVE_MAX_SPANS-1, so by default the pools hold 1024 faces (~512 KB) +// and 2048 spans (~104 KB). These are present only when the software path is compiled in. +// Added: the build may set the pool sizes (RDACTIVE_MAX_FACES / RDACTIVE_MAX_SPANS, see +// rdActive.h). // TODO(integration): for TARGET_TWL / TARGET_DREAMCAST, gate the caps / place the pools // in extram to reclaim RAM (see PROGRESS.md). -#define RDACTIVE_MAX_FACES 1024 -#define RDACTIVE_MAX_SPANS 2048 +#ifdef PLATFORM_SESSION_ARRAYS +// Added: pointers to pools the platform allocates for an engine session (see +// rdActive.h). With RDRASTER_SW_ZBUFFER the world is drawn by rdZRaster and these +// pools are never used, so the platform does not allocate them at all; the +// rasterizer below draws nothing while they are NULL. +rdActiveFace* aActiveFaces = NULL; +rdActiveSpan* aActiveSpans = NULL; +#else static rdActiveFace aActiveFaces[RDACTIVE_MAX_FACES]; static rdActiveSpan aActiveSpans[RDACTIVE_MAX_SPANS]; +#endif static int yCurScanLine; // scanline currently being rasterized by rdActive_DrawScene // Exposed so the rdAFRaster draw callbacks can validate a span-list link points into the @@ -30,8 +39,11 @@ int rdActive_Startup() // Working pools are re-populated from index 0 every frame (counters reset in // rdActive_AdvanceFrame), but zero them here so the soft-reset loop in main.c // never leaves stale pointers behind when swapping JK.EXE/JKM.EXE functionality. - _memset(aActiveFaces, 0, sizeof(aActiveFaces)); - _memset(aActiveSpans, 0, sizeof(aActiveSpans)); + // (Added: sized by the counts, not sizeof(): the pools may be pointers) + if (aActiveFaces) + _memset(aActiveFaces, 0, sizeof(rdActiveFace) * RDACTIVE_MAX_FACES); + if (aActiveSpans) + _memset(aActiveSpans, 0, sizeof(rdActiveSpan) * RDACTIVE_MAX_SPANS); yCurScanLine = 0; #endif return 1; @@ -351,7 +363,7 @@ void rdActive_BuildSpans() // uninitialized callbacks — see the rdAFRaster Setup*NGon families still to be ported. int rdActive_AddActiveFace(rdProcEntry* pProcEntry) { - if ((uint32_t)numActiveFaces > 0x3ff) + if ((uint32_t)numActiveFaces > RDACTIVE_MAX_FACES - 1) // JK: 0x3ff return 0; int geometryMode = pProcEntry->geometryMode; @@ -599,6 +611,11 @@ void rdActive_BuildEdges() // finished edges, and step the surviving edges one scanline (re-sorting any that cross). void rdActive_DrawScene() { +#ifdef PLATFORM_SESSION_ARRAYS + // Added: no pools this session (see the definitions above) + if (!aActiveFaces || !aActiveSpans) + return; +#endif rdActive_BuildEdges(); for (yCurScanLine = yMinEdge; (uint32_t)yCurScanLine <= (uint32_t)yMaxEdge; yCurScanLine++) diff --git a/src/Engine/rdActive.h b/src/Engine/rdActive.h index 92a73e8f2..d088a2f9f 100644 --- a/src/Engine/rdActive.h +++ b/src/Engine/rdActive.h @@ -18,12 +18,26 @@ void rdActive_AdvanceFrame(); void rdActive_ClearFrameCounters(); #ifdef RDRASTER_SOFTWARE_RENDERER +// Added: working-pool sizes (the build may set them; JK.EXE's are 1024 / 2048) +#ifndef RDACTIVE_MAX_FACES +#define RDACTIVE_MAX_FACES 1024 +#endif +#ifndef RDACTIVE_MAX_SPANS +#define RDACTIVE_MAX_SPANS 2048 +#endif + void rdActive_DrawScene(); void rdActive_BuildEdges(); void rdActive_BuildSpans(); void rdActive_FlushSpans(); rdActiveSpan* rdActive_pSpanPoolBegin(void); rdActiveSpan* rdActive_pSpanPoolEnd(void); +#ifdef PLATFORM_SESSION_ARRAYS +// Added: the working pools, RDACTIVE_MAX_FACES / RDACTIVE_MAX_SPANS entries, set by the +// platform before the engine starts (NULL when the build does not draw with them) +extern rdActiveFace* aActiveFaces; +extern rdActiveSpan* aActiveSpans; +#endif // Defined by the rdAFRaster family (P3): dispatches a proc face to the setup routine // that matches its shading/surface/bit-depth and admits it to the active-face pool. int rdActive_AddActiveFace(rdProcEntry* pProcEntry); diff --git a/src/Gui/jkGUIForce.c b/src/Gui/jkGUIForce.c index 9db678cef..874fc09ca 100644 --- a/src/Gui/jkGUIForce.c +++ b/src/Gui/jkGUIForce.c @@ -649,6 +649,13 @@ int jkGuiForce_Show(int bCanSpendStars, int isMulti, int a4, char16_t* a5, int * clicked = jkGuiRend_DisplayAndReturnClicked(jkGuiForce_pMenu); if ( clicked == -1 ) { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus): the Yes/No below returns -1 too, + // so this screen would reopen forever + if ( jkGuiRend_ESP32_bCloseMenus ) + break; +#endif if ( !jkGuiDialog_YesNoDialog(jkStrings_GetUniStringWithFallback("GUI_ABORT_GAME"), jkStrings_GetUniStringWithFallback("GUI_CONFIRM_ABORTCD")) ) continue; } diff --git a/src/Gui/jkGUIMain.c b/src/Gui/jkGUIMain.c index 8523f8752..3a6d04a27 100644 --- a/src/Gui/jkGUIMain.c +++ b/src/Gui/jkGUIMain.c @@ -156,6 +156,12 @@ void jkGuiMain_Show() do { if (g_should_exit) return; // Added +#ifdef TARGET_ESP32 + // Added: a level switch requested from outside the menus (the + // tab5-emu pause menu loading a save) waits for this loop to end; + // leave it the way starting a game from the menus does + if (jkGuiRend_ESP32_bCloseMenus) break; +#endif jkGuiRend_MenuSetEscapeKeyShortcutElement(&jkGuiMain_menu, &jkGuiMain_elements[2]); v1 = jkGuiRend_DisplayAndReturnClicked(&jkGuiMain_menu); @@ -278,6 +284,11 @@ void jkGuiMain_ShowCutscenes() } } while ( v4 != -1 ); +#ifdef TARGET_ESP32 + // Added: closed for a pending load (see jkGuiRend_ESP32_bCloseMenus): + // going back to the main menu would replace that switch + if ( !jkGuiRend_ESP32_bCloseMenus ) +#endif jkMain_MenuReturn(); LABEL_17: v7 = 0; diff --git a/src/Gui/jkGUIPlayer.c b/src/Gui/jkGUIPlayer.c index 42f44716e..1c6b3d11c 100644 --- a/src/Gui/jkGUIPlayer.c +++ b/src/Gui/jkGUIPlayer.c @@ -21,6 +21,9 @@ #include "General/stdFnames.h" #include "General/stdFileUtil.h" #include "World/jkPlayer.h" +#ifdef TARGET_ESP32 +#include "Devices/sithControl.h" +#endif static int jkGuiPlayer_bInitted = 0; @@ -350,7 +353,20 @@ void jkGuiPlayer_ShowNewPlayer(int a1) jkPlayer_CreateConf(jkGuiPlayer_awTmp_555D28); } if ( v7 < 0 ) + { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus). With no profile to go back to + // (v15) this dialog reopens until one is created, i.e. forever; + // leave with the default controls instead, since no profile was + // read. With profiles, going back to the list reads the selected + // one (its -1 case) and leaves. + if ( jkGuiRend_ESP32_bCloseMenus && v15 ) + sithControl_DefaultInit(); + else +#endif v14 = 1; + } } while ( v14 ); jkGuiRend_DarrayFree(&a1a); diff --git a/src/Gui/jkGUIRend.c b/src/Gui/jkGUIRend.c index 4e4df11ed..712ec6e0d 100644 --- a/src/Gui/jkGUIRend.c +++ b/src/Gui/jkGUIRend.c @@ -36,6 +36,20 @@ void jkGuiRend_ESP32_CancelActiveMenu(void) if (jkGuiRend_activeMenu && !jkGuiRend_activeMenu->lastClicked) jkGuiRend_activeMenu->lastClicked = -1; } + +// tab5-emu: set when the platform asks for a GUI state switch while a modal +// menu is open (the pause menu loading a save at the engine's main menu). +// jkMain_GuiAdvance switches only once no menu is open, and on this target a +// menu does not close by itself for that (the original's jkGuiRend_thing_four +// cancel is off for TARGET_RETRO_HOMEBREW, see below), so the load would wait +// for the user to leave the menus -- and New Game would replace it. While set, +// every menu returns -1 (back), the screens that would ask "abort?" or reopen +// themselves on -1 (Force, tally, new player) return instead, jkGuiMain_Show +// leaves its loop, and the state handlers that fall back to the main menu on +// -1 keep the pending switch; cleared by jkMain_GuiAdvance when the switch +// happens. Also set by a quit from outside the engine (Window_MessageLoop): +// the same loops are what kept a quit at the main menu from returning. +int jkGuiRend_ESP32_bCloseMenus = 0; #endif static tVBuffer* jkGuiRend_menuBuffer = NULL; static tVBuffer *jkGuiRend_texture_dword_8561E8 = NULL; @@ -343,6 +357,10 @@ int32_t jkGuiRend_DisplayAndReturnClicked(jkGuiMenu *menu) while ( !menu->lastClicked ) { msgret = Window_MessageLoop(); +#ifdef TARGET_ESP32 + if ( jkGuiRend_ESP32_bCloseMenus ) + menu->lastClicked = -1; +#endif if ( jkGuiRend_thing_four && jkGuiRend_thing_five ) { // Added: this makes the menu that appears when pressing ESC in jkGUISingleTally flicker, @@ -544,6 +562,11 @@ void jkGuiRend_Shutdown() jkGuiRend_thing_five = 0; jkGuiRend_thing_four = 0; +#ifdef TARGET_ESP32 + // Added: a quit (or a quit right after a load at a menu) ends the session + // before jkMain_GuiAdvance clears it; the next launch starts without it + jkGuiRend_ESP32_bCloseMenus = 0; +#endif jkGuiRend_bIsSurfaceValid = 0; jkGuiRend_bInitted = 0; jkGuiRend_bOpen = 0; diff --git a/src/Gui/jkGUIRend.h b/src/Gui/jkGUIRend.h index aa563c716..2b9f3513d 100644 --- a/src/Gui/jkGUIRend.h +++ b/src/Gui/jkGUIRend.h @@ -84,6 +84,11 @@ extern int32_t jkGuiRend_thing_five; extern int32_t jkGuiRend_thing_four; +#ifdef TARGET_ESP32 +// tab5-emu: close the open modal menus for a pending GUI state switch or a quit (see jkGUIRend.c) +extern int jkGuiRend_ESP32_bCloseMenus; +#endif + void jkGuiRend_CopyVBuffer(jkGuiMenu *menu, rdRect *rect); void jkGuiRend_SetPalette(uint8_t* pal); void jkGuiRend_DrawRect(tVBuffer *vbuf, rdRect *rect, __int16 color); diff --git a/src/Gui/jkGUISingleTally.c b/src/Gui/jkGUISingleTally.c index b24ca1806..a7db6b852 100644 --- a/src/Gui/jkGUISingleTally.c +++ b/src/Gui/jkGUISingleTally.c @@ -62,6 +62,13 @@ int jkGuiSingleTally_Show() ret = jkGuiRend_DisplayAndReturnClicked(&jkGuiSingleTally_menu); if ( ret == -1 ) { +#ifdef TARGET_ESP32 + // Added: closing the menus for a pending load or a quit (see + // jkGuiRend_ESP32_bCloseMenus): the Yes/No below returns -1 too, + // so this screen would reopen forever + if ( jkGuiRend_ESP32_bCloseMenus ) + break; +#endif if ( !jkGuiDialog_YesNoDialog(jkStrings_GetUniStringWithFallback("GUI_ABORT_GAME"), jkStrings_GetUniStringWithFallback("GUI_CONFIRM_ABORTCD")) ) continue; } diff --git a/src/Main/jkMain.c b/src/Main/jkMain.c index 38a278179..2a47a005b 100644 --- a/src/Main/jkMain.c +++ b/src/Main/jkMain.c @@ -302,6 +302,9 @@ void jkMain_GuiAdvance() if ( jkSmack_stopTick && !jkGuiRend_thing_five ) { jkGuiRend_thing_four = 0; +#ifdef TARGET_ESP32 + jkGuiRend_ESP32_bCloseMenus = 0; // Added: the switch it waited for happens now +#endif v4 = jkSmack_currentGuiState; v5 = jkMain_aGuiStateFuncs[jkSmack_currentGuiState].leaveFunc; if ( v5 ) @@ -493,6 +496,12 @@ void jkMain_EndLevelScreenShow(int a1, int a2) } } +#ifdef TARGET_ESP32 + // Added: the tally / Force screen was closed for a pending load (see + // jkGuiRend_ESP32_bCloseMenus); the main menu would replace that switch + if ( jkGuiRend_ESP32_bCloseMenus ) + return; +#endif if ( jkGuiRend_thing_five ) jkGuiRend_thing_four = 1; jkSmack_stopTick = 1; @@ -510,6 +519,26 @@ void jkMain_EndLevelScreenLeave(int a1, int a2) ; } +#ifdef TARGET_ESP32 +// Added: a level needs the static world (static.jkl: the player's templates, +// models, puppets and their keyframes) and the inventory bins (items.dat), +// which jkMain_TitleShow loads before any menu can start one. A level reached +// without the title (the tab5-emu pause menu loading a save while the intro +// is still playing) got neither, and the save's first static keyframe (id +// 0x8000 | n) dereferenced a NULL sithWorld_g_pStaticWorld in +// sithKeyFrame_GetByIdx. Same as the title screen and +// Main_StartupDedicated's level autostart. A no-op once the title has run. +static int jkMain_ESP32_OpenStaticIfNoTitle(const char* pCaller) +{ + if ( sithWorld_g_pStaticWorld ) + return 0; + stdPlatform_Printf("OpenJKDF2: %s: no title screen yet, opening static.jkl\n", pCaller); + sithOpenStatic("static.jkl"); + jkHudInv_InitItems(); + return 1; +} +#endif + void jkMain_GameplayShow(int a1, int a2) { signed int level_loaded; // esi @@ -556,6 +585,12 @@ void jkMain_GameplayShow(int a1, int a2) jkGuiTitle_ShowLoading(jkMain_aLevelJklFname, 0); } +#ifdef TARGET_ESP32 + // Added: fallback for a level reached without the title by any other + // way than jkMain_UnkShow (which opens the static world itself) + jkMain_ESP32_OpenStaticIfNoTitle(__func__); +#endif + // MOTS added: // jkEpisode_Shutdown v3 = 0; // Added @@ -888,6 +923,12 @@ void jkMain_ChoiceShow(int a1, int a2) if ( jkGuiForce_Show(0, 0.0, 1, 0, &v1, 1) == -1 ) { +#ifdef TARGET_ESP32 + // Added: closed for a pending load (see jkGuiRend_ESP32_bCloseMenus); + // the main menu would replace that switch + if ( jkGuiRend_ESP32_bCloseMenus ) + return; +#endif if ( jkGuiRend_thing_five ) jkGuiRend_thing_four = 1; jkSmack_stopTick = 1; @@ -911,6 +952,20 @@ void jkMain_ChoiceLeave(int a1, int a2) void jkMain_UnkShow(int a1, int a2) { +#ifdef TARGET_ESP32 + // Added: a save loaded before the title screen (from the intro, see + // jkMain_ESP32_OpenStaticIfNoTitle). Opened here, before jkMain_UnkTick + // mounts the save's episode GOB, so static.jkl resolves its files the way + // the title does (jkRes searches the episode directory before the + // resource GOBs, and the static world stays for the whole session), with + // the title's loading screen; the cutscene is closed by now. + if ( !sithWorld_g_pStaticWorld ) + { + jkGuiTitle_ShowLoadingStatic(); + jkMain_ESP32_OpenStaticIfNoTitle(__func__); + jkGuiTitle_LoadingFinalize(); + } +#endif jkPlayer_SetAmmoMaximums(0); // MOTS added } @@ -1486,6 +1541,14 @@ void jkMain_VideoLeave(int a1, int a2) // sithTime_Resume(); jkCutscene_sub_421410(); +#ifdef TARGET_ESP32 + // Added: a save loaded during an inter-level cutscene (the tab5-emu pause + // menu; the switch to JK_GAMEMODE_UNK is only ever a load): starting the + // next level here would replace it, as jkMain_GuiAdvance takes the state + // this sets over the one it is switching to + if ( a2 == JK_GAMEMODE_UNK ) + return; +#endif if ( a1 == JK_GAMEMODE_VIDEO3 || a1 == JK_GAMEMODE_VIDEO4 ) jkMain_StartNextLevelInEpisode(0, 1); } diff --git a/src/Platform/ESP32/Window_ESP32.c b/src/Platform/ESP32/Window_ESP32.c index 0cd7fd8c6..71a3e88fb 100644 --- a/src/Platform/ESP32/Window_ESP32.c +++ b/src/Platform/ESP32/Window_ESP32.c @@ -28,6 +28,15 @@ #include #include +// The gentle material (texture) cache eviction in jk_esp_engine_frame(); the +// build may set both (see there). +#ifndef JK_ESP_MATERIAL_PURGE_FREE_KB +#define JK_ESP_MATERIAL_PURGE_FREE_KB 2560 +#endif +#ifndef JK_ESP_MATERIAL_PURGE_AGE +#define JK_ESP_MATERIAL_PURGE_AGE 900 +#endif + extern int jkGuiBuildMulti_bRendering; int Window_lastXRel = 0; @@ -149,12 +158,14 @@ int jk_esp_engine_frame(void) #endif // keep PSRAM headroom: the engine's own purge only runs on allocation // failure and then cascades down to evicting textures used a second ago. - // Evict gently here instead: materials unused for ~a minute, only when - // memory is getting tight. - if (jk_esp_psram_free() < 2560u * 1024) { + // Evict gently here instead: materials unused for JK_ESP_MATERIAL_PURGE_AGE + // rendered frames (~a minute), only when less than + // JK_ESP_MATERIAL_PURGE_FREE_KB is free (both set by the build: tab5-emu's + // components/jk/Kconfig, "Material (texture) cache"; 0 KB turns it off). + if (jk_esp_psram_free() < (size_t)JK_ESP_MATERIAL_PURGE_FREE_KB * 1024) { extern int rdMaterial_PurgeMaterialCacheOlderThan(int minAge); static uint32_t purgeCalls = 0; - int n = rdMaterial_PurgeMaterialCacheOlderThan(900); + int n = rdMaterial_PurgeMaterialCacheOlderThan(JK_ESP_MATERIAL_PURGE_AGE); if (n && (purgeCalls++ % 20) == 0) { jk_esp_log("material cache: evicted %d unused materials (psram free %u KB)", n, (unsigned)(jk_esp_psram_free() / 1024)); } @@ -252,6 +263,12 @@ int Window_MessageLoop() Window_bFlipRequested = 1; // redraw after the pause } if (jk_esp_quit_requested) { + // Cancelling only the active menu is not enough: the main menu (and + // the Force / tally / new player screens) reopen on -1, so a quit at + // JK's main menu never got back to the frame loop and jk::deinit + // waited forever. The flag makes them return (see jkGUIRend.c); it is + // reset with the GUI at shutdown. + jkGuiRend_ESP32_bCloseMenus = 1; jkGuiRend_ESP32_CancelActiveMenu(); return 0; } diff --git a/src/Platform/ESP32/jk_esp_arrays.c b/src/Platform/ESP32/jk_esp_arrays.c new file mode 100644 index 000000000..acd9f0f08 --- /dev/null +++ b/src/Platform/ESP32/jk_esp_arrays.c @@ -0,0 +1,118 @@ +// The engine's largest arrays, allocated for one engine session. +// +// The firmware runs one game at a time, but every core's static .bss is +// reserved in PSRAM all the time, so the engine's biggest tables used to sit +// there unused while the other cores ran. With PLATFORM_SESSION_ARRAYS (see +// engine_config.h) they are pointers instead: tab5-emu's glue (jk.cpp) calls +// jk_esp_arrays_alloc() when Jedi Knight is launched, before the engine task +// starts, and jk_esp_arrays_free() once the engine (and the audio task) has +// stopped. Each launch gets fresh zeroed buffers, as static storage is at +// boot. +// +// They are one block from the engine heap (jk_esp_malloc: the ROM arena first, +// then PSRAM), taken before anything else, so it sits at the start of the +// fresh arena and cannot fragment the heap the level is loaded into. +#include +#include + +#include "types.h" +#include "globals.h" +#include "AI/sithAI.h" +#include "Engine/rdActive.h" +#include "World/jkPlayer.h" + +#include "jk_esp.h" + +#ifndef PLATFORM_SESSION_ARRAYS +#error "jk_esp_arrays.c needs PLATFORM_SESSION_ARRAYS (engine_config.h)" +#endif + +// X(pointer, element type, count). The counts are the static arrays' sizes +// (symbols.syms for the generated globals); the AI actors and the directory +// cache take theirs from the build (tab5-emu's components/jk/Kconfig, "Engine +// table sizes"), the rasterizer pools from rdActive.h (or the build). +#ifdef JKM_DSS +#define JK_ESP_MOTS_ARRAYS(X) X(jkPlayer_aMotsInfos, jkPlayerInfo, NUM_JKPLAYER_THINGS) +#else +#define JK_ESP_MOTS_ARRAYS(X) +#endif + +// The active-edge rasterizer's pools (616 KB at JK.EXE's sizes) are only +// needed when it draws the world; with RDRASTER_SW_ZBUFFER rdZRaster does, +// and rdActive_DrawScene() never runs, so they are not allocated at all. +#if defined(RDRASTER_SOFTWARE_RENDERER) && !defined(RDRASTER_SW_ZBUFFER) +#define JK_ESP_RDACTIVE_ARRAYS(X) \ + X(aActiveFaces, rdActiveFace, RDACTIVE_MAX_FACES) \ + X(aActiveSpans, rdActiveSpan, RDACTIVE_MAX_SPANS) +#else +#define JK_ESP_RDACTIVE_ARRAYS(X) +#endif + +#define JK_ESP_SESSION_ARRAYS(X) \ + X(sithAI_actors, SithAIControlBlock, SITHAI_MAX_ACTORS) \ + X(jkPlayer_playerInfos, SithPlayer, JKPLAYER_NUM_INFOS) \ + X(jkPlayer_otherThings, jkPlayerInfo, NUM_JKPLAYER_THINGS) \ + JK_ESP_MOTS_ARRAYS(X) \ + X(aEntryStack, char, 0x14 * (STDCONF_LINEBUFFER_LEN + 4)) \ + X(sithCollision_aCollisions, sithCollisionSearchResult, 4) \ + X(sithSurface_aSurfaces, rdSurface, 256) \ + JK_ESP_RDACTIVE_ARRAYS(X) + +#define JK_ESP_ARRAY_ALIGN(n) (((size_t)(n) + 15) & ~(size_t)15) + +static uint8_t* jk_esp_arrays_block = NULL; +static size_t jk_esp_arrays_size = 0; + +// Point every array into `base` (NULL: detach them all); returns the bytes +// the layout needs. +static size_t jk_esp_arrays_layout(uint8_t* base) +{ + size_t off = 0; +#define JK_ESP_ARRAY_PLACE(var, type, count) \ + var = base ? (type*)(base + off) : NULL; \ + off += JK_ESP_ARRAY_ALIGN(sizeof(type) * (size_t)(count)); + JK_ESP_SESSION_ARRAYS(JK_ESP_ARRAY_PLACE) +#undef JK_ESP_ARRAY_PLACE + // the port's directory-existence cache (jk_esp_fs.c) + jk_esp_fs_dir_cache_attach(base ? base + off : NULL); + off += JK_ESP_ARRAY_ALIGN(jk_esp_fs_dir_cache_bytes()); + return off; +} + +// (tab5-emu's debug allocation dump names the block's size with this) +size_t jk_esp_arrays_bytes(void) +{ + return jk_esp_arrays_size; +} + +int jk_esp_arrays_alloc(void) +{ + jk_esp_arrays_free(); // a block left from an earlier launch (not expected) + const size_t bytes = jk_esp_arrays_layout(NULL); + uint8_t* block = (uint8_t*)jk_esp_malloc(bytes); // zeroed + if (!block) { + jk_esp_log("session arrays: no memory for %u KB", (unsigned)(bytes / 1024)); + return 0; + } + jk_esp_arrays_block = block; + jk_esp_arrays_size = bytes; + jk_esp_arrays_layout(block); + jk_esp_log("session arrays: %u KB at %p (AI actors %d, dir cache %d)", (unsigned)(bytes / 1024), (void*)block, + (int)SITHAI_MAX_ACTORS, (int)JK_ESP_FS_DIRCACHE); + return 1; +} + +void jk_esp_arrays_free(void) +{ + if (!jk_esp_arrays_block) { + return; + } + jk_esp_arrays_layout(NULL); + // the one global pointer into these arrays (sithClose resets it when a + // level closes; cleared here too, so nothing can follow it into the + // freed block) + sithPlayer_g_pLocalPlayer = NULL; + jk_esp_free(jk_esp_arrays_block); + jk_esp_arrays_block = NULL; + jk_esp_arrays_size = 0; +} diff --git a/src/Platform/ESP32/jk_esp_fs.c b/src/Platform/ESP32/jk_esp_fs.c index ca5069948..8566e6d62 100644 --- a/src/Platform/ESP32/jk_esp_fs.c +++ b/src/Platform/ESP32/jk_esp_fs.c @@ -75,16 +75,34 @@ int jk_esp_chdir(const char* path) // thousands of GOB-content lookups that probe the card first never repeat a // FatFs directory scan (each of those costs a block read per directory // sector). -#define JK_ESP_FS_DIRCACHE 512 +// The table (JK_ESP_FS_DIRCACHE entries of JK_ESP_FS_DIRLEN + 1 bytes, 64.5 KB +// by default) is not static: it is part of the engine's per-session arrays +// (jk_esp_arrays.c), attached before the engine starts and detached after it +// has shut down. Without it every directory "might exist" and the callers ask +// the card directly: correct, only slower. #define JK_ESP_FS_DIRLEN 128 -static char jk_esp_fs_dirs[JK_ESP_FS_DIRCACHE][JK_ESP_FS_DIRLEN]; -static signed char jk_esp_fs_dir_state[JK_ESP_FS_DIRCACHE]; // 0 empty, 1 exists, -1 missing +typedef char jk_esp_fs_dir_t[JK_ESP_FS_DIRLEN]; +static jk_esp_fs_dir_t* jk_esp_fs_dirs = NULL; +static signed char* jk_esp_fs_dir_state = NULL; // 0 empty, 1 exists, -1 missing static int jk_esp_fs_dir_count = 0; uint32_t jk_esp_fs_dir_hits = 0, jk_esp_fs_dir_misses = 0, jk_esp_fs_dir_overflow = 0; +size_t jk_esp_fs_dir_cache_bytes(void) +{ + return (size_t)JK_ESP_FS_DIRCACHE * (sizeof(jk_esp_fs_dir_t) + sizeof(signed char)); +} + +void jk_esp_fs_dir_cache_attach(void* mem) +{ + jk_esp_fs_dirs = (jk_esp_fs_dir_t*)mem; + jk_esp_fs_dir_state = mem ? (signed char*)(jk_esp_fs_dirs + JK_ESP_FS_DIRCACHE) : NULL; + jk_esp_fs_dir_cache_reset(); +} + void jk_esp_fs_dir_cache_reset(void) { - memset(jk_esp_fs_dir_state, 0, sizeof(jk_esp_fs_dir_state)); + if (jk_esp_fs_dir_state) + memset(jk_esp_fs_dir_state, 0, JK_ESP_FS_DIRCACHE); jk_esp_fs_dir_count = 0; } @@ -97,7 +115,7 @@ static uint32_t jk_esp_fs_hash(const char* s, size_t n) int jk_esp_fs_dir_might_exist(const char* abs_path) { - if (!abs_path) return 1; + if (!abs_path || !jk_esp_fs_dir_state) return 1; const char* slash = strrchr(abs_path, '/'); if (!slash || slash == abs_path) return 1; size_t dlen = (size_t)(slash - abs_path); diff --git a/src/Platform/ESP32/jk_esp_fs.h b/src/Platform/ESP32/jk_esp_fs.h index c3188f206..3bc0e57e9 100644 --- a/src/Platform/ESP32/jk_esp_fs.h +++ b/src/Platform/ESP32/jk_esp_fs.h @@ -30,6 +30,14 @@ int jk_esp_chdir(const char* path); // jk_esp_fs_dir_cache_reset() after creating directories. int jk_esp_fs_dir_might_exist(const char* abs_path); void jk_esp_fs_dir_cache_reset(void); +// The cache's table is allocated with the engine's per-session arrays +// (jk_esp_arrays.c): its size, and attach (mem: that many zeroed bytes) / +// detach (NULL). Its capacity: the build may set JK_ESP_FS_DIRCACHE. +#ifndef JK_ESP_FS_DIRCACHE +#define JK_ESP_FS_DIRCACHE 512 +#endif +size_t jk_esp_fs_dir_cache_bytes(void); +void jk_esp_fs_dir_cache_attach(void* mem); #ifdef __cplusplus } diff --git a/src/Platform/ESP32/jk_esp_save.c b/src/Platform/ESP32/jk_esp_save.c index 638f7d906..a16e154ba 100644 --- a/src/Platform/ESP32/jk_esp_save.c +++ b/src/Platform/ESP32/jk_esp_save.c @@ -6,6 +6,8 @@ #include "Main/jkMain.h" #include "Main/jkSmack.h" #include "Gui/jkGUITitle.h" +#include "Gui/jkGUIRend.h" +#include "Devices/sithControl.h" #include "World/jkPlayer.h" #include "World/sithWorld.h" #include "Gameplay/sithPlayer.h" @@ -76,9 +78,34 @@ int jk_esp_engine_load(const char* path) for (char16_t* p = header.saveName; *p; p++) { if (*p == u'~') { label = p + 1; break; } } + if (!sithWorld_g_pStaticWorld) { + // Still in the intro: the engine has not shown its title (which + // reads static.jkl: jkMain_UnkShow now does that for a level + // reached without it) nor its Players dialog, which reads the + // player's profile (controls, options). Read the one the dialog + // would offer (the last player, from the registry); without one the + // controls have no bindings, so fall back to the defaults. + char name[32]; + stdString_WcharToChar(name, jkPlayer_playerShortName, 31); + name[31] = 0; + jk_esp_log("load: no title yet, reading player profile '%s'", name); + if (!jkPlayer_ReadConf(jkPlayer_playerShortName)) { + jk_esp_log("load: no player profile, default controls"); + sithControl_DefaultInit(); + } + } jk_esp_log("load: switching to episode '%s' level '%s' for %s", header.episodeName, header.jklName, path); jkSmack_gameMode = 1; // "load a save" flavour of the level switch - return jkMain_sub_4034D0(header.episodeName, (char*)path, header.jklName, label); + int ok = jkMain_sub_4034D0(header.episodeName, (char*)path, header.jklName, label); + // The switch happens once no engine menu is open (jkMain_GuiAdvance); + // at the main menu (or the Players dialog, Single Player, Setup, the Esc + // menu, a tally or Force screen...) close them, as the engine's own Load + // does by returning from them. + if (ok && jkGuiRend_thing_five) { + jk_esp_log("load: closing the engine's menus first"); + jkGuiRend_ESP32_bCloseMenus = 1; + } + return ok; } // Restart the current level. Like the engine's ESC menu "restart": restore diff --git a/src/Platform/ESP32/stdSound.c b/src/Platform/ESP32/stdSound.c index 0efa1c107..3b66fa876 100644 --- a/src/Platform/ESP32/stdSound.c +++ b/src/Platform/ESP32/stdSound.c @@ -283,6 +283,17 @@ void stdSound_ESP32_Pump(void) } } +// The port, once the engine and its audio task have stopped: the mix buffer is +// made once per process; free it so it is not left in the middle of the PSRAM +// heap after the game (the next startup makes a new one) +void stdSound_ESP32_Release(void) +{ + free(stdSound_pMixBuf); + stdSound_pMixBuf = NULL; + stdSound_bInitted = 0; + stdSound_pendingOff = stdSound_pendingLen = 0; +} + // Periodic diagnostics (called from the engine's frame report) void stdSound_ESP32_Report(void) { diff --git a/src/World/jkPlayer.c b/src/World/jkPlayer.c index 81e727e92..f2450af70 100644 --- a/src/World/jkPlayer.c +++ b/src/World/jkPlayer.c @@ -5,6 +5,7 @@ #endif #include +#include // Added: memset (was implicitly declared) #include "General/stdString.h" #include "General/stdFnames.h" #include "General/stdFileUtil.h" @@ -83,7 +84,13 @@ int jkPlayer_bJankyPhysics = 0; #endif #ifdef JKM_DSS +#ifdef PLATFORM_SESSION_ARRAYS +// Added: allocated (zeroed) by the platform for each engine session +// (Platform/ESP32/jk_esp_arrays.c), NUM_JKPLAYER_THINGS entries +jkPlayerInfo* jkPlayer_aMotsInfos = NULL; +#else jkPlayerInfo jkPlayer_aMotsInfos[NUM_JKPLAYER_THINGS] = {0}; +#endif jkBubbleInfo jkPlayer_aBubbleInfo[NUM_JKPLAYER_THINGS] = {0}; int jkPlayer_personality = 0; flex_t jkPlayer_aMultiParams[0x100]; @@ -260,7 +267,8 @@ void jkPlayer_ResetVars() #endif #ifdef JKM_DSS - memset(jkPlayer_aMotsInfos, 0, sizeof(jkPlayer_aMotsInfos)); + if (jkPlayer_aMotsInfos) // Added: may be a per-session pointer (PLATFORM_SESSION_ARRAYS) + memset(jkPlayer_aMotsInfos, 0, sizeof(jkPlayerInfo) * NUM_JKPLAYER_THINGS); memset(jkPlayer_aBubbleInfo, 0, sizeof(jkPlayer_aBubbleInfo)); jkPlayer_personality = 0; memset(jkPlayer_aMultiParams, 0, sizeof(jkPlayer_aMultiParams)); @@ -316,7 +324,7 @@ void jkPlayer_Shutdown() jkPlayer_otherThings[i].polylineThing.model3 = 0; } } - _memset(jkPlayer_otherThings, 0, sizeof(jkPlayer_otherThings)); + _memset(jkPlayer_otherThings, 0, sizeof(jkPlayerInfo) * NUM_JKPLAYER_THINGS); // Added: not sizeof(), it may be a pointer #ifdef JKM_DSS for (int i = 0; i < NUM_JKPLAYER_THINGS; i++) @@ -325,7 +333,7 @@ void jkPlayer_Shutdown() if (jkPlayer_aMotsInfos[i].polylineThing.model3) { - rdThing_FreeEntry(&jkPlayer_otherThings[i].polylineThing); + rdThing_FreeEntry(&jkPlayer_aMotsInfos[i].polylineThing); // Added: was jkPlayer_otherThings[i] (already zeroed above) jkPlayer_aMotsInfos[i].polylineThing.model3 = 0; } @@ -333,7 +341,7 @@ void jkPlayer_Shutdown() rdThing_FreeEntry(&jkPlayer_aMotsInfos[i].rd_thing); // Added: fix memleak } - _memset(jkPlayer_aMotsInfos, 0, sizeof(jkPlayer_aMotsInfos)); + _memset(jkPlayer_aMotsInfos, 0, sizeof(jkPlayerInfo) * NUM_JKPLAYER_THINGS); // Added: not sizeof(), it may be a pointer #endif //nullsub_28_free(); diff --git a/src/World/jkPlayer.h b/src/World/jkPlayer.h index 9a6103d4d..69715d60c 100644 --- a/src/World/jkPlayer.h +++ b/src/World/jkPlayer.h @@ -55,7 +55,11 @@ #define jkPlayer_SetRank_ADDR (0x004074E0) #ifdef JKM_DSS +#ifdef PLATFORM_SESSION_ARRAYS +extern jkPlayerInfo* jkPlayer_aMotsInfos; // Added: NUM_JKPLAYER_THINGS entries, allocated per session +#else extern jkPlayerInfo jkPlayer_aMotsInfos[64]; +#endif extern jkBubbleInfo jkPlayer_aBubbleInfo[64]; extern int jkPlayer_personality; extern flex_t jkPlayer_aMultiParams[0x100]; diff --git a/src/engine_config.h b/src/engine_config.h index 70dbdd403..ab87f474a 100644 --- a/src/engine_config.h +++ b/src/engine_config.h @@ -283,11 +283,25 @@ #define STDCONF_LINEBUFFER_LEN (2048) #endif +// Added: the build may set the AI actor pool size (-DSITHAI_MAX_ACTORS=n) +#ifndef SITHAI_MAX_ACTORS #ifdef TARGET_RETRO_HOMEBREW #define SITHAI_MAX_ACTORS (128) #else #define SITHAI_MAX_ACTORS (256) #endif +#endif + +// Added (ESP32): the engine's largest arrays are pointers to buffers the +// platform layer allocates (zeroed) when the engine starts and frees when it +// has shut down, instead of static .bss. The firmware runs one game at a time +// but reserves every core's .bss all the time, so ~0.4 MB of static engine +// state sat unused while the other cores ran. The list (and the allocation) +// is in Platform/ESP32/jk_esp_arrays.c; each array keeps its static form on +// the other targets. +#ifdef TARGET_ESP32 +#define PLATFORM_SESSION_ARRAYS +#endif #if defined(QOL_IMPROVEMENTS) && !defined(TARGET_RETRO_HOMEBREW) #define SITH_MIXER_NUMPLAYINGSOUNDS (256) diff --git a/src/globals.c.cog b/src/globals.c.cog index f96599d4a..047fd0e0c 100644 --- a/src/globals.c.cog +++ b/src/globals.c.cog @@ -32,6 +32,13 @@ # #ifndef so --gc-sections can strip the unused .bss array. heap_shadowed = { "sithComm_MsgTmpBuf": "SITHCOMM_HEAP_MSGBUF" } + # Added: arrays that are pointers to per-engine-session buffers when + # PLATFORM_SESSION_ARRAYS is set (see engine_config.h; the platform + # allocates them zeroed before the engine starts and frees them after it + # has shut down). Keep in sync with globals.h.cog and the reset below. + session_arrays = [ "sithSurface_aSurfaces", "jkPlayer_playerInfos", "jkPlayer_otherThings", + "sithCollision_aCollisions", "aEntryStack" ] + addrs = [] f = open(symbols_fpath, "r") @@ -93,6 +100,12 @@ cog.outl("#ifndef %s // Added: heap-shadowed on some platforms" % heap_shadowed[name]) cog.outl("%s %s%s MAYBE_UNUSED = %s;" % (c_type, name, c_type_post, c_val)) cog.outl("#endif") + elif name in session_arrays: + cog.outl("#ifdef PLATFORM_SESSION_ARRAYS // Added: allocated for each engine session") + cog.outl("%s* %s MAYBE_UNUSED = NULL;" % (c_type, name)) + cog.outl("#else") + cog.outl("%s %s%s MAYBE_UNUSED = %s;" % (c_type, name, c_type_post, c_val)) + cog.outl("#endif") else: cog.outl("%s %s%s MAYBE_UNUSED = %s;" % (c_type, name, c_type_post, c_val)) @@ -107,6 +120,8 @@ void OpenJKDF2_Globals_Reset() import cog heap_shadowed = { "sithComm_MsgTmpBuf": "SITHCOMM_HEAP_MSGBUF" } # Added: keep in sync with above + session_arrays = [ "sithSurface_aSurfaces", "jkPlayer_playerInfos", "jkPlayer_otherThings", + "sithCollision_aCollisions", "aEntryStack" ] # Added: keep in sync with above addrs = [] @@ -175,6 +190,13 @@ void OpenJKDF2_Globals_Reset() cog.outl("#ifndef %s" % heap_shadowed[name]) cog.outl("_memset_inline(&%s, 0, sizeof(%s));" % (name, name)) cog.outl("#endif") + elif name in session_arrays: + # Added: a pointer under PLATFORM_SESSION_ARRAYS (sizeof() is not the array's) + cog.outl("#ifdef PLATFORM_SESSION_ARRAYS") + cog.outl("if (%s) _memset_inline(%s, 0, sizeof(*%s) * (%s));" % (name, name, name, c_type_post.strip()[1:-1])) + cog.outl("#else") + cog.outl("_memset_inline(&%s, 0, sizeof(%s));" % (name, name)) + cog.outl("#endif") elif "NO_REINIT" in c_type or "NO_REINIT" in c_type_post: cog.outl("// NO_REINIT for %s" % (name)) elif c_val == "NULL" or c_val == "0" or c_val == "0.0f" or c_type_is_int_like: diff --git a/src/globals.h.cog b/src/globals.h.cog index 360499477..775f490a4 100644 --- a/src/globals.h.cog +++ b/src/globals.h.cog @@ -94,7 +94,18 @@ typedef struct embeddedResource_t c_type = s[0] c_type_post = "[" + s[1] - cog.outl("extern %s %s%s MAYBE_UNUSED;" % (c_type, name, c_type_post)) + # Added: pointers to per-engine-session buffers under PLATFORM_SESSION_ARRAYS + # (keep in sync with globals.c.cog) + session_arrays = [ "sithSurface_aSurfaces", "jkPlayer_playerInfos", "jkPlayer_otherThings", + "sithCollision_aCollisions", "aEntryStack" ] + if name in session_arrays: + cog.outl("#ifdef PLATFORM_SESSION_ARRAYS") + cog.outl("extern %s* %s MAYBE_UNUSED;" % (c_type, name)) + cog.outl("#else") + cog.outl("extern %s %s%s MAYBE_UNUSED;" % (c_type, name, c_type_post)) + cog.outl("#endif") + else: + cog.outl("extern %s %s%s MAYBE_UNUSED;" % (c_type, name, c_type_post)) ]]] [[[end]]]