| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <fmt/format.h> |
| 5 | |
| 6 | #include "Core/DSP/DSPCodeUtil.h" |
| 7 | #include "Core/DSP/DSPDisassembler.h" |
| 8 | |
| 9 | #include "DSPTestBinary.h" |
| 10 | #include "DSPTestText.h" |
| 11 | #include "HermesBinary.h" |
| 12 | #include "HermesText.h" |
| 13 | |
| 14 | #include <gtest/gtest.h> |
| 15 | |
| 16 | static bool RoundTrippableDisassemble(std::span<const u16> code, std::string& text) |
| 17 | { |
| 18 | DSP::AssemblerSettings settings; |
| 19 | settings.ext_separator = '\''; |
| 20 | settings.decode_names = true; |
| 21 | settings.decode_registers = true; |
| 22 | // These two prevent roundtripping. |
| 23 | settings.show_hex = false; |
| 24 | settings.show_pc = false; |
| 25 | DSP::DSPDisassembler disasm(settings); |
| 26 | |
| 27 | return disasm.Disassemble(code, text); |
| 28 | } |
| 29 | |
| 30 | // This test goes from text ASM to binary to text ASM and once again back to binary. |
| 31 | // Then the two binaries are compared. |
| 32 | static bool RoundTrip(std::span<const u16> code1) |
| 33 | { |
| 34 | std::vector<u16> code2; |
| 35 | std::string text; |
| 36 | if (!RoundTrippableDisassemble(code1, text)) |
| 37 | { |
| 38 | fmt::print("RoundTrip: Disassembly failed.\n"); |
| 39 | return false; |
| 40 | } |
| 41 | if (!DSP::Assemble(text, code2)) |
| 42 | { |
| 43 | fmt::print("RoundTrip: Assembly failed.\n"); |
| 44 | return false; |
| 45 | } |
| 46 | if (!DSP::Compare(code1, code2)) |
| 47 | { |
| 48 | fmt::print("RoundTrip: Assembled code does not match input code\n"); |
| 49 | return false; |
| 50 | } |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | // This test goes from text ASM to binary to text ASM and once again back to binary. |
| 55 | // Very convenient for testing. Then the two binaries are compared. |
| 56 | static bool SuperTrip(const char* asm_code) |
| 57 | { |
| 58 | std::vector<u16> code1, code2; |
| 59 | std::string text; |
| 60 | if (!DSP::Assemble(asm_code, code1)) |
| 61 | { |
| 62 | fmt::print("SuperTrip: First assembly failed\n"); |
| 63 | return false; |
| 64 | } |
| 65 | fmt::print("First assembly: {} words\n", code1.size()); |
| 66 | |
| 67 | if (!RoundTrippableDisassemble(code1, text)) |
| 68 | { |
| 69 | fmt::print("SuperTrip: Disassembly failed\n"); |
| 70 | return false; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | fmt::print("Disassembly:\n"); |
| 75 | fmt::print("{}", text); |
| 76 | } |
| 77 | |
| 78 | if (!DSP::Assemble(text, code2)) |
| 79 | { |
| 80 | fmt::print("SuperTrip: Second assembly failed\n"); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (!DSP::Compare(code1, code2)) |
| 85 | { |
| 86 | fmt::print("SuperTrip: Assembled code does not match between passes\n"); |
| 87 | return false; |
| 88 | } |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | // Assembles asm_code, and verifies that it matches code1. |
| 93 | static bool AssembleAndCompare(const char* asm_code, std::span<const u16> code1) |
| 94 | { |
| 95 | std::vector<u16> code2; |
| 96 | if (!DSP::Assemble(asm_code, code2)) |
| 97 | { |
| 98 | fmt::print("AssembleAndCompare: Assembly failed\n"); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | fmt::print("AssembleAndCompare: Produced {} words; padding to {} words\n", code2.size(), |
| 103 | code1.size()); |
| 104 | while (code2.size() < code1.size()) |
| 105 | code2.push_back(0); |
| 106 | |
| 107 | if (!DSP::Compare(code1, code2)) |
| 108 | { |
| 109 | fmt::print("AssembleAndCompare: Assembled code does not match expected code\n"); |
| 110 | return false; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | // Let's start out easy - a trivial instruction.. |
| 116 | TEST(DSPAssembly, TrivialInstruction) |
| 117 | { |
| 118 | ASSERT_TRUE(SuperTrip(" NOP\n")); |
| 119 | } |
| 120 | |
| 121 | // Now let's do several. |
| 122 | TEST(DSPAssembly, SeveralTrivialInstructions) |
| 123 | { |
| 124 | ASSERT_TRUE(SuperTrip(" NOP\n" |
| 125 | " NOP\n" |
| 126 | " NOP\n")); |
| 127 | } |
| 128 | |
| 129 | // Turning it up a notch. |
| 130 | TEST(DSPAssembly, SeveralNoParameterInstructions) |
| 131 | { |
| 132 | ASSERT_TRUE(SuperTrip(" SET16\n" |
| 133 | " SET40\n" |
| 134 | " CLR15\n" |
| 135 | " M0\n" |
| 136 | " M2\n")); |
| 137 | } |
| 138 | |
| 139 | // Time to try labels and parameters, and comments. |
| 140 | TEST(DSPAssembly, LabelsParametersAndComments) |
| 141 | { |
| 142 | ASSERT_TRUE(SuperTrip("DIRQ_TEST: equ 0xfffb ; DSP Irq Request\n" |
| 143 | " si @0xfffc, #0x8888\n" |
| 144 | " si @0xfffd, #0xbeef\n" |
| 145 | " si @DIRQ_TEST, #0x0001\n")); |
| 146 | } |
| 147 | |
| 148 | // Let's see if registers roundtrip. Also try predefined labels. |
| 149 | TEST(DSPAssembly, RegistersAndPredefinedLabels) |
| 150 | { |
| 151 | ASSERT_TRUE(SuperTrip(" si @0xfffc, #0x8888\n" |
| 152 | " si @0xfffd, #0xbeef\n" |
| 153 | " si @DIRQ, #0x0001\n")); |
| 154 | } |
| 155 | |
| 156 | // Let's try some messy extended instructions. |
| 157 | TEST(DSPAssembly, ExtendedInstructions) |
| 158 | { |
| 159 | ASSERT_TRUE(SuperTrip(" MULMV'SN $AX0.L, $AX0.H, $ACC0 : @$AR2, $AC1.M\n" |
| 160 | " ADDAXL'MV $ACC1, $AX1.L : $AX1.H, $AC1.M\n")); |
| 161 | } |
| 162 | |
| 163 | TEST(DSPAssembly, HermesText) |
| 164 | { |
| 165 | ASSERT_TRUE(SuperTrip(s_hermes_text)); |
| 166 | } |
| 167 | |
| 168 | TEST(DSPAssembly, HermesBinary) |
| 169 | { |
| 170 | ASSERT_TRUE(RoundTrip(s_hermes_bin)); |
| 171 | } |
| 172 | |
| 173 | TEST(DSPAssembly, HermesAssemble) |
| 174 | { |
| 175 | ASSERT_TRUE(AssembleAndCompare(s_hermes_text, s_hermes_bin)); |
| 176 | } |
| 177 | |
| 178 | TEST(DSPAssembly, DSPTestText) |
| 179 | { |
| 180 | ASSERT_TRUE(SuperTrip(s_dsp_test_text)); |
| 181 | } |
| 182 | |
| 183 | TEST(DSPAssembly, DSPTestBinary) |
| 184 | { |
| 185 | ASSERT_TRUE(RoundTrip(s_dsp_test_bin)); |
| 186 | } |
| 187 | |
| 188 | TEST(DSPAssembly, DSPTestAssemble) |
| 189 | { |
| 190 | ASSERT_TRUE(AssembleAndCompare(s_dsp_test_text, s_dsp_test_bin)); |
| 191 | } |