all repos — mgba @ 4380ec0260036841fda2b4e539286553a7601ef2

mGBA Game Boy Advance Emulator

Start implementing EEPROM
Jeffrey Pfau jeffrey@endrift.com
Sat, 27 Apr 2013 20:25:31 -0700
commit

4380ec0260036841fda2b4e539286553a7601ef2

parent

e02059947c0ee17c988b0c14f6787dc6143f4577

M src/gba/gba-memory.csrc/gba/gba-memory.c

@@ -196,8 +196,13 @@ case BASE_CART0_EX:

case BASE_CART1: case BASE_CART1_EX: case BASE_CART2: + return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1]; case BASE_CART2_EX: - return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1]; + if ((address & (SIZE_CART0 - 1)) < gbaMemory->romSize) { + return ((uint16_t*) gbaMemory->rom)[(address & (SIZE_CART0 - 1)) >> 1]; + } else { + return GBASavedataReadEEPROM(&gbaMemory->savedata); + } case BASE_CART_SRAM: break; default:

@@ -309,8 +314,6 @@ ((int32_t*) gbaMemory->p->video.oam.raw)[(address & (SIZE_OAM - 1)) >> 2] = value;

break; case BASE_CART0: break; - case BASE_CART2_EX: - break; case BASE_CART_SRAM: break; default:

@@ -345,6 +348,10 @@ break;

case BASE_CART0: break; case BASE_CART2_EX: + if (gbaMemory->savedata.type == SAVEDATA_NONE) { + GBASavedataInitEEPROM(&gbaMemory->savedata); + } + GBASavedataWriteEEPROM(&gbaMemory->savedata, value); break; case BASE_CART_SRAM: break;

@@ -372,8 +379,6 @@ break;

case BASE_OAM: break; case BASE_CART0: - break; - case BASE_CART2_EX: break; case BASE_CART_SRAM: if (gbaMemory->savedata.type == SAVEDATA_NONE) {
M src/gba/gba-memory.hsrc/gba/gba-memory.h

@@ -5,6 +5,8 @@ #include "arm.h"

#include "gba-savedata.h" +#include <string.h> + enum GBAMemoryRegion { REGION_BIOS = 0x0, REGION_WORKING_RAM = 0x2,

@@ -111,6 +113,7 @@ uint32_t* rom;

uint16_t io[SIZE_IO >> 1]; struct GBASavedata savedata; + size_t romSize; char waitstates32[256]; char waitstates16[256];
M src/gba/gba-savedata.csrc/gba/gba-savedata.c

@@ -43,7 +43,7 @@ GBALog(GBA_LOG_WARN, "Cannot open savedata file %s", savedata->filename);

return; } // mmap enough so that we can expand the file if we need to - savedata->data = mmap(0, SIZE_CART_FLASH1M, PROT_READ | PROT_WRITE, 0, savedata->fd, 0); + savedata->data = mmap(0, SIZE_CART_FLASH1M, PROT_READ | PROT_WRITE, MAP_SHARED, savedata->fd, 0); off_t end = lseek(savedata->fd, 0, SEEK_END); if (end < SIZE_CART_FLASH512) {

@@ -52,6 +52,22 @@ memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end);

} } +void GBASavedataInitEEPROM(struct GBASavedata* savedata) { + savedata->type = SAVEDATA_EEPROM; + savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666); + if (savedata->fd < 0) { + GBALog(GBA_LOG_WARN, "Cannot open savedata file %s", savedata->filename); + return; + } + savedata->data = mmap(0, SIZE_CART_EEPROM, PROT_READ | PROT_WRITE, MAP_SHARED, savedata->fd, 0); + + off_t end = lseek(savedata->fd, 0, SEEK_END); + if (end < SIZE_CART_EEPROM) { + ftruncate(savedata->fd, SIZE_CART_EEPROM); + memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM - end); + } +} + void GBASavedataInitSRAM(struct GBASavedata* savedata) { savedata->type = SAVEDATA_SRAM; savedata->fd = open(savedata->filename, O_RDWR | O_CREAT, 0666);

@@ -73,8 +89,21 @@ memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end);

} } + void GBASavedataWriteFlash(struct GBASavedata* savedata, uint8_t value) { (void)(savedata); (void)(value); GBALog(GBA_LOG_STUB, "Flash memory unimplemented"); } + +void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value) { + (void)(savedata); + (void)(value); + GBALog(GBA_LOG_STUB, "EEPROM unimplemented"); +} + +uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata) { + (void)(savedata); + GBALog(GBA_LOG_STUB, "EEPROM unimplemented"); + return 0; +}
M src/gba/gba-savedata.hsrc/gba/gba-savedata.h

@@ -26,8 +26,12 @@ void GBASavedataInit(struct GBASavedata* savedata, const char* filename);

void GBASavedataDeinit(struct GBASavedata* savedata); void GBASavedataInitFlash(struct GBASavedata* savedata); +void GBASavedataInitEEPROM(struct GBASavedata* savedata); void GBASavedataInitSRAM(struct GBASavedata* savedata); void GBASavedataWriteFlash(struct GBASavedata* savedata, uint8_t value); + +uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata); +void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value); #endif
M src/gba/gba.csrc/gba/gba.c

@@ -11,6 +11,7 @@ #include <stdio.h>

#include <stdlib.h> #include <string.h> #include <sys/mman.h> +#include <sys/stat.h> enum { SP_BASE_SYSTEM = 0x03FFFF00,

@@ -228,7 +229,10 @@ gba->debugger = debugger;

} void GBALoadROM(struct GBA* gba, int fd) { + struct stat info; gba->memory.rom = mmap(0, SIZE_CART0, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); + fstat(fd, &info); + gba->memory.romSize = info.st_size; // TODO: error check }