Consolidate both patch register functions into a single function (#40)

* Consolidate both patch register functions into one

* register_patches
This commit is contained in:
Anghelo Carvajal 2024-06-10 09:52:27 -04:00 committed by GitHub
parent b4dbddb555
commit 4d756c8df2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 52 additions and 63 deletions

View file

@ -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"

View file

@ -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);

View file

@ -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

View file

@ -1,20 +1,34 @@
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <unordered_map>
#include <vector>
#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<char> 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) {

View file

@ -1,23 +0,0 @@
#include <unordered_map>
#include <algorithm>
#include <vector>
#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);
}

View file

@ -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<char> patch_data;
std::unordered_map<std::u8string, recomp::GameEntry> 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<std::mutex> lock(patch_data_mutex);
patch_data.resize(size);
std::memcpy(patch_data.data(), patch, size);
}
bool check_hash(const std::vector<uint8_t>& 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;