Commit 1bcf94e5 authored by jdgleaver's avatar jdgleaver
Browse files

Improve save state efficiency

parent b1882f16
......@@ -706,11 +706,12 @@ void retro_run (void)
size_t retro_serialize_size (void)
{
uint8_t *tmpbuf = (uint8_t*)malloc(5000000);
memstream_set_buffer(tmpbuf, 5000000);
S9xFreezeGame("");
free(tmpbuf);
return memstream_get_last_size();
int32 size = SnapshotSize();
if (size < 0)
return 0;
return (size_t)size;
}
bool retro_serialize(void *data, size_t size)
......
......@@ -923,4 +923,67 @@ int UnfreezeBlock(char* name, uint8* block, int size)
return (SUCCESS);
}
static int32 BlockSize(char *name, int data_size)
{
char buffer [512];
sprintf(buffer, "%s:%06d:", name, data_size);
return strlen(buffer) + data_size;
}
static int32 StructSize(char* name, FreezeData* fields,
int num_fields)
{
// Work out the size of the required block
int len = 0;
int i;
for (i = 0; i < num_fields; i++)
{
if (fields [i].offset + FreezeSize(fields [i].size,
fields [i].type) > len)
len = fields [i].offset + FreezeSize(fields [i].size,
fields [i].type);
}
return BlockSize(name, len);
}
int32 SnapshotSize(void)
{
char buffer[1024];
int32 len = 0;
sprintf(buffer, "%s:%04d\n", SNAPSHOT_MAGIC, SNAPSHOT_VERSION);
len += strlen(buffer);
strcpy(buffer, "NAM:000001:");
len += strlen(buffer) + 1;
len += StructSize("CPU", SnapCPU, COUNT(SnapCPU));
len += StructSize("REG", SnapRegisters, COUNT(SnapRegisters));
len += StructSize("PPU", SnapPPU, COUNT(SnapPPU));
len += StructSize("DMA", SnapDMA, COUNT(SnapDMA));
// RAM and VRAM
len += BlockSize("VRA", 0x10000);
len += BlockSize("RAM", 0x20000);
len += BlockSize("SRA", 0x20000);
len += BlockSize("FIL", 0x8000);
if (Settings.APUEnabled)
{
// APU
len += StructSize("APU", SnapAPU, COUNT(SnapAPU));
len += StructSize("ARE", SnapAPURegisters, COUNT(SnapAPURegisters));
len += BlockSize("ARA", 0x10000);
len += StructSize("SOU", SnapSoundData, COUNT(SnapSoundData));
}
#ifdef USE_SA1
if (Settings.SA1)
{
len += StructSize("SA1", SnapSA1, COUNT(SnapSA1));
len += StructSize("SAR", SnapSA1Registers, COUNT(SnapSA1Registers));
}
#endif
return len;
}
......@@ -56,4 +56,6 @@ bool8 S9xUnfreezeGame(const char* filename);
bool8 Snapshot(const char* filename);
bool8 S9xLoadSnapshot(const char* filename);
int32 SnapshotSize(void);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment