CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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