mirror of
https://github.com/TomHarte/CLK.git
synced 2024-12-26 09:29:45 +00:00
92 lines
2.6 KiB
C++
92 lines
2.6 KiB
C++
//
|
|
// 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"
|
|
|
|
using namespace Storage;
|
|
|
|
TimedEventLoop::TimedEventLoop(unsigned int input_clock_rate) :
|
|
_input_clock_rate(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)
|
|
{
|
|
process_next_event();
|
|
}
|
|
}
|
|
|
|
void TimedEventLoop::reset_timer()
|
|
{
|
|
_time_into_interval = 0;
|
|
_stepper.reset();
|
|
}
|
|
|
|
void TimedEventLoop::reset_timer_to_offset(Time offset)
|
|
{
|
|
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()
|
|
{
|
|
reset_timer();
|
|
process_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;
|
|
|
|
// 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));
|
|
}
|
|
}
|
|
|
|
Time TimedEventLoop::get_time_into_next_event()
|
|
{
|
|
Time result = _event_interval;
|
|
result.length = _time_into_interval;
|
|
return result;
|
|
}
|