mirror of
https://github.com/siemens-mobile-hacks/teakra.git
synced 2026-11-25 22:27:18 +00:00
288 lines
15 KiB
C++
288 lines
15 KiB
C++
#include <algorithm>
|
|
#include <array>
|
|
#include <catch.hpp>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
#include <teakra/disassembler.h>
|
|
#include "../src/parser.h"
|
|
|
|
TEST_CASE("Assembler and disassembler round trip every opcode", "[assembler][disassembler]") {
|
|
auto parser = Teakra::GenerateParser();
|
|
constexpr std::array<u16, 5> expansions{{0x0000, 0x0001, 0x1234, 0x8000, 0xFFFF}};
|
|
|
|
for (u32 value = 0; value <= 0xFFFF; ++value) {
|
|
const u16 opcode = (u16)value;
|
|
const bool expanded = Teakra::Disassembler::NeedExpansion(opcode);
|
|
const std::size_t expansion_count = expanded ? expansions.size() : 1;
|
|
for (std::size_t i = 0; i < expansion_count; ++i) {
|
|
const u16 expansion = expansions[i];
|
|
const auto tokens = Teakra::Disassembler::GetTokenList(opcode, expansion);
|
|
if (std::any_of(tokens.begin(), tokens.end(), [](const auto& token) {
|
|
return token.find("[ERROR]") != std::string::npos;
|
|
}))
|
|
continue;
|
|
|
|
INFO("opcode = 0x" << std::hex << opcode << ", expansion = 0x" << expansion);
|
|
const auto assembled = parser->Parse(tokens);
|
|
REQUIRE(assembled.status == (expanded ? Teakra::Parser::Opcode::ValidWithExpansion
|
|
: Teakra::Parser::Opcode::Valid));
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(assembled.opcode, assembled.expansion) ==
|
|
tokens);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("TeakLite encoding fixtures from Architecture Specification Table 4-4",
|
|
"[assembler][disassembler][datasheet]") {
|
|
struct Fixture {
|
|
u16 opcode;
|
|
u16 expansion;
|
|
std::vector<std::string> tokens;
|
|
};
|
|
const std::array fixtures{
|
|
Fixture{0x0000, 0x0000, {"nop"}},
|
|
Fixture{0x0020, 0x0000, {"trap"}},
|
|
Fixture{0x4380, 0x0000, {"eint"}},
|
|
Fixture{0x43C0, 0x0000, {"dint"}},
|
|
Fixture{0x86C0, 0x1234, {"add", "0x1234", "a0"}},
|
|
Fixture{0xD4DB, 0x1234, {"add", "[r7+0x1234]", "a0"}},
|
|
Fixture{0xD4FB, 0x1234, {"add", "[0x1234]", "a0"}},
|
|
Fixture{0x80E0, 0x1234, {"set", "0x1234", "[r0]"}},
|
|
Fixture{0x81E0, 0x1234, {"set", "0x1234", "r0"}},
|
|
Fixture{0xE100, 0x1234, {"set", "0x1234", "[page:0x0000u8]"}},
|
|
Fixture{0x8000, 0x1234, {"mpy", "[r0]", "0x1234", "a0"}},
|
|
Fixture{0x90C0, 0x1234, {"msu", "[r0]", "0x1234", "a0"}},
|
|
Fixture{0xD4B8, 0x1234, {"mov", "[0x1234]", "a0"}},
|
|
Fixture{0xD4BC, 0x1234, {"mov", "a0l", "[0x1234]"}},
|
|
Fixture{0x4180, 0x1234, {"br", "0x00001234", "true"}},
|
|
Fixture{0x41C0, 0x1234, {"call", "0x00001234", "true"}},
|
|
Fixture{0x5E00, 0x1234, {"mov", "0x1234", "r0"}},
|
|
Fixture{0x5E20, 0x1234, {"mov", "0x1234", "b0"}},
|
|
Fixture{0xD498, 0x1234, {"mov", "[r7+0x1234]", "a0"}},
|
|
Fixture{0xD49C, 0x1234, {"mov", "a0l", "[r7+0x1234]"}},
|
|
Fixture{0x5F40, 0x1234, {"push", "0x1234"}},
|
|
Fixture{0x5C00, 0x1234, {"bkrep", "0x0000u8", "0x00001234"}},
|
|
Fixture{0x5D00, 0x1234, {"bkrep", "r0", "0x00001234"}},
|
|
};
|
|
|
|
auto parser = Teakra::GenerateParser();
|
|
for (const auto& fixture : fixtures) {
|
|
INFO("opcode = 0x" << std::hex << fixture.opcode);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(fixture.opcode, fixture.expansion) ==
|
|
fixture.tokens);
|
|
const auto assembled = parser->Parse(fixture.tokens);
|
|
REQUIRE(assembled.status == (fixture.expansion ? Teakra::Parser::Opcode::ValidWithExpansion
|
|
: Teakra::Parser::Opcode::Valid));
|
|
REQUIRE(assembled.opcode == fixture.opcode);
|
|
REQUIRE(assembled.expansion == fixture.expansion);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("TeakLite unused opcode bits preserve the decoded instruction",
|
|
"[disassembler][datasheet][hardware]") {
|
|
// These Table 4-4 patterns are also covered by the PMB887x hardware alias corpus in
|
|
// pmb887x-emu/bsp/unit/dsp/opcode-aliases. A dot is an ignored opcode bit.
|
|
constexpr std::array patterns{
|
|
"10000110110.....", "1001010011.00000", "1000000001100...",
|
|
"1000010001100...", "10001.0001100...", "010010011100....",
|
|
"1001000011.00000", "01101111.1100000", "1001100001.00000",
|
|
"10010000011....0", "1001110.01.00000", "1001010.01000000",
|
|
"1001010.011....0", "1101001011010...", "1101101010.11...",
|
|
"1101101011.11...", "0101111010.00000", "1101010.11010.00",
|
|
"1101010.11010.01", "1101010.11010.10", "1101010.11010.11",
|
|
"1001100011.00000", "11010100101110..", "11010100101111..",
|
|
"0101111.00000000", "01011110001.....", "0100111111.00000",
|
|
"0100111110.00000", "11010100100110..", "11010100100111..",
|
|
"0101111101......", "0100100110..0000", "010010111...0000",
|
|
"00001101...00000", "01011101...00000", "1101001111......",
|
|
"0100000110..0001", "0100000111..0001", "110101001..0....",
|
|
"0100010110..0001", "0100010111.00001", "1101011111.0....",
|
|
"1101001110.0....", "00000000000.....", "000000001.000000",
|
|
"0100001110......", "0100001111......", "00000000001.....",
|
|
"010011011.....00",
|
|
};
|
|
|
|
for (const std::string_view pattern : patterns) {
|
|
u16 base = 0;
|
|
u16 ignored = 0;
|
|
for (const char bit : pattern) {
|
|
base <<= 1;
|
|
ignored <<= 1;
|
|
base |= bit == '1';
|
|
ignored |= bit == '.';
|
|
}
|
|
|
|
const auto expected = Teakra::Disassembler::GetTokenList(base, 0x1234);
|
|
for (u16 bits = ignored; bits != 0; bits = (bits - 1) & ignored) {
|
|
INFO("pattern = " << pattern << ", opcode = 0x" << std::hex << (base | bits));
|
|
REQUIRE(Teakra::Disassembler::NeedExpansion(base | bits) ==
|
|
Teakra::Disassembler::NeedExpansion(base));
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(base | bits, 0x1234) == expected);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Every canonical TeakLite Table 4-4 encoding row is decoded",
|
|
"[disassembler][datasheet]") {
|
|
struct EncodingRow {
|
|
u16 opcode;
|
|
bool expanded;
|
|
};
|
|
|
|
// One canonical first word for every addressing-form row on pages 4-146 through 4-153.
|
|
constexpr std::array rows{
|
|
EncodingRow{0xA000, false}, EncodingRow{0x8080, false}, EncodingRow{0x80A0, false},
|
|
EncodingRow{0xC000, false}, EncodingRow{0x80C0, true}, EncodingRow{0x4000, false},
|
|
EncodingRow{0xD4D8, true}, EncodingRow{0xD4F8, true}, EncodingRow{0x6700, false},
|
|
EncodingRow{0x94C0, false}, EncodingRow{0x0E00, false}, EncodingRow{0x80E0, true},
|
|
EncodingRow{0x81E0, true}, EncodingRow{0xE100, true}, EncodingRow{0x8060, false},
|
|
EncodingRow{0x8460, false}, EncodingRow{0x8860, false}, EncodingRow{0x49C0, false},
|
|
EncodingRow{0x8020, false}, EncodingRow{0x8040, false}, EncodingRow{0xD000, false},
|
|
EncodingRow{0x8000, true}, EncodingRow{0xE000, false}, EncodingRow{0x0800, false},
|
|
EncodingRow{0x90C0, true}, EncodingRow{0xD080, false}, EncodingRow{0x9020, false},
|
|
EncodingRow{0x9000, false}, EncodingRow{0xF000, false}, EncodingRow{0xD280, false},
|
|
EncodingRow{0x9240, false}, EncodingRow{0x6F00, false}, EncodingRow{0x9840, false},
|
|
EncodingRow{0x9040, false}, EncodingRow{0x9060, false}, EncodingRow{0x9C40, false},
|
|
EncodingRow{0x9440, false}, EncodingRow{0x9460, false}, EncodingRow{0x5800, false},
|
|
EncodingRow{0xD290, false}, EncodingRow{0xD298, false}, EncodingRow{0xD2D8, false},
|
|
EncodingRow{0x5EC0, false}, EncodingRow{0x5E80, false}, EncodingRow{0x1C00, false},
|
|
EncodingRow{0x47C0, false}, EncodingRow{0xD490, false}, EncodingRow{0xD491, false},
|
|
EncodingRow{0xD492, false}, EncodingRow{0xD493, false}, EncodingRow{0x1800, false},
|
|
EncodingRow{0x98C0, false}, EncodingRow{0x47E0, false}, EncodingRow{0x2000, false},
|
|
EncodingRow{0x3000, false}, EncodingRow{0x6000, false}, EncodingRow{0x6100, false},
|
|
EncodingRow{0x6200, false}, EncodingRow{0x6500, false}, EncodingRow{0x6D00, false},
|
|
EncodingRow{0x7D00, false}, EncodingRow{0xD4B8, true}, EncodingRow{0xD4BC, true},
|
|
EncodingRow{0x5E00, true}, EncodingRow{0x5E20, true}, EncodingRow{0x2100, false},
|
|
EncodingRow{0x2500, false}, EncodingRow{0x2300, false}, EncodingRow{0x2900, false},
|
|
EncodingRow{0x0500, false}, EncodingRow{0x4FC0, false}, EncodingRow{0x4F80, false},
|
|
EncodingRow{0xD880, false}, EncodingRow{0xDC80, false}, EncodingRow{0xD498, true},
|
|
EncodingRow{0xD49C, true}, EncodingRow{0x0040, false}, EncodingRow{0x0600, false},
|
|
EncodingRow{0x0100, false}, EncodingRow{0x0180, false}, EncodingRow{0x6300, false},
|
|
EncodingRow{0x4080, false}, EncodingRow{0x9CC0, false}, EncodingRow{0x9CE0, false},
|
|
EncodingRow{0x8864, false}, EncodingRow{0x5E40, false}, EncodingRow{0x5F40, true},
|
|
EncodingRow{0x5E60, false}, EncodingRow{0x4980, false}, EncodingRow{0x4B80, false},
|
|
EncodingRow{0x5F80, false}, EncodingRow{0x0D00, false}, EncodingRow{0x0C00, false},
|
|
EncodingRow{0x5C00, true}, EncodingRow{0x5D00, true}, EncodingRow{0xD3C0, false},
|
|
EncodingRow{0x4180, true}, EncodingRow{0x5000, false}, EncodingRow{0x41C0, true},
|
|
EncodingRow{0x1000, false}, EncodingRow{0xD480, false}, EncodingRow{0x4580, false},
|
|
EncodingRow{0xD780, false}, EncodingRow{0x45C0, false}, EncodingRow{0xD7C0, false},
|
|
EncodingRow{0xD380, false}, EncodingRow{0xD390, false}, EncodingRow{0x0900, false},
|
|
EncodingRow{0x0000, false}, EncodingRow{0x0080, false}, EncodingRow{0x4380, false},
|
|
EncodingRow{0x43C0, false}, EncodingRow{0x0020, false}, EncodingRow{0x0400, false},
|
|
EncodingRow{0x0200, false}, EncodingRow{0x0A00, false}, EncodingRow{0xDB80, false},
|
|
EncodingRow{0xDF80, false}, EncodingRow{0x4D80, false},
|
|
};
|
|
|
|
for (const auto& row : rows) {
|
|
INFO("opcode = 0x" << std::hex << row.opcode);
|
|
const auto tokens = Teakra::Disassembler::GetTokenList(row.opcode, 0x1234);
|
|
REQUIRE(std::none_of(tokens.begin(), tokens.end(), [](const auto& token) {
|
|
return token.find("[ERROR]") != std::string::npos;
|
|
}));
|
|
REQUIRE(Teakra::Disassembler::NeedExpansion(row.opcode) == row.expanded);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("TeakLite Table 4-4 operation subgroups select the documented mnemonic",
|
|
"[disassembler][datasheet]") {
|
|
constexpr std::array alm_names{"or", "and", "xor", "add", "tst0", "tst1",
|
|
"cmp", "sub", "msu", "addh", "addl", "subh",
|
|
"subl", "sqr", "sqra", "cmpu"};
|
|
for (u16 operation = 0; operation < alm_names.size(); ++operation) {
|
|
INFO("ALM operation = " << operation);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(0xA000 | (operation << 9)).front() ==
|
|
alm_names[operation]);
|
|
}
|
|
|
|
constexpr std::array alu_operations{0u, 1u, 2u, 3u, 6u, 7u};
|
|
constexpr std::array alu_names{"or", "and", "xor", "add", "cmp", "sub"};
|
|
for (std::size_t i = 0; i < alu_operations.size(); ++i) {
|
|
INFO("ALU operation = " << alu_operations[i]);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(
|
|
(u16)(0xC000 | (alu_operations[i] << 9))).front() == alu_names[i]);
|
|
}
|
|
|
|
constexpr std::array alb_names{"set", "rst", "chng", "addv", "tst0", "tst1", "cmpv",
|
|
"subv"};
|
|
for (u16 operation = 0; operation < alb_names.size(); ++operation) {
|
|
INFO("ALB operation = " << operation);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(0xE100 | (operation << 9), 0x1234).front() ==
|
|
alb_names[operation]);
|
|
}
|
|
|
|
constexpr std::array mul_names{"mpy", "mpysu", "mac", "macus",
|
|
"maa", "macuu", "macsu", "maasu"};
|
|
for (u16 operation = 0; operation < mul_names.size(); ++operation) {
|
|
INFO("MUL operation = " << operation);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(0x8040 | (operation << 8)).front() ==
|
|
mul_names[operation]);
|
|
}
|
|
|
|
constexpr std::array moda_operations{0u, 1u, 2u, 3u, 4u, 5u, 6u, 8u,
|
|
9u, 10u, 11u, 12u, 13u, 14u, 15u};
|
|
constexpr std::array moda_names{"shr", "shr4", "shl", "shl4", "ror", "rol", "clr", "not",
|
|
"neg", "rnd", "pacr", "clrr", "inc", "dec", "copy"};
|
|
for (std::size_t i = 0; i < moda_operations.size(); ++i) {
|
|
INFO("MODA operation = " << moda_operations[i]);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(
|
|
(u16)(0x6700 | (moda_operations[i] << 4))).front() == moda_names[i]);
|
|
}
|
|
|
|
constexpr std::array condition_names{"true", "eq", "neq", "gt", "ge", "lt",
|
|
"le", "nn", "c", "v", "e", "l",
|
|
"nr", "niu0", "iu0", "iu1"};
|
|
for (u16 condition = 0; condition < condition_names.size(); ++condition) {
|
|
INFO("condition = " << condition);
|
|
REQUIRE(Teakra::Disassembler::GetTokenList(0x4580 | condition).back() ==
|
|
condition_names[condition]);
|
|
}
|
|
}
|
|
|
|
TEST_CASE("TeakLite mov register, register reads a0 and a1 for b, ext, accumulator half, lc and sv",
|
|
"[assembler][disassembler][datasheet]") {
|
|
auto parser = Teakra::GenerateParser();
|
|
for (u16 source = 24; source <= 25; ++source) {
|
|
for (u16 destination = 16; destination < 32; ++destination) {
|
|
if (destination == 24 || destination == 25)
|
|
continue;
|
|
const u16 opcode = 0x5800 | (destination << 5) | source;
|
|
INFO("opcode = 0x" << std::hex << opcode);
|
|
const auto tokens = Teakra::Disassembler::GetTokenList(opcode);
|
|
REQUIRE(tokens.size() == 3);
|
|
REQUIRE(tokens[0] == "mov");
|
|
REQUIRE(tokens[1] == (source == 24 ? "a0" : "a1"));
|
|
const auto assembled = parser->Parse(tokens);
|
|
REQUIRE(assembled.status == Teakra::Parser::Opcode::Valid);
|
|
REQUIRE(assembled.opcode == opcode);
|
|
}
|
|
}
|
|
}
|
|
|
|
TEST_CASE("Assembler accepts historical condition spellings", "[assembler]") {
|
|
auto parser = Teakra::GenerateParser();
|
|
const auto mn = parser->Parse({"ret", "mn"});
|
|
REQUIRE(mn.status == Teakra::Parser::Opcode::Valid);
|
|
REQUIRE(mn.opcode == 0x4587);
|
|
|
|
const auto always = parser->Parse({"ret", "always"});
|
|
REQUIRE(always.status == Teakra::Parser::Opcode::Valid);
|
|
REQUIRE(always.opcode == 0x4580);
|
|
}
|
|
|
|
TEST_CASE("TeakLite Table 4-3 register field is decoded", "[disassembler][datasheet]") {
|
|
// Teakra uses the Teak names r7/y0 for the TeakLite aliases rb/y and p* for the
|
|
// direction-dependent p/ph code.
|
|
constexpr std::array register_names{
|
|
"r0", "r1", "r2", "r3", "r4", "r5", "r7", "y0",
|
|
"st0", "st1", "st2", "p*", "pc", "sp", "cfgi", "cfgj",
|
|
"b0h", "b1h", "b0l", "b1l", "ext0", "ext1", "ext2", "ext3",
|
|
"a0", "a1", "a0l", "a1l", "a0h", "a1h", "lc", "sv",
|
|
};
|
|
|
|
for (u16 code = 0; code < register_names.size(); ++code) {
|
|
INFO("register code = " << code);
|
|
const auto tokens = Teakra::Disassembler::GetTokenList(0x5E00 | code, 0x1234);
|
|
REQUIRE(tokens.back() == register_names[code]);
|
|
}
|
|
}
|