xref: /openbsd-src/gnu/usr.bin/binutils/opcodes/tic30-dis.c (revision d2201f2f89f0be1a0be6f7568000ed297414a06d)
1f7cc78ecSespie /* Disassembly routines for TMS320C30 architecture
2*d2201f2fSdrahn    Copyright 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3f7cc78ecSespie    Contributed by Steven Haworth (steve@pm.cse.rmit.edu.au)
4f7cc78ecSespie 
5f7cc78ecSespie    This program is free software; you can redistribute it and/or modify
6f7cc78ecSespie    it under the terms of the GNU General Public License as published by
7f7cc78ecSespie    the Free Software Foundation; either version 2 of the License, or
8f7cc78ecSespie    (at your option) any later version.
9f7cc78ecSespie 
10f7cc78ecSespie    This program is distributed in the hope that it will be useful,
11f7cc78ecSespie    but WITHOUT ANY WARRANTY; without even the implied warranty of
12f7cc78ecSespie    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f7cc78ecSespie    GNU General Public License for more details.
14f7cc78ecSespie 
15f7cc78ecSespie    You should have received a copy of the GNU General Public License
16f7cc78ecSespie    along with this program; if not, write to the Free Software
17f7cc78ecSespie    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18f7cc78ecSespie    02111-1307, USA.  */
19f7cc78ecSespie 
20f7cc78ecSespie #include <errno.h>
21f7cc78ecSespie #include <math.h>
22f7cc78ecSespie #include "sysdep.h"
23f7cc78ecSespie #include "dis-asm.h"
24f7cc78ecSespie #include "opcode/tic30.h"
25f7cc78ecSespie 
26f7cc78ecSespie #define NORMAL_INSN   1
27f7cc78ecSespie #define PARALLEL_INSN 2
28f7cc78ecSespie 
29f7cc78ecSespie /* Gets the type of instruction based on the top 2 or 3 bits of the
30f7cc78ecSespie    instruction word.  */
31f7cc78ecSespie #define GET_TYPE(insn) (insn & 0x80000000 ? insn & 0xC0000000 : insn & 0xE0000000)
32f7cc78ecSespie 
33f7cc78ecSespie /* Instruction types.  */
34f7cc78ecSespie #define TWO_OPERAND_1 0x00000000
35f7cc78ecSespie #define TWO_OPERAND_2 0x40000000
36f7cc78ecSespie #define THREE_OPERAND 0x20000000
37f7cc78ecSespie #define PAR_STORE     0xC0000000
38f7cc78ecSespie #define MUL_ADDS      0x80000000
39f7cc78ecSespie #define BRANCHES      0x60000000
40f7cc78ecSespie 
41f7cc78ecSespie /* Specific instruction id bits.  */
42f7cc78ecSespie #define NORMAL_IDEN    0x1F800000
43f7cc78ecSespie #define PAR_STORE_IDEN 0x3E000000
44f7cc78ecSespie #define MUL_ADD_IDEN   0x2C000000
45f7cc78ecSespie #define BR_IMM_IDEN    0x1F000000
46f7cc78ecSespie #define BR_COND_IDEN   0x1C3F0000
47f7cc78ecSespie 
48f7cc78ecSespie /* Addressing modes.  */
49f7cc78ecSespie #define AM_REGISTER 0x00000000
50f7cc78ecSespie #define AM_DIRECT   0x00200000
51f7cc78ecSespie #define AM_INDIRECT 0x00400000
52f7cc78ecSespie #define AM_IMM      0x00600000
53f7cc78ecSespie 
54f7cc78ecSespie #define P_FIELD 0x03000000
55f7cc78ecSespie 
56f7cc78ecSespie #define REG_AR0 0x08
57f7cc78ecSespie #define LDP_INSN 0x08700000
58f7cc78ecSespie 
59f7cc78ecSespie /* TMS320C30 program counter for current instruction.  */
60f7cc78ecSespie static unsigned int _pc;
61f7cc78ecSespie 
62f7cc78ecSespie struct instruction
63f7cc78ecSespie {
64f7cc78ecSespie   int type;
65f7cc78ecSespie   template *tm;
66f7cc78ecSespie   partemplate *ptm;
67f7cc78ecSespie };
68f7cc78ecSespie 
69f7cc78ecSespie int get_tic30_instruction PARAMS ((unsigned long, struct instruction *));
70f7cc78ecSespie int print_two_operand
71f7cc78ecSespie   PARAMS ((disassemble_info *, unsigned long, struct instruction *));
72f7cc78ecSespie int print_three_operand
73f7cc78ecSespie   PARAMS ((disassemble_info *, unsigned long, struct instruction *));
74f7cc78ecSespie int print_par_insn
75f7cc78ecSespie   PARAMS ((disassemble_info *, unsigned long, struct instruction *));
76f7cc78ecSespie int print_branch
77f7cc78ecSespie   PARAMS ((disassemble_info *, unsigned long, struct instruction *));
78f7cc78ecSespie int get_indirect_operand PARAMS ((unsigned short, int, char *));
79f7cc78ecSespie int get_register_operand PARAMS ((unsigned char, char *));
80f7cc78ecSespie int cnvt_tmsfloat_ieee PARAMS ((unsigned long, int, float *));
81f7cc78ecSespie 
82f7cc78ecSespie int
print_insn_tic30(pc,info)83f7cc78ecSespie print_insn_tic30 (pc, info)
84f7cc78ecSespie      bfd_vma pc;
85f7cc78ecSespie      disassemble_info *info;
86f7cc78ecSespie {
87f7cc78ecSespie   unsigned long insn_word;
885f210c2aSfgsch   struct instruction insn = { 0, NULL, NULL };
89f7cc78ecSespie   bfd_vma bufaddr = pc - info->buffer_vma;
90f7cc78ecSespie   /* Obtain the current instruction word from the buffer.  */
91f7cc78ecSespie   insn_word = (*(info->buffer + bufaddr) << 24) | (*(info->buffer + bufaddr + 1) << 16) |
92f7cc78ecSespie     (*(info->buffer + bufaddr + 2) << 8) | *(info->buffer + bufaddr + 3);
93f7cc78ecSespie   _pc = pc / 4;
94f7cc78ecSespie   /* Get the instruction refered to by the current instruction word
95f7cc78ecSespie      and print it out based on its type.  */
96f7cc78ecSespie   if (!get_tic30_instruction (insn_word, &insn))
97f7cc78ecSespie     return -1;
98f7cc78ecSespie   switch (GET_TYPE (insn_word))
99f7cc78ecSespie     {
100f7cc78ecSespie     case TWO_OPERAND_1:
101f7cc78ecSespie     case TWO_OPERAND_2:
102f7cc78ecSespie       if (!print_two_operand (info, insn_word, &insn))
103f7cc78ecSespie 	return -1;
104f7cc78ecSespie       break;
105f7cc78ecSespie     case THREE_OPERAND:
106f7cc78ecSespie       if (!print_three_operand (info, insn_word, &insn))
107f7cc78ecSespie 	return -1;
108f7cc78ecSespie       break;
109f7cc78ecSespie     case PAR_STORE:
110f7cc78ecSespie     case MUL_ADDS:
111f7cc78ecSespie       if (!print_par_insn (info, insn_word, &insn))
112f7cc78ecSespie 	return -1;
113f7cc78ecSespie       break;
114f7cc78ecSespie     case BRANCHES:
115f7cc78ecSespie       if (!print_branch (info, insn_word, &insn))
116f7cc78ecSespie 	return -1;
117f7cc78ecSespie       break;
118f7cc78ecSespie     }
119f7cc78ecSespie   return 4;
120f7cc78ecSespie }
121f7cc78ecSespie 
122f7cc78ecSespie int
get_tic30_instruction(insn_word,insn)123f7cc78ecSespie get_tic30_instruction (insn_word, insn)
124f7cc78ecSespie      unsigned long insn_word;
125f7cc78ecSespie      struct instruction *insn;
126f7cc78ecSespie {
127f7cc78ecSespie   switch (GET_TYPE (insn_word))
128f7cc78ecSespie     {
129f7cc78ecSespie     case TWO_OPERAND_1:
130f7cc78ecSespie     case TWO_OPERAND_2:
131f7cc78ecSespie     case THREE_OPERAND:
132f7cc78ecSespie       insn->type = NORMAL_INSN;
133f7cc78ecSespie       {
134f7cc78ecSespie 	template *current_optab = (template *) tic30_optab;
135f7cc78ecSespie 	for (; current_optab < tic30_optab_end; current_optab++)
136f7cc78ecSespie 	  {
137f7cc78ecSespie 	    if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
138f7cc78ecSespie 	      {
139f7cc78ecSespie 		if (current_optab->operands == 0)
140f7cc78ecSespie 		  {
141f7cc78ecSespie 		    if (current_optab->base_opcode == insn_word)
142f7cc78ecSespie 		      {
143f7cc78ecSespie 			insn->tm = current_optab;
144f7cc78ecSespie 			break;
145f7cc78ecSespie 		      }
146f7cc78ecSespie 		  }
147f7cc78ecSespie 		else if ((current_optab->base_opcode & NORMAL_IDEN) == (insn_word & NORMAL_IDEN))
148f7cc78ecSespie 		  {
149f7cc78ecSespie 		    insn->tm = current_optab;
150f7cc78ecSespie 		    break;
151f7cc78ecSespie 		  }
152f7cc78ecSespie 	      }
153f7cc78ecSespie 	  }
154f7cc78ecSespie       }
155f7cc78ecSespie       break;
156f7cc78ecSespie     case PAR_STORE:
157f7cc78ecSespie       insn->type = PARALLEL_INSN;
158f7cc78ecSespie       {
159f7cc78ecSespie 	partemplate *current_optab = (partemplate *) tic30_paroptab;
160f7cc78ecSespie 	for (; current_optab < tic30_paroptab_end; current_optab++)
161f7cc78ecSespie 	  {
162f7cc78ecSespie 	    if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
163f7cc78ecSespie 	      {
164f7cc78ecSespie 		if ((current_optab->base_opcode & PAR_STORE_IDEN) == (insn_word & PAR_STORE_IDEN))
165f7cc78ecSespie 		  {
166f7cc78ecSespie 		    insn->ptm = current_optab;
167f7cc78ecSespie 		    break;
168f7cc78ecSespie 		  }
169f7cc78ecSespie 	      }
170f7cc78ecSespie 	  }
171f7cc78ecSespie       }
172f7cc78ecSespie       break;
173f7cc78ecSespie     case MUL_ADDS:
174f7cc78ecSespie       insn->type = PARALLEL_INSN;
175f7cc78ecSespie       {
176f7cc78ecSespie 	partemplate *current_optab = (partemplate *) tic30_paroptab;
177f7cc78ecSespie 	for (; current_optab < tic30_paroptab_end; current_optab++)
178f7cc78ecSespie 	  {
179f7cc78ecSespie 	    if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
180f7cc78ecSespie 	      {
181f7cc78ecSespie 		if ((current_optab->base_opcode & MUL_ADD_IDEN) == (insn_word & MUL_ADD_IDEN))
182f7cc78ecSespie 		  {
183f7cc78ecSespie 		    insn->ptm = current_optab;
184f7cc78ecSespie 		    break;
185f7cc78ecSespie 		  }
186f7cc78ecSespie 	      }
187f7cc78ecSespie 	  }
188f7cc78ecSespie       }
189f7cc78ecSespie       break;
190f7cc78ecSespie     case BRANCHES:
191f7cc78ecSespie       insn->type = NORMAL_INSN;
192f7cc78ecSespie       {
193f7cc78ecSespie 	template *current_optab = (template *) tic30_optab;
194f7cc78ecSespie 	for (; current_optab < tic30_optab_end; current_optab++)
195f7cc78ecSespie 	  {
196f7cc78ecSespie 	    if (GET_TYPE (current_optab->base_opcode) == GET_TYPE (insn_word))
197f7cc78ecSespie 	      {
198f7cc78ecSespie 		if (current_optab->operand_types[0] & Imm24)
199f7cc78ecSespie 		  {
200f7cc78ecSespie 		    if ((current_optab->base_opcode & BR_IMM_IDEN) == (insn_word & BR_IMM_IDEN))
201f7cc78ecSespie 		      {
202f7cc78ecSespie 			insn->tm = current_optab;
203f7cc78ecSespie 			break;
204f7cc78ecSespie 		      }
205f7cc78ecSespie 		  }
206f7cc78ecSespie 		else if (current_optab->operands > 0)
207f7cc78ecSespie 		  {
208f7cc78ecSespie 		    if ((current_optab->base_opcode & BR_COND_IDEN) == (insn_word & BR_COND_IDEN))
209f7cc78ecSespie 		      {
210f7cc78ecSespie 			insn->tm = current_optab;
211f7cc78ecSespie 			break;
212f7cc78ecSespie 		      }
213f7cc78ecSespie 		  }
214f7cc78ecSespie 		else
215f7cc78ecSespie 		  {
216f7cc78ecSespie 		    if ((current_optab->base_opcode & (BR_COND_IDEN | 0x00800000)) == (insn_word & (BR_COND_IDEN | 0x00800000)))
217f7cc78ecSespie 		      {
218f7cc78ecSespie 			insn->tm = current_optab;
219f7cc78ecSespie 			break;
220f7cc78ecSespie 		      }
221f7cc78ecSespie 		  }
222f7cc78ecSespie 	      }
223f7cc78ecSespie 	  }
224f7cc78ecSespie       }
225f7cc78ecSespie       break;
226f7cc78ecSespie     default:
227f7cc78ecSespie       return 0;
228f7cc78ecSespie     }
229f7cc78ecSespie   return 1;
230f7cc78ecSespie }
231f7cc78ecSespie 
232f7cc78ecSespie int
print_two_operand(info,insn_word,insn)233f7cc78ecSespie print_two_operand (info, insn_word, insn)
234f7cc78ecSespie      disassemble_info *info;
235f7cc78ecSespie      unsigned long insn_word;
236f7cc78ecSespie      struct instruction *insn;
237f7cc78ecSespie {
238f7cc78ecSespie   char name[12];
239f7cc78ecSespie   char operand[2][13] =
240f7cc78ecSespie   {
241f7cc78ecSespie     {0},
242f7cc78ecSespie     {0}};
243f7cc78ecSespie   float f_number;
244f7cc78ecSespie 
245f7cc78ecSespie   if (insn->tm == NULL)
246f7cc78ecSespie     return 0;
247f7cc78ecSespie   strcpy (name, insn->tm->name);
248f7cc78ecSespie   if (insn->tm->opcode_modifier == AddressMode)
249f7cc78ecSespie     {
250f7cc78ecSespie       int src_op, dest_op;
251f7cc78ecSespie       /* Determine whether instruction is a store or a normal instruction.  */
252f7cc78ecSespie       if ((insn->tm->operand_types[1] & (Direct | Indirect)) == (Direct | Indirect))
253f7cc78ecSespie 	{
254f7cc78ecSespie 	  src_op = 1;
255f7cc78ecSespie 	  dest_op = 0;
256f7cc78ecSespie 	}
257f7cc78ecSespie       else
258f7cc78ecSespie 	{
259f7cc78ecSespie 	  src_op = 0;
260f7cc78ecSespie 	  dest_op = 1;
261f7cc78ecSespie 	}
262f7cc78ecSespie       /* Get the destination register.  */
263f7cc78ecSespie       if (insn->tm->operands == 2)
264f7cc78ecSespie 	get_register_operand ((insn_word & 0x001F0000) >> 16, operand[dest_op]);
265f7cc78ecSespie       /* Get the source operand based on addressing mode.  */
266f7cc78ecSespie       switch (insn_word & AddressMode)
267f7cc78ecSespie 	{
268f7cc78ecSespie 	case AM_REGISTER:
269f7cc78ecSespie 	  /* Check for the NOP instruction before getting the operand.  */
270f7cc78ecSespie 	  if ((insn->tm->operand_types[0] & NotReq) == 0)
271f7cc78ecSespie 	    get_register_operand ((insn_word & 0x0000001F), operand[src_op]);
272f7cc78ecSespie 	  break;
273f7cc78ecSespie 	case AM_DIRECT:
274f7cc78ecSespie 	  sprintf (operand[src_op], "@0x%lX", (insn_word & 0x0000FFFF));
275f7cc78ecSespie 	  break;
276f7cc78ecSespie 	case AM_INDIRECT:
277f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x0000FFFF), 2, operand[src_op]);
278f7cc78ecSespie 	  break;
279f7cc78ecSespie 	case AM_IMM:
280f7cc78ecSespie 	  /* Get the value of the immediate operand based on variable type.  */
281f7cc78ecSespie 	  switch (insn->tm->imm_arg_type)
282f7cc78ecSespie 	    {
283f7cc78ecSespie 	    case Imm_Float:
284f7cc78ecSespie 	      cnvt_tmsfloat_ieee ((insn_word & 0x0000FFFF), 2, &f_number);
285f7cc78ecSespie 	      sprintf (operand[src_op], "%2.2f", f_number);
286f7cc78ecSespie 	      break;
287f7cc78ecSespie 	    case Imm_SInt:
288f7cc78ecSespie 	      sprintf (operand[src_op], "%d", (short) (insn_word & 0x0000FFFF));
289f7cc78ecSespie 	      break;
290f7cc78ecSespie 	    case Imm_UInt:
291f7cc78ecSespie 	      sprintf (operand[src_op], "%lu", (insn_word & 0x0000FFFF));
292f7cc78ecSespie 	      break;
293f7cc78ecSespie 	    default:
294f7cc78ecSespie 	      return 0;
295f7cc78ecSespie 	    }
296f7cc78ecSespie 	  /* Handle special case for LDP instruction.  */
297f7cc78ecSespie 	  if ((insn_word & 0xFFFFFF00) == LDP_INSN)
298f7cc78ecSespie 	    {
299f7cc78ecSespie 	      strcpy (name, "ldp");
300f7cc78ecSespie 	      sprintf (operand[0], "0x%06lX", (insn_word & 0x000000FF) << 16);
301f7cc78ecSespie 	      operand[1][0] = '\0';
302f7cc78ecSespie 	    }
303f7cc78ecSespie 	}
304f7cc78ecSespie     }
305f7cc78ecSespie   /* Handle case for stack and rotate instructions.  */
306f7cc78ecSespie   else if (insn->tm->operands == 1)
307f7cc78ecSespie     {
308f7cc78ecSespie       if (insn->tm->opcode_modifier == StackOp)
309f7cc78ecSespie 	{
310f7cc78ecSespie 	  get_register_operand ((insn_word & 0x001F0000) >> 16, operand[0]);
311f7cc78ecSespie 	}
312f7cc78ecSespie     }
313f7cc78ecSespie   /* Output instruction to stream.  */
314f7cc78ecSespie   info->fprintf_func (info->stream, "   %s %s%c%s", name,
315f7cc78ecSespie 		      operand[0][0] ? operand[0] : "",
316f7cc78ecSespie 		      operand[1][0] ? ',' : ' ',
317f7cc78ecSespie 		      operand[1][0] ? operand[1] : "");
318f7cc78ecSespie   return 1;
319f7cc78ecSespie }
320f7cc78ecSespie 
321f7cc78ecSespie int
print_three_operand(info,insn_word,insn)322f7cc78ecSespie print_three_operand (info, insn_word, insn)
323f7cc78ecSespie      disassemble_info *info;
324f7cc78ecSespie      unsigned long insn_word;
325f7cc78ecSespie      struct instruction *insn;
326f7cc78ecSespie {
327f7cc78ecSespie   char operand[3][13] =
328f7cc78ecSespie   {
329f7cc78ecSespie     {0},
330f7cc78ecSespie     {0},
331f7cc78ecSespie     {0}};
332f7cc78ecSespie 
333f7cc78ecSespie   if (insn->tm == NULL)
334f7cc78ecSespie     return 0;
335f7cc78ecSespie   switch (insn_word & AddressMode)
336f7cc78ecSespie     {
337f7cc78ecSespie     case AM_REGISTER:
338f7cc78ecSespie       get_register_operand ((insn_word & 0x000000FF), operand[0]);
339f7cc78ecSespie       get_register_operand ((insn_word & 0x0000FF00) >> 8, operand[1]);
340f7cc78ecSespie       break;
341f7cc78ecSespie     case AM_DIRECT:
342f7cc78ecSespie       get_register_operand ((insn_word & 0x000000FF), operand[0]);
343f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1]);
344f7cc78ecSespie       break;
345f7cc78ecSespie     case AM_INDIRECT:
346f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0]);
347f7cc78ecSespie       get_register_operand ((insn_word & 0x0000FF00) >> 8, operand[1]);
348f7cc78ecSespie       break;
349f7cc78ecSespie     case AM_IMM:
350f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0]);
351f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1]);
352f7cc78ecSespie       break;
353f7cc78ecSespie     default:
354f7cc78ecSespie       return 0;
355f7cc78ecSespie     }
356f7cc78ecSespie   if (insn->tm->operands == 3)
357f7cc78ecSespie     get_register_operand ((insn_word & 0x001F0000) >> 16, operand[2]);
358f7cc78ecSespie   info->fprintf_func (info->stream, "   %s %s,%s%c%s", insn->tm->name,
359f7cc78ecSespie 		      operand[0], operand[1],
360f7cc78ecSespie 		      operand[2][0] ? ',' : ' ',
361f7cc78ecSespie 		      operand[2][0] ? operand[2] : "");
362f7cc78ecSespie   return 1;
363f7cc78ecSespie }
364f7cc78ecSespie 
365f7cc78ecSespie int
print_par_insn(info,insn_word,insn)366f7cc78ecSespie print_par_insn (info, insn_word, insn)
367f7cc78ecSespie      disassemble_info *info;
368f7cc78ecSespie      unsigned long insn_word;
369f7cc78ecSespie      struct instruction *insn;
370f7cc78ecSespie {
371f7cc78ecSespie   size_t i, len;
372f7cc78ecSespie   char *name1, *name2;
373f7cc78ecSespie   char operand[2][3][13] =
374f7cc78ecSespie   {
375f7cc78ecSespie     {
376f7cc78ecSespie       {0},
377f7cc78ecSespie       {0},
378f7cc78ecSespie       {0}},
379f7cc78ecSespie     {
380f7cc78ecSespie       {0},
381f7cc78ecSespie       {0},
382f7cc78ecSespie       {0}}};
383f7cc78ecSespie 
384f7cc78ecSespie   if (insn->ptm == NULL)
385f7cc78ecSespie     return 0;
386f7cc78ecSespie   /* Parse out the names of each of the parallel instructions from the
387f7cc78ecSespie      q_insn1_insn2 format.  */
388f7cc78ecSespie   name1 = (char *) strdup (insn->ptm->name + 2);
389f7cc78ecSespie   name2 = "";
390f7cc78ecSespie   len = strlen (name1);
391f7cc78ecSespie   for (i = 0; i < len; i++)
392f7cc78ecSespie     {
393f7cc78ecSespie       if (name1[i] == '_')
394f7cc78ecSespie 	{
395f7cc78ecSespie 	  name2 = &name1[i + 1];
396f7cc78ecSespie 	  name1[i] = '\0';
397f7cc78ecSespie 	  break;
398f7cc78ecSespie 	}
399f7cc78ecSespie     }
400f7cc78ecSespie   /* Get the operands of the instruction based on the operand order.  */
401f7cc78ecSespie   switch (insn->ptm->oporder)
402f7cc78ecSespie     {
403f7cc78ecSespie     case OO_4op1:
404f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
405f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
406f7cc78ecSespie       get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
407f7cc78ecSespie       get_register_operand ((insn_word >> 22) & 0x07, operand[0][1]);
408f7cc78ecSespie       break;
409f7cc78ecSespie     case OO_4op2:
410f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
411f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][0]);
412f7cc78ecSespie       get_register_operand ((insn_word >> 19) & 0x07, operand[1][1]);
413f7cc78ecSespie       get_register_operand ((insn_word >> 22) & 0x07, operand[0][1]);
414f7cc78ecSespie       break;
415f7cc78ecSespie     case OO_4op3:
416f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
417f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
418f7cc78ecSespie       get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
419f7cc78ecSespie       get_register_operand ((insn_word >> 22) & 0x07, operand[0][0]);
420f7cc78ecSespie       break;
421f7cc78ecSespie     case OO_5op1:
422f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][0]);
423f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
424f7cc78ecSespie       get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
425f7cc78ecSespie       get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
426f7cc78ecSespie       get_register_operand ((insn_word >> 22) & 0x07, operand[0][2]);
427f7cc78ecSespie       break;
428f7cc78ecSespie     case OO_5op2:
429f7cc78ecSespie       get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
430f7cc78ecSespie       get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][1]);
431f7cc78ecSespie       get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
432f7cc78ecSespie       get_register_operand ((insn_word >> 19) & 0x07, operand[0][0]);
433f7cc78ecSespie       get_register_operand ((insn_word >> 22) & 0x07, operand[0][2]);
434f7cc78ecSespie       break;
435f7cc78ecSespie     case OO_PField:
436f7cc78ecSespie       if (insn_word & 0x00800000)
437f7cc78ecSespie 	get_register_operand (0x01, operand[0][2]);
438f7cc78ecSespie       else
439f7cc78ecSespie 	get_register_operand (0x00, operand[0][2]);
440f7cc78ecSespie       if (insn_word & 0x00400000)
441f7cc78ecSespie 	get_register_operand (0x03, operand[1][2]);
442f7cc78ecSespie       else
443f7cc78ecSespie 	get_register_operand (0x02, operand[1][2]);
444f7cc78ecSespie       switch (insn_word & P_FIELD)
445f7cc78ecSespie 	{
446f7cc78ecSespie 	case 0x00000000:
447f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x000000FF), 1, operand[0][1]);
448f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
449f7cc78ecSespie 	  get_register_operand ((insn_word >> 16) & 0x07, operand[1][1]);
450f7cc78ecSespie 	  get_register_operand ((insn_word >> 19) & 0x07, operand[1][0]);
451f7cc78ecSespie 	  break;
452f7cc78ecSespie 	case 0x01000000:
453f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][0]);
454f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
455f7cc78ecSespie 	  get_register_operand ((insn_word >> 16) & 0x07, operand[1][1]);
456f7cc78ecSespie 	  get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
457f7cc78ecSespie 	  break;
458f7cc78ecSespie 	case 0x02000000:
459f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][1]);
460f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[1][0]);
461f7cc78ecSespie 	  get_register_operand ((insn_word >> 16) & 0x07, operand[0][1]);
462f7cc78ecSespie 	  get_register_operand ((insn_word >> 19) & 0x07, operand[0][0]);
463f7cc78ecSespie 	  break;
464f7cc78ecSespie 	case 0x03000000:
465f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x000000FF), 1, operand[1][1]);
466f7cc78ecSespie 	  get_indirect_operand ((insn_word & 0x0000FF00) >> 8, 1, operand[0][0]);
467f7cc78ecSespie 	  get_register_operand ((insn_word >> 16) & 0x07, operand[1][0]);
468f7cc78ecSespie 	  get_register_operand ((insn_word >> 19) & 0x07, operand[0][1]);
469f7cc78ecSespie 	  break;
470f7cc78ecSespie 	}
471f7cc78ecSespie       break;
472f7cc78ecSespie     default:
473f7cc78ecSespie       return 0;
474f7cc78ecSespie     }
475f7cc78ecSespie   info->fprintf_func (info->stream, "   %s %s,%s%c%s", name1,
476f7cc78ecSespie 		      operand[0][0], operand[0][1],
477f7cc78ecSespie 		      operand[0][2][0] ? ',' : ' ',
478f7cc78ecSespie 		      operand[0][2][0] ? operand[0][2] : "");
479f7cc78ecSespie   info->fprintf_func (info->stream, "\n\t\t\t|| %s %s,%s%c%s", name2,
480f7cc78ecSespie 		      operand[1][0], operand[1][1],
481f7cc78ecSespie 		      operand[1][2][0] ? ',' : ' ',
482f7cc78ecSespie 		      operand[1][2][0] ? operand[1][2] : "");
483f7cc78ecSespie   free (name1);
484f7cc78ecSespie   return 1;
485f7cc78ecSespie }
486f7cc78ecSespie 
487f7cc78ecSespie int
print_branch(info,insn_word,insn)488f7cc78ecSespie print_branch (info, insn_word, insn)
489f7cc78ecSespie      disassemble_info *info;
490f7cc78ecSespie      unsigned long insn_word;
491f7cc78ecSespie      struct instruction *insn;
492f7cc78ecSespie {
493f7cc78ecSespie   char operand[2][13] =
494f7cc78ecSespie   {
495f7cc78ecSespie     {0},
496f7cc78ecSespie     {0}};
497f7cc78ecSespie   unsigned long address;
498f7cc78ecSespie   int print_label = 0;
499f7cc78ecSespie 
500f7cc78ecSespie   if (insn->tm == NULL)
501f7cc78ecSespie     return 0;
502f7cc78ecSespie   /* Get the operands for 24-bit immediate jumps.  */
503f7cc78ecSespie   if (insn->tm->operand_types[0] & Imm24)
504f7cc78ecSespie     {
505f7cc78ecSespie       address = insn_word & 0x00FFFFFF;
506f7cc78ecSespie       sprintf (operand[0], "0x%lX", address);
507f7cc78ecSespie       print_label = 1;
508f7cc78ecSespie     }
509f7cc78ecSespie   /* Get the operand for the trap instruction.  */
510f7cc78ecSespie   else if (insn->tm->operand_types[0] & IVector)
511f7cc78ecSespie     {
512f7cc78ecSespie       address = insn_word & 0x0000001F;
513f7cc78ecSespie       sprintf (operand[0], "0x%lX", address);
514f7cc78ecSespie     }
515f7cc78ecSespie   else
516f7cc78ecSespie     {
517f7cc78ecSespie       address = insn_word & 0x0000FFFF;
518f7cc78ecSespie       /* Get the operands for the DB instructions.  */
519f7cc78ecSespie       if (insn->tm->operands == 2)
520f7cc78ecSespie 	{
521f7cc78ecSespie 	  get_register_operand (((insn_word & 0x01C00000) >> 22) + REG_AR0, operand[0]);
522f7cc78ecSespie 	  if (insn_word & PCRel)
523f7cc78ecSespie 	    {
524f7cc78ecSespie 	      sprintf (operand[1], "%d", (short) address);
525f7cc78ecSespie 	      print_label = 1;
526f7cc78ecSespie 	    }
527f7cc78ecSespie 	  else
528f7cc78ecSespie 	    get_register_operand (insn_word & 0x0000001F, operand[1]);
529f7cc78ecSespie 	}
530f7cc78ecSespie       /* Get the operands for the standard branches.  */
531f7cc78ecSespie       else if (insn->tm->operands == 1)
532f7cc78ecSespie 	{
533f7cc78ecSespie 	  if (insn_word & PCRel)
534f7cc78ecSespie 	    {
535f7cc78ecSespie 	      address = (short) address;
536f7cc78ecSespie 	      sprintf (operand[0], "%ld", address);
537f7cc78ecSespie 	      print_label = 1;
538f7cc78ecSespie 	    }
539f7cc78ecSespie 	  else
540f7cc78ecSespie 	    get_register_operand (insn_word & 0x0000001F, operand[0]);
541f7cc78ecSespie 	}
542f7cc78ecSespie     }
543f7cc78ecSespie   info->fprintf_func (info->stream, "   %s %s%c%s", insn->tm->name,
544f7cc78ecSespie 		      operand[0][0] ? operand[0] : "",
545f7cc78ecSespie 		      operand[1][0] ? ',' : ' ',
546f7cc78ecSespie 		      operand[1][0] ? operand[1] : "");
547f7cc78ecSespie   /* Print destination of branch in relation to current symbol.  */
548f7cc78ecSespie   if (print_label && info->symbols)
549f7cc78ecSespie     {
550f7cc78ecSespie       asymbol *sym = *info->symbols;
551f7cc78ecSespie 
552f7cc78ecSespie       if ((insn->tm->opcode_modifier == PCRel) && (insn_word & PCRel))
553f7cc78ecSespie 	{
554f7cc78ecSespie 	  address = (_pc + 1 + (short) address) - ((sym->section->vma + sym->value) / 4);
555f7cc78ecSespie 	  /* Check for delayed instruction, if so adjust destination.  */
556f7cc78ecSespie 	  if (insn_word & 0x00200000)
557f7cc78ecSespie 	    address += 2;
558f7cc78ecSespie 	}
559f7cc78ecSespie       else
560f7cc78ecSespie 	{
561f7cc78ecSespie 	  address -= ((sym->section->vma + sym->value) / 4);
562f7cc78ecSespie 	}
563f7cc78ecSespie       if (address == 0)
564f7cc78ecSespie 	info->fprintf_func (info->stream, " <%s>", sym->name);
565f7cc78ecSespie       else
566f7cc78ecSespie 	info->fprintf_func (info->stream, " <%s %c %d>", sym->name,
567f7cc78ecSespie 			    ((short) address < 0) ? '-' : '+',
568f7cc78ecSespie 			    abs (address));
569f7cc78ecSespie     }
570f7cc78ecSespie   return 1;
571f7cc78ecSespie }
572f7cc78ecSespie 
573f7cc78ecSespie int
get_indirect_operand(fragment,size,buffer)574f7cc78ecSespie get_indirect_operand (fragment, size, buffer)
575f7cc78ecSespie      unsigned short fragment;
576f7cc78ecSespie      int size;
577f7cc78ecSespie      char *buffer;
578f7cc78ecSespie {
579f7cc78ecSespie   unsigned char mod;
580f7cc78ecSespie   unsigned arnum;
581f7cc78ecSespie   unsigned char disp;
582f7cc78ecSespie 
583f7cc78ecSespie   if (buffer == NULL)
584f7cc78ecSespie     return 0;
5855f210c2aSfgsch   /* Determine which bits identify the sections of the indirect
5865f210c2aSfgsch      operand based on the size in bytes.  */
587f7cc78ecSespie   switch (size)
588f7cc78ecSespie     {
589f7cc78ecSespie     case 1:
590f7cc78ecSespie       mod = (fragment & 0x00F8) >> 3;
591f7cc78ecSespie       arnum = (fragment & 0x0007);
592f7cc78ecSespie       disp = 0;
593f7cc78ecSespie       break;
594f7cc78ecSespie     case 2:
595f7cc78ecSespie       mod = (fragment & 0xF800) >> 11;
596f7cc78ecSespie       arnum = (fragment & 0x0700) >> 8;
597f7cc78ecSespie       disp = (fragment & 0x00FF);
598f7cc78ecSespie       break;
599f7cc78ecSespie     default:
600f7cc78ecSespie       return 0;
601f7cc78ecSespie     }
602f7cc78ecSespie   {
603f7cc78ecSespie     const ind_addr_type *current_ind = tic30_indaddr_tab;
604f7cc78ecSespie     for (; current_ind < tic30_indaddrtab_end; current_ind++)
605f7cc78ecSespie       {
606f7cc78ecSespie 	if (current_ind->modfield == mod)
607f7cc78ecSespie 	  {
608f7cc78ecSespie 	    if (current_ind->displacement == IMPLIED_DISP && size == 2)
609f7cc78ecSespie 	      {
610f7cc78ecSespie 		continue;
611f7cc78ecSespie 	      }
612f7cc78ecSespie 	    else
613f7cc78ecSespie 	      {
614f7cc78ecSespie 		size_t i, len;
615f7cc78ecSespie 		int bufcnt;
616f7cc78ecSespie 
617f7cc78ecSespie 		len = strlen (current_ind->syntax);
618f7cc78ecSespie 		for (i = 0, bufcnt = 0; i < len; i++, bufcnt++)
619f7cc78ecSespie 		  {
620f7cc78ecSespie 		    buffer[bufcnt] = current_ind->syntax[i];
621f7cc78ecSespie 		    if (buffer[bufcnt - 1] == 'a' && buffer[bufcnt] == 'r')
622f7cc78ecSespie 		      buffer[++bufcnt] = arnum + '0';
6235f210c2aSfgsch 		    if (buffer[bufcnt] == '('
6245f210c2aSfgsch 			&& current_ind->displacement == DISP_REQUIRED)
625f7cc78ecSespie 		      {
626f7cc78ecSespie 			sprintf (&buffer[bufcnt + 1], "%u", disp);
627f7cc78ecSespie 			bufcnt += strlen (&buffer[bufcnt + 1]);
628f7cc78ecSespie 		      }
629f7cc78ecSespie 		  }
630f7cc78ecSespie 		buffer[bufcnt + 1] = '\0';
631f7cc78ecSespie 		break;
632f7cc78ecSespie 	      }
633f7cc78ecSespie 	  }
634f7cc78ecSespie       }
635f7cc78ecSespie   }
636f7cc78ecSespie   return 1;
637f7cc78ecSespie }
638f7cc78ecSespie 
639f7cc78ecSespie int
get_register_operand(fragment,buffer)640f7cc78ecSespie get_register_operand (fragment, buffer)
641f7cc78ecSespie      unsigned char fragment;
642f7cc78ecSespie      char *buffer;
643f7cc78ecSespie {
644f7cc78ecSespie   const reg *current_reg = tic30_regtab;
645f7cc78ecSespie 
646f7cc78ecSespie   if (buffer == NULL)
647f7cc78ecSespie     return 0;
648f7cc78ecSespie   for (; current_reg < tic30_regtab_end; current_reg++)
649f7cc78ecSespie     {
650f7cc78ecSespie       if ((fragment & 0x1F) == current_reg->opcode)
651f7cc78ecSespie 	{
652f7cc78ecSespie 	  strcpy (buffer, current_reg->name);
653f7cc78ecSespie 	  return 1;
654f7cc78ecSespie 	}
655f7cc78ecSespie     }
656f7cc78ecSespie   return 0;
657f7cc78ecSespie }
658f7cc78ecSespie 
659f7cc78ecSespie int
cnvt_tmsfloat_ieee(tmsfloat,size,ieeefloat)660f7cc78ecSespie cnvt_tmsfloat_ieee (tmsfloat, size, ieeefloat)
661f7cc78ecSespie      unsigned long tmsfloat;
662f7cc78ecSespie      int size;
663f7cc78ecSespie      float *ieeefloat;
664f7cc78ecSespie {
665f7cc78ecSespie   unsigned long exp, sign, mant;
666*d2201f2fSdrahn   union {
667*d2201f2fSdrahn     unsigned long l;
668*d2201f2fSdrahn     float f;
669*d2201f2fSdrahn   } val;
670f7cc78ecSespie 
671f7cc78ecSespie   if (size == 2)
672f7cc78ecSespie     {
673f7cc78ecSespie       if ((tmsfloat & 0x0000F000) == 0x00008000)
674f7cc78ecSespie 	tmsfloat = 0x80000000;
675f7cc78ecSespie       else
676f7cc78ecSespie 	{
677f7cc78ecSespie 	  tmsfloat <<= 16;
678f7cc78ecSespie 	  tmsfloat = (long) tmsfloat >> 4;
679f7cc78ecSespie 	}
680f7cc78ecSespie     }
681f7cc78ecSespie   exp = tmsfloat & 0xFF000000;
682f7cc78ecSespie   if (exp == 0x80000000)
683f7cc78ecSespie     {
684f7cc78ecSespie       *ieeefloat = 0.0;
685f7cc78ecSespie       return 1;
686f7cc78ecSespie     }
687f7cc78ecSespie   exp += 0x7F000000;
688f7cc78ecSespie   sign = (tmsfloat & 0x00800000) << 8;
689f7cc78ecSespie   mant = tmsfloat & 0x007FFFFF;
690f7cc78ecSespie   if (exp == 0xFF000000)
691f7cc78ecSespie     {
692f7cc78ecSespie       if (mant == 0)
693f7cc78ecSespie 	*ieeefloat = ERANGE;
694f7cc78ecSespie       if (sign == 0)
695f7cc78ecSespie 	*ieeefloat = 1.0 / 0.0;
696f7cc78ecSespie       else
697f7cc78ecSespie 	*ieeefloat = -1.0 / 0.0;
698f7cc78ecSespie       return 1;
699f7cc78ecSespie     }
700f7cc78ecSespie   exp >>= 1;
701f7cc78ecSespie   if (sign)
702f7cc78ecSespie     {
703f7cc78ecSespie       mant = (~mant) & 0x007FFFFF;
704f7cc78ecSespie       mant += 1;
705f7cc78ecSespie       exp += mant & 0x00800000;
706f7cc78ecSespie       exp &= 0x7F800000;
707f7cc78ecSespie       mant &= 0x007FFFFF;
708f7cc78ecSespie     }
709f7cc78ecSespie   if (tmsfloat == 0x80000000)
710f7cc78ecSespie     sign = mant = exp = 0;
711f7cc78ecSespie   tmsfloat = sign | exp | mant;
712*d2201f2fSdrahn   val.l = tmsfloat;
713*d2201f2fSdrahn   *ieeefloat = val.f;
714f7cc78ecSespie   return 1;
715f7cc78ecSespie }
716