Coverage Report

Created: 2025-11-09 07:00

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