mirror of
https://github.com/TomHarte/CLK.git
synced 2024-11-26 08:49:37 +00:00
Made an attempt to do the time base conversion upfront, saving a lot of hassle and allowing greater prediction.
This commit is contained in:
parent
a98374995e
commit
14a9edcf5d
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "TimedEventLoop.hpp"
|
#include "TimedEventLoop.hpp"
|
||||||
#include "../NumberTheory/Factors.hpp"
|
#include "../NumberTheory/Factors.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using namespace Storage;
|
using namespace Storage;
|
||||||
|
|
||||||
@ -16,29 +17,34 @@ TimedEventLoop::TimedEventLoop(unsigned int input_clock_rate) :
|
|||||||
|
|
||||||
void TimedEventLoop::run_for_cycles(int number_of_cycles)
|
void TimedEventLoop::run_for_cycles(int number_of_cycles)
|
||||||
{
|
{
|
||||||
_time_into_interval += (unsigned int)_stepper->step((uint64_t)number_of_cycles);
|
_cycles_until_event -= number_of_cycles;
|
||||||
while(_time_into_interval >= _event_interval.length)
|
while(_cycles_until_event <= 0)
|
||||||
{
|
{
|
||||||
process_next_event();
|
process_next_event();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int TimedEventLoop::get_cycles_until_next_event()
|
||||||
|
{
|
||||||
|
return (unsigned int)std::max(_cycles_until_event, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void TimedEventLoop::reset_timer()
|
void TimedEventLoop::reset_timer()
|
||||||
{
|
{
|
||||||
_time_into_interval = 0;
|
_error.set_zero();
|
||||||
_stepper.reset();
|
_cycles_until_event = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventLoop::reset_timer_to_offset(Time offset)
|
void TimedEventLoop::reset_timer_to_offset(Time offset)
|
||||||
{
|
{
|
||||||
unsigned int common_clock_rate = NumberTheory::least_common_multiple(offset.clock_rate, _event_interval.clock_rate);
|
/* unsigned int common_clock_rate = NumberTheory::least_common_multiple(offset.clock_rate, _event_interval.clock_rate);
|
||||||
_time_into_interval = offset.length * (common_clock_rate / offset.clock_rate);
|
_time_into_interval = offset.length * (common_clock_rate / offset.clock_rate);
|
||||||
_event_interval.length *= common_clock_rate / _event_interval.clock_rate;
|
_event_interval.length *= common_clock_rate / _event_interval.clock_rate;
|
||||||
_event_interval.clock_rate = common_clock_rate;
|
_event_interval.clock_rate = common_clock_rate;
|
||||||
if(common_clock_rate != _stepper->get_output_rate())
|
if(common_clock_rate != _stepper->get_output_rate())
|
||||||
{
|
{
|
||||||
_stepper.reset(new SignalProcessing::Stepper(_event_interval.clock_rate, _input_clock_rate));
|
_stepper.reset(new SignalProcessing::Stepper(_event_interval.clock_rate, _input_clock_rate));
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
void TimedEventLoop::jump_to_next_event()
|
void TimedEventLoop::jump_to_next_event()
|
||||||
@ -49,43 +55,19 @@ void TimedEventLoop::jump_to_next_event()
|
|||||||
|
|
||||||
void TimedEventLoop::set_next_event_time_interval(Time interval)
|
void TimedEventLoop::set_next_event_time_interval(Time interval)
|
||||||
{
|
{
|
||||||
// figure out how much time has been run since the last bit ended
|
unsigned int common_divisor = NumberTheory::greatest_common_divisor(_error.clock_rate, interval.clock_rate);
|
||||||
if(_stepper)
|
uint64_t denominator = (interval.clock_rate * _error.clock_rate) / common_divisor;
|
||||||
{
|
uint64_t numerator = (_error.clock_rate / common_divisor) * _input_clock_rate * interval.length - (interval.clock_rate / common_divisor) * _error.length;
|
||||||
_time_into_interval -= _event_interval.length;
|
|
||||||
if(_time_into_interval)
|
|
||||||
{
|
|
||||||
// simplify the quotient
|
|
||||||
unsigned int common_divisor = NumberTheory::greatest_common_divisor(_time_into_interval, _event_interval.clock_rate);
|
|
||||||
_time_into_interval /= common_divisor;
|
|
||||||
_event_interval.clock_rate /= common_divisor;
|
|
||||||
|
|
||||||
// build a quotient that is the sum of the time overrun plus the incoming time and adjust the time overrun
|
_cycles_until_event = (int)(numerator / denominator);
|
||||||
// to be in terms of the new quotient
|
_error.length = (unsigned int)(numerator % denominator);
|
||||||
unsigned int denominator = NumberTheory::least_common_multiple(_event_interval.clock_rate, interval.clock_rate);
|
_error.clock_rate = (unsigned int)denominator;
|
||||||
interval.length *= denominator / interval.clock_rate;
|
_error.simplify();
|
||||||
interval.clock_rate = denominator;
|
|
||||||
_time_into_interval *= denominator / _event_interval.clock_rate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_time_into_interval = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// store new interval
|
|
||||||
_event_interval = interval;
|
|
||||||
|
|
||||||
// adjust stepper if required
|
|
||||||
if(!_stepper || _event_interval.clock_rate != _stepper->get_output_rate())
|
|
||||||
{
|
|
||||||
_stepper.reset(new SignalProcessing::Stepper(_event_interval.clock_rate, _input_clock_rate));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Time TimedEventLoop::get_time_into_next_event()
|
Time TimedEventLoop::get_time_into_next_event()
|
||||||
{
|
{
|
||||||
Time result = _event_interval;
|
Time result = _event_interval;
|
||||||
result.length = _time_into_interval;
|
// result.length = _time_into_interval;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,8 @@ namespace Storage {
|
|||||||
*/
|
*/
|
||||||
void run_for_cycles(int number_of_cycles);
|
void run_for_cycles(int number_of_cycles);
|
||||||
|
|
||||||
|
unsigned int get_cycles_until_next_event();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/*!
|
/*!
|
||||||
Sets the time interval, as a proportion of a second, until the next event should be triggered.
|
Sets the time interval, as a proportion of a second, until the next event should be triggered.
|
||||||
@ -86,9 +88,9 @@ namespace Storage {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned int _input_clock_rate;
|
unsigned int _input_clock_rate;
|
||||||
|
int _cycles_until_event;
|
||||||
Time _event_interval;
|
Time _event_interval;
|
||||||
std::unique_ptr<SignalProcessing::Stepper> _stepper;
|
Time _error;
|
||||||
uint32_t _time_into_interval;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user