2017-11-25 13:56:57 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
2014-04-08 23:11:21 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
#include <algorithm>
|
2015-09-16 12:38:12 +00:00
|
|
|
#include <cinttypes>
|
2017-11-25 13:56:57 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include "common/assert.h"
|
2015-06-21 14:11:32 +00:00
|
|
|
#include "common/logging/log.h"
|
2018-10-27 19:53:20 +00:00
|
|
|
#include "core/core_timing.h"
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
namespace Core {
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2020-01-12 20:01:29 +00:00
|
|
|
Timing* Timing::deserializing = nullptr;
|
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Sort by time, unless the times are the same, in which case sort by the order added to the queue
|
2018-10-27 19:53:20 +00:00
|
|
|
bool Timing::Event::operator>(const Event& right) const {
|
|
|
|
return std::tie(time, fifo_order) > std::tie(right.time, right.fifo_order);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
bool Timing::Event::operator<(const Event& right) const {
|
|
|
|
return std::tie(time, fifo_order) < std::tie(right.time, right.fifo_order);
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
TimingEventType* Timing::RegisterEvent(const std::string& name, TimedCallback callback) {
|
2017-11-25 13:56:57 +00:00
|
|
|
// check for existing type with same name.
|
|
|
|
// we want event type names to remain unique so that we can use them for serialization.
|
2020-01-12 00:24:44 +00:00
|
|
|
auto info = event_types.emplace(name, TimingEventType{});
|
2018-10-27 19:53:20 +00:00
|
|
|
TimingEventType* event_type = &info.first->second;
|
2017-11-25 13:56:57 +00:00
|
|
|
event_type->name = &info.first->first;
|
2020-01-12 20:01:29 +00:00
|
|
|
if (callback != nullptr) {
|
|
|
|
event_type->callback = callback;
|
|
|
|
}
|
2017-11-25 13:56:57 +00:00
|
|
|
return event_type;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
Timing::~Timing() {
|
2014-04-08 23:11:21 +00:00
|
|
|
MoveEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
u64 Timing::GetTicks() const {
|
2017-11-25 13:56:57 +00:00
|
|
|
u64 ticks = static_cast<u64>(global_timer);
|
|
|
|
if (!is_global_timer_sane) {
|
|
|
|
ticks += slice_length - downcount;
|
2017-09-30 16:25:49 +00:00
|
|
|
}
|
2017-11-25 13:56:57 +00:00
|
|
|
return ticks;
|
2017-09-30 16:25:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::AddTicks(u64 ticks) {
|
2017-11-25 13:56:57 +00:00
|
|
|
downcount -= ticks;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
u64 Timing::GetIdleTicks() const {
|
2017-11-25 13:56:57 +00:00
|
|
|
return static_cast<u64>(idled_cycles);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type,
|
|
|
|
u64 userdata) {
|
2017-11-25 13:56:57 +00:00
|
|
|
ASSERT(event_type != nullptr);
|
|
|
|
s64 timeout = GetTicks() + cycles_into_future;
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// If this event needs to be scheduled before the next advance(), force one early
|
|
|
|
if (!is_global_timer_sane)
|
|
|
|
ForceExceptionCheck(cycles_into_future);
|
2014-04-08 23:11:21 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
|
2018-08-06 01:15:13 +00:00
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::ScheduleEventThreadsafe(s64 cycles_into_future, const TimingEventType* event_type,
|
|
|
|
u64 userdata) {
|
2017-11-25 13:56:57 +00:00
|
|
|
ts_queue.Push(Event{global_timer + cycles_into_future, 0, userdata, event_type});
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::UnscheduleEvent(const TimingEventType* event_type, u64 userdata) {
|
2017-11-25 13:56:57 +00:00
|
|
|
auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
|
|
|
|
return e.type == event_type && e.userdata == userdata;
|
|
|
|
});
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
2018-08-06 01:15:13 +00:00
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::RemoveEvent(const TimingEventType* event_type) {
|
2017-11-25 13:56:57 +00:00
|
|
|
auto itr = std::remove_if(event_queue.begin(), event_queue.end(),
|
|
|
|
[&](const Event& e) { return e.type == event_type; });
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
// Removing random items breaks the invariant so we have to re-establish it.
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
2018-08-06 01:15:13 +00:00
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::RemoveNormalAndThreadsafeEvent(const TimingEventType* event_type) {
|
2017-11-25 13:56:57 +00:00
|
|
|
MoveEvents();
|
2014-04-08 23:11:21 +00:00
|
|
|
RemoveEvent(event_type);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::ForceExceptionCheck(s64 cycles) {
|
2017-11-25 13:56:57 +00:00
|
|
|
cycles = std::max<s64>(0, cycles);
|
|
|
|
if (downcount > cycles) {
|
2018-07-23 21:08:14 +00:00
|
|
|
slice_length -= downcount - cycles;
|
|
|
|
downcount = cycles;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::MoveEvents() {
|
2017-11-25 13:56:57 +00:00
|
|
|
for (Event ev; ts_queue.Pop(ev);) {
|
|
|
|
ev.fifo_order = event_fifo_id++;
|
|
|
|
event_queue.emplace_back(std::move(ev));
|
2018-08-06 01:15:13 +00:00
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::Advance() {
|
2017-11-25 13:56:57 +00:00
|
|
|
MoveEvents();
|
|
|
|
|
2018-07-23 21:08:14 +00:00
|
|
|
s64 cycles_executed = slice_length - downcount;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer += cycles_executed;
|
2017-11-25 13:56:57 +00:00
|
|
|
slice_length = MAX_SLICE_LENGTH;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = true;
|
|
|
|
|
|
|
|
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
|
|
|
|
Event evt = std::move(event_queue.front());
|
2018-08-06 01:15:13 +00:00
|
|
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
2017-11-25 13:56:57 +00:00
|
|
|
event_queue.pop_back();
|
2020-01-12 00:24:44 +00:00
|
|
|
if (event_types.find(*evt.type->name) == event_types.end()) {
|
2020-01-17 01:01:18 +00:00
|
|
|
LOG_ERROR(Core, "Unknown queued event {}", *evt.type->name);
|
|
|
|
} else if (evt.type->callback == nullptr) {
|
|
|
|
LOG_ERROR(Core, "Event '{}' has no callback", *evt.type->name);
|
2020-01-12 00:24:44 +00:00
|
|
|
}
|
2020-01-12 20:01:29 +00:00
|
|
|
if (evt.type->callback != nullptr) {
|
|
|
|
evt.type->callback(evt.userdata, global_timer - evt.time);
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
is_global_timer_sane = false;
|
|
|
|
|
|
|
|
// Still events left (scheduled in the future)
|
|
|
|
if (!event_queue.empty()) {
|
|
|
|
slice_length = static_cast<int>(
|
|
|
|
std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH));
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2017-11-25 13:56:57 +00:00
|
|
|
downcount = slice_length;
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
void Timing::Idle() {
|
2017-11-25 13:56:57 +00:00
|
|
|
idled_cycles += downcount;
|
|
|
|
downcount = 0;
|
2015-01-06 01:17:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
std::chrono::microseconds Timing::GetGlobalTimeUs() const {
|
2018-08-06 02:07:28 +00:00
|
|
|
return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE_ARM11};
|
2017-11-25 13:56:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
s64 Timing::GetDowncount() const {
|
2017-11-25 13:56:57 +00:00
|
|
|
return downcount;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:53:20 +00:00
|
|
|
} // namespace Core
|