mirror of
https://github.com/dborth/snes9xgx.git
synced 2025-01-12 19:29:08 +01:00
rewrite SRAM code
This commit is contained in:
parent
a5a8dba908
commit
8ff4c5a69d
@ -32,83 +32,43 @@ extern unsigned short gcpadmap[];
|
|||||||
char sramcomment[2][32];
|
char sramcomment[2][32];
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Prepare Memory Card SRAM Save Data
|
* Prepare 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
|
|
||||||
*
|
*
|
||||||
* This sets up the save buffer for saving in a format compatible with
|
* 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.
|
* snes9x on other platforms. This is used when saving to SD / USB / SMB.
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
int
|
int
|
||||||
prepareEXPORTsavedata ()
|
preparesavedata (int method)
|
||||||
{
|
{
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
int size;
|
int size;
|
||||||
|
|
||||||
ClearSaveBuffer ();
|
ClearSaveBuffer ();
|
||||||
|
|
||||||
/*** Copy in the sramcomments ***/
|
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||||
sprintf (sramcomment[1], "%s", Memory.ROMName);
|
{
|
||||||
|
// Copy in save icon
|
||||||
|
memcpy (savebuffer, saveicon, sizeof(saveicon));
|
||||||
|
offset += sizeof (saveicon);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy in the sramcomments
|
||||||
|
sprintf (sramcomment[0], "%s SRAM", VERSIONSTR);
|
||||||
|
sprintf (sramcomment[1], Memory.ROMName);
|
||||||
memcpy (savebuffer + offset, sramcomment, 64);
|
memcpy (savebuffer + offset, sramcomment, 64);
|
||||||
offset += 64;
|
offset += 64;
|
||||||
|
|
||||||
// /*** Save Joypad Configuration ***/
|
if(method != METHOD_MC_SLOTA && method != METHOD_MC_SLOTB)
|
||||||
// memcpy (savebuffer + offset, &currconfig, 16);
|
|
||||||
offset += 16;
|
|
||||||
// memcpy (savebuffer + offset, &padcal, 4);
|
|
||||||
offset += 4;
|
|
||||||
|
|
||||||
if ( offset <= 512 )
|
|
||||||
{
|
{
|
||||||
// make it a 512 byte header so it is compatible with other platforms
|
// make it a 512 byte header so it is compatible with other platforms
|
||||||
|
if (offset <= 512)
|
||||||
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;
|
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||||
|
|
||||||
if (size > 0x20000)
|
if (size > 0x20000)
|
||||||
@ -121,118 +81,56 @@ prepareEXPORTsavedata ()
|
|||||||
}
|
}
|
||||||
|
|
||||||
return offset;
|
return offset;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// header was longer than 512 bytes - hopefully this never happens!
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Decode Save Data
|
* Decode Save Data
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
void
|
void
|
||||||
decodesavedata (int readsize)
|
decodesavedata (int method, int readsize)
|
||||||
{
|
{
|
||||||
int offset;
|
int offset;
|
||||||
int size;
|
char sramsavecomment[32];
|
||||||
char sramcomment[32];
|
|
||||||
|
|
||||||
// Check for exportable format sram - it has the sram comment at the start
|
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
||||||
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;
|
|
||||||
|
|
||||||
if (size > 0x20000)
|
if (size > 0x20000)
|
||||||
size = 0x20000;
|
size = 0x20000;
|
||||||
|
|
||||||
memcpy (Memory.SRAM, savebuffer + offset, size);
|
// memory card save
|
||||||
offset += size;
|
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// else, check for a v2.0 memory card format save
|
|
||||||
offset = sizeof (saveicon);
|
offset = sizeof (saveicon);
|
||||||
memcpy (sramcomment, savebuffer + offset, 32);
|
// move to start of SRAM which is after the 512 byte header
|
||||||
|
|
||||||
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.
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
offset = 512;
|
||||||
// else, check for SRAM from other version/platform of snes9x
|
|
||||||
size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
|
||||||
|
|
||||||
if ( readsize == size || readsize == size + 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)
|
||||||
{
|
{
|
||||||
//WaitPrompt("readsize=size or + SRTC_SRAM_PAD");
|
|
||||||
// SRAM data should be at the start of the file, just import it and
|
// SRAM data should be at the start of the file, just import it and
|
||||||
// ignore anything after the SRAM
|
// ignore anything after the SRAM
|
||||||
memcpy (Memory.SRAM, savebuffer, size);
|
memcpy (Memory.SRAM, savebuffer, size);
|
||||||
}
|
}
|
||||||
else if ( readsize == size + 512 )
|
else if ( readsize == size + 512 )
|
||||||
{
|
{
|
||||||
//WaitPrompt("readsize=size+512");
|
|
||||||
// SRAM has a 512 byte header - remove it, then import the SRAM,
|
// SRAM has a 512 byte header - remove it, then import the SRAM,
|
||||||
// ignoring anything after the SRAM
|
// ignoring anything after the SRAM
|
||||||
memmove (savebuffer, savebuffer + 512, size);
|
memcpy(Memory.SRAM, savebuffer+512, size);
|
||||||
memcpy (Memory.SRAM, savebuffer, size);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
WaitPrompt((char*)"Incompatible SRAM save!");
|
WaitPrompt((char*)"Incompatible SRAM save!");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -272,7 +170,7 @@ LoadSRAM (int method, bool silent)
|
|||||||
|
|
||||||
if (offset > 0)
|
if (offset > 0)
|
||||||
{
|
{
|
||||||
decodesavedata (offset);
|
decodesavedata (method, offset);
|
||||||
S9xSoftReset();
|
S9xSoftReset();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -300,10 +198,7 @@ SaveSRAM (int method, bool silent)
|
|||||||
int datasize;
|
int datasize;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
|
|
||||||
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
|
datasize = preparesavedata (method);
|
||||||
datasize = prepareMCsavedata ();
|
|
||||||
else
|
|
||||||
datasize = prepareEXPORTsavedata ();
|
|
||||||
|
|
||||||
if ( datasize )
|
if ( datasize )
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user