From 1c69a714c7f383ab2a17fb27ea0aa59048868f67 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 1 Jan 2021 17:24:09 -0800 Subject: [PATCH] Core: Add memory block info lookup function --- include/mgba/core/core.h | 1 + src/core/core.c | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/include/mgba/core/core.h b/include/mgba/core/core.h index 9ba0edf44..fcd302279 100644 --- a/include/mgba/core/core.h +++ b/include/mgba/core/core.h @@ -198,6 +198,7 @@ void mCoreSetRTC(struct mCore* core, struct mRTCSource* rtc); void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size); void* mCoreGetMemoryBlockMasked(struct mCore* core, uint32_t start, size_t* size, uint32_t mask); +const struct mCoreMemoryBlock* mCoreGetMemoryBlockInfo(struct mCore* core, uint32_t address); #ifdef USE_ELF struct ELF; diff --git a/src/core/core.c b/src/core/core.c index 7d4283a34..082d547e2 100644 --- a/src/core/core.c +++ b/src/core/core.c @@ -360,6 +360,17 @@ void* mCoreGetMemoryBlock(struct mCore* core, uint32_t start, size_t* size) { } void* mCoreGetMemoryBlockMasked(struct mCore* core, uint32_t start, size_t* size, uint32_t mask) { + const struct mCoreMemoryBlock* block = mCoreGetMemoryBlockInfo(core, start); + if (!block || !(block->flags & mask)) { + return NULL; + } + uint8_t* out = core->getMemoryBlock(core, block->id, size); + out += start - block->start; + *size -= start - block->start; + return out; +} + +const struct mCoreMemoryBlock* mCoreGetMemoryBlockInfo(struct mCore* core, uint32_t address) { const struct mCoreMemoryBlock* blocks; size_t nBlocks = core->listMemoryBlocks(core, &blocks); size_t i; @@ -367,19 +378,13 @@ void* mCoreGetMemoryBlockMasked(struct mCore* core, uint32_t start, size_t* size if (!(blocks[i].flags & mCORE_MEMORY_MAPPED)) { continue; } - if (!(blocks[i].flags & mask)) { + if (address < blocks[i].start) { continue; } - if (start < blocks[i].start) { + if (address >= blocks[i].start + blocks[i].size) { continue; } - if (start >= blocks[i].start + blocks[i].size) { - continue; - } - uint8_t* out = core->getMemoryBlock(core, blocks[i].id, size); - out += start - blocks[i].start; - *size -= start - blocks[i].start; - return out; + return &blocks[i]; } return NULL; }