xref: /openbmc/qemu/target/microblaze/translate.c (revision dbdb77c4df97205f9fbe1ec10ad35c1cb9729c12)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Xilinx MicroBlaze emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Edgar E. Iglesias.
5fcf5ef2aSThomas Huth  *  Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "disas/disas.h"
24fcf5ef2aSThomas Huth #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "microblaze-decode.h"
28fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
29fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
3077fc6f5eSLluís Vilanova #include "exec/translator.h"
3190c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "trace-tcg.h"
34fcf5ef2aSThomas Huth #include "exec/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth #define SIM_COMPAT 0
38fcf5ef2aSThomas Huth #define DISAS_GNU 1
39fcf5ef2aSThomas Huth #define DISAS_MB 1
40fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
41fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42fcf5ef2aSThomas Huth #else
43fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
44fcf5ef2aSThomas Huth #endif
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth #define D(x)
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
49fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
50fcf5ef2aSThomas Huth 
5177fc6f5eSLluís Vilanova /* is_jmp field values */
5277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5477fc6f5eSLluís Vilanova #define DISAS_TB_JUMP DISAS_TARGET_2 /* only pc was modified statically */
5577fc6f5eSLluís Vilanova 
56cfeea807SEdgar E. Iglesias static TCGv_i32 env_debug;
57cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
580f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
593e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
606efd5599SRichard Henderson static TCGv_i32 cpu_esr;
61cfeea807SEdgar E. Iglesias static TCGv_i32 env_imm;
62cfeea807SEdgar E. Iglesias static TCGv_i32 env_btaken;
630f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
64cfeea807SEdgar E. Iglesias static TCGv_i32 env_iflags;
65403322eaSEdgar E. Iglesias static TCGv env_res_addr;
66cfeea807SEdgar E. Iglesias static TCGv_i32 env_res_val;
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
69fcf5ef2aSThomas Huth 
70fcf5ef2aSThomas Huth /* This is the state at translation time.  */
71fcf5ef2aSThomas Huth typedef struct DisasContext {
72fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
73cfeea807SEdgar E. Iglesias     uint32_t pc;
74fcf5ef2aSThomas Huth 
75fcf5ef2aSThomas Huth     /* Decoder.  */
76fcf5ef2aSThomas Huth     int type_b;
77fcf5ef2aSThomas Huth     uint32_t ir;
78fcf5ef2aSThomas Huth     uint8_t opcode;
79fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
80fcf5ef2aSThomas Huth     uint16_t imm;
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
83fcf5ef2aSThomas Huth     unsigned int delayed_branch;
84fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
85fcf5ef2aSThomas Huth     unsigned int clear_imm;
86fcf5ef2aSThomas Huth     int is_jmp;
87fcf5ef2aSThomas Huth 
88fcf5ef2aSThomas Huth #define JMP_NOJMP     0
89fcf5ef2aSThomas Huth #define JMP_DIRECT    1
90fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
91fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
92fcf5ef2aSThomas Huth     unsigned int jmp;
93fcf5ef2aSThomas Huth     uint32_t jmp_pc;
94fcf5ef2aSThomas Huth 
95fcf5ef2aSThomas Huth     int abort_at_next_insn;
96fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
97fcf5ef2aSThomas Huth     int singlestep_enabled;
98fcf5ef2aSThomas Huth } DisasContext;
99fcf5ef2aSThomas Huth 
100fcf5ef2aSThomas Huth static const char *regnames[] =
101fcf5ef2aSThomas Huth {
102fcf5ef2aSThomas Huth     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
103fcf5ef2aSThomas Huth     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
104fcf5ef2aSThomas Huth     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
105fcf5ef2aSThomas Huth     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
106fcf5ef2aSThomas Huth };
107fcf5ef2aSThomas Huth 
108fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
109fcf5ef2aSThomas Huth {
110fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
111fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
112cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_iflags, dc->tb_flags);
113fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
114fcf5ef2aSThomas Huth     }
115fcf5ef2aSThomas Huth }
116fcf5ef2aSThomas Huth 
117fcf5ef2aSThomas Huth static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
118fcf5ef2aSThomas Huth {
119fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth     t_sync_flags(dc);
1220f96e96bSRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->pc);
123fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
124fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
125fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_UPDATE;
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
129fcf5ef2aSThomas Huth {
130fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
131fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
132fcf5ef2aSThomas Huth #else
133fcf5ef2aSThomas Huth     return true;
134fcf5ef2aSThomas Huth #endif
135fcf5ef2aSThomas Huth }
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
138fcf5ef2aSThomas Huth {
139fcf5ef2aSThomas Huth     if (use_goto_tb(dc, dest)) {
140fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1410f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14207ea28b4SRichard Henderson         tcg_gen_exit_tb(dc->tb, n);
143fcf5ef2aSThomas Huth     } else {
1440f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14507ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
146fcf5ef2aSThomas Huth     }
147fcf5ef2aSThomas Huth }
148fcf5ef2aSThomas Huth 
149cfeea807SEdgar E. Iglesias static void read_carry(DisasContext *dc, TCGv_i32 d)
150fcf5ef2aSThomas Huth {
1513e0e16aeSRichard Henderson     tcg_gen_shri_i32(d, cpu_msr, 31);
152fcf5ef2aSThomas Huth }
153fcf5ef2aSThomas Huth 
154fcf5ef2aSThomas Huth /*
155fcf5ef2aSThomas Huth  * write_carry sets the carry bits in MSR based on bit 0 of v.
156fcf5ef2aSThomas Huth  * v[31:1] are ignored.
157fcf5ef2aSThomas Huth  */
158cfeea807SEdgar E. Iglesias static void write_carry(DisasContext *dc, TCGv_i32 v)
159fcf5ef2aSThomas Huth {
1600a22f8cfSEdgar E. Iglesias     /* Deposit bit 0 into MSR_C and the alias MSR_CC.  */
1613e0e16aeSRichard Henderson     tcg_gen_deposit_i32(cpu_msr, cpu_msr, v, 2, 1);
1623e0e16aeSRichard Henderson     tcg_gen_deposit_i32(cpu_msr, cpu_msr, v, 31, 1);
163fcf5ef2aSThomas Huth }
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth static void write_carryi(DisasContext *dc, bool carry)
166fcf5ef2aSThomas Huth {
167cfeea807SEdgar E. Iglesias     TCGv_i32 t0 = tcg_temp_new_i32();
168cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(t0, carry);
169fcf5ef2aSThomas Huth     write_carry(dc, t0);
170cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
171fcf5ef2aSThomas Huth }
172fcf5ef2aSThomas Huth 
173bdfc1e88SEdgar E. Iglesias /*
1749ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1759ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1769ba8cd45SEdgar E. Iglesias  */
1779ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1789ba8cd45SEdgar E. Iglesias {
1799ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1805143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
1816efd5599SRichard Henderson         tcg_gen_movi_i32(cpu_esr, ESR_EC_ILLEGAL_OP);
1829ba8cd45SEdgar E. Iglesias         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1839ba8cd45SEdgar E. Iglesias     }
1849ba8cd45SEdgar E. Iglesias     return cond;
1859ba8cd45SEdgar E. Iglesias }
1869ba8cd45SEdgar E. Iglesias 
1879ba8cd45SEdgar E. Iglesias /*
188bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
189bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
190bdfc1e88SEdgar E. Iglesias  */
191bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
192bdfc1e88SEdgar E. Iglesias {
193bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
194bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
195bdfc1e88SEdgar E. Iglesias 
196bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
1976efd5599SRichard Henderson         tcg_gen_movi_i32(cpu_esr, ESR_EC_PRIVINSN);
198bdfc1e88SEdgar E. Iglesias         t_gen_raise_exception(dc, EXCP_HW_EXCP);
199bdfc1e88SEdgar E. Iglesias     }
200bdfc1e88SEdgar E. Iglesias     return cond_user;
201bdfc1e88SEdgar E. Iglesias }
202bdfc1e88SEdgar E. Iglesias 
203fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
204fcf5ef2aSThomas Huth    faster treatment.  */
205fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
206fcf5ef2aSThomas Huth {
207fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
208fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
209fcf5ef2aSThomas Huth }
210fcf5ef2aSThomas Huth 
211cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
212fcf5ef2aSThomas Huth {
213fcf5ef2aSThomas Huth     if (dc->type_b) {
214fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
215cfeea807SEdgar E. Iglesias             tcg_gen_ori_i32(env_imm, env_imm, dc->imm);
216fcf5ef2aSThomas Huth         else
217cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_imm, (int32_t)((int16_t)dc->imm));
218fcf5ef2aSThomas Huth         return &env_imm;
219fcf5ef2aSThomas Huth     } else
220fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
221fcf5ef2aSThomas Huth }
222fcf5ef2aSThomas Huth 
223fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
224fcf5ef2aSThomas Huth {
225fcf5ef2aSThomas Huth     unsigned int k, c;
226cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
227fcf5ef2aSThomas Huth 
228fcf5ef2aSThomas Huth     k = dc->opcode & 4;
229fcf5ef2aSThomas Huth     c = dc->opcode & 2;
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
232fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
233fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
236fcf5ef2aSThomas Huth     if (k) {
237fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
238fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
239fcf5ef2aSThomas Huth         if (dc->rd) {
240cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
241fcf5ef2aSThomas Huth 
242fcf5ef2aSThomas Huth             if (c) {
243fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
244cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth                 read_carry(dc, cf);
247cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
248cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
249fcf5ef2aSThomas Huth             }
250fcf5ef2aSThomas Huth         }
251fcf5ef2aSThomas Huth         return;
252fcf5ef2aSThomas Huth     }
253fcf5ef2aSThomas Huth 
254fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
255fcf5ef2aSThomas Huth     /* Extract carry.  */
256cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
257fcf5ef2aSThomas Huth     if (c) {
258fcf5ef2aSThomas Huth         read_carry(dc, cf);
259fcf5ef2aSThomas Huth     } else {
260cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
261fcf5ef2aSThomas Huth     }
262fcf5ef2aSThomas Huth 
263fcf5ef2aSThomas Huth     if (dc->rd) {
264cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
265fcf5ef2aSThomas Huth         gen_helper_carry(ncf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
266cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
267cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
268fcf5ef2aSThomas Huth         write_carry(dc, ncf);
269cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
270fcf5ef2aSThomas Huth     } else {
271fcf5ef2aSThomas Huth         gen_helper_carry(cf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
272fcf5ef2aSThomas Huth         write_carry(dc, cf);
273fcf5ef2aSThomas Huth     }
274cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
275fcf5ef2aSThomas Huth }
276fcf5ef2aSThomas Huth 
277fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
278fcf5ef2aSThomas Huth {
279fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
280cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
281fcf5ef2aSThomas Huth 
282fcf5ef2aSThomas Huth     u = dc->imm & 2;
283fcf5ef2aSThomas Huth     k = dc->opcode & 4;
284fcf5ef2aSThomas Huth     c = dc->opcode & 2;
285fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth     if (cmp) {
288fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
289fcf5ef2aSThomas Huth         if (dc->rd) {
290fcf5ef2aSThomas Huth             if (u)
291fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
292fcf5ef2aSThomas Huth             else
293fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
294fcf5ef2aSThomas Huth         }
295fcf5ef2aSThomas Huth         return;
296fcf5ef2aSThomas Huth     }
297fcf5ef2aSThomas Huth 
298fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
299fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
300fcf5ef2aSThomas Huth 
301fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
302fcf5ef2aSThomas Huth     if (k) {
303fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
304fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
305fcf5ef2aSThomas Huth         if (dc->rd) {
306cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth             if (c) {
309fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
310cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
311fcf5ef2aSThomas Huth 
312fcf5ef2aSThomas Huth                 read_carry(dc, cf);
313cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
314cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
315fcf5ef2aSThomas Huth             }
316fcf5ef2aSThomas Huth         }
317fcf5ef2aSThomas Huth         return;
318fcf5ef2aSThomas Huth     }
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
321fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
322cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
323cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
324fcf5ef2aSThomas Huth     if (c) {
325fcf5ef2aSThomas Huth         read_carry(dc, cf);
326fcf5ef2aSThomas Huth     } else {
327cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
328fcf5ef2aSThomas Huth     }
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
331cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
332fcf5ef2aSThomas Huth 
333fcf5ef2aSThomas Huth     if (dc->rd) {
334cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
335fcf5ef2aSThomas Huth         gen_helper_carry(ncf, na, *(dec_alu_op_b(dc)), cf);
336cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
337cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
338fcf5ef2aSThomas Huth         write_carry(dc, ncf);
339cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
340fcf5ef2aSThomas Huth     } else {
341fcf5ef2aSThomas Huth         gen_helper_carry(cf, na, *(dec_alu_op_b(dc)), cf);
342fcf5ef2aSThomas Huth         write_carry(dc, cf);
343fcf5ef2aSThomas Huth     }
344cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
345cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
346fcf5ef2aSThomas Huth }
347fcf5ef2aSThomas Huth 
348fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
349fcf5ef2aSThomas Huth {
350fcf5ef2aSThomas Huth     unsigned int mode;
351fcf5ef2aSThomas Huth 
3529ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3539ba8cd45SEdgar E. Iglesias         return;
354fcf5ef2aSThomas Huth     }
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
357fcf5ef2aSThomas Huth     switch (mode) {
358fcf5ef2aSThomas Huth         case 0:
359fcf5ef2aSThomas Huth             /* pcmpbf.  */
360fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
361fcf5ef2aSThomas Huth             if (dc->rd)
362fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
363fcf5ef2aSThomas Huth             break;
364fcf5ef2aSThomas Huth         case 2:
365fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
366fcf5ef2aSThomas Huth             if (dc->rd) {
367cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
368fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
369fcf5ef2aSThomas Huth             }
370fcf5ef2aSThomas Huth             break;
371fcf5ef2aSThomas Huth         case 3:
372fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
373fcf5ef2aSThomas Huth             if (dc->rd) {
374cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
375fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
376fcf5ef2aSThomas Huth             }
377fcf5ef2aSThomas Huth             break;
378fcf5ef2aSThomas Huth         default:
379fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
380fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
381fcf5ef2aSThomas Huth             break;
382fcf5ef2aSThomas Huth     }
383fcf5ef2aSThomas Huth }
384fcf5ef2aSThomas Huth 
385fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
386fcf5ef2aSThomas Huth {
387fcf5ef2aSThomas Huth     unsigned int not;
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
390fcf5ef2aSThomas Huth         dec_pattern(dc);
391fcf5ef2aSThomas Huth         return;
392fcf5ef2aSThomas Huth     }
393fcf5ef2aSThomas Huth 
394fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
395fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
396fcf5ef2aSThomas Huth 
397fcf5ef2aSThomas Huth     if (!dc->rd)
398fcf5ef2aSThomas Huth         return;
399fcf5ef2aSThomas Huth 
400fcf5ef2aSThomas Huth     if (not) {
401cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
402fcf5ef2aSThomas Huth     } else
403cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
404fcf5ef2aSThomas Huth }
405fcf5ef2aSThomas Huth 
406fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
407fcf5ef2aSThomas Huth {
408fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
409fcf5ef2aSThomas Huth         dec_pattern(dc);
410fcf5ef2aSThomas Huth         return;
411fcf5ef2aSThomas Huth     }
412fcf5ef2aSThomas Huth 
413fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
414fcf5ef2aSThomas Huth     if (dc->rd)
415cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
416fcf5ef2aSThomas Huth }
417fcf5ef2aSThomas Huth 
418fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
419fcf5ef2aSThomas Huth {
420fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
421fcf5ef2aSThomas Huth         dec_pattern(dc);
422fcf5ef2aSThomas Huth         return;
423fcf5ef2aSThomas Huth     }
424fcf5ef2aSThomas Huth 
425fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
426fcf5ef2aSThomas Huth     if (dc->rd)
427cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
428fcf5ef2aSThomas Huth }
429fcf5ef2aSThomas Huth 
430cfeea807SEdgar E. Iglesias static inline void msr_read(DisasContext *dc, TCGv_i32 d)
431fcf5ef2aSThomas Huth {
4323e0e16aeSRichard Henderson     tcg_gen_mov_i32(d, cpu_msr);
433fcf5ef2aSThomas Huth }
434fcf5ef2aSThomas Huth 
435cfeea807SEdgar E. Iglesias static inline void msr_write(DisasContext *dc, TCGv_i32 v)
436fcf5ef2aSThomas Huth {
437fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4383e0e16aeSRichard Henderson     /* PVR bit is not writable, and is never set. */
4393e0e16aeSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~MSR_PVR);
440fcf5ef2aSThomas Huth }
441fcf5ef2aSThomas Huth 
442fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
443fcf5ef2aSThomas Huth {
444fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
445cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4462023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
447f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
448fcf5ef2aSThomas Huth 
4492023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4502023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4512023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
452fcf5ef2aSThomas Huth     dc->type_b = 1;
4532023e9a3SEdgar E. Iglesias     if (to) {
454fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
455f0f7e7f7SEdgar E. Iglesias     }
456f0f7e7f7SEdgar E. Iglesias 
457f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
458f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
459f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
460f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
461f0f7e7f7SEdgar E. Iglesias 
462f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4632023e9a3SEdgar E. Iglesias     }
464fcf5ef2aSThomas Huth 
465fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4662023e9a3SEdgar E. Iglesias     if (clrset) {
4672023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
468fcf5ef2aSThomas Huth 
469fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
470fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
471fcf5ef2aSThomas Huth 
47256837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
473fcf5ef2aSThomas Huth             /* nop??? */
474fcf5ef2aSThomas Huth             return;
475fcf5ef2aSThomas Huth         }
476fcf5ef2aSThomas Huth 
477bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
478fcf5ef2aSThomas Huth             return;
479fcf5ef2aSThomas Huth         }
480fcf5ef2aSThomas Huth 
481fcf5ef2aSThomas Huth         if (dc->rd)
482fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
483fcf5ef2aSThomas Huth 
484cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
485cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
486fcf5ef2aSThomas Huth         msr_read(dc, t0);
487cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
488fcf5ef2aSThomas Huth 
489fcf5ef2aSThomas Huth         if (clr) {
490cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
491cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
492fcf5ef2aSThomas Huth         } else
493cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
494fcf5ef2aSThomas Huth         msr_write(dc, t0);
495cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
496cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
4970f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
498fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
499fcf5ef2aSThomas Huth         return;
500fcf5ef2aSThomas Huth     }
501fcf5ef2aSThomas Huth 
502bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
503fcf5ef2aSThomas Huth         return;
504fcf5ef2aSThomas Huth     }
505fcf5ef2aSThomas Huth 
506fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
507fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
508fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
509f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
51005a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
51105a9a651SEdgar E. Iglesias 
512fcf5ef2aSThomas Huth         sr &= 7;
51305a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
514fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
51505a9a651SEdgar E. Iglesias         if (to) {
516f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
51705a9a651SEdgar E. Iglesias         } else {
518f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
51905a9a651SEdgar E. Iglesias         }
52005a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
521f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
522fcf5ef2aSThomas Huth         return;
523fcf5ef2aSThomas Huth     }
524fcf5ef2aSThomas Huth #endif
525fcf5ef2aSThomas Huth 
526fcf5ef2aSThomas Huth     if (to) {
527fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
528fcf5ef2aSThomas Huth         switch (sr) {
529aa28e6d4SRichard Henderson             case SR_PC:
530fcf5ef2aSThomas Huth                 break;
531aa28e6d4SRichard Henderson             case SR_MSR:
532fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
533fcf5ef2aSThomas Huth                 break;
534351527b7SEdgar E. Iglesias             case SR_EAR:
535*dbdb77c4SRichard Henderson                 {
536*dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
537*dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
538*dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
539*dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
540*dbdb77c4SRichard Henderson                 }
541aa28e6d4SRichard Henderson                 break;
542351527b7SEdgar E. Iglesias             case SR_ESR:
5436efd5599SRichard Henderson                 tcg_gen_mov_i32(cpu_esr, cpu_R[dc->ra]);
544aa28e6d4SRichard Henderson                 break;
545ab6dd380SEdgar E. Iglesias             case SR_FSR:
54686017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
54786017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
548aa28e6d4SRichard Henderson                 break;
549aa28e6d4SRichard Henderson             case SR_BTR:
550ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
551ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
552aa28e6d4SRichard Henderson                 break;
553aa28e6d4SRichard Henderson             case SR_EDR:
55439db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
55539db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
556fcf5ef2aSThomas Huth                 break;
557fcf5ef2aSThomas Huth             case 0x800:
558cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
559cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
560fcf5ef2aSThomas Huth                 break;
561fcf5ef2aSThomas Huth             case 0x802:
562cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
563cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
564fcf5ef2aSThomas Huth                 break;
565fcf5ef2aSThomas Huth             default:
566fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
567fcf5ef2aSThomas Huth                 break;
568fcf5ef2aSThomas Huth         }
569fcf5ef2aSThomas Huth     } else {
570fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
571fcf5ef2aSThomas Huth 
572fcf5ef2aSThomas Huth         switch (sr) {
573aa28e6d4SRichard Henderson             case SR_PC:
574cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
575fcf5ef2aSThomas Huth                 break;
576aa28e6d4SRichard Henderson             case SR_MSR:
577fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
578fcf5ef2aSThomas Huth                 break;
579351527b7SEdgar E. Iglesias             case SR_EAR:
580*dbdb77c4SRichard Henderson                 {
581*dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
582*dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
583a1b48e3aSEdgar E. Iglesias                     if (extended) {
584*dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
585aa28e6d4SRichard Henderson                     } else {
586*dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
587*dbdb77c4SRichard Henderson                     }
588*dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
589a1b48e3aSEdgar E. Iglesias                 }
590aa28e6d4SRichard Henderson                 break;
591351527b7SEdgar E. Iglesias             case SR_ESR:
5926efd5599SRichard Henderson                 tcg_gen_mov_i32(cpu_R[dc->rd], cpu_esr);
593aa28e6d4SRichard Henderson                 break;
594351527b7SEdgar E. Iglesias             case SR_FSR:
59586017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
59686017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
597aa28e6d4SRichard Henderson                 break;
598351527b7SEdgar E. Iglesias             case SR_BTR:
599ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
600ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
601aa28e6d4SRichard Henderson                 break;
6027cdae31dSTong Ho             case SR_EDR:
60339db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
60439db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
605fcf5ef2aSThomas Huth                 break;
606fcf5ef2aSThomas Huth             case 0x800:
607cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
608cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
609fcf5ef2aSThomas Huth                 break;
610fcf5ef2aSThomas Huth             case 0x802:
611cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
612cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
613fcf5ef2aSThomas Huth                 break;
614351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
615fcf5ef2aSThomas Huth                 rn = sr & 0xf;
616cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
617fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
618fcf5ef2aSThomas Huth                 break;
619fcf5ef2aSThomas Huth             default:
620fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
621fcf5ef2aSThomas Huth                 break;
622fcf5ef2aSThomas Huth         }
623fcf5ef2aSThomas Huth     }
624fcf5ef2aSThomas Huth 
625fcf5ef2aSThomas Huth     if (dc->rd == 0) {
626cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
627fcf5ef2aSThomas Huth     }
628fcf5ef2aSThomas Huth }
629fcf5ef2aSThomas Huth 
630fcf5ef2aSThomas Huth /* Multiplier unit.  */
631fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
632fcf5ef2aSThomas Huth {
633cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
634fcf5ef2aSThomas Huth     unsigned int subcode;
635fcf5ef2aSThomas Huth 
6369ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
637fcf5ef2aSThomas Huth         return;
638fcf5ef2aSThomas Huth     }
639fcf5ef2aSThomas Huth 
640fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
641fcf5ef2aSThomas Huth 
642fcf5ef2aSThomas Huth     if (dc->type_b) {
643fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
644cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
645fcf5ef2aSThomas Huth         return;
646fcf5ef2aSThomas Huth     }
647fcf5ef2aSThomas Huth 
648fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6499b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
650fcf5ef2aSThomas Huth         /* nop??? */
651fcf5ef2aSThomas Huth     }
652fcf5ef2aSThomas Huth 
653cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
654fcf5ef2aSThomas Huth     switch (subcode) {
655fcf5ef2aSThomas Huth         case 0:
656fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
657cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
658fcf5ef2aSThomas Huth             break;
659fcf5ef2aSThomas Huth         case 1:
660fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
661cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
662cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
663fcf5ef2aSThomas Huth             break;
664fcf5ef2aSThomas Huth         case 2:
665fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
666cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
667cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
668fcf5ef2aSThomas Huth             break;
669fcf5ef2aSThomas Huth         case 3:
670fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
671cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
672fcf5ef2aSThomas Huth             break;
673fcf5ef2aSThomas Huth         default:
674fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
675fcf5ef2aSThomas Huth             break;
676fcf5ef2aSThomas Huth     }
677cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
678fcf5ef2aSThomas Huth }
679fcf5ef2aSThomas Huth 
680fcf5ef2aSThomas Huth /* Div unit.  */
681fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
682fcf5ef2aSThomas Huth {
683fcf5ef2aSThomas Huth     unsigned int u;
684fcf5ef2aSThomas Huth 
685fcf5ef2aSThomas Huth     u = dc->imm & 2;
686fcf5ef2aSThomas Huth     LOG_DIS("div\n");
687fcf5ef2aSThomas Huth 
6889ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6899ba8cd45SEdgar E. Iglesias         return;
690fcf5ef2aSThomas Huth     }
691fcf5ef2aSThomas Huth 
692fcf5ef2aSThomas Huth     if (u)
693fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
694fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
695fcf5ef2aSThomas Huth     else
696fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
697fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
698fcf5ef2aSThomas Huth     if (!dc->rd)
699cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
700fcf5ef2aSThomas Huth }
701fcf5ef2aSThomas Huth 
702fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
703fcf5ef2aSThomas Huth {
704cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
705faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
706d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
707fcf5ef2aSThomas Huth 
7089ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
709fcf5ef2aSThomas Huth         return;
710fcf5ef2aSThomas Huth     }
711fcf5ef2aSThomas Huth 
712faa48d74SEdgar E. Iglesias     if (dc->type_b) {
713faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
714d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
715faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
716faa48d74SEdgar E. Iglesias     }
717e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
718e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
719faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
720faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
721fcf5ef2aSThomas Huth 
722faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
723faa48d74SEdgar E. Iglesias             e ? "e" : "",
724fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
725fcf5ef2aSThomas Huth 
726faa48d74SEdgar E. Iglesias     if (e) {
727faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
728faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
729faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
730faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
731faa48d74SEdgar E. Iglesias         } else {
732faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
733faa48d74SEdgar E. Iglesias         }
734d09b2585SEdgar E. Iglesias     } else if (i) {
735d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
736d09b2585SEdgar E. Iglesias 
737d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
738d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
739d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
740d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
741d09b2585SEdgar E. Iglesias         } else {
742d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
743d09b2585SEdgar E. Iglesias                                 imm_s, width);
744d09b2585SEdgar E. Iglesias         }
745faa48d74SEdgar E. Iglesias     } else {
746cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
747fcf5ef2aSThomas Huth 
748cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
749cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
750fcf5ef2aSThomas Huth 
7512acf6d53SEdgar E. Iglesias         if (s) {
752cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7532acf6d53SEdgar E. Iglesias         } else {
7542acf6d53SEdgar E. Iglesias             if (t) {
755cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7562acf6d53SEdgar E. Iglesias             } else {
757cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
758fcf5ef2aSThomas Huth             }
759fcf5ef2aSThomas Huth         }
760cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7612acf6d53SEdgar E. Iglesias     }
762faa48d74SEdgar E. Iglesias }
763fcf5ef2aSThomas Huth 
764fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
765fcf5ef2aSThomas Huth {
766fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
767cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
768fcf5ef2aSThomas Huth     unsigned int op;
769fcf5ef2aSThomas Huth 
770fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
771fcf5ef2aSThomas Huth     switch (op) {
772fcf5ef2aSThomas Huth         case 0x21:
773fcf5ef2aSThomas Huth             /* src.  */
774cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
775fcf5ef2aSThomas Huth 
776fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7773e0e16aeSRichard Henderson             tcg_gen_andi_i32(t0, cpu_msr, MSR_CC);
778fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
779fcf5ef2aSThomas Huth             if (dc->rd) {
780cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
781cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
782fcf5ef2aSThomas Huth             }
783cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
784fcf5ef2aSThomas Huth             break;
785fcf5ef2aSThomas Huth 
786fcf5ef2aSThomas Huth         case 0x1:
787fcf5ef2aSThomas Huth         case 0x41:
788fcf5ef2aSThomas Huth             /* srl.  */
789fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
790fcf5ef2aSThomas Huth 
791fcf5ef2aSThomas Huth             /* Update carry. Note that write carry only looks at the LSB.  */
792fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
793fcf5ef2aSThomas Huth             if (dc->rd) {
794fcf5ef2aSThomas Huth                 if (op == 0x41)
795cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
796fcf5ef2aSThomas Huth                 else
797cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
798fcf5ef2aSThomas Huth             }
799fcf5ef2aSThomas Huth             break;
800fcf5ef2aSThomas Huth         case 0x60:
801fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
802fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
803fcf5ef2aSThomas Huth             break;
804fcf5ef2aSThomas Huth         case 0x61:
805fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
806fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
807fcf5ef2aSThomas Huth             break;
808fcf5ef2aSThomas Huth         case 0x64:
809fcf5ef2aSThomas Huth         case 0x66:
810fcf5ef2aSThomas Huth         case 0x74:
811fcf5ef2aSThomas Huth         case 0x76:
812fcf5ef2aSThomas Huth             /* wdc.  */
813fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
814bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
815fcf5ef2aSThomas Huth             break;
816fcf5ef2aSThomas Huth         case 0x68:
817fcf5ef2aSThomas Huth             /* wic.  */
818fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
819bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
820fcf5ef2aSThomas Huth             break;
821fcf5ef2aSThomas Huth         case 0xe0:
8229ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
8239ba8cd45SEdgar E. Iglesias                 return;
824fcf5ef2aSThomas Huth             }
8258fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8265318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
827fcf5ef2aSThomas Huth             }
828fcf5ef2aSThomas Huth             break;
829fcf5ef2aSThomas Huth         case 0x1e0:
830fcf5ef2aSThomas Huth             /* swapb */
831fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
832fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
833fcf5ef2aSThomas Huth             break;
834fcf5ef2aSThomas Huth         case 0x1e2:
835fcf5ef2aSThomas Huth             /*swaph */
836fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
837fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
838fcf5ef2aSThomas Huth             break;
839fcf5ef2aSThomas Huth         default:
840fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
841fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
842fcf5ef2aSThomas Huth             break;
843fcf5ef2aSThomas Huth     }
844fcf5ef2aSThomas Huth }
845fcf5ef2aSThomas Huth 
846fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
847fcf5ef2aSThomas Huth {
848fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
849fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
850cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
851fcf5ef2aSThomas Huth         }
852fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8530f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
854fcf5ef2aSThomas Huth     }
855fcf5ef2aSThomas Huth }
856fcf5ef2aSThomas Huth 
857fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
858fcf5ef2aSThomas Huth {
859fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
860cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_imm, (dc->imm << 16));
861fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
862fcf5ef2aSThomas Huth     dc->clear_imm = 0;
863fcf5ef2aSThomas Huth }
864fcf5ef2aSThomas Huth 
865d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
866fcf5ef2aSThomas Huth {
8670e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8680e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8690e9033c8SEdgar E. Iglesias     bool stackprot = false;
870403322eaSEdgar E. Iglesias     TCGv_i32 t32;
871fcf5ef2aSThomas Huth 
872fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
873fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8740e9033c8SEdgar E. Iglesias         stackprot = true;
875fcf5ef2aSThomas Huth     }
876fcf5ef2aSThomas Huth 
877fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
878fcf5ef2aSThomas Huth     if (!dc->type_b) {
879d248e1beSEdgar E. Iglesias         if (ea) {
880d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
881d248e1beSEdgar E. Iglesias 
882d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
883d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
884d248e1beSEdgar E. Iglesias                 return;
885d248e1beSEdgar E. Iglesias             }
886d248e1beSEdgar E. Iglesias 
887d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
888d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
889d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
890d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
891d248e1beSEdgar E. Iglesias             }
892d248e1beSEdgar E. Iglesias             return;
893d248e1beSEdgar E. Iglesias         }
894d248e1beSEdgar E. Iglesias 
8950dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
896fcf5ef2aSThomas Huth         if (dc->ra == 0) {
897403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8980dc4af5cSEdgar E. Iglesias             return;
899fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
900403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
9010dc4af5cSEdgar E. Iglesias             return;
902fcf5ef2aSThomas Huth         }
903fcf5ef2aSThomas Huth 
904fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
9050e9033c8SEdgar E. Iglesias             stackprot = true;
906fcf5ef2aSThomas Huth         }
907fcf5ef2aSThomas Huth 
908403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
909403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
910403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
911403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth         if (stackprot) {
9140a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
915fcf5ef2aSThomas Huth         }
9160dc4af5cSEdgar E. Iglesias         return;
917fcf5ef2aSThomas Huth     }
918fcf5ef2aSThomas Huth     /* Immediate.  */
919403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
920fcf5ef2aSThomas Huth     if (!extimm) {
921f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
922403322eaSEdgar E. Iglesias     } else {
923403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
924403322eaSEdgar E. Iglesias     }
925403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
926403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
927fcf5ef2aSThomas Huth 
928fcf5ef2aSThomas Huth     if (stackprot) {
9290a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
930fcf5ef2aSThomas Huth     }
9310dc4af5cSEdgar E. Iglesias     return;
932fcf5ef2aSThomas Huth }
933fcf5ef2aSThomas Huth 
934fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
935fcf5ef2aSThomas Huth {
936403322eaSEdgar E. Iglesias     TCGv_i32 v;
937403322eaSEdgar E. Iglesias     TCGv addr;
9388534063aSEdgar E. Iglesias     unsigned int size;
939d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
940d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
94114776ab5STony Nguyen     MemOp mop;
942fcf5ef2aSThomas Huth 
943fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
944fcf5ef2aSThomas Huth     size = 1 << mop;
945fcf5ef2aSThomas Huth     if (!dc->type_b) {
946d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9478534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9488534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
949fcf5ef2aSThomas Huth     }
950fcf5ef2aSThomas Huth     mop |= MO_TE;
951fcf5ef2aSThomas Huth     if (rev) {
952fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
953fcf5ef2aSThomas Huth     }
954fcf5ef2aSThomas Huth 
9559ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
956fcf5ef2aSThomas Huth         return;
957fcf5ef2aSThomas Huth     }
958fcf5ef2aSThomas Huth 
959d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
960d248e1beSEdgar E. Iglesias         return;
961d248e1beSEdgar E. Iglesias     }
962d248e1beSEdgar E. Iglesias 
963d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
964d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
965d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
966fcf5ef2aSThomas Huth 
967fcf5ef2aSThomas Huth     t_sync_flags(dc);
968403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
969d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
970d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
971d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
972fcf5ef2aSThomas Huth 
973fcf5ef2aSThomas Huth     /*
974fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
975fcf5ef2aSThomas Huth      *
976fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
977fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
978fcf5ef2aSThomas Huth      */
979fcf5ef2aSThomas Huth     if (rev && size != 4) {
980fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
981fcf5ef2aSThomas Huth         switch (size) {
982fcf5ef2aSThomas Huth             case 1:
983fcf5ef2aSThomas Huth             {
984a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
985fcf5ef2aSThomas Huth                 break;
986fcf5ef2aSThomas Huth             }
987fcf5ef2aSThomas Huth 
988fcf5ef2aSThomas Huth             case 2:
989fcf5ef2aSThomas Huth                 /* 00 -> 10
990fcf5ef2aSThomas Huth                    10 -> 00.  */
991403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
992fcf5ef2aSThomas Huth                 break;
993fcf5ef2aSThomas Huth             default:
994fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
995fcf5ef2aSThomas Huth                 break;
996fcf5ef2aSThomas Huth         }
997fcf5ef2aSThomas Huth     }
998fcf5ef2aSThomas Huth 
999fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
1000fcf5ef2aSThomas Huth     if (ex) {
1001403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1002fcf5ef2aSThomas Huth     }
1003fcf5ef2aSThomas Huth 
1004fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1005fcf5ef2aSThomas Huth     sync_jmpstate(dc);
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
1008fcf5ef2aSThomas Huth     /*
1009fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
1010fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
1011fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
1012fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
1013fcf5ef2aSThomas Huth      */
1014cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
1015d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
1016fcf5ef2aSThomas Huth 
10171507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1018a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
1019a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1020a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1021a6338015SEdgar E. Iglesias 
10220f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1023a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
1024a6338015SEdgar E. Iglesias 
1025a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1026a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1027a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1028fcf5ef2aSThomas Huth     }
1029fcf5ef2aSThomas Huth 
1030fcf5ef2aSThomas Huth     if (ex) {
1031403322eaSEdgar E. Iglesias         tcg_gen_mov_tl(env_res_addr, addr);
1032cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(env_res_val, v);
1033fcf5ef2aSThomas Huth     }
1034fcf5ef2aSThomas Huth     if (dc->rd) {
1035cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1036fcf5ef2aSThomas Huth     }
1037cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1038fcf5ef2aSThomas Huth 
1039fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1040fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
1041fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1042fcf5ef2aSThomas Huth     }
1043fcf5ef2aSThomas Huth 
1044403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1045fcf5ef2aSThomas Huth }
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1048fcf5ef2aSThomas Huth {
1049403322eaSEdgar E. Iglesias     TCGv addr;
1050fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1051b51b3d43SEdgar E. Iglesias     unsigned int size;
1052d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1053d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
105414776ab5STony Nguyen     MemOp mop;
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1057fcf5ef2aSThomas Huth     size = 1 << mop;
1058fcf5ef2aSThomas Huth     if (!dc->type_b) {
1059d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1060b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1061b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1062fcf5ef2aSThomas Huth     }
1063fcf5ef2aSThomas Huth     mop |= MO_TE;
1064fcf5ef2aSThomas Huth     if (rev) {
1065fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1066fcf5ef2aSThomas Huth     }
1067fcf5ef2aSThomas Huth 
10689ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1069fcf5ef2aSThomas Huth         return;
1070fcf5ef2aSThomas Huth     }
1071fcf5ef2aSThomas Huth 
1072d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1073d248e1beSEdgar E. Iglesias 
1074d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1075d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1076d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1077fcf5ef2aSThomas Huth     t_sync_flags(dc);
1078fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1079fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10800dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1081403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1082d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1083d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1084d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1085fcf5ef2aSThomas Huth 
1086fcf5ef2aSThomas Huth     if (ex) { /* swx */
1087cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1088fcf5ef2aSThomas Huth 
1089fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1090403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1091fcf5ef2aSThomas Huth 
1092fcf5ef2aSThomas Huth         write_carryi(dc, 1);
1093fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
1094403322eaSEdgar E. Iglesias         tcg_gen_brcond_tl(TCG_COND_NE, env_res_addr, addr, swx_skip);
1095fcf5ef2aSThomas Huth 
1096071cdc67SEdgar E. Iglesias         /*
1097071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1098071cdc67SEdgar E. Iglesias          * the reserved location.
1099071cdc67SEdgar E. Iglesias          */
1100cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1101071cdc67SEdgar E. Iglesias 
1102071cdc67SEdgar E. Iglesias         tcg_gen_atomic_cmpxchg_i32(tval, addr, env_res_val,
1103071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1104071cdc67SEdgar E. Iglesias                                    mop);
1105071cdc67SEdgar E. Iglesias 
1106cfeea807SEdgar E. Iglesias         tcg_gen_brcond_i32(TCG_COND_NE, env_res_val, tval, swx_skip);
1107fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1108cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1109fcf5ef2aSThomas Huth     }
1110fcf5ef2aSThomas Huth 
1111fcf5ef2aSThomas Huth     if (rev && size != 4) {
1112fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1113fcf5ef2aSThomas Huth         switch (size) {
1114fcf5ef2aSThomas Huth             case 1:
1115fcf5ef2aSThomas Huth             {
1116a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1117fcf5ef2aSThomas Huth                 break;
1118fcf5ef2aSThomas Huth             }
1119fcf5ef2aSThomas Huth 
1120fcf5ef2aSThomas Huth             case 2:
1121fcf5ef2aSThomas Huth                 /* 00 -> 10
1122fcf5ef2aSThomas Huth                    10 -> 00.  */
1123fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1124403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1125fcf5ef2aSThomas Huth                 break;
1126fcf5ef2aSThomas Huth             default:
1127fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1128fcf5ef2aSThomas Huth                 break;
1129fcf5ef2aSThomas Huth         }
1130fcf5ef2aSThomas Huth     }
1131071cdc67SEdgar E. Iglesias 
1132071cdc67SEdgar E. Iglesias     if (!ex) {
1133d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1134071cdc67SEdgar E. Iglesias     }
1135fcf5ef2aSThomas Huth 
1136fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11371507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1138a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1139a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1140a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1141a6338015SEdgar E. Iglesias 
11420f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1143fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1144fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1145fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1146fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1147fcf5ef2aSThomas Huth          *        access.
1148fcf5ef2aSThomas Huth          */
1149a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1150a6338015SEdgar E. Iglesias 
1151a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1152a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1153a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1154fcf5ef2aSThomas Huth     }
1155fcf5ef2aSThomas Huth 
1156fcf5ef2aSThomas Huth     if (ex) {
1157fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1158fcf5ef2aSThomas Huth     }
1159fcf5ef2aSThomas Huth 
1160403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1161fcf5ef2aSThomas Huth }
1162fcf5ef2aSThomas Huth 
1163fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11649e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1165fcf5ef2aSThomas Huth {
1166d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1167d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1168d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1169d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1170d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1171d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1172d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1173d89b86e9SEdgar E. Iglesias     };
1174d89b86e9SEdgar E. Iglesias 
1175fcf5ef2aSThomas Huth     switch (cc) {
1176fcf5ef2aSThomas Huth     case CC_EQ:
1177fcf5ef2aSThomas Huth     case CC_NE:
1178fcf5ef2aSThomas Huth     case CC_LT:
1179fcf5ef2aSThomas Huth     case CC_LE:
1180fcf5ef2aSThomas Huth     case CC_GE:
1181fcf5ef2aSThomas Huth     case CC_GT:
11829e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1183fcf5ef2aSThomas Huth         break;
1184fcf5ef2aSThomas Huth     default:
1185fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1186fcf5ef2aSThomas Huth         break;
1187fcf5ef2aSThomas Huth     }
1188fcf5ef2aSThomas Huth }
1189fcf5ef2aSThomas Huth 
11900f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1191fcf5ef2aSThomas Huth {
11920f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1193e956caf2SEdgar E. Iglesias 
11940f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11950f96e96bSRichard Henderson                         env_btaken, zero,
1196e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1197e956caf2SEdgar E. Iglesias 
11980f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1199fcf5ef2aSThomas Huth }
1200fcf5ef2aSThomas Huth 
1201f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1202f91c60f0SEdgar E. Iglesias {
1203f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1204f91c60f0SEdgar E. Iglesias 
1205f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1206f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1207f91c60f0SEdgar E. Iglesias 
1208f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1209f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1210f91c60f0SEdgar E. Iglesias }
1211f91c60f0SEdgar E. Iglesias 
1212fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1213fcf5ef2aSThomas Huth {
1214fcf5ef2aSThomas Huth     unsigned int cc;
1215fcf5ef2aSThomas Huth     unsigned int dslot;
1216fcf5ef2aSThomas Huth 
1217fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1218fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1219fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1220fcf5ef2aSThomas Huth 
1221fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1222fcf5ef2aSThomas Huth     if (dslot) {
1223f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1224fcf5ef2aSThomas Huth     }
1225fcf5ef2aSThomas Huth 
1226fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1227fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1228fcf5ef2aSThomas Huth 
12290f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->pc + offset);
1230fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1231fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1232fcf5ef2aSThomas Huth     } else {
1233fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
12340f96e96bSRichard Henderson         tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1235fcf5ef2aSThomas Huth     }
12369e6e1828SEdgar E. Iglesias     eval_cc(dc, cc, env_btaken, cpu_R[dc->ra]);
1237fcf5ef2aSThomas Huth }
1238fcf5ef2aSThomas Huth 
1239fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1240fcf5ef2aSThomas Huth {
1241fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1242fcf5ef2aSThomas Huth 
1243fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1244fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1245fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1246fcf5ef2aSThomas Huth 
1247fcf5ef2aSThomas Huth     /* Memory barrier.  */
1248fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1249fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1250badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1251badcbf9dSEdgar E. Iglesias 
12526f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12536f3c458bSEdgar E. Iglesias 
12543f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12553f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12563f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12573f172744SEdgar E. Iglesias         }
12583f172744SEdgar E. Iglesias 
1259fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1260badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
1261fcf5ef2aSThomas Huth             TCGv_i32 tmp_hlt = tcg_const_i32(EXCP_HLT);
1262fcf5ef2aSThomas Huth             TCGv_i32 tmp_1 = tcg_const_i32(1);
1263fcf5ef2aSThomas Huth 
1264fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1265fcf5ef2aSThomas Huth 
1266b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1267b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1268b4919e7dSEdgar E. Iglesias                 return;
1269b4919e7dSEdgar E. Iglesias             }
1270b4919e7dSEdgar E. Iglesias 
1271fcf5ef2aSThomas Huth             t_sync_flags(dc);
1272fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1273fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1274fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
12750f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
1276fcf5ef2aSThomas Huth             gen_helper_raise_exception(cpu_env, tmp_hlt);
1277fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_hlt);
1278fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
1279fcf5ef2aSThomas Huth             return;
1280fcf5ef2aSThomas Huth         }
1281fcf5ef2aSThomas Huth         /* Break the TB.  */
1282fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1283fcf5ef2aSThomas Huth         return;
1284fcf5ef2aSThomas Huth     }
1285fcf5ef2aSThomas Huth 
1286fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1287fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1288fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1289fcf5ef2aSThomas Huth              dc->imm);
1290fcf5ef2aSThomas Huth 
1291fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1292fcf5ef2aSThomas Huth     if (dslot) {
1293f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1294fcf5ef2aSThomas Huth     }
1295fcf5ef2aSThomas Huth     if (link && dc->rd)
1296cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1297fcf5ef2aSThomas Huth 
1298fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1299fcf5ef2aSThomas Huth     if (abs) {
1300cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btaken, 1);
13010f96e96bSRichard Henderson         tcg_gen_mov_i32(cpu_btarget, *(dec_alu_op_b(dc)));
1302fcf5ef2aSThomas Huth         if (link && !dslot) {
1303fcf5ef2aSThomas Huth             if (!(dc->tb_flags & IMM_FLAG) && (dc->imm == 8 || dc->imm == 0x18))
1304fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_BREAK);
1305fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1306bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1307fcf5ef2aSThomas Huth                     return;
1308fcf5ef2aSThomas Huth                 }
1309fcf5ef2aSThomas Huth 
1310fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_DEBUG);
1311fcf5ef2aSThomas Huth             }
1312fcf5ef2aSThomas Huth         }
1313fcf5ef2aSThomas Huth     } else {
1314fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1315fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1316fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1317fcf5ef2aSThomas Huth         } else {
1318cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
13190f96e96bSRichard Henderson             tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1320fcf5ef2aSThomas Huth         }
1321fcf5ef2aSThomas Huth     }
1322fcf5ef2aSThomas Huth }
1323fcf5ef2aSThomas Huth 
1324fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1325fcf5ef2aSThomas Huth {
1326cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1327cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1328cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13293e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13300a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13310a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1332cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1333fcf5ef2aSThomas Huth 
1334cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1335cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1336fcf5ef2aSThomas Huth     msr_write(dc, t1);
1337cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1338cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1339fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1340fcf5ef2aSThomas Huth }
1341fcf5ef2aSThomas Huth 
1342fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1343fcf5ef2aSThomas Huth {
1344cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1345cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1346cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13473e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13480a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1349cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1350cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1351fcf5ef2aSThomas Huth 
1352cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1353cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1354fcf5ef2aSThomas Huth     msr_write(dc, t1);
1355cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1356cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1357fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1358fcf5ef2aSThomas Huth }
1359fcf5ef2aSThomas Huth 
1360fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1361fcf5ef2aSThomas Huth {
1362cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1363cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1364cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1365fcf5ef2aSThomas Huth 
13663e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13670a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1368cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1369cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1370cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1371fcf5ef2aSThomas Huth 
1372cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1373cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1374fcf5ef2aSThomas Huth     msr_write(dc, t1);
1375cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1376cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1377fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1378fcf5ef2aSThomas Huth }
1379fcf5ef2aSThomas Huth 
1380fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1381fcf5ef2aSThomas Huth {
1382fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1385fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1386fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1387fcf5ef2aSThomas Huth 
1388bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1389bdfc1e88SEdgar E. Iglesias         return;
1390bdfc1e88SEdgar E. Iglesias     }
1391bdfc1e88SEdgar E. Iglesias 
1392f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1393fcf5ef2aSThomas Huth 
1394fcf5ef2aSThomas Huth     if (i_bit) {
1395fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1396fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1397fcf5ef2aSThomas Huth     } else if (b_bit) {
1398fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1399fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1400fcf5ef2aSThomas Huth     } else if (e_bit) {
1401fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1402fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1403fcf5ef2aSThomas Huth     } else
1404fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1405fcf5ef2aSThomas Huth 
1406fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1407cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_btaken, 1);
14080f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
14146efd5599SRichard Henderson         tcg_gen_movi_i32(cpu_esr, ESR_EC_FPU);
1415fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1416fcf5ef2aSThomas Huth     }
14172016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1418fcf5ef2aSThomas Huth }
1419fcf5ef2aSThomas Huth 
1420fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1421fcf5ef2aSThomas Huth {
1422fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1423fcf5ef2aSThomas Huth 
14249ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1425fcf5ef2aSThomas Huth         return;
1426fcf5ef2aSThomas Huth     }
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1429fcf5ef2aSThomas Huth 
1430fcf5ef2aSThomas Huth     switch (fpu_insn) {
1431fcf5ef2aSThomas Huth         case 0:
1432fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1433fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1434fcf5ef2aSThomas Huth             break;
1435fcf5ef2aSThomas Huth 
1436fcf5ef2aSThomas Huth         case 1:
1437fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1438fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1439fcf5ef2aSThomas Huth             break;
1440fcf5ef2aSThomas Huth 
1441fcf5ef2aSThomas Huth         case 2:
1442fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1443fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1444fcf5ef2aSThomas Huth             break;
1445fcf5ef2aSThomas Huth 
1446fcf5ef2aSThomas Huth         case 3:
1447fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1448fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1449fcf5ef2aSThomas Huth             break;
1450fcf5ef2aSThomas Huth 
1451fcf5ef2aSThomas Huth         case 4:
1452fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1453fcf5ef2aSThomas Huth                 case 0:
1454fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1455fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1456fcf5ef2aSThomas Huth                     break;
1457fcf5ef2aSThomas Huth                 case 1:
1458fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1459fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1460fcf5ef2aSThomas Huth                     break;
1461fcf5ef2aSThomas Huth                 case 2:
1462fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1463fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1464fcf5ef2aSThomas Huth                     break;
1465fcf5ef2aSThomas Huth                 case 3:
1466fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1467fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1468fcf5ef2aSThomas Huth                     break;
1469fcf5ef2aSThomas Huth                 case 4:
1470fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1471fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1472fcf5ef2aSThomas Huth                     break;
1473fcf5ef2aSThomas Huth                 case 5:
1474fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1475fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1476fcf5ef2aSThomas Huth                     break;
1477fcf5ef2aSThomas Huth                 case 6:
1478fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1479fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1480fcf5ef2aSThomas Huth                     break;
1481fcf5ef2aSThomas Huth                 default:
1482fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1483fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1484fcf5ef2aSThomas Huth                                   " opc=%x\n",
1485fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1486fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1487fcf5ef2aSThomas Huth                     break;
1488fcf5ef2aSThomas Huth             }
1489fcf5ef2aSThomas Huth             break;
1490fcf5ef2aSThomas Huth 
1491fcf5ef2aSThomas Huth         case 5:
1492fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1493fcf5ef2aSThomas Huth                 return;
1494fcf5ef2aSThomas Huth             }
1495fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1496fcf5ef2aSThomas Huth             break;
1497fcf5ef2aSThomas Huth 
1498fcf5ef2aSThomas Huth         case 6:
1499fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1500fcf5ef2aSThomas Huth                 return;
1501fcf5ef2aSThomas Huth             }
1502fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1503fcf5ef2aSThomas Huth             break;
1504fcf5ef2aSThomas Huth 
1505fcf5ef2aSThomas Huth         case 7:
1506fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1507fcf5ef2aSThomas Huth                 return;
1508fcf5ef2aSThomas Huth             }
1509fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1510fcf5ef2aSThomas Huth             break;
1511fcf5ef2aSThomas Huth 
1512fcf5ef2aSThomas Huth         default:
1513fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1514fcf5ef2aSThomas Huth                           " opc=%x\n",
1515fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1516fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1517fcf5ef2aSThomas Huth             break;
1518fcf5ef2aSThomas Huth     }
1519fcf5ef2aSThomas Huth }
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1522fcf5ef2aSThomas Huth {
15239ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1524fcf5ef2aSThomas Huth         return;
1525fcf5ef2aSThomas Huth     }
1526fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1527fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1528fcf5ef2aSThomas Huth }
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1531fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1532fcf5ef2aSThomas Huth {
1533fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1534fcf5ef2aSThomas Huth     int ctrl;
1535fcf5ef2aSThomas Huth 
1536fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1537fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1538fcf5ef2aSThomas Huth 
1539bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1540fcf5ef2aSThomas Huth         return;
1541fcf5ef2aSThomas Huth     }
1542fcf5ef2aSThomas Huth 
1543cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1544fcf5ef2aSThomas Huth     if (dc->type_b) {
1545cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1546fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1547fcf5ef2aSThomas Huth     } else {
1548cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1549fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1550fcf5ef2aSThomas Huth     }
1551fcf5ef2aSThomas Huth 
1552cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1553fcf5ef2aSThomas Huth 
1554fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1555fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1556fcf5ef2aSThomas Huth     } else {
1557fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1558fcf5ef2aSThomas Huth     }
1559cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1560cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1561fcf5ef2aSThomas Huth }
1562fcf5ef2aSThomas Huth 
1563fcf5ef2aSThomas Huth static struct decoder_info {
1564fcf5ef2aSThomas Huth     struct {
1565fcf5ef2aSThomas Huth         uint32_t bits;
1566fcf5ef2aSThomas Huth         uint32_t mask;
1567fcf5ef2aSThomas Huth     };
1568fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1569fcf5ef2aSThomas Huth } decinfo[] = {
1570fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1571fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1572fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1573fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1574fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1575fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1576fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1577fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1578fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1579fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1580fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1581fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1582fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1583fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1584fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1585fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1586fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1587fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1588fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1589fcf5ef2aSThomas Huth };
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1592fcf5ef2aSThomas Huth {
1593fcf5ef2aSThomas Huth     int i;
1594fcf5ef2aSThomas Huth 
1595fcf5ef2aSThomas Huth     dc->ir = ir;
1596fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1597fcf5ef2aSThomas Huth 
1598462c2544SEdgar E. Iglesias     if (ir == 0) {
15991ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1600462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1601462c2544SEdgar E. Iglesias         return;
1602462c2544SEdgar E. Iglesias     }
1603fcf5ef2aSThomas Huth 
1604fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1605fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1606fcf5ef2aSThomas Huth 
1607fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1608fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1609fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1610fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1611fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1612fcf5ef2aSThomas Huth 
1613fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1614fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1615fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1616fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1617fcf5ef2aSThomas Huth             break;
1618fcf5ef2aSThomas Huth         }
1619fcf5ef2aSThomas Huth     }
1620fcf5ef2aSThomas Huth }
1621fcf5ef2aSThomas Huth 
1622fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
16238b86d6d2SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1624fcf5ef2aSThomas Huth {
16259c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1626f5c7e93aSRichard Henderson     MicroBlazeCPU *cpu = env_archcpu(env);
1627fcf5ef2aSThomas Huth     uint32_t pc_start;
1628fcf5ef2aSThomas Huth     struct DisasContext ctx;
1629fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
163056371527SEmilio G. Cota     uint32_t page_start, org_flags;
1631cfeea807SEdgar E. Iglesias     uint32_t npc;
1632fcf5ef2aSThomas Huth     int num_insns;
1633fcf5ef2aSThomas Huth 
1634fcf5ef2aSThomas Huth     pc_start = tb->pc;
1635fcf5ef2aSThomas Huth     dc->cpu = cpu;
1636fcf5ef2aSThomas Huth     dc->tb = tb;
1637fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1638fcf5ef2aSThomas Huth 
1639fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1640fcf5ef2aSThomas Huth     dc->jmp = 0;
1641fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1642fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1643fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1644fcf5ef2aSThomas Huth     }
1645fcf5ef2aSThomas Huth     dc->pc = pc_start;
1646fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1647fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1648fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1649fcf5ef2aSThomas Huth 
1650fcf5ef2aSThomas Huth     if (pc_start & 3) {
1651fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1652fcf5ef2aSThomas Huth     }
1653fcf5ef2aSThomas Huth 
165456371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1655fcf5ef2aSThomas Huth     num_insns = 0;
1656fcf5ef2aSThomas Huth 
1657fcf5ef2aSThomas Huth     gen_tb_start(tb);
1658fcf5ef2aSThomas Huth     do
1659fcf5ef2aSThomas Huth     {
1660fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1661fcf5ef2aSThomas Huth         num_insns++;
1662fcf5ef2aSThomas Huth 
1663fcf5ef2aSThomas Huth #if SIM_COMPAT
1664fcf5ef2aSThomas Huth         if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
16650f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->pc);
1666fcf5ef2aSThomas Huth             gen_helper_debug();
1667fcf5ef2aSThomas Huth         }
1668fcf5ef2aSThomas Huth #endif
1669fcf5ef2aSThomas Huth 
1670fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
1671fcf5ef2aSThomas Huth             t_gen_raise_exception(dc, EXCP_DEBUG);
1672fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
1673fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1674fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1675fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1676fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1677fcf5ef2aSThomas Huth             dc->pc += 4;
1678fcf5ef2aSThomas Huth             break;
1679fcf5ef2aSThomas Huth         }
1680fcf5ef2aSThomas Huth 
1681fcf5ef2aSThomas Huth         /* Pretty disas.  */
1682fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1683fcf5ef2aSThomas Huth 
1684c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1685fcf5ef2aSThomas Huth             gen_io_start();
1686fcf5ef2aSThomas Huth         }
1687fcf5ef2aSThomas Huth 
1688fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1689fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1690fcf5ef2aSThomas Huth         if (dc->clear_imm)
1691fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1692fcf5ef2aSThomas Huth         dc->pc += 4;
1693fcf5ef2aSThomas Huth 
1694fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1695fcf5ef2aSThomas Huth             dc->delayed_branch--;
1696fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1697fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1698fcf5ef2aSThomas Huth                     do_rti(dc);
1699fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1700fcf5ef2aSThomas Huth                     do_rtb(dc);
1701fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1702fcf5ef2aSThomas Huth                     do_rte(dc);
1703fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1704fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1705fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1706fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
17070f96e96bSRichard Henderson                     TCGv_i32 tmp_pc = tcg_const_i32(dc->pc);
17080f96e96bSRichard Henderson                     eval_cond_jmp(dc, cpu_btarget, tmp_pc);
17090f96e96bSRichard Henderson                     tcg_temp_free_i32(tmp_pc);
1710fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1711fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1712fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1713fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1714fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1715fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1716fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1717fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1718fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
1719cfeea807SEdgar E. Iglesias                     tcg_gen_brcondi_i32(TCG_COND_NE, env_btaken, 0, l1);
1720fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1721fcf5ef2aSThomas Huth                     gen_set_label(l1);
1722fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1723fcf5ef2aSThomas Huth 
1724fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1725fcf5ef2aSThomas Huth                 }
1726fcf5ef2aSThomas Huth                 break;
1727fcf5ef2aSThomas Huth             }
1728fcf5ef2aSThomas Huth         }
1729fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1730fcf5ef2aSThomas Huth             break;
1731fcf5ef2aSThomas Huth         }
1732fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1733fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1734fcf5ef2aSThomas Huth              && !singlestep
173556371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1736fcf5ef2aSThomas Huth              && num_insns < max_insns);
1737fcf5ef2aSThomas Huth 
1738fcf5ef2aSThomas Huth     npc = dc->pc;
1739fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1740fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1741fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
17420f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1743fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1744fcf5ef2aSThomas Huth         } else
1745fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1746fcf5ef2aSThomas Huth     }
1747fcf5ef2aSThomas Huth 
1748fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1749fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1750fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1751fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
17520f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, npc);
1753fcf5ef2aSThomas Huth     }
1754fcf5ef2aSThomas Huth     t_sync_flags(dc);
1755fcf5ef2aSThomas Huth 
1756fcf5ef2aSThomas Huth     if (unlikely(cs->singlestep_enabled)) {
1757fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1758fcf5ef2aSThomas Huth 
1759fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
17600f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1761fcf5ef2aSThomas Huth         }
1762fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1763fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1764fcf5ef2aSThomas Huth     } else {
1765fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1766fcf5ef2aSThomas Huth             case DISAS_NEXT:
1767fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1768fcf5ef2aSThomas Huth                 break;
1769fcf5ef2aSThomas Huth             default:
1770fcf5ef2aSThomas Huth             case DISAS_JUMP:
1771fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1772fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1773fcf5ef2aSThomas Huth                    to find the next TB */
177407ea28b4SRichard Henderson                 tcg_gen_exit_tb(NULL, 0);
1775fcf5ef2aSThomas Huth                 break;
1776fcf5ef2aSThomas Huth             case DISAS_TB_JUMP:
1777fcf5ef2aSThomas Huth                 /* nothing more to generate */
1778fcf5ef2aSThomas Huth                 break;
1779fcf5ef2aSThomas Huth         }
1780fcf5ef2aSThomas Huth     }
1781fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1782fcf5ef2aSThomas Huth 
1783fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1784fcf5ef2aSThomas Huth     tb->icount = num_insns;
1785fcf5ef2aSThomas Huth 
1786fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1787fcf5ef2aSThomas Huth #if !SIM_COMPAT
1788fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1789fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1790fc59d2d8SRobert Foley         FILE *logfile = qemu_log_lock();
1791fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17921d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1793fc59d2d8SRobert Foley         qemu_log_unlock(logfile);
1794fcf5ef2aSThomas Huth     }
1795fcf5ef2aSThomas Huth #endif
1796fcf5ef2aSThomas Huth #endif
1797fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1798fcf5ef2aSThomas Huth }
1799fcf5ef2aSThomas Huth 
180090c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1801fcf5ef2aSThomas Huth {
1802fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1803fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1804fcf5ef2aSThomas Huth     int i;
1805fcf5ef2aSThomas Huth 
180690c84c56SMarkus Armbruster     if (!env) {
1807fcf5ef2aSThomas Huth         return;
180890c84c56SMarkus Armbruster     }
1809fcf5ef2aSThomas Huth 
18100f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
181176e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
18126efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1813ccf628b7SRichard Henderson                  "debug=%x imm=%x iflags=%x fsr=%x rbtr=%x\n",
181478e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
18155a8e0136SRichard Henderson                  env->debug, env->imm, env->iflags, env->fsr,
18166fbf78f2SRichard Henderson                  env->btr);
18170f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1818fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
18192e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18202e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18212e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18222e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18232ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
18242ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
18252ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18262ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18272ead1b18SJoe Komlodi         }
18282ead1b18SJoe Komlodi     }
1829fcf5ef2aSThomas Huth 
18302ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
183139db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1832af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
18332ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1834fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
183590c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1836fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
183790c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1838fcf5ef2aSThomas Huth         }
183990c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1840fcf5ef2aSThomas Huth }
1841fcf5ef2aSThomas Huth 
1842fcf5ef2aSThomas Huth void mb_tcg_init(void)
1843fcf5ef2aSThomas Huth {
1844fcf5ef2aSThomas Huth     int i;
1845fcf5ef2aSThomas Huth 
1846cfeea807SEdgar E. Iglesias     env_debug = tcg_global_mem_new_i32(cpu_env,
1847fcf5ef2aSThomas Huth                     offsetof(CPUMBState, debug),
1848fcf5ef2aSThomas Huth                     "debug0");
1849cfeea807SEdgar E. Iglesias     env_iflags = tcg_global_mem_new_i32(cpu_env,
1850fcf5ef2aSThomas Huth                     offsetof(CPUMBState, iflags),
1851fcf5ef2aSThomas Huth                     "iflags");
1852cfeea807SEdgar E. Iglesias     env_imm = tcg_global_mem_new_i32(cpu_env,
1853fcf5ef2aSThomas Huth                     offsetof(CPUMBState, imm),
1854fcf5ef2aSThomas Huth                     "imm");
18550f96e96bSRichard Henderson     cpu_btarget = tcg_global_mem_new_i32(cpu_env,
1856fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btarget),
1857fcf5ef2aSThomas Huth                      "btarget");
1858cfeea807SEdgar E. Iglesias     env_btaken = tcg_global_mem_new_i32(cpu_env,
1859fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btaken),
1860fcf5ef2aSThomas Huth                      "btaken");
1861403322eaSEdgar E. Iglesias     env_res_addr = tcg_global_mem_new(cpu_env,
1862fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_addr),
1863fcf5ef2aSThomas Huth                      "res_addr");
1864cfeea807SEdgar E. Iglesias     env_res_val = tcg_global_mem_new_i32(cpu_env,
1865fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_val),
1866fcf5ef2aSThomas Huth                      "res_val");
1867fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1868cfeea807SEdgar E. Iglesias         cpu_R[i] = tcg_global_mem_new_i32(cpu_env,
1869fcf5ef2aSThomas Huth                           offsetof(CPUMBState, regs[i]),
1870fcf5ef2aSThomas Huth                           regnames[i]);
1871fcf5ef2aSThomas Huth     }
187276e8187dSRichard Henderson 
1873aa28e6d4SRichard Henderson     cpu_pc =
18740f96e96bSRichard Henderson         tcg_global_mem_new_i32(cpu_env, offsetof(CPUMBState, pc), "rpc");
1875aa28e6d4SRichard Henderson     cpu_msr =
18763e0e16aeSRichard Henderson         tcg_global_mem_new_i32(cpu_env, offsetof(CPUMBState, msr), "rmsr");
1877aa28e6d4SRichard Henderson     cpu_esr =
18786efd5599SRichard Henderson         tcg_global_mem_new_i32(cpu_env, offsetof(CPUMBState, esr), "resr");
1879fcf5ef2aSThomas Huth }
1880fcf5ef2aSThomas Huth 
1881fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1882fcf5ef2aSThomas Huth                           target_ulong *data)
1883fcf5ef2aSThomas Huth {
188476e8187dSRichard Henderson     env->pc = data[0];
1885fcf5ef2aSThomas Huth }
1886