src/core/timing.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/core/timing.h>
7
8void mTimingInit(struct mTiming* timing, int32_t* relativeCycles, int32_t* nextEvent) {
9 timing->root = NULL;
10 timing->reroot = NULL;
11 timing->masterCycles = 0;
12 timing->relativeCycles = relativeCycles;
13 timing->nextEvent = nextEvent;
14}
15
16void mTimingDeinit(struct mTiming* timing) {
17}
18
19void mTimingClear(struct mTiming* timing) {
20 timing->root = NULL;
21 timing->reroot = NULL;
22 timing->masterCycles = 0;
23}
24
25void mTimingSchedule(struct mTiming* timing, struct mTimingEvent* event, int32_t when) {
26 int32_t nextEvent = when + *timing->relativeCycles;
27 event->when = nextEvent + timing->masterCycles;
28 if (nextEvent < *timing->nextEvent) {
29 *timing->nextEvent = nextEvent;
30 }
31 struct mTimingEvent** previous = &timing->root;
32 struct mTimingEvent* next = timing->root;
33 unsigned priority = event->priority;
34 while (next) {
35 int32_t nextWhen = next->when - timing->masterCycles;
36 if (nextWhen > nextEvent || (nextWhen == nextEvent && next->priority > priority)) {
37 break;
38 }
39 previous = &next->next;
40 next = next->next;
41 }
42 event->next = next;
43 *previous = event;
44}
45
46void mTimingDeschedule(struct mTiming* timing, struct mTimingEvent* event) {
47 struct mTimingEvent** previous = &timing->root;
48 struct mTimingEvent* next = timing->root;
49 while (next) {
50 if (next == event) {
51 *previous = next->next;
52 return;
53 }
54 previous = &next->next;
55 next = next->next;
56 }
57}
58
59bool mTimingIsScheduled(const struct mTiming* timing, const struct mTimingEvent* event) {
60 const struct mTimingEvent* next = timing->root;
61 if (!next) {
62 next = timing->reroot;
63 }
64 while (next) {
65 if (next == event) {
66 return true;
67 }
68 next = next->next;
69 }
70 return false;
71}
72
73int32_t mTimingTick(struct mTiming* timing, int32_t cycles) {
74 timing->masterCycles += cycles;
75 uint32_t masterCycles = timing->masterCycles;
76 while (timing->root) {
77 struct mTimingEvent* next = timing->root;
78 int32_t nextWhen = next->when - masterCycles;
79 if (nextWhen > 0) {
80 return nextWhen;
81 }
82 timing->root = next->next;
83 next->callback(timing, next->context, -nextWhen);
84 }
85 if (timing->reroot) {
86 timing->root = timing->reroot;
87 timing->reroot = NULL;
88 *timing->nextEvent = mTimingNextEvent(timing);
89 }
90 return *timing->nextEvent;
91}
92
93int32_t mTimingCurrentTime(const struct mTiming* timing) {
94 return timing->masterCycles + *timing->relativeCycles;
95}
96
97int32_t mTimingNextEvent(struct mTiming* timing) {
98 struct mTimingEvent* next = timing->root;
99 if (!next) {
100 return INT_MAX;
101 }
102 return next->when - timing->masterCycles - *timing->relativeCycles;
103}
104
105int32_t mTimingUntil(const struct mTiming* timing, const struct mTimingEvent* event) {
106 return event->when - timing->masterCycles - *timing->relativeCycles;
107}