2016-07-05 18:55:20 +00:00
|
|
|
//
|
|
|
|
// SerialPort.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 05/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "SerialBus.hpp"
|
|
|
|
|
|
|
|
using namespace Commodore::Serial;
|
|
|
|
|
2016-07-06 11:46:21 +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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 11:46:20 +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);
|
|
|
|
}
|
|
|
|
|
2016-07-05 18:55:20 +00:00
|
|
|
void Bus::add_port(std::shared_ptr<Port> port)
|
|
|
|
{
|
|
|
|
_ports.push_back(port);
|
2016-07-06 00:52:33 +00:00
|
|
|
for(int line = (int)ServiceRequest; line <= (int)Reset; line++)
|
2016-07-06 00:57:31 +00:00
|
|
|
{
|
|
|
|
// the addition of a new device may change the line output...
|
2016-07-06 00:52:33 +00:00
|
|
|
set_line_output_did_change((Line)line);
|
2016-07-06 00:57:31 +00:00
|
|
|
|
|
|
|
// ... but the new device will need to be told the current state regardless
|
2016-07-07 10:44:13 +00:00
|
|
|
port->set_input((Line)line, _line_levels[line]);
|
2016-07-06 00:57:31 +00:00
|
|
|
}
|
2016-07-05 18:55:20 +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;
|
2016-07-05 18:55:20 +00:00
|
|
|
for(std::weak_ptr<Port> port : _ports)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Port> locked_port = port.lock();
|
|
|
|
if(locked_port)
|
|
|
|
{
|
2016-07-07 10:44:13 +00:00
|
|
|
new_line_level = (LineLevel)((bool)new_line_level & (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
|
2016-07-07 10:44:13 +00:00
|
|
|
if(new_line_level != _line_levels[line])
|
2016-07-05 18:55:20 +00:00
|
|
|
{
|
2016-07-07 10:44:13 +00:00
|
|
|
_line_levels[line] = new_line_level;
|
2016-07-05 18:55:20 +00:00
|
|
|
|
|
|
|
for(std::weak_ptr<Port> port : _ports)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Port> locked_port = port.lock();
|
|
|
|
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
|
|
|
|
|
|
|
#pragma mark - The debug port
|
|
|
|
|
|
|
|
void DebugPort::set_input(Line line, LineLevel value)
|
|
|
|
{
|
|
|
|
_input_levels[line] = value;
|
|
|
|
|
|
|
|
printf("[Bus] %s is %s\n", StringForLine(line), value ? "high" : "low");
|
|
|
|
if(!_incoming_count)
|
|
|
|
{
|
|
|
|
_incoming_count = (!_input_levels[Line::Clock] && !_input_levels[Line::Data]) ? 8 : 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(line == Line::Clock && value)
|
|
|
|
{
|
|
|
|
_incoming_byte = (_incoming_byte >> 1) | (_input_levels[Line::Data] ? 0x80 : 0x00);
|
|
|
|
}
|
|
|
|
_incoming_count--;
|
|
|
|
if(_incoming_count == 0) printf("[Bus] Observed %02x\n", _incoming_byte);
|
|
|
|
}
|
|
|
|
}
|