/
sceKernelTime.cpp
177 lines (150 loc) · 4.62 KB
/
sceKernelTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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.
// 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>
#endif
#include <time.h>
#include "Common/ChunkFile.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"
// The time when the game started.
static time_t start_time;
void __KernelTimeInit()
{
time(&start_time);
}
void __KernelTimeDoState(PointerWrap &p)
{
auto s = p.Section("sceKernelTime", 1, 2);
if (!s)
return;
if (s < 2) {
p.Do(start_time);
} else {
u64 t = start_time;
p.Do(t);
start_time = (time_t)t;
}
}
int sceKernelGetSystemTime(u32 sysclockPtr)
{
u64 t = CoreTiming::GetGlobalTimeUs();
if (Memory::IsValidAddress(sysclockPtr))
Memory::Write_U64(t, sysclockPtr);
VERBOSE_LOG(SCEKERNEL, "sceKernelGetSystemTime(out:%16llx)", t);
hleEatCycles(265);
hleReSchedule("system time");
return 0;
}
u32 sceKernelGetSystemTimeLow()
{
// This clock should tick at 1 Mhz.
u64 t = CoreTiming::GetGlobalTimeUs();
VERBOSE_LOG(SCEKERNEL,"%08x=sceKernelGetSystemTimeLow()",(u32)t);
hleEatCycles(165);
hleReSchedule("system time");
return (u32)t;
}
u64 sceKernelGetSystemTimeWide()
{
u64 t = CoreTiming::GetGlobalTimeUsScaled();
VERBOSE_LOG(SCEKERNEL,"%i=sceKernelGetSystemTimeWide()",(u32)t);
hleEatCycles(250);
hleReSchedule("system time");
return t;
}
int sceKernelUSec2SysClock(u32 usec, u32 clockPtr)
{
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClock(%i, %08x)", usec, clockPtr);
if (Memory::IsValidAddress(clockPtr))
Memory::Write_U64((usec & 0xFFFFFFFFL), clockPtr);
hleEatCycles(165);
return 0;
}
u64 sceKernelUSec2SysClockWide(u32 usec)
{
VERBOSE_LOG(SCEKERNEL, "sceKernelUSec2SysClockWide(%i)", usec);
hleEatCycles(150);
return usec;
}
int sceKernelSysClock2USec(u32 sysclockPtr, u32 highPtr, u32 lowPtr)
{
DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USec(clock = %08x, lo = %08x, hi = %08x)", sysclockPtr, highPtr, lowPtr);
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;
}
int sceKernelSysClock2USecWide(u32 lowClock, u32 highClock, u32 lowPtr, u32 highPtr)
{
u64 sysClock = lowClock | ((u64)highClock << 32);
DEBUG_LOG(SCEKERNEL, "sceKernelSysClock2USecWide(clock = %llu, lo = %08x, hi = %08x)", sysClock, lowPtr, highPtr);
if (Memory::IsValidAddress(lowPtr)) {
Memory::Write_U32((u32)(sysClock / 1000000), lowPtr);
if (Memory::IsValidAddress(highPtr))
Memory::Write_U32((u32)(sysClock % 1000000), highPtr);
} else
if (Memory::IsValidAddress(highPtr))
Memory::Write_U32((int) sysClock, highPtr);
hleEatCycles(385);
return 0;
}
u32 sceKernelLibcClock()
{
u32 retVal = (u32) CoreTiming::GetGlobalTimeUs();
DEBUG_LOG(SCEKERNEL, "%i = sceKernelLibcClock", retVal);
hleEatCycles(330);
hleReSchedule("libc clock");
return retVal;
}
u32 sceKernelLibcTime(u32 outPtr)
{
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;
}
u32 sceKernelLibcGettimeofday(u32 timeAddr, u32 tzAddr)
{
// TODO: tzAddr?
if (Memory::IsValidAddress(timeAddr))
{
PSPTimeval *tv = (PSPTimeval *)Memory::GetPointer(timeAddr);
__RtcTimeOfDay(tv);
}
DEBUG_LOG(SCEKERNEL,"sceKernelLibcGettimeofday(%08x, %08x)", timeAddr, tzAddr);
hleEatCycles(1885);
hleReSchedule("libc timeofday");
return 0;
}
u32 KernelTimeNow() {
return (u32)start_time + (u32)(CoreTiming::GetGlobalTimeUs() / 1000000ULL);
}