From c40d8a4fbe543fde21d253fd8431f78563aea6b3 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Thu, 22 Nov 2012 22:38:14 -0800 Subject: [PATCH] Implement sceRtcGetCurrentClock(). Not sure if this needs some kind of DST handling for the tz? --- Core/HLE/FunctionWrappers.h | 5 +++ Core/HLE/sceIo.cpp | 11 +----- Core/HLE/sceRtc.cpp | 75 ++++++++++++++++++++++++++++++++++--- Core/HLE/sceRtc.h | 10 +++++ 4 files changed, 87 insertions(+), 14 deletions(-) diff --git a/Core/HLE/FunctionWrappers.h b/Core/HLE/FunctionWrappers.h index 7f4c08e02b..b1a4eb8702 100644 --- a/Core/HLE/FunctionWrappers.h +++ b/Core/HLE/FunctionWrappers.h @@ -44,6 +44,11 @@ template void WrapU_U() { RETURN(retval); } +template void WrapU_UI() { + u32 retval = func(PARAM(0), PARAM(1)); + RETURN(retval); +} + template void WrapI_U() { int retval = func(PARAM(0)); RETURN(retval); diff --git a/Core/HLE/sceIo.cpp b/Core/HLE/sceIo.cpp index e82ec1bd63..c8b0347f1f 100644 --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -31,6 +31,7 @@ #include "../FileSystems/DirectoryFileSystem.h" #include "sceIo.h" +#include "sceRtc.h" #include "sceKernel.h" #include "sceKernelMemory.h" #include "sceKernelThread.h" @@ -90,15 +91,7 @@ enum TYPE_FILE=0x20 }; -struct ScePspDateTime { - unsigned short year; - unsigned short month; - unsigned short day; - unsigned short hour; - unsigned short minute; - unsigned short second; - unsigned int microsecond; -}; + struct SceIoStat { SceMode st_mode; diff --git a/Core/HLE/sceRtc.cpp b/Core/HLE/sceRtc.cpp index 9d300c664b..8a85aee8d3 100644 --- a/Core/HLE/sceRtc.cpp +++ b/Core/HLE/sceRtc.cpp @@ -17,8 +17,11 @@ #ifdef _WIN32 #include +#else +#include #endif +#include #include "base/timeutil.h" #include "HLE.h" @@ -36,6 +39,33 @@ u64 __RtcGetCurrentTick() return cyclesToUs(CoreTiming::GetTicks()) + rtcMagicOffset; } +#ifdef _WIN32 +#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL + +void gettimeofday(timeval *tv, void *ignore) +{ + FILETIME ft_utc, ft_local; + GetSystemTimeAsFileTime(&ft_utc); + ft_local = ft_utc; + + u64 from_1601_us = (((u64) ft_local.dwHighDateTime << 32ULL) + (u64) ft_local.dwLowDateTime) / 10ULL; + u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US; + + tv->tv_sec = long(from_1970_us / 1000000UL); + tv->tv_usec = from_1970_us % 1000000UL; +} +#endif + +void __RtcTmToPspTime(ScePspDateTime &t, tm *val) +{ + t.year = val->tm_year + 1900; + t.month = val->tm_mon + 1; + t.day = val->tm_mday; + t.hour = val->tm_hour; + t.minute = val->tm_min; + t.second = val->tm_sec; +} + u32 sceRtcGetTickResolution() { DEBUG_LOG(HLE, "sceRtcGetTickResolution()"); @@ -62,11 +92,46 @@ u64 sceRtcGetAcculumativeTime() return __RtcGetCurrentTick(); } -u32 sceRtcGetCurrentClockLocalTime() +u32 sceRtcGetCurrentClock(u32 pspTimePtr, int tz) { - ERROR_LOG(HLE,"UNIMPL 0=sceRtcGetCurrentClockLocalTime()"); + DEBUG_LOG(HLE,"sceRtcGetCurrentClock(%08x, %d)", pspTimePtr, tz); + timeval tv; + gettimeofday(&tv, NULL); + + time_t sec = (time_t) tv.tv_sec; + tm *utc = gmtime(&sec); + + utc->tm_isdst = -1; + utc->tm_min += tz; + mktime(utc); + + ScePspDateTime ret; + __RtcTmToPspTime(ret, utc); + ret.microsecond = tv.tv_usec; + + Memory::WriteStruct(pspTimePtr, &ret); + return 0; } + +u32 sceRtcGetCurrentClockLocalTime(u32 pspTimePtr) +{ + DEBUG_LOG(HLE,"sceRtcGetCurrentClockLocalTime(%08x)", pspTimePtr); + timeval tv; + gettimeofday(&tv, NULL); + + time_t sec = (time_t) tv.tv_sec; + tm *local = localtime(&sec); + + ScePspDateTime ret; + __RtcTmToPspTime(ret, local); + ret.microsecond = tv.tv_usec; + + Memory::WriteStruct(pspTimePtr, &ret); + + return 0; +} + u32 sceRtcGetTick() { ERROR_LOG(HLE,"UNIMPL 0=sceRtcGetTick(...)"); @@ -75,7 +140,7 @@ u32 sceRtcGetTick() u32 sceRtcGetDayOfWeek(u32 year, u32 month, u32 day) { - DEBUG_LOG(HLE,"sceRtcGetDayOfWeek()"); + DEBUG_LOG(HLE,"sceRtcGetDayOfWeek(%d, %d, %d)", year, month, day); static u32 t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 }; if (month > 12 || month < 1) { // Preventive crashfix @@ -123,8 +188,8 @@ const HLEFunction sceRtc[] = {0x3f7ad767, WrapU_U, "sceRtcGetCurrentTick"}, {0x011F03C1, WrapU64_V, "sceRtcGetAccumulativeTime"}, {0x029CA3B3, WrapU64_V, "sceRtcGetAccumlativeTime"}, - {0x4cfa57b0, 0, "sceRtcGetCurrentClock"}, - {0xE7C27D1B, WrapU_V, "sceRtcGetCurrentClockLocalTime"}, + {0x4cfa57b0, WrapU_UI, "sceRtcGetCurrentClock"}, + {0xE7C27D1B, WrapU_U, "sceRtcGetCurrentClockLocalTime"}, {0x34885E0D, 0, "sceRtcConvertUtcToLocalTime"}, {0x779242A2, 0, "sceRtcConvertLocalTimeToUTC"}, {0x42307A17, 0, "sceRtcIsLeapYear"}, diff --git a/Core/HLE/sceRtc.h b/Core/HLE/sceRtc.h index 0fbc074cb3..7e2af30e5c 100644 --- a/Core/HLE/sceRtc.h +++ b/Core/HLE/sceRtc.h @@ -19,3 +19,13 @@ void sceRtcGetCurrentTick(); void Register_sceRtc(); + +struct ScePspDateTime { + unsigned short year; + unsigned short month; + unsigned short day; + unsigned short hour; + unsigned short minute; + unsigned short second; + unsigned int microsecond; +};