From 34ddb09516c584e7a47cf61e0ad3fe00c7e13c5a Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 22 Sep 2013 16:45:19 -0700 Subject: [PATCH] Plumb through filename for proper saves --- src/gba/gba-memory.c | 5 +++-- src/gba/gba-thread.c | 25 ++++++++++++++++++++++++- src/gba/gba-thread.h | 1 + src/gba/gba.c | 3 ++- src/gba/gba.h | 4 +++- src/gl-main.c | 3 ++- 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/gba/gba-memory.c b/src/gba/gba-memory.c index 66c16e377..c6a157eb6 100644 --- a/src/gba/gba-memory.c +++ b/src/gba/gba-memory.c @@ -43,8 +43,6 @@ void GBAMemoryInit(struct GBAMemory* memory) { memory->p->errstr = GBA_CANNOT_MMAP; } - GBASavedataInit(&memory->savedata, "test.sav"); - int i; for (i = 0; i < 16; ++i) { memory->waitstates16[i] = GBA_BASE_WAITSTATES[i]; @@ -272,6 +270,7 @@ int8_t GBALoad8(struct ARMMemory* memory, uint32_t address, int* cycleCounter) { case BASE_CART_SRAM: wait = gbaMemory->waitstates16[address >> BASE_OFFSET]; if (gbaMemory->savedata.type == SAVEDATA_NONE) { + GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile); GBASavedataInitSRAM(&gbaMemory->savedata); } value = gbaMemory->savedata.data[address & (SIZE_CART_SRAM - 1)]; @@ -360,6 +359,7 @@ void GBAStore16(struct ARMMemory* memory, uint32_t address, int16_t value, int* break; case BASE_CART2_EX: if (gbaMemory->savedata.type == SAVEDATA_NONE) { + GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile); GBASavedataInitEEPROM(&gbaMemory->savedata); } GBASavedataWriteEEPROM(&gbaMemory->savedata, value, 1); @@ -400,6 +400,7 @@ void GBAStore8(struct ARMMemory* memory, uint32_t address, int8_t value, int* cy break; case BASE_CART_SRAM: if (gbaMemory->savedata.type == SAVEDATA_NONE) { + GBASavedataInit(&gbaMemory->savedata, gbaMemory->p->savefile); if (address == SAVEDATA_FLASH_BASE) { GBASavedataInitFlash(&gbaMemory->savedata); } else { diff --git a/src/gba/gba-thread.c b/src/gba/gba-thread.c index 3ffcfc502..027da1c9d 100644 --- a/src/gba/gba-thread.c +++ b/src/gba/gba-thread.c @@ -4,12 +4,14 @@ #include "debugger.h" #include "gba.h" +#include #include static void* _GBAThreadRun(void* context) { struct ARMDebugger debugger; struct GBA gba; struct GBAThread* threadContext = context; + char* savedata = 0; sigset_t signals; sigfillset(&signals); @@ -22,7 +24,27 @@ static void* _GBAThreadRun(void* context) { threadContext->gba = &gba; if (threadContext->fd >= 0) { - GBALoadROM(&gba, threadContext->fd); + if (threadContext->fname) { + char* dotPoint = strrchr(threadContext->fname, '.'); + if (dotPoint > strrchr(threadContext->fname, '/') && dotPoint[1] && dotPoint[2] && dotPoint[3]) { + savedata = strdup(threadContext->fname); + dotPoint = strrchr(savedata, '.'); + dotPoint[1] = 's'; + dotPoint[2] = 'a'; + dotPoint[3] = 'v'; + dotPoint[4] = '\0'; + } else if (dotPoint) { + savedata = malloc((dotPoint - threadContext->fname + 5) * sizeof(char)); + strncpy(savedata, threadContext->fname, dotPoint - threadContext->fname + 1); + strcat(savedata, "sav"); + } else { + savedata = malloc(strlen(threadContext->fname + 5)); + strcpy(savedata, threadContext->fname); + strcat(savedata, "sav"); + } + } + GBALoadROM(&gba, threadContext->fd, threadContext->fname); + gba.savefile = savedata; } if (threadContext->useDebugger) { threadContext->debugger = &debugger; @@ -46,6 +68,7 @@ static void* _GBAThreadRun(void* context) { } } GBADeinit(&gba); + free(savedata); return 0; } diff --git a/src/gba/gba-thread.h b/src/gba/gba-thread.h index c1a454f38..df2877ce2 100644 --- a/src/gba/gba-thread.h +++ b/src/gba/gba-thread.h @@ -13,6 +13,7 @@ struct GBAThread { // Input struct GBAVideoRenderer* renderer; int fd; + const char* fname; int activeKeys; // Threading state diff --git a/src/gba/gba.c b/src/gba/gba.c index 39fd4beeb..ebcbf30d9 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -231,9 +231,10 @@ void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger) { gba->debugger = debugger; } -void GBALoadROM(struct GBA* gba, int fd) { +void GBALoadROM(struct GBA* gba, int fd, const char* fname) { struct stat info; gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + gba->activeFile = fname; fstat(fd, &info); gba->memory.romSize = info.st_size; // TODO: error check diff --git a/src/gba/gba.h b/src/gba/gba.h index 8c854881e..0baad7fa6 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -78,6 +78,8 @@ struct GBA { int springIRQ; int* keySource; + const char* activeFile; + const char* savefile; enum GBAError errno; const char* errstr; enum GBALogLevel logLevel; @@ -105,7 +107,7 @@ int GBAHalt(struct GBA* gba); void GBAAttachDebugger(struct GBA* gba, struct ARMDebugger* debugger); -void GBALoadROM(struct GBA* gba, int fd); +void GBALoadROM(struct GBA* gba, int fd, const char* fname); __attribute__((format (printf, 3, 4))) void GBALog(struct GBA* gba, enum GBALogLevel level, const char* format, ...); diff --git a/src/gl-main.c b/src/gl-main.c index cc246a822..efa3fe3a1 100644 --- a/src/gl-main.c +++ b/src/gl-main.c @@ -65,7 +65,8 @@ int main(int argc, char** argv) { } context.fd = fd; - context.useDebugger = 0; + context.fname = fname; + context.useDebugger = 1; context.renderer = &renderer.d.d; GBAThreadStart(&context);