Fix typo.
[external/binutils.git] / opcodes / h8300-dis.c
1 /* Disassemble h8300 instructions.
2    Copyright (C) 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
17
18 #define DEFINE_TABLE
19
20 #define h8_opcodes h8ops
21 #include "opcode/h8300.h"
22 #include "dis-asm.h"
23
24
25 /* Run through the opcodes and sort them into order to make them easy
26    to disassemble
27  */
28 static void
29 bfd_h8_disassemble_init ()
30 {
31   unsigned int i;
32
33
34   struct h8_opcode *p;
35
36   for (p = h8_opcodes; p->name; p++)
37     {
38       int n1 = 0;
39       int n2 = 0;
40
41       if ((int) p->data.nib[0] < 16)
42         {
43           n1 = (int) p->data.nib[0];
44         }
45       else
46         n1 = 0;
47       if ((int) p->data.nib[1] < 16)
48         {
49           n2 = (int) p->data.nib[1];
50         }
51       else
52         n2 = 0;
53
54       /* Just make sure there are an even number of nibbles in it, and
55          that the count is the same s the length */
56       for (i = 0; p->data.nib[i] != E; i++)
57         /*EMPTY*/ ;
58       if (i & 1)
59         abort ();
60       p->length = i / 2;
61     }
62
63 }
64
65
66 unsigned int
67 bfd_h8_disassemble (addr, info, hmode)
68      bfd_vma addr;
69      disassemble_info *info;
70      int hmode;
71 {
72   /* Find the first entry in the table for this opcode */
73   static CONST char *regnames[] =
74     {
75       "r0h", "r1h", "r2h", "r3h", "r4h", "r5h", "r6h", "r7h",
76       "r0l", "r1l", "r2l", "r3l", "r4l", "r5l", "r6l", "r7l"};
77   
78   static CONST char *wregnames[] =
79     {
80       "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
81       "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7"
82       };
83   
84   static CONST char *lregnames[] =
85     {
86       "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7",
87       "er0", "er1", "er2", "er3", "er4", "er5", "er6", "er7"
88       }
89   ;
90
91   int rs = 0;
92   int rd = 0;
93   int rdisp = 0;
94   int abs = 0;
95   int plen = 0;
96   static boolean init;  
97   struct h8_opcode *q = h8_opcodes;
98   char CONST **pregnames = hmode ? lregnames : wregnames;
99   int status;
100   unsigned char data[20];  
101   void *stream = info->stream;
102   fprintf_ftype fprintf = info->fprintf_func;
103
104   if (!init)
105     {
106       bfd_h8_disassemble_init ();
107       init = true;
108     }
109
110   status = info->read_memory_func(addr, data, 2, info);
111   if (status != 0) 
112     {
113       info->memory_error_func(status, addr, info);
114       return -1;
115     }
116   status = info->read_memory_func(addr, data+2, 8, info);
117
118   
119
120   /* Find the exact opcode/arg combo */
121   while (q->name)
122     {
123       op_type *nib;
124       unsigned int len = 0;
125
126       nib = q->data.nib;
127       
128       while (1)
129         {
130           op_type looking_for = *nib;
131           int thisnib = data[len >> 1];
132           
133           thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
134           
135           if (looking_for < 16) 
136             {
137               
138               if (looking_for != thisnib) 
139                 goto fail;
140             }
141           
142           else 
143             {
144               
145               if ((int) looking_for & (int) B31)
146                 {
147                   if (! (((int) thisnib & 0x8) != 0)) 
148                     goto fail;
149                   looking_for = (op_type) ((int) looking_for & ~(int) B31);
150                 }
151               if ((int) looking_for & (int) B30)
152                 {
153                   if (!(((int) thisnib & 0x8) == 0)) 
154                     goto fail;
155                   looking_for = (op_type) ((int) looking_for & ~(int) B30);
156                 }
157
158               if (looking_for & DBIT)
159                 {
160                   if ((looking_for & 5) != (thisnib &5)) goto fail;
161                   abs = (thisnib & 0x8) ? 2 : 1;
162                 }                 
163               
164               else  if (looking_for & (REG | IND|INC|DEC))
165                 {
166                   if (looking_for & SRC)
167                     {
168                       rs = thisnib;
169                     }
170                   else
171                     {
172                       rd = thisnib;
173                     }
174                 }
175               else if (looking_for & L_16)
176                 {
177                   abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
178                   plen = 16;
179               
180                 }
181               else if(looking_for & ABSJMP)
182                 {
183                   abs =
184                     (data[1] << 16)
185                       | (data[2] << 8)
186                         | (data[3]);
187                 }
188               
189               else if (looking_for & L_32)
190                 {
191                   int i = len >> 1;
192                   abs = (data[i] << 24)
193                     | (data[i + 1] << 16)
194                       | (data[i + 2] << 8)
195                         | (data[i+ 3]);
196
197                   plen =32;
198               
199                 }
200               else if (looking_for & L_24)
201                 {
202                   int i = len >> 1;
203                   abs = (data[i] << 16) | (data[i + 1] << 8)|  (data[i+
204                                                                      2]);
205                   plen =24;
206                 }
207               else if (looking_for & IGNORE)
208                 {
209                   
210                 }
211               else if (looking_for & DISPREG)
212                 {
213                   rdisp = thisnib;
214                 }
215               else if (looking_for & KBIT)
216                 {
217                   switch (thisnib) 
218                     {
219                     case 9:
220                       abs = 4;
221                       break;
222                     case 8:
223                       abs = 2;
224                       break;
225                     case 0:
226                       abs = 1;
227                       break;
228                     }
229                 }
230               else if (looking_for & L_8)
231                 {
232                   plen = 8;               
233                   abs = data[len >> 1];
234                 }
235               else if (looking_for & L_3)
236                 {
237                   plen = 3;
238               
239                   abs = thisnib;
240                 }
241               else if (looking_for == E)
242                 {
243
244                   {
245                     int i;
246
247                     for (i = 0; i < q->length; i++)
248                       {
249                         fprintf (stream, "%02x ", data[i]);
250                       }
251                     for (; i < 6; i++)
252                       {
253                         fprintf (stream, "   ");
254                       }
255                   }
256                   fprintf (stream, "%s\t", q->name);
257                   /* Fill in the args */
258                   {
259                     op_type *args = q->args.nib;
260                     int hadone = 0;
261
262
263                     while (*args != E)
264                       {
265                         int x = *args;
266                         if (hadone)
267                           fprintf (stream, ",");
268
269
270                         if (x & (IMM|KBIT|DBIT))
271                           {
272                         
273                             fprintf (stream, "#0x%x", (unsigned) abs);
274                           }
275                         else if (x & REG)
276                           {
277                             int rn = (x & DST) ? rd : rs;
278                             switch (x & SIZE)
279                               {
280                               case L_8:
281                                 fprintf (stream, "%s", regnames[rn]);
282                                 break;
283                               case L_16:
284                                 fprintf (stream, "%s", wregnames[rn]);
285                                 break;
286                               case L_P:
287                               case L_32:
288                                 fprintf (stream, "%s", lregnames[rn]);
289                                 break;
290                     
291                               }
292                           }
293
294                         else if (x & INC)
295                           {
296                             fprintf (stream, "@%s+", pregnames[rs]);
297                           }
298                         else if (x & DEC)
299                           {
300                             fprintf (stream, "@-%s", pregnames[rd]);
301                           }
302
303                         else if (x & IND)
304                           {
305                             int rn = (x & DST) ? rd : rs;
306                             fprintf (stream, "@%s", pregnames[rn]);
307                           }
308
309                         else if (x & (ABS|ABSJMP|ABSMOV))
310                           {
311                             fprintf (stream, "@0x%x:%d", (unsigned) abs, plen);
312                           }
313
314                         else if (x & MEMIND)
315                           {
316                             fprintf (stream, "@@%d (%x)", abs, abs);
317                           }
318
319                         else if (x & PCREL)
320                           {
321                             fprintf (stream, ".%s%d (%x)", (char) abs > 0 ? "+" : "", (char) abs,
322                                      addr + (char) abs + 2);
323                           }
324                         else if (x & DISP)
325                           {
326                             fprintf (stream, "@(0x%x:%d,%s)", abs,plen, pregnames[rdisp]);
327                           }
328
329                         else if (x & CCR)
330                           {
331
332                             fprintf (stream, "ccr");
333                           }
334
335                         else
336                           fprintf (stream, "Hmmmm %x", x);
337                         hadone = 1;
338                         args++;
339                       }
340                   }
341                   return q->length;
342                 }
343
344       
345               else
346                 {
347                   fprintf (stream, "Dont understand %x \n", looking_for);
348                 }
349             }
350           
351           len++;
352           nib++;
353         }
354       
355     fail:
356       q++;
357     }
358
359   /* Fell of the end */
360   fprintf (stream, "%02x %02x        .word\tH'%x,H'%x",
361            data[0], data[1],
362            data[0], data[1]);
363   return 2;
364 }
365
366 int 
367 print_insn_h8300 (addr, info)
368 bfd_vma addr; 
369 disassemble_info *info;
370 {
371   return bfd_h8_disassemble (addr, info , 0);
372 }
373
374  int 
375 print_insn_h8300h (addr, info)
376 bfd_vma addr;
377 disassemble_info *info;
378 {
379   return bfd_h8_disassemble (addr, info , 1);
380 }
381