2008-08-06 03:09:59 +02:00
|
|
|
/****************************************************************************
|
2010-01-27 23:20:37 +01:00
|
|
|
* Snes9x 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
|
2023-01-30 22:07:14 +01:00
|
|
|
* Tantric 2008-2023
|
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>
|
|
|
|
|
2010-03-22 00:43:54 +01:00
|
|
|
#include "snes9xgx.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"
|
2010-03-22 00:43:54 +01:00
|
|
|
#include "snes9x/snes9x.h"
|
|
|
|
#include "snes9x/memmap.h"
|
|
|
|
#include "snes9x/srtc.h"
|
2008-08-06 03:09:59 +02:00
|
|
|
|
2021-09-02 18:04:28 +02:00
|
|
|
bool HiROM;
|
|
|
|
bool LoROM;
|
|
|
|
|
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
|
|
|
{
|
2009-11-25 07:35:14 +01:00
|
|
|
int len = 0;
|
2009-10-02 00:35:12 +02:00
|
|
|
int device;
|
2009-11-25 07:35:14 +01:00
|
|
|
bool result = false;
|
2010-01-25 23:31:54 +01:00
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
if(!FindDevice(filepath, &device))
|
|
|
|
return 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2009-11-25 07:35:14 +01:00
|
|
|
Memory.ClearSRAM();
|
2008-08-17 07:25:33 +02:00
|
|
|
|
2009-11-25 07:35:14 +01:00
|
|
|
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
|
2008-08-07 05:25:02 +02:00
|
|
|
|
2021-09-02 18:04:28 +02:00
|
|
|
if (LoROM)
|
|
|
|
size = size < 0x70000 ? size : 0x70000;
|
|
|
|
else if (HiROM)
|
|
|
|
size = size < 0x40000 ? size : 0x40000;
|
2009-03-20 22:08:19 +01:00
|
|
|
|
2009-11-25 07:35:14 +01:00
|
|
|
if (size)
|
|
|
|
{
|
2021-09-02 18:04:28 +02:00
|
|
|
len = LoadFile((char *)Memory.SRAM, filepath, 0, size, silent);
|
2009-03-20 22:08:19 +01:00
|
|
|
|
2009-11-25 07:35:14 +01:00
|
|
|
if (len > 0)
|
2009-03-20 22:08:19 +01:00
|
|
|
{
|
2009-11-25 07:35:14 +01:00
|
|
|
if (len - size == 512)
|
|
|
|
memmove(Memory.SRAM, Memory.SRAM + 512, size);
|
|
|
|
|
2010-01-27 23:08:56 +01:00
|
|
|
if (Settings.SRTC || Settings.SPC7110RTC)
|
|
|
|
{
|
|
|
|
int pathlen = strlen(filepath);
|
|
|
|
filepath[pathlen-3] = 'r';
|
|
|
|
filepath[pathlen-2] = 't';
|
|
|
|
filepath[pathlen-1] = 'c';
|
2018-08-28 22:47:49 +02:00
|
|
|
LoadFile((char *)RTCData.reg, filepath, 0, 20, silent);
|
2010-01-27 23:08:56 +01:00
|
|
|
}
|
2009-11-25 07:35:14 +01:00
|
|
|
result = true;
|
2009-03-20 22:08:19 +01:00
|
|
|
}
|
2009-11-25 07:35:14 +01:00
|
|
|
else if(!silent)
|
2009-03-20 22:08:19 +01:00
|
|
|
{
|
2009-11-25 07:35:14 +01:00
|
|
|
// if we reached here, nothing was done!
|
|
|
|
ErrorPrompt("SRAM file not found");
|
2009-03-20 22:08:19 +01:00
|
|
|
}
|
2008-08-07 05:25:02 +02:00
|
|
|
S9xSoftReset();
|
2008-09-27 09:13:52 +02:00
|
|
|
}
|
2009-11-25 07:35:14 +01:00
|
|
|
return result;
|
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];
|
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;
|
|
|
|
|
2019-01-30 04:31:25 +01:00
|
|
|
if (!GCSettings.AppendAuto)
|
|
|
|
return false;
|
|
|
|
|
2009-04-09 09:43:59 +02:00
|
|
|
// look for file with no number or Auto appended
|
2010-01-25 23:31:54 +01:00
|
|
|
if(!MakeFilePath(filepath, FILE_SRAM, Memory.ROMFilename, -1))
|
2009-04-09 09:43:59 +02:00
|
|
|
return false;
|
|
|
|
|
2010-01-25 23:31:54 +01:00
|
|
|
if(LoadSRAM(filepath, silent))
|
2009-04-09 09:43:59 +02:00
|
|
|
return true;
|
2010-01-25 23:31:54 +01:00
|
|
|
|
2009-04-09 09:43:59 +02:00
|
|
|
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-11-25 07:35:14 +01:00
|
|
|
if (Settings.SuperFX && Memory.ROMType < 0x15) // doesn't have SRAM
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Settings.SA1 && Memory.ROMType == 0x34) // doesn't have SRAM
|
|
|
|
return true;
|
|
|
|
|
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
|
|
|
|
2021-09-02 18:04:28 +02:00
|
|
|
if (LoROM)
|
|
|
|
size = size < 0x70000 ? size : 0x70000;
|
|
|
|
else if (HiROM)
|
|
|
|
size = size < 0x40000 ? size : 0x40000;
|
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-11-25 07:35:14 +01:00
|
|
|
offset = SaveFile((char *)Memory.SRAM, filepath, size, silent);
|
2009-03-20 22:08:19 +01:00
|
|
|
|
2010-01-27 23:08:56 +01:00
|
|
|
if (Settings.SRTC || Settings.SPC7110RTC)
|
|
|
|
{
|
|
|
|
int pathlen = strlen(filepath);
|
|
|
|
filepath[pathlen-3] = 'r';
|
|
|
|
filepath[pathlen-2] = 't';
|
|
|
|
filepath[pathlen-1] = 'c';
|
|
|
|
SaveFile((char *)RTCData.reg, filepath, 20, silent);
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
2010-01-25 23:31:54 +01:00
|
|
|
// look for file with no number or Auto appended
|
|
|
|
if(!MakeFilePath(filepath, FILE_SRAM, Memory.ROMFilename, -1))
|
2009-03-16 07:58:50 +01:00
|
|
|
return false;
|
|
|
|
|
2010-01-25 23:31:54 +01:00
|
|
|
FILE * fp = fopen (filepath, "rb");
|
|
|
|
|
|
|
|
if(fp) // file found
|
|
|
|
{
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!MakeFilePath(filepath, FILE_SRAM, Memory.ROMFilename, 0))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-02 00:35:12 +02:00
|
|
|
return SaveSRAM(filepath, silent);
|
2009-03-11 18:28:37 +01:00
|
|
|
}
|