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