all repos — mgba @ de39f23a1bb4b2cb7a005e16bdb8820d47c2702e

mGBA Game Boy Advance Emulator

Add ROM override checking, seed with BPEE
Jeffrey Pfau jeffrey@endrift.com
Fri, 11 Oct 2013 22:03:27 -0700
commit

de39f23a1bb4b2cb7a005e16bdb8820d47c2702e

parent

58611facf1366bbc0af5fe58c183efb816c41ed9

5 files changed, 82 insertions(+), 3 deletions(-)

jump to
M src/gba/gba-memory.hsrc/gba/gba-memory.h

@@ -114,6 +114,7 @@ uint16_t io[SIZE_IO >> 1];

struct GBASavedata savedata; size_t romSize; + uint16_t romID; int fullBios; char waitstates32[256];
M src/gba/gba-savedata.csrc/gba/gba-savedata.c

@@ -21,6 +21,14 @@ savedata->fd = -1;

savedata->filename = filename; } +void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type) { + if (savedata->type != SAVEDATA_NONE) { + GBALog(0, GBA_LOG_WARN, "Can't re-initialize savedata"); + return; + } + savedata->type = type; +} + void GBASavedataDeinit(struct GBASavedata* savedata) { switch (savedata->type) { case SAVEDATA_SRAM:

@@ -43,7 +51,13 @@ savedata->type = SAVEDATA_NONE;

} void GBASavedataInitFlash(struct GBASavedata* savedata) { - savedata->type = SAVEDATA_FLASH512; + if (savedata->type == SAVEDATA_NONE) { + savedata->type = SAVEDATA_FLASH512; + } + if (savedata->type != SAVEDATA_FLASH512 && savedata->type != SAVEDATA_FLASH1M) { + GBALog(0, GBA_LOG_WARN, "Can't re-initialize savedata"); + return; + } savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; int flags = MAP_SHARED;

@@ -66,7 +80,12 @@ }

} void GBASavedataInitEEPROM(struct GBASavedata* savedata) { - savedata->type = SAVEDATA_EEPROM; + if (savedata->type == SAVEDATA_NONE) { + savedata->type = SAVEDATA_EEPROM; + } else { + GBALog(0, GBA_LOG_WARN, "Can't re-initialize savedata"); + return; + } savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; int flags = MAP_SHARED;

@@ -87,7 +106,12 @@ }

} void GBASavedataInitSRAM(struct GBASavedata* savedata) { - savedata->type = SAVEDATA_SRAM; + if (savedata->type == SAVEDATA_NONE) { + savedata->type = SAVEDATA_SRAM; + } else { + GBALog(0, GBA_LOG_WARN, "Can't re-initialize savedata"); + return; + } savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); off_t end; int flags = MAP_SHARED;
M src/gba/gba-savedata.hsrc/gba/gba-savedata.h

@@ -68,8 +68,15 @@

enum FlashStateMachine flashState; }; +struct SavedataOverride { + uint32_t id; + enum SavedataType type; +}; + void GBASavedataInit(struct GBASavedata* savedata, const char* filename); void GBASavedataDeinit(struct GBASavedata* savedata); + +void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type); void GBASavedataInitFlash(struct GBASavedata* savedata); void GBASavedataInitEEPROM(struct GBASavedata* savedata);
M src/gba/gba.csrc/gba/gba.c

@@ -22,9 +22,16 @@ SP_BASE_IRQ = 0x03FFFFA0,

SP_BASE_SUPERVISOR = 0x03FFFFE0 }; +static const struct SavedataOverride _savedataOverrides[] = { + { 'EEPB', SAVEDATA_FLASH1M }, + { 0, 0 } +}; + static void GBAProcessEvents(struct ARMBoard* board); static int32_t GBATimersProcessEvents(struct GBA* gba, int32_t cycles); static void GBAHitStub(struct ARMBoard* board, uint32_t opcode); + +static void _checkOverrides(struct GBA* gba, uint32_t code); void GBAInit(struct GBA* gba) { gba->errno = GBA_NO_ERROR;

@@ -270,6 +277,7 @@ 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; + _checkOverrides(gba, ((struct GBACartridge*) gba->memory.rom)->id); // TODO: error check }

@@ -432,3 +440,27 @@ } else {

ARMDebuggerEnter(gbaBoard->p->debugger); } } + +void _checkOverrides(struct GBA* gba, uint32_t id) { + int i; + for (i = 0; _savedataOverrides[i].id; ++i) { + if (_savedataOverrides[i].id == id) { + gba->memory.savedata.type = _savedataOverrides[i].type; + switch (_savedataOverrides[i].type) { + case SAVEDATA_FLASH512: + case SAVEDATA_FLASH1M: + GBASavedataInitFlash(&gba->memory.savedata); + break; + case SAVEDATA_EEPROM: + GBASavedataInitEEPROM(&gba->memory.savedata); + break; + case SAVEDATA_SRAM: + GBASavedataInitSRAM(&gba->memory.savedata); + break; + case SAVEDATA_NONE: + break; + } + return; + } + } +}
M src/gba/gba.hsrc/gba/gba.h

@@ -93,6 +93,21 @@ const char* errstr;

int logLevel; }; +struct GBACartridge { + uint32_t entry; + uint8_t logo[156]; + char title[12]; + uint32_t id; + uint16_t maker; + uint8_t type; + uint8_t unit; + uint8_t device; + uint8_t reserved[7]; + uint8_t version; + uint8_t checksum; + // And ROM data... +}; + void GBAInit(struct GBA* gba); void GBADeinit(struct GBA* gba);