2016-01-18 21:46:41 +00:00
|
|
|
//
|
|
|
|
// Tape.hpp
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2017-07-26 00:20:55 +00:00
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
2018-05-28 03:17:06 +00:00
|
|
|
#include "../../ClockReceiver/ClockingHintSource.hpp"
|
2017-08-20 16:18:36 +00:00
|
|
|
|
2016-07-29 11:15:46 +00:00
|
|
|
#include "../TimedEventLoop.hpp"
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2021-03-13 04:09:51 +00:00
|
|
|
#include "../../Activity/Source.hpp"
|
2024-12-04 03:28:57 +00:00
|
|
|
#include "../TargetPlatforms.hpp"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
2021-03-13 04:09:51 +00:00
|
|
|
|
2023-05-10 21:02:18 +00:00
|
|
|
namespace Storage::Tape {
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
struct Pulse {
|
|
|
|
enum Type {
|
|
|
|
High, Low, Zero
|
|
|
|
} type;
|
|
|
|
Time length;
|
|
|
|
|
|
|
|
Pulse(Type type, Time length) : type(type), length(length) {}
|
|
|
|
Pulse() = default;
|
|
|
|
};
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
/*!
|
|
|
|
Provdes the means for tape serialiserion.
|
|
|
|
*/
|
2024-12-04 03:28:57 +00:00
|
|
|
class TapeSerialiser {
|
|
|
|
public:
|
2024-12-04 03:54:29 +00:00
|
|
|
virtual Pulse next_pulse() = 0;
|
2024-12-04 03:28:57 +00:00
|
|
|
virtual void reset() = 0;
|
|
|
|
virtual bool is_at_end() const = 0;
|
|
|
|
};
|
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
/*!
|
|
|
|
Models a tape as a sequence of pulses, each pulse being of arbitrary length and described
|
|
|
|
by their relationship with zero:
|
|
|
|
- high pulses exit from zero upward before returning to it;
|
|
|
|
- low pulses exit from zero downward before returning to it;
|
|
|
|
- zero pulses run along zero.
|
|
|
|
*/
|
2016-01-18 21:46:41 +00:00
|
|
|
class Tape {
|
2024-12-02 02:44:14 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
/*!
|
|
|
|
If at the start of the tape returns the first stored pulse. Otherwise advances past
|
|
|
|
the last-returned pulse and returns the next.
|
|
|
|
|
|
|
|
@returns the pulse that begins at the current cursor position.
|
|
|
|
*/
|
2024-12-04 03:54:29 +00:00
|
|
|
Pulse next_pulse();
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
/// Returns the tape to the beginning.
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
/// @returns @c true if the tape has progressed beyond all recorded content; @c false otherwise.
|
2024-12-04 03:28:57 +00:00
|
|
|
bool is_at_end() const;
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Returns a numerical representation of progression into the tape. Precision is arbitrary but
|
|
|
|
required to be at least to the whole pulse. Greater numbers are later than earlier numbers,
|
|
|
|
but not necessarily continuous.
|
|
|
|
*/
|
2024-12-04 03:54:29 +00:00
|
|
|
uint64_t offset() const;
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Moves the tape to the first time at which the specified offset would be returned by get_offset.
|
|
|
|
*/
|
2024-12-04 03:28:57 +00:00
|
|
|
void set_offset(uint64_t);
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Calculates and returns the amount of time that has elapsed since the time began. Potentially expensive.
|
|
|
|
*/
|
2024-12-04 03:54:29 +00:00
|
|
|
Time current_time();
|
2024-12-02 02:44:14 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Seeks to @c time. Potentially expensive.
|
|
|
|
*/
|
2024-12-04 03:28:57 +00:00
|
|
|
void seek(Time);
|
2024-12-02 02:44:14 +00:00
|
|
|
|
2024-12-04 03:28:57 +00:00
|
|
|
Tape(TapeSerialiser &);
|
2024-12-02 02:44:14 +00:00
|
|
|
virtual ~Tape() = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t offset_;
|
2024-12-04 03:28:57 +00:00
|
|
|
Pulse pulse_;
|
|
|
|
TapeSerialiser &serialiser_;
|
2016-01-18 21:46:41 +00:00
|
|
|
};
|
|
|
|
|
2016-07-10 12:54:39 +00:00
|
|
|
/*!
|
|
|
|
Provides a helper for: (i) retaining a reference to a tape; and (ii) running the tape at a certain
|
|
|
|
input clock rate.
|
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
Will call @c process(Pulse) instantaneously upon reaching *the end* of a pulse. Therefore a subclass
|
|
|
|
can decode pulses into data within @c process, using the supplied pulse's @c length and @c type.
|
2016-07-10 12:54:39 +00:00
|
|
|
*/
|
2018-05-28 03:17:06 +00:00
|
|
|
class TapePlayer: public TimedEventLoop, public ClockingHint::Source {
|
2024-12-02 02:44:14 +00:00
|
|
|
public:
|
|
|
|
TapePlayer(int input_clock_rate);
|
|
|
|
virtual ~TapePlayer() = default;
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
void set_tape(std::shared_ptr<Storage::Tape::Tape>);
|
2024-12-04 03:28:57 +00:00
|
|
|
bool has_tape() const;
|
2024-12-04 03:54:29 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> tape();
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
void run_for(const Cycles cycles);
|
|
|
|
void run_for_input_pulse();
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
ClockingHint::Preference preferred_clocking() const override;
|
2017-08-20 16:18:36 +00:00
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
Pulse current_pulse() const;
|
2024-12-02 02:44:14 +00:00
|
|
|
void complete_pulse();
|
2021-03-13 00:15:35 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
protected:
|
|
|
|
virtual void process_next_event() override;
|
2024-12-04 03:54:29 +00:00
|
|
|
virtual void process(const Pulse &) = 0;
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
private:
|
2024-12-04 03:54:29 +00:00
|
|
|
inline void next_pulse();
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> tape_;
|
2024-12-04 03:28:57 +00:00
|
|
|
Pulse current_pulse_;
|
2016-06-26 23:03:57 +00:00
|
|
|
};
|
|
|
|
|
2016-10-20 23:33:25 +00:00
|
|
|
/*!
|
|
|
|
A specific subclass of the tape player for machines that sample such as to report only either a
|
|
|
|
high or a low current input level.
|
|
|
|
|
|
|
|
Such machines can use @c get_input() to get the current level of the input.
|
|
|
|
|
|
|
|
They can also provide a delegate to be notified upon any change in the input level.
|
|
|
|
*/
|
2019-08-16 03:14:40 +00:00
|
|
|
class BinaryTapePlayer : public TapePlayer {
|
2024-12-02 02:44:14 +00:00
|
|
|
public:
|
|
|
|
BinaryTapePlayer(int input_clock_rate);
|
2024-12-04 03:54:29 +00:00
|
|
|
void set_motor_control(bool);
|
|
|
|
bool motor_control() const;
|
2017-12-27 03:13:28 +00:00
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
void set_tape_output(bool);
|
|
|
|
bool input() const;
|
2016-10-16 01:21:18 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
void run_for(const Cycles cycles);
|
2016-10-16 01:32:59 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
struct Delegate {
|
2024-12-04 03:54:29 +00:00
|
|
|
virtual void tape_did_change_input(BinaryTapePlayer *) = 0;
|
2024-12-02 02:44:14 +00:00
|
|
|
};
|
|
|
|
void set_delegate(Delegate *delegate);
|
2016-10-16 01:21:18 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
ClockingHint::Preference preferred_clocking() const final;
|
2017-08-20 16:18:36 +00:00
|
|
|
|
2024-12-04 03:54:29 +00:00
|
|
|
void set_activity_observer(Activity::Observer *);
|
2021-03-13 04:09:51 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
protected:
|
|
|
|
Delegate *delegate_ = nullptr;
|
2024-12-04 03:54:29 +00:00
|
|
|
void process(const Pulse &) final;
|
2024-12-02 02:44:14 +00:00
|
|
|
bool input_level_ = false;
|
|
|
|
bool motor_is_running_ = false;
|
2021-03-13 04:09:51 +00:00
|
|
|
|
2024-12-02 02:44:14 +00:00
|
|
|
Activity::Observer *observer_ = nullptr;
|
2016-10-16 01:21:18 +00:00
|
|
|
};
|
|
|
|
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|