update Native tool calls to use tool_return<> for return values.

This commit is contained in:
Kelvin Sherlock 2016-08-01 13:56:43 -04:00
parent 45e7e81169
commit 94f4c1c528
9 changed files with 293 additions and 257 deletions

View File

@ -263,8 +263,6 @@ namespace MPW
uint32_t devptr = 0; uint32_t devptr = 0;
uint32_t fptr = 0; uint32_t fptr = 0;
uint16_t error;
// create the argv-data. // create the argv-data.
{ {
uint32_t size = 0; uint32_t size = 0;
@ -278,8 +276,9 @@ namespace MPW
size += l; size += l;
} }
error = MM::Native::NewPtr(size, true, argvptr); auto tmp = MM::Native::NewPtr(size, true);
if (error) return error; if (tmp.error()) return tmp.error();
argvptr = tmp.value();
uint8_t *xptr = memoryPointer(argvptr); uint8_t *xptr = memoryPointer(argvptr);
@ -331,9 +330,9 @@ namespace MPW
size += 4; // space for null terminator. size += 4; // space for null terminator.
error = MM::Native::NewPtr(size, true, envptr); auto tmp = MM::Native::NewPtr(size, true);
if (error) return error; if (tmp.error()) return tmp.error();
envptr = tmp.value();
uint8_t *xptr = memoryPointer(envptr); uint8_t *xptr = memoryPointer(envptr);
uint32_t offset = 0; uint32_t offset = 0;
@ -364,9 +363,9 @@ namespace MPW
// these are ftraps for emulated/native function ptrs. // these are ftraps for emulated/native function ptrs.
uint32_t size = 6 * 4; uint32_t size = 6 * 4;
error = MM::Native::NewPtr(size, true, fptr); auto tmp = MM::Native::NewPtr(size, true);
if (tmp.error()) return tmp.error();
if (error) return error; fptr = tmp.value();
memoryWriteWord(fQuit, fptr + 0); memoryWriteWord(fQuit, fptr + 0);
memoryWriteWord(0x4E75, fptr + 2); // rts memoryWriteWord(0x4E75, fptr + 2); // rts
@ -393,9 +392,9 @@ namespace MPW
{ {
uint32_t size = 0x78; uint32_t size = 0x78;
error = MM::Native::NewPtr(size, true, devptr); auto tmp = MM::Native::NewPtr(size, true);
if (tmp.error()) return tmp.error();
if (error) return error; devptr = tmp.value();
memoryWriteLong(0x46535953, devptr + 0); // 'FSYS' memoryWriteLong(0x46535953, devptr + 0); // 'FSYS'
memoryWriteLong(fptr + 4, devptr + 4); memoryWriteLong(fptr + 4, devptr + 4);
@ -415,9 +414,9 @@ namespace MPW
uint32_t size = 0x3c; uint32_t size = 0x3c;
uint32_t ptr; uint32_t ptr;
error = MM::Native::NewPtr(size, true, ioptr); auto tmp = MM::Native::NewPtr(size, true);
if (tmp.error()) return tmp.error();
if (error) return error; ioptr = tmp.value();
ptr = ioptr; ptr = ioptr;
// stdin // stdin
@ -450,10 +449,9 @@ namespace MPW
uint32_t mpi = 0; auto tmp = MM::Native::NewPtr(8 + 0x30, true);
if (tmp.error()) return tmp.error();
error = MM::Native::NewPtr(8 + 0x30, true, mpi); uint32_t mpi = tmp.value();
if (error) return error;
MacProgramInfo = mpi + 8; MacProgramInfo = mpi + 8;

View File

@ -350,7 +350,8 @@ namespace ToolBox {
// alternate entry code // alternate entry code
MM::Native::NewPtr(1024 * 4 + 256 * 4, false, ToolGlue); auto tmp = MM::Native::NewPtr(1024 * 4 + 256 * 4, false);
ToolGlue = tmp.value();
OSGlue = ToolGlue + 1024 * 4; OSGlue = ToolGlue + 1024 * 4;
uint16_t *code = (uint16_t *)memoryPointer(ToolGlue); uint16_t *code = (uint16_t *)memoryPointer(ToolGlue);

View File

@ -148,21 +148,24 @@ namespace Loader {
// load code seg 0. // load code seg 0.
uint16_t LoadCode0(Segment0Info &rv) uint16_t LoadCode0(Segment0Info &rv)
{ {
uint16_t err;
uint32_t rHandle; uint32_t rHandle;
uint32_t size; uint32_t size;
uint32_t address; uint32_t address;
SegmentInfo si; SegmentInfo si;
err = RM::Native::GetResource(kCODE, 0, rHandle); {
if (err) return err; auto tmp = RM::Native::GetResource(kCODE, 0);
if (tmp.error()) return tmp.error();
rHandle = tmp.value();
}
MM::Native::HLock(rHandle); MM::Native::HLock(rHandle);
MM::Native::GetHandleSize(rHandle, size); {
auto tmp = MM::Native::GetHandleInfo(rHandle);
address = memoryReadLong(rHandle); address = tmp.value().address;
size = tmp.value().size;
}
uint32_t above = memoryReadLong(address); uint32_t above = memoryReadLong(address);
uint32_t below = memoryReadLong(address + 4); uint32_t below = memoryReadLong(address + 4);
@ -173,8 +176,12 @@ namespace Loader {
// create a new handle for the a5 segment. // create a new handle for the a5 segment.
err = MM::Native::NewHandle(si.size, true, si.handle, si.address); {
if (err) return err; auto tmp = MM::Native::NewHandle(si.size, true);
if (tmp.error()) return tmp.error();
si.handle = tmp.value().handle;
si.address = tmp.value().pointer;
}
MM::Native::HLock(si.handle); MM::Native::HLock(si.handle);
@ -198,17 +205,21 @@ namespace Loader {
// load a standard code segment. // load a standard code segment.
uint16_t LoadCode(uint16_t segment) uint16_t LoadCode(uint16_t segment)
{ {
uint16_t err;
SegmentInfo si; SegmentInfo si;
err = RM::Native::GetResource(kCODE, segment, si.handle); {
if (err) return err; auto tmp = RM::Native::GetResource(kCODE, segment);
if (tmp.error()) return tmp.error();
si.handle = tmp.value();
}
MM::Native::HLock(si.handle); MM::Native::HLock(si.handle);
MM::Native::GetHandleSize(si.handle, si.size);
si.address = memoryReadLong(si.handle); {
auto tmp = MM::Native::GetHandleInfo(si.handle);
si.size = tmp.value().size;
si.address = tmp.value().address;
}
if (memoryReadWord(si.address) == 0xffff) if (memoryReadWord(si.address) == 0xffff)
si.farModel = true; si.farModel = true;
@ -343,7 +354,7 @@ namespace Loader {
{ {
std::string s; std::string s;
auto ix = path.rfind('/'); auto ix = path.find_last_of("/:", 0, 2);
if (ix == path.npos) if (ix == path.npos)
{ {
s = path.substr(0, 31); s = path.substr(0, 31);
@ -552,5 +563,35 @@ namespace Loader {
} }
uint16_t LoadSeg(uint16_t trap) {
/*
* Jump Table Entry:
*
* Unloaded:
* +0 offset
* +2 instruction that pushes segment number onto stack
* +6 LoadSeg trap
*
* Loaded:
* +0 offset
* +2 jump instruction.
*
*/
uint32_t sp;
uint16_t segment;
sp = StackFrame<2>(segment);
Log("%04x LoadSeg(%04x)\n", trap, segment);
return 0;
}
} }

View File

@ -26,6 +26,7 @@ namespace Loader {
} }
uint16_t LoadSeg(uint16_t trap);
uint16_t UnloadSeg(uint16_t trap); uint16_t UnloadSeg(uint16_t trap);
} }

View File

@ -45,6 +45,9 @@
using ToolBox::Log; using ToolBox::Log;
using MacOS::tool_return;
using MacOS::macos_error_from_errno;
using MacOS::macos_error;
namespace namespace
{ {
@ -65,15 +68,15 @@ namespace
// map of handle -> size [? just use Ptr map?] // map of handle -> size [? just use Ptr map?]
std::map<uint32_t, MM::HandleInfo> HandleMap; std::map<uint32_t, MM::HandleInfo> HandleMap;
inline MacOS::macos_error SetMemError(MacOS::macos_error error) inline macos_error SetMemError(macos_error error)
{ {
memoryWriteWord(error, MacOS::MemErr); memoryWriteWord(error, MacOS::MemErr);
return error; return error;
} }
inline MacOS::macos_error SetMemError(int16_t error) inline macos_error SetMemError(int16_t error)
{ {
return SetMemError((MacOS::macos_error)error); return SetMemError((macos_error)error);
} }
@ -98,13 +101,6 @@ namespace
} }
template<class Fx>
int16_t with_handle(uint32_t handle, Fx fx)
{
auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) return MacOS::memWZErr;
return fx(iter->second);
}
} }
namespace MM namespace MM
@ -139,6 +135,46 @@ namespace MM
namespace Native { namespace Native {
template<class T>
struct tool_return_base { typedef T type; };
template<class T>
struct tool_return_base<tool_return<T>> { typedef T type; };
template<class T, class F>
tool_return<T> with_handle_helper(F &&f, HandleInfo &info, typename std::enable_if<!std::is_void<T>::value>::type* = 0) {
tool_return<T> rv = f(info);
return rv;
}
template<class T, class F>
tool_return<void> with_handle_helper(F &&f, HandleInfo &info, typename std::enable_if<std::is_void<T>::value>::type* = 0) {
f(info);
return tool_return<void>();
}
template<class F, typename T = typename tool_return_base<typename std::result_of<F(HandleInfo &)>::type>::type>
tool_return<T>
__with_handle(uint32_t handle, F &&f)
{
const auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) {
tool_return<T> rv = SetMemError(MacOS::memWZErr);
return rv;
}
auto &info = iter->second;
//tool_return<T> rv = f(info);
tool_return<T> rv = with_handle_helper<T>(std::forward<F>(f), info);
SetMemError(rv.error());
return rv;
}
// debugger support. // debugger support.
// print info on an address. // print info on an address.
void MemoryInfo(uint32_t address) void MemoryInfo(uint32_t address)
@ -233,11 +269,11 @@ namespace MM
} }
uint16_t NewPtr(uint32_t size, bool clear, uint32_t &mcptr) tool_return<uint32_t> NewPtr(uint32_t size, bool clear)
{ {
// native pointers. // native pointers.
mcptr = 0; uint32_t mcptr = 0;
//if (size == 0) return 0; //if (size == 0) return 0;
uint8_t *ptr = nullptr; uint8_t *ptr = nullptr;
@ -253,10 +289,11 @@ namespace MM
mcptr = ptr - Memory; mcptr = ptr - Memory;
PtrMap.emplace(std::make_pair(mcptr, size)); PtrMap.emplace(std::make_pair(mcptr, size));
return SetMemError(0); SetMemError(0);
return mcptr;
} }
uint16_t DisposePtr(uint32_t mcptr) tool_return<void> DisposePtr(uint32_t mcptr)
{ {
auto iter = PtrMap.find(mcptr); auto iter = PtrMap.find(mcptr);
@ -271,13 +308,13 @@ namespace MM
return SetMemError(0); return SetMemError(0);
} }
uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle, uint32_t &mcptr) tool_return<hp> NewHandle(uint32_t size, bool clear)
{ {
uint8_t *ptr; uint8_t *ptr;
uint32_t hh; uint32_t hh;
handle = 0; uint32_t handle = 0;
mcptr = 0; uint32_t mcptr = 0;
if (!HandleQueue.size()) if (!HandleQueue.size())
{ {
@ -316,18 +353,12 @@ namespace MM
memoryWriteLong(mcptr, hh); memoryWriteLong(mcptr, hh);
handle = hh; handle = hh;
return SetMemError(0); SetMemError(0);
} return hp{handle, mcptr};
uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle)
{
uint32_t ptr;
return NewHandle(size, clear, handle, ptr);
} }
tool_return<void> DisposeHandle(uint32_t handle)
uint16_t DisposeHandle(uint32_t handle)
{ {
auto iter = HandleMap.find(handle); auto iter = HandleMap.find(handle);
@ -349,19 +380,9 @@ namespace MM
} }
uint16_t GetHandleSize(uint32_t handle, uint32_t &handleSize)
{
handleSize = 0;
const auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
handleSize = iter->second.size;
return SetMemError(0);
}
uint16_t ReallocHandle(uint32_t handle, uint32_t logicalSize) tool_return<void> ReallocHandle(uint32_t handle, uint32_t logicalSize)
{ {
auto iter = HandleMap.find(handle); auto iter = HandleMap.find(handle);
@ -400,12 +421,12 @@ namespace MM
// lock? clear purged flag? // lock? clear purged flag?
return 0; return MacOS::noErr;
} }
uint16_t SetHandleSize(uint32_t handle, uint32_t newSize) tool_return<void> SetHandleSize(uint32_t handle, uint32_t newSize)
{ {
if (handle == 0) return SetMemError(MacOS::nilHandleErr); if (handle == 0) return SetMemError(MacOS::nilHandleErr);
@ -519,61 +540,35 @@ namespace MM
} }
// template class to validate handle and work on it.
template<class FX> tool_return<void> HSetRBit(uint32_t handle)
uint16_t HandleIt(uint32_t handle, FX fx)
{ {
const auto iter = HandleMap.find(handle); return __with_handle(handle, [](HandleInfo &info) { info.resource = true; });
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
auto &info = iter->second;
fx(info);
return SetMemError(0);
} }
uint16_t HSetRBit(uint32_t handle) tool_return<void> HClrRBit(uint32_t handle)
{ {
const auto iter = HandleMap.find(handle); return __with_handle(handle, [](HandleInfo &info) { info.resource = false; });
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
auto &info = iter->second;
info.resource = true;
return SetMemError(0);
} }
uint16_t HClrRBit(uint32_t handle) tool_return<void> HLock(uint32_t handle)
{ {
const auto iter = HandleMap.find(handle); return __with_handle(handle, [](HandleInfo &info) { info.locked = true; });
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
auto &info = iter->second;
info.resource = false;
return SetMemError(0);
} }
uint16_t HLock(uint32_t handle) tool_return<void> HUnlock(uint32_t handle)
{ {
const auto iter = HandleMap.find(handle); return __with_handle(handle, [](HandleInfo &info) { info.locked = false; });
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
auto &info = iter->second;
info.locked = true;
return SetMemError(0);
} }
uint16_t HUnlock(uint32_t handle) tool_return<uint32_t> GetHandleSize(uint32_t handle)
{ {
const auto iter = HandleMap.find(handle); return __with_handle(handle, [](const HandleInfo &info){ return info.size; });
}
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); tool_return<HandleInfo> GetHandleInfo(uint32_t handle)
{
auto &info = iter->second; return __with_handle(handle, [](const HandleInfo &info){ return info; });
info.locked = false;
return SetMemError(0);
} }
@ -583,25 +578,7 @@ namespace MM
#pragma mark -- #pragma mark --
tool_return<uint32_t> GetHandleSize(uint32_t handle)
{
const auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
SetMemError(0);
return iter->second.size;
}
tool_return<HandleInfo> GetHandleInfo(uint32_t handle)
{
const auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
SetMemError(0);
return iter->second;
}
#pragma mark -- #pragma mark --
@ -828,12 +805,10 @@ namespace MM
// todo -- separate pools for sys vs non-sys? // todo -- separate pools for sys vs non-sys?
// todo -- NewPtr(0) -- null or empty ptr? // todo -- NewPtr(0) -- null or empty ptr?
uint32_t mcptr; auto rv = Native::NewPtr(size, clear);
uint16_t error;
error = Native::NewPtr(size, clear, mcptr);
cpuSetAReg(0, mcptr); cpuSetAReg(0, rv.value());
return error; //SetMemError(error); return rv.error(); //SetMemError(error);
} }
uint16_t DisposePtr(uint16_t trap) uint16_t DisposePtr(uint16_t trap)
@ -852,10 +827,9 @@ namespace MM
Log("%04x DisposePtr(%08x)\n", trap, mcptr); Log("%04x DisposePtr(%08x)\n", trap, mcptr);
uint16_t error; auto rv = Native::DisposePtr(mcptr);
error = Native::DisposePtr(mcptr);
return error; //SetMemError(error); return rv.error(); //SetMemError(error);
} }
uint32_t GetPtrSize(uint16_t trap) uint32_t GetPtrSize(uint16_t trap)
@ -928,8 +902,6 @@ namespace MM
* *
*/ */
uint32_t hh = 0;
uint16_t error;
bool clear = trap & (1 << 9); bool clear = trap & (1 << 9);
//bool sys = trap & (1 << 10); //bool sys = trap & (1 << 10);
@ -938,10 +910,10 @@ namespace MM
Log("%04x NewHandle(%08x)\n", trap, size); Log("%04x NewHandle(%08x)\n", trap, size);
error = Native::NewHandle(size, clear, hh); auto rv = Native::NewHandle(size, clear);
cpuSetAReg(0, hh); cpuSetAReg(0, rv.value().handle);
return error; return rv.error();
} }
uint16_t DisposeHandle(uint16_t trap) uint16_t DisposeHandle(uint16_t trap)
@ -959,7 +931,7 @@ namespace MM
Log("%04x DisposeHandle(%08x)\n", trap, hh); Log("%04x DisposeHandle(%08x)\n", trap, hh);
return Native::DisposeHandle(hh); return Native::DisposeHandle(hh).error();
} }
uint16_t EmptyHandle(uint16_t trap) uint16_t EmptyHandle(uint16_t trap)
@ -1026,7 +998,7 @@ namespace MM
Log("%04x ReallocHandle(%08x, %08x)\n", trap, hh, logicalSize); Log("%04x ReallocHandle(%08x, %08x)\n", trap, hh, logicalSize);
return Native::ReallocHandle(hh, logicalSize); return Native::ReallocHandle(hh, logicalSize).error();
#if 0 #if 0
auto iter = HandleMap.find(hh); auto iter = HandleMap.find(hh);
@ -1121,7 +1093,7 @@ namespace MM
Log("%04x SetHandleSize(%08x, %08x)\n", trap, hh, newSize); Log("%04x SetHandleSize(%08x, %08x)\n", trap, hh, newSize);
return Native::SetHandleSize(hh, newSize); return Native::SetHandleSize(hh, newSize).error();
} }
uint32_t RecoverHandle(uint16_t trap) uint32_t RecoverHandle(uint16_t trap)
@ -1368,16 +1340,15 @@ namespace MM
auto const info = iter->second; auto const info = iter->second;
uint32_t destHandle; auto rv = Native::NewHandle(info.size, false);
uint32_t destPtr;
uint32_t d0 = Native::NewHandle(info.size, false, destHandle, destPtr); if (!rv.error())
if (d0 == 0)
{ {
std::memmove(memoryPointer(destPtr), memoryPointer(info.address), info.size); std::memmove(memoryPointer(rv.value().pointer), memoryPointer(info.address), info.size);
} }
cpuSetAReg(0, destHandle); cpuSetAReg(0, rv.value().handle);
return d0; // SetMemError called by Native::NewHandle. return rv.error(); // SetMemError called by Native::NewHandle.
} }
@ -1399,16 +1370,14 @@ namespace MM
Log("%04x PtrToHand(%08x, %08x)\n", trap, mcptr, size); Log("%04x PtrToHand(%08x, %08x)\n", trap, mcptr, size);
uint32_t destHandle; auto rv = Native::NewHandle(size, false);
uint32_t destPtr; if (!rv.error())
uint32_t d0 = Native::NewHandle(size, false, destHandle, destPtr);
if (d0 == 0)
{ {
std::memmove(memoryPointer(destPtr), memoryPointer(mcptr), size); std::memmove(memoryPointer(rv.value().pointer), memoryPointer(mcptr), size);
} }
cpuSetAReg(0, destHandle); cpuSetAReg(0, rv.value().handle);
return d0; // SetMemError called by Native::NewHandle. return rv.error(); // SetMemError called by Native::NewHandle.
} }
uint16_t PtrAndHand(uint16_t trap) uint16_t PtrAndHand(uint16_t trap)
@ -1435,18 +1404,16 @@ namespace MM
cpuSetAReg(0, handle); cpuSetAReg(0, handle);
uint32_t oldSize = 0; auto tmp = Native::GetHandleSize(handle);
uint32_t d0; if (tmp.error()) return tmp.error();
uint32_t oldSize = tmp.value();
d0 = Native::GetHandleSize(handle, oldSize);
if (d0) return d0;
if ((uint64_t)oldSize + (uint64_t)size > UINT32_MAX) if ((uint64_t)oldSize + (uint64_t)size > UINT32_MAX)
return SetMemError(MacOS::memFullErr); return SetMemError(MacOS::memFullErr);
d0 = Native::SetHandleSize(handle, oldSize + size); auto err = Native::SetHandleSize(handle, oldSize + size);
if (d0) return d0; if (err.error()) return err.error();
auto iter = HandleMap.find(handle); auto iter = HandleMap.find(handle);
if (iter == HandleMap.end()) if (iter == HandleMap.end())
@ -1637,20 +1604,18 @@ namespace MM
{ {
// FUNCTION TempNewHandle (logicalSize: Size; // FUNCTION TempNewHandle (logicalSize: Size;
// VAR resultCode: OSErr): Handle; // VAR resultCode: OSErr): Handle;
uint16_t rv;
uint32_t logicalSize; uint32_t logicalSize;
uint32_t resultCode; uint32_t resultCode;
uint32_t theHandle;
uint32_t sp = StackFrame<8>(logicalSize, resultCode); uint32_t sp = StackFrame<8>(logicalSize, resultCode);
Log(" TempNewHandle(%08x, %08x)\n", logicalSize, resultCode); Log(" TempNewHandle(%08x, %08x)\n", logicalSize, resultCode);
rv = Native::NewHandle(logicalSize, true, theHandle); auto rv = Native::NewHandle(logicalSize, true);
if (resultCode) memoryWriteWord(rv, resultCode); if (resultCode) memoryWriteWord(rv.error(), resultCode);
ToolReturn<4>(sp, theHandle); ToolReturn<4>(sp, rv.value().handle);
return rv; return rv.error();
} }
uint16_t TempHLock(void) uint16_t TempHLock(void)
@ -1663,10 +1628,10 @@ namespace MM
Log(" TempHLock(%08x, %08x)\n", theHandle, resultCode); Log(" TempHLock(%08x, %08x)\n", theHandle, resultCode);
uint16_t rv = Native::HLock(theHandle); auto rv = Native::HLock(theHandle);
if (resultCode) memoryWriteWord(rv, resultCode); if (resultCode) memoryWriteWord(rv.error(), resultCode);
return rv; return rv.error();
} }
uint16_t TempHUnlock(void) uint16_t TempHUnlock(void)
@ -1679,10 +1644,10 @@ namespace MM
Log(" TempHUnlock(%08x, %08x)\n", theHandle, resultCode); Log(" TempHUnlock(%08x, %08x)\n", theHandle, resultCode);
uint16_t rv = Native::HUnlock(theHandle); auto rv = Native::HUnlock(theHandle);
if (resultCode) memoryWriteWord(rv, resultCode); if (resultCode) memoryWriteWord(rv.error(), resultCode);
return rv; return rv.error();
} }
@ -1696,9 +1661,9 @@ namespace MM
Log(" TempDisposeHandle(%08x, %08x)\n", theHandle, resultCode); Log(" TempDisposeHandle(%08x, %08x)\n", theHandle, resultCode);
uint16_t rv = Native::DisposeHandle(theHandle); auto rv = Native::DisposeHandle(theHandle);
if (resultCode) memoryWriteWord(rv, resultCode); if (resultCode) memoryWriteWord(rv.error(), resultCode);
return rv; return rv.error();
} }
} }

View File

@ -5,37 +5,9 @@
#include <macos/tool_return.h> #include <macos/tool_return.h>
namespace MM namespace MM
{ {
// native functions.
namespace Native
{
void MemoryInfo(uint32_t address);
void PrintMemoryStats();
uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle);
uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle, uint32_t &ptr);
uint16_t NewPtr(uint32_t size, bool clear, uint32_t &pointer);
uint16_t DisposeHandle(uint32_t handle);
uint16_t DisposePtr(uint32_t pointer);
uint16_t GetHandleSize(uint32_t handle, uint32_t &handleSize);
uint16_t SetHandleSize(uint32_t handle, uint32_t newSize);
uint16_t ReallocHandle(uint32_t handle, uint32_t newSize);
uint16_t HSetRBit(uint32_t handle);
uint16_t HClrRBit(uint32_t handle);
uint16_t HLock(uint32_t handle);
uint16_t HUnlock(uint32_t handle);
}
bool Init(uint8_t *memory, uint32_t memorySize, uint32_t globals, uint32_t stack);
struct HandleInfo struct HandleInfo
{ {
@ -50,11 +22,54 @@ namespace MM
{} {}
}; };
// native functions.
namespace Native
{
using MacOS::tool_return; using MacOS::tool_return;
struct hp {
uint32_t handle = 0;
uint32_t pointer = 0;
hp() = default;
hp(uint32_t a, uint32_t b) : handle(a), pointer(b) {};
};
void MemoryInfo(uint32_t address);
void PrintMemoryStats();
tool_return<hp> NewHandle(uint32_t size, bool clear);
tool_return<uint32_t> NewPtr(uint32_t size, bool clear);
tool_return<void> DisposeHandle(uint32_t handle);
tool_return<void> DisposePtr(uint32_t pointer);
tool_return<uint32_t> GetHandleSize(uint32_t handle);
tool_return<void> SetHandleSize(uint32_t handle, uint32_t newSize);
tool_return<void> ReallocHandle(uint32_t handle, uint32_t newSize);
tool_return<void> HSetRBit(uint32_t handle);
tool_return<void> HClrRBit(uint32_t handle);
tool_return<void> HLock(uint32_t handle);
tool_return<void> HUnlock(uint32_t handle);
tool_return<HandleInfo> GetHandleInfo(uint32_t handle); tool_return<HandleInfo> GetHandleInfo(uint32_t handle);
tool_return<uint32_t> GetHandleSize(uint32_t handle); tool_return<uint32_t> GetHandleSize(uint32_t handle);
}
bool Init(uint8_t *memory, uint32_t memorySize, uint32_t globals, uint32_t stack);
using MacOS::tool_return;
uint16_t BlockMove(uint16_t trap); uint16_t BlockMove(uint16_t trap);

View File

@ -112,10 +112,10 @@ namespace
std::map<uint32_t, Handle> rhandle_map; std::map<uint32_t, Handle> rhandle_map;
inline uint16_t SetResError(uint16_t error) inline macos_error SetResError(uint16_t error)
{ {
memoryWriteWord(error, MacOS::ResErr); memoryWriteWord(error, MacOS::ResErr);
return error; return (macos_error)error;
} }
bool LoadResType(uint32_t type) bool LoadResType(uint32_t type)
@ -165,10 +165,9 @@ namespace RM
// not to be confused with MacOS LoadResource(theHandle) // not to be confused with MacOS LoadResource(theHandle)
template<class FX> template<class FX>
uint16_t LoadResource(uint32_t type, uint32_t &theHandle, FX fx) macos_error LoadResource(uint32_t type, uint32_t &theHandle, FX fx)
{ {
uint32_t ptr; uint32_t ptr;
uint16_t error;
Handle nativeHandle; Handle nativeHandle;
uint32_t size; uint32_t size;
@ -190,14 +189,18 @@ namespace RM
if (ResLoad) ::LoadResource(nativeHandle); if (ResLoad) ::LoadResource(nativeHandle);
size = ::GetHandleSize(nativeHandle); size = ::GetHandleSize(nativeHandle);
error = MM::Native::NewHandle(size, false, theHandle, ptr); {
auto tmp = MM::Native::NewHandle(size, false);
if (tmp.error()) {
::ReleaseResource(nativeHandle);
return SetResError(tmp.error());
}
theHandle = tmp.value().handle;
ptr = tmp.value().pointer;
}
// TODO -- need to lock if native handle locked. // TODO -- need to lock if native handle locked.
if (!theHandle)
{
::ReleaseResource(nativeHandle);
return SetResError(error);
}
MM::Native::HSetRBit(theHandle); MM::Native::HSetRBit(theHandle);
if (size) if (size)
@ -213,15 +216,19 @@ namespace RM
// used by GetString (utility.h) // used by GetString (utility.h)
// used by Loader. // used by Loader.
uint16_t GetResource(uint32_t type, uint16_t id, uint32_t &theHandle) tool_return<uint32_t> GetResource(uint32_t type, uint16_t id)
{ {
return LoadResource(type, theHandle, uint32_t theHandle;
auto err = LoadResource(type, theHandle,
[type, id](){ [type, id](){
return ::GetResource(type, id); return ::GetResource(type, id);
}); });
if (!err) return theHandle;
return err;
} }
uint16_t SetResLoad(bool load) tool_return<void> SetResLoad(bool load)
{ {
ResLoad = load; ResLoad = load;
@ -232,6 +239,19 @@ namespace RM
} }
tool_return<void> CloseResFile(uint16_t refNum)
{
// If the value of the refNum parameter is 0, it represents the System file and is ignored.
if (refNum != 0)
{
::CloseResFile(refNum);
return SetResError(::ResError());
}
return SetResError(0);
}
} }
uint16_t CloseResFile(uint16_t trap) uint16_t CloseResFile(uint16_t trap)
@ -844,7 +864,7 @@ namespace RM
// check if handle size changed, resync data // check if handle size changed, resync data
auto info = MM::GetHandleInfo(theResource); auto info = MM::Native::GetHandleInfo(theResource);
if (!info.error()) return SetResError(info.error()); if (!info.error()) return SetResError(info.error());
@ -928,7 +948,7 @@ namespace RM
Handle nativeHandle = NULL; Handle nativeHandle = NULL;
auto info = MM::GetHandleInfo(theData); auto info = MM::Native::GetHandleInfo(theData);
if (info.error()) return SetResError(MacOS::addResFailed); if (info.error()) return SetResError(MacOS::addResFailed);
@ -1179,7 +1199,7 @@ namespace RM
// if it has a size, it's loaded... // if it has a size, it's loaded...
auto info = MM::GetHandleInfo(theResource); auto info = MM::Native::GetHandleInfo(theResource);
if (info.error()) return SetResError(resNotFound); if (info.error()) return SetResError(resNotFound);
if (info->size) return SetResError(0); if (info->size) return SetResError(0);
@ -1198,14 +1218,14 @@ namespace RM
size = ::GetHandleSize(nativeHandle); size = ::GetHandleSize(nativeHandle);
err = MM::Native::ReallocHandle(theResource, size); auto tmp = MM::Native::ReallocHandle(theResource, size);
if (err) return SetResError(err); if (tmp.error()) return SetResError(tmp.error());
// todo -- need to lock if resource locked. // todo -- need to lock if resource locked.
if (size) if (size)
{ {
info = MM::GetHandleInfo(theResource); info = MM::Native::GetHandleInfo(theResource);
std::memcpy(memoryPointer(info->address), *(void **)nativeHandle, size); std::memcpy(memoryPointer(info->address), *(void **)nativeHandle, size);
} }

View File

@ -3,13 +3,19 @@
#include <cstdint> #include <cstdint>
#include <macos/tool_return.h>
namespace RM namespace RM
{ {
namespace Native namespace Native
{ {
uint16_t SetResLoad(bool tf); using MacOS::tool_return;
uint16_t GetResource(uint32_t type, uint16_t id, uint32_t &theHandle);
tool_return<void> SetResLoad(bool tf);
tool_return<uint32_t> GetResource(uint32_t type, uint16_t id);
tool_return<void> OpenResFile(const std::string &path_name, uint16_t permissions = 0);
tool_return<void> CloseResFile(uint16_t refNum);
} }
uint16_t CloseResFile(uint16_t trap); uint16_t CloseResFile(uint16_t trap);

View File

@ -49,10 +49,8 @@ namespace Utility {
*/ */
uint32_t theString; uint32_t theString;
uint32_t theHandle;
uint32_t sp; uint32_t sp;
uint32_t length = 0; uint32_t length = 0;
uint32_t d0;
std::string s; std::string s;
@ -64,19 +62,12 @@ namespace Utility {
length = s.length() + 1; length = s.length() + 1;
// if (length) // always true...
{
uint32_t ptr;
d0 = MM::Native::NewHandle(s.length(), false, theHandle, ptr);
if (!d0) auto rv = MM::Native::NewHandle(s.length(), false);
{ if (!rv.error()) ToolBox::WritePString(rv.value().pointer, s);
ToolBox::WritePString(ptr, s);
}
}
ToolReturn<4>(sp, theHandle); ToolReturn<4>(sp, rv.value().handle);
return d0; return rv.error();
} }
// FUNCTION GetString (stringID: Integer): StringHandle; // FUNCTION GetString (stringID: Integer): StringHandle;
@ -91,17 +82,15 @@ namespace Utility {
uint16_t stringID; uint16_t stringID;
uint32_t sp; uint32_t sp;
uint32_t d0;
uint32_t theHandle;
sp = StackFrame<2>(stringID); sp = StackFrame<2>(stringID);
Log("%04x GetString($%04x)\n", trap, stringID); Log("%04x GetString($%04x)\n", trap, stringID);
d0 = RM::Native::GetResource(0x53545220, stringID, theHandle); auto rv = RM::Native::GetResource(0x53545220, stringID);
ToolReturn<4>(sp, theHandle); ToolReturn<4>(sp, rv.value());
return d0; return rv.error();
} }
// FUNCTION BitTst (bytePtr: Ptr; bitNum: LONGINT) : BOOLEAN; // FUNCTION BitTst (bytePtr: Ptr; bitNum: LONGINT) : BOOLEAN;