GB: Clean up BIOS unmapping
Vicki Pfau vi@endrift.com
Thu, 25 Feb 2021 23:42:45 -0800
4 files changed,
11 insertions(+),
11 deletions(-)
M
include/mgba/internal/gb/memory.h
→
include/mgba/internal/gb/memory.h
@@ -180,6 +180,7 @@ GBMemoryBankControllerWrite mbcWrite;
GBMemoryBankControllerRead mbcRead; union GBMBCState mbcState; int currentBank; + int currentBank0; uint8_t* wram; uint8_t* wramBank;
M
src/gb/gb.c
→
src/gb/gb.c
@@ -113,7 +113,6 @@ if (!gb->memory.rom) {
return false; } gb->yankedRomSize = 0; - gb->memory.romBase = gb->memory.rom; gb->memory.romSize = gb->pristineRomSize; gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize); memset(&gb->memory.mbcState, 0, sizeof(gb->memory.mbcState));@@ -294,9 +293,6 @@ }
void GBUnloadROM(struct GB* gb) { // TODO: Share with GBAUnloadROM - if (gb->memory.rom && gb->memory.romBase != gb->memory.rom && !gb->isPristine) { - free(gb->memory.romBase); - } if (gb->memory.rom && !gb->isPristine) { if (gb->yankedRomSize) { gb->yankedRomSize = 0;@@ -374,6 +370,7 @@ gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
} void GBDestroy(struct GB* gb) { + GBUnmapBIOS(gb); GBUnloadROM(gb); if (gb->biosVf) {@@ -588,17 +585,18 @@ }
void GBMapBIOS(struct GB* gb) { gb->biosVf->seek(gb->biosVf, 0, SEEK_SET); - uint8_t* oldRomBase = gb->memory.romBase; gb->memory.romBase = malloc(GB_SIZE_CART_BANK0); ssize_t size = gb->biosVf->read(gb->biosVf, gb->memory.romBase, GB_SIZE_CART_BANK0); - memcpy(&gb->memory.romBase[size], &oldRomBase[size], GB_SIZE_CART_BANK0 - size); - if (size > 0x100) { - memcpy(&gb->memory.romBase[0x100], &oldRomBase[0x100], sizeof(struct GBCartridge)); + if (gb->memory.rom) { + memcpy(&gb->memory.romBase[size], &gb->memory.rom[size], GB_SIZE_CART_BANK0 - size); + if (size > 0x100) { + memcpy(&gb->memory.romBase[0x100], &gb->memory.rom[0x100], sizeof(struct GBCartridge)); + } } } void GBUnmapBIOS(struct GB* gb) { - if (gb->memory.romBase < gb->memory.rom || gb->memory.romBase > &gb->memory.rom[gb->memory.romSize - 1]) { + if (gb->memory.io[GB_REG_BANK] == 0xFF && gb->memory.romBase != gb->memory.rom) { free(gb->memory.romBase); if (gb->memory.mbcType == GB_MMM01) { GBMBCSwitchBank0(gb, gb->memory.romSize / GB_SIZE_CART_BANK0 - 2);
M
src/gb/mbc.c
→
src/gb/mbc.c
@@ -76,6 +76,7 @@ mLOG(GB_MBC, GAME_ERROR, "Attempting to switch to an invalid ROM bank: %0X", bank);
bankStart &= (gb->memory.romSize - 1); } gb->memory.romBase = &gb->memory.rom[bankStart]; + gb->memory.currentBank0 = bank; if (gb->cpu->pc < GB_SIZE_CART_BANK0) { gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc); }@@ -436,6 +437,7 @@ }
} void GBMBCReset(struct GB* gb) { + gb->memory.currentBank0 = 0; gb->memory.romBank = &gb->memory.rom[GB_SIZE_CART_BANK0]; memset(&gb->memory.mbcState, 0, sizeof(gb->memory.mbcState));
M
src/gb/serialize.c
→
src/gb/serialize.c
@@ -195,6 +195,7 @@ } else {
gb->audio.style = GB_AUDIO_CGB; } + GBUnmapBIOS(gb); GBMemoryDeserialize(gb, state); GBVideoDeserialize(&gb->video, state); GBIODeserialize(gb, state);@@ -203,8 +204,6 @@ GBAudioDeserialize(&gb->audio, state);
if (gb->memory.io[GB_REG_BANK] == 0xFF) { GBMapBIOS(gb); - } else { - GBUnmapBIOS(gb); } if (gb->model & GB_MODEL_SGB && canSgb) {