1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-19 08:31:11 +00:00

Fixes extra time accumulation during track running.

Introduces a bunch of further asserts, which aided me in determining the fix, i.e. that Drives being responsible for their own setup_track could double-pump the event loop.
This commit is contained in:
Thomas Harte 2017-09-10 22:44:14 -04:00
parent dc0b65f9c9
commit 6d6cac429d
2 changed files with 31 additions and 8 deletions

View File

@ -66,6 +66,7 @@ Storage::Time Drive::get_time_into_track() {
Time result(cycles_since_index_hole_, (int)get_input_clock_rate()); Time result(cycles_since_index_hole_, (int)get_input_clock_rate());
result /= rotational_multiplier_; result /= rotational_multiplier_;
result.simplify(); result.simplify();
assert(result <= Time(1));
return result; return result;
} }
@ -92,6 +93,7 @@ void Drive::set_event_delegate(Storage::Disk::Drive::EventDelegate *delegate) {
} }
void Drive::advance(const Cycles cycles) { void Drive::advance(const Cycles cycles) {
cycles_since_index_hole_ += (unsigned int)cycles.as_int();
if(event_delegate_) event_delegate_->advance(cycles); if(event_delegate_) event_delegate_->advance(cycles);
} }
@ -99,9 +101,6 @@ void Drive::run_for(const Cycles cycles) {
Time zero(0); Time zero(0);
if(has_disk_ && motor_is_on_) { if(has_disk_ && motor_is_on_) {
// Grab a new track if not already in possession of one.
if(!track_) setup_track();
int number_of_cycles = cycles.as_int(); int number_of_cycles = cycles.as_int();
while(number_of_cycles) { while(number_of_cycles) {
int cycles_until_next_event = (int)get_cycles_until_next_event(); int cycles_until_next_event = (int)get_cycles_until_next_event();
@ -112,8 +111,6 @@ void Drive::run_for(const Cycles cycles) {
cycles_to_run_for = std::min(cycles_to_run_for, write_cycles_target); cycles_to_run_for = std::min(cycles_to_run_for, write_cycles_target);
} }
cycles_since_index_hole_ += (unsigned int)cycles_to_run_for;
number_of_cycles -= cycles_to_run_for; number_of_cycles -= cycles_to_run_for;
if(!is_reading_) { if(!is_reading_) {
if(cycles_until_bits_written_ > zero) { if(cycles_until_bits_written_ > zero) {
@ -137,6 +134,13 @@ void Drive::run_for(const Cycles cycles) {
#pragma mark - Track timed event loop #pragma mark - Track timed event loop
void Drive::get_next_event(const Time &duration_already_passed) { void Drive::get_next_event(const Time &duration_already_passed) {
// Grab a new track if not already in possession of one. This will recursively call get_next_event,
// supplying a proper duration_already_passed.
if(!track_) {
setup_track();
return;
}
if(track_) { if(track_) {
current_event_ = track_->get_next_event(); current_event_ = track_->get_next_event();
} else { } else {
@ -154,7 +158,10 @@ void Drive::get_next_event(const Time &duration_already_passed) {
void Drive::process_next_event() { void Drive::process_next_event() {
// TODO: ready test here. // TODO: ready test here.
if(current_event_.type == Track::Event::IndexHole) cycles_since_index_hole_ = 0; if(current_event_.type == Track::Event::IndexHole) {
assert(get_time_into_track() == Time(1) || get_time_into_track() == Time(0));
cycles_since_index_hole_ = 0;
}
if(event_delegate_) event_delegate_->process_event(current_event_); if(event_delegate_) event_delegate_->process_event(current_event_);
get_next_event(Time(0)); get_next_event(Time(0));
} }
@ -181,9 +188,9 @@ void Drive::setup_track() {
assert(track_time_now >= Time(0) && current_event_.length <= Time(1)); assert(track_time_now >= Time(0) && current_event_.length <= Time(1));
Time time_found = track_->seek_to(track_time_now); Time time_found = track_->seek_to(track_time_now);
assert(time_found >= Time(0) && time_found <= track_time_now); assert(time_found >= Time(0) && time_found < Time(1) && time_found <= track_time_now);
offset = track_time_now - time_found;
offset = track_time_now - time_found;
get_next_event(offset); get_next_event(offset);
} }

View File

@ -8,7 +8,9 @@
#include "TimedEventLoop.hpp" #include "TimedEventLoop.hpp"
#include "../NumberTheory/Factors.hpp" #include "../NumberTheory/Factors.hpp"
#include <algorithm> #include <algorithm>
#include <cassert>
using namespace Storage; using namespace Storage;
@ -17,8 +19,14 @@ TimedEventLoop::TimedEventLoop(unsigned int input_clock_rate) :
void TimedEventLoop::run_for(const Cycles cycles) { void TimedEventLoop::run_for(const Cycles cycles) {
int remaining_cycles = cycles.as_int(); int remaining_cycles = cycles.as_int();
#ifndef NDEBUG
int cycles_advanced = 0;
#endif
while(cycles_until_event_ <= remaining_cycles) { while(cycles_until_event_ <= remaining_cycles) {
#ifndef NDEBUG
cycles_advanced += cycles_until_event_;
#endif
advance(cycles_until_event_); advance(cycles_until_event_);
remaining_cycles -= cycles_until_event_; remaining_cycles -= cycles_until_event_;
cycles_until_event_ = 0; cycles_until_event_ = 0;
@ -27,8 +35,14 @@ void TimedEventLoop::run_for(const Cycles cycles) {
if(remaining_cycles) { if(remaining_cycles) {
cycles_until_event_ -= remaining_cycles; cycles_until_event_ -= remaining_cycles;
#ifndef NDEBUG
cycles_advanced += remaining_cycles;
#endif
advance(remaining_cycles); advance(remaining_cycles);
} }
assert(cycles_advanced == cycles.as_int());
assert(cycles_until_event_ > 0);
} }
unsigned int TimedEventLoop::get_cycles_until_next_event() { unsigned int TimedEventLoop::get_cycles_until_next_event() {
@ -65,7 +79,9 @@ void TimedEventLoop::set_next_event_time_interval(Time interval) {
// So this event will fire in the integral number of cycles from now, putting us at the remainder // So this event will fire in the integral number of cycles from now, putting us at the remainder
// number of subcycles // number of subcycles
assert(cycles_until_event_ == 0);
cycles_until_event_ += (int)(numerator / denominator); cycles_until_event_ += (int)(numerator / denominator);
assert(cycles_until_event_ >= 0);
subcycles_until_event_.length = (unsigned int)(numerator % denominator); subcycles_until_event_.length = (unsigned int)(numerator % denominator);
subcycles_until_event_.clock_rate = (unsigned int)denominator; subcycles_until_event_.clock_rate = (unsigned int)denominator;
} }