Move Time:: to OS::

add tick support (std::chrono::steady_clock)
This commit is contained in:
Kelvin Sherlock 2013-02-17 23:00:28 -05:00
parent 27168ed419
commit 24611657a8
6 changed files with 143 additions and 155 deletions

View File

@ -10,7 +10,6 @@ set(TOOLBOX_SRC
mm.cpp
os.cpp
qd.cpp
mpw_time.cpp
)

View File

@ -1,120 +0,0 @@
#include "mpw_time.h"
#include "toolbox.h"
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include <chrono>
#include "stackframe.h"
using ToolBox::Log;
// todo -- have background thread to update Ticks and Time global variables?
namespace
{
const long EpochAdjust = 86400 * (365 * (1970 - 1904) + 17); // 17 leap years.
}
namespace Time
{
time_t UnixToMac(time_t t)
{
return t + EpochAdjust;
}
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.
memoryWriteLong(now, 0x020c);
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)
{
uint32_t ticks;
Log("%04x TickCount()\n", trap);
//auto t = std::chrono::steady_clock::now();
ticks = 0;
// global Ticks
memoryWriteLong(ticks, 0x16A);
ToolReturn<4>(-1, ticks);
return 0;
}
}

View File

@ -1,19 +0,0 @@
#ifndef __MPW_TIME_H__
#define __MPW_TIME_H__
#include <cstdint>
#include <ctime>
namespace Time
{
time_t UnixToMac(time_t);
time_t MacToUnix(time_t);
uint16_t ReadDateTime(uint16_t trap);
uint16_t SecondsToDate(uint16_t trap);
uint16_t TickCount(uint16_t trap);
}
#endif

View File

@ -1,15 +1,8 @@
#include "os.h"
#include "toolbox.h"
#include "mpw_time.h"
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include <string>
#include <cerrno>
#include <cctype>
#include <algorithm>
#include <chrono>
#include <sys/xattr.h>
#include <sys/stat.h>
@ -20,12 +13,27 @@
#include <strings.h>
#include <cpu/defs.h>
#include <cpu/CpuModule.h>
#include <cpu/fmem.h>
#include "os.h"
#include "toolbox.h"
#include "stackframe.h"
using ToolBox::Log;
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;
// should make this public since it's needed by mpw/*
uint16_t errno_to_oserr(int xerrno)
{
@ -89,6 +97,14 @@ namespace {
namespace OS
{
bool Init()
{
BootTime = std::chrono::steady_clock::now();
memoryWriteLong(0, 0x16A); // 0 ticks since boot.
return true;
}
// known text file extensions
bool IsTextFile(const std::string &s)
@ -388,8 +404,8 @@ namespace OS
memoryWriteLong(st.st_size, parm + 58);
// create date.
memoryWriteLong(Time::UnixToMac(st.st_birthtime), parm + 72);
memoryWriteLong(Time::UnixToMac(st.st_mtime), parm + 76);
memoryWriteLong(UnixToMac(st.st_birthtime), parm + 72);
memoryWriteLong(UnixToMac(st.st_mtime), parm + 76);
// res fork...
// do this last since it adjusts the name and the stat.
@ -500,7 +516,7 @@ namespace OS
}
#pragma mark string utilities
#pragma mark - String Utilities
uint16_t CmpString(uint16_t trap)
{
@ -556,4 +572,102 @@ namespace OS
}
#pragma mark - Time Utilities
time_t UnixToMac(time_t t)
{
return t + EpochAdjust;
}
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.
memoryWriteLong(now, 0x020c);
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
memoryWriteLong(t, 0x16A);
ToolReturn<4>(-1, t);
return 0;
}
}

View File

@ -3,6 +3,7 @@
#include <cstdint>
#include <string>
#include <ctime>
namespace OS
{
@ -100,10 +101,16 @@ namespace OS
};
bool Init();
bool IsTextFile(const std::string &s);
bool IsBinaryFile(const std::string &s);
time_t UnixToMac(time_t);
time_t MacToUnix(time_t);
#pragma mark FS Utilities
uint16_t Create(uint16_t trap);
uint16_t Delete(uint16_t trap);
@ -113,8 +120,16 @@ namespace OS
uint16_t GetEOF(uint16_t trap);
uint16_t GetVol(uint16_t trap);
#pragma mark String Utilities
uint16_t CmpString(uint16_t trap);
#pragma mark - Time Utilities
uint16_t ReadDateTime(uint16_t trap);
uint16_t SecondsToDate(uint16_t trap);
uint16_t TickCount(uint16_t trap);
}
#endif

View File

@ -12,7 +12,6 @@
#include "mm.h"
#include "os.h"
#include "qd.h"
#include "mpw_time.h"
// yuck. TST.W d0
@ -88,17 +87,17 @@ namespace ToolBox {
// ReadDateTime (VAR sees: LONGINT) : OSErr;
case 0xa039:
d0 = Time::ReadDateTime(trap);
d0 = OS::ReadDateTime(trap);
break;
// SecondsToDate (s: LongInt; VAR d: DateTimeRec);
case 0xa9c6:
d0 = Time::SecondsToDate(trap);
d0 = OS::SecondsToDate(trap);
break;
// TickCount : LONGINT;
case 0xa975:
d0 = Time::TickCount(trap);
d0 = OS::TickCount(trap);
break;
//_CmpString [MARKS,CASE]