2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2013-07-29 03:43:25 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2013-05-01 02:58:51 +00:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/Serializer.h"
|
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2013-05-05 17:12:29 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/HLE/HLE.h"
|
|
|
|
#include "Core/HLE/sceKernel.h"
|
|
|
|
#include "Core/HLE/sceKernelTime.h"
|
2013-08-04 17:11:56 +00:00
|
|
|
#include "Core/HLE/sceKernelThread.h"
|
2013-05-05 17:12:29 +00:00
|
|
|
#include "Core/HLE/sceRtc.h"
|
2014-03-15 18:22:19 +00:00
|
|
|
#include "Core/MemMap.h"
|
2020-10-11 08:19:06 +00:00
|
|
|
#include "Core/System.h"
|
2017-11-14 06:16:40 +00:00
|
|
|
#include "StringUtils.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-01-02 09:09:44 +00:00
|
|
|
// The time when the game started.
|
2013-09-07 20:02:55 +00:00
|
|
|
static time_t start_time;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-01-02 09:09:44 +00:00
|
|
|
void __KernelTimeInit()
|
|
|
|
{
|
|
|
|
time(&start_time);
|
2020-10-11 08:19:06 +00:00
|
|
|
if (PSP_CoreParameter().compat.flags().DateLimited) {
|
|
|
|
// Car Jack Streets(NPUZ00043) requires that the date cannot exceed a certain time.
|
|
|
|
// 2011 year makes it work fine.
|
|
|
|
tm *tm;
|
|
|
|
tm = localtime(&start_time);
|
|
|
|
tm->tm_year = 111;// 2011 year.
|
|
|
|
start_time = mktime(tm);
|
|
|
|
}
|
2013-01-02 09:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void __KernelTimeDoState(PointerWrap &p)
|
|
|
|
{
|
2014-01-15 16:03:37 +00:00
|
|
|
auto s = p.Section("sceKernelTime", 1, 2);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2014-01-15 16:03:37 +00:00
|
|
|
if (s < 2) {
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, start_time);
|
2014-01-15 16:03:37 +00:00
|
|
|
} else {
|
|
|
|
u64 t = start_time;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, t);
|
2014-01-15 16:03:37 +00:00
|
|
|
start_time = (time_t)t;
|
|
|
|
}
|
2013-01-02 09:09:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 04:42:16 +00:00
|
|
|
int sceKernelGetSystemTime(u32 sysclockPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-10-24 08:10:09 +00:00
|
|
|
u64 t = CoreTiming::GetGlobalTimeUs();
|
2013-03-30 16:51:34 +00:00
|
|
|
if (Memory::IsValidAddress(sysclockPtr))
|
|
|
|
Memory::Write_U64(t, sysclockPtr);
|
2014-01-28 04:38:14 +00:00
|
|
|
VERBOSE_LOG(SCEKERNEL, "sceKernelGetSystemTime(out:%16llx)", t);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(265);
|
2013-08-04 17:11:56 +00:00
|
|
|
hleReSchedule("system time");
|
2013-02-03 04:42:16 +00:00
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 15:33:01 +00:00
|
|
|
u32 sceKernelGetSystemTimeLow()
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
// This clock should tick at 1 Mhz.
|
2013-10-24 08:10:09 +00:00
|
|
|
u64 t = CoreTiming::GetGlobalTimeUs();
|
2013-09-07 20:02:55 +00:00
|
|
|
VERBOSE_LOG(SCEKERNEL,"%08x=sceKernelGetSystemTimeLow()",(u32)t);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(165);
|
2013-08-04 17:11:56 +00:00
|
|
|
hleReSchedule("system time");
|
2013-03-30 17:08:23 +00:00
|
|
|
return (u32)t;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 15:33:01 +00:00
|
|
|
u64 sceKernelGetSystemTimeWide()
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-11-20 02:55:56 +00:00
|
|
|
u64 t = CoreTiming::GetGlobalTimeUsScaled();
|
2014-06-29 10:53:03 +00:00
|
|
|
VERBOSE_LOG(SCEKERNEL,"%i=sceKernelGetSystemTimeWide()",(u32)t);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(250);
|
2013-08-04 17:11:56 +00:00
|
|
|
hleReSchedule("system time");
|
2013-02-03 15:33:01 +00:00
|
|
|
return t;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-30 16:51:34 +00:00
|
|
|
int sceKernelUSec2SysClock(u32 usec, u32 clockPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-06-29 10:53:03 +00:00
|
|
|
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClock(%i, %08x)", usec, clockPtr);
|
2013-03-30 16:51:34 +00:00
|
|
|
if (Memory::IsValidAddress(clockPtr))
|
2013-10-21 08:57:00 +00:00
|
|
|
Memory::Write_U64((usec & 0xFFFFFFFFL), clockPtr);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(165);
|
2013-02-03 15:33:01 +00:00
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 21:14:19 +00:00
|
|
|
u64 sceKernelUSec2SysClockWide(u32 usec)
|
|
|
|
{
|
2014-06-29 10:53:03 +00:00
|
|
|
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClockWide(%i)", usec);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(150);
|
2013-03-30 17:08:23 +00:00
|
|
|
return usec;
|
2013-02-03 21:14:19 +00:00
|
|
|
}
|
|
|
|
|
2013-03-31 10:09:27 +00:00
|
|
|
int sceKernelSysClock2USec(u32 sysclockPtr, u32 highPtr, u32 lowPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USec(clock = %08x, lo = %08x, hi = %08x)", sysclockPtr, highPtr, lowPtr);
|
2013-03-31 10:09:27 +00:00
|
|
|
u64 time = Memory::Read_U64(sysclockPtr);
|
|
|
|
u32 highResult = (u32)(time / 1000000);
|
|
|
|
u32 lowResult = (u32)(time % 1000000);
|
|
|
|
if (Memory::IsValidAddress(highPtr))
|
|
|
|
Memory::Write_U32(highResult, highPtr);
|
|
|
|
if (Memory::IsValidAddress(lowPtr))
|
|
|
|
Memory::Write_U32(lowResult, lowPtr);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(415);
|
2013-02-03 04:42:16 +00:00
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-03 15:33:01 +00:00
|
|
|
int sceKernelSysClock2USecWide(u32 lowClock, u32 highClock, u32 lowPtr, u32 highPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-03-30 16:51:34 +00:00
|
|
|
u64 sysClock = lowClock | ((u64)highClock << 32);
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USecWide(clock = %llu, lo = %08x, hi = %08x)", sysClock, lowPtr, highPtr);
|
2013-03-30 16:51:34 +00:00
|
|
|
if (Memory::IsValidAddress(lowPtr)) {
|
|
|
|
Memory::Write_U32((u32)(sysClock / 1000000), lowPtr);
|
2013-03-30 17:08:23 +00:00
|
|
|
if (Memory::IsValidAddress(highPtr))
|
|
|
|
Memory::Write_U32((u32)(sysClock % 1000000), highPtr);
|
2013-03-30 16:51:34 +00:00
|
|
|
} else
|
2013-03-30 17:08:23 +00:00
|
|
|
if (Memory::IsValidAddress(highPtr))
|
|
|
|
Memory::Write_U32((int) sysClock, highPtr);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(385);
|
2013-02-03 15:33:01 +00:00
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 09:59:23 +00:00
|
|
|
u32 sceKernelLibcClock()
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-10-24 08:10:09 +00:00
|
|
|
u32 retVal = (u32) CoreTiming::GetGlobalTimeUs();
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcClock", retVal);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(330);
|
2013-08-04 17:11:56 +00:00
|
|
|
hleReSchedule("libc clock");
|
2012-11-20 09:59:23 +00:00
|
|
|
return retVal;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 05:27:18 +00:00
|
|
|
u32 sceKernelLibcTime(u32 outPtr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-10-24 08:10:09 +00:00
|
|
|
u32 t = (u32) start_time + (u32) (CoreTiming::GetGlobalTimeUs() / 1000000ULL);
|
2013-01-02 05:27:18 +00:00
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcTime(%08X)", t, outPtr);
|
2013-05-05 06:14:28 +00:00
|
|
|
// The PSP sure takes its sweet time on this function.
|
|
|
|
hleEatCycles(3385);
|
2013-01-02 05:27:18 +00:00
|
|
|
|
|
|
|
if (Memory::IsValidAddress(outPtr))
|
2013-01-02 09:09:44 +00:00
|
|
|
Memory::Write_U32(t, outPtr);
|
2013-01-02 08:35:31 +00:00
|
|
|
else if (outPtr != 0)
|
|
|
|
return 0;
|
2013-01-02 05:27:18 +00:00
|
|
|
|
2013-08-04 17:11:56 +00:00
|
|
|
hleReSchedule("libc time");
|
2013-01-02 09:09:44 +00:00
|
|
|
return t;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 06:14:28 +00:00
|
|
|
u32 sceKernelLibcGettimeofday(u32 timeAddr, u32 tzAddr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-05 06:14:28 +00:00
|
|
|
// TODO: tzAddr?
|
2013-05-05 17:12:29 +00:00
|
|
|
if (Memory::IsValidAddress(timeAddr))
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-07-25 06:58:45 +00:00
|
|
|
PSPTimeval *tv = (PSPTimeval *)Memory::GetPointer(timeAddr);
|
2013-05-05 17:12:29 +00:00
|
|
|
__RtcTimeOfDay(tv);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-09-07 20:02:55 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL,"sceKernelLibcGettimeofday(%08x, %08x)", timeAddr, tzAddr);
|
2013-05-05 06:14:28 +00:00
|
|
|
hleEatCycles(1885);
|
2013-08-04 17:11:56 +00:00
|
|
|
|
|
|
|
hleReSchedule("libc timeofday");
|
|
|
|
return 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2017-11-13 23:25:49 +00:00
|
|
|
|
2017-11-14 06:16:40 +00:00
|
|
|
std::string KernelTimeNowFormatted() {
|
2017-11-14 06:42:58 +00:00
|
|
|
time_t emulatedTime = (time_t)start_time + (u32)(CoreTiming::GetGlobalTimeUs() / 1000000ULL);
|
2017-11-14 06:16:40 +00:00
|
|
|
tm* timePtr = localtime(&emulatedTime);
|
2017-11-25 20:34:52 +00:00
|
|
|
bool DST = timePtr->tm_isdst != 0;
|
2017-11-14 06:16:40 +00:00
|
|
|
u8 seconds = timePtr->tm_sec;
|
|
|
|
u8 minutes = timePtr->tm_min;
|
|
|
|
u8 hours = timePtr->tm_hour;
|
|
|
|
if (DST)
|
|
|
|
hours = timePtr->tm_hour + 1;
|
|
|
|
u8 days = timePtr->tm_mday;
|
|
|
|
u8 months = timePtr->tm_mon + 1;
|
|
|
|
u16 years = timePtr->tm_year + 1900;
|
2017-11-14 08:46:31 +00:00
|
|
|
std::string timestamp = StringFromFormat("%04d-%02d-%02d_%02d-%02d-%02d", years, months, days, hours, minutes, seconds);
|
2017-11-14 06:16:40 +00:00
|
|
|
return timestamp;
|
2017-11-25 20:34:52 +00:00
|
|
|
}
|
2019-07-05 03:36:17 +00:00
|
|
|
|
|
|
|
void KernelTimeSetBase(int64_t seconds) {
|
|
|
|
start_time = (time_t)seconds;
|
|
|
|
}
|