From 6e45fac005f883b1c5c98d8486f3af226d5b6e00 Mon Sep 17 00:00:00 2001 From: Mr-Wiseguy Date: Sun, 20 Nov 2022 22:56:13 -0500 Subject: [PATCH] "Fixed" implementation of lwl/swl (fixes initial test games), implemented more controller functionality --- recomp.h | 24 +- src/recompilation.cpp | 12 +- test/RecompTest.vcxproj | 169 +-- test/RecompTest.vcxproj.filters | 1712 ++++++++++++++++++++++--------- test/portultra/events.cpp | 12 +- test/src/cont.cpp | 15 +- test/src/recomp.cpp | 12 +- 7 files changed, 1271 insertions(+), 685 deletions(-) diff --git a/recomp.h b/recomp.h index 3568bed..c6b3d3d 100644 --- a/recomp.h +++ b/recomp.h @@ -65,9 +65,27 @@ static inline uint64_t load_doubleword(uint8_t* rdram, gpr reg, gpr offset) { load_doubleword(rdram, offset, reg) // TODO proper lwl/lwr/swl/swr -#define MEM_WL(offset, reg) \ - (*(int32_t*)(rdram + ((((reg) + (offset))) - 0xFFFFFFFF80000000))) - //(*(int32_t*)(rdram + ((((reg) + (offset))) & 0x3FFFFFF))) +static inline void do_swl(uint8_t* rdram, gpr offset, gpr reg, gpr val) { + uint8_t byte0 = val >> 24; + uint8_t byte1 = val >> 16; + uint8_t byte2 = val >> 8; + uint8_t byte3 = val >> 0; + + MEM_B(offset + 0, reg) = byte0; + MEM_B(offset + 1, reg) = byte1; + MEM_B(offset + 2, reg) = byte2; + MEM_B(offset + 3, reg) = byte3; +} + +static inline gpr do_lwl(uint8_t* rdram, gpr offset, gpr reg) { + uint8_t byte0 = MEM_B(offset + 0, reg); + uint8_t byte1 = MEM_B(offset + 1, reg); + uint8_t byte2 = MEM_B(offset + 2, reg); + uint8_t byte3 = MEM_B(offset + 3, reg); + + // Cast to int32_t to sign extend first + return (gpr)(int32_t)((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | (byte3 << 0)); +} #define S32(val) \ ((int32_t)(val)) diff --git a/src/recompilation.cpp b/src/recompilation.cpp index ff0fa86..03c710b 100644 --- a/src/recompilation.cpp +++ b/src/recompilation.cpp @@ -244,16 +244,20 @@ bool process_instruction(const RecompPort::Context& context, const RecompPort::F // LWR x + 2 -> 00000000 0189ABCD // LWR x + 3 -> FFFFFFFF 89ABCDEF case InstrId::cpu_lwl: - print_line("{}{} = MEM_WL({:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); + print_line("{}{} = do_lwl(rdram, {:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); + //print_line("{}{} = MEM_WL({:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); break; case InstrId::cpu_lwr: - print_line("//{}{} = MEM_WR({:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); + //print_line("{}{} = do_lwr(rdram, {:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); + //print_line("//{}{} = MEM_WR({:#X}, {}{})", ctx_gpr_prefix(rt), rt, (int16_t)imm, ctx_gpr_prefix(base), base); break; case InstrId::cpu_swl: - print_line("MEM_WL({:#X}, {}{}) = {}{}", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); + print_line("do_swl(rdram, {:#X}, {}{}, {}{})", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); + //print_line("MEM_WL({:#X}, {}{}) = {}{}", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); break; case InstrId::cpu_swr: - print_line("//MEM_WR({:#X}, {}{}) = {}{}", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); + //print_line("do_swr(rdram, {:#X}, {}{}, {}{})", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); + //print_line("//MEM_WR({:#X}, {}{}) = {}{}", (int16_t)imm, ctx_gpr_prefix(base), base, ctx_gpr_prefix(rt), rt); break; // Branches diff --git a/test/RecompTest.vcxproj b/test/RecompTest.vcxproj index ae0faee..0500d98 100644 --- a/test/RecompTest.vcxproj +++ b/test/RecompTest.vcxproj @@ -131,173 +131,9 @@ XCOPY "$(ProjectDir)Lib\SDL2-2.24.0\lib\$(Platform)\SDL2.dll" "$(TargetDir)" /S - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + <_WildCardClCompile Include="funcs\*.c" /> + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -320,7 +156,6 @@ 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 01c37f3..949c13e 100644 --- a/test/RecompTest.vcxproj.filters +++ b/test/RecompTest.vcxproj.filters @@ -75,507 +75,1220 @@ Source Files - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Funcs - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - - - Funcs - @@ -602,8 +1315,5 @@ Header Files - - Funcs - \ No newline at end of file diff --git a/test/portultra/events.cpp b/test/portultra/events.cpp index febc50d..14f9626 100644 --- a/test/portultra/events.cpp +++ b/test/portultra/events.cpp @@ -180,10 +180,14 @@ void RT64SendDL(uint8_t* rdram, const OSTask* task); void RT64UpdateScreen(uint32_t vi_origin); std::unordered_map button_map{ - { SDL_Scancode::SDL_SCANCODE_LEFT, 0x0002 }, - { SDL_Scancode::SDL_SCANCODE_RIGHT, 0x0001 }, - { SDL_Scancode::SDL_SCANCODE_UP, 0x0008 }, - { SDL_Scancode::SDL_SCANCODE_DOWN, 0x0004 } + { SDL_Scancode::SDL_SCANCODE_LEFT, 0x0002 }, // c left + { SDL_Scancode::SDL_SCANCODE_RIGHT, 0x0001 }, // c right + { SDL_Scancode::SDL_SCANCODE_UP, 0x0008 }, // c up + { SDL_Scancode::SDL_SCANCODE_DOWN, 0x0004 }, // c down + { SDL_Scancode::SDL_SCANCODE_RETURN, 0x1000 }, // start + { SDL_Scancode::SDL_SCANCODE_SPACE, 0x8000 }, // a + { SDL_Scancode::SDL_SCANCODE_LSHIFT, 0x4000 }, // b + { SDL_Scancode::SDL_SCANCODE_Q, 0x2000 }, // z }; extern int button; diff --git a/test/src/cont.cpp b/test/src/cont.cpp index 348235a..b428552 100644 --- a/test/src/cont.cpp +++ b/test/src/cont.cpp @@ -2,7 +2,20 @@ #include "recomp.h" extern "C" void osContInit_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { - ; + gpr bitpattern = ctx->r5; + gpr status = ctx->r6; + MEM_B(0, bitpattern) = 0x01; + + MEM_H(0, status) = 0x0005; // CONT_TYPE_NORMAL + MEM_B(2, status) = 0; // controller status + MEM_B(3, status) = 0; // controller errno + + // Write CHNL_ERR_NORESP for the other controllers + for (size_t controller = 1; controller < 4; controller++) { + MEM_B(4 * controller + 3, status) = 0x80; + } + + ctx->r2 = 0; } extern "C" void osContStartReadData_recomp(uint8_t* restrict rdram, recomp_context* restrict ctx) { diff --git a/test/src/recomp.cpp b/test/src/recomp.cpp index b1f7a12..5905316 100644 --- a/test/src/recomp.cpp +++ b/test/src/recomp.cpp @@ -64,8 +64,10 @@ void run_thread_function(uint8_t* rdram, uint64_t addr, uint64_t sp, uint64_t ar 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); +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; @@ -158,18 +160,18 @@ int main(int argc, char **argv) { // Clear bss // TODO run the entrypoint instead // rocket robot - //memset(rdram_buffer.get() + 0xAF860, 0, 0xC00A0u - 0XAF860); + memset(rdram_buffer.get() + 0xAF860, 0, 0xC00A0u - 0XAF860); // test rom - memset(rdram_buffer.get() + 0x18670, 0, 0x20D120); + //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); + game_init(rdram_buffer.get(), &context); // test rom - init(rdram_buffer.get(), &context); + //init(rdram_buffer.get(), &context); debug_printf("[Recomp] Quitting\n");