mpw/toolbox/os.cpp

1107 lines
22 KiB
C++
Raw Normal View History

2013-07-30 05:06:19 +00:00
/*
* Copyright (c) 2013, 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:
2013-07-30 05:06:19 +00:00
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
2013-07-30 05:06:19 +00:00
* 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.
2013-07-30 05:06:19 +00:00
*
* 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.
*
*/
2013-02-13 03:35:15 +00:00
#include <cerrno>
2013-02-16 18:32:47 +00:00
#include <cctype>
2013-02-25 04:21:48 +00:00
#include <ctime>
#include <algorithm>
#include <chrono>
2013-02-25 04:23:05 +00:00
#include <deque>
#include <string>
2013-02-12 22:32:31 +00:00
#include <sys/stat.h>
#include <stdlib.h>
2013-02-14 03:16:26 +00:00
#include <unistd.h>
#include <fcntl.h>
2013-02-12 22:32:31 +00:00
2013-02-16 18:32:47 +00:00
#include <strings.h>
2016-11-11 17:01:06 +00:00
#include <filesystem>
namespace fs = std::experimental::filesystem;
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
2013-05-13 02:36:22 +00:00
#include <macos/sysequ.h>
#include <macos/errors.h>
#include <macos/traps.h>
2013-05-13 02:36:22 +00:00
2016-11-04 16:31:22 +00:00
#include <native/native.h>
#include <native/file.h>
#include "os.h"
2013-02-26 01:41:35 +00:00
#include "os_internal.h"
#include "toolbox.h"
#include "stackframe.h"
#include "fs_spec.h"
2013-02-16 23:51:28 +00:00
using ToolBox::Log;
2013-03-03 01:55:47 +00:00
2014-12-21 00:32:54 +00:00
using MacOS::macos_error_from_errno;
2013-02-16 23:51:28 +00:00
2013-02-13 03:35:15 +00:00
namespace {
using namespace OS;
// time stuff.
const long EpochAdjust = 86400 * (365 * (1970 - 1904) + 17); // 17 leap years.
std::chrono::time_point<std::chrono::steady_clock> BootTime;
#if 0
2013-02-16 18:32:47 +00:00
std::string extension(const std::string &s)
{
std::string tmp;
int pos;
pos = s.find_last_of("./:");
if (pos == s.npos) return tmp;
2013-02-16 18:32:47 +00:00
if (s[pos++] != '.') return tmp;
if (pos >= s.length()) return tmp;
tmp = s.substr(pos);
std::transform(tmp.begin(), tmp.end(), tmp.begin(),
2013-02-16 18:32:47 +00:00
[](char c) { return tolower(c); }
);
return tmp;
2013-02-18 01:44:01 +00:00
}
std::string basename(const std::string &s)
{
int pos = s.find_last_of("/:");
if (pos == s.npos) return s;
2013-02-16 18:32:47 +00:00
2013-02-18 01:44:01 +00:00
return s.substr(pos + 1);
2013-02-16 18:32:47 +00:00
}
#endif
2013-02-25 04:23:05 +00:00
2013-02-13 03:35:15 +00:00
}
2013-02-12 22:32:31 +00:00
namespace OS
{
bool Init()
{
BootTime = std::chrono::steady_clock::now();
2013-05-13 02:36:22 +00:00
memoryWriteLong(0, MacOS::Ticks); // 0 ticks since boot.
2013-07-14 20:45:06 +00:00
//std::chrono::system_clock::now(), to_time_t
// set global variable Time to the current time
time_t now = UnixToMac(::time(NULL));
memoryWriteLong(now, MacOS::TimeLM);
return true;
}
2013-02-16 18:32:47 +00:00
2013-02-25 22:34:25 +00:00
uint16_t Close(uint16_t trap)
{
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
Log("%04x Close(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
2016-11-04 16:31:22 +00:00
int rv = OS::Internal::close_file(ioRefNum, true);
if (rv < 0) d0 = MacOS::rfNumErr;
2013-02-26 01:41:35 +00:00
else d0 = 0;
2013-02-25 22:34:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-02-16 18:32:47 +00:00
2013-02-14 03:16:26 +00:00
uint16_t Create(uint16_t trap)
{
2015-01-07 22:00:57 +00:00
enum {
/* FileParam */
_qLink = 0,
_qType = 4,
_ioTrap = 6,
_ioCmdAddr = 8,
_ioCompletion = 12,
_ioResult = 16,
_ioNamePtr = 18,
_ioVRefNum = 22,
_ioFRefNum = 24,
_ioFVersNum = 26,
_filler1 = 27,
_ioFDirIndex = 28,
_ioFlAttrib = 30,
_ioFlVersNum = 31,
_ioFlFndrInfo = 32,
_ioFlNum = 48,
_ioFlStBlk = 52,
_ioFlLgLen = 54,
_ioFlPyLen = 58,
_ioFlRStBlk = 62,
_ioFlRLgLen = 64,
_ioFlRPyLen = 68,
_ioFlCrDat = 72,
_ioFlMdDat = 76,
// HFileParam
_ioDirID = 48,
};
bool htrap = trap & 0x0200;
const char *func = htrap ? "HCreate" : "Create";
2013-02-14 03:16:26 +00:00
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
2015-01-07 22:00:57 +00:00
Log("%04x %s(%08x)\n", trap, func, parm);
2013-02-14 03:16:26 +00:00
2015-01-07 22:00:57 +00:00
uint32_t namePtr = memoryReadLong(parm + _ioNamePtr);
2013-02-14 03:16:26 +00:00
std::string sname = ToolBox::ReadPString(namePtr, true);
2013-02-14 03:16:26 +00:00
if (!sname.length())
{
2015-01-07 22:00:57 +00:00
memoryWriteWord(MacOS::bdNamErr, parm + _ioResult);
2013-05-19 01:44:02 +00:00
return MacOS::bdNamErr;
2013-02-14 03:16:26 +00:00
}
2015-01-07 22:00:57 +00:00
if (htrap)
{
uint32_t ioDirID = memoryReadLong(parm + _ioDirID);
sname = FSSpecManager::ExpandPath(sname, ioDirID);
}
Log(" %s(%s)\n", func, sname.c_str());
2013-02-14 03:16:26 +00:00
int fd;
fd = ::open(sname.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
{
2014-12-21 00:32:54 +00:00
d0 = macos_error_from_errno();
2013-02-14 03:16:26 +00:00
}
else
{
::close(fd);
d0 = 0;
2013-02-14 03:16:26 +00:00
}
2015-01-07 22:00:57 +00:00
memoryWriteWord(d0, parm + _ioResult);
2013-02-14 03:16:26 +00:00
return d0;
2013-02-14 03:38:34 +00:00
}
2013-03-03 01:54:43 +00:00
uint16_t OpenCommon(uint32_t parm, bool fsspec, bool resource)
2013-02-25 04:22:31 +00:00
{
enum {
/* IOParam */
_qLink = 0,
_qType = 4,
_ioTrap = 6,
_ioCmdAddr = 8,
_ioCompletion = 12,
_ioResult = 16,
_ioNamePtr = 18,
_ioVRefNum = 22,
_ioRefNum = 24,
_ioVersNum = 26,
_ioPermssn = 27,
_ioMisc = 28,
_ioBuffer = 32,
_ioReqCount = 36,
_ioActCount = 40,
_ioPosMode = 44,
_ioPosOffset = 46,
_ioDirID = 48,
};
2016-11-04 16:31:22 +00:00
uint32_t d0 = 0;
2013-02-25 04:22:31 +00:00
uint8_t ioPermission = memoryReadByte(parm + _ioPermssn);
uint32_t namePtr = memoryReadLong(parm + _ioNamePtr);
2013-02-25 04:22:31 +00:00
std::string sname = ToolBox::ReadPString(namePtr, true);
if (fsspec)
{
uint32_t ioDirID = memoryReadLong(parm + _ioDirID);
sname = FSSpecManager::ExpandPath(sname, ioDirID);
}
2013-02-25 04:22:31 +00:00
2016-11-04 19:30:12 +00:00
Log(" open(%s, %02x, %02x)\n", sname.c_str(), resource, ioPermission);
2016-11-04 16:31:22 +00:00
auto fd = Internal::open_file(sname, resource, ioPermission);
if (fd) memoryWriteWord(fd.value(), parm + _ioRefNum);
d0 = fd.error();
2013-02-25 04:22:31 +00:00
memoryWriteWord(d0, parm + _ioResult);
2013-05-19 02:11:42 +00:00
return d0;
}
2013-02-25 04:22:31 +00:00
uint16_t Open(uint16_t trap)
2013-05-19 02:11:42 +00:00
{
2015-01-07 22:01:44 +00:00
bool htrap = trap & 0x0200;
const char *func = htrap ? "HOpen" : "Open";
2013-05-19 02:11:42 +00:00
uint32_t parm = cpuGetAReg(0);
2015-01-07 22:01:44 +00:00
Log("%04x %s(%08x)\n", trap, func, parm);
return OpenCommon(parm, htrap, false);
}
2013-03-03 01:54:43 +00:00
uint16_t OpenRF(uint16_t trap)
{
2015-01-07 22:01:44 +00:00
bool htrap = trap & 0x0200;
const char *func = htrap ? "HOpenRF" : "OpenRF";
uint32_t parm = cpuGetAReg(0);
2015-01-07 22:01:44 +00:00
Log("%04x %s(%08x)\n", trap, func, parm);
return OpenCommon(parm, htrap, true);
2013-02-25 04:22:31 +00:00
}
2013-05-19 02:11:42 +00:00
2013-02-25 04:22:31 +00:00
uint16_t Read(uint16_t trap)
{
2016-11-04 16:31:22 +00:00
uint32_t d0 = 0;
2013-02-25 04:22:31 +00:00
uint32_t parm = cpuGetAReg(0);
Log("%04x Read(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
uint32_t ioBuffer = memoryReadLong(parm + 32);
int32_t ioReqCount = memoryReadLong(parm + 36);
uint16_t ioPosMode = memoryReadWord(parm + 44);
int32_t ioPosOffset = memoryReadLong(parm + 46);
2016-11-04 16:31:22 +00:00
Log(" read(%04x, %08x, %08x)\n", ioRefNum, ioBuffer, ioReqCount);
/*
* per inside macintosh II-101:
* bit 6 (0x40) of ioPosOffset is used for read-verify operation
* (write date, call read w/ bit 6 to verify disk matches memory)
* returns ioError if it doesn't match.
*
* bit 7 (0x80) of ioPosMode indicate new-line mode is enabled --
* bits 8-15 are a newline character. reading terminates when newline
* char is encountered (or eof or ioReqCount).
*
* bit 4 is please cache bit
* bit 5 is please don't cache bit
* (if neither or both set, no preference)
*
*/
2013-02-25 04:22:31 +00:00
if (ioReqCount < 0)
{
2013-05-19 01:44:02 +00:00
d0 = MacOS::paramErr;
2013-02-25 04:22:31 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2016-11-04 16:31:22 +00:00
if (ioPosMode & 0x80) {
fprintf(stderr, "Newline Read (eof char = %02x)\n", ioPosMode >> 8);
exit(1);
}
ssize_t offset = ioPosOffset;
int whence = ioPosMode;
uint32_t transferCount = 0;
Internal::remap_seek(offset, whence);
2013-03-04 03:07:25 +00:00
2016-11-04 16:31:22 +00:00
auto ff = Internal::find_file(ioRefNum);
if (!ff) {
d0 = MacOS::rfNumErr;
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
2013-02-25 04:22:31 +00:00
}
2016-11-04 16:31:22 +00:00
auto pos = ff->seek(offset, whence);
if (!pos) {
d0 = pos.error();
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-03-04 03:07:25 +00:00
2016-11-04 16:31:22 +00:00
auto xfer = ff->read(memoryPointer(ioBuffer), ioReqCount);
if (xfer && *xfer == 0) xfer = MacOS::eofErr;
d0 = xfer.error();
transferCount = xfer.value_or(0);
2016-11-04 20:42:45 +00:00
offset = ff->get_mark().value_or((size_t)0);
2013-03-04 03:07:25 +00:00
2016-11-04 16:31:22 +00:00
memoryWriteLong(offset, parm + 46); // new offset.
memoryWriteLong(transferCount, parm + 40);
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-02-25 04:22:31 +00:00
2013-03-04 03:07:25 +00:00
uint16_t Write(uint16_t trap)
{
2016-11-04 16:31:22 +00:00
uint32_t d0 = 0;
2013-03-04 03:07:25 +00:00
uint32_t parm = cpuGetAReg(0);
Log("%04x Write(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
uint32_t ioBuffer = memoryReadLong(parm + 32);
int32_t ioReqCount = memoryReadLong(parm + 36);
uint16_t ioPosMode = memoryReadWord(parm + 44);
int32_t ioPosOffset = memoryReadLong(parm + 46);
2016-11-04 16:31:22 +00:00
Log(" write(%04x, %08x, %08x)\n", ioRefNum, ioBuffer, ioReqCount);
2013-03-04 03:07:25 +00:00
if (ioReqCount < 0)
2013-02-25 04:22:31 +00:00
{
2013-05-19 01:44:02 +00:00
d0 = MacOS::paramErr;
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2016-11-04 16:31:22 +00:00
ssize_t offset = ioPosOffset;
int whence = ioPosMode;
uint32_t transferCount = 0;
Internal::remap_seek(offset, whence);
2013-03-04 03:07:25 +00:00
2016-11-04 16:31:22 +00:00
auto ff = Internal::find_file(ioRefNum);
if (!ff) {
d0 = MacOS::rfNumErr;
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
2013-02-25 04:22:31 +00:00
}
2016-11-04 16:31:22 +00:00
auto pos = ff->seek(offset, whence);
if (!pos) {
d0 = pos.error();
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-02-25 04:22:31 +00:00
2016-11-04 16:31:22 +00:00
auto xfer = ff->write(memoryPointer(ioBuffer), ioReqCount);
if (xfer && *xfer == 0) xfer = MacOS::eofErr;
d0 = xfer.error();
transferCount = xfer.value_or(0);
2016-11-04 20:42:45 +00:00
offset = ff->get_mark().value_or((size_t)0);
2013-02-25 04:22:31 +00:00
2016-11-04 16:31:22 +00:00
memoryWriteLong(offset, parm + 46); // new offset.
memoryWriteLong(transferCount, parm + 40);
2013-02-25 04:22:31 +00:00
memoryWriteWord(d0, parm + 16);
2013-03-04 03:07:25 +00:00
return d0;
2013-02-25 04:22:31 +00:00
}
2013-03-04 03:07:25 +00:00
2013-02-14 03:38:34 +00:00
uint16_t Delete(uint16_t trap)
{
2015-01-07 22:01:20 +00:00
enum {
/* FileParam */
_qLink = 0,
_qType = 4,
_ioTrap = 6,
_ioCmdAddr = 8,
_ioCompletion = 12,
_ioResult = 16,
_ioNamePtr = 18,
_ioVRefNum = 22,
_ioFRefNum = 24,
_ioFVersNum = 26,
_filler1 = 27,
_ioFDirIndex = 28,
_ioFlAttrib = 30,
_ioFlVersNum = 31,
_ioFlFndrInfo = 32,
_ioFlNum = 48,
_ioFlStBlk = 52,
_ioFlLgLen = 54,
_ioFlPyLen = 58,
_ioFlRStBlk = 62,
_ioFlRLgLen = 64,
_ioFlRPyLen = 68,
_ioFlCrDat = 72,
_ioFlMdDat = 76,
// HFileParam
_ioDirID = 48,
};
bool htrap = trap & 0x0200;
const char *func = htrap ? "HDelete" : "Delete";
2013-02-14 03:38:34 +00:00
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
2015-01-07 22:01:20 +00:00
Log("%04x %s(%08x)\n", trap, func, parm);
2013-02-14 03:38:34 +00:00
2015-01-07 22:01:20 +00:00
uint32_t namePtr = memoryReadLong(parm + _ioNamePtr);
2013-02-14 03:16:26 +00:00
std::string sname = ToolBox::ReadPString(namePtr, true);
2013-02-14 03:38:34 +00:00
if (!sname.length())
{
2015-01-07 22:01:20 +00:00
memoryWriteWord(MacOS::bdNamErr, parm + _ioResult);
2013-05-19 01:44:02 +00:00
return MacOS::bdNamErr;
2013-02-14 03:38:34 +00:00
}
2015-01-07 22:01:20 +00:00
if (htrap)
{
uint32_t ioDirID = memoryReadLong(parm + _ioDirID);
sname = FSSpecManager::ExpandPath(sname, ioDirID);
}
Log(" %s(%s)\n", func, sname.c_str());
2013-02-14 03:38:34 +00:00
2016-11-11 17:01:06 +00:00
std::error_code ec;
if (fs::remove(sname, ec)) d0 = 0;
else if (ec) d0 = macos_error_from_errno(ec);
else d0 = MacOS::fnfErr;
2013-02-14 03:38:34 +00:00
2015-01-07 22:01:20 +00:00
memoryWriteWord(d0, parm + _ioResult);
2013-02-14 03:38:34 +00:00
return d0;
2013-02-14 03:16:26 +00:00
}
2015-01-07 22:01:20 +00:00
2013-02-16 21:11:10 +00:00
uint16_t GetEOF(uint16_t trap)
{
2016-11-04 16:31:22 +00:00
uint32_t d0 = 0;
size_t size = 0;
2013-02-16 21:11:10 +00:00
uint32_t parm = cpuGetAReg(0);
2013-02-16 23:51:28 +00:00
Log("%04x GetEOF(%08x)\n", trap, parm);
2013-02-16 21:11:10 +00:00
2013-02-16 23:51:28 +00:00
//uint32_t ioCompletion = memoryReadLong(parm + 12);
2013-02-16 21:11:10 +00:00
uint16_t ioRefNum = memoryReadWord(parm + 24);
2016-11-04 16:31:22 +00:00
auto ff = Internal::find_file(ioRefNum);
if (ff) {
auto eof = ff->get_eof();
d0 = eof.error();
size = eof.value_or(0);
} else {
d0 = MacOS::rfNumErr;
2013-02-16 21:11:10 +00:00
size = 0;
}
memoryWriteWord(d0, parm + 16);
memoryWriteLong(size, parm + 28);
return d0;
}
2013-03-04 03:07:25 +00:00
uint16_t SetEOF(uint16_t trap)
{
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
Log("%04x SetEOF(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
uint32_t ioMisc = memoryReadLong(parm + 28);
2016-11-04 16:31:22 +00:00
auto ff = Internal::find_file(ioRefNum);
if (ff) {
auto eof = ff->set_eof(ioMisc);
d0 = eof.error();
} else {
d0 = MacOS::rfNumErr;
}
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-05-18 04:00:47 +00:00
uint16_t GetFPos(uint16_t trap)
{
2016-11-04 16:31:22 +00:00
uint32_t d0 = 0;
size_t size = 0;
2013-05-18 04:00:47 +00:00
uint32_t parm = cpuGetAReg(0);
Log("%04x GetFPos(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
2016-11-04 16:31:22 +00:00
auto ff = Internal::find_file(ioRefNum);
if (ff) {
auto mark = ff->get_mark();
d0 = mark.error();
size = mark.value_or(0);
} else {
d0 = MacOS::rfNumErr;
size = 0;
2013-05-18 04:00:47 +00:00
}
2016-11-04 16:31:22 +00:00
memoryWriteLong(0, parm + 36); // ioReqCount
memoryWriteLong(0, parm + 40); // ioActCount
memoryWriteWord(0, parm + 44); // ioPosMode
memoryWriteLong(size, parm + 46); // ioPosOffset
2013-05-18 04:00:47 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-03-04 03:07:25 +00:00
uint16_t SetFPos(uint16_t trap)
{
uint32_t d0;
uint32_t parm = cpuGetAReg(0);
Log("%04x SetFPos(%08x)\n", trap, parm);
//uint32_t ioCompletion = memoryReadLong(parm + 12);
uint16_t ioRefNum = memoryReadWord(parm + 24);
uint16_t ioPosMode = memoryReadWord(parm + 44);
int32_t ioPosOffset = memoryReadLong(parm + 46);
2016-11-04 16:31:22 +00:00
ssize_t offset = ioPosOffset;
int whence = ioPosMode;
Internal::remap_seek(offset, whence);
auto ff = Internal::find_file(ioRefNum);
if (ff) {
auto pos = ff->seek(offset, whence);
d0 = pos.error();
offset = pos.value_or(0);
} else {
d0 = MacOS::rfNumErr;
offset = 0;
2013-03-04 03:07:25 +00:00
}
2016-11-04 16:31:22 +00:00
memoryWriteLong(offset, parm + 46); // new offset.
2013-03-04 03:07:25 +00:00
memoryWriteWord(d0, parm + 16);
return d0;
}
2013-02-16 21:11:10 +00:00
2015-01-06 01:11:10 +00:00
#pragma mark - String Utilities
uint16_t CmpString(uint16_t trap)
{
/*
* on entry:
* A0 Pointer to first character of first string
* A1 Pointer to first character of second string
* D0 (high) length of first string
* D0 (low) length of second string
*
* on exit:
* D0 0 if strings equal, 1 if strings not equal.
*
*/
bool caseSens = trap & (1 << 9);
//bool diacSens = trap & (1 << 10); // ignore for now...
uint32_t aStr = cpuGetAReg(0);
uint32_t bStr = cpuGetAReg(1);
uint32_t length = cpuGetDReg(0);
uint32_t aLen = (length >> 16);
uint32_t bLen = (length & 0xffff);
std::string a = ToolBox::ReadString(aStr, aLen);
std::string b = ToolBox::ReadString(bStr, bLen);
Log("%04x CmpString(%s, %s)\n", trap, a.c_str(), b.c_str());
if (aLen != bLen) return 1; // different length...
if (aStr == bStr) return 0; // same ptr...
bool eq;
eq = std::equal(
a.begin(),
a.end(),
b.begin(),
[caseSens](char a, char b){
if (!caseSens)
{
a = toupper(a);
b = toupper(b);
}
return a == b;
}
);
return eq ? 0 : 1;
}
#pragma mark - Time Utilities
time_t UnixToMac(time_t t)
{
return t + EpochAdjust;
}
2013-07-14 20:45:06 +00:00
time_t MacToUnix(time_t t)
{
return t - EpochAdjust;
}
uint16_t ReadDateTime(uint16_t trap)
{
/*
* on entry:
* A0 Pointer to long word secs
*
* on exit:
* A0 pointer to long word secs
* D0 Result code
*
*/
time_t now;
uint32_t secsPtr = cpuGetAReg(0);
Log("%04x ReadDateTime(%08x)\n", trap, secsPtr);
now = ::time(NULL);
now = UnixToMac(now);
if (secsPtr) memoryWriteLong(now, secsPtr);
// also set global variable Time.
2013-05-13 02:36:22 +00:00
memoryWriteLong(now, MacOS::TimeLM);
return 0;
}
uint16_t SecondsToDate(uint16_t trap)
{
/*
* on entry:
* D0 Seconds since midnight, January 1, 1904
* A0 pointer to date-time record
*
* on exit:
* D0 Result code
*
*/
uint32_t s = cpuGetDReg(0);
uint32_t dtPtr = cpuGetAReg(0);
Log("%04x SecondsToDate(%08x, %08x)\n", trap, s, dtPtr);
if (dtPtr)
{
struct tm *tm;
time_t t;
t = MacToUnix(s);
tm = ::localtime(&t);
memoryWriteWord(tm->tm_year + 1900, dtPtr + 0);
memoryWriteWord(tm->tm_mon + 1, dtPtr + 2);
memoryWriteWord(tm->tm_mday, dtPtr + 4);
memoryWriteWord(tm->tm_hour, dtPtr + 6);
memoryWriteWord(tm->tm_min, dtPtr + 8);
memoryWriteWord(tm->tm_sec, dtPtr + 10);
memoryWriteWord(tm->tm_wday + 1, dtPtr + 12);
}
return 0;
}
uint16_t TickCount(uint16_t trap)
{
typedef std::chrono::duration<int32_t, std::ratio<1, 60> > ticks;
Log("%04x TickCount()\n", trap);
auto now = std::chrono::steady_clock::now();
uint32_t t = std::chrono::duration_cast< ticks >(now - BootTime).count();
// global Ticks
2013-05-13 02:36:22 +00:00
memoryWriteLong(t, MacOS::Ticks);
ToolReturn<4>(-1, t);
return 0;
}
uint16_t Microseconds(uint16_t trap)
{
// UnsignedWide is a uint64_t
// Microseconds(UnsignedWide * microTickCount)
// FOURWORDINLINE(0xA193, 0x225F, 0x22C8, 0x2280);
uint32_t microTickCount;
StackFrame<4>(microTickCount);
Log("%04x %s(%08x)\n", trap, __func__, microTickCount);
auto now = std::chrono::steady_clock::now();
uint64_t t = std::chrono::duration_cast< std::chrono::microseconds >(now - BootTime).count();
if (microTickCount)
memoryWriteLongLong(t, microTickCount);
return 0;
}
2013-02-25 04:21:48 +00:00
#pragma mark - Trap Manager
Squashed commit of the following: commit f4807cd071ba5c4ce9ba4aa8c906ba335da07a2d Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Feb 13 16:09:55 2015 -0500 merge in missing tool calls commit 6830ab51ce7c48e3d0d1352f04df8df914146b3e Merge: 1675a47 24c17a9 Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Fri Feb 13 13:22:07 2015 -0500 SetOSTrapAddress stub commit b255937e6c9a6765a9763b011bd99c3f675d997c Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Fri Jan 23 10:26:52 2015 -0500 HWPriv OS Trap commit 16d0038fd1b9b6db3e154ab5e810624ab7d3ca08 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Jan 23 10:26:40 2015 -0500 HWPriv OS Trap commit bf92f806326925b68b497127da421f833e2cdba2 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Wed Jan 21 17:34:30 2015 -0500 RM::GetNamedResource commit 540490d0c5ba7c8310271b1c7fd68259a77a2f28 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Wed Jan 21 14:31:03 2015 -0500 GetNamedResource trap commit 09aa8d0f2600668b37a51e7872db9a9f9c9ea4ef Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 19:32:04 2015 -0500 Set ApplZone global (ARMCFront) commit 45fa54abca52a80c980c870f43f1f82225d7c43d Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 13:58:24 2015 -0500 update test makefile commit b1e6635630d2fc112b613312000e091ff7f297bc Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 13:35:07 2015 -0500 SysEnvirons ($a090) OS Trap commit a5126544b8bdda155a5f8e9395d547a7bf3eae94 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Jan 19 15:02:29 2015 -0500 undo HGetVolume cwd, for now commit 912c8748254a83d3a5eacf776a6b6b3a4439aa75 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Jan 19 10:55:58 2015 -0500 Fix SetPtrSize commit c0fe74c1b81d1782a61240a18ecc4e51a3ecb88c Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Sun Jan 18 15:20:38 2015 -0500 update dispatch code... commit c7c548e5ac2096e29aebf532e0eb69e2227bdecd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun Jan 18 14:48:22 2015 -0500 add back new tool calls. commit cbb554174ef186b8b74383596212d9b8f04ebfea Merge: 44d19f7 5e616d3 Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Sun Dec 14 18:01:38 2014 -0500 new dispatcher. commit ea5f2139217df8aebe975b8e4680080d58a772bd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Dec 8 11:42:23 2014 -0500 get ready for trap overrides. commit 17c5b40ac866a7adaa559bc9164ffe32e926cc96 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Dec 5 14:39:17 2014 -0500 remove dead code commit fc7df738cc9dd72d65ba656df71dd6aa124ce8af Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Dec 5 14:32:18 2014 -0500 display trap name for GetToolTrap, etc.
2015-02-13 21:11:55 +00:00
#ifdef OLD_TRAP_DISPATCH
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);
cpuSetAReg(0, 0);
return MacOS::dsCoreErr;
}
2013-08-04 18:34:34 +00:00
uint16_t SetToolTrapAddress(uint16_t trap)
{
//pascal void SetToolTrapAddress(long trapAddr, short trapNum);
/*
2013-08-04 18:34:34 +00:00
* 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";
2013-08-04 18:34:34 +00:00
Log("%04x SetToolTrapAddress($%08x, $%04x %s)\n",
trap, trapAddress, trapNumber, trapName);
2013-08-04 18:34:34 +00:00
return MacOS::dsCoreErr;
}
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);
cpuSetAReg(0, 0);
return 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:
*
*/
uint16_t trapNumber = cpuGetDReg(0);
uint32_t trapAddress = cpuGetAReg(0);
const char *trapName = TrapName(trapNumber | 0xa000);
if (!trapName) trapName = "Unknown";
Log("%04x SetOSTrapAddress($%08x, $%04x %s)\n",
trap, trapAddress, trapNumber, trapName);
return MacOS::dsCoreErr;
}
Squashed commit of the following: commit f4807cd071ba5c4ce9ba4aa8c906ba335da07a2d Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Feb 13 16:09:55 2015 -0500 merge in missing tool calls commit 6830ab51ce7c48e3d0d1352f04df8df914146b3e Merge: 1675a47 24c17a9 Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Fri Feb 13 13:22:07 2015 -0500 SetOSTrapAddress stub commit b255937e6c9a6765a9763b011bd99c3f675d997c Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Fri Jan 23 10:26:52 2015 -0500 HWPriv OS Trap commit 16d0038fd1b9b6db3e154ab5e810624ab7d3ca08 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Jan 23 10:26:40 2015 -0500 HWPriv OS Trap commit bf92f806326925b68b497127da421f833e2cdba2 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Wed Jan 21 17:34:30 2015 -0500 RM::GetNamedResource commit 540490d0c5ba7c8310271b1c7fd68259a77a2f28 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Wed Jan 21 14:31:03 2015 -0500 GetNamedResource trap commit 09aa8d0f2600668b37a51e7872db9a9f9c9ea4ef Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 19:32:04 2015 -0500 Set ApplZone global (ARMCFront) commit 45fa54abca52a80c980c870f43f1f82225d7c43d Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 13:58:24 2015 -0500 update test makefile commit b1e6635630d2fc112b613312000e091ff7f297bc Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Tue Jan 20 13:35:07 2015 -0500 SysEnvirons ($a090) OS Trap commit a5126544b8bdda155a5f8e9395d547a7bf3eae94 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Jan 19 15:02:29 2015 -0500 undo HGetVolume cwd, for now commit 912c8748254a83d3a5eacf776a6b6b3a4439aa75 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Jan 19 10:55:58 2015 -0500 Fix SetPtrSize commit c0fe74c1b81d1782a61240a18ecc4e51a3ecb88c Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Sun Jan 18 15:20:38 2015 -0500 update dispatch code... commit c7c548e5ac2096e29aebf532e0eb69e2227bdecd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Sun Jan 18 14:48:22 2015 -0500 add back new tool calls. commit cbb554174ef186b8b74383596212d9b8f04ebfea Merge: 44d19f7 5e616d3 Author: Kelvin Sherlock <ksherlock@gmail.com> 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 <ksherlock@gmail.com> Date: Sun Dec 14 18:01:38 2014 -0500 new dispatcher. commit ea5f2139217df8aebe975b8e4680080d58a772bd Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Mon Dec 8 11:42:23 2014 -0500 get ready for trap overrides. commit 17c5b40ac866a7adaa559bc9164ffe32e926cc96 Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Dec 5 14:39:17 2014 -0500 remove dead code commit fc7df738cc9dd72d65ba656df71dd6aa124ce8af Author: Kelvin Sherlock <ksherlock@gmail.com> Date: Fri Dec 5 14:32:18 2014 -0500 display trap name for GetToolTrap, etc.
2015-02-13 21:11:55 +00:00
#endif
2014-12-16 18:47:33 +00:00
#pragma mark XP - RAM
// these are not particularly documented.
uint16_t ReadXPRam(uint16_t trap)
{
// a0 = address?
// d0 = count? item?
Log("%04x ReadXPRam()\n", trap);
return MacOS::prWrErr;
}
uint16_t WriteXPRam(uint16_t trap)
{
Log("%04x WriteXPRam()\n", trap);
return MacOS::prWrErr;
}
2014-12-18 19:29:20 +00:00
#pragma mark - Timer
struct TimerEntry {
uint32_t tmTaskPtr = 0; // address of the queue. passed back in A1.
uint32_t tmAddr = 0;
bool extended = false;
bool active = false;
std::chrono::time_point<std::chrono::steady_clock> when;
TimerEntry(uint32_t a, uint32_t b) : tmTaskPtr(a), tmAddr(b)
{}
};
// heap sorted by next task to run?
static std::deque<TimerEntry> TimerQueue;
namespace TMTask {
enum {
_qLink = 0,
_qType = 4,
_tmAddr = 6,
_tmCount = 10,
_tmWakeUp = 14,
_tmReserved = 18
};
}
uint16_t InsTime(uint16_t trap)
{
// PROCEDURE InsTime (tmTaskPtr: QElemPtr);
// this adds an entry to the queue but does not schedule it.
/*
* on entry
* A0 Address of the task record
*
* on exit
* D0 Result code
*/
using namespace TMTask;
uint32_t tmTaskPtr = cpuGetAReg(0);
Log("%04x InsTime(%08x)\n", trap, tmTaskPtr);
if (tmTaskPtr)
{
memoryWriteLong(0, tmTaskPtr + _qLink);
memoryWriteWord(0, tmTaskPtr + _qType);
memoryWriteLong(0, tmTaskPtr + _tmCount);
memoryWriteLong(0, tmTaskPtr + _tmWakeUp);
memoryWriteLong(0, tmTaskPtr + _tmReserved);
TimerQueue.emplace_back(tmTaskPtr, memoryReadLong(tmTaskPtr + _tmAddr));
}
return MacOS::noErr;
}
uint16_t PrimeTime(uint16_t trap)
{
// PROCEDURE PrimeTime (tmTaskPtr: QElemPtr; count: LongInt);
// this activates an entry.
/*
* on entry
* A0 Address of the task record
* D0 Specified delay time (long)
*
* on exit
* D0 Result code
*/
using namespace TMTask;
uint32_t tmTaskPtr = cpuGetAReg(0);
uint32_t count = cpuGetDReg(0);
Log("%04x PrimeTime(%08x, %08x)\n", trap, tmTaskPtr, count);
if (tmTaskPtr)
{
auto iter = std::find_if(TimerQueue.begin(), TimerQueue.end(), [tmTaskPtr](const TimerEntry &e){
return e.tmTaskPtr == tmTaskPtr;
});
if (iter != TimerQueue.end() && !iter->active)
{
auto now = std::chrono::steady_clock::now();
iter->active = true;
if (count == 0) {
// retain the original time or set it to now.
iter->when = std::max(now, iter->when);
}
else
{
int64_t micro;
if (count < 0x80000000)
micro = count * 1000;
else
micro = -(int32_t)count;
iter->when = now + std::chrono::microseconds(micro);
}
memoryWriteWord(0x8000, tmTaskPtr + _qType);
2014-12-18 19:29:20 +00:00
}
}
return MacOS::noErr;
}
uint16_t RmvTime(uint16_t trap)
{
// PROCEDURE RmvTime (tmTaskPtr: QElemPtr);
// unschedule (but not actually remove)
/*
* on entry
* A0 Address of the task record
*
* on exit
* D0 Result code
*/
using namespace TMTask;
uint32_t tmTaskPtr = cpuGetAReg(0);
Log("%04x RmvTime(%08x)\n", trap, tmTaskPtr);
if (tmTaskPtr)
{
auto iter = std::find_if(TimerQueue.begin(), TimerQueue.end(), [tmTaskPtr](const TimerEntry &e){
return e.tmTaskPtr == tmTaskPtr;
});
if (iter != TimerQueue.end())
{
uint32_t count = 0;
if (iter->active)
{
iter->active = false;
// update tmCount to the amount of time remaining.
// uses negative microseconds
// or positive milliseconds.
auto now = std::chrono::steady_clock::now();
int64_t micro = std::chrono::duration_cast< std::chrono::microseconds >(iter->when - now).count();
if (micro < 0)
count = 0;
else if (micro < 0x80000000)
count = -micro;
else
count = micro / 10000;
}
memoryWriteWord(0, tmTaskPtr + _qType);
memoryWriteLong(count, tmTaskPtr + _tmCount);
2014-12-18 19:29:20 +00:00
}
}
return MacOS::noErr;
}
}