Coverage Report

Created: 2026-12-01 07:03

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