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"
|
2016-09-17 23:52:27 +00:00
|
|
|
#include <algorithm>
|
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-07-25 01:19:05 +00:00
|
|
|
cycles_until_event_ -= cycles.as_int();
|
2017-03-26 18:34:47 +00:00
|
|
|
while(cycles_until_event_ <= 0) {
|
2016-07-29 11:15:46 +00:00
|
|
|
process_next_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
unsigned int TimedEventLoop::get_cycles_until_next_event() {
|
2016-12-03 16:59:28 +00:00
|
|
|
return (unsigned int)std::max(cycles_until_event_, 0);
|
2016-09-17 23:52:27 +00:00
|
|
|
}
|
|
|
|
|
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].
|
2016-12-03 16:59:28 +00:00
|
|
|
int64_t denominator = (int64_t)interval.clock_rate * (int64_t)subcycles_until_event_.clock_rate;
|
2016-09-18 14:24:09 +00:00
|
|
|
int64_t numerator =
|
2016-12-03 16:59:28 +00:00
|
|
|
(int64_t)subcycles_until_event_.clock_rate * (int64_t)input_clock_rate_ * (int64_t)interval.length +
|
|
|
|
(int64_t)interval.clock_rate * (int64_t)subcycles_until_event_.length;
|
2016-09-18 14:24:09 +00:00
|
|
|
|
|
|
|
// Simplify now, to prepare for stuffing into possibly 32-bit quantities
|
|
|
|
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
|
|
|
|
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
|
2016-12-03 16:59:28 +00:00
|
|
|
cycles_until_event_ = (int)(numerator / denominator);
|
|
|
|
subcycles_until_event_.length = (unsigned int)(numerator % denominator);
|
|
|
|
subcycles_until_event_.clock_rate = (unsigned int)denominator;
|
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() {
|
2016-09-18 14:29:45 +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
|
|
|
}
|