From 9786c6dcc528b7e91a68a49ecfc2f40c3d49f684 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Wed, 27 Dec 2017 12:51:13 +0300 Subject: [PATCH] Move persistent memory outside of the RAM #483 --- src/console.c | 2 +- src/jsapi.c | 4 ++-- src/luaapi.c | 4 ++-- src/run.c | 31 +++++++++++++++---------------- src/run.h | 3 ++- src/tic.c | 2 +- src/tic.h | 4 ++-- src/ticapi.h | 1 + 8 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/console.c b/src/console.c index c7d6c1b..5ec481e 100644 --- a/src/console.c +++ b/src/console.c @@ -2186,7 +2186,7 @@ static void onConsoleRamCommand(Console* console, const char* param) {offsetof(tic_ram, tiles), "TILES"}, {offsetof(tic_ram, sprites), "SPRITES"}, {offsetof(tic_ram, map), "MAP"}, - {offsetof(tic_ram, persistent), "PERSISTENT MEMORY"}, + {offsetof(tic_ram, unknown), "..."}, {offsetof(tic_ram, registers), "SOUND REGISTERS"}, {offsetof(tic_ram, sfx.waveform), "WAVEFORMS"}, {offsetof(tic_ram, sfx.samples), "SFX"}, diff --git a/src/jsapi.c b/src/jsapi.c index b3cdad0..b5e6240 100644 --- a/src/jsapi.c +++ b/src/jsapi.c @@ -518,10 +518,10 @@ static duk_ret_t duk_pmem(duk_context* duk) if(index < TIC_PERSISTENT_SIZE) { - s32 val = memory->ram.persistent.data[index]; + s32 val = memory->persistent.data[index]; if(!duk_is_null_or_undefined(duk, 1)) - memory->ram.persistent.data[index] = duk_to_int(duk, 1); + memory->persistent.data[index] = duk_to_int(duk, 1); duk_push_int(duk, val); diff --git a/src/luaapi.c b/src/luaapi.c index 9155f50..ed08c78 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -993,11 +993,11 @@ static s32 lua_pmem(lua_State *lua) if(index < TIC_PERSISTENT_SIZE) { - s32 val = memory->ram.persistent.data[index]; + s32 val = memory->persistent.data[index]; if(top >= 2) { - memory->ram.persistent.data[index] = getLuaNumber(lua, 2); + memory->persistent.data[index] = getLuaNumber(lua, 2); } lua_pushinteger(lua, val); diff --git a/src/run.c b/src/run.c index b0113c2..4cfdcdb 100644 --- a/src/run.c +++ b/src/run.c @@ -76,16 +76,12 @@ static char* data2md5(const void* data, s32 length) return out; } -static const char* getPMemName(Run* run) +static void initPMemName(Run* run) { - static char buffer[FILENAME_MAX]; - const char* data = strlen(run->tic->saveid) ? run->tic->saveid : run->tic->cart.bank0.code.data; char* md5 = data2md5(data, (s32)strlen(data)); - strcpy(buffer, TIC_LOCAL); - strcat(buffer, md5); - - return buffer; + strcpy(run->saveid, TIC_LOCAL); + strcat(run->saveid, md5); } static void tick(Run* run) @@ -99,11 +95,10 @@ static void tick(Run* run) enum {Size = sizeof(tic_persistent)}; - if(SDL_memcmp(&run->tic->ram.persistent, run->persistent, Size) != 0) + if(SDL_memcmp(&run->tic->persistent, &run->persistent, Size) != 0) { - fsSaveRootFile(run->console->fs, getPMemName(run), &run->tic->ram.persistent, Size, true); - - SDL_memcpy(run->persistent, &run->tic->ram.persistent, Size); + fsSaveRootFile(run->console->fs, run->saveid, &run->tic->persistent, Size, true); + SDL_memcpy(&run->persistent, &run->tic->persistent, Size); } if(run->exit) @@ -203,15 +198,19 @@ void initRun(Run* run, Console* console, tic_mem* tic) { enum {Size = sizeof(tic_persistent)}; - SDL_memset(&run->tic->ram.persistent, 0, Size); + SDL_memset(&run->tic->persistent, 0, Size); + + initPMemName(run); s32 size = 0; - void* data = fsLoadRootFile(run->console->fs, getPMemName(run), &size); + void* data = fsLoadRootFile(run->console->fs, run->saveid, &size); - if(size == Size && data) + if(size > Size) size = Size; + + if(data) { - SDL_memcpy(&run->tic->ram.persistent, data, Size); - SDL_memcpy(run->persistent, data, Size); + SDL_memcpy(&run->tic->persistent, data, size); + SDL_memcpy(&run->persistent, data, size); } if(data) SDL_free(data); diff --git a/src/run.h b/src/run.h index 3e3cca7..e05b4e5 100644 --- a/src/run.h +++ b/src/run.h @@ -34,7 +34,8 @@ struct Run bool exit; - s32 persistent[TIC_PERSISTENT_SIZE]; + tic_persistent persistent; + char saveid[TIC_SAVEID_SIZE]; void(*tick)(Run*); }; diff --git a/src/tic.c b/src/tic.c index 6227a8c..627f569 100644 --- a/src/tic.c +++ b/src/tic.c @@ -1509,7 +1509,7 @@ static void updateSaveid(tic_mem* memory) const char* saveid = readMetatag(memory->cart.bank0.code.data, "saveid", api_get_script_config(memory)->singleComment); if(saveid) { - strcpy(memory->saveid, saveid); + strncpy(memory->saveid, saveid, TIC_SAVEID_SIZE-1); free((void*)saveid); } } diff --git a/src/tic.h b/src/tic.h index 511e371..0f2c05b 100644 --- a/src/tic.h +++ b/src/tic.h @@ -75,7 +75,7 @@ #define TIC_MAP_WIDTH (TIC_MAP_SCREEN_WIDTH * TIC_MAP_ROWS) #define TIC_MAP_HEIGHT (TIC_MAP_SCREEN_HEIGHT * TIC_MAP_COLS) -#define TIC_PERSISTENT_SIZE ((56-25)/sizeof(s32)) +#define TIC_PERSISTENT_SIZE (1024/sizeof(s32)) // 1K #define TIC_SAVEID_SIZE 64 #define TIC_SOUND_CHANNELS 4 @@ -415,7 +415,7 @@ typedef union tic_tiles tiles; tic_tiles sprites; tic_map map; - tic_persistent persistent; + u8 unknown[28]; tic_sound_register registers[TIC_SOUND_CHANNELS]; tic_sfx sfx; tic_music music; diff --git a/src/ticapi.h b/src/ticapi.h index d803612..3a223b7 100644 --- a/src/ticapi.h +++ b/src/ticapi.h @@ -173,6 +173,7 @@ struct tic_mem tic_input_method input; tic_font font; tic_api api; + tic_persistent persistent; char saveid[TIC_SAVEID_SIZE];