175fd0b74Schristos /* Disassemble Xilinx microblaze instructions.
275fd0b74Schristos
3*e992f068Schristos Copyright (C) 2009-2022 Free Software Foundation, Inc.
475fd0b74Schristos
575fd0b74Schristos This file is part of the GNU opcodes library.
675fd0b74Schristos
775fd0b74Schristos This library is free software; you can redistribute it and/or modify
875fd0b74Schristos it under the terms of the GNU General Public License as published by
975fd0b74Schristos the Free Software Foundation; either version 3, or (at your option)
1075fd0b74Schristos any later version.
1175fd0b74Schristos
1275fd0b74Schristos It is distributed in the hope that it will be useful, but WITHOUT
1375fd0b74Schristos ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1475fd0b74Schristos or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1575fd0b74Schristos License for more details.
1675fd0b74Schristos
1775fd0b74Schristos You should have received a copy of the GNU General Public License
1875fd0b74Schristos along with this file; see the file COPYING. If not, write to the
1975fd0b74Schristos Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
2075fd0b74Schristos MA 02110-1301, USA. */
2175fd0b74Schristos
2275fd0b74Schristos
2375fd0b74Schristos #include "sysdep.h"
2475fd0b74Schristos #define STATIC_TABLE
2575fd0b74Schristos #define DEFINE_TABLE
2675fd0b74Schristos
27ede78133Schristos #include "disassemble.h"
2875fd0b74Schristos #include <strings.h>
2975fd0b74Schristos #include "microblaze-opc.h"
3075fd0b74Schristos #include "microblaze-dis.h"
3175fd0b74Schristos
32012573ebSchristos #define get_field_rd(buf, instr) get_field (buf, instr, RD_MASK, RD_LOW)
33012573ebSchristos #define get_field_r1(buf, instr) get_field (buf, instr, RA_MASK, RA_LOW)
34012573ebSchristos #define get_field_r2(buf, instr) get_field (buf, instr, RB_MASK, RB_LOW)
3575fd0b74Schristos #define get_int_field_imm(instr) ((instr & IMM_MASK) >> IMM_LOW)
3675fd0b74Schristos #define get_int_field_r1(instr) ((instr & RA_MASK) >> RA_LOW)
3775fd0b74Schristos
38012573ebSchristos #define NUM_STRBUFS 3
39012573ebSchristos #define STRBUF_SIZE 25
4075fd0b74Schristos
41012573ebSchristos struct string_buf
4275fd0b74Schristos {
43012573ebSchristos unsigned int which;
44012573ebSchristos char str[NUM_STRBUFS][STRBUF_SIZE];
45012573ebSchristos };
4675fd0b74Schristos
47012573ebSchristos static inline char *
strbuf(struct string_buf * buf)48012573ebSchristos strbuf (struct string_buf *buf)
49012573ebSchristos {
50012573ebSchristos #ifdef ENABLE_CHECKING
51012573ebSchristos if (buf->which >= NUM_STRBUFS)
52012573ebSchristos abort ();
53012573ebSchristos #endif
54012573ebSchristos return buf->str[buf->which++];
5575fd0b74Schristos }
5675fd0b74Schristos
5775fd0b74Schristos static char *
get_field(struct string_buf * buf,long instr,long mask,unsigned short low)58012573ebSchristos get_field (struct string_buf *buf, long instr, long mask, unsigned short low)
5975fd0b74Schristos {
60012573ebSchristos char *p = strbuf (buf);
6175fd0b74Schristos
62012573ebSchristos sprintf (p, "%s%d", register_prefix, (int)((instr & mask) >> low));
63012573ebSchristos return p;
6475fd0b74Schristos }
6575fd0b74Schristos
6675fd0b74Schristos static char *
get_field_imm(struct string_buf * buf,long instr)67012573ebSchristos get_field_imm (struct string_buf *buf, long instr)
6875fd0b74Schristos {
69012573ebSchristos char *p = strbuf (buf);
7075fd0b74Schristos
71012573ebSchristos sprintf (p, "%d", (short)((instr & IMM_MASK) >> IMM_LOW));
72012573ebSchristos return p;
7375fd0b74Schristos }
7475fd0b74Schristos
7575fd0b74Schristos static char *
get_field_imm5(struct string_buf * buf,long instr)76012573ebSchristos get_field_imm5 (struct string_buf *buf, long instr)
7775fd0b74Schristos {
78012573ebSchristos char *p = strbuf (buf);
7975fd0b74Schristos
80012573ebSchristos sprintf (p, "%d", (short)((instr & IMM5_MASK) >> IMM_LOW));
81012573ebSchristos return p;
8275fd0b74Schristos }
8375fd0b74Schristos
8475fd0b74Schristos static char *
get_field_imm5_mbar(struct string_buf * buf,long instr)85012573ebSchristos get_field_imm5_mbar (struct string_buf *buf, long instr)
8675fd0b74Schristos {
87012573ebSchristos char *p = strbuf (buf);
8875fd0b74Schristos
89012573ebSchristos sprintf (p, "%d", (short)((instr & IMM5_MBAR_MASK) >> IMM_MBAR));
90012573ebSchristos return p;
91012573ebSchristos }
92012573ebSchristos
93012573ebSchristos static char *
get_field_rfsl(struct string_buf * buf,long instr)94012573ebSchristos get_field_rfsl (struct string_buf *buf, long instr)
95012573ebSchristos {
96012573ebSchristos char *p = strbuf (buf);
97012573ebSchristos
98012573ebSchristos sprintf (p, "%s%d", fsl_register_prefix,
9975fd0b74Schristos (short)((instr & RFSL_MASK) >> IMM_LOW));
100012573ebSchristos return p;
10175fd0b74Schristos }
10275fd0b74Schristos
10375fd0b74Schristos static char *
get_field_imm15(struct string_buf * buf,long instr)104012573ebSchristos get_field_imm15 (struct string_buf *buf, long instr)
10575fd0b74Schristos {
106012573ebSchristos char *p = strbuf (buf);
10775fd0b74Schristos
108012573ebSchristos sprintf (p, "%d", (short)((instr & IMM15_MASK) >> IMM_LOW));
109012573ebSchristos return p;
11075fd0b74Schristos }
11175fd0b74Schristos
11275fd0b74Schristos static char *
get_field_special(struct string_buf * buf,long instr,const struct op_code_struct * op)113012573ebSchristos get_field_special (struct string_buf *buf, long instr,
114*e992f068Schristos const struct op_code_struct *op)
11575fd0b74Schristos {
116012573ebSchristos char *p = strbuf (buf);
117012573ebSchristos char *spr;
11875fd0b74Schristos
11975fd0b74Schristos switch ((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask))
12075fd0b74Schristos {
12175fd0b74Schristos case REG_MSR_MASK :
122012573ebSchristos spr = "msr";
12375fd0b74Schristos break;
12475fd0b74Schristos case REG_PC_MASK :
125012573ebSchristos spr = "pc";
12675fd0b74Schristos break;
12775fd0b74Schristos case REG_EAR_MASK :
128012573ebSchristos spr = "ear";
12975fd0b74Schristos break;
13075fd0b74Schristos case REG_ESR_MASK :
131012573ebSchristos spr = "esr";
13275fd0b74Schristos break;
13375fd0b74Schristos case REG_FSR_MASK :
134012573ebSchristos spr = "fsr";
13575fd0b74Schristos break;
13675fd0b74Schristos case REG_BTR_MASK :
137012573ebSchristos spr = "btr";
13875fd0b74Schristos break;
13975fd0b74Schristos case REG_EDR_MASK :
140012573ebSchristos spr = "edr";
14175fd0b74Schristos break;
14275fd0b74Schristos case REG_PID_MASK :
143012573ebSchristos spr = "pid";
14475fd0b74Schristos break;
14575fd0b74Schristos case REG_ZPR_MASK :
146012573ebSchristos spr = "zpr";
14775fd0b74Schristos break;
14875fd0b74Schristos case REG_TLBX_MASK :
149012573ebSchristos spr = "tlbx";
15075fd0b74Schristos break;
15175fd0b74Schristos case REG_TLBLO_MASK :
152012573ebSchristos spr = "tlblo";
15375fd0b74Schristos break;
15475fd0b74Schristos case REG_TLBHI_MASK :
155012573ebSchristos spr = "tlbhi";
15675fd0b74Schristos break;
15775fd0b74Schristos case REG_TLBSX_MASK :
158012573ebSchristos spr = "tlbsx";
15975fd0b74Schristos break;
16075fd0b74Schristos case REG_SHR_MASK :
161012573ebSchristos spr = "shr";
16275fd0b74Schristos break;
16375fd0b74Schristos case REG_SLR_MASK :
164012573ebSchristos spr = "slr";
16575fd0b74Schristos break;
16675fd0b74Schristos default :
16775fd0b74Schristos if (((((instr & IMM_MASK) >> IMM_LOW) ^ op->immval_mask) & 0xE000)
16875fd0b74Schristos == REG_PVR_MASK)
16975fd0b74Schristos {
170012573ebSchristos sprintf (p, "%spvr%d", register_prefix,
17175fd0b74Schristos (unsigned short)(((instr & IMM_MASK) >> IMM_LOW)
17275fd0b74Schristos ^ op->immval_mask) ^ REG_PVR_MASK);
173012573ebSchristos return p;
17475fd0b74Schristos }
17575fd0b74Schristos else
176012573ebSchristos spr = "pc";
17775fd0b74Schristos break;
17875fd0b74Schristos }
17975fd0b74Schristos
180012573ebSchristos sprintf (p, "%s%s", register_prefix, spr);
181012573ebSchristos return p;
18275fd0b74Schristos }
18375fd0b74Schristos
18475fd0b74Schristos static unsigned long
read_insn_microblaze(bfd_vma memaddr,struct disassemble_info * info,const struct op_code_struct ** opr)18575fd0b74Schristos read_insn_microblaze (bfd_vma memaddr,
18675fd0b74Schristos struct disassemble_info *info,
187*e992f068Schristos const struct op_code_struct **opr)
18875fd0b74Schristos {
18975fd0b74Schristos unsigned char ibytes[4];
19075fd0b74Schristos int status;
191*e992f068Schristos const struct op_code_struct *op;
19275fd0b74Schristos unsigned long inst;
19375fd0b74Schristos
19475fd0b74Schristos status = info->read_memory_func (memaddr, ibytes, 4, info);
19575fd0b74Schristos
19675fd0b74Schristos if (status != 0)
19775fd0b74Schristos {
19875fd0b74Schristos info->memory_error_func (status, memaddr, info);
19975fd0b74Schristos return 0;
20075fd0b74Schristos }
20175fd0b74Schristos
20275fd0b74Schristos if (info->endian == BFD_ENDIAN_BIG)
203012573ebSchristos inst = (((unsigned) ibytes[0] << 24) | (ibytes[1] << 16)
204012573ebSchristos | (ibytes[2] << 8) | ibytes[3]);
20575fd0b74Schristos else if (info->endian == BFD_ENDIAN_LITTLE)
206012573ebSchristos inst = (((unsigned) ibytes[3] << 24) | (ibytes[2] << 16)
207012573ebSchristos | (ibytes[1] << 8) | ibytes[0]);
20875fd0b74Schristos else
20975fd0b74Schristos abort ();
21075fd0b74Schristos
21175fd0b74Schristos /* Just a linear search of the table. */
212*e992f068Schristos for (op = microblaze_opcodes; op->name != 0; op ++)
21375fd0b74Schristos if (op->bit_sequence == (inst & op->opcode_mask))
21475fd0b74Schristos break;
21575fd0b74Schristos
21675fd0b74Schristos *opr = op;
21775fd0b74Schristos return inst;
21875fd0b74Schristos }
21975fd0b74Schristos
22075fd0b74Schristos
22175fd0b74Schristos int
print_insn_microblaze(bfd_vma memaddr,struct disassemble_info * info)22275fd0b74Schristos print_insn_microblaze (bfd_vma memaddr, struct disassemble_info * info)
22375fd0b74Schristos {
22475fd0b74Schristos fprintf_ftype print_func = info->fprintf_func;
22575fd0b74Schristos void *stream = info->stream;
22675fd0b74Schristos unsigned long inst, prev_inst;
227*e992f068Schristos const struct op_code_struct *op, *pop;
22875fd0b74Schristos int immval = 0;
229*e992f068Schristos bool immfound = false;
23075fd0b74Schristos static bfd_vma prev_insn_addr = -1; /* Init the prev insn addr. */
23175fd0b74Schristos static int prev_insn_vma = -1; /* Init the prev insn vma. */
23275fd0b74Schristos int curr_insn_vma = info->buffer_vma;
233012573ebSchristos struct string_buf buf;
23475fd0b74Schristos
235012573ebSchristos buf.which = 0;
23675fd0b74Schristos info->bytes_per_chunk = 4;
23775fd0b74Schristos
23875fd0b74Schristos inst = read_insn_microblaze (memaddr, info, &op);
23975fd0b74Schristos if (inst == 0)
24075fd0b74Schristos return -1;
24175fd0b74Schristos
24275fd0b74Schristos if (prev_insn_vma == curr_insn_vma)
24375fd0b74Schristos {
24475fd0b74Schristos if (memaddr-(info->bytes_per_chunk) == prev_insn_addr)
24575fd0b74Schristos {
24675fd0b74Schristos prev_inst = read_insn_microblaze (prev_insn_addr, info, &pop);
24775fd0b74Schristos if (prev_inst == 0)
24875fd0b74Schristos return -1;
24975fd0b74Schristos if (pop->instr == imm)
25075fd0b74Schristos {
25175fd0b74Schristos immval = (get_int_field_imm (prev_inst) << 16) & 0xffff0000;
252*e992f068Schristos immfound = true;
25375fd0b74Schristos }
25475fd0b74Schristos else
25575fd0b74Schristos {
25675fd0b74Schristos immval = 0;
257*e992f068Schristos immfound = false;
25875fd0b74Schristos }
25975fd0b74Schristos }
26075fd0b74Schristos }
26175fd0b74Schristos
26275fd0b74Schristos /* Make curr insn as prev insn. */
26375fd0b74Schristos prev_insn_addr = memaddr;
26475fd0b74Schristos prev_insn_vma = curr_insn_vma;
26575fd0b74Schristos
26675fd0b74Schristos if (op->name == NULL)
26775fd0b74Schristos print_func (stream, ".short 0x%04x", (unsigned int) inst);
26875fd0b74Schristos else
26975fd0b74Schristos {
27075fd0b74Schristos print_func (stream, "%s", op->name);
27175fd0b74Schristos
27275fd0b74Schristos switch (op->inst_type)
27375fd0b74Schristos {
27475fd0b74Schristos case INST_TYPE_RD_R1_R2:
275012573ebSchristos print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
276012573ebSchristos get_field_r1 (&buf, inst), get_field_r2 (&buf, inst));
27775fd0b74Schristos break;
27875fd0b74Schristos case INST_TYPE_RD_R1_IMM:
279012573ebSchristos print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
280012573ebSchristos get_field_r1 (&buf, inst), get_field_imm (&buf, inst));
28175fd0b74Schristos if (info->print_address_func && get_int_field_r1 (inst) == 0
28275fd0b74Schristos && info->symbol_at_address_func)
28375fd0b74Schristos {
28475fd0b74Schristos if (immfound)
28575fd0b74Schristos immval |= (get_int_field_imm (inst) & 0x0000ffff);
28675fd0b74Schristos else
28775fd0b74Schristos {
28875fd0b74Schristos immval = get_int_field_imm (inst);
28975fd0b74Schristos if (immval & 0x8000)
29075fd0b74Schristos immval |= 0xFFFF0000;
29175fd0b74Schristos }
29275fd0b74Schristos if (immval > 0 && info->symbol_at_address_func (immval, info))
29375fd0b74Schristos {
29475fd0b74Schristos print_func (stream, "\t// ");
29575fd0b74Schristos info->print_address_func (immval, info);
29675fd0b74Schristos }
29775fd0b74Schristos }
29875fd0b74Schristos break;
29975fd0b74Schristos case INST_TYPE_RD_R1_IMM5:
300012573ebSchristos print_func (stream, "\t%s, %s, %s", get_field_rd (&buf, inst),
301012573ebSchristos get_field_r1 (&buf, inst), get_field_imm5 (&buf, inst));
30275fd0b74Schristos break;
30375fd0b74Schristos case INST_TYPE_RD_RFSL:
304012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
305012573ebSchristos get_field_rfsl (&buf, inst));
30675fd0b74Schristos break;
30775fd0b74Schristos case INST_TYPE_R1_RFSL:
308012573ebSchristos print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
309012573ebSchristos get_field_rfsl (&buf, inst));
31075fd0b74Schristos break;
31175fd0b74Schristos case INST_TYPE_RD_SPECIAL:
312012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
313012573ebSchristos get_field_special (&buf, inst, op));
31475fd0b74Schristos break;
31575fd0b74Schristos case INST_TYPE_SPECIAL_R1:
316012573ebSchristos print_func (stream, "\t%s, %s", get_field_special (&buf, inst, op),
317012573ebSchristos get_field_r1 (&buf, inst));
31875fd0b74Schristos break;
31975fd0b74Schristos case INST_TYPE_RD_R1:
320012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
321012573ebSchristos get_field_r1 (&buf, inst));
32275fd0b74Schristos break;
32375fd0b74Schristos case INST_TYPE_R1_R2:
324012573ebSchristos print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
325012573ebSchristos get_field_r2 (&buf, inst));
32675fd0b74Schristos break;
32775fd0b74Schristos case INST_TYPE_R1_IMM:
328012573ebSchristos print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
329012573ebSchristos get_field_imm (&buf, inst));
33075fd0b74Schristos /* The non-pc relative instructions are returns, which shouldn't
33175fd0b74Schristos have a label printed. */
33275fd0b74Schristos if (info->print_address_func && op->inst_offset_type == INST_PC_OFFSET
33375fd0b74Schristos && info->symbol_at_address_func)
33475fd0b74Schristos {
33575fd0b74Schristos if (immfound)
33675fd0b74Schristos immval |= (get_int_field_imm (inst) & 0x0000ffff);
33775fd0b74Schristos else
33875fd0b74Schristos {
33975fd0b74Schristos immval = get_int_field_imm (inst);
34075fd0b74Schristos if (immval & 0x8000)
34175fd0b74Schristos immval |= 0xFFFF0000;
34275fd0b74Schristos }
34375fd0b74Schristos immval += memaddr;
34475fd0b74Schristos if (immval > 0 && info->symbol_at_address_func (immval, info))
34575fd0b74Schristos {
34675fd0b74Schristos print_func (stream, "\t// ");
34775fd0b74Schristos info->print_address_func (immval, info);
34875fd0b74Schristos }
34975fd0b74Schristos else
35075fd0b74Schristos {
35175fd0b74Schristos print_func (stream, "\t\t// ");
35275fd0b74Schristos print_func (stream, "%x", immval);
35375fd0b74Schristos }
35475fd0b74Schristos }
35575fd0b74Schristos break;
35675fd0b74Schristos case INST_TYPE_RD_IMM:
357012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
358012573ebSchristos get_field_imm (&buf, inst));
35975fd0b74Schristos if (info->print_address_func && info->symbol_at_address_func)
36075fd0b74Schristos {
36175fd0b74Schristos if (immfound)
36275fd0b74Schristos immval |= (get_int_field_imm (inst) & 0x0000ffff);
36375fd0b74Schristos else
36475fd0b74Schristos {
36575fd0b74Schristos immval = get_int_field_imm (inst);
36675fd0b74Schristos if (immval & 0x8000)
36775fd0b74Schristos immval |= 0xFFFF0000;
36875fd0b74Schristos }
36975fd0b74Schristos if (op->inst_offset_type == INST_PC_OFFSET)
37075fd0b74Schristos immval += (int) memaddr;
37175fd0b74Schristos if (info->symbol_at_address_func (immval, info))
37275fd0b74Schristos {
37375fd0b74Schristos print_func (stream, "\t// ");
37475fd0b74Schristos info->print_address_func (immval, info);
37575fd0b74Schristos }
37675fd0b74Schristos }
37775fd0b74Schristos break;
37875fd0b74Schristos case INST_TYPE_IMM:
379012573ebSchristos print_func (stream, "\t%s", get_field_imm (&buf, inst));
38075fd0b74Schristos if (info->print_address_func && info->symbol_at_address_func
38175fd0b74Schristos && op->instr != imm)
38275fd0b74Schristos {
38375fd0b74Schristos if (immfound)
38475fd0b74Schristos immval |= (get_int_field_imm (inst) & 0x0000ffff);
38575fd0b74Schristos else
38675fd0b74Schristos {
38775fd0b74Schristos immval = get_int_field_imm (inst);
38875fd0b74Schristos if (immval & 0x8000)
38975fd0b74Schristos immval |= 0xFFFF0000;
39075fd0b74Schristos }
39175fd0b74Schristos if (op->inst_offset_type == INST_PC_OFFSET)
39275fd0b74Schristos immval += (int) memaddr;
39375fd0b74Schristos if (immval > 0 && info->symbol_at_address_func (immval, info))
39475fd0b74Schristos {
39575fd0b74Schristos print_func (stream, "\t// ");
39675fd0b74Schristos info->print_address_func (immval, info);
39775fd0b74Schristos }
39875fd0b74Schristos else if (op->inst_offset_type == INST_PC_OFFSET)
39975fd0b74Schristos {
40075fd0b74Schristos print_func (stream, "\t\t// ");
40175fd0b74Schristos print_func (stream, "%x", immval);
40275fd0b74Schristos }
40375fd0b74Schristos }
40475fd0b74Schristos break;
40575fd0b74Schristos case INST_TYPE_RD_R2:
406012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
407012573ebSchristos get_field_r2 (&buf, inst));
40875fd0b74Schristos break;
40975fd0b74Schristos case INST_TYPE_R2:
410012573ebSchristos print_func (stream, "\t%s", get_field_r2 (&buf, inst));
41175fd0b74Schristos break;
41275fd0b74Schristos case INST_TYPE_R1:
413012573ebSchristos print_func (stream, "\t%s", get_field_r1 (&buf, inst));
41475fd0b74Schristos break;
41575fd0b74Schristos case INST_TYPE_R1_R2_SPECIAL:
416012573ebSchristos print_func (stream, "\t%s, %s", get_field_r1 (&buf, inst),
417012573ebSchristos get_field_r2 (&buf, inst));
41875fd0b74Schristos break;
41975fd0b74Schristos case INST_TYPE_RD_IMM15:
420012573ebSchristos print_func (stream, "\t%s, %s", get_field_rd (&buf, inst),
421012573ebSchristos get_field_imm15 (&buf, inst));
42275fd0b74Schristos break;
42375fd0b74Schristos /* For mbar insn. */
42475fd0b74Schristos case INST_TYPE_IMM5:
425012573ebSchristos print_func (stream, "\t%s", get_field_imm5_mbar (&buf, inst));
42675fd0b74Schristos break;
42775fd0b74Schristos /* For mbar 16 or sleep insn. */
42875fd0b74Schristos case INST_TYPE_NONE:
42975fd0b74Schristos break;
43075fd0b74Schristos /* For tuqula instruction */
43175fd0b74Schristos case INST_TYPE_RD:
432012573ebSchristos print_func (stream, "\t%s", get_field_rd (&buf, inst));
43375fd0b74Schristos break;
43475fd0b74Schristos case INST_TYPE_RFSL:
435012573ebSchristos print_func (stream, "\t%s", get_field_rfsl (&buf, inst));
43675fd0b74Schristos break;
43775fd0b74Schristos default:
43875fd0b74Schristos /* If the disassembler lags the instruction set. */
439012573ebSchristos print_func (stream, "\tundecoded operands, inst is 0x%04x",
440012573ebSchristos (unsigned int) inst);
44175fd0b74Schristos break;
44275fd0b74Schristos }
44375fd0b74Schristos }
44475fd0b74Schristos
44575fd0b74Schristos /* Say how many bytes we consumed. */
44675fd0b74Schristos return 4;
44775fd0b74Schristos }
44875fd0b74Schristos
44975fd0b74Schristos enum microblaze_instr
get_insn_microblaze(long inst,bool * isunsignedimm,enum microblaze_instr_type * insn_type,short * delay_slots)45075fd0b74Schristos get_insn_microblaze (long inst,
451*e992f068Schristos bool *isunsignedimm,
45275fd0b74Schristos enum microblaze_instr_type *insn_type,
45375fd0b74Schristos short *delay_slots)
45475fd0b74Schristos {
455*e992f068Schristos const struct op_code_struct *op;
456*e992f068Schristos *isunsignedimm = false;
45775fd0b74Schristos
45875fd0b74Schristos /* Just a linear search of the table. */
459*e992f068Schristos for (op = microblaze_opcodes; op->name != 0; op ++)
46075fd0b74Schristos if (op->bit_sequence == (inst & op->opcode_mask))
46175fd0b74Schristos break;
46275fd0b74Schristos
46375fd0b74Schristos if (op->name == 0)
46475fd0b74Schristos return invalid_inst;
46575fd0b74Schristos else
46675fd0b74Schristos {
46775fd0b74Schristos *isunsignedimm = (op->inst_type == INST_TYPE_RD_R1_UNSIGNED_IMM);
46875fd0b74Schristos *insn_type = op->instr_type;
46975fd0b74Schristos *delay_slots = op->delay_slots;
47075fd0b74Schristos return op->instr;
47175fd0b74Schristos }
47275fd0b74Schristos }
47375fd0b74Schristos
47475fd0b74Schristos enum microblaze_instr
microblaze_decode_insn(long insn,int * rd,int * ra,int * rb,int * immed)47575fd0b74Schristos microblaze_decode_insn (long insn, int *rd, int *ra, int *rb, int *immed)
47675fd0b74Schristos {
47775fd0b74Schristos enum microblaze_instr op;
478*e992f068Schristos bool t1;
47975fd0b74Schristos enum microblaze_instr_type t2;
48075fd0b74Schristos short t3;
48175fd0b74Schristos
48275fd0b74Schristos op = get_insn_microblaze (insn, &t1, &t2, &t3);
48375fd0b74Schristos *rd = (insn & RD_MASK) >> RD_LOW;
48475fd0b74Schristos *ra = (insn & RA_MASK) >> RA_LOW;
48575fd0b74Schristos *rb = (insn & RB_MASK) >> RB_LOW;
48675fd0b74Schristos t3 = (insn & IMM_MASK) >> IMM_LOW;
48775fd0b74Schristos *immed = (int) t3;
48875fd0b74Schristos return (op);
48975fd0b74Schristos }
49075fd0b74Schristos
49175fd0b74Schristos unsigned long
microblaze_get_target_address(long inst,bool immfound,int immval,long pcval,long r1val,long r2val,bool * targetvalid,bool * unconditionalbranch)492*e992f068Schristos microblaze_get_target_address (long inst, bool immfound, int immval,
49375fd0b74Schristos long pcval, long r1val, long r2val,
494*e992f068Schristos bool *targetvalid,
495*e992f068Schristos bool *unconditionalbranch)
49675fd0b74Schristos {
497*e992f068Schristos const struct op_code_struct *op;
49875fd0b74Schristos long targetaddr = 0;
49975fd0b74Schristos
500*e992f068Schristos *unconditionalbranch = false;
50175fd0b74Schristos /* Just a linear search of the table. */
502*e992f068Schristos for (op = microblaze_opcodes; op->name != 0; op ++)
50375fd0b74Schristos if (op->bit_sequence == (inst & op->opcode_mask))
50475fd0b74Schristos break;
50575fd0b74Schristos
50675fd0b74Schristos if (op->name == 0)
50775fd0b74Schristos {
508*e992f068Schristos *targetvalid = false;
50975fd0b74Schristos }
51075fd0b74Schristos else if (op->instr_type == branch_inst)
51175fd0b74Schristos {
51275fd0b74Schristos switch (op->inst_type)
51375fd0b74Schristos {
51475fd0b74Schristos case INST_TYPE_R2:
515*e992f068Schristos *unconditionalbranch = true;
51675fd0b74Schristos /* Fall through. */
51775fd0b74Schristos case INST_TYPE_RD_R2:
51875fd0b74Schristos case INST_TYPE_R1_R2:
51975fd0b74Schristos targetaddr = r2val;
520*e992f068Schristos *targetvalid = true;
52175fd0b74Schristos if (op->inst_offset_type == INST_PC_OFFSET)
52275fd0b74Schristos targetaddr += pcval;
52375fd0b74Schristos break;
52475fd0b74Schristos case INST_TYPE_IMM:
525*e992f068Schristos *unconditionalbranch = true;
52675fd0b74Schristos /* Fall through. */
52775fd0b74Schristos case INST_TYPE_RD_IMM:
52875fd0b74Schristos case INST_TYPE_R1_IMM:
52975fd0b74Schristos if (immfound)
53075fd0b74Schristos {
53175fd0b74Schristos targetaddr = (immval << 16) & 0xffff0000;
53275fd0b74Schristos targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
53375fd0b74Schristos }
53475fd0b74Schristos else
53575fd0b74Schristos {
53675fd0b74Schristos targetaddr = get_int_field_imm (inst);
53775fd0b74Schristos if (targetaddr & 0x8000)
53875fd0b74Schristos targetaddr |= 0xFFFF0000;
53975fd0b74Schristos }
54075fd0b74Schristos if (op->inst_offset_type == INST_PC_OFFSET)
54175fd0b74Schristos targetaddr += pcval;
542*e992f068Schristos *targetvalid = true;
54375fd0b74Schristos break;
54475fd0b74Schristos default:
545*e992f068Schristos *targetvalid = false;
54675fd0b74Schristos break;
54775fd0b74Schristos }
54875fd0b74Schristos }
54975fd0b74Schristos else if (op->instr_type == return_inst)
55075fd0b74Schristos {
55175fd0b74Schristos if (immfound)
55275fd0b74Schristos {
55375fd0b74Schristos targetaddr = (immval << 16) & 0xffff0000;
55475fd0b74Schristos targetaddr |= (get_int_field_imm (inst) & 0x0000ffff);
55575fd0b74Schristos }
55675fd0b74Schristos else
55775fd0b74Schristos {
55875fd0b74Schristos targetaddr = get_int_field_imm (inst);
55975fd0b74Schristos if (targetaddr & 0x8000)
56075fd0b74Schristos targetaddr |= 0xFFFF0000;
56175fd0b74Schristos }
56275fd0b74Schristos targetaddr += r1val;
563*e992f068Schristos *targetvalid = true;
56475fd0b74Schristos }
56575fd0b74Schristos else
566*e992f068Schristos *targetvalid = false;
56775fd0b74Schristos return targetaddr;
56875fd0b74Schristos }
569