From bb3d88172d97d9e59699f20a954bcd9e27f2b6e8 Mon Sep 17 00:00:00 2001 From: risingPhil Date: Fri, 9 Aug 2024 12:50:05 +0200 Subject: [PATCH] Fix Endianness issues with sprite decoding Sprite decoding was broken on Big Endian CPUs (such as AMD or the Nintendo 64 MIPS CPU), while working on Little Endian CPUs (Intel): the color palettes came out wrong because they were stored as little endian. Now we byte swap the 16-bit colors when reading them from the rom/cartridge if running on a big endian CPU, which solves the issue (while still remaining compatible with Little Endian CPUs) --- src/gen1/Gen1GameReader.cpp | 7 +------ src/gen2/Gen2GameReader.cpp | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/gen1/Gen1GameReader.cpp b/src/gen1/Gen1GameReader.cpp index 9cd405b..dcdab90 100644 --- a/src/gen1/Gen1GameReader.cpp +++ b/src/gen1/Gen1GameReader.cpp @@ -271,8 +271,6 @@ void Gen1GameReader::readColorPalette(uint8_t paletteId, uint16_t* outColorPalet { uint16_t* cur = outColorPalette; const uint16_t* end = cur + 4; - uint8_t byte1; - uint8_t byte2; // based on https://datacrystal.romhacking.net/wiki/Pok%C3%A9mon_Red_and_Blue/ROM_map // and https://bulbapedia.bulbagarden.net/wiki/List_of_color_palettes_by_index_number_(Generation_I) @@ -280,10 +278,7 @@ void Gen1GameReader::readColorPalette(uint8_t paletteId, uint16_t* outColorPalet romReader_.seek(romOffset + (paletteId * 8)); while(cur < end) { - romReader_.readByte(byte1); - romReader_.readByte(byte2); - // stored in little endian -> least significant bits of the 16 bit value are stored first - (*cur) = (((uint16_t)byte2 << 8) | byte1); + romReader_.readUint16((*cur), Endianness::LITTLE); ++cur; } diff --git a/src/gen2/Gen2GameReader.cpp b/src/gen2/Gen2GameReader.cpp index 805fbb7..22e6a3b 100644 --- a/src/gen2/Gen2GameReader.cpp +++ b/src/gen2/Gen2GameReader.cpp @@ -358,9 +358,9 @@ bool Gen2GameReader::readColorPaletteForPokemon(uint8_t index, bool shiny, uint1 (*curColor) = 0x7FFF; ++curColor; // only the second and third color are actually stored in gen 2 - romReader_.read((uint8_t *)curColor, 2); + romReader_.readUint16(*curColor, Endianness::LITTLE); ++curColor; - romReader_.read((uint8_t *)curColor, 2); + romReader_.readUint16(*curColor, Endianness::LITTLE); ++curColor; // last color is always black (*curColor) = 0;