diff --git a/src/gb/gb.c b/src/gb/gb.c index 19331e1a6..137e81ac0 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -133,7 +133,7 @@ void GBResizeSram(struct GB* gb, size_t size) { 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); } @@ -145,6 +145,9 @@ void GBResizeSram(struct GB* gb, size_t size) { } 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); if (gb->memory.sram) { @@ -160,7 +163,9 @@ void GBResizeSram(struct GB* gb, size_t size) { } gb->memory.sram = newSram; } - gb->sramSize = size; + if (gb->sramSize < size) { + gb->sramSize = size; + } } void GBSavedataMask(struct GB* gb, struct VFile* vf) { diff --git a/src/gb/mbc.c b/src/gb/mbc.c index 88d0c5b0d..ece45db44 100644 --- a/src/gb/mbc.c +++ b/src/gb/mbc.c @@ -48,20 +48,34 @@ void GBMBCSwitchSramBank(struct GB* gb, int bank) { 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 @@ void GBMBCInit(struct GB* gb) { 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 @@ void GBMBCInit(struct GB* gb) { 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 @@ void GBMBCInit(struct GB* gb) { case 0xFE: gb->memory.mbc = _GBHuC3; gb->memory.mbcType = GB_HuC3; - gb->sramSize = 0x2000; break; }