diff --git a/librecomp/include/disable_warnings.h b/librecomp/include/disable_warnings.h index 6526983..3591422 100644 --- a/librecomp/include/disable_warnings.h +++ b/librecomp/include/disable_warnings.h @@ -1,4 +1,4 @@ #ifdef __GNUC__ -#pragma GCC diagnostic ignored "-Wunused-variable" -#pragma GCC diagnostic ignored "-Wimplicit-function-declaration" +# pragma GCC diagnostic ignored "-Wunused-variable" +# pragma GCC diagnostic ignored "-Wimplicit-function-declaration" #endif diff --git a/librecomp/include/recomp.h b/librecomp/include/recomp.h index 2f55a04..b36cad6 100644 --- a/librecomp/include/recomp.h +++ b/librecomp/include/recomp.h @@ -1,10 +1,10 @@ #ifndef __RECOMP_H__ #define __RECOMP_H__ -#include -#include -#include #include +#include +#include +#include typedef uint64_t gpr; diff --git a/librecomp/include/recomp_game.h b/librecomp/include/recomp_game.h index a8d5b6c..d296893 100644 --- a/librecomp/include/recomp_game.h +++ b/librecomp/include/recomp_game.h @@ -1,8 +1,8 @@ #ifndef __RECOMP_GAME__ #define __RECOMP_GAME__ -#include #include +#include #include "recomp.h" #include "rsp.h" @@ -21,29 +21,36 @@ namespace recomp { std::u8string stored_filename() const; }; - enum class RomValidationError { - Good, - FailedToOpen, - NotARom, - IncorrectRom, - NotYet, - IncorrectVersion, - OtherError - }; - 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); - bool is_rom_valid(std::u8string& game_id); - bool is_rom_loaded(); - void set_rom_contents(std::vector&& new_rom); - void do_rom_read(uint8_t* rdram, gpr ram_address, uint32_t physical_addr, size_t num_bytes); - void do_rom_pio(uint8_t* rdram, gpr ram_address, uint32_t physical_addr); - void start(ultramodern::WindowHandle window_handle, const recomp::rsp::callbacks_t& rsp_callbacks, const ultramodern::audio_callbacks_t& audio_callbacks, const ultramodern::input_callbacks_t& input_callbacks, const ultramodern::gfx_callbacks_t& gfx_callbacks, const ultramodern::events::callbacks_t& thread_callbacks, const ultramodern::error_handling::callbacks_t& error_handling_callbacks_); - void start_game(const std::u8string& game_id); - std::u8string current_game_id(); -} + enum class RomValidationError { + Good, + FailedToOpen, + NotARom, + IncorrectRom, + NotYet, + IncorrectVersion, + OtherError + }; + + 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); + bool is_rom_valid(std::u8string& game_id); + bool is_rom_loaded(); + void set_rom_contents(std::vector&& new_rom); + void do_rom_read(uint8_t* rdram, gpr ram_address, uint32_t physical_addr, size_t num_bytes); + void do_rom_pio(uint8_t* rdram, gpr ram_address, uint32_t physical_addr); + + void start( + ultramodern::WindowHandle window_handle, const recomp::rsp::callbacks_t& rsp_callbacks, + const ultramodern::audio_callbacks_t& audio_callbacks, const ultramodern::input_callbacks_t& input_callbacks, + const ultramodern::gfx_callbacks_t& gfx_callbacks, const ultramodern::events::callbacks_t& thread_callbacks, + const ultramodern::error_handling::callbacks_t& error_handling_callbacks_); + void start_game(const std::u8string& game_id); + + std::u8string current_game_id(); +} // namespace recomp #endif diff --git a/librecomp/include/recomp_helpers.h b/librecomp/include/recomp_helpers.h index 1562b99..38f6534 100644 --- a/librecomp/include/recomp_helpers.h +++ b/librecomp/include/recomp_helpers.h @@ -15,13 +15,14 @@ T _arg(uint8_t* rdram, recomp_context* ctx) { } else { // static_assert in else workaround - [] () { + []() { static_assert(flag, "Floats in a2/a3 not supported"); - }(); + } + (); } } else if constexpr (std::is_pointer_v) { - static_assert (!std::is_pointer_v>, "Double pointers not supported"); + static_assert(!std::is_pointer_v>, "Double pointers not supported"); return TO_PTR(std::remove_pointer_t, raw_arg); } else if constexpr (std::is_integral_v) { @@ -30,13 +31,14 @@ T _arg(uint8_t* rdram, recomp_context* ctx) { } else { // static_assert in else workaround - [] () { + []() { static_assert(flag, "Unsupported type"); - }(); + } + (); } } -template +template void _return(recomp_context* ctx, T val) { static_assert(sizeof(T) <= 4 && "Only 32-bit value returns supported currently"); if (std::is_same_v) { diff --git a/librecomp/include/recomp_overlays.h b/librecomp/include/recomp_overlays.h index 97c5942..6c488fa 100644 --- a/librecomp/include/recomp_overlays.h +++ b/librecomp/include/recomp_overlays.h @@ -2,6 +2,7 @@ #define __RECOMP_OVERLAYS_H__ #include + #include "sections.h" namespace recomp { @@ -19,7 +20,7 @@ namespace recomp { 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(); -}; +}; // namespace recomp 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); diff --git a/librecomp/include/rsp.h b/librecomp/include/rsp.h index 7e1a1e4..1a68a27 100644 --- a/librecomp/include/rsp.h +++ b/librecomp/include/rsp.h @@ -3,8 +3,9 @@ #include -#include "rsp_vu.h" #include "recomp.h" +#include "rsp_vu.h" + #include "ultramodern/ultra64.h" // TODO: Move these to recomp namespace? @@ -23,11 +24,9 @@ extern uint8_t dmem[]; extern uint16_t rspReciprocals[512]; extern uint16_t rspInverseSquareRoots[512]; -#define RSP_MEM_B(offset, addr) \ - (*reinterpret_cast(dmem + (0xFFF & (((offset) + (addr)) ^ 3)))) +#define RSP_MEM_B(offset, addr) (*reinterpret_cast(dmem + (0xFFF & (((offset) + (addr)) ^ 3)))) -#define RSP_MEM_BU(offset, addr) \ - (*reinterpret_cast(dmem + (0xFFF & (((offset) + (addr)) ^ 3)))) +#define RSP_MEM_BU(offset, addr) (*reinterpret_cast(dmem + (0xFFF & (((offset) + (addr)) ^ 3)))) static inline uint32_t RSP_MEM_W_LOAD(uint32_t offset, uint32_t addr) { uint32_t out; @@ -65,14 +64,11 @@ static inline void RSP_MEM_H_STORE(uint32_t offset, uint32_t addr, uint32_t val) } } -#define RSP_ADD32(a, b) \ - ((int32_t)((a) + (b))) +#define RSP_ADD32(a, b) ((int32_t)((a) + (b))) -#define RSP_SUB32(a, b) \ - ((int32_t)((a) - (b))) +#define RSP_SUB32(a, b) ((int32_t)((a) - (b))) -#define RSP_SIGNED(val) \ - ((int32_t)(val)) +#define RSP_SIGNED(val) ((int32_t)(val)) #define SET_DMA_DMEM(dmem_addr) dma_dmem_address = (dmem_addr) #define SET_DMA_DRAM(dram_addr) dma_dram_address = (dram_addr) @@ -105,9 +101,11 @@ namespace recomp { /** * Return a function pointer to the corresponding RSP microcode function for the given `task_type`. * - * The full OSTask (`task` parameter) is passed in case the `task_type` number is not enough information to distinguish out the exact microcode function. + * The full OSTask (`task` parameter) is passed in case the `task_type` number is not enough information to distinguish out the + * exact microcode function. * - * This function is allowed to return `nullptr` if no microcode matches the specified task. In this case a message will be printed to stderr and the program will exit. + * This function is allowed to return `nullptr` if no microcode matches the specified task. In this case a message will be + * printed to stderr and the program will exit. */ get_rsp_microcode_t* get_rsp_microcode; }; @@ -117,7 +115,7 @@ namespace recomp { void constants_init(); bool run_task(uint8_t* rdram, const OSTask* task); - } -} + } // namespace rsp +} // namespace recomp #endif diff --git a/librecomp/include/sections.h b/librecomp/include/sections.h index 1d1b228..9b3d370 100644 --- a/librecomp/include/sections.h +++ b/librecomp/include/sections.h @@ -2,6 +2,7 @@ #define __SECTIONS_H__ #include + #include "recomp.h" #define ARRLEN(x) (sizeof(x) / sizeof((x)[0])) @@ -15,7 +16,7 @@ typedef struct { uint32_t rom_addr; uint32_t ram_addr; uint32_t size; - FuncEntry *funcs; + FuncEntry* funcs; size_t num_funcs; size_t index; } SectionTableEntry; diff --git a/librecomp/src/ai.cpp b/librecomp/src/ai.cpp index 0195f2b..38e658b 100644 --- a/librecomp/src/ai.cpp +++ b/librecomp/src/ai.cpp @@ -9,8 +9,8 @@ extern "C" void osAiSetFrequency_recomp(uint8_t* rdram, recomp_context* ctx) { uint32_t freq = ctx->r4; // This makes actual audio frequency more accurate to console, but may not be desirable - //uint32_t dacRate = (uint32_t)(((float)VI_NTSC_CLOCK / freq) + 0.5f); - //freq = VI_NTSC_CLOCK / dacRate; + // uint32_t dacRate = (uint32_t)(((float)VI_NTSC_CLOCK / freq) + 0.5f); + // freq = VI_NTSC_CLOCK / dacRate; ctx->r2 = freq; ultramodern::set_audio_frequency(freq); } diff --git a/librecomp/src/cont.cpp b/librecomp/src/cont.cpp index 0d7aec8..cb1e2a6 100644 --- a/librecomp/src/cont.cpp +++ b/librecomp/src/cont.cpp @@ -1,4 +1,5 @@ -#include +#include "ultramodern/ultramodern.hpp" + #include "recomp_helpers.h" static ultramodern::input_callbacks_t input_callbacks; @@ -18,7 +19,9 @@ extern "C" void recomp_measure_latency(uint8_t* rdram, recomp_context* ctx) { } void ultramodern::measure_input_latency() { - // printf("Delta: %ld micros\n", std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - input_poll_time)); +#if 0 + // printf("Delta: %ld micros\n", std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - input_poll_time)); +#endif } void set_input_callbacks(const ultramodern::input_callbacks_t& callbacks) { @@ -36,8 +39,8 @@ extern "C" void osContInit_recomp(uint8_t* rdram, recomp_context* ctx) { // Mark controller 0 as present MEM_H(0, status) = 0x0005; // type: CONT_TYPE_NORMAL (from joybus) - MEM_B(2, status) = 0x00; // status: 0 (from joybus) - MEM_B(3, status) = 0x00; // errno: 0 (from libultra) + MEM_B(2, status) = 0x00; // status: 0 (from joybus) + MEM_B(3, status) = 0x00; // errno: 0 (from libultra) max_controllers = 4; @@ -85,17 +88,17 @@ extern "C" void osContGetReadData_recomp(uint8_t* rdram, recomp_context* ctx) { } } -extern "C" void osContStartQuery_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osContStartQuery_recomp(uint8_t* rdram, recomp_context* ctx) { ultramodern::send_si_message(rdram); } -extern "C" void osContGetQuery_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osContGetQuery_recomp(uint8_t* rdram, recomp_context* ctx) { PTR(void) status = _arg<0, PTR(void)>(rdram, ctx); // Mark controller 0 as present MEM_H(0, status) = 0x0005; // type: CONT_TYPE_NORMAL (from joybus) - MEM_B(2, status) = 0x01; // status: 0x01 (from joybus, indicates that a pak is plugged into the controller) - MEM_B(3, status) = 0x00; // errno: 0 (from libultra) + MEM_B(2, status) = 0x01; // status: 0x01 (from joybus, indicates that a pak is plugged into the controller) + MEM_B(3, status) = 0x00; // errno: 0 (from libultra) // Mark controllers 1-3 as not connected for (size_t controller = 1; controller < max_controllers; controller++) { diff --git a/librecomp/src/eep.cpp b/librecomp/src/eep.cpp index 2485ec8..7426ba8 100644 --- a/librecomp/src/eep.cpp +++ b/librecomp/src/eep.cpp @@ -1,5 +1,6 @@ #include "recomp.h" -#include + +#include "ultramodern/ultra64.h" void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count); @@ -15,7 +16,7 @@ extern "C" void osEepromProbe_recomp(uint8_t* rdram, recomp_context* ctx) { } extern "C" void osEepromWrite_recomp(uint8_t* rdram, recomp_context* ctx) { - assert(false);// ctx->r2 = 8; // CONT_NO_RESPONSE_ERROR + assert(false); // ctx->r2 = 8; // CONT_NO_RESPONSE_ERROR } extern "C" void osEepromLongWrite_recomp(uint8_t* rdram, recomp_context* ctx) { @@ -32,7 +33,7 @@ extern "C" void osEepromLongWrite_recomp(uint8_t* rdram, recomp_context* ctx) { } extern "C" void osEepromRead_recomp(uint8_t* rdram, recomp_context* ctx) { - assert(false);// ctx->r2 = 8; // CONT_NO_RESPONSE_ERROR + assert(false); // ctx->r2 = 8; // CONT_NO_RESPONSE_ERROR } extern "C" void osEepromLongRead_recomp(uint8_t* rdram, recomp_context* ctx) { diff --git a/librecomp/src/flash.cpp b/librecomp/src/flash.cpp index 43407e8..b2a6844 100644 --- a/librecomp/src/flash.cpp +++ b/librecomp/src/flash.cpp @@ -1,8 +1,8 @@ +#include "recomp.h" #include #include #include #include -#include "recomp.h" // TODO move this out into ultramodern code @@ -20,17 +20,17 @@ void save_clear(uint32_t start, uint32_t size, char value); std::array write_buffer; -extern "C" void osFlashInit_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashInit_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = ultramodern::flash_handle; } -extern "C" void osFlashReadStatus_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashReadStatus_recomp(uint8_t* rdram, recomp_context* ctx) { PTR(u8) flash_status = ctx->r4; MEM_B(0, flash_status) = 0; } -extern "C" void osFlashReadId_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashReadId_recomp(uint8_t* rdram, recomp_context* ctx) { PTR(u32) flash_type = ctx->r4; PTR(u32) flash_maker = ctx->r5; @@ -39,24 +39,23 @@ extern "C" void osFlashReadId_recomp(uint8_t * rdram, recomp_context * ctx) { MEM_W(0, flash_maker) = 0x00C2001E; } -extern "C" void osFlashClearStatus_recomp(uint8_t * rdram, recomp_context * ctx) { - +extern "C" void osFlashClearStatus_recomp(uint8_t* rdram, recomp_context* ctx) { } -extern "C" void osFlashAllErase_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashAllErase_recomp(uint8_t* rdram, recomp_context* ctx) { save_clear(0, ultramodern::save_size, 0xFF); ctx->r2 = 0; } -extern "C" void osFlashAllEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashAllEraseThrough_recomp(uint8_t* rdram, recomp_context* ctx) { save_clear(0, ultramodern::save_size, 0xFF); ctx->r2 = 0; } // This function is named sector but really means page. -extern "C" void osFlashSectorErase_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashSectorErase_recomp(uint8_t* rdram, recomp_context* ctx) { uint32_t page_num = (uint32_t)ctx->r4; // Prevent out of bounds erase @@ -71,7 +70,7 @@ extern "C" void osFlashSectorErase_recomp(uint8_t * rdram, recomp_context * ctx) } // Same naming issue as above. -extern "C" void osFlashSectorEraseThrough_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashSectorEraseThrough_recomp(uint8_t* rdram, recomp_context* ctx) { uint32_t page_num = (uint32_t)ctx->r4; // Prevent out of bounds erase @@ -85,17 +84,17 @@ extern "C" void osFlashSectorEraseThrough_recomp(uint8_t * rdram, recomp_context ctx->r2 = 0; } -extern "C" void osFlashCheckEraseEnd_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashCheckEraseEnd_recomp(uint8_t* rdram, recomp_context* ctx) { // All erases are blocking in this implementation, so this should always return OK. ctx->r2 = 0; // FLASH_STATUS_ERASE_OK } -extern "C" void osFlashWriteBuffer_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashWriteBuffer_recomp(uint8_t* rdram, recomp_context* ctx) { OSIoMesg* mb = TO_PTR(OSIoMesg, ctx->r4); int32_t pri = ctx->r5; PTR(void) dramAddr = ctx->r6; PTR(OSMesgQueue) mq = ctx->r7; - + // Copy the input data into the write buffer for (size_t i = 0; i < page_size; i++) { write_buffer[i] = MEM_B(i, dramAddr); @@ -107,7 +106,7 @@ extern "C" void osFlashWriteBuffer_recomp(uint8_t * rdram, recomp_context * ctx) ctx->r2 = 0; } -extern "C" void osFlashWriteArray_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashWriteArray_recomp(uint8_t* rdram, recomp_context* ctx) { uint32_t page_num = ctx->r4; // Copy the write buffer into the save file @@ -116,7 +115,7 @@ extern "C" void osFlashWriteArray_recomp(uint8_t * rdram, recomp_context * ctx) ctx->r2 = 0; } -extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashReadArray_recomp(uint8_t* rdram, recomp_context* ctx) { OSIoMesg* mb = TO_PTR(OSIoMesg, ctx->r4); int32_t pri = ctx->r5; uint32_t page_num = ctx->r6; @@ -136,6 +135,6 @@ extern "C" void osFlashReadArray_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r2 = 0; } -extern "C" void osFlashChange_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osFlashChange_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } diff --git a/librecomp/src/math_routines.cpp b/librecomp/src/math_routines.cpp index 2bd21ce..910c914 100644 --- a/librecomp/src/math_routines.cpp +++ b/librecomp/src/math_routines.cpp @@ -1,10 +1,11 @@ -#include +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" // TODO remove these by implementing the necessary instructions and control flow handling in the recompiler. // This has already been partially completed. -extern "C" void __udivdi3_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __udivdi3_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); uint64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); uint64_t ret = a / b; @@ -13,7 +14,7 @@ extern "C" void __udivdi3_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __divdi3_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __divdi3_recomp(uint8_t* rdram, recomp_context* ctx) { int64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); int64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); int64_t ret = a / b; @@ -22,7 +23,7 @@ extern "C" void __divdi3_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __umoddi3_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __umoddi3_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); uint64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); uint64_t ret = a % b; @@ -31,7 +32,7 @@ extern "C" void __umoddi3_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __ull_div_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ull_div_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); uint64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); uint64_t ret = a / b; @@ -40,7 +41,7 @@ extern "C" void __ull_div_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __ll_div_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ll_div_recomp(uint8_t* rdram, recomp_context* ctx) { int64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); int64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); int64_t ret = a / b; @@ -49,7 +50,7 @@ extern "C" void __ll_div_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __ll_mul_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ll_mul_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); uint64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); uint64_t ret = a * b; @@ -58,7 +59,7 @@ extern "C" void __ll_mul_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __ull_rem_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ull_rem_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); uint64_t b = (ctx->r6 << 32) | ((ctx->r7 << 0) & 0xFFFFFFFFu); uint64_t ret = a % b; @@ -67,14 +68,14 @@ extern "C" void __ull_rem_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r3 = (int32_t)(ret >> 0); } -extern "C" void __ull_to_d_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ull_to_d_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); double ret = (double)a; ctx->f0.d = ret; } -extern "C" void __ull_to_f_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __ull_to_f_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t a = (ctx->r4 << 32) | ((ctx->r5 << 0) & 0xFFFFFFFFu); float ret = (float)a; diff --git a/librecomp/src/overlays.cpp b/librecomp/src/overlays.cpp index 12764cf..11d731a 100644 --- a/librecomp/src/overlays.cpp +++ b/librecomp/src/overlays.cpp @@ -7,8 +7,8 @@ #include "recomp_overlays.h" #include "sections.h" -static recomp::overlay_section_table_data_t sections_info {}; -static recomp::overlays_by_index_t overlays_info {}; +static recomp::overlay_section_table_data_t sections_info{}; +static recomp::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) { sections_info = sections; @@ -51,7 +51,6 @@ void load_special_overlay(const SectionTableEntry& section, int32_t ram) { } } - extern "C" { int32_t* section_addresses = nullptr; } @@ -59,16 +58,16 @@ int32_t* section_addresses = nullptr; extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size) { // Search for the first section that's included in the loaded rom range // Sections were sorted by `init_overlays` so we can use the bounds functions - auto lower = std::lower_bound(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], rom, + auto lower = std::lower_bound( + §ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], rom, [](const SectionTableEntry& entry, uint32_t addr) { return entry.rom_addr < addr; - } - ); - auto upper = std::upper_bound(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], (uint32_t)(rom + size), + }); + auto upper = std::upper_bound( + §ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], (uint32_t)(rom + size), [](uint32_t addr, const SectionTableEntry& entry) { return addr < entry.size + entry.rom_addr; - } - ); + }); // Load the overlays that were found for (auto it = lower; it != upper; ++it) { load_overlay(std::distance(§ions_info.code_sections[0], it), it->rom_addr - rom + ram_addr); @@ -79,7 +78,9 @@ extern "C" void unload_overlay_by_id(uint32_t id) { uint32_t section_table_index = overlays_info.table[id]; const SectionTableEntry& section = sections_info.code_sections[section_table_index]; - auto find_it = std::find_if(loaded_sections.begin(), loaded_sections.end(), [section_table_index](const LoadedSection& s) { return s.section_table_index == section_table_index; }); + auto find_it = std::find_if(loaded_sections.begin(), loaded_sections.end(), [section_table_index](const LoadedSection& s) { + return s.section_table_index == section_table_index; + }); if (find_it != loaded_sections.end()) { // Determine where each function was loaded to and remove that entry from the function map @@ -117,11 +118,12 @@ extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) { if (ram_addr < (it->loaded_ram_addr + section.size) && (ram_addr + size) >= it->loaded_ram_addr) { // Check if the section isn't entirely in the loaded region if (ram_addr > it->loaded_ram_addr || (ram_addr + size) < (it->loaded_ram_addr + section.size)) { - fprintf(stderr, + fprintf( + stderr, "Cannot partially unload section\n" " rom: 0x%08X size: 0x%08X loaded_addr: 0x%08X\n" " unloaded_ram: 0x%08X unloaded_size : 0x%08X\n", - section.rom_addr, section.size, it->loaded_ram_addr, ram_addr, size); + section.rom_addr, section.size, it->loaded_ram_addr, ram_addr, size); assert(false); std::exit(EXIT_FAILURE); } @@ -143,23 +145,23 @@ extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) { } void init_overlays() { - section_addresses = (int32_t *)malloc(sections_info.total_num_sections * sizeof(int32_t)); + section_addresses = (int32_t*)malloc(sections_info.total_num_sections * sizeof(int32_t)); for (size_t section_index = 0; section_index < sections_info.total_num_sections; section_index++) { section_addresses[sections_info.code_sections[section_index].index] = sections_info.code_sections[section_index].ram_addr; } // Sort the executable sections by rom address - std::sort(§ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], + std::sort( + §ions_info.code_sections[0], §ions_info.code_sections[sections_info.num_code_sections], [](const SectionTableEntry& a, const SectionTableEntry& b) { return a.rom_addr < b.rom_addr; - } - ); + }); recomp::load_patch_functions(); } -extern "C" recomp_func_t * get_function(int32_t addr) { +extern "C" recomp_func_t* get_function(int32_t addr) { auto func_find = func_map.find(addr); if (func_find == func_map.end()) { fprintf(stderr, "Failed to find function at 0x%08X\n", addr); @@ -168,4 +170,3 @@ extern "C" recomp_func_t * get_function(int32_t addr) { } return func_find->second; } - diff --git a/librecomp/src/pak.cpp b/librecomp/src/pak.cpp index 92e0f10..834e8e7 100644 --- a/librecomp/src/pak.cpp +++ b/librecomp/src/pak.cpp @@ -2,34 +2,34 @@ #include #include -extern "C" void osPfsInitPak_recomp(uint8_t * rdram, recomp_context* ctx) { +extern "C" void osPfsInitPak_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsFreeBlocks_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsFreeBlocks_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsAllocateFile_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsAllocateFile_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsDeleteFile_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsDeleteFile_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsFileState_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsFileState_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsFindFile_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsFindFile_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsReadWriteFile_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsReadWriteFile_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } -extern "C" void osPfsChecker_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osPfsChecker_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 1; // PFS_ERR_NOPACK } diff --git a/librecomp/src/patch_loading.cpp b/librecomp/src/patch_loading.cpp index e456a51..b80d35c 100644 --- a/librecomp/src/patch_loading.cpp +++ b/librecomp/src/patch_loading.cpp @@ -1,9 +1,10 @@ -#include #include +#include #include + #include "recomp.h" -#include "sections.h" #include "recomp_overlays.h" +#include "sections.h" static SectionTableEntry* code_sections = nullptr; diff --git a/librecomp/src/pi.cpp b/librecomp/src/pi.cpp index 2234f82..3e6d84b 100644 --- a/librecomp/src/pi.cpp +++ b/librecomp/src/pi.cpp @@ -1,13 +1,15 @@ -#include -#include #include #include -#include +#include +#include #include +#include + #include "recomp.h" #include "recomp_game.h" -#include -#include + +#include "ultramodern/ultra64.h" +#include "ultramodern/ultramodern.hpp" static std::vector rom; @@ -49,7 +51,7 @@ extern "C" void osCartRomInit_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = (gpr)ultramodern::cart_handle; } -extern "C" void osDriveRomInit_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osDriveRomInit_recomp(uint8_t* rdram, recomp_context* ctx) { OSPiHandle* handle = TO_PTR(OSPiHandle, ultramodern::drive_handle); handle->type = 1; // bulk handle->baseAddress = phys_to_k1(drive_base); @@ -98,7 +100,7 @@ const std::u8string save_folder = u8"saves"; extern std::filesystem::path config_path; std::filesystem::path get_save_file_path() { - return config_path / save_folder / (std::u8string{recomp::current_game_id()} + u8".bin"); + return config_path / save_folder / (std::u8string{ recomp::current_game_id() } + u8".bin"); } void update_save_file() { @@ -107,7 +109,8 @@ void update_save_file() { if (save_file.good()) { std::lock_guard lock{ save_context.save_buffer_mutex }; save_file.write(save_context.save_buffer.data(), save_context.save_buffer.size()); - } else { + } + else { fprintf(stderr, "Failed to save!\n"); std::exit(EXIT_FAILURE); } @@ -140,16 +143,16 @@ void saving_thread_func(RDRAM_ARG1) { void save_write_ptr(const void* in, uint32_t offset, uint32_t count) { { - std::lock_guard lock { save_context.save_buffer_mutex }; + std::lock_guard lock{ save_context.save_buffer_mutex }; memcpy(&save_context.save_buffer[offset], in, count); } - + save_context.write_sempahore.signal(); } void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { { - std::lock_guard lock { save_context.save_buffer_mutex }; + std::lock_guard lock{ save_context.save_buffer_mutex }; for (uint32_t i = 0; i < count; i++) { save_context.save_buffer[offset + i] = MEM_B(i, rdram_address); } @@ -159,7 +162,7 @@ void save_write(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t cou } void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t count) { - std::lock_guard lock { save_context.save_buffer_mutex }; + std::lock_guard lock{ save_context.save_buffer_mutex }; for (size_t i = 0; i < count; i++) { MEM_B(i, rdram_address) = save_context.save_buffer[offset + i]; } @@ -167,7 +170,7 @@ void save_read(RDRAM_ARG PTR(void) rdram_address, uint32_t offset, uint32_t coun void save_clear(uint32_t start, uint32_t size, char value) { { - std::lock_guard lock { save_context.save_buffer_mutex }; + std::lock_guard lock{ save_context.save_buffer_mutex }; std::fill_n(save_context.save_buffer.begin() + start, size, value); } @@ -184,12 +187,13 @@ void ultramodern::init_saving(RDRAM_ARG1) { std::ifstream save_file{ save_file_path, std::ios_base::binary }; if (save_file.good()) { save_file.read(save_context.save_buffer.data(), save_context.save_buffer.size()); - } else { + } + else { // Otherwise clear the save file to all zeroes. save_context.save_buffer.fill(0); } - save_context.saving_thread = std::thread{saving_thread_func, PASS_RDRAM}; + save_context.saving_thread = std::thread{ saving_thread_func, PASS_RDRAM }; } void ultramodern::join_saving_thread() { @@ -206,26 +210,31 @@ void do_dma(RDRAM_ARG PTR(OSMesgQueue) mq, gpr rdram_address, uint32_t physical_ // Send a message to the mq to indicate that the transfer completed osSendMesg(rdram, mq, 0, OS_MESG_NOBLOCK); - } else if (physical_addr >= sram_base) { + } + else if (physical_addr >= sram_base) { // read sram save_read(rdram, rdram_address, physical_addr - sram_base, size); // Send a message to the mq to indicate that the transfer completed osSendMesg(rdram, mq, 0, OS_MESG_NOBLOCK); - } else { + } + else { fprintf(stderr, "[WARN] PI DMA read from unknown region, phys address 0x%08X\n", physical_addr); } - } else { + } + else { if (physical_addr >= rom_base) { // write cart rom throw std::runtime_error("ROM DMA write unimplemented"); - } else if (physical_addr >= sram_base) { + } + else if (physical_addr >= sram_base) { // write sram save_write(rdram, rdram_address, physical_addr - sram_base, size); // Send a message to the mq to indicate that the transfer completed osSendMesg(rdram, mq, 0, OS_MESG_NOBLOCK); - } else { + } + else { fprintf(stderr, "[WARN] PI DMA write to unknown region, phys address 0x%08X\n", physical_addr); } } @@ -265,7 +274,7 @@ extern "C" void osEPiStartDma_recomp(RDRAM_ARG recomp_context* ctx) { ctx->r2 = 0; } -extern "C" void osEPiReadIo_recomp(RDRAM_ARG recomp_context * ctx) { +extern "C" void osEPiReadIo_recomp(RDRAM_ARG recomp_context* ctx) { OSPiHandle* handle = TO_PTR(OSPiHandle, ctx->r4); uint32_t devAddr = handle->baseAddress | ctx->r5; gpr dramAddr = ctx->r6; @@ -274,7 +283,8 @@ extern "C" void osEPiReadIo_recomp(RDRAM_ARG recomp_context * ctx) { if (physical_addr > rom_base) { // cart rom recomp::do_rom_pio(PASS_RDRAM dramAddr, physical_addr); - } else { + } + else { // sram assert(false && "SRAM ReadIo unimplemented"); } @@ -282,10 +292,10 @@ extern "C" void osEPiReadIo_recomp(RDRAM_ARG recomp_context * ctx) { ctx->r2 = 0; } -extern "C" void osPiGetStatus_recomp(RDRAM_ARG recomp_context * ctx) { +extern "C" void osPiGetStatus_recomp(RDRAM_ARG recomp_context* ctx) { ctx->r2 = 0; } -extern "C" void osPiRawStartDma_recomp(RDRAM_ARG recomp_context * ctx) { +extern "C" void osPiRawStartDma_recomp(RDRAM_ARG recomp_context* ctx) { ctx->r2 = 0; } diff --git a/librecomp/src/print.cpp b/librecomp/src/print.cpp index 2e10ca4..f404a07 100644 --- a/librecomp/src/print.cpp +++ b/librecomp/src/print.cpp @@ -1,35 +1,36 @@ #include -#include -#include -#include "recomp.h" +#include "ultramodern/ultra64.h" +#include "ultramodern/ultramodern.hpp" + #include "euc-jp.h" +#include "recomp.h" -extern "C" void __checkHardware_msp_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __checkHardware_msp_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 0; } -extern "C" void __checkHardware_kmc_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __checkHardware_kmc_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 0; } -extern "C" void __checkHardware_isv_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __checkHardware_isv_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 0; } -extern "C" void __osInitialize_msp_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osInitialize_msp_recomp(uint8_t* rdram, recomp_context* ctx) { } -extern "C" void __osInitialize_kmc_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osInitialize_kmc_recomp(uint8_t* rdram, recomp_context* ctx) { } -extern "C" void __osInitialize_isv_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osInitialize_isv_recomp(uint8_t* rdram, recomp_context* ctx) { } -extern "C" void isPrintfInit_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void isPrintfInit_recomp(uint8_t* rdram, recomp_context* ctx) { } -extern "C" void __osRdbSend_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osRdbSend_recomp(uint8_t* rdram, recomp_context* ctx) { gpr buf = ctx->r4; size_t size = ctx->r5; u32 type = (u32)ctx->r6; @@ -45,16 +46,16 @@ extern "C" void __osRdbSend_recomp(uint8_t * rdram, recomp_context * ctx) { ctx->r2 = size; } -extern "C" void is_proutSyncPrintf_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void is_proutSyncPrintf_recomp(uint8_t* rdram, recomp_context* ctx) { // Buffering to speed up print performance static std::vector print_buffer; gpr buf = ctx->r5; size_t size = ctx->r6; - //for (size_t i = 0; i < size; i++) { - // // Add the new character to the buffer - // char cur_char = MEM_B(i, buf); + // for (size_t i = 0; i < size; i++) { + // // Add the new character to the buffer + // char cur_char = MEM_B(i, buf); // // If the new character is a newline, flush the buffer // if (cur_char == '\n') { @@ -66,7 +67,7 @@ extern "C" void is_proutSyncPrintf_recomp(uint8_t * rdram, recomp_context * ctx) // } //} - //fwrite(to_print.get(), size, 1, stdout); + // fwrite(to_print.get(), size, 1, stdout); ctx->r2 = 1; } diff --git a/librecomp/src/recomp.cpp b/librecomp/src/recomp.cpp index 41ab597..c82d214 100644 --- a/librecomp/src/recomp.cpp +++ b/librecomp/src/recomp.cpp @@ -1,22 +1,23 @@ +#include +#include #include #include #include -#include -#include -#include -#include #include #include -#include +#include #include -#include +#include +#include +#include + +#include "xxHash/xxh3.h" #include "recomp.h" -#include "recomp_overlays.h" #include "recomp_game.h" -#include "xxHash/xxh3.h" -#include "ultramodern/ultramodern.hpp" +#include "recomp_overlays.h" #include "ultramodern/error_handling.hpp" +#include "ultramodern/ultramodern.hpp" #ifdef _MSC_VER inline uint32_t byteswap(uint32_t val) { @@ -42,7 +43,7 @@ std::mutex current_game_mutex; // Global variables std::filesystem::path config_path; std::vector patch_data; -std::unordered_map game_roms {}; +std::unordered_map game_roms{}; std::u8string recomp::GameEntry::stored_filename() const { return game_id + u8".z64"; @@ -72,7 +73,7 @@ bool check_hash(const std::vector& rom_data, uint64_t expected_hash) { static std::vector read_file(const std::filesystem::path& path) { std::vector ret; - std::ifstream file{ path, std::ios::binary}; + std::ifstream file{ path, std::ios::binary }; if (file.good()) { file.seekg(0, std::ios::end); @@ -116,7 +117,7 @@ bool recomp::is_rom_valid(std::u8string& game_id) { } void recomp::check_all_stored_roms() { - for (const auto& cur_rom_entry: game_roms) { + for (const auto& cur_rom_entry : game_roms) { if (check_stored_rom(cur_rom_entry.second)) { valid_game_roms.insert(cur_rom_entry.first); } @@ -129,7 +130,7 @@ bool recomp::load_stored_rom(std::u8string& game_id) { if (find_it == game_roms.end()) { return false; } - + std::vector stored_rom_data = read_file(config_path / find_it->second.stored_filename()); if (!check_hash(stored_rom_data, find_it->second.rom_hash)) { @@ -142,7 +143,7 @@ bool recomp::load_stored_rom(std::u8string& game_id) { return true; } -const std::array first_rom_bytes { 0x80, 0x37, 0x12, 0x40 }; +const std::array first_rom_bytes{ 0x80, 0x37, 0x12, 0x40 }; enum class ByteswapType { NotByteswapped, @@ -157,25 +158,22 @@ ByteswapType check_rom_start(const std::vector& rom_data) { } auto check_match = [&](uint8_t index0, uint8_t index1, uint8_t index2, uint8_t index3) { - return - rom_data[0] == first_rom_bytes[index0] && - rom_data[1] == first_rom_bytes[index1] && - rom_data[2] == first_rom_bytes[index2] && - rom_data[3] == first_rom_bytes[index3]; + return rom_data[0] == first_rom_bytes[index0] && rom_data[1] == first_rom_bytes[index1] && rom_data[2] == first_rom_bytes[index2] && + rom_data[3] == first_rom_bytes[index3]; }; // Check if the ROM is already in the correct byte order. - if (check_match(0,1,2,3)) { + if (check_match(0, 1, 2, 3)) { return ByteswapType::NotByteswapped; } // Check if the ROM has been byteswapped in groups of 4 bytes. - if (check_match(3,2,1,0)) { + if (check_match(3, 2, 1, 0)) { return ByteswapType::Byteswapped4; } // Check if the ROM has been byteswapped in groups of 2 bytes. - if (check_match(1,0,3,2)) { + if (check_match(1, 0, 3, 2)) { return ByteswapType::Byteswapped2; } @@ -231,12 +229,13 @@ recomp::RomValidationError recomp::select_rom(const std::filesystem::path& rom_p } if (!check_hash(rom_data, game_entry.rom_hash)) { - const std::string_view name{ reinterpret_cast(rom_data.data()) + 0x20, game_entry.internal_name.size()}; + const std::string_view name{ reinterpret_cast(rom_data.data()) + 0x20, game_entry.internal_name.size() }; if (name == game_entry.internal_name) { return recomp::RomValidationError::IncorrectVersion; } else { - if (game_entry.is_enabled && std::string_view{ reinterpret_cast(rom_data.data()) + 0x20, 19 } == game_entry.internal_name) { + if (game_entry.is_enabled && + std::string_view{ reinterpret_cast(rom_data.data()) + 0x20, 19 } == game_entry.internal_name) { return recomp::RomValidationError::NotYet; } else { @@ -246,11 +245,11 @@ recomp::RomValidationError recomp::select_rom(const std::filesystem::path& rom_p } write_file(config_path / game_entry.stored_filename(), rom_data); - + return recomp::RomValidationError::Good; } -extern "C" void osGetMemSize_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osGetMemSize_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 8 * 1024 * 1024; } @@ -288,7 +287,7 @@ extern "C" void cop0_status_write(recomp_context* ctx, gpr value) { assert(false); exit(EXIT_FAILURE); } - + // Update the status register in the context ctx->status_reg = new_sr; } @@ -354,9 +353,9 @@ void init(uint8_t* rdram, recomp_context* ctx, gpr entrypoint) { constexpr int32_t osVersion = 0x80000314; constexpr int32_t osMemSize = 0x80000318; constexpr int32_t osAppNMIBuffer = 0x8000031c; - MEM_W(osTvType, 0) = 1; // NTSC - MEM_W(osRomBase, 0) = 0xB0000000u; // standard rom base - MEM_W(osResetType, 0) = 0; // cold reset + MEM_W(osTvType, 0) = 1; // NTSC + MEM_W(osRomBase, 0) = 0xB0000000u; // standard rom base + MEM_W(osResetType, 0) = 0; // cold reset MEM_W(osMemSize, 0) = 8 * 1024 * 1024; // 8MB } @@ -392,17 +391,22 @@ void ultramodern::quit() { current_game.reset(); } -void recomp::start(ultramodern::WindowHandle window_handle, const recomp::rsp::callbacks_t& rsp_callbacks, const ultramodern::audio_callbacks_t& audio_callbacks, const ultramodern::input_callbacks_t& input_callbacks, const ultramodern::gfx_callbacks_t& gfx_callbacks_, const ultramodern::events::callbacks_t& thread_callbacks_, const ultramodern::error_handling::callbacks_t& error_handling_callbacks_) { +void recomp::start( + ultramodern::WindowHandle window_handle, const recomp::rsp::callbacks_t& rsp_callbacks, + const ultramodern::audio_callbacks_t& audio_callbacks, const ultramodern::input_callbacks_t& input_callbacks, + const ultramodern::gfx_callbacks_t& gfx_callbacks_, const ultramodern::events::callbacks_t& thread_callbacks_, + const ultramodern::error_handling::callbacks_t& error_handling_callbacks_) { recomp::check_all_stored_roms(); recomp::rsp::set_callbacks(rsp_callbacks); - static const ultramodern::rsp::callbacks_t ultramodern_rsp_callbacks { + static const ultramodern::rsp::callbacks_t ultramodern_rsp_callbacks{ .init = recomp::rsp::constants_init, .run_task = recomp::rsp::run_task, }; - ultramodern::set_callbacks(ultramodern_rsp_callbacks, audio_callbacks, input_callbacks, gfx_callbacks_, thread_callbacks_, error_handling_callbacks_); + ultramodern::set_callbacks( + ultramodern_rsp_callbacks, audio_callbacks, input_callbacks, gfx_callbacks_, thread_callbacks_, error_handling_callbacks_); set_input_callbacks(input_callbacks); @@ -427,20 +431,20 @@ void recomp::start(ultramodern::WindowHandle window_handle, const recomp::rsp::c std::unique_ptr rdram_buffer = std::make_unique(ultramodern::rdram_size); std::memset(rdram_buffer.get(), 0, ultramodern::rdram_size); - std::thread game_thread{[](ultramodern::WindowHandle window_handle, uint8_t* rdram) { - debug_printf("[Recomp] Starting\n"); + std::thread game_thread{ + [](ultramodern::WindowHandle window_handle, uint8_t* rdram) { + debug_printf("[Recomp] Starting\n"); - ultramodern::set_native_thread_name("Game Start Thread"); + ultramodern::set_native_thread_name("Game Start Thread"); - ultramodern::preinit(rdram, window_handle); + ultramodern::preinit(rdram, window_handle); - game_status.wait(GameStatus::None); - recomp_context context{}; + game_status.wait(GameStatus::None); + recomp_context context{}; - switch (game_status.load()) { - // TODO refactor this to allow a project to specify what entrypoint function to run for a give game. - case GameStatus::Running: - { + switch (game_status.load()) { + // TODO refactor this to allow a project to specify what entrypoint function to run for a give game. + case GameStatus::Running: { if (!recomp::load_stored_rom(current_game.value())) { ultramodern::error_handling::message_box("Error opening stored ROM! Please restart this program."); } @@ -455,20 +459,21 @@ void recomp::start(ultramodern::WindowHandle window_handle, const recomp::rsp::c try { game_entry.entrypoint(rdram, &context); } catch (ultramodern::thread_terminated& terminated) { - } - } - break; + } break; - case GameStatus::Quit: - break; + case GameStatus::Quit: + break; - case GameStatus::None: - break; - } + case GameStatus::None: + break; + } - debug_printf("[Recomp] Quitting\n"); - }, window_handle, rdram_buffer.get()}; + debug_printf("[Recomp] Quitting\n"); + }, + window_handle, + rdram_buffer.get(), + }; while (!exited) { ultramodern::sleep_milliseconds(1); diff --git a/librecomp/src/rsp.cpp b/librecomp/src/rsp.cpp index d8df936..3d5e9ce 100644 --- a/librecomp/src/rsp.cpp +++ b/librecomp/src/rsp.cpp @@ -1,10 +1,10 @@ #include -#include #include +#include #include "rsp.h" -static recomp::rsp::callbacks_t rsp_callbacks {}; +static recomp::rsp::callbacks_t rsp_callbacks{}; void recomp::rsp::set_callbacks(const callbacks_t& callbacks) { rsp_callbacks = callbacks; @@ -26,8 +26,9 @@ void recomp::rsp::constants_init() { for (u16 index = 0; index < 512; index++) { u64 a = (index + 512) >> ((index % 2 == 1) ? 1 : 0); u64 b = 1 << 17; - //find the largest b where b < 1.0 / sqrt(a) - while (a * (b + 1) * (b + 1) < (u64(1) << 44)) b++; + // find the largest b where b < 1.0 / sqrt(a) + while (a * (b + 1) * (b + 1) < (u64(1) << 44)) + b++; rspInverseSquareRoots[index] = u16(b >> 1); } } diff --git a/librecomp/src/sp.cpp b/librecomp/src/sp.cpp index ae0f44f..c9e5f4f 100644 --- a/librecomp/src/sp.cpp +++ b/librecomp/src/sp.cpp @@ -1,6 +1,8 @@ #include #include -#include + +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" extern "C" void osSpTaskLoad_recomp(uint8_t* rdram, recomp_context* ctx) { @@ -10,12 +12,13 @@ extern "C" void osSpTaskLoad_recomp(uint8_t* rdram, recomp_context* ctx) { bool dump_frame = false; extern "C" void osSpTaskStartGo_recomp(uint8_t* rdram, recomp_context* ctx) { - //printf("[sp] osSpTaskStartGo(0x%08X)\n", (uint32_t)ctx->r4); + // printf("[sp] osSpTaskStartGo(0x%08X)\n", (uint32_t)ctx->r4); OSTask* task = TO_PTR(OSTask, ctx->r4); if (task->t.type == M_GFXTASK) { - //printf("[sp] Gfx task: %08X\n", (uint32_t)ctx->r4); - } else if (task->t.type == M_AUDTASK) { - //printf("[sp] Audio task: %08X\n", (uint32_t)ctx->r4); + // printf("[sp] Gfx task: %08X\n", (uint32_t)ctx->r4); + } + else if (task->t.type == M_AUDTASK) { + // printf("[sp] Audio task: %08X\n", (uint32_t)ctx->r4); } // For debugging if (dump_frame) { @@ -24,7 +27,7 @@ extern "C" void osSpTaskStartGo_recomp(uint8_t* rdram, recomp_context* ctx) { std::unique_ptr ram_unswapped = std::make_unique(ram_size); snprintf(addr_str, sizeof(addr_str) - 1, "%08X", task->t.data_ptr); addr_str[sizeof(addr_str) - 1] = '\0'; - std::ofstream dump_file{ "ramdump" + std::string{ addr_str } + ".bin", std::ios::binary}; + std::ofstream dump_file{ "ramdump" + std::string{ addr_str } + ".bin", std::ios::binary }; for (size_t i = 0; i < ram_size; i++) { ram_unswapped[i] = rdram[i ^ 3]; diff --git a/librecomp/src/ultra_stubs.cpp b/librecomp/src/ultra_stubs.cpp index 9dd263b..d5c24b2 100644 --- a/librecomp/src/ultra_stubs.cpp +++ b/librecomp/src/ultra_stubs.cpp @@ -1,45 +1,45 @@ -#include -#include +#include "ultramodern/ultra64.h" +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" // None of these functions need to be reimplemented, so stub them out -extern "C" void osUnmapTLBAll_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osUnmapTLBAll_recomp(uint8_t* rdram, recomp_context* ctx) { // TODO this will need to be implemented in the future for any games that actually use the TLB } -extern "C" void osVoiceInit_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceInit_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 11; // CONT_ERR_DEVICE } -extern "C" void osVoiceSetWord_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceSetWord_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceCheckWord_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceCheckWord_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceStopReadData_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceStopReadData_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceMaskDictionary_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceMaskDictionary_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceStartReadData_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceStartReadData_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceControlGain_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceControlGain_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceGetReadData_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceGetReadData_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } -extern "C" void osVoiceClearDictionary_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVoiceClearDictionary_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); } - diff --git a/librecomp/src/ultra_translation.cpp b/librecomp/src/ultra_translation.cpp index 24ea0bd..b64d3ea 100644 --- a/librecomp/src/ultra_translation.cpp +++ b/librecomp/src/ultra_translation.cpp @@ -1,34 +1,37 @@ #include -#include -#include + +#include "ultramodern/ultra64.h" +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" -extern "C" void osInitialize_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osInitialize_recomp(uint8_t* rdram, recomp_context* ctx) { osInitialize(); } -extern "C" void __osInitialize_common_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osInitialize_common_recomp(uint8_t* rdram, recomp_context* ctx) { osInitialize(); } extern "C" void osCreateThread_recomp(uint8_t* rdram, recomp_context* ctx) { - osCreateThread(rdram, (int32_t)ctx->r4, (OSId)ctx->r5, (int32_t)ctx->r6, (int32_t)ctx->r7, - (int32_t)MEM_W(0x10, ctx->r29), (OSPri)MEM_W(0x14, ctx->r29)); + osCreateThread( + rdram, (int32_t)ctx->r4, (OSId)ctx->r5, (int32_t)ctx->r6, (int32_t)ctx->r7, (int32_t)MEM_W(0x10, ctx->r29), + (OSPri)MEM_W(0x14, ctx->r29)); } extern "C" void osStartThread_recomp(uint8_t* rdram, recomp_context* ctx) { osStartThread(rdram, (int32_t)ctx->r4); } -extern "C" void osStopThread_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osStopThread_recomp(uint8_t* rdram, recomp_context* ctx) { osStopThread(rdram, (int32_t)ctx->r4); } -extern "C" void osDestroyThread_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osDestroyThread_recomp(uint8_t* rdram, recomp_context* ctx) { osDestroyThread(rdram, (int32_t)ctx->r4); } -extern "C" void osYieldThread_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osYieldThread_recomp(uint8_t* rdram, recomp_context* ctx) { assert(false); // osYieldThread(rdram); } @@ -37,11 +40,11 @@ extern "C" void osSetThreadPri_recomp(uint8_t* rdram, recomp_context* ctx) { osSetThreadPri(rdram, (int32_t)ctx->r4, (OSPri)ctx->r5); } -extern "C" void osGetThreadPri_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osGetThreadPri_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = osGetThreadPri(rdram, (int32_t)ctx->r4); } -extern "C" void osGetThreadId_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osGetThreadId_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = osGetThreadId(rdram, (int32_t)ctx->r4); } @@ -65,68 +68,68 @@ extern "C" void osSetEventMesg_recomp(uint8_t* rdram, recomp_context* ctx) { osSetEventMesg(rdram, (OSEvent)ctx->r4, (int32_t)ctx->r5, (OSMesg)ctx->r6); } -extern "C" void osViSetEvent_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osViSetEvent_recomp(uint8_t* rdram, recomp_context* ctx) { osViSetEvent(rdram, (int32_t)ctx->r4, (OSMesg)ctx->r5, (u32)ctx->r6); } -extern "C" void osGetCount_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osGetCount_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = osGetCount(); } -extern "C" void osGetTime_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osGetTime_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t total_count = osGetTime(); ctx->r2 = (int32_t)(total_count >> 32); ctx->r3 = (int32_t)(total_count >> 0); } -extern "C" void osSetTimer_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osSetTimer_recomp(uint8_t* rdram, recomp_context* ctx) { uint64_t countdown = ((uint64_t)(ctx->r6) << 32) | ((ctx->r7) & 0xFFFFFFFFu); uint64_t interval = load_doubleword(rdram, ctx->r29, 0x10); ctx->r2 = osSetTimer(rdram, (int32_t)ctx->r4, countdown, interval, (int32_t)MEM_W(0x18, ctx->r29), (OSMesg)MEM_W(0x1C, ctx->r29)); } -extern "C" void osStopTimer_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osStopTimer_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = osStopTimer(rdram, (int32_t)ctx->r4); } -extern "C" void osVirtualToPhysical_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osVirtualToPhysical_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = osVirtualToPhysical((int32_t)ctx->r4); } -extern "C" void osInvalDCache_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osInvalDCache_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void osInvalICache_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osInvalICache_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void osWritebackDCache_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osWritebackDCache_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void osWritebackDCacheAll_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osWritebackDCacheAll_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void osSetIntMask_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void osSetIntMask_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void __osDisableInt_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osDisableInt_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void __osRestoreInt_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osRestoreInt_recomp(uint8_t* rdram, recomp_context* ctx) { ; } -extern "C" void __osSetFpcCsr_recomp(uint8_t * rdram, recomp_context * ctx) { +extern "C" void __osSetFpcCsr_recomp(uint8_t* rdram, recomp_context* ctx) { ctx->r2 = 0; } // For the Mario Party games (not working) -//extern "C" void longjmp_recomp(uint8_t * rdram, recomp_context * ctx) { +// extern "C" void longjmp_recomp(uint8_t * rdram, recomp_context * ctx) { // RecompJmpBuf* buf = TO_PTR(RecompJmpBuf, ctx->r4); // // // Check if this is a buffer that was set up with setjmp @@ -153,11 +156,11 @@ extern "C" void __osSetFpcCsr_recomp(uint8_t * rdram, recomp_context * ctx) { //} // //#undef setjmp_recomp -//extern "C" void setjmp_recomp(uint8_t * rdram, recomp_context * ctx) { +// extern "C" void setjmp_recomp(uint8_t * rdram, recomp_context * ctx) { // fprintf(stderr, "Program called setjmp_recomp\n"); // std::quick_exit(EXIT_FAILURE); //} // -//extern "C" int32_t osGetThreadEx(void) { +// extern "C" int32_t osGetThreadEx(void) { // return ultramodern::this_thread(); //} diff --git a/librecomp/src/vi.cpp b/librecomp/src/vi.cpp index e38152f..7667dbe 100644 --- a/librecomp/src/vi.cpp +++ b/librecomp/src/vi.cpp @@ -1,11 +1,12 @@ -#include +#include "ultramodern/ultramodern.hpp" + #include "recomp.h" -extern "C" void osViSetYScale_recomp(uint8_t* rdram, recomp_context * ctx) { +extern "C" void osViSetYScale_recomp(uint8_t* rdram, recomp_context* ctx) { osViSetYScale(ctx->f12.fl); } -extern "C" void osViSetXScale_recomp(uint8_t* rdram, recomp_context * ctx) { +extern "C" void osViSetXScale_recomp(uint8_t* rdram, recomp_context* ctx) { osViSetXScale(ctx->f12.fl); }