From 4d756c8df238540df9e7e1d7fabea3c4226eebca Mon Sep 17 00:00:00 2001 From: Anghelo Carvajal Date: Mon, 10 Jun 2024 09:52:27 -0400 Subject: [PATCH] Consolidate both patch register functions into a single function (#40) * Consolidate both patch register functions into one * register_patches --- librecomp/CMakeLists.txt | 1 - librecomp/include/librecomp/game.hpp | 1 - librecomp/include/librecomp/overlays.hpp | 30 ++++++++++-------- librecomp/src/overlays.cpp | 39 ++++++++++++++++++++---- librecomp/src/patch_loading.cpp | 23 -------------- librecomp/src/recomp.cpp | 21 ++----------- 6 files changed, 52 insertions(+), 63 deletions(-) delete mode 100644 librecomp/src/patch_loading.cpp diff --git a/librecomp/CMakeLists.txt b/librecomp/CMakeLists.txt index f8b10fe..7318386 100644 --- a/librecomp/CMakeLists.txt +++ b/librecomp/CMakeLists.txt @@ -17,7 +17,6 @@ add_library(librecomp STATIC "${CMAKE_CURRENT_SOURCE_DIR}/src/math_routines.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/overlays.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/pak.cpp" - "${CMAKE_CURRENT_SOURCE_DIR}/src/patch_loading.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/pi.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/print.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/src/recomp.cpp" diff --git a/librecomp/include/librecomp/game.hpp b/librecomp/include/librecomp/game.hpp index 5b8f88e..0e108a9 100644 --- a/librecomp/include/librecomp/game.hpp +++ b/librecomp/include/librecomp/game.hpp @@ -32,7 +32,6 @@ namespace recomp { }; void register_config_path(std::filesystem::path path); bool register_game(const recomp::GameEntry& entry); - void register_patch(const char* patch, std::size_t size); void check_all_stored_roms(); bool load_stored_rom(std::u8string& game_id); RomValidationError select_rom(const std::filesystem::path& rom_path, std::u8string& game_id); diff --git a/librecomp/include/librecomp/overlays.hpp b/librecomp/include/librecomp/overlays.hpp index 97c5942..d66ae47 100644 --- a/librecomp/include/librecomp/overlays.hpp +++ b/librecomp/include/librecomp/overlays.hpp @@ -5,24 +5,28 @@ #include "sections.h" namespace recomp { - struct overlay_section_table_data_t { - SectionTableEntry* code_sections; - size_t num_code_sections; - size_t total_num_sections; - }; + namespace overlays { + struct overlay_section_table_data_t { + SectionTableEntry* code_sections; + size_t num_code_sections; + size_t total_num_sections; + }; - struct overlays_by_index_t { - int* table; - size_t len; - }; + struct overlays_by_index_t { + int* table; + size_t len; + }; - void register_overlays(const overlay_section_table_data_t& sections, const overlays_by_index_t& overlays); - void register_patch_section(SectionTableEntry* code_sections); - void load_patch_functions(); + void register_overlays(const overlay_section_table_data_t& sections, const overlays_by_index_t& overlays); + + void register_patches(const char* patch_data, std::size_t patch_size, SectionTableEntry* code_sections); + void read_patch_data(uint8_t* rdram, gpr patch_data_address); + + void init_overlays(); + } }; extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size); extern "C" void unload_overlays(int32_t ram_addr, uint32_t size); -void init_overlays(); #endif diff --git a/librecomp/src/overlays.cpp b/librecomp/src/overlays.cpp index 1168b88..9abd751 100644 --- a/librecomp/src/overlays.cpp +++ b/librecomp/src/overlays.cpp @@ -1,20 +1,34 @@ #include +#include #include +#include #include #include +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" #include "overlays.hpp" #include "sections.h" -static recomp::overlay_section_table_data_t sections_info {}; -static recomp::overlays_by_index_t overlays_info {}; +static recomp::overlays::overlay_section_table_data_t sections_info {}; +static recomp::overlays::overlays_by_index_t overlays_info {}; -void recomp::register_overlays(const recomp::overlay_section_table_data_t& sections, const recomp::overlays_by_index_t& overlays) { +static SectionTableEntry* patch_code_sections = nullptr; +static std::vector patch_data; + +void recomp::overlays::register_overlays(const overlay_section_table_data_t& sections, const overlays_by_index_t& overlays) { sections_info = sections; overlays_info = overlays; } +void recomp::overlays::register_patches(const char* patch, std::size_t size, SectionTableEntry* sections) { + patch_code_sections = sections; + + patch_data.resize(size); + std::memcpy(patch_data.data(), patch, size); +} + struct LoadedSection { int32_t loaded_ram_addr; size_t section_table_index; @@ -44,13 +58,26 @@ void load_overlay(size_t section_table_index, int32_t ram) { section_addresses[section.index] = ram; } -void load_special_overlay(const SectionTableEntry& section, int32_t ram) { +static void load_special_overlay(const SectionTableEntry& section, int32_t ram) { for (size_t function_index = 0; function_index < section.num_funcs; function_index++) { const FuncEntry& func = section.funcs[function_index]; func_map[ram + func.offset] = func.func; } } +static void load_patch_functions() { + if (patch_code_sections == nullptr) { + debug_printf("[Patch] No patch section was registered\n"); + return; + } + load_special_overlay(patch_code_sections[0], patch_code_sections[0].ram_addr); +} + +void recomp::overlays::read_patch_data(uint8_t* rdram, gpr patch_data_address) { + for (size_t i = 0; i < patch_data.size(); i++) { + MEM_B(i, patch_data_address) = patch_data[i]; + } +} extern "C" { int32_t* section_addresses = nullptr; @@ -142,7 +169,7 @@ extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) { } } -void init_overlays() { +void recomp::overlays::init_overlays() { section_addresses = (int32_t *)calloc(sections_info.total_num_sections, sizeof(int32_t)); for (size_t section_index = 0; section_index < sections_info.num_code_sections; section_index++) { @@ -156,7 +183,7 @@ void init_overlays() { } ); - recomp::load_patch_functions(); + load_patch_functions(); } extern "C" recomp_func_t * get_function(int32_t addr) { diff --git a/librecomp/src/patch_loading.cpp b/librecomp/src/patch_loading.cpp deleted file mode 100644 index 04b3410..0000000 --- a/librecomp/src/patch_loading.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include "recomp.h" -#include "sections.h" -#include "overlays.hpp" -#include "ultramodern/ultramodern.hpp" - -static SectionTableEntry* code_sections = nullptr; - -void load_special_overlay(const SectionTableEntry& section, int32_t ram); - -void recomp::register_patch_section(SectionTableEntry* sections) { - code_sections = sections; -} - -void recomp::load_patch_functions() { - if (code_sections == nullptr) { - debug_printf("[Patch] No patch section was registered\n"); - return; - } - load_special_overlay(code_sections[0], code_sections[0].ram_addr); -} diff --git a/librecomp/src/recomp.cpp b/librecomp/src/recomp.cpp index c45f4f0..ce50a81 100644 --- a/librecomp/src/recomp.cpp +++ b/librecomp/src/recomp.cpp @@ -36,12 +36,10 @@ enum GameStatus { // Mutexes std::mutex game_roms_mutex; -std::mutex patch_data_mutex; std::mutex current_game_mutex; // Global variables std::filesystem::path config_path; -std::vector patch_data; std::unordered_map game_roms {}; std::u8string recomp::GameEntry::stored_filename() const { @@ -58,12 +56,6 @@ bool recomp::register_game(const recomp::GameEntry& entry) { return true; } -void recomp::register_patch(const char* patch, std::size_t size) { - std::lock_guard lock(patch_data_mutex); - patch_data.resize(size); - std::memcpy(patch_data.data(), patch, size); -} - bool check_hash(const std::vector& rom_data, uint64_t expected_hash) { uint64_t calculated_hash = XXH3_64bits(rom_data.data(), rom_data.size()); return calculated_hash == expected_hash; @@ -319,15 +311,9 @@ void run_thread_function(uint8_t* rdram, uint64_t addr, uint64_t sp, uint64_t ar func(rdram, &ctx); } -void read_patch_data(uint8_t* rdram, gpr patch_data_address) { - for (size_t i = 0; i < patch_data.size(); i++) { - MEM_B(i, patch_data_address) = patch_data[i]; - } -} - void init(uint8_t* rdram, recomp_context* ctx, gpr entrypoint) { // Initialize the overlays - init_overlays(); + recomp::overlays::init_overlays(); // Load overlays in the first 1MB load_overlays(0x1000, (int32_t)entrypoint, 1024 * 1024); @@ -336,10 +322,7 @@ void init(uint8_t* rdram, recomp_context* ctx, gpr entrypoint) { recomp::do_rom_read(rdram, entrypoint, 0x10001000, 0x100000); // Read in any extra data from patches - read_patch_data(rdram, (gpr)(s32)0x80801000); - - // Set up stack pointer - ctx->r29 = 0xFFFFFFFF803FFFF0u; + recomp::overlays::read_patch_data(rdram, (gpr)(s32)0x80801000); // Set up context floats ctx->f_odd = &ctx->f0.u32h;