From 6456a886695e6290fee373255fef014f47c20562 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 19 Jul 2014 04:13:25 -0700 Subject: [PATCH] Reset memory when reseting the board (fixes #94) --- src/gba/gba-memory.c | 50 ++++++++++++++++++++++++++++---------------- src/gba/gba-memory.h | 2 ++ src/gba/gba.c | 3 +++ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/gba/gba-memory.c b/src/gba/gba-memory.c index 25ceba92b..381684331 100644 --- a/src/gba/gba-memory.c +++ b/src/gba/gba-memory.c @@ -31,27 +31,12 @@ void GBAMemoryInit(struct GBA* gba) { gba->memory.bios = (uint32_t*) hleBios; gba->memory.fullBios = 0; - gba->memory.wram = anonymousMemoryMap(SIZE_WORKING_RAM); - gba->memory.iwram = anonymousMemoryMap(SIZE_WORKING_IRAM); + gba->memory.wram = 0; + gba->memory.iwram = 0; gba->memory.rom = 0; gba->memory.gpio.p = gba; - memset(gba->memory.io, 0, sizeof(gba->memory.io)); - memset(gba->memory.dma, 0, sizeof(gba->memory.dma)); + int i; - for (i = 0; i < 4; ++i) { - gba->memory.dma[i].count = 0x10000; - gba->memory.dma[i].nextEvent = INT_MAX; - } - gba->memory.activeDMA = -1; - gba->memory.nextDMA = INT_MAX; - gba->memory.eventDiff = 0; - - if (!gba->memory.wram || !gba->memory.iwram) { - GBAMemoryDeinit(gba); - GBALog(gba, GBA_LOG_FATAL, "Could not map memory"); - return; - } - for (i = 0; i < 16; ++i) { gba->memory.waitstatesNonseq16[i] = GBA_BASE_WAITSTATES[i]; gba->memory.waitstatesSeq16[i] = GBA_BASE_WAITSTATES_SEQ[i]; @@ -92,6 +77,35 @@ void GBAMemoryDeinit(struct GBA* gba) { GBASavedataDeinit(&gba->memory.savedata); } +void GBAMemoryReset(struct GBA* gba) { + if (gba->memory.wram) { + mappedMemoryFree(gba->memory.wram, SIZE_WORKING_RAM); + } + gba->memory.wram = anonymousMemoryMap(SIZE_WORKING_RAM); + + if (gba->memory.iwram) { + mappedMemoryFree(gba->memory.iwram, SIZE_WORKING_IRAM); + } + gba->memory.iwram = anonymousMemoryMap(SIZE_WORKING_IRAM); + + memset(gba->memory.io, 0, sizeof(gba->memory.io)); + memset(gba->memory.dma, 0, sizeof(gba->memory.dma)); + int i; + for (i = 0; i < 4; ++i) { + gba->memory.dma[i].count = 0x10000; + gba->memory.dma[i].nextEvent = INT_MAX; + } + gba->memory.activeDMA = -1; + gba->memory.nextDMA = INT_MAX; + gba->memory.eventDiff = 0; + + if (!gba->memory.wram || !gba->memory.iwram) { + GBAMemoryDeinit(gba); + GBALog(gba, GBA_LOG_FATAL, "Could not map memory"); + return; + } +} + static void GBASetActiveRegion(struct ARMCore* cpu, uint32_t address) { struct GBA* gba = (struct GBA*) cpu->master; struct GBAMemory* memory = &gba->memory; diff --git a/src/gba/gba-memory.h b/src/gba/gba-memory.h index 84ff283c6..761995adc 100644 --- a/src/gba/gba-memory.h +++ b/src/gba/gba-memory.h @@ -138,6 +138,8 @@ struct GBAMemory { void GBAMemoryInit(struct GBA* gba); void GBAMemoryDeinit(struct GBA* gba); +void GBAMemoryReset(struct GBA* gba); + int32_t GBALoad32(struct ARMCore* cpu, uint32_t address, int* cycleCounter); int16_t GBALoad16(struct ARMCore* cpu, uint32_t address, int* cycleCounter); uint16_t GBALoadU16(struct ARMCore* cpu, uint32_t address, int* cycleCounter); diff --git a/src/gba/gba.c b/src/gba/gba.c index 1243ed98e..867958af4 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -179,6 +179,9 @@ void GBAReset(struct ARMCore* cpu) { cpu->gprs[ARM_SP] = SP_BASE_SUPERVISOR; ARMSetPrivilegeMode(cpu, MODE_SYSTEM); cpu->gprs[ARM_SP] = SP_BASE_SYSTEM; + + struct GBA* gba = (struct GBA*) cpu->master; + GBAMemoryReset(gba); } static void GBAProcessEvents(struct ARMCore* cpu) {