2016-07-05 18:55:20 +00:00
|
|
|
//
|
|
|
|
// SerialPort.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-05 18:55:20 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef SerialBus_hpp
|
|
|
|
#define SerialBus_hpp
|
|
|
|
|
2017-11-11 01:37:18 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
2016-09-19 12:16:06 +00:00
|
|
|
#include <vector>
|
2016-07-05 18:55:20 +00:00
|
|
|
|
|
|
|
namespace Commodore {
|
|
|
|
namespace Serial {
|
|
|
|
|
|
|
|
enum Line {
|
|
|
|
ServiceRequest = 0,
|
|
|
|
Attention,
|
|
|
|
Clock,
|
|
|
|
Data,
|
|
|
|
Reset
|
|
|
|
};
|
|
|
|
|
2016-07-07 10:44:13 +00:00
|
|
|
enum LineLevel: bool {
|
|
|
|
High = true,
|
|
|
|
Low = false
|
|
|
|
};
|
|
|
|
|
2016-07-05 18:55:20 +00:00
|
|
|
class Port;
|
2016-07-10 11:46:20 +00:00
|
|
|
class Bus;
|
2016-07-05 18:55:20 +00:00
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Returns a C string giving a human-readable name for the supplied line.
|
|
|
|
*/
|
|
|
|
const char *StringForLine(Line line);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Calls both of the necessary methods to (i) set @c bus as the target for @c port outputs; and
|
|
|
|
(ii) add @c port as one of the targets to which @c bus propagates line changes.
|
|
|
|
*/
|
|
|
|
void AttachPortAndBus(std::shared_ptr<Port> port, std::shared_ptr<Bus> bus);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A serial bus is responsible for retaining a weakly-held collection of attached ports and for deciding the
|
|
|
|
current bus levels based upon the net result of each port's output, and for communicating changes in bus
|
|
|
|
levels to every port.
|
|
|
|
*/
|
2016-07-05 18:55:20 +00:00
|
|
|
class Bus {
|
|
|
|
public:
|
2016-12-03 18:14:03 +00:00
|
|
|
Bus() : line_levels_{High, High, High, High, High} {}
|
2016-07-05 18:55:20 +00:00
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Adds the supplied port to the bus.
|
|
|
|
*/
|
2016-07-05 18:55:20 +00:00
|
|
|
void add_port(std::shared_ptr<Port> port);
|
2016-07-10 11:46:20 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Communicates to the bus that one of its attached port has changed its output level for the given line.
|
|
|
|
The bus will therefore recalculate bus state and propagate as necessary.
|
|
|
|
*/
|
2016-07-05 18:55:20 +00:00
|
|
|
void set_line_output_did_change(Line line);
|
|
|
|
|
|
|
|
private:
|
2016-12-03 18:14:03 +00:00
|
|
|
LineLevel line_levels_[5];
|
|
|
|
std::vector<std::weak_ptr<Port>> ports_;
|
2016-07-05 18:55:20 +00:00
|
|
|
};
|
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
A serial port is an endpoint on a serial bus; this class provides a direct setter for current line outputs and
|
|
|
|
expects to be subclassed in order for specific port-housing devices to deal with input.
|
|
|
|
*/
|
2016-07-05 18:55:20 +00:00
|
|
|
class Port {
|
|
|
|
public:
|
2016-12-03 18:14:03 +00:00
|
|
|
Port() : line_levels_{High, High, High, High, High} {}
|
2016-07-05 18:55:20 +00:00
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Sets the current level of an output line on this serial port.
|
|
|
|
*/
|
2016-07-07 10:44:13 +00:00
|
|
|
void set_output(Line line, LineLevel level) {
|
2017-03-26 18:34:47 +00:00
|
|
|
if(line_levels_[line] != level) {
|
2016-12-03 18:14:03 +00:00
|
|
|
line_levels_[line] = level;
|
|
|
|
std::shared_ptr<Bus> bus = serial_bus_.lock();
|
2016-07-09 19:40:25 +00:00
|
|
|
if(bus) bus->set_line_output_did_change(line);
|
|
|
|
}
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Gets the previously set level of an output line.
|
|
|
|
*/
|
2016-07-07 10:44:13 +00:00
|
|
|
LineLevel get_output(Line line) {
|
2016-12-03 18:14:03 +00:00
|
|
|
return line_levels_[line];
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Called by the bus to signal a change in any input line level. Subclasses should implement this.
|
|
|
|
*/
|
2016-07-07 10:44:13 +00:00
|
|
|
virtual void set_input(Line line, LineLevel value) = 0;
|
2016-07-05 18:55:20 +00:00
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
Sets the supplied serial bus as that to which line levels will be communicated.
|
|
|
|
*/
|
2016-07-05 18:55:20 +00:00
|
|
|
inline void set_serial_bus(std::shared_ptr<Bus> serial_bus) {
|
2016-12-03 18:14:03 +00:00
|
|
|
serial_bus_ = serial_bus;
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-03 18:14:03 +00:00
|
|
|
std::weak_ptr<Bus> serial_bus_;
|
|
|
|
LineLevel line_levels_[5];
|
2016-07-05 18:55:20 +00:00
|
|
|
};
|
|
|
|
|
2016-07-10 11:46:20 +00:00
|
|
|
/*!
|
|
|
|
A debugging port, which makes some attempt to log bus activity. Incomplete. TODO: complete.
|
|
|
|
*/
|
2016-07-10 02:25:44 +00:00
|
|
|
class DebugPort: public Port {
|
|
|
|
public:
|
|
|
|
void set_input(Line line, LineLevel value);
|
|
|
|
|
2016-12-03 18:14:03 +00:00
|
|
|
DebugPort() : incoming_count_(0) {}
|
2016-07-10 02:25:44 +00:00
|
|
|
|
|
|
|
private:
|
2016-12-03 18:14:03 +00:00
|
|
|
uint8_t incoming_byte_;
|
|
|
|
int incoming_count_;
|
|
|
|
LineLevel input_levels_[5];
|
2016-07-10 02:25:44 +00:00
|
|
|
};
|
|
|
|
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SerialPort_hpp */
|