Core: Add memory block info lookup function
Vicki Pfau vi@endrift.com
Fri, 01 Jan 2021 17:24:09 -0800
2 files changed,
15 insertions(+),
9 deletions(-)
M
include/mgba/core/core.h
→
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;
M
src/core/core.c
→
src/core/core.c
@@ -360,6 +360,17 @@ return mCoreGetMemoryBlockMasked(core, start, size, mCORE_MEMORY_MAPPED);
} 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 @@ for (i = 0; i < nBlocks; ++i) {
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; }