libsidplayfp 2.15.0
EventScheduler.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright (C) 2011-2015 Leandro Nini
5 * Copyright (C) 2009 Antti S. Lankila
6 * Copyright (C) 2001 Simon White
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef EVENTSCHEDULER_H
24#define EVENTSCHEDULER_H
25
26#include "Event.h"
27
28#include "sidcxx11.h"
29
30
31namespace libsidplayfp
32{
33
40using event_phase_t = enum
41{
42 EVENT_CLOCK_PHI1 = 0,
43 EVENT_CLOCK_PHI2 = 1
44};
45
46
62{
63private:
65 Event *firstEvent = nullptr;
66
68 event_clock_t currentTime = 0;
69
70private:
76 void schedule(Event &event)
77 {
78 // find the right spot where to tuck this new event
79 Event **scan = &firstEvent;
80 for (;;)
81 {
82 if ((*scan == nullptr) || ((*scan)->triggerTime > event.triggerTime))
83 {
84 event.next = *scan;
85 *scan = &event;
86 break;
87 }
88 scan = &((*scan)->next);
89 }
90 }
91
92public:
102 void schedule(Event &event, unsigned int cycles, event_phase_t phase)
103 {
104 // this strange formulation always selects the next available slot regardless of specified phase.
105 event.triggerTime = currentTime + ((currentTime & 1) ^ phase) + (cycles << 1);
106 schedule(event);
107 }
108
115 void schedule(Event &event, unsigned int cycles)
116 {
117 event.triggerTime = currentTime + (cycles << 1);
118 schedule(event);
119 }
120
126 void cancel(Event &event);
127
131 void reset();
132
136 void clock()
137 {
138 Event &event = *firstEvent;
139 firstEvent = firstEvent->next;
140 currentTime = event.triggerTime;
141 event.event();
142 }
143
150 bool isPending(Event &event) const;
151
158 event_clock_t getTime(event_phase_t phase) const
159 {
160 return (currentTime + (phase ^ 1)) >> 1;
161 }
162
168 event_phase_t phase() const { return static_cast<event_phase_t>(currentTime & 1); }
169
170 event_clock_t remaining(Event &event) const { return event.triggerTime - currentTime; }
171};
172
173}
174
175#endif // EVENTSCHEDULER_H
Definition EventScheduler.h:62
void schedule(Event &event, unsigned int cycles, event_phase_t phase)
Definition EventScheduler.h:102
void schedule(Event &event, unsigned int cycles)
Definition EventScheduler.h:115
event_clock_t getTime(event_phase_t phase) const
Definition EventScheduler.h:158
bool isPending(Event &event) const
Definition EventScheduler.cpp:50
void reset()
Definition EventScheduler.cpp:29
void clock()
Definition EventScheduler.h:136
event_phase_t phase() const
Definition EventScheduler.h:168
void cancel(Event &event)
Definition EventScheduler.cpp:35
Definition Event.h:39