2016-01-18 21:46:41 +00:00
|
|
|
//
|
|
|
|
// Tape.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/01/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-01-18 21:46:41 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Tape.hpp"
|
|
|
|
|
2016-08-27 21:18:12 +00:00
|
|
|
using namespace Storage::Tape;
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Lifecycle
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2019-07-10 20:05:59 +00:00
|
|
|
TapePlayer::TapePlayer(int input_clock_rate) :
|
2016-07-29 11:15:46 +00:00
|
|
|
TimedEventLoop(input_clock_rate)
|
2016-06-26 23:03:57 +00:00
|
|
|
{}
|
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
Tape::Tape(TapeSerialiser &serialiser) : serialiser_(serialiser) {}
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Seeking
|
2016-09-11 21:09:00 +00:00
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
void Storage::Tape::Tape::seek(const Time seek_time) {
|
2017-07-21 22:55:03 +00:00
|
|
|
Time next_time(0);
|
|
|
|
reset();
|
|
|
|
while(next_time <= seek_time) {
|
2024-12-04 03:54:29 +00:00
|
|
|
next_pulse();
|
2017-07-21 22:55:03 +00:00
|
|
|
next_time += pulse_.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
Storage::Time Tape::current_time() {
|
2017-07-21 22:55:03 +00:00
|
|
|
Time time(0);
|
2024-12-04 03:54:29 +00:00
|
|
|
uint64_t steps = offset();
|
2017-07-21 22:55:03 +00:00
|
|
|
reset();
|
|
|
|
while(steps--) {
|
2024-12-04 03:54:29 +00:00
|
|
|
next_pulse();
|
2017-07-21 22:55:03 +00:00
|
|
|
time += pulse_.length;
|
|
|
|
}
|
|
|
|
return time;
|
2016-09-11 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void Storage::Tape::Tape::reset() {
|
2017-07-21 22:55:03 +00:00
|
|
|
offset_ = 0;
|
2024-12-04 03:28:57 +00:00
|
|
|
serialiser_.reset();
|
2016-09-11 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
Pulse Tape::next_pulse() {
|
|
|
|
pulse_ = serialiser_.next_pulse();
|
2017-07-21 22:55:03 +00:00
|
|
|
offset_++;
|
|
|
|
return pulse_;
|
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
uint64_t Tape::offset() const {
|
2017-07-21 22:55:03 +00:00
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tape::set_offset(uint64_t offset) {
|
2017-07-22 00:43:20 +00:00
|
|
|
if(offset == offset_) return;
|
|
|
|
if(offset < offset_) {
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
offset -= offset_;
|
2024-12-04 03:54:29 +00:00
|
|
|
while(offset--) next_pulse();
|
2016-09-11 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
bool Tape::is_at_end() const {
|
|
|
|
return serialiser_.is_at_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Player
|
2016-09-11 21:09:00 +00:00
|
|
|
|
2020-05-10 01:22:51 +00:00
|
|
|
ClockingHint::Preference TapePlayer::preferred_clocking() const {
|
2018-05-28 03:17:06 +00:00
|
|
|
return (!tape_ || tape_->is_at_end()) ? ClockingHint::Preference::None : ClockingHint::Preference::JustInTime;
|
2017-08-20 16:18:36 +00:00
|
|
|
}
|
|
|
|
|
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();
|
2024-12-04 03:54:29 +00:00
|
|
|
next_pulse();
|
2018-05-28 03:17:06 +00:00
|
|
|
update_clocking_observer();
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> TapePlayer::tape() {
|
2016-12-03 17:05:19 +00:00
|
|
|
return tape_;
|
2016-09-13 02:22:23 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
bool TapePlayer::has_tape() const {
|
2020-05-10 03:00:39 +00:00
|
|
|
return bool(tape_);
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
void TapePlayer::next_pulse() {
|
2016-07-29 09:19:01 +00:00
|
|
|
// get the new pulse
|
2017-08-20 16:18:36 +00:00
|
|
|
if(tape_) {
|
2024-12-04 03:54:29 +00:00
|
|
|
current_pulse_ = tape_->next_pulse();
|
2018-05-28 03:17:06 +00:00
|
|
|
if(tape_->is_at_end()) update_clocking_observer();
|
2017-12-23 23:42:04 +00:00
|
|
|
} else {
|
2016-12-03 17:05:19 +00:00
|
|
|
current_pulse_.length.length = 1;
|
|
|
|
current_pulse_.length.clock_rate = 1;
|
2024-12-04 03:28:57 +00:00
|
|
|
current_pulse_.type = 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
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
Pulse TapePlayer::current_pulse() const {
|
2021-03-13 00:15:35 +00:00
|
|
|
return current_pulse_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TapePlayer::complete_pulse() {
|
|
|
|
jump_to_next_event();
|
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void TapePlayer::run_for(const Cycles cycles) {
|
2017-06-11 21:29:22 +00:00
|
|
|
if(has_tape()) {
|
2017-07-25 01:19:05 +00:00
|
|
|
TimedEventLoop::run_for(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() {
|
2024-12-04 03:54:29 +00:00
|
|
|
process(current_pulse_);
|
|
|
|
next_pulse();
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
2016-10-20 23:33:25 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - Binary Player
|
2016-10-20 23:33:25 +00:00
|
|
|
|
2019-07-10 20:05:59 +00:00
|
|
|
BinaryTapePlayer::BinaryTapePlayer(int input_clock_rate) :
|
2017-11-11 03:57:03 +00:00
|
|
|
TapePlayer(input_clock_rate)
|
2016-10-20 23:33:25 +00:00
|
|
|
{}
|
|
|
|
|
2020-05-10 01:22:51 +00:00
|
|
|
ClockingHint::Preference BinaryTapePlayer::preferred_clocking() const {
|
2018-05-28 03:17:06 +00:00
|
|
|
if(!motor_is_running_) return ClockingHint::Preference::None;
|
|
|
|
return TapePlayer::preferred_clocking();
|
2017-08-20 16:18:36 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:29:22 +00:00
|
|
|
void BinaryTapePlayer::set_motor_control(bool enabled) {
|
2017-08-20 17:18:46 +00:00
|
|
|
if(motor_is_running_ != enabled) {
|
|
|
|
motor_is_running_ = enabled;
|
2018-05-28 03:17:06 +00:00
|
|
|
update_clocking_observer();
|
2021-03-13 04:09:51 +00:00
|
|
|
|
|
|
|
if(observer_) {
|
|
|
|
observer_->set_led_status("Tape motor", enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinaryTapePlayer::set_activity_observer(Activity::Observer *observer) {
|
|
|
|
observer_ = observer;
|
|
|
|
if(observer) {
|
|
|
|
observer->register_led("Tape motor");
|
|
|
|
observer_->set_led_status("Tape motor", motor_is_running_);
|
2017-08-20 17:18:46 +00:00
|
|
|
}
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
bool BinaryTapePlayer::motor_control() const {
|
2017-12-27 03:13:28 +00:00
|
|
|
return motor_is_running_;
|
|
|
|
}
|
|
|
|
|
2020-05-30 04:37:06 +00:00
|
|
|
void BinaryTapePlayer::set_tape_output(bool) {
|
2016-10-20 23:33:25 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
bool BinaryTapePlayer::input() const {
|
2017-07-19 02:49:11 +00:00
|
|
|
return motor_is_running_ && input_level_;
|
2016-10-20 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 02:05:29 +00:00
|
|
|
void BinaryTapePlayer::run_for(const Cycles cycles) {
|
2017-07-25 01:19:05 +00:00
|
|
|
if(motor_is_running_) TapePlayer::run_for(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
|
|
|
}
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
void BinaryTapePlayer::process(const Storage::Tape::Pulse &pulse) {
|
2024-12-04 03:28:57 +00:00
|
|
|
bool new_input_level = pulse.type == 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
|
|
|
}
|
|
|
|
}
|