2017-11-25 14:56:57 +01:00
|
|
|
// Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
2014-04-09 01:11:21 +02:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2014-04-09 01:11:21 +02:00
|
|
|
#pragma once
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01: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.
|
|
|
|
*
|
|
|
|
* The int cyclesLate that the callbacks get is how many cycles late it was.
|
|
|
|
* So to schedule a new event on a regular basis:
|
|
|
|
* inside callback:
|
|
|
|
* ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
|
|
|
|
*/
|
|
|
|
|
2016-09-20 17:21:23 +02:00
|
|
|
#include <functional>
|
2015-01-06 02:17:49 +01:00
|
|
|
#include <string>
|
2016-09-20 17:21:23 +02:00
|
|
|
#include "common/common_types.h"
|
2015-01-06 02:17:49 +01:00
|
|
|
|
2018-04-30 09:24:27 +02:00
|
|
|
namespace CoreTiming {
|
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
/**
|
|
|
|
* CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
|
|
|
|
* required to end slice -1 and start slice 0 before the first cycle of code is executed.
|
|
|
|
*/
|
2014-04-09 01:11:21 +02:00
|
|
|
void Init();
|
|
|
|
void Shutdown();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2015-01-06 02:17:49 +01:00
|
|
|
typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-09-30 18:25:49 +02:00
|
|
|
/**
|
2017-11-25 14:56:57 +01:00
|
|
|
* This should only be called from the emu thread, if you are calling it any other thread, you are
|
|
|
|
* doing something evil
|
|
|
|
*/
|
2014-04-09 01:11:21 +02:00
|
|
|
u64 GetTicks();
|
|
|
|
u64 GetIdleTicks();
|
2017-11-25 14:56:57 +01:00
|
|
|
void AddTicks(u64 ticks);
|
|
|
|
|
|
|
|
struct EventType;
|
2015-01-06 02:17:49 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-25 14:56:57 +01:00
|
|
|
* Returns the event_type identifier. if name is not unique, it will assert.
|
2015-01-06 02:17:49 +01:00
|
|
|
*/
|
2017-11-25 14:56:57 +01:00
|
|
|
EventType* RegisterEvent(const std::string& name, TimedCallback callback);
|
2014-04-09 01:11:21 +02:00
|
|
|
void UnregisterAllEvents();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2015-01-06 02:17:49 +01:00
|
|
|
/**
|
2017-11-25 14:56:57 +01:00
|
|
|
* After the first Advance, the slice lengths and the downcount will be reduced whenever an event
|
|
|
|
* is scheduled earlier than the current values.
|
|
|
|
* Scheduling from a callback will not update the downcount until the Advance() completes.
|
2015-01-06 02:17:49 +01:00
|
|
|
*/
|
2017-11-25 14:56:57 +01:00
|
|
|
void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata = 0);
|
2015-01-06 02:17:49 +01:00
|
|
|
|
|
|
|
/**
|
2017-11-25 14:56:57 +01:00
|
|
|
* This is to be called when outside of hle threads, such as the graphics thread, wants to
|
|
|
|
* schedule things to be executed on the main thread.
|
|
|
|
* Not that this doesn't change slice_length and thus events scheduled by this might be called
|
|
|
|
* with a delay of up to MAX_SLICE_LENGTH
|
2015-01-06 02:17:49 +01:00
|
|
|
*/
|
2017-11-25 14:56:57 +01:00
|
|
|
void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type, u64 userdata);
|
|
|
|
|
|
|
|
void UnscheduleEvent(const EventType* event_type, u64 userdata);
|
2015-01-06 02:17:49 +01:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
/// We only permit one event of each type in the queue at a time.
|
|
|
|
void RemoveEvent(const EventType* event_type);
|
|
|
|
void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
/** Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
|
|
|
|
* the previous timing slice and begins the next one, you must Advance from the previous
|
|
|
|
* slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
|
|
|
|
* Advance() is required to initialize the slice length before the first cycle of emulated
|
|
|
|
* instructions is executed.
|
|
|
|
*/
|
2014-04-09 01:11:21 +02:00
|
|
|
void Advance();
|
|
|
|
void MoveEvents();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2015-01-06 02:17:49 +01:00
|
|
|
/// Pretend that the main CPU has executed enough cycles to reach the next event.
|
2017-11-25 14:56:57 +01:00
|
|
|
void Idle();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
/// Clear all pending events. This should ONLY be done on exit.
|
2014-04-09 01:11:21 +02:00
|
|
|
void ClearPendingEvents();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
void ForceExceptionCheck(s64 cycles);
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
u64 GetGlobalTimeUs();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
int GetDowncount();
|
2013-10-02 01:10:47 +02:00
|
|
|
|
2017-11-25 14:56:57 +01:00
|
|
|
} // namespace CoreTiming
|