2016-02-12 03:12:12 +00:00
|
|
|
//
|
|
|
|
// Flywheel.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 11/02/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-02-12 03:12:12 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Flywheel_hpp
|
|
|
|
#define Flywheel_hpp
|
|
|
|
|
2019-02-19 02:29:39 +00:00
|
|
|
#include <cassert>
|
2017-11-11 20:28:40 +00:00
|
|
|
#include <cstdlib>
|
2018-11-15 01:09:57 +00:00
|
|
|
#include <cstdint>
|
2016-04-15 00:30:45 +00:00
|
|
|
|
2016-02-12 03:12:12 +00:00
|
|
|
namespace Outputs {
|
2016-03-09 01:49:07 +00:00
|
|
|
namespace CRT {
|
2016-02-12 03:12:12 +00:00
|
|
|
|
|
|
|
/*!
|
2016-02-13 00:53:49 +00:00
|
|
|
Provides timing for a two-phase signal consisting of a retrace phase followed by a scan phase,
|
|
|
|
announcing the start and end of retrace and providing the abiliy to read the current
|
|
|
|
scanning position.
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2016-02-13 00:53:49 +00:00
|
|
|
The @c Flywheel will attempt to converge with timing implied by synchronisation pulses.
|
2016-02-12 03:12:12 +00:00
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
struct Flywheel {
|
2016-02-12 03:12:12 +00:00
|
|
|
/*!
|
|
|
|
Constructs an instance of @c Flywheel.
|
|
|
|
|
|
|
|
@param standard_period The expected amount of time between one synchronisation and the next.
|
|
|
|
@param retrace_time The amount of time it takes to complete a retrace.
|
2016-05-12 02:11:01 +00:00
|
|
|
@param sync_error_window The permitted deviation of sync timings from the norm.
|
2016-02-12 03:12:12 +00:00
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
Flywheel(int standard_period, int retrace_time, int sync_error_window) :
|
2016-11-21 03:48:31 +00:00
|
|
|
standard_period_(standard_period),
|
|
|
|
retrace_time_(retrace_time),
|
|
|
|
sync_error_window_(sync_error_window),
|
|
|
|
counter_before_retrace_(standard_period - retrace_time),
|
2018-11-15 01:09:57 +00:00
|
|
|
expected_next_sync_(standard_period) {}
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2016-02-13 00:53:49 +00:00
|
|
|
enum SyncEvent {
|
|
|
|
/// Indicates that no synchronisation events will occur in the queried window.
|
|
|
|
None,
|
|
|
|
/// Indicates that the next synchronisation event will be a transition into retrce.
|
|
|
|
StartRetrace,
|
|
|
|
/// Indicates that the next synchronisation event will be a transition out of retrace.
|
|
|
|
EndRetrace
|
|
|
|
};
|
2016-02-12 03:12:12 +00:00
|
|
|
/*!
|
|
|
|
Asks the flywheel for the first synchronisation event that will occur in a given time period,
|
|
|
|
indicating whether a synchronisation request occurred at the start of the query window.
|
|
|
|
|
|
|
|
@param sync_is_requested @c true indicates that the flywheel should act as though having
|
|
|
|
received a synchronisation request now; @c false indicates no such event was detected.
|
|
|
|
|
|
|
|
@param cycles_to_run_for The number of cycles to look ahead.
|
|
|
|
|
|
|
|
@param cycles_advanced After this method has completed, contains the amount of time until
|
|
|
|
the returned synchronisation event.
|
|
|
|
|
|
|
|
@returns The next synchronisation event.
|
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline SyncEvent get_next_event_in_period(bool sync_is_requested, int cycles_to_run_for, int *cycles_advanced) {
|
2020-01-03 04:13:12 +00:00
|
|
|
// If sync is signalled _now_, consider adjusting expected_next_sync_.
|
2017-03-26 18:34:47 +00:00
|
|
|
if(sync_is_requested) {
|
2020-01-21 02:58:34 +00:00
|
|
|
const auto last_sync = expected_next_sync_;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(counter_ < sync_error_window_ || counter_ > expected_next_sync_ - sync_error_window_) {
|
2018-11-15 01:09:57 +00:00
|
|
|
const int time_now = (counter_ < sync_error_window_) ? expected_next_sync_ + counter_ : counter_;
|
2016-11-21 03:48:31 +00:00
|
|
|
expected_next_sync_ = (3*expected_next_sync_ + time_now) >> 2;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2018-11-15 01:09:57 +00:00
|
|
|
++number_of_surprises_;
|
2016-02-19 04:22:06 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(counter_ < retrace_time_ + (expected_next_sync_ >> 1)) {
|
2016-11-21 03:48:31 +00:00
|
|
|
expected_next_sync_ = (3*expected_next_sync_ + standard_period_ + sync_error_window_) >> 2;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-11-21 03:48:31 +00:00
|
|
|
expected_next_sync_ = (3*expected_next_sync_ + standard_period_ - sync_error_window_) >> 2;
|
2016-02-12 04:43:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-21 02:58:34 +00:00
|
|
|
last_adjustment_ = expected_next_sync_ - last_sync;
|
2016-02-12 03:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SyncEvent proposed_event = SyncEvent::None;
|
2018-11-03 23:58:44 +00:00
|
|
|
int proposed_sync_time = cycles_to_run_for;
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2019-02-13 03:30:40 +00:00
|
|
|
// End an ongoing retrace?
|
2017-03-26 18:34:47 +00:00
|
|
|
if(counter_ < retrace_time_ && counter_ + proposed_sync_time >= retrace_time_) {
|
2016-11-21 03:48:31 +00:00
|
|
|
proposed_sync_time = retrace_time_ - counter_;
|
2016-02-12 03:12:12 +00:00
|
|
|
proposed_event = SyncEvent::EndRetrace;
|
|
|
|
}
|
|
|
|
|
2019-02-13 03:30:40 +00:00
|
|
|
// Start a retrace?
|
2017-03-26 18:34:47 +00:00
|
|
|
if(counter_ + proposed_sync_time >= expected_next_sync_) {
|
2016-11-21 03:48:31 +00:00
|
|
|
proposed_sync_time = expected_next_sync_ - counter_;
|
2016-02-12 03:12:12 +00:00
|
|
|
proposed_event = SyncEvent::StartRetrace;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cycles_advanced = proposed_sync_time;
|
|
|
|
return proposed_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Advances a nominated amount of time, applying a previously returned synchronisation event
|
|
|
|
at the end of that period.
|
|
|
|
|
|
|
|
@param cycles_advanced The amount of time to run for.
|
|
|
|
|
|
|
|
@param event The synchronisation event to apply after that period.
|
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline void apply_event(int cycles_advanced, SyncEvent event) {
|
2019-02-13 03:30:40 +00:00
|
|
|
// In debug builds, perform a sanity check for counter overflow.
|
|
|
|
#ifndef NDEBUG
|
|
|
|
const int old_counter = counter_;
|
|
|
|
#endif
|
2016-11-21 03:48:31 +00:00
|
|
|
counter_ += cycles_advanced;
|
2019-02-13 03:30:40 +00:00
|
|
|
assert(old_counter <= counter_);
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(event) {
|
2016-02-12 03:12:12 +00:00
|
|
|
default: return;
|
|
|
|
case StartRetrace:
|
2016-11-21 03:48:31 +00:00
|
|
|
counter_before_retrace_ = counter_ - retrace_time_;
|
|
|
|
counter_ = 0;
|
2016-02-12 03:12:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns the current output position; while in retrace this will go down towards 0, while in scan
|
|
|
|
it will go upward.
|
|
|
|
|
|
|
|
@returns The current output position.
|
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline int get_current_output_position() {
|
2017-03-26 18:34:47 +00:00
|
|
|
if(counter_ < retrace_time_) {
|
2018-11-15 01:09:57 +00:00
|
|
|
const int retrace_distance = int((int64_t(counter_) * int64_t(standard_period_)) / int64_t(retrace_time_));
|
2016-11-21 03:48:31 +00:00
|
|
|
if(retrace_distance > counter_before_retrace_) return 0;
|
|
|
|
return counter_before_retrace_ - retrace_distance;
|
2016-02-12 03:12:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-21 03:48:31 +00:00
|
|
|
return counter_ - retrace_time_;
|
2016-02-12 03:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2016-02-12 04:43:08 +00:00
|
|
|
@returns the amount of time since retrace last began. Time then counts monotonically up from zero.
|
2016-02-12 03:12:12 +00:00
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline int get_current_time() {
|
2016-11-21 03:48:31 +00:00
|
|
|
return counter_;
|
2016-02-12 03:12:12 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 04:43:08 +00:00
|
|
|
/*!
|
|
|
|
@returns whether the output is currently retracing.
|
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool is_in_retrace() {
|
2016-11-21 03:48:31 +00:00
|
|
|
return counter_ < retrace_time_;
|
2016-02-12 04:43:08 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 00:53:49 +00:00
|
|
|
/*!
|
2016-02-28 03:39:01 +00:00
|
|
|
@returns the expected length of the scan period (excluding retrace).
|
2016-02-13 00:53:49 +00:00
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline int get_scan_period() {
|
2016-11-21 03:48:31 +00:00
|
|
|
return standard_period_ - retrace_time_;
|
2016-02-13 00:53:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-28 03:39:01 +00:00
|
|
|
/*!
|
|
|
|
@returns the expected length of a complete scan and retrace cycle.
|
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline int get_standard_period() {
|
2016-11-21 03:48:31 +00:00
|
|
|
return standard_period_;
|
2016-02-28 03:39:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 02:58:34 +00:00
|
|
|
/*!
|
|
|
|
@returns the actual current period for a complete scan (including retrace).
|
|
|
|
*/
|
|
|
|
inline int get_locked_period() {
|
|
|
|
return expected_next_sync_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns the amount by which the @c locked_period was adjusted, the last time that an adjustment was applied.
|
|
|
|
*/
|
|
|
|
inline int get_last_period_adjustment() {
|
|
|
|
return last_adjustment_;
|
|
|
|
}
|
|
|
|
|
2016-02-19 04:22:06 +00:00
|
|
|
/*!
|
|
|
|
@returns the number of synchronisation events that have seemed surprising since the last time this method was called;
|
|
|
|
a low number indicates good synchronisation.
|
|
|
|
*/
|
2018-11-03 23:58:44 +00:00
|
|
|
inline int get_and_reset_number_of_surprises() {
|
|
|
|
const int result = number_of_surprises_;
|
2016-11-21 03:48:31 +00:00
|
|
|
number_of_surprises_ = 0;
|
2016-02-19 04:22:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-01-23 03:01:17 +00:00
|
|
|
/*!
|
|
|
|
@returns A count of the number of retraces so far performed.
|
|
|
|
*/
|
|
|
|
inline int get_number_of_retraces() {
|
|
|
|
return number_of_retraces_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns The amount of time this flywheel spends in retrace, as supplied at construction.
|
|
|
|
*/
|
|
|
|
inline int get_retrace_period() {
|
|
|
|
return retrace_time_;
|
|
|
|
}
|
|
|
|
|
2016-04-15 00:30:45 +00:00
|
|
|
/*!
|
2019-02-13 03:30:40 +00:00
|
|
|
@returns `true` if a sync is expected soon or if the time at which it was expected (or received) was recent.
|
2016-04-15 00:30:45 +00:00
|
|
|
*/
|
2017-03-26 18:34:47 +00:00
|
|
|
inline bool is_near_expected_sync() {
|
2019-02-13 03:30:40 +00:00
|
|
|
return
|
|
|
|
(counter_ < (standard_period_ / 100)) ||
|
|
|
|
(counter_ >= expected_next_sync_ - (standard_period_ / 100));
|
2016-04-15 00:30:45 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 03:12:12 +00:00
|
|
|
private:
|
2020-01-23 03:01:17 +00:00
|
|
|
const int standard_period_; // The idealised length of time between syncs.
|
|
|
|
const int retrace_time_; // A constant indicating the amount of time it takes to perform a retrace.
|
|
|
|
const int sync_error_window_; // A constant indicating the window either side of the next expected sync in which we'll accept other syncs.
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2020-01-23 03:01:17 +00:00
|
|
|
int counter_ = 0; // Time since the _start_ of the last sync.
|
|
|
|
int counter_before_retrace_; // The value of _counter immediately before retrace began.
|
|
|
|
int expected_next_sync_; // Our current expection of when the next sync will be encountered (which implies velocity).
|
2016-02-12 03:12:12 +00:00
|
|
|
|
2020-01-23 03:01:17 +00:00
|
|
|
int number_of_surprises_ = 0; // A count of the surprising syncs.
|
|
|
|
int number_of_retraces_ = 0; // A count of the number of retraces to date.
|
2016-02-19 04:22:06 +00:00
|
|
|
|
2020-01-23 03:01:17 +00:00
|
|
|
int last_adjustment_ = 0; // The amount by which expected_next_sync_ was adjusted at the last sync.
|
2020-01-21 02:58:34 +00:00
|
|
|
|
2016-02-12 03:12:12 +00:00
|
|
|
/*
|
|
|
|
Implementation notes:
|
|
|
|
|
|
|
|
Retrace takes a fixed amount of time and runs during [0, _retrace_time).
|
|
|
|
|
|
|
|
For the current line, scan then occurs from [_retrace_time, _expected_next_sync), at which point
|
|
|
|
retrace begins and the internal counter is reset.
|
|
|
|
|
|
|
|
All synchronisation events that occur within (-_sync_error_window, _sync_error_window) of the
|
2016-02-13 00:53:49 +00:00
|
|
|
expected synchronisation time will cause a proportional adjustment in the expected time for the next
|
|
|
|
synchronisation. Other synchronisation events are clamped as though they occurred in that range.
|
2016-02-12 03:12:12 +00:00
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
2016-03-09 01:49:07 +00:00
|
|
|
}
|
2016-02-12 03:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Flywheel_hpp */
|