Coverage Report

Created: 2026-10-02 09:53

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