Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/MIPS/MIPSDis.cpp
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/StringUtils.h"
19
#include "Core/HLE/HLE.h"
20
#include "Core/MemMap.h"
21
#include "Core/MIPS/MIPS.h"
22
#include "Core/MIPS/MIPSCodeUtils.h"
23
#include "Core/MIPS/MIPSDis.h"
24
#include "Core/MIPS/MIPSTables.h"
25
#include "Core/MIPS/MIPSDebugInterface.h"
26
27
#define _RS ((op>>21) & 0x1F)
28
#define _RT ((op>>16) & 0x1F)
29
#define _RD ((op>>11) & 0x1F)
30
#define _FS ((op>>11) & 0x1F)
31
#define _FT ((op>>16) & 0x1F)
32
#define _FD ((op>>6 ) & 0x1F)
33
#define _POS ((op>>6 ) & 0x1F)
34
#define _SIZE ((op>>11) & 0x1F)
35
36
#define RN(i) (currentDebugMIPS->GetRegName(0, i).c_str())
37
#define FN(i) (currentDebugMIPS->GetRegName(1, i).c_str())
38
//#define VN(i) (currentDebugMIPS->GetRegName(2, i).c_str())
39
40
namespace MIPSDis
41
{
42
std::string SignedHex(int i) {
43
char temp[32];
44
int offset = 0;
45
if (i < 0)
46
{
47
temp[0] = '-';
48
offset = 1;
49
i = -i;
50
}
51
52
snprintf(&temp[offset], sizeof(temp) - offset, "0x%X", i);
53
return temp;
54
}
55
56
void Dis_Generic(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
57
snprintf(out, outSize, "%s\t --- unknown ---", MIPSGetName(op));
58
}
59
60
void Dis_Cache(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
61
int imm = SignExtend16ToS32(op & 0xFFFF);
62
int rs = _RS;
63
int func = (op >> 16) & 0x1F;
64
snprintf(out, outSize, "%s\tfunc=%i, %s(%s)", MIPSGetName(op), func, RN(rs), SignedHex(imm).c_str());
65
}
66
67
void Dis_mxc1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
68
int fs = _FS;
69
int rt = _RT;
70
const char *name = MIPSGetName(op);
71
snprintf(out, outSize, "%s\t%s, %s", name, RN(rt), FN(fs));
72
}
73
74
void Dis_FPU3op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
75
int ft = _FT;
76
int fs = _FS;
77
int fd = _FD;
78
const char *name = MIPSGetName(op);
79
snprintf(out, outSize, "%s\t%s, %s, %s", name, FN(fd), FN(fs), FN(ft));
80
}
81
82
void Dis_FPU2op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
83
int fs = _FS;
84
int fd = _FD;
85
const char *name = MIPSGetName(op);
86
snprintf(out, outSize, "%s\t%s, %s", name, FN(fd), FN(fs));
87
}
88
89
void Dis_FPULS(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
90
int offset = SignExtend16ToS32(op & 0xFFFF);
91
int ft = _FT;
92
int rs = _RS;
93
const char *name = MIPSGetName(op);
94
snprintf(out, outSize, "%s\t%s, %s(%s)", name, FN(ft), SignedHex(offset).c_str(), RN(rs));
95
}
96
97
void Dis_FPUComp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
98
int fs = _FS;
99
int ft = _FT;
100
const char *name = MIPSGetName(op);
101
snprintf(out, outSize, "%s\t%s, %s", name, FN(fs), FN(ft));
102
}
103
104
void Dis_FPUBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
105
u32 off = pc;
106
int imm = SignExtend16ToS32(op & 0xFFFF) << 2;
107
off += imm + 4;
108
const char *name = MIPSGetName(op);
109
snprintf(out, outSize, "%s\t->$%08x", name, off);
110
}
111
112
void Dis_RelBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
113
u32 off = pc;
114
int imm = SignExtend16ToS32(op & 0xFFFF) << 2;
115
int rs = _RS;
116
off += imm + 4;
117
118
const char *name = MIPSGetName(op);
119
snprintf(out, outSize, "%s\t%s, ->$%08x", name, RN(rs), off);
120
}
121
122
void Dis_Syscall(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
123
u32 callno = (op>>6) & 0xFFFFF; //20 bits
124
int funcnum = callno & 0xFFF;
125
int modulenum = (callno & 0xFF000) >> 12;
126
snprintf(out, outSize, "syscall\t %s", GetHLEFuncName(modulenum, funcnum));
127
}
128
129
void Dis_ToHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
130
int rs = _RS;
131
const char *name = MIPSGetName(op);
132
snprintf(out, outSize, "%s\t%s", name, RN(rs));
133
}
134
void Dis_FromHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
135
int rd = _RD;
136
const char *name = MIPSGetName(op);
137
snprintf(out, outSize, "%s\t%s", name, RN(rd));
138
}
139
140
void Dis_RelBranch2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
141
u32 off = pc;
142
int imm = SignExtend16ToS32(op & 0xFFFF) << 2;
143
int rt = _RT;
144
int rs = _RS;
145
off += imm + 4;
146
147
const char *name = MIPSGetName(op);
148
int o = op>>26;
149
if (o==4 && rs == rt)//beq
150
snprintf(out, outSize, "b\t->$%08x", off);
151
else if (o==20 && rs == rt)//beql
152
snprintf(out, outSize, "bl\t->$%08x", off);
153
else
154
snprintf(out, outSize, "%s\t%s, %s, ->$%08x", name, RN(rs), RN(rt), off);
155
}
156
157
void Dis_IType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
158
u32 uimm = op & 0xFFFF;
159
u32 suimm = SignExtend16ToU32(op);
160
s32 simm = SignExtend16ToS32(op);
161
162
int rt = _RT;
163
int rs = _RS;
164
const char *name = MIPSGetName(op);
165
switch (op >> 26)
166
{
167
case 8: //addi
168
case 9: //addiu
169
case 10: //slti
170
snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rt), RN(rs), SignedHex(simm).c_str());
171
break;
172
case 11: //sltiu
173
snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), suimm);
174
break;
175
default:
176
snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm);
177
break;
178
}
179
}
180
void Dis_ori(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
181
u32 uimm = op & 0xFFFF;
182
int rt = _RT;
183
int rs = _RS;
184
const char *name = MIPSGetName(op);
185
if (rs == 0)
186
snprintf(out, outSize, "li\t%s, 0x%X", RN(rt), uimm);
187
else
188
snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm);
189
}
190
191
void Dis_IType1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
192
u32 uimm = op & 0xFFFF;
193
int rt = _RT;
194
const char *name = MIPSGetName(op);
195
snprintf(out, outSize, "%s\t%s, 0x%X", name, RN(rt), uimm);
196
}
197
198
void Dis_addi(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
199
int imm = SignExtend16ToS32(op & 0xFFFF);
200
int rt = _RT;
201
int rs = _RS;
202
if (rs == 0)
203
snprintf(out, outSize, "li\t%s, %s", RN(rt), SignedHex(imm).c_str());
204
else
205
Dis_IType(op, pc, out, outSize);
206
}
207
208
void Dis_ITypeMem(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
209
int imm = SignExtend16ToS32(op & 0xFFFF);
210
int rt = _RT;
211
int rs = _RS;
212
const char *name = MIPSGetName(op);
213
snprintf(out, outSize, "%s\t%s, %s(%s)", name, RN(rt), SignedHex(imm).c_str(), RN(rs));
214
}
215
216
void Dis_RType2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
217
int rs = _RS;
218
int rd = _RD;
219
const char *name = MIPSGetName(op);
220
snprintf(out, outSize, "%s\t%s, %s", name, RN(rd), RN(rs));
221
}
222
223
void Dis_RType3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
224
int rt = _RT;
225
int rs = _RS;
226
int rd = _RD;
227
const char *name = MIPSGetName(op);
228
snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt));
229
}
230
231
void Dis_addu(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
232
int rt = _RT;
233
int rs = _RS;
234
int rd = _RD;
235
const char *name = MIPSGetName(op);
236
if (rs==0 && rt==0)
237
snprintf(out, outSize, "li\t%s, 0", RN(rd));
238
else if (rs == 0)
239
snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rt));
240
else if (rt == 0)
241
snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rs));
242
else
243
snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt));
244
}
245
246
void Dis_ShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
247
int rt = _RT;
248
int rs = _RS;
249
int rd = _RD;
250
int sa = (op>>6) & 0x1F;
251
const char *name = MIPSGetName(op);
252
if (((op & 0x3f) == 2) && rs == 1)
253
name = "rotr";
254
if (((op & 0x3f) == 6) && sa == 1)
255
name = "rotrv";
256
snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rd), RN(rt), sa);
257
}
258
259
void Dis_VarShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
260
int rt = _RT;
261
int rs = _RS;
262
int rd = _RD;
263
int sa = (op>>6) & 0x1F;
264
const char *name = MIPSGetName(op);
265
if (((op & 0x3f) == 6) && sa == 1)
266
name = "rotrv";
267
snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rt), RN(rs));
268
}
269
270
void Dis_MulDivType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
271
int rt = _RT;
272
int rs = _RS;
273
const char *name = MIPSGetName(op);
274
snprintf(out, outSize, "%s\t%s, %s", name, RN(rs), RN(rt));
275
}
276
277
void Dis_Special3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
278
int rs = _RS;
279
int Rt = _RT;
280
int pos = _POS;
281
const char *name = MIPSGetName(op);
282
283
switch (op & 0x3f)
284
{
285
case 0x0: //ext
286
{
287
int size = _SIZE + 1;
288
snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size);
289
}
290
break;
291
case 0x4: // ins
292
{
293
int size = (_SIZE + 1) - pos;
294
snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size);
295
}
296
break;
297
}
298
}
299
300
void Dis_JumpType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
301
u32 off = ((op & 0x03FFFFFF) << 2);
302
u32 addr = (pc & 0xF0000000) | off;
303
const char *name = MIPSGetName(op);
304
snprintf(out, outSize, "%s\t->$%08x", name, addr);
305
}
306
307
void Dis_JumpRegType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
308
int rs = _RS;
309
int rd = _RD;
310
const char *name = MIPSGetName(op);
311
if ((op & 0x3f) == 9 && rd != MIPS_REG_RA)
312
snprintf(out, outSize, "%s\t%s,->%s", name, RN(rd), RN(rs));
313
else
314
snprintf(out, outSize, "%s\t->%s", name, RN(rs));
315
}
316
317
void Dis_Allegrex(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
318
int rt = _RT;
319
int rd = _RD;
320
const char *name = MIPSGetName(op);
321
snprintf(out, outSize, "%s\t%s,%s", name, RN(rd), RN(rt));
322
}
323
324
void Dis_Allegrex2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
325
int rt = _RT;
326
int rd = _RD;
327
const char *name = MIPSGetName(op);
328
snprintf(out, outSize,"%s\t%s,%s", name, RN(rd), RN(rt));
329
}
330
331
void Dis_Emuhack(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) {
332
auto resolved = Memory::Read_Instruction(pc, true);
333
char disasm[256];
334
if (MIPS_IS_EMUHACK(resolved)) {
335
truncate_cpy(disasm, sizeof(disasm), "(invalid emuhack)");
336
} else {
337
MIPSDisAsm(resolved, pc, disasm, sizeof(disasm), true);
338
}
339
340
switch (op.encoding >> 24) {
341
case 0x68:
342
snprintf(out, outSize, "* jitblock: %s", disasm);
343
break;
344
case 0x6a:
345
snprintf(out, outSize, "* replacement: %s", disasm);
346
break;
347
default:
348
snprintf(out, outSize, "* (invalid): %s", disasm);
349
break;
350
}
351
}
352
353
354
}
355
356