all repos — mgba @ d8a6d940ed497987a79bb8e92675fc0147bce773

mGBA Game Boy Advance Emulator

GB: Fix resizing SRAM being spammed too much
Jeffrey Pfau jeffrey@endrift.com
Fri, 09 Sep 2016 15:33:43 -0700
commit

d8a6d940ed497987a79bb8e92675fc0147bce773

parent

ec33060390834d1ad2cd65277bef08e05929f71e

2 files changed, 24 insertions(+), 8 deletions(-)

jump to
M src/gb/gb.csrc/gb/gb.c

@@ -133,7 +133,7 @@ memset(&gb->memory.sram[gb->sramSize], 0xFF, size - gb->sramSize);

if (size & 0xFF) { memcpy(&gb->memory.sram[gb->sramSize - (size & 0xFF)], extdataBuffer, size & 0xFF); } - } else { + } else if (size > gb->sramSize || !gb->memory.sram) { if (gb->memory.sram) { vf->unmap(vf, gb->memory.sram, gb->sramSize); }

@@ -144,6 +144,9 @@ if (gb->memory.sram) {

vf->unmap(vf, gb->memory.sram, gb->sramSize); } gb->memory.sram = vf->map(vf, size, MAP_READ); + } + if (gb->memory.sram == (void*) -1) { + gb->memory.sram = NULL; } } else { uint8_t* newSram = anonymousMemoryMap(size);

@@ -160,7 +163,9 @@ memset(newSram, 0xFF, size);

} gb->memory.sram = newSram; } - gb->sramSize = size; + if (gb->sramSize < size) { + gb->sramSize = size; + } } void GBSavedataMask(struct GB* gb, struct VFile* vf) {
M src/gb/mbc.csrc/gb/mbc.c

@@ -48,20 +48,34 @@ }

void GBMBCInit(struct GB* gb) { const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100]; + switch (cart->ramSize) { + case 0: + gb->sramSize = 0; + break; + case 1: + gb->sramSize = 0x800; + break; + default: + case 2: + gb->sramSize = 0x2000; + break; + case 3: + gb->sramSize = 0x8000; + break; + } + switch (cart->type) { case 0: case 8: case 9: gb->memory.mbc = _GBMBCNone; gb->memory.mbcType = GB_MBC_NONE; - gb->sramSize = 0; return; case 1: case 2: case 3: gb->memory.mbc = _GBMBC1; gb->memory.mbcType = GB_MBC1; - gb->sramSize = 0x2000; break; case 5: case 6:

@@ -76,7 +90,7 @@ case 0x12:

case 0x13: gb->memory.mbc = _GBMBC3; gb->memory.mbcType = GB_MBC3; - gb->sramSize = 0x2048; + gb->sramSize += 0x48; break; default: mLOG(GB_MBC, WARN, "Unknown MBC type: %02X", cart->type);

@@ -86,14 +100,12 @@ case 0x1A:

case 0x1B: gb->memory.mbc = _GBMBC5; gb->memory.mbcType = GB_MBC5; - gb->sramSize = 0x2000; break; case 0x1C: case 0x1D: case 0x1E: gb->memory.mbc = _GBMBC5; gb->memory.mbcType = GB_MBC5_RUMBLE; - gb->sramSize = 0x2000; break; case 0x20: gb->memory.mbc = _GBMBC6;

@@ -108,7 +120,6 @@ break;

case 0xFE: gb->memory.mbc = _GBHuC3; gb->memory.mbcType = GB_HuC3; - gb->sramSize = 0x2000; break; }