From 249bb97ca9411db451559cbed414ff685b4a7eb1 Mon Sep 17 00:00:00 2001 From: SimoneN64 Date: Wed, 15 May 2024 22:55:32 +0200 Subject: [PATCH] Introduce panic_trace and small accuracy fix --- CMakeLists.txt | 1 + src/backend/core/Mem.cpp | 16 ++++++------- src/backend/core/RSP.cpp | 10 ++++++-- src/backend/core/mmio/MI.cpp | 2 +- src/utils/log.hpp | 46 ++++++++++++++++++++++++++++++------ 5 files changed, 57 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 511a7379..8f6e1226 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ if(${CMAKE_BUILD_TYPE} MATCHES Debug) add_compile_definitions(VULKAN_DEBUG) #add_compile_options(-fsanitize=address -fsanitize=undefined) #add_link_options(-fsanitize=address -fsanitize=undefined) + add_link_options(-Wl,--export-dynamic) endif() find_package(Qt6 COMPONENTS Core Gui Widgets) diff --git a/src/backend/core/Mem.cpp b/src/backend/core/Mem.cpp index c909f485..9ca44788 100644 --- a/src/backend/core/Mem.cpp +++ b/src/backend/core/Mem.cpp @@ -208,7 +208,7 @@ template<> u8 Mem::Read(n64::Registers ®s, u32 paddr) { return si.pif.bootrom[BYTE_ADDRESS(paddr) - PIF_ROM_REGION_START]; case PIF_RAM_REGION: return si.pif.ram[paddr - PIF_RAM_REGION_START]; - case 0x00800000 ... 0x03FFFFFF: // unused + case 0x00800000 ... 0x03EFFFFF: // unused case 0x04200000 ... 0x042FFFFF: // unused case 0x04900000 ... 0x04FFFFFF: // unused case 0x1FC00800 ... 0xFFFFFFFF: // unused @@ -243,7 +243,7 @@ template<> u16 Mem::Read(n64::Registers ®s, u32 paddr) { return Util::ReadAccess(si.pif.bootrom, HALF_ADDRESS(paddr) - PIF_ROM_REGION_START); case PIF_RAM_REGION: return be16toh(Util::ReadAccess(si.pif.ram, paddr - PIF_RAM_REGION_START)); - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: @@ -278,7 +278,7 @@ template<> u32 Mem::Read(n64::Registers ®s, u32 paddr) { return Util::ReadAccess(si.pif.bootrom, paddr - PIF_ROM_REGION_START); case PIF_RAM_REGION: return be32toh(Util::ReadAccess(si.pif.ram, paddr - PIF_RAM_REGION_START)); - case 0x00800000 ... 0x03FFFFFF: case 0x04200000 ... 0x042FFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: return 0; default: Util::panic("Unimplemented 32-bit read at address {:08X} (PC = {:016X})", paddr, (u64) regs.pc); @@ -310,7 +310,7 @@ template<> u64 Mem::Read(n64::Registers ®s, u32 paddr) { return Util::ReadAccess(si.pif.bootrom, paddr - PIF_ROM_REGION_START); case PIF_RAM_REGION: return be64toh(Util::ReadAccess(si.pif.ram, paddr - PIF_RAM_REGION_START)); - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case 0x1FC00800 ... 0xFFFFFFFF: @@ -352,7 +352,7 @@ template<> void Mem::Write(Registers& regs, u32 paddr, u32 val) { Util::WriteAccess(si.pif.ram, paddr, htobe32(val)); si.pif.ProcessCommands(*this); break; - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case PIF_ROM_REGION: @@ -397,7 +397,7 @@ template<> void Mem::Write(Registers& regs, u32 paddr, u32 val) { Util::WriteAccess(si.pif.ram, paddr - PIF_RAM_REGION_START, htobe32(val)); si.pif.ProcessCommands(*this); break; - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case PIF_ROM_REGION: @@ -439,7 +439,7 @@ template<> void Mem::Write(Registers& regs, u32 paddr, u32 val) { Util::WriteAccess(si.pif.ram, paddr - PIF_RAM_REGION_START, htobe32(val)); si.pif.ProcessCommands(*this); break; - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case PIF_ROM_REGION: @@ -478,7 +478,7 @@ void Mem::Write(Registers& regs, u32 paddr, u64 val) { Util::WriteAccess(si.pif.ram, paddr - PIF_RAM_REGION_START, htobe64(val)); si.pif.ProcessCommands(*this); break; - case 0x00800000 ... 0x03FFFFFF: + case 0x00800000 ... 0x03EFFFFF: case 0x04200000 ... 0x042FFFFF: case 0x04900000 ... 0x04FFFFFF: case 0x1FC00000 ... 0x1FC007BF: diff --git a/src/backend/core/RSP.cpp b/src/backend/core/RSP.cpp index cc454e77..99175f55 100644 --- a/src/backend/core/RSP.cpp +++ b/src/backend/core/RSP.cpp @@ -146,7 +146,9 @@ void RSP::DMAtoRDRAM(std::vector& rdram) { for (u32 i = 0; i < spDMALen.count + 1; i++) { for(u32 j = 0; j < length; j++) { - dst[dram_address + j] = src[(mem_address + j) & 0xFFF]; + if((dram_address + j) < RDRAM_SIZE) { + dst[dram_address + j] = src[(mem_address + j) & DMEM_DSIZE]; + } } int skip = i == spDMALen.count ? 0 : spDMALen.skip; @@ -176,7 +178,11 @@ void RSP::DMAtoRSP(std::vector& rdram) { for (u32 i = 0; i < spDMALen.count + 1; i++) { for(u32 j = 0; j < length; j++) { - dst[(mem_address + j) & 0xFFF] = src[dram_address + j]; + if((dram_address + j) < RDRAM_SIZE) { + dst[(mem_address + j) & DMEM_DSIZE] = src[dram_address + j]; + } else { + dst[(mem_address + j) & DMEM_DSIZE] = 0; + } } int skip = i == spDMALen.count ? 0 : spDMALen.skip; diff --git a/src/backend/core/mmio/MI.cpp b/src/backend/core/mmio/MI.cpp index 4fb85ff9..9140c17c 100644 --- a/src/backend/core/mmio/MI.cpp +++ b/src/backend/core/mmio/MI.cpp @@ -78,7 +78,7 @@ void MI::Write(u32 paddr, u32 val) { UpdateInterrupt(); break; default: - Util::panic("Unhandled MI[{:08X}] write ({:08X})", val, paddr); + Util::panic_trace("Unhandled MI write @ 0x{:08X} with value 0x{:08X}", paddr, val); } } } diff --git a/src/utils/log.hpp b/src/utils/log.hpp index fff4d027..f9fdc0ba 100644 --- a/src/utils/log.hpp +++ b/src/utils/log.hpp @@ -2,6 +2,10 @@ #include #include #include +#include +#ifndef NDEBUG +#include +#endif namespace Util { enum LogLevel : u8 { @@ -22,7 +26,7 @@ constexpr void print(const std::string& fmt, Args... args) { fmt::print(fmt::emphasis::bold | fg(fmt::color::red), fmt, args...); } else if constexpr(messageType == Warn) { fmt::print(fg(fmt::color::yellow), fmt, args...); - } else if constexpr(messageType == Info) { + } else if constexpr(messageType == Info || messageType == Trace) { fmt::print(fmt, args...); } else if constexpr(messageType == Debug) { #ifndef NDEBUG @@ -43,32 +47,60 @@ constexpr void print(const std::string& fmt, Args... args) { template constexpr void panic(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[FATAL] " + fmt + "\n", args...); exit(-1); } template constexpr void error(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[ERROR] " + fmt + "\n", args...); } template constexpr void warn(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[WARN] " + fmt + "\n", args...); } template constexpr void info(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[INFO] " + fmt + "\n", args...); } template constexpr void debug(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[DEBUG] " + fmt + "\n", args...); } template constexpr void trace(const std::string& fmt, Args... args) { - print(fmt + "\n", args...); + print("[TRACE] " + fmt + "\n", args...); +} + +template +constexpr void panic_trace(const std::string& fmt, Args... args) { +#ifndef NDEBUG + Dl_info info; + auto tmp = fmt::format(fmt + '\n', args...); + tmp += "Called by:\n"; + if(dladdr(__builtin_return_address(0), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(1), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(2), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(3), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(4), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(5), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + if(dladdr(__builtin_return_address(6), &info)) + tmp += fmt::format("\t-> {}\n", info.dli_sname); + + print("[FATAL TRACE] " + tmp); + exit(-1); +#else + panic(fmt, args...); +#endif } } \ No newline at end of file