From 0af9d489b3de5396b42430ea4bbcb3950d752313 Mon Sep 17 00:00:00 2001 From: Mr-Wiseguy Date: Fri, 25 Nov 2022 15:31:16 -0500 Subject: [PATCH] Made recompiled output run the game's entrypoint, various reorganization, clang warning fixes --- disable_warnings.h | 4 + src/main.cpp | 44 +- src/recompilation.cpp | 1 + test/RecompTest.vcxproj | 29 +- test/RecompTest.vcxproj.filters | 3759 +++++++++++++++++++++++++++- test/portultra/events.cpp | 6 +- test/portultra/misc_ultra.cpp | 24 + test/portultra/ultra64.h | 1 + test/src/misc_ultra.cpp | 47 - test/src/portultra_translation.cpp | 36 + test/src/recomp.cpp | 50 +- test/src/sp.cpp | 18 + test/src/vi.cpp | 2 +- 13 files changed, 3921 insertions(+), 100 deletions(-) create mode 100644 disable_warnings.h create mode 100644 test/portultra/misc_ultra.cpp delete mode 100644 test/src/misc_ultra.cpp diff --git a/disable_warnings.h b/disable_warnings.h new file mode 100644 index 0000000..ff36f12 --- /dev/null +++ b/disable_warnings.h @@ -0,0 +1,4 @@ +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wunused-variable" +#pragma clang diagnostic ignored "-Wimplicit-function-declaration" +#endif diff --git a/src/main.cpp b/src/main.cpp index 19a7e9c..c64e595 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "rabbitizer.hpp" #include "elfio/elfio.hpp" @@ -243,6 +244,7 @@ std::unordered_set renamed_funcs{ "memcpy", "memset", "strchr", + "bzero", }; // Functions that weren't declared properly and thus have no size in the elf @@ -256,8 +258,8 @@ std::unordered_map unsized_funcs{ }; int main(int argc, char** argv) { - if (argc != 2) { - fmt::print("Usage: {} [input elf file]\n", argv[0]); + if (argc != 3) { + fmt::print("Usage: {} [input elf file] [entrypoint RAM address]\n", argv[0]); std::exit(EXIT_SUCCESS); } @@ -271,10 +273,18 @@ int main(int argc, char** argv) { std::exit(EXIT_FAILURE); }; - if (!elf_file.load(argv[1])) { + std::string elf_name{ argv[1] }; + + if (!elf_file.load(elf_name)) { exit_failure("Failed to load provided elf file\n"); } + char* end; + const uint32_t entrypoint = (uint32_t)strtoul(argv[2], &end, 0); + if (argv[2] == end) { + exit_failure("Invalid entrypoint value: " + std::string(argv[2]) + "\n"); + } + if (elf_file.get_class() != ELFIO::ELFCLASS32) { exit_failure("Incorrect elf class\n"); } @@ -320,6 +330,8 @@ int main(int argc, char** argv) { fmt::print("Num symbols: {}\n", symbols.get_symbols_num()); + bool found_entrypoint_func = false; + for (int sym_index = 0; sym_index < symbols.get_symbols_num(); sym_index++) { std::string name; ELFIO::Elf64_Addr value; @@ -336,10 +348,16 @@ int main(int argc, char** argv) { // Check if this symbol is unsized and if so populate its size from the unsized_funcs map if (size == 0) { - auto size_find = unsized_funcs.find(name); - if (size_find != unsized_funcs.end()) { - size = size_find->second; - type = ELFIO::STT_FUNC; + if (value == entrypoint && type == ELFIO::STT_FUNC) { + found_entrypoint_func = true; + size = 0x50; // dummy size for entrypoints, should cover them all + name = "recomp_entrypoint"; + } else { + auto size_find = unsized_funcs.find(name); + if (size_find != unsized_funcs.end()) { + size = size_find->second; + type = ELFIO::STT_FUNC; + } } } @@ -383,6 +401,10 @@ int main(int argc, char** argv) { } } + if (!found_entrypoint_func) { + exit_failure("Could not find entrypoint function\n"); + } + fmt::print("Function count: {}\n", context.functions.size()); std::ofstream func_lookup_file{ "test/funcs/lookup.cpp" }; @@ -421,9 +443,17 @@ int main(int argc, char** argv) { } } } + fmt::print(func_lookup_file, "}};\n" "extern const size_t num_funcs = sizeof(funcs) / sizeof(funcs[0]);\n" + "\n" + "gpr get_entrypoint_address() {{ return (gpr)(int32_t)0x{:08X}u; }}\n" + "\n" + "const char* get_rom_name() {{ return \"{}\"; }}\n" + "\n", + entrypoint, + std::filesystem::path{ elf_name }.replace_extension(".z64").string() ); fmt::print(func_header_file, diff --git a/src/recompilation.cpp b/src/recompilation.cpp index 03c710b..fc5c8d1 100644 --- a/src/recompilation.cpp +++ b/src/recompilation.cpp @@ -771,6 +771,7 @@ bool RecompPort::recompile_function(const RecompPort::Context& context, const Re std::ofstream output_file{ output_path.data() }; fmt::print(output_file, "#include \"recomp.h\"\n" + "#include \"disable_warnings.h\"\n" "\n" "void {}(uint8_t* restrict rdram, recomp_context* restrict ctx) {{\n" // these variables shouldn't need to be preserved across function boundaries, so make them local for more efficient output diff --git a/test/RecompTest.vcxproj b/test/RecompTest.vcxproj index 0500d98..0ce4329 100644 --- a/test/RecompTest.vcxproj +++ b/test/RecompTest.vcxproj @@ -28,12 +28,12 @@ Application true - v142 + ClangCL Application false - v142 + ClangCL Application @@ -69,6 +69,12 @@ true + + true + + + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) @@ -105,11 +111,15 @@ $(ProjectDir)..;$(ProjectDir)thirdparty;$(ProjectDir)Lib\SDL2-2.24.0\include;%(AdditionalIncludeDirectories) - stdcpp20 + stdcpplatest + + + true - Console + Windows $(ProjectDir)RT64\$(Configuration)\RT64.lib;$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.lib;%(AdditionalDependencies) + mainCRTStartup XCOPY "$(ProjectDir)RT64\$(Configuration)\*.dll" "$(TargetDir)" /S /Y @@ -119,11 +129,15 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S $(ProjectDir)..;$(ProjectDir)thirdparty;$(ProjectDir)Lib\SDL2-2.24.0\include;%(AdditionalIncludeDirectories) - stdcpp20 + stdcpplatest + + + true - Console + Windows $(ProjectDir)RT64\$(Configuration)\RT64.lib;$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.lib;%(AdditionalDependencies) + mainCRTStartup XCOPY "$(ProjectDir)RT64\$(Configuration)\*.dll" "$(TargetDir)" /S /Y @@ -147,7 +161,7 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S - + @@ -155,6 +169,7 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S + diff --git a/test/RecompTest.vcxproj.filters b/test/RecompTest.vcxproj.filters index 949c13e..aff44b2 100644 --- a/test/RecompTest.vcxproj.filters +++ b/test/RecompTest.vcxproj.filters @@ -36,7 +36,7 @@ Source Files - + Source Files @@ -1289,6 +1289,3760 @@ Funcs + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1315,5 +5069,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/test/portultra/events.cpp b/test/portultra/events.cpp index 14f9626..b895566 100644 --- a/test/portultra/events.cpp +++ b/test/portultra/events.cpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include #include "SDL.h" @@ -18,7 +20,7 @@ struct SpTaskAction { }; struct SwapBuffersAction { - int32_t origin; + uint32_t origin; }; using Action = std::variant; @@ -262,7 +264,7 @@ void gfx_thread_func(uint8_t* rdram, uint8_t* rom) { } extern "C" void osViSwapBuffer(RDRAM_ARG PTR(void) frameBufPtr) { - events_context.action_queue.enqueue(SwapBuffersAction{ frameBufPtr + 640 }); + events_context.action_queue.enqueue(SwapBuffersAction{ osVirtualToPhysical(frameBufPtr) + 640 }); } void Multilibultra::submit_rsp_task(RDRAM_ARG PTR(OSTask) task_) { diff --git a/test/portultra/misc_ultra.cpp b/test/portultra/misc_ultra.cpp new file mode 100644 index 0000000..aa143a3 --- /dev/null +++ b/test/portultra/misc_ultra.cpp @@ -0,0 +1,24 @@ +#include "ultra64.h" + +extern uint64_t start_time; + +#define K0BASE 0x80000000 +#define K1BASE 0xA0000000 +#define K2BASE 0xC0000000 +#define IS_KSEG0(x) ((u32)(x) >= K0BASE && (u32)(x) < K1BASE) +#define IS_KSEG1(x) ((u32)(x) >= K1BASE && (u32)(x) < K2BASE) +#define K0_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg0 to physical */ +#define K1_TO_PHYS(x) ((u32)(x)&0x1FFFFFFF) /* kseg1 to physical */ + +u32 osVirtualToPhysical(PTR(void) addr) { + uintptr_t addr_val = (uintptr_t)addr; + if (IS_KSEG0(addr_val)) { + return K0_TO_PHYS(addr_val); + } else if (IS_KSEG1(addr_val)) { + return K1_TO_PHYS(addr_val); + } else { + // TODO handle TLB mappings + return (u32)addr_val; + } +} + diff --git a/test/portultra/ultra64.h b/test/portultra/ultra64.h index c5d88eb..dcf5bed 100644 --- a/test/portultra/ultra64.h +++ b/test/portultra/ultra64.h @@ -164,6 +164,7 @@ void osViSetEvent(RDRAM_ARG PTR(OSMesgQueue), OSMesg, u32); void osViSwapBuffer(RDRAM_ARG PTR(void) frameBufPtr); u32 osGetCount(); OSTime osGetTime(); +u32 osVirtualToPhysical(PTR(void) addr); #ifdef __cplusplus } // extern "C" diff --git a/test/src/misc_ultra.cpp b/test/src/misc_ultra.cpp deleted file mode 100644 index 238dfbd..0000000 --- a/test/src/misc_ultra.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#ifdef _WIN32 -#include -#endif - -#include -#include "recomp.h" - -extern uint64_t start_time; - - -extern "C" void osVirtualToPhysical_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - uint32_t virtual_addr = ctx->r4; - // TODO handle TLB mappings - ctx->r2 = virtual_addr - 0x80000000; -} - -extern "C" void osInvalDCache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void osInvalICache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void osWritebackDCache_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void osWritebackDCacheAll_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void osSetIntMask_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void __osDisableInt_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void __osRestoreInt_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; -} - -extern "C" void __osSetFpcCsr_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { - ctx->r2 = 0; -} diff --git a/test/src/portultra_translation.cpp b/test/src/portultra_translation.cpp index 2176684..acbed8d 100644 --- a/test/src/portultra_translation.cpp +++ b/test/src/portultra_translation.cpp @@ -55,3 +55,39 @@ extern "C" void osGetTime_recomp(uint8_t * restrict rdram, recomp_context * rest ctx->r2 = (uint32_t)(total_count >> 32); ctx->r3 = (int32_t)(total_count >> 0); } + +extern "C" void osVirtualToPhysical_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ctx->r2 = osVirtualToPhysical((int32_t)ctx->r2); +} + +extern "C" void osInvalDCache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void osInvalICache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void osWritebackDCache_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void osWritebackDCacheAll_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void osSetIntMask_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void __osDisableInt_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void __osRestoreInt_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ; +} + +extern "C" void __osSetFpcCsr_recomp(uint8_t * restrict rdram, recomp_context * restrict ctx) { + ctx->r2 = 0; +} diff --git a/test/src/recomp.cpp b/test/src/recomp.cpp index 5905316..0bef55b 100644 --- a/test/src/recomp.cpp +++ b/test/src/recomp.cpp @@ -34,7 +34,7 @@ extern "C" recomp_func_t* get_function(uint32_t addr) { return func_find->second; } -extern "C" void bzero(uint8_t* restrict rdram, recomp_context* restrict ctx) { +extern "C" void _bzero(uint8_t* restrict rdram, recomp_context* restrict ctx) { gpr start_addr = ctx->r4; gpr size = ctx->r5; @@ -61,13 +61,6 @@ void run_thread_function(uint8_t* rdram, uint64_t addr, uint64_t sp, uint64_t ar func(rdram, &ctx); } -extern "C" void init(uint8_t * restrict rdram, recomp_context * restrict ctx); - -// rocket robot -extern "C" void game_init(uint8_t* restrict rdram, recomp_context* restrict ctx); -// test rom -//extern "C" void init(uint8_t * restrict rdram, recomp_context * restrict ctx); - void do_rom_read(uint8_t* rdram, gpr ram_address, uint32_t dev_address, size_t num_bytes); std::unique_ptr rom; @@ -75,21 +68,26 @@ size_t rom_size; uint64_t start_time; +// Recomp generation functions +extern "C" void recomp_entrypoint(uint8_t * restrict rdram, recomp_context * restrict ctx); +gpr get_entrypoint_address(); +const char* get_rom_name(); + int main(int argc, char **argv) { - if (argc != 2) { - printf("Usage: %s [baserom]\n", argv[0]); - exit(EXIT_SUCCESS); - } + //if (argc != 2) { + // printf("Usage: %s [baserom]\n", argv[0]); + // exit(EXIT_SUCCESS); + //} { - std::basic_ifstream rom_file{ argv[1], std::ios::binary }; + std::basic_ifstream rom_file{ get_rom_name(), std::ios::binary }; size_t iobuf_size = 0x100000; std::unique_ptr iobuf = std::make_unique(iobuf_size); rom_file.rdbuf()->pubsetbuf(iobuf.get(), iobuf_size); if (!rom_file) { - fprintf(stderr, "Failed to open rom: %s\n", argv[1]); + fprintf(stderr, "Failed to open rom: %s\n", get_rom_name()); exit(EXIT_FAILURE); } @@ -102,16 +100,8 @@ int main(int argc, char **argv) { rom_file.read(rom.get(), rom_size); } - // Byteswap the rom - //for (size_t rom_addr = 0; rom_addr < rom_size; rom_addr += 4) { - // uint32_t word = *reinterpret_cast(rom.get() + rom_addr); - // word = byteswap(word); - // *reinterpret_cast(rom.get() + rom_addr) = word; - //} - - // Get entrypoint from ROM - // TODO fix this for other IPL3 versions - gpr entrypoint = (int32_t)byteswap(*reinterpret_cast(rom.get() + 0x8)); + // Get entrypoint from recomp function + gpr entrypoint = get_entrypoint_address(); // Allocate rdram_buffer std::unique_ptr rdram_buffer = std::make_unique(8 * 1024 * 1024); @@ -157,21 +147,11 @@ int main(int argc, char **argv) { MEM_W(osResetType, 0) = 0; // cold reset MEM_W(osMemSize, 0) = 8 * 1024 * 1024; // 8MB - // Clear bss - // TODO run the entrypoint instead - // rocket robot - memset(rdram_buffer.get() + 0xAF860, 0, 0xC00A0u - 0XAF860); - // test rom - //memset(rdram_buffer.get() + 0x18670, 0, 0x20D120); - debug_printf("[Recomp] Starting\n"); Multilibultra::preinit(rdram_buffer.get(), rom.get()); - // rocket robot - game_init(rdram_buffer.get(), &context); - // test rom - //init(rdram_buffer.get(), &context); + recomp_entrypoint(rdram_buffer.get(), &context); debug_printf("[Recomp] Quitting\n"); diff --git a/test/src/sp.cpp b/test/src/sp.cpp index fb8ce76..24e4bf5 100644 --- a/test/src/sp.cpp +++ b/test/src/sp.cpp @@ -1,4 +1,5 @@ #include +#include #include "../portultra/multilibultra.hpp" #include "recomp.h" @@ -6,6 +7,8 @@ extern "C" void osSpTaskLoad_recomp(uint8_t* restrict rdram, recomp_context* res ; } +bool dump_frame = false; + extern "C" void osSpTaskStartGo_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { //printf("[sp] osSpTaskStartGo(0x%08X)\n", (uint32_t)ctx->r4); OSTask* task = TO_PTR(OSTask, ctx->r4); @@ -14,6 +17,21 @@ extern "C" void osSpTaskStartGo_recomp(uint8_t* restrict rdram, recomp_context* } else if (task->t.type == M_AUDTASK) { printf("[sp] Audio task: %08X\n", (uint32_t)ctx->r4); } + // For debugging + if (dump_frame) { + char addr_str[32]; + constexpr size_t ram_size = 0x800000; + std::unique_ptr ram_unswapped = std::make_unique(ram_size); + sprintf(addr_str, "%08X", task->t.data_ptr); + 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]; + } + + dump_file.write(ram_unswapped.get(), ram_size); + dump_frame = false; + } Multilibultra::submit_rsp_task(rdram, ctx->r4); } diff --git a/test/src/vi.cpp b/test/src/vi.cpp index 910ebc5..694a526 100644 --- a/test/src/vi.cpp +++ b/test/src/vi.cpp @@ -22,7 +22,7 @@ extern "C" void osViGetNextFramebuffer_recomp(uint8_t* restrict rdram, recomp_co } extern "C" void osViSwapBuffer_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - osViSwapBuffer(rdram, ctx->r4); + osViSwapBuffer(rdram, (int32_t)ctx->r4); } extern "C" void osViSetMode_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) {