From c5dc97a4323a1ebd97de42367b60097a70c7c4b6 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 15 Sep 2023 22:06:05 -0700 Subject: [PATCH] GBA: Make sure unloading a ROM frees the AGBPrint buffer --- include/mgba/internal/gba/memory.h | 1 + src/gba/gba.c | 1 + src/gba/memory.c | 26 +++++++++++++++----------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/mgba/internal/gba/memory.h b/include/mgba/internal/gba/memory.h index 93d565a16..a11c7e1d3 100644 --- a/include/mgba/internal/gba/memory.h +++ b/include/mgba/internal/gba/memory.h @@ -147,6 +147,7 @@ void GBAMemoryInit(struct GBA* gba); void GBAMemoryDeinit(struct GBA* gba); void GBAMemoryReset(struct GBA* gba); +void GBAMemoryClearAGBPrint(struct GBA* gba); uint32_t GBALoad32(struct ARMCore* cpu, uint32_t address, int* cycleCounter); uint32_t GBALoad16(struct ARMCore* cpu, uint32_t address, int* cycleCounter); diff --git a/src/gba/gba.c b/src/gba/gba.c index 6b81a99c6..26c2058ae 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -132,6 +132,7 @@ static void GBAInit(void* cpu, struct mCPUComponent* component) { } void GBAUnloadROM(struct GBA* gba) { + GBAMemoryClearAGBPrint(gba); if (gba->memory.rom && !gba->isPristine) { if (gba->yankedRomSize) { gba->yankedRomSize = 0; diff --git a/src/gba/memory.c b/src/gba/memory.c index ba862afb9..8283b64a0 100644 --- a/src/gba/memory.c +++ b/src/gba/memory.c @@ -128,6 +128,21 @@ void GBAMemoryReset(struct GBA* gba) { GBAAdjustWaitstates(gba, 0); GBAAdjustEWRAMWaitstates(gba, 0x0D00); + GBAMemoryClearAGBPrint(gba); + + gba->memory.prefetch = false; + gba->memory.lastPrefetchedPc = 0; + + if (!gba->memory.wram || !gba->memory.iwram) { + GBAMemoryDeinit(gba); + mLOG(GBA_MEM, FATAL, "Could not map memory"); + } + + GBADMAReset(gba); + memset(&gba->memory.matrix, 0, sizeof(gba->memory.matrix)); +} + +void GBAMemoryClearAGBPrint(struct GBA* gba) { gba->memory.activeRegion = -1; gba->memory.agbPrintProtect = 0; gba->memory.agbPrintBase = 0; @@ -140,17 +155,6 @@ void GBAMemoryReset(struct GBA* gba) { mappedMemoryFree(gba->memory.agbPrintBufferBackup, SIZE_AGB_PRINT); gba->memory.agbPrintBufferBackup = NULL; } - - gba->memory.prefetch = false; - gba->memory.lastPrefetchedPc = 0; - - if (!gba->memory.wram || !gba->memory.iwram) { - GBAMemoryDeinit(gba); - mLOG(GBA_MEM, FATAL, "Could not map memory"); - } - - GBADMAReset(gba); - memset(&gba->memory.matrix, 0, sizeof(gba->memory.matrix)); } static void _analyzeForIdleLoop(struct GBA* gba, struct ARMCore* cpu, uint32_t address) {