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
msToCycles(int ms)41 inline s64 msToCycles(int ms) {
42 return CPU_HZ / 1000 * ms;
43 }
44
msToCycles(float ms)45 inline s64 msToCycles(float ms) {
46 return (s64)(CPU_HZ * ms * (0.001f));
47 }
48
msToCycles(double ms)49 inline s64 msToCycles(double ms) {
50 return (s64)(CPU_HZ * ms * (0.001));
51 }
52
usToCycles(float us)53 inline s64 usToCycles(float us) {
54 return (s64)(CPU_HZ * us * (0.000001f));
55 }
56
usToCycles(int us)57 inline s64 usToCycles(int us) {
58 return (CPU_HZ / 1000000 * (s64)us);
59 }
60
usToCycles(s64 us)61 inline s64 usToCycles(s64 us) {
62 return (CPU_HZ / 1000000 * us);
63 }
64
usToCycles(u64 us)65 inline s64 usToCycles(u64 us) {
66 return (s64)(CPU_HZ / 1000000 * us);
67 }
68
cyclesToUs(s64 cycles)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 void ScheduleEvent_Threadsafe(s64 cyclesIntoFuture, int event_type, u64 userdata=0);
96 void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata=0);
97 s64 UnscheduleEvent(int event_type, u64 userdata);
98 s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata);
99
100 void RemoveEvent(int event_type);
101 void RemoveThreadsafeEvent(int event_type);
102 void RemoveAllEvents(int event_type);
103 bool IsScheduled(int event_type);
104 void Advance();
105 void MoveEvents();
106 void ProcessFifoWaitEvents();
107 void ForceCheck();
108
109 // Pretend that the main CPU has executed enough cycles to reach the next event.
110 void Idle(int maxIdle = 0);
111
112 // Clear all pending events. This should ONLY be done on exit or state load.
113 void ClearPendingEvents();
114
115 void LogPendingEvents();
116
117 // Warning: not included in save states.
118 void RegisterMHzChangeCallback(MHzChangeCallback callback);
119
120 std::string GetScheduledEventsSummary();
121
122 void DoState(PointerWrap &p);
123
124 void SetClockFrequencyHz(int cpuHz);
125 int GetClockFrequencyHz();
126 extern int slicelength;
127
128 }; // end of namespace
129