1
// Copyright (c) 2012- PPSSPP Project / Dolphin Project.
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.
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.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
21
#include "Common/CommonTypes.h"
23
// This is a system to schedule events into the emulated machine's future. Time is measured
24
// in main CPU clock cycles.
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.
29
// See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.
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:
34
// ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
38
//const int CPU_HZ = 222000000;
41
inline s64 msToCycles(int ms) {
42
return CPU_HZ / 1000 * ms;
45
inline s64 msToCycles(float ms) {
46
return (s64)(CPU_HZ * ms * (0.001f));
49
inline s64 msToCycles(double ms) {
50
return (s64)(CPU_HZ * ms * (0.001));
53
inline s64 usToCycles(float us) {
54
return (s64)(CPU_HZ * us * (0.000001f));
57
inline s64 usToCycles(int us) {
58
return (CPU_HZ / 1000000 * (s64)us);
61
inline s64 usToCycles(s64 us) {
62
return (CPU_HZ / 1000000 * us);
65
inline s64 usToCycles(u64 us) {
66
return (s64)(CPU_HZ / 1000000 * us);
69
inline s64 cyclesToUs(s64 cycles) {
70
return cycles / (CPU_HZ / 1000000);
78
typedef void (*MHzChangeCallback)();
79
typedef void (*TimedCallback)(u64 userdata, int cyclesLate);
83
u64 GetGlobalTimeUs();
84
u64 GetGlobalTimeUsScaled();
86
// Returns the event_type identifier.
87
int RegisterEvent(const char *name, TimedCallback callback);
89
void RestoreRegisterEvent(int event_type, const char *name, TimedCallback callback);
90
void UnregisterAllEvents();
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);
100
void RemoveEvent(int event_type);
101
void RemoveThreadsafeEvent(int event_type);
102
void RemoveAllEvents(int event_type);
103
bool IsScheduled(int event_type);
106
void ProcessFifoWaitEvents();
109
// Pretend that the main CPU has executed enough cycles to reach the next event.
110
void Idle(int maxIdle = 0);
112
// Clear all pending events. This should ONLY be done on exit or state load.
113
void ClearPendingEvents();
115
void LogPendingEvents();
117
// Warning: not included in save states.
118
void RegisterMHzChangeCallback(MHzChangeCallback callback);
120
std::string GetScheduledEventsSummary();
122
void DoState(PointerWrap &p);
124
void SetClockFrequencyMHz(int cpuMhz);
125
int GetClockFrequencyMHz();
126
extern int slicelength;
128
}; // end of namespace