2016-07-29 07:15:46 -04:00
|
|
|
//
|
|
|
|
// TimedEventLoop.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/07/2016.
|
2018-05-13 15:19:52 -04:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-29 07:15:46 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "TimedEventLoop.hpp"
|
2017-09-10 22:44:14 -04:00
|
|
|
|
2016-09-17 19:52:27 -04:00
|
|
|
#include <algorithm>
|
2017-09-10 22:44:14 -04:00
|
|
|
#include <cassert>
|
2018-04-30 22:07:17 -04:00
|
|
|
#include <cmath>
|
2016-07-29 07:15:46 -04:00
|
|
|
|
|
|
|
using namespace Storage;
|
|
|
|
|
2019-10-29 22:36:29 -04:00
|
|
|
TimedEventLoop::TimedEventLoop(Cycles::IntType input_clock_rate) :
|
2016-12-03 11:59:28 -05:00
|
|
|
input_clock_rate_(input_clock_rate) {}
|
2016-07-29 07:15:46 -04:00
|
|
|
|
2017-07-27 22:05:29 -04:00
|
|
|
void TimedEventLoop::run_for(const Cycles cycles) {
|
2019-10-29 22:36:29 -04:00
|
|
|
auto remaining_cycles = cycles.as_integral();
|
2017-09-10 22:44:14 -04:00
|
|
|
#ifndef NDEBUG
|
2019-10-29 22:36:29 -04:00
|
|
|
decltype(remaining_cycles) cycles_advanced = 0;
|
2017-09-10 22:44:14 -04:00
|
|
|
#endif
|
2017-09-10 14:44:38 -04:00
|
|
|
|
|
|
|
while(cycles_until_event_ <= remaining_cycles) {
|
2017-09-10 22:44:14 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
cycles_advanced += cycles_until_event_;
|
|
|
|
#endif
|
2017-09-10 14:44:38 -04:00
|
|
|
advance(cycles_until_event_);
|
|
|
|
remaining_cycles -= cycles_until_event_;
|
|
|
|
cycles_until_event_ = 0;
|
2016-07-29 07:15:46 -04:00
|
|
|
process_next_event();
|
|
|
|
}
|
2017-09-10 14:44:38 -04:00
|
|
|
|
|
|
|
if(remaining_cycles) {
|
|
|
|
cycles_until_event_ -= remaining_cycles;
|
2017-09-10 22:44:14 -04:00
|
|
|
#ifndef NDEBUG
|
|
|
|
cycles_advanced += remaining_cycles;
|
|
|
|
#endif
|
2017-09-10 14:44:38 -04:00
|
|
|
advance(remaining_cycles);
|
|
|
|
}
|
2017-09-10 22:44:14 -04:00
|
|
|
|
2019-10-29 22:36:29 -04:00
|
|
|
assert(cycles_advanced == cycles.as_integral());
|
2017-09-10 22:44:14 -04:00
|
|
|
assert(cycles_until_event_ > 0);
|
2016-07-29 07:15:46 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:53:37 -05:00
|
|
|
Cycles::IntType TimedEventLoop::get_cycles_until_next_event() const {
|
2019-10-29 22:36:29 -04:00
|
|
|
return std::max(cycles_until_event_, Cycles::IntType(0));
|
2016-09-17 19:52:27 -04:00
|
|
|
}
|
|
|
|
|
2019-12-24 20:53:37 -05:00
|
|
|
Cycles::IntType TimedEventLoop::get_input_clock_rate() const {
|
2017-09-10 17:31:43 -04:00
|
|
|
return input_clock_rate_;
|
|
|
|
}
|
|
|
|
|
2017-03-26 14:34:47 -04:00
|
|
|
void TimedEventLoop::reset_timer() {
|
2018-04-30 22:07:17 -04:00
|
|
|
subcycles_until_event_ = 0.0;
|
2016-12-03 11:59:28 -05:00
|
|
|
cycles_until_event_ = 0;
|
2016-07-29 07:15:46 -04:00
|
|
|
}
|
|
|
|
|
2017-03-26 14:34:47 -04:00
|
|
|
void TimedEventLoop::jump_to_next_event() {
|
2016-07-29 07:15:46 -04:00
|
|
|
reset_timer();
|
|
|
|
process_next_event();
|
|
|
|
}
|
|
|
|
|
2017-03-26 14:34:47 -04:00
|
|
|
void TimedEventLoop::set_next_event_time_interval(Time interval) {
|
2019-07-02 15:43:03 -04:00
|
|
|
set_next_event_time_interval(interval.get<float>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TimedEventLoop::set_next_event_time_interval(float interval) {
|
2018-04-25 19:54:39 -04:00
|
|
|
// Calculate [interval]*[input clock rate] + [subcycles until this event]
|
2020-07-17 23:18:41 -04:00
|
|
|
const float float_interval = interval * float(input_clock_rate_) + subcycles_until_event_;
|
2017-12-20 21:03:24 -05:00
|
|
|
|
2020-07-17 23:18:41 -04:00
|
|
|
// This event will fire in the integral number of cycles from now, putting us at the remainder
|
|
|
|
// number of subcycles.
|
2019-10-29 22:36:29 -04:00
|
|
|
const Cycles::IntType addition = Cycles::IntType(float_interval);
|
2018-04-25 19:54:39 -04:00
|
|
|
cycles_until_event_ += addition;
|
2020-07-17 23:18:41 -04:00
|
|
|
subcycles_until_event_ = fmodf(float_interval, 1.0f);
|
2018-04-30 22:07:17 -04:00
|
|
|
|
|
|
|
assert(cycles_until_event_ >= 0);
|
2020-07-17 23:18:41 -04:00
|
|
|
assert(subcycles_until_event_ >= 0.0f);
|
2016-07-29 07:15:46 -04:00
|
|
|
}
|
2016-08-03 07:49:00 -04:00
|
|
|
|
2017-03-26 14:34:47 -04:00
|
|
|
Time TimedEventLoop::get_time_into_next_event() {
|
2017-11-07 22:54:22 -05:00
|
|
|
// TODO: calculate, presumably as [length of interval] - ([cycles left] + [subcycles left])
|
2016-09-18 10:24:09 -04:00
|
|
|
Time zero;
|
|
|
|
return zero;
|
2016-08-03 07:49:00 -04:00
|
|
|
}
|