-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsp430x_disas.c
382 lines (337 loc) · 13 KB
/
msp430x_disas.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <r_types.h>
#include <r_util.h>
#include "msp430x_disas.h"
/* Known "issues":
- mov #0, offset(reg) is shown as clr offset(reg) instead of mov
*/
typedef struct {
char name[8];
ut16 id;
ut16 mask;
ut8 as;
ut8 ad;
ut8 type;
} opcode_table;
static const opcode_table opcodes[] = {
// Emulated instructions
{"nop", 0x4303, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"ret", 0x4130, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"adc", 0x6300, 0xff30, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"br", 0x4000, 0xf08f, MSP430_ADDR_AUTO, MSP430_ADDR_NONE, MSP430_TWOOP},
{"clr", 0x4300, 0xff30, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"clrc", 0xc312, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"clrn", 0xc222, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"clrz", 0xc322, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"dadc", 0xa300, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"dec", 0x8310, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"decd", 0x8320, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"dint", 0xc232, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"eint", 0xd232, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"inc", 0x5310, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"incd", 0x5320, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"pop", 0x4130, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"rla", 0x5500, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"rlc", 0x6500, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"sbc", 0x7300, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"setc", 0xd312, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"setn", 0xd222, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"setz", 0xd322, 0xffff, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_TWOOP},
{"tst", 0x9300, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_AUTO, MSP430_TWOOP},
// Two operand extended instructions
{"mova", 0x0000, 0xf0f0, MSP430_ADDR_INDIRECT, MSP430_ADDR_DIRECT, MSP430_X},
{"mova", 0x0010, 0xf0f0, MSP430_ADDR_INDIRECT_POST_INC, MSP430_ADDR_DIRECT, MSP430_X},
{"mova", 0x0020, 0xf0f0, MSP430_ADDR_ABS20, MSP430_ADDR_DIRECT, MSP430_X},
{"mova", 0x0030, 0xf0f0, MSP430_ADDR_INDEXED, MSP430_ADDR_DIRECT, MSP430_X},
{"mova", 0x0060, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_ABS20, MSP430_X},
{"mova", 0x0070, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_INDEXED, MSP430_X},
{"mova", 0x0080, 0xf0f0, MSP430_ADDR_IMM20, MSP430_ADDR_DIRECT, MSP430_X},
{"cmpa", 0x0090, 0xf0f0, MSP430_ADDR_IMM20, MSP430_ADDR_DIRECT, MSP430_X},
{"adda", 0x00a0, 0xf0f0, MSP430_ADDR_IMM20, MSP430_ADDR_DIRECT, MSP430_X},
{"suba", 0x00b0, 0xf0f0, MSP430_ADDR_IMM20, MSP430_ADDR_DIRECT, MSP430_X},
{"mova", 0x00c0, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_DIRECT, MSP430_X},
{"cmpa", 0x00d0, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_DIRECT, MSP430_X},
{"adda", 0x00e0, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_DIRECT, MSP430_X},
{"suba", 0x00f0, 0xf0f0, MSP430_ADDR_DIRECT, MSP430_ADDR_DIRECT, MSP430_X},
// One operand extended instructions
{"rrcm.a", 0x0040, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rram.a", 0x0140, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rlam.a", 0x0240, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rrum.a", 0x0340, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rrcm", 0x0050, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rram", 0x0150, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rlam", 0x0250, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
{"rrum", 0x0350, 0xf3f0, MSP430_ADDR_REPEAT, MSP430_ADDR_DIRECT, MSP430_X},
// Third table extended
// TODO: Should be able to do nicer decoding of these...
{"calla", 0x13b0, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_IMM20, MSP430_X},
{"calla", 0x1380, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_ABS20, MSP430_X},
{"calla", 0x1370, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_INDIRECT_POST_INC, MSP430_X},
{"calla", 0x1360, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_INDIRECT, MSP430_X},
{"calla", 0x1340, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_DIRECT, MSP430_X},
{"calla", 0x1350, 0xfff0, MSP430_ADDR_NONE, MSP430_ADDR_INDEXED, MSP430_X},
{"pushm.a", 0x1400, 0xff00, MSP430_ADDR_PUSHPOP, MSP430_ADDR_DIRECT, MSP430_X},
{"pushm", 0x1500, 0xff00, MSP430_ADDR_PUSHPOP, MSP430_ADDR_DIRECT, MSP430_X},
{"popm.a", 0x1600, 0xff00, MSP430_ADDR_PUSHPOP, MSP430_ADDR_POPM, MSP430_X},
{"popm", 0x1700, 0xff00, MSP430_ADDR_PUSHPOP, MSP430_ADDR_POPM, MSP430_X},
// Two operand instructions
{"mov", 0x4000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"add", 0x5000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"addc", 0x6000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"subc", 0x7000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"sub", 0x8000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"cmp", 0x9000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"dadd", 0xa000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"bit", 0xb000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"bic", 0xc000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"bis", 0xd000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"xor", 0xe000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
{"and", 0xf000, 0xf000, MSP430_ADDR_AUTO, MSP430_ADDR_AUTO, MSP430_TWOOP},
// Jumps
{"jnz", 0x2000, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jz", 0x2400, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jnc", 0x2800, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jc", 0x2c00, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jn", 0x3000, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jge", 0x3400, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jl", 0x3800, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
{"jmp", 0x3c00, 0xfc00, MSP430_ADDR_NONE, MSP430_ADDR_JUMP, MSP430_JUMP},
// One operand instructions
{"rrc", 0x1000, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"swpb", 0x1080, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"rra", 0x1100, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"sxt", 0x1180, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"push", 0x1200, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"call", 0x1280, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_ONEOP, MSP430_ONEOP},
{"reti", 0x1300, 0xf380, MSP430_ADDR_NONE, MSP430_ADDR_NONE, MSP430_ONEOP},
// Do not remove
{},
};
static int get_src (instr) {
return (instr >> 8) & 0xF;
}
static int get_dst (instr) {
return instr & 0xF;
}
static ut8 get_as (ut16 instr) {
return (instr >> 4) & 3;
}
static ut8 get_bw (ut16 instr) {
return (instr >> 6) & 1;
}
static ut8 get_ad (ut16 instr) {
return (instr >> 7) & 1;
}
static ut8 is_extension_word (ut16 instr) {
return ((instr >> 11) & 0x1f) == 3;
}
static ut8 decode_addr (char *buf, ssize_t max, ut8 as, ut8 mode, ut8 reg, ut16 op, ut16 ext, st32 *addr) {
char postfix = 0;
int ret = 0;
st32 address = 0;
switch (mode) {
case MSP430_ADDR_DIRECT:
snprintf (buf, max, "r%d", reg);
ret = 0;
break;
case MSP430_ADDR_INDEXED:
// Do not print out sign, just raw hex
snprintf (buf, max, "0x%04x(r%d)", (ext << 16) | op, reg);
ret = 2;
break;
case MSP430_ADDR_INDIRECT_POST_INC:
postfix = '+';
// same same, fall through
case MSP430_ADDR_INDIRECT:
snprintf (buf, max, "@r%d%c", reg, postfix);
break;
case MSP430_ADDR_IMM:
address = ext << 16 | op;
snprintf (buf, max, "#0x%04x", address);
ret = 2;
break;
case MSP430_ADDR_REPEAT:
snprintf (buf, max, "#%d", ((reg >> 2) & 0xf) + 1);
break;
case MSP430_ADDR_PUSHPOP:
snprintf (buf, max, "#%d", op);
break;
case MSP430_ADDR_POPM:
snprintf (buf, max, "r%d", op);
break;
case MSP430_ADDR_IMM20:
address = (reg << 16) | op;
snprintf (buf, max, "#0x%05x", address);
ret = 2;
break;
case MSP430_ADDR_ABS20:
snprintf (buf, max, "&0x%04x", (reg << 16) | op);
ret = 2;
break;
case MSP430_ADDR_ABS:
snprintf (buf, max, "&0x%04x", (ext << 16) | op);
ret = 2;
break;
case MSP430_ADDR_CG1:
snprintf (buf, max, "#%d", 4 * (as - 1));
break;
case MSP430_ADDR_CG2:
snprintf (buf, max, "#%d", as == 3 ? -1 : as);
break;
default:
buf[0] = '\0';
break;
}
*addr = address;
return ret;
}
static ut8 decode_addr_mode (ut8 mode, ut8 dst) {
if (mode > 1 && dst == MSP430_SR)
return MSP430_ADDR_CG1;
else if (dst == MSP430_R3)
return MSP430_ADDR_CG2;
else if (mode && dst == MSP430_SR)
return MSP430_ADDR_ABS;
else if (mode == MSP430_ADDR_INDIRECT_POST_INC
&& dst == MSP430_PC)
return MSP430_ADDR_IMM;
else
return mode;
}
static ut8 output_twoop (ut16 instr,
ut16 ext,
ut16 op1,
ut16 op2,
const opcode_table *op,
struct msp430_cmd *cmd) {
int ret;
ut8 as, src_mode;
ut8 ad = MSP430_ADDR_NONE, dst_mode;
ut8 src_ext = 0;
ut8 dst_ext = 0;
st32 data_ptr;
snprintf (cmd->instr, MSP430_INSTR_MAXLEN - 1, "%s%s%s",
op->name, ext ? "x" : "",
get_bw (instr) && op->type != MSP430_X ? ".b" : "");
if (op->as == MSP430_ADDR_AUTO) {
as = get_as (instr);
src_ext = (ext >> 7) & 0xf;
} else {
as = op->as;
}
// Push/pop have their very own encoding of as / op
if (as == MSP430_ADDR_PUSHPOP) {
src_mode = as;
op1 = ((instr >> 4) & 0xf) + 1;
} else if (as != MSP430_ADDR_NONE) {
src_mode = decode_addr_mode (as, get_src (instr));
} else {
src_mode = as;
}
if (op->ad == MSP430_ADDR_AUTO) {
ad = get_ad (instr);
if (ad && get_dst (instr) == MSP430_SR)
dst_mode = MSP430_ADDR_ABS;
else
dst_mode = ad;
dst_ext = get_dst (ext);
} else if (op->ad == MSP430_ADDR_POPM) {
dst_mode = MSP430_ADDR_POPM;
op1 = get_dst (instr) + (instr >> 4) & 0xf;
} else {
dst_mode = op->ad;
}
ret = decode_addr (cmd->operands, MSP430_INSTR_MAXLEN - 1,
as, src_mode,
get_src (instr), op1, src_ext,
&data_ptr);
char dstbuf[16] = {0};
if (data_ptr != 0) {
cmd->ptr_addr = data_ptr;
}
ret += decode_addr (dstbuf, sizeof(dstbuf),
ad, dst_mode,
get_dst (instr), ret > 0 ? op2 : op1, dst_ext,
&data_ptr);
if (data_ptr != 0) {
cmd->ptr_addr = data_ptr;
}
if (cmd->operands[0] && dstbuf[0]) {
strncat (cmd->operands, ", ", MSP430_INSTR_MAXLEN - 1 - strlen (cmd->operands));
}
strncat (cmd->operands, dstbuf, MSP430_INSTR_MAXLEN - 1 - strlen (cmd->operands));
return ret;
}
static ut8 get_jmp_opcode (ut16 instr) {
return instr >> 13;
}
static ut8 get_jmp_cond (ut16 instr) {
return (instr >> 10) & 7;
}
static ut8 output_jump (ut16 instr, struct msp430_cmd *cmd) {
ut16 addr = instr & 0x3FF;
cmd->jmp_addr = (st16)(addr >= 0x300 ? (st16)((0xFE00 | addr) * 2 + 2) : (addr & 0x1FF) * 2 + 2);
snprintf (cmd->operands, MSP430_INSTR_MAXLEN - 1, "$%c0x%04x", addr >= 0x300 ? '-' : '+',
addr >= 0x300 ? 0x400 - ((addr & 0x1FF) * 2 + 2) : (addr & 0x1FF) * 2 + 2);
cmd->jmp_cond = get_jmp_cond (instr);
cmd->opcode = get_jmp_opcode (instr);
cmd->type = MSP430_JUMP;
return 0;
}
static ut8 output_oneop (ut16 instr, ut16 ext, ut16 op1, struct msp430_cmd *cmd) {
ut8 as = get_as (instr);
ut8 reg = get_dst (instr);
ut8 mode = decode_addr_mode (as, reg);
return decode_addr (cmd->operands, MSP430_INSTR_MAXLEN - 1,
as, mode, reg, op1, get_dst (ext), &cmd->jmp_addr);
}
static void output_prefix(ut8 len, ut16 ext, char *prefix) {
// Check if we have a repeat count
if (len == 2 && ((ext & 0xf) != 0)) {
if (ext & 0x80) {
snprintf (prefix, MSP430_INSTR_MAXLEN, ".rpt r%d", ext & 0x0f);
} else {
snprintf (prefix, MSP430_INSTR_MAXLEN, ".rpt #%d", 1 + (ext & 0x0f));
}
}
}
static ut8 decode_430x (ut16 instr, ut16 op1, ut16 op2, ut16 ext, struct msp430_cmd *cmd) {
const opcode_table *ot = opcodes;
for (; ot->name[0] != '\0'; ot++) {
ut8 len = 0;
if ((instr & ot->mask) == ot->id) {
snprintf (cmd->instr, MSP430_INSTR_MAXLEN - 1, "%s%c",
ot->name, ext ? 'x' : '\0');
switch (ot->ad) {
case MSP430_ADDR_JUMP:
len = 2 + output_jump (instr, cmd);
break;
case MSP430_ADDR_ONEOP:
len = 2 + output_oneop (instr, ext, op1, cmd);
break;
default:
len = 2 + output_twoop (instr, ext, op1, op2, ot, cmd);
break;
}
output_prefix(len, ext, cmd->prefix);
return len;
}
}
return -1;
}
int msp430x_decode_command (const ut8 *in, struct msp430_cmd *cmd) {
int ret = -1;
ut16 instr;
ut16 extension = 0;
ut16 operand1, operand2;
r_mem_copyendian ((ut8 *)&instr, in, sizeof(ut16), LIL_ENDIAN);
if (is_extension_word (instr)) {
in += 2;
extension = instr;
r_mem_copyendian ((ut8 *)&instr, in, sizeof(ut16), LIL_ENDIAN);
}
r_mem_copyendian ((ut8 *)&operand1, in + 2, sizeof(ut16), LIL_ENDIAN);
r_mem_copyendian ((ut8 *)&operand2, in + 4, sizeof(ut16), LIL_ENDIAN);
ret = decode_430x (instr, operand1, operand2, extension, cmd);
ret += extension ? 2 : 0;
return ret;
}