-
Notifications
You must be signed in to change notification settings - Fork 0
/
jagcpu.c
310 lines (293 loc) · 5.78 KB
/
jagcpu.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdint.h>
#include <stdio.h>
#include "jagcpu.h"
char *gpu_mnemonics[] = {
"add",
"addc",
"addq",
"addqt",
"sub",
"subc",
"subq",
"subqt",
"neg",
"and",
"or",
"xor",
"not",
"btst",
"bset",
"bclr",
"mult",
"imult",
"imultn",
"resmac",
"imacn",
"div",
"abs",
"sh",
"shlq",
"shrq",
"sha",
"sharq",
"ror",
"rorq",
"cmp",
"cmpq",
"sat8",
"sat16",
"move",
"moveq",
"moveta",
"movefa",
"movei",
"loadb",
"loadw",
"load",
"loadp",
"load", //r14 relative
"load", //r15 relative
"storeb",
"storew",
"store",
"storep",
"store", //r14 relative
"store", //r15 relative
"move", //PC
"jump",
"jr",
"mmult",
"mtoi",
"normi",
"nop",
"load", //r14 indexed
"load", //r15 indexed
"store", //r14 indexed
"store", //r15 indexed
"sat24",
"pack",
"unpack"
};
char *dsp_mnemonics[DSP_ADDQMOD+1] = {
[DSP_SUBQMOD] = "subqmod",
[DSP_SAT16S] = "sat16s",
[DSP_SAT32S] = "sat32s",
[DSP_MIRROR] = "mirror",
[DSP_ADDQMOD] = "addqmod"
};
void init_dsp_mnemonic_table()
{
static uint8_t init_done;
if (init_done) {
return;
}
for (int i = 0; i < DSP_ADDQMOD; i++)
{
if (!dsp_mnemonics[i]) {
dsp_mnemonics[i] = gpu_mnemonics[i];
}
}
init_done = 1;
}
uint16_t jag_opcode(uint16_t inst, uint8_t is_gpu)
{
uint16_t opcode = inst >> 10;
if (is_gpu && opcode == GPU_PACK && (inst & 0x20)) {
return GPU_UNPACK;
}
return opcode;
}
uint16_t jag_reg2(uint16_t inst)
{
return inst & 0x1F;
}
uint16_t jag_reg1(uint16_t inst)
{
return inst >> 5 & 0x1F;
}
//moveq and bit instructions should just use jag_reg1 instead
uint32_t jag_quick(uint16_t inst)
{
uint32_t val = inst >> 5 & 0x1F;
return val ? val : 32;
}
uint8_t is_quick_1_32_opcode(uint16_t opcode, uint8_t is_gpu)
{
return opcode == JAG_ADDQ
|| opcode == JAG_ADDQT
|| opcode == JAG_SUBQ
|| opcode == JAG_SUBQT
|| opcode == JAG_SHLQ
|| opcode == JAG_SHRQ
|| opcode == JAG_SHARQ
|| opcode == JAG_RORQ
|| (!is_gpu && (
opcode == DSP_SUBQMOD
|| opcode == DSP_ADDQMOD
));
}
uint8_t is_quick_0_31_opcode(uint16_t opcode)
{
return opcode == JAG_MOVEQ
|| opcode == JAG_BTST
|| opcode == JAG_BSET
|| opcode == JAG_BCLR;
}
uint8_t is_load(uint16_t opcode, uint8_t is_gpu)
{
return opcode == JAG_LOAD
|| opcode == JAG_LOADB
|| opcode == JAG_LOADW
|| (is_gpu && opcode == GPU_LOADP);
}
uint8_t is_store(uint16_t opcode, uint8_t is_gpu)
{
return opcode == JAG_STORE
|| opcode == JAG_STOREB
|| opcode == JAG_STOREW
|| (is_gpu && opcode == GPU_STOREP);
}
uint8_t is_single_source(uint16_t opcode, uint8_t is_gpu)
{
return opcode == JAG_NEG
|| opcode == JAG_NOT
|| opcode == JAG_ABS
|| opcode == GPU_SAT16 //no is_gpu check needed as DSP_SAT16S is also single source
|| (is_gpu && (
opcode == GPU_SAT8
|| opcode == GPU_SAT24
|| opcode == GPU_PACK
|| opcode == GPU_UNPACK
))
|| (!is_gpu && (
opcode == DSP_SAT32S
|| opcode == DSP_MIRROR
));
}
char * jag_cc_names[] = {
"t",
"ne",
"eq",
"f",
"cc",
"hi",
"eq_cc",
"f",
"cs",
"ne_cs",
"eq_cs",
"f",
"f",
"f",
"f",
"f"
"t_alt",
"ne_alt",
"eq_alt",
"f",
"pl",
"ne_pl",
"eq_pl",
"f",
"mi",
"ne_mi",
"eq_mi"
};
char * jag_cc(uint16_t inst)
{
uint16_t ccnum = jag_reg2(inst);
if (ccnum >= sizeof(jag_cc_names)/sizeof(*jag_cc_names)) {
return jag_cc_names[3];
}
return jag_cc_names[ccnum];
}
uint8_t jag_is_alwyas_falsse(uint16_t cond)
{
return (cond & 3) == 3 || (cond && 0xC) == 0xC;
}
uint32_t jag_jr_dest(uint16_t inst, uint32_t address)
{
uint32_t rel = jag_reg1(inst);
if (rel & 0x10) {
rel |= 0xFFFFFFE0;
}
return address + 2 + rel*2;
}
int jag_cpu_disasm(uint16_t **stream, uint32_t address, char *dst, uint8_t is_gpu, uint8_t labels)
{
uint16_t inst = **stream;
(*stream)++;
uint16_t opcode = jag_opcode(inst, is_gpu);
char **mnemonics;
if (is_gpu) {
mnemonics = gpu_mnemonics;
} else {
init_dsp_mnemonic_table();
mnemonics = dsp_mnemonics;
}
switch (opcode)
{
case JAG_MOVEI: {
uint32_t immed = **stream;
(*stream)++;
immed |= **stream << 16;
(*stream)++;
return sprintf(dst, "%s $%X, r%d", mnemonics[opcode], immed, jag_reg2(inst));
}
case JAG_JR:
return sprintf(dst,
labels ? "%s %s, ADR_%X" : "%s %s, $W%X",
mnemonics[opcode], jag_cc(inst), jag_jr_dest(inst, address)
);
case JAG_JUMP:
return sprintf(dst, "%s %s, (r%d)", mnemonics[opcode], jag_cc(inst), jag_reg1(inst));
case JAG_LOAD_R14_REL:
case JAG_LOAD_R15_REL:
return sprintf(dst, "%s (r%d+%d), r%d",
mnemonics[opcode],
opcode == JAG_LOAD_R14_REL ? 14 : 15,
jag_quick(inst),
jag_reg2(inst)
);
break;
case JAG_STORE_R14_REL:
case JAG_STORE_R15_REL:
return sprintf(dst, "%s r%d, (r%d+%d)",
mnemonics[opcode],
jag_reg2(inst),
opcode == JAG_STORE_R14_REL ? 14 : 15,
jag_quick(inst)
);
break;
case JAG_LOAD_R14_INDEXED:
case JAG_LOAD_R15_INDEXED:
return sprintf(dst, "%s (r%d+r%d), r%d",
mnemonics[opcode],
opcode == JAG_LOAD_R14_INDEXED ? 14 : 15,
jag_reg1(inst),
jag_reg2(inst)
);
break;
case JAG_STORE_R14_INDEXED:
case JAG_STORE_R15_INDEXED:
return sprintf(dst, "%s r%d, (r%d+r%d)",
mnemonics[opcode],
jag_reg2(inst),
opcode == JAG_STORE_R14_INDEXED ? 14 : 15,
jag_reg1(inst)
);
break;
default:
if (is_quick_1_32_opcode(opcode, is_gpu)) {
return sprintf(dst, "%s %d, r%d", mnemonics[opcode], jag_quick(inst), jag_reg2(inst));
} else if (is_quick_0_31_opcode(opcode)) {
return sprintf(dst, "%s %d, r%d", mnemonics[opcode], jag_reg1(inst), jag_reg2(inst));
} else if (is_load(opcode, is_gpu)) {
return sprintf(dst, "%s (r%d), r%d", mnemonics[opcode], jag_reg1(inst), jag_reg2(inst));
} else if (is_store(opcode, is_gpu)) {
return sprintf(dst, "%s r%d, (r%d)", mnemonics[opcode], jag_reg2(inst), jag_reg1(inst));
} else {
return sprintf(dst, "%s r%d, r%d", mnemonics[opcode], jag_reg1(inst), jag_reg2(inst));
}
}
}