2016-07-29 11:15:46 +00:00
|
|
|
//
|
|
|
|
// TimedEventLoop.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#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>
|
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() {
|
2016-12-03 16:59:28 +00:00
|
|
|
subcycles_until_event_.set_zero();
|
|
|
|
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) {
|
2016-09-18 14:24:09 +00:00
|
|
|
// Calculate [interval]*[input clock rate] + [subcycles until this event].
|
2017-12-21 02:03:24 +00:00
|
|
|
int64_t denominator = static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.clock_rate);
|
2016-09-18 14:24:09 +00:00
|
|
|
int64_t numerator =
|
2017-12-21 02:03:24 +00:00
|
|
|
static_cast<int64_t>(subcycles_until_event_.clock_rate) * static_cast<int64_t>(input_clock_rate_) * static_cast<int64_t>(interval.length) +
|
|
|
|
static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.length);
|
|
|
|
|
|
|
|
// Simplify if necessary: try just simplifying the interval and recalculating; if that doesn't
|
|
|
|
// work then try simplifying the whole thing.
|
|
|
|
if(numerator < 0 || denominator < 0 || denominator > std::numeric_limits<unsigned int>::max()) {
|
|
|
|
interval.simplify();
|
|
|
|
denominator = static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.clock_rate);
|
|
|
|
numerator =
|
|
|
|
static_cast<int64_t>(subcycles_until_event_.clock_rate) * static_cast<int64_t>(input_clock_rate_) * static_cast<int64_t>(interval.length) +
|
|
|
|
static_cast<int64_t>(interval.clock_rate) * static_cast<int64_t>(subcycles_until_event_.length);
|
|
|
|
}
|
2016-09-18 14:24:09 +00:00
|
|
|
|
2017-12-21 02:03:24 +00:00
|
|
|
if(numerator < 0 || denominator < 0 || denominator > std::numeric_limits<unsigned int>::max()) {
|
2017-08-11 23:05:46 +00:00
|
|
|
int64_t common_divisor = NumberTheory::greatest_common_divisor(numerator % denominator, denominator);
|
|
|
|
denominator /= common_divisor;
|
|
|
|
numerator /= common_divisor;
|
|
|
|
}
|
2016-07-29 11:15:46 +00:00
|
|
|
|
2017-12-21 02:03:24 +00:00
|
|
|
// TODO: if that doesn't work then reduce precision.
|
|
|
|
|
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
|
2017-09-11 02:44:14 +00:00
|
|
|
assert(cycles_until_event_ == 0);
|
2017-10-22 01:00:40 +00:00
|
|
|
cycles_until_event_ += static_cast<int>(numerator / denominator);
|
2017-09-11 02:44:14 +00:00
|
|
|
assert(cycles_until_event_ >= 0);
|
2017-10-21 23:49:04 +00:00
|
|
|
subcycles_until_event_.length = static_cast<unsigned int>(numerator % denominator);
|
|
|
|
subcycles_until_event_.clock_rate = static_cast<unsigned int>(denominator);
|
2017-09-12 02:24:24 +00:00
|
|
|
subcycles_until_event_.simplify();
|
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
|
|
|
}
|