1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@nerim.fr)
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "opcode/m68hc11.h"
27 static const char *const reg_name
[] = {
31 static const char *const reg_src_table
[] = {
32 "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
35 static const char *const reg_dst_table
[] = {
36 "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
39 #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
41 /* Prototypes for local functions. */
42 static int read_memory (bfd_vma
, bfd_byte
*, int, struct disassemble_info
*);
43 static int print_indexed_operand (bfd_vma
, struct disassemble_info
*,
44 int*, int, int, bfd_vma
);
45 static int print_insn (bfd_vma
, struct disassemble_info
*, int);
48 read_memory (bfd_vma memaddr
, bfd_byte
* buffer
, int size
,
49 struct disassemble_info
* info
)
53 /* Get first byte. Only one at a time because we don't know the
55 status
= (*info
->read_memory_func
) (memaddr
, buffer
, size
, info
);
58 (*info
->memory_error_func
) (status
, memaddr
, info
);
65 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
66 Returns the number of bytes read or -1 if failure. */
68 print_indexed_operand (bfd_vma memaddr
, struct disassemble_info
* info
,
69 int* indirect
, int mov_insn
, int pc_offset
,
81 status
= read_memory (memaddr
, &buffer
[0], 1, info
);
87 /* n,r with 5-bits signed constant. */
88 if ((buffer
[0] & 0x20) == 0)
90 reg
= (buffer
[0] >> 6) & 3;
91 sval
= (buffer
[0] & 0x1f);
94 /* 68HC12 requires an adjustment for movb/movw pc relative modes. */
95 if (reg
== PC_REGNUM
&& info
->mach
== bfd_mach_m6812
&& mov_insn
)
97 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
98 (int) sval
, reg_name
[reg
]);
100 if (reg
== PC_REGNUM
)
102 (* info
->fprintf_func
) (info
->stream
, " {");
103 (* info
->print_address_func
) (endaddr
+ sval
, info
);
104 (* info
->fprintf_func
) (info
->stream
, "}");
108 /* Auto pre/post increment/decrement. */
109 else if ((buffer
[0] & 0xc0) != 0xc0)
113 reg
= (buffer
[0] >> 6) & 3;
114 sval
= (buffer
[0] & 0x0f);
126 (*info
->fprintf_func
) (info
->stream
, "%d,%s%s%s",
128 (buffer
[0] & 0x10 ? "" : mode
),
129 reg_name
[reg
], (buffer
[0] & 0x10 ? mode
: ""));
132 /* [n,r] 16-bits offset indexed indirect. */
133 else if ((buffer
[0] & 0x07) == 3)
137 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
141 reg
= (buffer
[0] >> 3) & 0x03;
142 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
149 sval
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
150 (*info
->fprintf_func
) (info
->stream
, "[%u,%s]",
151 sval
& 0x0ffff, reg_name
[reg
]);
156 /* n,r with 9 and 16 bit signed constant. */
157 else if ((buffer
[0] & 0x4) == 0)
161 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
165 reg
= (buffer
[0] >> 3) & 0x03;
166 status
= read_memory (memaddr
+ pos
,
167 &buffer
[1], (buffer
[0] & 0x2 ? 2 : 1), info
);
174 sval
= ((buffer
[1] << 8) | (buffer
[2] & 0x0FF));
181 sval
= buffer
[1] & 0x00ff;
182 if (buffer
[0] & 0x01)
187 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
188 (int) sval
, reg_name
[reg
]);
189 if (reg
== PC_REGNUM
)
191 (* info
->fprintf_func
) (info
->stream
, " {");
192 (* info
->print_address_func
) (endaddr
+ sval
, info
);
193 (* info
->fprintf_func
) (info
->stream
, "}");
198 reg
= (buffer
[0] >> 3) & 0x03;
199 switch (buffer
[0] & 3)
202 (*info
->fprintf_func
) (info
->stream
, "A,%s", reg_name
[reg
]);
205 (*info
->fprintf_func
) (info
->stream
, "B,%s", reg_name
[reg
]);
208 (*info
->fprintf_func
) (info
->stream
, "D,%s", reg_name
[reg
]);
212 (*info
->fprintf_func
) (info
->stream
, "[D,%s]", reg_name
[reg
]);
222 /* Disassemble one instruction at address 'memaddr'. Returns the number
223 of bytes used by that instruction. */
225 print_insn (bfd_vma memaddr
, struct disassemble_info
* info
, int arch
)
232 const struct m68hc11_opcode
*opcode
;
234 /* Get first byte. Only one at a time because we don't know the
236 status
= read_memory (memaddr
, buffer
, 1, info
);
246 /* Look for page2,3,4 opcodes. */
247 if (code
== M6811_OPCODE_PAGE2
)
250 format
= M6811_OP_PAGE2
;
252 else if (code
== M6811_OPCODE_PAGE3
&& arch
== cpu6811
)
255 format
= M6811_OP_PAGE3
;
257 else if (code
== M6811_OPCODE_PAGE4
&& arch
== cpu6811
)
260 format
= M6811_OP_PAGE4
;
263 /* We are in page2,3,4; get the real opcode. */
266 status
= read_memory (memaddr
+ pos
, &buffer
[1], 1, info
);
275 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
276 in page 1. There is no operand to print. We read the second byte
277 only when we have a possible match. */
278 if ((arch
& cpu6812
) && format
== 0)
282 /* Walk the alias table to find a code1+code2 match. */
283 for (i
= 0; i
< m68hc12_num_alias
; i
++)
285 if (m68hc12_alias
[i
].code1
== code
)
289 status
= read_memory (memaddr
+ pos
+ 1,
290 &buffer
[1], 1, info
);
296 if (m68hc12_alias
[i
].code2
== (unsigned char) buffer
[1])
298 (*info
->fprintf_func
) (info
->stream
, "%s",
299 m68hc12_alias
[i
].name
);
308 /* Scan the opcode table until we find the opcode
309 with the corresponding page. */
310 opcode
= m68hc11_opcodes
;
311 for (i
= 0; i
< m68hc11_num_opcodes
; i
++, opcode
++)
315 int pc_dst_offset
= 0;
317 if ((opcode
->arch
& arch
) == 0)
319 if (opcode
->opcode
!= code
)
321 if ((opcode
->format
& OP_PAGE_MASK
) != format
)
324 if (opcode
->format
& M6812_OP_REG
)
329 if (opcode
->format
& M6811_OP_JUMP_REL
)
334 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
339 for (j
= 0; i
+ j
< m68hc11_num_opcodes
; j
++)
341 if ((opcode
[j
].arch
& arch
) == 0)
343 if (opcode
[j
].opcode
!= code
)
347 if (!(opcode
[j
].format
& M6811_OP_JUMP_REL
))
350 if ((opcode
[j
].format
& M6812_OP_IBCC_MARKER
)
351 && (buffer
[0] & 0xc0) != 0x80)
353 if ((opcode
[j
].format
& M6812_OP_TBCC_MARKER
)
354 && (buffer
[0] & 0xc0) != 0x40)
356 if ((opcode
[j
].format
& M6812_OP_DBCC_MARKER
)
357 && (buffer
[0] & 0xc0) != 0)
359 if ((opcode
[j
].format
& M6812_OP_EQ_MARKER
)
360 && (buffer
[0] & 0x20) == 0)
362 if (!(opcode
[j
].format
& M6812_OP_EQ_MARKER
)
363 && (buffer
[0] & 0x20) != 0)
367 if (opcode
[j
].format
& M6812_OP_EXG_MARKER
&& buffer
[0] & 0x80)
369 if ((opcode
[j
].format
& M6812_OP_SEX_MARKER
)
370 && (((buffer
[0] & 0x07) >= 3 && (buffer
[0] & 7) <= 7))
371 && ((buffer
[0] & 0x0f0) <= 0x20))
373 if (opcode
[j
].format
& M6812_OP_TFR_MARKER
374 && !(buffer
[0] & 0x80))
377 if (i
+ j
< m68hc11_num_opcodes
)
381 /* We have found the opcode. Extract the operand and print it. */
382 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
384 format
= opcode
->format
;
385 if (format
& (M6811_OP_MASK
| M6811_OP_BITMASK
386 | M6811_OP_JUMP_REL
| M6812_OP_JUMP_REL16
))
388 (*info
->fprintf_func
) (info
->stream
, "\t");
391 /* The movb and movw must be handled in a special way...
392 The source constant 'ii' is not always at the same place.
393 This is the same for the destination for the post-indexed byte.
394 The 'offset' is used to do the appropriate correction.
397 for constant for destination
398 movb 18 OB ii hh ll 0 0
400 18 0C hh ll hh ll 0 0
405 movw 18 03 jj kk hh ll 0 0
407 18 04 hh ll hh ll 0 0
412 After the source operand is read, the position 'pos' is incremented
413 this explains the negative offset for destination.
415 movb/movw above are the only instructions with this matching
417 offset
= ((format
& M6812_OP_IDX_P2
)
418 && (format
& (M6811_OP_IMM8
| M6811_OP_IMM16
|
421 /* Operand with one more byte: - immediate, offset,
422 direct-low address. */
424 (M6811_OP_IMM8
| M6811_OP_IX
| M6811_OP_IY
| M6811_OP_DIRECT
))
426 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
434 /* This movb/movw is special (see above). */
438 if (format
& M6811_OP_IMM8
)
440 (*info
->fprintf_func
) (info
->stream
, "#%d", (int) buffer
[0]);
441 format
&= ~M6811_OP_IMM8
;
442 /* Set PC destination offset. */
445 else if (format
& M6811_OP_IX
)
447 /* Offsets are in range 0..255, print them unsigned. */
448 (*info
->fprintf_func
) (info
->stream
, "%u,x", buffer
[0] & 0x0FF);
449 format
&= ~M6811_OP_IX
;
451 else if (format
& M6811_OP_IY
)
453 (*info
->fprintf_func
) (info
->stream
, "%u,y", buffer
[0] & 0x0FF);
454 format
&= ~M6811_OP_IY
;
456 else if (format
& M6811_OP_DIRECT
)
458 (*info
->fprintf_func
) (info
->stream
, "*");
459 (*info
->print_address_func
) (buffer
[0] & 0x0FF, info
);
460 format
&= ~M6811_OP_DIRECT
;
464 #define M6812_DST_MOVE (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)
465 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
466 /* Analyze the 68HC12 indexed byte. */
467 if (format
& M6812_INDEXED_FLAGS
)
472 endaddr
= memaddr
+ pos
+ 1;
473 if (format
& M6811_OP_IND16
)
477 status
= print_indexed_operand (memaddr
+ pos
, info
, &indirect
,
478 (format
& M6812_DST_MOVE
),
479 pc_src_offset
, endaddr
);
486 /* The indirect addressing mode of the call instruction does
487 not need the page code. */
488 if ((format
& M6812_OP_PAGE
) && indirect
)
489 format
&= ~M6812_OP_PAGE
;
492 /* 68HC12 dbcc/ibcc/tbcc operands. */
493 if ((format
& M6812_OP_REG
) && (format
& M6811_OP_JUMP_REL
))
495 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
500 (*info
->fprintf_func
) (info
->stream
, "%s,",
501 reg_src_table
[buffer
[0] & 0x07]);
502 sval
= buffer
[1] & 0x0ff;
503 if (buffer
[0] & 0x10)
507 (*info
->print_address_func
) (memaddr
+ pos
+ sval
, info
);
508 format
&= ~(M6812_OP_REG
| M6811_OP_JUMP_REL
);
510 else if (format
& (M6812_OP_REG
| M6812_OP_REG_2
))
512 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
519 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
520 reg_src_table
[(buffer
[0] >> 4) & 7],
521 reg_dst_table
[(buffer
[0] & 7)]);
524 if (format
& (M6811_OP_IMM16
| M6811_OP_IND16
))
530 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
535 if (format
& M6812_OP_IDX_P2
)
541 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
545 if (format
& M6812_OP_PAGE
)
547 status
= read_memory (memaddr
+ pos
+ offset
, buffer
, 1, info
);
551 page
= (unsigned) buffer
[0];
552 if (addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
553 addr
= ((val
- M68HC12_BANK_BASE
)
554 | (page
<< M68HC12_BANK_SHIFT
))
557 else if ((arch
& cpu6812
)
558 && addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
563 if (memaddr
>= M68HC12_BANK_VIRT
)
564 cur_page
= ((memaddr
- M68HC12_BANK_VIRT
)
565 >> M68HC12_BANK_SHIFT
);
569 vaddr
= ((addr
- M68HC12_BANK_BASE
)
570 + (cur_page
<< M68HC12_BANK_SHIFT
))
572 if (!info
->symbol_at_address_func (addr
, info
)
573 && info
->symbol_at_address_func (vaddr
, info
))
576 if (format
& M6811_OP_IMM16
)
578 format
&= ~M6811_OP_IMM16
;
579 (*info
->fprintf_func
) (info
->stream
, "#");
582 format
&= ~M6811_OP_IND16
;
584 (*info
->print_address_func
) (addr
, info
);
585 if (format
& M6812_OP_PAGE
)
587 (* info
->fprintf_func
) (info
->stream
, " {");
588 (* info
->print_address_func
) (val
, info
);
589 (* info
->fprintf_func
) (info
->stream
, ", %d}", page
);
590 format
&= ~M6812_OP_PAGE
;
595 if (format
& M6812_OP_IDX_P2
)
597 (*info
->fprintf_func
) (info
->stream
, ", ");
598 status
= print_indexed_operand (memaddr
+ pos
+ offset
, info
,
600 memaddr
+ pos
+ offset
+ 1);
606 if (format
& M6812_OP_IND16_P2
)
610 (*info
->fprintf_func
) (info
->stream
, ", ");
612 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
619 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
621 (*info
->print_address_func
) (val
, info
);
624 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
625 and in that order. The brset/brclr insn have a bitmask and then
626 a relative branch offset. */
627 if (format
& M6811_OP_BITMASK
)
629 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
635 (*info
->fprintf_func
) (info
->stream
, " #$%02x%s",
637 (format
& M6811_OP_JUMP_REL
? " " : ""));
638 format
&= ~M6811_OP_BITMASK
;
640 if (format
& M6811_OP_JUMP_REL
)
644 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
651 val
= (buffer
[0] & 0x80) ? buffer
[0] | 0xFFFFFF00 : buffer
[0];
652 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
653 format
&= ~M6811_OP_JUMP_REL
;
655 else if (format
& M6812_OP_JUMP_REL16
)
659 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
666 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
670 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
671 format
&= ~M6812_OP_JUMP_REL16
;
674 if (format
& M6812_OP_PAGE
)
678 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
685 val
= buffer
[0] & 0x0ff;
686 (*info
->fprintf_func
) (info
->stream
, ", %d", val
);
690 /* Consistency check. 'format' must be 0, so that we have handled
691 all formats; and the computed size of the insn must match the
692 opcode table content. */
693 if (format
& ~(M6811_OP_PAGE4
| M6811_OP_PAGE3
| M6811_OP_PAGE2
))
695 (*info
->fprintf_func
) (info
->stream
, "; Error, format: %x", format
);
697 if (pos
!= opcode
->size
)
699 (*info
->fprintf_func
) (info
->stream
, "; Error, size: %d expect %d",
706 /* Opcode not recognized. */
707 if (format
== M6811_OP_PAGE2
&& arch
& cpu6812
708 && ((code
>= 0x30 && code
<= 0x39) || (code
>= 0x40)))
709 (*info
->fprintf_func
) (info
->stream
, "trap\t#%d", code
& 0x0ff);
711 else if (format
== M6811_OP_PAGE2
)
712 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
713 M6811_OPCODE_PAGE2
, code
);
714 else if (format
== M6811_OP_PAGE3
)
715 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
716 M6811_OPCODE_PAGE3
, code
);
717 else if (format
== M6811_OP_PAGE4
)
718 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
719 M6811_OPCODE_PAGE4
, code
);
721 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", code
);
726 /* Disassemble one instruction at address 'memaddr'. Returns the number
727 of bytes used by that instruction. */
729 print_insn_m68hc11 (bfd_vma memaddr
, struct disassemble_info
* info
)
731 return print_insn (memaddr
, info
, cpu6811
);
735 print_insn_m68hc12 (bfd_vma memaddr
, struct disassemble_info
* info
)
737 return print_insn (memaddr
, info
, cpu6812
);