1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright 1999, 2000, 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "opcode/m68hc11.h"
25 static const char *const reg_name
[] = {
29 static const char *const reg_src_table
[] = {
30 "A", "B", "CCR", "TMP3", "D", "X", "Y", "SP"
33 static const char *const reg_dst_table
[] = {
34 "A", "B", "CCR", "TMP2", "D", "X", "Y", "SP"
37 #define OP_PAGE_MASK (M6811_OP_PAGE2|M6811_OP_PAGE3|M6811_OP_PAGE4)
39 /* Prototypes for local functions. */
40 static int read_memory
41 PARAMS ((bfd_vma
, bfd_byte
*, int, struct disassemble_info
*));
42 static int print_indexed_operand
43 PARAMS ((bfd_vma
, struct disassemble_info
*, int*, int));
45 PARAMS ((bfd_vma
, struct disassemble_info
*, int));
48 read_memory (memaddr
, buffer
, size
, info
)
52 struct disassemble_info
*info
;
56 /* Get first byte. Only one at a time because we don't know the
58 status
= (*info
->read_memory_func
) (memaddr
, buffer
, size
, info
);
61 (*info
->memory_error_func
) (status
, memaddr
, info
);
68 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
69 Returns the number of bytes read or -1 if failure. */
71 print_indexed_operand (memaddr
, info
, indirect
, mov_insn
)
73 struct disassemble_info
*info
;
86 status
= read_memory (memaddr
, &buffer
[0], 1, info
);
92 /* n,r with 5-bits signed constant. */
93 if ((buffer
[0] & 0x20) == 0)
95 reg
= (buffer
[0] >> 6) & 3;
96 sval
= (buffer
[0] & 0x1f);
99 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
100 (int) sval
, reg_name
[reg
]);
103 /* Auto pre/post increment/decrement. */
104 else if ((buffer
[0] & 0xc0) != 0xc0)
108 reg
= (buffer
[0] >> 6) & 3;
109 sval
= (buffer
[0] & 0x0f);
121 (*info
->fprintf_func
) (info
->stream
, "%d,%s%s%s",
123 (buffer
[0] & 0x10 ? "" : mode
),
124 reg_name
[reg
], (buffer
[0] & 0x10 ? mode
: ""));
127 /* [n,r] 16-bits offset indexed indirect. */
128 else if ((buffer
[0] & 0x07) == 3)
132 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
136 reg
= (buffer
[0] >> 3) & 0x03;
137 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
144 sval
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
145 (*info
->fprintf_func
) (info
->stream
, "[%u,%s]",
146 sval
& 0x0ffff, reg_name
[reg
]);
150 else if ((buffer
[0] & 0x4) == 0)
154 (*info
->fprintf_func
) (info
->stream
, "<invalid op: 0x%x>",
158 reg
= (buffer
[0] >> 3) & 0x03;
159 status
= read_memory (memaddr
+ pos
,
160 &buffer
[1], (buffer
[0] & 0x2 ? 2 : 1), info
);
167 sval
= ((buffer
[1] << 8) | (buffer
[2] & 0x0FF));
173 sval
= buffer
[1] & 0x00ff;
174 if (buffer
[0] & 0x01)
178 (*info
->fprintf_func
) (info
->stream
, "%d,%s",
179 (int) sval
, reg_name
[reg
]);
183 reg
= (buffer
[0] >> 3) & 0x03;
184 switch (buffer
[0] & 3)
187 (*info
->fprintf_func
) (info
->stream
, "A,%s", reg_name
[reg
]);
190 (*info
->fprintf_func
) (info
->stream
, "B,%s", reg_name
[reg
]);
193 (*info
->fprintf_func
) (info
->stream
, "D,%s", reg_name
[reg
]);
197 (*info
->fprintf_func
) (info
->stream
, "[D,%s]", reg_name
[reg
]);
207 /* Disassemble one instruction at address 'memaddr'. Returns the number
208 of bytes used by that instruction. */
210 print_insn (memaddr
, info
, arch
)
212 struct disassemble_info
*info
;
220 const struct m68hc11_opcode
*opcode
;
222 /* Get first byte. Only one at a time because we don't know the
224 status
= read_memory (memaddr
, buffer
, 1, info
);
234 /* Look for page2,3,4 opcodes. */
235 if (code
== M6811_OPCODE_PAGE2
)
238 format
= M6811_OP_PAGE2
;
240 else if (code
== M6811_OPCODE_PAGE3
&& arch
== cpu6811
)
243 format
= M6811_OP_PAGE3
;
245 else if (code
== M6811_OPCODE_PAGE4
&& arch
== cpu6811
)
248 format
= M6811_OP_PAGE4
;
251 /* We are in page2,3,4; get the real opcode. */
254 status
= read_memory (memaddr
+ pos
, &buffer
[1], 1, info
);
263 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
264 in page 1. There is no operand to print. We read the second byte
265 only when we have a possible match. */
266 if ((arch
& cpu6812
) && format
== 0)
270 /* Walk the alias table to find a code1+code2 match. */
271 for (i
= 0; i
< m68hc12_num_alias
; i
++)
273 if (m68hc12_alias
[i
].code1
== code
)
277 status
= read_memory (memaddr
+ pos
+ 1,
278 &buffer
[1], 1, info
);
284 if (m68hc12_alias
[i
].code2
== (unsigned char) buffer
[1])
286 (*info
->fprintf_func
) (info
->stream
, "%s",
287 m68hc12_alias
[i
].name
);
296 /* Scan the opcode table until we find the opcode
297 with the corresponding page. */
298 opcode
= m68hc11_opcodes
;
299 for (i
= 0; i
< m68hc11_num_opcodes
; i
++, opcode
++)
303 if ((opcode
->arch
& arch
) == 0)
305 if (opcode
->opcode
!= code
)
307 if ((opcode
->format
& OP_PAGE_MASK
) != format
)
310 if (opcode
->format
& M6812_OP_REG
)
315 if (opcode
->format
& M6811_OP_JUMP_REL
)
320 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
325 for (j
= 0; i
+ j
< m68hc11_num_opcodes
; j
++)
327 if ((opcode
[j
].arch
& arch
) == 0)
329 if (opcode
[j
].opcode
!= code
)
333 if (!(opcode
[j
].format
& M6811_OP_JUMP_REL
))
336 if ((opcode
[j
].format
& M6812_OP_IBCC_MARKER
)
337 && (buffer
[0] & 0xc0) != 0x80)
339 if ((opcode
[j
].format
& M6812_OP_TBCC_MARKER
)
340 && (buffer
[0] & 0xc0) != 0x40)
342 if ((opcode
[j
].format
& M6812_OP_DBCC_MARKER
)
343 && (buffer
[0] & 0xc0) != 0)
345 if ((opcode
[j
].format
& M6812_OP_EQ_MARKER
)
346 && (buffer
[0] & 0x20) == 0)
348 if (!(opcode
[j
].format
& M6812_OP_EQ_MARKER
)
349 && (buffer
[0] & 0x20) != 0)
353 if (opcode
[j
].format
& M6812_OP_EXG_MARKER
&& buffer
[0] & 0x80)
355 if ((opcode
[j
].format
& M6812_OP_SEX_MARKER
)
356 && (((buffer
[0] & 0x07) >= 3 && (buffer
[0] & 7) <= 7))
357 && ((buffer
[0] & 0x0f0) <= 0x20))
359 if (opcode
[j
].format
& M6812_OP_TFR_MARKER
360 && !(buffer
[0] & 0x80))
363 if (i
+ j
< m68hc11_num_opcodes
)
367 /* We have found the opcode. Extract the operand and print it. */
368 (*info
->fprintf_func
) (info
->stream
, "%s", opcode
->name
);
370 format
= opcode
->format
;
371 if (format
& (M6811_OP_MASK
| M6811_OP_BITMASK
372 | M6811_OP_JUMP_REL
| M6812_OP_JUMP_REL16
))
374 (*info
->fprintf_func
) (info
->stream
, "\t");
377 /* The movb and movw must be handled in a special way...
378 The source constant 'ii' is not always at the same place.
379 This is the same for the destination for the post-indexed byte.
380 The 'offset' is used to do the appropriate correction.
383 for constant for destination
384 movb 18 OB ii hh ll 0 0
386 18 0C hh ll hh ll 0 0
391 movw 18 03 jj kk hh ll 0 0
393 18 04 hh ll hh ll 0 0
398 After the source operand is read, the position 'pos' is incremented
399 this explains the negative offset for destination.
401 movb/movw above are the only instructions with this matching
403 offset
= ((format
& M6812_OP_IDX_P2
)
404 && (format
& (M6811_OP_IMM8
| M6811_OP_IMM16
|
407 /* Operand with one more byte: - immediate, offset,
408 direct-low address. */
410 (M6811_OP_IMM8
| M6811_OP_IX
| M6811_OP_IY
| M6811_OP_DIRECT
))
412 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
420 /* This movb/movw is special (see above). */
423 if (format
& M6811_OP_IMM8
)
425 (*info
->fprintf_func
) (info
->stream
, "#%d", (int) buffer
[0]);
426 format
&= ~M6811_OP_IMM8
;
428 else if (format
& M6811_OP_IX
)
430 /* Offsets are in range 0..255, print them unsigned. */
431 (*info
->fprintf_func
) (info
->stream
, "%u,x", buffer
[0] & 0x0FF);
432 format
&= ~M6811_OP_IX
;
434 else if (format
& M6811_OP_IY
)
436 (*info
->fprintf_func
) (info
->stream
, "%u,y", buffer
[0] & 0x0FF);
437 format
&= ~M6811_OP_IY
;
439 else if (format
& M6811_OP_DIRECT
)
441 (*info
->fprintf_func
) (info
->stream
, "*");
442 (*info
->print_address_func
) (buffer
[0] & 0x0FF, info
);
443 format
&= ~M6811_OP_DIRECT
;
447 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
448 /* Analyze the 68HC12 indexed byte. */
449 if (format
& M6812_INDEXED_FLAGS
)
453 status
= print_indexed_operand (memaddr
+ pos
, info
, &indirect
, 0);
460 /* The indirect addressing mode of the call instruction does
461 not need the page code. */
462 if ((format
& M6812_OP_PAGE
) && indirect
)
463 format
&= ~M6812_OP_PAGE
;
466 /* 68HC12 dbcc/ibcc/tbcc operands. */
467 if ((format
& M6812_OP_REG
) && (format
& M6811_OP_JUMP_REL
))
469 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
474 (*info
->fprintf_func
) (info
->stream
, "%s,",
475 reg_src_table
[buffer
[0] & 0x07]);
476 sval
= buffer
[1] & 0x0ff;
477 if (buffer
[0] & 0x10)
481 (*info
->print_address_func
) (memaddr
+ pos
+ sval
, info
);
482 format
&= ~(M6812_OP_REG
| M6811_OP_JUMP_REL
);
484 else if (format
& (M6812_OP_REG
| M6812_OP_REG_2
))
486 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
493 (*info
->fprintf_func
) (info
->stream
, "%s,%s",
494 reg_src_table
[(buffer
[0] >> 4) & 7],
495 reg_dst_table
[(buffer
[0] & 7)]);
498 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
499 and in that order. The brset/brclr insn have a bitmask and then
500 a relative branch offset. */
501 if (format
& M6811_OP_BITMASK
)
503 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
509 (*info
->fprintf_func
) (info
->stream
, " #$%02x%s",
511 (format
& M6811_OP_JUMP_REL
? " " : ""));
512 format
&= ~M6811_OP_BITMASK
;
514 if (format
& M6811_OP_JUMP_REL
)
518 status
= read_memory (memaddr
+ pos
, &buffer
[0], 1, info
);
525 val
= (buffer
[0] & 0x80) ? buffer
[0] | 0xFFFFFF00 : buffer
[0];
526 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
527 format
&= ~M6811_OP_JUMP_REL
;
529 else if (format
& M6812_OP_JUMP_REL16
)
533 status
= read_memory (memaddr
+ pos
, &buffer
[0], 2, info
);
540 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
544 (*info
->print_address_func
) (memaddr
+ pos
+ val
, info
);
545 format
&= ~M6812_OP_JUMP_REL16
;
547 if (format
& (M6811_OP_IMM16
| M6811_OP_IND16
))
553 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
558 if (format
& M6812_OP_IDX_P2
)
564 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
567 if (format
& M6812_OP_PAGE
)
569 status
= read_memory (memaddr
+ pos
+ offset
, buffer
, 1, info
);
573 page
= (unsigned) buffer
[0];
574 if (addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
575 addr
= ((val
- M68HC12_BANK_BASE
)
576 | (page
<< M68HC12_BANK_SHIFT
))
579 else if ((arch
& cpu6812
)
580 && addr
>= M68HC12_BANK_BASE
&& addr
< 0x0c000)
585 if (memaddr
>= M68HC12_BANK_VIRT
)
586 cur_page
= ((memaddr
- M68HC12_BANK_VIRT
)
587 >> M68HC12_BANK_SHIFT
);
591 vaddr
= ((addr
- M68HC12_BANK_BASE
)
592 + (cur_page
<< M68HC12_BANK_SHIFT
))
594 if (!info
->symbol_at_address_func (addr
, info
)
595 && info
->symbol_at_address_func (vaddr
, info
))
598 if (format
& M6811_OP_IMM16
)
600 format
&= ~M6811_OP_IMM16
;
601 (*info
->fprintf_func
) (info
->stream
, "#");
604 format
&= ~M6811_OP_IND16
;
606 (*info
->print_address_func
) (addr
, info
);
607 if (format
& M6812_OP_PAGE
)
609 (* info
->fprintf_func
) (info
->stream
, " {");
610 (* info
->print_address_func
) (val
, info
);
611 (* info
->fprintf_func
) (info
->stream
, ", %d}", page
);
612 format
&= ~M6812_OP_PAGE
;
617 if (format
& M6812_OP_IDX_P2
)
619 (*info
->fprintf_func
) (info
->stream
, ", ");
620 status
= print_indexed_operand (memaddr
+ pos
+ offset
, info
, 0, 1);
626 if (format
& M6812_OP_IND16_P2
)
630 (*info
->fprintf_func
) (info
->stream
, ", ");
632 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 2, info
);
639 val
= ((buffer
[0] << 8) | (buffer
[1] & 0x0FF));
641 (*info
->print_address_func
) (val
, info
);
644 if (format
& M6812_OP_PAGE
)
648 status
= read_memory (memaddr
+ pos
+ offset
, &buffer
[0], 1, info
);
655 val
= buffer
[0] & 0x0ff;
656 (*info
->fprintf_func
) (info
->stream
, ", %d", val
);
660 /* Consistency check. 'format' must be 0, so that we have handled
661 all formats; and the computed size of the insn must match the
662 opcode table content. */
663 if (format
& ~(M6811_OP_PAGE4
| M6811_OP_PAGE3
| M6811_OP_PAGE2
))
665 (*info
->fprintf_func
) (info
->stream
, "; Error, format: %x", format
);
667 if (pos
!= opcode
->size
)
669 (*info
->fprintf_func
) (info
->stream
, "; Error, size: %d expect %d",
676 /* Opcode not recognized. */
677 if (format
== M6811_OP_PAGE2
&& arch
& cpu6812
678 && ((code
>= 0x30 && code
<= 0x39) || (code
>= 0x40 && code
<= 0xff)))
679 (*info
->fprintf_func
) (info
->stream
, "trap\t#%d", code
& 0x0ff);
681 else if (format
== M6811_OP_PAGE2
)
682 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
683 M6811_OPCODE_PAGE2
, code
);
684 else if (format
== M6811_OP_PAGE3
)
685 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
686 M6811_OPCODE_PAGE3
, code
);
687 else if (format
== M6811_OP_PAGE4
)
688 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x, 0x%02x",
689 M6811_OPCODE_PAGE4
, code
);
691 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02x", code
);
696 /* Disassemble one instruction at address 'memaddr'. Returns the number
697 of bytes used by that instruction. */
699 print_insn_m68hc11 (memaddr
, info
)
701 struct disassemble_info
*info
;
703 return print_insn (memaddr
, info
, cpu6811
);
707 print_insn_m68hc12 (memaddr
, info
)
709 struct disassemble_info
*info
;
711 return print_insn (memaddr
, info
, cpu6812
);