Debian Codegraph @

arch/MOS65XX/MOS65XXDisassembler.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* MOS65XX Backend by Sebastian Macke <[email protected]> 2018 */
3
4 #include "capstone/mos65xx.h"
5 #include "MOS65XXDisassembler.h"
6 #include "MOS65XXDisassemblerInternals.h"
7
8 typedef struct OpInfo {
9 mos65xx_insn ins;
10 mos65xx_address_mode am;
11 int operand_bytes;
12 } OpInfo;
13
14 static const struct OpInfo OpInfoTable[]= {
15
16 #include "m6502.inc"
17 #include "m65c02.inc"
18 #include "mw65c02.inc"
19 #include "m65816.inc"
20
21 };
22
23 static const char* const RegNames[] = {
24 "invalid", "A", "X", "Y", "P", "SP", "DP", "B", "K"
25 };
26
27 #ifndef CAPSTONE_DIET
28 static const char* const GroupNames[] = {
29 NULL,
30 "jump",
31 "call",
32 "ret",
33 "int",
34 "iret",
35 "branch_relative"
36 };
37
38 typedef struct InstructionInfo {
39 const char* name;
40 mos65xx_group_type group_type;
41 mos65xx_reg write, read;
42 bool modifies_status;
43 } InstructionInfo;
44
45 static const struct InstructionInfo InstructionInfoTable[]= {
46
47 #include "instruction_info.inc"
48
49 };
50 #endif
51
52 #ifndef CAPSTONE_DIET
53 static void fillDetails(MCInst *MI, struct OpInfo opinfo, int cpu_type)
54 {
55 int i;
56 cs_detail *detail = MI->flat_insn->detail;
57
58 InstructionInfo insinfo = InstructionInfoTable[opinfo.ins];
59
60 detail->mos65xx.am = opinfo.am;
61 detail->mos65xx.modifies_flags = insinfo.modifies_status;
62 detail->groups_count = 0;
63 detail->regs_read_count = 0;
64 detail->regs_write_count = 0;
65 detail->mos65xx.op_count = 0;
66
67 if (insinfo.group_type != MOS65XX_GRP_INVALID) {
68 detail->groups[detail->groups_count] = insinfo.group_type;
69 detail->groups_count++;
70 }
71
72 if (opinfo.am == MOS65XX_AM_REL || opinfo.am == MOS65XX_AM_ZP_REL) {
73 detail->groups[detail->groups_count] = MOS65XX_GRP_BRANCH_RELATIVE;
74 detail->groups_count++;
75 }
76
77 if (insinfo.read != MOS65XX_REG_INVALID) {
78 detail->regs_read[detail->regs_read_count++] = insinfo.read;
79 } else switch(opinfo.am) {
80 case MOS65XX_AM_ACC:
81 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_ACC;
82 break;
83 case MOS65XX_AM_ZP_Y:
84 case MOS65XX_AM_ZP_IND_Y:
85 case MOS65XX_AM_ABS_Y:
86 case MOS65XX_AM_ZP_IND_LONG_Y:
87 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_Y;
88 break;
89
90 case MOS65XX_AM_ZP_X:
91 case MOS65XX_AM_ZP_X_IND:
92 case MOS65XX_AM_ABS_X:
93 case MOS65XX_AM_ABS_X_IND:
94 case MOS65XX_AM_ABS_LONG_X:
95 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_X;
96 break;
97
98 case MOS65XX_AM_SR:
99 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_SP;
100 break;
101 case MOS65XX_AM_SR_IND_Y:
102 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_SP;
103 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_Y;
104 break;
105
106 default:
107 break;
108 }
109
110 if (insinfo.write != MOS65XX_REG_INVALID) {
111 detail->regs_write[detail->regs_write_count++] = insinfo.write;
112 } else if (opinfo.am == MOS65XX_AM_ACC) {
113 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_ACC;
114 }
115
116
117 switch(opinfo.ins) {
118 case MOS65XX_INS_ADC:
119 case MOS65XX_INS_SBC:
120 case MOS65XX_INS_ROL:
121 case MOS65XX_INS_ROR:
122 /* these read carry flag (and decimal for ADC/SBC) */
123 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_P;
124 break;
125 /* stack operations */
126 case MOS65XX_INS_JSL:
127 case MOS65XX_INS_JSR:
128 case MOS65XX_INS_PEA:
129 case MOS65XX_INS_PEI:
130 case MOS65XX_INS_PER:
131 case MOS65XX_INS_PHA:
132 case MOS65XX_INS_PHB:
133 case MOS65XX_INS_PHD:
134 case MOS65XX_INS_PHK:
135 case MOS65XX_INS_PHP:
136 case MOS65XX_INS_PHX:
137 case MOS65XX_INS_PHY:
138 case MOS65XX_INS_PLA:
139 case MOS65XX_INS_PLB:
140 case MOS65XX_INS_PLD:
141 case MOS65XX_INS_PLP:
142 case MOS65XX_INS_PLX:
143 case MOS65XX_INS_PLY:
144 case MOS65XX_INS_RTI:
145 case MOS65XX_INS_RTL:
146 case MOS65XX_INS_RTS:
147 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_SP;
148 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_SP;
149 break;
150 default:
151 break;
152 }
153
154 if (cpu_type == MOS65XX_CPU_TYPE_65816) {
155 switch (opinfo.am) {
156 case MOS65XX_AM_ZP:
157 case MOS65XX_AM_ZP_X:
158 case MOS65XX_AM_ZP_Y:
159 case MOS65XX_AM_ZP_IND:
160 case MOS65XX_AM_ZP_X_IND:
161 case MOS65XX_AM_ZP_IND_Y:
162 case MOS65XX_AM_ZP_IND_LONG:
163 case MOS65XX_AM_ZP_IND_LONG_Y:
164 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_DP;
165 break;
166 case MOS65XX_AM_BLOCK:
167 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_ACC;
168 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_X;
169 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_Y;
170 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_ACC;
171 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_X;
172 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_Y;
173 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_B;
174 break;
175 default:
176 break;
177 }
178
179 switch (opinfo.am) {
180 case MOS65XX_AM_ZP_IND:
181 case MOS65XX_AM_ZP_X_IND:
182 case MOS65XX_AM_ZP_IND_Y:
183 case MOS65XX_AM_ABS:
184 case MOS65XX_AM_ABS_X:
185 case MOS65XX_AM_ABS_Y:
186 case MOS65XX_AM_ABS_X_IND:
187 /* these depend on the databank to generate a 24-bit address */
188 /* exceptions: PEA, PEI, and JMP (abs) */
189 if (opinfo.ins == MOS65XX_INS_PEI || opinfo.ins == MOS65XX_INS_PEA) break;
190 detail->regs_read[detail->regs_read_count++] = MOS65XX_REG_B;
191 break;
192 default:
193 break;
194 }
195 }
196
197 if (insinfo.modifies_status) {
198 detail->regs_write[detail->regs_write_count++] = MOS65XX_REG_P;
199 }
200
201 switch(opinfo.am) {
202 case MOS65XX_AM_IMP:
203 break;
204 case MOS65XX_AM_IMM:
205 detail->mos65xx.operands[detail->mos65xx.op_count].type = MOS65XX_OP_IMM;
206 detail->mos65xx.operands[detail->mos65xx.op_count].imm = MI->Operands[0].ImmVal;
207 detail->mos65xx.op_count++;
208 break;
209 case MOS65XX_AM_ACC:
210 detail->mos65xx.operands[detail->mos65xx.op_count].type = MOS65XX_OP_REG;
211 detail->mos65xx.operands[detail->mos65xx.op_count].reg = MOS65XX_REG_ACC;
212 detail->mos65xx.op_count++;
213 break;
214 case MOS65XX_AM_REL: {
215 int value = MI->Operands[0].ImmVal;
216 if (MI->op1_size == 1)
217 value = 2 + (signed char)value;
218 else
219 value = 3 + (signed short)value;
220 detail->mos65xx.operands[detail->mos65xx.op_count].type = MOS65XX_OP_MEM;
221 detail->mos65xx.operands[detail->mos65xx.op_count].mem = (MI->address + value) & 0xffff;
222 detail->mos65xx.op_count++;
223 break;
224 }
225 case MOS65XX_AM_ZP_REL: {
226 int value = 3 + (signed char)MI->Operands[1].ImmVal;
227 /* BBR0, zp, rel and BBS0, zp, rel */
228 detail->mos65xx.operands[detail->mos65xx.op_count].type = MOS65XX_OP_MEM;
229 detail->mos65xx.operands[detail->mos65xx.op_count].mem = MI->Operands[0].ImmVal;
230 detail->mos65xx.operands[detail->mos65xx.op_count+1].type = MOS65XX_OP_MEM;
231 detail->mos65xx.operands[detail->mos65xx.op_count+1].mem = (MI->address + value) & 0xffff;
232 detail->mos65xx.op_count+=2;
233 break;
234 }
235 default:
236 for (i = 0; i < MI->size; ++i) {
237 detail->mos65xx.operands[detail->mos65xx.op_count].type = MOS65XX_OP_MEM;
238 detail->mos65xx.operands[detail->mos65xx.op_count].mem = MI->Operands[i].ImmVal;
239 detail->mos65xx.op_count++;
240 }
241 break;
242 }
243 }
244 #endif
245
246 void MOS65XX_printInst(MCInst *MI, struct SStream *O, void *PrinterInfo)
247 {
248 #ifndef CAPSTONE_DIET
249 unsigned int value;
250 unsigned opcode = MCInst_getOpcode(MI);
251 mos65xx_info *info = (mos65xx_info *)PrinterInfo;
252
253 OpInfo opinfo = OpInfoTable[opcode];
254
255 const char *prefix = info->hex_prefix ? info->hex_prefix : "0x";
256
257 SStream_concat0(O, InstructionInfoTable[opinfo.ins].name);
258 switch (opinfo.ins) {
259 /* special case - bit included as part of the instruction name */
260 case MOS65XX_INS_BBR:
261 case MOS65XX_INS_BBS:
262 case MOS65XX_INS_RMB:
263 case MOS65XX_INS_SMB:
264 SStream_concat(O, "%d", (opcode >> 4) & 0x07);
265 break;
266 default:
267 break;
268 }
269
270 value = MI->Operands[0].ImmVal;
271
272 switch (opinfo.am) {
273 default:
274 break;
275
276 case MOS65XX_AM_IMP:
277 break;
278
279 case MOS65XX_AM_ACC:
280 SStream_concat0(O, " a");
281 break;
282
283 case MOS65XX_AM_IMM:
284 if (MI->imm_size == 1)
285 SStream_concat(O, " #%s%02x", prefix, value);
286 else
287 SStream_concat(O, " #%s%04x", prefix, value);
288 break;
289
290 case MOS65XX_AM_ZP:
291 SStream_concat(O, " %s%02x", prefix, value);
292 break;
293
294 case MOS65XX_AM_ABS:
295 SStream_concat(O, " %s%04x", prefix, value);
296 break;
297
298 case MOS65XX_AM_ABS_LONG_X:
299 SStream_concat(O, " %s%06x, x", prefix, value);
300 break;
301
302 case MOS65XX_AM_INT:
303 SStream_concat(O, " %s%02x", prefix, value);
304 break;
305
306 case MOS65XX_AM_ABS_X:
307 SStream_concat(O, " %s%04x, x", prefix, value);
308 break;
309
310 case MOS65XX_AM_ABS_Y:
311 SStream_concat(O, " %s%04x, y", prefix, value);
312 break;
313
314 case MOS65XX_AM_ABS_LONG:
315 SStream_concat(O, " %s%06x", prefix, value);
316 break;
317
318 case MOS65XX_AM_ZP_X:
319 SStream_concat(O, " %s%02x, x", prefix, value);
320 break;
321
322 case MOS65XX_AM_ZP_Y:
323 SStream_concat(O, " %s%02x, y", prefix, value);
324 break;
325
326 case MOS65XX_AM_REL:
327 if (MI->op1_size == 1)
328 value = 2 + (signed char)value;
329 else
330 value = 3 + (signed short)value;
331
332 SStream_concat(O, " %s%04x", prefix,
333 (MI->address + value) & 0xffff);
334 break;
335
336 case MOS65XX_AM_ABS_IND:
337 SStream_concat(O, " (%s%04x)", prefix, value);
338 break;
339
340 case MOS65XX_AM_ABS_X_IND:
341 SStream_concat(O, " (%s%04x, x)", prefix, value);
342 break;
343
344 case MOS65XX_AM_ABS_IND_LONG:
345 SStream_concat(O, " [%s%04x]", prefix, value);
346 break;
347
348 case MOS65XX_AM_ZP_IND:
349 SStream_concat(O, " (%s%02x)", prefix, value);
350 break;
351
352 case MOS65XX_AM_ZP_X_IND:
353 SStream_concat(O, " (%s%02x, x)", prefix, value);
354 break;
355
356 case MOS65XX_AM_ZP_IND_Y:
357 SStream_concat(O, " (%s%02x), y", prefix, value);
358 break;
359
360 case MOS65XX_AM_ZP_IND_LONG:
361 SStream_concat(O, " [%s%02x]", prefix, value);
362 break;
363
364 case MOS65XX_AM_ZP_IND_LONG_Y:
365 SStream_concat(O, " [%s%02x], y", prefix, value);
366 break;
367
368 case MOS65XX_AM_SR:
369 SStream_concat(O, " %s%02x, s", prefix, value);
370 break;
371
372 case MOS65XX_AM_SR_IND_Y:
373 SStream_concat(O, " (%s%02x, s), y", prefix, value);
374 break;
375
376 case MOS65XX_AM_BLOCK:
377 SStream_concat(O, " %s%02x, %s%02x",
378 prefix, MI->Operands[0].ImmVal,
379 prefix, MI->Operands[1].ImmVal);
380 break;
381
382 case MOS65XX_AM_ZP_REL:
383 value = 3 + (signed char)MI->Operands[1].ImmVal;
384 /* BBR0, zp, rel and BBS0, zp, rel */
385 SStream_concat(O, " %s%02x, %s%04x",
386 prefix, MI->Operands[0].ImmVal,
387 prefix, (MI->address + value) & 0xffff);
388 break;
389
390 }
391 #endif
392 }
393
394 bool MOS65XX_getInstruction(csh ud, const uint8_t *code, size_t code_len,
395 MCInst *MI, uint16_t *size, uint64_t address, void *inst_info)
396 {
397 int i;
398 unsigned char opcode;
399 unsigned char len;
400 unsigned cpu_offset = 0;
401 int cpu_type = MOS65XX_CPU_TYPE_6502;
402 cs_struct* handle = MI->csh;
403 mos65xx_info *info = (mos65xx_info *)handle->printer_info;
404 OpInfo opinfo;
405
406 if (code_len == 0) {
407 *size = 1;
408 return false;
409 }
410
411 cpu_type = info->cpu_type;
412 cpu_offset = cpu_type * 256;
413
414 opcode = code[0];
415 opinfo = OpInfoTable[cpu_offset + opcode];
416 if (opinfo.ins == MOS65XX_INS_INVALID) {
417 *size = 1;
418 return false;
419 }
420
421 len = opinfo.operand_bytes + 1;
422
423 if (cpu_type == MOS65XX_CPU_TYPE_65816 && opinfo.am == MOS65XX_AM_IMM) {
424 switch(opinfo.ins) {
425 case MOS65XX_INS_CPX:
426 case MOS65XX_INS_CPY:
427 case MOS65XX_INS_LDX:
428 case MOS65XX_INS_LDY:
429 if (info->long_x) ++len;
430 break;
431 case MOS65XX_INS_ADC:
432 case MOS65XX_INS_AND:
433 case MOS65XX_INS_BIT:
434 case MOS65XX_INS_CMP:
435 case MOS65XX_INS_EOR:
436 case MOS65XX_INS_LDA:
437 case MOS65XX_INS_ORA:
438 case MOS65XX_INS_SBC:
439 if (info->long_m) ++len;
440 break;
441 default:
442 break;
443 }
444 }
445
446 if (code_len < len) {
447 *size = 1;
448 return false;
449 }
450
451 MI->address = address;
452
453 MCInst_setOpcode(MI, cpu_offset + opcode);
454 MCInst_setOpcodePub(MI, opinfo.ins);
455
456 *size = len;
457
458 /* needed to differentiate relative vs relative long */
459 MI->op1_size = len - 1;
460 if (opinfo.ins == MOS65XX_INS_NOP) {
461 for (i = 1; i < len; ++i)
462 MCOperand_CreateImm0(MI, code[i]);
463 }
464
465 switch (opinfo.am) {
466 case MOS65XX_AM_ZP_REL:
467 MCOperand_CreateImm0(MI, code[1]);
468 MCOperand_CreateImm0(MI, code[2]);
469 break;
470 case MOS65XX_AM_BLOCK:
471 MCOperand_CreateImm0(MI, code[2]);
472 MCOperand_CreateImm0(MI, code[1]);
473 break;
474 case MOS65XX_AM_IMP:
475 case MOS65XX_AM_ACC:
476 break;
477
478 case MOS65XX_AM_IMM:
479 MI->has_imm = 1;
480 MI->imm_size = len - 1;
481 /* 65816 immediate is either 1 or 2 bytes */
482 /* drop through */
483 default:
484 if (len == 2)
485 MCOperand_CreateImm0(MI, code[1]);
486 else if (len == 3)
487 MCOperand_CreateImm0(MI, (code[2]<<8) | code[1]);
488 else if (len == 4)
489 MCOperand_CreateImm0(MI, (code[3]<<16) | (code[2]<<8) | code[1]);
490 break;
491 }
492
493 #ifndef CAPSTONE_DIET
494 if (MI->flat_insn->detail) {
495 fillDetails(MI, opinfo, cpu_type);
496 }
497 #endif
498
499 return true;
500 }
501
502 const char *MOS65XX_insn_name(csh handle, unsigned int id)
503 {
504 #ifdef CAPSTONE_DIET
505 return NULL;
506 #else
507 if (id >= ARR_SIZE(InstructionInfoTable)) {
508 return NULL;
509 }
510 return InstructionInfoTable[id].name;
511 #endif
512 }
513
514 const char* MOS65XX_reg_name(csh handle, unsigned int reg)
515 {
516 #ifdef CAPSTONE_DIET
517 return NULL;
518 #else
519 if (reg >= ARR_SIZE(RegNames)) {
520 return NULL;
521 }
522 return RegNames[(int)reg];
523 #endif
524 }
525
526 void MOS65XX_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
527 {
528 /* id is cpu_offset + opcode */
529 if (id < ARR_SIZE(OpInfoTable)) {
530 insn->id = OpInfoTable[id].ins;
531 }
532 }
533
534 const char *MOS65XX_group_name(csh handle, unsigned int id)
535 {
536 #ifdef CAPSTONE_DIET
537 return NULL;
538 #else
539 if (id >= ARR_SIZE(GroupNames)) {
540 return NULL;
541 }
542 return GroupNames[(int)id];
543 #endif
544 }