Coverage Report

Created: 2025-07-11 06:32

/src/capstonev5/arch/M680X/M680XDisassembler.c
Line
Count
Source (jump to first uncovered line)
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
451k
{
157
451k
  if (address < info->offset ||
158
451k
    (uint32_t)(address - info->offset) >= info->size)
159
    // out of code buffer range
160
950
    return false;
161
162
450k
  *byte = info->code[address - info->offset];
163
164
450k
  return true;
165
451k
}
166
167
static bool read_byte_sign_extended(const m680x_info *info, int16_t *word,
168
  uint16_t address)
169
25.2k
{
170
25.2k
  if (address < info->offset ||
171
25.2k
    (uint32_t)(address - info->offset) >= info->size)
172
    // out of code buffer range
173
0
    return false;
174
175
25.2k
  *word = (int16_t) info->code[address - info->offset];
176
177
25.2k
  if (*word & 0x80)
178
9.19k
    *word |= 0xFF00;
179
180
25.2k
  return true;
181
25.2k
}
182
183
static bool read_word(const m680x_info *info, uint16_t *word, uint16_t address)
184
32.2k
{
185
32.2k
  if (address < info->offset ||
186
32.2k
    (uint32_t)(address + 1 - info->offset) >= info->size)
187
    // out of code buffer range
188
11
    return false;
189
190
32.1k
  *word = (uint16_t)info->code[address - info->offset] << 8;
191
32.1k
  *word |= (uint16_t)info->code[address + 1 - info->offset];
192
193
32.1k
  return true;
194
32.2k
}
195
196
static bool read_sdword(const m680x_info *info, int32_t *sdword,
197
  uint16_t address)
198
208
{
199
208
  if (address < info->offset ||
200
208
    (uint32_t)(address + 3 - info->offset) >= info->size)
201
    // out of code buffer range
202
0
    return false;
203
204
208
  *sdword = (uint32_t)info->code[address - info->offset] << 24;
205
208
  *sdword |= (uint32_t)info->code[address + 1 - info->offset] << 16;
206
208
  *sdword |= (uint32_t)info->code[address + 2 - info->offset] << 8;
207
208
  *sdword |= (uint32_t)info->code[address + 3 - info->offset];
208
209
208
  return true;
210
208
}
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
60.2k
{
219
  // As part of the algorithm last may get negative.
220
  // => signed integer has to be used.
221
60.2k
  int first = 0;
222
60.2k
  int last = (int)table_size - 1;
223
60.2k
  int middle = (first + last) / 2;
224
225
300k
  while (first <= last) {
226
277k
    if (inst_pageX_table[middle].opcode < opcode) {
227
97.1k
      first = middle + 1;
228
97.1k
    }
229
180k
    else if (inst_pageX_table[middle].opcode == opcode) {
230
37.6k
      return middle;  /* item found */
231
37.6k
    }
232
143k
    else
233
143k
      last = middle - 1;
234
235
240k
    middle = (first + last) / 2;
236
240k
  }
237
238
22.5k
  if (first > last)
239
22.5k
    return -1;  /* item not found */
240
241
0
  return -2;
242
22.5k
}
243
244
void M680X_get_insn_id(cs_struct *handle, cs_insn *insn, unsigned int id)
245
186k
{
246
186k
  const m680x_info *const info = (const m680x_info *)handle->printer_info;
247
186k
  const cpu_tables *cpu = info->cpu;
248
186k
  uint8_t insn_prefix = (id >> 8) & 0xff;
249
  // opcode is the first instruction byte without the prefix.
250
186k
  uint8_t opcode = id & 0xff;
251
186k
  int index;
252
186k
  int i;
253
254
186k
  insn->id = M680X_INS_ILLGL;
255
256
439k
  for (i = 0; i < ARR_SIZE(cpu->pageX_prefix); ++i) {
257
432k
    if (cpu->pageX_table_size[i] == 0 ||
258
432k
      (cpu->inst_pageX_table[i] == NULL))
259
164k
      break;
260
261
268k
    if (cpu->pageX_prefix[i] == insn_prefix) {
262
15.6k
      index = binary_search(cpu->inst_pageX_table[i],
263
15.6k
          cpu->pageX_table_size[i], opcode);
264
15.6k
      insn->id = (index >= 0) ?
265
10.8k
        cpu->inst_pageX_table[i][index].insn :
266
15.6k
        M680X_INS_ILLGL;
267
15.6k
      return;
268
15.6k
    }
269
268k
  }
270
271
171k
  if (insn_prefix != 0)
272
0
    return;
273
274
171k
  insn->id = cpu->inst_page1_table[id].insn;
275
276
171k
  if (insn->id != M680X_INS_ILLGL)
277
152k
    return;
278
279
  // Check if opcode byte is present in an overlay table
280
25.2k
  for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
281
23.5k
    if (cpu->overlay_table_size[i] == 0 ||
282
23.5k
      (cpu->inst_overlay_table[i] == NULL))
283
9.08k
      break;
284
285
14.4k
    if ((index = binary_search(cpu->inst_overlay_table[i],
286
14.4k
            cpu->overlay_table_size[i],
287
14.4k
            opcode)) >= 0) {
288
8.02k
      insn->id = cpu->inst_overlay_table[i][index].insn;
289
8.02k
      return;
290
8.02k
    }
291
14.4k
  }
292
18.7k
}
293
294
static void add_insn_group(cs_detail *detail, m680x_group_type group)
295
179k
{
296
179k
  if (detail != NULL &&
297
179k
    (group != M680X_GRP_INVALID) && (group != M680X_GRP_ENDING))
298
41.3k
    detail->groups[detail->groups_count++] = (uint8_t)group;
299
179k
}
300
301
static bool exists_reg_list(uint16_t *regs, uint8_t count, m680x_reg reg)
302
515k
{
303
515k
  uint8_t i;
304
305
855k
  for (i = 0; i < count; ++i) {
306
355k
    if (regs[i] == (uint16_t)reg)
307
15.8k
      return true;
308
355k
  }
309
310
499k
  return false;
311
515k
}
312
313
static void add_reg_to_rw_list(MCInst *MI, m680x_reg reg, e_access access)
314
340k
{
315
340k
  cs_detail *detail = MI->flat_insn->detail;
316
317
340k
  if (detail == NULL || (reg == M680X_REG_INVALID))
318
0
    return;
319
320
340k
  switch (access) {
321
175k
  case MODIFY:
322
175k
    if (!exists_reg_list(detail->regs_read,
323
175k
        detail->regs_read_count, reg))
324
171k
      detail->regs_read[detail->regs_read_count++] =
325
171k
        (uint16_t)reg;
326
327
  // intentionally fall through
328
329
226k
  case WRITE:
330
226k
    if (!exists_reg_list(detail->regs_write,
331
226k
        detail->regs_write_count, reg))
332
222k
      detail->regs_write[detail->regs_write_count++] =
333
222k
        (uint16_t)reg;
334
335
226k
    break;
336
337
113k
  case READ:
338
113k
    if (!exists_reg_list(detail->regs_read,
339
113k
        detail->regs_read_count, reg))
340
105k
      detail->regs_read[detail->regs_read_count++] =
341
105k
        (uint16_t)reg;
342
343
113k
    break;
344
345
0
  case UNCHANGED:
346
0
  default:
347
0
    break;
348
340k
  }
349
340k
}
350
351
static void update_am_reg_list(MCInst *MI, m680x_info *info, cs_m680x_op *op,
352
  e_access access)
353
240k
{
354
240k
  if (MI->flat_insn->detail == NULL)
355
0
    return;
356
357
240k
  switch (op->type) {
358
107k
  case M680X_OP_REGISTER:
359
107k
    add_reg_to_rw_list(MI, op->reg, access);
360
107k
    break;
361
362
48.0k
  case M680X_OP_INDEXED:
363
48.0k
    add_reg_to_rw_list(MI, op->idx.base_reg, READ);
364
365
48.0k
    if (op->idx.base_reg == M680X_REG_X &&
366
48.0k
      info->cpu->reg_byte_size[M680X_REG_H])
367
5.25k
      add_reg_to_rw_list(MI, M680X_REG_H, READ);
368
369
370
48.0k
    if (op->idx.offset_reg != M680X_REG_INVALID)
371
4.40k
      add_reg_to_rw_list(MI, op->idx.offset_reg, READ);
372
373
48.0k
    if (op->idx.inc_dec) {
374
9.67k
      add_reg_to_rw_list(MI, op->idx.base_reg, WRITE);
375
376
9.67k
      if (op->idx.base_reg == M680X_REG_X &&
377
9.67k
        info->cpu->reg_byte_size[M680X_REG_H])
378
1.24k
        add_reg_to_rw_list(MI, M680X_REG_H, WRITE);
379
9.67k
    }
380
381
48.0k
    break;
382
383
85.3k
  default:
384
85.3k
    break;
385
240k
  }
386
240k
}
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
512k
{
409
512k
  int idx = (operator_index > 3) ? 3 : operator_index;
410
411
512k
  return g_access_mode_to_access[idx][access_mode];
412
512k
}
413
414
static void build_regs_read_write_counts(MCInst *MI, m680x_info *info,
415
  e_access_mode access_mode)
416
163k
{
417
163k
  cs_m680x *m680x = &info->m680x;
418
163k
  int i;
419
420
163k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count))
421
21.3k
    return;
422
423
383k
  for (i = 0; i < m680x->op_count; ++i) {
424
425
240k
    e_access access = get_access(i, access_mode);
426
240k
    update_am_reg_list(MI, info, &m680x->operands[i], access);
427
240k
  }
428
142k
}
429
430
static void add_operators_access(MCInst *MI, m680x_info *info,
431
  e_access_mode access_mode)
432
163k
{
433
163k
  cs_m680x *m680x = &info->m680x;
434
163k
  int offset = 0;
435
163k
  int i;
436
437
163k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count) ||
438
163k
    (access_mode == uuuu))
439
37.2k
    return;
440
441
350k
  for (i = 0; i < m680x->op_count; ++i) {
442
223k
    e_access access;
443
444
    // Ugly fix: MULD has a register operand, an immediate operand
445
    // AND an implicitly changed register W
446
223k
    if (info->insn == M680X_INS_MULD && (i == 1))
447
216
      offset = 1;
448
449
223k
    access = get_access(i + offset, access_mode);
450
223k
    m680x->operands[i].access = access;
451
223k
  }
452
126k
}
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
17.8k
{
462
  //TABLE
463
980k
#define EOL M680X_REG_INVALID
464
17.8k
  static const insn_to_changed_regs changed_regs[] = {
465
17.8k
    { M680X_INS_BSR, mmmm, { M680X_REG_S, EOL } },
466
17.8k
    { M680X_INS_CALL, mmmm, { M680X_REG_S, EOL } },
467
17.8k
    {
468
17.8k
      M680X_INS_CWAI, mrrr, {
469
17.8k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
470
17.8k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
471
17.8k
        M680X_REG_D, M680X_REG_CC, EOL
472
17.8k
      },
473
17.8k
    },
474
17.8k
    { M680X_INS_DAA, mrrr, { M680X_REG_A, EOL } },
475
17.8k
    {
476
17.8k
      M680X_INS_DIV, mmrr, {
477
17.8k
        M680X_REG_A, M680X_REG_H, M680X_REG_X, EOL
478
17.8k
      }
479
17.8k
    },
480
17.8k
    {
481
17.8k
      M680X_INS_EDIV, mmrr, {
482
17.8k
        M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL
483
17.8k
      }
484
17.8k
    },
485
17.8k
    {
486
17.8k
      M680X_INS_EDIVS, mmrr, {
487
17.8k
        M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL
488
17.8k
      }
489
17.8k
    },
490
17.8k
    { M680X_INS_EMACS, mrrr, { M680X_REG_X, M680X_REG_Y, EOL } },
491
17.8k
    { M680X_INS_EMAXM, rrrr, { M680X_REG_D, EOL } },
492
17.8k
    { M680X_INS_EMINM, rrrr, { M680X_REG_D, EOL } },
493
17.8k
    { M680X_INS_EMUL, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
494
17.8k
    { M680X_INS_EMULS, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
495
17.8k
    { M680X_INS_ETBL, wmmm, { M680X_REG_A, M680X_REG_B, EOL } },
496
17.8k
    { M680X_INS_FDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
497
17.8k
    { M680X_INS_IDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
498
17.8k
    { M680X_INS_IDIVS, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
499
17.8k
    { M680X_INS_JSR, mmmm, { M680X_REG_S, EOL } },
500
17.8k
    { M680X_INS_LBSR, mmmm, { M680X_REG_S, EOL } },
501
17.8k
    { M680X_INS_MAXM, rrrr, { M680X_REG_A, EOL } },
502
17.8k
    { M680X_INS_MINM, rrrr, { M680X_REG_A, EOL } },
503
17.8k
    {
504
17.8k
      M680X_INS_MEM, mmrr, {
505
17.8k
        M680X_REG_X, M680X_REG_Y, M680X_REG_A, EOL
506
17.8k
      }
507
17.8k
    },
508
17.8k
    { M680X_INS_MUL, mmmm, { M680X_REG_A, M680X_REG_B, EOL } },
509
17.8k
    { M680X_INS_MULD, mwrr, { M680X_REG_D, M680X_REG_W, EOL } },
510
17.8k
    { M680X_INS_PSHA, rmmm, { M680X_REG_A, M680X_REG_S, EOL } },
511
17.8k
    { M680X_INS_PSHB, rmmm, { M680X_REG_B, M680X_REG_S, EOL } },
512
17.8k
    { M680X_INS_PSHC, rmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
513
17.8k
    { M680X_INS_PSHD, rmmm, { M680X_REG_D, M680X_REG_S, EOL } },
514
17.8k
    { M680X_INS_PSHH, rmmm, { M680X_REG_H, M680X_REG_S, EOL } },
515
17.8k
    { M680X_INS_PSHX, rmmm, { M680X_REG_X, M680X_REG_S, EOL } },
516
17.8k
    { M680X_INS_PSHY, rmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
517
17.8k
    { M680X_INS_PULA, wmmm, { M680X_REG_A, M680X_REG_S, EOL } },
518
17.8k
    { M680X_INS_PULB, wmmm, { M680X_REG_B, M680X_REG_S, EOL } },
519
17.8k
    { M680X_INS_PULC, wmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
520
17.8k
    { M680X_INS_PULD, wmmm, { M680X_REG_D, M680X_REG_S, EOL } },
521
17.8k
    { M680X_INS_PULH, wmmm, { M680X_REG_H, M680X_REG_S, EOL } },
522
17.8k
    { M680X_INS_PULX, wmmm, { M680X_REG_X, M680X_REG_S, EOL } },
523
17.8k
    { M680X_INS_PULY, wmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
524
17.8k
    {
525
17.8k
      M680X_INS_REV, mmrr, {
526
17.8k
        M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL
527
17.8k
      }
528
17.8k
    },
529
17.8k
    {
530
17.8k
      M680X_INS_REVW, mmmm, {
531
17.8k
        M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL
532
17.8k
      }
533
17.8k
    },
534
17.8k
    { M680X_INS_RTC, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
535
17.8k
    {
536
17.8k
      M680X_INS_RTI, mwww, {
537
17.8k
        M680X_REG_S, M680X_REG_CC, M680X_REG_B,
538
17.8k
        M680X_REG_A, M680X_REG_DP, M680X_REG_X,
539
17.8k
        M680X_REG_Y, M680X_REG_U, M680X_REG_PC,
540
17.8k
        EOL
541
17.8k
      },
542
17.8k
    },
543
17.8k
    { M680X_INS_RTS, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
544
17.8k
    { M680X_INS_SEX, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
545
17.8k
    { M680X_INS_SEXW, rwww, { M680X_REG_W, M680X_REG_D, EOL } },
546
17.8k
    {
547
17.8k
      M680X_INS_SWI, mmrr, {
548
17.8k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
549
17.8k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
550
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
551
17.8k
        EOL
552
17.8k
      }
553
17.8k
    },
554
17.8k
    {
555
17.8k
      M680X_INS_SWI2, mmrr, {
556
17.8k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
557
17.8k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
558
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
559
17.8k
        EOL
560
17.8k
      },
561
17.8k
    },
562
17.8k
    {
563
17.8k
      M680X_INS_SWI3, mmrr, {
564
17.8k
        M680X_REG_S, M680X_REG_PC, M680X_REG_U,
565
17.8k
        M680X_REG_Y, M680X_REG_X, M680X_REG_DP,
566
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
567
17.8k
        EOL
568
17.8k
      },
569
17.8k
    },
570
17.8k
    { M680X_INS_TBL, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
571
17.8k
    {
572
17.8k
      M680X_INS_WAI, mrrr, {
573
17.8k
        M680X_REG_S, M680X_REG_PC, M680X_REG_X,
574
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_CC,
575
17.8k
        EOL
576
17.8k
      }
577
17.8k
    },
578
17.8k
    {
579
17.8k
      M680X_INS_WAV, rmmm, {
580
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_X,
581
17.8k
        M680X_REG_Y, EOL
582
17.8k
      }
583
17.8k
    },
584
17.8k
    {
585
17.8k
      M680X_INS_WAVR, rmmm, {
586
17.8k
        M680X_REG_A, M680X_REG_B, M680X_REG_X,
587
17.8k
        M680X_REG_Y, EOL
588
17.8k
      }
589
17.8k
    },
590
17.8k
  };
591
592
17.8k
  int i, j;
593
594
17.8k
  if (MI->flat_insn->detail == NULL)
595
0
    return;
596
597
927k
  for (i = 0; i < ARR_SIZE(changed_regs); ++i) {
598
909k
    if (info->insn == changed_regs[i].insn) {
599
17.8k
      e_access_mode access_mode = changed_regs[i].access_mode;
600
601
70.9k
      for (j = 0; changed_regs[i].regs[j] != EOL; ++j) {
602
53.1k
        e_access access;
603
604
53.1k
        m680x_reg reg = changed_regs[i].regs[j];
605
606
53.1k
        if (!info->cpu->reg_byte_size[reg]) {
607
5.46k
          if (info->insn != M680X_INS_MUL)
608
5.23k
            continue;
609
610
          // Hack for M68HC05: MUL uses reg. A,X
611
229
          reg = M680X_REG_X;
612
229
        }
613
614
47.9k
        access = get_access(j, access_mode);
615
47.9k
        add_reg_to_rw_list(MI, reg, access);
616
47.9k
      }
617
17.8k
    }
618
909k
  }
619
620
17.8k
#undef EOL
621
17.8k
}
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
20.8k
{
636
20.8k
  uint8_t ir = 0;
637
20.8k
  uint8_t post_byte;
638
639
  // Read the indexed addressing post byte.
640
20.8k
  if (!read_byte(info, &post_byte, address))
641
93
    return -1;
642
643
  // Depending on the indexed addressing mode more bytes have to be read.
644
20.7k
  switch (post_byte & 0x9F) {
645
458
  case 0x87:
646
1.27k
  case 0x8A:
647
1.59k
  case 0x8E:
648
2.30k
  case 0x8F:
649
2.56k
  case 0x90:
650
2.85k
  case 0x92:
651
3.08k
  case 0x97:
652
3.30k
  case 0x9A:
653
3.54k
  case 0x9E:
654
3.54k
    return -1; // illegal indexed post bytes
655
656
626
  case 0x88: // n8,R
657
1.54k
  case 0x8C: // n8,PCR
658
1.83k
  case 0x98: // [n8,R]
659
2.13k
  case 0x9C: // [n8,PCR]
660
2.13k
    if (!read_byte(info, &ir, address + 1))
661
16
      return -1;
662
2.11k
    return 2;
663
664
1.21k
  case 0x89: // n16,R
665
1.50k
  case 0x8D: // n16,PCR
666
1.82k
  case 0x99: // [n16,R]
667
2.08k
  case 0x9D: // [n16,PCR]
668
2.08k
    if (!read_byte(info, &ir, address + 2))
669
39
      return -1;
670
2.04k
    return 3;
671
672
485
  case 0x9F: // [n]
673
485
    if ((post_byte & 0x60) != 0 ||
674
485
      !read_byte(info, &ir, address + 2))
675
263
      return -1;
676
222
    return  3;
677
20.7k
  }
678
679
  // Any other indexed post byte is valid and
680
  // no additional bytes have to be read.
681
12.5k
  return 1;
682
20.7k
}
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
20.9k
{
690
20.9k
  uint8_t ir;
691
20.9k
  uint8_t post_byte;
692
693
  // Read the indexed addressing post byte.
694
20.9k
  if (!read_byte(info, &post_byte, address))
695
86
    return -1;
696
697
  // Depending on the indexed addressing mode more bytes have to be read.
698
20.8k
  if (!(post_byte & 0x20)) // n5,R
699
7.66k
    return 1;
700
701
13.1k
  switch (post_byte & 0xe7) {
702
1.09k
  case 0xe0:
703
2.50k
  case 0xe1: // n9,R
704
2.50k
    if (is_subset)
705
285
      return -1;
706
707
2.22k
    if (!read_byte(info, &ir, address))
708
0
      return -1;
709
2.22k
    return 2;
710
711
1.64k
  case 0xe2: // n16,R
712
2.68k
  case 0xe3: // [n16,R]
713
2.68k
    if (is_subset)
714
206
      return -1;
715
716
2.47k
    if (!read_byte(info, &ir, address + 1))
717
25
      return -1;
718
2.45k
    return 3;
719
720
438
  case 0xe4: // A,R
721
997
  case 0xe5: // B,R
722
1.30k
  case 0xe6: // D,R
723
2.44k
  case 0xe7: // [D,R]
724
7.98k
  default: // n,-r n,+r n,r- n,r+
725
7.98k
    break;
726
13.1k
  }
727
728
7.98k
  return 1;
729
13.1k
}
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
3.64k
{
734
3.64k
  if (info->cpu->tfr_reg_valid != NULL)
735
1.66k
    return info->cpu->tfr_reg_valid[reg_nibble];
736
737
1.98k
  return true; // e.g. for the M6309 all registers are valid
738
3.64k
}
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
741
{
744
741
  return !(post_byte & 0x08);
745
741
}
746
747
static bool is_tfm_reg_valid(const m680x_info *info, uint8_t reg_nibble)
748
1.03k
{
749
  // HD6809 TFM instruction: Only register X,Y,U,S,D is allowed
750
1.03k
  return reg_nibble <= 4;
751
1.03k
}
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
2.02k
{
758
2.02k
  uint8_t post_byte;
759
2.02k
  uint8_t rr;
760
761
2.02k
  if (!read_byte(info, &post_byte, address))
762
6
    return -1;
763
764
  // According to documentation bit 3 is don't care and not checked here.
765
2.02k
  if ((post_byte >= 0xc0) ||
766
2.02k
    ((post_byte & 0x07) == 2) || ((post_byte & 0x07) == 3))
767
1.17k
    return -1;
768
769
853
  if (!read_byte(info, &rr, address + 1))
770
8
    return -1;
771
772
845
  return 2;
773
853
}
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
467
{
781
467
  uint8_t post_byte;
782
467
  uint8_t rr;
783
784
467
  if (!read_byte(info, &post_byte, address))
785
2
    return -1;
786
787
465
  if ((post_byte & 0xc0) == 0xc0)
788
217
    return -1; // Invalid register specified
789
248
  else {
790
248
    if (!read_byte(info, &rr, address + 1))
791
5
      return -1;
792
248
  }
793
794
243
  return 2;
795
465
}
796
797
static bool is_sufficient_code_size(const m680x_info *info, uint16_t address,
798
  insn_desc *insn_description)
799
171k
{
800
171k
  int i;
801
171k
  bool retval = true;
802
171k
  uint16_t size = 0;
803
171k
  int sz;
804
805
499k
  for (i = 0; i < 2; i++) {
806
335k
    uint8_t ir = 0;
807
335k
    bool is_subset = false;
808
809
335k
    switch (insn_description->hid[i]) {
810
811
219
    case imm32_hid:
812
219
      if ((retval = read_byte(info, &ir, address + size + 3)))
813
208
        size += 4;
814
219
      break;
815
816
20.2k
    case ext_hid:
817
22.4k
    case imm16_hid:
818
24.3k
    case rel16_hid:
819
25.2k
    case imm8rel_hid:
820
27.7k
    case opidxdr_hid:
821
28.9k
    case idxX16_hid:
822
29.1k
    case idxS16_hid:
823
29.1k
      if ((retval = read_byte(info, &ir, address + size + 1)))
824
28.8k
        size += 2;
825
29.1k
      break;
826
827
9.24k
    case rel8_hid:
828
34.6k
    case dir_hid:
829
37.4k
    case rbits_hid:
830
47.1k
    case imm8_hid:
831
49.8k
    case idxX_hid:
832
50.5k
    case idxXp_hid:
833
51.3k
    case idxY_hid:
834
52.1k
    case idxS_hid:
835
52.9k
    case index_hid:
836
52.9k
      if ((retval = read_byte(info, &ir, address + size)))
837
52.7k
        size++;
838
52.9k
      break;
839
840
0
    case illgl_hid:
841
200k
    case inh_hid:
842
203k
    case idxX0_hid:
843
204k
    case idxX0p_hid:
844
205k
    case opidx_hid:
845
205k
      retval = true;
846
205k
      break;
847
848
20.8k
    case idx09_hid:
849
20.8k
      sz = get_indexed09_post_byte_size(info, address + size);
850
20.8k
      if (sz >= 0)
851
16.9k
        size += sz;
852
3.95k
      else
853
3.95k
        retval = false;
854
20.8k
      break;
855
856
525
    case idx12s_hid:
857
525
      is_subset = true;
858
859
    // intentionally fall through
860
861
17.1k
    case idx12_hid:
862
17.1k
      sz = get_indexed12_post_byte_size(info,
863
17.1k
          address + size, is_subset);
864
17.1k
      if (sz >= 0)
865
16.5k
        size += sz;
866
582
      else
867
582
        retval = false;
868
17.1k
      break;
869
870
804
    case exti12x_hid:
871
2.05k
    case imm16i12x_hid:
872
2.05k
      sz = get_indexed12_post_byte_size(info,
873
2.05k
          address + size, false);
874
2.05k
      if (sz >= 0) {
875
2.04k
        size += sz;
876
2.04k
        if ((retval = read_byte(info, &ir,
877
2.04k
            address + size + 1)))
878
2.01k
          size += 2;
879
2.04k
      } else
880
12
        retval = false;
881
2.05k
      break;
882
883
1.75k
    case imm8i12x_hid:
884
1.75k
      sz = get_indexed12_post_byte_size(info,
885
1.75k
          address + size, false);
886
1.75k
      if (sz >= 0) {
887
1.74k
        size += sz;
888
1.74k
        if ((retval = read_byte(info, &ir,
889
1.74k
            address + size)))
890
1.71k
          size++;
891
1.74k
      } else
892
8
        retval = false;
893
1.75k
      break;
894
895
570
    case tfm_hid:
896
570
      if ((retval = read_byte(info, &ir, address + size))) {
897
569
        size++;
898
569
        retval = is_tfm_reg_valid(info, (ir >> 4) & 0x0F) &&
899
569
          is_tfm_reg_valid(info, ir & 0x0F);
900
569
      }
901
570
      break;
902
903
1.94k
    case rr09_hid:
904
1.94k
      if ((retval = read_byte(info, &ir, address + size))) {
905
1.92k
        size++;
906
1.92k
        retval = is_tfr09_reg_valid(info, (ir >> 4) & 0x0F) &&
907
1.92k
          is_tfr09_reg_valid(info, ir & 0x0F);
908
1.92k
      }
909
1.94k
      break;
910
911
749
    case rr12_hid:
912
749
      if ((retval = read_byte(info, &ir, address + size))) {
913
741
        size++;
914
741
        retval = is_exg_tfr12_post_byte_valid(info, ir);
915
741
      }
916
749
      break;
917
918
467
    case bitmv_hid:
919
467
      sz = get_bitmv_post_byte_size(info, address + size);
920
467
      if (sz >= 0)
921
243
        size += sz;
922
224
      else
923
224
        retval = false;
924
467
      break;
925
926
2.02k
    case loop_hid:
927
2.02k
      sz = get_loop_post_byte_size(info, address + size);
928
2.02k
      if (sz >= 0)
929
845
        size += sz;
930
1.18k
      else
931
1.18k
        retval = false;
932
2.02k
      break;
933
934
0
    default:
935
0
      CS_ASSERT(0 && "Unexpected instruction handler id");
936
0
      retval = false;
937
0
      break;
938
335k
    }
939
940
335k
    if (!retval)
941
7.43k
      return false;
942
335k
  }
943
944
163k
  insn_description->insn_size += size;
945
946
163k
  return retval;
947
171k
}
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
186k
{
954
186k
  const inst_pageX *inst_table = NULL;
955
186k
  const cpu_tables *cpu = info->cpu;
956
186k
  size_t table_size = 0;
957
186k
  uint16_t base_address = address;
958
186k
  uint8_t ir; // instruction register
959
186k
  int i;
960
186k
  int index;
961
962
186k
  if (!read_byte(info, &ir, address++))
963
0
    return false;
964
965
186k
  insn_description->insn = M680X_INS_ILLGL;
966
186k
  insn_description->opcode = ir;
967
968
  // Check if a page prefix byte is present
969
439k
  for (i = 0; i < ARR_SIZE(cpu->pageX_table_size); ++i) {
970
432k
    if (cpu->pageX_table_size[i] == 0 ||
971
432k
      (cpu->inst_pageX_table[i] == NULL))
972
164k
      break;
973
974
268k
    if ((cpu->pageX_prefix[i] == ir)) {
975
      // Get pageX instruction and handler id.
976
      // Abort for illegal instr.
977
15.6k
      inst_table = cpu->inst_pageX_table[i];
978
15.6k
      table_size = cpu->pageX_table_size[i];
979
980
15.6k
      if (!read_byte(info, &ir, address++))
981
26
        return false;
982
983
15.6k
      insn_description->opcode =
984
15.6k
        (insn_description->opcode << 8) | ir;
985
986
15.6k
      if ((index = binary_search(inst_table, table_size,
987
15.6k
        ir)) < 0)
988
4.83k
        return false;
989
990
10.8k
      insn_description->hid[0] =
991
10.8k
        inst_table[index].handler_id1;
992
10.8k
      insn_description->hid[1] =
993
10.8k
        inst_table[index].handler_id2;
994
10.8k
      insn_description->insn = inst_table[index].insn;
995
10.8k
      break;
996
15.6k
    }
997
268k
  }
998
999
182k
  if (insn_description->insn == M680X_INS_ILLGL) {
1000
    // Get page1 insn description
1001
171k
    insn_description->insn = cpu->inst_page1_table[ir].insn;
1002
171k
    insn_description->hid[0] =
1003
171k
      cpu->inst_page1_table[ir].handler_id1;
1004
171k
    insn_description->hid[1] =
1005
171k
      cpu->inst_page1_table[ir].handler_id2;
1006
171k
  }
1007
1008
182k
  if (insn_description->insn == M680X_INS_ILLGL) {
1009
    // Check if opcode byte is present in an overlay table
1010
25.1k
    for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
1011
23.5k
      if (cpu->overlay_table_size[i] == 0 ||
1012
23.5k
        (cpu->inst_overlay_table[i] == NULL))
1013
9.06k
        break;
1014
1015
14.4k
      inst_table = cpu->inst_overlay_table[i];
1016
14.4k
      table_size = cpu->overlay_table_size[i];
1017
1018
14.4k
      if ((index = binary_search(inst_table, table_size,
1019
14.4k
              ir)) >= 0) {
1020
8.02k
        insn_description->hid[0] =
1021
8.02k
          inst_table[index].handler_id1;
1022
8.02k
        insn_description->hid[1] =
1023
8.02k
          inst_table[index].handler_id2;
1024
8.02k
        insn_description->insn = inst_table[index].insn;
1025
8.02k
        break;
1026
8.02k
      }
1027
14.4k
    }
1028
18.7k
  }
1029
1030
182k
  insn_description->insn_size = address - base_address;
1031
1032
182k
  return (insn_description->insn != M680X_INS_ILLGL) &&
1033
182k
    (insn_description->insn != M680X_INS_INVLD) &&
1034
182k
    is_sufficient_code_size(info, address, insn_description);
1035
186k
}
1036
1037
static void illegal_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1038
22.9k
{
1039
22.9k
  cs_m680x_op *op0 = &info->m680x.operands[info->m680x.op_count++];
1040
22.9k
  uint8_t temp8 = 0;
1041
1042
22.9k
  info->insn = M680X_INS_ILLGL;
1043
22.9k
  read_byte(info, &temp8, (*address)++);
1044
22.9k
  op0->imm = (int32_t)temp8 & 0xff;
1045
22.9k
  op0->type = M680X_OP_IMMEDIATE;
1046
22.9k
  op0->size = 1;
1047
22.9k
}
1048
1049
static void inherent_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1050
200k
{
1051
  // There is nothing to do here :-)
1052
200k
}
1053
1054
static void add_reg_operand(m680x_info *info, m680x_reg reg)
1055
107k
{
1056
107k
  cs_m680x *m680x = &info->m680x;
1057
107k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1058
1059
107k
  op->type = M680X_OP_REGISTER;
1060
107k
  op->reg = reg;
1061
107k
  op->size = info->cpu->reg_byte_size[reg];
1062
107k
}
1063
1064
static void set_operand_size(m680x_info *info, cs_m680x_op *op,
1065
  uint8_t default_size)
1066
112k
{
1067
112k
  cs_m680x *m680x = &info->m680x;
1068
1069
112k
  if (info->insn == M680X_INS_JMP || info->insn == M680X_INS_JSR)
1070
5.40k
    op->size = 0;
1071
107k
  else if (info->insn == M680X_INS_DIVD ||
1072
107k
    ((info->insn == M680X_INS_AIS || info->insn == M680X_INS_AIX) &&
1073
106k
      op->type != M680X_OP_REGISTER))
1074
1.26k
    op->size = 1;
1075
106k
  else if (info->insn == M680X_INS_DIVQ ||
1076
106k
    info->insn == M680X_INS_MOVW)
1077
4.58k
    op->size = 2;
1078
101k
  else if (info->insn == M680X_INS_EMACS)
1079
295
    op->size = 4;
1080
101k
  else if ((m680x->op_count > 0) &&
1081
101k
    (m680x->operands[0].type == M680X_OP_REGISTER))
1082
62.0k
    op->size = m680x->operands[0].size;
1083
39.3k
  else
1084
39.3k
    op->size = default_size;
1085
112k
}
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
2.78k
{
1099
2.78k
  cs_m680x_op *op0 = &info->m680x.operands[0];
1100
2.78k
  uint8_t reg_bits = 0;
1101
2.78k
  uint16_t bit_index;
1102
2.78k
  const m680x_reg *reg_to_reg_ids = NULL;
1103
1104
2.78k
  read_byte(info, &reg_bits, (*address)++);
1105
1106
2.78k
  switch (op0->reg) {
1107
1.86k
  case M680X_REG_U:
1108
1.86k
    reg_to_reg_ids = &reg_u_reg_ids[0];
1109
1.86k
    break;
1110
1111
916
  case M680X_REG_S:
1112
916
    reg_to_reg_ids = &reg_s_reg_ids[0];
1113
916
    break;
1114
1115
0
  default:
1116
0
    CS_ASSERT(0 && "Unexpected operand0 register");
1117
0
    break;
1118
2.78k
  }
1119
1120
2.78k
  if ((info->insn == M680X_INS_PULU ||
1121
2.78k
      (info->insn == M680X_INS_PULS)) &&
1122
2.78k
    ((reg_bits & 0x80) != 0))
1123
    // PULS xxx,PC or PULU xxx,PC which is like return from
1124
    // subroutine (RTS)
1125
352
    add_insn_group(MI->flat_insn->detail, M680X_GRP_RET);
1126
1127
25.0k
  for (bit_index = 0; bit_index < 8; ++bit_index) {
1128
22.2k
    if (reg_bits & (1 << bit_index))
1129
10.9k
      add_reg_operand(info, reg_to_reg_ids[bit_index]);
1130
22.2k
  }
1131
2.78k
}
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
1.24k
{
1144
1.24k
  uint8_t regs = 0;
1145
1146
1.24k
  read_byte(info, &regs, (*address)++);
1147
1148
1.24k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs >> 4]);
1149
1.24k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs & 0x0f]);
1150
1151
1.24k
  if ((regs & 0x0f) == 0x05) {
1152
    // EXG xxx,PC or TFR xxx,PC which is like a JMP
1153
425
    add_insn_group(MI->flat_insn->detail, M680X_GRP_JUMP);
1154
425
  }
1155
1.24k
}
1156
1157
1158
static void reg_reg12_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1159
687
{
1160
687
  static const m680x_reg g_tfr_exg12_reg0_ids[] = {
1161
687
    M680X_REG_A, M680X_REG_B,  M680X_REG_CC,  M680X_REG_TMP3,
1162
687
    M680X_REG_D, M680X_REG_X, M680X_REG_Y,  M680X_REG_S,
1163
687
  };
1164
687
  static const m680x_reg g_tfr_exg12_reg1_ids[] = {
1165
687
    M680X_REG_A, M680X_REG_B,  M680X_REG_CC,  M680X_REG_TMP2,
1166
687
    M680X_REG_D, M680X_REG_X, M680X_REG_Y,  M680X_REG_S,
1167
687
  };
1168
687
  uint8_t regs = 0;
1169
1170
687
  read_byte(info, &regs, (*address)++);
1171
1172
  // The opcode of this instruction depends on
1173
  // the msb of its post byte.
1174
687
  if (regs & 0x80)
1175
360
    info->insn = M680X_INS_EXG;
1176
327
  else
1177
327
    info->insn = M680X_INS_TFR;
1178
1179
687
  add_reg_operand(info, g_tfr_exg12_reg0_ids[(regs >> 4) & 0x07]);
1180
687
  add_reg_operand(info, g_tfr_exg12_reg1_ids[regs & 0x07]);
1181
687
}
1182
1183
static void add_rel_operand(m680x_info *info, int16_t offset, uint16_t address)
1184
14.4k
{
1185
14.4k
  cs_m680x *m680x = &info->m680x;
1186
14.4k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1187
1188
14.4k
  op->type = M680X_OP_RELATIVE;
1189
14.4k
  op->size = 0;
1190
14.4k
  op->rel.offset = offset;
1191
14.4k
  op->rel.address = address;
1192
14.4k
}
1193
1194
static void relative8_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1195
12.5k
{
1196
12.5k
  int16_t offset = 0;
1197
1198
12.5k
  read_byte_sign_extended(info, &offset, (*address)++);
1199
12.5k
  add_rel_operand(info, offset, *address + offset);
1200
12.5k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1201
1202
12.5k
  if ((info->insn != M680X_INS_BRA) &&
1203
12.5k
    (info->insn != M680X_INS_BSR) &&
1204
12.5k
    (info->insn != M680X_INS_BRN))
1205
10.1k
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1206
12.5k
}
1207
1208
static void relative16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1209
1.85k
{
1210
1.85k
  uint16_t offset = 0;
1211
1212
1.85k
  read_word(info, &offset, *address);
1213
1.85k
  *address += 2;
1214
1.85k
  add_rel_operand(info, (int16_t)offset, *address + offset);
1215
1.85k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1216
1217
1.85k
  if ((info->insn != M680X_INS_LBRA) &&
1218
1.85k
    (info->insn != M680X_INS_LBSR) &&
1219
1.85k
    (info->insn != M680X_INS_LBRN))
1220
301
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1221
1.85k
}
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
11.0k
{
1231
11.0k
  cs_m680x *m680x = &info->m680x;
1232
11.0k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1233
1234
11.0k
  op->type = M680X_OP_INDEXED;
1235
11.0k
  set_operand_size(info, op, 1);
1236
11.0k
  op->idx.base_reg = base_reg;
1237
11.0k
  op->idx.offset_reg = M680X_REG_INVALID;
1238
11.0k
  op->idx.inc_dec = inc_dec;
1239
1240
11.0k
  if (inc_dec && post_inc_dec)
1241
1.71k
    op->idx.flags |= M680X_IDX_POST_INC_DEC;
1242
1243
11.0k
  if (offset_bits != M680X_OFFSET_NONE) {
1244
6.32k
    op->idx.offset = offset;
1245
6.32k
    op->idx.offset_addr = 0;
1246
6.32k
  }
1247
1248
11.0k
  op->idx.offset_bits = offset_bits;
1249
11.0k
  op->idx.flags |= (no_comma ? M680X_IDX_NO_COMMA : 0);
1250
11.0k
}
1251
1252
// M6800/1/2/3 indexed mode handler
1253
static void indexedX_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1254
2.61k
{
1255
2.61k
  uint8_t offset = 0;
1256
1257
2.61k
  read_byte(info, &offset, (*address)++);
1258
1259
2.61k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_8,
1260
2.61k
    (uint16_t)offset, false);
1261
2.61k
}
1262
1263
static void indexedY_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1264
807
{
1265
807
  uint8_t offset = 0;
1266
1267
807
  read_byte(info, &offset, (*address)++);
1268
1269
807
  add_indexed_operand(info, M680X_REG_Y, false, 0, M680X_OFFSET_BITS_8,
1270
807
    (uint16_t)offset, false);
1271
807
}
1272
1273
// M6809/M6309 indexed mode handler
1274
static void indexed09_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1275
16.9k
{
1276
16.9k
  cs_m680x *m680x = &info->m680x;
1277
16.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1278
16.9k
  uint8_t post_byte = 0;
1279
16.9k
  uint16_t offset = 0;
1280
16.9k
  int16_t soffset = 0;
1281
1282
16.9k
  read_byte(info, &post_byte, (*address)++);
1283
1284
16.9k
  op->type = M680X_OP_INDEXED;
1285
16.9k
  set_operand_size(info, op, 1);
1286
16.9k
  op->idx.base_reg = g_rr5_to_reg_ids[(post_byte >> 5) & 0x03];
1287
16.9k
  op->idx.offset_reg = M680X_REG_INVALID;
1288
1289
16.9k
  if (!(post_byte & 0x80)) {
1290
    // n5,R
1291
7.50k
    if ((post_byte & 0x10) == 0x10)
1292
3.54k
      op->idx.offset = post_byte | 0xfff0;
1293
3.96k
    else
1294
3.96k
      op->idx.offset = post_byte & 0x0f;
1295
1296
7.50k
    op->idx.offset_addr = op->idx.offset + *address;
1297
7.50k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1298
7.50k
  }
1299
9.42k
  else {
1300
9.42k
    if ((post_byte & 0x10) == 0x10)
1301
2.83k
      op->idx.flags |= M680X_IDX_INDIRECT;
1302
1303
    // indexed addressing
1304
9.42k
    switch (post_byte & 0x1f) {
1305
711
    case 0x00: // ,R+
1306
711
      op->idx.inc_dec = 1;
1307
711
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1308
711
      break;
1309
1310
214
    case 0x11: // [,R++]
1311
661
    case 0x01: // ,R++
1312
661
      op->idx.inc_dec = 2;
1313
661
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1314
661
      break;
1315
1316
461
    case 0x02: // ,-R
1317
461
      op->idx.inc_dec = -1;
1318
461
      break;
1319
1320
240
    case 0x13: // [,--R]
1321
604
    case 0x03: // ,--R
1322
604
      op->idx.inc_dec = -2;
1323
604
      break;
1324
1325
275
    case 0x14: // [,R]
1326
643
    case 0x04: // ,R
1327
643
      break;
1328
1329
213
    case 0x15: // [B,R]
1330
583
    case 0x05: // B,R
1331
583
      op->idx.offset_reg = M680X_REG_B;
1332
583
      break;
1333
1334
213
    case 0x16: // [A,R]
1335
781
    case 0x06: // A,R
1336
781
      op->idx.offset_reg = M680X_REG_A;
1337
781
      break;
1338
1339
290
    case 0x1c: // [n8,PCR]
1340
1.20k
    case 0x0c: // n8,PCR
1341
1.20k
      op->idx.base_reg = M680X_REG_PC;
1342
1.20k
      read_byte_sign_extended(info, &soffset, (*address)++);
1343
1.20k
      op->idx.offset_addr = offset + *address;
1344
1.20k
      op->idx.offset = soffset;
1345
1.20k
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1346
1.20k
      break;
1347
1348
293
    case 0x18: // [n8,R]
1349
914
    case 0x08: // n8,R
1350
914
      read_byte_sign_extended(info, &soffset, (*address)++);
1351
914
      op->idx.offset = soffset;
1352
914
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1353
914
      break;
1354
1355
252
    case 0x1d: // [n16,PCR]
1356
529
    case 0x0d: // n16,PCR
1357
529
      op->idx.base_reg = M680X_REG_PC;
1358
529
      read_word(info, &offset, *address);
1359
529
      *address += 2;
1360
529
      op->idx.offset_addr = offset + *address;
1361
529
      op->idx.offset = (int16_t)offset;
1362
529
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1363
529
      break;
1364
1365
315
    case 0x19: // [n16,R]
1366
1.51k
    case 0x09: // n16,R
1367
1.51k
      read_word(info, &offset, *address);
1368
1.51k
      *address += 2;
1369
1.51k
      op->idx.offset = (int16_t)offset;
1370
1.51k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1371
1.51k
      break;
1372
1373
311
    case 0x1b: // [D,R]
1374
598
    case 0x0b: // D,R
1375
598
      op->idx.offset_reg = M680X_REG_D;
1376
598
      break;
1377
1378
222
    case 0x1f: // [n16]
1379
222
      op->type = M680X_OP_EXTENDED;
1380
222
      op->ext.indirect = true;
1381
222
      read_word(info, &op->ext.address, *address);
1382
222
      *address += 2;
1383
222
      break;
1384
1385
0
    default:
1386
0
      op->idx.base_reg = M680X_REG_INVALID;
1387
0
      break;
1388
9.42k
    }
1389
9.42k
  }
1390
1391
16.9k
  if (((info->insn == M680X_INS_LEAU) ||
1392
16.9k
      (info->insn == M680X_INS_LEAS) ||
1393
16.9k
      (info->insn == M680X_INS_LEAX) ||
1394
16.9k
      (info->insn == M680X_INS_LEAY)) &&
1395
16.9k
    (m680x->operands[0].reg == M680X_REG_X ||
1396
2.22k
      (m680x->operands[0].reg == M680X_REG_Y)))
1397
    // Only LEAX and LEAY modify CC register
1398
1.25k
    add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1399
16.9k
}
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
20.2k
{
1413
20.2k
  cs_m680x *m680x = &info->m680x;
1414
20.2k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1415
20.2k
  uint8_t post_byte = 0;
1416
20.2k
  uint8_t offset8 = 0;
1417
1418
20.2k
  read_byte(info, &post_byte, (*address)++);
1419
1420
20.2k
  op->type = M680X_OP_INDEXED;
1421
20.2k
  set_operand_size(info, op, 1);
1422
20.2k
  op->idx.offset_reg = M680X_REG_INVALID;
1423
1424
20.2k
  if (!(post_byte & 0x20)) {
1425
    // n5,R      n5 is a 5-bit signed offset
1426
7.65k
    op->idx.base_reg = g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1427
1428
7.65k
    if ((post_byte & 0x10) == 0x10)
1429
2.71k
      op->idx.offset = post_byte | 0xfff0;
1430
4.94k
    else
1431
4.94k
      op->idx.offset = post_byte & 0x0f;
1432
1433
7.65k
    op->idx.offset_addr = op->idx.offset + *address;
1434
7.65k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1435
7.65k
  }
1436
12.6k
  else {
1437
12.6k
    if ((post_byte & 0xe0) == 0xe0)
1438
7.08k
      op->idx.base_reg =
1439
7.08k
        g_idx12_to_reg_ids[(post_byte >> 3) & 0x03];
1440
1441
12.6k
    switch (post_byte & 0xe7) {
1442
1.08k
    case 0xe0:
1443
2.20k
    case 0xe1: // n9,R
1444
2.20k
      read_byte(info, &offset8, (*address)++);
1445
2.20k
      op->idx.offset = offset8;
1446
1447
2.20k
      if (post_byte & 0x01) // sign extension
1448
1.12k
        op->idx.offset |= 0xff00;
1449
1450
2.20k
      op->idx.offset_bits = M680X_OFFSET_BITS_9;
1451
1452
2.20k
      if (op->idx.base_reg == M680X_REG_PC)
1453
1.07k
        op->idx.offset_addr = op->idx.offset + *address;
1454
1455
2.20k
      break;
1456
1457
819
    case 0xe3: // [n16,R]
1458
819
      op->idx.flags |= M680X_IDX_INDIRECT;
1459
1460
    // intentionally fall through
1461
2.43k
    case 0xe2: // n16,R
1462
2.43k
      read_word(info, (uint16_t *)&op->idx.offset, *address);
1463
2.43k
      (*address) += 2;
1464
2.43k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1465
1466
2.43k
      if (op->idx.base_reg == M680X_REG_PC)
1467
453
        op->idx.offset_addr = op->idx.offset + *address;
1468
1469
2.43k
      break;
1470
1471
437
    case 0xe4: // A,R
1472
996
    case 0xe5: // B,R
1473
1.29k
    case 0xe6: // D,R
1474
1.29k
      op->idx.offset_reg =
1475
1.29k
        g_or12_to_reg_ids[post_byte & 0x03];
1476
1.29k
      break;
1477
1478
1.14k
    case 0xe7: // [D,R]
1479
1.14k
      op->idx.offset_reg = M680X_REG_D;
1480
1.14k
      op->idx.flags |= M680X_IDX_INDIRECT;
1481
1.14k
      break;
1482
1483
5.52k
    default: // n,-r n,+r n,r- n,r+
1484
      // PC is not allowed in this mode
1485
5.52k
      op->idx.base_reg =
1486
5.52k
        g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1487
5.52k
      op->idx.inc_dec = post_byte & 0x0f;
1488
1489
5.52k
      if (op->idx.inc_dec & 0x08) // evtl. sign extend value
1490
3.07k
        op->idx.inc_dec |= 0xf0;
1491
1492
5.52k
      if (op->idx.inc_dec >= 0)
1493
2.44k
        op->idx.inc_dec++;
1494
1495
5.52k
      if (post_byte & 0x10)
1496
1.91k
        op->idx.flags |= M680X_IDX_POST_INC_DEC;
1497
1498
5.52k
      break;
1499
1500
12.6k
    }
1501
12.6k
  }
1502
20.2k
}
1503
1504
static void index_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1505
787
{
1506
787
  cs_m680x *m680x = &info->m680x;
1507
787
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1508
1509
787
  op->type = M680X_OP_CONSTANT;
1510
787
  read_byte(info, &op->const_val, (*address)++);
1511
787
};
1512
1513
static void direct_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1514
27.9k
{
1515
27.9k
  cs_m680x *m680x = &info->m680x;
1516
27.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1517
1518
27.9k
  op->type = M680X_OP_DIRECT;
1519
27.9k
  set_operand_size(info, op, 1);
1520
27.9k
  read_byte(info, &op->direct_addr, (*address)++);
1521
27.9k
};
1522
1523
static void extended_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1524
20.0k
{
1525
20.0k
  cs_m680x *m680x = &info->m680x;
1526
20.0k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1527
1528
20.0k
  op->type = M680X_OP_EXTENDED;
1529
20.0k
  set_operand_size(info, op, 1);
1530
20.0k
  read_word(info, &op->ext.address, *address);
1531
20.0k
  *address += 2;
1532
20.0k
}
1533
1534
static void immediate_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1535
12.9k
{
1536
12.9k
  cs_m680x *m680x = &info->m680x;
1537
12.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1538
12.9k
  uint16_t word = 0;
1539
12.9k
  int16_t sword = 0;
1540
1541
12.9k
  op->type = M680X_OP_IMMEDIATE;
1542
12.9k
  set_operand_size(info, op, 1);
1543
1544
12.9k
  switch (op->size) {
1545
10.5k
  case 1:
1546
10.5k
    read_byte_sign_extended(info, &sword, *address);
1547
10.5k
    op->imm = sword;
1548
10.5k
    break;
1549
1550
2.18k
  case 2:
1551
2.18k
    read_word(info, &word, *address);
1552
2.18k
    op->imm = (int16_t)word;
1553
2.18k
    break;
1554
1555
208
  case 4:
1556
208
    read_sdword(info, &op->imm, *address);
1557
208
    break;
1558
1559
0
  default:
1560
0
    op->imm = 0;
1561
0
    CS_ASSERT(0 && "Unexpected immediate byte size");
1562
12.9k
  }
1563
1564
12.9k
  *address += op->size;
1565
12.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
243
{
1570
243
  static const m680x_reg m680x_reg[] = {
1571
243
    M680X_REG_CC, M680X_REG_A, M680X_REG_B, M680X_REG_INVALID,
1572
243
  };
1573
1574
243
  uint8_t post_byte = 0;
1575
243
  cs_m680x *m680x = &info->m680x;
1576
243
  cs_m680x_op *op;
1577
1578
243
  read_byte(info, &post_byte, *address);
1579
243
  (*address)++;
1580
1581
  // operand[0] = register
1582
243
  add_reg_operand(info, m680x_reg[post_byte >> 6]);
1583
1584
  // operand[1] = bit index in source operand
1585
243
  op = &m680x->operands[m680x->op_count++];
1586
243
  op->type = M680X_OP_CONSTANT;
1587
243
  op->const_val = (post_byte >> 3) & 0x07;
1588
1589
  // operand[2] = bit index in destination operand
1590
243
  op = &m680x->operands[m680x->op_count++];
1591
243
  op->type = M680X_OP_CONSTANT;
1592
243
  op->const_val = post_byte & 0x07;
1593
1594
243
  direct_hdlr(MI, info, address);
1595
243
}
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
459
{
1600
459
  static const uint8_t inc_dec_r0[] = {
1601
459
    1, -1, 1, 0,
1602
459
  };
1603
459
  static const uint8_t inc_dec_r1[] = {
1604
459
    1, -1, 0, 1,
1605
459
  };
1606
459
  uint8_t regs = 0;
1607
459
  uint8_t index = (MI->Opcode & 0xff) - 0x38;
1608
1609
459
  read_byte(info, &regs, *address);
1610
1611
459
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs >> 4], true,
1612
459
    inc_dec_r0[index], M680X_OFFSET_NONE, 0, true);
1613
459
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs & 0x0f], true,
1614
459
    inc_dec_r1[index], M680X_OFFSET_NONE, 0, true);
1615
1616
459
  add_reg_to_rw_list(MI, M680X_REG_W, READ | WRITE);
1617
459
}
1618
1619
static void opidx_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1620
1.50k
{
1621
1.50k
  cs_m680x *m680x = &info->m680x;
1622
1.50k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1623
1624
  // bit index is coded in Opcode
1625
1.50k
  op->type = M680X_OP_CONSTANT;
1626
1.50k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1627
1.50k
}
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
2.40k
{
1634
2.40k
  cs_m680x *m680x = &info->m680x;
1635
2.40k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1636
1637
  // bit index is coded in Opcode
1638
2.40k
  op->type = M680X_OP_CONSTANT;
1639
2.40k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1640
2.40k
  direct_hdlr(MI, info, address);
1641
2.40k
  relative8_hdlr(MI, info, address);
1642
1643
2.40k
  add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1644
2.40k
}
1645
1646
static void indexedX0_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1647
3.32k
{
1648
3.32k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_NONE,
1649
3.32k
    0, false);
1650
3.32k
}
1651
1652
static void indexedX16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1653
1.18k
{
1654
1.18k
  uint16_t offset = 0;
1655
1656
1.18k
  read_word(info, &offset, *address);
1657
1.18k
  *address += 2;
1658
1.18k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_16,
1659
1.18k
    offset, false);
1660
1.18k
}
1661
1662
static void imm_rel_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1663
946
{
1664
946
  immediate_hdlr(MI, info, address);
1665
946
  relative8_hdlr(MI, info, address);
1666
946
}
1667
1668
static void indexedS_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1669
748
{
1670
748
  uint8_t offset = 0;
1671
1672
748
  read_byte(info, &offset, (*address)++);
1673
1674
748
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_8,
1675
748
    (uint16_t)offset, false);
1676
748
}
1677
1678
static void indexedS16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1679
231
{
1680
231
  uint16_t offset = 0;
1681
1682
231
  read_word(info, &offset, *address);
1683
231
  address += 2;
1684
1685
231
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_16,
1686
231
    offset, false);
1687
231
}
1688
1689
static void indexedX0p_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1690
506
{
1691
506
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_NONE,
1692
506
    0, true);
1693
506
}
1694
1695
static void indexedXp_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1696
742
{
1697
742
  uint8_t offset = 0;
1698
1699
742
  read_byte(info, &offset, (*address)++);
1700
1701
742
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_BITS_8,
1702
742
    (uint16_t)offset, false);
1703
742
}
1704
1705
static void imm_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1706
2.93k
{
1707
2.93k
  cs_m680x *m680x = &info->m680x;
1708
2.93k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1709
1710
2.93k
  indexed12_hdlr(MI, info, address);
1711
2.93k
  op->type = M680X_OP_IMMEDIATE;
1712
1713
2.93k
  if (info->insn == M680X_INS_MOVW) {
1714
1.22k
    uint16_t imm16 = 0;
1715
1716
1.22k
    read_word(info, &imm16, *address);
1717
1.22k
    op->imm = (int16_t)imm16;
1718
1.22k
    op->size = 2;
1719
1.22k
  }
1720
1.71k
  else {
1721
1.71k
    uint8_t imm8 = 0;
1722
1723
1.71k
    read_byte(info, &imm8, *address);
1724
1.71k
    op->imm = (int8_t)imm8;
1725
1.71k
    op->size = 1;
1726
1.71k
  }
1727
1728
2.93k
  set_operand_size(info, op, 1);
1729
2.93k
}
1730
1731
static void ext_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1732
796
{
1733
796
  cs_m680x *m680x = &info->m680x;
1734
796
  cs_m680x_op *op0 = &m680x->operands[m680x->op_count++];
1735
796
  uint16_t imm16 = 0;
1736
1737
796
  indexed12_hdlr(MI, info, address);
1738
796
  read_word(info, &imm16, *address);
1739
796
  op0->type = M680X_OP_EXTENDED;
1740
796
  op0->ext.address = (int16_t)imm16;
1741
796
  set_operand_size(info, op0, 1);
1742
796
}
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
845
{
1748
845
  static const m680x_reg index_to_reg_id[] = {
1749
845
    M680X_REG_A, M680X_REG_B, M680X_REG_INVALID, M680X_REG_INVALID,
1750
845
    M680X_REG_D, M680X_REG_X, M680X_REG_Y, M680X_REG_S,
1751
845
  };
1752
845
  static const m680x_insn index_to_insn_id[] = {
1753
845
    M680X_INS_DBEQ, M680X_INS_DBNE, M680X_INS_TBEQ, M680X_INS_TBNE,
1754
845
    M680X_INS_IBEQ, M680X_INS_IBNE, M680X_INS_ILLGL, M680X_INS_ILLGL
1755
845
  };
1756
845
  cs_m680x *m680x = &info->m680x;
1757
845
  uint8_t post_byte = 0;
1758
845
  uint8_t rel = 0;
1759
845
  cs_m680x_op *op;
1760
1761
845
  read_byte(info, &post_byte, (*address)++);
1762
1763
845
  info->insn = index_to_insn_id[(post_byte >> 5) & 0x07];
1764
1765
845
  if (info->insn == M680X_INS_ILLGL) {
1766
0
    illegal_hdlr(MI, info, address);
1767
0
  };
1768
1769
845
  read_byte(info, &rel, (*address)++);
1770
1771
845
  add_reg_operand(info, index_to_reg_id[post_byte & 0x07]);
1772
1773
845
  op = &m680x->operands[m680x->op_count++];
1774
1775
845
  op->type = M680X_OP_RELATIVE;
1776
1777
845
  op->rel.offset = (post_byte & 0x10) ? 0xff00 | rel : rel;
1778
1779
845
  op->rel.address = *address + op->rel.offset;
1780
1781
845
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1782
845
}
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
186k
{
1824
186k
  cs_m680x *m680x = &info->m680x;
1825
186k
  cs_detail *detail = MI->flat_insn->detail;
1826
186k
  uint16_t base_address = address;
1827
186k
  insn_desc insn_description;
1828
186k
  e_access_mode access_mode;
1829
1830
186k
  if (detail != NULL) {
1831
186k
    memset(detail, 0, offsetof(cs_detail, m680x)+sizeof(cs_m680x));
1832
186k
  }
1833
1834
186k
  memset(&insn_description, 0, sizeof(insn_description));
1835
186k
  memset(m680x, 0, sizeof(*m680x));
1836
186k
  info->insn_size = 1;
1837
1838
186k
  if (decode_insn(info, address, &insn_description)) {
1839
163k
    m680x_reg reg;
1840
1841
163k
    if (insn_description.opcode > 0xff)
1842
9.83k
      address += 2; // 8-bit opcode + page prefix
1843
154k
    else
1844
154k
      address++; // 8-bit opcode only
1845
1846
163k
    info->insn = insn_description.insn;
1847
1848
163k
    MCInst_setOpcode(MI, insn_description.opcode);
1849
1850
163k
    reg = g_insn_props[info->insn].reg0;
1851
1852
163k
    if (reg != M680X_REG_INVALID) {
1853
88.6k
      if (reg == M680X_REG_HX &&
1854
88.6k
        (!info->cpu->reg_byte_size[reg]))
1855
378
        reg = M680X_REG_X;
1856
1857
88.6k
      add_reg_operand(info, reg);
1858
      // First (or second) operand is a register which is
1859
      // part of the mnemonic
1860
88.6k
      m680x->flags |= M680X_FIRST_OP_IN_MNEM;
1861
88.6k
      reg = g_insn_props[info->insn].reg1;
1862
1863
88.6k
      if (reg != M680X_REG_INVALID) {
1864
2.54k
        if (reg == M680X_REG_HX &&
1865
2.54k
          (!info->cpu->reg_byte_size[reg]))
1866
526
          reg = M680X_REG_X;
1867
1868
2.54k
        add_reg_operand(info, reg);
1869
2.54k
        m680x->flags |= M680X_SECOND_OP_IN_MNEM;
1870
2.54k
      }
1871
88.6k
    }
1872
1873
    // Call addressing mode specific instruction handler
1874
163k
    (g_insn_handler[insn_description.hid[0]])(MI, info,
1875
163k
      &address);
1876
163k
    (g_insn_handler[insn_description.hid[1]])(MI, info,
1877
163k
      &address);
1878
1879
163k
    add_insn_group(detail, g_insn_props[info->insn].group);
1880
1881
163k
    if (g_insn_props[info->insn].cc_modified &&
1882
163k
      (info->cpu->insn_cc_not_modified[0] != info->insn) &&
1883
163k
      (info->cpu->insn_cc_not_modified[1] != info->insn))
1884
102k
      add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1885
1886
163k
    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
163k
    if ((info->cpu->insn_cc_not_modified[0] == info->insn) ||
1891
163k
      (info->cpu->insn_cc_not_modified[1] == info->insn))
1892
1.50k
      access_mode = rmmm;
1893
1894
163k
    build_regs_read_write_counts(MI, info, access_mode);
1895
163k
    add_operators_access(MI, info, access_mode);
1896
1897
163k
    if (g_insn_props[info->insn].update_reg_access)
1898
17.8k
      set_changed_regs_read_write_counts(MI, info);
1899
1900
163k
    info->insn_size = (uint8_t)insn_description.insn_size;
1901
1902
163k
    return info->insn_size;
1903
163k
  }
1904
22.9k
  else
1905
22.9k
    MCInst_setOpcode(MI, insn_description.opcode);
1906
1907
  // Illegal instruction
1908
22.9k
  address = base_address;
1909
22.9k
  illegal_hdlr(MI, info, &address);
1910
22.9k
  return 1;
1911
186k
}
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
186k
{
2137
186k
  if (cpu_type == M680X_CPU_TYPE_INVALID) {
2138
0
    return false;
2139
0
  }
2140
2141
186k
  info->code = code;
2142
186k
  info->size = code_len;
2143
186k
  info->offset = address;
2144
186k
  info->cpu_type = cpu_type;
2145
2146
186k
  info->cpu = &g_cpu_tables[info->cpu_type];
2147
2148
186k
  return true;
2149
186k
}
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
186k
{
2154
186k
  unsigned int insn_size = 0;
2155
186k
  e_cpu_type cpu_type = M680X_CPU_TYPE_INVALID; // No default CPU type
2156
186k
  cs_struct *handle = (cs_struct *)ud;
2157
186k
  m680x_info *info = (m680x_info *)handle->printer_info;
2158
2159
186k
  MCInst_clear(MI);
2160
2161
186k
  if (handle->mode & CS_MODE_M680X_6800)
2162
646
    cpu_type = M680X_CPU_TYPE_6800;
2163
2164
186k
  else if (handle->mode & CS_MODE_M680X_6801)
2165
541
    cpu_type = M680X_CPU_TYPE_6801;
2166
2167
185k
  else if (handle->mode & CS_MODE_M680X_6805)
2168
6.75k
    cpu_type = M680X_CPU_TYPE_6805;
2169
2170
178k
  else if (handle->mode & CS_MODE_M680X_6808)
2171
9.30k
    cpu_type = M680X_CPU_TYPE_6808;
2172
2173
169k
  else if (handle->mode & CS_MODE_M680X_HCS08)
2174
7.51k
    cpu_type = M680X_CPU_TYPE_HCS08;
2175
2176
162k
  else if (handle->mode & CS_MODE_M680X_6809)
2177
23.6k
    cpu_type = M680X_CPU_TYPE_6809;
2178
2179
138k
  else if (handle->mode & CS_MODE_M680X_6301)
2180
1.27k
    cpu_type = M680X_CPU_TYPE_6301;
2181
2182
137k
  else if (handle->mode & CS_MODE_M680X_6309)
2183
53.0k
    cpu_type = M680X_CPU_TYPE_6309;
2184
2185
84.2k
  else if (handle->mode & CS_MODE_M680X_6811)
2186
8.46k
    cpu_type = M680X_CPU_TYPE_6811;
2187
2188
75.7k
  else if (handle->mode & CS_MODE_M680X_CPU12)
2189
75.7k
    cpu_type = M680X_CPU_TYPE_CPU12;
2190
2191
186k
  if (cpu_type != M680X_CPU_TYPE_INVALID &&
2192
186k
    m680x_setup_internals(info, cpu_type, (uint16_t)address, code,
2193
186k
      (uint16_t)code_len))
2194
186k
    insn_size = m680x_disassemble(MI, info, (uint16_t)address);
2195
2196
186k
  if (insn_size == 0) {
2197
0
    *size = 1;
2198
0
    return false;
2199
0
  }
2200
2201
  // Make sure we always stay within range
2202
186k
  if (insn_size > code_len) {
2203
28
    *size = (uint16_t)code_len;
2204
28
    return false;
2205
28
  }
2206
186k
  else
2207
186k
    *size = (uint16_t)insn_size;
2208
2209
186k
  return true;
2210
186k
}
2211
2212
cs_err M680X_disassembler_init(cs_struct *ud)
2213
1.97k
{
2214
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  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
1.97k
  return CS_ERR_OK;
2282
1.97k
}
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