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