Coverage Report

Created: 2026-09-14 08:07

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
127k
#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
753k
#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
1.20M
{
53
1.20M
  int status;
54
55
  /* Get first byte.  Only one at a time because we don't know the
56
     size of the insn.  */
57
1.20M
  status = (*info->read_memory_func) (memaddr, buffer, size, info);
58
1.20M
  if (status != 0)
59
407
    {
60
407
      (*info->memory_error_func) (status, memaddr, info);
61
407
      return -1;
62
407
    }
63
1.20M
  return 0;
64
1.20M
}
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
86.9k
{
74
86.9k
  bfd_byte buffer[4];
75
86.9k
  int reg;
76
86.9k
  int status;
77
86.9k
  bfd_vma val;
78
86.9k
  int pos = 1;
79
80
86.9k
  if (indirect)
81
84.1k
    *indirect = 0;
82
83
86.9k
  status = read_memory (memaddr, &buffer[0], 1, info);
84
86.9k
  if (status != 0)
85
60
    {
86
60
      return status;
87
60
    }
88
89
  /* n,r with 5-bits signed constant.  */
90
86.8k
  if ((buffer[0] & 0x20) == 0)
91
41.1k
    {
92
41.1k
      reg = (buffer[0] >> 6) & 3;
93
41.1k
      val = ((buffer[0] & 0x1f) ^ 0x10) - 0x10;
94
      /* 68HC12 requires an adjustment for movb/movw pc relative modes.  */
95
41.1k
      if (reg == PC_REGNUM && info->mach == bfd_mach_m6812 && mov_insn)
96
0
  val += pc_offset;
97
41.1k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
98
41.1k
           (unsigned) val & 0xffff, reg_name[reg]);
99
100
41.1k
      if (reg == PC_REGNUM)
101
4.47k
  {
102
4.47k
    (* info->fprintf_func) (info->stream, " {");
103
     /* Avoid duplicate 0x from core binutils.  */
104
4.47k
    if (info->symtab_size > 0)
105
0
      (*info->fprintf_func) (info->stream, "0x");
106
4.47k
    (* info->print_address_func) (endaddr + val, info);
107
4.47k
    (* info->fprintf_func) (info->stream, "}");
108
4.47k
  }
109
41.1k
    }
110
111
  /* Auto pre/post increment/decrement.  */
112
45.7k
  else if ((buffer[0] & 0xc0) != 0xc0)
113
31.4k
    {
114
31.4k
      const char *mode;
115
116
31.4k
      reg = (buffer[0] >> 6) & 3;
117
31.4k
      val = buffer[0] & 7;
118
31.4k
      if (buffer[0] & 8)
119
16.0k
  {
120
16.0k
    val = 8 - val;
121
16.0k
    mode = "-";
122
16.0k
  }
123
15.4k
      else
124
15.4k
  {
125
15.4k
    val = val + 1;
126
15.4k
    mode = "+";
127
15.4k
  }
128
31.4k
      (*info->fprintf_func) (info->stream, "%d,%s%s%s",
129
31.4k
           (unsigned) val,
130
31.4k
           buffer[0] & 0x10 ? "" : mode,
131
31.4k
           reg_name[reg], buffer[0] & 0x10 ? mode : "");
132
31.4k
    }
133
134
  /* [n,r] 16-bits offset indexed indirect.  */
135
14.2k
  else if ((buffer[0] & 0x07) == 3)
136
1.52k
    {
137
1.52k
      if ((mov_insn) && (!(arch & cpu9s12x)))
138
120
  {
139
120
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
140
120
         buffer[0] & 0x0ff);
141
120
    return 0;
142
120
  }
143
1.40k
      reg = (buffer[0] >> 3) & 0x03;
144
1.40k
      status = read_memory (memaddr + pos, &buffer[0], 2, info);
145
1.40k
      if (status != 0)
146
3
  return status;
147
148
1.39k
      pos += 2;
149
1.39k
      val = (buffer[0] << 8) | buffer[1];
150
1.39k
      (*info->fprintf_func) (info->stream, "[0x%x,%s]",
151
1.39k
           (unsigned) val & 0xffff, reg_name[reg]);
152
1.39k
      if (indirect)
153
1.30k
  *indirect = 1;
154
1.39k
    }
155
156
  /* n,r with 9 and 16 bit signed constant.  */
157
12.7k
  else if ((buffer[0] & 0x4) == 0)
158
5.67k
    {
159
5.67k
      if ((mov_insn) && (!(arch & cpu9s12x)))
160
1.13k
  {
161
1.13k
    (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
162
1.13k
         buffer[0] & 0x0ff);
163
1.13k
    return 0;
164
1.13k
  }
165
166
4.53k
      reg = (buffer[0] >> 3) & 0x03;
167
4.53k
      status = read_memory (memaddr + pos,
168
4.53k
          &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
169
4.53k
      if (status != 0)
170
5
  return status;
171
172
4.53k
      if (buffer[0] & 2)
173
2.14k
  {
174
2.14k
    val = (((buffer[1] << 8) | buffer[2]) ^ 0x8000) - 0x8000;
175
2.14k
    pos += 2;
176
2.14k
    endaddr += 2;
177
2.14k
  }
178
2.39k
      else
179
2.39k
  {
180
2.39k
    val = buffer[1] - ((buffer[0] & 1) << 8);
181
2.39k
    pos++;
182
2.39k
    endaddr++;
183
2.39k
  }
184
4.53k
      (*info->fprintf_func) (info->stream, "0x%x,%s",
185
4.53k
           (unsigned) val & 0xffff, reg_name[reg]);
186
4.53k
      if (reg == PC_REGNUM)
187
824
  {
188
824
    (* info->fprintf_func) (info->stream, " {0x");
189
824
    (* info->print_address_func) (endaddr + val, info);
190
824
    (* info->fprintf_func) (info->stream, "}");
191
824
  }
192
4.53k
    }
193
7.08k
  else
194
7.08k
    {
195
7.08k
      reg = (buffer[0] >> 3) & 0x03;
196
7.08k
      switch (buffer[0] & 3)
197
7.08k
  {
198
1.52k
  case 0:
199
1.52k
    (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
200
1.52k
    break;
201
1.27k
  case 1:
202
1.27k
    (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
203
1.27k
    break;
204
1.11k
  case 2:
205
1.11k
    (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
206
1.11k
    break;
207
3.16k
  case 3:
208
3.16k
  default:
209
3.16k
    (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
210
3.16k
    if (indirect)
211
2.91k
      *indirect = 1;
212
3.16k
    break;
213
7.08k
  }
214
7.08k
    }
215
216
85.6k
  return pos;
217
86.8k
}
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
739k
{
224
739k
  int status;
225
739k
  bfd_byte buffer[4];
226
739k
  unsigned int code;
227
739k
  long format, pos, i;
228
739k
  bfd_vma val;
229
739k
  const struct m68hc11_opcode *opcode;
230
231
739k
  if (arch & cpuxgate)
232
34.0k
    {
233
      /* Get two bytes as all XGATE instructions are 16bit.  */
234
34.0k
      status = read_memory (memaddr, buffer, 2, info);
235
34.0k
      if (status != 0)
236
51
  return status;
237
238
33.9k
      format = 0;
239
33.9k
      code = (buffer[0] << 8) + buffer[1];
240
241
      /* Scan the opcode table until we find the opcode
242
   with the corresponding page.  */
243
33.9k
      opcode = m68hc11_opcodes;
244
41.0M
      for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
245
41.0M
  {
246
41.0M
    if ((opcode->opcode != (code & opcode->xg_mask)) || (opcode->arch != cpuxgate))
247
41.0M
        continue;
248
    /* We have found the opcode.  Extract the operand and print it.  */
249
30.4k
    (*info->fprintf_func) (info->stream, "%s", opcode->name);
250
30.4k
    format = opcode->format;
251
30.4k
    if (format & (M68XG_OP_NONE))
252
4.02k
      {
253
        /* Nothing to print.  */
254
4.02k
      }
255
26.4k
    else if (format & M68XG_OP_IMM3)
256
123
      (*info->fprintf_func) (info->stream, " #0x%x", (code >> 8) & 0x7);
257
26.2k
    else if (format & M68XG_OP_R_R)
258
361
      (*info->fprintf_func) (info->stream, " R%x, R%x",
259
361
           (code >> 8) & 0x7, (code >> 5) & 0x7);
260
25.9k
    else if (format & M68XG_OP_R_R_R)
261
2.02k
      (*info->fprintf_func) (info->stream, " R%x, R%x, R%x",
262
2.02k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
263
23.9k
    else if (format & M68XG_OP_RD_RB_RI)
264
1.88k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x)",
265
1.88k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
266
22.0k
    else if (format & M68XG_OP_RD_RB_RIp)
267
1.04k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, R%x+)",
268
1.04k
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
269
20.9k
    else if (format & M68XG_OP_RD_RB_mRI)
270
960
      (*info->fprintf_func) (info->stream, " R%x, (R%x, -R%x)",
271
960
           (code >> 8) & 0x7, (code >> 5) & 0x7, (code >> 2) & 0x7);
272
20.0k
    else if (format & M68XG_OP_R_R_OFFS5)
273
1.67k
      (*info->fprintf_func) (info->stream, " R%x, (R%x, #0x%x)",
274
1.67k
           (code >> 8) & 0x7, (code >> 5) & 0x7, code & 0x1f);
275
18.3k
    else if (format & M68XG_OP_R_IMM8)
276
12.4k
      (*info->fprintf_func) (info->stream, " R%x, #0x%02x",
277
12.4k
           (code >> 8) & 0x7, code & 0xff);
278
5.90k
    else if (format & M68XG_OP_R_IMM4)
279
914
      (*info->fprintf_func) (info->stream, " R%x, #0x%x",
280
914
           (code >> 8) & 0x7, (code & 0xf0) >> 4);
281
4.98k
    else if (format & M68XG_OP_REL9)
282
3.09k
      {
283
3.09k
        (*info->fprintf_func) (info->stream, " 0x");
284
3.09k
        val = buffer[1] - ((buffer[0] & 1) << 8);
285
3.09k
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
286
3.09k
      }
287
1.89k
    else if (format & M68XG_OP_REL10)
288
1.02k
      {
289
1.02k
        (*info->fprintf_func) (info->stream, " 0x");
290
1.02k
        val = (buffer[0] << 8) | buffer[1];
291
1.02k
        val = ((val & 0x3ff) ^ 0x200) - 0x200;
292
1.02k
        (*info->print_address_func) (memaddr + (val << 1) + 2, info);
293
1.02k
      }
294
868
    else if ((code & 0x00ff) == 0x00f8)
295
82
        (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
296
786
    else if ((code & 0x00ff) == 0x00f9)
297
251
        (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
298
535
    else if ((code & 0x00ff) == 0x0)
299
0
        (*info->fprintf_func) (info->stream, " R%x, PC", (code >> 8) & 0x7);
300
535
    else if (format & M68XG_OP_R)
301
535
        {
302
        /* Special cases for TFR.  */
303
535
        if ((code & 0xf8ff) == 0x00f8)
304
0
    (*info->fprintf_func) (info->stream, " R%x, CCR", (code >> 8) & 0x7);
305
535
        else if ((code & 0xf8ff) == 0x00f9)
306
0
    (*info->fprintf_func) (info->stream, " CCR, R%x", (code >> 8) & 0x7);
307
535
        else if ((code & 0xf8ff) == 0x00fa)
308
259
    (*info->fprintf_func) (info->stream, " R%x, PC",  (code >> 8) & 0x7);
309
276
        else
310
276
    (*info->fprintf_func) (info->stream, " R%x", (code >> 8) & 0x7);
311
535
      }
312
0
    else
313
      /* Opcode not recognized.  */
314
0
      (*info->fprintf_func) (info->stream, "Not yet handled TEST .byte\t0x%04x", code);
315
30.4k
    return 2;
316
41.0M
  }
317
318
      /* Opcode not recognized.  */
319
3.56k
      (*info->fprintf_func) (info->stream, ".byte\t0x%04x", code);
320
3.56k
      return 2; /* Everything is two bytes.  */
321
33.9k
    }
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
705k
  status = read_memory (memaddr, buffer, 1, info);
328
705k
  if (status != 0)
329
1
    return status;
330
331
705k
  format = 0;
332
705k
  code = buffer[0];
333
705k
  pos = 0;
334
335
  /* Look for page2,3,4 opcodes.  */
336
705k
  if (code == M6811_OPCODE_PAGE2)
337
7.51k
    {
338
7.51k
      pos++;
339
7.51k
      format = M6811_OP_PAGE2;
340
7.51k
    }
341
697k
  else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
342
277
    {
343
277
      pos++;
344
277
      format = M6811_OP_PAGE3;
345
277
    }
346
697k
  else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
347
507
    {
348
507
      pos++;
349
507
      format = M6811_OP_PAGE4;
350
507
    }
351
352
  /* We are in page2,3,4; get the real opcode.  */
353
705k
  if (pos == 1)
354
8.29k
    {
355
8.29k
      status = read_memory (memaddr + pos, &buffer[1], 1, info);
356
8.29k
      if (status != 0)
357
4
  return status;
358
359
8.29k
      code = buffer[1];
360
8.29k
    }
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
705k
  if ((arch & cpu6812) && format == 0)
366
642k
    {
367
642k
      int must_read = 1;
368
369
      /* Walk the alias table to find a code1+code2 match.  */
370
12.2M
      for (i = 0; i < m68hc12_num_alias; i++)
371
11.5M
  {
372
11.5M
    if (m68hc12_alias[i].code1 == code)
373
65.4k
      {
374
65.4k
        if (must_read)
375
65.4k
    {
376
65.4k
      status = read_memory (memaddr + pos + 1,
377
65.4k
          &buffer[1], 1, info);
378
65.4k
      if (status != 0)
379
8
        break;
380
381
65.4k
      must_read = 1;
382
65.4k
    }
383
65.4k
        if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
384
437
    {
385
437
      (*info->fprintf_func) (info->stream, "%s",
386
437
           m68hc12_alias[i].name);
387
437
      return 2;
388
437
    }
389
65.4k
      }
390
11.5M
  }
391
642k
    }
392
393
705k
  pos++;
394
395
  /* Scan the opcode table until we find the opcode
396
     with the corresponding page.  */
397
705k
  opcode = m68hc11_opcodes;
398
317M
  for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
399
317M
    {
400
317M
      int offset;
401
317M
      int pc_src_offset;
402
317M
      int pc_dst_offset = 0;
403
404
317M
      if ((opcode->arch & arch) == 0)
405
153M
  continue;
406
163M
      if (opcode->opcode != code)
407
162M
  continue;
408
753k
      if ((opcode->format & OP_PAGE_MASK) != format)
409
54.8k
  continue;
410
411
698k
      if (opcode->format & M6812_OP_REG)
412
16.0k
  {
413
16.0k
    int j;
414
16.0k
    int is_jump;
415
416
16.0k
    if (opcode->format & M6811_OP_JUMP_REL)
417
12.1k
      is_jump = 1;
418
3.90k
    else
419
3.90k
      is_jump = 0;
420
421
16.0k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
422
16.0k
    if (status != 0)
423
16
      {
424
16
        return status;
425
16
      }
426
2.87M
    for (j = 0; i + j < m68hc11_num_opcodes; j++)
427
2.87M
      {
428
2.87M
        if ((opcode[j].arch & arch) == 0)
429
1.21M
    continue;
430
1.65M
        if (opcode[j].opcode != code)
431
1.62M
    continue;
432
33.4k
        if (is_jump)
433
26.3k
    {
434
26.3k
      if (!(opcode[j].format & M6811_OP_JUMP_REL))
435
2.15k
        continue;
436
437
24.2k
      if ((opcode[j].format & M6812_OP_IBCC_MARKER)
438
5.27k
          && (buffer[0] & 0xc0) != 0x80)
439
4.30k
        continue;
440
19.9k
      if ((opcode[j].format & M6812_OP_TBCC_MARKER)
441
3.37k
          && (buffer[0] & 0xc0) != 0x40)
442
1.66k
        continue;
443
18.2k
      if ((opcode[j].format & M6812_OP_DBCC_MARKER)
444
15.5k
          && (buffer[0] & 0xc0) != 0)
445
5.94k
        continue;
446
12.3k
      if ((opcode[j].format & M6812_OP_EQ_MARKER)
447
11.3k
          && (buffer[0] & 0x20) == 0)
448
10.2k
        break;
449
2.02k
      if (!(opcode[j].format & M6812_OP_EQ_MARKER)
450
1.01k
          && (buffer[0] & 0x20) != 0)
451
1.01k
        break;
452
1.01k
      continue;
453
2.02k
    }
454
7.11k
        if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
455
1.86k
    break;
456
5.24k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
457
2.03k
      && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
458
1.12k
      && ((buffer[0] & 0x0f0) <= 0x20))
459
469
    break;
460
4.77k
        if ((opcode[j].format & M6812_OP_SEX_MARKER)
461
1.56k
      && (arch & cpu9s12x)
462
1.00k
      && ((buffer[0] == 0x4d) || (buffer[0] == 0x4e)))
463
400
    break;
464
4.37k
        if (opcode[j].format & M6812_OP_TFR_MARKER
465
1.16k
      && !(buffer[0] & 0x80))
466
1.16k
    break;
467
4.37k
      }
468
16.0k
    if (i + j < m68hc11_num_opcodes)
469
15.2k
      opcode = &opcode[j];
470
16.0k
  }
471
472
      /* We have found the opcode.  Extract the operand and print it.  */
473
698k
      (*info->fprintf_func) (info->stream, "%s", opcode->name);
474
475
698k
      format = opcode->format;
476
698k
      if (format & (M6811_OP_MASK | M6811_OP_BITMASK
477
698k
        | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
478
326k
  {
479
326k
    (*info->fprintf_func) (info->stream, "\t");
480
326k
  }
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
698k
      offset = ((format & M6812_OP_IDX_P2)
511
2.78k
    && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
512
2.78k
            M6811_OP_IND16)));
513
514
698k
      if (offset)
515
2.50k
  {
516
    /* Check xb to see position of data.  */
517
2.50k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
518
2.50k
    if (status != 0)
519
3
      {
520
3
        return status;
521
3
      }
522
523
2.50k
    if (((buffer[0] & 0xe0) == 0xe0) && ((buffer[0] & 0x04) == 0))
524
1.37k
      {
525
        /* 9 or 16 bit.  */
526
1.37k
        if ((buffer[0] & 0x02) == 0)
527
303
    {
528
      /* 9 bit.  */
529
303
      offset = 2;
530
303
    }
531
1.07k
        else
532
1.07k
    {
533
      /* 16 bit.  */
534
1.07k
      offset = 3;
535
1.07k
    }
536
1.37k
      }
537
2.50k
  }
538
539
      /* Operand with one more byte: - immediate, offset,
540
   direct-low address.  */
541
698k
      if (format &
542
698k
    (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
543
82.3k
  {
544
82.3k
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
545
82.3k
    if (status != 0)
546
33
      return status;
547
548
    /* This movb/movw is special (see above).  */
549
82.3k
    if (offset < 2)
550
81.7k
      {
551
81.7k
        offset = -offset;
552
81.7k
        pc_dst_offset = 2;
553
81.7k
      }
554
573
    else
555
573
      {
556
573
        offset = -1;
557
573
        pc_dst_offset = 5;
558
573
      }
559
82.3k
    pos++;
560
561
82.3k
    if (format & M6811_OP_IMM8)
562
34.4k
      {
563
34.4k
        (*info->fprintf_func) (info->stream, "#0x%x", (int) buffer[0]);
564
34.4k
        format &= ~M6811_OP_IMM8;
565
        /* Set PC destination offset.  */
566
34.4k
        pc_dst_offset = 1;
567
34.4k
      }
568
47.9k
    else if (format & M6811_OP_IX)
569
5.43k
      {
570
        /* Offsets are in range 0..255, print them unsigned.  */
571
5.43k
        (*info->fprintf_func) (info->stream, "0x%x,x", buffer[0] & 0x0FF);
572
5.43k
        format &= ~M6811_OP_IX;
573
5.43k
      }
574
42.5k
    else if (format & M6811_OP_IY)
575
101
      {
576
101
        (*info->fprintf_func) (info->stream, "0x%x,y", buffer[0] & 0x0FF);
577
101
        format &= ~M6811_OP_IY;
578
101
      }
579
42.4k
    else if (format & M6811_OP_DIRECT)
580
42.4k
      {
581
42.4k
        (*info->fprintf_func) (info->stream, "*");
582
42.4k
        if (info->symtab_size > 0) /* Avoid duplicate 0x. */
583
9
    (*info->fprintf_func) (info->stream, "0x");
584
42.4k
        (*info->print_address_func) (buffer[0] & 0x0FF, info);
585
42.4k
        format &= ~M6811_OP_DIRECT;
586
42.4k
      }
587
82.3k
  }
588
589
698k
#define M6812_DST_MOVE  (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
590
698k
#define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
591
      /* Analyze the 68HC12 indexed byte.  */
592
698k
      if (format & M6812_INDEXED_FLAGS)
593
84.1k
  {
594
84.1k
    int indirect;
595
84.1k
    bfd_vma endaddr;
596
597
84.1k
    endaddr = memaddr + pos + 1;
598
84.1k
    if (format & M6811_OP_IND16)
599
0
      endaddr += 2;
600
84.1k
    pc_src_offset = -1;
601
84.1k
    pc_dst_offset = 1;
602
84.1k
    status = print_indexed_operand (memaddr + pos, info, &indirect,
603
84.1k
            (format & M6812_DST_MOVE),
604
84.1k
            pc_src_offset, endaddr, arch);
605
84.1k
    if (status < 0)
606
65
      return status;
607
608
84.0k
    pos += status;
609
610
    /* The indirect addressing mode of the call instruction does
611
       not need the page code.  */
612
84.0k
    if ((format & M6812_OP_PAGE) && indirect)
613
126
      format &= ~M6812_OP_PAGE;
614
84.0k
  }
615
616
      /* 68HC12 dbcc/ibcc/tbcc operands.  */
617
698k
      if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
618
12.1k
  {
619
12.1k
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
620
12.1k
    if (status != 0)
621
8
      return status;
622
623
12.1k
    (*info->fprintf_func) (info->stream, "%s,",
624
12.1k
         reg_src_table[buffer[0] & 0x07]);
625
12.1k
    val = buffer[1] - ((buffer[0] & 0x10) << 4);
626
627
12.1k
    pos += 2;
628
12.1k
    (*info->fprintf_func) (info->stream, "0x");
629
12.1k
    (*info->print_address_func) (memaddr + pos + val, info);
630
12.1k
    format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
631
12.1k
  }
632
686k
      else if (format & (M6812_OP_REG | M6812_OP_REG_2))
633
3.90k
  {
634
3.90k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
635
3.90k
    if (status != 0)
636
0
      return status;
637
638
3.90k
    pos++;
639
3.90k
    (*info->fprintf_func) (info->stream, "%s,%s",
640
3.90k
         reg_src_table[(buffer[0] >> 4) & 7],
641
3.90k
         reg_dst_table[(buffer[0] & 7)]);
642
3.90k
  }
643
644
698k
      if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
645
109k
  {
646
109k
    bfd_vma addr;
647
109k
    unsigned page = 0;
648
649
109k
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
650
109k
    if (status != 0)
651
165
      return status;
652
653
109k
    if (format & M6812_OP_IDX_P2)
654
1.90k
      offset = -2;
655
107k
    else
656
107k
      offset = 0;
657
109k
    pos += 2;
658
659
109k
    addr = val = (buffer[0] << 8) | buffer[1];
660
109k
    pc_dst_offset = 2;
661
109k
    if (format & M6812_OP_PAGE)
662
1.93k
      {
663
1.93k
        status = read_memory (memaddr + pos + offset, buffer, 1, info);
664
1.93k
        if (status != 0)
665
7
    return status;
666
667
1.93k
        page = buffer[0];
668
1.93k
        if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
669
251
    addr = (val - M68HC12_BANK_BASE + (page << M68HC12_BANK_SHIFT)
670
251
      + M68HC12_BANK_VIRT);
671
1.93k
      }
672
107k
    else if ((arch & cpu6812)
673
101k
       && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
674
12.1k
      {
675
12.1k
        unsigned cur_page;
676
12.1k
        bfd_vma vaddr;
677
678
12.1k
        if (memaddr >= M68HC12_BANK_VIRT)
679
2.61k
    cur_page = ((memaddr - M68HC12_BANK_VIRT)
680
2.61k
          >> M68HC12_BANK_SHIFT);
681
9.53k
        else
682
9.53k
    cur_page = 0;
683
684
12.1k
        vaddr = (addr - M68HC12_BANK_BASE
685
12.1k
           + (cur_page << M68HC12_BANK_SHIFT)) + M68HC12_BANK_VIRT;
686
12.1k
        if (!info->symbol_at_address_func (addr, info)
687
12.1k
      && info->symbol_at_address_func (vaddr, info))
688
0
    addr = vaddr;
689
12.1k
      }
690
109k
    if (format & M6811_OP_IMM16)
691
10.7k
      {
692
10.7k
        format &= ~M6811_OP_IMM16;
693
10.7k
        (*info->fprintf_func) (info->stream, "#");
694
10.7k
      }
695
98.7k
    else
696
98.7k
      {
697
98.7k
        format &= ~M6811_OP_IND16;
698
98.7k
      }
699
700
    /* Avoid duplicate 0x from core binutils.  */
701
109k
    if (info->symtab_size > 0)
702
17
      (*info->fprintf_func) (info->stream, "0x");
703
704
109k
    (*info->print_address_func) (addr, info);
705
109k
    if (format & M6812_OP_PAGE)
706
1.93k
      {
707
1.93k
        (* info->fprintf_func) (info->stream, " {");
708
        /* Avoid duplicate 0x from core binutils.  */
709
1.93k
        if (info->symtab_size > 0)
710
0
    (*info->fprintf_func) (info->stream, "0x");
711
1.93k
        (* info->print_address_func) (val, info);
712
1.93k
        (* info->fprintf_func) (info->stream, ", 0x%x}", page);
713
1.93k
        format &= ~M6812_OP_PAGE;
714
1.93k
        pos += 1;
715
1.93k
      }
716
109k
  }
717
718
698k
      if (format & M6812_OP_IDX_P2)
719
2.78k
  {
720
2.78k
    (*info->fprintf_func) (info->stream, ", ");
721
2.78k
    status = print_indexed_operand (memaddr + pos + offset, info,
722
2.78k
            0, 1, pc_dst_offset,
723
2.78k
            memaddr + pos + offset + 1, arch);
724
2.78k
    if (status < 0)
725
3
      return status;
726
2.78k
    pos += status;
727
2.78k
  }
728
729
698k
      if (format & M6812_OP_IND16_P2)
730
244
  {
731
244
    (*info->fprintf_func) (info->stream, ", ");
732
733
244
    status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
734
244
    if (status != 0)
735
1
      return status;
736
737
243
    pos += 2;
738
739
243
    val = (buffer[0] << 8) | buffer[1];
740
    /* Avoid duplicate 0x from core binutils.  */
741
243
    if (info->symtab_size > 0)
742
0
      (*info->fprintf_func) (info->stream, "0x");
743
243
    (*info->print_address_func) (val, info);
744
243
  }
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
698k
      if (format & M6811_OP_BITMASK)
750
21.3k
  {
751
21.3k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
752
21.3k
    if (status != 0)
753
9
      return status;
754
755
21.3k
    pos++;
756
21.3k
    (*info->fprintf_func) (info->stream, ", #0x%02x%s",
757
21.3k
         buffer[0] & 0x0FF,
758
21.3k
         (format & M6811_OP_JUMP_REL ? ", " : ""));
759
21.3k
    format &= ~M6811_OP_BITMASK;
760
21.3k
  }
761
698k
      if (format & M6811_OP_JUMP_REL)
762
43.2k
  {
763
43.2k
    status = read_memory (memaddr + pos, &buffer[0], 1, info);
764
43.2k
    if (status != 0)
765
29
      return status;
766
767
43.2k
    (*info->fprintf_func) (info->stream, "0x");
768
43.2k
    pos++;
769
43.2k
    val = (buffer[0] ^ 0x80) - 0x80;
770
43.2k
    (*info->print_address_func) (memaddr + pos + val, info);
771
43.2k
    format &= ~M6811_OP_JUMP_REL;
772
43.2k
  }
773
655k
      else if (format & M6812_OP_JUMP_REL16)
774
172
  {
775
172
    status = read_memory (memaddr + pos, &buffer[0], 2, info);
776
172
    if (status != 0)
777
1
      return status;
778
779
171
    pos += 2;
780
171
    val = (((buffer[0] << 8) | buffer[1]) ^ 0x8000) - 0x8000;
781
782
171
    (*info->fprintf_func) (info->stream, "0x");
783
171
    (*info->print_address_func) (memaddr + pos + val, info);
784
171
    format &= ~M6812_OP_JUMP_REL16;
785
171
  }
786
787
698k
      if (format & M6812_OP_PAGE)
788
2.02k
  {
789
2.02k
    status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
790
2.02k
    if (status != 0)
791
3
      return status;
792
793
2.01k
    pos += 1;
794
795
2.01k
    val = buffer[0];
796
2.01k
    (*info->fprintf_func) (info->stream, ", 0x%x", (unsigned) val);
797
2.01k
  }
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
698k
      return pos;
811
698k
    }
812
813
  /* Opcode not recognized.  */
814
6.28k
  if (format == M6811_OP_PAGE2 && arch & cpu6812
815
1.04k
      && ((code >= 0x30 && code <= 0x39) || (code >= 0x40)))
816
1.04k
    (*info->fprintf_func) (info->stream, "trap\t#0x%02x", code & 0x0ff);
817
818
5.24k
  else if (format == M6811_OP_PAGE2)
819
534
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
820
534
         M6811_OPCODE_PAGE2, code);
821
4.70k
  else if (format == M6811_OP_PAGE3)
822
277
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
823
277
         M6811_OPCODE_PAGE3, code);
824
4.42k
  else if (format == M6811_OP_PAGE4)
825
506
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
826
506
         M6811_OPCODE_PAGE4, code);
827
3.92k
  else
828
3.92k
    (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
829
830
6.28k
  return pos;
831
705k
}
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
55.6k
{
838
55.6k
  return print_insn (memaddr, info, cpu6811);
839
55.6k
}
840
841
int
842
print_insn_m68hc12 (bfd_vma memaddr, struct disassemble_info* info)
843
570k
{
844
570k
  return print_insn (memaddr, info, cpu6812);
845
570k
}
846
847
int
848
print_insn_m9s12x (bfd_vma memaddr, struct disassemble_info* info)
849
78.9k
{
850
78.9k
  return print_insn (memaddr, info, cpu6812|cpu9s12x);
851
78.9k
}
852
853
int
854
print_insn_m9s12xg (bfd_vma memaddr, struct disassemble_info* info)
855
34.0k
{
856
34.0k
  return print_insn (memaddr, info, cpuxgate);
857
34.0k
}