Coverage Report

Created: 2026-07-25 10:20

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