diff --git a/Source/Core/Core/HW/Triforce/SerialDevice.cpp b/Source/Core/Core/HW/Triforce/SerialDevice.cpp index 33e06e695d..cf9d2ca706 100644 --- a/Source/Core/Core/HW/Triforce/SerialDevice.cpp +++ b/Source/Core/Core/HW/Triforce/SerialDevice.cpp @@ -16,9 +16,7 @@ void SerialDevice::WriteRxBytes(std::span 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 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); diff --git a/Source/Core/Core/HW/Triforce/SerialDevice.h b/Source/Core/Core/HW/Triforce/SerialDevice.h index e3aba79635..fa3af0b954 100644 --- a/Source/Core/Core/HW/Triforce/SerialDevice.h +++ b/Source/Core/Core/HW/Triforce/SerialDevice.h @@ -45,6 +45,9 @@ protected: void WriteTxByte(u8 byte) { m_tx_buffer.emplace_back(byte); } void WriteTxBytes(std::span 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,