From 572e8b52e16ddc9fa70f46b19168c391d803cf98 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 26 Jul 2019 17:20:32 -0400 Subject: [PATCH] At the cost of extra storage, attempts to simplify away potential rounding issues around the index hole. --- Storage/Disk/Drive.cpp | 11 ++++++++--- Storage/Disk/Drive.hpp | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Storage/Disk/Drive.cpp b/Storage/Disk/Drive.cpp index e2844c2a4..4142884d6 100644 --- a/Storage/Disk/Drive.cpp +++ b/Storage/Disk/Drive.cpp @@ -38,10 +38,15 @@ Drive::Drive(int input_clock_rate, int revolutions_per_minute, int number_of_hea Drive::Drive(int input_clock_rate, int number_of_heads) : Drive(input_clock_rate, 300, number_of_heads) {} void Drive::set_rotation_speed(float revolutions_per_minute) { - const float new_rotational_multiplier = 60.0f / revolutions_per_minute; + // Rationalise the supplied speed so that cycles_per_revolution_ is exact. + cycles_per_revolution_ = int(0.5f + float(get_input_clock_rate()) * 60.0f / revolutions_per_minute); + + // From there derive the appropriate rotational multiplier and possibly update the + // count of cycles since the index hole proportionally. + const float new_rotational_multiplier = float(cycles_per_revolution_) / float(get_input_clock_rate()); cycles_since_index_hole_ *= new_rotational_multiplier / rotational_multiplier_; rotational_multiplier_ = new_rotational_multiplier; - cycles_since_index_hole_ %= int(get_input_clock_rate() * rotational_multiplier_); + cycles_since_index_hole_ %= cycles_per_revolution_; } Drive::~Drive() { @@ -358,7 +363,7 @@ void Drive::end_writing() { } } patched_track_->add_segment(write_start_time_, write_segment_, clamp_writing_to_index_hole_); - cycles_since_index_hole_ %= int(get_input_clock_rate() * rotational_multiplier_); + cycles_since_index_hole_ %= cycles_per_revolution_; invalidate_track(); } } diff --git a/Storage/Disk/Drive.hpp b/Storage/Disk/Drive.hpp index 183976938..5bf2c0bde 100644 --- a/Storage/Disk/Drive.hpp +++ b/Storage/Disk/Drive.hpp @@ -198,6 +198,10 @@ class Drive: public ClockingHint::Source, public TimedEventLoop { // a new track. int cycles_since_index_hole_ = 0; + // The number of cycles that should fall within one revolution at the + // current rotation speed. + int cycles_per_revolution_ = 1; + // A record of head position and active head. HeadPosition head_position_; int head_ = 0;