From 194b062b9281ad21d561372e57ec2ede740face4 Mon Sep 17 00:00:00 2001 From: BrokeStudio Date: Mon, 6 Nov 2023 09:51:50 +0100 Subject: [PATCH] rainbow: fixed FILE_WRITE and FILE_APPEND commands --- Core/NES/Mappers/Homebrew/RainbowESP.cpp | 25 ++++++++++++++++++++---- Core/NES/Mappers/Homebrew/RainbowESP.h | 2 ++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Core/NES/Mappers/Homebrew/RainbowESP.cpp b/Core/NES/Mappers/Homebrew/RainbowESP.cpp index 1a2a88cb2..bf4bfc5f7 100644 --- a/Core/NES/Mappers/Homebrew/RainbowESP.cpp +++ b/Core/NES/Mappers/Homebrew/RainbowESP.cpp @@ -850,16 +850,15 @@ void BrokeStudioFirmware::processBufferedMessage() break; case toesp_cmds_t::FILE_WRITE: UDBG("[Rainbow] ESP received command FILE_WRITE"); - if(message_size >= 3 && this->working_file.active) { + if(message_size >= 2 && this->working_file.active) { this->writeFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); this->working_file.offset += message_size - 1; } break; case toesp_cmds_t::FILE_APPEND: UDBG("[Rainbow] ESP received command FILE_APPEND"); - if(message_size >= 3 && this->working_file.active) { - UDBG("[Rainbow] ESP command FILE_APPEND not implemented"); - //this->appendFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); + if(message_size >= 2 && this->working_file.active) { + this->appendFile(this->rx_buffer.begin() + 2, this->rx_buffer.begin() + message_size + 1); } break; case toesp_cmds_t::FILE_COUNT: @@ -1301,6 +1300,24 @@ void BrokeStudioFirmware::writeFile(I data_begin, I data_end) } } +template +void BrokeStudioFirmware::appendFile(I data_begin, I data_end) +{ + if(this->working_file.active == false) { + return; + } + + auto const data_size = data_end - data_begin; + size_t file_size = this->working_file.file->data.size(); + uint32_t const offset_end = file_size + data_size; + this->working_file.file->data.resize(offset_end, 0); + + for(vector::size_type i = file_size; i < offset_end; ++i) { + this->working_file.file->data[i] = *data_begin; + ++data_begin; + } +} + void BrokeStudioFirmware::saveFiles() { #ifdef _WIN32 diff --git a/Core/NES/Mappers/Homebrew/RainbowESP.h b/Core/NES/Mappers/Homebrew/RainbowESP.h index 731634917..a4adc7562 100644 --- a/Core/NES/Mappers/Homebrew/RainbowESP.h +++ b/Core/NES/Mappers/Homebrew/RainbowESP.h @@ -283,6 +283,8 @@ class BrokeStudioFirmware void readFile(uint8_t n); template void writeFile(I data_begin, I data_end); + template + void appendFile(I data_begin, I data_end); void saveFiles(); void saveFile(uint8_t drive, char const* filename); void loadFiles();