diff --git a/src/backend/RomHelpers.hpp b/src/backend/RomHelpers.hpp new file mode 100644 index 00000000..482ff476 --- /dev/null +++ b/src/backend/RomHelpers.hpp @@ -0,0 +1,71 @@ +#pragma once +#include +#include + +namespace util { +enum RomTypes { + Z64 = 0x80371240, + N64 = 0x40123780, + V64 = 0x37804012 +}; + +inline void GetRomCRC(size_t size, u8 *rom, u32 &crc) { + RomTypes endianness; + memcpy(&endianness, rom, 4); + endianness = static_cast(be32toh(endianness)); + + switch (endianness) { + case RomTypes::V64: { + SwapBuffer16(size, rom); + crc = crc32(0, rom, size); + } + break; + case RomTypes::N64: { + SwapBuffer32(size, rom); + crc = crc32(0, rom, size); + } + break; + case RomTypes::Z64: + crc = crc32(0, rom, size); + break; + default: + panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!\n"); + } +} + +inline void SwapN64Rom(size_t size, u8 *rom, u32 &crc, u32 &cicChecksum) { + RomTypes endianness; + memcpy(&endianness, rom, 4); + endianness = static_cast(be32toh(endianness)); + + switch (endianness) { + case RomTypes::V64: { + u8 *temp = (u8 *) calloc(size, 1); + memcpy(temp, rom, size); + SwapBuffer16(size, temp); + crc = crc32(0, temp, size); + cicChecksum = crc32(0, &temp[0x40], 0x9c0); + free(temp); + SwapBuffer32(size, rom); + SwapBuffer16(size, rom); + } + break; + case RomTypes::N64: { + u8 *temp = (u8 *) calloc(size, 1); + memcpy(temp, rom, size); + SwapBuffer32(size, temp); + crc = crc32(0, temp, size); + cicChecksum = crc32(0, &temp[0x40], 0x9c0); + free(temp); + } + break; + case RomTypes::Z64: + crc = crc32(0, rom, size); + cicChecksum = crc32(0, &rom[0x40], 0x9c0); + SwapBuffer32(size, rom); + break; + default: + panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!\n"); + } +} +} \ No newline at end of file diff --git a/src/backend/core/Mem.hpp b/src/backend/core/Mem.hpp index 7db1c4fa..9975b7de 100644 --- a/src/backend/core/Mem.hpp +++ b/src/backend/core/Mem.hpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include namespace n64 { diff --git a/src/utils/MemoryHelpers.hpp b/src/utils/MemoryHelpers.hpp index af8a082c..8b7f7519 100644 --- a/src/utils/MemoryHelpers.hpp +++ b/src/utils/MemoryHelpers.hpp @@ -85,70 +85,4 @@ inline u32 crc32(u32 crc, const u8 *buf, size_t len) { } return ~crc; } - -enum RomTypes { - Z64 = 0x80371240, - N64 = 0x40123780, - V64 = 0x37804012 -}; - -inline void SwapN64RomJustCRC(size_t size, u8 *rom, u32 &crc) { - RomTypes endianness; - memcpy(&endianness, rom, 4); - endianness = static_cast(be32toh(endianness)); - - switch (endianness) { - case RomTypes::V64: { - SwapBuffer16(size, rom); - crc = crc32(0, rom, size); - } - break; - case RomTypes::N64: { - SwapBuffer32(size, rom); - crc = crc32(0, rom, size); - } - break; - case RomTypes::Z64: - crc = crc32(0, rom, size); - break; - default: - panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!\n"); - } -} - -inline void SwapN64Rom(size_t size, u8 *rom, u32 &crc, u32 &cicChecksum) { - RomTypes endianness; - memcpy(&endianness, rom, 4); - endianness = static_cast(be32toh(endianness)); - - switch (endianness) { - case RomTypes::V64: { - u8 *temp = (u8 *) calloc(size, 1); - memcpy(temp, rom, size); - SwapBuffer16(size, temp); - crc = crc32(0, temp, size); - cicChecksum = crc32(0, &temp[0x40], 0x9c0); - free(temp); - SwapBuffer32(size, rom); - SwapBuffer16(size, rom); - } - break; - case RomTypes::N64: { - u8 *temp = (u8 *) calloc(size, 1); - memcpy(temp, rom, size); - SwapBuffer32(size, temp); - crc = crc32(0, temp, size); - cicChecksum = crc32(0, &temp[0x40], 0x9c0); - free(temp); - } - break; - case RomTypes::Z64: - crc = crc32(0, rom, size); - cicChecksum = crc32(0, &rom[0x40], 0x9c0); - SwapBuffer32(size, rom); - break; - default: - panic("Unrecognized rom format! Make sure this is a valid Nintendo 64 ROM dump!\n"); - } -} } \ No newline at end of file