From 68e5a0aa475e865561242a2ca32304fcd5c5c79c Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 11 Dec 2023 22:05:55 -0800 Subject: [PATCH] GB: Fix applying a patch that changes the cartridge mapper (fixes #3077) --- CHANGES | 1 + src/gb/gb.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGES b/CHANGES index 036378226..0e732633f 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ Emulation fixes: - GBA BIOS: Fix clobbering registers with word-sized CpuSet - GBA SIO: Fix normal mode SI/SO semantics (fixes mgba.io/i/2925) Other fixes: + - GB: Fix applying a patch that changes the cartridge mapper (fixes mgba.io/i/3077) - GBA Savedata: Fix crash when resizing flash save games for RTC data - mGUI: Fix cases where an older save state screenshot would be shown (fixes mgba.io/i/2183) - Qt: Re-enable sync for multiplayer windows that aren't connected (fixes mgba.io/i/2974) diff --git a/src/gb/gb.c b/src/gb/gb.c index 4cbff7e4f..6c1c265dc 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -455,6 +455,9 @@ void GBApplyPatch(struct GB* gb, struct Patch* patch) { if (patchedSize > GB_SIZE_CART_MAX) { patchedSize = GB_SIZE_CART_MAX; } + + const struct GBCartridge* cart = (const struct GBCartridge*) &gb->memory.rom[0x100]; + uint8_t type = cart->type; void* newRom = anonymousMemoryMap(GB_SIZE_CART_MAX); if (!patch->applyPatch(patch, gb->memory.rom, gb->pristineRomSize, newRom, patchedSize)) { mappedMemoryFree(newRom, GB_SIZE_CART_MAX); @@ -473,6 +476,12 @@ void GBApplyPatch(struct GB* gb, struct Patch* patch) { } gb->memory.rom = newRom; gb->memory.romSize = patchedSize; + + cart = (const struct GBCartridge*) &gb->memory.rom[0x100]; + if (cart->type != type) { + gb->memory.mbcType = GB_MBC_AUTODETECT; + GBMBCInit(gb); + } gb->romCrc32 = doCrc32(gb->memory.rom, gb->memory.romSize); gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc); }