2016-09-26 01:24:16 +00:00
|
|
|
//
|
|
|
|
// Drive.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 25/09/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-09-26 01:24:16 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Drive.hpp"
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2017-11-10 03:04:49 +00:00
|
|
|
#include "Track/UnformattedTrack.hpp"
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2016-09-26 01:24:16 +00:00
|
|
|
#include <algorithm>
|
2017-09-10 21:33:01 +00:00
|
|
|
#include <cassert>
|
2018-05-15 00:01:20 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <chrono>
|
|
|
|
#include <random>
|
2016-09-26 01:24:16 +00:00
|
|
|
|
|
|
|
using namespace Storage::Disk;
|
|
|
|
|
2019-07-10 20:05:59 +00:00
|
|
|
Drive::Drive(int input_clock_rate, int revolutions_per_minute, int number_of_heads):
|
2017-09-10 18:43:20 +00:00
|
|
|
Storage::TimedEventLoop(input_clock_rate),
|
2018-05-15 00:03:32 +00:00
|
|
|
available_heads_(number_of_heads) {
|
2019-09-29 03:23:15 +00:00
|
|
|
set_rotation_speed(revolutions_per_minute);
|
2018-05-15 00:01:20 +00:00
|
|
|
|
|
|
|
const auto seed = static_cast<std::default_random_engine::result_type>(std::chrono::system_clock::now().time_since_epoch().count());
|
|
|
|
std::default_random_engine randomiser(seed);
|
|
|
|
|
|
|
|
// Get at least 64 bits of random information; rounding is likey to give this a slight bias.
|
|
|
|
random_source_ = 0;
|
|
|
|
auto half_range = (randomiser.max() - randomiser.min()) / 2;
|
|
|
|
for(int bit = 0; bit < 64; ++bit) {
|
|
|
|
random_source_ <<= 1;
|
|
|
|
random_source_ |= ((randomiser() - randomiser.min()) >= half_range) ? 1 : 0;
|
|
|
|
}
|
2017-09-10 18:43:20 +00:00
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
|
2019-07-10 20:05:59 +00:00
|
|
|
Drive::Drive(int input_clock_rate, int number_of_heads) : Drive(input_clock_rate, 300, number_of_heads) {}
|
2019-06-05 01:41:54 +00:00
|
|
|
|
|
|
|
void Drive::set_rotation_speed(float revolutions_per_minute) {
|
2019-07-26 21:20:32 +00:00
|
|
|
// 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());
|
2019-07-26 02:29:54 +00:00
|
|
|
cycles_since_index_hole_ *= new_rotational_multiplier / rotational_multiplier_;
|
|
|
|
rotational_multiplier_ = new_rotational_multiplier;
|
2019-07-26 21:20:32 +00:00
|
|
|
cycles_since_index_hole_ %= cycles_per_revolution_;
|
2019-06-05 01:41:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-07 23:14:18 +00:00
|
|
|
Drive::~Drive() {
|
|
|
|
if(disk_) disk_->flush_tracks();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Drive::set_disk(const std::shared_ptr<Disk> &disk) {
|
2017-10-07 23:14:18 +00:00
|
|
|
if(disk_) disk_->flush_tracks();
|
2016-12-03 16:59:28 +00:00
|
|
|
disk_ = disk;
|
2017-08-20 14:55:08 +00:00
|
|
|
has_disk_ = !!disk_;
|
2016-12-26 19:24:33 +00:00
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
invalidate_track();
|
2019-07-12 17:17:24 +00:00
|
|
|
did_set_disk();
|
2018-05-28 03:17:06 +00:00
|
|
|
update_clocking_observer();
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Drive::has_disk() {
|
2017-08-20 14:55:08 +00:00
|
|
|
return has_disk_;
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 03:17:06 +00:00
|
|
|
ClockingHint::Preference Drive::preferred_clocking() {
|
|
|
|
return (!motor_is_on_ || !has_disk_) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime;
|
2017-08-20 15:54:54 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Drive::get_is_track_zero() {
|
2018-05-07 03:17:36 +00:00
|
|
|
return head_position_ == HeadPosition(0);
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:17:36 +00:00
|
|
|
void Drive::step(HeadPosition offset) {
|
|
|
|
HeadPosition old_head_position = head_position_;
|
|
|
|
head_position_ += offset;
|
2018-05-11 01:58:14 +00:00
|
|
|
if(head_position_ < HeadPosition(0)) {
|
|
|
|
head_position_ = HeadPosition(0);
|
|
|
|
if(observer_) observer_->announce_drive_event(drive_name_, Activity::Observer::DriveEvent::StepBelowZero);
|
2018-05-12 01:44:08 +00:00
|
|
|
} else {
|
|
|
|
if(observer_) observer_->announce_drive_event(drive_name_, Activity::Observer::DriveEvent::StepNormal);
|
2018-05-11 01:58:14 +00:00
|
|
|
}
|
2017-09-10 18:43:20 +00:00
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
// If the head moved, flush the old track.
|
|
|
|
if(head_position_ != old_head_position) {
|
|
|
|
track_ = nullptr;
|
|
|
|
}
|
2019-06-05 01:41:54 +00:00
|
|
|
|
|
|
|
// Allow a subclass to react, if desired.
|
|
|
|
did_step(head_position_);
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 16:51:53 +00:00
|
|
|
std::shared_ptr<Track> Drive::step_to(HeadPosition offset) {
|
|
|
|
HeadPosition old_head_position = head_position_;
|
|
|
|
head_position_ = std::max(offset, HeadPosition(0));
|
|
|
|
|
2019-10-27 02:57:05 +00:00
|
|
|
if(disk_ && head_position_ != old_head_position) {
|
2018-06-09 16:51:53 +00:00
|
|
|
track_ = nullptr;
|
|
|
|
setup_track();
|
|
|
|
}
|
|
|
|
|
|
|
|
return track_;
|
|
|
|
}
|
|
|
|
|
2017-10-07 01:45:12 +00:00
|
|
|
void Drive::set_head(int head) {
|
2017-09-16 01:18:36 +00:00
|
|
|
head = std::min(head, available_heads_ - 1);
|
2017-09-10 21:33:01 +00:00
|
|
|
if(head != head_) {
|
|
|
|
head_ = head;
|
2017-09-10 18:43:20 +00:00
|
|
|
track_ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 01:41:54 +00:00
|
|
|
int Drive::get_head_count() {
|
|
|
|
return available_heads_;
|
|
|
|
}
|
|
|
|
|
2019-06-06 22:32:11 +00:00
|
|
|
bool Drive::get_tachometer() {
|
2019-06-07 01:36:19 +00:00
|
|
|
// I have made a guess here that the tachometer is a symmetric square wave;
|
|
|
|
// if that is correct then around 60 beats per rotation appears to be correct
|
|
|
|
// to proceed beyond the speed checks I've so far uncovered.
|
|
|
|
const float ticks_per_rotation = 60.0f; // 56 was too low; 64 too high.
|
|
|
|
return int(get_rotation() * 2.0f * ticks_per_rotation) & 1;
|
2019-06-06 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float Drive::get_rotation() {
|
2019-07-02 19:43:03 +00:00
|
|
|
return get_time_into_track();
|
2019-06-06 22:32:11 +00:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
float Drive::get_time_into_track() {
|
|
|
|
// i.e. amount of time since the index hole was seen, as a proportion of a second,
|
|
|
|
// converted to a proportion of a rotation.
|
|
|
|
return float(cycles_since_index_hole_) / (float(get_input_clock_rate()) * rotational_multiplier_);
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Drive::get_is_read_only() {
|
2016-12-25 03:11:31 +00:00
|
|
|
if(disk_) return disk_->get_is_read_only();
|
2017-08-13 15:50:49 +00:00
|
|
|
return true;
|
2016-12-25 03:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 01:15:56 +00:00
|
|
|
bool Drive::get_is_ready() {
|
2017-09-12 02:27:50 +00:00
|
|
|
return ready_index_count_ == 2;
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::set_motor_on(bool motor_is_on) {
|
2018-08-04 01:11:22 +00:00
|
|
|
if(motor_is_on_ != motor_is_on) {
|
|
|
|
motor_is_on_ = motor_is_on;
|
|
|
|
|
|
|
|
if(observer_) {
|
|
|
|
observer_->set_drive_motor_status(drive_name_, motor_is_on_);
|
|
|
|
if(announce_motor_led_) {
|
|
|
|
observer_->set_led_status(drive_name_, motor_is_on_);
|
|
|
|
}
|
2018-05-11 01:54:10 +00:00
|
|
|
}
|
|
|
|
|
2018-08-04 01:11:22 +00:00
|
|
|
if(!motor_is_on) {
|
|
|
|
ready_index_count_ = 0;
|
|
|
|
if(disk_) disk_->flush_tracks();
|
|
|
|
}
|
|
|
|
update_clocking_observer();
|
2017-09-12 02:27:50 +00:00
|
|
|
}
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
bool Drive::get_motor_on() {
|
|
|
|
return motor_is_on_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::set_event_delegate(Storage::Disk::Drive::EventDelegate *delegate) {
|
|
|
|
event_delegate_ = delegate;
|
|
|
|
}
|
|
|
|
|
2017-09-11 00:51:05 +00:00
|
|
|
void Drive::advance(const Cycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
cycles_since_index_hole_ += cycles.as_integral();
|
2017-09-11 00:51:05 +00:00
|
|
|
if(event_delegate_) event_delegate_->advance(cycles);
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
void Drive::run_for(const Cycles cycles) {
|
2019-10-27 02:57:05 +00:00
|
|
|
if(motor_is_on_) {
|
|
|
|
if(has_disk_) {
|
|
|
|
Time zero(0);
|
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
auto number_of_cycles = cycles.as_integral();
|
2019-10-27 02:57:05 +00:00
|
|
|
while(number_of_cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
auto cycles_until_next_event = get_cycles_until_next_event();
|
|
|
|
auto cycles_to_run_for = std::min(cycles_until_next_event, number_of_cycles);
|
2019-10-27 02:57:05 +00:00
|
|
|
if(!is_reading_ && cycles_until_bits_written_ > zero) {
|
2019-10-30 02:36:29 +00:00
|
|
|
auto write_cycles_target = cycles_until_bits_written_.get<Cycles::IntType>();
|
|
|
|
if(cycles_until_bits_written_.length % cycles_until_bits_written_.clock_rate) ++write_cycles_target;
|
2019-10-27 02:57:05 +00:00
|
|
|
cycles_to_run_for = std::min(cycles_to_run_for, write_cycles_target);
|
|
|
|
}
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2019-10-27 02:57:05 +00:00
|
|
|
number_of_cycles -= cycles_to_run_for;
|
|
|
|
if(!is_reading_) {
|
|
|
|
if(cycles_until_bits_written_ > zero) {
|
2019-10-30 02:36:29 +00:00
|
|
|
Storage::Time cycles_to_run_for_time(static_cast<int>(cycles_to_run_for));
|
2019-10-27 02:57:05 +00:00
|
|
|
if(cycles_until_bits_written_ <= cycles_to_run_for_time) {
|
|
|
|
if(event_delegate_) event_delegate_->process_write_completed();
|
|
|
|
if(cycles_until_bits_written_ <= cycles_to_run_for_time)
|
|
|
|
cycles_until_bits_written_.set_zero();
|
|
|
|
else
|
|
|
|
cycles_until_bits_written_ -= cycles_to_run_for_time;
|
|
|
|
} else {
|
2017-09-10 21:33:01 +00:00
|
|
|
cycles_until_bits_written_ -= cycles_to_run_for_time;
|
2019-10-27 02:57:05 +00:00
|
|
|
}
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-27 02:57:05 +00:00
|
|
|
TimedEventLoop::run_for(Cycles(cycles_to_run_for));
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
2019-10-27 02:57:05 +00:00
|
|
|
} else {
|
|
|
|
TimedEventLoop::run_for(cycles);
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Track timed event loop
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
void Drive::get_next_event(float duration_already_passed) {
|
2019-10-27 02:57:05 +00:00
|
|
|
if(!disk_) {
|
|
|
|
current_event_.type = Track::Event::IndexHole;
|
|
|
|
current_event_.length = 1.0f;
|
|
|
|
set_next_event_time_interval((current_event_.length - duration_already_passed) * rotational_multiplier_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-11 02:44:14 +00:00
|
|
|
// 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_) {
|
2019-07-02 19:43:03 +00:00
|
|
|
random_interval_ = 0.0f;
|
2017-09-11 02:44:14 +00:00
|
|
|
setup_track();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-14 23:17:34 +00:00
|
|
|
// If gain has now been turned up so as to generate noise, generate some noise.
|
2019-07-02 19:43:03 +00:00
|
|
|
if(random_interval_ > 0.0f) {
|
|
|
|
current_event_.type = Track::Event::FluxTransition;
|
|
|
|
current_event_.length = float(2 + (random_source_&1)) / 1000000.0f;
|
2018-05-14 23:17:34 +00:00
|
|
|
random_source_ = (random_source_ >> 1) | (random_source_ << 63);
|
|
|
|
|
|
|
|
if(random_interval_ < current_event_.length) {
|
|
|
|
current_event_.length = random_interval_;
|
2019-07-02 19:43:03 +00:00
|
|
|
random_interval_ = 0.0f;
|
2018-05-14 23:17:34 +00:00
|
|
|
} else {
|
|
|
|
random_interval_ -= current_event_.length;
|
|
|
|
}
|
|
|
|
set_next_event_time_interval(current_event_.length);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
if(track_) {
|
2019-07-02 19:43:03 +00:00
|
|
|
const auto track_event = track_->get_next_event();
|
|
|
|
current_event_.type = track_event.type;
|
|
|
|
current_event_.length = track_event.length.get<float>();
|
2017-09-10 21:33:01 +00:00
|
|
|
} else {
|
2019-07-02 19:43:03 +00:00
|
|
|
current_event_.length = 1.0f;
|
2017-09-10 21:33:01 +00:00
|
|
|
current_event_.type = Track::Event::IndexHole;
|
|
|
|
}
|
|
|
|
|
|
|
|
// divide interval, which is in terms of a single rotation of the disk, by rotation speed to
|
|
|
|
// convert it into revolutions per second; this is achieved by multiplying by rotational_multiplier_
|
2019-07-02 19:43:03 +00:00
|
|
|
float interval = std::max((current_event_.length - duration_already_passed) * rotational_multiplier_, 0.0f);
|
2018-05-14 23:17:34 +00:00
|
|
|
|
|
|
|
// An interval greater than 15ms => adjust gain up the point where noise starts happening.
|
|
|
|
// Seed that up and leave a 15ms gap until it starts.
|
2019-07-02 19:43:03 +00:00
|
|
|
const float safe_gain_period = 15.0f / 1000000.0f;
|
2018-05-14 23:17:34 +00:00
|
|
|
if(interval >= safe_gain_period) {
|
|
|
|
random_interval_ = interval - safe_gain_period;
|
|
|
|
interval = safe_gain_period;
|
|
|
|
}
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
set_next_event_time_interval(interval);
|
2017-08-09 01:15:56 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
void Drive::process_next_event() {
|
2017-09-11 02:44:14 +00:00
|
|
|
if(current_event_.type == Track::Event::IndexHole) {
|
2017-09-12 02:27:50 +00:00
|
|
|
if(ready_index_count_ < 2) ready_index_count_++;
|
2017-09-11 02:44:14 +00:00
|
|
|
cycles_since_index_hole_ = 0;
|
|
|
|
}
|
2017-09-15 02:32:13 +00:00
|
|
|
if(
|
|
|
|
event_delegate_ &&
|
|
|
|
(current_event_.type == Track::Event::IndexHole || is_reading_)
|
|
|
|
){
|
|
|
|
event_delegate_->process_event(current_event_);
|
|
|
|
}
|
2019-07-02 19:43:03 +00:00
|
|
|
get_next_event(0.0f);
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Track management
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
std::shared_ptr<Track> Drive::get_track() {
|
2017-10-07 01:45:12 +00:00
|
|
|
if(disk_) return disk_->get_track_at_position(Track::Address(head_, head_position_));
|
2016-09-26 01:24:16 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-25 03:11:31 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Drive::set_track(const std::shared_ptr<Track> &track) {
|
2017-10-07 01:45:12 +00:00
|
|
|
if(disk_) disk_->set_track_at_position(Track::Address(head_, head_position_), track);
|
2016-12-25 03:11:31 +00:00
|
|
|
}
|
2017-09-10 18:43:20 +00:00
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
void Drive::setup_track() {
|
|
|
|
track_ = get_track();
|
|
|
|
if(!track_) {
|
|
|
|
track_.reset(new UnformattedTrack);
|
|
|
|
}
|
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
float offset = 0.0f;
|
|
|
|
const auto track_time_now = get_time_into_track();
|
|
|
|
const auto time_found = track_->seek_to(Time(track_time_now)).get<float>();
|
2017-09-10 21:33:01 +00:00
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
// `time_found` can be greater than `track_time_now` if limited precision caused rounding.
|
2018-05-03 01:26:39 +00:00
|
|
|
if(time_found <= track_time_now) {
|
|
|
|
offset = track_time_now - time_found;
|
|
|
|
}
|
|
|
|
|
2019-07-27 03:23:01 +00:00
|
|
|
// Reseed cycles_since_index_hole_; 99.99% of the time it'll still be correct as is,
|
|
|
|
// but if the track has rounded one way or the other it may now be very slightly adrift.
|
|
|
|
cycles_since_index_hole_ = (int((time_found + offset) * cycles_per_revolution_)) % cycles_per_revolution_;
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
get_next_event(offset);
|
2017-09-10 18:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 21:33:01 +00:00
|
|
|
void Drive::invalidate_track() {
|
2019-07-02 19:43:03 +00:00
|
|
|
random_interval_ = 0.0f;
|
2017-09-10 21:33:01 +00:00
|
|
|
track_ = nullptr;
|
2017-09-10 23:23:23 +00:00
|
|
|
if(patched_track_) {
|
|
|
|
set_track(patched_track_);
|
|
|
|
patched_track_ = nullptr;
|
|
|
|
}
|
2017-09-10 18:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Writing
|
2017-09-10 21:33:01 +00:00
|
|
|
|
|
|
|
void Drive::begin_writing(Time bit_length, bool clamp_to_index_hole) {
|
2019-07-12 22:53:41 +00:00
|
|
|
// Do nothing if already writing.
|
2019-10-27 02:57:05 +00:00
|
|
|
// TODO: cope properly if there's no disk to write to.
|
|
|
|
if(!is_reading_ || !disk_) return;
|
2019-07-12 22:53:41 +00:00
|
|
|
|
|
|
|
// Get a copy of the track if that hasn't happened yet.
|
|
|
|
if(!track_) {
|
|
|
|
setup_track();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the relevant parameters, and kick off writing.
|
2017-09-10 21:33:01 +00:00
|
|
|
is_reading_ = false;
|
|
|
|
clamp_writing_to_index_hole_ = clamp_to_index_hole;
|
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
cycles_per_bit_ = Storage::Time(int(get_input_clock_rate())) * bit_length;
|
2017-09-10 23:23:23 +00:00
|
|
|
cycles_per_bit_.simplify();
|
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
write_segment_.length_of_a_bit = bit_length / Time(rotational_multiplier_);
|
2017-09-10 21:33:01 +00:00
|
|
|
write_segment_.data.clear();
|
|
|
|
|
2019-07-02 19:43:03 +00:00
|
|
|
write_start_time_ = Time(get_time_into_track());
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::write_bit(bool value) {
|
2018-07-01 16:05:41 +00:00
|
|
|
write_segment_.data.push_back(value);
|
2017-09-10 21:33:01 +00:00
|
|
|
cycles_until_bits_written_ += cycles_per_bit_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Drive::end_writing() {
|
2018-07-03 01:51:53 +00:00
|
|
|
// If the user modifies a track, it's scaled up to a "high" resolution and modifications
|
|
|
|
// are plotted on top of that.
|
2019-07-26 19:26:51 +00:00
|
|
|
//
|
|
|
|
// "High" is defined as: two samples per clock relative to an idiomatic
|
|
|
|
// 8Mhz disk controller and 300RPM disk speed.
|
|
|
|
const size_t high_resolution_track_rate = 3200000;
|
2018-07-02 02:49:57 +00:00
|
|
|
|
2017-09-16 21:07:36 +00:00
|
|
|
if(!is_reading_) {
|
|
|
|
is_reading_ = true;
|
2017-09-10 21:33:01 +00:00
|
|
|
|
|
|
|
if(!patched_track_) {
|
2017-09-16 21:07:36 +00:00
|
|
|
// Avoid creating a new patched track if this one is already patched
|
2018-07-02 02:49:57 +00:00
|
|
|
patched_track_ = std::dynamic_pointer_cast<PCMTrack>(track_);
|
|
|
|
if(!patched_track_ || !patched_track_->is_resampled_clone()) {
|
2019-07-16 02:40:45 +00:00
|
|
|
Track *const tr = track_.get();
|
2018-07-02 02:49:57 +00:00
|
|
|
patched_track_.reset(PCMTrack::resampled_clone(tr, high_resolution_track_rate));
|
2017-09-16 21:07:36 +00:00
|
|
|
}
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
2017-09-16 21:07:36 +00:00
|
|
|
patched_track_->add_segment(write_start_time_, write_segment_, clamp_writing_to_index_hole_);
|
2019-07-26 21:20:32 +00:00
|
|
|
cycles_since_index_hole_ %= cycles_per_revolution_;
|
2017-09-16 21:07:36 +00:00
|
|
|
invalidate_track();
|
2017-09-10 21:33:01 +00:00
|
|
|
}
|
2017-09-10 18:43:20 +00:00
|
|
|
}
|
2018-05-11 01:54:10 +00:00
|
|
|
|
2019-07-12 22:53:41 +00:00
|
|
|
bool Drive::is_writing() {
|
|
|
|
return !is_reading_;
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:54:10 +00:00
|
|
|
void Drive::set_activity_observer(Activity::Observer *observer, const std::string &name, bool add_motor_led) {
|
|
|
|
observer_ = observer;
|
|
|
|
announce_motor_led_ = add_motor_led;
|
|
|
|
if(observer) {
|
|
|
|
drive_name_ = name;
|
|
|
|
|
|
|
|
observer->register_drive(drive_name_);
|
|
|
|
observer->set_drive_motor_status(drive_name_, motor_is_on_);
|
|
|
|
|
|
|
|
if(add_motor_led) {
|
|
|
|
observer->register_led(drive_name_);
|
|
|
|
observer->set_led_status(drive_name_, motor_is_on_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|