2016-01-18 21:46:41 +00:00
|
|
|
//
|
|
|
|
// Tape.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 18/01/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Tape_hpp
|
|
|
|
#define Tape_hpp
|
|
|
|
|
2016-06-26 23:03:57 +00:00
|
|
|
#include <memory>
|
2016-07-29 11:15:46 +00:00
|
|
|
#include "../TimedEventLoop.hpp"
|
2016-01-18 21:46:41 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
2016-08-27 21:09:45 +00:00
|
|
|
namespace Tape {
|
2016-01-18 21:46:41 +00:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
Subclasses should implement at least @c get_next_pulse and @c reset to provide a serial feeding
|
|
|
|
of pulses and the ability to return to the start of the feed. They may also implement @c seek if
|
|
|
|
a better implementation than a linear search from the @c reset time can be implemented.
|
|
|
|
*/
|
2016-01-18 21:46:41 +00:00
|
|
|
class Tape {
|
|
|
|
public:
|
2016-01-18 23:06:09 +00:00
|
|
|
struct Pulse {
|
2016-09-08 11:41:44 +00:00
|
|
|
enum Type {
|
2016-01-18 21:46:41 +00:00
|
|
|
High, Low, Zero
|
|
|
|
} type;
|
|
|
|
Time length;
|
2016-09-08 11:41:44 +00:00
|
|
|
|
|
|
|
Pulse(Type type, Time length) : type(type), length(length) {}
|
|
|
|
Pulse() {}
|
2016-01-18 21:46:41 +00:00
|
|
|
};
|
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
/*!
|
|
|
|
If at the start of the tape returns the first stored pulse. Otherwise advances past
|
|
|
|
the last-returned pulse and returns the next.
|
2016-08-30 01:53:06 +00:00
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
@returns the pulse that begins at the current cursor position.
|
|
|
|
*/
|
|
|
|
Pulse get_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.
|
2016-08-30 01:53:06 +00:00
|
|
|
virtual bool is_at_end() = 0;
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
/// @returns the amount of time preceeding the most recently-returned pulse.
|
2016-12-03 17:05:19 +00:00
|
|
|
virtual Time get_current_time() { return current_time_; }
|
2016-09-11 21:09:00 +00:00
|
|
|
|
|
|
|
/// Advances or reverses the tape to the last time before or at @c time from which a pulse starts.
|
|
|
|
virtual void seek(Time &time);
|
|
|
|
|
2017-07-12 02:41:10 +00:00
|
|
|
virtual ~Tape() {};
|
|
|
|
|
2016-09-11 21:09:00 +00:00
|
|
|
private:
|
2016-12-03 17:05:19 +00:00
|
|
|
Time current_time_, next_time_;
|
2016-09-11 21:09:00 +00:00
|
|
|
|
|
|
|
virtual Pulse virtual_get_next_pulse() = 0;
|
|
|
|
virtual void virtual_reset() = 0;
|
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.
|
|
|
|
|
|
|
|
Will call @c process_input_pulse instantaneously upon reaching *the end* of a pulse. Therefore a subclass
|
|
|
|
can decode pulses into data within process_input_pulse, using the supplied pulse's @c length and @c type.
|
|
|
|
*/
|
2016-07-29 11:15:46 +00:00
|
|
|
class TapePlayer: public TimedEventLoop {
|
2016-06-26 23:03:57 +00:00
|
|
|
public:
|
|
|
|
TapePlayer(unsigned int input_clock_rate);
|
|
|
|
|
2016-08-27 21:09:45 +00:00
|
|
|
void set_tape(std::shared_ptr<Storage::Tape::Tape> tape);
|
2016-06-26 23:03:57 +00:00
|
|
|
bool has_tape();
|
2016-09-13 02:22:23 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> get_tape();
|
2016-06-26 23:03:57 +00:00
|
|
|
|
2016-07-29 15:03:09 +00:00
|
|
|
void run_for_cycles(int number_of_cycles);
|
2016-06-26 23:03:57 +00:00
|
|
|
void run_for_input_pulse();
|
2016-01-18 21:46:41 +00:00
|
|
|
|
2016-06-26 23:03:57 +00:00
|
|
|
protected:
|
2016-07-29 11:15:46 +00:00
|
|
|
virtual void process_next_event();
|
2017-07-16 23:49:31 +00:00
|
|
|
virtual void process_input_pulse(const Tape::Pulse &pulse) = 0;
|
2016-06-26 23:03:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
inline void get_next_pulse();
|
|
|
|
|
2016-12-03 17:05:19 +00:00
|
|
|
std::shared_ptr<Storage::Tape::Tape> tape_;
|
|
|
|
Tape::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.
|
|
|
|
*/
|
2016-10-16 01:21:18 +00:00
|
|
|
class BinaryTapePlayer: public TapePlayer {
|
|
|
|
public:
|
2016-10-20 23:33:25 +00:00
|
|
|
BinaryTapePlayer(unsigned int input_clock_rate);
|
|
|
|
void set_motor_control(bool enabled);
|
|
|
|
void set_tape_output(bool set);
|
|
|
|
bool get_input();
|
2016-10-16 01:21:18 +00:00
|
|
|
|
2016-10-20 23:33:25 +00:00
|
|
|
void run_for_cycles(int number_of_cycles);
|
2016-10-16 01:32:59 +00:00
|
|
|
|
2016-10-16 01:21:18 +00:00
|
|
|
class Delegate {
|
|
|
|
public:
|
|
|
|
virtual void tape_did_change_input(BinaryTapePlayer *tape_player) = 0;
|
|
|
|
};
|
2016-10-20 23:33:25 +00:00
|
|
|
void set_delegate(Delegate *delegate);
|
2016-10-16 01:21:18 +00:00
|
|
|
|
2016-11-03 02:30:53 +00:00
|
|
|
protected:
|
2016-12-03 17:05:19 +00:00
|
|
|
Delegate *delegate_;
|
2017-07-16 23:49:31 +00:00
|
|
|
virtual void process_input_pulse(const Storage::Tape::Tape::Pulse &pulse);
|
2016-12-03 17:05:19 +00:00
|
|
|
bool input_level_;
|
|
|
|
bool motor_is_running_;
|
2016-10-16 01:21:18 +00:00
|
|
|
};
|
|
|
|
|
2016-08-27 21:09:45 +00:00
|
|
|
}
|
2016-06-26 23:03:57 +00:00
|
|
|
}
|
2016-01-18 21:46:41 +00:00
|
|
|
|
|
|
|
#endif /* Tape_hpp */
|