Mon Jan 20 12:48:57 1997 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[deliverable/binutils-gdb.git] / opcodes / mn10200-dis.c
CommitLineData
ae1b99e4 1/* Disassemble MN10200 instructions.
1b8a127f 2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
e7c50cef
JL
3
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18
19#include <stdio.h>
20
21#include "ansidecl.h"
ae1b99e4 22#include "opcode/mn10200.h"
e7c50cef
JL
23#include "dis-asm.h"
24
781766e7
JL
25static void disassemble PARAMS ((bfd_vma, struct disassemble_info *,
26 unsigned long insn, unsigned long,
27 unsigned int));
e7c50cef
JL
28
29int
ae1b99e4 30print_insn_mn10200 (memaddr, info)
e7c50cef
JL
31 bfd_vma memaddr;
32 struct disassemble_info *info;
33{
781766e7
JL
34 int status;
35 bfd_byte buffer[4];
c6b62ad1 36 unsigned long insn, extension;
781766e7
JL
37 unsigned int consume;
38
39 /* First figure out how big the opcode is. */
40 status = (*info->read_memory_func) (memaddr, buffer, 1, info);
41 if (status != 0)
42 {
43 (*info->memory_error_func) (status, memaddr, info);
44 return -1;
45 }
46 insn = *(unsigned char *) buffer;
47
48 /* These are one byte insns. */
c6b62ad1 49 if ((insn & 0xf0) == 0x00
781766e7 50 || (insn & 0xf0) == 0x10
c6b62ad1
JL
51 || (insn & 0xf0) == 0x20
52 || (insn & 0xf0) == 0x30
781766e7
JL
53 || ((insn & 0xf0) == 0x80
54 && (insn & 0x0c) >> 2 != (insn & 0x03))
c6b62ad1
JL
55 || (insn & 0xf0) == 0x90
56 || (insn & 0xf0) == 0xa0
57 || (insn & 0xf0) == 0xb0
58 || (insn & 0xff) == 0xeb
59 || (insn & 0xff) == 0xf6
60 || (insn & 0xff) == 0xfe)
781766e7
JL
61 {
62 extension = 0;
63 consume = 1;
64 }
65
66 /* These are two byte insns. */
c6b62ad1
JL
67 else if ((insn & 0xf0) == 0x40
68 || (insn & 0xf0) == 0x50
69 || (insn & 0xf0) == 0x60
70 || (insn & 0xf0) == 0x70
71 || (insn & 0xf0) == 0x80
72 || (insn & 0xfc) == 0xd0
73 || (insn & 0xfc) == 0xd4
74 || (insn & 0xfc) == 0xd8
75 || (insn & 0xfc) == 0xe0
76 || (insn & 0xfc) == 0xe4
77 || (insn & 0xff) == 0xe8
78 || (insn & 0xff) == 0xe9
79 || (insn & 0xff) == 0xea
781766e7
JL
80 || (insn & 0xff) == 0xf0
81 || (insn & 0xff) == 0xf1
82 || (insn & 0xff) == 0xf2
c6b62ad1 83 || (insn & 0xff) == 0xf3)
781766e7
JL
84 {
85 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
86 if (status != 0)
87 {
88 (*info->memory_error_func) (status, memaddr, info);
89 return -1;
90 }
91 insn = bfd_getb16 (buffer);
781766e7
JL
92 consume = 2;
93 }
94
c6b62ad1
JL
95 /* These are three byte insns with a 16bit operand in little
96 endian form. */
97 else if ((insn & 0xf0) == 0xc0
98 || (insn & 0xfc) == 0xdc
99 || (insn & 0xfc) == 0xec
100 || (insn & 0xff) == 0xf8
781766e7 101 || (insn & 0xff) == 0xf9
c6b62ad1
JL
102 || (insn & 0xff) == 0xfa
103 || (insn & 0xff) == 0xfb
104 || (insn & 0xff) == 0xfc
105 || (insn & 0xff) == 0xfd)
781766e7 106 {
c6b62ad1 107 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
781766e7
JL
108 if (status != 0)
109 {
110 (*info->memory_error_func) (status, memaddr, info);
111 return -1;
112 }
c6b62ad1
JL
113 insn <<= 16;
114 insn |= bfd_getl16 (buffer);
781766e7
JL
115 extension = 0;
116 consume = 3;
117 }
c6b62ad1
JL
118 /* These are three byte insns too, but we don't have to mess with
119 endianness stuff. */
120 else if ((insn & 0xff) == 0xf5)
781766e7 121 {
c6b62ad1 122 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
781766e7
JL
123 if (status != 0)
124 {
125 (*info->memory_error_func) (status, memaddr, info);
126 return -1;
127 }
c6b62ad1
JL
128 insn <<= 16;
129 insn |= bfd_getb16 (buffer);
781766e7 130 extension = 0;
c6b62ad1 131 consume = 3;
781766e7
JL
132 }
133
c6b62ad1
JL
134 /* These are four byte insns. */
135 else if ((insn & 0xff) == 0xf7)
781766e7 136 {
c6b62ad1 137 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
781766e7
JL
138 if (status != 0)
139 {
140 (*info->memory_error_func) (status, memaddr, info);
141 return -1;
142 }
c6b62ad1
JL
143 insn = bfd_getb16 (buffer);
144 insn <<= 16;
145 status = (*info->read_memory_func) (memaddr + 2, buffer, 2, info);
781766e7
JL
146 if (status != 0)
147 {
c6b62ad1 148 (*info->memory_error_func) (status, memaddr, info);
781766e7
JL
149 return -1;
150 }
c6b62ad1
JL
151 insn |= bfd_getl16 (buffer);
152 extension = 0;
153 consume = 4;
781766e7
JL
154 }
155
c6b62ad1
JL
156 /* These are five byte insns. */
157 else if ((insn & 0xff) == 0xf4)
781766e7 158 {
c6b62ad1 159 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
781766e7
JL
160 if (status != 0)
161 {
162 (*info->memory_error_func) (status, memaddr, info);
163 return -1;
164 }
c6b62ad1
JL
165 insn = bfd_getb16 (buffer);
166 insn <<= 16;
781766e7 167
c6b62ad1 168 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
781766e7
JL
169 if (status != 0)
170 {
c6b62ad1 171 (*info->memory_error_func) (status, memaddr, info);
781766e7
JL
172 return -1;
173 }
c6b62ad1 174 insn |= *(unsigned char *)buffer << 8;
781766e7 175
c6b62ad1 176 status = (*info->read_memory_func) (memaddr + 3, buffer, 1, info);
781766e7
JL
177 if (status != 0)
178 {
179 (*info->memory_error_func) (status, memaddr, info);
180 return -1;
181 }
c6b62ad1 182 insn |= *(unsigned char *)buffer;
781766e7 183
c6b62ad1 184 status = (*info->read_memory_func) (memaddr + 2, buffer, 1, info);
781766e7
JL
185 if (status != 0)
186 {
c6b62ad1 187 (*info->memory_error_func) (status, memaddr, info);
781766e7
JL
188 return -1;
189 }
c6b62ad1
JL
190 extension = *(unsigned char *)buffer;
191 consume = 5;
781766e7 192 }
c6b62ad1
JL
193 else
194 return -1;
781766e7
JL
195
196 disassemble (memaddr, info, insn, extension, consume);
197
198 return consume;
199}
200
201static void
202disassemble (memaddr, info, insn, extension, size)
203 bfd_vma memaddr;
204 struct disassemble_info *info;
205 unsigned long insn;
206 unsigned long extension;
207 unsigned int size;
208{
209 struct mn10200_opcode *op = (struct mn10200_opcode *)mn10200_opcodes;
210 const struct mn10200_operand *operand;
211 int match = 0;
212
213 /* Find the opcode. */
214 while (op->name)
215 {
216 int mysize, extra_shift;
217
c6b62ad1 218 if (op->format == FMT_1)
781766e7 219 mysize = 1;
c6b62ad1
JL
220 else if (op->format == FMT_2
221 || op->format == FMT_4)
781766e7 222 mysize = 2;
c6b62ad1
JL
223 else if (op->format == FMT_3
224 || op->format == FMT_5)
781766e7 225 mysize = 3;
c6b62ad1 226 else if (op->format == FMT_6)
781766e7 227 mysize = 4;
c6b62ad1
JL
228 else if (op->format == FMT_7)
229 mysize = 5;
781766e7 230 else
c6b62ad1 231 abort ();
781766e7 232
c6b62ad1 233 if (op->format == FMT_2 || op->format == FMT_5)
781766e7 234 extra_shift = 8;
c6b62ad1
JL
235 else if (op->format == FMT_3
236 || op->format == FMT_6
237 || op->format == FMT_7)
781766e7
JL
238 extra_shift = 16;
239 else
240 extra_shift = 0;
781766e7
JL
241
242 if ((op->mask & insn) == op->opcode
243 && size == mysize)
244 {
245 const unsigned char *opindex_ptr;
246 unsigned int nocomma;
247 int paren = 0;
248
249 match = 1;
250 (*info->fprintf_func) (info->stream, "%s\t", op->name);
251
252 /* Now print the operands. */
253 for (opindex_ptr = op->operands, nocomma = 1;
254 *opindex_ptr != 0;
255 opindex_ptr++)
256 {
257 unsigned long value;
258
259 operand = &mn10200_operands[*opindex_ptr];
260
c6b62ad1 261 if ((operand->flags & MN10200_OPERAND_EXTENDED) != 0)
781766e7 262 {
c6b62ad1
JL
263 value = (insn & 0xffff) << 8;
264 value |= extension;
781766e7
JL
265 }
266 else
267 {
268 value = ((insn >> (operand->shift))
269 & ((1 << operand->bits) - 1));
270 }
271
272 if ((operand->flags & MN10200_OPERAND_SIGNED) != 0)
273 value = ((long)(value << (32 - operand->bits))
274 >> (32 - operand->bits));
275
276 if (!nocomma
277 && (!paren
278 || ((operand->flags & MN10200_OPERAND_PAREN) == 0)))
279 (*info->fprintf_func) (info->stream, ",");
280
281 nocomma = 0;
282
283 if ((operand->flags & MN10200_OPERAND_DREG) != 0)
284 {
285 value = ((insn >> (operand->shift + extra_shift))
286 & ((1 << operand->bits) - 1));
287 (*info->fprintf_func) (info->stream, "d%d", value);
288 }
289
290 else if ((operand->flags & MN10200_OPERAND_AREG) != 0)
291 {
292 value = ((insn >> (operand->shift + extra_shift))
293 & ((1 << operand->bits) - 1));
294 (*info->fprintf_func) (info->stream, "a%d", value);
295 }
296
297 else if ((operand->flags & MN10200_OPERAND_PSW) != 0)
298 (*info->fprintf_func) (info->stream, "psw");
299
300 else if ((operand->flags & MN10200_OPERAND_MDR) != 0)
301 (*info->fprintf_func) (info->stream, "mdr");
302
303 else if ((operand->flags & MN10200_OPERAND_PAREN) != 0)
304 {
305 if (paren)
306 (*info->fprintf_func) (info->stream, ")");
307 else
308 {
309 (*info->fprintf_func) (info->stream, "(");
310 nocomma = 1;
311 }
312 paren = !paren;
313 }
314
315 else if ((operand->flags & MN10200_OPERAND_PCREL) != 0)
09171e3f 316 (*info->print_address_func) ((value + memaddr) & 0xffffff, info);
781766e7
JL
317
318 else if ((operand->flags & MN10200_OPERAND_MEMADDR) != 0)
319 (*info->print_address_func) (value, info);
320
321 else
322 (*info->fprintf_func) (info->stream, "%d", value);
323 }
324 /* All done. */
325 break;
326 }
327 op++;
328 }
329
330 if (!match)
331 {
332 (*info->fprintf_func) (info->stream, "unknown\t0x%04x", insn);
333 }
e7c50cef 334}
This page took 0.046537 seconds and 4 git commands to generate.