1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Written by Stephane Carrez (stcarrez@nerim.fr)
6 This file is part of the GNU opcodes library.
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)
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.
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. */
26 #include "opcode/m68hc11.h"
31 static const char *const reg_name
[] = {
35 static const char *const reg_src_table
[] = {
36 "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
39 static const char *const reg_dst_table
[] = {
40 "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
43 #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
45 /* Prototypes for local functions. */
46 static int read_memory (bfd_vma
, bfd_byte
*, int, struct disassemble_info
*);
47 static int print_indexed_operand (bfd_vma
, struct disassemble_info
*,
48 int*, int, int, bfd_vma
);
49 static int print_insn (bfd_vma
, struct disassemble_info
*, int);
52 read_memory (bfd_vma memaddr
, bfd_byte
* buffer
, int size
,
53 struct disassemble_info
* info
)
57 /* Get first byte. Only one at a time because we don't know the
59 status
= (*info
->read_memory_func
) (memaddr
, buffer
, size
, info
);
62 (*info
->memory_error_func
) (status
, memaddr
, info
);
69 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
70 Returns the number of bytes read or -1 if failure. */
72 print_indexed_operand (bfd_vma memaddr
, struct disassemble_info
* info
,
73 int* indirect
, int mov_insn
, int pc_offset
,
85 status
= read_memory (memaddr
, &buffer
[0], 1, info
);
91 /* n,r with 5-bits signed constant. */
92 if ((buffer
[0] & 0x20) == 0)
94 reg
= (buffer
[0] >> 6) & 3;
95 sval
= (buffer
[0] & 0x1f);
98 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
99 if (reg
== PC_REGNUM
&& info
->mach
== bfd_mach_m6812
&& mov_insn
)
101 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
102 (int) sval
, reg_name
[reg
]);
104 if (reg
== PC_REGNUM
)
106 (* info
->fprintf_func
) (info
->stream
, " {");
107 (* info
->print_address_func
) (endaddr
+ sval
, info
);
108 (* info
->fprintf_func
) (info
->stream
, "}");
112 /* Auto pre/post increment/decrement. */
113 else if ((buffer
[0] & 0xc0) != 0xc0)
117 reg
= (buffer
[0] >> 6) & 3;
118 sval
= (buffer
[0] & 0x0f);
130 (*info
->fprintf_func
) (info
->stream
, "%d,%s%s%s",
132 (buffer
[0] & 0x10 ? "" : mode
),
133 reg_name
[reg
], (buffer
[0] & 0x10 ? mode
: ""));
136 /* [n,r] 16-bits offset indexed indirect. */
137 else if ((buffer
[0] & 0x07) == 3)
141 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
145 reg
= (buffer
[0] >> 3) & 0x03;
146 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
153 sval
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
154 (*info
->fprintf_func
) (info
->stream
, "[%u,%s]",
155 sval
& 0x0ffff, reg_name
[reg
]);
160 /* n,r with 9 and 16 bit signed constant. */
161 else if ((buffer
[0] & 0x4) == 0)
165 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
169 reg
= (buffer
[0] >> 3) & 0x03;
170 status
= read_memory (memaddr
+ pos
,
171 &buffer
[1], (buffer
[0] & 0x2 ? 2 : 1), info
);
178 sval
= ((buffer
[1] << 8) | (buffer
[2] & 0x0FF));
185 sval
= buffer
[1] & 0x00ff;
186 if (buffer
[0] & 0x01)
191 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
192 (int) sval
, reg_name
[reg
]);
193 if (reg
== PC_REGNUM
)
195 (* info
->fprintf_func
) (info
->stream
, " {");
196 (* info
->print_address_func
) (endaddr
+ sval
, info
);
197 (* info
->fprintf_func
) (info
->stream
, "}");
202 reg
= (buffer
[0] >> 3) & 0x03;
203 switch (buffer
[0] & 3)
206 (*info
->fprintf_func
) (info
->stream
, "A,%s", reg_name
[reg
]);
209 (*info
->fprintf_func
) (info
->stream
, "B,%s", reg_name
[reg
]);
212 (*info
->fprintf_func
) (info
->stream
, "D,%s", reg_name
[reg
]);
216 (*info
->fprintf_func
) (info
->stream
, "[D,%s]", reg_name
[reg
]);
226 /* Disassemble one instruction at address 'memaddr'. Returns the number
227 of bytes used by that instruction. */
229 print_insn (bfd_vma memaddr
, struct disassemble_info
* info
, int arch
)
236 const struct m68hc11_opcode
*opcode
;
238 /* Get first byte. Only one at a time because we don't know the
240 status
= read_memory (memaddr
, buffer
, 1, info
);
250 /* Look for page2,3,4 opcodes. */
251 if (code
== M6811_OPCODE_PAGE2
)
254 format
= M6811_OP_PAGE2
;
256 else if (code
== M6811_OPCODE_PAGE3
&& arch
== cpu6811
)
259 format
= M6811_OP_PAGE3
;
261 else if (code
== M6811_OPCODE_PAGE4
&& arch
== cpu6811
)
264 format
= M6811_OP_PAGE4
;
267 /* We are in page2,3,4; get the real opcode. */
270 status
= read_memory (memaddr
+ pos
, &buffer
[1], 1, info
);
279 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
280 in page 1. There is no operand to print. We read the second byte
281 only when we have a possible match. */
282 if ((arch
& cpu6812
) && format
== 0)
286 /* Walk the alias table to find a code1+code2 match. */
287 for (i
= 0; i
< m68hc12_num_alias
; i
++)
289 if (m68hc12_alias
[i
].code1
== code
)
293 status
= read_memory (memaddr
+ pos
+ 1,
294 &buffer
[1], 1, info
);
300 if (m68hc12_alias
[i
].code2
== (unsigned char) buffer
[1])
302 (*info
->fprintf_func
) (info
->stream
, "%s",
303 m68hc12_alias
[i
].name
);
312 /* Scan the opcode table until we find the opcode
313 with the corresponding page. */
314 opcode
= m68hc11_opcodes
;
315 for (i
= 0; i
< m68hc11_num_opcodes
; i
++, opcode
++)
319 int pc_dst_offset
= 0;
321 if ((opcode
->arch
& arch
) == 0)
323 if (opcode
->opcode
!= code
)
325 if ((opcode
->format
& OP_PAGE_MASK
) != format
)
328 if (opcode
->format
& M6812_OP_REG
)
333 if (opcode
->format
& M6811_OP_JUMP_REL
)
338 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
343 for (j
= 0; i
+ j
< m68hc11_num_opcodes
; j
++)
345 if ((opcode
[j
].arch
& arch
) == 0)
347 if (opcode
[j
].opcode
!= code
)
351 if (!(opcode
[j
].format
& M6811_OP_JUMP_REL
))
354 if ((opcode
[j
].format
& M6812_OP_IBCC_MARKER
)
355 && (buffer
[0] & 0xc0) != 0x80)
357 if ((opcode
[j
].format
& M6812_OP_TBCC_MARKER
)
358 && (buffer
[0] & 0xc0) != 0x40)
360 if ((opcode
[j
].format
& M6812_OP_DBCC_MARKER
)
361 && (buffer
[0] & 0xc0) != 0)
363 if ((opcode
[j
].format
& M6812_OP_EQ_MARKER
)
364 && (buffer
[0] & 0x20) == 0)
366 if (!(opcode
[j
].format
& M6812_OP_EQ_MARKER
)
367 && (buffer
[0] & 0x20) != 0)
371 if (opcode
[j
].format
& M6812_OP_EXG_MARKER
&& buffer
[0] & 0x80)
373 if ((opcode
[j
].format
& M6812_OP_SEX_MARKER
)
374 && (((buffer
[0] & 0x07) >= 3 && (buffer
[0] & 7) <= 7))
375 && ((buffer
[0] & 0x0f0) <= 0x20))
377 if (opcode
[j
].format
& M6812_OP_TFR_MARKER
378 && !(buffer
[0] & 0x80))
381 if (i
+ j
< m68hc11_num_opcodes
)
385 /* We have found the opcode. Extract the operand and print it. */
386 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
388 format
= opcode
->format
;
389 if (format
& (M6811_OP_MASK
| M6811_OP_BITMASK
390 | M6811_OP_JUMP_REL
| M6812_OP_JUMP_REL16
))
392 (*info
->fprintf_func
) (info
->stream
, "\t");
395 /* The movb and movw must be handled in a special way...
396 The source constant 'ii' is not always at the same place.
397 This is the same for the destination for the post-indexed byte.
398 The 'offset' is used to do the appropriate correction.
401 for constant for destination
402 movb 18 OB ii hh ll 0 0
404 18 0C hh ll hh ll 0 0
409 movw 18 03 jj kk hh ll 0 0
411 18 04 hh ll hh ll 0 0
416 After the source operand is read, the position 'pos' is incremented
417 this explains the negative offset for destination.
419 movb/movw above are the only instructions with this matching
421 offset
= ((format
& M6812_OP_IDX_P2
)
422 && (format
& (M6811_OP_IMM8
| M6811_OP_IMM16
|
425 /* Operand with one more byte: - immediate, offset,
426 direct-low address. */
428 (M6811_OP_IMM8
| M6811_OP_IX
| M6811_OP_IY
| M6811_OP_DIRECT
))
430 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
438 /* This movb/movw is special (see above). */
442 if (format
& M6811_OP_IMM8
)
444 (*info
->fprintf_func
) (info
->stream
, "#%d", (int) buffer
[0]);
445 format
&= ~M6811_OP_IMM8
;
446 /* Set PC destination offset. */
449 else if (format
& M6811_OP_IX
)
451 /* Offsets are in range 0..255, print them unsigned. */
452 (*info
->fprintf_func
) (info
->stream
, "%u,x", buffer
[0] & 0x0FF);
453 format
&= ~M6811_OP_IX
;
455 else if (format
& M6811_OP_IY
)
457 (*info
->fprintf_func
) (info
->stream
, "%u,y", buffer
[0] & 0x0FF);
458 format
&= ~M6811_OP_IY
;
460 else if (format
& M6811_OP_DIRECT
)
462 (*info
->fprintf_func
) (info
->stream
, "*");
463 (*info
->print_address_func
) (buffer
[0] & 0x0FF, info
);
464 format
&= ~M6811_OP_DIRECT
;
468 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
469 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
470 /* Analyze the 68HC12 indexed byte. */
471 if (format
& M6812_INDEXED_FLAGS
)
476 endaddr
= memaddr
+ pos
+ 1;
477 if (format
& M6811_OP_IND16
)
481 status
= print_indexed_operand (memaddr
+ pos
, info
, &indirect
,
482 (format
& M6812_DST_MOVE
),
483 pc_src_offset
, endaddr
);
490 /* The indirect addressing mode of the call instruction does
491 not need the page code. */
492 if ((format
& M6812_OP_PAGE
) && indirect
)
493 format
&= ~M6812_OP_PAGE
;
496 /* 68HC12 dbcc/ibcc/tbcc operands. */
497 if ((format
& M6812_OP_REG
) && (format
& M6811_OP_JUMP_REL
))
499 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
504 (*info
->fprintf_func
) (info
->stream
, "%s,",
505 reg_src_table
[buffer
[0] & 0x07]);
506 sval
= buffer
[1] & 0x0ff;
507 if (buffer
[0] & 0x10)
511 (*info
->print_address_func
) (memaddr
+ pos
+ sval
, info
);
512 format
&= ~(M6812_OP_REG
| M6811_OP_JUMP_REL
);
514 else if (format
& (M6812_OP_REG
| M6812_OP_REG_2
))
516 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
523 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
524 reg_src_table
[(buffer
[0] >> 4) & 7],
525 reg_dst_table
[(buffer
[0] & 7)]);
528 if (format
& (M6811_OP_IMM16
| M6811_OP_IND16
))
534 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
539 if (format
& M6812_OP_IDX_P2
)
545 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
549 if (format
& M6812_OP_PAGE
)
551 status
= read_memory (memaddr
+ pos
+ offset
, buffer
, 1, info
);
555 page
= (unsigned) buffer
[0];
556 if (addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
557 addr
= ((val
- M68HC12_BANK_BASE
)
558 | (page
<< M68HC12_BANK_SHIFT
))
561 else if ((arch
& cpu6812
)
562 && addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
567 if (memaddr
>= M68HC12_BANK_VIRT
)
568 cur_page
= ((memaddr
- M68HC12_BANK_VIRT
)
569 >> M68HC12_BANK_SHIFT
);
573 vaddr
= ((addr
- M68HC12_BANK_BASE
)
574 + (cur_page
<< M68HC12_BANK_SHIFT
))
576 if (!info
->symbol_at_address_func (addr
, info
)
577 && info
->symbol_at_address_func (vaddr
, info
))
580 if (format
& M6811_OP_IMM16
)
582 format
&= ~M6811_OP_IMM16
;
583 (*info
->fprintf_func
) (info
->stream
, "#");
586 format
&= ~M6811_OP_IND16
;
588 (*info
->print_address_func
) (addr
, info
);
589 if (format
& M6812_OP_PAGE
)
591 (* info
->fprintf_func
) (info
->stream
, " {");
592 (* info
->print_address_func
) (val
, info
);
593 (* info
->fprintf_func
) (info
->stream
, ", %d}", page
);
594 format
&= ~M6812_OP_PAGE
;
599 if (format
& M6812_OP_IDX_P2
)
601 (*info
->fprintf_func
) (info
->stream
, ", ");
602 status
= print_indexed_operand (memaddr
+ pos
+ offset
, info
,
604 memaddr
+ pos
+ offset
+ 1);
610 if (format
& M6812_OP_IND16_P2
)
614 (*info
->fprintf_func
) (info
->stream
, ", ");
616 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
623 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
625 (*info
->print_address_func
) (val
, info
);
628 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
629 and in that order. The brset/brclr insn have a bitmask and then
630 a relative branch offset. */
631 if (format
& M6811_OP_BITMASK
)
633 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
639 (*info
->fprintf_func
) (info
->stream
, " #$%02x%s",
641 (format
& M6811_OP_JUMP_REL
? " " : ""));
642 format
&= ~M6811_OP_BITMASK
;
644 if (format
& M6811_OP_JUMP_REL
)
648 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
655 val
= (buffer
[0] & 0x80) ? buffer
[0] | 0xFFFFFF00 : buffer
[0];
656 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
657 format
&= ~M6811_OP_JUMP_REL
;
659 else if (format
& M6812_OP_JUMP_REL16
)
663 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
670 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
674 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
675 format
&= ~M6812_OP_JUMP_REL16
;
678 if (format
& M6812_OP_PAGE
)
682 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
689 val
= buffer
[0] & 0x0ff;
690 (*info
->fprintf_func
) (info
->stream
, ", %d", val
);
694 /* Consistency check. 'format' must be 0, so that we have handled
695 all formats; and the computed size of the insn must match the
696 opcode table content. */
697 if (format
& ~(M6811_OP_PAGE4
| M6811_OP_PAGE3
| M6811_OP_PAGE2
))
699 (*info
->fprintf_func
) (info
->stream
, "; Error, format: %lx", format
);
701 if (pos
!= opcode
->size
)
703 (*info
->fprintf_func
) (info
->stream
, "; Error, size: %ld expect %d",
710 /* Opcode not recognized. */
711 if (format
== M6811_OP_PAGE2
&& arch
& cpu6812
712 && ((code
>= 0x30 && code
<= 0x39) || (code
>= 0x40)))
713 (*info
->fprintf_func
) (info
->stream
, "trap\t#%d", code
& 0x0ff);
715 else if (format
== M6811_OP_PAGE2
)
716 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
717 M6811_OPCODE_PAGE2
, code
);
718 else if (format
== M6811_OP_PAGE3
)
719 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
720 M6811_OPCODE_PAGE3
, code
);
721 else if (format
== M6811_OP_PAGE4
)
722 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
723 M6811_OPCODE_PAGE4
, code
);
725 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", code
);
730 /* Disassemble one instruction at address 'memaddr'. Returns the number
731 of bytes used by that instruction. */
733 print_insn_m68hc11 (bfd_vma memaddr
, struct disassemble_info
* info
)
735 return print_insn (memaddr
, info
, cpu6811
);
739 print_insn_m68hc12 (bfd_vma memaddr
, struct disassemble_info
* info
)
741 return print_insn (memaddr
, info
, cpu6812
);