summaryrefslogtreecommitdiffstats
path: root/src/core/core_timing.cpp
blob: 9fe3747952ffbc58564507da293b425c3ce2ac84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
// Copyright (c) 2012- PPSSPP Project / Dolphin Project.
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <atomic>
#include <cinttypes>
#include <mutex>
#include <vector>
#include "common/chunk_file.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/arm/arm_interface.h"
#include "core/core.h"
#include "core/core_timing.h"

int g_clock_rate_arm11 = 268123480;

// is this really necessary?
#define INITIAL_SLICE_LENGTH 20000
#define MAX_SLICE_LENGTH 100000000

namespace CoreTiming {
struct EventType {
    EventType() {}

    EventType(TimedCallback cb, const char* n) : callback(cb), name(n) {}

    TimedCallback callback;
    const char* name;
};

static std::vector<EventType> event_types;

struct BaseEvent {
    s64 time;
    u64 userdata;
    int type;
};

typedef LinkedListItem<BaseEvent> Event;

static Event* first;
static Event* ts_first;
static Event* ts_last;

// event pools
static Event* event_pool = nullptr;
static Event* event_ts_pool = nullptr;
static int allocated_ts_events = 0;
// Optimization to skip MoveEvents when possible.
static std::atomic<bool> has_ts_events(false);

int g_slice_length;

static s64 global_timer;
static s64 idled_cycles;
static s64 last_global_time_ticks;
static s64 last_global_time_us;

static std::recursive_mutex external_event_section;

// Warning: not included in save state.
using AdvanceCallback = void(int cycles_executed);
static AdvanceCallback* advance_callback = nullptr;
static std::vector<MHzChangeCallback> mhz_change_callbacks;

static void FireMhzChange() {
    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;
    // TODO: Rescale times of scheduled events?

    FireMhzChange();
}

int GetClockFrequencyMHz() {
    return g_clock_rate_arm11 / 1000000;
}

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;
}

static Event* GetNewEvent() {
    if (!event_pool)
        return new Event;

    Event* event = event_pool;
    event_pool = event->next;
    return event;
}

static Event* GetNewTsEvent() {
    allocated_ts_events++;

    if (!event_ts_pool)
        return new Event;

    Event* event = event_ts_pool;
    event_ts_pool = event->next;
    return event;
}

static void FreeEvent(Event* event) {
    event->next = event_pool;
    event_pool = event;
}

static void FreeTsEvent(Event* event) {
    event->next = event_ts_pool;
    event_ts_pool = event;
    allocated_ts_events--;
}

int RegisterEvent(const char* name, TimedCallback callback) {
    event_types.emplace_back(callback, name);
    return (int)event_types.size() - 1;
}

static void AntiCrashCallback(u64 userdata, int cycles_late) {
    LOG_CRITICAL(Core_Timing, "Savestate broken: an unregistered event was called.");
}

void RestoreRegisterEvent(int event_type, const char* name, TimedCallback callback) {
    if (event_type >= (int)event_types.size())
        event_types.resize(event_type + 1, EventType(AntiCrashCallback, "INVALID EVENT"));

    event_types[event_type] = EventType(callback, name);
}

void UnregisterAllEvents() {
    if (first)
        LOG_ERROR(Core_Timing, "Cannot unregister events with events pending");
    event_types.clear();
}

void Init() {
    Core::AppCore().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();

    first = nullptr;
    ts_first = nullptr;
    ts_last = nullptr;

    event_pool = nullptr;
    event_ts_pool = nullptr;
    allocated_ts_events = 0;

    advance_callback = nullptr;
}

void Shutdown() {
    MoveEvents();
    ClearPendingEvents();
    UnregisterAllEvents();

    while (event_pool) {
        Event* event = event_pool;
        event_pool = event->next;
        delete event;
    }

    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;
    }
}

u64 GetTicks() {
    return (u64)global_timer + g_slice_length - Core::AppCore().down_count;
}

u64 GetIdleTicks() {
    return (u64)idled_cycles;
}

// This is to be called when outside threads, such as the graphics thread, wants to
// schedule things to be executed on the main thread.
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;
    new_event->next = nullptr;
    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;
}

// 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.
void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata) {
    if (false) // Core::IsCPUThread())
    {
        std::lock_guard<std::recursive_mutex> lock(external_event_section);
        event_types[event_type].callback(userdata, 0);
    } else
        ScheduleEvent_Threadsafe(0, event_type, userdata);
}

void ClearPendingEvents() {
    while (first) {
        Event* event = first->next;
        FreeEvent(first);
        first = event;
    }
}

static void AddEventToQueue(Event* new_event) {
    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;
            break;
        }
        prev_event = next;
        next_event = &prev_event->next;
    }
}

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);
}

s64 UnscheduleEvent(int event_type, u64 userdata) {
    s64 result = 0;
    if (!first)
        return result;
    while (first) {
        if (first->type == event_type && first->userdata == userdata) {
            result = first->time - GetTicks();

            Event* next = first->next;
            FreeEvent(first);
            first = next;
        } else {
            break;
        }
    }
    if (!first)
        return result;

    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;
            FreeEvent(ptr);
            ptr = prev_event->next;
        } else {
            prev_event = ptr;
            ptr = ptr->next;
        }
    }

    return result;
}

s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata) {
    s64 result = 0;
    std::lock_guard<std::recursive_mutex> lock(external_event_section);
    if (!ts_first)
        return result;

    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 {
            break;
        }
    }

    if (!ts_first) {
        ts_last = nullptr;
        return result;
    }

    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;
        }
    }

    return result;
}

// Warning: not included in save state.
void RegisterAdvanceCallback(AdvanceCallback* callback) {
    advance_callback = callback;
}

void RegisterMHzChangeCallback(MHzChangeCallback callback) {
    mhz_change_callbacks.push_back(callback);
}

bool IsScheduled(int event_type) {
    if (!first)
        return false;
    Event* event = first;
    while (event) {
        if (event->type == event_type)
            return true;
        event = event->next;
    }
    return false;
}

void RemoveEvent(int event_type) {
    if (!first)
        return;
    while (first) {
        if (first->type == event_type) {
            Event* next = first->next;
            FreeEvent(first);
            first = next;
        } else {
            break;
        }
    }
    if (!first)
        return;
    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;
        }
    }
}

void RemoveThreadsafeEvent(int event_type) {
    std::lock_guard<std::recursive_mutex> lock(external_event_section);
    if (!ts_first)
        return;

    while (ts_first) {
        if (ts_first->type == event_type) {
            Event* next = ts_first->next;
            FreeTsEvent(ts_first);
            ts_first = next;
        } else {
            break;
        }
    }

    if (!ts_first) {
        ts_last = nullptr;
        return;
    }

    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;
        }
    }
}

void RemoveAllEvents(int event_type) {
    RemoveThreadsafeEvent(event_type);
    RemoveEvent(event_type);
}

// This raise only the events required while the fifo is processing data
void ProcessFifoWaitEvents() {
    while (first) {
        if (first->time <= (s64)GetTicks()) {
            Event* evt = first;
            first = first->next;
            event_types[evt->type].callback(evt->userdata, (int)(GetTicks() - evt->time));
            FreeEvent(evt);
        } else {
            break;
        }
    }
}

void MoveEvents() {
    has_ts_events = false;

    std::lock_guard<std::recursive_mutex> lock(external_event_section);
    // Move events from async queue into main queue
    while (ts_first) {
        Event* next = ts_first->next;
        AddEventToQueue(ts_first);
        ts_first = next;
    }
    ts_last = nullptr;

    // Move free events to threadsafe pool
    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--;
    }
}

void ForceCheck() {
    s64 cycles_executed = g_slice_length - Core::AppCore().down_count;
    global_timer += cycles_executed;
    // This will cause us to check for new events immediately.
    Core::AppCore().down_count = 0;
    // But let's not eat a bunch more time in Advance() because of this.
    g_slice_length = 0;
}

void Advance() {
    s64 cycles_executed = g_slice_length - Core::AppCore().down_count;
    global_timer += cycles_executed;
    Core::AppCore().down_count = g_slice_length;

    if (has_ts_events)
        MoveEvents();
    ProcessFifoWaitEvents();

    if (!first) {
        if (g_slice_length < 10000) {
            g_slice_length += 10000;
            Core::AppCore().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::AppCore().down_count += diff;
    }
    if (advance_callback)
        advance_callback(static_cast<int>(cycles_executed));
}

void LogPendingEvents() {
    Event* event = first;
    while (event) {
        // LOG_TRACE(Core_Timing, "PENDING: Now: %lld Pending: %lld Type: %d", globalTimer,
        // next->time, next->type);
        event = event->next;
    }
}

void Idle(int max_idle) {
    s64 cycles_down = Core::AppCore().down_count;
    if (max_idle != 0 && cycles_down > max_idle)
        cycles_down = max_idle;

    if (first && cycles_down > 0) {
        s64 cycles_executed = g_slice_length - Core::AppCore().down_count;
        s64 cycles_next_event = first->time - global_timer;

        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;
        }
    }

    LOG_TRACE(Core_Timing, "Idle for %" PRId64 " cycles! (%f ms)", cycles_down,
              cycles_down / (float)(g_clock_rate_arm11 * 0.001f));

    idled_cycles += cycles_down;
    Core::AppCore().down_count -= cycles_down;
    if (Core::AppCore().down_count == 0)
        Core::AppCore().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())
            LOG_ERROR(Core_Timing, "Invalid event type"); // %i", t);
        const char* name = event_types[event->type].name;
        if (!name)
            name = "[unknown]";
        text += Common::StringFromFormat("%s : %i %08x%08x\n", name, (int)event->time,
                                         (u32)(event->userdata >> 32), (u32)(event->userdata));
        event = event->next;
    }
    return text;
}

} // namespace