mirror of
https://github.com/dborth/snes9xgx.git
synced 2024-11-01 00:15:14 +01:00
rewrite SRAM code
This commit is contained in:
parent
a5a8dba908
commit
8ff4c5a69d
@ -32,207 +32,105 @@ extern unsigned short gcpadmap[];
|
||||
char sramcomment[2][32];
|
||||
|
||||
/****************************************************************************
|
||||
* Prepare Memory Card SRAM Save Data
|
||||
*
|
||||
* This sets up the save buffer for saving to a memory card.
|
||||
****************************************************************************/
|
||||
int
|
||||
prepareMCsavedata ()
|
||||
{
|
||||
int offset = sizeof (saveicon);
|
||||
int size;
|
||||
|
||||
ClearSaveBuffer ();
|
||||
|
||||
/*** Copy in save icon ***/
|
||||
memcpy (savebuffer, saveicon, offset);
|
||||
|
||||
/*** And the sramcomments ***/
|
||||
sprintf (sramcomment[0], "%s SRAM", VERSIONSTR);
|
||||
sprintf (sramcomment[1], Memory.ROMName);
|
||||
memcpy (savebuffer + offset, sramcomment, 64);
|
||||
offset += 64;
|
||||
|
||||
/*** Copy SRAM size ***/
|
||||
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
|
||||
if (size > 0x20000)
|
||||
size = 0x20000;
|
||||
|
||||
memcpy (savebuffer + offset, &size, 4);
|
||||
offset += 4;
|
||||
|
||||
/*** Copy SRAM ***/
|
||||
if (size != 0)
|
||||
{
|
||||
memcpy (savebuffer + offset, Memory.SRAM, size);
|
||||
offset += size;
|
||||
}
|
||||
|
||||
// /*** Save Joypad Configuration ***/
|
||||
// memcpy (savebuffer + offset, &currconfig, 16);
|
||||
offset += 16;
|
||||
// memcpy (savebuffer + offset, &padcal, 4);
|
||||
offset += 4;
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Prepare Exportable SRAM Save Data
|
||||
* Prepare SRAM Save Data
|
||||
*
|
||||
* This sets up the save buffer for saving in a format compatible with
|
||||
* snes9x on other platforms. This is used when saving to SD / USB / SMB.
|
||||
****************************************************************************/
|
||||
int
|
||||
prepareEXPORTsavedata ()
|
||||
preparesavedata (int method)
|
||||
{
|
||||
int offset = 0;
|
||||
int size;
|
||||
int offset = 0;
|
||||
int size;
|
||||
|
||||
ClearSaveBuffer ();
|
||||
ClearSaveBuffer ();
|
||||
|
||||
/*** Copy in the sramcomments ***/
|
||||
sprintf (sramcomment[1], "%s", Memory.ROMName);
|
||||
memcpy (savebuffer + offset, sramcomment, 64);
|
||||
offset += 64;
|
||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||
{
|
||||
// Copy in save icon
|
||||
memcpy (savebuffer, saveicon, sizeof(saveicon));
|
||||
offset += sizeof (saveicon);
|
||||
}
|
||||
|
||||
// /*** Save Joypad Configuration ***/
|
||||
// memcpy (savebuffer + offset, &currconfig, 16);
|
||||
offset += 16;
|
||||
// memcpy (savebuffer + offset, &padcal, 4);
|
||||
offset += 4;
|
||||
// Copy in the sramcomments
|
||||
sprintf (sramcomment[0], "%s SRAM", VERSIONSTR);
|
||||
sprintf (sramcomment[1], Memory.ROMName);
|
||||
memcpy (savebuffer + offset, sramcomment, 64);
|
||||
offset += 64;
|
||||
|
||||
if ( offset <= 512 )
|
||||
{
|
||||
// make it a 512 byte header so it is compatible with other platforms
|
||||
offset = 512;
|
||||
if(method != METHOD_MC_SLOTA && method != METHOD_MC_SLOTB)
|
||||
{
|
||||
// make it a 512 byte header so it is compatible with other platforms
|
||||
if (offset <= 512)
|
||||
offset = 512;
|
||||
// header was longer than 512 bytes - hopefully this never happens!
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*** Copy in the SRAM ***/
|
||||
// Copy in the SRAM
|
||||
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
|
||||
if (size > 0x20000)
|
||||
size = 0x20000;
|
||||
size = 0x20000;
|
||||
|
||||
if (size != 0)
|
||||
{
|
||||
memcpy (savebuffer + offset, Memory.SRAM, size);
|
||||
offset += size;
|
||||
memcpy (savebuffer + offset, Memory.SRAM, size);
|
||||
offset += size;
|
||||
}
|
||||
|
||||
return offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
// header was longer than 512 bytes - hopefully this never happens!
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Decode Save Data
|
||||
****************************************************************************/
|
||||
void
|
||||
decodesavedata (int readsize)
|
||||
decodesavedata (int method, int readsize)
|
||||
{
|
||||
int offset;
|
||||
int size;
|
||||
char sramcomment[32];
|
||||
int offset;
|
||||
char sramsavecomment[32];
|
||||
|
||||
// Check for exportable format sram - it has the sram comment at the start
|
||||
memcpy (sramcomment, savebuffer, 32);
|
||||
|
||||
if ( (strncmp (sramcomment, "Snes9x GX 2.0", 13) == 0) || (strncmp (sramcomment, "Snes9x GX 00", 12) == 0) ) // version 2.0.XX or 00x
|
||||
{
|
||||
offset = 64;
|
||||
|
||||
// Get the control pad configuration
|
||||
// memcpy (&currconfig, savebuffer + offset, 16);
|
||||
offset += 16;
|
||||
// memcpy (&padcal, savebuffer + offset, 4);
|
||||
offset += 4;
|
||||
|
||||
// for (size = 0; size < 4; size++)
|
||||
// gcpadmap[size] = padmap[currconfig[size]];
|
||||
|
||||
// move to start of SRAM which is after the 512 byte header
|
||||
offset = 512;
|
||||
|
||||
// import the SRAM
|
||||
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
|
||||
if (size > 0x20000)
|
||||
size = 0x20000;
|
||||
size = 0x20000;
|
||||
|
||||
memcpy (Memory.SRAM, savebuffer + offset, size);
|
||||
offset += size;
|
||||
}
|
||||
else
|
||||
{
|
||||
// else, check for a v2.0 memory card format save
|
||||
offset = sizeof (saveicon);
|
||||
memcpy (sramcomment, savebuffer + offset, 32);
|
||||
|
||||
if ( strncmp (sramcomment, "Snes9x GX 2.0", 13) == 0 )
|
||||
{
|
||||
//WaitPrompt((char*) "Memory Card format save");
|
||||
offset += 64;
|
||||
memcpy (&size, savebuffer + offset, 4);
|
||||
offset += 4;
|
||||
memcpy (Memory.SRAM, savebuffer + offset, size);
|
||||
offset += size;
|
||||
|
||||
// If it is an old 2.0 format save, skip over the Settings as we
|
||||
// don't save them in SRAM now
|
||||
if ( strcmp (sramcomment, "Snes9x GX 2.0") == 0 )
|
||||
offset += sizeof (Settings);
|
||||
|
||||
// Get the control pad configuration
|
||||
// memcpy (&currconfig, savebuffer + offset, 16);
|
||||
offset += 16;
|
||||
// memcpy (&padcal, savebuffer + offset, 4);
|
||||
offset += 4;
|
||||
|
||||
// for (size = 0; size < 4; size++)
|
||||
// gcpadmap[size] = padmap[currconfig[size]];
|
||||
}
|
||||
else if ( strncmp (sramcomment, "Snes9x 1.43 SRAM (GX", 20) == 0)
|
||||
{
|
||||
// it's an older SnesGx memory card format save
|
||||
size = Memory.SRAMSize ? ( 1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
|
||||
// import the SRAM
|
||||
if ( size )
|
||||
memcpy(&Memory.SRAM[0], &savebuffer[sizeof(saveicon)+68], size);
|
||||
|
||||
// Ignore the settings saved in the file
|
||||
|
||||
// NOTE: need to add import of joypad config?? Nah.
|
||||
}
|
||||
// memory card save
|
||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||
offset = sizeof (saveicon);
|
||||
// move to start of SRAM which is after the 512 byte header
|
||||
else
|
||||
{
|
||||
// else, check for SRAM from other version/platform of snes9x
|
||||
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||
offset = 512;
|
||||
|
||||
if ( readsize == size || readsize == size + SRTC_SRAM_PAD)
|
||||
{
|
||||
//WaitPrompt("readsize=size or + SRTC_SRAM_PAD");
|
||||
// Check for exportable format sram - it has the sram comment at the start
|
||||
memcpy (sramsavecomment, savebuffer+offset, 32);
|
||||
|
||||
// version 2.0.XX or 00x found!
|
||||
if ( (strncmp (sramsavecomment, "Snes9x GX 2.0", 13) == 0)
|
||||
|| (strncmp (sramsavecomment, "Snes9x GX 00", 12) == 0) )
|
||||
{
|
||||
// import the SRAM
|
||||
memcpy (Memory.SRAM, savebuffer + offset, size);
|
||||
}
|
||||
// check for SRAM from other version/platform of snes9x
|
||||
else if ( readsize == size || readsize == size + SRTC_SRAM_PAD)
|
||||
{
|
||||
// SRAM data should be at the start of the file, just import it and
|
||||
// ignore anything after the SRAM
|
||||
memcpy (Memory.SRAM, savebuffer, size);
|
||||
}
|
||||
else if ( readsize == size + 512 )
|
||||
{
|
||||
//WaitPrompt("readsize=size+512");
|
||||
}
|
||||
else if ( readsize == size + 512 )
|
||||
{
|
||||
// SRAM has a 512 byte header - remove it, then import the SRAM,
|
||||
// ignoring anything after the SRAM
|
||||
memmove (savebuffer, savebuffer + 512, size);
|
||||
memcpy (Memory.SRAM, savebuffer, size);
|
||||
}
|
||||
else
|
||||
WaitPrompt((char*)"Incompatible SRAM save!");
|
||||
memcpy(Memory.SRAM, savebuffer+512, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
WaitPrompt((char*)"Incompatible SRAM save!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -272,7 +170,7 @@ LoadSRAM (int method, bool silent)
|
||||
|
||||
if (offset > 0)
|
||||
{
|
||||
decodesavedata (offset);
|
||||
decodesavedata (method, offset);
|
||||
S9xSoftReset();
|
||||
return 1;
|
||||
}
|
||||
@ -300,10 +198,7 @@ SaveSRAM (int method, bool silent)
|
||||
int datasize;
|
||||
int offset = 0;
|
||||
|
||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||
datasize = prepareMCsavedata ();
|
||||
else
|
||||
datasize = prepareEXPORTsavedata ();
|
||||
datasize = preparesavedata (method);
|
||||
|
||||
if ( datasize )
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user