2016-01-18 21:46:41 +00:00
|
|
|
//
|
|
|
|
// Tape.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Tape.hpp"
|
2016-07-29 09:19:01 +00:00
|
|
|
#include "../../NumberTheory/Factors.hpp"
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2016-08-27 21:18:12 +00:00
|
|
|
using namespace Storage::Tape;
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
#pragma mark - Lifecycle
|
2016-06-26 23:03:57 +00:00
|
|
|
|
|
|
|
TapePlayer::TapePlayer(unsigned int input_clock_rate) :
|
2016-07-29 11:15:46 +00:00
|
|
|
TimedEventLoop(input_clock_rate)
|
2016-06-26 23:03:57 +00:00
|
|
|
{}
|
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
#pragma mark - Seeking
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void Storage::Tape::Tape::seek(Time &seek_time) {
|
2016-12-03 17:05:19 +00:00
|
|
|
current_time_.set_zero();
|
|
|
|
next_time_.set_zero();
|
2017-07-07 02:31:45 +00:00
|
|
|
while(next_time_ <= seek_time) get_next_pulse();
|
2016-09-11 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void Storage::Tape::Tape::reset() {
|
2016-12-03 17:05:19 +00:00
|
|
|
current_time_.set_zero();
|
|
|
|
next_time_.set_zero();
|
2016-09-11 21:09:00 +00:00
|
|
|
virtual_reset();
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
Tape::Pulse Tape::get_next_pulse() {
|
2016-09-11 21:09:00 +00:00
|
|
|
Tape::Pulse pulse = virtual_get_next_pulse();
|
2016-12-03 17:05:19 +00:00
|
|
|
current_time_ = next_time_;
|
|
|
|
next_time_ += pulse.length;
|
2016-09-11 21:09:00 +00:00
|
|
|
return pulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Player
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void TapePlayer::set_tape(std::shared_ptr<Storage::Tape::Tape> tape) {
|
2016-12-03 17:05:19 +00:00
|
|
|
tape_ = tape;
|
2016-07-29 11:15:46 +00:00
|
|
|
reset_timer();
|
2016-06-26 23:03:57 +00:00
|
|
|
get_next_pulse();
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> TapePlayer::get_tape() {
|
2016-12-03 17:05:19 +00:00
|
|
|
return tape_;
|
2016-09-13 02:22:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
bool TapePlayer::has_tape() {
|
2016-12-03 17:05:19 +00:00
|
|
|
return (bool)tape_;
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void TapePlayer::get_next_pulse() {
|
2016-07-29 09:19:01 +00:00
|
|
|
// get the new pulse
|
2016-12-03 17:05:19 +00:00
|
|
|
if(tape_)
|
|
|
|
current_pulse_ = tape_->get_next_pulse();
|
2017-06-11 21:29:22 +00:00
|
|
|
else {
|
2016-12-03 17:05:19 +00:00
|
|
|
current_pulse_.length.length = 1;
|
|
|
|
current_pulse_.length.clock_rate = 1;
|
|
|
|
current_pulse_.type = Tape::Pulse::Zero;
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
2016-07-29 09:19:01 +00:00
|
|
|
|
2016-12-03 17:05:19 +00:00
|
|
|
set_next_event_time_interval(current_pulse_.length);
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void TapePlayer::run_for_cycles(int number_of_cycles) {
|
|
|
|
if(has_tape()) {
|
2016-08-27 21:18:12 +00:00
|
|
|
TimedEventLoop::run_for_cycles(number_of_cycles);
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void TapePlayer::run_for_input_pulse() {
|
2016-07-29 11:15:46 +00:00
|
|
|
jump_to_next_event();
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void TapePlayer::process_next_event() {
|
2016-12-03 17:05:19 +00:00
|
|
|
process_input_pulse(current_pulse_);
|
2016-06-26 23:03:57 +00:00
|
|
|
get_next_pulse();
|
|
|
|
}
|
2016-10-20 23:33:25 +00:00
|
|
|
|
|
|
|
#pragma mark - Binary Player
|
|
|
|
|
|
|
|
BinaryTapePlayer::BinaryTapePlayer(unsigned int input_clock_rate) :
|
2016-12-03 17:05:19 +00:00
|
|
|
TapePlayer(input_clock_rate), motor_is_running_(false)
|
2016-10-20 23:33:25 +00:00
|
|
|
{}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::set_motor_control(bool enabled) {
|
2016-12-03 17:05:19 +00:00
|
|
|
motor_is_running_ = enabled;
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::set_tape_output(bool set) {
|
2016-10-20 23:33:25 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
bool BinaryTapePlayer::get_input() {
|
2016-12-03 17:05:19 +00:00
|
|
|
return input_level_;
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::run_for_cycles(int number_of_cycles) {
|
2016-12-03 17:05:19 +00:00
|
|
|
if(motor_is_running_) TapePlayer::run_for_cycles(number_of_cycles);
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::set_delegate(Delegate *delegate) {
|
2016-12-03 17:05:19 +00:00
|
|
|
delegate_ = delegate;
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::process_input_pulse(Storage::Tape::Tape::Pulse pulse) {
|
2017-06-22 02:13:24 +00:00
|
|
|
bool new_input_level = pulse.type == Tape::Pulse::High;
|
2017-06-11 21:29:22 +00:00
|
|
|
if(input_level_ != new_input_level) {
|
2016-12-03 17:05:19 +00:00
|
|
|
input_level_ = new_input_level;
|
|
|
|
if(delegate_) delegate_->tape_did_change_input(this);
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
}
|