Core: Add memory block info lookup function

This commit is contained in:
Vicki Pfau 2021-01-01 17:24:09 -08:00
parent cab1415d07
commit 1c69a714c7
2 changed files with 15 additions and 9 deletions

View File

@ -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;

View File

@ -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;
}