mirror of
https://github.com/TomHarte/CLK.git
synced 2024-11-25 16:31:42 +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 "../NumberTheory/Factors.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
using namespace Storage;
|
||||
|
||||
@ -16,29 +17,34 @@ TimedEventLoop::TimedEventLoop(unsigned int input_clock_rate) :
|
||||
|
||||
void TimedEventLoop::run_for_cycles(int number_of_cycles)
|
||||
{
|
||||
_time_into_interval += (unsigned int)_stepper->step((uint64_t)number_of_cycles);
|
||||
while(_time_into_interval >= _event_interval.length)
|
||||
_cycles_until_event -= number_of_cycles;
|
||||
while(_cycles_until_event <= 0)
|
||||
{
|
||||
process_next_event();
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int TimedEventLoop::get_cycles_until_next_event()
|
||||
{
|
||||
return (unsigned int)std::max(_cycles_until_event, 0);
|
||||
}
|
||||
|
||||
void TimedEventLoop::reset_timer()
|
||||
{
|
||||
_time_into_interval = 0;
|
||||
_stepper.reset();
|
||||
_error.set_zero();
|
||||
_cycles_until_event = 0;
|
||||
}
|
||||
|
||||
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);
|
||||
_event_interval.length *= common_clock_rate / _event_interval.clock_rate;
|
||||
_event_interval.clock_rate = common_clock_rate;
|
||||
if(common_clock_rate != _stepper->get_output_rate())
|
||||
{
|
||||
_stepper.reset(new SignalProcessing::Stepper(_event_interval.clock_rate, _input_clock_rate));
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// figure out how much time has been run since the last bit ended
|
||||
if(_stepper)
|
||||
{
|
||||
_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;
|
||||
unsigned int common_divisor = NumberTheory::greatest_common_divisor(_error.clock_rate, interval.clock_rate);
|
||||
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;
|
||||
|
||||
// build a quotient that is the sum of the time overrun plus the incoming time and adjust the time overrun
|
||||
// to be in terms of the new quotient
|
||||
unsigned int denominator = NumberTheory::least_common_multiple(_event_interval.clock_rate, interval.clock_rate);
|
||||
interval.length *= denominator / interval.clock_rate;
|
||||
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));
|
||||
}
|
||||
_cycles_until_event = (int)(numerator / denominator);
|
||||
_error.length = (unsigned int)(numerator % denominator);
|
||||
_error.clock_rate = (unsigned int)denominator;
|
||||
_error.simplify();
|
||||
}
|
||||
|
||||
Time TimedEventLoop::get_time_into_next_event()
|
||||
{
|
||||
Time result = _event_interval;
|
||||
result.length = _time_into_interval;
|
||||
// result.length = _time_into_interval;
|
||||
return result;
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ namespace Storage {
|
||||
*/
|
||||
void run_for_cycles(int number_of_cycles);
|
||||
|
||||
unsigned int get_cycles_until_next_event();
|
||||
|
||||
protected:
|
||||
/*!
|
||||
Sets the time interval, as a proportion of a second, until the next event should be triggered.
|
||||
@ -86,9 +88,9 @@ namespace Storage {
|
||||
|
||||
private:
|
||||
unsigned int _input_clock_rate;
|
||||
int _cycles_until_event;
|
||||
Time _event_interval;
|
||||
std::unique_ptr<SignalProcessing::Stepper> _stepper;
|
||||
uint32_t _time_into_interval;
|
||||
Time _error;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user