CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/CoreTiming.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project / Dolphin 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
#pragma once
19
20
#include <string>
21
#include "Common/CommonTypes.h"
22
23
// This is a system to schedule events into the emulated machine's future. Time is measured
24
// in main CPU clock cycles.
25
26
// To schedule an event, you first have to register its type. This is where you pass in the
27
// callback. You then schedule events using the type id you get back.
28
29
// See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.
30
31
// The int cyclesLate that the callbacks get is how many cycles late it was.
32
// So to schedule a new event on a regular basis:
33
// inside callback:
34
// ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
35
36
class PointerWrap;
37
38
//const int CPU_HZ = 222000000;
39
extern int CPU_HZ;
40
41
inline s64 msToCycles(int ms) {
42
return CPU_HZ / 1000 * ms;
43
}
44
45
inline s64 msToCycles(float ms) {
46
return (s64)(CPU_HZ * ms * (0.001f));
47
}
48
49
inline s64 msToCycles(double ms) {
50
return (s64)(CPU_HZ * ms * (0.001));
51
}
52
53
inline s64 usToCycles(float us) {
54
return (s64)(CPU_HZ * us * (0.000001f));
55
}
56
57
inline s64 usToCycles(int us) {
58
return (CPU_HZ / 1000000 * (s64)us);
59
}
60
61
inline s64 usToCycles(s64 us) {
62
return (CPU_HZ / 1000000 * us);
63
}
64
65
inline s64 usToCycles(u64 us) {
66
return (s64)(CPU_HZ / 1000000 * us);
67
}
68
69
inline s64 cyclesToUs(s64 cycles) {
70
return (cycles * 1000000) / CPU_HZ;
71
}
72
73
namespace CoreTiming
74
{
75
void Init();
76
void Shutdown();
77
78
typedef void (*MHzChangeCallback)();
79
typedef void (*TimedCallback)(u64 userdata, int cyclesLate);
80
81
u64 GetTicks();
82
u64 GetIdleTicks();
83
u64 GetGlobalTimeUs();
84
u64 GetGlobalTimeUsScaled();
85
86
// Returns the event_type identifier.
87
int RegisterEvent(const char *name, TimedCallback callback);
88
// For save states.
89
void RestoreRegisterEvent(int &event_type, const char *name, TimedCallback callback);
90
void UnregisterAllEvents();
91
92
// userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk,
93
// when we implement state saves.
94
void ScheduleEvent(s64 cyclesIntoFuture, int event_type, u64 userdata=0);
95
s64 UnscheduleEvent(int event_type, u64 userdata);
96
97
void RemoveEvent(int event_type);
98
bool IsScheduled(int event_type);
99
void Advance();
100
void ForceCheck();
101
102
// Pretend that the main CPU has executed enough cycles to reach the next event.
103
void Idle(int maxIdle = 0);
104
105
// Clear all pending events. This should ONLY be done on exit or state load.
106
void ClearPendingEvents();
107
108
void LogPendingEvents();
109
110
// Warning: not included in save states.
111
void RegisterMHzChangeCallback(MHzChangeCallback callback);
112
113
std::string GetScheduledEventsSummary();
114
115
void DoState(PointerWrap &p);
116
117
void SetClockFrequencyHz(int cpuHz);
118
int GetClockFrequencyHz();
119
extern int slicelength;
120
121
}; // end of namespace
122
123