mpw/toolbox/toolbox.cpp

452 lines
7.5 KiB
C++
Raw Normal View History

2013-02-08 00:21:47 +00:00
#include <cstdio>
#include <cstdint>
#include <stdlib.h>
2013-02-14 03:16:40 +00:00
#include <string>
2013-02-08 00:21:47 +00:00
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include "toolbox.h"
#include "rm.h"
2013-02-08 03:49:04 +00:00
#include "mm.h"
2013-02-12 22:32:31 +00:00
#include "os.h"
2013-02-15 04:08:38 +00:00
#include "qd.h"
2013-02-21 04:28:13 +00:00
#include "sane.h"
2013-03-27 03:29:08 +00:00
#include "utility.h"
2013-02-13 03:35:15 +00:00
2013-02-08 00:21:47 +00:00
// yuck. TST.W d0
extern "C" void cpuSetFlagsNZ00NewW(UWO res);
namespace ToolBox {
2013-02-16 23:51:28 +00:00
bool Trace = false;
2013-02-13 03:35:15 +00:00
2013-02-08 00:21:47 +00:00
void dispatch(uint16_t trap)
{
// todo -- check/remove extra bits for save a0, toolglue, etc.
uint32_t d0 = 0;
switch (trap)
{
2013-03-03 01:54:43 +00:00
case 0xa000: // open
case 0xa00a: // openrf
2013-02-25 04:22:31 +00:00
d0 = OS::Open(trap);
break;
2013-02-25 22:34:25 +00:00
case 0xa001:
d0 = OS::Close(trap);
break;
2013-02-25 04:22:31 +00:00
case 0xa002:
d0 = OS::Read(trap);
break;
2013-03-04 03:07:25 +00:00
case 0xa003:
d0 = OS::Write(trap);
break;
2013-02-14 03:16:40 +00:00
case 0xa008:
d0 = OS::Create(trap);
break;
2013-02-14 03:38:34 +00:00
case 0xa009:
d0 = OS::Delete(trap);
break;
2013-02-14 04:07:33 +00:00
2013-02-12 22:32:31 +00:00
case 0xA00C:
d0 = OS::GetFileInfo(trap);
break;
2013-02-16 21:11:10 +00:00
2013-02-13 03:35:15 +00:00
case 0xa00d:
d0 = OS::SetFileInfo(trap);
break;
2013-02-08 00:21:47 +00:00
2013-02-16 21:11:10 +00:00
case 0xa011:
d0 = OS::GetEOF(trap);
break;
2013-03-04 03:07:25 +00:00
case 0xa012:
d0 = OS::SetEOF(trap);
break;
2013-03-27 04:00:47 +00:00
case 0xa013:
d0 = OS::FlushVol(trap);
break;
2013-02-16 21:11:10 +00:00
2013-02-14 03:16:40 +00:00
case 0xa014:
d0 = OS::GetVol(trap);
break;
2013-03-04 03:07:25 +00:00
case 0xa044:
d0 = OS::SetFPos(trap);
2013-03-02 22:39:54 +00:00
break;
2013-03-04 03:07:25 +00:00
case 0xa060:
// also a260 for async
d0 = OS::HFSDispatch(trap);
break;
2013-03-02 22:39:54 +00:00
2013-02-28 23:05:39 +00:00
case 0xaa52:
d0 = OS::HighLevelHFSDispatch(trap);
break;
2013-02-16 01:33:00 +00:00
// SetPtrSize (p: Ptr; newSize: Size);
case 0xa020:
d0 = MM::SetPtrSize(trap);
break;
2013-02-16 20:37:55 +00:00
// GetPtrSize (p: Ptr) : Size
case 0xa021:
d0 = MM::GetPtrSize(trap);
break;
2013-02-15 04:08:56 +00:00
case 0xA023:
d0 = MM::DisposeHandle(trap);
break;
2013-02-26 23:31:43 +00:00
case 0xa024:
d0 = MM::SetHandleSize(trap);
break;
2013-02-25 04:20:38 +00:00
case 0xa025:
d0 = MM::GetHandleSize(trap);
break;
2013-02-16 20:37:55 +00:00
case 0xa029:
d0 = MM::HLock(trap);
break;
case 0xa02a:
d0 = MM::HUnlock(trap);
break;
2013-02-08 04:44:58 +00:00
// BlockMove (sourcePtr,destPtr: Ptr; byteCount: Size);
case 0xa02e:
d0 = MM::BlockMove(trap);
break;
2013-02-25 04:20:38 +00:00
case 0xa049:
d0 = MM::HPurge(trap);
break;
2013-02-15 05:08:22 +00:00
// CompactMem (cbNeeded: Size) : Size;
case 0xa04c:
d0 = MM::CompactMem(trap);
break;
2013-02-25 22:33:26 +00:00
case 0xa055:
d0 = MM::StripAddress(trap);
break;
2013-02-19 23:28:29 +00:00
// MoveHHi (h: Handle);
case 0xa064:
d0 = MM::MoveHHi(trap);
break;
2013-02-14 03:16:40 +00:00
2013-02-26 23:32:39 +00:00
case 0xa9e3:
d0 = MM::PtrToHand(trap);
2013-02-14 03:16:40 +00:00
// ReadDateTime (VAR sees: LONGINT) : OSErr;
case 0xa039:
d0 = OS::ReadDateTime(trap);
2013-02-14 03:16:40 +00:00
break;
// SecondsToDate (s: LongInt; VAR d: DateTimeRec);
case 0xa9c6:
d0 = OS::SecondsToDate(trap);
2013-02-14 03:16:40 +00:00
break;
2013-02-11 01:19:35 +00:00
2013-02-16 01:41:57 +00:00
// TickCount : LONGINT;
case 0xa975:
d0 = OS::TickCount(trap);
2013-02-16 01:41:57 +00:00
break;
2013-02-25 04:21:48 +00:00
case 0xa9ed:
d0 = OS::Pack6(trap);
break;
//_CmpString [MARKS,CASE]
case 0xa03c:
case 0xa23c:
case 0xa43c:
case 0xa63c:
d0 = OS::CmpString(trap);
break;
2013-02-08 03:12:30 +00:00
// NewPtr [Sys, Clear] (logicalSize: Size): Ptr;
case 0xa11e:
case 0xa31e:
case 0xa51e:
case 0xa71e:
// clear = bit 9, sys = bit 10
2013-02-08 03:49:04 +00:00
d0 = MM::NewPtr(trap);
2013-02-08 03:12:30 +00:00
break;
2013-02-11 01:19:35 +00:00
// DisposPtr (p: Ptr);
case 0xa01f:
d0 = MM::DisposePtr(trap);
break;
2013-02-08 03:12:30 +00:00
2013-02-15 04:08:56 +00:00
// NewHandle (logicalSize: Size) : Handle;
case 0xA122:
case 0xa322:
d0 = MM::NewHandle(trap);
break;
2013-03-04 02:54:53 +00:00
case 0xA027:
d0 = MM::ReallocHandle(trap);
break;
case 0xA02B:
d0 = MM::EmptyHandle(trap);
break;
2013-02-14 04:07:33 +00:00
// resource manager stuff.
2013-02-19 23:28:58 +00:00
// Get1Resource (theType: ResType; thelD: INTEGER) : Handle;
case 0xa81f:
d0 = RM::Get1Resource(trap);
break;
2013-02-08 00:21:47 +00:00
// Get1NamedResource (theType: ResType; name: Str255) : Handle;
case 0xa820:
2013-02-08 03:12:30 +00:00
d0 = RM::Get1NamedResource(trap);
2013-02-08 00:21:47 +00:00
break;
2013-03-03 01:53:54 +00:00
case 0xa994:
d0 = RM::CurResFile(trap);
break;
2013-02-26 23:33:21 +00:00
case 0xA997:
d0 = RM::OpenResFile(trap);
break;
2013-03-04 02:55:33 +00:00
case 0xA998:
d0 = RM::UseResFile(trap);
break;
2013-02-26 23:33:21 +00:00
case 0xa99a:
d0 = RM::CloseResFile(trap);
break;
case 0xa99b:
d0 = RM::SetResLoad(trap);
break;
2013-02-14 04:07:33 +00:00
// GetResource (theType: ResType; thelD: INTEGER) : Handle;
case 0xa9a0:
d0 = RM::GetResource(trap);
break;
2013-02-19 23:28:58 +00:00
// ReleaseResource (theResource: Handle);
case 0xa9a3:
d0 = RM::ReleaseResource(trap);
break;
2013-02-26 23:33:21 +00:00
// ResError : INTEGER;
case 0xa9af:
d0 = RM::ResError(trap);
break;
2013-02-16 01:42:20 +00:00
// UnloadSeg (routineAddr: Ptr);
case 0xa9f1:
d0 = RM::UnloadSeg(trap);
break;
2013-02-15 04:08:38 +00:00
// quickdraw (AsmIIgs ... )
2013-02-14 04:07:33 +00:00
// _ShowCursor();
case 0xA853:
2013-02-15 04:08:38 +00:00
d0 = QD::ShowCursor(trap);
break;
// GetCursor (cursorlD: INTEGER) : CursHandle;
case 0xA9B9:
d0 = QD::GetCursor(trap);
break;
//SetCursor (crsr: Cursor)
case 0xA851:
d0 = QD::SetCursor(trap);
break;
// GetFNum (fontName: Str255; VAR theNum: INTEGER);
case 0xa900:
d0 = QD::GetFNum(trap);
2013-02-14 04:07:33 +00:00
break;
2013-02-21 04:28:13 +00:00
// sane
case 0xa9Eb:
d0 = SANE::fp68k(trap);
break;
2013-02-25 22:35:01 +00:00
case 0x0a9ee:
d0 = SANE::decstr68k(trap);
break;
2013-03-27 03:29:08 +00:00
// utility
case 0xa906:
d0 = Utility::NewString(trap);
break;
2013-03-27 03:54:28 +00:00
case 0xa9ba:
d0 = Utility::GetString(trap);
break;
2013-03-27 04:00:47 +00:00
2013-02-08 00:21:47 +00:00
default:
fprintf(stderr, "Unsupported tool trap: %04x\n", trap);
fprintf(stderr, "pc: %08x\n", cpuGetPC());
exit(255);
}
2013-03-02 22:39:54 +00:00
if (d0)
{
int16_t v = (int16_t)d0;
Log(" -> %d\n", v);
}
2013-02-08 00:21:47 +00:00
cpuSetDReg(0, d0);
cpuSetFlagsNZ00NewW(d0);
}
std::string ReadCString(uint32_t address, bool fname)
2013-02-13 03:35:15 +00:00
{
std::string tmp;
if (address)
{
tmp.assign((char *)memoryPointer(address));
}
2013-03-07 00:17:55 +00:00
if (fname) tmp = MacToUnix(tmp);
2013-02-13 03:35:15 +00:00
return tmp;
}
std::string ReadPString(uint32_t address, bool fname)
2013-02-13 03:35:15 +00:00
{
std::string tmp;
if (address)
{
unsigned length = memoryReadByte(address);
tmp.assign((char *)memoryPointer(address + 1), length);
2013-03-07 00:17:55 +00:00
if (fname) tmp = MacToUnix(tmp);
2013-02-13 03:35:15 +00:00
}
return tmp;
}
std::string ReadString(uint32_t address, uint32_t length)
{
std::string tmp;
if (address)
{
tmp.assign((char *)memoryPointer(address), length);
}
return tmp;
}
2013-02-14 03:16:05 +00:00
bool WritePString(uint32_t address, const std::string &s)
{
int length = s.length();
if (length > 255) return false;
if (address == 0) return false;
uint8_t *ptr = memoryPointer(address);
*ptr++ = (uint8_t)length;
for (char c : s)
{
*ptr++ = (uint8_t)c;
}
return true;
}
2013-03-07 00:17:55 +00:00
/*
* MacOS basically does the absolute/relative paths backwards vs unix.
*
* file -> file
* :dir -> dir
* :dir:file -> dir/file
* volume:file -> /volume/file
*/
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;
2013-02-13 03:35:15 +00:00
2013-03-07 00:17:55 +00:00
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;
}
2013-02-08 00:21:47 +00:00
}