Coverage Report

Created: 2025-12-05 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/arch/M680X/M680XDisassembler.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* M680X Backend by Wolfgang Schwotzer <wolfgang.schwotzer@gmx.net> 2017 */
3
4
/* ======================================================================== */
5
/* ================================ INCLUDES ============================== */
6
/* ======================================================================== */
7
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11
12
#include "../../cs_priv.h"
13
#include "../../utils.h"
14
15
#include "../../MCInst.h"
16
#include "../../MCInstrDesc.h"
17
#include "../../MCRegisterInfo.h"
18
#include "M680XInstPrinter.h"
19
#include "M680XDisassembler.h"
20
#include "M680XDisassemblerInternals.h"
21
22
#ifdef CAPSTONE_HAS_M680X
23
24
#ifndef DECL_SPEC
25
#ifdef _MSC_VER
26
#define DECL_SPEC __cdecl
27
#else
28
#define DECL_SPEC
29
#endif  // _MSC_VER
30
#endif  // DECL_SPEC
31
32
/* ======================================================================== */
33
/* ============================ GENERAL DEFINES =========================== */
34
/* ======================================================================== */
35
36
/* ======================================================================== */
37
/* =============================== PROTOTYPES ============================= */
38
/* ======================================================================== */
39
40
typedef enum insn_hdlr_id {
41
  illgl_hid,
42
  rel8_hid,
43
  rel16_hid,
44
  imm8_hid,
45
  imm16_hid,
46
  imm32_hid,
47
  dir_hid,
48
  ext_hid,
49
  idxX_hid,
50
  idxY_hid,
51
  idx09_hid,
52
  inh_hid,
53
  rr09_hid,
54
  rbits_hid,
55
  bitmv_hid,
56
  tfm_hid,
57
  opidx_hid,
58
  opidxdr_hid,
59
  idxX0_hid,
60
  idxX16_hid,
61
  imm8rel_hid,
62
  idxS_hid,
63
  idxS16_hid,
64
  idxXp_hid,
65
  idxX0p_hid,
66
  idx12_hid,
67
  idx12s_hid,
68
  rr12_hid,
69
  loop_hid,
70
  index_hid,
71
  imm8i12x_hid,
72
  imm16i12x_hid,
73
  exti12x_hid,
74
  HANDLER_ID_ENDING,
75
} insn_hdlr_id;
76
77
// Access modes for the first 4 operands. If there are more than
78
// four operands they use the same access mode as the 4th operand.
79
//
80
// u: unchanged
81
// r: (r)read access
82
// w: (w)write access
83
// m: (m)odify access (= read + write)
84
//
85
typedef enum e_access_mode {
86
87
  uuuu,
88
  rrrr,
89
  wwww,
90
  rwww,
91
  rrrm,
92
  rmmm,
93
  wrrr,
94
  mrrr,
95
  mwww,
96
  mmmm,
97
  mwrr,
98
  mmrr,
99
  wmmm,
100
  rruu,
101
  muuu,
102
  ACCESS_MODE_ENDING,
103
} e_access_mode;
104
105
// Access type values are compatible with enum cs_ac_type:
106
typedef enum e_access {
107
  UNCHANGED = CS_AC_INVALID,
108
  READ = CS_AC_READ,
109
  WRITE = CS_AC_WRITE,
110
  MODIFY = (CS_AC_READ | CS_AC_WRITE),
111
} e_access;
112
113
/* Properties of one instruction in PAGE1 (without prefix) */
114
typedef struct inst_page1 {
115
  unsigned insn : 9;        // A value of type m680x_insn
116
  unsigned handler_id1 : 6; // Type insn_hdlr_id, first instr. handler id
117
  unsigned handler_id2 : 6; // Type insn_hdlr_id, second instr. handler id
118
} inst_page1;
119
120
/* Properties of one instruction in any other PAGE X */
121
typedef struct inst_pageX {
122
  unsigned opcode : 8;      // The opcode byte
123
  unsigned insn : 9;        // A value of type m680x_insn
124
  unsigned handler_id1 : 6; // Type insn_hdlr_id, first instr. handler id
125
  unsigned handler_id2 : 6; // Type insn_hdlr_id, second instr. handler id
126
} inst_pageX;
127
128
typedef struct insn_props {
129
  unsigned group : 4;
130
  unsigned access_mode : 5; // A value of type e_access_mode
131
  unsigned reg0 : 5;        // A value of type m680x_reg
132
  unsigned reg1 : 5;        // A value of type m680x_reg
133
  bool cc_modified : 1;
134
  bool update_reg_access : 1;
135
} insn_props;
136
137
#include "m6800.inc"
138
#include "m6801.inc"
139
#include "hd6301.inc"
140
#include "m6811.inc"
141
#include "cpu12.inc"
142
#include "m6805.inc"
143
#include "m6808.inc"
144
#include "hcs08.inc"
145
#include "m6809.inc"
146
#include "hd6309.inc"
147
148
#include "insn_props.inc"
149
150
//////////////////////////////////////////////////////////////////////////////
151
152
// M680X instuctions have 1 up to 8 bytes (CPU12: MOVW IDX2,IDX2).
153
// A reader is needed to read a byte or word from a given memory address.
154
// See also X86 reader(...)
155
static bool read_byte(const m680x_info *info, uint8_t *byte, uint16_t address)
156
1.06M
{
157
1.06M
  if (address < info->offset ||
158
1.06M
    (uint32_t)(address - info->offset) >= info->size)
159
    // out of code buffer range
160
2.16k
    return false;
161
162
1.06M
  *byte = info->code[address - info->offset];
163
164
1.06M
  return true;
165
1.06M
}
166
167
static bool read_byte_sign_extended(const m680x_info *info, int16_t *word,
168
  uint16_t address)
169
65.6k
{
170
65.6k
  if (address < info->offset ||
171
65.6k
    (uint32_t)(address - info->offset) >= info->size)
172
    // out of code buffer range
173
0
    return false;
174
175
65.6k
  *word = (int16_t) info->code[address - info->offset];
176
177
65.6k
  if (*word & 0x80)
178
24.4k
    *word |= 0xFF00;
179
180
65.6k
  return true;
181
65.6k
}
182
183
static bool read_word(const m680x_info *info, uint16_t *word, uint16_t address)
184
81.8k
{
185
81.8k
  if (address < info->offset ||
186
81.8k
    (uint32_t)(address + 1 - info->offset) >= info->size)
187
    // out of code buffer range
188
19
    return false;
189
190
81.8k
  *word = (uint16_t)info->code[address - info->offset] << 8;
191
81.8k
  *word |= (uint16_t)info->code[address + 1 - info->offset];
192
193
81.8k
  return true;
194
81.8k
}
195
196
static bool read_sdword(const m680x_info *info, int32_t *sdword,
197
  uint16_t address)
198
913
{
199
913
  if (address < info->offset ||
200
913
    (uint32_t)(address + 3 - info->offset) >= info->size)
201
    // out of code buffer range
202
0
    return false;
203
204
913
  *sdword = (uint32_t)info->code[address - info->offset] << 24;
205
913
  *sdword |= (uint32_t)info->code[address + 1 - info->offset] << 16;
206
913
  *sdword |= (uint32_t)info->code[address + 2 - info->offset] << 8;
207
913
  *sdword |= (uint32_t)info->code[address + 3 - info->offset];
208
209
913
  return true;
210
913
}
211
212
// For PAGE2 and PAGE3 opcodes when using an an array of inst_page1 most
213
// entries have M680X_INS_ILLGL. To avoid wasting memory an inst_pageX is
214
// used which contains the opcode. Using a binary search for the right opcode
215
// is much faster (= O(log n) ) in comparison to a linear search ( = O(n) ).
216
static int binary_search(const inst_pageX *const inst_pageX_table,
217
  size_t table_size, unsigned int opcode)
218
155k
{
219
  // As part of the algorithm last may get negative.
220
  // => signed integer has to be used.
221
155k
  int first = 0;
222
155k
  int last = (int)table_size - 1;
223
155k
  int middle = (first + last) / 2;
224
225
741k
  while (first <= last) {
226
686k
    if (inst_pageX_table[middle].opcode < opcode) {
227
237k
      first = middle + 1;
228
237k
    }
229
448k
    else if (inst_pageX_table[middle].opcode == opcode) {
230
99.6k
      return middle;  /* item found */
231
99.6k
    }
232
348k
    else
233
348k
      last = middle - 1;
234
235
586k
    middle = (first + last) / 2;
236
586k
  }
237
238
55.4k
  if (first > last)
239
55.4k
    return -1;  /* item not found */
240
241
0
  return -2;
242
55.4k
}
243
244
void M680X_get_insn_id(cs_struct *handle, cs_insn *insn, unsigned int id)
245
438k
{
246
438k
  const m680x_info *const info = (const m680x_info *)handle->printer_info;
247
438k
  const cpu_tables *cpu = info->cpu;
248
438k
  uint8_t insn_prefix = (id >> 8) & 0xff;
249
  // opcode is the first instruction byte without the prefix.
250
438k
  uint8_t opcode = id & 0xff;
251
438k
  int index;
252
438k
  int i;
253
254
438k
  insn->id = M680X_INS_ILLGL;
255
256
1.05M
  for (i = 0; i < ARR_SIZE(cpu->pageX_prefix); ++i) {
257
1.04M
    if (cpu->pageX_table_size[i] == 0 ||
258
660k
      (cpu->inst_pageX_table[i] == NULL))
259
380k
      break;
260
261
660k
    if (cpu->pageX_prefix[i] == insn_prefix) {
262
39.3k
      index = binary_search(cpu->inst_pageX_table[i],
263
39.3k
          cpu->pageX_table_size[i], opcode);
264
39.3k
      insn->id = (index >= 0) ?
265
30.0k
        cpu->inst_pageX_table[i][index].insn :
266
39.3k
        M680X_INS_ILLGL;
267
39.3k
      return;
268
39.3k
    }
269
660k
  }
270
271
398k
  if (insn_prefix != 0)
272
0
    return;
273
274
398k
  insn->id = cpu->inst_page1_table[id].insn;
275
276
398k
  if (insn->id != M680X_INS_ILLGL)
277
360k
    return;
278
279
  // Check if opcode byte is present in an overlay table
280
56.5k
  for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
281
54.9k
    if (cpu->overlay_table_size[i] == 0 ||
282
38.2k
      (cpu->inst_overlay_table[i] == NULL))
283
16.6k
      break;
284
285
38.2k
    if ((index = binary_search(cpu->inst_overlay_table[i],
286
38.2k
            cpu->overlay_table_size[i],
287
38.2k
            opcode)) >= 0) {
288
19.7k
      insn->id = cpu->inst_overlay_table[i][index].insn;
289
19.7k
      return;
290
19.7k
    }
291
38.2k
  }
292
38.0k
}
293
294
static void add_insn_group(cs_detail *detail, m680x_group_type group)
295
428k
{
296
428k
  if (detail != NULL &&
297
428k
    (group != M680X_GRP_INVALID) && (group != M680X_GRP_ENDING))
298
98.0k
    detail->groups[detail->groups_count++] = (uint8_t)group;
299
428k
}
300
301
static bool exists_reg_list(uint16_t *regs, uint8_t count, m680x_reg reg)
302
1.22M
{
303
1.22M
  uint8_t i;
304
305
2.05M
  for (i = 0; i < count; ++i) {
306
868k
    if (regs[i] == (uint16_t)reg)
307
36.5k
      return true;
308
868k
  }
309
310
1.18M
  return false;
311
1.22M
}
312
313
static void add_reg_to_rw_list(MCInst *MI, m680x_reg reg, e_access access)
314
808k
{
315
808k
  cs_detail *detail = MI->flat_insn->detail;
316
317
808k
  if (detail == NULL || (reg == M680X_REG_INVALID))
318
0
    return;
319
320
808k
  switch (access) {
321
412k
  case MODIFY:
322
412k
    if (!exists_reg_list(detail->regs_read,
323
412k
        detail->regs_read_count, reg))
324
405k
      detail->regs_read[detail->regs_read_count++] =
325
405k
        (uint16_t)reg;
326
327
  // intentionally fall through
328
329
537k
  case WRITE:
330
537k
    if (!exists_reg_list(detail->regs_write,
331
537k
        detail->regs_write_count, reg))
332
526k
      detail->regs_write[detail->regs_write_count++] =
333
526k
        (uint16_t)reg;
334
335
537k
    break;
336
337
271k
  case READ:
338
271k
    if (!exists_reg_list(detail->regs_read,
339
271k
        detail->regs_read_count, reg))
340
253k
      detail->regs_read[detail->regs_read_count++] =
341
253k
        (uint16_t)reg;
342
343
271k
    break;
344
345
0
  case UNCHANGED:
346
0
  default:
347
0
    break;
348
808k
  }
349
808k
}
350
351
static void update_am_reg_list(MCInst *MI, m680x_info *info, cs_m680x_op *op,
352
  e_access access)
353
570k
{
354
570k
  if (MI->flat_insn->detail == NULL)
355
0
    return;
356
357
570k
  switch (op->type) {
358
248k
  case M680X_OP_REGISTER:
359
248k
    add_reg_to_rw_list(MI, op->reg, access);
360
248k
    break;
361
362
116k
  case M680X_OP_INDEXED:
363
116k
    add_reg_to_rw_list(MI, op->idx.base_reg, READ);
364
365
116k
    if (op->idx.base_reg == M680X_REG_X &&
366
47.8k
      info->cpu->reg_byte_size[M680X_REG_H])
367
13.2k
      add_reg_to_rw_list(MI, M680X_REG_H, READ);
368
369
370
116k
    if (op->idx.offset_reg != M680X_REG_INVALID)
371
10.0k
      add_reg_to_rw_list(MI, op->idx.offset_reg, READ);
372
373
116k
    if (op->idx.inc_dec) {
374
24.7k
      add_reg_to_rw_list(MI, op->idx.base_reg, WRITE);
375
376
24.7k
      if (op->idx.base_reg == M680X_REG_X &&
377
9.15k
        info->cpu->reg_byte_size[M680X_REG_H])
378
2.99k
        add_reg_to_rw_list(MI, M680X_REG_H, WRITE);
379
24.7k
    }
380
381
116k
    break;
382
383
205k
  default:
384
205k
    break;
385
570k
  }
386
570k
}
387
388
static const e_access g_access_mode_to_access[4][15] = {
389
  {
390
    UNCHANGED, READ, WRITE, READ,  READ, READ,   WRITE, MODIFY,
391
    MODIFY, MODIFY, MODIFY, MODIFY, WRITE, READ, MODIFY,
392
  },
393
  {
394
    UNCHANGED, READ, WRITE, WRITE, READ, MODIFY, READ,  READ,
395
    WRITE, MODIFY, WRITE, MODIFY, MODIFY, READ, UNCHANGED,
396
  },
397
  {
398
    UNCHANGED, READ, WRITE, WRITE, READ, MODIFY, READ,  READ,
399
    WRITE, MODIFY, READ, READ, MODIFY, UNCHANGED, UNCHANGED,
400
  },
401
  {
402
    UNCHANGED, READ, WRITE, WRITE, MODIFY, MODIFY, READ, READ,
403
    WRITE, MODIFY, READ, READ, MODIFY, UNCHANGED, UNCHANGED,
404
  },
405
};
406
407
static e_access get_access(int operator_index, e_access_mode access_mode)
408
1.21M
{
409
1.21M
  int idx = (operator_index > 3) ? 3 : operator_index;
410
411
1.21M
  return g_access_mode_to_access[idx][access_mode];
412
1.21M
}
413
414
static void build_regs_read_write_counts(MCInst *MI, m680x_info *info,
415
  e_access_mode access_mode)
416
391k
{
417
391k
  cs_m680x *m680x = &info->m680x;
418
391k
  int i;
419
420
391k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count))
421
54.7k
    return;
422
423
907k
  for (i = 0; i < m680x->op_count; ++i) {
424
425
570k
    e_access access = get_access(i, access_mode);
426
570k
    update_am_reg_list(MI, info, &m680x->operands[i], access);
427
570k
  }
428
336k
}
429
430
static void add_operators_access(MCInst *MI, m680x_info *info,
431
  e_access_mode access_mode)
432
391k
{
433
391k
  cs_m680x *m680x = &info->m680x;
434
391k
  int offset = 0;
435
391k
  int i;
436
437
391k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count) ||
438
336k
    (access_mode == uuuu))
439
95.6k
    return;
440
441
824k
  for (i = 0; i < m680x->op_count; ++i) {
442
528k
    e_access access;
443
444
    // Ugly fix: MULD has a register operand, an immediate operand
445
    // AND an implicitly changed register W
446
528k
    if (info->insn == M680X_INS_MULD && (i == 1))
447
424
      offset = 1;
448
449
528k
    access = get_access(i + offset, access_mode);
450
528k
    m680x->operands[i].access = access;
451
528k
  }
452
296k
}
453
454
typedef struct insn_to_changed_regs {
455
  m680x_insn insn;
456
  e_access_mode access_mode;
457
  m680x_reg regs[10];
458
} insn_to_changed_regs;
459
460
static void set_changed_regs_read_write_counts(MCInst *MI, m680x_info *info)
461
39.4k
{
462
  //TABLE
463
2.17M
#define EOL M680X_REG_INVALID
464
39.4k
  static const insn_to_changed_regs changed_regs[] = {
465
39.4k
    { M680X_INS_BSR, mmmm, { M680X_REG_S, EOL } },
466
39.4k
    { M680X_INS_CALL, mmmm, { M680X_REG_S, EOL } },
467
39.4k
    {
468
39.4k
      M680X_INS_CWAI, mrrr, {
469
39.4k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
470
39.4k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
471
39.4k
        M680X_REG_D, M680X_REG_CC, EOL
472
39.4k
      },
473
39.4k
    },
474
39.4k
    { M680X_INS_DAA, mrrr, { M680X_REG_A, EOL } },
475
39.4k
    {
476
39.4k
      M680X_INS_DIV, mmrr, {
477
39.4k
        M680X_REG_A, M680X_REG_H, M680X_REG_X, EOL
478
39.4k
      }
479
39.4k
    },
480
39.4k
    {
481
39.4k
      M680X_INS_EDIV, mmrr, {
482
39.4k
        M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL
483
39.4k
      }
484
39.4k
    },
485
39.4k
    {
486
39.4k
      M680X_INS_EDIVS, mmrr, {
487
39.4k
        M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL
488
39.4k
      }
489
39.4k
    },
490
39.4k
    { M680X_INS_EMACS, mrrr, { M680X_REG_X, M680X_REG_Y, EOL } },
491
39.4k
    { M680X_INS_EMAXM, rrrr, { M680X_REG_D, EOL } },
492
39.4k
    { M680X_INS_EMINM, rrrr, { M680X_REG_D, EOL } },
493
39.4k
    { M680X_INS_EMUL, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
494
39.4k
    { M680X_INS_EMULS, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
495
39.4k
    { M680X_INS_ETBL, wmmm, { M680X_REG_A, M680X_REG_B, EOL } },
496
39.4k
    { M680X_INS_FDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
497
39.4k
    { M680X_INS_IDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
498
39.4k
    { M680X_INS_IDIVS, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
499
39.4k
    { M680X_INS_JSR, mmmm, { M680X_REG_S, EOL } },
500
39.4k
    { M680X_INS_LBSR, mmmm, { M680X_REG_S, EOL } },
501
39.4k
    { M680X_INS_MAXM, rrrr, { M680X_REG_A, EOL } },
502
39.4k
    { M680X_INS_MINM, rrrr, { M680X_REG_A, EOL } },
503
39.4k
    {
504
39.4k
      M680X_INS_MEM, mmrr, {
505
39.4k
        M680X_REG_X, M680X_REG_Y, M680X_REG_A, EOL
506
39.4k
      }
507
39.4k
    },
508
39.4k
    { M680X_INS_MUL, mmmm, { M680X_REG_A, M680X_REG_B, EOL } },
509
39.4k
    { M680X_INS_MULD, mwrr, { M680X_REG_D, M680X_REG_W, EOL } },
510
39.4k
    { M680X_INS_PSHA, rmmm, { M680X_REG_A, M680X_REG_S, EOL } },
511
39.4k
    { M680X_INS_PSHB, rmmm, { M680X_REG_B, M680X_REG_S, EOL } },
512
39.4k
    { M680X_INS_PSHC, rmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
513
39.4k
    { M680X_INS_PSHD, rmmm, { M680X_REG_D, M680X_REG_S, EOL } },
514
39.4k
    { M680X_INS_PSHH, rmmm, { M680X_REG_H, M680X_REG_S, EOL } },
515
39.4k
    { M680X_INS_PSHX, rmmm, { M680X_REG_X, M680X_REG_S, EOL } },
516
39.4k
    { M680X_INS_PSHY, rmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
517
39.4k
    { M680X_INS_PULA, wmmm, { M680X_REG_A, M680X_REG_S, EOL } },
518
39.4k
    { M680X_INS_PULB, wmmm, { M680X_REG_B, M680X_REG_S, EOL } },
519
39.4k
    { M680X_INS_PULC, wmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
520
39.4k
    { M680X_INS_PULD, wmmm, { M680X_REG_D, M680X_REG_S, EOL } },
521
39.4k
    { M680X_INS_PULH, wmmm, { M680X_REG_H, M680X_REG_S, EOL } },
522
39.4k
    { M680X_INS_PULX, wmmm, { M680X_REG_X, M680X_REG_S, EOL } },
523
39.4k
    { M680X_INS_PULY, wmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
524
39.4k
    {
525
39.4k
      M680X_INS_REV, mmrr, {
526
39.4k
        M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL
527
39.4k
      }
528
39.4k
    },
529
39.4k
    {
530
39.4k
      M680X_INS_REVW, mmmm, {
531
39.4k
        M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL
532
39.4k
      }
533
39.4k
    },
534
39.4k
    { M680X_INS_RTC, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
535
39.4k
    {
536
39.4k
      M680X_INS_RTI, mwww, {
537
39.4k
        M680X_REG_S, M680X_REG_CC, M680X_REG_B,
538
39.4k
        M680X_REG_A, M680X_REG_DP, M680X_REG_X,
539
39.4k
        M680X_REG_Y, M680X_REG_U, M680X_REG_PC,
540
39.4k
        EOL
541
39.4k
      },
542
39.4k
    },
543
39.4k
    { M680X_INS_RTS, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
544
39.4k
    { M680X_INS_SEX, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
545
39.4k
    { M680X_INS_SEXW, rwww, { M680X_REG_W, M680X_REG_D, EOL } },
546
39.4k
    {
547
39.4k
      M680X_INS_SWI, mmrr, {
548
39.4k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
549
39.4k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
550
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
551
39.4k
        EOL
552
39.4k
      }
553
39.4k
    },
554
39.4k
    {
555
39.4k
      M680X_INS_SWI2, mmrr, {
556
39.4k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
557
39.4k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
558
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
559
39.4k
        EOL
560
39.4k
      },
561
39.4k
    },
562
39.4k
    {
563
39.4k
      M680X_INS_SWI3, mmrr, {
564
39.4k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
565
39.4k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
566
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
567
39.4k
        EOL
568
39.4k
      },
569
39.4k
    },
570
39.4k
    { M680X_INS_TBL, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
571
39.4k
    {
572
39.4k
      M680X_INS_WAI, mrrr, {
573
39.4k
        M680X_REG_S, M680X_REG_PC, M680X_REG_X,
574
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
575
39.4k
        EOL
576
39.4k
      }
577
39.4k
    },
578
39.4k
    {
579
39.4k
      M680X_INS_WAV, rmmm, {
580
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_X,
581
39.4k
        M680X_REG_Y, EOL
582
39.4k
      }
583
39.4k
    },
584
39.4k
    {
585
39.4k
      M680X_INS_WAVR, rmmm, {
586
39.4k
        M680X_REG_A, M680X_REG_B, M680X_REG_X,
587
39.4k
        M680X_REG_Y, EOL
588
39.4k
      }
589
39.4k
    },
590
39.4k
  };
591
592
39.4k
  int i, j;
593
594
39.4k
  if (MI->flat_insn->detail == NULL)
595
0
    return;
596
597
2.05M
  for (i = 0; i < ARR_SIZE(changed_regs); ++i) {
598
2.01M
    if (info->insn == changed_regs[i].insn) {
599
39.4k
      e_access_mode access_mode = changed_regs[i].access_mode;
600
601
164k
      for (j = 0; changed_regs[i].regs[j] != EOL; ++j) {
602
125k
        e_access access;
603
604
125k
        m680x_reg reg = changed_regs[i].regs[j];
605
606
125k
        if (!info->cpu->reg_byte_size[reg]) {
607
12.4k
          if (info->insn != M680X_INS_MUL)
608
12.0k
            continue;
609
610
          // Hack for M68HC05: MUL uses reg. A,X
611
417
          reg = M680X_REG_X;
612
417
        }
613
614
113k
        access = get_access(j, access_mode);
615
113k
        add_reg_to_rw_list(MI, reg, access);
616
113k
      }
617
39.4k
    }
618
2.01M
  }
619
620
39.4k
#undef EOL
621
39.4k
}
622
623
typedef struct insn_desc {
624
  uint32_t opcode;
625
  m680x_insn insn;
626
  insn_hdlr_id hid[2];
627
  uint16_t insn_size;
628
} insn_desc;
629
630
// If successfull return the additional byte size needed for M6809
631
// indexed addressing mode (including the indexed addressing post_byte).
632
// On error return -1.
633
static int get_indexed09_post_byte_size(const m680x_info *info,
634
          uint16_t address)
635
54.4k
{
636
54.4k
  uint8_t ir = 0;
637
54.4k
  uint8_t post_byte;
638
639
  // Read the indexed addressing post byte.
640
54.4k
  if (!read_byte(info, &post_byte, address))
641
232
    return -1;
642
643
  // Depending on the indexed addressing mode more bytes have to be read.
644
54.1k
  switch (post_byte & 0x9F) {
645
2.14k
  case 0x87:
646
3.71k
  case 0x8A:
647
5.49k
  case 0x8E:
648
7.74k
  case 0x8F:
649
8.40k
  case 0x90:
650
9.13k
  case 0x92:
651
9.61k
  case 0x97:
652
10.0k
  case 0x9A:
653
10.8k
  case 0x9E:
654
10.8k
    return -1; // illegal indexed post bytes
655
656
1.08k
  case 0x88: // n8,R
657
2.79k
  case 0x8C: // n8,PCR
658
3.51k
  case 0x98: // [n8,R]
659
4.11k
  case 0x9C: // [n8,PCR]
660
4.11k
    if (!read_byte(info, &ir, address + 1))
661
38
      return -1;
662
4.07k
    return 2;
663
664
804
  case 0x89: // n16,R
665
2.91k
  case 0x8D: // n16,PCR
666
3.71k
  case 0x99: // [n16,R]
667
4.60k
  case 0x9D: // [n16,PCR]
668
4.60k
    if (!read_byte(info, &ir, address + 2))
669
84
      return -1;
670
4.52k
    return 3;
671
672
1.60k
  case 0x9F: // [n]
673
1.60k
    if ((post_byte & 0x60) != 0 ||
674
804
      !read_byte(info, &ir, address + 2))
675
813
      return -1;
676
792
    return  3;
677
54.1k
  }
678
679
  // Any other indexed post byte is valid and
680
  // no additional bytes have to be read.
681
33.0k
  return 1;
682
54.1k
}
683
684
// If successfull return the additional byte size needed for CPU12
685
// indexed addressing mode (including the indexed addressing post_byte).
686
// On error return -1.
687
static int get_indexed12_post_byte_size(const m680x_info *info,
688
          uint16_t address, bool is_subset)
689
49.0k
{
690
49.0k
  uint8_t ir;
691
49.0k
  uint8_t post_byte;
692
693
  // Read the indexed addressing post byte.
694
49.0k
  if (!read_byte(info, &post_byte, address))
695
206
    return -1;
696
697
  // Depending on the indexed addressing mode more bytes have to be read.
698
48.8k
  if (!(post_byte & 0x20)) // n5,R
699
17.2k
    return 1;
700
701
31.5k
  switch (post_byte & 0xe7) {
702
3.30k
  case 0xe0:
703
6.78k
  case 0xe1: // n9,R
704
6.78k
    if (is_subset)
705
444
      return -1;
706
707
6.34k
    if (!read_byte(info, &ir, address))
708
0
      return -1;
709
6.34k
    return 2;
710
711
2.54k
  case 0xe2: // n16,R
712
5.80k
  case 0xe3: // [n16,R]
713
5.80k
    if (is_subset)
714
597
      return -1;
715
716
5.21k
    if (!read_byte(info, &ir, address + 1))
717
44
      return -1;
718
5.16k
    return 3;
719
720
1.27k
  case 0xe4: // A,R
721
2.35k
  case 0xe5: // B,R
722
3.38k
  case 0xe6: // D,R
723
5.17k
  case 0xe7: // [D,R]
724
18.9k
  default: // n,-r n,+r n,r- n,r+
725
18.9k
    break;
726
31.5k
  }
727
728
18.9k
  return 1;
729
31.5k
}
730
731
// Check for M6809/HD6309 TFR/EXG instruction for valid register
732
static bool is_tfr09_reg_valid(const m680x_info *info, uint8_t reg_nibble)
733
7.46k
{
734
7.46k
  if (info->cpu->tfr_reg_valid != NULL)
735
1.93k
    return info->cpu->tfr_reg_valid[reg_nibble];
736
737
5.52k
  return true; // e.g. for the M6309 all registers are valid
738
7.46k
}
739
740
// Check for CPU12 TFR/EXG instruction for valid register
741
static bool is_exg_tfr12_post_byte_valid(const m680x_info *info,
742
  uint8_t post_byte)
743
2.81k
{
744
2.81k
  return !(post_byte & 0x08);
745
2.81k
}
746
747
static bool is_tfm_reg_valid(const m680x_info *info, uint8_t reg_nibble)
748
3.90k
{
749
  // HD6809 TFM instruction: Only register X,Y,U,S,D is allowed
750
3.90k
  return reg_nibble <= 4;
751
3.90k
}
752
753
// If successfull return the additional byte size needed for CPU12
754
// loop instructions DBEQ/DBNE/IBEQ/IBNE/TBEQ/TBNE (including the post byte).
755
// On error return -1.
756
static int get_loop_post_byte_size(const m680x_info *info, uint16_t address)
757
3.83k
{
758
3.83k
  uint8_t post_byte;
759
3.83k
  uint8_t rr;
760
761
3.83k
  if (!read_byte(info, &post_byte, address))
762
13
    return -1;
763
764
  // According to documentation bit 3 is don't care and not checked here.
765
3.82k
  if ((post_byte >= 0xc0) ||
766
3.30k
    ((post_byte & 0x07) == 2) || ((post_byte & 0x07) == 3))
767
2.08k
    return -1;
768
769
1.74k
  if (!read_byte(info, &rr, address + 1))
770
12
    return -1;
771
772
1.72k
  return 2;
773
1.74k
}
774
775
// If successfull return the additional byte size needed for HD6309
776
// bit move instructions BAND/BEOR/BIAND/BIEOR/BIOR/BOR/LDBT/STBT
777
// (including the post byte).
778
// On error return -1.
779
static int get_bitmv_post_byte_size(const m680x_info *info, uint16_t address)
780
1.27k
{
781
1.27k
  uint8_t post_byte;
782
1.27k
  uint8_t rr;
783
784
1.27k
  if (!read_byte(info, &post_byte, address))
785
3
    return -1;
786
787
1.27k
  if ((post_byte & 0xc0) == 0xc0)
788
699
    return -1; // Invalid register specified
789
577
  else {
790
577
    if (!read_byte(info, &rr, address + 1))
791
5
      return -1;
792
577
  }
793
794
572
  return 2;
795
1.27k
}
796
797
static bool is_sufficient_code_size(const m680x_info *info, uint16_t address,
798
  insn_desc *insn_description)
799
410k
{
800
410k
  int i;
801
410k
  bool retval = true;
802
410k
  uint16_t size = 0;
803
410k
  int sz;
804
805
1.19M
  for (i = 0; i < 2; i++) {
806
802k
    uint8_t ir = 0;
807
802k
    bool is_subset = false;
808
809
802k
    switch (insn_description->hid[i]) {
810
811
956
    case imm32_hid:
812
956
      if ((retval = read_byte(info, &ir, address + size + 3)))
813
913
        size += 4;
814
956
      break;
815
816
53.3k
    case ext_hid:
817
59.8k
    case imm16_hid:
818
62.3k
    case rel16_hid:
819
65.1k
    case imm8rel_hid:
820
68.8k
    case opidxdr_hid:
821
71.2k
    case idxX16_hid:
822
72.0k
    case idxS16_hid:
823
72.0k
      if ((retval = read_byte(info, &ir, address + size + 1)))
824
71.3k
        size += 2;
825
72.0k
      break;
826
827
25.8k
    case rel8_hid:
828
80.8k
    case dir_hid:
829
87.2k
    case rbits_hid:
830
114k
    case imm8_hid:
831
120k
    case idxX_hid:
832
122k
    case idxXp_hid:
833
125k
    case idxY_hid:
834
126k
    case idxS_hid:
835
127k
    case index_hid:
836
127k
      if ((retval = read_byte(info, &ir, address + size)))
837
126k
        size++;
838
127k
      break;
839
840
0
    case illgl_hid:
841
472k
    case inh_hid:
842
481k
    case idxX0_hid:
843
482k
    case idxX0p_hid:
844
484k
    case opidx_hid:
845
484k
      retval = true;
846
484k
      break;
847
848
54.4k
    case idx09_hid:
849
54.4k
      sz = get_indexed09_post_byte_size(info, address + size);
850
54.4k
      if (sz >= 0)
851
42.4k
        size += sz;
852
11.9k
      else
853
11.9k
        retval = false;
854
54.4k
      break;
855
856
1.41k
    case idx12s_hid:
857
1.41k
      is_subset = true;
858
859
    // intentionally fall through
860
861
38.0k
    case idx12_hid:
862
38.0k
      sz = get_indexed12_post_byte_size(info,
863
38.0k
          address + size, is_subset);
864
38.0k
      if (sz >= 0)
865
36.7k
        size += sz;
866
1.24k
      else
867
1.24k
        retval = false;
868
38.0k
      break;
869
870
3.14k
    case exti12x_hid:
871
6.52k
    case imm16i12x_hid:
872
6.52k
      sz = get_indexed12_post_byte_size(info,
873
6.52k
          address + size, false);
874
6.52k
      if (sz >= 0) {
875
6.49k
        size += sz;
876
6.49k
        if ((retval = read_byte(info, &ir,
877
6.49k
            address + size + 1)))
878
6.43k
          size += 2;
879
6.49k
      } else
880
29
        retval = false;
881
6.52k
      break;
882
883
4.45k
    case imm8i12x_hid:
884
4.45k
      sz = get_indexed12_post_byte_size(info,
885
4.45k
          address + size, false);
886
4.45k
      if (sz >= 0) {
887
4.44k
        size += sz;
888
4.44k
        if ((retval = read_byte(info, &ir,
889
4.44k
            address + size)))
890
4.39k
          size++;
891
4.44k
      } else
892
15
        retval = false;
893
4.45k
      break;
894
895
2.12k
    case tfm_hid:
896
2.12k
      if ((retval = read_byte(info, &ir, address + size))) {
897
2.11k
        size++;
898
2.11k
        retval = is_tfm_reg_valid(info, (ir >> 4) & 0x0F) &&
899
1.79k
          is_tfm_reg_valid(info, ir & 0x0F);
900
2.11k
      }
901
2.12k
      break;
902
903
3.97k
    case rr09_hid:
904
3.97k
      if ((retval = read_byte(info, &ir, address + size))) {
905
3.95k
        size++;
906
3.95k
        retval = is_tfr09_reg_valid(info, (ir >> 4) & 0x0F) &&
907
3.50k
          is_tfr09_reg_valid(info, ir & 0x0F);
908
3.95k
      }
909
3.97k
      break;
910
911
2.82k
    case rr12_hid:
912
2.82k
      if ((retval = read_byte(info, &ir, address + size))) {
913
2.81k
        size++;
914
2.81k
        retval = is_exg_tfr12_post_byte_valid(info, ir);
915
2.81k
      }
916
2.82k
      break;
917
918
1.27k
    case bitmv_hid:
919
1.27k
      sz = get_bitmv_post_byte_size(info, address + size);
920
1.27k
      if (sz >= 0)
921
572
        size += sz;
922
707
      else
923
707
        retval = false;
924
1.27k
      break;
925
926
3.83k
    case loop_hid:
927
3.83k
      sz = get_loop_post_byte_size(info, address + size);
928
3.83k
      if (sz >= 0)
929
1.72k
        size += sz;
930
2.10k
      else
931
2.10k
        retval = false;
932
3.83k
      break;
933
934
0
    default:
935
0
      CS_ASSERT(0 && "Unexpected instruction handler id");
936
0
      retval = false;
937
0
      break;
938
802k
    }
939
940
802k
    if (!retval)
941
18.9k
      return false;
942
802k
  }
943
944
391k
  insn_description->insn_size += size;
945
946
391k
  return retval;
947
410k
}
948
949
// Check for a valid M680X instruction AND for enough bytes in the code buffer
950
// Return an instruction description in insn_desc.
951
static bool decode_insn(const m680x_info *info, uint16_t address,
952
  insn_desc *insn_description)
953
438k
{
954
438k
  const inst_pageX *inst_table = NULL;
955
438k
  const cpu_tables *cpu = info->cpu;
956
438k
  size_t table_size = 0;
957
438k
  uint16_t base_address = address;
958
438k
  uint8_t ir; // instruction register
959
438k
  int i;
960
438k
  int index;
961
962
438k
  if (!read_byte(info, &ir, address++))
963
0
    return false;
964
965
438k
  insn_description->insn = M680X_INS_ILLGL;
966
438k
  insn_description->opcode = ir;
967
968
  // Check if a page prefix byte is present
969
1.05M
  for (i = 0; i < ARR_SIZE(cpu->pageX_table_size); ++i) {
970
1.04M
    if (cpu->pageX_table_size[i] == 0 ||
971
660k
      (cpu->inst_pageX_table[i] == NULL))
972
380k
      break;
973
974
660k
    if ((cpu->pageX_prefix[i] == ir)) {
975
      // Get pageX instruction and handler id.
976
      // Abort for illegal instr.
977
39.3k
      inst_table = cpu->inst_pageX_table[i];
978
39.3k
      table_size = cpu->pageX_table_size[i];
979
980
39.3k
      if (!read_byte(info, &ir, address++))
981
68
        return false;
982
983
39.3k
      insn_description->opcode =
984
39.3k
        (insn_description->opcode << 8) | ir;
985
986
39.3k
      if ((index = binary_search(inst_table, table_size,
987
39.3k
        ir)) < 0)
988
9.26k
        return false;
989
990
30.0k
      insn_description->hid[0] =
991
30.0k
        inst_table[index].handler_id1;
992
30.0k
      insn_description->hid[1] =
993
30.0k
        inst_table[index].handler_id2;
994
30.0k
      insn_description->insn = inst_table[index].insn;
995
30.0k
      break;
996
39.3k
    }
997
660k
  }
998
999
428k
  if (insn_description->insn == M680X_INS_ILLGL) {
1000
    // Get page1 insn description
1001
398k
    insn_description->insn = cpu->inst_page1_table[ir].insn;
1002
398k
    insn_description->hid[0] =
1003
398k
      cpu->inst_page1_table[ir].handler_id1;
1004
398k
    insn_description->hid[1] =
1005
398k
      cpu->inst_page1_table[ir].handler_id2;
1006
398k
  }
1007
1008
428k
  if (insn_description->insn == M680X_INS_ILLGL) {
1009
    // Check if opcode byte is present in an overlay table
1010
56.4k
    for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
1011
54.8k
      if (cpu->overlay_table_size[i] == 0 ||
1012
38.2k
        (cpu->inst_overlay_table[i] == NULL))
1013
16.6k
        break;
1014
1015
38.2k
      inst_table = cpu->inst_overlay_table[i];
1016
38.2k
      table_size = cpu->overlay_table_size[i];
1017
1018
38.2k
      if ((index = binary_search(inst_table, table_size,
1019
38.2k
              ir)) >= 0) {
1020
19.7k
        insn_description->hid[0] =
1021
19.7k
          inst_table[index].handler_id1;
1022
19.7k
        insn_description->hid[1] =
1023
19.7k
          inst_table[index].handler_id2;
1024
19.7k
        insn_description->insn = inst_table[index].insn;
1025
19.7k
        break;
1026
19.7k
      }
1027
38.2k
    }
1028
38.0k
  }
1029
1030
428k
  insn_description->insn_size = address - base_address;
1031
1032
428k
  return (insn_description->insn != M680X_INS_ILLGL) &&
1033
410k
    (insn_description->insn != M680X_INS_INVLD) &&
1034
410k
    is_sufficient_code_size(info, address, insn_description);
1035
438k
}
1036
1037
static void illegal_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1038
46.4k
{
1039
46.4k
  cs_m680x_op *op0 = &info->m680x.operands[info->m680x.op_count++];
1040
46.4k
  uint8_t temp8 = 0;
1041
1042
46.4k
  info->insn = M680X_INS_ILLGL;
1043
46.4k
  read_byte(info, &temp8, (*address)++);
1044
46.4k
  op0->imm = (int32_t)temp8 & 0xff;
1045
46.4k
  op0->type = M680X_OP_IMMEDIATE;
1046
46.4k
  op0->size = 1;
1047
46.4k
}
1048
1049
static void inherent_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1050
472k
{
1051
  // There is nothing to do here :-)
1052
472k
}
1053
1054
static void add_reg_operand(m680x_info *info, m680x_reg reg)
1055
248k
{
1056
248k
  cs_m680x *m680x = &info->m680x;
1057
248k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1058
1059
248k
  op->type = M680X_OP_REGISTER;
1060
248k
  op->reg = reg;
1061
248k
  op->size = info->cpu->reg_byte_size[reg];
1062
248k
}
1063
1064
static void set_operand_size(m680x_info *info, cs_m680x_op *op,
1065
  uint8_t default_size)
1066
277k
{
1067
277k
  cs_m680x *m680x = &info->m680x;
1068
1069
277k
  if (info->insn == M680X_INS_JMP || info->insn == M680X_INS_JSR)
1070
14.1k
    op->size = 0;
1071
262k
  else if (info->insn == M680X_INS_DIVD ||
1072
260k
    ((info->insn == M680X_INS_AIS || info->insn == M680X_INS_AIX) &&
1073
1.10k
      op->type != M680X_OP_REGISTER))
1074
3.13k
    op->size = 1;
1075
259k
  else if (info->insn == M680X_INS_DIVQ ||
1076
257k
    info->insn == M680X_INS_MOVW)
1077
13.2k
    op->size = 2;
1078
246k
  else if (info->insn == M680X_INS_EMACS)
1079
422
    op->size = 4;
1080
246k
  else if ((m680x->op_count > 0) &&
1081
246k
    (m680x->operands[0].type == M680X_OP_REGISTER))
1082
150k
    op->size = m680x->operands[0].size;
1083
95.8k
  else
1084
95.8k
    op->size = default_size;
1085
277k
}
1086
1087
static const m680x_reg reg_s_reg_ids[] = {
1088
  M680X_REG_CC, M680X_REG_A, M680X_REG_B, M680X_REG_DP,
1089
  M680X_REG_X,  M680X_REG_Y, M680X_REG_U, M680X_REG_PC,
1090
};
1091
1092
static const m680x_reg reg_u_reg_ids[] = {
1093
  M680X_REG_CC, M680X_REG_A, M680X_REG_B, M680X_REG_DP,
1094
  M680X_REG_X,  M680X_REG_Y, M680X_REG_S, M680X_REG_PC,
1095
};
1096
1097
static void reg_bits_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1098
3.39k
{
1099
3.39k
  cs_m680x_op *op0 = &info->m680x.operands[0];
1100
3.39k
  uint8_t reg_bits = 0;
1101
3.39k
  uint16_t bit_index;
1102
3.39k
  const m680x_reg *reg_to_reg_ids = NULL;
1103
1104
3.39k
  read_byte(info, &reg_bits, (*address)++);
1105
1106
3.39k
  switch (op0->reg) {
1107
1.47k
  case M680X_REG_U:
1108
1.47k
    reg_to_reg_ids = &reg_u_reg_ids[0];
1109
1.47k
    break;
1110
1111
1.92k
  case M680X_REG_S:
1112
1.92k
    reg_to_reg_ids = &reg_s_reg_ids[0];
1113
1.92k
    break;
1114
1115
0
  default:
1116
0
    CS_ASSERT(0 && "Unexpected operand0 register");
1117
0
    break;
1118
3.39k
  }
1119
1120
3.39k
  if ((info->insn == M680X_INS_PULU ||
1121
2.44k
      (info->insn == M680X_INS_PULS)) &&
1122
2.20k
    ((reg_bits & 0x80) != 0))
1123
    // PULS xxx,PC or PULU xxx,PC which is like return from
1124
    // subroutine (RTS)
1125
300
    add_insn_group(MI->flat_insn->detail, M680X_GRP_RET);
1126
1127
30.5k
  for (bit_index = 0; bit_index < 8; ++bit_index) {
1128
27.1k
    if (reg_bits & (1 << bit_index))
1129
12.5k
      add_reg_operand(info, reg_to_reg_ids[bit_index]);
1130
27.1k
  }
1131
3.39k
}
1132
1133
static const m680x_reg g_tfr_exg_reg_ids[] = {
1134
  /* 16-bit registers */
1135
  M680X_REG_D, M680X_REG_X,  M680X_REG_Y,  M680X_REG_U,
1136
  M680X_REG_S, M680X_REG_PC, M680X_REG_W,  M680X_REG_V,
1137
  /* 8-bit registers */
1138
  M680X_REG_A, M680X_REG_B,  M680X_REG_CC, M680X_REG_DP,
1139
  M680X_REG_0, M680X_REG_0,  M680X_REG_E,  M680X_REG_F,
1140
};
1141
1142
static void reg_reg09_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1143
3.05k
{
1144
3.05k
  uint8_t regs = 0;
1145
1146
3.05k
  read_byte(info, &regs, (*address)++);
1147
1148
3.05k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs >> 4]);
1149
3.05k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs & 0x0f]);
1150
1151
3.05k
  if ((regs & 0x0f) == 0x05) {
1152
    // EXG xxx,PC or TFR xxx,PC which is like a JMP
1153
445
    add_insn_group(MI->flat_insn->detail, M680X_GRP_JUMP);
1154
445
  }
1155
3.05k
}
1156
1157
1158
static void reg_reg12_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1159
2.70k
{
1160
2.70k
  static const m680x_reg g_tfr_exg12_reg0_ids[] = {
1161
2.70k
    M680X_REG_A, M680X_REG_B,  M680X_REG_CC,  M680X_REG_TMP3,
1162
2.70k
    M680X_REG_D, M680X_REG_X, M680X_REG_Y,  M680X_REG_S,
1163
2.70k
  };
1164
2.70k
  static const m680x_reg g_tfr_exg12_reg1_ids[] = {
1165
2.70k
    M680X_REG_A, M680X_REG_B,  M680X_REG_CC,  M680X_REG_TMP2,
1166
2.70k
    M680X_REG_D, M680X_REG_X, M680X_REG_Y,  M680X_REG_S,
1167
2.70k
  };
1168
2.70k
  uint8_t regs = 0;
1169
1170
2.70k
  read_byte(info, &regs, (*address)++);
1171
1172
  // The opcode of this instruction depends on
1173
  // the msb of its post byte.
1174
2.70k
  if (regs & 0x80)
1175
1.51k
    info->insn = M680X_INS_EXG;
1176
1.18k
  else
1177
1.18k
    info->insn = M680X_INS_TFR;
1178
1179
2.70k
  add_reg_operand(info, g_tfr_exg12_reg0_ids[(regs >> 4) & 0x07]);
1180
2.70k
  add_reg_operand(info, g_tfr_exg12_reg1_ids[regs & 0x07]);
1181
2.70k
}
1182
1183
static void add_rel_operand(m680x_info *info, int16_t offset, uint16_t address)
1184
34.5k
{
1185
34.5k
  cs_m680x *m680x = &info->m680x;
1186
34.5k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1187
1188
34.5k
  op->type = M680X_OP_RELATIVE;
1189
34.5k
  op->size = 0;
1190
34.5k
  op->rel.offset = offset;
1191
34.5k
  op->rel.address = address;
1192
34.5k
}
1193
1194
static void relative8_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1195
32.0k
{
1196
32.0k
  int16_t offset = 0;
1197
1198
32.0k
  read_byte_sign_extended(info, &offset, (*address)++);
1199
32.0k
  add_rel_operand(info, offset, *address + offset);
1200
32.0k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1201
1202
32.0k
  if ((info->insn != M680X_INS_BRA) &&
1203
28.7k
    (info->insn != M680X_INS_BSR) &&
1204
26.8k
    (info->insn != M680X_INS_BRN))
1205
25.5k
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1206
32.0k
}
1207
1208
static void relative16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1209
2.42k
{
1210
2.42k
  uint16_t offset = 0;
1211
1212
2.42k
  read_word(info, &offset, *address);
1213
2.42k
  *address += 2;
1214
2.42k
  add_rel_operand(info, (int16_t)offset, *address + offset);
1215
2.42k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1216
1217
2.42k
  if ((info->insn != M680X_INS_LBRA) &&
1218
1.67k
    (info->insn != M680X_INS_LBSR) &&
1219
952
    (info->insn != M680X_INS_LBRN))
1220
547
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1221
2.42k
}
1222
1223
static const m680x_reg g_rr5_to_reg_ids[] = {
1224
  M680X_REG_X, M680X_REG_Y, M680X_REG_U, M680X_REG_S,
1225
};
1226
1227
static void add_indexed_operand(m680x_info *info, m680x_reg base_reg,
1228
  bool post_inc_dec, uint8_t inc_dec, uint8_t offset_bits,
1229
  uint16_t offset, bool no_comma)
1230
27.4k
{
1231
27.4k
  cs_m680x *m680x = &info->m680x;
1232
27.4k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1233
1234
27.4k
  op->type = M680X_OP_INDEXED;
1235
27.4k
  set_operand_size(info, op, 1);
1236
27.4k
  op->idx.base_reg = base_reg;
1237
27.4k
  op->idx.offset_reg = M680X_REG_INVALID;
1238
27.4k
  op->idx.inc_dec = inc_dec;
1239
1240
27.4k
  if (inc_dec && post_inc_dec)
1241
4.86k
    op->idx.flags |= M680X_IDX_POST_INC_DEC;
1242
1243
27.4k
  if (offset_bits != M680X_OFFSET_NONE) {
1244
14.5k
    op->idx.offset = offset;
1245
14.5k
    op->idx.offset_addr = 0;
1246
14.5k
  }
1247
1248
27.4k
  op->idx.offset_bits = offset_bits;
1249
27.4k
  op->idx.flags |= (no_comma ? M680X_IDX_NO_COMMA : 0);
1250
27.4k
}
1251
1252
// M6800/1/2/3 indexed mode handler
1253
static void indexedX_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1254
5.44k
{
1255
5.44k
  uint8_t offset = 0;
1256
1257
5.44k
  read_byte(info, &offset, (*address)++);
1258
1259
5.44k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_8,
1260
5.44k
    (uint16_t)offset, false);
1261
5.44k
}
1262
1263
static void indexedY_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1264
3.02k
{
1265
3.02k
  uint8_t offset = 0;
1266
1267
3.02k
  read_byte(info, &offset, (*address)++);
1268
1269
3.02k
  add_indexed_operand(info, M680X_REG_Y, false, 0, M680X_OFFSET_BITS_8,
1270
3.02k
    (uint16_t)offset, false);
1271
3.02k
}
1272
1273
// M6809/M6309 indexed mode handler
1274
static void indexed09_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1275
42.4k
{
1276
42.4k
  cs_m680x *m680x = &info->m680x;
1277
42.4k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1278
42.4k
  uint8_t post_byte = 0;
1279
42.4k
  uint16_t offset = 0;
1280
42.4k
  int16_t soffset = 0;
1281
1282
42.4k
  read_byte(info, &post_byte, (*address)++);
1283
1284
42.4k
  op->type = M680X_OP_INDEXED;
1285
42.4k
  set_operand_size(info, op, 1);
1286
42.4k
  op->idx.base_reg = g_rr5_to_reg_ids[(post_byte >> 5) & 0x03];
1287
42.4k
  op->idx.offset_reg = M680X_REG_INVALID;
1288
1289
42.4k
  if (!(post_byte & 0x80)) {
1290
    // n5,R
1291
20.4k
    if ((post_byte & 0x10) == 0x10)
1292
8.78k
      op->idx.offset = post_byte | 0xfff0;
1293
11.6k
    else
1294
11.6k
      op->idx.offset = post_byte & 0x0f;
1295
1296
20.4k
    op->idx.offset_addr = op->idx.offset + *address;
1297
20.4k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1298
20.4k
  }
1299
22.0k
  else {
1300
22.0k
    if ((post_byte & 0x10) == 0x10)
1301
8.69k
      op->idx.flags |= M680X_IDX_INDIRECT;
1302
1303
    // indexed addressing
1304
22.0k
    switch (post_byte & 0x1f) {
1305
1.23k
    case 0x00: // ,R+
1306
1.23k
      op->idx.inc_dec = 1;
1307
1.23k
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1308
1.23k
      break;
1309
1310
620
    case 0x11: // [,R++]
1311
1.44k
    case 0x01: // ,R++
1312
1.44k
      op->idx.inc_dec = 2;
1313
1.44k
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1314
1.44k
      break;
1315
1316
923
    case 0x02: // ,-R
1317
923
      op->idx.inc_dec = -1;
1318
923
      break;
1319
1320
1.71k
    case 0x13: // [,--R]
1321
2.55k
    case 0x03: // ,--R
1322
2.55k
      op->idx.inc_dec = -2;
1323
2.55k
      break;
1324
1325
565
    case 0x14: // [,R]
1326
1.61k
    case 0x04: // ,R
1327
1.61k
      break;
1328
1329
555
    case 0x15: // [B,R]
1330
1.61k
    case 0x05: // B,R
1331
1.61k
      op->idx.offset_reg = M680X_REG_B;
1332
1.61k
      break;
1333
1334
507
    case 0x16: // [A,R]
1335
1.46k
    case 0x06: // A,R
1336
1.46k
      op->idx.offset_reg = M680X_REG_A;
1337
1.46k
      break;
1338
1339
586
    case 0x1c: // [n8,PCR]
1340
2.28k
    case 0x0c: // n8,PCR
1341
2.28k
      op->idx.base_reg = M680X_REG_PC;
1342
2.28k
      read_byte_sign_extended(info, &soffset, (*address)++);
1343
2.28k
      op->idx.offset_addr = offset + *address;
1344
2.28k
      op->idx.offset = soffset;
1345
2.28k
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1346
2.28k
      break;
1347
1348
716
    case 0x18: // [n8,R]
1349
1.79k
    case 0x08: // n8,R
1350
1.79k
      read_byte_sign_extended(info, &soffset, (*address)++);
1351
1.79k
      op->idx.offset = soffset;
1352
1.79k
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1353
1.79k
      break;
1354
1355
881
    case 0x1d: // [n16,PCR]
1356
2.95k
    case 0x0d: // n16,PCR
1357
2.95k
      op->idx.base_reg = M680X_REG_PC;
1358
2.95k
      read_word(info, &offset, *address);
1359
2.95k
      *address += 2;
1360
2.95k
      op->idx.offset_addr = offset + *address;
1361
2.95k
      op->idx.offset = (int16_t)offset;
1362
2.95k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1363
2.95k
      break;
1364
1365
794
    case 0x19: // [n16,R]
1366
1.56k
    case 0x09: // n16,R
1367
1.56k
      read_word(info, &offset, *address);
1368
1.56k
      *address += 2;
1369
1.56k
      op->idx.offset = (int16_t)offset;
1370
1.56k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1371
1.56k
      break;
1372
1373
969
    case 0x1b: // [D,R]
1374
1.76k
    case 0x0b: // D,R
1375
1.76k
      op->idx.offset_reg = M680X_REG_D;
1376
1.76k
      break;
1377
1378
792
    case 0x1f: // [n16]
1379
792
      op->type = M680X_OP_EXTENDED;
1380
792
      op->ext.indirect = true;
1381
792
      read_word(info, &op->ext.address, *address);
1382
792
      *address += 2;
1383
792
      break;
1384
1385
0
    default:
1386
0
      op->idx.base_reg = M680X_REG_INVALID;
1387
0
      break;
1388
22.0k
    }
1389
22.0k
  }
1390
1391
42.4k
  if (((info->insn == M680X_INS_LEAU) ||
1392
41.2k
      (info->insn == M680X_INS_LEAS) ||
1393
40.1k
      (info->insn == M680X_INS_LEAX) ||
1394
38.3k
      (info->insn == M680X_INS_LEAY)) &&
1395
5.47k
    (m680x->operands[0].reg == M680X_REG_X ||
1396
3.67k
      (m680x->operands[0].reg == M680X_REG_Y)))
1397
    // Only LEAX and LEAY modify CC register
1398
3.16k
    add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1399
42.4k
}
1400
1401
1402
static const m680x_reg g_idx12_to_reg_ids[4] = {
1403
  M680X_REG_X, M680X_REG_Y, M680X_REG_S, M680X_REG_PC,
1404
};
1405
1406
static const m680x_reg g_or12_to_reg_ids[3] = {
1407
  M680X_REG_A, M680X_REG_B, M680X_REG_D
1408
};
1409
1410
// CPU12 indexed mode handler
1411
static void indexed12_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1412
47.5k
{
1413
47.5k
  cs_m680x *m680x = &info->m680x;
1414
47.5k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1415
47.5k
  uint8_t post_byte = 0;
1416
47.5k
  uint8_t offset8 = 0;
1417
1418
47.5k
  read_byte(info, &post_byte, (*address)++);
1419
1420
47.5k
  op->type = M680X_OP_INDEXED;
1421
47.5k
  set_operand_size(info, op, 1);
1422
47.5k
  op->idx.offset_reg = M680X_REG_INVALID;
1423
1424
47.5k
  if (!(post_byte & 0x20)) {
1425
    // n5,R      n5 is a 5-bit signed offset
1426
17.2k
    op->idx.base_reg = g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1427
1428
17.2k
    if ((post_byte & 0x10) == 0x10)
1429
6.10k
      op->idx.offset = post_byte | 0xfff0;
1430
11.1k
    else
1431
11.1k
      op->idx.offset = post_byte & 0x0f;
1432
1433
17.2k
    op->idx.offset_addr = op->idx.offset + *address;
1434
17.2k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1435
17.2k
  }
1436
30.3k
  else {
1437
30.3k
    if ((post_byte & 0xe0) == 0xe0)
1438
16.6k
      op->idx.base_reg =
1439
16.6k
        g_idx12_to_reg_ids[(post_byte >> 3) & 0x03];
1440
1441
30.3k
    switch (post_byte & 0xe7) {
1442
3.11k
    case 0xe0:
1443
6.29k
    case 0xe1: // n9,R
1444
6.29k
      read_byte(info, &offset8, (*address)++);
1445
6.29k
      op->idx.offset = offset8;
1446
1447
6.29k
      if (post_byte & 0x01) // sign extension
1448
3.18k
        op->idx.offset |= 0xff00;
1449
1450
6.29k
      op->idx.offset_bits = M680X_OFFSET_BITS_9;
1451
1452
6.29k
      if (op->idx.base_reg == M680X_REG_PC)
1453
2.35k
        op->idx.offset_addr = op->idx.offset + *address;
1454
1455
6.29k
      break;
1456
1457
2.77k
    case 0xe3: // [n16,R]
1458
2.77k
      op->idx.flags |= M680X_IDX_INDIRECT;
1459
1460
    // intentionally fall through
1461
5.13k
    case 0xe2: // n16,R
1462
5.13k
      read_word(info, (uint16_t *)&op->idx.offset, *address);
1463
5.13k
      (*address) += 2;
1464
5.13k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1465
1466
5.13k
      if (op->idx.base_reg == M680X_REG_PC)
1467
805
        op->idx.offset_addr = op->idx.offset + *address;
1468
1469
5.13k
      break;
1470
1471
1.27k
    case 0xe4: // A,R
1472
2.35k
    case 0xe5: // B,R
1473
3.37k
    case 0xe6: // D,R
1474
3.37k
      op->idx.offset_reg =
1475
3.37k
        g_or12_to_reg_ids[post_byte & 0x03];
1476
3.37k
      break;
1477
1478
1.79k
    case 0xe7: // [D,R]
1479
1.79k
      op->idx.offset_reg = M680X_REG_D;
1480
1.79k
      op->idx.flags |= M680X_IDX_INDIRECT;
1481
1.79k
      break;
1482
1483
13.7k
    default: // n,-r n,+r n,r- n,r+
1484
      // PC is not allowed in this mode
1485
13.7k
      op->idx.base_reg =
1486
13.7k
        g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1487
13.7k
      op->idx.inc_dec = post_byte & 0x0f;
1488
1489
13.7k
      if (op->idx.inc_dec & 0x08) // evtl. sign extend value
1490
6.21k
        op->idx.inc_dec |= 0xf0;
1491
1492
13.7k
      if (op->idx.inc_dec >= 0)
1493
7.54k
        op->idx.inc_dec++;
1494
1495
13.7k
      if (post_byte & 0x10)
1496
3.60k
        op->idx.flags |= M680X_IDX_POST_INC_DEC;
1497
1498
13.7k
      break;
1499
1500
30.3k
    }
1501
30.3k
  }
1502
47.5k
}
1503
1504
static void index_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1505
1.41k
{
1506
1.41k
  cs_m680x *m680x = &info->m680x;
1507
1.41k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1508
1509
1.41k
  op->type = M680X_OP_CONSTANT;
1510
1.41k
  read_byte(info, &op->const_val, (*address)++);
1511
1.41k
};
1512
1513
static void direct_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1514
58.9k
{
1515
58.9k
  cs_m680x *m680x = &info->m680x;
1516
58.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1517
1518
58.9k
  op->type = M680X_OP_DIRECT;
1519
58.9k
  set_operand_size(info, op, 1);
1520
58.9k
  read_byte(info, &op->direct_addr, (*address)++);
1521
58.9k
};
1522
1523
static void extended_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1524
52.9k
{
1525
52.9k
  cs_m680x *m680x = &info->m680x;
1526
52.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1527
1528
52.9k
  op->type = M680X_OP_EXTENDED;
1529
52.9k
  set_operand_size(info, op, 1);
1530
52.9k
  read_word(info, &op->ext.address, *address);
1531
52.9k
  *address += 2;
1532
52.9k
}
1533
1534
static void immediate_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1535
36.9k
{
1536
36.9k
  cs_m680x *m680x = &info->m680x;
1537
36.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1538
36.9k
  uint16_t word = 0;
1539
36.9k
  int16_t sword = 0;
1540
1541
36.9k
  op->type = M680X_OP_IMMEDIATE;
1542
36.9k
  set_operand_size(info, op, 1);
1543
1544
36.9k
  switch (op->size) {
1545
29.5k
  case 1:
1546
29.5k
    read_byte_sign_extended(info, &sword, *address);
1547
29.5k
    op->imm = sword;
1548
29.5k
    break;
1549
1550
6.46k
  case 2:
1551
6.46k
    read_word(info, &word, *address);
1552
6.46k
    op->imm = (int16_t)word;
1553
6.46k
    break;
1554
1555
913
  case 4:
1556
913
    read_sdword(info, &op->imm, *address);
1557
913
    break;
1558
1559
0
  default:
1560
0
    op->imm = 0;
1561
0
    CS_ASSERT(0 && "Unexpected immediate byte size");
1562
36.9k
  }
1563
1564
36.9k
  *address += op->size;
1565
36.9k
}
1566
1567
// handler for bit move instructions, e.g: BAND A,5,1,$40  Used by HD6309
1568
static void bit_move_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1569
572
{
1570
572
  static const m680x_reg m680x_reg[] = {
1571
572
    M680X_REG_CC, M680X_REG_A, M680X_REG_B, M680X_REG_INVALID,
1572
572
  };
1573
1574
572
  uint8_t post_byte = 0;
1575
572
  cs_m680x *m680x = &info->m680x;
1576
572
  cs_m680x_op *op;
1577
1578
572
  read_byte(info, &post_byte, *address);
1579
572
  (*address)++;
1580
1581
  // operand[0] = register
1582
572
  add_reg_operand(info, m680x_reg[post_byte >> 6]);
1583
1584
  // operand[1] = bit index in source operand
1585
572
  op = &m680x->operands[m680x->op_count++];
1586
572
  op->type = M680X_OP_CONSTANT;
1587
572
  op->const_val = (post_byte >> 3) & 0x07;
1588
1589
  // operand[2] = bit index in destination operand
1590
572
  op = &m680x->operands[m680x->op_count++];
1591
572
  op->type = M680X_OP_CONSTANT;
1592
572
  op->const_val = post_byte & 0x07;
1593
1594
572
  direct_hdlr(MI, info, address);
1595
572
}
1596
1597
// handler for TFM instruction, e.g: TFM X+,Y+  Used by HD6309
1598
static void tfm_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1599
1.69k
{
1600
1.69k
  static const uint8_t inc_dec_r0[] = {
1601
1.69k
    1, -1, 1, 0,
1602
1.69k
  };
1603
1.69k
  static const uint8_t inc_dec_r1[] = {
1604
1.69k
    1, -1, 0, 1,
1605
1.69k
  };
1606
1.69k
  uint8_t regs = 0;
1607
1.69k
  uint8_t index = (MI->Opcode & 0xff) - 0x38;
1608
1609
1.69k
  read_byte(info, &regs, *address);
1610
1611
1.69k
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs >> 4], true,
1612
1.69k
    inc_dec_r0[index], M680X_OFFSET_NONE, 0, true);
1613
1.69k
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs & 0x0f], true,
1614
1.69k
    inc_dec_r1[index], M680X_OFFSET_NONE, 0, true);
1615
1616
1.69k
  add_reg_to_rw_list(MI, M680X_REG_W, READ | WRITE);
1617
1.69k
}
1618
1619
static void opidx_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1620
2.57k
{
1621
2.57k
  cs_m680x *m680x = &info->m680x;
1622
2.57k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1623
1624
  // bit index is coded in Opcode
1625
2.57k
  op->type = M680X_OP_CONSTANT;
1626
2.57k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1627
2.57k
}
1628
1629
// handler for bit test and branch instruction. Used by M6805.
1630
// The bit index is part of the opcode.
1631
// Example: BRSET 3,<$40,LOOP
1632
static void opidx_dir_rel_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1633
3.60k
{
1634
3.60k
  cs_m680x *m680x = &info->m680x;
1635
3.60k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1636
1637
  // bit index is coded in Opcode
1638
3.60k
  op->type = M680X_OP_CONSTANT;
1639
3.60k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1640
3.60k
  direct_hdlr(MI, info, address);
1641
3.60k
  relative8_hdlr(MI, info, address);
1642
1643
3.60k
  add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1644
3.60k
}
1645
1646
static void indexedX0_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1647
8.48k
{
1648
8.48k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_NONE,
1649
8.48k
    0, false);
1650
8.48k
}
1651
1652
static void indexedX16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1653
2.33k
{
1654
2.33k
  uint16_t offset = 0;
1655
1656
2.33k
  read_word(info, &offset, *address);
1657
2.33k
  *address += 2;
1658
2.33k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_16,
1659
2.33k
    offset, false);
1660
2.33k
}
1661
1662
static void imm_rel_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1663
2.79k
{
1664
2.79k
  immediate_hdlr(MI, info, address);
1665
2.79k
  relative8_hdlr(MI, info, address);
1666
2.79k
}
1667
1668
static void indexedS_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1669
993
{
1670
993
  uint8_t offset = 0;
1671
1672
993
  read_byte(info, &offset, (*address)++);
1673
1674
993
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_8,
1675
993
    (uint16_t)offset, false);
1676
993
}
1677
1678
static void indexedS16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1679
808
{
1680
808
  uint16_t offset = 0;
1681
1682
808
  read_word(info, &offset, *address);
1683
808
  address += 2;
1684
1685
808
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_16,
1686
808
    offset, false);
1687
808
}
1688
1689
static void indexedX0p_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1690
1.04k
{
1691
1.04k
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_NONE,
1692
1.04k
    0, true);
1693
1.04k
}
1694
1695
static void indexedXp_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1696
1.95k
{
1697
1.95k
  uint8_t offset = 0;
1698
1699
1.95k
  read_byte(info, &offset, (*address)++);
1700
1701
1.95k
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_BITS_8,
1702
1.95k
    (uint16_t)offset, false);
1703
1.95k
}
1704
1705
static void imm_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1706
7.72k
{
1707
7.72k
  cs_m680x *m680x = &info->m680x;
1708
7.72k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1709
1710
7.72k
  indexed12_hdlr(MI, info, address);
1711
7.72k
  op->type = M680X_OP_IMMEDIATE;
1712
1713
7.72k
  if (info->insn == M680X_INS_MOVW) {
1714
3.32k
    uint16_t imm16 = 0;
1715
1716
3.32k
    read_word(info, &imm16, *address);
1717
3.32k
    op->imm = (int16_t)imm16;
1718
3.32k
    op->size = 2;
1719
3.32k
  }
1720
4.39k
  else {
1721
4.39k
    uint8_t imm8 = 0;
1722
1723
4.39k
    read_byte(info, &imm8, *address);
1724
4.39k
    op->imm = (int8_t)imm8;
1725
4.39k
    op->size = 1;
1726
4.39k
  }
1727
1728
7.72k
  set_operand_size(info, op, 1);
1729
7.72k
}
1730
1731
static void ext_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1732
3.10k
{
1733
3.10k
  cs_m680x *m680x = &info->m680x;
1734
3.10k
  cs_m680x_op *op0 = &m680x->operands[m680x->op_count++];
1735
3.10k
  uint16_t imm16 = 0;
1736
1737
3.10k
  indexed12_hdlr(MI, info, address);
1738
3.10k
  read_word(info, &imm16, *address);
1739
3.10k
  op0->type = M680X_OP_EXTENDED;
1740
3.10k
  op0->ext.address = (int16_t)imm16;
1741
3.10k
  set_operand_size(info, op0, 1);
1742
3.10k
}
1743
1744
// handler for CPU12 DBEQ/DNBE/IBEQ/IBNE/TBEQ/TBNE instructions.
1745
// Example: DBNE X,$1000
1746
static void loop_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1747
1.72k
{
1748
1.72k
  static const m680x_reg index_to_reg_id[] = {
1749
1.72k
    M680X_REG_A, M680X_REG_B, M680X_REG_INVALID, M680X_REG_INVALID,
1750
1.72k
    M680X_REG_D, M680X_REG_X, M680X_REG_Y, M680X_REG_S,
1751
1.72k
  };
1752
1.72k
  static const m680x_insn index_to_insn_id[] = {
1753
1.72k
    M680X_INS_DBEQ, M680X_INS_DBNE, M680X_INS_TBEQ, M680X_INS_TBNE,
1754
1.72k
    M680X_INS_IBEQ, M680X_INS_IBNE, M680X_INS_ILLGL, M680X_INS_ILLGL
1755
1.72k
  };
1756
1.72k
  cs_m680x *m680x = &info->m680x;
1757
1.72k
  uint8_t post_byte = 0;
1758
1.72k
  uint8_t rel = 0;
1759
1.72k
  cs_m680x_op *op;
1760
1761
1.72k
  read_byte(info, &post_byte, (*address)++);
1762
1763
1.72k
  info->insn = index_to_insn_id[(post_byte >> 5) & 0x07];
1764
1765
1.72k
  if (info->insn == M680X_INS_ILLGL) {
1766
0
    illegal_hdlr(MI, info, address);
1767
0
  };
1768
1769
1.72k
  read_byte(info, &rel, (*address)++);
1770
1771
1.72k
  add_reg_operand(info, index_to_reg_id[post_byte & 0x07]);
1772
1773
1.72k
  op = &m680x->operands[m680x->op_count++];
1774
1775
1.72k
  op->type = M680X_OP_RELATIVE;
1776
1777
1.72k
  op->rel.offset = (post_byte & 0x10) ? 0xff00 | rel : rel;
1778
1779
1.72k
  op->rel.address = *address + op->rel.offset;
1780
1781
1.72k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1782
1.72k
}
1783
1784
static void (*const g_insn_handler[])(MCInst *, m680x_info *, uint16_t *) = {
1785
  illegal_hdlr,
1786
  relative8_hdlr,
1787
  relative16_hdlr,
1788
  immediate_hdlr, // 8-bit
1789
  immediate_hdlr, // 16-bit
1790
  immediate_hdlr, // 32-bit
1791
  direct_hdlr,
1792
  extended_hdlr,
1793
  indexedX_hdlr,
1794
  indexedY_hdlr,
1795
  indexed09_hdlr,
1796
  inherent_hdlr,
1797
  reg_reg09_hdlr,
1798
  reg_bits_hdlr,
1799
  bit_move_hdlr,
1800
  tfm_hdlr,
1801
  opidx_hdlr,
1802
  opidx_dir_rel_hdlr,
1803
  indexedX0_hdlr,
1804
  indexedX16_hdlr,
1805
  imm_rel_hdlr,
1806
  indexedS_hdlr,
1807
  indexedS16_hdlr,
1808
  indexedXp_hdlr,
1809
  indexedX0p_hdlr,
1810
  indexed12_hdlr,
1811
  indexed12_hdlr, // subset of indexed12
1812
  reg_reg12_hdlr,
1813
  loop_hdlr,
1814
  index_hdlr,
1815
  imm_idx12_x_hdlr,
1816
  imm_idx12_x_hdlr,
1817
  ext_idx12_x_hdlr,
1818
}; /* handler function pointers */
1819
1820
/* Disasemble one instruction at address and store in str_buff */
1821
static unsigned int m680x_disassemble(MCInst *MI, m680x_info *info,
1822
  uint16_t address)
1823
438k
{
1824
438k
  cs_m680x *m680x = &info->m680x;
1825
438k
  cs_detail *detail = MI->flat_insn->detail;
1826
438k
  uint16_t base_address = address;
1827
438k
  insn_desc insn_description;
1828
438k
  e_access_mode access_mode;
1829
1830
438k
  if (detail != NULL) {
1831
438k
    memset(detail, 0, offsetof(cs_detail, m680x)+sizeof(cs_m680x));
1832
438k
  }
1833
1834
438k
  memset(&insn_description, 0, sizeof(insn_description));
1835
438k
  memset(m680x, 0, sizeof(*m680x));
1836
438k
  info->insn_size = 1;
1837
1838
438k
  if (decode_insn(info, address, &insn_description)) {
1839
391k
    m680x_reg reg;
1840
1841
391k
    if (insn_description.opcode > 0xff)
1842
27.5k
      address += 2; // 8-bit opcode + page prefix
1843
364k
    else
1844
364k
      address++; // 8-bit opcode only
1845
1846
391k
    info->insn = insn_description.insn;
1847
1848
391k
    MCInst_setOpcode(MI, insn_description.opcode);
1849
1850
391k
    reg = g_insn_props[info->insn].reg0;
1851
1852
391k
    if (reg != M680X_REG_INVALID) {
1853
205k
      if (reg == M680X_REG_HX &&
1854
1.73k
        (!info->cpu->reg_byte_size[reg]))
1855
467
        reg = M680X_REG_X;
1856
1857
205k
      add_reg_operand(info, reg);
1858
      // First (or second) operand is a register which is
1859
      // part of the mnemonic
1860
205k
      m680x->flags |= M680X_FIRST_OP_IN_MNEM;
1861
205k
      reg = g_insn_props[info->insn].reg1;
1862
1863
205k
      if (reg != M680X_REG_INVALID) {
1864
4.71k
        if (reg == M680X_REG_HX &&
1865
1.35k
          (!info->cpu->reg_byte_size[reg]))
1866
734
          reg = M680X_REG_X;
1867
1868
4.71k
        add_reg_operand(info, reg);
1869
4.71k
        m680x->flags |= M680X_SECOND_OP_IN_MNEM;
1870
4.71k
      }
1871
205k
    }
1872
1873
    // Call addressing mode specific instruction handler
1874
391k
    (g_insn_handler[insn_description.hid[0]])(MI, info,
1875
391k
      &address);
1876
391k
    (g_insn_handler[insn_description.hid[1]])(MI, info,
1877
391k
      &address);
1878
1879
391k
    add_insn_group(detail, g_insn_props[info->insn].group);
1880
1881
391k
    if (g_insn_props[info->insn].cc_modified &&
1882
246k
      (info->cpu->insn_cc_not_modified[0] != info->insn) &&
1883
245k
      (info->cpu->insn_cc_not_modified[1] != info->insn))
1884
244k
      add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1885
1886
391k
    access_mode = g_insn_props[info->insn].access_mode;
1887
1888
    // Fix for M6805 BSET/BCLR. It has a differnt operand order
1889
    // in comparison to the M6811
1890
391k
    if ((info->cpu->insn_cc_not_modified[0] == info->insn) ||
1891
390k
      (info->cpu->insn_cc_not_modified[1] == info->insn))
1892
2.57k
      access_mode = rmmm;
1893
1894
391k
    build_regs_read_write_counts(MI, info, access_mode);
1895
391k
    add_operators_access(MI, info, access_mode);
1896
1897
391k
    if (g_insn_props[info->insn].update_reg_access)
1898
39.4k
      set_changed_regs_read_write_counts(MI, info);
1899
1900
391k
    info->insn_size = (uint8_t)insn_description.insn_size;
1901
1902
391k
    return info->insn_size;
1903
391k
  }
1904
46.4k
  else
1905
46.4k
    MCInst_setOpcode(MI, insn_description.opcode);
1906
1907
  // Illegal instruction
1908
46.4k
  address = base_address;
1909
46.4k
  illegal_hdlr(MI, info, &address);
1910
46.4k
  return 1;
1911
438k
}
1912
1913
// Tables to get the byte size of a register on the CPU
1914
// based on an enum m680x_reg value.
1915
// Invalid registers return 0.
1916
static const uint8_t g_m6800_reg_byte_size[22] = {
1917
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1918
  0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0
1919
};
1920
1921
static const uint8_t g_m6805_reg_byte_size[22] = {
1922
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1923
  0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 2, 0, 0
1924
};
1925
1926
static const uint8_t g_m6808_reg_byte_size[22] = {
1927
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1928
  0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 1, 1, 0, 2, 0, 0, 0, 2, 0, 0
1929
};
1930
1931
static const uint8_t g_m6801_reg_byte_size[22] = {
1932
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1933
  0, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0
1934
};
1935
1936
static const uint8_t g_m6811_reg_byte_size[22] = {
1937
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1938
  0, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 0, 0
1939
};
1940
1941
static const uint8_t g_cpu12_reg_byte_size[22] = {
1942
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1943
  0, 1, 1, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 2, 2, 2
1944
};
1945
1946
static const uint8_t g_m6809_reg_byte_size[22] = {
1947
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1948
  0, 1, 1, 0, 0, 0, 2, 0, 1, 1, 0, 0, 0, 2, 2, 2, 2, 0, 0, 2, 0, 0
1949
};
1950
1951
static const uint8_t g_hd6309_reg_byte_size[22] = {
1952
  // A  B  E  F  0  D  W  CC DP MD HX H  X  Y  S  U  V  Q  PC T2 T3
1953
  0, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0, 0, 2, 2, 2, 2, 2, 4, 2, 0, 0
1954
};
1955
1956
// Table to check for a valid register nibble on the M6809 CPU
1957
// used for TFR and EXG instruction.
1958
static const bool m6809_tfr_reg_valid[16] = {
1959
  true, true, true, true, true,  true,  false, false,
1960
  true, true, true, true, false, false, false, false,
1961
};
1962
1963
static const cpu_tables g_cpu_tables[] = {
1964
  {
1965
    // M680X_CPU_TYPE_INVALID
1966
    NULL,
1967
    { NULL, NULL },
1968
    { 0, 0 },
1969
    { 0x00, 0x00, 0x00 },
1970
    { NULL, NULL, NULL },
1971
    { 0, 0, 0 },
1972
    NULL,
1973
    NULL,
1974
    { M680X_INS_INVLD, M680X_INS_INVLD }
1975
  },
1976
  {
1977
    // M680X_CPU_TYPE_6301
1978
    &g_m6800_inst_page1_table[0],
1979
    { &g_m6801_inst_overlay_table[0], &g_hd6301_inst_overlay_table[0] },
1980
    {
1981
      ARR_SIZE(g_m6801_inst_overlay_table),
1982
      ARR_SIZE(g_hd6301_inst_overlay_table)
1983
    },
1984
    { 0x00, 0x00, 0x00 },
1985
    { NULL, NULL, NULL },
1986
    { 0, 0, 0 },
1987
    &g_m6801_reg_byte_size[0],
1988
    NULL,
1989
    { M680X_INS_INVLD, M680X_INS_INVLD }
1990
  },
1991
  {
1992
    // M680X_CPU_TYPE_6309
1993
    &g_m6809_inst_page1_table[0],
1994
    { &g_hd6309_inst_overlay_table[0], NULL },
1995
    { ARR_SIZE(g_hd6309_inst_overlay_table), 0 },
1996
    { 0x10, 0x11, 0x00 },
1997
    { &g_hd6309_inst_page2_table[0], &g_hd6309_inst_page3_table[0], NULL },
1998
    {
1999
      ARR_SIZE(g_hd6309_inst_page2_table),
2000
      ARR_SIZE(g_hd6309_inst_page3_table),
2001
      0
2002
    },
2003
    &g_hd6309_reg_byte_size[0],
2004
    NULL,
2005
    { M680X_INS_INVLD, M680X_INS_INVLD }
2006
  },
2007
  {
2008
    // M680X_CPU_TYPE_6800
2009
    &g_m6800_inst_page1_table[0],
2010
    { NULL, NULL },
2011
    { 0, 0 },
2012
    { 0x00, 0x00, 0x00 },
2013
    { NULL, NULL, NULL },
2014
    { 0, 0, 0 },
2015
    &g_m6800_reg_byte_size[0],
2016
    NULL,
2017
    { M680X_INS_INVLD, M680X_INS_INVLD }
2018
  },
2019
  {
2020
    // M680X_CPU_TYPE_6801
2021
    &g_m6800_inst_page1_table[0],
2022
    { &g_m6801_inst_overlay_table[0], NULL },
2023
    { ARR_SIZE(g_m6801_inst_overlay_table), 0 },
2024
    { 0x00, 0x00, 0x00 },
2025
    { NULL, NULL, NULL },
2026
    { 0, 0, 0 },
2027
    &g_m6801_reg_byte_size[0],
2028
    NULL,
2029
    { M680X_INS_INVLD, M680X_INS_INVLD }
2030
  },
2031
  {
2032
    // M680X_CPU_TYPE_6805
2033
    &g_m6805_inst_page1_table[0],
2034
    { NULL, NULL },
2035
    { 0, 0 },
2036
    { 0x00, 0x00, 0x00 },
2037
    { NULL, NULL, NULL },
2038
    { 0, 0, 0 },
2039
    &g_m6805_reg_byte_size[0],
2040
    NULL,
2041
    { M680X_INS_BCLR, M680X_INS_BSET }
2042
  },
2043
  {
2044
    // M680X_CPU_TYPE_6808
2045
    &g_m6805_inst_page1_table[0],
2046
    { &g_m6808_inst_overlay_table[0], NULL },
2047
    { ARR_SIZE(g_m6808_inst_overlay_table), 0 },
2048
    { 0x9E, 0x00, 0x00 },
2049
    { &g_m6808_inst_page2_table[0], NULL, NULL },
2050
    { ARR_SIZE(g_m6808_inst_page2_table), 0, 0 },
2051
    &g_m6808_reg_byte_size[0],
2052
    NULL,
2053
    { M680X_INS_BCLR, M680X_INS_BSET }
2054
  },
2055
  {
2056
    // M680X_CPU_TYPE_6809
2057
    &g_m6809_inst_page1_table[0],
2058
    { NULL, NULL },
2059
    { 0, 0 },
2060
    { 0x10, 0x11, 0x00 },
2061
    {
2062
      &g_m6809_inst_page2_table[0],
2063
      &g_m6809_inst_page3_table[0],
2064
      NULL
2065
    },
2066
    {
2067
      ARR_SIZE(g_m6809_inst_page2_table),
2068
      ARR_SIZE(g_m6809_inst_page3_table),
2069
      0
2070
    },
2071
    &g_m6809_reg_byte_size[0],
2072
    &m6809_tfr_reg_valid[0],
2073
    { M680X_INS_INVLD, M680X_INS_INVLD }
2074
  },
2075
  {
2076
    // M680X_CPU_TYPE_6811
2077
    &g_m6800_inst_page1_table[0],
2078
    {
2079
      &g_m6801_inst_overlay_table[0],
2080
      &g_m6811_inst_overlay_table[0]
2081
    },
2082
    {
2083
      ARR_SIZE(g_m6801_inst_overlay_table),
2084
      ARR_SIZE(g_m6811_inst_overlay_table)
2085
    },
2086
    { 0x18, 0x1A, 0xCD },
2087
    {
2088
      &g_m6811_inst_page2_table[0],
2089
      &g_m6811_inst_page3_table[0],
2090
      &g_m6811_inst_page4_table[0]
2091
    },
2092
    {
2093
      ARR_SIZE(g_m6811_inst_page2_table),
2094
      ARR_SIZE(g_m6811_inst_page3_table),
2095
      ARR_SIZE(g_m6811_inst_page4_table)
2096
    },
2097
    &g_m6811_reg_byte_size[0],
2098
    NULL,
2099
    { M680X_INS_INVLD, M680X_INS_INVLD }
2100
  },
2101
  {
2102
    // M680X_CPU_TYPE_CPU12
2103
    &g_cpu12_inst_page1_table[0],
2104
    { NULL, NULL },
2105
    { 0, 0 },
2106
    { 0x18, 0x00, 0x00 },
2107
    { &g_cpu12_inst_page2_table[0], NULL, NULL },
2108
    { ARR_SIZE(g_cpu12_inst_page2_table), 0, 0 },
2109
    &g_cpu12_reg_byte_size[0],
2110
    NULL,
2111
    { M680X_INS_INVLD, M680X_INS_INVLD }
2112
  },
2113
  {
2114
    // M680X_CPU_TYPE_HCS08
2115
    &g_m6805_inst_page1_table[0],
2116
    {
2117
      &g_m6808_inst_overlay_table[0],
2118
      &g_hcs08_inst_overlay_table[0]
2119
    },
2120
    {
2121
      ARR_SIZE(g_m6808_inst_overlay_table),
2122
      ARR_SIZE(g_hcs08_inst_overlay_table)
2123
    },
2124
    { 0x9E, 0x00, 0x00 },
2125
    { &g_hcs08_inst_page2_table[0], NULL, NULL },
2126
    { ARR_SIZE(g_hcs08_inst_page2_table), 0, 0 },
2127
    &g_m6808_reg_byte_size[0],
2128
    NULL,
2129
    { M680X_INS_BCLR, M680X_INS_BSET }
2130
  },
2131
};
2132
2133
static bool m680x_setup_internals(m680x_info *info, e_cpu_type cpu_type,
2134
  uint16_t address,
2135
  const uint8_t *code, uint16_t code_len)
2136
438k
{
2137
438k
  if (cpu_type == M680X_CPU_TYPE_INVALID) {
2138
0
    return false;
2139
0
  }
2140
2141
438k
  info->code = code;
2142
438k
  info->size = code_len;
2143
438k
  info->offset = address;
2144
438k
  info->cpu_type = cpu_type;
2145
2146
438k
  info->cpu = &g_cpu_tables[info->cpu_type];
2147
2148
438k
  return true;
2149
438k
}
2150
2151
bool M680X_getInstruction(csh ud, const uint8_t *code, size_t code_len,
2152
  MCInst *MI, uint16_t *size, uint64_t address, void *inst_info)
2153
438k
{
2154
438k
  unsigned int insn_size = 0;
2155
438k
  e_cpu_type cpu_type = M680X_CPU_TYPE_INVALID; // No default CPU type
2156
438k
  cs_struct *handle = (cs_struct *)ud;
2157
438k
  m680x_info *info = (m680x_info *)handle->printer_info;
2158
2159
438k
  MCInst_clear(MI);
2160
2161
438k
  if (handle->mode & CS_MODE_M680X_6800)
2162
1.55k
    cpu_type = M680X_CPU_TYPE_6800;
2163
2164
436k
  else if (handle->mode & CS_MODE_M680X_6801)
2165
3.55k
    cpu_type = M680X_CPU_TYPE_6801;
2166
2167
433k
  else if (handle->mode & CS_MODE_M680X_6805)
2168
5.77k
    cpu_type = M680X_CPU_TYPE_6805;
2169
2170
427k
  else if (handle->mode & CS_MODE_M680X_6808)
2171
18.2k
    cpu_type = M680X_CPU_TYPE_6808;
2172
2173
409k
  else if (handle->mode & CS_MODE_M680X_HCS08)
2174
12.0k
    cpu_type = M680X_CPU_TYPE_HCS08;
2175
2176
396k
  else if (handle->mode & CS_MODE_M680X_6809)
2177
37.6k
    cpu_type = M680X_CPU_TYPE_6809;
2178
2179
359k
  else if (handle->mode & CS_MODE_M680X_6301)
2180
1.81k
    cpu_type = M680X_CPU_TYPE_6301;
2181
2182
357k
  else if (handle->mode & CS_MODE_M680X_6309)
2183
155k
    cpu_type = M680X_CPU_TYPE_6309;
2184
2185
202k
  else if (handle->mode & CS_MODE_M680X_6811)
2186
25.0k
    cpu_type = M680X_CPU_TYPE_6811;
2187
2188
177k
  else if (handle->mode & CS_MODE_M680X_CPU12)
2189
177k
    cpu_type = M680X_CPU_TYPE_CPU12;
2190
2191
438k
  if (cpu_type != M680X_CPU_TYPE_INVALID &&
2192
438k
    m680x_setup_internals(info, cpu_type, (uint16_t)address, code,
2193
438k
      (uint16_t)code_len))
2194
438k
    insn_size = m680x_disassemble(MI, info, (uint16_t)address);
2195
2196
438k
  if (insn_size == 0) {
2197
0
    *size = 1;
2198
0
    return false;
2199
0
  }
2200
2201
  // Make sure we always stay within range
2202
438k
  if (insn_size > code_len) {
2203
53
    *size = (uint16_t)code_len;
2204
53
    return false;
2205
53
  }
2206
438k
  else
2207
438k
    *size = (uint16_t)insn_size;
2208
2209
438k
  return true;
2210
438k
}
2211
2212
cs_err M680X_disassembler_init(cs_struct *ud)
2213
4.15k
{
2214
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6800_reg_byte_size)) {
2215
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6800_reg_byte_size));
2216
2217
0
    return CS_ERR_MODE;
2218
0
  }
2219
2220
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6801_reg_byte_size)) {
2221
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6801_reg_byte_size));
2222
2223
0
    return CS_ERR_MODE;
2224
0
  }
2225
2226
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6805_reg_byte_size)) {
2227
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6805_reg_byte_size));
2228
2229
0
    return CS_ERR_MODE;
2230
0
  }
2231
2232
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6808_reg_byte_size)) {
2233
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6808_reg_byte_size));
2234
2235
0
    return CS_ERR_MODE;
2236
0
  }
2237
2238
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6811_reg_byte_size)) {
2239
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6811_reg_byte_size));
2240
2241
0
    return CS_ERR_MODE;
2242
0
  }
2243
2244
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_cpu12_reg_byte_size)) {
2245
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_cpu12_reg_byte_size));
2246
2247
0
    return CS_ERR_MODE;
2248
0
  }
2249
2250
4.15k
  if (M680X_REG_ENDING != ARR_SIZE(g_m6809_reg_byte_size)) {
2251
0
    CS_ASSERT(M680X_REG_ENDING == ARR_SIZE(g_m6809_reg_byte_size));
2252
2253
0
    return CS_ERR_MODE;
2254
0
  }
2255
2256
4.15k
  if (M680X_INS_ENDING != ARR_SIZE(g_insn_props)) {
2257
0
    CS_ASSERT(M680X_INS_ENDING == ARR_SIZE(g_insn_props));
2258
2259
0
    return CS_ERR_MODE;
2260
0
  }
2261
2262
4.15k
  if (M680X_CPU_TYPE_ENDING != ARR_SIZE(g_cpu_tables)) {
2263
0
    CS_ASSERT(M680X_CPU_TYPE_ENDING == ARR_SIZE(g_cpu_tables));
2264
2265
0
    return CS_ERR_MODE;
2266
0
  }
2267
2268
4.15k
  if (HANDLER_ID_ENDING != ARR_SIZE(g_insn_handler)) {
2269
0
    CS_ASSERT(HANDLER_ID_ENDING == ARR_SIZE(g_insn_handler));
2270
2271
0
    return CS_ERR_MODE;
2272
0
  }
2273
2274
4.15k
  if (ACCESS_MODE_ENDING !=  MATRIX_SIZE(g_access_mode_to_access)) {
2275
0
    CS_ASSERT(ACCESS_MODE_ENDING ==
2276
0
      MATRIX_SIZE(g_access_mode_to_access));
2277
2278
0
    return CS_ERR_MODE;
2279
0
  }
2280
2281
4.15k
  return CS_ERR_OK;
2282
4.15k
}
2283
2284
#ifndef CAPSTONE_DIET
2285
void M680X_reg_access(const cs_insn *insn,
2286
  cs_regs regs_read, uint8_t *regs_read_count,
2287
  cs_regs regs_write, uint8_t *regs_write_count)
2288
0
{
2289
0
  if (insn->detail == NULL) {
2290
0
    *regs_read_count = 0;
2291
0
    *regs_write_count = 0;
2292
0
  }
2293
0
  else {
2294
0
    *regs_read_count = insn->detail->regs_read_count;
2295
0
    *regs_write_count = insn->detail->regs_write_count;
2296
2297
0
    memcpy(regs_read, insn->detail->regs_read,
2298
0
      *regs_read_count * sizeof(insn->detail->regs_read[0]));
2299
0
    memcpy(regs_write, insn->detail->regs_write,
2300
0
      *regs_write_count *
2301
0
      sizeof(insn->detail->regs_write[0]));
2302
0
  }
2303
0
}
2304
#endif
2305
2306
#endif
2307