From 84ff99c177493893c04c77306c9d7f4a23fa064a Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Fri, 13 Feb 2015 16:11:55 -0500 Subject: [PATCH] Squashed commit of the following: commit f4807cd071ba5c4ce9ba4aa8c906ba335da07a2d Author: Kelvin Sherlock Date: Fri Feb 13 16:09:55 2015 -0500 merge in missing tool calls commit 6830ab51ce7c48e3d0d1352f04df8df914146b3e Merge: 1675a47 24c17a9 Author: Kelvin Sherlock Date: Fri Feb 13 16:06:05 2015 -0500 Merge branch 'feature_trap_address_2015' into merge_dispatcher Conflicts: bin/loader.cpp toolbox/rm.cpp toolbox/toolbox.cpp commit 24c17a9a888b2a95e99761ee7ee8b15874a9ce42 Author: Kelvin Sherlock Date: Fri Feb 13 13:22:07 2015 -0500 SetOSTrapAddress stub commit b255937e6c9a6765a9763b011bd99c3f675d997c Author: Kelvin Sherlock Date: Sat Jan 24 14:40:45 2015 -0500 Use the $Commands environment variable as a list of directories where commands may be located. Conflicts: bin/loader.cpp commit a3925747cef0203d178091cdd827c50ded134484 Author: Kelvin Sherlock Date: Fri Jan 23 10:26:52 2015 -0500 HWPriv OS Trap commit 16d0038fd1b9b6db3e154ab5e810624ab7d3ca08 Author: Kelvin Sherlock Date: Fri Jan 23 10:26:40 2015 -0500 HWPriv OS Trap commit bf92f806326925b68b497127da421f833e2cdba2 Author: Kelvin Sherlock Date: Wed Jan 21 17:34:30 2015 -0500 RM::GetNamedResource commit 540490d0c5ba7c8310271b1c7fd68259a77a2f28 Author: Kelvin Sherlock Date: Wed Jan 21 14:31:03 2015 -0500 GetNamedResource trap commit 09aa8d0f2600668b37a51e7872db9a9f9c9ea4ef Author: Kelvin Sherlock Date: Tue Jan 20 19:32:04 2015 -0500 Set ApplZone global (ARMCFront) commit 45fa54abca52a80c980c870f43f1f82225d7c43d Author: Kelvin Sherlock Date: Tue Jan 20 13:58:24 2015 -0500 update test makefile commit b1e6635630d2fc112b613312000e091ff7f297bc Author: Kelvin Sherlock Date: Tue Jan 20 13:35:07 2015 -0500 SysEnvirons ($a090) OS Trap commit a5126544b8bdda155a5f8e9395d547a7bf3eae94 Author: Kelvin Sherlock Date: Mon Jan 19 15:02:29 2015 -0500 undo HGetVolume cwd, for now commit 912c8748254a83d3a5eacf776a6b6b3a4439aa75 Author: Kelvin Sherlock Date: Mon Jan 19 10:55:58 2015 -0500 Fix SetPtrSize commit c0fe74c1b81d1782a61240a18ecc4e51a3ecb88c Author: Kelvin Sherlock Date: Sun Jan 18 17:31:09 2015 -0500 support the auto-pop bit for tool calls (MPW Pascal IIgs 1.0b1) commit 2e9ab5200c285bb97bc15a11dcc7e7127e779245 Author: Kelvin Sherlock Date: Sun Jan 18 15:20:38 2015 -0500 update dispatch code... commit c7c548e5ac2096e29aebf532e0eb69e2227bdecd Author: Kelvin Sherlock Date: Sun Jan 18 14:48:22 2015 -0500 add back new tool calls. commit cbb554174ef186b8b74383596212d9b8f04ebfea Merge: 44d19f7 5e616d3 Author: Kelvin Sherlock Date: Sun Jan 18 14:43:58 2015 -0500 Merge branch 'feature_trap_address' into feature_trap_address_2015 Conflicts: toolbox/CMakeLists.txt toolbox/os.cpp toolbox/toolbox.cpp commit 5e616d353b1421b96cc4e3a4f82d4422911d1243 Author: Kelvin Sherlock Date: Sun Dec 14 18:01:38 2014 -0500 new dispatcher. commit ea5f2139217df8aebe975b8e4680080d58a772bd Author: Kelvin Sherlock Date: Mon Dec 8 11:42:23 2014 -0500 get ready for trap overrides. commit 17c5b40ac866a7adaa559bc9164ffe32e926cc96 Author: Kelvin Sherlock Date: Fri Dec 5 14:39:17 2014 -0500 remove dead code commit fc7df738cc9dd72d65ba656df71dd6aa124ce8af Author: Kelvin Sherlock Date: Fri Dec 5 14:32:18 2014 -0500 display trap name for GetToolTrap, etc. --- bin/loader.cpp | 2 +- toolbox/CMakeLists.txt | 1 + toolbox/dispatch.cpp | 1035 ++++++++++++++++++++++++++++++++++++++++ toolbox/os.cpp | 2 + toolbox/stackframe.h | 39 ++ toolbox/toolbox.cpp | 90 +--- toolbox/toolbox.h | 1 + 7 files changed, 1087 insertions(+), 83 deletions(-) create mode 100644 toolbox/dispatch.cpp diff --git a/bin/loader.cpp b/bin/loader.cpp index 0e8d870..ba28274 100644 --- a/bin/loader.cpp +++ b/bin/loader.cpp @@ -687,7 +687,6 @@ std::string find_exe(const std::string &name) return ""; } - // otherwise, check the Commands variable for locations. std::string commands = MPW::GetEnv("Commands"); if (commands.empty()) return old_find_exe(name); @@ -938,6 +937,7 @@ int main(int argc, char **argv) MM::Init(Memory, MemorySize, kGlobalSize, Flags.stackSize); OS::Init(); + ToolBox::Init(); MPW::Init(argc, argv); diff --git a/toolbox/CMakeLists.txt b/toolbox/CMakeLists.txt index bbeb198..9f5f447 100644 --- a/toolbox/CMakeLists.txt +++ b/toolbox/CMakeLists.txt @@ -27,6 +27,7 @@ set(TOOLBOX_SRC utility.cpp fs_spec.cpp realpath.c + dispatch.cpp ) diff --git a/toolbox/dispatch.cpp b/toolbox/dispatch.cpp new file mode 100644 index 0000000..74ad55a --- /dev/null +++ b/toolbox/dispatch.cpp @@ -0,0 +1,1035 @@ +/* + * Copyright (c) 2014, Kelvin W Sherlock + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + + +#include +#include +#include +#include +#include + +#include + + +#include +#include +#include + +#include "loader.h" +#include "mm.h" +#include "os.h" +#include "packages.h" +#include "qd.h" +#include "rm.h" +#include "sane.h" +#include "stackframe.h" +#include "toolbox.h" +#include "utility.h" + +#include +#include +#include + +// yuck. TST.W d0 +extern "C" void cpuSetFlagsNZ00NewW(UWO res); + + +namespace { + + uint32_t trap_address[1024]; + uint32_t os_address[256]; + + uint32_t ToolGlue; + uint32_t OSGlue; + + + inline constexpr bool is_tool_trap(uint16_t trap) + { + // %1010 | x . . . | . . . . . . . . | + // + // x = 1 if this is a toolbox call, x = 0 if this is an os call. + + return (trap & 0x0800) == 0x0800; + } + + inline constexpr bool auto_pop(uint16_t trap) + { + // %1010 | 1 x . . | . . . . . . . . | + // + // if x = 1, an extra rts is pushed, + return (trap & 0x0c00) == 0x0c00; + } + + inline constexpr bool save_a0(uint16_t trap) + { + // %1010 | 0 . . x | . . . . . . . . | + // + // if x = 0, a0 is saved and restored. + // if x = 1, a0 is not saved and restored. + return (trap & 0x0900) == 0x0000; + } + + inline constexpr unsigned os_flags(uint16_t trap) + { + // %1010 | 0 x x . | . . . . . . . . | + return trap & 0x0600; + } + +} + +namespace OS { + + using ToolBox::Log; + +#pragma mark - Trap Manager + + uint16_t GetToolTrapAddress(uint16_t trap) + { + /* + * on entry: + * D0 trap number + * + * on exit: + * A0 Address of patch + * + */ + uint16_t trapNumber = cpuGetDReg(0); + const char *trapName = TrapName(trapNumber | 0xa800); + if (!trapName) trapName = "Unknown"; + + Log("%04x GetToolTrapAddress($%04x (%s))\n", trap, trapNumber, trapName); + + trapNumber &= 0x03ff; + + if (trap_address[trapNumber]) + { + cpuSetAReg(0, trap_address[trapNumber]); + return 0; + } + + cpuSetAReg(0, ToolGlue + trapNumber * 4); + return 0; //MacOS::dsCoreErr; + } + + uint16_t SetToolTrapAddress(uint16_t trap) + { + //pascal void SetToolTrapAddress(long trapAddr, short trapNum); + + /* + * on entry: + * A0 Address of patch + * D0 trap number + * + * on exit: + * + */ + + // this is used by the far model stub, presumably + // to replace LoadSeg. + + uint16_t trapNumber = cpuGetDReg(0); + uint32_t trapAddress = cpuGetAReg(0); + const char *trapName = TrapName(trapNumber | 0xa800); + if (!trapName) trapName = "Unknown"; + + Log("%04x SetToolTrapAddress($%08x, $%04x (%s))\n", + trap, trapAddress, trapNumber, trapName); + + + trapNumber &= 0x03ff; + trap_address[trapNumber] = trapAddress; + + return 0; + } + + uint16_t GetOSTrapAddress(uint16_t trap) + { + /* + * on entry: + * D0 trap number + * + * on exit: + * A0 Address of patch + * + */ + uint16_t trapNumber = cpuGetDReg(0); + const char *trapName = TrapName(trapNumber | 0xa000); + if (!trapName) trapName = "Unknown"; + + Log("%04x GetOSTrapAddress($%04x (%s))\n", trap, trapNumber, trapName); + + trapNumber &= 0x00ff; + + if (os_address[trapNumber]) + { + cpuSetAReg(0, os_address[trapNumber]); + return 0; + } + + cpuSetAReg(0, OSGlue + trapNumber * 4); + return 0; // MacOS::dsCoreErr; + } + + uint16_t SetOSTrapAddress(uint16_t trap) + { + //pascal void SetOSTrapAddress(long trapAddr, short trapNum); + + /* + * on entry: + * A0 Address of patch + * D0 trap number + * + * on exit: + * + */ + + // Used by 3.2, 3.3 Link to patch + // MaxBlock (bug with CloseResFile). + + uint16_t trapNumber = cpuGetDReg(0); + uint32_t trapAddress = cpuGetAReg(0); + const char *trapName = TrapName(trapNumber | 0xa000); + if (!trapName) trapName = "Unknown"; + + Log("%04x SetToolTrapAddress($%08x, $%04x (%s))\n", + trap, trapAddress, trapNumber, trapName); + + + trapNumber &= 0x00ff; + os_address[trapNumber] = trapAddress; + + return 0; + } + + + uint16_t OSDispatch(uint16_t trap) + { + uint16_t selector; + + + StackFrame<2>(selector); + Log("%04x OSDispatch(%04x)\n", trap, selector); + + switch(selector) + { + case 0x0015: + return MM::TempMaxMem(); + + case 0x0018: + return MM::TempFreeMem(); + + case 0x001d: + return MM::TempNewHandle(); + + case 0x001e: + return MM::TempHLock(); + + case 0x001f: + return MM::TempHUnlock(); + + case 0x0020: + return MM::TempDisposeHandle(); + + + default: + fprintf(stderr, "OSDispatch: selector %04x not implemented\n", + selector); + exit(1); + } + + } + +} + +namespace ToolBox { + + bool Init() { + // + std::fill(std::begin(trap_address), std::end(trap_address), 0); + std::fill(std::begin(os_address), std::end(os_address), 0); + + + // alternate entry code + MM::Native::NewPtr(1024 * 4 + 256 * 4, false, ToolGlue); + OSGlue = ToolGlue + 1024 * 4; + + uint16_t *code = (uint16_t *)memoryPointer(ToolGlue); + + for (unsigned i = 0; i < 1024; ++i) { + *code++ = 0xafff; + *code++ = 0xa800 | i; + } + + for (unsigned i = 0; i < 256; ++i) { + *code++ = 0xafff; + *code++ = 0xa000 | i; + } + + // os return code... pull registers, TST.W d0, etc. + return true; + } + + void native_dispatch(uint16_t trap); + void dispatch(uint16_t trap) + { + + uint32_t returnPC = 0; + + bool saveA0 = false; + uint32_t a0 = cpuGetAReg(0); + + + // hold off on actually _calling_ for now... + #if 0 + + if (trap == 0xafff) + { + // called from the trap address stub. + + // uint16_t 0xafff + // uint16_t trap + // return address, etc, is on stack. + + + uint32_t pc = cpuGetPC(); + trap = memoryReadWord(pc); + + if (is_tool_trap(trap)) + { + StackFrame<4>(returnPC); + } + + #if 0 + + /* + * os stubs: + * os_return_a0 + * pop registers a0, a1, d0, d1, d2 + * pop long word + * tst.w d0 + * rts + * + * os_return: + * pop registers a0, a1, d0, d1, d2 + * pop long word + * tst.w d0 + * rts + * + * os_entry_a0: + * + */ + else + { + // os calls push the trap dispatcher pc as well. + uint32_t tmp; + uint32_t a0, a1, d0, d1, d2; + if (save_a0(trap)) + { + StackFrame<4*8>(returnPC, tmp, a0, a1, d0, d1, d2, tmp); + } + else + { + StackFrame<4*7>(returnPC, tmp, a1, d0, d1, d2, tmp); + } + trap = d1; // trap w/ flag bits. + } + #endif + + native_dispatch(trap); + cpuInitializeFromNewPC(returnPC); + return; + } + + #endif + + /* + * The auto-pop bit is bit 10 in an A-line instruction for a + * Toolbox routine. Some language systems prefer to generate + * jump-subroutine calls (JSR) to intermediate routines, called + * glue routines, which then call Toolbox routines instead of + * executing the Toolbox routine directly. This glue method + * would normally interfere with Toolbox traps because the + * return address of the glue subroutine is placed on the stack + * between the Toolbox routine's parameters and the address of + * the place where the glue routine was called from (where + * control returns once the Toolbox routine has completed + * execution). + * + * The auto-pop bit forces the trap dispatcher to remove the + * top 4 bytes from the stack before dispatching to the Toolbox + * routine. After the Toolbox routine completes execution, + * control is transferred back to the place where the glue + * routine was called from, not back to the glue routine. + * + * Most development environments, including MPW, do not use + * this feature. + * + * (Trap Manager, 8-20) + */ + + if (auto_pop(trap)) + { + /* + * | args | + * | returnPC | + * ------------- + */ + + StackFrame<4>(returnPC); + trap &= ~0x0400; + } + + + #if 0 + if (is_tool_trap(trap)) + { + uint16_t tt = trap & 0x03ff; + uint32_t address = trap_address[tt]; + if (address) + { + /* + * parameters were pushed on the stack but + * the a-line instruction was intercepted + * before an exception occurred. Therefore, + * we need to push the return address on the + * stack and set the new pc to the trap address. + * + * returnPC may have been previously set from the + * auto pop bit. + */ + + Push<4>(returnPC == 0 ? cpuGetPC() : returnPC); + Log("$04x *%s - $%08x", trap, TrapName(trap), address); + cpuInitializeFromNewPC(address); + return; + } + } + else + { + // os calls - check the return/save a0 bit. + // if bit 8 is 0, a0 is saved and restored. + // if bit 8 is 1, a0 is modified. + // Trap Manager 8-12. + + // not actually an issue yet, will only matter + // if os address is overridden. + + if (save_a0(trap)) { + saveA0 = true; + } + uint16_t tt = trap & 0x00ff; + + uint32_t address = os_address[tt]; + + if (address) { + assert("OS trap overrides are not yet supported."); + } + } + #endif + + native_dispatch(trap); + + if (saveA0) cpuSetAReg(0, a0); + if (returnPC) cpuInitializeFromNewPC(returnPC); + } + + void native_dispatch(uint16_t trap) + { + + uint32_t d0 = 0; + switch (trap) + { + case 0xa000: // Open + case 0xa200: // HOpen + d0 = OS::Open(trap); + break; + + case 0xa00a: // OpenRF + case 0xa20a: // HOpenRF + d0 = OS::OpenRF(trap); + break; + + + case 0xa001: + d0 = OS::Close(trap); + break; + + case 0xa002: + d0 = OS::Read(trap); + break; + + case 0xa003: + d0 = OS::Write(trap); + break; + + case 0xa207: + d0 = OS::HGetVInfo(trap); + break; + + case 0xa008: // Create + case 0xa208: // HCreate + d0 = OS::Create(trap); + break; + + case 0xa009: // Delete + case 0xa209: // HDelete + d0 = OS::Delete(trap); + break; + + case 0xa00c: // GetFInfo + case 0xa20c: // HGetFInfo + d0 = OS::GetFileInfo(trap); + break; + + + case 0xa00d: // SetFileInfo + case 0xa20d: // HSetFileInfo + d0 = OS::SetFileInfo(trap); + break; + + case 0xa011: + d0 = OS::GetEOF(trap); + break; + + case 0xa012: + d0 = OS::SetEOF(trap); + break; + case 0xa013: + d0 = OS::FlushVol(trap); + break; + + case 0xa014: + d0 = OS::GetVol(trap); + break; + + case 0xa214: + d0 = OS::HGetVol(trap); + break; + + + case 0xa015: // SetVol + d0 = OS::SetVol(trap); + break; + + case 0xa215: // HSetVol + d0 = OS::HSetVol(trap); + break; + + + case 0xa018: + d0 = OS::GetFPos(trap); + break; + + case 0xa044: + d0 = OS::SetFPos(trap); + break; + + case 0xa051: + d0 = OS::ReadXPRam(trap); + break; + + case 0xa060: + d0 = OS::FSDispatch(trap); + break; + + case 0xa260: + d0 = OS::HFSDispatch(trap); + break; + + case 0xaa52: + d0 = OS::HighLevelHFSDispatch(trap); + break; + + + case 0xA746: + d0 = OS::GetToolTrapAddress(trap); + break; + + case 0xa647: + d0 = OS::SetToolTrapAddress(trap); + break; + + case 0xA346: + d0 = OS::GetOSTrapAddress(trap); + break; + + case 0xa247: + d0 = OS::SetOSTrapAddress(trap); + break; + + case 0xA823: + d0 = OS::AliasDispatch(trap); + break; + + case 0xA1AD: + d0 = OS::Gestalt(trap); + break; + + case 0xa090: + d0 = OS::SysEnvirons(trap); + break; + + // SetPtrSize (p: Ptr; newSize: Size); + case 0xa020: + d0 = MM::SetPtrSize(trap); + break; + + // GetPtrSize (p: Ptr) : Size + case 0xa021: + d0 = MM::GetPtrSize(trap); + break; + + case 0xA023: + d0 = MM::DisposeHandle(trap); + break; + + case 0xa024: + d0 = MM::SetHandleSize(trap); + break; + + case 0xa025: + d0 = MM::GetHandleSize(trap); + break; + + case 0xa029: + d0 = MM::HLock(trap); + break; + + case 0xa02a: + d0 = MM::HUnlock(trap); + break; + + // BlockMove (sourcePtr,destPtr: Ptr; byteCount: Size); + case 0xa02e: // BlockMove + case 0xa22e: // BlockMoveData + d0 = MM::BlockMove(trap); + break; + + case 0xa049: + d0 = MM::HPurge(trap); + break; + + case 0xa04a: + d0 = MM::HNoPurge(trap); + break; + + + case 0xA11D: + d0 = MM::MaxMem(trap); + break; + + case 0xA01C: + d0 = MM::FreeMem(trap); + break; + + // CompactMem (cbNeeded: Size) : Size; + case 0xa04c: + d0 = MM::CompactMem(trap); + break; + + case 0xa040: + d0 = MM::ReserveMem(trap); + break; + + case 0xa055: + d0 = MM::StripAddress(trap); + break; + + case 0xa061: + d0 = MM::MaxBlock(trap); + break; + + case 0xa069: + d0 = MM::HGetState(trap); + break; + + case 0xa06a: + d0 = MM::HSetState(trap); + break; + + + // MoveHHi (h: Handle); + case 0xa064: + d0 = MM::MoveHHi(trap); + break; + + case 0xa9e1: + d0 = MM::HandToHand(trap); + break; + + case 0xa9e3: + d0 = MM::PtrToHand(trap); + break; + case 0xa9ef: + d0 = MM::PtrAndHand(trap); + break; + + case 0xa11a: + d0 = MM::GetZone(trap); + break; + + case 0xa01b: + d0 = MM::SetZone(trap); + break; + + case 0xa126: + d0 = MM::HandleZone(trap); + break; + + case 0xa128: + d0 = MM::RecoverHandle(trap); + break; + + // MaxApplZone + case 0xa063: + d0 = MM::MaxApplZone(trap); + break; + + case 0xa162: + d0 = MM::PurgeSpace(trap); + break; + + // ReadDateTime (VAR sees: LONGINT) : OSErr; + case 0xa039: + d0 = OS::ReadDateTime(trap); + break; + + // SecondsToDate (s: LongInt; VAR d: DateTimeRec); + case 0xa9c6: + d0 = OS::SecondsToDate(trap); + break; + + // TickCount : LONGINT; + case 0xa975: + d0 = OS::TickCount(trap); + break; + + case 0xa193: + d0 = OS::Microseconds(trap); + break; + + + case 0xa9ed: + d0 = Packages::Pack6(trap); + break; + + //_CmpString [MARKS,CASE] + case 0xa03c: + case 0xa23c: + case 0xa43c: + case 0xa63c: + d0 = OS::CmpString(trap); + break; + + // NewPtr [Sys, Clear] (logicalSize: Size): Ptr; + case 0xa11e: + case 0xa31e: + case 0xa51e: + case 0xa71e: + // clear = bit 9, sys = bit 10 + d0 = MM::NewPtr(trap); + break; + + // DisposPtr (p: Ptr); + case 0xa01f: + d0 = MM::DisposePtr(trap); + break; + + case 0xa065: + d0 = MM::StackSpace(trap); + break; + + // NewHandle (logicalSize: Size) : Handle; + case 0xA122: + case 0xa322: + d0 = MM::NewHandle(trap); + break; + + case 0xA027: + d0 = MM::ReallocHandle(trap); + break; + + case 0xA02B: + d0 = MM::EmptyHandle(trap); + break; + + + case 0xa058: + d0 = OS::InsTime(trap); + break; + + case 0xa059: + d0 = OS::RmvTime(trap); + break; + + case 0xa05a: + d0 = OS::PrimeTime(trap); + break; + + + case 0xA0BD: + d0 = OS::FlushCodeCache(trap); + break; + + case 0xA098: + case 0xA198: // don't save a0 + d0 = OS::HWPriv(trap); + break; + + // resource manager stuff. + + // Count1Resources (theType: ResType): Integer; + case 0xa80d: + d0 = RM::Count1Resources(trap); + break; + + case 0xa80e: + d0 = RM::Get1IndResource(trap); + break; + + case 0xa80f: + d0 = RM::Get1IndType(trap); + break; + + case 0xa81a: + d0 = RM::HOpenResFile(trap); + break; + + case 0xa81b: + d0 = RM::HCreateResFile(trap); + break; + + case 0xa81c: + d0 = RM::Count1Types(trap); + break; + + // Get1Resource (theType: ResType; thelD: INTEGER) : Handle; + case 0xa81f: + d0 = RM::Get1Resource(trap); + break; + + // Get1NamedResource (theType: ResType; name: Str255) : Handle; + case 0xa820: + d0 = RM::Get1NamedResource(trap); + break; + + + case 0xa992: + d0 = RM::DetachResource(trap); + break; + + case 0xa994: + d0 = RM::CurResFile(trap); + break; + + case 0xA997: + d0 = RM::OpenResFile(trap); + break; + + case 0xA998: + d0 = RM::UseResFile(trap); + break; + + case 0xa999: + d0 = RM::UpdateResFile(trap); + break; + + case 0xa99a: + d0 = RM::CloseResFile(trap); + break; + + case 0xa99b: + d0 = RM::SetResLoad(trap); + break; + + // GetResource (theType: ResType; thelD: INTEGER) : Handle; + case 0xa9a0: + d0 = RM::GetResource(trap); + break; + + case 0xa9a1: + d0 = RM::GetNamedResource(trap); + break; + + case 0xa9a2: + d0 = RM::LoadResource(trap); + break; + + // ReleaseResource (theResource: Handle); + case 0xa9a3: + d0 = RM::ReleaseResource(trap); + break; + + case 0xa9a4: + d0 = RM::HomeResFile(trap); + break; + + case 0xa9a5: + d0 = RM::GetResourceSizeOnDisk(trap); + break; + + case 0xa9a6: + d0 = RM::GetResAttrs(trap); + break; + + case 0xa9a7: + d0 = RM::SetResAttrs(trap); + break; + + case 0xa9a8: + d0 = RM::GetResInfo(trap); + break; + + case 0xa9ab: + d0 = RM::AddResource(trap); + break; + + case 0xa9aa: + d0 = RM::ChangedResource(trap); + break; + + case 0xa9ad: + d0 = RM::RemoveResource(trap); + break; + + // ResError : INTEGER; + case 0xa9af: + d0 = RM::ResError(trap); + break; + + case 0xa9b0: + d0 = RM::WriteResource(trap); + break; + + // CreateResFile(fileName: Str255); + case 0xa9b1: + d0 = RM::CreateResFile(trap); + break; + + // OpenRFPerm (fileName: Str255; vRefNum: Integer; + // permission: SignedByte): Integer; + case 0xa9c4: + d0 = RM::OpenRFPerm(trap); + break; + + case 0xa9f6: + d0 = RM::GetResFileAttrs(trap); + break; + + case 0xa9f7: + d0 = RM::SetResFileAttrs(trap); + break; + + + // UnloadSeg (routineAddr: Ptr); + case 0xa9f1: + d0 = Loader::UnloadSeg(trap); + break; + + + // quickdraw (AsmIIgs ... ) + + // _ShowCursor(); + case 0xA853: + d0 = QD::ShowCursor(trap); + break; + + // GetCursor (cursorlD: INTEGER) : CursHandle; + case 0xA9B9: + d0 = QD::GetCursor(trap); + break; + + + case 0xa834: + d0 = QD::SetFScaleDisable(trap); + break; + + //SetCursor (crsr: Cursor) + case 0xA851: + d0 = QD::SetCursor(trap); + break; + + case 0xa86e: + d0 = QD::InitGraf(trap); + break; + + // GetFNum (fontName: Str255; VAR theNum: INTEGER); + case 0xa900: + d0 = QD::GetFNum(trap); + break; + + + // sane + case 0xa9Eb: + d0 = SANE::fp68k(trap); + cpuSetDReg(0, d0); + return; // SANE sets the flags. + break; + + case 0x0a9ee: + d0 = SANE::decstr68k(trap); + break; + + // utility + + case 0xa906: + d0 = Utility::NewString(trap); + break; + case 0xa9ba: + d0 = Utility::GetString(trap); + break; + + case 0xa85d: + d0 = Utility::BitTst(trap); + break; + + case 0xa88f: + d0 = OS::OSDispatch(trap); + break; + + default: + fprintf(stderr, "Unsupported tool trap: %04x (%s)\n", + trap, TrapName(trap)); + fprintf(stderr, "pc: %08x\n", cpuGetPC()); + exit(255); + } + + // n.b. - os calls return via d0 and tst.w d0. + // toolcalls return any error info as the return + // value on the stack. + if (d0) + { + int16_t v = (int16_t)d0; + Log(" -> %d\n", v); + } + + + cpuSetDReg(0, d0); + cpuSetFlagsNZ00NewW(d0); + + } + +} \ No newline at end of file diff --git a/toolbox/os.cpp b/toolbox/os.cpp index 0284c61..db64e89 100644 --- a/toolbox/os.cpp +++ b/toolbox/os.cpp @@ -958,6 +958,7 @@ namespace OS #pragma mark - Trap Manager +#ifdef OLD_TRAP_DISPATCH uint16_t GetToolTrapAddress(uint16_t trap) { /* @@ -1050,6 +1051,7 @@ namespace OS return MacOS::dsCoreErr; } +#endif #pragma mark XP - RAM diff --git a/toolbox/stackframe.h b/toolbox/stackframe.h index 590d39a..a84ad51 100644 --- a/toolbox/stackframe.h +++ b/toolbox/stackframe.h @@ -6,6 +6,45 @@ // + +template +void Push__(uint32_t sp) +{ + static_assert(Bytes == 0, "Invalid Stack Size"); +} + +template +void Push__(uint32_t sp, uint16_t value, Args&&... args); + +template +void Push__(uint32_t sp, uint32_t value, Args&&... args); + + +template +void Push__(uint32_t sp, uint16_t value, Args&&... args) +{ + memoryWriteWord(value, sp); + Push__(sp + 2, std::forward(args)...); +} + +template +void Push__(uint32_t sp, uint32_t value, Args&&... args) +{ + memoryWriteLong(value, sp); + Push__(sp + 4, std::forward(args)...); +} + +template +void Push(Args&&... args) +{ + uint32_t sp = cpuGetAReg(7) - Bytes; + cpuSetAReg(7, sp); + + Push__(sp, std::forward(args)...); + +} + + template void ToolReturn(uint32_t sp, uint32_t value) { diff --git a/toolbox/toolbox.cpp b/toolbox/toolbox.cpp index 3a810fb..42aa3af 100644 --- a/toolbox/toolbox.cpp +++ b/toolbox/toolbox.cpp @@ -26,8 +26,11 @@ #include #include +#include + #include #include +#include #include #include @@ -54,10 +57,13 @@ // yuck. TST.W d0 extern "C" void cpuSetFlagsNZ00NewW(UWO res); + + namespace ToolBox { bool Trace = false; +#ifdef OLD_TRAP_DISPATCH uint16_t OSDispatch(uint16_t trap) { @@ -103,7 +109,6 @@ namespace ToolBox { void dispatch(uint16_t trap) { - // todo -- check/remove extra bits for save a0, toolglue, etc. uint32_t d0 = 0; switch (trap) @@ -672,7 +677,9 @@ namespace ToolBox { cpuSetDReg(0, d0); cpuSetFlagsNZ00NewW(d0); + } +#endif std::string ReadCString(uint32_t address, bool fname) { @@ -749,86 +756,5 @@ namespace ToolBox { return std::string(tmp); } - /* - * MacOS basically does the absolute/relative paths backwards vs unix. - * - * file -> file - * :dir -> dir - * :dir:file -> dir/file - * volume:file -> /volume/file - */ - #if 0 - std::string UnixToMac(const std::string &path) - { - // ./..., //... etc. - std::string tmp; - int sep; - - if (path.empty()) return path; - - sep = path.find_first_of('/'); - - if (sep == path.npos) - { - // no sep -- relative file. treat as-is - return path; - } - if (sep == 0) - { - // absolute path -- drop the leading / - // '/' doesn't make sense. - tmp = path.substr(1); - } - else - { - // relative path. - tmp = '/'; - tmp.append(path); - } - - std::replace(tmp.begin(), tmp.end(), '/', ':'); - - return tmp; - } - - - std::string MacToUnix(const std::string &path) - { - std::string tmp; - int sep; - int slash; - - if (path.empty()) return path; - - sep = path.find_first_of(':'); - slash = path.find_first_of('/'); - - // if there's a / prior to the :, assume it's a unix prefix. - if (sep == path.npos) return path; - - if (slash == path.npos || slash > sep) - { - if (sep == 0) - { - // :directory... -> directory - tmp = path.substr(1); - } - else - { - // volume:path... - tmp = '/'; - tmp.append(path); - } - } - else // assume a unix prefix. - { - tmp = path; - } - - std::replace(tmp.begin(), tmp.end(), ':', '/'); - - return tmp; - } - #endif } diff --git a/toolbox/toolbox.h b/toolbox/toolbox.h index 3f03cea..10adb06 100644 --- a/toolbox/toolbox.h +++ b/toolbox/toolbox.h @@ -22,6 +22,7 @@ namespace ToolBox } + bool Init(); void dispatch(uint16_t trap);