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
423k
#define READ CS_AC_READ
113
551k
#define WRITE CS_AC_WRITE
114
541k
#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
1.02M
{
162
1.02M
  if (address < info->offset ||
163
1.02M
      (uint32_t)(address - info->offset) >= info->size)
164
    // out of code buffer range
165
2.15k
    return false;
166
167
1.01M
  *byte = info->code[address - info->offset];
168
169
1.01M
  return true;
170
1.02M
}
171
172
static bool read_byte_sign_extended(const m680x_info *info, int16_t *word,
173
            uint16_t address)
174
62.3k
{
175
62.3k
  if (address < info->offset ||
176
62.3k
      (uint32_t)(address - info->offset) >= info->size)
177
    // out of code buffer range
178
0
    return false;
179
180
62.3k
  *word = (int16_t)info->code[address - info->offset];
181
182
62.3k
  if (*word & 0x80)
183
24.3k
    *word |= 0xFF00;
184
185
62.3k
  return true;
186
62.3k
}
187
188
static bool read_word(const m680x_info *info, uint16_t *word, uint16_t address)
189
75.4k
{
190
75.4k
  if (address < info->offset ||
191
75.4k
      (uint32_t)(address + 1 - info->offset) >= info->size)
192
    // out of code buffer range
193
34
    return false;
194
195
75.4k
  *word = (uint16_t)info->code[address - info->offset] << 8;
196
75.4k
  *word |= (uint16_t)info->code[address + 1 - info->offset];
197
198
75.4k
  return true;
199
75.4k
}
200
201
static bool read_sdword(const m680x_info *info, int32_t *sdword,
202
      uint16_t address)
203
833
{
204
833
  if (address < info->offset ||
205
833
      (uint32_t)(address + 3 - info->offset) >= info->size)
206
    // out of code buffer range
207
0
    return false;
208
209
833
  *sdword = (uint32_t)info->code[address - info->offset] << 24;
210
833
  *sdword |= (uint32_t)info->code[address + 1 - info->offset] << 16;
211
833
  *sdword |= (uint32_t)info->code[address + 2 - info->offset] << 8;
212
833
  *sdword |= (uint32_t)info->code[address + 3 - info->offset];
213
214
833
  return true;
215
833
}
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
152k
{
224
  // As part of the algorithm last may get negative.
225
  // => signed integer has to be used.
226
152k
  int first = 0;
227
152k
  int last = (int)table_size - 1;
228
152k
  int middle = (first + last) / 2;
229
230
770k
  while (first <= last) {
231
715k
    if (inst_pageX_table[middle].opcode < opcode) {
232
227k
      first = middle + 1;
233
488k
    } else if (inst_pageX_table[middle].opcode == opcode) {
234
97.3k
      return middle; /* item found */
235
97.3k
    } else
236
390k
      last = middle - 1;
237
238
618k
    middle = (first + last) / 2;
239
618k
  }
240
241
54.9k
  if (first > last)
242
54.9k
    return -1; /* item not found */
243
244
0
  return -2;
245
54.9k
}
246
247
void M680X_get_insn_id(cs_struct *handle, cs_insn *insn, unsigned int id)
248
424k
{
249
424k
  const m680x_info *const info = (const m680x_info *)handle->printer_info;
250
424k
  const cpu_tables *cpu = info->cpu;
251
424k
  uint8_t insn_prefix = (id >> 8) & 0xff;
252
  // opcode is the first instruction byte without the prefix.
253
424k
  uint8_t opcode = id & 0xff;
254
424k
  int index;
255
424k
  int i;
256
257
424k
  insn->id = M680X_INS_ILLGL;
258
259
994k
  for (i = 0; i < ARR_SIZE(cpu->pageX_prefix); ++i) {
260
975k
    if (cpu->pageX_table_size[i] == 0 ||
261
605k
        (cpu->inst_pageX_table[i] == NULL))
262
369k
      break;
263
264
605k
    if (cpu->pageX_prefix[i] == insn_prefix) {
265
36.2k
      index = binary_search(cpu->inst_pageX_table[i],
266
36.2k
                cpu->pageX_table_size[i], opcode);
267
36.2k
      insn->id =
268
36.2k
        (index >= 0) ?
269
29.1k
          cpu->inst_pageX_table[i][index].insn :
270
36.2k
          M680X_INS_ILLGL;
271
36.2k
      return;
272
36.2k
    }
273
605k
  }
274
275
388k
  if (insn_prefix != 0)
276
0
    return;
277
278
388k
  insn->id = cpu->inst_page1_table[id].insn;
279
280
388k
  if (insn->id != M680X_INS_ILLGL)
281
351k
    return;
282
283
  // Check if opcode byte is present in an overlay table
284
57.0k
  for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
285
55.1k
    if (cpu->overlay_table_size[i] == 0 ||
286
39.9k
        (cpu->inst_overlay_table[i] == NULL))
287
15.2k
      break;
288
289
39.9k
    if ((index = binary_search(cpu->inst_overlay_table[i],
290
39.9k
             cpu->overlay_table_size[i],
291
39.9k
             opcode)) >= 0) {
292
19.5k
      insn->id = cpu->inst_overlay_table[i][index].insn;
293
19.5k
      return;
294
19.5k
    }
295
39.9k
  }
296
36.6k
}
297
298
static void add_insn_group(cs_detail *detail, m680x_group_type group)
299
417k
{
300
417k
  if (detail != NULL && (group != M680X_GRP_INVALID) &&
301
88.9k
      (group != M680X_GRP_ENDING))
302
88.9k
    detail->groups[detail->groups_count++] = (uint8_t)group;
303
417k
}
304
305
static bool exists_reg_list(uint16_t *regs, uint8_t count, m680x_reg reg)
306
1.19M
{
307
1.19M
  uint8_t i;
308
309
1.97M
  for (i = 0; i < count; ++i) {
310
819k
    if (regs[i] == (uint16_t)reg)
311
35.9k
      return true;
312
819k
  }
313
314
1.16M
  return false;
315
1.19M
}
316
317
static void add_reg_to_rw_list(MCInst *MI, m680x_reg reg, e_access access)
318
783k
{
319
783k
  cs_detail *detail = MI->flat_insn->detail;
320
321
783k
  if (detail == NULL || (reg == M680X_REG_INVALID))
322
0
    return;
323
324
783k
  switch (access) {
325
412k
  case MODIFY:
326
412k
    if (!exists_reg_list(detail->regs_read, detail->regs_read_count,
327
412k
             reg))
328
405k
      detail->regs_read[detail->regs_read_count++] =
329
405k
        (uint16_t)reg;
330
331
    // intentionally fall through
332
333
522k
  case WRITE:
334
522k
    if (!exists_reg_list(detail->regs_write,
335
522k
             detail->regs_write_count, reg))
336
512k
      detail->regs_write[detail->regs_write_count++] =
337
512k
        (uint16_t)reg;
338
339
522k
    break;
340
341
261k
  case READ:
342
261k
    if (!exists_reg_list(detail->regs_read, detail->regs_read_count,
343
261k
             reg))
344
241k
      detail->regs_read[detail->regs_read_count++] =
345
241k
        (uint16_t)reg;
346
347
261k
    break;
348
349
0
  case UNCHANGED:
350
0
  default:
351
0
    break;
352
783k
  }
353
783k
}
354
355
static void update_am_reg_list(MCInst *MI, m680x_info *info, cs_m680x_op *op,
356
             e_access access)
357
558k
{
358
558k
  if (MI->flat_insn->detail == NULL)
359
0
    return;
360
361
558k
  switch (op->type) {
362
250k
  case M680X_OP_REGISTER:
363
250k
    add_reg_to_rw_list(MI, op->reg, access);
364
250k
    break;
365
366
111k
  case M680X_OP_INDEXED:
367
111k
    add_reg_to_rw_list(MI, op->idx.base_reg, READ);
368
369
111k
    if (op->idx.base_reg == M680X_REG_X &&
370
45.3k
        info->cpu->reg_byte_size[M680X_REG_H])
371
12.3k
      add_reg_to_rw_list(MI, M680X_REG_H, READ);
372
373
111k
    if (op->idx.offset_reg != M680X_REG_INVALID)
374
11.2k
      add_reg_to_rw_list(MI, op->idx.offset_reg, READ);
375
376
111k
    if (op->idx.inc_dec) {
377
25.4k
      add_reg_to_rw_list(MI, op->idx.base_reg, WRITE);
378
379
25.4k
      if (op->idx.base_reg == M680X_REG_X &&
380
7.05k
          info->cpu->reg_byte_size[M680X_REG_H])
381
2.07k
        add_reg_to_rw_list(MI, M680X_REG_H, WRITE);
382
25.4k
    }
383
384
111k
    break;
385
386
196k
  default:
387
196k
    break;
388
558k
  }
389
558k
}
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
1.17M
{
464
1.17M
  int idx = (operator_index > 3) ? 3 : operator_index;
465
466
1.17M
  return g_access_mode_to_access[idx][access_mode];
467
1.17M
}
468
469
static void build_regs_read_write_counts(MCInst *MI, m680x_info *info,
470
           e_access_mode access_mode)
471
383k
{
472
383k
  cs_m680x *m680x = &info->m680x;
473
383k
  int i;
474
475
383k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count))
476
57.6k
    return;
477
478
883k
  for (i = 0; i < m680x->op_count; ++i) {
479
558k
    e_access access = get_access(i, access_mode);
480
558k
    update_am_reg_list(MI, info, &m680x->operands[i], access);
481
558k
  }
482
325k
}
483
484
static void add_operators_access(MCInst *MI, m680x_info *info,
485
         e_access_mode access_mode)
486
383k
{
487
383k
  cs_m680x *m680x = &info->m680x;
488
383k
  int offset = 0;
489
383k
  int i;
490
491
383k
  if (MI->flat_insn->detail == NULL || (!m680x->op_count) ||
492
325k
      (access_mode == uuuu))
493
94.8k
    return;
494
495
808k
  for (i = 0; i < m680x->op_count; ++i) {
496
520k
    e_access access;
497
498
    // Ugly fix: MULD has a register operand, an immediate operand
499
    // AND an implicitly changed register W
500
520k
    if (info->insn == M680X_INS_MULD && (i == 1))
501
637
      offset = 1;
502
503
520k
    access = get_access(i + offset, access_mode);
504
520k
    m680x->operands[i].access = access;
505
520k
  }
506
288k
}
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
37.2k
{
516
  //TABLE
517
2.04M
#define EOL M680X_REG_INVALID
518
37.2k
  static const insn_to_changed_regs changed_regs[] = {
519
37.2k
    { M680X_INS_BSR, mmmm, { M680X_REG_S, EOL } },
520
37.2k
    { M680X_INS_CALL, mmmm, { M680X_REG_S, EOL } },
521
37.2k
    {
522
37.2k
      M680X_INS_CWAI,
523
37.2k
      mrrr,
524
37.2k
      { M680X_REG_S, M680X_REG_PC, M680X_REG_U, M680X_REG_Y,
525
37.2k
        M680X_REG_X, M680X_REG_DP, M680X_REG_D, M680X_REG_CC,
526
37.2k
        EOL },
527
37.2k
    },
528
37.2k
    { M680X_INS_DAA, mrrr, { M680X_REG_A, EOL } },
529
37.2k
    { M680X_INS_DIV,
530
37.2k
      mmrr,
531
37.2k
      { M680X_REG_A, M680X_REG_H, M680X_REG_X, EOL } },
532
37.2k
    { M680X_INS_EDIV,
533
37.2k
      mmrr,
534
37.2k
      { M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL } },
535
37.2k
    { M680X_INS_EDIVS,
536
37.2k
      mmrr,
537
37.2k
      { M680X_REG_D, M680X_REG_Y, M680X_REG_X, EOL } },
538
37.2k
    { M680X_INS_EMACS, mrrr, { M680X_REG_X, M680X_REG_Y, EOL } },
539
37.2k
    { M680X_INS_EMAXM, rrrr, { M680X_REG_D, EOL } },
540
37.2k
    { M680X_INS_EMINM, rrrr, { M680X_REG_D, EOL } },
541
37.2k
    { M680X_INS_EMUL, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
542
37.2k
    { M680X_INS_EMULS, mmrr, { M680X_REG_D, M680X_REG_Y, EOL } },
543
37.2k
    { M680X_INS_ETBL, wmmm, { M680X_REG_A, M680X_REG_B, EOL } },
544
37.2k
    { M680X_INS_FDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
545
37.2k
    { M680X_INS_IDIV, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
546
37.2k
    { M680X_INS_IDIVS, mmmm, { M680X_REG_D, M680X_REG_X, EOL } },
547
37.2k
    { M680X_INS_JSR, mmmm, { M680X_REG_S, EOL } },
548
37.2k
    { M680X_INS_LBSR, mmmm, { M680X_REG_S, EOL } },
549
37.2k
    { M680X_INS_MAXM, rrrr, { M680X_REG_A, EOL } },
550
37.2k
    { M680X_INS_MINM, rrrr, { M680X_REG_A, EOL } },
551
37.2k
    { M680X_INS_MEM,
552
37.2k
      mmrr,
553
37.2k
      { M680X_REG_X, M680X_REG_Y, M680X_REG_A, EOL } },
554
37.2k
    { M680X_INS_MUL, mmmm, { M680X_REG_A, M680X_REG_B, EOL } },
555
37.2k
    { M680X_INS_MULD, mwrr, { M680X_REG_D, M680X_REG_W, EOL } },
556
37.2k
    { M680X_INS_PSHA, rmmm, { M680X_REG_A, M680X_REG_S, EOL } },
557
37.2k
    { M680X_INS_PSHB, rmmm, { M680X_REG_B, M680X_REG_S, EOL } },
558
37.2k
    { M680X_INS_PSHC, rmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
559
37.2k
    { M680X_INS_PSHD, rmmm, { M680X_REG_D, M680X_REG_S, EOL } },
560
37.2k
    { M680X_INS_PSHH, rmmm, { M680X_REG_H, M680X_REG_S, EOL } },
561
37.2k
    { M680X_INS_PSHX, rmmm, { M680X_REG_X, M680X_REG_S, EOL } },
562
37.2k
    { M680X_INS_PSHY, rmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
563
37.2k
    { M680X_INS_PULA, wmmm, { M680X_REG_A, M680X_REG_S, EOL } },
564
37.2k
    { M680X_INS_PULB, wmmm, { M680X_REG_B, M680X_REG_S, EOL } },
565
37.2k
    { M680X_INS_PULC, wmmm, { M680X_REG_CC, M680X_REG_S, EOL } },
566
37.2k
    { M680X_INS_PULD, wmmm, { M680X_REG_D, M680X_REG_S, EOL } },
567
37.2k
    { M680X_INS_PULH, wmmm, { M680X_REG_H, M680X_REG_S, EOL } },
568
37.2k
    { M680X_INS_PULX, wmmm, { M680X_REG_X, M680X_REG_S, EOL } },
569
37.2k
    { M680X_INS_PULY, wmmm, { M680X_REG_Y, M680X_REG_S, EOL } },
570
37.2k
    { M680X_INS_REV,
571
37.2k
      mmrr,
572
37.2k
      { M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL } },
573
37.2k
    { M680X_INS_REVW,
574
37.2k
      mmmm,
575
37.2k
      { M680X_REG_A, M680X_REG_X, M680X_REG_Y, EOL } },
576
37.2k
    { M680X_INS_RTC, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
577
37.2k
    {
578
37.2k
      M680X_INS_RTI,
579
37.2k
      mwww,
580
37.2k
      { M680X_REG_S, M680X_REG_CC, M680X_REG_B, M680X_REG_A,
581
37.2k
        M680X_REG_DP, M680X_REG_X, M680X_REG_Y, M680X_REG_U,
582
37.2k
        M680X_REG_PC, EOL },
583
37.2k
    },
584
37.2k
    { M680X_INS_RTS, mwww, { M680X_REG_S, M680X_REG_PC, EOL } },
585
37.2k
    { M680X_INS_SEX, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
586
37.2k
    { M680X_INS_SEXW, rwww, { M680X_REG_W, M680X_REG_D, EOL } },
587
37.2k
    { M680X_INS_SWI,
588
37.2k
      mmrr,
589
37.2k
      { M680X_REG_S, M680X_REG_PC, M680X_REG_U, M680X_REG_Y,
590
37.2k
        M680X_REG_X, M680X_REG_DP, M680X_REG_A, M680X_REG_B,
591
37.2k
        M680X_REG_CC, EOL } },
592
37.2k
    {
593
37.2k
      M680X_INS_SWI2,
594
37.2k
      mmrr,
595
37.2k
      { M680X_REG_S, M680X_REG_PC, M680X_REG_U, M680X_REG_Y,
596
37.2k
        M680X_REG_X, M680X_REG_DP, M680X_REG_A, M680X_REG_B,
597
37.2k
        M680X_REG_CC, EOL },
598
37.2k
    },
599
37.2k
    {
600
37.2k
      M680X_INS_SWI3,
601
37.2k
      mmrr,
602
37.2k
      { M680X_REG_S, M680X_REG_PC, M680X_REG_U, M680X_REG_Y,
603
37.2k
        M680X_REG_X, M680X_REG_DP, M680X_REG_A, M680X_REG_B,
604
37.2k
        M680X_REG_CC, EOL },
605
37.2k
    },
606
37.2k
    { M680X_INS_TBL, wrrr, { M680X_REG_A, M680X_REG_B, EOL } },
607
37.2k
    { M680X_INS_WAI,
608
37.2k
      mrrr,
609
37.2k
      { M680X_REG_S, M680X_REG_PC, M680X_REG_X, M680X_REG_A,
610
37.2k
        M680X_REG_B, M680X_REG_CC, EOL } },
611
37.2k
    { M680X_INS_WAV,
612
37.2k
      rmmm,
613
37.2k
      { M680X_REG_A, M680X_REG_B, M680X_REG_X, M680X_REG_Y, EOL } },
614
37.2k
    { M680X_INS_WAVR,
615
37.2k
      rmmm,
616
37.2k
      { M680X_REG_A, M680X_REG_B, M680X_REG_X, M680X_REG_Y, EOL } },
617
37.2k
  };
618
619
37.2k
  int i, j;
620
621
37.2k
  if (MI->flat_insn->detail == NULL)
622
0
    return;
623
624
1.93M
  for (i = 0; i < ARR_SIZE(changed_regs); ++i) {
625
1.90M
    if (info->insn == changed_regs[i].insn) {
626
37.2k
      e_access_mode access_mode = changed_regs[i].access_mode;
627
628
140k
      for (j = 0; changed_regs[i].regs[j] != EOL; ++j) {
629
103k
        e_access access;
630
631
103k
        m680x_reg reg = changed_regs[i].regs[j];
632
633
103k
        if (!info->cpu->reg_byte_size[reg]) {
634
7.84k
          if (info->insn != M680X_INS_MUL)
635
7.39k
            continue;
636
637
          // Hack for M68HC05: MUL uses reg. A,X
638
444
          reg = M680X_REG_X;
639
444
        }
640
641
96.1k
        access = get_access(j, access_mode);
642
96.1k
        add_reg_to_rw_list(MI, reg, access);
643
96.1k
      }
644
37.2k
    }
645
1.90M
  }
646
647
37.2k
#undef EOL
648
37.2k
}
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
49.8k
{
663
49.8k
  uint8_t ir = 0;
664
49.8k
  uint8_t post_byte;
665
666
  // Read the indexed addressing post byte.
667
49.8k
  if (!read_byte(info, &post_byte, address))
668
201
    return -1;
669
670
  // Depending on the indexed addressing mode more bytes have to be read.
671
49.6k
  switch (post_byte & 0x9F) {
672
1.36k
  case 0x87:
673
3.30k
  case 0x8A:
674
4.88k
  case 0x8E:
675
6.52k
  case 0x8F:
676
7.11k
  case 0x90:
677
7.65k
  case 0x92:
678
8.18k
  case 0x97:
679
8.78k
  case 0x9A:
680
9.29k
  case 0x9E:
681
9.29k
    return -1; // illegal indexed post bytes
682
683
1.68k
  case 0x88: // n8,R
684
2.76k
  case 0x8C: // n8,PCR
685
3.66k
  case 0x98: // [n8,R]
686
4.24k
  case 0x9C: // [n8,PCR]
687
4.24k
    if (!read_byte(info, &ir, address + 1))
688
44
      return -1;
689
4.20k
    return 2;
690
691
993
  case 0x89: // n16,R
692
2.36k
  case 0x8D: // n16,PCR
693
3.18k
  case 0x99: // [n16,R]
694
3.79k
  case 0x9D: // [n16,PCR]
695
3.79k
    if (!read_byte(info, &ir, address + 2))
696
70
      return -1;
697
3.72k
    return 3;
698
699
1.85k
  case 0x9F: // [n]
700
1.85k
    if ((post_byte & 0x60) != 0 ||
701
635
        !read_byte(info, &ir, address + 2))
702
1.23k
      return -1;
703
625
    return 3;
704
49.6k
  }
705
706
  // Any other indexed post byte is valid and
707
  // no additional bytes have to be read.
708
30.4k
  return 1;
709
49.6k
}
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
49.0k
{
717
49.0k
  uint8_t ir;
718
49.0k
  uint8_t post_byte;
719
720
  // Read the indexed addressing post byte.
721
49.0k
  if (!read_byte(info, &post_byte, address))
722
174
    return -1;
723
724
  // Depending on the indexed addressing mode more bytes have to be read.
725
48.8k
  if (!(post_byte & 0x20)) // n5,R
726
14.7k
    return 1;
727
728
34.1k
  switch (post_byte & 0xe7) {
729
2.79k
  case 0xe0:
730
6.29k
  case 0xe1: // n9,R
731
6.29k
    if (is_subset)
732
516
      return -1;
733
734
5.78k
    if (!read_byte(info, &ir, address))
735
0
      return -1;
736
5.78k
    return 2;
737
738
2.96k
  case 0xe2: // n16,R
739
6.76k
  case 0xe3: // [n16,R]
740
6.76k
    if (is_subset)
741
408
      return -1;
742
743
6.35k
    if (!read_byte(info, &ir, address + 1))
744
49
      return -1;
745
6.30k
    return 3;
746
747
1.42k
  case 0xe4: // A,R
748
2.83k
  case 0xe5: // B,R
749
3.91k
  case 0xe6: // D,R
750
5.85k
  case 0xe7: // [D,R]
751
21.0k
  default: // n,-r n,+r n,r- n,r+
752
21.0k
    break;
753
34.1k
  }
754
755
21.0k
  return 1;
756
34.1k
}
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
7.97k
{
761
7.97k
  if (info->cpu->tfr_reg_valid != NULL)
762
2.56k
    return info->cpu->tfr_reg_valid[reg_nibble];
763
764
5.41k
  return true; // e.g. for the M6309 all registers are valid
765
7.97k
}
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
4.24k
{
777
  // HD6809 TFM instruction: Only register X,Y,U,S,D is allowed
778
4.24k
  return reg_nibble <= 4;
779
4.24k
}
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
3.76k
{
786
3.76k
  uint8_t post_byte;
787
3.76k
  uint8_t rr;
788
789
3.76k
  if (!read_byte(info, &post_byte, address))
790
13
    return -1;
791
792
  // According to documentation bit 3 is don't care and not checked here.
793
3.75k
  if ((post_byte >= 0xc0) || ((post_byte & 0x07) == 2) ||
794
2.88k
      ((post_byte & 0x07) == 3))
795
1.47k
    return -1;
796
797
2.27k
  if (!read_byte(info, &rr, address + 1))
798
20
    return -1;
799
800
2.25k
  return 2;
801
2.27k
}
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
1.84k
{
809
1.84k
  uint8_t post_byte;
810
1.84k
  uint8_t rr;
811
812
1.84k
  if (!read_byte(info, &post_byte, address))
813
8
    return -1;
814
815
1.83k
  if ((post_byte & 0xc0) == 0xc0)
816
729
    return -1; // Invalid register specified
817
1.10k
  else {
818
1.10k
    if (!read_byte(info, &rr, address + 1))
819
14
      return -1;
820
1.10k
  }
821
822
1.09k
  return 2;
823
1.83k
}
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
424k
{
997
424k
  const inst_pageX *inst_table = NULL;
998
424k
  const cpu_tables *cpu = info->cpu;
999
424k
  size_t table_size = 0;
1000
424k
  uint16_t base_address = address;
1001
424k
  uint8_t ir; // instruction register
1002
424k
  int i;
1003
424k
  int index;
1004
1005
424k
  if (!read_byte(info, &ir, address++))
1006
0
    return false;
1007
1008
424k
  insn_description->insn = M680X_INS_ILLGL;
1009
424k
  insn_description->opcode = ir;
1010
1011
  // Check if a page prefix byte is present
1012
994k
  for (i = 0; i < ARR_SIZE(cpu->pageX_table_size); ++i) {
1013
975k
    if (cpu->pageX_table_size[i] == 0 ||
1014
605k
        (cpu->inst_pageX_table[i] == NULL))
1015
369k
      break;
1016
1017
605k
    if ((cpu->pageX_prefix[i] == ir)) {
1018
      // Get pageX instruction and handler id.
1019
      // Abort for illegal instr.
1020
36.3k
      inst_table = cpu->inst_pageX_table[i];
1021
36.3k
      table_size = cpu->pageX_table_size[i];
1022
1023
36.3k
      if (!read_byte(info, &ir, address++))
1024
73
        return false;
1025
1026
36.2k
      insn_description->opcode =
1027
36.2k
        (insn_description->opcode << 8) | ir;
1028
1029
36.2k
      if ((index = binary_search(inst_table, table_size,
1030
36.2k
               ir)) < 0)
1031
7.10k
        return false;
1032
1033
29.1k
      insn_description->hid[0] =
1034
29.1k
        inst_table[index].handler_id1;
1035
29.1k
      insn_description->hid[1] =
1036
29.1k
        inst_table[index].handler_id2;
1037
29.1k
      insn_description->insn = inst_table[index].insn;
1038
29.1k
      break;
1039
36.2k
    }
1040
605k
  }
1041
1042
417k
  if (insn_description->insn == M680X_INS_ILLGL) {
1043
    // Get page1 insn description
1044
388k
    insn_description->insn = cpu->inst_page1_table[ir].insn;
1045
388k
    insn_description->hid[0] =
1046
388k
      cpu->inst_page1_table[ir].handler_id1;
1047
388k
    insn_description->hid[1] =
1048
388k
      cpu->inst_page1_table[ir].handler_id2;
1049
388k
  }
1050
1051
417k
  if (insn_description->insn == M680X_INS_ILLGL) {
1052
    // Check if opcode byte is present in an overlay table
1053
56.9k
    for (i = 0; i < ARR_SIZE(cpu->overlay_table_size); ++i) {
1054
55.0k
      if (cpu->overlay_table_size[i] == 0 ||
1055
39.8k
          (cpu->inst_overlay_table[i] == NULL))
1056
15.2k
        break;
1057
1058
39.8k
      inst_table = cpu->inst_overlay_table[i];
1059
39.8k
      table_size = cpu->overlay_table_size[i];
1060
1061
39.8k
      if ((index = binary_search(inst_table, table_size,
1062
39.8k
               ir)) >= 0) {
1063
19.5k
        insn_description->hid[0] =
1064
19.5k
          inst_table[index].handler_id1;
1065
19.5k
        insn_description->hid[1] =
1066
19.5k
          inst_table[index].handler_id2;
1067
19.5k
        insn_description->insn = inst_table[index].insn;
1068
19.5k
        break;
1069
19.5k
      }
1070
39.8k
    }
1071
36.5k
  }
1072
1073
417k
  insn_description->insn_size = address - base_address;
1074
1075
417k
  return (insn_description->insn != M680X_INS_ILLGL) &&
1076
400k
         (insn_description->insn != M680X_INS_INVLD) &&
1077
400k
         is_sufficient_code_size(info, address, insn_description);
1078
424k
}
1079
1080
static void illegal_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1081
41.8k
{
1082
41.8k
  cs_m680x_op *op0 = &info->m680x.operands[info->m680x.op_count++];
1083
41.8k
  uint8_t temp8 = 0;
1084
1085
41.8k
  info->insn = M680X_INS_ILLGL;
1086
41.8k
  read_byte(info, &temp8, (*address)++);
1087
41.8k
  op0->imm = (int32_t)temp8 & 0xff;
1088
41.8k
  op0->type = M680X_OP_IMMEDIATE;
1089
41.8k
  op0->size = 1;
1090
41.8k
}
1091
1092
static void inherent_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1093
470k
{
1094
  // There is nothing to do here :-)
1095
470k
}
1096
1097
static void add_reg_operand(m680x_info *info, m680x_reg reg)
1098
250k
{
1099
250k
  cs_m680x *m680x = &info->m680x;
1100
250k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1101
1102
250k
  op->type = M680X_OP_REGISTER;
1103
250k
  op->reg = reg;
1104
250k
  op->size = info->cpu->reg_byte_size[reg];
1105
250k
}
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
3.03k
{
1202
3.03k
  uint8_t regs = 0;
1203
1204
3.03k
  read_byte(info, &regs, (*address)++);
1205
1206
3.03k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs >> 4]);
1207
3.03k
  add_reg_operand(info, g_tfr_exg_reg_ids[regs & 0x0f]);
1208
1209
3.03k
  if ((regs & 0x0f) == 0x05) {
1210
    // EXG xxx,PC or TFR xxx,PC which is like a JMP
1211
353
    add_insn_group(MI->flat_insn->detail, M680X_GRP_JUMP);
1212
353
  }
1213
3.03k
}
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
31.0k
{
1350
31.0k
  cs_m680x *m680x = &info->m680x;
1351
31.0k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1352
1353
31.0k
  op->type = M680X_OP_RELATIVE;
1354
31.0k
  op->size = 0;
1355
31.0k
  op->rel.offset = offset;
1356
31.0k
  op->rel.address = address;
1357
31.0k
}
1358
1359
static void relative8_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1360
29.0k
{
1361
29.0k
  int16_t offset = 0;
1362
1363
29.0k
  read_byte_sign_extended(info, &offset, (*address)++);
1364
29.0k
  add_rel_operand(info, offset, *address + offset);
1365
29.0k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1366
1367
29.0k
  if ((info->insn != M680X_INS_BRA) && (info->insn != M680X_INS_BSR) &&
1368
25.7k
      (info->insn != M680X_INS_BRN))
1369
23.9k
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1370
29.0k
}
1371
1372
static void relative16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1373
2.03k
{
1374
2.03k
  uint16_t offset = 0;
1375
1376
2.03k
  read_word(info, &offset, *address);
1377
2.03k
  *address += 2;
1378
2.03k
  add_rel_operand(info, (int16_t)offset, *address + offset);
1379
2.03k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1380
1381
2.03k
  if ((info->insn != M680X_INS_LBRA) && (info->insn != M680X_INS_LBSR) &&
1382
939
      (info->insn != M680X_INS_LBRN))
1383
650
    add_reg_to_rw_list(MI, M680X_REG_CC, READ);
1384
2.03k
}
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
25.7k
{
1398
25.7k
  cs_m680x *m680x = &info->m680x;
1399
25.7k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1400
1401
25.7k
  op->type = M680X_OP_INDEXED;
1402
25.7k
  set_operand_size(info, op, 1);
1403
25.7k
  op->idx.base_reg = base_reg;
1404
25.7k
  op->idx.offset_reg = M680X_REG_INVALID;
1405
25.7k
  op->idx.inc_dec = inc_dec;
1406
1407
25.7k
  if (inc_dec && post_inc_dec)
1408
3.88k
    op->idx.flags |= M680X_IDX_POST_INC_DEC;
1409
1410
25.7k
  if (offset_bits != M680X_OFFSET_NONE) {
1411
11.7k
    op->idx.offset = offset;
1412
11.7k
    op->idx.offset_addr = 0;
1413
11.7k
  }
1414
1415
25.7k
  op->idx.offset_bits = offset_bits;
1416
25.7k
  op->idx.flags |= (no_comma ? M680X_IDX_NO_COMMA : 0);
1417
25.7k
}
1418
1419
// M6800/1/2/3 indexed mode handler
1420
static void indexedX_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1421
5.95k
{
1422
5.95k
  uint8_t offset = 0;
1423
1424
5.95k
  read_byte(info, &offset, (*address)++);
1425
1426
5.95k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_8,
1427
5.95k
          (uint16_t)offset, false);
1428
5.95k
}
1429
1430
static void indexedY_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1431
1.37k
{
1432
1.37k
  uint8_t offset = 0;
1433
1434
1.37k
  read_byte(info, &offset, (*address)++);
1435
1436
1.37k
  add_indexed_operand(info, M680X_REG_Y, false, 0, M680X_OFFSET_BITS_8,
1437
1.37k
          (uint16_t)offset, false);
1438
1.37k
}
1439
1440
// M6809/M6309 indexed mode handler
1441
static void indexed09_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1442
39.0k
{
1443
39.0k
  cs_m680x *m680x = &info->m680x;
1444
39.0k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1445
39.0k
  uint8_t post_byte = 0;
1446
39.0k
  uint16_t offset = 0;
1447
39.0k
  int16_t soffset = 0;
1448
1449
39.0k
  read_byte(info, &post_byte, (*address)++);
1450
1451
39.0k
  op->type = M680X_OP_INDEXED;
1452
39.0k
  set_operand_size(info, op, 1);
1453
39.0k
  op->idx.base_reg = g_rr5_to_reg_ids[(post_byte >> 5) & 0x03];
1454
39.0k
  op->idx.offset_reg = M680X_REG_INVALID;
1455
1456
39.0k
  if (!(post_byte & 0x80)) {
1457
    // n5,R
1458
16.6k
    if ((post_byte & 0x10) == 0x10)
1459
7.89k
      op->idx.offset = post_byte | 0xfff0;
1460
8.79k
    else
1461
8.79k
      op->idx.offset = post_byte & 0x0f;
1462
1463
16.6k
    op->idx.offset_addr = op->idx.offset + *address;
1464
16.6k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1465
22.3k
  } else {
1466
22.3k
    if ((post_byte & 0x10) == 0x10)
1467
7.70k
      op->idx.flags |= M680X_IDX_INDIRECT;
1468
1469
    // indexed addressing
1470
22.3k
    switch (post_byte & 0x1f) {
1471
947
    case 0x00: // ,R+
1472
947
      op->idx.inc_dec = 1;
1473
947
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1474
947
      break;
1475
1476
719
    case 0x11: // [,R++]
1477
3.06k
    case 0x01: // ,R++
1478
3.06k
      op->idx.inc_dec = 2;
1479
3.06k
      op->idx.flags |= M680X_IDX_POST_INC_DEC;
1480
3.06k
      break;
1481
1482
665
    case 0x02: // ,-R
1483
665
      op->idx.inc_dec = -1;
1484
665
      break;
1485
1486
787
    case 0x13: // [,--R]
1487
1.70k
    case 0x03: // ,--R
1488
1.70k
      op->idx.inc_dec = -2;
1489
1.70k
      break;
1490
1491
689
    case 0x14: // [,R]
1492
1.97k
    case 0x04: // ,R
1493
1.97k
      break;
1494
1495
468
    case 0x15: // [B,R]
1496
1.84k
    case 0x05: // B,R
1497
1.84k
      op->idx.offset_reg = M680X_REG_B;
1498
1.84k
      break;
1499
1500
860
    case 0x16: // [A,R]
1501
1.71k
    case 0x06: // A,R
1502
1.71k
      op->idx.offset_reg = M680X_REG_A;
1503
1.71k
      break;
1504
1505
575
    case 0x1c: // [n8,PCR]
1506
1.64k
    case 0x0c: // n8,PCR
1507
1.64k
      op->idx.base_reg = M680X_REG_PC;
1508
1.64k
      read_byte_sign_extended(info, &soffset, (*address)++);
1509
1.64k
      op->idx.offset_addr = offset + *address;
1510
1.64k
      op->idx.offset = soffset;
1511
1.64k
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1512
1.64k
      break;
1513
1514
883
    case 0x18: // [n8,R]
1515
2.55k
    case 0x08: // n8,R
1516
2.55k
      read_byte_sign_extended(info, &soffset, (*address)++);
1517
2.55k
      op->idx.offset = soffset;
1518
2.55k
      op->idx.offset_bits = M680X_OFFSET_BITS_8;
1519
2.55k
      break;
1520
1521
600
    case 0x1d: // [n16,PCR]
1522
1.94k
    case 0x0d: // n16,PCR
1523
1.94k
      op->idx.base_reg = M680X_REG_PC;
1524
1.94k
      read_word(info, &offset, *address);
1525
1.94k
      *address += 2;
1526
1.94k
      op->idx.offset_addr = offset + *address;
1527
1.94k
      op->idx.offset = (int16_t)offset;
1528
1.94k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1529
1.94k
      break;
1530
1531
808
    case 0x19: // [n16,R]
1532
1.77k
    case 0x09: // n16,R
1533
1.77k
      read_word(info, &offset, *address);
1534
1.77k
      *address += 2;
1535
1.77k
      op->idx.offset = (int16_t)offset;
1536
1.77k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1537
1.77k
      break;
1538
1539
691
    case 0x1b: // [D,R]
1540
1.88k
    case 0x0b: // D,R
1541
1.88k
      op->idx.offset_reg = M680X_REG_D;
1542
1.88k
      break;
1543
1544
625
    case 0x1f: // [n16]
1545
625
      op->type = M680X_OP_EXTENDED;
1546
625
      op->ext.indirect = true;
1547
625
      read_word(info, &op->ext.address, *address);
1548
625
      *address += 2;
1549
625
      break;
1550
1551
0
    default:
1552
0
      op->idx.base_reg = M680X_REG_INVALID;
1553
0
      break;
1554
22.3k
    }
1555
22.3k
  }
1556
1557
39.0k
  if (((info->insn == M680X_INS_LEAU) || (info->insn == M680X_INS_LEAS) ||
1558
35.9k
       (info->insn == M680X_INS_LEAX) ||
1559
33.9k
       (info->insn == M680X_INS_LEAY)) &&
1560
6.58k
      (m680x->operands[0].reg == M680X_REG_X ||
1561
4.57k
       (m680x->operands[0].reg == M680X_REG_Y)))
1562
    // Only LEAX and LEAY modify CC register
1563
3.53k
    add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1564
39.0k
}
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
47.7k
{
1579
47.7k
  cs_m680x *m680x = &info->m680x;
1580
47.7k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1581
47.7k
  uint8_t post_byte = 0;
1582
47.7k
  uint8_t offset8 = 0;
1583
1584
47.7k
  read_byte(info, &post_byte, (*address)++);
1585
1586
47.7k
  op->type = M680X_OP_INDEXED;
1587
47.7k
  set_operand_size(info, op, 1);
1588
47.7k
  op->idx.offset_reg = M680X_REG_INVALID;
1589
1590
47.7k
  if (!(post_byte & 0x20)) {
1591
    // n5,R      n5 is a 5-bit signed offset
1592
14.7k
    op->idx.base_reg = g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1593
1594
14.7k
    if ((post_byte & 0x10) == 0x10)
1595
6.67k
      op->idx.offset = post_byte | 0xfff0;
1596
8.03k
    else
1597
8.03k
      op->idx.offset = post_byte & 0x0f;
1598
1599
14.7k
    op->idx.offset_addr = op->idx.offset + *address;
1600
14.7k
    op->idx.offset_bits = M680X_OFFSET_BITS_5;
1601
33.0k
  } else {
1602
33.0k
    if ((post_byte & 0xe0) == 0xe0)
1603
17.8k
      op->idx.base_reg =
1604
17.8k
        g_idx12_to_reg_ids[(post_byte >> 3) & 0x03];
1605
1606
33.0k
    switch (post_byte & 0xe7) {
1607
2.55k
    case 0xe0:
1608
5.72k
    case 0xe1: // n9,R
1609
5.72k
      read_byte(info, &offset8, (*address)++);
1610
5.72k
      op->idx.offset = offset8;
1611
1612
5.72k
      if (post_byte & 0x01) // sign extension
1613
3.17k
        op->idx.offset |= 0xff00;
1614
1615
5.72k
      op->idx.offset_bits = M680X_OFFSET_BITS_9;
1616
1617
5.72k
      if (op->idx.base_reg == M680X_REG_PC)
1618
1.57k
        op->idx.offset_addr = op->idx.offset + *address;
1619
1620
5.72k
      break;
1621
1622
3.34k
    case 0xe3: // [n16,R]
1623
3.34k
      op->idx.flags |= M680X_IDX_INDIRECT;
1624
1625
    // intentionally fall through
1626
6.25k
    case 0xe2: // n16,R
1627
6.25k
      read_word(info, (uint16_t *)&op->idx.offset, *address);
1628
6.25k
      (*address) += 2;
1629
6.25k
      op->idx.offset_bits = M680X_OFFSET_BITS_16;
1630
1631
6.25k
      if (op->idx.base_reg == M680X_REG_PC)
1632
985
        op->idx.offset_addr = op->idx.offset + *address;
1633
1634
6.25k
      break;
1635
1636
1.42k
    case 0xe4: // A,R
1637
2.82k
    case 0xe5: // B,R
1638
3.91k
    case 0xe6: // D,R
1639
3.91k
      op->idx.offset_reg =
1640
3.91k
        g_or12_to_reg_ids[post_byte & 0x03];
1641
3.91k
      break;
1642
1643
1.93k
    case 0xe7: // [D,R]
1644
1.93k
      op->idx.offset_reg = M680X_REG_D;
1645
1.93k
      op->idx.flags |= M680X_IDX_INDIRECT;
1646
1.93k
      break;
1647
1648
15.1k
    default: // n,-r n,+r n,r- n,r+
1649
      // PC is not allowed in this mode
1650
15.1k
      op->idx.base_reg =
1651
15.1k
        g_idx12_to_reg_ids[(post_byte >> 6) & 0x03];
1652
15.1k
      op->idx.inc_dec = post_byte & 0x0f;
1653
1654
15.1k
      if (op->idx.inc_dec & 0x08) // evtl. sign extend value
1655
7.13k
        op->idx.inc_dec |= 0xf0;
1656
1657
15.1k
      if (op->idx.inc_dec >= 0)
1658
8.03k
        op->idx.inc_dec++;
1659
1660
15.1k
      if (post_byte & 0x10)
1661
4.54k
        op->idx.flags |= M680X_IDX_POST_INC_DEC;
1662
1663
15.1k
      break;
1664
33.0k
    }
1665
33.0k
  }
1666
47.7k
}
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
58.0k
{
1689
58.0k
  cs_m680x *m680x = &info->m680x;
1690
58.0k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1691
1692
58.0k
  op->type = M680X_OP_DIRECT;
1693
58.0k
  set_operand_size(info, op, 1);
1694
58.0k
  read_byte(info, &op->direct_addr, (*address)++);
1695
58.0k
};
1696
1697
static void extended_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1698
49.9k
{
1699
49.9k
  cs_m680x *m680x = &info->m680x;
1700
49.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1701
1702
49.9k
  op->type = M680X_OP_EXTENDED;
1703
49.9k
  set_operand_size(info, op, 1);
1704
49.9k
  read_word(info, &op->ext.address, *address);
1705
49.9k
  *address += 2;
1706
49.9k
}
1707
1708
static void immediate_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1709
34.9k
{
1710
34.9k
  cs_m680x *m680x = &info->m680x;
1711
34.9k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1712
34.9k
  uint16_t word = 0;
1713
34.9k
  int16_t sword = 0;
1714
1715
34.9k
  op->type = M680X_OP_IMMEDIATE;
1716
34.9k
  set_operand_size(info, op, 1);
1717
1718
34.9k
  switch (op->size) {
1719
29.1k
  case 1:
1720
29.1k
    read_byte_sign_extended(info, &sword, *address);
1721
29.1k
    op->imm = sword;
1722
29.1k
    break;
1723
1724
4.97k
  case 2:
1725
4.97k
    read_word(info, &word, *address);
1726
4.97k
    op->imm = (int16_t)word;
1727
4.97k
    break;
1728
1729
833
  case 4:
1730
833
    read_sdword(info, &op->imm, *address);
1731
833
    break;
1732
1733
0
  default:
1734
0
    op->imm = 0;
1735
0
    CS_ASSERT(0 && "Unexpected immediate byte size");
1736
34.9k
  }
1737
1738
34.9k
  *address += op->size;
1739
34.9k
}
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
1.09k
{
1744
1.09k
  static const m680x_reg m680x_reg[] = {
1745
1.09k
    M680X_REG_CC,
1746
1.09k
    M680X_REG_A,
1747
1.09k
    M680X_REG_B,
1748
1.09k
    M680X_REG_INVALID,
1749
1.09k
  };
1750
1751
1.09k
  uint8_t post_byte = 0;
1752
1.09k
  cs_m680x *m680x = &info->m680x;
1753
1.09k
  cs_m680x_op *op;
1754
1755
1.09k
  read_byte(info, &post_byte, *address);
1756
1.09k
  (*address)++;
1757
1758
  // operand[0] = register
1759
1.09k
  add_reg_operand(info, m680x_reg[post_byte >> 6]);
1760
1761
  // operand[1] = bit index in source operand
1762
1.09k
  op = &m680x->operands[m680x->op_count++];
1763
1.09k
  op->type = M680X_OP_CONSTANT;
1764
1.09k
  op->const_val = (post_byte >> 3) & 0x07;
1765
1766
  // operand[2] = bit index in destination operand
1767
1.09k
  op = &m680x->operands[m680x->op_count++];
1768
1.09k
  op->type = M680X_OP_CONSTANT;
1769
1.09k
  op->const_val = post_byte & 0x07;
1770
1771
1.09k
  direct_hdlr(MI, info, address);
1772
1.09k
}
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
1.76k
{
1777
1.76k
  static const uint8_t inc_dec_r0[] = {
1778
1.76k
    1,
1779
1.76k
    -1,
1780
1.76k
    1,
1781
1.76k
    0,
1782
1.76k
  };
1783
1.76k
  static const uint8_t inc_dec_r1[] = {
1784
1.76k
    1,
1785
1.76k
    -1,
1786
1.76k
    0,
1787
1.76k
    1,
1788
1.76k
  };
1789
1.76k
  uint8_t regs = 0;
1790
1.76k
  uint8_t index = (MI->Opcode & 0xff) - 0x38;
1791
1792
1.76k
  read_byte(info, &regs, *address);
1793
1794
1.76k
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs >> 4], true,
1795
1.76k
          inc_dec_r0[index], M680X_OFFSET_NONE, 0, true);
1796
1.76k
  add_indexed_operand(info, g_tfr_exg_reg_ids[regs & 0x0f], true,
1797
1.76k
          inc_dec_r1[index], M680X_OFFSET_NONE, 0, true);
1798
1799
1.76k
  add_reg_to_rw_list(MI, M680X_REG_W, READ | WRITE);
1800
1.76k
}
1801
1802
static void opidx_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1803
2.08k
{
1804
2.08k
  cs_m680x *m680x = &info->m680x;
1805
2.08k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1806
1807
  // bit index is coded in Opcode
1808
2.08k
  op->type = M680X_OP_CONSTANT;
1809
2.08k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1810
2.08k
}
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
3.50k
{
1817
3.50k
  cs_m680x *m680x = &info->m680x;
1818
3.50k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1819
1820
  // bit index is coded in Opcode
1821
3.50k
  op->type = M680X_OP_CONSTANT;
1822
3.50k
  op->const_val = (MI->Opcode & 0x0e) >> 1;
1823
3.50k
  direct_hdlr(MI, info, address);
1824
3.50k
  relative8_hdlr(MI, info, address);
1825
1826
3.50k
  add_reg_to_rw_list(MI, M680X_REG_CC, MODIFY);
1827
3.50k
}
1828
1829
static void indexedX0_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1830
9.57k
{
1831
9.57k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_NONE, 0,
1832
9.57k
          false);
1833
9.57k
}
1834
1835
static void indexedX16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1836
2.05k
{
1837
2.05k
  uint16_t offset = 0;
1838
1839
2.05k
  read_word(info, &offset, *address);
1840
2.05k
  *address += 2;
1841
2.05k
  add_indexed_operand(info, M680X_REG_X, false, 0, M680X_OFFSET_BITS_16,
1842
2.05k
          offset, false);
1843
2.05k
}
1844
1845
static void imm_rel_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1846
3.08k
{
1847
3.08k
  immediate_hdlr(MI, info, address);
1848
3.08k
  relative8_hdlr(MI, info, address);
1849
3.08k
}
1850
1851
static void indexedS_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1852
738
{
1853
738
  uint8_t offset = 0;
1854
1855
738
  read_byte(info, &offset, (*address)++);
1856
1857
738
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_8,
1858
738
          (uint16_t)offset, false);
1859
738
}
1860
1861
static void indexedS16_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1862
460
{
1863
460
  uint16_t offset = 0;
1864
1865
460
  read_word(info, &offset, *address);
1866
460
  *address += 2;
1867
1868
460
  add_indexed_operand(info, M680X_REG_S, false, 0, M680X_OFFSET_BITS_16,
1869
460
          offset, false);
1870
460
}
1871
1872
static void indexedX0p_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1873
917
{
1874
917
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_NONE, 0,
1875
917
          true);
1876
917
}
1877
1878
static void indexedXp_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1879
1.16k
{
1880
1.16k
  uint8_t offset = 0;
1881
1882
1.16k
  read_byte(info, &offset, (*address)++);
1883
1884
1.16k
  add_indexed_operand(info, M680X_REG_X, true, 1, M680X_OFFSET_BITS_8,
1885
1.16k
          (uint16_t)offset, false);
1886
1.16k
}
1887
1888
static void imm_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1889
7.77k
{
1890
7.77k
  cs_m680x *m680x = &info->m680x;
1891
7.77k
  cs_m680x_op *op = &m680x->operands[m680x->op_count++];
1892
1893
7.77k
  indexed12_hdlr(MI, info, address);
1894
7.77k
  op->type = M680X_OP_IMMEDIATE;
1895
1896
7.77k
  if (info->insn == M680X_INS_MOVW) {
1897
3.21k
    uint16_t imm16 = 0;
1898
1899
3.21k
    read_word(info, &imm16, *address);
1900
3.21k
    op->imm = (int16_t)imm16;
1901
3.21k
    op->size = 2;
1902
4.56k
  } else {
1903
4.56k
    uint8_t imm8 = 0;
1904
1905
4.56k
    read_byte(info, &imm8, *address);
1906
4.56k
    op->imm = (int8_t)imm8;
1907
4.56k
    op->size = 1;
1908
4.56k
  }
1909
1910
7.77k
  set_operand_size(info, op, 1);
1911
7.77k
}
1912
1913
static void ext_idx12_x_hdlr(MCInst *MI, m680x_info *info, uint16_t *address)
1914
2.22k
{
1915
2.22k
  cs_m680x *m680x = &info->m680x;
1916
2.22k
  cs_m680x_op *op0 = &m680x->operands[m680x->op_count++];
1917
2.22k
  uint16_t imm16 = 0;
1918
1919
2.22k
  indexed12_hdlr(MI, info, address);
1920
2.22k
  read_word(info, &imm16, *address);
1921
2.22k
  op0->type = M680X_OP_EXTENDED;
1922
2.22k
  op0->ext.address = (int16_t)imm16;
1923
2.22k
  set_operand_size(info, op0, 1);
1924
2.22k
}
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
2.25k
{
1930
2.25k
  static const m680x_reg index_to_reg_id[] = {
1931
2.25k
    M680X_REG_A, M680X_REG_B, M680X_REG_INVALID, M680X_REG_INVALID,
1932
2.25k
    M680X_REG_D, M680X_REG_X, M680X_REG_Y,       M680X_REG_S,
1933
2.25k
  };
1934
2.25k
  static const m680x_insn index_to_insn_id[] = {
1935
2.25k
    M680X_INS_DBEQ, M680X_INS_DBNE, M680X_INS_TBEQ,  M680X_INS_TBNE,
1936
2.25k
    M680X_INS_IBEQ, M680X_INS_IBNE, M680X_INS_ILLGL, M680X_INS_ILLGL
1937
2.25k
  };
1938
2.25k
  cs_m680x *m680x = &info->m680x;
1939
2.25k
  uint8_t post_byte = 0;
1940
2.25k
  uint8_t rel = 0;
1941
2.25k
  cs_m680x_op *op;
1942
1943
2.25k
  read_byte(info, &post_byte, (*address)++);
1944
1945
2.25k
  info->insn = index_to_insn_id[(post_byte >> 5) & 0x07];
1946
1947
2.25k
  if (info->insn == M680X_INS_ILLGL) {
1948
0
    illegal_hdlr(MI, info, address);
1949
0
  };
1950
1951
2.25k
  read_byte(info, &rel, (*address)++);
1952
1953
2.25k
  add_reg_operand(info, index_to_reg_id[post_byte & 0x07]);
1954
1955
2.25k
  op = &m680x->operands[m680x->op_count++];
1956
1957
2.25k
  op->type = M680X_OP_RELATIVE;
1958
1959
2.25k
  op->rel.offset = (post_byte & 0x10) ? (int16_t)(0xff00 | rel) : rel;
1960
1961
2.25k
  op->rel.address = *address + op->rel.offset;
1962
1963
2.25k
  add_insn_group(MI->flat_insn->detail, M680X_GRP_BRAREL);
1964
2.25k
}
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
424k
{
2341
424k
  if (cpu_type == M680X_CPU_TYPE_INVALID) {
2342
0
    return false;
2343
0
  }
2344
2345
424k
  info->code = code;
2346
424k
  info->size = code_len;
2347
424k
  info->offset = address;
2348
424k
  info->cpu_type = cpu_type;
2349
2350
424k
  info->cpu = &g_cpu_tables[info->cpu_type];
2351
2352
424k
  return true;
2353
424k
}
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