1/* Disassemble ft32 instructions.
2   Copyright (C) 2013-2017 Free Software Foundation, Inc.
3   Contributed by FTDI (support@ftdichip.com)
4
5   This file is part of the GNU opcodes library.
6
7   This library is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3, or (at your option)
10   any later version.
11
12   It is distributed in the hope that it will be useful, but WITHOUT
13   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15   License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20   MA 02110-1301, USA.  */
21
22#include "sysdep.h"
23#include <stdio.h>
24#define STATIC_TABLE
25#define DEFINE_TABLE
26
27#include "opcode/ft32.h"
28#include "dis-asm.h"
29
30extern const ft32_opc_info_t ft32_opc_info[128];
31
32static fprintf_ftype fpr;
33static void *stream;
34
35int
36print_insn_ft32 (bfd_vma addr, struct disassemble_info *info)
37{
38  int status;
39  stream = info->stream;
40  bfd_byte buffer[4];
41  unsigned int iword;
42  const ft32_opc_info_t *oo;
43
44  fpr = info->fprintf_func;
45
46  if ((status = info->read_memory_func (addr, buffer, 4, info)))
47    goto fail;
48
49  iword = bfd_getl32 (buffer);
50
51  for (oo = ft32_opc_info; oo->name; oo++)
52    if ((iword & oo->mask) == oo->bits)
53      break;
54
55  if (oo->name)
56    {
57      int f = oo->fields;
58      int imm;
59
60      fpr (stream, "%08x %s", iword, oo->name);
61      if (oo->dw)
62        {
63          fpr (stream, ".%c ", "bsl"[(iword >> FT32_FLD_DW_BIT) & 3]);
64        }
65      else
66        {
67          fpr (stream, " ");
68        }
69
70      while (f)
71        {
72          int lobit = f & -f;
73          if (f & lobit)
74            {
75              switch (lobit)
76              {
77              case  FT32_FLD_CBCRCV:
78                // imm is {CB, CV}
79                imm = ((iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1)) << 4;
80                imm |= ((iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1));
81                switch (imm)
82                {
83                case 0x00: fpr(stream, "nz");  break;
84                case 0x01: fpr(stream, "z");   break;
85                case 0x10: fpr(stream, "ae");  break;
86                case 0x11: fpr(stream, "b");   break;
87                case 0x20: fpr(stream, "no");  break;
88                case 0x21: fpr(stream, "o");   break;
89                case 0x30: fpr(stream, "ns");  break;
90                case 0x31: fpr(stream, "s");   break;
91                case 0x40: fpr(stream, "lt");  break;
92                case 0x41: fpr(stream, "gte"); break;
93                case 0x50: fpr(stream, "lte"); break;
94                case 0x51: fpr(stream, "gt");  break;
95                case 0x60: fpr(stream, "be");  break;
96                case 0x61: fpr(stream, "a");   break;
97                default:   fpr(stream, "%d,$r30,%d", (imm >> 4), (imm & 1)); break;
98                }
99                break;
100              case  FT32_FLD_CB:
101                imm = (iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1);
102                fpr(stream, "%d", imm);
103                break;
104              case  FT32_FLD_R_D:
105                fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
106                break;
107              case  FT32_FLD_CR:
108                imm = (iword >> FT32_FLD_CR_BIT) & ((1 << FT32_FLD_CR_SIZ) - 1);
109                fpr(stream, "$r%d", 28 + imm);
110                break;
111              case  FT32_FLD_CV:
112                imm = (iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1);
113                fpr(stream, "%d", imm);
114                break;
115              case  FT32_FLD_R_1:
116                fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
117                break;
118              case  FT32_FLD_RIMM:
119                imm = (iword >> FT32_FLD_RIMM_BIT) & ((1 << FT32_FLD_RIMM_SIZ) - 1);
120                if (imm & 0x400)
121                  info->print_address_func ((bfd_vma) imm & 0x3ff, info);
122                else
123                  fpr(stream, "$r%d", imm & 0x1f);
124                break;
125              case  FT32_FLD_R_2:
126                fpr(stream, "$r%d", (iword >> FT32_FLD_R_2_BIT) & 0x1f);
127                break;
128              case  FT32_FLD_K20:
129                imm = iword & ((1 << FT32_FLD_K20_SIZ) - 1);
130                info->print_address_func ((bfd_vma) imm, info);
131                break;
132              case  FT32_FLD_PA:
133                imm = (iword & ((1 << FT32_FLD_PA_SIZ) - 1)) << 2;
134                info->print_address_func ((bfd_vma) imm, info);
135                break;
136              case  FT32_FLD_AA:
137                imm = iword & ((1 << FT32_FLD_AA_SIZ) - 1);
138                info->print_address_func ((bfd_vma) imm, info);
139                break;
140                break;
141              case  FT32_FLD_K16:
142                imm = iword & ((1 << FT32_FLD_K16_SIZ) - 1);
143                info->print_address_func ((bfd_vma) imm, info);
144                break;
145              case  FT32_FLD_K8:
146                imm = iword & ((1 << FT32_FLD_K8_SIZ) - 1);
147                info->print_address_func ((bfd_vma) imm, info);
148                break;
149                break;
150              case  FT32_FLD_R_D_POST:
151                fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
152                break;
153              case  FT32_FLD_R_1_POST:
154                fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
155                break;
156              default:
157                break;
158              }
159              f &= ~lobit;
160              if (f)
161                {
162                  fpr(stream, ",");
163                }
164            }
165        }
166    }
167    else
168    {
169      fpr (stream, "%08x!", iword);
170    }
171
172  return 4;
173
174 fail:
175  info->memory_error_func (status, addr, info);
176  return -1;
177}
178