1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-27 01:31:42 +00:00

Accounts for periods near an expected sync after a sync actually occurred.

This commit is contained in:
Thomas Harte 2019-02-12 22:30:40 -05:00
parent 5d68a5bdd0
commit 77bebd4a65

View File

@ -79,13 +79,13 @@ struct Flywheel {
SyncEvent proposed_event = SyncEvent::None; SyncEvent proposed_event = SyncEvent::None;
int proposed_sync_time = cycles_to_run_for; int proposed_sync_time = cycles_to_run_for;
// will we end an ongoing retrace? // End an ongoing retrace?
if(counter_ < retrace_time_ && counter_ + proposed_sync_time >= retrace_time_) { if(counter_ < retrace_time_ && counter_ + proposed_sync_time >= retrace_time_) {
proposed_sync_time = retrace_time_ - counter_; proposed_sync_time = retrace_time_ - counter_;
proposed_event = SyncEvent::EndRetrace; proposed_event = SyncEvent::EndRetrace;
} }
// will we start a retrace? // Start a retrace?
if(counter_ + proposed_sync_time >= expected_next_sync_) { if(counter_ + proposed_sync_time >= expected_next_sync_) {
proposed_sync_time = expected_next_sync_ - counter_; proposed_sync_time = expected_next_sync_ - counter_;
proposed_event = SyncEvent::StartRetrace; proposed_event = SyncEvent::StartRetrace;
@ -104,7 +104,14 @@ struct Flywheel {
@param event The synchronisation event to apply after that period. @param event The synchronisation event to apply after that period.
*/ */
inline void apply_event(int cycles_advanced, SyncEvent event) { inline void apply_event(int cycles_advanced, SyncEvent event) {
// In debug builds, perform a sanity check for counter overflow.
#ifndef NDEBUG
const int old_counter = counter_;
#endif
counter_ += cycles_advanced; counter_ += cycles_advanced;
#ifndef NDEBUG
assert(old_counter <= counter_);
#endif
switch(event) { switch(event) {
default: return; default: return;
@ -170,10 +177,12 @@ struct Flywheel {
} }
/*! /*!
@returns `true` if a sync is expected soon or the time at which it was expected was recent. @returns `true` if a sync is expected soon or if the time at which it was expected (or received) was recent.
*/ */
inline bool is_near_expected_sync() { inline bool is_near_expected_sync() {
return abs(counter_ - expected_next_sync_) < standard_period_ / 50; return
(counter_ < (standard_period_ / 100)) ||
(counter_ >= expected_next_sync_ - (standard_period_ / 100));
} }
private: private: