diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a70280..a47c2f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,11 @@ set(CMAKE_C_COMPILER "clang") set(CMAKE_CXX_COMPILER "clang++") set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -g") +set(CMAKE_C_FLAGS " -Wall -g") project(mpw) cmake_minimum_required(VERSION 2.6) add_subdirectory(bin) -add_subdirectory(cpu) \ No newline at end of file +add_subdirectory(cpu) +add_subdirectory(toolbox) \ No newline at end of file diff --git a/bin/CMakeLists.txt b/bin/CMakeLists.txt index db4ff85..0f65c45 100644 --- a/bin/CMakeLists.txt +++ b/bin/CMakeLists.txt @@ -1,6 +1,6 @@ #set(CMAKE_C_COMPILER "clang") #set(CMAKE_CXX_COMPILER "clang++") -set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -g -Wno-deprecated-declarations ") +set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -Wno-deprecated-declarations -g") SET(CMAKE_EXE_LINKER_FLAGS "-framework Carbon") add_definitions(-I ${CMAKE_SOURCE_DIR}/) @@ -8,3 +8,4 @@ add_definitions(-I ${CMAKE_SOURCE_DIR}/) add_executable(mpw loader.cpp) target_link_libraries(mpw CPU_LIB) +target_link_libraries(mpw TOOLBOX_LIB) diff --git a/toolbox/CMakeLists.txt b/toolbox/CMakeLists.txt new file mode 100644 index 0000000..0db46cf --- /dev/null +++ b/toolbox/CMakeLists.txt @@ -0,0 +1,14 @@ +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -Wno-deprecated-declarations -g") + +add_definitions(-I ${CMAKE_SOURCE_DIR}/) + +set(TOOLBOX_SRC + dispatch.cpp + rm.cpp +) + + + +add_library(TOOLBOX_LIB ${TOOLBOX_SRC}) \ No newline at end of file diff --git a/toolbox/dispatch.cpp b/toolbox/dispatch.cpp new file mode 100644 index 0000000..edef35d --- /dev/null +++ b/toolbox/dispatch.cpp @@ -0,0 +1,37 @@ +#include +#include + +#include +#include +#include + +#include "toolbox.h" + +// yuck. TST.W d0 +extern "C" void cpuSetFlagsNZ00NewW(UWO res); + + + +void toolbox(uint16_t trap) +{ + // todo -- remove extra bits for save a0, toolglue + + uint32_t d0 = 0; + switch (trap) + { + + _Get1NamedResource + // Get1NamedResource (theType: ResType; name: Str255) : Handle; + case 0xa820: + d0 = RM::Get1NamedResource(); + break; + + default: + fprintf(stderr, "Unsupported tool trap: %08x\n", trap); + fprintf(stderr, "pc: %08x\n", cpuGetPC()); + exit(255); + } + + cpuSetDReg(0, d0); + cpuSetFlagsNZ00NewW(d0); +} \ No newline at end of file diff --git a/toolbox/rm.cpp b/toolbox/rm.cpp new file mode 100644 index 0000000..138372b --- /dev/null +++ b/toolbox/rm.cpp @@ -0,0 +1,90 @@ +#include "rm.h" + +#include +#include +#include + +#include + +uint32_t PopLong() +{ + uint32_t sp = cpuGetAReg(7); + uint32_t value = memoryReadLong(a7); + cpuSetAReg(7, sp + 4); + return value; +} + + +uint16_t PopWord() +{ + uint32_t sp = cpuGetAReg(7); + uint16_t value = memoryReadWord(a7); + cpuSetAReg(7, sp + 2); + return value; +} + +uint32_t StackFrame(uint32_t &b, uint32_t &a) +{ + uint32_t sp = cpuGetAReg(7); + a = memoryReadLong(sp); sp += 4; + b = memoryReadLong(sp); sp += 4; + cpuSetAReg(7, sp); +} + +void ToolReturn(uint32_t sp, uint32_t value) +{ + memoryWriteLong(sp, value); +} + +std::string PString(uint32_t address) +{ + std::string s; + + unsigned length = address == 0 ? 0 : memoryReadByte(address++); + if (length == 0) + { + return s; + } + + s.reserve(length); + for (unsigned i = 0; i < length; ++i) + { + s.push_back((char)memoryReadByte(address++)); + } + + return s; +} + +namespace RM +{ + uint16_t Get1NamedResource() + { + // Get1NamedResource (theType: ResType; name: Str255) : Handle; + + /* + * ----------- + * +8 outHandle + * ------------ + * +4 theType + * ------------ + * +0 name + * ------------ + * + */ + + // nb - return address is not on the stack. + + uint32_t sp; + uint32_t theType; + uint32_t name; + + sp = StackFrame(theType, name); + + std::string sname = PString(name); + + printf(stderr, "Get1NamedResource(%08x, %s)\n", theType, sname.c_str()); + + ToolReturn(sp, (uint32_t)0); + return –192; + } +} diff --git a/toolbox/rm.h b/toolbox/rm.h new file mode 100644 index 0000000..7c996d7 --- /dev/null +++ b/toolbox/rm.h @@ -0,0 +1,11 @@ +#ifndef __mpw_rm_h__ +#define __mpw_rm_h__ + +#include + +namespace RM +{ + uint16_t Get1NamedResource(); +} + +#endif \ No newline at end of file diff --git a/toolbox/toolbox.h b/toolbox/toolbox.h new file mode 100644 index 0000000..d9736c7 --- /dev/null +++ b/toolbox/toolbox.h @@ -0,0 +1,6 @@ +#ifndef __mpw_toolbox_h__ +#define __mpw_toolbox_h__ + +extern "C" void toolbox(uint16_t trap); + +#endif \ No newline at end of file