Split GBA Reset and Eject logic into two sets
This allows solving some crashes and provides more flexibility in how GBA cartridges change state between soft and hard resets. Since save states including GBA data do not carry over the original save file path, and the GBA cartridge is being reset along with the other parts of the system, this is needed to avoid losing the GBA state on reset following a state load, while preserving the behavior where cartridges are ejected when calling Stop().
This commit is contained in:
parent
f380767fab
commit
22d11209b0
|
@ -76,10 +76,14 @@ void DeInit()
|
||||||
}
|
}
|
||||||
|
|
||||||
void Reset()
|
void Reset()
|
||||||
|
{
|
||||||
|
// do nothing, we don't want to clear GBA SRAM on reset
|
||||||
|
}
|
||||||
|
|
||||||
|
void Eject()
|
||||||
{
|
{
|
||||||
if (SRAMFile) fclose(SRAMFile);
|
if (SRAMFile) fclose(SRAMFile);
|
||||||
if (SRAM) delete[] SRAM;
|
if (SRAM) delete[] SRAM;
|
||||||
|
|
||||||
SRAM = NULL;
|
SRAM = NULL;
|
||||||
SRAMFile = NULL;
|
SRAMFile = NULL;
|
||||||
SRAMLength = 0;
|
SRAMLength = 0;
|
||||||
|
@ -524,17 +528,32 @@ void DeInit()
|
||||||
|
|
||||||
void Reset()
|
void Reset()
|
||||||
{
|
{
|
||||||
CartInserted = false;
|
// Do not reset cartridge ROM.
|
||||||
HasSolarSensor = false;
|
// Prefer keeping the inserted cartridge on reset.
|
||||||
if (CartROM) delete[] CartROM;
|
// This allows resetting a DS game without losing GBA state,
|
||||||
CartROM = NULL;
|
// and resetting to firmware without the slot being emptied.
|
||||||
CartROMSize = 0;
|
// The Stop function will clear the cartridge state via Eject().
|
||||||
CartGPIO = {};
|
|
||||||
|
|
||||||
GBACart_SRAM::Reset();
|
GBACart_SRAM::Reset();
|
||||||
GBACart_SolarSensor::Reset();
|
GBACart_SolarSensor::Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Eject()
|
||||||
|
{
|
||||||
|
if (CartROM) delete[] CartROM;
|
||||||
|
|
||||||
|
CartInserted = false;
|
||||||
|
HasSolarSensor = false;
|
||||||
|
CartROM = NULL;
|
||||||
|
CartROMSize = 0;
|
||||||
|
CartCRC = NULL;
|
||||||
|
CartID = NULL;
|
||||||
|
CartGPIO = {};
|
||||||
|
|
||||||
|
GBACart_SRAM::Eject();
|
||||||
|
Reset();
|
||||||
|
}
|
||||||
|
|
||||||
void DoSavestate(Savestate* file)
|
void DoSavestate(Savestate* file)
|
||||||
{
|
{
|
||||||
file->Section("GBAC"); // Game Boy Advance Cartridge
|
file->Section("GBAC"); // Game Boy Advance Cartridge
|
||||||
|
@ -545,10 +564,16 @@ void DoSavestate(Savestate* file)
|
||||||
// since unlike with DS, it's not loaded in advance
|
// since unlike with DS, it's not loaded in advance
|
||||||
|
|
||||||
file->Var32(&CartROMSize);
|
file->Var32(&CartROMSize);
|
||||||
if (!CartROMSize) return; // no GBA cartridge state? nothing to do here.
|
if (!CartROMSize) // no GBA cartridge state? nothing to do here
|
||||||
|
{
|
||||||
|
// do eject the cartridge if something is inserted
|
||||||
|
Eject();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
u32 oldCRC = CartCRC;
|
u32 oldCRC = CartCRC;
|
||||||
file->Var32(&CartCRC);
|
file->Var32(&CartCRC);
|
||||||
|
|
||||||
if (CartCRC != oldCRC)
|
if (CartCRC != oldCRC)
|
||||||
{
|
{
|
||||||
// delete and reallocate ROM so that it is zero-padded to its full length
|
// delete and reallocate ROM so that it is zero-padded to its full length
|
||||||
|
|
|
@ -57,10 +57,12 @@ extern bool CartInserted;
|
||||||
extern bool HasSolarSensor;
|
extern bool HasSolarSensor;
|
||||||
extern u8* CartROM;
|
extern u8* CartROM;
|
||||||
extern u32 CartROMSize;
|
extern u32 CartROMSize;
|
||||||
|
extern u32 CartCRC;
|
||||||
|
|
||||||
bool Init();
|
bool Init();
|
||||||
void DeInit();
|
void DeInit();
|
||||||
void Reset();
|
void Reset();
|
||||||
|
void Eject();
|
||||||
|
|
||||||
void DoSavestate(Savestate* file);
|
void DoSavestate(Savestate* file);
|
||||||
bool LoadROM(const char* path, const char* sram);
|
bool LoadROM(const char* path, const char* sram);
|
||||||
|
|
|
@ -1694,6 +1694,7 @@ void Stop(bool internal)
|
||||||
RunningSomething = false;
|
RunningSomething = false;
|
||||||
|
|
||||||
// eject any inserted GBA cartridge
|
// eject any inserted GBA cartridge
|
||||||
|
GBACart::Eject();
|
||||||
ROMPath[1][0] = '\0';
|
ROMPath[1][0] = '\0';
|
||||||
|
|
||||||
uiWindowSetTitle(MainWindow, "melonDS " MELONDS_VERSION);
|
uiWindowSetTitle(MainWindow, "melonDS " MELONDS_VERSION);
|
||||||
|
@ -1834,6 +1835,8 @@ void LoadState(int slot)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 oldGBACartCRC = GBACart::CartCRC;
|
||||||
|
|
||||||
// backup
|
// backup
|
||||||
Savestate* backup = new Savestate("timewarp.mln", true);
|
Savestate* backup = new Savestate("timewarp.mln", true);
|
||||||
NDS::DoSavestate(backup);
|
NDS::DoSavestate(backup);
|
||||||
|
@ -1870,9 +1873,24 @@ void LoadState(int slot)
|
||||||
NDS::RelocateSave(SRAMPath[0], false);
|
NDS::RelocateSave(SRAMPath[0], false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool loadedPartialGBAROM = false;
|
||||||
|
|
||||||
|
// in case we have a GBA cart inserted, and the GBA ROM changes
|
||||||
|
// due to having loaded a save state, we do not want to reload
|
||||||
|
// the previous cartridge on reset, or commit writes to any
|
||||||
|
// loaded save file. therefore, their paths are "nulled".
|
||||||
|
if (GBACart::CartInserted && GBACart::CartCRC != oldGBACartCRC)
|
||||||
|
{
|
||||||
|
ROMPath[1][0] = '\0';
|
||||||
|
SRAMPath[1][0] = '\0';
|
||||||
|
loadedPartialGBAROM = true;
|
||||||
|
}
|
||||||
|
|
||||||
char msg[64];
|
char msg[64];
|
||||||
if (slot > 0) sprintf(msg, "State loaded from slot %d", slot);
|
if (slot > 0) sprintf(msg, "State loaded from slot %d%s",
|
||||||
else sprintf(msg, "State loaded from file");
|
slot, loadedPartialGBAROM ? " (GBA ROM header only)" : "");
|
||||||
|
else sprintf(msg, "State loaded from file%s",
|
||||||
|
loadedPartialGBAROM ? " (GBA ROM header only)" : "");
|
||||||
OSD::AddMessage(0, msg);
|
OSD::AddMessage(0, msg);
|
||||||
|
|
||||||
SavestateLoaded = true;
|
SavestateLoaded = true;
|
||||||
|
|
Loading…
Reference in New Issue