Plumb through filename for proper saves

This commit is contained in:
Jeffrey Pfau 2013-09-22 16:45:19 -07:00
parent 388dbc0851
commit 34ddb09516
6 changed files with 35 additions and 6 deletions

View File

@ -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 {

View File

@ -4,12 +4,14 @@
#include "debugger.h"
#include "gba.h"
#include <stdlib.h>
#include <signal.h>
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;
}

View File

@ -13,6 +13,7 @@ struct GBAThread {
// Input
struct GBAVideoRenderer* renderer;
int fd;
const char* fname;
int activeKeys;
// Threading state

View File

@ -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

View File

@ -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, ...);

View File

@ -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);