Thu Jan 30 11:30:45 1997 Martin M. Hunt <hunt@pizza.cygnus.com>
[deliverable/binutils-gdb.git] / opcodes / mn10200-dis.c
1 /* Disassemble MN10200 instructions.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17
18
19 #include <stdio.h>
20
21 #include "ansidecl.h"
22 #include "opcode/mn10200.h"
23 #include "dis-asm.h"
24
25 static void disassemble PARAMS ((bfd_vma, struct disassemble_info *,
26 unsigned long insn, unsigned long,
27 unsigned int));
28
29 int
30 print_insn_mn10200 (memaddr, info)
31 bfd_vma memaddr;
32 struct disassemble_info *info;
33 {
34 int status;
35 bfd_byte buffer[4];
36 unsigned long insn, extension;
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. */
49 if ((insn & 0xf0) == 0x00
50 || (insn & 0xf0) == 0x10
51 || (insn & 0xf0) == 0x20
52 || (insn & 0xf0) == 0x30
53 || ((insn & 0xf0) == 0x80
54 && (insn & 0x0c) >> 2 != (insn & 0x03))
55 || (insn & 0xf0) == 0x90
56 || (insn & 0xf0) == 0xa0
57 || (insn & 0xf0) == 0xb0
58 || (insn & 0xff) == 0xeb
59 || (insn & 0xff) == 0xf6
60 || (insn & 0xff) == 0xfe)
61 {
62 extension = 0;
63 consume = 1;
64 }
65
66 /* These are two byte insns. */
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
80 || (insn & 0xff) == 0xf0
81 || (insn & 0xff) == 0xf1
82 || (insn & 0xff) == 0xf2
83 || (insn & 0xff) == 0xf3)
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);
92 consume = 2;
93 }
94
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
101 || (insn & 0xff) == 0xf9
102 || (insn & 0xff) == 0xfa
103 || (insn & 0xff) == 0xfb
104 || (insn & 0xff) == 0xfc
105 || (insn & 0xff) == 0xfd)
106 {
107 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
108 if (status != 0)
109 {
110 (*info->memory_error_func) (status, memaddr, info);
111 return -1;
112 }
113 insn <<= 16;
114 insn |= bfd_getl16 (buffer);
115 extension = 0;
116 consume = 3;
117 }
118 /* These are three byte insns too, but we don't have to mess with
119 endianness stuff. */
120 else if ((insn & 0xff) == 0xf5)
121 {
122 status = (*info->read_memory_func) (memaddr + 1, buffer, 2, info);
123 if (status != 0)
124 {
125 (*info->memory_error_func) (status, memaddr, info);
126 return -1;
127 }
128 insn <<= 16;
129 insn |= bfd_getb16 (buffer);
130 extension = 0;
131 consume = 3;
132 }
133
134 /* These are four byte insns. */
135 else if ((insn & 0xff) == 0xf7)
136 {
137 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
138 if (status != 0)
139 {
140 (*info->memory_error_func) (status, memaddr, info);
141 return -1;
142 }
143 insn = bfd_getb16 (buffer);
144 insn <<= 16;
145 status = (*info->read_memory_func) (memaddr + 2, buffer, 2, info);
146 if (status != 0)
147 {
148 (*info->memory_error_func) (status, memaddr, info);
149 return -1;
150 }
151 insn |= bfd_getl16 (buffer);
152 extension = 0;
153 consume = 4;
154 }
155
156 /* These are five byte insns. */
157 else if ((insn & 0xff) == 0xf4)
158 {
159 status = (*info->read_memory_func) (memaddr, buffer, 2, info);
160 if (status != 0)
161 {
162 (*info->memory_error_func) (status, memaddr, info);
163 return -1;
164 }
165 insn = bfd_getb16 (buffer);
166 insn <<= 16;
167
168 status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info);
169 if (status != 0)
170 {
171 (*info->memory_error_func) (status, memaddr, info);
172 return -1;
173 }
174 insn |= *(unsigned char *)buffer << 8;
175
176 status = (*info->read_memory_func) (memaddr + 3, buffer, 1, info);
177 if (status != 0)
178 {
179 (*info->memory_error_func) (status, memaddr, info);
180 return -1;
181 }
182 insn |= *(unsigned char *)buffer;
183
184 status = (*info->read_memory_func) (memaddr + 2, buffer, 1, info);
185 if (status != 0)
186 {
187 (*info->memory_error_func) (status, memaddr, info);
188 return -1;
189 }
190 extension = *(unsigned char *)buffer;
191 consume = 5;
192 }
193 else
194 return -1;
195
196 disassemble (memaddr, info, insn, extension, consume);
197
198 return consume;
199 }
200
201 static void
202 disassemble (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
218 if (op->format == FMT_1)
219 mysize = 1;
220 else if (op->format == FMT_2
221 || op->format == FMT_4)
222 mysize = 2;
223 else if (op->format == FMT_3
224 || op->format == FMT_5)
225 mysize = 3;
226 else if (op->format == FMT_6)
227 mysize = 4;
228 else if (op->format == FMT_7)
229 mysize = 5;
230 else
231 abort ();
232
233 if (op->format == FMT_2 || op->format == FMT_5)
234 extra_shift = 8;
235 else if (op->format == FMT_3
236 || op->format == FMT_6
237 || op->format == FMT_7)
238 extra_shift = 16;
239 else
240 extra_shift = 0;
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
261 if ((operand->flags & MN10200_OPERAND_EXTENDED) != 0)
262 {
263 value = (insn & 0xffff) << 8;
264 value |= extension;
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)
316 (*info->print_address_func) ((value + memaddr) & 0xffffff, info);
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 }
334 }
This page took 0.043679 seconds and 4 git commands to generate.