1 /* m68hc11-dis.c -- Motorola 68HC11 & 68HC12 disassembly
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@worldnet.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)
40 read_memory (memaddr, buffer, size, info)
44 struct disassemble_info *info;
48 /* Get first byte. Only one at a time because we don't know the
50 status = (*info->read_memory_func) (memaddr, buffer, size, info);
53 (*info->memory_error_func) (status, memaddr, info);
60 /* Read the 68HC12 indexed operand byte and print the corresponding mode.
61 Returns the number of bytes read or -1 if failure. */
63 print_indexed_operand (memaddr, info, mov_insn)
65 struct disassemble_info *info;
74 status = read_memory (memaddr, &buffer[0], 1, info);
80 /* n,r with 5-bits signed constant. */
81 if ((buffer[0] & 0x20) == 0)
83 reg = (buffer[0] >> 6) & 3;
84 sval = (buffer[0] & 0x1f);
87 (*info->fprintf_func) (info->stream, "%d,%s",
88 (int) sval, reg_name[reg]);
91 /* Auto pre/post increment/decrement. */
92 else if ((buffer[0] & 0xc0) != 0xc0)
96 reg = (buffer[0] >> 6) & 3;
97 sval = (buffer[0] & 0x0f);
109 (*info->fprintf_func) (info->stream, "%d,%s%s%s",
111 (buffer[0] & 0x10 ? "" : mode),
112 reg_name[reg], (buffer[0] & 0x10 ? mode : ""));
115 /* [n,r] 16-bits offset indexed indirect. */
116 else if ((buffer[0] & 0x07) == 3)
120 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
124 reg = (buffer[0] >> 3) & 0x03;
125 status = read_memory (memaddr + pos, &buffer[0], 2, info);
132 sval = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
133 (*info->fprintf_func) (info->stream, "[%u,%s]",
134 sval & 0x0ffff, reg_name[reg]);
136 else if ((buffer[0] & 0x4) == 0)
140 (*info->fprintf_func) (info->stream, "<invalid op: 0x%x>",
144 reg = (buffer[0] >> 3) & 0x03;
145 status = read_memory (memaddr + pos,
146 &buffer[1], (buffer[0] & 0x2 ? 2 : 1), info);
153 sval = ((buffer[1] << 8) | (buffer[2] & 0x0FF));
159 sval = buffer[1] & 0x00ff;
160 if (buffer[0] & 0x01)
164 (*info->fprintf_func) (info->stream, "%d,%s",
165 (int) sval, reg_name[reg]);
169 reg = (buffer[0] >> 3) & 0x03;
170 switch (buffer[0] & 3)
173 (*info->fprintf_func) (info->stream, "A,%s", reg_name[reg]);
176 (*info->fprintf_func) (info->stream, "B,%s", reg_name[reg]);
179 (*info->fprintf_func) (info->stream, "D,%s", reg_name[reg]);
183 (*info->fprintf_func) (info->stream, "[D,%s]", reg_name[reg]);
191 /* Disassemble one instruction at address 'memaddr'. Returns the number
192 of bytes used by that instruction. */
194 print_insn (memaddr, info, arch)
196 struct disassemble_info *info;
204 const struct m68hc11_opcode *opcode;
206 /* Get first byte. Only one at a time because we don't know the
208 status = read_memory (memaddr, buffer, 1, info);
218 /* Look for page2,3,4 opcodes. */
219 if (code == M6811_OPCODE_PAGE2)
222 format = M6811_OP_PAGE2;
224 else if (code == M6811_OPCODE_PAGE3 && arch == cpu6811)
227 format = M6811_OP_PAGE3;
229 else if (code == M6811_OPCODE_PAGE4 && arch == cpu6811)
232 format = M6811_OP_PAGE4;
235 /* We are in page2,3,4; get the real opcode. */
238 status = read_memory (memaddr + pos, &buffer[1], 1, info);
247 /* Look first for a 68HC12 alias. All of them are 2-bytes long and
248 in page 1. There is no operand to print. We read the second byte
249 only when we have a possible match. */
250 if ((arch & cpu6812) && format == 0)
254 /* Walk the alias table to find a code1+code2 match. */
255 for (i = 0; i < m68hc12_num_alias; i++)
257 if (m68hc12_alias[i].code1 == code)
261 status = read_memory (memaddr + pos + 1,
262 &buffer[1], 1, info);
268 if (m68hc12_alias[i].code2 == (unsigned char) buffer[1])
270 (*info->fprintf_func) (info->stream, "%s",
271 m68hc12_alias[i].name);
280 /* Scan the opcode table until we find the opcode
281 with the corresponding page. */
282 opcode = m68hc11_opcodes;
283 for (i = 0; i < m68hc11_num_opcodes; i++, opcode++)
287 if ((opcode->arch & arch) == 0)
289 if (opcode->opcode != code)
291 if ((opcode->format & OP_PAGE_MASK) != format)
294 if (opcode->format & M6812_OP_REG)
299 if (opcode->format & M6811_OP_JUMP_REL)
304 status = read_memory (memaddr + pos, &buffer[0], 1, info);
309 for (j = 0; i + j < m68hc11_num_opcodes; j++)
311 if ((opcode[j].arch & arch) == 0)
313 if (opcode[j].opcode != code)
317 if (!(opcode[j].format & M6811_OP_JUMP_REL))
320 if ((opcode[j].format & M6812_OP_IBCC_MARKER)
321 && (buffer[0] & 0xc0) != 0x80)
323 if ((opcode[j].format & M6812_OP_TBCC_MARKER)
324 && (buffer[0] & 0xc0) != 0x40)
326 if ((opcode[j].format & M6812_OP_DBCC_MARKER)
327 && (buffer[0] & 0xc0) != 0)
329 if ((opcode[j].format & M6812_OP_EQ_MARKER)
330 && (buffer[0] & 0x20) == 0)
332 if (!(opcode[j].format & M6812_OP_EQ_MARKER)
333 && (buffer[0] & 0x20) != 0)
337 if (opcode[j].format & M6812_OP_EXG_MARKER && buffer[0] & 0x80)
339 if ((opcode[j].format & M6812_OP_SEX_MARKER)
340 && (((buffer[0] & 0x07) >= 3 && (buffer[0] & 7) <= 7))
341 && ((buffer[0] & 0x0f0) <= 0x20))
343 if (opcode[j].format & M6812_OP_TFR_MARKER
344 && !(buffer[0] & 0x80))
347 if (i + j < m68hc11_num_opcodes)
351 /* We have found the opcode. Extract the operand and print it. */
352 (*info->fprintf_func) (info->stream, "%s", opcode->name);
354 format = opcode->format;
355 if (format & (M6811_OP_MASK | M6811_OP_BITMASK
356 | M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
358 (*info->fprintf_func) (info->stream, "\t");
361 /* The movb and movw must be handled in a special way... */
363 if (format & (M6812_OP_IDX_P2 | M6812_OP_IND16_P2))
365 if ((format & M6812_OP_IDX_P2)
366 && (format & (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_IND16)))
370 /* Operand with one more byte: - immediate, offset,
371 direct-low address. */
373 (M6811_OP_IMM8 | M6811_OP_IX | M6811_OP_IY | M6811_OP_DIRECT))
375 status = read_memory (memaddr + pos + offset, &buffer[0], 1, info);
383 if (format & M6811_OP_IMM8)
385 (*info->fprintf_func) (info->stream, "#%d", (int) buffer[0]);
386 format &= ~M6811_OP_IMM8;
388 else if (format & M6811_OP_IX)
390 /* Offsets are in range 0..255, print them unsigned. */
391 (*info->fprintf_func) (info->stream, "%u,x", buffer[0] & 0x0FF);
392 format &= ~M6811_OP_IX;
394 else if (format & M6811_OP_IY)
396 (*info->fprintf_func) (info->stream, "%u,y", buffer[0] & 0x0FF);
397 format &= ~M6811_OP_IY;
399 else if (format & M6811_OP_DIRECT)
401 (*info->fprintf_func) (info->stream, "*");
402 (*info->print_address_func) (buffer[0] & 0x0FF, info);
403 format &= ~M6811_OP_DIRECT;
407 #define M6812_INDEXED_FLAGS (M6812_OP_IDX|M6812_OP_IDX_1|M6812_OP_IDX_2)
408 /* Analyze the 68HC12 indexed byte. */
409 if (format & M6812_INDEXED_FLAGS)
411 status = print_indexed_operand (memaddr + pos, info, 0);
419 /* 68HC12 dbcc/ibcc/tbcc operands. */
420 if ((format & M6812_OP_REG) && (format & M6811_OP_JUMP_REL))
422 status = read_memory (memaddr + pos, &buffer[0], 2, info);
427 (*info->fprintf_func) (info->stream, "%s,",
428 reg_src_table[buffer[0] & 0x07]);
429 sval = buffer[1] & 0x0ff;
430 if (buffer[0] & 0x10)
434 (*info->print_address_func) (memaddr + pos + sval, info);
435 format &= ~(M6812_OP_REG | M6811_OP_JUMP_REL);
437 else if (format & (M6812_OP_REG | M6812_OP_REG_2))
439 status = read_memory (memaddr + pos, &buffer[0], 1, info);
446 (*info->fprintf_func) (info->stream, "%s,%s",
447 reg_src_table[(buffer[0] >> 4) & 7],
448 reg_dst_table[(buffer[0] & 7)]);
451 /* M6811_OP_BITMASK and M6811_OP_JUMP_REL must be treated separately
452 and in that order. The brset/brclr insn have a bitmask and then
453 a relative branch offset. */
454 if (format & M6811_OP_BITMASK)
456 status = read_memory (memaddr + pos, &buffer[0], 1, info);
462 (*info->fprintf_func) (info->stream, " #$%02x%s",
464 (format & M6811_OP_JUMP_REL ? " " : ""));
465 format &= ~M6811_OP_BITMASK;
467 if (format & M6811_OP_JUMP_REL)
471 status = read_memory (memaddr + pos, &buffer[0], 1, info);
478 val = (buffer[0] & 0x80) ? buffer[0] | 0xFFFFFF00 : buffer[0];
479 (*info->print_address_func) (memaddr + pos + val, info);
480 format &= ~M6811_OP_JUMP_REL;
482 else if (format & M6812_OP_JUMP_REL16)
486 status = read_memory (memaddr + pos, &buffer[0], 2, info);
493 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
497 (*info->print_address_func) (memaddr + pos + val, info);
498 format &= ~M6812_OP_JUMP_REL16;
500 if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
504 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
509 if (format & M6812_OP_IDX_P2)
515 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
517 if (format & M6811_OP_IMM16)
519 format &= ~M6811_OP_IMM16;
520 (*info->fprintf_func) (info->stream, "#");
523 format &= ~M6811_OP_IND16;
525 (*info->print_address_func) (val, info);
528 if (format & M6812_OP_IDX_P2)
530 (*info->fprintf_func) (info->stream, ", ");
531 status = print_indexed_operand (memaddr + pos + offset, info, 1);
537 if (format & M6812_OP_IND16_P2)
541 (*info->fprintf_func) (info->stream, ", ");
543 status = read_memory (memaddr + pos + offset, &buffer[0], 2, info);
550 val = ((buffer[0] << 8) | (buffer[1] & 0x0FF));
552 (*info->print_address_func) (val, info);
556 /* Consistency check. 'format' must be 0, so that we have handled
557 all formats; and the computed size of the insn must match the
558 opcode table content. */
559 if (format & ~(M6811_OP_PAGE4 | M6811_OP_PAGE3 | M6811_OP_PAGE2))
561 (*info->fprintf_func) (info->stream, "; Error, format: %x", format);
563 if (pos != opcode->size)
565 (*info->fprintf_func) (info->stream, "; Error, size: %d expect %d",
572 /* Opcode not recognized. */
573 if (format == M6811_OP_PAGE2 && arch & cpu6812
574 && ((code >= 0x30 && code <= 0x39) || (code >= 0x40 && code <= 0xff)))
575 (*info->fprintf_func) (info->stream, "trap\t#%d", code & 0x0ff);
577 else if (format == M6811_OP_PAGE2)
578 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
579 M6811_OPCODE_PAGE2, code);
580 else if (format == M6811_OP_PAGE3)
581 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
582 M6811_OPCODE_PAGE3, code);
583 else if (format == M6811_OP_PAGE4)
584 (*info->fprintf_func) (info->stream, ".byte\t0x%02x, 0x%02x",
585 M6811_OPCODE_PAGE4, code);
587 (*info->fprintf_func) (info->stream, ".byte\t0x%02x", code);
592 /* Disassemble one instruction at address 'memaddr'. Returns the number
593 of bytes used by that instruction. */
595 print_insn_m68hc11 (memaddr, info)
597 struct disassemble_info *info;
599 return print_insn (memaddr, info, cpu6811);
603 print_insn_m68hc12 (memaddr, info)
605 struct disassemble_info *info;
607 return print_insn (memaddr, info, cpu6812);