Move persistent memory outside of the RAM #483
This commit is contained in:
		@@ -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"},
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										31
									
								
								src/run.c
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								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);
 | 
			
		||||
 
 | 
			
		||||
@@ -34,7 +34,8 @@ struct Run
 | 
			
		||||
 | 
			
		||||
	bool exit;
 | 
			
		||||
	
 | 
			
		||||
	s32 persistent[TIC_PERSISTENT_SIZE];
 | 
			
		||||
	tic_persistent persistent;
 | 
			
		||||
	char saveid[TIC_SAVEID_SIZE];
 | 
			
		||||
 | 
			
		||||
	void(*tick)(Run*);
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 
 | 
			
		||||
@@ -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];
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user