Coverage Report

Created: 2026-05-11 07:54

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