all repos — mgba @ 3ac0b20ff86438bcea93ad610aeef184781fa9e3

mGBA Game Boy Advance Emulator

Core: Cores can now have multiple sets of callbacks
Vicki Pfau vi@endrift.com
Wed, 08 Feb 2017 11:34:05 -0800
commit

3ac0b20ff86438bcea93ad610aeef184781fa9e3

parent

be3e884ba51609b058b0da46c7e8bded3d2a7376

M CHANGESCHANGES

@@ -68,6 +68,7 @@ - GBA, GB: ROM is now unloaded if a patch is applied

- Util: Add 8-bit PNG write support - Qt: Rename "Resample video" option to "Bilinear filtering" - GBA Video: Optimize when BLD* registers are written frequently + - Core: Cores can now have multiple sets of callbacks 0.5.2: (2016-12-31) Bugfixes:
M include/mgba/core/core.hinclude/mgba/core/core.h

@@ -73,7 +73,8 @@ struct blip_t* (*getAudioChannel)(struct mCore*, int ch);

void (*setAudioBufferSize)(struct mCore*, size_t samples); size_t (*getAudioBufferSize)(struct mCore*); - void (*setCoreCallbacks)(struct mCore*, struct mCoreCallbacks*); + void (*addCoreCallbacks)(struct mCore*, struct mCoreCallbacks*); + void (*clearCoreCallbacks)(struct mCore*); void (*setAVStream)(struct mCore*, struct mAVStream*); bool (*isROM)(struct VFile* vf);
M include/mgba/core/interface.hinclude/mgba/core/interface.h

@@ -10,6 +10,8 @@ #include <mgba-util/common.h>

CXX_GUARD_START +#include <mgba-util/vector.h> + struct mCore; #ifdef COLOR_16_BIT

@@ -36,6 +38,8 @@ void (*videoFrameStarted)(void* context);

void (*videoFrameEnded)(void* context); void (*coreCrashed)(void* context); }; + +DECLARE_VECTOR(mCoreCallbacksList, struct mCoreCallbacks); struct mAVStream { void (*videoDimensionsChanged)(struct mAVStream*, unsigned width, unsigned height);
M include/mgba/internal/gb/gb.hinclude/mgba/internal/gb/gb.h

@@ -11,6 +11,7 @@

CXX_GUARD_START #include <mgba/core/cpu.h> +#include <mgba/core/interface.h> #include <mgba/core/log.h> #include <mgba/core/timing.h>

@@ -46,7 +47,6 @@

struct LR35902Core; struct mCoreSync; struct mAVStream; -struct mCoreCallbacks; struct GB { struct mCPUComponent d;

@@ -76,7 +76,7 @@ int sramDirty;

int32_t sramDirtAge; bool sramMaskWriteback; - struct mCoreCallbacks* coreCallbacks; + struct mCoreCallbacksList coreCallbacks; struct mAVStream* stream; bool cpuBlocked;
M include/mgba/internal/gba/gba.hinclude/mgba/internal/gba/gba.h

@@ -100,7 +100,7 @@

struct mAVStream* stream; struct mKeyCallback* keyCallback; struct mStopCallback* stopCallback; - struct mCoreCallbacks* coreCallbacks; + struct mCoreCallbacksList coreCallbacks; enum GBAIdleLoopOptimization idleOptimization; uint32_t idleLoop;
M src/core/interface.csrc/core/interface.c

@@ -7,6 +7,8 @@ #include <mgba/core/interface.h>

#include <mgba/core/core.h> +DEFINE_VECTOR(mCoreCallbacksList, struct mCoreCallbacks); + static time_t _rtcGenericCallback(struct mRTCSource* source) { struct mRTCGenericSource* rtc = (struct mRTCGenericSource*) source; switch (rtc->override) {
M src/core/thread.csrc/core/thread.c

@@ -143,7 +143,7 @@ .videoFrameEnded = _frameEnded,

.coreCrashed = _crashed, .context = threadContext }; - core->setCoreCallbacks(core, &callbacks); + core->addCoreCallbacks(core, &callbacks); core->setSync(core, &threadContext->sync); core->reset(core);

@@ -223,7 +223,7 @@

if (threadContext->cleanCallback) { threadContext->cleanCallback(threadContext); } - core->setCoreCallbacks(core, NULL); + core->clearCoreCallbacks(core); return 0; }
M src/gb/core.csrc/gb/core.c

@@ -156,9 +156,14 @@ struct GB* gb = core->board;

return gb->audio.samples; } -static void _GBCoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) { +static void _GBCoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) { + struct GB* gb = core->board; + *mCoreCallbacksListAppend(&gb->coreCallbacks) = *coreCallbacks; +} + +static void _GBCoreClearCoreCallbacks(struct mCore* core) { struct GB* gb = core->board; - gb->coreCallbacks = coreCallbacks; + mCoreCallbacksListClear(&gb->coreCallbacks); } static void _GBCoreSetAVStream(struct mCore* core, struct mAVStream* stream) {

@@ -576,7 +581,8 @@ core->getAudioChannel = _GBCoreGetAudioChannel;

core->setAudioBufferSize = _GBCoreSetAudioBufferSize; core->getAudioBufferSize = _GBCoreGetAudioBufferSize; core->setAVStream = _GBCoreSetAVStream; - core->setCoreCallbacks = _GBCoreSetCoreCallbacks; + core->addCoreCallbacks = _GBCoreAddCoreCallbacks; + core->clearCoreCallbacks = _GBCoreClearCoreCallbacks; core->isROM = GBIsROM; core->loadROM = _GBCoreLoadROM; core->loadBIOS = _GBCoreLoadBIOS;
M src/gb/gb.csrc/gb/gb.c

@@ -83,7 +83,7 @@ gb->isPristine = false;

gb->pristineRomSize = 0; gb->yankedRomSize = 0; - gb->coreCallbacks = NULL; + mCoreCallbacksListInit(&gb->coreCallbacks, 0); gb->stream = NULL; mTimingInit(&gb->timing, &gb->cpu->cycles, &gb->cpu->nextEvent);

@@ -349,6 +349,7 @@ GBMemoryDeinit(gb);

GBAudioDeinit(&gb->audio); GBVideoDeinit(&gb->video); GBSIODeinit(&gb->sio); + mCoreCallbacksListDeinit(&gb->coreCallbacks); } void GBInterruptHandlerInit(struct LR35902InterruptHandler* irqh) {
M src/gb/video.csrc/gb/video.c

@@ -129,9 +129,12 @@ video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);

} video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK); - struct mCoreCallbacks* callbacks = video->p->coreCallbacks; - if (callbacks && callbacks->videoFrameEnded) { - callbacks->videoFrameEnded(callbacks->context); + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c); + if (callbacks->videoFrameEnded) { + callbacks->videoFrameEnded(callbacks->context); + } } } if (!GBRegisterSTATIsHblankIRQ(video->stat) && GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {

@@ -244,9 +247,12 @@ video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);

video->p->stream->postVideoFrame(video->p->stream, pixels, stride); } - struct mCoreCallbacks* callbacks = video->p->coreCallbacks; - if (callbacks && callbacks->videoFrameStarted) { - callbacks->videoFrameStarted(callbacks->context); + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c); + if (callbacks->videoFrameEnded) { + callbacks->videoFrameStarted(callbacks->context); + } } if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
M src/gba/core.csrc/gba/core.c

@@ -186,9 +186,14 @@ struct GBA* gba = core->board;

return gba->audio.samples; } -static void _GBACoreSetCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) { +static void _GBACoreAddCoreCallbacks(struct mCore* core, struct mCoreCallbacks* coreCallbacks) { + struct GBA* gba = core->board; + *mCoreCallbacksListAppend(&gba->coreCallbacks) = *coreCallbacks; +} + +static void _GBACoreClearCoreCallbacks(struct mCore* core) { struct GBA* gba = core->board; - gba->coreCallbacks = coreCallbacks; + mCoreCallbacksListClear(&gba->coreCallbacks); } static void _GBACoreSetAVStream(struct mCore* core, struct mAVStream* stream) {

@@ -589,7 +594,8 @@ core->putPixels = _GBACorePutPixels;

core->getAudioChannel = _GBACoreGetAudioChannel; core->setAudioBufferSize = _GBACoreSetAudioBufferSize; core->getAudioBufferSize = _GBACoreGetAudioBufferSize; - core->setCoreCallbacks = _GBACoreSetCoreCallbacks; + core->addCoreCallbacks = _GBACoreAddCoreCallbacks; + core->clearCoreCallbacks = _GBACoreClearCoreCallbacks; core->setAVStream = _GBACoreSetAVStream; core->isROM = GBAIsROM; core->loadROM = _GBACoreLoadROM;
M src/gba/gba.csrc/gba/gba.c

@@ -91,7 +91,7 @@ gba->stream = NULL;

gba->keyCallback = NULL; gba->stopCallback = NULL; gba->stopCallback = NULL; - gba->coreCallbacks = NULL; + mCoreCallbacksListInit(&gba->coreCallbacks, 0); gba->biosChecksum = GBAChecksum(gba->memory.bios, SIZE_BIOS);

@@ -152,6 +152,7 @@ GBAAudioDeinit(&gba->audio);

GBASIODeinit(&gba->sio); gba->rr = 0; mTimingDeinit(&gba->timing); + mCoreCallbacksListDeinit(&gba->coreCallbacks); } void GBAInterruptHandlerInit(struct ARMInterruptHandler* irqh) {

@@ -632,9 +633,12 @@

void GBAFrameStarted(struct GBA* gba) { UNUSED(gba); - struct mCoreCallbacks* callbacks = gba->coreCallbacks; - if (callbacks && callbacks->videoFrameStarted) { - callbacks->videoFrameStarted(callbacks->context); + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c); + if (callbacks->videoFrameEnded) { + callbacks->videoFrameStarted(callbacks->context); + } } }

@@ -665,9 +669,12 @@ if (gba->memory.hw.devices & (HW_GB_PLAYER | HW_GB_PLAYER_DETECTION)) {

GBAHardwarePlayerUpdate(gba); } - struct mCoreCallbacks* callbacks = gba->coreCallbacks; - if (callbacks && callbacks->videoFrameEnded) { - callbacks->videoFrameEnded(callbacks->context); + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c); + if (callbacks->videoFrameEnded) { + callbacks->videoFrameEnded(callbacks->context); + } } }
M src/gba/memory.csrc/gba/memory.c

@@ -303,9 +303,15 @@ cpu->memory.activeRegion = _deadbeef;

cpu->memory.activeMask = 0; if (gba->yankedRomSize || !gba->hardCrash) { mLOG(GBA_MEM, GAME_ERROR, "Jumped to invalid address: %08X", address); - } else if (gba->coreCallbacks && gba->coreCallbacks->coreCrashed) { + } else if (mCoreCallbacksListSize(&gba->coreCallbacks)) { mLOG(GBA_MEM, GAME_ERROR, "Jumped to invalid address: %08X", address); - gba->coreCallbacks->coreCrashed(gba->coreCallbacks->context); + size_t c; + for (c = 0; c < mCoreCallbacksListSize(&gba->coreCallbacks); ++c) { + struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&gba->coreCallbacks, c); + if (callbacks->coreCrashed) { + callbacks->coreCrashed(callbacks->context); + } + } } else { mLOG(GBA_MEM, FATAL, "Jumped to invalid address: %08X", address); }