snes9xgx/source/sram.cpp

184 lines
3.7 KiB
C++
Raw Normal View History

/****************************************************************************
2010-01-27 23:20:37 +01:00
* Snes9x Nintendo Wii/Gamecube Port
2008-09-05 00:42:33 +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
*
* sram.cpp
*
* SRAM save/load/import/export handling
***************************************************************************/
#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"
#include "input.h"
2010-03-22 00:43:54 +01:00
#include "snes9x/snes9x.h"
#include "snes9x/memmap.h"
#include "snes9x/srtc.h"
bool HiROM;
bool LoROM;
/****************************************************************************
* Load SRAM
***************************************************************************/
bool
2009-10-02 00:35:12 +02:00
LoadSRAM (char * filepath, bool silent)
{
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;
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;
if (LoROM)
size = size < 0x70000 ? size : 0x70000;
else if (HiROM)
size = size < 0x40000 ? size : 0x40000;
2009-11-25 07:35:14 +01:00
if (size)
{
len = LoadFile((char *)Memory.SRAM, filepath, 0, size, silent);
2009-11-25 07:35:14 +01:00
if (len > 0)
{
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';
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-11-25 07:35:14 +01:00
else if(!silent)
{
2009-11-25 07:35:14 +01:00
// if we reached here, nothing was done!
ErrorPrompt("SRAM file not found");
}
S9xSoftReset();
2008-09-27 09:13:52 +02:00
}
2009-11-25 07:35:14 +01:00
return result;
}
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
{
char filepath[MAXPATHLEN];
2009-03-11 18:28:37 +01: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))
return true;
if (!GCSettings.AppendAuto)
return false;
// 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))
return false;
2010-01-25 23:31:54 +01:00
if(LoadSRAM(filepath, silent))
return true;
2010-01-25 23:31:54 +01:00
return false;
2009-03-11 18:28:37 +01:00
}
/****************************************************************************
* Save SRAM
***************************************************************************/
bool
2009-10-02 00:35:12 +02:00
SaveSRAM (char * filepath, bool silent)
{
bool retval = false;
int offset = 0;
2009-10-02 00:35:12 +02:00
int device;
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;
// determine SRAM size
int size = Memory.SRAMSize ? (1 << (Memory.SRAMSize + 3)) * 128 : 0;
2008-09-27 09:13:52 +02:00
if (LoROM)
size = size < 0x70000 ? size : 0x70000;
else if (HiROM)
size = size < 0x40000 ? size : 0x40000;
if (size > 0)
{
2009-11-25 07:35:14 +01:00
offset = SaveFile((char *)Memory.SRAM, filepath, size, silent);
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);
}
if (offset > 0)
{
2009-03-11 18:28:37 +01:00
if (!silent)
InfoPrompt("Save successful");
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
}
return retval;
}
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
}