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
This commit is contained in:
retro-wertz 2019-01-29 19:42:07 +08:00 committed by Rafael Kitover
parent 2a796d48a0
commit a0cec107a2
9 changed files with 45 additions and 84 deletions

View File

@ -206,7 +206,7 @@ int rewindSaveNeeded = 0;
int rewindTimer = 0; int rewindTimer = 0;
int rewindTopPos; int rewindTopPos;
int rtcEnabled; int rtcEnabled;
int saveType = 0; int saveType = GBA_SAVE_AUTO;
int screenMessage; int screenMessage;
int sensorX; int sensorX;
int sensorY; int sensorY;

View File

@ -30,6 +30,8 @@ variable_desc eepromSaveData[] = {
void eepromInit() void eepromInit()
{ {
eepromInUse = false;
eepromSize = 512;
memset(eepromData, 255, sizeof(eepromData)); memset(eepromData, 255, sizeof(eepromData));
} }
@ -39,8 +41,6 @@ void eepromReset()
eepromByte = 0; eepromByte = 0;
eepromBits = 0; eepromBits = 0;
eepromAddress = 0; eepromAddress = 0;
eepromInUse = false;
eepromSize = 512;
} }
#ifdef __LIBRETRO__ #ifdef __LIBRETRO__

View File

@ -152,17 +152,16 @@ uint8_t flashRead(uint32_t address)
void flashSaveDecide(uint32_t address, uint8_t byte) void flashSaveDecide(uint32_t address, uint8_t byte)
{ {
if (saveType == 1) if (saveType == GBA_SAVE_EEPROM)
return; return;
if (cpuSramEnabled && cpuFlashEnabled) { if (cpuSramEnabled && cpuFlashEnabled) {
// log("Deciding save type %08x\n", address);
if (address == 0x0e005555) { if (address == 0x0e005555) {
saveType = 3; saveType = GBA_SAVE_FLASH;
cpuSramEnabled = false; cpuSramEnabled = false;
cpuSaveGameFunc = flashWrite; cpuSaveGameFunc = flashWrite;
} else { } else {
saveType = 2; saveType = GBA_SAVE_SRAM;
cpuFlashEnabled = false; cpuFlashEnabled = false;
cpuSaveGameFunc = sramWrite; cpuSaveGameFunc = sramWrite;
} }
@ -176,7 +175,7 @@ void flashSaveDecide(uint32_t address, uint8_t byte)
void flashDelayedWrite(uint32_t address, uint8_t byte) void flashDelayedWrite(uint32_t address, uint8_t byte)
{ {
saveType = 3; saveType = GBA_SAVE_FLASH;
cpuSaveGameFunc = flashWrite; cpuSaveGameFunc = flashWrite;
flashWrite(address, byte); flashWrite(address, byte);
} }

View File

@ -56,7 +56,6 @@ int dummyAddress = 0;
bool cpuBreakLoop = false; bool cpuBreakLoop = false;
int cpuNextEvent = 0; int cpuNextEvent = 0;
int gbaSaveType = 0; // used to remember the save type on reset
bool intState = false; bool intState = false;
bool stopState = false; bool stopState = false;
bool holdState = false; bool holdState = false;
@ -672,10 +671,8 @@ bool CPUReadState(const uint8_t* data, unsigned size)
CPUUpdateWindow0(); CPUUpdateWindow0();
CPUUpdateWindow1(); CPUUpdateWindow1();
gbaSaveType = 0;
SetSaveType(saveType); SetSaveType(saveType);
if (eepromInUse)
gbaSaveType = 3;
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
if (armState) { if (armState) {
@ -894,10 +891,8 @@ static bool CPUReadState(gzFile gzFile)
CPUUpdateRenderBuffers(true); CPUUpdateRenderBuffers(true);
CPUUpdateWindow0(); CPUUpdateWindow0();
CPUUpdateWindow1(); CPUUpdateWindow1();
gbaSaveType = 0;
SetSaveType(saveType); SetSaveType(saveType);
if (eepromInUse)
gbaSaveType = 3;
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
if (armState) { if (armState) {
@ -964,21 +959,7 @@ bool CPUExportEepromFile(const char* fileName)
bool CPUWriteBatteryFile(const char* fileName) bool CPUWriteBatteryFile(const char* fileName)
{ {
if (gbaSaveType == 0) { if ((saveType) && (saveType != GBA_SAVE_NONE)) {
if (eepromInUse)
gbaSaveType = 3;
else
switch (saveType) {
case 1:
gbaSaveType = 1;
break;
case 2:
gbaSaveType = 2;
break;
}
}
if ((gbaSaveType) && (gbaSaveType != 5)) {
FILE* file = fopen(fileName, "wb"); FILE* file = fopen(fileName, "wb");
if (!file) { if (!file) {
@ -988,19 +969,19 @@ bool CPUWriteBatteryFile(const char* fileName)
} }
// only save if Flash/Sram in use or EEprom in use // only save if Flash/Sram in use or EEprom in use
if (gbaSaveType != 3) { if (!eepromInUse) {
if (gbaSaveType == 2) { if (saveType == GBA_SAVE_FLASH) { // save flash type
if (fwrite(flashSaveMemory, 1, flashSize, file) != (size_t)flashSize) { if (fwrite(flashSaveMemory, 1, flashSize, file) != (size_t)flashSize) {
fclose(file); fclose(file);
return false; return false;
} }
} else { } else if (saveType == GBA_SAVE_SRAM) { // save sram type
if (fwrite(flashSaveMemory, 1, 0x8000, file) != 0x8000) { if (fwrite(flashSaveMemory, 1, 0x8000, file) != 0x8000) {
fclose(file); fclose(file);
return false; return false;
} }
} }
} else { } else { // save eeprom type
if (fwrite(eepromData, 1, eepromSize, file) != (size_t)eepromSize) { if (fwrite(eepromData, 1, eepromSize, file) != (size_t)eepromSize) {
fclose(file); fclose(file);
return false; return false;
@ -1161,7 +1142,7 @@ bool CPUWriteGSASnapshot(const char* fileName,
fwrite(buffer, 1, 4, file); // notes length fwrite(buffer, 1, 4, file); // notes length
fwrite(notes, 1, strlen(notes), file); fwrite(notes, 1, strlen(notes), file);
int saveSize = 0x10000; int saveSize = 0x10000;
if (gbaSaveType == 2) if (saveType == GBA_SAVE_FLASH)
saveSize = flashSize; saveSize = flashSize;
int totalSize = saveSize + 0x1c; int totalSize = saveSize + 0x1c;
@ -3254,7 +3235,6 @@ void CPUInit(const char* biosFileName, bool useBiosFile)
cpuBiosSwapped = true; cpuBiosSwapped = true;
} }
#endif #endif
gbaSaveType = 0;
eepromInUse = 0; eepromInUse = 0;
useBios = false; useBios = false;
@ -3342,72 +3322,50 @@ void CPUInit(const char* biosFileName, bool useBiosFile)
void SetSaveType(int st) void SetSaveType(int st)
{ {
switch (st) { switch (st) {
case 0: // automatic case GBA_SAVE_AUTO:
cpuSramEnabled = true; cpuSramEnabled = true;
cpuFlashEnabled = true; cpuFlashEnabled = true;
cpuEEPROMEnabled = true; cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = false; cpuEEPROMSensorEnabled = false;
gbaSaveType = 0;
cpuSaveGameFunc = flashSaveDecide; cpuSaveGameFunc = flashSaveDecide;
break; break;
case 1: // EEPROM case GBA_SAVE_EEPROM:
cpuSramEnabled = false; cpuSramEnabled = false;
cpuFlashEnabled = false; cpuFlashEnabled = false;
cpuEEPROMEnabled = true; cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = false; cpuEEPROMSensorEnabled = false;
gbaSaveType = 3;
// EEPROM usage is automatically detected
break; break;
case 2: // SRAM case GBA_SAVE_SRAM:
cpuSramEnabled = true; cpuSramEnabled = true;
cpuFlashEnabled = false; cpuFlashEnabled = false;
cpuEEPROMEnabled = false; cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false; cpuEEPROMSensorEnabled = false;
cpuSaveGameFunc = sramDelayedWrite; // to insure we detect the write cpuSaveGameFunc = sramDelayedWrite; // to insure we detect the write
gbaSaveType = 1;
break; break;
case 3: // FLASH case GBA_SAVE_FLASH:
cpuSramEnabled = false; cpuSramEnabled = false;
cpuFlashEnabled = true; cpuFlashEnabled = true;
cpuEEPROMEnabled = false; cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false; cpuEEPROMSensorEnabled = false;
cpuSaveGameFunc = flashDelayedWrite; // to insure we detect the write cpuSaveGameFunc = flashDelayedWrite; // to insure we detect the write
gbaSaveType = 2;
break; break;
case 4: // EEPROM+Sensor case GBA_SAVE_EEPROM_SENSOR:
cpuSramEnabled = false; cpuSramEnabled = false;
cpuFlashEnabled = false; cpuFlashEnabled = false;
cpuEEPROMEnabled = true; cpuEEPROMEnabled = true;
cpuEEPROMSensorEnabled = true; cpuEEPROMSensorEnabled = true;
// EEPROM usage is automatically detected
gbaSaveType = 3;
break; break;
case 5: // NONE case GBA_SAVE_NONE:
cpuSramEnabled = false; cpuSramEnabled = false;
cpuFlashEnabled = false; cpuFlashEnabled = false;
cpuEEPROMEnabled = false; cpuEEPROMEnabled = false;
cpuEEPROMSensorEnabled = false; cpuEEPROMSensorEnabled = false;
// no save at all
gbaSaveType = 5;
break; break;
} }
} }
void CPUReset() void CPUReset()
{ {
if (gbaSaveType == 0) {
if (eepromInUse)
gbaSaveType = 3;
else
switch (saveType) {
case 2:
gbaSaveType = 1;
break;
case 3:
gbaSaveType = 2;
break;
}
}
switch (CheckEReaderRegion()) { switch (CheckEReaderRegion()) {
case 1: //US case 1: //US
EReaderWriteMemory(0x8009134, 0x46C0DFE0); EReaderWriteMemory(0x8009134, 0x46C0DFE0);

View File

@ -18,6 +18,15 @@ const uint64_t TICKS_PER_SECOND = 16777216;
#define SAVE_GAME_VERSION_10 10 #define SAVE_GAME_VERSION_10 10
#define SAVE_GAME_VERSION SAVE_GAME_VERSION_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 { typedef struct {
uint8_t* address; uint8_t* address;
uint32_t mask; uint32_t mask;

View File

@ -9,7 +9,7 @@ uint8_t sramRead(uint32_t address)
} }
void sramDelayedWrite(uint32_t address, uint8_t byte) void sramDelayedWrite(uint32_t address, uint8_t byte)
{ {
saveType = 2; saveType = GBA_SAVE_SRAM;
cpuSaveGameFunc = sramWrite; cpuSaveGameFunc = sramWrite;
sramWrite(address, byte); sramWrite(address, byte);
} }

View File

@ -174,6 +174,9 @@ static void set_gbPalette(void)
{ {
const uint16_t *pal = defaultGBPalettes[current_gbPalette]; const uint16_t *pal = defaultGBPalettes[current_gbPalette];
if (type != IMAGE_GB)
return;
if (gbCgbMode || gbSgbMode) if (gbCgbMode || gbSgbMode)
return; return;
@ -306,9 +309,9 @@ void* retro_get_memory_data(unsigned id)
if (type == IMAGE_GBA) { if (type == IMAGE_GBA) {
switch (id) { switch (id) {
case RETRO_MEMORY_SAVE_RAM: case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4)) if ((saveType == GBA_SAVE_EEPROM) | (saveType == GBA_SAVE_EEPROM_SENSOR))
return eepromData; return eepromData;
if ((saveType == 2) | (saveType == 3)) if ((saveType == GBA_SAVE_SRAM) | (saveType == GBA_SAVE_FLASH))
return flashSaveMemory; return flashSaveMemory;
return NULL; return NULL;
case RETRO_MEMORY_SYSTEM_RAM: case RETRO_MEMORY_SYSTEM_RAM:
@ -338,15 +341,15 @@ size_t retro_get_memory_size(unsigned id)
if (type == IMAGE_GBA) { if (type == IMAGE_GBA) {
switch (id) { switch (id) {
case RETRO_MEMORY_SAVE_RAM: case RETRO_MEMORY_SAVE_RAM:
if ((saveType == 1) | (saveType == 4)) if ((saveType == GBA_SAVE_EEPROM) | (saveType == GBA_SAVE_EEPROM_SENSOR))
return eepromSize; return eepromSize;
if ((saveType == 2) | (saveType == 3)) if ((saveType == GBA_SAVE_SRAM) | (saveType == GBA_SAVE_FLASH))
return flashSize; return (saveType == GBA_SAVE_SRAM) ? 0x8000 : flashSize;
return 0; return 0;
case RETRO_MEMORY_SYSTEM_RAM: case RETRO_MEMORY_SYSTEM_RAM:
return 0x40000; return 0x40000;
case RETRO_MEMORY_VIDEO_RAM: case RETRO_MEMORY_VIDEO_RAM:
return 0x20000; return 0x18000;
} }
} }
else if (type == IMAGE_GB) { else if (type == IMAGE_GB) {
@ -866,11 +869,7 @@ static void gba_init(void)
width = GBAWidth; width = GBAWidth;
height = GBAHeight; height = GBAHeight;
// CPUReset() will reset eepromSize to 512.
// Save current eepromSize override then restore after CPUReset()
int tmp = eepromSize;
CPUReset(); CPUReset();
eepromSize = tmp;
} }
static void gb_init(void) static void gb_init(void)
@ -954,11 +953,7 @@ void retro_deinit(void)
void retro_reset(void) void retro_reset(void)
{ {
// save current eepromSize
int tmp = eepromSize;
core->emuReset(); core->emuReset();
eepromSize = tmp;
set_gbPalette(); set_gbPalette();
} }

View File

@ -1468,7 +1468,7 @@ public:
utilGBAFindSave(sz); utilGBAFindSave(sz);
type->SetSelection(saveType); type->SetSelection(saveType);
if (saveType == 3) { if (saveType == GBA_SAVE_FLASH) {
size->SetSelection(flashSize == 0x20000 ? 1 : 0); size->SetSelection(flashSize == 0x20000 ? 1 : 0);
size->Enable(); size->Enable();
} else { } else {

View File

@ -361,19 +361,19 @@ void GameArea::LoadGame(const wxString& name)
switch (bat.GetSize().GetValue()) { switch (bat.GetSize().GetValue()) {
case 0x200: case 0x200:
case 0x2000: case 0x2000:
saveType = 1; saveType = GBA_SAVE_EEPROM;
break; break;
case 0x8000: case 0x8000:
saveType = 2; saveType = GBA_SAVE_SRAM;
break; break;
case 0x10000: case 0x10000:
if (saveType == 1 || saveType == 2) if (saveType == GBA_SAVE_EEPROM || saveType == GBA_SAVE_SRAM)
break; break;
case 0x20000: case 0x20000:
saveType = 3; saveType = GBA_SAVE_FLASH;
flashSetSize(bat.GetSize().GetValue()); flashSetSize(bat.GetSize().GetValue());
break; break;