diff --git a/src/backend/RomHelpers.hpp b/src/backend/RomHelpers.hpp index 8e4e6c95..8b29b3d2 100644 --- a/src/backend/RomHelpers.hpp +++ b/src/backend/RomHelpers.hpp @@ -10,10 +10,10 @@ namespace Util { template FORCE_INLINE void SwapN64Rom(std::vector &rom, u32 endianness) { u8 altByteShift = 0; - if ((endianness >> 24) != 0x80) { + if (endianness >> 24 != 0x80) { if ((endianness & 0xFF) != 0x80) { - if (((endianness >> 16) & 0xff) != 0x80) { - Util::panic("TODO: Unrecognized rom endianness. Ideally, this should be more robust"); + if ((endianness >> 16 & 0xff) != 0x80) { + panic("TODO: Unrecognized rom endianness. Ideally, this should be more robust"); } else { altByteShift = 12; } @@ -28,17 +28,17 @@ FORCE_INLINE void SwapN64Rom(std::vector &rom, u32 endianness) { switch (endianness) { case V64: - SwapBuffer16(rom); + SwapBuffer(rom); if constexpr (!toBE) - SwapBuffer32(rom); + SwapBuffer(rom); break; case N64: if constexpr (toBE) - SwapBuffer32(rom); + SwapBuffer(rom); break; case Z64: if constexpr (!toBE) - SwapBuffer32(rom); + SwapBuffer(rom); break; default: panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!"); diff --git a/src/backend/core/Mem.hpp b/src/backend/core/Mem.hpp index e0320707..5f03d36d 100644 --- a/src/backend/core/Mem.hpp +++ b/src/backend/core/Mem.hpp @@ -109,21 +109,21 @@ struct Mem { std::vector temp{}; temp.resize(RDRAM_SIZE); std::copy(mmio.rdp.rdram.begin(), mmio.rdp.rdram.end(), temp.begin()); - Util::SwapBuffer32(temp); + Util::SwapBuffer(temp); Util::WriteFileBinary(temp, "rdram.bin"); } FORCE_INLINE void DumpIMEM() const { std::array temp{}; std::copy(mmio.rsp.imem.begin(), mmio.rsp.imem.end(), temp.begin()); - Util::SwapBuffer32(temp); + Util::SwapBuffer(temp); Util::WriteFileBinary(temp, "imem.bin"); } FORCE_INLINE void DumpDMEM() const { std::array temp{}; std::copy(mmio.rsp.dmem.begin(), mmio.rsp.dmem.end(), temp.begin()); - Util::SwapBuffer32(temp); + Util::SwapBuffer(temp); Util::WriteFileBinary(temp, "dmem.bin"); } ROM rom; diff --git a/src/utils/MemoryHelpers.hpp b/src/utils/MemoryHelpers.hpp index 08338014..8201c555 100644 --- a/src/utils/MemoryHelpers.hpp +++ b/src/utils/MemoryHelpers.hpp @@ -91,37 +91,23 @@ static FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const T val) { } } -FORCE_INLINE void SwapBuffer32(std::vector &data) { - for (size_t i = 0; i < data.size(); i += 4) { - const u32 original = *reinterpret_cast(&data[i]); - *reinterpret_cast(&data[i]) = bswap(original); +template +static FORCE_INLINE void SwapBuffer(std::vector &data) { + for (size_t i = 0; i < data.size(); i += sizeof(T)) { + const T original = *reinterpret_cast(&data[i]); + *reinterpret_cast(&data[i]) = bswap(original); } } -FORCE_INLINE void SwapBuffer16(std::vector &data) { - for (size_t i = 0; i < data.size(); i += 2) { - const u16 original = *reinterpret_cast(&data[i]); - *reinterpret_cast(&data[i]) = bswap(original); +template +static FORCE_INLINE void SwapBuffer(std::array &data) { + for (size_t i = 0; i < data.size(); i += sizeof(T)) { + const T original = *reinterpret_cast(&data[i]); + *reinterpret_cast(&data[i]) = bswap(original); } } -template -FORCE_INLINE void SwapBuffer32(std::array &data) { - for (size_t i = 0; i < Size; i += 4) { - const u32 original = *static_cast(&data[i]); - *static_cast(&data[i]) = bswap(original); - } -} - -template -FORCE_INLINE void SwapBuffer16(std::array &data) { - for (size_t i = 0; i < Size; i += 2) { - const u16 original = *static_cast(&data[i]); - *static_cast(&data[i]) = bswap(original); - } -} - -FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, const size_t len) { +static FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, const size_t len) { static u32 table[256]; static int have_table = 0; diff --git a/src/utils/byteswap.hpp b/src/utils/byteswap.hpp index 7ad59f87..02a42ab8 100644 --- a/src/utils/byteswap.hpp +++ b/src/utils/byteswap.hpp @@ -1,21 +1,13 @@ #pragma once #include -template -T bswap(T); +static FORCE_INLINE u16 bswap(const u16 x) { return (x & 0xFF00u) >> 8 | (x & 0x00FFu) << 8; } -template <> -inline u16 bswap(const u16 x) { - return (x & 0xFF00u) >> 8 | (x & 0x00FFu) << 8; -} - -template <> -inline u32 bswap(const u32 x) { +static FORCE_INLINE u32 bswap(const u32 x) { return (x & 0xFF000000u) >> 24u | (x & 0x00FF0000u) >> 8u | (x & 0x0000FF00u) << 8u | (x & 0x000000FFu) << 24u; } -template <> -inline u64 bswap(const u64 x) { +static FORCE_INLINE u64 bswap(const u64 x) { return (x & 0xFF00000000000000u) >> 56u | (x & 0x00FF000000000000u) >> 40u | (x & 0x0000FF0000000000u) >> 24u | (x & 0x000000FF00000000u) >> 8u | (x & 0x00000000FF000000u) << 8u | (x & 0x0000000000FF0000u) << 24u | (x & 0x000000000000FF00u) << 40u | (x & 0x00000000000000FFu) << 56u;