use a little more c++20/23 features

This commit is contained in:
irisz64
2025-07-22 16:07:00 +02:00
parent a5bb2c8b0a
commit f7ec6df924
5 changed files with 97 additions and 85 deletions

View File

@@ -118,7 +118,7 @@ int JIT::Step() {
if((instrEndsBlock = InstrEndsBlock(instruction)))
continue;
/*u32 bswapped = bswap(instruction);
/*u32 bswapped = std::byteswap(instruction);
auto count = cs_disasm(disassemblerMips, reinterpret_cast<const u8 *>(&bswapped), 4, blockPC, 0, &insn);
if (count > 0) {

View File

@@ -150,7 +150,7 @@ void Mem::LoadROM(const bool isArchive, const std::string &filename) {
buf = OpenROM(filename, sizeAdjusted);
}
endianness = bswap(*reinterpret_cast<u32 *>(buf.data()));
endianness = std::byteswap(Util::ReadAccess<u32>(buf, 0));
Util::SwapN64Rom<true>(buf, endianness);
std::ranges::copy(buf, rom.cart.begin());
@@ -159,15 +159,15 @@ void Mem::LoadROM(const bool isArchive, const std::string &filename) {
}
memcpy(rom.gameNameCart, rom.header.imageName, sizeof(rom.header.imageName));
rom.header.clockRate = bswap(rom.header.clockRate);
rom.header.programCounter = bswap(rom.header.programCounter);
rom.header.release = bswap(rom.header.release);
rom.header.crc1 = bswap(rom.header.crc1);
rom.header.crc2 = bswap(rom.header.crc2);
rom.header.unknown = bswap(rom.header.unknown);
rom.header.unknown2 = bswap(rom.header.unknown2);
rom.header.manufacturerId = bswap(rom.header.manufacturerId);
rom.header.cartridgeId = bswap(rom.header.cartridgeId);
rom.header.clockRate = std::byteswap(rom.header.clockRate);
rom.header.programCounter = std::byteswap(rom.header.programCounter);
rom.header.release = std::byteswap(rom.header.release);
rom.header.crc1 = std::byteswap(rom.header.crc1);
rom.header.crc2 = std::byteswap(rom.header.crc2);
rom.header.unknown = std::byteswap(rom.header.unknown);
rom.header.unknown2 = std::byteswap(rom.header.unknown2);
rom.header.manufacturerId = std::byteswap(rom.header.manufacturerId);
rom.header.cartridgeId = std::byteswap(rom.header.cartridgeId);
rom.code[0] = rom.header.manufacturerId & 0xFF;
rom.code[1] = (rom.header.cartridgeId >> 8) & 0xFF;
@@ -180,7 +180,7 @@ void Mem::LoadROM(const bool isArchive, const std::string &filename) {
const u32 checksum = Util::crc32(0, &rom.cart[0x40], 0x9c0);
SetROMCIC(checksum, rom);
endianness = bswap(*reinterpret_cast<u32 *>(rom.cart.data()));
endianness = std::byteswap(Util::ReadAccess<u32>(rom.cart, 0));
Util::SwapN64Rom(rom.cart, endianness);
rom.pal = IsROMPAL();
}
@@ -244,7 +244,7 @@ u16 Mem::Read(Registers &regs, const u32 paddr) {
case PIF_ROM_REGION:
return Util::ReadAccess<u16>(si.pif.bootrom, HALF_ADDRESS(paddr) - PIF_ROM_REGION_START);
case PIF_RAM_REGION:
return bswap(Util::ReadAccess<u16>(si.pif.ram, paddr - PIF_RAM_REGION_START));
return std::byteswap(Util::ReadAccess<u16>(si.pif.ram, paddr - PIF_RAM_REGION_START));
case 0x00800000 ... 0x03EFFFFF:
case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x04FFFFFF:
@@ -275,7 +275,7 @@ u32 Mem::Read(Registers &regs, const u32 paddr) {
case PIF_ROM_REGION:
return Util::ReadAccess<u32>(si.pif.bootrom, paddr - PIF_ROM_REGION_START);
case PIF_RAM_REGION:
return bswap(Util::ReadAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START));
return std::byteswap(Util::ReadAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START));
case 0x00800000 ... 0x03FFFFFF:
case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x04FFFFFF:
@@ -306,7 +306,7 @@ u64 Mem::Read(Registers &regs, const u32 paddr) {
case PIF_ROM_REGION:
return Util::ReadAccess<u64>(si.pif.bootrom, paddr - PIF_ROM_REGION_START);
case PIF_RAM_REGION:
return bswap(Util::ReadAccess<u64>(si.pif.ram, paddr - PIF_RAM_REGION_START));
return std::byteswap(Util::ReadAccess<u64>(si.pif.ram, paddr - PIF_RAM_REGION_START));
case 0x00800000 ... 0x03EFFFFF:
case 0x04200000 ... 0x042FFFFF:
case 0x04900000 ... 0x04FFFFFF:
@@ -343,7 +343,7 @@ void Mem::WriteInterpreter<u8>(Registers &regs, u32 paddr, u32 val) {
case PIF_RAM_REGION:
val = val << (8 * (3 - (paddr & 3)));
paddr = (paddr - PIF_RAM_REGION_START) & ~3;
Util::WriteAccess<u32>(si.pif.ram, paddr, bswap(val));
Util::WriteAccess<u32>(si.pif.ram, paddr, std::byteswap(val));
si.pif.ProcessCommands(*this);
break;
case 0x00800000 ... 0x03EFFFFF:
@@ -398,7 +398,7 @@ void Mem::WriteInterpreter<u16>(Registers &regs, u32 paddr, u32 val) {
case PIF_RAM_REGION:
val = val << (16 * !(paddr & 2));
paddr &= ~3;
Util::WriteAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START, bswap(val));
Util::WriteAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START, std::byteswap(val));
si.pif.ProcessCommands(*this);
break;
case 0x00800000 ... 0x03EFFFFF:
@@ -450,7 +450,7 @@ void Mem::WriteInterpreter<u32>(Registers &regs, const u32 paddr, const u32 val)
mmio.Write(paddr, val);
break;
case PIF_RAM_REGION:
Util::WriteAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START, bswap(val));
Util::WriteAccess<u32>(si.pif.ram, paddr - PIF_RAM_REGION_START, std::byteswap(val));
si.pif.ProcessCommands(*this);
break;
case 0x00800000 ... 0x03EFFFFF:
@@ -511,7 +511,7 @@ void Mem::WriteInterpreter(const Registers &regs, const u32 paddr, u64 val) {
case MMIO_REGION:
panic("MMIO Write!");
case PIF_RAM_REGION:
Util::WriteAccess<u64>(si.pif.ram, paddr - PIF_RAM_REGION_START, bswap(val));
Util::WriteAccess<u64>(si.pif.ram, paddr - PIF_RAM_REGION_START, std::byteswap(val));
si.pif.ProcessCommands(*this);
break;
case 0x00800000 ... 0x03EFFFFF:

View File

@@ -253,7 +253,7 @@ auto PI::BusRead<u32, false>(u32 addr) -> u32 {
if (index > mem.rom.cart.size() - 3) { // -3 because we're reading an entire word
switch (addr) {
case REGION_CART_ISVIEWER_BUFFER:
return bswap(Util::ReadAccess<u32>(mem.isviewer, addr - SREGION_CART_ISVIEWER_BUFFER));
return std::byteswap<u32>(Util::ReadAccess<u32>(mem.isviewer, addr - SREGION_CART_ISVIEWER_BUFFER));
case CART_ISVIEWER_FLUSH:
panic("Read from ISViewer flush!");
default:
@@ -306,7 +306,7 @@ void PI::BusWrite<u32, false>(u32 addr, u32 val) {
case REGION_PI_ROM:
switch (addr) {
case REGION_CART_ISVIEWER_BUFFER:
Util::WriteAccess<u32>(mem.isviewer, addr - SREGION_CART_ISVIEWER_BUFFER, bswap(val));
Util::WriteAccess<u32>(mem.isviewer, addr - SREGION_CART_ISVIEWER_BUFFER, std::byteswap(val));
break;
case CART_ISVIEWER_FLUSH:
{

View File

@@ -2,101 +2,121 @@
#include <common.hpp>
#include <cstring>
#include <functional>
#include <byteswap.hpp>
#include <bit>
#include <algorithm>
#include <vector>
#include <concepts>
namespace Util {
template <typename T>
static FORCE_INLINE std::vector<u8> IntegralToBuffer(const T &val) {
static FORCE_INLINE std::vector<u8> IntegralToBuffer(const std::integral auto &val) {
std::vector<u8> ret{};
ret.resize(sizeof(T));
ret.resize(sizeof(val));
memcpy(ret.data(), &val, sizeof(T));
memcpy(ret.data(), &val, sizeof(val));
return ret;
}
template <typename T>
static constexpr FORCE_INLINE T ReadAccess(const u8 *data, const u32 index);
template <typename T>
static constexpr FORCE_INLINE T ReadAccess(const std::vector<u8> &data, const u32 index);
template <typename T, size_t Size>
static constexpr FORCE_INLINE T ReadAccess(const std::array<u8, Size> &data, const u32 index);
template <typename T>
static constexpr FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const T val);
template <typename T>
static constexpr FORCE_INLINE void WriteAccess(std::vector<u8> &data, const u32 index, const T val);
template <typename T, size_t Size>
static constexpr FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, const u32 index, const T val);
template <>
constexpr FORCE_INLINE u64 ReadAccess(const u8 *data, const u32 index) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const auto& result = static_cast<u64>(hi) << 32 | static_cast<u64>(lo);
return result;
}
template <typename T>
static constexpr FORCE_INLINE T ReadAccess(const u8 *data, const u32 index) {
if constexpr (sizeof(T) == 8) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const T result = static_cast<T>(hi) << 32 | static_cast<T>(lo);
return result;
} else {
return *reinterpret_cast<const T *>(&data[index]);
}
return *reinterpret_cast<const T *>(&data[index]);
}
template <>
constexpr FORCE_INLINE u64 ReadAccess(const std::vector<u8> &data, const u32 index) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const u64& result = (static_cast<u64>(hi) << 32) | static_cast<u64>(lo);
return result;
}
template <typename T>
static constexpr FORCE_INLINE T ReadAccess(const std::vector<u8> &data, const u32 index) {
if constexpr (sizeof(T) == 8) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const T result = (static_cast<T>(hi) << 32) | static_cast<T>(lo);
return result;
} else {
return *reinterpret_cast<const T *>(&data[index]);
}
return *reinterpret_cast<const T *>(&data[index]);
}
template <size_t Size>
constexpr FORCE_INLINE u64 ReadAccess(const std::array<u8, Size> &data, const u32 index) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const u64& result = static_cast<u64>(hi) << 32 | static_cast<u64>(lo);
return result;
}
template <typename T, size_t Size>
static constexpr FORCE_INLINE T ReadAccess(const std::array<u8, Size> &data, const u32 index) {
if constexpr (sizeof(T) == 8) {
u32 hi = *reinterpret_cast<const u32 *>(&data[index + 0]);
u32 lo = *reinterpret_cast<const u32 *>(&data[index + 4]);
const T result = static_cast<T>(hi) << 32 | static_cast<T>(lo);
return result;
} else {
return *reinterpret_cast<const T *>(&data[index]);
}
return *reinterpret_cast<const T *>(&data[index]);
}
template <size_t Size>
constexpr FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, const u32 index, const u64& val) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
}
template <typename T, size_t Size>
static constexpr FORCE_INLINE void WriteAccess(std::array<u8, Size> &data, const u32 index, const T val) {
if constexpr (sizeof(T) == 8) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<T *>(&data[index]) = val;
}
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
} else {
*reinterpret_cast<T *>(&data[index]) = val;
}
template <>
constexpr FORCE_INLINE void WriteAccess(std::vector<u8> &data, const u32 index, const u64 val) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
}
template <typename T>
static constexpr FORCE_INLINE void WriteAccess(std::vector<u8> &data, const u32 index, const T val) {
if constexpr (sizeof(T) == 8) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<T *>(&data[index]) = val;
}
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
} else {
*reinterpret_cast<T *>(&data[index]) = val;
}
template <>
constexpr FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const u64 val) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
}
template <typename T>
static constexpr FORCE_INLINE void WriteAccess(u8 *data, const u32 index, const T val) {
if constexpr (sizeof(T) == 8) {
const u32 hi = val >> 32;
const u32 lo = val;
*reinterpret_cast<u32 *>(&data[index + 0]) = hi;
*reinterpret_cast<u32 *>(&data[index + 4]) = lo;
} else {
*reinterpret_cast<T *>(&data[index]) = val;
}
*reinterpret_cast<T *>(&data[index]) = val;
}
template <typename T>
static constexpr FORCE_INLINE void SwapBuffer(std::vector<u8> &data) {
for (size_t i = 0; i < data.size(); i += sizeof(T)) {
const T original = *reinterpret_cast<T *>(&data[i]);
*reinterpret_cast<T *>(&data[i]) = bswap(original);
*reinterpret_cast<T *>(&data[i]) = std::byteswap(original);
}
}
@@ -104,7 +124,7 @@ template <typename T, size_t Size>
static constexpr FORCE_INLINE void SwapBuffer(std::array<u8, Size> &data) {
for (size_t i = 0; i < data.size(); i += sizeof(T)) {
const T original = *reinterpret_cast<T *>(&data[i]);
*reinterpret_cast<T *>(&data[i]) = bswap(original);
*reinterpret_cast<T *>(&data[i]) = std::byteswap(original);
}
}

View File

@@ -1,8 +0,0 @@
#pragma once
#include <common.hpp>
#include <bit>
template <class T>
static constexpr T bswap(const T x) {
return std::byteswap<T>(x);
}