From 223500af6321568eded433643b667ebc31bef924 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Sun, 10 Feb 2013 20:18:47 -0500 Subject: [PATCH] mpw api --- CMakeLists.txt | 3 +- mpw/CMakeLists.txt | 11 ++++ mpw/mpw.cpp | 140 +++++++++++++++++++++++++++++++++++++++++++++ mpw/mpw.h | 32 +++++++++++ 4 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 mpw/CMakeLists.txt create mode 100644 mpw/mpw.cpp create mode 100644 mpw/mpw.h diff --git a/CMakeLists.txt b/CMakeLists.txt index cb7a1e6..ae1ba71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,4 +9,5 @@ cmake_minimum_required(VERSION 2.6) add_subdirectory(bin) add_subdirectory(cpu) add_subdirectory(toolbox) -add_subdirectory(mplite) \ No newline at end of file +add_subdirectory(mplite) +add_subdirectory(mpw) diff --git a/mpw/CMakeLists.txt b/mpw/CMakeLists.txt new file mode 100644 index 0000000..49d4efb --- /dev/null +++ b/mpw/CMakeLists.txt @@ -0,0 +1,11 @@ +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wall -g") + +add_definitions(-I ${CMAKE_SOURCE_DIR}/) + +set(MPW_SRC mpw.cpp) + + + +add_library(MPW_LIB ${MPW_SRC}) \ No newline at end of file diff --git a/mpw/mpw.cpp b/mpw/mpw.cpp new file mode 100644 index 0000000..95d5034 --- /dev/null +++ b/mpw/mpw.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + + + +#include "mpw.h" + + +namespace { + + using namespace MPW; + + void ftrap_access(uint16_t trap) + { + // should never hit... only used for opening a file + fprintf(stderr, "%04x Access()\n", trap); + } + + void ftrap_close(uint16_t trap) + { + // this can be nopped since it's stdin/stdout/stderr. + + fprintf(stderr, "%04x Close()\n", trap); + cpuSetDReg(0, 0); + } + + void ftrap_read(uint16_t trap) + { + fprintf(stderr, "%04x Read()\n", trap); + } + + void ftrap_write(uint16_t trap) + { + uint32_t sp = cpuGetAReg(7); + uint32_t parm = memoryReadLong(sp + 4); + + MPWFile f; + + f.flags = memoryReadWord(parm); + f.error = memoryReadWord(parm + 2); + f.device = memoryReadLong(parm + 4); + f.cookie = memoryReadLong(parm + 8); + f.count = memoryReadLong(parm + 12); + f.buffer = memoryReadLong(parm + 16); + + fprintf(stderr, "%04x Write(%08x)\n", trap, parm); + + // hmmm how to handle crlf? + + + if (f.count) + { + std::unique_ptr buffer(new uint8_t[f.count]); + uint8_t *ptr = memoryPointer(f.buffer); + + + std::transform(ptr, ptr + f.count, buffer.get(), + [](uint8_t c) { return c == '\r' ? '\n' : c; } + ); + + int fd = f.cookie; + + ::write(fd, buffer.get(), f.count); + + // hmm, also needs to update the count actually transferred? + + } + cpuSetDReg(0, 0); + } + + void ftrap_ioctl(uint16_t trap) + { + // int ioctl(int fildes, unsigned int cmd, long *arg); + + uint32_t sp = cpuGetAReg(7); + + uint32_t fd = memoryReadLong(sp + 4); + uint32_t cmd = memoryReadLong(sp + 8); + uint32_t arg = memoryReadLong(sp + 12); + + fprintf(stderr, "%04x IOCtl(%08x, %08x, %08x)\n", trap, fd, cmd, arg); + + cpuSetDReg(0, 0); + } + + void ftrap_quit(uint16_t trap) + { + fprintf(stderr, "%04x Quit()\n", trap); + cpuSetStop(true); + } + +} + +namespace MPW +{ + + void dispatch(uint16_t trap) + { + switch (trap) + { + case fQuit: + ftrap_quit(trap); + break; + case fAccess: + ftrap_access(trap); + break; + + case fClose: + ftrap_close(trap); + break; + case fRead: + ftrap_read(trap); + break; + case fWrite: + ftrap_write(trap); + break; + case fIOCtl: + ftrap_ioctl(trap); + break; + + default: + fprintf(stderr, "Unsupported f trap: %04x\n", trap); + exit(1); + break; + } + + } + +} diff --git a/mpw/mpw.h b/mpw/mpw.h new file mode 100644 index 0000000..4f21f5f --- /dev/null +++ b/mpw/mpw.h @@ -0,0 +1,32 @@ +#ifndef __mpw_mpw_h__ +#define __mpw_mpw_h__ + +#include + +namespace MPW { + + + struct MPWFile + { + uint16_t flags; + uint16_t error; + uint32_t device; + uint32_t cookie; + uint32_t count; + uint32_t buffer; + }; + + enum { + fQuit = 0xf000, + fAccess, + fClose, + fRead, + fWrite, + fIOCtl, + }; + + void dispatch(uint16_t trap); + +} + +#endif \ No newline at end of file