2015-01-06 01:17:49 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project / Dolphin Project.
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-04-08 23:11:21 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
#pragma once
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-20 15:21:23 +00:00
|
|
|
#include <functional>
|
2015-01-06 01:17:49 +00:00
|
|
|
#include <string>
|
2016-09-20 15:21:23 +00:00
|
|
|
#include "common/common_types.h"
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2013-10-01 23:10:47 +00:00
|
|
|
// This is a system to schedule events into the emulated machine's future. Time is measured
|
|
|
|
// in main CPU clock cycles.
|
|
|
|
|
|
|
|
// To schedule an event, you first have to register its type. This is where you pass in the
|
|
|
|
// callback. You then schedule events using the type id you get back.
|
|
|
|
|
|
|
|
// See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
// The int cycles_late that the callbacks get is how many cycles late it was.
|
2013-10-01 23:10:47 +00:00
|
|
|
// So to schedule a new event on a regular basis:
|
|
|
|
// inside callback:
|
2015-01-06 01:17:49 +00:00
|
|
|
// ScheduleEvent(periodInCycles - cycles_late, callback, "whatever")
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-01-07 05:58:06 +00:00
|
|
|
constexpr int BASE_CLOCK_RATE = 383778816; // Switch clock speed is 384MHz docked
|
2013-10-01 23:10:47 +00:00
|
|
|
extern int g_clock_rate_arm11;
|
|
|
|
|
|
|
|
inline s64 msToCycles(int ms) {
|
2016-07-20 12:49:01 +00:00
|
|
|
return (s64)g_clock_rate_arm11 / 1000 * ms;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 msToCycles(float ms) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (s64)(g_clock_rate_arm11 * ms * (0.001f));
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 msToCycles(double ms) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (s64)(g_clock_rate_arm11 * ms * (0.001));
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 usToCycles(float us) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (s64)(g_clock_rate_arm11 * us * (0.000001f));
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 usToCycles(int us) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (g_clock_rate_arm11 / 1000000 * (s64)us);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 usToCycles(s64 us) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (g_clock_rate_arm11 / 1000000 * us);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 usToCycles(u64 us) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return (s64)(g_clock_rate_arm11 / 1000000 * us);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline s64 cyclesToUs(s64 cycles) {
|
2014-04-08 23:11:21 +00:00
|
|
|
return cycles / (g_clock_rate_arm11 / 1000000);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
inline u64 cyclesToMs(s64 cycles) {
|
|
|
|
return cycles / (g_clock_rate_arm11 / 1000);
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
namespace CoreTiming {
|
2014-04-08 23:11:21 +00:00
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
typedef void (*MHzChangeCallback)();
|
2015-01-06 01:17:49 +00:00
|
|
|
typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-09-30 16:25:49 +00:00
|
|
|
/**
|
|
|
|
* Advance the CPU core by the specified number of ticks (e.g. to simulate CPU execution time)
|
|
|
|
* @param ticks Number of ticks to advance the CPU core
|
|
|
|
*/
|
|
|
|
void AddTicks(u64 ticks);
|
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
u64 GetTicks();
|
|
|
|
u64 GetIdleTicks();
|
2015-01-06 01:17:49 +00:00
|
|
|
u64 GetGlobalTimeUs();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers an event type with the specified name and callback
|
|
|
|
* @param name Name of the event type
|
|
|
|
* @param callback Function that will execute when this event fires
|
|
|
|
* @returns An identifier for the event type that was registered
|
|
|
|
*/
|
|
|
|
int RegisterEvent(const char* name, TimedCallback callback);
|
|
|
|
/// For save states.
|
2016-09-18 00:38:01 +00:00
|
|
|
void RestoreRegisterEvent(int event_type, const char* name, TimedCallback callback);
|
2014-04-08 23:11:21 +00:00
|
|
|
void UnregisterAllEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
/// userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk,
|
|
|
|
/// when we implement state saves.
|
|
|
|
/**
|
2015-05-25 18:34:09 +00:00
|
|
|
* Schedules an event to run after the specified number of cycles,
|
2015-01-06 01:17:49 +00:00
|
|
|
* with an optional parameter to be passed to the callback handler.
|
|
|
|
* This must be run ONLY from within the cpu thread.
|
|
|
|
* @param cycles_into_future The number of cycles after which this event will be fired
|
|
|
|
* @param event_type The event type to fire, as returned from RegisterEvent
|
|
|
|
* @param userdata Optional parameter to pass to the callback when fired
|
|
|
|
*/
|
|
|
|
void ScheduleEvent(s64 cycles_into_future, int event_type, u64 userdata = 0);
|
|
|
|
|
|
|
|
void ScheduleEvent_Threadsafe(s64 cycles_into_future, int event_type, u64 userdata = 0);
|
2014-04-08 23:11:21 +00:00
|
|
|
void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata = 0);
|
2015-01-06 01:17:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unschedules an event with the specified type and userdata
|
|
|
|
* @param event_type The type of event to unschedule, as returned from RegisterEvent
|
|
|
|
* @param userdata The userdata that identifies this event, as passed to ScheduleEvent
|
|
|
|
* @returns The remaining ticks until the next invocation of the event callback
|
|
|
|
*/
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 UnscheduleEvent(int event_type, u64 userdata);
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata);
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
void RemoveEvent(int event_type);
|
|
|
|
void RemoveThreadsafeEvent(int event_type);
|
|
|
|
void RemoveAllEvents(int event_type);
|
|
|
|
bool IsScheduled(int event_type);
|
2015-01-06 01:17:49 +00:00
|
|
|
/// Runs any pending events and updates downcount for the next slice of cycles
|
2014-04-08 23:11:21 +00:00
|
|
|
void Advance();
|
|
|
|
void MoveEvents();
|
|
|
|
void ProcessFifoWaitEvents();
|
2015-01-06 01:17:49 +00:00
|
|
|
void ForceCheck();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
/// Pretend that the main CPU has executed enough cycles to reach the next event.
|
2014-04-08 23:11:21 +00:00
|
|
|
void Idle(int maxIdle = 0);
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
/// Clear all pending events. This should ONLY be done on exit or state load.
|
2014-04-08 23:11:21 +00:00
|
|
|
void ClearPendingEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
void LogPendingEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
/// Warning: not included in save states.
|
2016-09-18 00:38:01 +00:00
|
|
|
void RegisterAdvanceCallback(void (*callback)(int cycles_executed));
|
2015-01-06 01:17:49 +00:00
|
|
|
void RegisterMHzChangeCallback(MHzChangeCallback callback);
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
std::string GetScheduledEventsSummary();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void SetClockFrequencyMHz(int cpu_mhz);
|
2014-04-08 23:11:21 +00:00
|
|
|
int GetClockFrequencyMHz();
|
2015-01-06 01:17:49 +00:00
|
|
|
extern int g_slice_length;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-11-18 13:27:16 +00:00
|
|
|
} // namespace
|