From 3b014b4c759ee3bfda88c985f62b3e717c008b99 Mon Sep 17 00:00:00 2001 From: Jaby Date: Fri, 23 Dec 2022 22:06:10 +0100 Subject: [PATCH] Improves return types with Progress type --- include/PSX/Auxiliary/lz4_decompressor.hpp | 25 +++++++++++++++++++ include/PSX/Auxiliary/types.hpp | 12 +++++++++ include/PSX/File/Processor/file_processor.hpp | 13 +++++++--- .../src/Auxiliary/lz4_decompressor.cpp | 12 +++++++++ src/Library/src/BootLoader/gpu_boot.cpp | 2 +- .../src/File/Processor/simplehelper.hpp | 4 ++- .../src/File/Processor/tim_processor.cpp | 18 ++++++------- 7 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 include/PSX/Auxiliary/types.hpp diff --git a/include/PSX/Auxiliary/lz4_decompressor.hpp b/include/PSX/Auxiliary/lz4_decompressor.hpp index 28d22630..48583ba7 100644 --- a/include/PSX/Auxiliary/lz4_decompressor.hpp +++ b/include/PSX/Auxiliary/lz4_decompressor.hpp @@ -1,7 +1,32 @@ #ifndef __JABYENGINE_LZ4_DECOMPRESSOR_HPP__ #define __JABYENGINE_LZ4_DECOMPRESSOR_HPP__ +#include "../../stddef.h" +#include "types.hpp" namespace JabyEngine { + class LZ4Decompressor { + public: + enum struct Result { + InProgress = 0, + Done, + Error + }; + + private: + struct State {}; + + private: + uint8_t* dst_adr = nullptr; + State state; + + public: + LZ4Decompressor() = default; + + void setup(uint8_t* dst_adr); + void reset(); + + Progress process(const uint8_t* data, size_t size); + }; } #endif //!__JABYENGINE_LZ4_DECOMPRESSOR_HPP__ \ No newline at end of file diff --git a/include/PSX/Auxiliary/types.hpp b/include/PSX/Auxiliary/types.hpp new file mode 100644 index 00000000..7362e94b --- /dev/null +++ b/include/PSX/Auxiliary/types.hpp @@ -0,0 +1,12 @@ +#ifndef __JABYENGINE_TYPES_HPP__ +#define __JABYENGINE_TYPES_HPP__ + +namespace JabyEngine { + enum struct Progress { + InProgress = 0, + Done, + Error + }; +} + +#endif //!__JABYENGINE_TYPES_HPP__ \ No newline at end of file diff --git a/include/PSX/File/Processor/file_processor.hpp b/include/PSX/File/Processor/file_processor.hpp index c283fbef..bdfba65f 100644 --- a/include/PSX/File/Processor/file_processor.hpp +++ b/include/PSX/File/Processor/file_processor.hpp @@ -1,5 +1,6 @@ #ifndef __JABYENGINE_FILE_PROCESSOR_HPP__ #define __JABYENGINE_FILE_PROCESSOR_HPP__ +#include "../../Auxiliary/types.hpp" #include "../file_types.hpp" namespace JabyEngine { @@ -11,7 +12,11 @@ namespace JabyEngine { }; struct Configuration; - typedef bool (*ProcessRoutine)(Configuration&, Reserved&); + + template + using GenericProcessRoutine = Progress (*)(Configuration&, T&); + + typedef GenericProcessRoutine ProcessRoutine; struct Configuration { ProcessRoutine process_routine = nullptr; @@ -19,7 +24,7 @@ namespace JabyEngine { size_t data_word_size = 0ull; template - static __always_inline Configuration from(bool (*process_routine)(Configuration&, T&), const uint32_t* data_adr) { + static __always_inline Configuration from(GenericProcessRoutine process_routine, const uint32_t* data_adr) { return {reinterpret_cast(process_routine), data_adr}; } @@ -34,13 +39,13 @@ namespace JabyEngine { Reserved reserved; template - static __always_inline State from(const T& reserved, const uint32_t* data_adr, bool (*process_routine)(Configuration&, T&)) { + static __always_inline State from(const T& reserved, const uint32_t* data_adr, GenericProcessRoutine process_routine) { return {Configuration::from(process_routine, data_adr), *reinterpret_cast(&reserved)}; static_assert(sizeof(T) <= sizeof(Reserved)); } public: - bool process(size_t word_size) { + Progress process(size_t word_size) { this->config.data_word_size += word_size; return (*this->config.process_routine)(this->config, this->reserved); } diff --git a/src/Library/src/Auxiliary/lz4_decompressor.cpp b/src/Library/src/Auxiliary/lz4_decompressor.cpp index b3051185..b10bb092 100644 --- a/src/Library/src/Auxiliary/lz4_decompressor.cpp +++ b/src/Library/src/Auxiliary/lz4_decompressor.cpp @@ -1,4 +1,16 @@ #include namespace JabyEngine { + void LZ4Decompressor :: setup(uint8_t* dst_adr) { + this->dst_adr = dst_adr; + LZ4Decompressor::reset(); + } + + void LZ4Decompressor :: reset() { + + } + + Progress LZ4Decompressor :: process(const uint8_t* data, size_t size) { + return Progress::Error; + } } \ No newline at end of file diff --git a/src/Library/src/BootLoader/gpu_boot.cpp b/src/Library/src/BootLoader/gpu_boot.cpp index 12e91b52..24424ddf 100644 --- a/src/Library/src/BootLoader/gpu_boot.cpp +++ b/src/Library/src/BootLoader/gpu_boot.cpp @@ -13,7 +13,7 @@ namespace JabyEngine { void display_logo() { // Upload SplashScreen picture auto state = FileProcessor::create(reinterpret_cast(SplashScreen), SimpleTIM(32, 0, 0, 0)); - while(state.process((sizeof(SplashScreen)/sizeof(uint32_t)))); + while(state.process((sizeof(SplashScreen)/sizeof(uint32_t))) == Progress::InProgress); Display::enable(); } diff --git a/src/Library/src/File/Processor/simplehelper.hpp b/src/Library/src/File/Processor/simplehelper.hpp index 2161513b..0c179381 100644 --- a/src/Library/src/File/Processor/simplehelper.hpp +++ b/src/Library/src/File/Processor/simplehelper.hpp @@ -1,5 +1,7 @@ #ifndef __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__ #define __JABYENGINE_INTERNAL_SIMPLE_HELPER_HPP__ + +// Instead of using friend we use this to access the private members #define private public #include @@ -17,7 +19,7 @@ namespace JabyEngine { } template - static bool exchange_and_execute_process_function(bool (*process_routine)(State::Configuration&, T&), State::Configuration& config, T& state) { + static Progress exchange_and_execute_process_function(State::GenericProcessRoutine process_routine, State::Configuration& config, T& state) { config.process_routine = reinterpret_cast(process_routine); return process_routine(config, state); } diff --git a/src/Library/src/File/Processor/tim_processor.cpp b/src/Library/src/File/Processor/tim_processor.cpp index 3a0ccc35..b5faee68 100644 --- a/src/Library/src/File/Processor/tim_processor.cpp +++ b/src/Library/src/File/Processor/tim_processor.cpp @@ -49,7 +49,7 @@ namespace JabyEngine { set_gpu_receive(src, state.dst_info.getTextureX(), state.dst_info.getTextureY(), width, height); } - static bool parse_data(State::Configuration& config, SimpleTIMState& state) { + static Progress parse_data(State::Configuration& config, SimpleTIMState& state) { const auto words_to_use = (config.data_word_size > state.words_left) ? state.words_left : config.data_word_size; bool is_last = (words_to_use == state.words_left); auto block_count = (words_to_use >> 4); @@ -77,7 +77,7 @@ namespace JabyEngine { state.words_left = 0; config.processed(words_to_use); - return false; + return Progress::Done; } else { @@ -85,18 +85,18 @@ namespace JabyEngine { state.words_left -= words_used; config.processed(words_used); - return true; + return Progress::InProgress; } } - static bool switch_state_parse_data(State::Configuration& config, SimpleTIMState& state) { + static Progress switch_state_parse_data(State::Configuration& config, SimpleTIMState& state) { set_gpu_receive_data(config.data_adr, state, state.size_info.getTextureWidth(), state.size_info.getTextureHeight()); return Helper::exchange_and_execute_process_function(parse_data, config, state); } - static bool parse_clut(State::Configuration& config, SimpleTIMState& state) { - if(parse_data(config, state)) { - return true; + static Progress parse_clut(State::Configuration& config, SimpleTIMState& state) { + if(const auto result = parse_data(config, state); result != Progress::Done) { + return result; } else { @@ -104,7 +104,7 @@ namespace JabyEngine { } } - static bool parse_header(State::Configuration& config, SimpleTIMState& state) { + static Progress parse_header(State::Configuration& config, SimpleTIMState& state) { if(config.data_word_size >= (sizeof(SimpleTIMSize)/sizeof(uint32_t))) { Helper::simple_read(state.size_info, config); @@ -121,7 +121,7 @@ namespace JabyEngine { } } - return true; + return Progress::InProgress; } State create(const uint32_t* data_adr, const SimpleTIM& file) {