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"
|
|
|
|
#include "../NumberTheory/Factors.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;
|
|
|
|
|
|
|
|
TimedEventLoop::TimedEventLoop(unsigned int 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) {
|
2017-09-10 18:44:38 +00:00
|
|
|
int remaining_cycles = cycles.as_int();
|
2017-09-11 02:44:14 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
int cycles_advanced = 0;
|
|
|
|
#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
|
|
|
|
|
|
|
assert(cycles_advanced == cycles.as_int());
|
|
|
|
assert(cycles_until_event_ > 0);
|
2016-07-29 11:15:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int TimedEventLoop::get_cycles_until_next_event() {
|
2017-10-21 23:49:04 +00:00
|
|
|
return static_cast<unsigned int>(std::max(cycles_until_event_, 0));
|
2016-09-17 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:31:43 +00:00
|
|
|
unsigned int TimedEventLoop::get_input_clock_rate() {
|
|
|
|
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) {
|
2018-04-25 23:54:39 +00:00
|
|
|
// Calculate [interval]*[input clock rate] + [subcycles until this event]
|
2018-05-01 02:07:17 +00:00
|
|
|
double double_interval = interval.get<double>() * static_cast<double>(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
|
2018-05-01 02:07:17 +00:00
|
|
|
const int addition = static_cast<int>(double_interval);
|
2018-04-25 23:54:39 +00:00
|
|
|
cycles_until_event_ += addition;
|
2018-05-01 02:07:17 +00:00
|
|
|
subcycles_until_event_ = fmod(double_interval, 1.0);
|
|
|
|
|
|
|
|
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
|
|
|
}
|