2016-07-05 18:55:20 +00:00
|
|
|
//
|
|
|
|
// SerialPort.cpp
|
|
|
|
// 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
|
|
|
//
|
|
|
|
|
|
|
|
#include "SerialBus.hpp"
|
|
|
|
|
2017-11-11 01:37:18 +00:00
|
|
|
#include <cstdio>
|
2019-03-02 23:07:05 +00:00
|
|
|
#include <iostream>
|
2017-11-11 01:37:18 +00:00
|
|
|
|
2016-07-05 18:55:20 +00:00
|
|
|
using namespace Commodore::Serial;
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
const char *::Commodore::Serial::StringForLine(Line line) {
|
|
|
|
switch(line) {
|
2016-07-06 11:46:21 +00:00
|
|
|
case ServiceRequest: return "Service request";
|
|
|
|
case Attention: return "Attention";
|
|
|
|
case Clock: return "Clock";
|
|
|
|
case Data: return "Data";
|
|
|
|
case Reset: return "Reset";
|
2017-11-12 21:37:18 +00:00
|
|
|
default: return nullptr;
|
2016-07-06 11:46:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void ::Commodore::Serial::AttachPortAndBus(std::shared_ptr<Port> port, std::shared_ptr<Bus> bus) {
|
2016-07-10 11:46:20 +00:00
|
|
|
port->set_serial_bus(bus);
|
|
|
|
bus->add_port(port);
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Bus::add_port(std::shared_ptr<Port> port) {
|
2016-12-03 18:14:03 +00:00
|
|
|
ports_.push_back(port);
|
2017-10-22 01:00:40 +00:00
|
|
|
for(int line = static_cast<int>(ServiceRequest); line <= static_cast<int>(Reset); line++) {
|
2016-07-06 00:57:31 +00:00
|
|
|
// the addition of a new device may change the line output...
|
2017-10-22 01:00:40 +00:00
|
|
|
set_line_output_did_change(static_cast<Line>(line));
|
2016-07-06 00:57:31 +00:00
|
|
|
|
|
|
|
// ... but the new device will need to be told the current state regardless
|
2017-10-22 01:00:40 +00:00
|
|
|
port->set_input(static_cast<Line>(line), line_levels_[line]);
|
2016-07-06 00:57:31 +00:00
|
|
|
}
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void Bus::set_line_output_did_change(Line line) {
|
2016-07-07 10:44:13 +00:00
|
|
|
// i.e. I believe these lines to be open collector
|
|
|
|
LineLevel new_line_level = High;
|
2017-03-26 18:34:47 +00:00
|
|
|
for(std::weak_ptr<Port> port : ports_) {
|
2016-07-05 18:55:20 +00:00
|
|
|
std::shared_ptr<Port> locked_port = port.lock();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(locked_port) {
|
2018-09-12 00:37:15 +00:00
|
|
|
new_line_level = (LineLevel)(static_cast<bool>(new_line_level) & static_cast<bool>(locked_port->get_output(line)));
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 00:52:33 +00:00
|
|
|
// post an update only if one occurred
|
2017-03-26 18:34:47 +00:00
|
|
|
if(new_line_level != line_levels_[line]) {
|
2016-12-03 18:14:03 +00:00
|
|
|
line_levels_[line] = new_line_level;
|
2016-07-05 18:55:20 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
for(std::weak_ptr<Port> port : ports_) {
|
2016-07-05 18:55:20 +00:00
|
|
|
std::shared_ptr<Port> locked_port = port.lock();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(locked_port) {
|
2016-07-07 10:44:13 +00:00
|
|
|
locked_port->set_input(line, new_line_level);
|
2016-07-05 18:55:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 02:25:44 +00:00
|
|
|
|
2017-11-12 20:59:11 +00:00
|
|
|
// MARK: - The debug port
|
2016-07-10 02:25:44 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
void DebugPort::set_input(Line line, LineLevel value) {
|
2016-12-03 18:14:03 +00:00
|
|
|
input_levels_[line] = value;
|
2016-07-10 02:25:44 +00:00
|
|
|
|
2019-03-02 23:07:05 +00:00
|
|
|
std::cout << "[Bus] " << StringForLine(line) << " is " << (value ? "high" : "low");
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!incoming_count_) {
|
2016-12-03 18:14:03 +00:00
|
|
|
incoming_count_ = (!input_levels_[Line::Clock] && !input_levels_[Line::Data]) ? 8 : 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
} else {
|
|
|
|
if(line == Line::Clock && value) {
|
2016-12-03 18:14:03 +00:00
|
|
|
incoming_byte_ = (incoming_byte_ >> 1) | (input_levels_[Line::Data] ? 0x80 : 0x00);
|
2016-07-10 02:25:44 +00:00
|
|
|
}
|
2016-12-03 18:14:03 +00:00
|
|
|
incoming_count_--;
|
2019-03-02 23:07:05 +00:00
|
|
|
if(incoming_count_ == 0) std::cout << "[Bus] Observed value " << std::hex << int(incoming_byte_);
|
2016-07-10 02:25:44 +00:00
|
|
|
}
|
|
|
|
}
|