]>
Commit | Line | Data |
---|---|---|
ae1b99e4 | 1 | /* Disassemble MN10300 instructions. |
e7c50cef JL |
2 | Copyright (C) 1996 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" | |
ae1b99e4 | 22 | #include "opcode/mn10300.h" |
e7c50cef JL |
23 | #include "dis-asm.h" |
24 | ||
4aa92185 JL |
25 | static void disassemble PARAMS ((bfd_vma, struct disassemble_info *, |
26 | unsigned long insn, unsigned long, | |
27 | unsigned int)); | |
28 | ||
e7c50cef | 29 | int |
ae1b99e4 | 30 | print_insn_mn10300 (memaddr, info) |
e7c50cef JL |
31 | bfd_vma memaddr; |
32 | struct disassemble_info *info; | |
33 | { | |
4aa92185 JL |
34 | int status; |
35 | bfd_byte buffer[4]; | |
36 | unsigned long insn; | |
37 | unsigned long extension; | |
38 | unsigned int consume; | |
39 | ||
40 | /* First figure out how big the opcode is. */ | |
41 | status = (*info->read_memory_func) (memaddr, buffer, 1, info); | |
42 | if (status != 0) | |
43 | { | |
44 | (*info->memory_error_func) (status, memaddr, info); | |
45 | return -1; | |
46 | } | |
47 | insn = *(unsigned char *) buffer; | |
48 | ||
d91028d2 | 49 | /* These are one byte insns. */ |
4aa92185 JL |
50 | if ((insn & 0xf3) == 0x00 |
51 | || (insn & 0xf0) == 0x10 | |
52 | || (insn & 0xfc) == 0x3c | |
53 | || (insn & 0xf3) == 0x41 | |
54 | || (insn & 0xf3) == 0x40 | |
55 | || (insn & 0xfc) == 0x50 | |
56 | || (insn & 0xfc) == 0x54 | |
57 | || (insn & 0xf0) == 0x60 | |
58 | || (insn & 0xf0) == 0x70 | |
59 | || ((insn & 0xf0) == 0x80 | |
60 | && (insn & 0x0c) >> 2 != (insn & 0x03)) | |
61 | || ((insn & 0xf0) == 0x90 | |
62 | && (insn & 0x0c) >> 2 != (insn & 0x03)) | |
63 | || ((insn & 0xf0) == 0xa0 | |
64 | && (insn & 0x0c) >> 2 != (insn & 0x03)) | |
65 | || ((insn & 0xf0) == 0xb0 | |
66 | && (insn & 0x0c) >> 2 != (insn & 0x03)) | |
67 | || (insn & 0xff) == 0xcb | |
68 | || (insn & 0xfc) == 0xd0 | |
69 | || (insn & 0xfc) == 0xd4 | |
70 | || (insn & 0xfc) == 0xd8 | |
71 | || (insn & 0xf0) == 0xe0) | |
72 | { | |
73 | extension = 0; | |
74 | consume = 1; | |
75 | } | |
76 | ||
77 | /* These are two byte insns. */ | |
78 | else if ((insn & 0xf0) == 0x80 | |
79 | || (insn & 0xf0) == 0x90 | |
80 | || (insn & 0xf0) == 0xa0 | |
81 | || (insn & 0xf0) == 0xb0 | |
82 | || (insn & 0xfc) == 0x20 | |
83 | || (insn & 0xfc) == 0x28 | |
84 | || (insn & 0xf3) == 0x43 | |
85 | || (insn & 0xf3) == 0x42 | |
86 | || (insn & 0xfc) == 0x58 | |
87 | || (insn & 0xfc) == 0x5c | |
88 | || ((insn & 0xf0) == 0xc0 | |
89 | && (insn & 0xff) != 0xcb | |
90 | && (insn & 0xff) != 0xcc | |
91 | && (insn & 0xff) != 0xcd) | |
92 | || (insn & 0xff) == 0xf0 | |
93 | || (insn & 0xff) == 0xf1 | |
94 | || (insn & 0xff) == 0xf2 | |
95 | || (insn & 0xff) == 0xf3 | |
96 | || (insn & 0xff) == 0xf4 | |
97 | || (insn & 0xff) == 0xf5 | |
98 | || (insn & 0xff) == 0xf6) | |
99 | { | |
100 | status = (*info->read_memory_func) (memaddr, buffer, 2, info); | |
101 | if (status != 0) | |
102 | { | |
103 | (*info->memory_error_func) (status, memaddr, info); | |
104 | return -1; | |
105 | } | |
106 | insn = bfd_getb16 (buffer); | |
107 | extension = 0; | |
108 | consume = 2; | |
109 | } | |
110 | ||
111 | /* These are three byte insns. */ | |
112 | else if ((insn & 0xff) == 0xf8 | |
113 | || (insn & 0xff) == 0xcc | |
114 | || (insn & 0xff) == 0xf9 | |
115 | || (insn & 0xf3) == 0x01 | |
116 | || (insn & 0xf3) == 0x02 | |
117 | || (insn & 0xf3) == 0x03 | |
118 | || (insn & 0xfc) == 0x24 | |
119 | || (insn & 0xfc) == 0x2c | |
120 | || (insn & 0xfc) == 0x30 | |
121 | || (insn & 0xfc) == 0x34 | |
122 | || (insn & 0xfc) == 0x38 | |
123 | || (insn & 0xff) == 0xde | |
124 | || (insn & 0xff) == 0xdf | |
125 | || (insn & 0xff) == 0xcc) | |
126 | { | |
127 | status = (*info->read_memory_func) (memaddr, buffer, 2, info); | |
128 | if (status != 0) | |
129 | { | |
130 | (*info->memory_error_func) (status, memaddr, info); | |
131 | return -1; | |
132 | } | |
133 | insn = bfd_getb16 (buffer); | |
134 | insn <<= 8; | |
d91028d2 JL |
135 | status = (*info->read_memory_func) (memaddr + 2, buffer, 1, info); |
136 | if (status != 0) | |
137 | { | |
138 | (*info->memory_error_func) (status, memaddr, info); | |
139 | return -1; | |
140 | } | |
141 | insn |= *(unsigned char *)buffer; | |
4aa92185 JL |
142 | extension = 0; |
143 | consume = 3; | |
144 | } | |
145 | ||
146 | /* These are four byte insns. */ | |
147 | else if ((insn & 0xff) == 0xfa | |
148 | || (insn & 0xff) == 0xfb) | |
149 | { | |
150 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | |
151 | if (status != 0) | |
152 | { | |
153 | (*info->memory_error_func) (status, memaddr, info); | |
154 | return -1; | |
155 | } | |
156 | insn = bfd_getb32 (buffer); | |
157 | extension = 0; | |
158 | consume = 4; | |
159 | } | |
160 | ||
161 | /* These are five byte insns. */ | |
162 | else if ((insn & 0xff) == 0xcd | |
163 | || (insn & 0xff) == 0xdc) | |
164 | { | |
165 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | |
166 | if (status != 0) | |
167 | { | |
168 | (*info->memory_error_func) (status, memaddr, info); | |
169 | return -1; | |
170 | } | |
171 | insn = bfd_getb32 (buffer); | |
172 | ||
d91028d2 | 173 | status = (*info->read_memory_func) (memaddr + 4, buffer, 1, info); |
4aa92185 JL |
174 | if (status != 0) |
175 | { | |
d91028d2 | 176 | (*info->memory_error_func) (status, memaddr + 4, info); |
4aa92185 JL |
177 | return -1; |
178 | } | |
d91028d2 | 179 | extension = *(unsigned char *) buffer; |
4aa92185 JL |
180 | consume = 5; |
181 | } | |
182 | ||
183 | /* These are six byte insns. */ | |
184 | else if ((insn & 0xff) == 0xfd | |
185 | || (insn & 0xff) == 0xfc) | |
186 | { | |
187 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | |
188 | if (status != 0) | |
189 | { | |
190 | (*info->memory_error_func) (status, memaddr, info); | |
191 | return -1; | |
192 | } | |
193 | ||
194 | insn = bfd_getb32 (buffer); | |
d91028d2 | 195 | status = (*info->read_memory_func) (memaddr + 4, buffer, 2, info); |
4aa92185 JL |
196 | if (status != 0) |
197 | { | |
d91028d2 | 198 | (*info->memory_error_func) (status, memaddr + 4, info); |
4aa92185 JL |
199 | return -1; |
200 | } | |
d91028d2 | 201 | extension = bfd_getb16 (buffer); |
4aa92185 JL |
202 | consume = 6; |
203 | } | |
204 | ||
205 | /* Else its a seven byte insns (in theory). */ | |
206 | else | |
207 | { | |
208 | status = (*info->read_memory_func) (memaddr, buffer, 4, info); | |
209 | if (status != 0) | |
210 | { | |
211 | (*info->memory_error_func) (status, memaddr, info); | |
212 | return -1; | |
213 | } | |
214 | ||
215 | insn = bfd_getb32 (buffer); | |
d91028d2 | 216 | status = (*info->read_memory_func) (memaddr + 4, buffer, 2, info); |
4aa92185 JL |
217 | if (status != 0) |
218 | { | |
d91028d2 | 219 | (*info->memory_error_func) (status, memaddr + 4, info); |
4aa92185 JL |
220 | return -1; |
221 | } | |
222 | extension = bfd_getb16 (buffer); | |
223 | extension <<= 8; | |
d91028d2 JL |
224 | status = (*info->read_memory_func) (memaddr + 7, buffer, 1, info); |
225 | if (status != 0) | |
226 | { | |
227 | (*info->memory_error_func) (status, memaddr + 7, info); | |
228 | return -1; | |
229 | } | |
230 | extension |= *(unsigned char *)buffer; | |
4aa92185 JL |
231 | consume = 7; |
232 | } | |
233 | ||
234 | disassemble (memaddr, info, insn, extension, consume); | |
235 | ||
236 | return consume; | |
237 | } | |
238 | ||
239 | static void | |
240 | disassemble (memaddr, info, insn, extension, size) | |
241 | bfd_vma memaddr; | |
242 | struct disassemble_info *info; | |
243 | unsigned long insn; | |
244 | unsigned long extension; | |
245 | unsigned int size; | |
246 | { | |
247 | struct mn10300_opcode *op = (struct mn10300_opcode *)mn10300_opcodes; | |
248 | const struct mn10300_operand *operand; | |
249 | int match = 0; | |
250 | ||
251 | /* Find the opcode. */ | |
252 | while (op->name) | |
253 | { | |
d91028d2 | 254 | int mysize, extra_shift; |
4aa92185 JL |
255 | |
256 | if (op->format == FMT_S0) | |
257 | mysize = 1; | |
258 | else if (op->format == FMT_S1 | |
259 | || op->format == FMT_D0) | |
260 | mysize = 2; | |
261 | else if (op->format == FMT_S2 | |
262 | || op->format == FMT_D1) | |
263 | mysize = 3; | |
264 | else if (op->format == FMT_S4) | |
265 | mysize = 5; | |
266 | else if (op->format == FMT_D2) | |
267 | mysize = 4; | |
268 | else if (op->format == FMT_D4) | |
269 | mysize = 6; | |
270 | else | |
271 | mysize = 7; | |
272 | ||
d91028d2 JL |
273 | if (op->format == FMT_D1 || op->format == FMT_S1) |
274 | extra_shift = 8; | |
275 | else if (op->format == FMT_D2 || op->format == FMT_D4 | |
276 | || op->format == FMT_S2 || op->format == FMT_S4 | |
277 | || op->format == FMT_S6 || op->format == FMT_D5) | |
278 | extra_shift = 16; | |
279 | else | |
280 | extra_shift = 0; | |
281 | ||
4aa92185 JL |
282 | if ((op->mask & insn) == op->opcode |
283 | && size == mysize) | |
284 | { | |
285 | const unsigned char *opindex_ptr; | |
d91028d2 JL |
286 | unsigned int nocomma, memop; |
287 | int paren = 0; | |
4aa92185 JL |
288 | |
289 | match = 1; | |
290 | (*info->fprintf_func) (info->stream, "%s\t", op->name); | |
291 | ||
292 | /* Now print the operands. */ | |
d91028d2 | 293 | for (opindex_ptr = op->operands, nocomma = 1; |
4aa92185 | 294 | *opindex_ptr != 0; |
d91028d2 | 295 | opindex_ptr++) |
4aa92185 JL |
296 | { |
297 | unsigned long value; | |
298 | ||
299 | operand = &mn10300_operands[*opindex_ptr]; | |
300 | ||
d91028d2 JL |
301 | value = ((insn >> (operand->shift)) |
302 | & ((1 << operand->bits) - 1)); | |
4aa92185 JL |
303 | |
304 | if ((operand->flags & MN10300_OPERAND_SIGNED) != 0) | |
305 | value = ((long)(value << (32 - operand->bits)) | |
306 | >> (32 - operand->bits)); | |
307 | ||
d91028d2 JL |
308 | if (!nocomma |
309 | && (!paren | |
310 | || ((operand->flags & MN10300_OPERAND_PAREN) == 0))) | |
311 | (*info->fprintf_func) (info->stream, ","); | |
312 | ||
313 | nocomma = 0; | |
314 | ||
315 | if ((operand->flags & MN10300_OPERAND_DREG) != 0) | |
316 | { | |
317 | value = ((insn >> (operand->shift + extra_shift)) | |
318 | & ((1 << operand->bits) - 1)); | |
319 | (*info->fprintf_func) (info->stream, "d%d", value); | |
320 | } | |
321 | ||
322 | else if ((operand->flags & MN10300_OPERAND_AREG) != 0) | |
323 | { | |
324 | value = ((insn >> (operand->shift + extra_shift)) | |
325 | & ((1 << operand->bits) - 1)); | |
326 | (*info->fprintf_func) (info->stream, "a%d", value); | |
327 | } | |
328 | ||
329 | else if ((operand->flags & MN10300_OPERAND_SP) != 0) | |
330 | (*info->fprintf_func) (info->stream, "sp"); | |
331 | ||
332 | else if ((operand->flags & MN10300_OPERAND_PSW) != 0) | |
333 | (*info->fprintf_func) (info->stream, "psw"); | |
334 | ||
335 | else if ((operand->flags & MN10300_OPERAND_MDR) != 0) | |
336 | (*info->fprintf_func) (info->stream, "mdr"); | |
337 | ||
338 | else if ((operand->flags & MN10300_OPERAND_PAREN) != 0) | |
339 | { | |
340 | if (paren) | |
341 | (*info->fprintf_func) (info->stream, ")"); | |
342 | else | |
343 | { | |
344 | (*info->fprintf_func) (info->stream, "("); | |
345 | nocomma = 1; | |
346 | } | |
347 | paren = !paren; | |
348 | } | |
349 | ||
350 | else | |
351 | (*info->fprintf_func) (info->stream, "%d", value); | |
352 | } | |
4aa92185 JL |
353 | /* All done. */ |
354 | break; | |
355 | } | |
356 | op++; | |
357 | } | |
358 | ||
359 | if (!match) | |
360 | { | |
361 | (*info->fprintf_func) (info->stream, "unknown\t0x%04x", insn); | |
362 | } | |
e7c50cef | 363 | } |