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"
|
2017-09-10 23:23:23 +00:00
|
|
|
|
2017-09-23 02:39:23 +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
|
|
|
|
2017-09-11 02:56:05 +00:00
|
|
|
Controller::Controller(Cycles clock_rate) :
|
|
|
|
clock_rate_multiplier_(128000000 / clock_rate.as_int()),
|
|
|
|
clock_rate_(clock_rate.as_int() * clock_rate_multiplier_),
|
2017-09-16 01:18:36 +00:00
|
|
|
empty_drive_(new Drive((unsigned int)clock_rate.as_int(), 1, 1)) {
|
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);
|
2017-09-10 23:23:23 +00:00
|
|
|
set_drive(empty_drive_);
|
2016-07-29 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 15:54:54 +00:00
|
|
|
void Controller::set_component_is_sleeping(void *component, bool is_sleeping) {
|
|
|
|
update_sleep_observer();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Controller::is_sleeping() {
|
2017-09-10 23:23:23 +00:00
|
|
|
return !drive_ || drive_->is_sleeping();
|
2017-08-20 15:54:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void Controller::run_for(const Cycles cycles) {
|
2017-09-10 23:23:23 +00:00
|
|
|
if(drive_) drive_->run_for(cycles);
|
2016-07-29 11:31:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
Drive &Controller::get_drive() {
|
|
|
|
return *drive_.get();
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
#pragma mark - Drive::EventDelegate
|
2016-12-24 18:07:23 +00:00
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void Controller::process_event(const Track::Event &event) {
|
|
|
|
switch(event.type) {
|
|
|
|
case Track::Event::FluxTransition: pll_->add_pulse(); break;
|
|
|
|
case Track::Event::IndexHole: process_index_hole(); break;
|
|
|
|
}
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void Controller::advance(const Cycles cycles) {
|
2017-09-15 02:30:40 +00:00
|
|
|
if(is_reading_) pll_->run_for(Cycles(cycles.as_int() * clock_rate_multiplier_));
|
2016-12-24 18:07:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void Controller::process_write_completed() {
|
|
|
|
// Provided for subclasses to override.
|
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
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
Time cycles_per_bit = Storage::Time(clock_rate_) * bit_length;
|
|
|
|
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
|
2017-09-10 23:23:23 +00:00
|
|
|
int clocks_per_bit = (int)cycles_per_bit.get_unsigned_int();
|
2017-07-16 23:03:50 +00:00
|
|
|
pll_.reset(new DigitalPhaseLockedLoop(clocks_per_bit, 3));
|
2016-12-03 16:59:28 +00:00
|
|
|
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) {
|
2017-09-15 02:30:40 +00:00
|
|
|
if(is_reading_) process_input_bit(value);
|
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) {
|
2017-08-15 20:05:10 +00:00
|
|
|
if(drive_ != drive) {
|
2017-09-10 23:23:23 +00:00
|
|
|
bool was_sleeping = is_sleeping();
|
|
|
|
// invalidate_track();
|
|
|
|
|
|
|
|
if(drive_) {
|
|
|
|
drive_->set_event_delegate(nullptr);
|
|
|
|
drive_->set_sleep_observer(nullptr);
|
|
|
|
}
|
2016-12-26 01:38:25 +00:00
|
|
|
drive_ = drive;
|
2017-09-10 23:23:23 +00:00
|
|
|
if(drive_) {
|
|
|
|
drive_->set_event_delegate(this);
|
|
|
|
drive_->set_sleep_observer(this);
|
|
|
|
} else {
|
|
|
|
drive_ = empty_drive_;
|
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
if(is_sleeping() != was_sleeping) {
|
|
|
|
update_sleep_observer();
|
|
|
|
}
|
2016-12-26 00:19:22 +00:00
|
|
|
}
|
2016-09-26 01:24:16 +00:00
|
|
|
}
|
2016-12-25 17:31:38 +00:00
|
|
|
|
2017-09-10 23:23:23 +00:00
|
|
|
void Controller::begin_writing(bool clamp_to_index_hole) {
|
2017-09-15 02:30:40 +00:00
|
|
|
is_reading_ = false;
|
2017-09-10 23:23:23 +00:00
|
|
|
get_drive().begin_writing(bit_length_, clamp_to_index_hole);
|
|
|
|
}
|
2017-09-15 02:30:40 +00:00
|
|
|
|
|
|
|
void Controller::end_writing() {
|
2017-09-16 21:07:36 +00:00
|
|
|
if(!is_reading_) {
|
|
|
|
is_reading_ = true;
|
|
|
|
get_drive().end_writing();
|
|
|
|
}
|
2017-09-15 02:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Controller::is_reading() {
|
|
|
|
return is_reading_;
|
|
|
|
}
|