Commit a0cec107 authored by retro-wertz's avatar retro-wertz Committed by Rafael Kitover
Browse files

Update GBA save type detection and cleanup...

1. EEPROM: move eepromInUse and eepromSize from EepromReset() to eepromInit()
to avoid re-initializing during a reset (makes item below redundant)
2. Remove gbaSaveType variable - this is now redundant due to change above
which probably was added for this reason since games using eeprom fails with
gamepak error after a reset.
3. Add labels to identify cpuSaveTypes
4. libretro: remove workaround for eeprom reset issue (#1), do not apply
custom gbPalettes if not running in GB, change vram size to 0x18000 in
memory map
parent 2a796d48
......@@ -206,7 +206,7 @@ int rewindSaveNeeded = 0;
int rewindTimer = 0;
int rewindTopPos;
int rtcEnabled;
int saveType = 0;
int saveType = GBA_SAVE_AUTO;
int screenMessage;
int sensorX;
int sensorY;
......
......@@ -30,6 +30,8 @@ variable_desc eepromSaveData[] = {
void eepromInit()
{
eepromInUse = false;
eepromSize = 512;
memset(eepromData, 255, sizeof(eepromData));
}
......@@ -39,8 +41,6 @@ void eepromReset()
eepromByte = 0;
eepromBits = 0;
eepromAddress = 0;
eepromInUse = false;
eepromSize = 512;
}
#ifdef __LIBRETRO__
......
......@@ -152,17 +152,16 @@ uint8_t flashRead(uint32_t address)
void flashSaveDecide(uint32_t address, uint8_t byte)
{
if (saveType == 1)
if (saveType == GBA_SAVE_EEPROM)
return;
if (cpuSramEnabled && cpuFlashEnabled) {
// log("Deciding save type %08x\n", address);
if (address == 0x0e005555) {
saveType = 3;
saveType = GBA_SAVE_FLASH;
cpuSramEnabled = false;
cpuSaveGameFunc = flashWrite;
} else {
saveType = 2;
saveType = GBA_SAVE_SRAM;
cpuFlashEnabled = false;
cpuSaveGameFunc = sramWrite;
}
......@@ -176,7 +175,7 @@ void flashSaveDecide(uint32_t address, uint8_t byte)
void flashDelayedWrite(uint32_t address, uint8_t byte)
{
saveType = 3;
saveType = GBA_SAVE_FLASH;
cpuSaveGameFunc = flashWrite;
flashWrite(address, byte);
}
......
......@@ -56,7 +56,6 @@ int dummyAddress = 0;
bool cpuBreakLoop = false;
int cpuNextEvent = 0;
int gbaSaveType = 0; // used to remember the save type on reset
bool intState = false;
bool stopState = false;
bool holdState = false;
......@@ -672,10 +671,8 @@ bool CPUReadState(const uint8_t* data, unsigned size)
CPUUpdateWindow0();
CPUUpdateWindow1();
gbaSaveType = 0;
SetSaveType(saveType);
if (eepromInUse)
gbaSaveType = 3;
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
if (armState) {
......@@ -894,10 +891,8 @@ static bool CPUReadState(gzFile gzFile)
CPUUpdateRenderBuffers(true);
CPUUpdateWindow0();
CPUUpdateWindow1();
gbaSaveType = 0;
SetSaveType(saveType);
if (eepromInUse)
gbaSaveType = 3;
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
if (armState) {
......@@ -964,21 +959,7 @@ bool CPUExportEepromFile(const char* fileName)
bool CPUWriteBatteryFile(const char* fileName)
{
if (gbaSaveType == 0) {
if (eepromInUse)
gbaSaveType = 3;
else
switch (saveType) {
case 1:
gbaSaveType = 1;
break;
case 2:
gbaSaveType = 2;
break;
}
}
if ((gbaSaveType) && (gbaSaveType != 5)) {
if ((saveType) && (saveType != GBA_SAVE_NONE)) {
FILE* file = fopen(fileName, "wb");
if (!file) {
......@@ -988,19 +969,19 @@ bool CPUWriteBatteryFile(const char* fileName)
}
// only save if Flash/Sram in use or EEprom in use
if (gbaSaveType != 3) {
if (gbaSaveType == 2) {
if (!eepromInUse) {
if (saveType == GBA_SAVE_FLASH) { // save flash type
if (fwrite(flashSaveMemory, 1, flashSize, file) != (size_t)flashSize) {
fclose(file);
return false;
}
} else {
} else if (saveType == GBA_SAVE_SRAM) { // save sram type
if (fwrite(flashSaveMemory, 1, 0x8000, file) != 0x8000) {
fclose(file);
return false;
}
}
} else {
} else { // save eeprom type
if (fwrite(eepromData, 1, eepromSize, file) != (size_t)eepromSize) {
fclose(file);
return false;
......@@ -1161,7 +1142,7 @@ bool CPUWriteGSASnapshot(const char* fileName,
fwrite(buffer, 1, 4, file); // notes length
fwrite(notes, 1, strlen(notes), file);
int saveSize = 0x10000;
if (gbaSaveType == 2)
if (saveType == GBA_SAVE_FLASH)
saveSize = flashSize;
int totalSize = saveSize + 0x1c;
......@@ -3254,7 +3235,6 @@ void CPUInit(const char* biosFileName, bool useBiosFile)
cpuBiosSwapped = true;
}
#endif
gbaSaveType = 0;
eepromInUse = 0;
useBios = false;
......@@ -3342,72 +3322,50 @@ void CPUInit(const char* biosFileName, bool useBiosFile)
void SetSaveType(int st)
{
switch (st) {
case 0: // automatic
case GBA_SAVE_AUTO:
cpuSramEnabled = true;
cpuFlashEnabled = true;
cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = false;
gbaSaveType = 0;
cpuSaveGameFunc = flashSaveDecide;
break;
case 1: // EEPROM
case GBA_SAVE_EEPROM:
cpuSramEnabled = false;
cpuFlashEnabled = false;
cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = false;
gbaSaveType = 3;
// EEPROM usage is automatically detected
break;
case 2: // SRAM
case GBA_SAVE_SRAM:
cpuSramEnabled = true;
cpuFlashEnabled = false;
cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false;
cpuSaveGameFunc = sramDelayedWrite; // to insure we detect the write
gbaSaveType = 1;
break;
case 3: // FLASH
case GBA_SAVE_FLASH:
cpuSramEnabled = false;
cpuFlashEnabled = true;
cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false;
cpuSaveGameFunc = flashDelayedWrite; // to insure we detect the write
gbaSaveType = 2;
break;
case 4: // EEPROM+Sensor
case GBA_SAVE_EEPROM_SENSOR:
cpuSramEnabled = false;
cpuFlashEnabled = false;
cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = true;
// EEPROM usage is automatically detected
gbaSaveType = 3;
break;
case 5: // NONE
case GBA_SAVE_NONE:
cpuSramEnabled = false;
cpuFlashEnabled = false;
cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false;
// no save at all
gbaSaveType = 5;
break;
}
}
void CPUReset()
{
if (gbaSaveType == 0) {
if (eepromInUse)
gbaSaveType = 3;
else
switch (saveType) {
case 2:
gbaSaveType = 1;
break;
case 3:
gbaSaveType = 2;
break;
}
}
switch (CheckEReaderRegion()) {
case 1: //US
EReaderWriteMemory(0x8009134, 0x46C0DFE0);
......
......@@ -18,6 +18,15 @@ const uint64_t TICKS_PER_SECOND = 16777216;
#define SAVE_GAME_VERSION_10 10
#define SAVE_GAME_VERSION SAVE_GAME_VERSION_10
enum {
GBA_SAVE_AUTO = 0,
GBA_SAVE_EEPROM,
GBA_SAVE_SRAM,
GBA_SAVE_FLASH,
GBA_SAVE_EEPROM_SENSOR,
GBA_SAVE_NONE
};
typedef struct {
uint8_t* address;
uint32_t mask;
......
......@@ -9,7 +9,7 @@ uint8_t sramRead(uint32_t address)
}
void sramDelayedWrite(uint32_t address, uint8_t byte)
{
saveType = 2;
saveType = GBA_SAVE_SRAM;
cpuSaveGameFunc = sramWrite;
sramWrite(address, byte);
}
......
......@@ -174,6 +174,9 @@ static void set_gbPalette(void)
{
const uint16_t *pal = defaultGBPalettes[current_gbPalette];
if (type != IMAGE_GB)
return;
if (gbCgbMode || gbSgbMode)
return;
......@@ -306,9 +309,9 @@ void* retro_get_memory_data(unsigned id)
if (type == IMAGE_GBA) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4))
if ((saveType == GBA_SAVE_EEPROM) | (saveType == GBA_SAVE_EEPROM_SENSOR))
return eepromData;
if ((saveType == 2) | (saveType == 3))
if ((saveType == GBA_SAVE_SRAM) | (saveType == GBA_SAVE_FLASH))
return flashSaveMemory;
return NULL;
case RETRO_MEMORY_SYSTEM_RAM:
......@@ -338,15 +341,15 @@ size_t retro_get_memory_size(unsigned id)
if (type == IMAGE_GBA) {
switch (id) {
case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4))
if ((saveType == GBA_SAVE_EEPROM) | (saveType == GBA_SAVE_EEPROM_SENSOR))
return eepromSize;
if ((saveType == 2) | (saveType == 3))
return flashSize;
if ((saveType == GBA_SAVE_SRAM) | (saveType == GBA_SAVE_FLASH))
return (saveType == GBA_SAVE_SRAM) ? 0x8000 : flashSize;
return 0;
case RETRO_MEMORY_SYSTEM_RAM:
return 0x40000;
case RETRO_MEMORY_VIDEO_RAM:
return 0x20000;
return 0x18000;
}
}
else if (type == IMAGE_GB) {
......@@ -866,11 +869,7 @@ static void gba_init(void)
width = GBAWidth;
height = GBAHeight;
// CPUReset() will reset eepromSize to 512.
// Save current eepromSize override then restore after CPUReset()
int tmp = eepromSize;
CPUReset();
eepromSize = tmp;
}
static void gb_init(void)
......@@ -954,11 +953,7 @@ void retro_deinit(void)
void retro_reset(void)
{
// save current eepromSize
int tmp = eepromSize;
core->emuReset();
eepromSize = tmp;
set_gbPalette();
}
......
......@@ -1468,7 +1468,7 @@ public:
utilGBAFindSave(sz);
type->SetSelection(saveType);
if (saveType == 3) {
if (saveType == GBA_SAVE_FLASH) {
size->SetSelection(flashSize == 0x20000 ? 1 : 0);
size->Enable();
} else {
......
......@@ -361,19 +361,19 @@ void GameArea::LoadGame(const wxString& name)
switch (bat.GetSize().GetValue()) {
case 0x200:
case 0x2000:
saveType = 1;
saveType = GBA_SAVE_EEPROM;
break;
case 0x8000:
saveType = 2;
saveType = GBA_SAVE_SRAM;
break;
case 0x10000:
if (saveType == 1 || saveType == 2)
if (saveType == GBA_SAVE_EEPROM || saveType == GBA_SAVE_SRAM)
break;
case 0x20000:
saveType = 3;
saveType = GBA_SAVE_FLASH;
flashSetSize(bat.GetSize().GetValue());
break;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment