-
Notifications
You must be signed in to change notification settings - Fork 0
/
disassembler.cpp
241 lines (206 loc) · 6.01 KB
/
disassembler.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "disassembler.h"
#include "cpu_types.h"
#include "magic_enum.hpp"
#include <vector>
#include <cstring>
const char* parser_get_symbolic_cop0_name(int i);
const char* parser_get_symbolic_gpr_name(int i);
// implemented in cpu_instructions.cpp
extern std::vector<EncodingDescriptor> encodings;
// string compare that is limited to the length of the second string
constexpr bool str_find(const char* s1, const char* s2)
{
while (*s2)
{
if (!s1)
return false;
if (*s1 != *s2)
return false;
s1++;
s2++;
}
return true;
};
void disassembler_init()
{
/*auto err = cs_open(CS_ARCH_MIPS, CS_MODE_MIPS32, &cs);
if (err != CS_ERR_OK)
{
printf("%s\n", cs_strerror(err));
}*/
}
/*const EncodingDescriptor* disassembler_decode_instruction(uint32_t opcode)
{
cs_insn* decoded{};
auto addr = cpu.pc;
if (auto count = cs_disasm(cs, (const uint8_t*)&opcode, sizeof(uint32_t), addr, 1, &decoded))
{
auto d = decoded[0];
char m[32]{};
strcpy(m, d.mnemonic);
std::transform(m + 0, m + strlen(m), m, toupper);
for (const auto& desc : encodings)
{
if (strcmp(m, magic_enum::enum_name(desc.type).data()) == 0)
{
return &desc;
}
}
printf("Unknown opcode '%s'\n", m);
}
return nullptr;
}*/
const EncodingDescriptor* disassembler_find_descriptor(InstructionType type)
{
for (const auto& encoding : encodings)
{
if (encoding.type == type)
return &encoding;
}
return nullptr;
}
const EncodingDescriptor* disassembler_decode_instruction(uint32_t opcode)
{
for (const auto& encoding : encodings)
{
if (encoding.match(opcode))
return &encoding;
}
return nullptr;
}
bool disassembler_parse_instruction(uint32_t opcode, const EncodingDescriptor* desc, char* dst_buf, int)
{
if (desc)
{
dst_buf += sprintf(dst_buf, "%-8s", magic_enum::enum_name(desc->type).data());
auto* str = desc->debug_format;
while (*str)
{
if (str_find(str, "RTI"))
{
dst_buf += sprintf(dst_buf, "%d", (uint8_t)GET_RT_BITS(opcode));
str += 3;
}
else if (str_find(str, "COP_RD"))
{
dst_buf += sprintf(dst_buf, "%s", parser_get_symbolic_cop0_name(GET_RD_BITS(opcode)));
str += 6;
}
else if (str_find(str, "RS"))
{
dst_buf += sprintf(dst_buf, "$%s", parser_get_symbolic_gpr_name(GET_RS_BITS(opcode)));
str += 2;
}
else if (str_find(str, "RT"))
{
dst_buf += sprintf(dst_buf, "$%s", parser_get_symbolic_gpr_name(GET_RT_BITS(opcode)));
str += 2;
}
else if (str_find(str, "RD"))
{
dst_buf += sprintf(dst_buf, "$%s", parser_get_symbolic_gpr_name(GET_RD_BITS(opcode)));
str += 2;
}
else if (str_find(str, "IMM"))
{
dst_buf += sprintf(dst_buf, "0x%04X", (uint16_t)GET_IMM_BITS(opcode));
str += 3;
}
else if (str_find(str, "OFFSET"))
{
dst_buf += sprintf(dst_buf, "%d", (int16_t)GET_IMM_BITS(opcode));
str += 6;
}
else if (str_find(str, "SA"))
{
dst_buf += sprintf(dst_buf, "%d", GET_SHIFT_BITS(opcode));
str += 2;
}
else if (str_find(str, "TARGET"))
{
auto address = (cpu.pc & 0xF0000000) + (GET_JMP_BITS(opcode) << 2);
dst_buf += sprintf(dst_buf, "0x%08X", (uint32_t)address);
str += 6;
}
else
{
dst_buf += sprintf(dst_buf, "%c", *str);
str++;
}
}
*dst_buf = '\0';
return true;
}
return false;
}
const char* parser_get_symbolic_cop0_name(int i)
{
switch (i)
{
case 0: return "index";
case 1: return "random";
case 2: return "entry_lo0";
case 3: return "entry_hi0";
case 4: return "context";
case 5: return "pagemask";
case 6: return "wired";
case 8: return "bad_vaddr";
case 9: return "count";
case 10: return "entry_hi";
case 11: return "compare";
case 12: return "status";
case 13: return "cause";
case 14: return "epc";
case 15: return "prid";
case 16: return "config";
case 17: return "ll_addr";
case 18: return "watch_lo";
case 19: return "watch_hi";
case 20: return "xcontext";
case 26: return "parity_error";
case 27: return "cache_error";
case 28: return "tag_lo";
case 29: return "tag_hi";
case 30: return "error_epc";
}
return nullptr;
}
const char* parser_get_symbolic_gpr_name(int i)
{
switch (i)
{
case 0: return "0";
case 1: return "at";
case 2: return "v0";
case 3: return "v1";
case 4: return "a0";
case 5: return "a1";
case 6: return "a2";
case 7: return "a3";
case 8: return "t0";
case 9: return "t1";
case 10: return "t2";
case 11: return "t3";
case 12: return "t4";
case 13: return "t5";
case 14: return "t6";
case 15: return "t7";
case 24: return "t8";
case 25: return "t9";
case 16: return "s0";
case 17: return "s1";
case 18: return "s2";
case 19: return "s3";
case 20: return "s4";
case 21: return "s5";
case 22: return "s6";
case 23: return "s7";
case 26: return "k0";
case 27: return "k1";
case 28: return "gp";
case 29: return "sp";
case 30: return "fp";
case 31: return "ra";
}
return nullptr;
}