2016-07-15 10:51:11 +00:00
|
|
|
//
|
2016-09-26 00:05:56 +00:00
|
|
|
// DiskController.cpp
|
2016-07-15 10:51:11 +00:00
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 14/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2016-09-26 00:05:56 +00:00
|
|
|
#include "DiskController.hpp"
|
2016-12-25 17:31:38 +00:00
|
|
|
#include "../../NumberTheory/Factors.hpp"
|
2016-07-15 12:28:34 +00:00
|
|
|
|
2016-08-27 21:18:12 +00:00
|
|
|
using namespace Storage::Disk;
|
2016-07-15 12:28:34 +00:00
|
|
|
|
2016-09-26 00:05:56 +00:00
|
|
|
Controller::Controller(unsigned int clock_rate, unsigned int clock_rate_multiplier, unsigned int revolutions_per_minute) :
|
2017-03-26 18:34:47 +00:00
|
|
|
clock_rate_(clock_rate * clock_rate_multiplier),
|
|
|
|
clock_rate_multiplier_(clock_rate_multiplier),
|
|
|
|
rotational_multiplier_(60u, revolutions_per_minute),
|
2016-12-24 20:18:46 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
cycles_since_index_hole_(0),
|
|
|
|
motor_is_on_(false),
|
2016-12-24 20:18:46 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
is_reading_(true),
|
2016-07-29 11:31:02 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
TimedEventLoop(clock_rate * clock_rate_multiplier) {
|
2016-09-26 01:24:16 +00:00
|
|
|
// seed this class with a PLL, any PLL, so that it's safe to assume non-nullptr later
|
2016-12-24 20:18:46 +00:00
|
|
|
Time one(1);
|
2016-09-26 01:24:16 +00:00
|
|
|
set_expected_bit_length(one);
|
2016-07-29 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::setup_track() {
|
2016-12-03 16:59:28 +00:00
|
|
|
track_ = drive_->get_track();
|
2016-07-29 09:19:01 +00:00
|
|
|
|
2016-12-02 23:36:47 +00:00
|
|
|
Time offset;
|
2016-12-24 18:07:23 +00:00
|
|
|
Time track_time_now = get_time_into_track();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(track_) {
|
2016-12-24 18:07:23 +00:00
|
|
|
Time time_found = track_->seek_to(track_time_now);
|
|
|
|
offset = track_time_now - time_found;
|
2016-08-03 12:16:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 01:54:27 +00:00
|
|
|
get_next_event(offset);
|
2016-07-29 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::run_for_cycles(int number_of_cycles) {
|
2016-12-25 17:31:38 +00:00
|
|
|
Time zero(0);
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(drive_ && drive_->has_disk() && motor_is_on_) {
|
2016-12-03 16:59:28 +00:00
|
|
|
if(!track_) setup_track();
|
2016-12-24 18:07:23 +00:00
|
|
|
|
2016-12-25 14:16:09 +00:00
|
|
|
number_of_cycles *= clock_rate_multiplier_;
|
2017-03-26 18:34:47 +00:00
|
|
|
while(number_of_cycles) {
|
2016-12-25 14:16:09 +00:00
|
|
|
int cycles_until_next_event = (int)get_cycles_until_next_event();
|
|
|
|
int cycles_to_run_for = std::min(cycles_until_next_event, number_of_cycles);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!is_reading_ && cycles_until_bits_written_ > zero) {
|
2016-12-25 20:46:49 +00:00
|
|
|
int write_cycles_target = (int)cycles_until_bits_written_.get_unsigned_int();
|
|
|
|
if(cycles_until_bits_written_.length % cycles_until_bits_written_.clock_rate) write_cycles_target++;
|
|
|
|
cycles_to_run_for = std::min(cycles_to_run_for, write_cycles_target);
|
|
|
|
}
|
2016-12-25 14:16:09 +00:00
|
|
|
|
|
|
|
cycles_since_index_hole_ += (unsigned int)cycles_to_run_for;
|
|
|
|
|
|
|
|
number_of_cycles -= cycles_to_run_for;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(is_reading_) {
|
2016-12-25 17:31:38 +00:00
|
|
|
pll_->run_for_cycles(cycles_to_run_for);
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(cycles_until_bits_written_ > zero) {
|
2016-12-25 20:46:49 +00:00
|
|
|
Storage::Time cycles_to_run_for_time(cycles_to_run_for);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(cycles_until_bits_written_ <= cycles_to_run_for_time) {
|
2016-12-25 17:31:38 +00:00
|
|
|
process_write_completed();
|
2016-12-25 21:35:39 +00:00
|
|
|
if(cycles_until_bits_written_ <= cycles_to_run_for_time)
|
2016-12-25 20:46:49 +00:00
|
|
|
cycles_until_bits_written_.set_zero();
|
|
|
|
else
|
|
|
|
cycles_until_bits_written_ -= cycles_to_run_for_time;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-25 20:46:49 +00:00
|
|
|
cycles_until_bits_written_ -= cycles_to_run_for_time;
|
2016-12-25 17:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-25 14:16:09 +00:00
|
|
|
TimedEventLoop::run_for_cycles(cycles_to_run_for);
|
2016-07-31 17:32:30 +00:00
|
|
|
}
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Track timed event loop
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::get_next_event(const Time &duration_already_passed) {
|
|
|
|
if(track_) {
|
2016-12-03 16:59:28 +00:00
|
|
|
current_event_ = track_->get_next_event();
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
2016-12-03 16:59:28 +00:00
|
|
|
current_event_.length.length = 1;
|
|
|
|
current_event_.length.clock_rate = 1;
|
|
|
|
current_event_.type = Track::Event::IndexHole;
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-25 02:54:43 +00:00
|
|
|
// 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_
|
2016-12-25 01:54:27 +00:00
|
|
|
set_next_event_time_interval((current_event_.length - duration_already_passed) * rotational_multiplier_);
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
2016-07-29 09:19:01 +00:00
|
|
|
|
2016-09-26 00:05:56 +00:00
|
|
|
void Controller::process_next_event()
|
2016-07-29 11:31:02 +00:00
|
|
|
{
|
2017-03-26 18:34:47 +00:00
|
|
|
switch(current_event_.type) {
|
2016-07-29 11:31:02 +00:00
|
|
|
case Track::Event::FluxTransition:
|
2016-12-25 14:19:18 +00:00
|
|
|
if(is_reading_) pll_->add_pulse();
|
2016-07-29 11:31:02 +00:00
|
|
|
break;
|
|
|
|
case Track::Event::IndexHole:
|
2016-12-24 20:18:46 +00:00
|
|
|
printf("%p %d [/%d = %d]\n", this, cycles_since_index_hole_, clock_rate_multiplier_, cycles_since_index_hole_ / clock_rate_multiplier_);
|
2016-12-03 16:59:28 +00:00
|
|
|
cycles_since_index_hole_ = 0;
|
2016-07-29 11:31:02 +00:00
|
|
|
process_index_hole();
|
|
|
|
break;
|
2016-07-29 09:19:01 +00:00
|
|
|
}
|
2016-12-25 01:54:27 +00:00
|
|
|
get_next_event(Time(0));
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Time Controller::get_time_into_track() {
|
2016-12-24 18:07:23 +00:00
|
|
|
// this is proportion of a second
|
|
|
|
Time result(cycles_since_index_hole_, 8000000 * clock_rate_multiplier_);
|
|
|
|
result /= rotational_multiplier_;
|
|
|
|
result.simplify();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Writing
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::begin_writing() {
|
2016-12-25 02:54:43 +00:00
|
|
|
is_reading_ = false;
|
|
|
|
|
2016-12-26 00:19:22 +00:00
|
|
|
write_segment_.length_of_a_bit = bit_length_ / rotational_multiplier_;
|
2016-12-24 18:07:23 +00:00
|
|
|
write_segment_.data.clear();
|
|
|
|
write_segment_.number_of_bits = 0;
|
|
|
|
|
2016-12-25 02:54:43 +00:00
|
|
|
write_start_time_ = get_time_into_track();
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::write_bit(bool value) {
|
2016-12-25 02:54:43 +00:00
|
|
|
bool needs_new_byte = !(write_segment_.number_of_bits&7);
|
|
|
|
if(needs_new_byte) write_segment_.data.push_back(0);
|
|
|
|
if(value) write_segment_.data[write_segment_.number_of_bits >> 3] |= 0x80 >> (write_segment_.number_of_bits & 7);
|
|
|
|
write_segment_.number_of_bits++;
|
2016-12-25 17:31:38 +00:00
|
|
|
|
|
|
|
cycles_until_bits_written_ += cycles_per_bit_;
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::end_writing() {
|
2016-12-25 02:54:43 +00:00
|
|
|
is_reading_ = true;
|
2016-12-25 03:51:26 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!patched_track_) {
|
2016-12-26 03:00:39 +00:00
|
|
|
// Avoid creating a new patched track if this one is already patched
|
|
|
|
patched_track_ = std::dynamic_pointer_cast<PCMPatchedTrack>(track_);
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!patched_track_) {
|
2016-12-26 03:00:39 +00:00
|
|
|
patched_track_.reset(new PCMPatchedTrack(track_));
|
|
|
|
}
|
2016-12-25 03:51:26 +00:00
|
|
|
}
|
|
|
|
patched_track_->add_segment(write_start_time_, write_segment_);
|
2016-12-26 03:00:39 +00:00
|
|
|
invalidate_track(); // TEMPORARY: to force a seek
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-26 01:24:16 +00:00
|
|
|
#pragma mark - PLL control and delegate
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::set_expected_bit_length(Time bit_length) {
|
2016-12-03 16:59:28 +00:00
|
|
|
bit_length_ = bit_length;
|
2016-12-25 17:32:25 +00:00
|
|
|
bit_length_.simplify();
|
2016-09-26 01:24:16 +00:00
|
|
|
|
2016-12-25 21:35:39 +00:00
|
|
|
cycles_per_bit_ = Storage::Time(clock_rate_) * bit_length;
|
2016-12-25 17:32:25 +00:00
|
|
|
cycles_per_bit_.simplify();
|
2016-12-25 17:31:38 +00:00
|
|
|
|
2016-09-26 01:24:16 +00:00
|
|
|
// this conversion doesn't need to be exact because there's a lot of variation to be taken
|
|
|
|
// account of in rotation speed, air turbulence, etc, so a direct conversion will do
|
2016-12-25 21:35:39 +00:00
|
|
|
int clocks_per_bit = (int)cycles_per_bit_.get_unsigned_int();
|
2016-12-03 16:59:28 +00:00
|
|
|
pll_.reset(new DigitalPhaseLockedLoop(clocks_per_bit, clocks_per_bit / 5, 3));
|
|
|
|
pll_->set_delegate(this);
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
2016-07-29 11:31:02 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::digital_phase_locked_loop_output_bit(int value) {
|
2016-12-03 16:59:28 +00:00
|
|
|
process_input_bit(value, cycles_since_index_hole_);
|
2016-07-16 00:35:19 +00:00
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
|
|
|
|
#pragma mark - Drive actions
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Controller::get_is_track_zero() {
|
2016-12-03 16:59:28 +00:00
|
|
|
if(!drive_) return false;
|
|
|
|
return drive_->get_is_track_zero();
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Controller::get_drive_is_ready() {
|
2016-12-03 16:59:28 +00:00
|
|
|
if(!drive_) return false;
|
|
|
|
return drive_->has_disk();
|
2016-12-02 02:13:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Controller::get_drive_is_read_only() {
|
2016-12-25 14:46:12 +00:00
|
|
|
if(!drive_) return false;
|
|
|
|
return drive_->get_is_read_only();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::step(int direction) {
|
2016-09-26 01:24:16 +00:00
|
|
|
invalidate_track();
|
2016-12-26 00:19:22 +00:00
|
|
|
if(drive_) drive_->step(direction);
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::set_motor_on(bool motor_on) {
|
2016-12-03 16:59:28 +00:00
|
|
|
motor_is_on_ = motor_on;
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
bool Controller::get_motor_on() {
|
2016-12-03 16:59:28 +00:00
|
|
|
return motor_is_on_;
|
2016-12-01 03:26:02 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::set_drive(std::shared_ptr<Drive> drive) {
|
2016-12-26 01:38:25 +00:00
|
|
|
if(drive_ != drive)
|
|
|
|
{
|
|
|
|
invalidate_track();
|
|
|
|
drive_ = drive;
|
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::invalidate_track() {
|
2016-12-03 16:59:28 +00:00
|
|
|
track_ = nullptr;
|
2017-03-26 18:34:47 +00:00
|
|
|
if(patched_track_) {
|
2016-12-26 00:19:22 +00:00
|
|
|
drive_->set_track(patched_track_);
|
|
|
|
patched_track_ = nullptr;
|
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
2016-12-25 17:31:38 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Controller::process_write_completed() {}
|