mpw/toolbox/mpw_time.cpp
2013-02-16 18:51:28 -05:00

120 lines
1.9 KiB
C++

#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;
}
}