diff --git a/src/emucore/elf/ElfUtil.cxx b/src/emucore/elf/ElfUtil.cxx index c786b0c00..daf0d4824 100644 --- a/src/emucore/elf/ElfUtil.cxx +++ b/src/emucore/elf/ElfUtil.cxx @@ -30,7 +30,7 @@ Int32 elfUtil::decode_B_BL(uInt32 opcode) const uInt32 imm11 = hw2 & 0x7ff; const uInt32 imm10 = hw1 & 0x3ff; - Int32 offset = imm11 | (imm10 << 11) | (i1 << 21) | (i2 << 22) | (s << 23); + Int32 offset = imm11 | (imm10 << 11) | (i2 << 21) | (i1 << 22) | (s << 23); offset <<= 8; offset >>= 7; @@ -45,8 +45,8 @@ uInt32 elfUtil::encode_B_BL(Int32 offset, bool link) offset >>= 1; uInt8 s = (offset >> 23) & 0x01; - uInt8 j2 = ((~offset >> 22) ^ s) & 0x01; - uInt8 j1 = ((~offset >> 21) ^ s) & 0x01; + uInt8 j2 = ((~offset >> 21) ^ s) & 0x01; + uInt8 j1 = ((~offset >> 22) ^ s) & 0x01; uInt32 imm11 = offset & 0x7ff; uInt32 imm10 = (offset >> 11) & 0x3ff; diff --git a/src/emucore/elf/EncodingTest.cxx b/src/emucore/elf/EncodingTest.cxx index a24cec16e..17579ca4c 100644 --- a/src/emucore/elf/EncodingTest.cxx +++ b/src/emucore/elf/EncodingTest.cxx @@ -17,4 +17,42 @@ #include -TEST(HelloWorld, Passes) {} +#include "bspf.hxx" +#include "ElfUtil.hxx" + +namespace { + + struct Encoding { + Int32 offset; + uInt32 opcodeBL; + uInt32 opcodeB; + }; + + class EncodingTest: public testing::TestWithParam {}; + + TEST_P(EncodingTest, OffsetIsEncodedCorrectlyToBL) { + EXPECT_EQ(elfUtil::encode_B_BL(GetParam().offset - 4, true), GetParam().opcodeBL); + } + + TEST_P(EncodingTest, OffsetIsEncodedCorrectlyToBW) { + EXPECT_EQ(elfUtil::encode_B_BL(GetParam().offset - 4, false), GetParam().opcodeB); + } + + TEST_P(EncodingTest, OffsetIsDecodedCorrectlyFromBL) { + EXPECT_EQ(elfUtil::decode_B_BL(GetParam().opcodeBL), GetParam().offset - 4); + } + + TEST_P(EncodingTest, OffsetIsDecodedCorrectlyFromB) { + EXPECT_EQ(elfUtil::decode_B_BL(GetParam().opcodeB), GetParam().offset - 4); + } + + INSTANTIATE_TEST_SUITE_P(EncodingSuite, EncodingTest, testing::Values( + Encoding{.offset = 10, .opcodeBL = 0xf803f000, .opcodeB = 0xb803f000}, + Encoding{.offset = 16777090, .opcodeBL = 0xd7bff3ff, .opcodeB = 0x97bff3ff}, + Encoding{.offset = 8388606, .opcodeBL = 0xf7fdf3ff, .opcodeB = 0xb7fdf3ff}, + Encoding{.offset = -10, .opcodeBL = 0xfff9f7ff, .opcodeB = 0xbff9f7ff}, + Encoding{.offset = -16777090, .opcodeBL = 0xd03df400, .opcodeB = 0x903df400}, + Encoding{.offset = -8388606, .opcodeBL = 0xdffff7ff, .opcodeB = 0x9ffff7ff} + )); + +}