1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-11-03 08:05:40 +00:00
CLK/Machines/Commodore/SerialBus.cpp

82 lines
2.3 KiB
C++
Raw Normal View History

//
// SerialPort.cpp
// Clock Signal
//
// Created by Thomas Harte on 05/07/2016.
// Copyright 2016 Thomas Harte. All rights reserved.
//
#include "SerialBus.hpp"
#include <cstdio>
2019-03-02 23:07:05 +00:00
#include <iostream>
using namespace Commodore::Serial;
2017-03-26 18:34:47 +00:00
const char *::Commodore::Serial::StringForLine(Line line) {
switch(line) {
case ServiceRequest: return "Service request";
case Attention: return "Attention";
case Clock: return "Clock";
case Data: return "Data";
case Reset: return "Reset";
default: return nullptr;
}
}
2017-03-26 18:34:47 +00:00
void ::Commodore::Serial::AttachPortAndBus(std::shared_ptr<Port> port, std::shared_ptr<Bus> bus) {
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) {
ports_.push_back(port);
for(int line = static_cast<int>(ServiceRequest); line <= static_cast<int>(Reset); line++) {
// the addition of a new device may change the line output...
set_line_output_did_change(static_cast<Line>(line));
// ... but the new device will need to be told the current state regardless
port->set_input(static_cast<Line>(line), line_levels_[line]);
}
}
2017-03-26 18:34:47 +00:00
void Bus::set_line_output_did_change(Line line) {
// 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_) {
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)));
}
}
// post an update only if one occurred
2017-03-26 18:34:47 +00:00
if(new_line_level != line_levels_[line]) {
line_levels_[line] = new_line_level;
2017-03-26 18:34:47 +00:00
for(std::weak_ptr<Port> port : ports_) {
std::shared_ptr<Port> locked_port = port.lock();
2017-03-26 18:34:47 +00:00
if(locked_port) {
locked_port->set_input(line, new_line_level);
}
}
}
}
// MARK: - The debug port
2017-03-26 18:34:47 +00:00
void DebugPort::set_input(Line line, LineLevel value) {
input_levels_[line] = value;
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_) {
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) {
incoming_byte_ = (incoming_byte_ >> 1) | (input_levels_[Line::Data] ? 0x80 : 0x00);
}
incoming_count_--;
2019-03-02 23:07:05 +00:00
if(incoming_count_ == 0) std::cout << "[Bus] Observed value " << std::hex << int(incoming_byte_);
}
}