EventScheduler.h
/*
* This file is part of libsidplayfp, a SID player engine.
*
* Copyright (C) 2011-2015 Leandro Nini
* Copyright (C) 2009 Antti S. Lankila
* Copyright (C) 2001 Simon White
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EVENTSCHEDULER_H
#define EVENTSCHEDULER_H
#include "Event.h"
#include "sidcxx11.h"
namespace libsidplayfp
{
typedef enum
{
EVENT_CLOCK_PHI1 = 0,
EVENT_CLOCK_PHI2 = 1
} event_phase_t;
class EventScheduler
{
private:
Event *firstEvent;
event_clock_t currentTime;
private:
void schedule(Event &event)
{
// find the right spot where to tuck this new event
Event **scan = &firstEvent;
for (;;)
{
if ((*scan == nullptr) || ((*scan)->triggerTime > event.triggerTime))
{
event.next = *scan;
*scan = &event;
break;
}
scan = &((*scan)->next);
}
}
public:
EventScheduler() :
firstEvent(nullptr),
currentTime(0) {}
void schedule(Event &event, unsigned int cycles, event_phase_t phase)
{
// this strange formulation always selects the next available slot regardless of specified phase.
event.triggerTime = currentTime + ((currentTime & 1) ^ phase) + (cycles << 1);
schedule(event);
}
void schedule(Event &event, unsigned int cycles)
{
event.triggerTime = currentTime + (cycles << 1);
schedule(event);
}
void cancel(Event &event);
void reset();
void clock()
{
Event &event = *firstEvent;
firstEvent = firstEvent->next;
currentTime = event.triggerTime;
event.event();
}
bool isPending(Event &event) const;
event_clock_t getTime(event_phase_t phase) const
{
return (currentTime + (phase ^ 1)) >> 1;
}
event_phase_t phase() const { return static_cast<event_phase_t>(currentTime & 1); }
event_clock_t remaining(Event &event) const { return event.triggerTime - currentTime; }
};
}
#endif // EVENTSCHEDULER_H
libsidplayfp::EventScheduler
Definition: EventScheduler.h:62
libsidplayfp::EventScheduler::schedule
void schedule(Event &event, unsigned int cycles, event_phase_t phase)
Definition: EventScheduler.h:106
libsidplayfp::EventScheduler::schedule
void schedule(Event &event, unsigned int cycles)
Definition: EventScheduler.h:119
libsidplayfp::EventScheduler::getTime
event_clock_t getTime(event_phase_t phase) const
Definition: EventScheduler.h:162
libsidplayfp::EventScheduler::isPending
bool isPending(Event &event) const
Definition: EventScheduler.cpp:50
libsidplayfp::EventScheduler::reset
void reset()
Definition: EventScheduler.cpp:29
libsidplayfp::EventScheduler::clock
void clock()
Definition: EventScheduler.h:140
libsidplayfp::EventScheduler::phase
event_phase_t phase() const
Definition: EventScheduler.h:172
libsidplayfp::EventScheduler::cancel
void cancel(Event &event)
Definition: EventScheduler.cpp:35
libsidplayfp::Event
Definition: Event.h:39