1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #ifdef _WIN32
19 #include "Common/CommonWindows.h"
20 #else
21 #include <sys/time.h>
22 #endif
23 
24 #include <time.h>
25 
26 #include "Common/Serialize/Serializer.h"
27 #include "Common/Serialize/SerializeFuncs.h"
28 #include "Core/CoreTiming.h"
29 #include "Core/HLE/HLE.h"
30 #include "Core/HLE/sceKernel.h"
31 #include "Core/HLE/sceKernelTime.h"
32 #include "Core/HLE/sceKernelThread.h"
33 #include "Core/HLE/sceRtc.h"
34 #include "Core/MemMap.h"
35 #include "Core/System.h"
36 #include "StringUtils.h"
37 
38 // The time when the game started.
39 static time_t start_time;
40 
__KernelTimeInit()41 void __KernelTimeInit()
42 {
43 	time(&start_time);
44 	if (PSP_CoreParameter().compat.flags().DateLimited) {
45 		// Car Jack Streets(NPUZ00043) requires that the date cannot exceed a certain time.
46 		// 2011 year makes it work fine.
47 		tm *tm;
48 		tm = localtime(&start_time);
49 		tm->tm_year = 111;// 2011 year.
50 		start_time = mktime(tm);
51 	}
52 }
53 
__KernelTimeDoState(PointerWrap & p)54 void __KernelTimeDoState(PointerWrap &p)
55 {
56 	auto s = p.Section("sceKernelTime", 1, 2);
57 	if (!s)
58 		return;
59 
60 	if (s < 2) {
61 		Do(p, start_time);
62 	} else {
63 		u64 t = start_time;
64 		Do(p, t);
65 		start_time = (time_t)t;
66 	}
67 }
68 
sceKernelGetSystemTime(u32 sysclockPtr)69 int sceKernelGetSystemTime(u32 sysclockPtr)
70 {
71 	u64 t = CoreTiming::GetGlobalTimeUs();
72 	if (Memory::IsValidAddress(sysclockPtr))
73 		Memory::Write_U64(t, sysclockPtr);
74 	VERBOSE_LOG(SCEKERNEL, "sceKernelGetSystemTime(out:%16llx)", t);
75 	hleEatCycles(265);
76 	hleReSchedule("system time");
77 	return 0;
78 }
79 
sceKernelGetSystemTimeLow()80 u32 sceKernelGetSystemTimeLow()
81 {
82 	// This clock should tick at 1 Mhz.
83 	u64 t = CoreTiming::GetGlobalTimeUs();
84 	VERBOSE_LOG(SCEKERNEL,"%08x=sceKernelGetSystemTimeLow()",(u32)t);
85 	hleEatCycles(165);
86 	hleReSchedule("system time");
87 	return (u32)t;
88 }
89 
sceKernelGetSystemTimeWide()90 u64 sceKernelGetSystemTimeWide()
91 {
92 	u64 t = CoreTiming::GetGlobalTimeUsScaled();
93 	VERBOSE_LOG(SCEKERNEL,"%i=sceKernelGetSystemTimeWide()",(u32)t);
94 	hleEatCycles(250);
95 	hleReSchedule("system time");
96 	return t;
97 }
98 
sceKernelUSec2SysClock(u32 usec,u32 clockPtr)99 int sceKernelUSec2SysClock(u32 usec, u32 clockPtr)
100 {
101 	VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClock(%i, %08x)", usec, clockPtr);
102 	if (Memory::IsValidAddress(clockPtr))
103 		Memory::Write_U64((usec & 0xFFFFFFFFL), clockPtr);
104 	hleEatCycles(165);
105 	return 0;
106 }
107 
sceKernelUSec2SysClockWide(u32 usec)108 u64 sceKernelUSec2SysClockWide(u32 usec)
109 {
110 	VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClockWide(%i)", usec);
111 	hleEatCycles(150);
112 	return usec;
113 }
114 
sceKernelSysClock2USec(u32 sysclockPtr,u32 highPtr,u32 lowPtr)115 int sceKernelSysClock2USec(u32 sysclockPtr, u32 highPtr, u32 lowPtr)
116 {
117 	DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USec(clock = %08x, lo = %08x, hi = %08x)", sysclockPtr, highPtr, lowPtr);
118 	u64 time = Memory::Read_U64(sysclockPtr);
119 	u32 highResult = (u32)(time / 1000000);
120 	u32 lowResult = (u32)(time % 1000000);
121 	if (Memory::IsValidAddress(highPtr))
122 		Memory::Write_U32(highResult, highPtr);
123 	if (Memory::IsValidAddress(lowPtr))
124 		Memory::Write_U32(lowResult, lowPtr);
125 	hleEatCycles(415);
126 	return 0;
127 }
128 
sceKernelSysClock2USecWide(u32 lowClock,u32 highClock,u32 lowPtr,u32 highPtr)129 int sceKernelSysClock2USecWide(u32 lowClock, u32 highClock, u32 lowPtr, u32 highPtr)
130 {
131 	u64 sysClock = lowClock | ((u64)highClock << 32);
132 	DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USecWide(clock = %llu, lo = %08x, hi = %08x)", sysClock, lowPtr, highPtr);
133 	if (Memory::IsValidAddress(lowPtr)) {
134 		Memory::Write_U32((u32)(sysClock / 1000000), lowPtr);
135 		if (Memory::IsValidAddress(highPtr))
136 			Memory::Write_U32((u32)(sysClock % 1000000), highPtr);
137 	} else
138 		if (Memory::IsValidAddress(highPtr))
139 			Memory::Write_U32((int) sysClock, highPtr);
140 	hleEatCycles(385);
141 	return 0;
142 }
143 
sceKernelLibcClock()144 u32 sceKernelLibcClock()
145 {
146 	u32 retVal = (u32) CoreTiming::GetGlobalTimeUs();
147 	DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcClock", retVal);
148 	hleEatCycles(330);
149 	hleReSchedule("libc clock");
150 	return retVal;
151 }
152 
sceKernelLibcTime(u32 outPtr)153 u32 sceKernelLibcTime(u32 outPtr)
154 {
155 	u32 t = (u32) start_time + (u32) (CoreTiming::GetGlobalTimeUs() / 1000000ULL);
156 
157 	DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcTime(%08X)", t, outPtr);
158 	// The PSP sure takes its sweet time on this function.
159 	hleEatCycles(3385);
160 
161 	if (Memory::IsValidAddress(outPtr))
162 		Memory::Write_U32(t, outPtr);
163 	else if (outPtr != 0)
164 		return 0;
165 
166 	hleReSchedule("libc time");
167 	return t;
168 }
169 
sceKernelLibcGettimeofday(u32 timeAddr,u32 tzAddr)170 u32 sceKernelLibcGettimeofday(u32 timeAddr, u32 tzAddr)
171 {
172 	// TODO: tzAddr?
173 	if (Memory::IsValidAddress(timeAddr))
174 	{
175 		PSPTimeval *tv = (PSPTimeval *)Memory::GetPointer(timeAddr);
176 		__RtcTimeOfDay(tv);
177 	}
178 
179 	DEBUG_LOG(SCEKERNEL,"sceKernelLibcGettimeofday(%08x, %08x)", timeAddr, tzAddr);
180 	hleEatCycles(1885);
181 
182 	hleReSchedule("libc timeofday");
183 	return 0;
184 }
185 
KernelTimeNowFormatted()186 std::string KernelTimeNowFormatted() {
187 	time_t emulatedTime = (time_t)start_time + (u32)(CoreTiming::GetGlobalTimeUs() / 1000000ULL);
188 	tm* timePtr = localtime(&emulatedTime);
189 	bool DST = timePtr->tm_isdst != 0;
190 	u8 seconds = timePtr->tm_sec;
191 	u8 minutes = timePtr->tm_min;
192 	u8 hours = timePtr->tm_hour;
193 	if (DST)
194 		hours = timePtr->tm_hour + 1;
195 	u8 days = timePtr->tm_mday;
196 	u8 months = timePtr->tm_mon + 1;
197 	u16 years = timePtr->tm_year + 1900;
198 	std::string timestamp = StringFromFormat("%04d-%02d-%02d_%02d-%02d-%02d", years, months, days, hours, minutes, seconds);
199 	return timestamp;
200 }
201 
KernelTimeSetBase(int64_t seconds)202 void KernelTimeSetBase(int64_t seconds) {
203 	start_time = (time_t)seconds;
204 }
205