Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/m68hc11-dis.c
Line
Count
Source
1
/* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2
   Copyright (C) 1999-2026 Free Software Foundation, Inc.
3
   Written by Stephane Carrez (stcarrez@nerim.fr)
4
   XGATE and S12X added by James Murray (jsm@jsm-net.demon.co.uk)
5
6
   This file is part of the GNU opcodes library.
7
8
   This library is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3, or (at your option)
11
   any later version.
12
13
   It is distributed in the hope that it will be useful, but WITHOUT
14
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
16
   License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
   MA 02110-1301, USA.  */
22
23
#include "sysdep.h"
24
#include <stdio.h>
25
26
#include "opcode/m68hc11.h"
27
#include "disassemble.h"
28
29
69.6k
#define PC_REGNUM 3
30
31
static const char *const reg_name[] =
32
{
33
  "X", "Y", "SP", "PC"
34
};
35
36
static const char *const reg_src_table[] =
37
{
38
  "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
39
};
40
41
static const char *const reg_dst_table[] =
42
{
43
  "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
44
};
45
46
395k
#define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
47
48
49
static int
50
read_memory (bfd_vma memaddr, bfd_byte* buffer, int size,
51
             struct disassemble_info* info)
52
633k
{
53
633k
  int status;
54
55
  /* Get first byte.  Only one at a time because we don't know the
56
     size of the insn.  */
57
633k
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
58
633k
  if (status != 0)
59
199
    {
60
199
      (*info->memory_error_func) (status, memaddr, info);
61
199
      return -1;
62
199
    }
63
633k
  return 0;
64
633k
}
65
66
67
/* Read the 68HC12 indexed operand byte and print the corresponding mode.
68
   Returns the number of bytes read or -1 if failure.  */
69
static int
70
print_indexed_operand (bfd_vma memaddr, struct disassemble_info* info,
71
           int* indirect, int mov_insn, int pc_offset,
72
           bfd_vma endaddr, int arch)
73
48.0k
{
74
48.0k
  bfd_byte buffer[4];
75
48.0k
  int reg;
76
48.0k
  int status;
77
48.0k
  bfd_vma val;
78
48.0k
  int pos = 1;
79
80
48.0k
  if (indirect)
81
46.5k
    *indirect = 0;
82
83
48.0k
  status = read_memory (memaddr, &buffer[0], 1, info);
84
48.0k
  if (status != 0)
85
30
    {
86
30
      return status;
87
30
    }
88
89
  /* n,r with 5-bits signed constant.  */
90
48.0k
  if ((buffer[0] & 0x20) == 0)
91
22.3k
    {
92
22.3k
      reg = (buffer[0] >> 6) & 3;
93
22.3k
      val = ((buffer[0] & 0x1f) ^ 0x10) - 0x10;
94
      /* 68HC12 requires an adjustment for movb/movw pc relative modes.  */
95
22.3k
      if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
96
0
  val += pc_offset;
97
22.3k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
98
22.3k
           (unsigned) val & 0xffff, reg_name[reg]);
99
100
22.3k
      if (reg == PC_REGNUM)
101
2.34k
  {
102
2.34k
    (* info->fprintf_func) (info->stream, " {");
103
     /* Avoid duplicate 0x from core binutils.  */
104
2.34k
    if (info->symtab_size > 0)
105
0
      (*info->fprintf_func) (info->stream, "0x");
106
2.34k
    (* info->print_address_func) (endaddr + val, info);
107
2.34k
    (* info->fprintf_func) (info->stream, "}");
108
2.34k
  }
109
22.3k
    }
110
111
  /* Auto pre/post increment/decrement.  */
112
25.7k
  else if ((buffer[0] & 0xc0) != 0xc0)
113
17.6k
    {
114
17.6k
      const char *mode;
115
116
17.6k
      reg = (buffer[0] >> 6) & 3;
117
17.6k
      val = buffer[0] & 7;
118
17.6k
      if (buffer[0] & 8)
119
8.42k
  {
120
8.42k
    val = 8 - val;
121
8.42k
    mode = "-";
122
8.42k
  }
123
9.23k
      else
124
9.23k
  {
125
9.23k
    val = val + 1;
126
9.23k
    mode = "+";
127
9.23k
  }
128
17.6k
      (*info->fprintf_func) (info->stream, "%d,%s%s%s",
129
17.6k
           (unsigned) val,
130
17.6k
           buffer[0] & 0x10 ? "" : mode,
131
17.6k
           reg_name[reg], buffer[0] & 0x10 ? mode : "");
132
17.6k
    }
133
134
  /* [n,r] 16-bits offset indexed indirect.  */
135
8.05k
  else if ((buffer[0] & 0x07) == 3)
136
625
    {
137
625
      if ((mov_insn) && (!(arch & cpu9s12x)))
138
31
  {
139
31
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
140
31
         buffer[0] & 0x0ff);
141
31
    return 0;
142
31
  }
143
594
      reg = (buffer[0] >> 3) & 0x03;
144
594
      status = read_memory (memaddr + pos, &buffer[0], 2, info);
145
594
      if (status != 0)
146
1
  return status;
147
148
593
      pos += 2;
149
593
      val = (buffer[0] << 8) | buffer[1];
150
593
      (*info->fprintf_func) (info->stream, "[0x%x,%s]",
151
593
           (unsigned) val & 0xffff, reg_name[reg]);
152
593
      if (indirect)
153
582
  *indirect = 1;
154
593
    }
155
156
  /* n,r with 9 and 16 bit signed constant.  */
157
7.43k
  else if ((buffer[0] & 0x4) == 0)
158
3.41k
    {
159
3.41k
      if ((mov_insn) && (!(arch & cpu9s12x)))
160
828
  {
161
828
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
162
828
         buffer[0] & 0x0ff);
163
828
    return 0;
164
828
  }
165
166
2.58k
      reg = (buffer[0] >> 3) & 0x03;
167
2.58k
      status = read_memory (memaddr + pos,
168
2.58k
          &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
169
2.58k
      if (status != 0)
170
2
  return status;
171
172
2.58k
      if (buffer[0] & 2)
173
1.38k
  {
174
1.38k
    val = (((buffer[1] << 8) | buffer[2]) ^ 0x8000) - 0x8000;
175
1.38k
    pos += 2;
176
1.38k
    endaddr += 2;
177
1.38k
  }
178
1.19k
      else
179
1.19k
  {
180
1.19k
    val = buffer[1] - ((buffer[0] & 1) << 8);
181
1.19k
    pos++;
182
1.19k
    endaddr++;
183
1.19k
  }
184
2.58k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
185
2.58k
           (unsigned) val & 0xffff, reg_name[reg]);
186
2.58k
      if (reg == PC_REGNUM)
187
302
  {
188
302
    (* info->fprintf_func) (info->stream, " {0x");
189
302
    (* info->print_address_func) (endaddr + val, info);
190
302
    (* info->fprintf_func) (info->stream, "}");
191
302
  }
192
2.58k
    }
193
4.02k
  else
194
4.02k
    {
195
4.02k
      reg = (buffer[0] >> 3) & 0x03;
196
4.02k
      switch (buffer[0] & 3)
197
4.02k
  {
198
997
  case 0:
199
997
    (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
200
997
    break;
201
832
  case 1:
202
832
    (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
203
832
    break;
204
675
  case 2:
205
675
    (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
206
675
    break;
207
1.51k
  case 3:
208
1.51k
  default:
209
1.51k
    (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
210
1.51k
    if (indirect)
211
1.44k
      *indirect = 1;
212
1.51k
    break;
213
4.02k
  }
214
4.02k
    }
215
216
47.2k
  return pos;
217
48.0k
}
218
219
/* Disassemble one instruction at address 'memaddr'.  Returns the number
220
   of bytes used by that instruction.  */
221
static int
222
print_insn (bfd_vma memaddr, struct disassemble_info* info, int arch)
223
397k
{
224
397k
  int status;
225
397k
  bfd_byte buffer[4];
226
397k
  unsigned int code;
227
397k
  long format, pos, i;
228
397k
  bfd_vma val;
229
397k
  const struct m68hc11_opcode *opcode;
230
231
397k
  if (arch & cpuxgate)
232
28.4k
    {
233
      /* Get two bytes as all XGATE instructions are 16bit.  */
234
28.4k
      status = read_memory (memaddr, buffer, 2, info);
235
28.4k
      if (status != 0)
236
39
  return status;
237
238
28.3k
      format = 0;
239
28.3k
      code = (buffer[0] << 8) + buffer[1];
240
241
      /* Scan the opcode table until we find the opcode
242
   with the corresponding page.  */
243
28.3k
      opcode = m68hc11_opcodes;
244
34.3M
      for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
245
34.3M
  {
246
34.3M
    if ((opcode->opcode != (code & opcode->xg_mask)) || (opcode->arch != cpuxgate))
247
34.3M
        continue;
248
    /* We have found the opcode.  Extract the operand and print it.  */
249
23.5k
    (*info->fprintf_func) (info->stream, "%s", opcode->name);
250
23.5k
    format = opcode->format;
251
23.5k
    if (format & (M68XG_OP_NONE))
252
2.93k
      {
253
        /* Nothing to print.  */
254
2.93k
      }
255
20.5k
    else if (format & M68XG_OP_IMM3)
256
115
      (*info->fprintf_func) (info->stream, " #0x%x", (code >> 8) & 0x7);
257
20.4k
    else if (format & M68XG_OP_R_R)
258
528
      (*info->fprintf_func) (info->stream, " R%x, R%x",
259
528
           (code >> 8) & 0x7, (code >> 5) & 0x7);
260
19.9k
    else if (format & M68XG_OP_R_R_R)
261
1.73k
      (*info->fprintf_func) (info->stream, " R%x, R%x, R%x",
262
1.73k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
263
18.1k
    else if (format & M68XG_OP_RD_RB_RI)
264
1.06k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x)",
265
1.06k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
266
17.1k
    else if (format & M68XG_OP_RD_RB_RIp)
267
467
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x+)",
268
467
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
269
16.6k
    else if (format & M68XG_OP_RD_RB_mRI)
270
632
      (*info->fprintf_func) (info->stream, " R%x, (R%x, -R%x)",
271
632
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
272
16.0k
    else if (format & M68XG_OP_R_R_OFFS5)
273
1.53k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, #0x%x)",
274
1.53k
           (code >> 8) & 0x7, (code >> 5) & 0x7, code & 0x1f);
275
14.4k
    else if (format & M68XG_OP_R_IMM8)
276
10.4k
      (*info->fprintf_func) (info->stream, " R%x, #0x%02x",
277
10.4k
           (code >> 8) & 0x7, code & 0xff);
278
4.07k
    else if (format & M68XG_OP_R_IMM4)
279
670
      (*info->fprintf_func) (info->stream, " R%x, #0x%x",
280
670
           (code >> 8) & 0x7, (code & 0xf0) >> 4);
281
3.40k
    else if (format & M68XG_OP_REL9)
282
2.56k
      {
283
2.56k
        (*info->fprintf_func) (info->stream, " 0x");
284
2.56k
        val = buffer[1] - ((buffer[0] & 1) << 8);
285
2.56k
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
286
2.56k
      }
287
842
    else if (format & M68XG_OP_REL10)
288
426
      {
289
426
        (*info->fprintf_func) (info->stream, " 0x");
290
426
        val = (buffer[0] << 8) | buffer[1];
291
426
        val = ((val & 0x3ff) ^ 0x200) - 0x200;
292
426
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
293
426
      }
294
416
    else if ((code & 0x00ff) == 0x00f8)
295
101
        (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
296
315
    else if ((code & 0x00ff) == 0x00f9)
297
81
        (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
298
234
    else if ((code & 0x00ff) == 0x0)
299
0
        (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
300
234
    else if (format & M68XG_OP_R)
301
234
        {
302
        /* Special cases for TFR.  */
303
234
        if ((code & 0xf8ff) == 0x00f8)
304
0
    (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
305
234
        else if ((code & 0xf8ff) == 0x00f9)
306
0
    (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
307
234
        else if ((code & 0xf8ff) == 0x00fa)
308
106
    (*info->fprintf_func) (info->stream, " R%x, PC",  (code >> 8) & 0x7);
309
128
        else
310
128
    (*info->fprintf_func) (info->stream, " R%x", (code >> 8) & 0x7);
311
234
      }
312
0
    else
313
      /* Opcode not recognized.  */
314
0
      (*info->fprintf_func) (info->stream, "Not yet handled TEST .byte\t0x%04x", code);
315
23.5k
    return 2;
316
34.3M
  }
317
318
      /* Opcode not recognized.  */
319
4.85k
      (*info->fprintf_func) (info->stream, ".byte\t0x%04x", code);
320
4.85k
      return 2; /* Everything is two bytes.  */
321
28.3k
    }
322
323
  /* HC11 and HC12.  */
324
325
  /* Get first byte.  Only one at a time because we don't know the
326
     size of the insn.  */
327
369k
  status = read_memory (memaddr, buffer, 1, info);
328
369k
  if (status != 0)
329
1
    return status;
330
331
369k
  format = 0;
332
369k
  code = buffer[0];
333
369k
  pos = 0;
334
335
  /* Look for page2,3,4 opcodes.  */
336
369k
  if (code == M6811_OPCODE_PAGE2)
337
3.05k
    {
338
3.05k
      pos++;
339
3.05k
      format = M6811_OP_PAGE2;
340
3.05k
    }
341
366k
  else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
342
268
    {
343
268
      pos++;
344
268
      format = M6811_OP_PAGE3;
345
268
    }
346
365k
  else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
347
330
    {
348
330
      pos++;
349
330
      format = M6811_OP_PAGE4;
350
330
    }
351
352
  /* We are in page2,3,4; get the real opcode.  */
353
369k
  if (pos == 1)
354
3.64k
    {
355
3.64k
      status = read_memory (memaddr + pos, &buffer[1], 1, info);
356
3.64k
      if (status != 0)
357
0
  return status;
358
359
3.64k
      code = buffer[1];
360
3.64k
    }
361
362
  /* Look first for a 68HC12 alias.  All of them are 2-bytes long and
363
     in page 1.  There is no operand to print.  We read the second byte
364
     only when we have a possible match.  */
365
369k
  if ((arch & cpu6812) && format == 0)
366
346k
    {
367
346k
      int must_read = 1;
368
369
      /* Walk the alias table to find a code1+code2 match.  */
370
6.58M
      for (i = 0; i < m68hc12_num_alias; i++)
371
6.23M
  {
372
6.23M
    if (m68hc12_alias[i].code1 == code)
373
30.8k
      {
374
30.8k
        if (must_read)
375
30.8k
    {
376
30.8k
      status = read_memory (memaddr + pos + 1,
377
30.8k
          &buffer[1], 1, info);
378
30.8k
      if (status != 0)
379
3
        break;
380
381
30.8k
      must_read = 1;
382
30.8k
    }
383
30.8k
        if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
384
151
    {
385
151
      (*info->fprintf_func) (info->stream, "%s",
386
151
           m68hc12_alias[i].name);
387
151
      return 2;
388
151
    }
389
30.8k
      }
390
6.23M
  }
391
346k
    }
392
393
368k
  pos++;
394
395
  /* Scan the opcode table until we find the opcode
396
     with the corresponding page.  */
397
368k
  opcode = m68hc11_opcodes;
398
173M
  for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
399
173M
    {
400
173M
      int offset;
401
173M
      int pc_src_offset;
402
173M
      int pc_dst_offset = 0;
403
404
173M
      if ((opcode->arch & arch) == 0)
405
83.2M
  continue;
406
89.8M
      if (opcode->opcode != code)
407
89.4M
  continue;
408
395k
      if ((opcode->format & OP_PAGE_MASK) != format)
409
29.9k
  continue;
410
411
365k
      if (opcode->format & M6812_OP_REG)
412
7.88k
  {
413
7.88k
    int j;
414
7.88k
    int is_jump;
415
416
7.88k
    if (opcode->format & M6811_OP_JUMP_REL)
417
6.08k
      is_jump = 1;
418
1.80k
    else
419
1.80k
      is_jump = 0;
420
421
7.88k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
422
7.88k
    if (status != 0)
423
8
      {
424
8
        return status;
425
8
      }
426
1.57M
    for (j = 0; i + j < m68hc11_num_opcodes; j++)
427
1.57M
      {
428
1.57M
        if ((opcode[j].arch & arch) == 0)
429
580k
    continue;
430
993k
        if (opcode[j].opcode != code)
431
975k
    continue;
432
17.6k
        if (is_jump)
433
14.1k
    {
434
14.1k
      if (!(opcode[j].format & M6811_OP_JUMP_REL))
435
1.19k
        continue;
436
437
12.9k
      if ((opcode[j].format & M6812_OP_IBCC_MARKER)
438
2.90k
          && (buffer[0] & 0xc0) != 0x80)
439
2.39k
        continue;
440
10.5k
      if ((opcode[j].format & M6812_OP_TBCC_MARKER)
441
1.84k
          && (buffer[0] & 0xc0) != 0x40)
442
876
        continue;
443
9.66k
      if ((opcode[j].format & M6812_OP_DBCC_MARKER)
444
8.18k
          && (buffer[0] & 0xc0) != 0)
445
3.32k
        continue;
446
6.33k
      if ((opcode[j].format & M6812_OP_EQ_MARKER)
447
5.63k
          && (buffer[0] & 0x20) == 0)
448
4.94k
        break;
449
1.38k
      if (!(opcode[j].format & M6812_OP_EQ_MARKER)
450
694
          && (buffer[0] & 0x20) != 0)
451
694
        break;
452
694
      continue;
453
1.38k
    }
454
3.52k
        if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
455
712
    break;
456
2.81k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
457
1.09k
      && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
458
599
      && ((buffer[0] & 0x0f0) <= 0x20))
459
132
    break;
460
2.67k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
461
959
      && (arch & cpu9s12x)
462
795
      && ((buffer[0] == 0x4d) || (buffer[0] == 0x4e)))
463
331
    break;
464
2.34k
        if (opcode[j].format & M6812_OP_TFR_MARKER
465
628
      && !(buffer[0] & 0x80))
466
628
    break;
467
2.34k
      }
468
7.87k
    if (i + j < m68hc11_num_opcodes)
469
7.44k
      opcode = &opcode[j];
470
7.87k
  }
471
472
      /* We have found the opcode.  Extract the operand and print it.  */
473
365k
      (*info->fprintf_func) (info->stream, "%s", opcode->name);
474
475
365k
      format = opcode->format;
476
365k
      if (format & (M6811_OP_MASK | M6811_OP_BITMASK
477
365k
        | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
478
170k
  {
479
170k
    (*info->fprintf_func) (info->stream, "\t");
480
170k
  }
481
482
      /* The movb and movw must be handled in a special way...
483
   The source constant 'ii' is not always at the same place.
484
   This is the same for the destination for the post-indexed byte.
485
   The 'offset' is used to do the appropriate correction.
486
487
   offset          offset
488
   for constant     for destination
489
   movb   18 OB ii hh ll       0          0
490
   18 08 xb ii          1          -1
491
   18 08 xb ff ii       2          1  9 bit
492
   18 08 xb ee ff ii    3          1  16 bit
493
   18 0C hh ll hh ll    0          0
494
   18 09 xb hh ll       1          -1
495
   18 0D xb hh ll       0          0
496
   18 0A xb xb          0          0
497
498
   movw   18 03 jj kk hh ll    0          0
499
   18 00 xb jj kk       1          -1
500
   18 04 hh ll hh ll    0          0
501
   18 01 xb hh ll       1          -1
502
   18 05 xb hh ll       0          0
503
   18 02 xb xb          0          0
504
505
   After the source operand is read, the position 'pos' is incremented
506
   this explains the negative offset for destination.
507
508
   movb/movw above are the only instructions with this matching
509
   format.  */
510
365k
      offset = ((format & M6812_OP_IDX_P2)
511
1.59k
    && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
512
1.59k
            M6811_OP_IND16)));
513
514
365k
      if (offset)
515
1.58k
  {
516
    /* Check xb to see position of data.  */
517
1.58k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
518
1.58k
    if (status != 0)
519
2
      {
520
2
        return status;
521
2
      }
522
523
1.58k
    if (((buffer[0] & 0xe0) == 0xe0) && ((buffer[0] & 0x04) == 0))
524
900
      {
525
        /* 9 or 16 bit.  */
526
900
        if ((buffer[0] & 0x02) == 0)
527
101
    {
528
      /* 9 bit.  */
529
101
      offset = 2;
530
101
    }
531
799
        else
532
799
    {
533
      /* 16 bit.  */
534
799
      offset = 3;
535
799
    }
536
900
      }
537
1.58k
  }
538
539
      /* Operand with one more byte: - immediate, offset,
540
   direct-low address.  */
541
365k
      if (format &
542
365k
    (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
543
44.1k
  {
544
44.1k
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
545
44.1k
    if (status != 0)
546
15
      return status;
547
548
    /* This movb/movw is special (see above).  */
549
44.1k
    if (offset < 2)
550
43.6k
      {
551
43.6k
        offset = -offset;
552
43.6k
        pc_dst_offset = 2;
553
43.6k
      }
554
519
    else
555
519
      {
556
519
        offset = -1;
557
519
        pc_dst_offset = 5;
558
519
      }
559
44.1k
    pos++;
560
561
44.1k
    if (format & M6811_OP_IMM8)
562
19.2k
      {
563
19.2k
        (*info->fprintf_func) (info->stream, "#0x%x", (int) buffer[0]);
564
19.2k
        format &= ~M6811_OP_IMM8;
565
        /* Set PC destination offset.  */
566
19.2k
        pc_dst_offset = 1;
567
19.2k
      }
568
24.9k
    else if (format & M6811_OP_IX)
569
1.54k
      {
570
        /* Offsets are in range 0..255, print them unsigned.  */
571
1.54k
        (*info->fprintf_func) (info->stream, "0x%x,x", buffer[0] & 0x0FF);
572
1.54k
        format &= ~M6811_OP_IX;
573
1.54k
      }
574
23.3k
    else if (format & M6811_OP_IY)
575
30
      {
576
30
        (*info->fprintf_func) (info->stream, "0x%x,y", buffer[0] & 0x0FF);
577
30
        format &= ~M6811_OP_IY;
578
30
      }
579
23.3k
    else if (format & M6811_OP_DIRECT)
580
23.3k
      {
581
23.3k
        (*info->fprintf_func) (info->stream, "*");
582
23.3k
        if (info->symtab_size > 0) /* Avoid duplicate 0x. */
583
0
    (*info->fprintf_func) (info->stream, "0x");
584
23.3k
        (*info->print_address_func) (buffer[0] & 0x0FF, info);
585
23.3k
        format &= ~M6811_OP_DIRECT;
586
23.3k
      }
587
44.1k
  }
588
589
365k
#define M6812_DST_MOVE  (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
590
365k
#define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
591
      /* Analyze the 68HC12 indexed byte.  */
592
365k
      if (format & M6812_INDEXED_FLAGS)
593
46.5k
  {
594
46.5k
    int indirect;
595
46.5k
    bfd_vma endaddr;
596
597
46.5k
    endaddr = memaddr + pos + 1;
598
46.5k
    if (format & M6811_OP_IND16)
599
0
      endaddr += 2;
600
46.5k
    pc_src_offset = -1;
601
46.5k
    pc_dst_offset = 1;
602
46.5k
    status = print_indexed_operand (memaddr + pos, info, &indirect,
603
46.5k
            (format & M6812_DST_MOVE),
604
46.5k
            pc_src_offset, endaddr, arch);
605
46.5k
    if (status < 0)
606
33
      return status;
607
608
46.4k
    pos += status;
609
610
    /* The indirect addressing mode of the call instruction does
611
       not need the page code.  */
612
46.4k
    if ((format & M6812_OP_PAGE) && indirect)
613
56
      format &= ~M6812_OP_PAGE;
614
46.4k
  }
615
616
      /* 68HC12 dbcc/ibcc/tbcc operands.  */
617
365k
      if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
618
6.07k
  {
619
6.07k
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
620
6.07k
    if (status != 0)
621
5
      return status;
622
623
6.07k
    (*info->fprintf_func) (info->stream, "%s,",
624
6.07k
         reg_src_table[buffer[0] & 0x07]);
625
6.07k
    val = buffer[1] - ((buffer[0] & 0x10) << 4);
626
627
6.07k
    pos += 2;
628
6.07k
    (*info->fprintf_func) (info->stream, "0x");
629
6.07k
    (*info->print_address_func) (memaddr + pos + val, info);
630
6.07k
    format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
631
6.07k
  }
632
359k
      else if (format & (M6812_OP_REG | M6812_OP_REG_2))
633
1.80k
  {
634
1.80k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
635
1.80k
    if (status != 0)
636
0
      return status;
637
638
1.80k
    pos++;
639
1.80k
    (*info->fprintf_func) (info->stream, "%s,%s",
640
1.80k
         reg_src_table[(buffer[0] >> 4) & 7],
641
1.80k
         reg_dst_table[(buffer[0] & 7)]);
642
1.80k
  }
643
644
365k
      if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
645
55.8k
  {
646
55.8k
    bfd_vma addr;
647
55.8k
    unsigned page = 0;
648
649
55.8k
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
650
55.8k
    if (status != 0)
651
74
      return status;
652
653
55.8k
    if (format & M6812_OP_IDX_P2)
654
1.04k
      offset = -2;
655
54.7k
    else
656
54.7k
      offset = 0;
657
55.8k
    pos += 2;
658
659
55.8k
    addr = val = (buffer[0] << 8) | buffer[1];
660
55.8k
    pc_dst_offset = 2;
661
55.8k
    if (format & M6812_OP_PAGE)
662
674
      {
663
674
        status = read_memory (memaddr + pos + offset, buffer, 1, info);
664
674
        if (status != 0)
665
0
    return status;
666
667
674
        page = buffer[0];
668
674
        if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
669
75
    addr = (val - M68HC12_BANK_BASE + (page << M68HC12_BANK_SHIFT)
670
75
      + M68HC12_BANK_VIRT);
671
674
      }
672
55.1k
    else if ((arch & cpu6812)
673
52.6k
       && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
674
7.01k
      {
675
7.01k
        unsigned cur_page;
676
7.01k
        bfd_vma vaddr;
677
678
7.01k
        if (memaddr >= M68HC12_BANK_VIRT)
679
1.28k
    cur_page = ((memaddr - M68HC12_BANK_VIRT)
680
1.28k
          >> M68HC12_BANK_SHIFT);
681
5.72k
        else
682
5.72k
    cur_page = 0;
683
684
7.01k
        vaddr = (addr - M68HC12_BANK_BASE
685
7.01k
           + (cur_page << M68HC12_BANK_SHIFT)) + M68HC12_BANK_VIRT;
686
7.01k
        if (!info->symbol_at_address_func (addr, info)
687
7.01k
      && info->symbol_at_address_func (vaddr, info))
688
0
    addr = vaddr;
689
7.01k
      }
690
55.8k
    if (format & M6811_OP_IMM16)
691
5.73k
      {
692
5.73k
        format &= ~M6811_OP_IMM16;
693
5.73k
        (*info->fprintf_func) (info->stream, "#");
694
5.73k
      }
695
50.0k
    else
696
50.0k
      {
697
50.0k
        format &= ~M6811_OP_IND16;
698
50.0k
      }
699
700
    /* Avoid duplicate 0x from core binutils.  */
701
55.8k
    if (info->symtab_size > 0)
702
0
      (*info->fprintf_func) (info->stream, "0x");
703
704
55.8k
    (*info->print_address_func) (addr, info);
705
55.8k
    if (format & M6812_OP_PAGE)
706
674
      {
707
674
        (* info->fprintf_func) (info->stream, " {");
708
        /* Avoid duplicate 0x from core binutils.  */
709
674
        if (info->symtab_size > 0)
710
0
    (*info->fprintf_func) (info->stream, "0x");
711
674
        (* info->print_address_func) (val, info);
712
674
        (* info->fprintf_func) (info->stream, ", 0x%x}", page);
713
674
        format &= ~M6812_OP_PAGE;
714
674
        pos += 1;
715
674
      }
716
55.8k
  }
717
718
365k
      if (format & M6812_OP_IDX_P2)
719
1.59k
  {
720
1.59k
    (*info->fprintf_func) (info->stream, ", ");
721
1.59k
    status = print_indexed_operand (memaddr + pos + offset, info,
722
1.59k
            0, 1, pc_dst_offset,
723
1.59k
            memaddr + pos + offset + 1, arch);
724
1.59k
    if (status < 0)
725
0
      return status;
726
1.59k
    pos += status;
727
1.59k
  }
728
729
365k
      if (format & M6812_OP_IND16_P2)
730
143
  {
731
143
    (*info->fprintf_func) (info->stream, ", ");
732
733
143
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
734
143
    if (status != 0)
735
0
      return status;
736
737
143
    pos += 2;
738
739
143
    val = (buffer[0] << 8) | buffer[1];
740
    /* Avoid duplicate 0x from core binutils.  */
741
143
    if (info->symtab_size > 0)
742
0
      (*info->fprintf_func) (info->stream, "0x");
743
143
    (*info->print_address_func) (val, info);
744
143
  }
745
746
      /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
747
   and in that order.  The brset/brclr insn have a bitmask and then
748
   a relative branch offset.  */
749
365k
      if (format & M6811_OP_BITMASK)
750
10.4k
  {
751
10.4k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
752
10.4k
    if (status != 0)
753
2
      return status;
754
755
10.4k
    pos++;
756
10.4k
    (*info->fprintf_func) (info->stream, ", #0x%02x%s",
757
10.4k
         buffer[0] & 0x0FF,
758
10.4k
         (format & M6811_OP_JUMP_REL ? ", " : ""));
759
10.4k
    format &= ~M6811_OP_BITMASK;
760
10.4k
  }
761
365k
      if (format & M6811_OP_JUMP_REL)
762
20.7k
  {
763
20.7k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
764
20.7k
    if (status != 0)
765
15
      return status;
766
767
20.7k
    (*info->fprintf_func) (info->stream, "0x");
768
20.7k
    pos++;
769
20.7k
    val = (buffer[0] ^ 0x80) - 0x80;
770
20.7k
    (*info->print_address_func) (memaddr + pos + val, info);
771
20.7k
    format &= ~M6811_OP_JUMP_REL;
772
20.7k
  }
773
344k
      else if (format & M6812_OP_JUMP_REL16)
774
40
  {
775
40
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
776
40
    if (status != 0)
777
1
      return status;
778
779
39
    pos += 2;
780
39
    val = (((buffer[0] << 8) | buffer[1]) ^ 0x8000) - 0x8000;
781
782
39
    (*info->fprintf_func) (info->stream, "0x");
783
39
    (*info->print_address_func) (memaddr + pos + val, info);
784
39
    format &= ~M6812_OP_JUMP_REL16;
785
39
  }
786
787
365k
      if (format & M6812_OP_PAGE)
788
595
  {
789
595
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
790
595
    if (status != 0)
791
1
      return status;
792
793
594
    pos += 1;
794
795
594
    val = buffer[0];
796
594
    (*info->fprintf_func) (info->stream, ", 0x%x", (unsigned) val);
797
594
  }
798
799
#ifdef DEBUG
800
      /* Consistency check.  'format' must be 0, so that we have handled
801
   all formats; and the computed size of the insn must match the
802
   opcode table content.  */
803
      if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
804
  (*info->fprintf_func) (info->stream, "; Error, format: %lx", format);
805
806
      if (pos != opcode->size)
807
  (*info->fprintf_func) (info->stream, "; Error, size: %ld expect %d",
808
             pos, opcode->size);
809
#endif
810
365k
      return pos;
811
365k
    }
812
813
  /* Opcode not recognized.  */
814
3.56k
  if (format == M6811_OP_PAGE2 && arch & cpu6812
815
581
      && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
816
581
    (*info->fprintf_func) (info->stream, "trap\t#0x%02x", code & 0x0ff);
817
818
2.98k
  else if (format == M6811_OP_PAGE2)
819
125
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
820
125
         M6811_OPCODE_PAGE2, code);
821
2.85k
  else if (format == M6811_OP_PAGE3)
822
259
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
823
259
         M6811_OPCODE_PAGE3, code);
824
2.60k
  else if (format == M6811_OP_PAGE4)
825
330
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
826
330
         M6811_OPCODE_PAGE4, code);
827
2.27k
  else
828
2.27k
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
829
830
3.56k
  return pos;
831
368k
}
832
833
/* Disassemble one instruction at address 'memaddr'.  Returns the number
834
   of bytes used by that instruction.  */
835
int
836
print_insn_m68hc11 (bfd_vma memaddr, struct disassemble_info* info)
837
19.7k
{
838
19.7k
  return print_insn (memaddr, info, cpu6811);
839
19.7k
}
840
841
int
842
print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
843
305k
{
844
305k
  return print_insn (memaddr, info, cpu6812);
845
305k
}
846
847
int
848
print_insn_m9s12x (bfd_vma memaddr, struct disassemble_info* info)
849
44.1k
{
850
44.1k
  return print_insn (memaddr, info, cpu6812|cpu9s12x);
851
44.1k
}
852
853
int
854
print_insn_m9s12xg (bfd_vma memaddr, struct disassemble_info* info)
855
28.4k
{
856
28.4k
  return print_insn (memaddr, info, cpuxgate);
857
28.4k
}