BL / B.W encoding tests 6 fixes.

This commit is contained in:
Christian Speckner 2024-07-11 22:40:00 +02:00
parent 4f4f4d5a0d
commit 69c916d2a8
2 changed files with 42 additions and 4 deletions

View File

@ -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;

View File

@ -17,4 +17,42 @@
#include <gtest/gtest.h>
TEST(HelloWorld, Passes) {}
#include "bspf.hxx"
#include "ElfUtil.hxx"
namespace {
struct Encoding {
Int32 offset;
uInt32 opcodeBL;
uInt32 opcodeB;
};
class EncodingTest: public testing::TestWithParam<Encoding> {};
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}
));
}