1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-22 14:30:29 +00:00
CLK/Storage/Tape/Tape.hpp

172 lines
4.1 KiB
C++
Raw Normal View History

//
// Tape.hpp
// Clock Signal
//
// Created by Thomas Harte on 18/01/2016.
// Copyright 2016 Thomas Harte. All rights reserved.
//
#pragma once
#include "../../ClockReceiver/ClockReceiver.hpp"
#include "../../ClockReceiver/ClockingHintSource.hpp"
#include "../TimedEventLoop.hpp"
2021-03-13 04:09:51 +00:00
#include "../../Activity/Source.hpp"
#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 {
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.
*/
class TapeSerialiser {
public:
2024-12-04 03:54:29 +00:00
virtual Pulse next_pulse() = 0;
virtual void reset() = 0;
virtual bool is_at_end() const = 0;
};
/*!
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.
*/
class Tape {
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();
/// Returns the tape to the beginning.
void reset();
/// @returns @c true if the tape has progressed beyond all recorded content; @c false otherwise.
bool is_at_end() const;
/*!
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;
/*!
Moves the tape to the first time at which the specified offset would be returned by get_offset.
*/
void set_offset(uint64_t);
/*!
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();
/*!
Seeks to @c time. Potentially expensive.
*/
void seek(Time);
Tape(TapeSerialiser &);
virtual ~Tape() = default;
private:
uint64_t offset_;
Pulse pulse_;
TapeSerialiser &serialiser_;
};
/*!
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.
*/
class TapePlayer: public TimedEventLoop, public ClockingHint::Source {
public:
TapePlayer(int input_clock_rate);
virtual ~TapePlayer() = default;
2024-12-04 03:54:29 +00:00
void set_tape(std::shared_ptr<Storage::Tape::Tape>);
bool has_tape() const;
2024-12-04 03:54:29 +00:00
std::shared_ptr<Storage::Tape::Tape> tape();
void run_for(const Cycles cycles);
void run_for_input_pulse();
ClockingHint::Preference preferred_clocking() const override;
2024-12-04 03:54:29 +00:00
Pulse current_pulse() const;
void complete_pulse();
protected:
virtual void process_next_event() override;
2024-12-04 03:54:29 +00:00
virtual void process(const Pulse &) = 0;
private:
2024-12-04 03:54:29 +00:00
inline void next_pulse();
std::shared_ptr<Storage::Tape::Tape> tape_;
Pulse current_pulse_;
};
/*!
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.
*/
class BinaryTapePlayer : public TapePlayer {
public:
BinaryTapePlayer(int input_clock_rate);
2024-12-04 03:54:29 +00:00
void set_motor_control(bool);
bool motor_control() const;
2024-12-04 03:54:29 +00:00
void set_tape_output(bool);
bool input() const;
void run_for(const Cycles cycles);
struct Delegate {
2024-12-04 03:54:29 +00:00
virtual void tape_did_change_input(BinaryTapePlayer *) = 0;
};
void set_delegate(Delegate *delegate);
ClockingHint::Preference preferred_clocking() const final;
2024-12-04 03:54:29 +00:00
void set_activity_observer(Activity::Observer *);
2021-03-13 04:09:51 +00:00
protected:
Delegate *delegate_ = nullptr;
2024-12-04 03:54:29 +00:00
void process(const Pulse &) final;
bool input_level_ = false;
bool motor_is_running_ = false;
2021-03-13 04:09:51 +00:00
Activity::Observer *observer_ = nullptr;
};
}