GB: Sync savedata after being written

This commit is contained in:
Jeffrey Pfau 2016-09-15 18:11:12 -07:00
parent eaf331b249
commit 00e7a5b285
4 changed files with 35 additions and 2 deletions

View File

@ -15,6 +15,8 @@
#include "util/patch.h" #include "util/patch.h"
#include "util/vfs.h" #include "util/vfs.h"
#define CLEANUP_THRESHOLD 15
const uint32_t CGB_LR35902_FREQUENCY = 0x800000; const uint32_t CGB_LR35902_FREQUENCY = 0x800000;
const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E; const uint32_t SGB_LR35902_FREQUENCY = 0x418B1E;
@ -174,6 +176,27 @@ void GBResizeSram(struct GB* gb, size_t size) {
} }
} }
void GBSramClean(struct GB* gb, uint32_t frameCount) {
// TODO: Share with GBASavedataClean
if (!gb->sramVf) {
return;
}
if (gb->sramDirty & GB_SRAM_DIRT_NEW) {
gb->sramDirtAge = frameCount;
gb->sramDirty &= ~GB_SRAM_DIRT_NEW;
if (!(gb->sramDirty & GB_SRAM_DIRT_SEEN)) {
gb->sramDirty |= GB_SRAM_DIRT_SEEN;
}
} else if ((gb->sramDirty & GB_SRAM_DIRT_SEEN) && frameCount - gb->sramDirtAge > CLEANUP_THRESHOLD) {
gb->sramDirty = 0;
if (gb->memory.sram && gb->sramVf->sync(gb->sramVf, gb->memory.sram, gb->sramSize)) {
mLOG(GB_MEM, INFO, "Savedata synced");
} else {
mLOG(GB_MEM, INFO, "Savedata failed to sync!");
}
}
}
void GBSavedataMask(struct GB* gb, struct VFile* vf) { void GBSavedataMask(struct GB* gb, struct VFile* vf) {
GBSramDeinit(gb); GBSramDeinit(gb);
gb->sramVf = vf; gb->sramVf = vf;
@ -601,6 +624,8 @@ void GBGetGameCode(struct GB* gb, char* out) {
} }
void GBFrameEnded(struct GB* gb) { void GBFrameEnded(struct GB* gb) {
GBSramClean(gb, gb->video.frameCounter);
if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) { if (gb->cpu->components && gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]) {
struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE]; struct mCheatDevice* device = (struct mCheatDevice*) gb->cpu->components[CPU_COMPONENT_CHEAT_DEVICE];
size_t i; size_t i;

View File

@ -68,6 +68,8 @@ struct GB {
struct VFile* sramVf; struct VFile* sramVf;
struct VFile* sramRealVf; struct VFile* sramRealVf;
uint32_t sramSize; uint32_t sramSize;
int sramDirty;
int32_t sramDirtAge;
struct mAVStream* stream; struct mAVStream* stream;
@ -111,12 +113,13 @@ void GBHalt(struct LR35902Core* cpu);
struct VFile; struct VFile;
bool GBLoadROM(struct GB* gb, struct VFile* vf); bool GBLoadROM(struct GB* gb, struct VFile* vf);
bool GBLoadSave(struct GB* gb, struct VFile* vf); bool GBLoadSave(struct GB* gb, struct VFile* vf);
void GBResizeSram(struct GB* gb, size_t size);
void GBYankROM(struct GB* gb); void GBYankROM(struct GB* gb);
void GBUnloadROM(struct GB* gb); void GBUnloadROM(struct GB* gb);
void GBLoadBIOS(struct GB* gb, struct VFile* vf); void GBLoadBIOS(struct GB* gb, struct VFile* vf);
void GBSramClean(struct GB* gb, uint32_t frameCount);
void GBResizeSram(struct GB* gb, size_t size);
void GBSavedataMask(struct GB* gb, struct VFile* vf); void GBSavedataMask(struct GB* gb, struct VFile* vf);
void GBSavedataUnmask(struct GB* gb); void GBSavedataUnmask(struct GB* gb);
@ -127,7 +130,6 @@ bool GBIsROM(struct VFile* vf);
void GBGetGameTitle(struct GB* gba, char* out); void GBGetGameTitle(struct GB* gba, char* out);
void GBGetGameCode(struct GB* gba, char* out); void GBGetGameCode(struct GB* gba, char* out);
void GBFrameStarted(struct GB* gb);
void GBFrameEnded(struct GB* gb); void GBFrameEnded(struct GB* gb);
#endif #endif

View File

@ -220,6 +220,7 @@ void GBStore8(struct LR35902Core* cpu, uint16_t address, int8_t value) {
} else if (memory->mbcType == GB_MBC7) { } else if (memory->mbcType == GB_MBC7) {
GBMBC7Write(memory, address, value); GBMBC7Write(memory, address, value);
} }
gb->sramDirty |= GB_SRAM_DIRT_NEW;
return; return;
case GB_REGION_WORKING_RAM_BANK0: case GB_REGION_WORKING_RAM_BANK0:
case GB_REGION_WORKING_RAM_BANK0 + 2: case GB_REGION_WORKING_RAM_BANK0 + 2:

View File

@ -55,6 +55,11 @@ enum {
GB_SIZE_HRAM = 0x7F, GB_SIZE_HRAM = 0x7F,
}; };
enum {
GB_SRAM_DIRT_NEW = 1,
GB_SRAM_DIRT_SEEN = 2
};
struct GBMemory; struct GBMemory;
typedef void (*GBMemoryBankController)(struct GB*, uint16_t address, uint8_t value); typedef void (*GBMemoryBankController)(struct GB*, uint16_t address, uint8_t value);