vbagx/source/vba/gba/EEprom.cpp

221 lines
5.2 KiB
C++
Raw Permalink Normal View History

2008-09-23 01:00:10 +02:00
#include <string.h>
2009-01-08 08:35:44 +01:00
#include "GBA.h"
2008-09-23 01:00:10 +02:00
#include "EEprom.h"
2009-01-08 08:35:44 +01:00
#include "../Util.h"
2008-09-23 01:00:10 +02:00
extern int cpuDmaCount;
int eepromMode = EEPROM_IDLE;
int eepromByte = 0;
int eepromBits = 0;
int eepromAddress = 0;
#ifdef __LIBRETRO__
// Workaround for broken-by-design GBA save semantics
extern u8 libretro_save_buf[0x20000 + 0x2000];
u8 *eepromData = libretro_save_buf + 0x20000;
#else
2008-09-23 01:00:10 +02:00
u8 eepromData[0x2000];
#endif
2008-09-23 01:00:10 +02:00
u8 eepromBuffer[16];
bool eepromInUse = false;
2010-04-06 00:46:26 +02:00
int eepromSize = 512;
2008-09-23 01:00:10 +02:00
variable_desc eepromSaveData[] = {
{ &eepromMode, sizeof(int) },
{ &eepromByte, sizeof(int) },
{ &eepromBits , sizeof(int) },
{ &eepromAddress , sizeof(int) },
{ &eepromInUse, sizeof(bool) },
{ &eepromData[0], 512 },
{ &eepromBuffer[0], 16 },
{ NULL, 0 }
};
void eepromInit()
{
#ifdef __LIBRETRO__
memset(eepromData, 255, 0x2000);
#else
memset(eepromData, 255, sizeof(eepromData));
#endif
2008-09-23 01:00:10 +02:00
}
void eepromReset()
{
eepromMode = EEPROM_IDLE;
eepromByte = 0;
eepromBits = 0;
eepromAddress = 0;
eepromInUse = false;
eepromSize = 512;
}
#ifdef __LIBRETRO__
void eepromSaveGame(uint8_t *& data)
{
utilWriteDataMem(data, eepromSaveData);
utilWriteIntMem(data, eepromSize);
utilWriteMem(data, eepromData, 0x2000);
}
void eepromReadGame(const uint8_t *& data, int version)
{
utilReadDataMem(data, eepromSaveData);
if (version >= SAVE_GAME_VERSION_3) {
eepromSize = utilReadIntMem(data);
utilReadMem(eepromData, data, 0x2000);
} else {
// prior to 0.7.1, only 4K EEPROM was supported
eepromSize = 512;
}
}
#else
2008-09-23 01:00:10 +02:00
void eepromSaveGame(gzFile gzFile)
{
utilWriteData(gzFile, eepromSaveData);
utilWriteInt(gzFile, eepromSize);
utilGzWrite(gzFile, eepromData, 0x2000);
}
void eepromReadGame(gzFile gzFile, int version)
{
utilReadData(gzFile, eepromSaveData);
if(version >= SAVE_GAME_VERSION_3) {
eepromSize = utilReadInt(gzFile);
utilGzRead(gzFile, eepromData, 0x2000);
} else {
// prior to 0.7.1, only 4K EEPROM was supported
eepromSize = 512;
}
}
void eepromReadGameSkip(gzFile gzFile, int version)
{
// skip the eeprom data in a save game
2009-01-10 03:41:39 +01:00
utilReadDataSkip(gzFile, eepromSaveData);
2008-09-23 01:00:10 +02:00
if(version >= SAVE_GAME_VERSION_3) {
utilGzSeek(gzFile, sizeof(int), SEEK_CUR);
utilGzSeek(gzFile, 0x2000, SEEK_CUR);
}
}
#endif
2008-09-23 01:00:10 +02:00
int eepromRead(u32 /* address */)
{
switch(eepromMode) {
case EEPROM_IDLE:
case EEPROM_READADDRESS:
case EEPROM_WRITEDATA:
return 1;
case EEPROM_READDATA:
{
2010-04-06 00:46:26 +02:00
eepromBits++;
2008-09-23 01:00:10 +02:00
if(eepromBits == 4) {
eepromMode = EEPROM_READDATA2;
eepromBits = 0;
eepromByte = 0;
}
return 0;
}
case EEPROM_READDATA2:
{
2010-04-06 00:46:26 +02:00
int data = 0;
2008-09-23 01:00:10 +02:00
int address = eepromAddress << 3;
int mask = 1 << (7 - (eepromBits & 7));
2010-04-06 00:46:26 +02:00
data = (eepromData[address+eepromByte] & mask) ? 1 : 0;
eepromBits++;
2008-09-23 01:00:10 +02:00
if((eepromBits & 7) == 0)
2010-04-06 00:46:26 +02:00
eepromByte++;
2008-09-23 01:00:10 +02:00
if(eepromBits == 0x40)
eepromMode = EEPROM_IDLE;
return data;
}
default:
return 0;
}
return 1;
}
void eepromWrite(u32 /* address */, u8 value)
{
if(cpuDmaCount == 0)
return;
int bit = value & 1;
switch(eepromMode) {
case EEPROM_IDLE:
eepromByte = 0;
eepromBits = 1;
eepromBuffer[eepromByte] = bit;
eepromMode = EEPROM_READADDRESS;
break;
case EEPROM_READADDRESS:
eepromBuffer[eepromByte] <<= 1;
eepromBuffer[eepromByte] |= bit;
2010-04-06 00:46:26 +02:00
eepromBits++;
2008-09-23 01:00:10 +02:00
if((eepromBits & 7) == 0) {
2010-04-06 00:46:26 +02:00
eepromByte++;
2008-09-23 01:00:10 +02:00
}
if(cpuDmaCount == 0x11 || cpuDmaCount == 0x51) {
if(eepromBits == 0x11) {
eepromInUse = true;
eepromSize = 0x2000;
eepromAddress = ((eepromBuffer[0] & 0x3F) << 8) |
((eepromBuffer[1] & 0xFF));
if(!(eepromBuffer[0] & 0x40)) {
eepromBuffer[0] = bit;
eepromBits = 1;
eepromByte = 0;
eepromMode = EEPROM_WRITEDATA;
} else {
eepromMode = EEPROM_READDATA;
eepromByte = 0;
eepromBits = 0;
}
}
2010-04-06 00:46:26 +02:00
} else {
if(eepromBits == 9) {
2008-09-23 01:00:10 +02:00
eepromInUse = true;
eepromAddress = (eepromBuffer[0] & 0x3F);
if(!(eepromBuffer[0] & 0x40)) {
eepromBuffer[0] = bit;
eepromBits = 1;
eepromByte = 0;
eepromMode = EEPROM_WRITEDATA;
} else {
eepromMode = EEPROM_READDATA;
eepromByte = 0;
eepromBits = 0;
}
2010-04-06 00:46:26 +02:00
}
2008-09-23 01:00:10 +02:00
}
break;
case EEPROM_READDATA:
case EEPROM_READDATA2:
// should we reset here?
eepromMode = EEPROM_IDLE;
break;
case EEPROM_WRITEDATA:
eepromBuffer[eepromByte] <<= 1;
eepromBuffer[eepromByte] |= bit;
2010-04-06 00:46:26 +02:00
eepromBits++;
2008-09-23 01:00:10 +02:00
if((eepromBits & 7) == 0) {
2010-04-06 00:46:26 +02:00
eepromByte++;
2008-09-23 01:00:10 +02:00
}
if(eepromBits == 0x40) {
eepromInUse = true;
// write data;
2010-04-06 00:46:26 +02:00
for(int i = 0; i < 8; i++) {
eepromData[(eepromAddress << 3) + i] = eepromBuffer[i];
2008-09-23 01:00:10 +02:00
}
systemSaveUpdateCounter = SYSTEM_SAVE_UPDATED;
} else if(eepromBits == 0x41) {
eepromMode = EEPROM_IDLE;
eepromByte = 0;
eepromBits = 0;
}
break;
}
}