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-09-03 05:40:02 +00:00
|
|
|
#include <atomic>
|
2015-09-16 12:38:12 +00:00
|
|
|
#include <cinttypes>
|
2014-09-03 05:05:45 +00:00
|
|
|
#include <mutex>
|
2015-01-06 01:17:49 +00:00
|
|
|
#include <vector>
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-09 00:15:08 +00:00
|
|
|
#include "common/chunk_file.h"
|
2015-06-21 14:11:32 +00:00
|
|
|
#include "common/logging/log.h"
|
2015-06-20 21:24:01 +00:00
|
|
|
#include "common/string_util.h"
|
2014-12-22 06:30:09 +00:00
|
|
|
|
|
|
|
#include "core/arm/arm_interface.h"
|
2014-04-09 00:15:08 +00:00
|
|
|
#include "core/core.h"
|
2014-09-13 01:18:46 +00:00
|
|
|
#include "core/core_timing.h"
|
2013-10-01 23:10:47 +00:00
|
|
|
|
|
|
|
int g_clock_rate_arm11 = 268123480;
|
|
|
|
|
|
|
|
// is this really necessary?
|
|
|
|
#define INITIAL_SLICE_LENGTH 20000
|
|
|
|
#define MAX_SLICE_LENGTH 100000000
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
namespace CoreTiming {
|
|
|
|
struct EventType {
|
2016-09-19 01:01:46 +00:00
|
|
|
EventType() {}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-19 01:01:46 +00:00
|
|
|
EventType(TimedCallback cb, const char* n) : callback(cb), name(n) {}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
TimedCallback callback;
|
2015-01-06 01:17:49 +00:00
|
|
|
const char* name;
|
2013-10-01 23:10:47 +00:00
|
|
|
};
|
|
|
|
|
2015-01-20 18:47:53 +00:00
|
|
|
static std::vector<EventType> event_types;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
struct BaseEvent {
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 time;
|
|
|
|
u64 userdata;
|
|
|
|
int type;
|
2013-10-01 23:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef LinkedListItem<BaseEvent> Event;
|
|
|
|
|
2015-01-20 18:47:53 +00:00
|
|
|
static Event* first;
|
|
|
|
static Event* ts_first;
|
|
|
|
static Event* ts_last;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
|
|
|
// event pools
|
2015-01-20 18:47:53 +00:00
|
|
|
static Event* event_pool = nullptr;
|
|
|
|
static Event* event_ts_pool = nullptr;
|
|
|
|
static int allocated_ts_events = 0;
|
2013-10-01 23:10:47 +00:00
|
|
|
// Optimization to skip MoveEvents when possible.
|
2015-01-20 18:47:53 +00:00
|
|
|
static std::atomic<bool> has_ts_events(false);
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
int g_slice_length;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-20 18:47:53 +00:00
|
|
|
static s64 global_timer;
|
|
|
|
static s64 idled_cycles;
|
|
|
|
static s64 last_global_time_ticks;
|
|
|
|
static s64 last_global_time_us;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
static std::recursive_mutex external_event_section;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
|
|
|
// Warning: not included in save state.
|
2015-01-06 01:17:49 +00:00
|
|
|
using AdvanceCallback = void(int cycles_executed);
|
2015-01-20 18:47:53 +00:00
|
|
|
static AdvanceCallback* advance_callback = nullptr;
|
|
|
|
static std::vector<MHzChangeCallback> mhz_change_callbacks;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static void FireMhzChange() {
|
2015-01-06 01:17:49 +00:00
|
|
|
for (auto callback : mhz_change_callbacks)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetClockFrequencyMHz(int cpu_mhz) {
|
|
|
|
// When the mhz changes, we keep track of what "time" it was before hand.
|
|
|
|
// This way, time always moves forward, even if mhz is changed.
|
|
|
|
last_global_time_us = GetGlobalTimeUs();
|
|
|
|
last_global_time_ticks = GetTicks();
|
|
|
|
|
|
|
|
g_clock_rate_arm11 = cpu_mhz * 1000000;
|
2014-04-08 23:11:21 +00:00
|
|
|
// TODO: Rescale times of scheduled events?
|
2015-01-06 01:17:49 +00:00
|
|
|
|
|
|
|
FireMhzChange();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
int GetClockFrequencyMHz() {
|
2014-04-08 23:11:21 +00:00
|
|
|
return g_clock_rate_arm11 / 1000000;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
u64 GetGlobalTimeUs() {
|
|
|
|
s64 ticks_since_last = GetTicks() - last_global_time_ticks;
|
|
|
|
int freq = GetClockFrequencyMHz();
|
|
|
|
s64 us_since_last = ticks_since_last / freq;
|
|
|
|
return last_global_time_us + us_since_last;
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static Event* GetNewEvent() {
|
2015-01-06 01:17:49 +00:00
|
|
|
if (!event_pool)
|
2014-04-08 23:11:21 +00:00
|
|
|
return new Event;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* event = event_pool;
|
|
|
|
event_pool = event->next;
|
|
|
|
return event;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static Event* GetNewTsEvent() {
|
2015-01-06 01:17:49 +00:00
|
|
|
allocated_ts_events++;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
if (!event_ts_pool)
|
2014-04-08 23:11:21 +00:00
|
|
|
return new Event;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* event = event_ts_pool;
|
|
|
|
event_ts_pool = event->next;
|
|
|
|
return event;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static void FreeEvent(Event* event) {
|
2015-01-06 01:17:49 +00:00
|
|
|
event->next = event_pool;
|
|
|
|
event_pool = event;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static void FreeTsEvent(Event* event) {
|
2015-01-06 01:17:49 +00:00
|
|
|
event->next = event_ts_pool;
|
|
|
|
event_ts_pool = event;
|
|
|
|
allocated_ts_events--;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
int RegisterEvent(const char* name, TimedCallback callback) {
|
2015-02-17 22:42:28 +00:00
|
|
|
event_types.emplace_back(callback, name);
|
2014-04-08 23:11:21 +00:00
|
|
|
return (int)event_types.size() - 1;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static void AntiCrashCallback(u64 userdata, int cycles_late) {
|
2015-01-06 01:17:49 +00:00
|
|
|
LOG_CRITICAL(Core_Timing, "Savestate broken: an unregistered event was called.");
|
2014-04-08 23:11:21 +00:00
|
|
|
Core::Halt("invalid timing events");
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void RestoreRegisterEvent(int event_type, const char* name, TimedCallback callback) {
|
2014-04-08 23:11:21 +00:00
|
|
|
if (event_type >= (int)event_types.size())
|
|
|
|
event_types.resize(event_type + 1, EventType(AntiCrashCallback, "INVALID EVENT"));
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2014-04-08 23:11:21 +00:00
|
|
|
event_types[event_type] = EventType(callback, name);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void UnregisterAllEvents() {
|
2014-04-08 23:11:21 +00:00
|
|
|
if (first)
|
2015-02-19 06:18:47 +00:00
|
|
|
LOG_ERROR(Core_Timing, "Cannot unregister events with events pending");
|
2014-04-08 23:11:21 +00:00
|
|
|
event_types.clear();
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Init() {
|
|
|
|
Core::g_app_core->down_count = INITIAL_SLICE_LENGTH;
|
|
|
|
g_slice_length = INITIAL_SLICE_LENGTH;
|
|
|
|
global_timer = 0;
|
|
|
|
idled_cycles = 0;
|
|
|
|
last_global_time_ticks = 0;
|
|
|
|
last_global_time_us = 0;
|
|
|
|
has_ts_events = 0;
|
|
|
|
mhz_change_callbacks.clear();
|
2015-04-28 02:46:19 +00:00
|
|
|
|
|
|
|
first = nullptr;
|
|
|
|
ts_first = nullptr;
|
|
|
|
ts_last = nullptr;
|
|
|
|
|
|
|
|
event_pool = nullptr;
|
|
|
|
event_ts_pool = nullptr;
|
|
|
|
allocated_ts_events = 0;
|
|
|
|
|
|
|
|
advance_callback = nullptr;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Shutdown() {
|
2014-04-08 23:11:21 +00:00
|
|
|
MoveEvents();
|
|
|
|
ClearPendingEvents();
|
|
|
|
UnregisterAllEvents();
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
while (event_pool) {
|
|
|
|
Event* event = event_pool;
|
|
|
|
event_pool = event->next;
|
|
|
|
delete event;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
|
|
|
while (event_ts_pool) {
|
|
|
|
Event* event = event_ts_pool;
|
|
|
|
event_ts_pool = event->next;
|
|
|
|
delete event;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
u64 GetTicks() {
|
|
|
|
return (u64)global_timer + g_slice_length - Core::g_app_core->down_count;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
u64 GetIdleTicks() {
|
|
|
|
return (u64)idled_cycles;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is to be called when outside threads, such as the graphics thread, wants to
|
|
|
|
// schedule things to be executed on the main thread.
|
2015-01-06 01:17:49 +00:00
|
|
|
void ScheduleEvent_Threadsafe(s64 cycles_into_future, int event_type, u64 userdata) {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
|
|
|
Event* new_event = GetNewTsEvent();
|
|
|
|
new_event->time = GetTicks() + cycles_into_future;
|
|
|
|
new_event->type = event_type;
|
2015-09-12 03:04:19 +00:00
|
|
|
new_event->next = nullptr;
|
2015-01-06 01:17:49 +00:00
|
|
|
new_event->userdata = userdata;
|
|
|
|
if (!ts_first)
|
|
|
|
ts_first = new_event;
|
|
|
|
if (ts_last)
|
|
|
|
ts_last->next = new_event;
|
|
|
|
ts_last = new_event;
|
|
|
|
|
|
|
|
has_ts_events = true;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Same as ScheduleEvent_Threadsafe(0, ...) EXCEPT if we are already on the CPU thread
|
|
|
|
// in which case the event will get handled immediately, before returning.
|
2015-01-06 01:17:49 +00:00
|
|
|
void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata) {
|
2016-09-18 00:38:01 +00:00
|
|
|
if (false) // Core::IsCPUThread())
|
2014-04-08 23:11:21 +00:00
|
|
|
{
|
2015-01-06 01:17:49 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
2014-04-08 23:11:21 +00:00
|
|
|
event_types[event_type].callback(userdata, 0);
|
2016-09-18 00:38:01 +00:00
|
|
|
} else
|
2014-04-08 23:11:21 +00:00
|
|
|
ScheduleEvent_Threadsafe(0, event_type, userdata);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void ClearPendingEvents() {
|
|
|
|
while (first) {
|
|
|
|
Event* event = first->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
FreeEvent(first);
|
2015-01-06 01:17:49 +00:00
|
|
|
first = event;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 15:40:07 +00:00
|
|
|
static void AddEventToQueue(Event* new_event) {
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* prev_event = nullptr;
|
|
|
|
Event** next_event = &first;
|
|
|
|
for (;;) {
|
|
|
|
Event*& next = *next_event;
|
|
|
|
if (!next || new_event->time < next->time) {
|
|
|
|
new_event->next = next;
|
|
|
|
next = new_event;
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
prev_event = next;
|
|
|
|
next_event = &prev_event->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void ScheduleEvent(s64 cycles_into_future, int event_type, u64 userdata) {
|
|
|
|
Event* new_event = GetNewEvent();
|
|
|
|
new_event->userdata = userdata;
|
|
|
|
new_event->type = event_type;
|
|
|
|
new_event->time = GetTicks() + cycles_into_future;
|
|
|
|
AddEventToQueue(new_event);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
s64 UnscheduleEvent(int event_type, u64 userdata) {
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 result = 0;
|
|
|
|
if (!first)
|
|
|
|
return result;
|
2015-01-06 01:17:49 +00:00
|
|
|
while (first) {
|
|
|
|
if (first->type == event_type && first->userdata == userdata) {
|
|
|
|
result = first->time - GetTicks();
|
2014-04-08 23:11:21 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* next = first->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
FreeEvent(first);
|
|
|
|
first = next;
|
2015-01-06 01:17:49 +00:00
|
|
|
} else {
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!first)
|
|
|
|
return result;
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* prev_event = first;
|
|
|
|
Event* ptr = prev_event->next;
|
|
|
|
|
|
|
|
while (ptr) {
|
|
|
|
if (ptr->type == event_type && ptr->userdata == userdata) {
|
|
|
|
result = ptr->time - GetTicks();
|
|
|
|
|
|
|
|
prev_event->next = ptr->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
FreeEvent(ptr);
|
2015-01-06 01:17:49 +00:00
|
|
|
ptr = prev_event->next;
|
|
|
|
} else {
|
|
|
|
prev_event = ptr;
|
2014-04-08 23:11:21 +00:00
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata) {
|
2014-04-08 23:11:21 +00:00
|
|
|
s64 result = 0;
|
2015-01-06 01:17:49 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
|
|
|
if (!ts_first)
|
2014-04-08 23:11:21 +00:00
|
|
|
return result;
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
while (ts_first) {
|
|
|
|
if (ts_first->type == event_type && ts_first->userdata == userdata) {
|
|
|
|
result = ts_first->time - GetTicks();
|
|
|
|
|
|
|
|
Event* next = ts_first->next;
|
|
|
|
FreeTsEvent(ts_first);
|
|
|
|
ts_first = next;
|
|
|
|
} else {
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
if (!ts_first) {
|
2015-01-06 01:17:49 +00:00
|
|
|
ts_last = nullptr;
|
2014-04-08 23:11:21 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* prev_event = ts_first;
|
|
|
|
Event* next = prev_event->next;
|
|
|
|
while (next) {
|
|
|
|
if (next->type == event_type && next->userdata == userdata) {
|
|
|
|
result = next->time - GetTicks();
|
|
|
|
|
|
|
|
prev_event->next = next->next;
|
|
|
|
if (next == ts_last)
|
|
|
|
ts_last = prev_event;
|
|
|
|
FreeTsEvent(next);
|
|
|
|
next = prev_event->next;
|
|
|
|
} else {
|
|
|
|
prev_event = next;
|
|
|
|
next = next->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Warning: not included in save state.
|
2015-01-06 01:17:49 +00:00
|
|
|
void RegisterAdvanceCallback(AdvanceCallback* callback) {
|
|
|
|
advance_callback = callback;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void RegisterMHzChangeCallback(MHzChangeCallback callback) {
|
|
|
|
mhz_change_callbacks.push_back(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsScheduled(int event_type) {
|
2014-04-08 23:11:21 +00:00
|
|
|
if (!first)
|
|
|
|
return false;
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* event = first;
|
|
|
|
while (event) {
|
|
|
|
if (event->type == event_type)
|
2014-04-08 23:11:21 +00:00
|
|
|
return true;
|
2015-01-06 01:17:49 +00:00
|
|
|
event = event->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
|
|
|
return false;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void RemoveEvent(int event_type) {
|
2014-04-08 23:11:21 +00:00
|
|
|
if (!first)
|
|
|
|
return;
|
2015-01-06 01:17:49 +00:00
|
|
|
while (first) {
|
|
|
|
if (first->type == event_type) {
|
2016-09-18 00:38:01 +00:00
|
|
|
Event* next = first->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
FreeEvent(first);
|
|
|
|
first = next;
|
2015-01-06 01:17:49 +00:00
|
|
|
} else {
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!first)
|
|
|
|
return;
|
2015-01-06 01:17:49 +00:00
|
|
|
Event* prev = first;
|
|
|
|
Event* next = prev->next;
|
|
|
|
while (next) {
|
|
|
|
if (next->type == event_type) {
|
|
|
|
prev->next = next->next;
|
|
|
|
FreeEvent(next);
|
|
|
|
next = prev->next;
|
|
|
|
} else {
|
|
|
|
prev = next;
|
|
|
|
next = next->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void RemoveThreadsafeEvent(int event_type) {
|
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
|
|
|
if (!ts_first)
|
2014-04-08 23:11:21 +00:00
|
|
|
return;
|
2015-01-06 01:17:49 +00:00
|
|
|
|
|
|
|
while (ts_first) {
|
|
|
|
if (ts_first->type == event_type) {
|
|
|
|
Event* next = ts_first->next;
|
|
|
|
FreeTsEvent(ts_first);
|
|
|
|
ts_first = next;
|
|
|
|
} else {
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
|
|
|
|
if (!ts_first) {
|
|
|
|
ts_last = nullptr;
|
2014-04-08 23:11:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
|
|
|
|
Event* prev = ts_first;
|
|
|
|
Event* next = prev->next;
|
|
|
|
while (next) {
|
|
|
|
if (next->type == event_type) {
|
|
|
|
prev->next = next->next;
|
|
|
|
if (next == ts_last)
|
|
|
|
ts_last = prev;
|
|
|
|
FreeTsEvent(next);
|
|
|
|
next = prev->next;
|
|
|
|
} else {
|
|
|
|
prev = next;
|
|
|
|
next = next->next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void RemoveAllEvents(int event_type) {
|
2014-04-08 23:11:21 +00:00
|
|
|
RemoveThreadsafeEvent(event_type);
|
|
|
|
RemoveEvent(event_type);
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
// This raise only the events required while the fifo is processing data
|
|
|
|
void ProcessFifoWaitEvents() {
|
|
|
|
while (first) {
|
|
|
|
if (first->time <= (s64)GetTicks()) {
|
2014-04-08 23:11:21 +00:00
|
|
|
Event* evt = first;
|
|
|
|
first = first->next;
|
2015-01-06 01:17:49 +00:00
|
|
|
event_types[evt->type].callback(evt->userdata, (int)(GetTicks() - evt->time));
|
2014-04-08 23:11:21 +00:00
|
|
|
FreeEvent(evt);
|
2015-01-06 01:17:49 +00:00
|
|
|
} else {
|
2014-04-08 23:11:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void MoveEvents() {
|
|
|
|
has_ts_events = false;
|
2014-04-08 23:11:21 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(external_event_section);
|
2014-04-08 23:11:21 +00:00
|
|
|
// Move events from async queue into main queue
|
2015-01-06 01:17:49 +00:00
|
|
|
while (ts_first) {
|
|
|
|
Event* next = ts_first->next;
|
|
|
|
AddEventToQueue(ts_first);
|
|
|
|
ts_first = next;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
ts_last = nullptr;
|
2014-04-08 23:11:21 +00:00
|
|
|
|
|
|
|
// Move free events to threadsafe pool
|
2015-01-06 01:17:49 +00:00
|
|
|
while (allocated_ts_events > 0 && event_pool) {
|
|
|
|
Event* event = event_pool;
|
|
|
|
event_pool = event->next;
|
|
|
|
event->next = event_ts_pool;
|
|
|
|
event_ts_pool = event;
|
|
|
|
allocated_ts_events--;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void ForceCheck() {
|
2015-02-01 20:31:21 +00:00
|
|
|
s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer += cycles_executed;
|
|
|
|
// This will cause us to check for new events immediately.
|
|
|
|
Core::g_app_core->down_count = 0;
|
|
|
|
// But let's not eat a bunch more time in Advance() because of this.
|
|
|
|
g_slice_length = 0;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Advance() {
|
2015-02-01 20:31:21 +00:00
|
|
|
s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
|
2015-01-06 01:17:49 +00:00
|
|
|
global_timer += cycles_executed;
|
|
|
|
Core::g_app_core->down_count = g_slice_length;
|
2014-09-13 01:18:46 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
if (has_ts_events)
|
|
|
|
MoveEvents();
|
|
|
|
ProcessFifoWaitEvents();
|
2014-09-13 01:18:46 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
if (!first) {
|
|
|
|
if (g_slice_length < 10000) {
|
|
|
|
g_slice_length += 10000;
|
|
|
|
Core::g_app_core->down_count += g_slice_length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Note that events can eat cycles as well.
|
|
|
|
int target = (int)(first->time - global_timer);
|
|
|
|
if (target > MAX_SLICE_LENGTH)
|
|
|
|
target = MAX_SLICE_LENGTH;
|
|
|
|
|
|
|
|
const int diff = target - g_slice_length;
|
|
|
|
g_slice_length += diff;
|
|
|
|
Core::g_app_core->down_count += diff;
|
2014-04-08 23:11:21 +00:00
|
|
|
}
|
2015-01-06 01:17:49 +00:00
|
|
|
if (advance_callback)
|
2015-06-28 02:29:21 +00:00
|
|
|
advance_callback(static_cast<int>(cycles_executed));
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void LogPendingEvents() {
|
|
|
|
Event* event = first;
|
|
|
|
while (event) {
|
2016-09-18 00:38:01 +00:00
|
|
|
// LOG_TRACE(Core_Timing, "PENDING: Now: %lld Pending: %lld Type: %d", globalTimer,
|
|
|
|
// next->time, next->type);
|
2015-01-06 01:17:49 +00:00
|
|
|
event = event->next;
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
void Idle(int max_idle) {
|
2015-02-01 20:31:21 +00:00
|
|
|
s64 cycles_down = Core::g_app_core->down_count;
|
2015-01-06 01:17:49 +00:00
|
|
|
if (max_idle != 0 && cycles_down > max_idle)
|
|
|
|
cycles_down = max_idle;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
if (first && cycles_down > 0) {
|
2015-02-01 20:31:21 +00:00
|
|
|
s64 cycles_executed = g_slice_length - Core::g_app_core->down_count;
|
|
|
|
s64 cycles_next_event = first->time - global_timer;
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
if (cycles_next_event < cycles_executed + cycles_down) {
|
|
|
|
cycles_down = cycles_next_event - cycles_executed;
|
|
|
|
// Now, now... no time machines, please.
|
|
|
|
if (cycles_down < 0)
|
|
|
|
cycles_down = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
LOG_TRACE(Core_Timing, "Idle for %" PRId64 " cycles! (%f ms)", cycles_down,
|
|
|
|
cycles_down / (float)(g_clock_rate_arm11 * 0.001f));
|
2013-10-01 23:10:47 +00:00
|
|
|
|
2015-01-06 01:17:49 +00:00
|
|
|
idled_cycles += cycles_down;
|
|
|
|
Core::g_app_core->down_count -= cycles_down;
|
|
|
|
if (Core::g_app_core->down_count == 0)
|
|
|
|
Core::g_app_core->down_count = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetScheduledEventsSummary() {
|
|
|
|
Event* event = first;
|
|
|
|
std::string text = "Scheduled events\n";
|
|
|
|
text.reserve(1000);
|
|
|
|
while (event) {
|
|
|
|
unsigned int t = event->type;
|
|
|
|
if (t >= event_types.size())
|
2015-02-19 06:18:47 +00:00
|
|
|
LOG_ERROR(Core_Timing, "Invalid event type"); // %i", t);
|
2015-01-06 01:17:49 +00:00
|
|
|
const char* name = event_types[event->type].name;
|
|
|
|
if (!name)
|
|
|
|
name = "[unknown]";
|
2015-05-25 18:34:09 +00:00
|
|
|
text += Common::StringFromFormat("%s : %i %08x%08x\n", name, (int)event->time,
|
2016-09-18 00:38:01 +00:00
|
|
|
(u32)(event->userdata >> 32), (u32)(event->userdata));
|
2015-01-06 01:17:49 +00:00
|
|
|
event = event->next;
|
|
|
|
}
|
|
|
|
return text;
|
2013-10-01 23:10:47 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 09:02:05 +00:00
|
|
|
} // namespace
|