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