Leverage new bswap functions to the fullest

This commit is contained in:
SimoneN64
2024-09-25 22:18:57 +02:00
parent 2744de8d3e
commit 521cd7c2e0
4 changed files with 24 additions and 46 deletions

View File

@@ -10,10 +10,10 @@ namespace Util {
template <bool toBE = false> template <bool toBE = false>
FORCE_INLINE void SwapN64Rom(std::vector<u8> &rom, u32 endianness) { FORCE_INLINE void SwapN64Rom(std::vector<u8> &rom, u32 endianness) {
u8 altByteShift = 0; u8 altByteShift = 0;
if ((endianness >> 24) != 0x80) { if (endianness >> 24 != 0x80) {
if ((endianness & 0xFF) != 0x80) { if ((endianness & 0xFF) != 0x80) {
if (((endianness >> 16) & 0xff) != 0x80) { if ((endianness >> 16 & 0xff) != 0x80) {
Util::panic("TODO: Unrecognized rom endianness. Ideally, this should be more robust"); panic("TODO: Unrecognized rom endianness. Ideally, this should be more robust");
} else { } else {
altByteShift = 12; altByteShift = 12;
} }
@@ -28,17 +28,17 @@ FORCE_INLINE void SwapN64Rom(std::vector<u8> &rom, u32 endianness) {
switch (endianness) { switch (endianness) {
case V64: case V64:
SwapBuffer16(rom); SwapBuffer<u16>(rom);
if constexpr (!toBE) if constexpr (!toBE)
SwapBuffer32(rom); SwapBuffer<u32>(rom);
break; break;
case N64: case N64:
if constexpr (toBE) if constexpr (toBE)
SwapBuffer32(rom); SwapBuffer<u32>(rom);
break; break;
case Z64: case Z64:
if constexpr (!toBE) if constexpr (!toBE)
SwapBuffer32(rom); SwapBuffer<u32>(rom);
break; break;
default: default:
panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!"); panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!");

View File

@@ -109,21 +109,21 @@ struct Mem {
std::vector<u8> temp{}; std::vector<u8> temp{};
temp.resize(RDRAM_SIZE); temp.resize(RDRAM_SIZE);
std::copy(mmio.rdp.rdram.begin(), mmio.rdp.rdram.end(), temp.begin()); std::copy(mmio.rdp.rdram.begin(), mmio.rdp.rdram.end(), temp.begin());
Util::SwapBuffer32(temp); Util::SwapBuffer<u32>(temp);
Util::WriteFileBinary(temp, "rdram.bin"); Util::WriteFileBinary(temp, "rdram.bin");
} }
FORCE_INLINE void DumpIMEM() const { FORCE_INLINE void DumpIMEM() const {
std::array<u8, IMEM_SIZE> temp{}; std::array<u8, IMEM_SIZE> temp{};
std::copy(mmio.rsp.imem.begin(), mmio.rsp.imem.end(), temp.begin()); std::copy(mmio.rsp.imem.begin(), mmio.rsp.imem.end(), temp.begin());
Util::SwapBuffer32(temp); Util::SwapBuffer<u32>(temp);
Util::WriteFileBinary(temp, "imem.bin"); Util::WriteFileBinary(temp, "imem.bin");
} }
FORCE_INLINE void DumpDMEM() const { FORCE_INLINE void DumpDMEM() const {
std::array<u8, DMEM_SIZE> temp{}; std::array<u8, DMEM_SIZE> temp{};
std::copy(mmio.rsp.dmem.begin(), mmio.rsp.dmem.end(), temp.begin()); std::copy(mmio.rsp.dmem.begin(), mmio.rsp.dmem.end(), temp.begin());
Util::SwapBuffer32(temp); Util::SwapBuffer<u32>(temp);
Util::WriteFileBinary(temp, "dmem.bin"); Util::WriteFileBinary(temp, "dmem.bin");
} }
ROM rom; ROM rom;

View File

@@ -91,37 +91,23 @@ static FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const T val) {
} }
} }
FORCE_INLINE void SwapBuffer32(std::vector<u8> &data) { template <typename T>
for (size_t i = 0; i < data.size(); i += 4) { static FORCE_INLINE void SwapBuffer(std::vector<u8> &data) {
const u32 original = *reinterpret_cast<u32 *>(&data[i]); for (size_t i = 0; i < data.size(); i += sizeof(T)) {
*reinterpret_cast<u32 *>(&data[i]) = bswap(original); const T original = *reinterpret_cast<T *>(&data[i]);
*reinterpret_cast<T *>(&data[i]) = bswap(original);
} }
} }
FORCE_INLINE void SwapBuffer16(std::vector<u8> &data) { template <typename T, size_t Size>
for (size_t i = 0; i < data.size(); i += 2) { static FORCE_INLINE void SwapBuffer(std::array<u8, Size> &data) {
const u16 original = *reinterpret_cast<u16 *>(&data[i]); for (size_t i = 0; i < data.size(); i += sizeof(T)) {
*reinterpret_cast<u16 *>(&data[i]) = bswap(original); const T original = *reinterpret_cast<T *>(&data[i]);
*reinterpret_cast<T *>(&data[i]) = bswap(original);
} }
} }
template <size_t Size> static FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, const size_t len) {
FORCE_INLINE void SwapBuffer32(std::array<u8, Size> &data) {
for (size_t i = 0; i < Size; i += 4) {
const u32 original = *static_cast<u32 *>(&data[i]);
*static_cast<u32 *>(&data[i]) = bswap(original);
}
}
template <size_t Size>
FORCE_INLINE void SwapBuffer16(std::array<u8, Size> &data) {
for (size_t i = 0; i < Size; i += 2) {
const u16 original = *static_cast<u16 *>(&data[i]);
*static_cast<u16 *>(&data[i]) = bswap(original);
}
}
FORCE_INLINE u32 crc32(u32 crc, const u8 *buf, const size_t len) {
static u32 table[256]; static u32 table[256];
static int have_table = 0; static int have_table = 0;

View File

@@ -1,21 +1,13 @@
#pragma once #pragma once
#include <common.hpp> #include <common.hpp>
template <typename T> static FORCE_INLINE u16 bswap(const u16 x) { return (x & 0xFF00u) >> 8 | (x & 0x00FFu) << 8; }
T bswap(T);
template <> static FORCE_INLINE u32 bswap(const u32 x) {
inline u16 bswap(const u16 x) {
return (x & 0xFF00u) >> 8 | (x & 0x00FFu) << 8;
}
template <>
inline u32 bswap(const u32 x) {
return (x & 0xFF000000u) >> 24u | (x & 0x00FF0000u) >> 8u | (x & 0x0000FF00u) << 8u | (x & 0x000000FFu) << 24u; return (x & 0xFF000000u) >> 24u | (x & 0x00FF0000u) >> 8u | (x & 0x0000FF00u) << 8u | (x & 0x000000FFu) << 24u;
} }
template <> static FORCE_INLINE u64 bswap(const u64 x) {
inline u64 bswap(const u64 x) {
return (x & 0xFF00000000000000u) >> 56u | (x & 0x00FF000000000000u) >> 40u | (x & 0x0000FF0000000000u) >> 24u | return (x & 0xFF00000000000000u) >> 56u | (x & 0x00FF000000000000u) >> 40u | (x & 0x0000FF0000000000u) >> 24u |
(x & 0x000000FF00000000u) >> 8u | (x & 0x00000000FF000000u) << 8u | (x & 0x0000000000FF0000u) << 24u | (x & 0x000000FF00000000u) >> 8u | (x & 0x00000000FF000000u) << 8u | (x & 0x0000000000FF0000u) << 24u |
(x & 0x000000000000FF00u) << 40u | (x & 0x00000000000000FFu) << 56u; (x & 0x000000000000FF00u) << 40u | (x & 0x00000000000000FFu) << 56u;