Triforce/SerialDevice: Add PassThroughTxBytes function and some minor cleanups.

This commit is contained in:
Jordan Woyak
2026-03-17 21:46:08 -05:00
parent 816e3a654a
commit b5bc70a6cc
2 changed files with 29 additions and 6 deletions

View File

@@ -16,9 +16,7 @@ void SerialDevice::WriteRxBytes(std::span<const u8> bytes)
#if defined(__cpp_lib_containers_ranges)
m_rx_buffer.append_range(bytes);
#else
const auto prev_size = m_rx_buffer.size();
m_rx_buffer.resize(prev_size + bytes.size());
std::ranges::copy(bytes, m_rx_buffer.begin() + prev_size);
m_rx_buffer.insert(m_rx_buffer.end(), bytes.begin(), bytes.end());
#endif
}
@@ -42,12 +40,34 @@ void SerialDevice::WriteTxBytes(std::span<const u8> bytes)
#if defined(__cpp_lib_containers_ranges)
m_tx_buffer.append_range(bytes);
#else
const auto prev_size = m_tx_buffer.size();
m_tx_buffer.resize(prev_size + bytes.size());
std::ranges::copy(bytes, m_tx_buffer.begin() + prev_size);
m_tx_buffer.insert(m_tx_buffer.end(), bytes.begin(), bytes.end());
#endif
}
void SerialDevice::PassThroughTxBytes(SerialDevice& other)
{
// The destination buffer will often be empty or near-empty.
if (m_tx_buffer.size() < other.m_tx_buffer.size()) [[likely]]
{
#if defined(__cpp_lib_containers_ranges)
other.m_tx_buffer.prepend_range(m_tx_buffer);
#else
other.m_tx_buffer.insert(other.m_tx_buffer.begin(), m_tx_buffer.begin(), m_tx_buffer.end());
#endif
m_tx_buffer.swap(other.m_tx_buffer);
}
else
{
#if defined(__cpp_lib_containers_ranges)
m_tx_buffer.append_range(other.m_tx_buffer);
#else
m_tx_buffer.insert(m_tx_buffer.end(), other.m_tx_buffer.begin(), other.m_tx_buffer.end());
#endif
}
other.m_tx_buffer.clear();
}
void SerialDevice::DoState(PointerWrap& p)
{
p.Do(m_rx_buffer);

View File

@@ -45,6 +45,9 @@ protected:
void WriteTxByte(u8 byte) { m_tx_buffer.emplace_back(byte); }
void WriteTxBytes(std::span<const u8> bytes);
// Transfer the entirety of other's tx buffer to this tx buffer.
void PassThroughTxBytes(SerialDevice& other);
private:
// The stream of bytes from the baseboard to the device.
// FYI: Current device implementations tend to empty the entire buffer in one go,