2019-10-12 22:19:55 +00:00
|
|
|
//
|
|
|
|
// SerialPort.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 12/10/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2019-11-09 20:21:51 +00:00
|
|
|
#include "Line.hpp"
|
2019-10-13 03:14:29 +00:00
|
|
|
|
2021-11-24 21:28:18 +00:00
|
|
|
#include <cassert>
|
2021-11-06 19:01:32 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2019-10-13 03:14:29 +00:00
|
|
|
using namespace Serial;
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
void Line<include_clock>::set_writer_clock_rate(const HalfCycles clock_rate) {
|
2019-10-14 00:41:08 +00:00
|
|
|
clock_rate_ = clock_rate;
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
|
|
|
void Line<include_clock>::advance_writer(HalfCycles cycles) {
|
2019-10-30 02:36:29 +00:00
|
|
|
if(cycles == HalfCycles(0)) return;
|
2019-10-21 03:13:44 +00:00
|
|
|
|
2019-10-30 02:36:29 +00:00
|
|
|
const auto integral_cycles = cycles.as_integral();
|
|
|
|
remaining_delays_ = std::max(remaining_delays_ - integral_cycles, Cycles::IntType(0));
|
2019-10-18 03:59:43 +00:00
|
|
|
if(events_.empty()) {
|
2019-10-30 02:36:29 +00:00
|
|
|
write_cycles_since_delegate_call_ += integral_cycles;
|
2019-10-21 02:10:05 +00:00
|
|
|
if(transmission_extra_) {
|
2019-10-30 02:36:29 +00:00
|
|
|
transmission_extra_ -= integral_cycles;
|
2019-10-21 02:10:05 +00:00
|
|
|
if(transmission_extra_ <= 0) {
|
|
|
|
transmission_extra_ = 0;
|
2021-11-07 13:18:40 +00:00
|
|
|
if constexpr (!include_clock) {
|
|
|
|
update_delegate(level_);
|
|
|
|
}
|
2019-10-21 02:10:05 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 03:59:43 +00:00
|
|
|
} else {
|
|
|
|
while(!events_.empty()) {
|
2019-10-30 02:36:29 +00:00
|
|
|
if(events_.front().delay <= integral_cycles) {
|
2019-10-18 03:59:43 +00:00
|
|
|
cycles -= events_.front().delay;
|
|
|
|
write_cycles_since_delegate_call_ += events_.front().delay;
|
|
|
|
const auto old_level = level_;
|
2019-10-17 03:21:14 +00:00
|
|
|
|
2019-10-18 03:59:43 +00:00
|
|
|
auto iterator = events_.begin() + 1;
|
|
|
|
while(iterator != events_.end() && iterator->type != Event::Delay) {
|
|
|
|
level_ = iterator->type == Event::SetHigh;
|
2021-11-07 13:18:40 +00:00
|
|
|
if constexpr(include_clock) {
|
|
|
|
update_delegate(level_);
|
|
|
|
}
|
2019-10-18 03:59:43 +00:00
|
|
|
++iterator;
|
|
|
|
}
|
|
|
|
events_.erase(events_.begin(), iterator);
|
2019-10-17 03:21:14 +00:00
|
|
|
|
2021-11-07 13:18:40 +00:00
|
|
|
if constexpr (!include_clock) {
|
|
|
|
if(old_level != level_) {
|
|
|
|
update_delegate(old_level);
|
|
|
|
}
|
2019-10-18 03:59:43 +00:00
|
|
|
}
|
2019-10-21 02:10:05 +00:00
|
|
|
|
|
|
|
// Book enough extra time for the read delegate to be posted
|
|
|
|
// the final bit if one is attached.
|
|
|
|
if(events_.empty()) {
|
|
|
|
transmission_extra_ = minimum_write_cycles_for_read_delegate_bit();
|
|
|
|
}
|
2019-10-18 03:59:43 +00:00
|
|
|
} else {
|
2019-10-30 02:36:29 +00:00
|
|
|
events_.front().delay -= integral_cycles;
|
|
|
|
write_cycles_since_delegate_call_ += integral_cycles;
|
2019-10-18 03:59:43 +00:00
|
|
|
break;
|
2019-10-17 03:21:14 +00:00
|
|
|
}
|
2019-10-13 03:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
void Line<include_clock>::write(const bool level) {
|
2019-10-13 03:14:29 +00:00
|
|
|
if(!events_.empty()) {
|
|
|
|
events_.emplace_back();
|
|
|
|
events_.back().type = level ? Event::SetHigh : Event::SetLow;
|
|
|
|
} else {
|
|
|
|
level_ = level;
|
2019-10-21 02:10:05 +00:00
|
|
|
transmission_extra_ = minimum_write_cycles_for_read_delegate_bit();
|
2019-10-13 03:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
template <bool lsb_first, typename IntT> void Line<include_clock>::write_internal(
|
|
|
|
const HalfCycles cycles,
|
|
|
|
int count,
|
2024-11-30 22:32:16 +00:00
|
|
|
IntT levels
|
2024-11-30 22:21:00 +00:00
|
|
|
) {
|
2019-10-30 02:36:29 +00:00
|
|
|
remaining_delays_ += count * cycles.as_integral();
|
2019-10-13 03:14:29 +00:00
|
|
|
|
|
|
|
auto event = events_.size();
|
|
|
|
events_.resize(events_.size() + size_t(count)*2);
|
|
|
|
while(count--) {
|
|
|
|
events_[event].type = Event::Delay;
|
2019-10-30 02:36:29 +00:00
|
|
|
events_[event].delay = int(cycles.as_integral());
|
2021-11-06 19:01:32 +00:00
|
|
|
IntT bit;
|
|
|
|
if constexpr (lsb_first) {
|
|
|
|
bit = levels & 1;
|
|
|
|
levels >>= 1;
|
|
|
|
} else {
|
2021-11-07 13:18:40 +00:00
|
|
|
constexpr auto top_bit = IntT(0x80) << ((sizeof(IntT) - 1) * 8);
|
2021-11-06 19:01:32 +00:00
|
|
|
bit = levels & top_bit;
|
|
|
|
levels <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
events_[event+1].type = bit ? Event::SetHigh : Event::SetLow;
|
2019-10-13 03:14:29 +00:00
|
|
|
event += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
void Line<include_clock>::write(const HalfCycles cycles, const int count, const int levels) {
|
2021-11-06 19:01:32 +00:00
|
|
|
write_internal<true, int>(cycles, count, levels);
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
template <bool lsb_first, typename IntT> void Line<include_clock>::write(const HalfCycles cycles, const IntT value) {
|
2021-11-06 19:01:32 +00:00
|
|
|
write_internal<lsb_first, IntT>(cycles, 8 * sizeof(IntT), value);
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
|
|
|
void Line<include_clock>::reset_writing() {
|
2019-10-14 01:39:25 +00:00
|
|
|
remaining_delays_ = 0;
|
2019-10-13 03:14:29 +00:00
|
|
|
events_.clear();
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
|
|
|
bool Line<include_clock>::read() const {
|
2019-10-13 03:14:29 +00:00
|
|
|
return level_;
|
|
|
|
}
|
2019-10-17 03:21:14 +00:00
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
void Line<include_clock>::set_read_delegate(
|
|
|
|
ReadDelegate *const delegate,
|
|
|
|
[[maybe_unused]] const Storage::Time bit_length
|
|
|
|
) {
|
2019-10-17 03:21:14 +00:00
|
|
|
read_delegate_ = delegate;
|
2021-11-24 21:15:27 +00:00
|
|
|
if constexpr (!include_clock) {
|
|
|
|
assert(bit_length > Storage::Time(0));
|
2021-11-07 13:18:40 +00:00
|
|
|
read_delegate_bit_length_ = bit_length;
|
|
|
|
read_delegate_bit_length_.simplify();
|
|
|
|
write_cycles_since_delegate_call_ = 0;
|
|
|
|
}
|
2019-10-17 03:21:14 +00:00
|
|
|
}
|
2019-10-18 03:34:39 +00:00
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
2024-11-30 22:21:00 +00:00
|
|
|
void Line<include_clock>::update_delegate(const bool level) {
|
2019-10-18 03:34:39 +00:00
|
|
|
// Exit early if there's no delegate, or if the delegate is waiting for
|
|
|
|
// zero and this isn't zero.
|
|
|
|
if(!read_delegate_) return;
|
|
|
|
|
2021-11-07 13:18:40 +00:00
|
|
|
if constexpr (!include_clock) {
|
|
|
|
const int cycles_to_forward = write_cycles_since_delegate_call_;
|
|
|
|
write_cycles_since_delegate_call_ = 0;
|
|
|
|
if(level && read_delegate_phase_ == ReadDelegatePhase::WaitingForZero) return;
|
|
|
|
|
|
|
|
// Deal with a transition out of waiting-for-zero mode by seeding time left
|
|
|
|
// in bit at half a bit.
|
|
|
|
if(read_delegate_phase_ == ReadDelegatePhase::WaitingForZero) {
|
|
|
|
time_left_in_bit_ = read_delegate_bit_length_;
|
|
|
|
time_left_in_bit_.clock_rate <<= 1;
|
|
|
|
read_delegate_phase_ = ReadDelegatePhase::Serialising;
|
|
|
|
}
|
2019-10-18 03:34:39 +00:00
|
|
|
|
2021-11-07 13:18:40 +00:00
|
|
|
// Forward as many bits as occur.
|
|
|
|
Storage::Time time_left(cycles_to_forward, int(clock_rate_.as_integral()));
|
|
|
|
const int bit = level ? 1 : 0;
|
|
|
|
while(time_left >= time_left_in_bit_) {
|
|
|
|
if(!read_delegate_->serial_line_did_produce_bit(this, bit)) {
|
|
|
|
read_delegate_phase_ = ReadDelegatePhase::WaitingForZero;
|
|
|
|
if(bit) return;
|
|
|
|
}
|
2019-10-18 03:34:39 +00:00
|
|
|
|
2021-11-07 13:18:40 +00:00
|
|
|
time_left -= time_left_in_bit_;
|
|
|
|
time_left_in_bit_ = read_delegate_bit_length_;
|
2019-10-18 03:34:39 +00:00
|
|
|
}
|
2021-11-07 13:18:40 +00:00
|
|
|
time_left_in_bit_ -= time_left;
|
|
|
|
} else {
|
|
|
|
read_delegate_->serial_line_did_produce_bit(this, level);
|
2019-10-18 03:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-21 02:10:05 +00:00
|
|
|
|
2021-11-06 23:54:20 +00:00
|
|
|
template <bool include_clock>
|
|
|
|
Cycles::IntType Line<include_clock>::minimum_write_cycles_for_read_delegate_bit() {
|
2019-10-21 02:10:05 +00:00
|
|
|
if(!read_delegate_) return 0;
|
2021-11-06 23:54:20 +00:00
|
|
|
return 1 + (read_delegate_bit_length_ * unsigned(clock_rate_.as_integral())).template get<int>();
|
2019-10-21 02:10:05 +00:00
|
|
|
}
|
2021-11-06 23:54:20 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Explicitly instantiate the meaningful instances of templates above;
|
|
|
|
// this class uses templates primarily to keep the interface compact and
|
|
|
|
// to take advantage of constexpr functionality selection, not so as
|
|
|
|
// to be generic.
|
|
|
|
//
|
|
|
|
|
|
|
|
template class Serial::Line<true>;
|
|
|
|
template class Serial::Line<false>;
|
|
|
|
|
|
|
|
template void Line<true>::write<true, uint8_t>(HalfCycles, uint8_t);
|
|
|
|
template void Line<true>::write<false, uint8_t>(HalfCycles, uint8_t);
|
|
|
|
template void Line<true>::write<true, uint16_t>(HalfCycles, uint16_t);
|
|
|
|
template void Line<true>::write<false, uint16_t>(HalfCycles, uint16_t);
|
|
|
|
template void Line<true>::write<true, uint32_t>(HalfCycles, uint32_t);
|
|
|
|
template void Line<true>::write<false, uint32_t>(HalfCycles, uint32_t);
|
|
|
|
template void Line<true>::write<true, uint64_t>(HalfCycles, uint64_t);
|
|
|
|
template void Line<true>::write<false, uint64_t>(HalfCycles, uint64_t);
|
|
|
|
|
|
|
|
template void Line<false>::write<true, uint8_t>(HalfCycles, uint8_t);
|
|
|
|
template void Line<false>::write<false, uint8_t>(HalfCycles, uint8_t);
|
|
|
|
template void Line<false>::write<true, uint16_t>(HalfCycles, uint16_t);
|
|
|
|
template void Line<false>::write<false, uint16_t>(HalfCycles, uint16_t);
|
|
|
|
template void Line<false>::write<true, uint32_t>(HalfCycles, uint32_t);
|
|
|
|
template void Line<false>::write<false, uint32_t>(HalfCycles, uint32_t);
|
|
|
|
template void Line<false>::write<true, uint64_t>(HalfCycles, uint64_t);
|
|
|
|
template void Line<false>::write<false, uint64_t>(HalfCycles, uint64_t);
|