From c8da211026371dde1a30d82fa84f68034409bb59 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Thu, 7 Feb 2013 22:49:20 -0500 Subject: [PATCH] mm init, newptr --- bin/CMakeLists.txt | 1 + bin/loader.cpp | 4 ++- toolbox/mm.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++-- toolbox/mm.h | 8 +++-- 4 files changed, 93 insertions(+), 6 deletions(-) diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index 0f65c45..8f9cbf5 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -9,3 +9,4 @@ add_definitions(-I ${CMAKE_SOURCE_DIR}/) add_executable(mpw loader.cpp) target_link_libraries(mpw CPU_LIB) target_link_libraries(mpw TOOLBOX_LIB) +target_link_libraries(mpw MPLITE_LIB) diff --git a/bin/loader.cpp b/bin/loader.cpp index 7d5e67f..e6dbc65 100644 --- a/bin/loader.cpp +++ b/bin/loader.cpp @@ -14,6 +14,8 @@ #include #include +#include +#include uint8_t *Memory; uint32_t HighWater = 0x10000; @@ -467,7 +469,7 @@ int main(int argc, char **argv) cpuSetALineExceptionFunc(ToolBox::dispatch); memorySetMemory(Memory, MemorySize); - + MM::Init(Memory, MemorySize, HighWater); for (unsigned i = 0; i < 10000; ++i) { diff --git a/toolbox/mm.cpp b/toolbox/mm.cpp index 3de24c1..5be9f7d 100644 --- a/toolbox/mm.cpp +++ b/toolbox/mm.cpp @@ -5,17 +5,74 @@ #include #include +#include +#include +#include #include namespace { mplite_t pool; + + uint8_t *Memory; + uint32_t MemorySize; + + // queue of free Handles + std::deque HandleQueue; + + // map of ptr -> size + std::map PtrMap; + + // map of handle -> size [? just use Ptr map?] + std::map HandleMap; + + bool alloc_handle_block() + { + const unsigned HandleCount = 128; + uint8_t *block = (uint8_t *)mplite_malloc(&pool, + sizeof(uint32_t) * HandleCount); + + if (!block) return false; + + uint32_t hh = block - Memory; + uint32_t end = hh + 128 * sizeof(uint32_t); + + for ( ; hh < end; hh += sizeof(uint32_t)) + { + HandleQueue.push_back(hh); + } + + return true; + } + } namespace MM { + bool Init(uint8_t *memory, uint32_t memorySize, uint32_t reserved) + { + int ok; + + Memory = memory; + MemorySize = memorySize; + + ok = mplite_init(&pool, + memory + reserved, + memorySize - reserved, + 32, + NULL); + + if (ok != MPLITE_OK) return false; + + // allocate a handle block... + + if (!alloc_handle_block()) return false; + + return true; + } + uint16_t NewPtr(uint16_t trap) { /* on entry: @@ -32,8 +89,33 @@ namespace MM fprintf(stderr, "%04x NewPtr(%08x)\n", trap, size); - cpuSetAReg(0, 0); - return memFullErr; + + // todo -- separate pools for sys vs non-sys? + // todo -- NewPtr(0) -- null or empty ptr? + + if (size == 0) + { + cpuSetAReg(0, 0); + return 0; + } + + uint8_t *ptr = NULL; + ptr = (uint8_t *)mplite_malloc(&pool, size); + if (!ptr) + { + cpuSetAReg(0, 0); + return memFullErr; + } + + if (clear) + { + std::memset(ptr, 0, size); + } + + uint32_t mcptr = ptr - Memory; + PtrMap.emplace(std::make_pair(mcptr, size)); + cpuSetAReg(0, mcptr); + return 0; } diff --git a/toolbox/mm.h b/toolbox/mm.h index c50f8f6..5d483eb 100644 --- a/toolbox/mm.h +++ b/toolbox/mm.h @@ -1,5 +1,5 @@ -#ifndef __mpw_rm_h__ -#define __mpw_rm_h__ +#ifndef __mpw_mm_h__ +#define __mpw_mm_h__ #include @@ -8,7 +8,9 @@ namespace MM enum { memFullErr = -108 - } + }; + + bool Init(uint8_t *memory, uint32_t memorySize, uint32_t reserved); uint16_t NewPtr(uint16_t trap); }