Coverage Report

Created: 2023-08-28 06:23

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