// Copyright (c) 2012- PPSSPP Project / Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <string>20#include <vector>21#include "Common/CommonTypes.h"22#include "Common/Data/Collections/LinkedList.h"2324// This is a system to schedule events into the emulated machine's future. Time is measured25// in main CPU clock cycles.2627// To schedule an event, you first have to register its type. This is where you pass in the28// callback. You then schedule events using the type id you get back.2930// See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.3132// The int cyclesLate that the callbacks get is how many cycles late it was.33// So to schedule a new event on a regular basis:34// inside callback:35// ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")3637class PointerWrap;3839//const int CPU_HZ = 222000000;40extern int CPU_HZ;4142inline s64 msToCycles(int ms) {43return CPU_HZ / 1000 * ms;44}4546inline s64 msToCycles(float ms) {47return (s64)(CPU_HZ * ms * (0.001f));48}4950inline s64 msToCycles(double ms) {51return (s64)(CPU_HZ * ms * (0.001));52}5354inline s64 usToCycles(float us) {55return (s64)(CPU_HZ * us * (0.000001f));56}5758inline s64 usToCycles(int us) {59return (CPU_HZ / 1000000 * (s64)us);60}6162inline s64 usToCycles(s64 us) {63return (CPU_HZ / 1000000 * us);64}6566inline s64 usToCycles(u64 us) {67return (s64)(CPU_HZ / 1000000 * us);68}6970inline s64 cyclesToUs(s64 cycles) {71return (cycles * 1000000) / CPU_HZ;72}7374namespace CoreTiming {75typedef void (*MHzChangeCallback)();76typedef void (*TimedCallback)(u64 userdata, int cyclesLate);7778struct EventType {79TimedCallback callback;80const char *name;81};8283struct BaseEvent {84s64 time;85u64 userdata;86int type;87};88typedef LinkedListItem<BaseEvent> Event;8990void Init();91void Shutdown();9293u64 GetTicks();94u64 GetIdleTicks();95u64 GetGlobalTimeUs();96u64 GetGlobalTimeUsScaled();9798// Returns the event_type identifier.99int RegisterEvent(const char *name, TimedCallback callback);100101// For save states.102void RestoreRegisterEvent(int &event_type, const char *name, TimedCallback callback);103void UnregisterAllEvents();104105// userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk,106// when we implement state saves.107void ScheduleEvent(s64 cyclesIntoFuture, int event_type, u64 userdata=0);108s64 UnscheduleEvent(int event_type, u64 userdata);109110const std::vector<EventType> &GetEventTypes();111const Event *GetFirstEvent();112void RemoveEvent(int event_type);113bool IsScheduled(int event_type);114void Advance();115void ForceCheck();116117// Pretend that the main CPU has executed enough cycles to reach the next event.118void Idle(int maxIdle = 0);119120// Clear all pending events. This should ONLY be done on exit or state load.121void ClearPendingEvents();122123void LogPendingEvents();124125// Warning: not included in save states.126void RegisterMHzChangeCallback(MHzChangeCallback callback);127128std::string GetScheduledEventsSummary();129130void DoState(PointerWrap &p);131132void SetClockFrequencyHz(int cpuHz);133int GetClockFrequencyHz();134135// TODO: Add accessors?136extern int slicelength;137138}; // end of namespace139140141