From 3202811e416e8b7c6ddc8a68442536510aa2b85e Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sat, 19 Jul 2014 14:34:24 -0700 Subject: [PATCH] Be more rigorous when reseting --- src/gba/gba-audio.c | 15 +++++++++++---- src/gba/gba-audio.h | 1 + src/gba/gba-memory.c | 1 - src/gba/gba-video.c | 19 ++++++++++++++++++- src/gba/gba-video.h | 2 ++ src/gba/gba.c | 3 +++ src/gba/renderers/video-software.c | 1 + 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/gba/gba-audio.c b/src/gba/gba-audio.c index 50bb51468..05427f7a0 100644 --- a/src/gba/gba-audio.c +++ b/src/gba/gba-audio.c @@ -20,6 +20,13 @@ static int _applyBias(struct GBAAudio* audio, int sample); static void _sample(struct GBAAudio* audio); void GBAAudioInit(struct GBAAudio* audio) { + CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t)); + CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t)); + CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE); + CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE); +} + +void GBAAudioReset(struct GBAAudio* audio) { audio->nextEvent = 0; audio->nextCh1 = 0; audio->nextCh2 = 0; @@ -49,10 +56,10 @@ void GBAAudioInit(struct GBAAudio* audio) { audio->soundcntX = 0; audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate; - CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t)); - CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t)); - CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE); - CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE); + CircleBufferClear(&audio->left); + CircleBufferClear(&audio->right); + CircleBufferClear(&audio->chA.fifo); + CircleBufferClear(&audio->chB.fifo); } void GBAAudioDeinit(struct GBAAudio* audio) { diff --git a/src/gba/gba-audio.h b/src/gba/gba-audio.h index f905af45c..e6ce78fc0 100644 --- a/src/gba/gba-audio.h +++ b/src/gba/gba-audio.h @@ -219,6 +219,7 @@ struct GBAStereoSample { }; void GBAAudioInit(struct GBAAudio* audio); +void GBAAudioReset(struct GBAAudio* audio); void GBAAudioDeinit(struct GBAAudio* audio); int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles); diff --git a/src/gba/gba-memory.c b/src/gba/gba-memory.c index 381684331..5993a7c71 100644 --- a/src/gba/gba-memory.c +++ b/src/gba/gba-memory.c @@ -102,7 +102,6 @@ void GBAMemoryReset(struct GBA* gba) { if (!gba->memory.wram || !gba->memory.iwram) { GBAMemoryDeinit(gba); GBALog(gba, GBA_LOG_FATAL, "Could not map memory"); - return; } } diff --git a/src/gba/gba-video.c b/src/gba/gba-video.c index 37fbfc772..20c2f4d38 100644 --- a/src/gba/gba-video.c +++ b/src/gba/gba-video.c @@ -9,6 +9,7 @@ #include "util/memory.h" static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer); +static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer); static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer); static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value); static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y); @@ -16,6 +17,7 @@ static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer); static struct GBAVideoRenderer dummyRenderer = { .init = GBAVideoDummyRendererInit, + .reset = GBAVideoDummyRendererReset, .deinit = GBAVideoDummyRendererDeinit, .writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister, .drawScanline = GBAVideoDummyRendererDrawScanline, @@ -24,7 +26,10 @@ static struct GBAVideoRenderer dummyRenderer = { void GBAVideoInit(struct GBAVideo* video) { video->renderer = &dummyRenderer; + video->vram = 0; +} +void GBAVideoReset(struct GBAVideo* video) { video->inHblank = 0; video->inVblank = 0; video->vcounter = 0; @@ -44,11 +49,18 @@ void GBAVideoInit(struct GBAVideo* video) { video->nextVblankIRQ = 0; video->nextVcounterIRQ = 0; + if (video->vram) { + mappedMemoryFree(video->vram, SIZE_VRAM); + } video->vram = anonymousMemoryMap(SIZE_VRAM); + video->renderer->vram = video->vram; int i; for (i = 0; i < 128; ++i) { - video->oam.obj[i].disable = 1; + video->oam.raw[i * 4] = 0x0200; + video->oam.raw[i * 4 + 1] = 0x0000; + video->oam.raw[i * 4 + 2] = 0x0000; + video->oam.raw[i * 4 + 3] = 0x0000; } } @@ -161,6 +173,11 @@ static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) { // Nothing to do } +static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) { + UNUSED(renderer); + // Nothing to do +} + static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) { UNUSED(renderer); // Nothing to do diff --git a/src/gba/gba-video.h b/src/gba/gba-video.h index b8a31f0bd..053b60938 100644 --- a/src/gba/gba-video.h +++ b/src/gba/gba-video.h @@ -169,6 +169,7 @@ union GBARegisterBGCNT { struct GBAVideoRenderer { void (*init)(struct GBAVideoRenderer* renderer); + void (*reset)(struct GBAVideoRenderer* renderer); void (*deinit)(struct GBAVideoRenderer* renderer); uint16_t (*writeVideoRegister)(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value); @@ -213,6 +214,7 @@ struct GBAVideo { }; void GBAVideoInit(struct GBAVideo* video); +void GBAVideoReset(struct GBAVideo* video); void GBAVideoDeinit(struct GBAVideo* video); void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer); int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles); diff --git a/src/gba/gba.c b/src/gba/gba.c index 867958af4..79530b8d6 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -182,6 +182,9 @@ void GBAReset(struct ARMCore* cpu) { struct GBA* gba = (struct GBA*) cpu->master; GBAMemoryReset(gba); + GBAVideoReset(&gba->video); + GBAAudioReset(&gba->audio); + GBAIOInit(gba); } static void GBAProcessEvents(struct ARMCore* cpu) { diff --git a/src/gba/renderers/video-software.c b/src/gba/renderers/video-software.c index 13c9ba1ed..300e2bdd5 100644 --- a/src/gba/renderers/video-software.c +++ b/src/gba/renderers/video-software.c @@ -60,6 +60,7 @@ static unsigned _mix(int weightA, unsigned colorA, int weightB, unsigned colorB) void GBAVideoSoftwareRendererCreate(struct GBAVideoSoftwareRenderer* renderer) { renderer->d.init = GBAVideoSoftwareRendererInit; + renderer->d.reset = GBAVideoSoftwareRendererInit; renderer->d.deinit = GBAVideoSoftwareRendererDeinit; renderer->d.writeVideoRegister = GBAVideoSoftwareRendererWriteVideoRegister; renderer->d.writeOAM = GBAVideoSoftwareRendererWriteOAM;