* bfd.c (struct bfd_preserve): New.
[binutils.git] / opcodes / m68hc11-dis.c
blobbcd3ee5103fd0888593f610f53e66d6b8b64284c
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. */
19 #include <stdio.h>
21 #include "ansidecl.h"
22 #include "opcode/m68hc11.h"
23 #include "dis-asm.h"
25 static const char *const reg_name[] = {
26 "X", "Y", "SP", "PC"
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));
44 static int print_insn
45 PARAMS ((bfd_vma, struct disassemble_info *, int));
47 static int
48 read_memory (memaddr, buffer, size, info)
49 bfd_vma memaddr;
50 bfd_byte *buffer;
51 int size;
52 struct disassemble_info *info;
54 int status;
56 /* Get first byte. Only one at a time because we don't know the
57 size of the insn. */
58 status = (*info->read_memory_func) (memaddr, buffer, size, info);
59 if (status != 0)
61 (*info->memory_error_func) (status, memaddr, info);
62 return -1;
64 return 0;
68 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
69 Returns the number of bytes read or -1 if failure. */
70 static int
71 print_indexed_operand (memaddr, info, indirect, mov_insn)
72 bfd_vma memaddr;
73 struct disassemble_info *info;
74 int *indirect;
75 int mov_insn;
77 bfd_byte buffer[4];
78 int reg;
79 int status;
80 short sval;
81 int pos = 1;
83 if (indirect)
84 *indirect = 0;
86 status = read_memory (memaddr, &buffer[0], 1, info);
87 if (status != 0)
89 return status;
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);
97 if (sval & 0x10)
98 sval |= 0xfff0;
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)
106 const char *mode;
108 reg = (buffer[0] >> 6) & 3;
109 sval = (buffer[0] & 0x0f);
110 if (sval & 0x8)
112 sval |= 0xfff0;
113 sval = -sval;
114 mode = "-";
116 else
118 sval = sval + 1;
119 mode = "+";
121 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
122 (int) sval,
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)
130 if (mov_insn)
132 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
133 buffer[0] & 0x0ff);
134 return 0;
136 reg = (buffer[0] >> 3) & 0x03;
137 status = read_memory (memaddr + pos, &buffer[0], 2, info);
138 if (status != 0)
140 return status;
143 pos += 2;
144 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
145 (*info->fprintf_func) (info->stream, "[%u,%s]",
146 sval & 0x0ffff, reg_name[reg]);
147 if (indirect)
148 *indirect = 1;
150 else if ((buffer[0] & 0x4) == 0)
152 if (mov_insn)
154 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
155 buffer[0] & 0x0ff);
156 return 0;
158 reg = (buffer[0] >> 3) & 0x03;
159 status = read_memory (memaddr + pos,
160 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
161 if (status != 0)
163 return status;
165 if (buffer[0] & 2)
167 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
168 sval &= 0x0FFFF;
169 pos += 2;
171 else
173 sval = buffer[1] & 0x00ff;
174 if (buffer[0] & 0x01)
175 sval |= 0xff00;
176 pos++;
178 (*info->fprintf_func) (info->stream, "%d,%s",
179 (int) sval, reg_name[reg]);
181 else
183 reg = (buffer[0] >> 3) & 0x03;
184 switch (buffer[0] & 3)
186 case 0:
187 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
188 break;
189 case 1:
190 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
191 break;
192 case 2:
193 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
194 break;
195 case 3:
196 default:
197 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
198 if (indirect)
199 *indirect = 1;
200 break;
204 return pos;
207 /* Disassemble one instruction at address 'memaddr'. Returns the number
208 of bytes used by that instruction. */
209 static int
210 print_insn (memaddr, info, arch)
211 bfd_vma memaddr;
212 struct disassemble_info *info;
213 int arch;
215 int status;
216 bfd_byte buffer[4];
217 unsigned char code;
218 long format, pos, i;
219 short sval;
220 const struct m68hc11_opcode *opcode;
222 /* Get first byte. Only one at a time because we don't know the
223 size of the insn. */
224 status = read_memory (memaddr, buffer, 1, info);
225 if (status != 0)
227 return status;
230 format = 0;
231 code = buffer[0];
232 pos = 0;
234 /* Look for page2,3,4 opcodes. */
235 if (code == M6811_OPCODE_PAGE2)
237 pos++;
238 format = M6811_OP_PAGE2;
240 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
242 pos++;
243 format = M6811_OP_PAGE3;
245 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
247 pos++;
248 format = M6811_OP_PAGE4;
251 /* We are in page2,3,4; get the real opcode. */
252 if (pos == 1)
254 status = read_memory (memaddr + pos, &buffer[1], 1, info);
255 if (status != 0)
257 return status;
259 code = buffer[1];
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)
268 int must_read = 1;
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)
275 if (must_read)
277 status = read_memory (memaddr + pos + 1,
278 &buffer[1], 1, info);
279 if (status != 0)
280 break;
282 must_read = 1;
284 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
286 (*info->fprintf_func) (info->stream, "%s",
287 m68hc12_alias[i].name);
288 return 2;
294 pos++;
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++)
301 int offset;
303 if ((opcode->arch & arch) == 0)
304 continue;
305 if (opcode->opcode != code)
306 continue;
307 if ((opcode->format & OP_PAGE_MASK) != format)
308 continue;
310 if (opcode->format & M6812_OP_REG)
312 int j;
313 int is_jump;
315 if (opcode->format & M6811_OP_JUMP_REL)
316 is_jump = 1;
317 else
318 is_jump = 0;
320 status = read_memory (memaddr + pos, &buffer[0], 1, info);
321 if (status != 0)
323 return status;
325 for (j = 0; i + j < m68hc11_num_opcodes; j++)
327 if ((opcode[j].arch & arch) == 0)
328 continue;
329 if (opcode[j].opcode != code)
330 continue;
331 if (is_jump)
333 if (!(opcode[j].format & M6811_OP_JUMP_REL))
334 continue;
336 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
337 && (buffer[0] & 0xc0) != 0x80)
338 continue;
339 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
340 && (buffer[0] & 0xc0) != 0x40)
341 continue;
342 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
343 && (buffer[0] & 0xc0) != 0)
344 continue;
345 if ((opcode[j].format & M6812_OP_EQ_MARKER)
346 && (buffer[0] & 0x20) == 0)
347 break;
348 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
349 && (buffer[0] & 0x20) != 0)
350 break;
351 continue;
353 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
354 break;
355 if ((opcode[j].format & M6812_OP_SEX_MARKER)
356 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
357 && ((buffer[0] & 0x0f0) <= 0x20))
358 break;
359 if (opcode[j].format & M6812_OP_TFR_MARKER
360 && !(buffer[0] & 0x80))
361 break;
363 if (i + j < m68hc11_num_opcodes)
364 opcode = &opcode[j];
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.
382 offset offset
383 for constant for destination
384 movb 18 OB ii hh ll 0 0
385 18 08 xb ii 1 -1
386 18 0C hh ll hh ll 0 0
387 18 09 xb hh ll 1 -1
388 18 0D xb hh ll 0 0
389 18 0A xb xb 0 0
391 movw 18 03 jj kk hh ll 0 0
392 18 00 xb jj kk 1 -1
393 18 04 hh ll hh ll 0 0
394 18 01 xb hh ll 1 -1
395 18 05 xb hh ll 0 0
396 18 02 xb xb 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
402 format. */
403 offset = ((format & M6812_OP_IDX_P2)
404 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 |
405 M6811_OP_IND16)));
407 /* Operand with one more byte: - immediate, offset,
408 direct-low address. */
409 if (format &
410 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
412 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
413 if (status != 0)
415 return status;
418 pos++;
420 /* This movb/movw is special (see above). */
421 offset = -offset;
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)
451 int indirect;
453 status = print_indexed_operand (memaddr + pos, info, &indirect, 0);
454 if (status < 0)
456 return status;
458 pos += status;
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);
470 if (status != 0)
472 return status;
474 (*info->fprintf_func) (info->stream, "%s,",
475 reg_src_table[buffer[0] & 0x07]);
476 sval = buffer[1] & 0x0ff;
477 if (buffer[0] & 0x10)
478 sval |= 0xff00;
480 pos += 2;
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);
487 if (status != 0)
489 return status;
492 pos++;
493 (*info->fprintf_func) (info->stream, "%s,%s",
494 reg_src_table[(buffer[0] >> 4) & 7],
495 reg_dst_table[(buffer[0] & 7)]);
498 if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
500 int val;
501 bfd_vma addr;
502 unsigned page = 0;
504 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
505 if (status != 0)
507 return status;
509 if (format & M6812_OP_IDX_P2)
510 offset = -2;
511 else
512 offset = 0;
513 pos += 2;
515 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
516 val &= 0x0FFFF;
517 addr = val;
518 if (format & M6812_OP_PAGE)
520 status = read_memory (memaddr + pos + offset, buffer, 1, info);
521 if (status != 0)
522 return status;
524 page = (unsigned) buffer[0];
525 if (addr >= M68HC12_BANK_BASE && addr < 0x0c000)
526 addr = ((val - M68HC12_BANK_BASE)
527 | (page << M68HC12_BANK_SHIFT))
528 + M68HC12_BANK_VIRT;
530 else if ((arch & cpu6812)
531 && addr >= M68HC12_BANK_BASE && addr < 0x0c000)
533 int cur_page;
534 bfd_vma vaddr;
536 if (memaddr >= M68HC12_BANK_VIRT)
537 cur_page = ((memaddr - M68HC12_BANK_VIRT)
538 >> M68HC12_BANK_SHIFT);
539 else
540 cur_page = 0;
542 vaddr = ((addr - M68HC12_BANK_BASE)
543 + (cur_page << M68HC12_BANK_SHIFT))
544 + M68HC12_BANK_VIRT;
545 if (!info->symbol_at_address_func (addr, info)
546 && info->symbol_at_address_func (vaddr, info))
547 addr = vaddr;
549 if (format & M6811_OP_IMM16)
551 format &= ~M6811_OP_IMM16;
552 (*info->fprintf_func) (info->stream, "#");
554 else
555 format &= ~M6811_OP_IND16;
557 (*info->print_address_func) (addr, info);
558 if (format & M6812_OP_PAGE)
560 (* info->fprintf_func) (info->stream, " {");
561 (* info->print_address_func) (val, info);
562 (* info->fprintf_func) (info->stream, ", %d}", page);
563 format &= ~M6812_OP_PAGE;
564 pos += 1;
568 if (format & M6812_OP_IDX_P2)
570 (*info->fprintf_func) (info->stream, ", ");
571 status = print_indexed_operand (memaddr + pos + offset, info, 0, 1);
572 if (status < 0)
573 return status;
574 pos += status;
577 if (format & M6812_OP_IND16_P2)
579 int val;
581 (*info->fprintf_func) (info->stream, ", ");
583 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
584 if (status != 0)
586 return status;
588 pos += 2;
590 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
591 val &= 0x0FFFF;
592 (*info->print_address_func) (val, info);
595 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
596 and in that order. The brset/brclr insn have a bitmask and then
597 a relative branch offset. */
598 if (format & M6811_OP_BITMASK)
600 status = read_memory (memaddr + pos, &buffer[0], 1, info);
601 if (status != 0)
603 return status;
605 pos++;
606 (*info->fprintf_func) (info->stream, " #$%02x%s",
607 buffer[0] & 0x0FF,
608 (format & M6811_OP_JUMP_REL ? " " : ""));
609 format &= ~M6811_OP_BITMASK;
611 if (format & M6811_OP_JUMP_REL)
613 int val;
615 status = read_memory (memaddr + pos, &buffer[0], 1, info);
616 if (status != 0)
618 return status;
621 pos++;
622 val = (buffer[0] & 0x80) ? buffer[0] | 0xFFFFFF00 : buffer[0];
623 (*info->print_address_func) (memaddr + pos + val, info);
624 format &= ~M6811_OP_JUMP_REL;
626 else if (format & M6812_OP_JUMP_REL16)
628 int val;
630 status = read_memory (memaddr + pos, &buffer[0], 2, info);
631 if (status != 0)
633 return status;
636 pos += 2;
637 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
638 if (val & 0x8000)
639 val |= 0xffff0000;
641 (*info->print_address_func) (memaddr + pos + val, info);
642 format &= ~M6812_OP_JUMP_REL16;
645 if (format & M6812_OP_PAGE)
647 int val;
649 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
650 if (status != 0)
652 return status;
654 pos += 1;
656 val = buffer[0] & 0x0ff;
657 (*info->fprintf_func) (info->stream, ", %d", val);
660 #ifdef DEBUG
661 /* Consistency check. 'format' must be 0, so that we have handled
662 all formats; and the computed size of the insn must match the
663 opcode table content. */
664 if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
666 (*info->fprintf_func) (info->stream, "; Error, format: %x", format);
668 if (pos != opcode->size)
670 (*info->fprintf_func) (info->stream, "; Error, size: %d expect %d",
671 pos, opcode->size);
673 #endif
674 return pos;
677 /* Opcode not recognized. */
678 if (format == M6811_OP_PAGE2 && arch & cpu6812
679 && ((code >= 0x30 && code <= 0x39) || (code >= 0x40 && code <= 0xff)))
680 (*info->fprintf_func) (info->stream, "trap\t#%d", code & 0x0ff);
682 else if (format == M6811_OP_PAGE2)
683 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
684 M6811_OPCODE_PAGE2, code);
685 else if (format == M6811_OP_PAGE3)
686 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
687 M6811_OPCODE_PAGE3, code);
688 else if (format == M6811_OP_PAGE4)
689 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
690 M6811_OPCODE_PAGE4, code);
691 else
692 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
694 return pos;
697 /* Disassemble one instruction at address 'memaddr'. Returns the number
698 of bytes used by that instruction. */
700 print_insn_m68hc11 (memaddr, info)
701 bfd_vma memaddr;
702 struct disassemble_info *info;
704 return print_insn (memaddr, info, cpu6811);
708 print_insn_m68hc12 (memaddr, info)
709 bfd_vma memaddr;
710 struct disassemble_info *info;
712 return print_insn (memaddr, info, cpu6812);