ppsspp/Core/HLE/sceKernelTime.cpp

196 lines
5.3 KiB
C++
Raw Normal View History

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
// 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
#include "Common/CommonWindows.h"
#else
#include <sys/time.h>
2012-11-01 15:19:01 +00:00
#endif
#include <time.h>
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Core/CoreTiming.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/sceKernel.h"
#include "Core/HLE/sceKernelTime.h"
#include "Core/HLE/sceKernelThread.h"
#include "Core/HLE/sceRtc.h"
#include "Core/MemMap.h"
#include "StringUtils.h"
2012-11-01 15:19:01 +00:00
// The time when the game started.
static time_t start_time;
2012-11-01 15:19:01 +00:00
void __KernelTimeInit()
{
time(&start_time);
}
void __KernelTimeDoState(PointerWrap &p)
{
auto s = p.Section("sceKernelTime", 1, 2);
if (!s)
return;
if (s < 2) {
Do(p, start_time);
} else {
u64 t = start_time;
Do(p, t);
start_time = (time_t)t;
}
}
int sceKernelGetSystemTime(u32 sysclockPtr)
2012-11-01 15:19:01 +00:00
{
u64 t = CoreTiming::GetGlobalTimeUs();
2013-03-30 16:51:34 +00:00
if (Memory::IsValidAddress(sysclockPtr))
Memory::Write_U64(t, sysclockPtr);
VERBOSE_LOG(SCEKERNEL, "sceKernelGetSystemTime(out:%16llx)", t);
hleEatCycles(265);
hleReSchedule("system time");
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.
u64 t = CoreTiming::GetGlobalTimeUs();
VERBOSE_LOG(SCEKERNEL,"%08x=sceKernelGetSystemTimeLow()",(u32)t);
hleEatCycles(165);
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();
VERBOSE_LOG(SCEKERNEL,"%i=sceKernelGetSystemTimeWide()",(u32)t);
hleEatCycles(250);
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
{
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClock(%i, %08x)", usec, clockPtr);
2013-03-30 16:51:34 +00:00
if (Memory::IsValidAddress(clockPtr))
Memory::Write_U64((usec & 0xFFFFFFFFL), clockPtr);
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)
{
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClockWide(%i)", usec);
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
{
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);
hleEatCycles(415);
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);
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);
hleEatCycles(385);
2013-02-03 15:33:01 +00:00
return 0;
2012-11-01 15:19:01 +00:00
}
u32 sceKernelLibcClock()
2012-11-01 15:19:01 +00:00
{
u32 retVal = (u32) CoreTiming::GetGlobalTimeUs();
DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcClock", retVal);
hleEatCycles(330);
hleReSchedule("libc clock");
return retVal;
2012-11-01 15:19:01 +00:00
}
u32 sceKernelLibcTime(u32 outPtr)
2012-11-01 15:19:01 +00:00
{
u32 t = (u32) start_time + (u32) (CoreTiming::GetGlobalTimeUs() / 1000000ULL);
DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcTime(%08X)", t, outPtr);
// The PSP sure takes its sweet time on this function.
hleEatCycles(3385);
if (Memory::IsValidAddress(outPtr))
Memory::Write_U32(t, outPtr);
else if (outPtr != 0)
return 0;
hleReSchedule("libc time");
return t;
2012-11-01 15:19:01 +00:00
}
u32 sceKernelLibcGettimeofday(u32 timeAddr, u32 tzAddr)
2012-11-01 15:19:01 +00:00
{
// TODO: tzAddr?
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);
__RtcTimeOfDay(tv);
}
2012-11-01 15:19:01 +00:00
DEBUG_LOG(SCEKERNEL,"sceKernelLibcGettimeofday(%08x, %08x)", timeAddr, tzAddr);
hleEatCycles(1885);
hleReSchedule("libc timeofday");
return 0;
2012-11-01 15:19:01 +00:00
}
std::string KernelTimeNowFormatted() {
2017-11-14 06:42:58 +00:00
time_t emulatedTime = (time_t)start_time + (u32)(CoreTiming::GetGlobalTimeUs() / 1000000ULL);
tm* timePtr = localtime(&emulatedTime);
2017-11-25 20:34:52 +00:00
bool DST = timePtr->tm_isdst != 0;
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);
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;
}