diff --git a/Storage/TimedEventLoop.cpp b/Storage/TimedEventLoop.cpp index 377af80fd..523f006a0 100644 --- a/Storage/TimedEventLoop.cpp +++ b/Storage/TimedEventLoop.cpp @@ -16,10 +16,19 @@ TimedEventLoop::TimedEventLoop(unsigned int input_clock_rate) : input_clock_rate_(input_clock_rate) {} void TimedEventLoop::run_for(const Cycles cycles) { - cycles_until_event_ -= cycles.as_int(); - while(cycles_until_event_ <= 0) { + int remaining_cycles = cycles.as_int(); + + while(cycles_until_event_ <= remaining_cycles) { + advance(cycles_until_event_); + remaining_cycles -= cycles_until_event_; + cycles_until_event_ = 0; process_next_event(); } + + if(remaining_cycles) { + cycles_until_event_ -= remaining_cycles; + advance(remaining_cycles); + } } unsigned int TimedEventLoop::get_cycles_until_next_event() { @@ -52,7 +61,7 @@ void TimedEventLoop::set_next_event_time_interval(Time interval) { // So this event will fire in the integral number of cycles from now, putting us at the remainder // number of subcycles - cycles_until_event_ = (int)(numerator / denominator); + cycles_until_event_ += (int)(numerator / denominator); subcycles_until_event_.length = (unsigned int)(numerator % denominator); subcycles_until_event_.clock_rate = (unsigned int)denominator; } diff --git a/Storage/TimedEventLoop.hpp b/Storage/TimedEventLoop.hpp index 660767beb..bfe97eaad 100644 --- a/Storage/TimedEventLoop.hpp +++ b/Storage/TimedEventLoop.hpp @@ -66,6 +66,15 @@ namespace Storage { */ virtual void process_next_event() = 0; + /*! + Optionally allows a subclass to track time within run_for periods; if a subclass implements + advnace then it will receive advance increments that add up to the number of cycles supplied + to run_for, but calls to process_next_event will be precisely interspersed. No time will carry + forward between calls into run_for; a subclass can receive arbitrarily many instructions to + advance before receiving a process_next_event. + */ + virtual void advance(const Cycles cycles) {}; + /*! Resets timing, throwing away any current internal state. So clears any fractional ticks that the event loop is currently tracking.