2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
2008-09-12 07:28:40 +02:00
|
|
|
* Snes9x 1.51 Nintendo Wii/Gamecube Port
|
2008-09-05 00:42:33 +02:00
|
|
|
*
|
2008-08-06 03:09:59 +02:00
|
|
|
* crunchy2 April 2007-July 2007
|
2008-09-23 06:13:33 +02:00
|
|
|
* Michniewski 2008
|
2009-03-11 18:28:37 +01:00
|
|
|
* Tantric 2008-2009
|
2008-08-06 03:09:59 +02:00
|
|
|
*
|
|
|
|
* sram.cpp
|
|
|
|
*
|
|
|
|
* SRAM save/load/import/export handling
|
2008-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
|
|
|
|
2008-08-06 03:09:59 +02:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ogcsys.h>
|
|
|
|
|
|
|
|
#include "snes9x.h"
|
|
|
|
#include "memmap.h"
|
|
|
|
#include "srtc.h"
|
2008-08-10 05:14:39 +02:00
|
|
|
|
2008-09-09 19:36:48 +02:00
|
|
|
#include "snes9xGX.h"
|
2009-03-21 03:01:40 +01:00
|
|
|
#include "memcardop.h"
|
2009-03-11 18:28:37 +01:00
|
|
|
#include "menu.h"
|
2008-12-18 19:36:30 +01:00
|
|
|
#include "fileop.h"
|
2009-03-11 18:28:37 +01:00
|
|
|
#include "filebrowser.h"
|
2009-01-05 23:23:41 +01:00
|
|
|
#include "input.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Load SRAM
|
2008-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
2009-01-05 23:23:41 +01:00
|
|
|
bool
|
2009-10-02 00:35:12 +02:00
|
|
|
LoadSRAM (char * filepath, bool silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-11-12 08:50:39 +01:00
|
|
|
int offset = 0;
|
2009-10-02 00:35:12 +02:00
|
|
|
int device;
|
|
|
|
|
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2008-11-12 08:50:39 +01:00
|
|
|
AllocSaveBuffer();
|
2008-08-17 07:25:33 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
offset = LoadFile(filepath, silent);
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
2009-03-20 22:08:19 +01:00
|
|
|
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
|
|
|
|
|
|
|
if (size > 0x20000)
|
|
|
|
size = 0x20000;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if (device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB ||
|
2009-03-21 03:01:40 +01:00
|
|
|
offset == size + 512 || offset == size + 512 + SRTC_SRAM_PAD)
|
2009-03-20 22:08:19 +01:00
|
|
|
{
|
|
|
|
// SRAM has a 512 byte header - remove it, then import the SRAM,
|
|
|
|
// ignoring anything after the SRAM
|
|
|
|
memcpy(Memory.SRAM, savebuffer+512, size);
|
|
|
|
}
|
2009-03-21 03:01:40 +01:00
|
|
|
else if (offset == size || offset == size + SRTC_SRAM_PAD)
|
2009-03-20 22:08:19 +01:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
{
|
|
|
|
ErrorPrompt("Incompatible SRAM save!");
|
|
|
|
}
|
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
S9xSoftReset();
|
2008-11-12 08:50:39 +01:00
|
|
|
FreeSaveBuffer ();
|
2009-01-05 23:23:41 +01:00
|
|
|
return true;
|
2008-09-27 09:13:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-12 08:50:39 +01:00
|
|
|
FreeSaveBuffer ();
|
|
|
|
|
2008-09-27 09:13:52 +02:00
|
|
|
// if we reached here, nothing was done!
|
|
|
|
if(!silent)
|
2009-03-11 18:28:37 +01:00
|
|
|
ErrorPrompt("SRAM file not found");
|
2008-09-27 09:13:52 +02:00
|
|
|
|
2009-01-05 23:23:41 +01:00
|
|
|
return false;
|
2008-09-27 09:13:52 +02:00
|
|
|
}
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
|
|
|
|
2009-03-11 18:28:37 +01:00
|
|
|
bool
|
2009-10-02 00:35:12 +02:00
|
|
|
LoadSRAMAuto (bool silent)
|
2009-03-11 18:28:37 +01:00
|
|
|
{
|
2009-04-09 09:43:59 +02:00
|
|
|
char filepath[MAXPATHLEN];
|
|
|
|
char filepath2[MAXPATHLEN];
|
2009-03-11 18:28:37 +01:00
|
|
|
|
2009-04-09 09:43:59 +02:00
|
|
|
// look for Auto save file
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!MakeFilePath(filepath, FILE_SRAM, Memory.ROMFilename, 0))
|
2009-03-20 09:26:10 +01:00
|
|
|
return false;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if (LoadSRAM(filepath, silent))
|
2009-04-09 09:43:59 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// look for file with no number or Auto appended
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!MakeFilePath(filepath2, FILE_SRAM, Memory.ROMFilename, -1))
|
2009-04-09 09:43:59 +02:00
|
|
|
return false;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(LoadSRAM(filepath2, silent))
|
2009-04-09 09:43:59 +02:00
|
|
|
{
|
|
|
|
// rename this file - append Auto
|
2009-10-02 00:35:12 +02:00
|
|
|
rename(filepath2, filepath); // rename file (to avoid duplicates)
|
2009-04-09 09:43:59 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2009-03-11 18:28:37 +01:00
|
|
|
}
|
|
|
|
|
2008-08-07 05:25:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Save SRAM
|
2008-09-12 07:28:40 +02:00
|
|
|
***************************************************************************/
|
2008-08-07 05:25:02 +02:00
|
|
|
bool
|
2009-10-02 00:35:12 +02:00
|
|
|
SaveSRAM (char * filepath, bool silent)
|
2008-08-06 03:09:59 +02:00
|
|
|
{
|
2008-08-07 05:25:02 +02:00
|
|
|
bool retval = false;
|
|
|
|
int offset = 0;
|
2009-10-02 00:35:12 +02:00
|
|
|
int device;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return 0;
|
2008-11-12 08:50:39 +01:00
|
|
|
|
2009-03-20 22:08:19 +01:00
|
|
|
// determine SRAM size
|
|
|
|
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
2008-09-27 09:13:52 +02:00
|
|
|
|
2009-03-20 22:08:19 +01:00
|
|
|
if (size > 0x20000)
|
|
|
|
size = 0x20000;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2009-03-20 22:08:19 +01:00
|
|
|
if (size > 0)
|
2008-08-07 05:25:02 +02:00
|
|
|
{
|
2009-10-02 00:35:12 +02:00
|
|
|
if(device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB)
|
2009-03-20 22:08:19 +01:00
|
|
|
{
|
|
|
|
// Set the sramcomments
|
|
|
|
char sramcomment[2][32];
|
|
|
|
memset(sramcomment, 0, 64);
|
|
|
|
sprintf (sramcomment[0], "%s SRAM", APPNAME);
|
|
|
|
sprintf (sramcomment[1], Memory.ROMName);
|
|
|
|
SetMCSaveComments(sramcomment);
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocSaveBuffer ();
|
|
|
|
// copy in the SRAM, leaving a 512 byte header (for compatibility with other platforms)
|
|
|
|
memcpy(savebuffer+512, Memory.SRAM, size);
|
2009-10-02 00:35:12 +02:00
|
|
|
offset = SaveFile(filepath, size+512, silent);
|
2009-03-20 22:08:19 +01:00
|
|
|
FreeSaveBuffer ();
|
2008-08-07 05:25:02 +02:00
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
2009-03-11 18:28:37 +01:00
|
|
|
if (!silent)
|
|
|
|
InfoPrompt("Save successful");
|
2008-08-07 05:25:02 +02:00
|
|
|
retval = true;
|
|
|
|
}
|
|
|
|
}
|
2008-09-09 06:13:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!silent)
|
2009-03-11 18:28:37 +01:00
|
|
|
ErrorPrompt("No SRAM data to save!");
|
2008-09-09 06:13:42 +02:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
return retval;
|
2008-08-06 03:09:59 +02:00
|
|
|
}
|
2009-03-11 18:28:37 +01:00
|
|
|
|
|
|
|
bool
|
2009-10-02 00:35:12 +02:00
|
|
|
SaveSRAMAuto (bool silent)
|
2009-03-11 18:28:37 +01:00
|
|
|
{
|
|
|
|
char filepath[1024];
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!MakeFilePath(filepath, FILE_SRAM, Memory.ROMFilename, 0))
|
2009-03-16 07:58:50 +01:00
|
|
|
return false;
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
return SaveSRAM(filepath, silent);
|
2009-03-11 18:28:37 +01:00
|
|
|
}
|