summaryrefslogtreecommitdiff
path: root/Source/UnitTests/Core/DSP/DSPAssemblyTest.cpp
blob: 53354525885128b9e3668979ce073b55fa60c906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2017 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <fmt/format.h>

#include "Core/DSP/DSPCodeUtil.h"
#include "Core/DSP/DSPDisassembler.h"

#include "DSPTestBinary.h"
#include "DSPTestText.h"
#include "HermesBinary.h"
#include "HermesText.h"

#include <gtest/gtest.h>

static bool RoundTrippableDisassemble(std::span<const u16> code, std::string& text)
{
  DSP::AssemblerSettings settings;
  settings.ext_separator = '\'';
  settings.decode_names = true;
  settings.decode_registers = true;
  // These two prevent roundtripping.
  settings.show_hex = false;
  settings.show_pc = false;
  DSP::DSPDisassembler disasm(settings);

  return disasm.Disassemble(code, text);
}

// This test goes from text ASM to binary to text ASM and once again back to binary.
// Then the two binaries are compared.
static bool RoundTrip(std::span<const u16> code1)
{
  std::vector<u16> code2;
  std::string text;
  if (!RoundTrippableDisassemble(code1, text))
  {
    fmt::print("RoundTrip: Disassembly failed.\n");
    return false;
  }
  if (!DSP::Assemble(text, code2))
  {
    fmt::print("RoundTrip: Assembly failed.\n");
    return false;
  }
  if (!DSP::Compare(code1, code2))
  {
    fmt::print("RoundTrip: Assembled code does not match input code\n");
    return false;
  }
  return true;
}

// This test goes from text ASM to binary to text ASM and once again back to binary.
// Very convenient for testing. Then the two binaries are compared.
static bool SuperTrip(const char* asm_code)
{
  std::vector<u16> code1, code2;
  std::string text;
  if (!DSP::Assemble(asm_code, code1))
  {
    fmt::print("SuperTrip: First assembly failed\n");
    return false;
  }
  fmt::print("First assembly: {} words\n", code1.size());

  if (!RoundTrippableDisassemble(code1, text))
  {
    fmt::print("SuperTrip: Disassembly failed\n");
    return false;
  }
  else
  {
    fmt::print("Disassembly:\n");
    fmt::print("{}", text);
  }

  if (!DSP::Assemble(text, code2))
  {
    fmt::print("SuperTrip: Second assembly failed\n");
    return false;
  }

  if (!DSP::Compare(code1, code2))
  {
    fmt::print("SuperTrip: Assembled code does not match between passes\n");
    return false;
  }
  return true;
}

// Assembles asm_code, and verifies that it matches code1.
static bool AssembleAndCompare(const char* asm_code, std::span<const u16> code1)
{
  std::vector<u16> code2;
  if (!DSP::Assemble(asm_code, code2))
  {
    fmt::print("AssembleAndCompare: Assembly failed\n");
    return false;
  }

  fmt::print("AssembleAndCompare: Produced {} words; padding to {} words\n", code2.size(),
             code1.size());
  while (code2.size() < code1.size())
    code2.push_back(0);

  if (!DSP::Compare(code1, code2))
  {
    fmt::print("AssembleAndCompare: Assembled code does not match expected code\n");
    return false;
  }
  return true;
}

// Let's start out easy - a trivial instruction..
TEST(DSPAssembly, TrivialInstruction)
{
  ASSERT_TRUE(SuperTrip("	NOP\n"));
}

// Now let's do several.
TEST(DSPAssembly, SeveralTrivialInstructions)
{
  ASSERT_TRUE(SuperTrip("	NOP\n"
                        "	NOP\n"
                        "	NOP\n"));
}

// Turning it up a notch.
TEST(DSPAssembly, SeveralNoParameterInstructions)
{
  ASSERT_TRUE(SuperTrip("	SET16\n"
                        "	SET40\n"
                        "	CLR15\n"
                        "	M0\n"
                        "	M2\n"));
}

// Time to try labels and parameters, and comments.
TEST(DSPAssembly, LabelsParametersAndComments)
{
  ASSERT_TRUE(SuperTrip("DIRQ_TEST:	equ	0xfffb	; DSP Irq Request\n"
                        "	si		@0xfffc, #0x8888\n"
                        "	si		@0xfffd, #0xbeef\n"
                        "	si		@DIRQ_TEST, #0x0001\n"));
}

// Let's see if registers roundtrip. Also try predefined labels.
TEST(DSPAssembly, RegistersAndPredefinedLabels)
{
  ASSERT_TRUE(SuperTrip("	si		@0xfffc, #0x8888\n"
                        "	si		@0xfffd, #0xbeef\n"
                        "	si		@DIRQ, #0x0001\n"));
}

// Let's try some messy extended instructions.
TEST(DSPAssembly, ExtendedInstructions)
{
  ASSERT_TRUE(SuperTrip("   MULMV'SN    $AX0.L, $AX0.H, $ACC0 : @$AR2, $AC1.M\n"
                        "   ADDAXL'MV   $ACC1, $AX1.L : $AX1.H, $AC1.M\n"));
}

TEST(DSPAssembly, HermesText)
{
  ASSERT_TRUE(SuperTrip(s_hermes_text));
}

TEST(DSPAssembly, HermesBinary)
{
  ASSERT_TRUE(RoundTrip(s_hermes_bin));
}

TEST(DSPAssembly, HermesAssemble)
{
  ASSERT_TRUE(AssembleAndCompare(s_hermes_text, s_hermes_bin));
}

TEST(DSPAssembly, DSPTestText)
{
  ASSERT_TRUE(SuperTrip(s_dsp_test_text));
}

TEST(DSPAssembly, DSPTestBinary)
{
  ASSERT_TRUE(RoundTrip(s_dsp_test_bin));
}

TEST(DSPAssembly, DSPTestAssemble)
{
  ASSERT_TRUE(AssembleAndCompare(s_dsp_test_text, s_dsp_test_bin));
}