mpw/toolbox/mm.cpp

161 lines
2.6 KiB
C++
Raw Normal View History

2013-02-08 03:12:30 +00:00
#include "mm.h"
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include <string>
2013-02-08 03:49:20 +00:00
#include <deque>
#include <vector>
#include <map>
2013-02-08 03:12:30 +00:00
#include <mplite/mplite.h>
namespace
{
mplite_t pool;
2013-02-08 03:49:20 +00:00
uint8_t *Memory;
uint32_t MemorySize;
// queue of free Handles
std::deque<uint32_t> HandleQueue;
// map of ptr -> size
std::map<uint32_t, uint32_t> PtrMap;
// map of handle -> size [? just use Ptr map?]
std::map<uint32_t, uint32_t> 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;
}
2013-02-08 03:12:30 +00:00
}
namespace MM
{
2013-02-08 03:49:20 +00:00
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;
}
2013-02-08 04:44:58 +00:00
uint16_t BlockMove(uint16_t trap)
{
/*
* on entry:
* A0 Pointer to source
* A1 Pointer to destination
* D0 Number of bytes to copy
*
* on exit:
* A0 Address of the new block or NIL
* D0 Result code
*
*/
uint32_t source = cpuGetAReg(0);
uint32_t dest = cpuGetAReg(1);
uint32_t count = cpuGetDReg(0);
fprintf(stderr, "%04x BlockMove(%08x, %08x, %08x)\n",
trap, source, dest, count);
// TODO -- 32-bit clean?
// TODO -- verify within MemorySize?
#if 0
if (source == 0 || dest == 0 || count == 0)
return 0;
#endif
std::memmove(Memory + dest, Memory + source, count);
return 0;
}
2013-02-08 03:12:30 +00:00
uint16_t NewPtr(uint16_t trap)
{
2013-02-08 04:44:58 +00:00
/*
* on entry:
* D0 Number of logical bytes requested
*
2013-02-08 03:12:30 +00:00
* on exit:
* A0 Address of the new block or NIL
* D0 Result code
2013-02-08 04:44:58 +00:00
*
2013-02-08 03:12:30 +00:00
*/
bool clear = trap & (1 << 9);
2013-02-08 04:44:58 +00:00
//bool sys = trap & (1 << 10);
2013-02-08 03:12:30 +00:00
2013-02-08 04:44:58 +00:00
uint32_t size = cpuGetDReg(0);
2013-02-08 03:12:30 +00:00
fprintf(stderr, "%04x NewPtr(%08x)\n", trap, size);
2013-02-08 03:49:20 +00:00
// 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;
2013-02-08 03:12:30 +00:00
}
}