GB: Clean up BIOS unmapping

This commit is contained in:
Vicki Pfau 2021-02-25 23:42:45 -08:00
parent 003e181add
commit b966d71a2c
4 changed files with 11 additions and 11 deletions

View File

@ -180,6 +180,7 @@ struct GBMemory {
GBMemoryBankControllerRead mbcRead;
union GBMBCState mbcState;
int currentBank;
int currentBank0;
uint8_t* wram;
uint8_t* wramBank;

View File

@ -113,7 +113,6 @@ bool GBLoadROM(struct GB* gb, struct VFile* vf) {
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 GBSavedataUnmask(struct GB* gb) {
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 @@ void GBApplyPatch(struct GB* gb, struct Patch* patch) {
}
void GBDestroy(struct GB* gb) {
GBUnmapBIOS(gb);
GBUnloadROM(gb);
if (gb->biosVf) {
@ -588,17 +585,18 @@ void GBSkipBIOS(struct GB* gb) {
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);

View File

@ -76,6 +76,7 @@ void GBMBCSwitchBank0(struct GB* gb, int 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 GBMBCInit(struct GB* gb) {
}
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));

View File

@ -195,6 +195,7 @@ bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
gb->audio.style = GB_AUDIO_CGB;
}
GBUnmapBIOS(gb);
GBMemoryDeserialize(gb, state);
GBVideoDeserialize(&gb->video, state);
GBIODeserialize(gb, state);
@ -203,8 +204,6 @@ bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
if (gb->memory.io[GB_REG_BANK] == 0xFF) {
GBMapBIOS(gb);
} else {
GBUnmapBIOS(gb);
}
if (gb->model & GB_MODEL_SGB && canSgb) {