xref: /openbmc/qemu/target/microblaze/translate.c (revision bdfc1e886939f06b94f4fee3105a41dbd20a97db)
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"
25fcf5ef2aSThomas Huth #include "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"
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth #include "trace-tcg.h"
33fcf5ef2aSThomas Huth #include "exec/log.h"
34fcf5ef2aSThomas Huth 
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth #define SIM_COMPAT 0
37fcf5ef2aSThomas Huth #define DISAS_GNU 1
38fcf5ef2aSThomas Huth #define DISAS_MB 1
39fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
40fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
41fcf5ef2aSThomas Huth #else
42fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
43fcf5ef2aSThomas Huth #endif
44fcf5ef2aSThomas Huth 
45fcf5ef2aSThomas Huth #define D(x)
46fcf5ef2aSThomas Huth 
47fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
48fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
49fcf5ef2aSThomas Huth 
5077fc6f5eSLluís Vilanova /* is_jmp field values */
5177fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5277fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_TB_JUMP DISAS_TARGET_2 /* only pc was modified statically */
5477fc6f5eSLluís Vilanova 
55cfeea807SEdgar E. Iglesias static TCGv_i32 env_debug;
56cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
57cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_SR[14];
58cfeea807SEdgar E. Iglesias static TCGv_i32 env_imm;
59cfeea807SEdgar E. Iglesias static TCGv_i32 env_btaken;
60cfeea807SEdgar E. Iglesias static TCGv_i32 env_btarget;
61cfeea807SEdgar E. Iglesias static TCGv_i32 env_iflags;
62403322eaSEdgar E. Iglesias static TCGv env_res_addr;
63cfeea807SEdgar E. Iglesias static TCGv_i32 env_res_val;
64fcf5ef2aSThomas Huth 
65fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
66fcf5ef2aSThomas Huth 
67fcf5ef2aSThomas Huth /* This is the state at translation time.  */
68fcf5ef2aSThomas Huth typedef struct DisasContext {
69fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
70cfeea807SEdgar E. Iglesias     uint32_t pc;
71fcf5ef2aSThomas Huth 
72fcf5ef2aSThomas Huth     /* Decoder.  */
73fcf5ef2aSThomas Huth     int type_b;
74fcf5ef2aSThomas Huth     uint32_t ir;
75fcf5ef2aSThomas Huth     uint8_t opcode;
76fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
77fcf5ef2aSThomas Huth     uint16_t imm;
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
80fcf5ef2aSThomas Huth     unsigned int delayed_branch;
81fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
82fcf5ef2aSThomas Huth     unsigned int clear_imm;
83fcf5ef2aSThomas Huth     int is_jmp;
84fcf5ef2aSThomas Huth 
85fcf5ef2aSThomas Huth #define JMP_NOJMP     0
86fcf5ef2aSThomas Huth #define JMP_DIRECT    1
87fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
88fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
89fcf5ef2aSThomas Huth     unsigned int jmp;
90fcf5ef2aSThomas Huth     uint32_t jmp_pc;
91fcf5ef2aSThomas Huth 
92fcf5ef2aSThomas Huth     int abort_at_next_insn;
93fcf5ef2aSThomas Huth     int nr_nops;
94fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
95fcf5ef2aSThomas Huth     int singlestep_enabled;
96fcf5ef2aSThomas Huth } DisasContext;
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth static const char *regnames[] =
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
101fcf5ef2aSThomas Huth     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
102fcf5ef2aSThomas Huth     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
103fcf5ef2aSThomas Huth     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
104fcf5ef2aSThomas Huth };
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth static const char *special_regnames[] =
107fcf5ef2aSThomas Huth {
1080031eef2SEdgar E. Iglesias     "rpc", "rmsr", "sr2", "rear", "sr4", "resr", "sr6", "rfsr",
1090031eef2SEdgar E. Iglesias     "sr8", "sr9", "sr10", "rbtr", "sr12", "redr"
110fcf5ef2aSThomas Huth };
111fcf5ef2aSThomas Huth 
112fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
113fcf5ef2aSThomas Huth {
114fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
115fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
116cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_iflags, dc->tb_flags);
117fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
118fcf5ef2aSThomas Huth     }
119fcf5ef2aSThomas Huth }
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
122fcf5ef2aSThomas Huth {
123fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth     t_sync_flags(dc);
126cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc);
127fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
128fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
129fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_UPDATE;
130fcf5ef2aSThomas Huth }
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
135fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
136fcf5ef2aSThomas Huth #else
137fcf5ef2aSThomas Huth     return true;
138fcf5ef2aSThomas Huth #endif
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
142fcf5ef2aSThomas Huth {
143fcf5ef2aSThomas Huth     if (use_goto_tb(dc, dest)) {
144fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
145cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], dest);
146fcf5ef2aSThomas Huth         tcg_gen_exit_tb((uintptr_t)dc->tb + n);
147fcf5ef2aSThomas Huth     } else {
148cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], dest);
149fcf5ef2aSThomas Huth         tcg_gen_exit_tb(0);
150fcf5ef2aSThomas Huth     }
151fcf5ef2aSThomas Huth }
152fcf5ef2aSThomas Huth 
153cfeea807SEdgar E. Iglesias static void read_carry(DisasContext *dc, TCGv_i32 d)
154fcf5ef2aSThomas Huth {
155cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(d, cpu_SR[SR_MSR], 31);
156fcf5ef2aSThomas Huth }
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth /*
159fcf5ef2aSThomas Huth  * write_carry sets the carry bits in MSR based on bit 0 of v.
160fcf5ef2aSThomas Huth  * v[31:1] are ignored.
161fcf5ef2aSThomas Huth  */
162cfeea807SEdgar E. Iglesias static void write_carry(DisasContext *dc, TCGv_i32 v)
163fcf5ef2aSThomas Huth {
164cfeea807SEdgar E. Iglesias     TCGv_i32 t0 = tcg_temp_new_i32();
165cfeea807SEdgar E. Iglesias     tcg_gen_shli_i32(t0, v, 31);
166cfeea807SEdgar E. Iglesias     tcg_gen_sari_i32(t0, t0, 31);
167cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_C | MSR_CC));
168cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(cpu_SR[SR_MSR], cpu_SR[SR_MSR],
169fcf5ef2aSThomas Huth                     ~(MSR_C | MSR_CC));
170cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t0);
171cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
172fcf5ef2aSThomas Huth }
173fcf5ef2aSThomas Huth 
174fcf5ef2aSThomas Huth static void write_carryi(DisasContext *dc, bool carry)
175fcf5ef2aSThomas Huth {
176cfeea807SEdgar E. Iglesias     TCGv_i32 t0 = tcg_temp_new_i32();
177cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(t0, carry);
178fcf5ef2aSThomas Huth     write_carry(dc, t0);
179cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
180fcf5ef2aSThomas Huth }
181fcf5ef2aSThomas Huth 
182*bdfc1e88SEdgar E. Iglesias /*
183*bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
184*bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
185*bdfc1e88SEdgar E. Iglesias  */
186*bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
187*bdfc1e88SEdgar E. Iglesias {
188*bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
189*bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
190*bdfc1e88SEdgar E. Iglesias 
191*bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
192*bdfc1e88SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
193*bdfc1e88SEdgar E. Iglesias         t_gen_raise_exception(dc, EXCP_HW_EXCP);
194*bdfc1e88SEdgar E. Iglesias     }
195*bdfc1e88SEdgar E. Iglesias     return cond_user;
196*bdfc1e88SEdgar E. Iglesias }
197*bdfc1e88SEdgar E. Iglesias 
198fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
199fcf5ef2aSThomas Huth    faster treatment.  */
200fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
201fcf5ef2aSThomas Huth {
202fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
203fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
204fcf5ef2aSThomas Huth }
205fcf5ef2aSThomas Huth 
206cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
207fcf5ef2aSThomas Huth {
208fcf5ef2aSThomas Huth     if (dc->type_b) {
209fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
210cfeea807SEdgar E. Iglesias             tcg_gen_ori_i32(env_imm, env_imm, dc->imm);
211fcf5ef2aSThomas Huth         else
212cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_imm, (int32_t)((int16_t)dc->imm));
213fcf5ef2aSThomas Huth         return &env_imm;
214fcf5ef2aSThomas Huth     } else
215fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
216fcf5ef2aSThomas Huth }
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
219fcf5ef2aSThomas Huth {
220fcf5ef2aSThomas Huth     unsigned int k, c;
221cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
222fcf5ef2aSThomas Huth 
223fcf5ef2aSThomas Huth     k = dc->opcode & 4;
224fcf5ef2aSThomas Huth     c = dc->opcode & 2;
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
227fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
228fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
229fcf5ef2aSThomas Huth 
230fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
231fcf5ef2aSThomas Huth     if (k) {
232fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
233fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
234fcf5ef2aSThomas Huth         if (dc->rd) {
235cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
236fcf5ef2aSThomas Huth 
237fcf5ef2aSThomas Huth             if (c) {
238fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
239cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth                 read_carry(dc, cf);
242cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
243cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
244fcf5ef2aSThomas Huth             }
245fcf5ef2aSThomas Huth         }
246fcf5ef2aSThomas Huth         return;
247fcf5ef2aSThomas Huth     }
248fcf5ef2aSThomas Huth 
249fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
250fcf5ef2aSThomas Huth     /* Extract carry.  */
251cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
252fcf5ef2aSThomas Huth     if (c) {
253fcf5ef2aSThomas Huth         read_carry(dc, cf);
254fcf5ef2aSThomas Huth     } else {
255cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
256fcf5ef2aSThomas Huth     }
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth     if (dc->rd) {
259cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
260fcf5ef2aSThomas Huth         gen_helper_carry(ncf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
261cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
262cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
263fcf5ef2aSThomas Huth         write_carry(dc, ncf);
264cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
265fcf5ef2aSThomas Huth     } else {
266fcf5ef2aSThomas Huth         gen_helper_carry(cf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
267fcf5ef2aSThomas Huth         write_carry(dc, cf);
268fcf5ef2aSThomas Huth     }
269cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
270fcf5ef2aSThomas Huth }
271fcf5ef2aSThomas Huth 
272fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
273fcf5ef2aSThomas Huth {
274fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
275cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
276fcf5ef2aSThomas Huth 
277fcf5ef2aSThomas Huth     u = dc->imm & 2;
278fcf5ef2aSThomas Huth     k = dc->opcode & 4;
279fcf5ef2aSThomas Huth     c = dc->opcode & 2;
280fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
281fcf5ef2aSThomas Huth 
282fcf5ef2aSThomas Huth     if (cmp) {
283fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
284fcf5ef2aSThomas Huth         if (dc->rd) {
285fcf5ef2aSThomas Huth             if (u)
286fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
287fcf5ef2aSThomas Huth             else
288fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
289fcf5ef2aSThomas Huth         }
290fcf5ef2aSThomas Huth         return;
291fcf5ef2aSThomas Huth     }
292fcf5ef2aSThomas Huth 
293fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
294fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
297fcf5ef2aSThomas Huth     if (k) {
298fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
299fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
300fcf5ef2aSThomas Huth         if (dc->rd) {
301cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
302fcf5ef2aSThomas Huth 
303fcf5ef2aSThomas Huth             if (c) {
304fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
305cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth                 read_carry(dc, cf);
308cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
309cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
310fcf5ef2aSThomas Huth             }
311fcf5ef2aSThomas Huth         }
312fcf5ef2aSThomas Huth         return;
313fcf5ef2aSThomas Huth     }
314fcf5ef2aSThomas Huth 
315fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
316fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
317cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
318cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
319fcf5ef2aSThomas Huth     if (c) {
320fcf5ef2aSThomas Huth         read_carry(dc, cf);
321fcf5ef2aSThomas Huth     } else {
322cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
323fcf5ef2aSThomas Huth     }
324fcf5ef2aSThomas Huth 
325fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
326cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth     if (dc->rd) {
329cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
330fcf5ef2aSThomas Huth         gen_helper_carry(ncf, na, *(dec_alu_op_b(dc)), cf);
331cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
332cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
333fcf5ef2aSThomas Huth         write_carry(dc, ncf);
334cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
335fcf5ef2aSThomas Huth     } else {
336fcf5ef2aSThomas Huth         gen_helper_carry(cf, na, *(dec_alu_op_b(dc)), cf);
337fcf5ef2aSThomas Huth         write_carry(dc, cf);
338fcf5ef2aSThomas Huth     }
339cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
340cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
341fcf5ef2aSThomas Huth }
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
344fcf5ef2aSThomas Huth {
345fcf5ef2aSThomas Huth     unsigned int mode;
346fcf5ef2aSThomas Huth 
347fcf5ef2aSThomas Huth     if ((dc->tb_flags & MSR_EE_FLAG)
348fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
3498fc5239eSEdgar E. Iglesias           && !dc->cpu->cfg.use_pcmp_instr) {
350cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
351fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
352fcf5ef2aSThomas Huth     }
353fcf5ef2aSThomas Huth 
354fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
355fcf5ef2aSThomas Huth     switch (mode) {
356fcf5ef2aSThomas Huth         case 0:
357fcf5ef2aSThomas Huth             /* pcmpbf.  */
358fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
359fcf5ef2aSThomas Huth             if (dc->rd)
360fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
361fcf5ef2aSThomas Huth             break;
362fcf5ef2aSThomas Huth         case 2:
363fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
364fcf5ef2aSThomas Huth             if (dc->rd) {
365cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
366fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
367fcf5ef2aSThomas Huth             }
368fcf5ef2aSThomas Huth             break;
369fcf5ef2aSThomas Huth         case 3:
370fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
371fcf5ef2aSThomas Huth             if (dc->rd) {
372cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
373fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
374fcf5ef2aSThomas Huth             }
375fcf5ef2aSThomas Huth             break;
376fcf5ef2aSThomas Huth         default:
377fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
378fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
379fcf5ef2aSThomas Huth             break;
380fcf5ef2aSThomas Huth     }
381fcf5ef2aSThomas Huth }
382fcf5ef2aSThomas Huth 
383fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
384fcf5ef2aSThomas Huth {
385fcf5ef2aSThomas Huth     unsigned int not;
386fcf5ef2aSThomas Huth 
387fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
388fcf5ef2aSThomas Huth         dec_pattern(dc);
389fcf5ef2aSThomas Huth         return;
390fcf5ef2aSThomas Huth     }
391fcf5ef2aSThomas Huth 
392fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
393fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
394fcf5ef2aSThomas Huth 
395fcf5ef2aSThomas Huth     if (!dc->rd)
396fcf5ef2aSThomas Huth         return;
397fcf5ef2aSThomas Huth 
398fcf5ef2aSThomas Huth     if (not) {
399cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
400fcf5ef2aSThomas Huth     } else
401cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
402fcf5ef2aSThomas Huth }
403fcf5ef2aSThomas Huth 
404fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
405fcf5ef2aSThomas Huth {
406fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
407fcf5ef2aSThomas Huth         dec_pattern(dc);
408fcf5ef2aSThomas Huth         return;
409fcf5ef2aSThomas Huth     }
410fcf5ef2aSThomas Huth 
411fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
412fcf5ef2aSThomas Huth     if (dc->rd)
413cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
416fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
417fcf5ef2aSThomas Huth {
418fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
419fcf5ef2aSThomas Huth         dec_pattern(dc);
420fcf5ef2aSThomas Huth         return;
421fcf5ef2aSThomas Huth     }
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
424fcf5ef2aSThomas Huth     if (dc->rd)
425cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
426fcf5ef2aSThomas Huth }
427fcf5ef2aSThomas Huth 
428cfeea807SEdgar E. Iglesias static inline void msr_read(DisasContext *dc, TCGv_i32 d)
429fcf5ef2aSThomas Huth {
430cfeea807SEdgar E. Iglesias     tcg_gen_mov_i32(d, cpu_SR[SR_MSR]);
431fcf5ef2aSThomas Huth }
432fcf5ef2aSThomas Huth 
433cfeea807SEdgar E. Iglesias static inline void msr_write(DisasContext *dc, TCGv_i32 v)
434fcf5ef2aSThomas Huth {
435cfeea807SEdgar E. Iglesias     TCGv_i32 t;
436fcf5ef2aSThomas Huth 
437cfeea807SEdgar E. Iglesias     t = tcg_temp_new_i32();
438fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
439fcf5ef2aSThomas Huth     /* PVR bit is not writable.  */
440cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t, v, ~MSR_PVR);
441cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(cpu_SR[SR_MSR], cpu_SR[SR_MSR], MSR_PVR);
442cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t);
443fcf5ef2aSThomas Huth     tcg_temp_free(t);
444fcf5ef2aSThomas Huth }
445fcf5ef2aSThomas Huth 
446fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
447fcf5ef2aSThomas Huth {
448fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
449cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
450fcf5ef2aSThomas Huth     unsigned int sr, to, rn;
451fcf5ef2aSThomas Huth 
452fcf5ef2aSThomas Huth     sr = dc->imm & ((1 << 14) - 1);
453fcf5ef2aSThomas Huth     to = dc->imm & (1 << 14);
454fcf5ef2aSThomas Huth     dc->type_b = 1;
455fcf5ef2aSThomas Huth     if (to)
456fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
457fcf5ef2aSThomas Huth 
458fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
459fcf5ef2aSThomas Huth     if (!(dc->imm & (1 << 15))) {
460fcf5ef2aSThomas Huth         unsigned int clr = dc->ir & (1 << 16);
461fcf5ef2aSThomas Huth 
462fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
463fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
464fcf5ef2aSThomas Huth 
46556837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
466fcf5ef2aSThomas Huth             /* nop??? */
467fcf5ef2aSThomas Huth             return;
468fcf5ef2aSThomas Huth         }
469fcf5ef2aSThomas Huth 
470*bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
471fcf5ef2aSThomas Huth             return;
472fcf5ef2aSThomas Huth         }
473fcf5ef2aSThomas Huth 
474fcf5ef2aSThomas Huth         if (dc->rd)
475fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
476fcf5ef2aSThomas Huth 
477cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
478cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
479fcf5ef2aSThomas Huth         msr_read(dc, t0);
480cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
481fcf5ef2aSThomas Huth 
482fcf5ef2aSThomas Huth         if (clr) {
483cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
484cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
485fcf5ef2aSThomas Huth         } else
486cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
487fcf5ef2aSThomas Huth         msr_write(dc, t0);
488cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
489cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
490cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc + 4);
491fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
492fcf5ef2aSThomas Huth         return;
493fcf5ef2aSThomas Huth     }
494fcf5ef2aSThomas Huth 
495*bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
496fcf5ef2aSThomas Huth         return;
497fcf5ef2aSThomas Huth     }
498fcf5ef2aSThomas Huth 
499fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
500fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
501fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
502fcf5ef2aSThomas Huth         sr &= 7;
503fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
504fcf5ef2aSThomas Huth         if (to)
505cfeea807SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tcg_const_i32(sr), cpu_R[dc->ra]);
506fcf5ef2aSThomas Huth         else
507cfeea807SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tcg_const_i32(sr));
508fcf5ef2aSThomas Huth         return;
509fcf5ef2aSThomas Huth     }
510fcf5ef2aSThomas Huth #endif
511fcf5ef2aSThomas Huth 
512fcf5ef2aSThomas Huth     if (to) {
513fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
514fcf5ef2aSThomas Huth         switch (sr) {
515fcf5ef2aSThomas Huth             case 0:
516fcf5ef2aSThomas Huth                 break;
517fcf5ef2aSThomas Huth             case 1:
518fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
519fcf5ef2aSThomas Huth                 break;
520fcf5ef2aSThomas Huth             case 0x3:
521cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_SR[SR_EAR], cpu_R[dc->ra]);
522fcf5ef2aSThomas Huth                 break;
523fcf5ef2aSThomas Huth             case 0x5:
524cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_SR[SR_ESR], cpu_R[dc->ra]);
525fcf5ef2aSThomas Huth                 break;
526fcf5ef2aSThomas Huth             case 0x7:
527cfeea807SEdgar E. Iglesias                 tcg_gen_andi_i32(cpu_SR[SR_FSR], cpu_R[dc->ra], 31);
528fcf5ef2aSThomas Huth                 break;
529fcf5ef2aSThomas Huth             case 0x800:
530cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
531cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
532fcf5ef2aSThomas Huth                 break;
533fcf5ef2aSThomas Huth             case 0x802:
534cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
535cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
536fcf5ef2aSThomas Huth                 break;
537fcf5ef2aSThomas Huth             default:
538fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
539fcf5ef2aSThomas Huth                 break;
540fcf5ef2aSThomas Huth         }
541fcf5ef2aSThomas Huth     } else {
542fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth         switch (sr) {
545fcf5ef2aSThomas Huth             case 0:
546cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
547fcf5ef2aSThomas Huth                 break;
548fcf5ef2aSThomas Huth             case 1:
549fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
550fcf5ef2aSThomas Huth                 break;
551fcf5ef2aSThomas Huth             case 0x3:
552cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_R[dc->rd], cpu_SR[SR_EAR]);
553fcf5ef2aSThomas Huth                 break;
554fcf5ef2aSThomas Huth             case 0x5:
555cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_R[dc->rd], cpu_SR[SR_ESR]);
556fcf5ef2aSThomas Huth                 break;
557fcf5ef2aSThomas Huth              case 0x7:
558cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_R[dc->rd], cpu_SR[SR_FSR]);
559fcf5ef2aSThomas Huth                 break;
560fcf5ef2aSThomas Huth             case 0xb:
561cfeea807SEdgar E. Iglesias                 tcg_gen_mov_i32(cpu_R[dc->rd], cpu_SR[SR_BTR]);
562fcf5ef2aSThomas Huth                 break;
563fcf5ef2aSThomas Huth             case 0x800:
564cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
565cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
566fcf5ef2aSThomas Huth                 break;
567fcf5ef2aSThomas Huth             case 0x802:
568cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
569cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
570fcf5ef2aSThomas Huth                 break;
571fcf5ef2aSThomas Huth             case 0x2000:
572fcf5ef2aSThomas Huth             case 0x2001:
573fcf5ef2aSThomas Huth             case 0x2002:
574fcf5ef2aSThomas Huth             case 0x2003:
575fcf5ef2aSThomas Huth             case 0x2004:
576fcf5ef2aSThomas Huth             case 0x2005:
577fcf5ef2aSThomas Huth             case 0x2006:
578fcf5ef2aSThomas Huth             case 0x2007:
579fcf5ef2aSThomas Huth             case 0x2008:
580fcf5ef2aSThomas Huth             case 0x2009:
581fcf5ef2aSThomas Huth             case 0x200a:
582fcf5ef2aSThomas Huth             case 0x200b:
583fcf5ef2aSThomas Huth             case 0x200c:
584fcf5ef2aSThomas Huth                 rn = sr & 0xf;
585cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
586fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
587fcf5ef2aSThomas Huth                 break;
588fcf5ef2aSThomas Huth             default:
589fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
590fcf5ef2aSThomas Huth                 break;
591fcf5ef2aSThomas Huth         }
592fcf5ef2aSThomas Huth     }
593fcf5ef2aSThomas Huth 
594fcf5ef2aSThomas Huth     if (dc->rd == 0) {
595cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
596fcf5ef2aSThomas Huth     }
597fcf5ef2aSThomas Huth }
598fcf5ef2aSThomas Huth 
599fcf5ef2aSThomas Huth /* Multiplier unit.  */
600fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
601fcf5ef2aSThomas Huth {
602cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
603fcf5ef2aSThomas Huth     unsigned int subcode;
604fcf5ef2aSThomas Huth 
605fcf5ef2aSThomas Huth     if ((dc->tb_flags & MSR_EE_FLAG)
606fcf5ef2aSThomas Huth          && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
6079b964318SEdgar E. Iglesias          && !dc->cpu->cfg.use_hw_mul) {
608cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
609fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
610fcf5ef2aSThomas Huth         return;
611fcf5ef2aSThomas Huth     }
612fcf5ef2aSThomas Huth 
613fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth     if (dc->type_b) {
616fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
617cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
618fcf5ef2aSThomas Huth         return;
619fcf5ef2aSThomas Huth     }
620fcf5ef2aSThomas Huth 
621fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6229b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
623fcf5ef2aSThomas Huth         /* nop??? */
624fcf5ef2aSThomas Huth     }
625fcf5ef2aSThomas Huth 
626cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
627fcf5ef2aSThomas Huth     switch (subcode) {
628fcf5ef2aSThomas Huth         case 0:
629fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
630cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
631fcf5ef2aSThomas Huth             break;
632fcf5ef2aSThomas Huth         case 1:
633fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
634cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
635cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
636fcf5ef2aSThomas Huth             break;
637fcf5ef2aSThomas Huth         case 2:
638fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
639cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
640cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
641fcf5ef2aSThomas Huth             break;
642fcf5ef2aSThomas Huth         case 3:
643fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
644cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
645fcf5ef2aSThomas Huth             break;
646fcf5ef2aSThomas Huth         default:
647fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
648fcf5ef2aSThomas Huth             break;
649fcf5ef2aSThomas Huth     }
650cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
651fcf5ef2aSThomas Huth }
652fcf5ef2aSThomas Huth 
653fcf5ef2aSThomas Huth /* Div unit.  */
654fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
655fcf5ef2aSThomas Huth {
656fcf5ef2aSThomas Huth     unsigned int u;
657fcf5ef2aSThomas Huth 
658fcf5ef2aSThomas Huth     u = dc->imm & 2;
659fcf5ef2aSThomas Huth     LOG_DIS("div\n");
660fcf5ef2aSThomas Huth 
661fcf5ef2aSThomas Huth     if ((dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
66247709e4cSEdgar E. Iglesias           && !dc->cpu->cfg.use_div) {
663cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
664fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
665fcf5ef2aSThomas Huth     }
666fcf5ef2aSThomas Huth 
667fcf5ef2aSThomas Huth     if (u)
668fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
669fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
670fcf5ef2aSThomas Huth     else
671fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
672fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
673fcf5ef2aSThomas Huth     if (!dc->rd)
674cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
675fcf5ef2aSThomas Huth }
676fcf5ef2aSThomas Huth 
677fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
678fcf5ef2aSThomas Huth {
679cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
680faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
681d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
682fcf5ef2aSThomas Huth 
683fcf5ef2aSThomas Huth     if ((dc->tb_flags & MSR_EE_FLAG)
684fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
6857faa66aaSEdgar E. Iglesias           && !dc->cpu->cfg.use_barrel) {
686cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
687fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
688fcf5ef2aSThomas Huth         return;
689fcf5ef2aSThomas Huth     }
690fcf5ef2aSThomas Huth 
691faa48d74SEdgar E. Iglesias     if (dc->type_b) {
692faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
693d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
694faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
695faa48d74SEdgar E. Iglesias     }
696e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
697e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
698faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
699faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
700fcf5ef2aSThomas Huth 
701faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
702faa48d74SEdgar E. Iglesias             e ? "e" : "",
703fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
704fcf5ef2aSThomas Huth 
705faa48d74SEdgar E. Iglesias     if (e) {
706faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
707faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
708faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
709faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
710faa48d74SEdgar E. Iglesias         } else {
711faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
712faa48d74SEdgar E. Iglesias         }
713d09b2585SEdgar E. Iglesias     } else if (i) {
714d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
715d09b2585SEdgar E. Iglesias 
716d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
717d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
718d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
719d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
720d09b2585SEdgar E. Iglesias         } else {
721d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
722d09b2585SEdgar E. Iglesias                                 imm_s, width);
723d09b2585SEdgar E. Iglesias         }
724faa48d74SEdgar E. Iglesias     } else {
725cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
726fcf5ef2aSThomas Huth 
727cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
728cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
729fcf5ef2aSThomas Huth 
7302acf6d53SEdgar E. Iglesias         if (s) {
731cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7322acf6d53SEdgar E. Iglesias         } else {
7332acf6d53SEdgar E. Iglesias             if (t) {
734cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7352acf6d53SEdgar E. Iglesias             } else {
736cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
737fcf5ef2aSThomas Huth             }
738fcf5ef2aSThomas Huth         }
739cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7402acf6d53SEdgar E. Iglesias     }
741faa48d74SEdgar E. Iglesias }
742fcf5ef2aSThomas Huth 
743fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
744fcf5ef2aSThomas Huth {
745fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
746cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
747fcf5ef2aSThomas Huth     unsigned int op;
748fcf5ef2aSThomas Huth 
749fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
750fcf5ef2aSThomas Huth     switch (op) {
751fcf5ef2aSThomas Huth         case 0x21:
752fcf5ef2aSThomas Huth             /* src.  */
753cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
754fcf5ef2aSThomas Huth 
755fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
756cfeea807SEdgar E. Iglesias             tcg_gen_andi_i32(t0, cpu_SR[SR_MSR], MSR_CC);
757fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
758fcf5ef2aSThomas Huth             if (dc->rd) {
759cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
760cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
761fcf5ef2aSThomas Huth             }
762cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
763fcf5ef2aSThomas Huth             break;
764fcf5ef2aSThomas Huth 
765fcf5ef2aSThomas Huth         case 0x1:
766fcf5ef2aSThomas Huth         case 0x41:
767fcf5ef2aSThomas Huth             /* srl.  */
768fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
769fcf5ef2aSThomas Huth 
770fcf5ef2aSThomas Huth             /* Update carry. Note that write carry only looks at the LSB.  */
771fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
772fcf5ef2aSThomas Huth             if (dc->rd) {
773fcf5ef2aSThomas Huth                 if (op == 0x41)
774cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
775fcf5ef2aSThomas Huth                 else
776cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
777fcf5ef2aSThomas Huth             }
778fcf5ef2aSThomas Huth             break;
779fcf5ef2aSThomas Huth         case 0x60:
780fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
781fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
782fcf5ef2aSThomas Huth             break;
783fcf5ef2aSThomas Huth         case 0x61:
784fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
785fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
786fcf5ef2aSThomas Huth             break;
787fcf5ef2aSThomas Huth         case 0x64:
788fcf5ef2aSThomas Huth         case 0x66:
789fcf5ef2aSThomas Huth         case 0x74:
790fcf5ef2aSThomas Huth         case 0x76:
791fcf5ef2aSThomas Huth             /* wdc.  */
792fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
793*bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
794fcf5ef2aSThomas Huth             break;
795fcf5ef2aSThomas Huth         case 0x68:
796fcf5ef2aSThomas Huth             /* wic.  */
797fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
798*bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
799fcf5ef2aSThomas Huth             break;
800fcf5ef2aSThomas Huth         case 0xe0:
801fcf5ef2aSThomas Huth             if ((dc->tb_flags & MSR_EE_FLAG)
802fcf5ef2aSThomas Huth                 && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
8038fc5239eSEdgar E. Iglesias                 && !dc->cpu->cfg.use_pcmp_instr) {
804cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
805fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_HW_EXCP);
806fcf5ef2aSThomas Huth             }
8078fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8085318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
809fcf5ef2aSThomas Huth             }
810fcf5ef2aSThomas Huth             break;
811fcf5ef2aSThomas Huth         case 0x1e0:
812fcf5ef2aSThomas Huth             /* swapb */
813fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
814fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
815fcf5ef2aSThomas Huth             break;
816fcf5ef2aSThomas Huth         case 0x1e2:
817fcf5ef2aSThomas Huth             /*swaph */
818fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
819fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
820fcf5ef2aSThomas Huth             break;
821fcf5ef2aSThomas Huth         default:
822fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
823fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
824fcf5ef2aSThomas Huth             break;
825fcf5ef2aSThomas Huth     }
826fcf5ef2aSThomas Huth }
827fcf5ef2aSThomas Huth 
828fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
829fcf5ef2aSThomas Huth {
830fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
831fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
832cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
833fcf5ef2aSThomas Huth         }
834fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
835cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btarget, dc->jmp_pc);
836fcf5ef2aSThomas Huth     }
837fcf5ef2aSThomas Huth }
838fcf5ef2aSThomas Huth 
839fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
840fcf5ef2aSThomas Huth {
841fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
842cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_imm, (dc->imm << 16));
843fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
844fcf5ef2aSThomas Huth     dc->clear_imm = 0;
845fcf5ef2aSThomas Huth }
846fcf5ef2aSThomas Huth 
847403322eaSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, TCGv t)
848fcf5ef2aSThomas Huth {
8490e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8500e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8510e9033c8SEdgar E. Iglesias     bool stackprot = false;
852403322eaSEdgar E. Iglesias     TCGv_i32 t32;
853fcf5ef2aSThomas Huth 
854fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
855fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8560e9033c8SEdgar E. Iglesias         stackprot = true;
857fcf5ef2aSThomas Huth     }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
860fcf5ef2aSThomas Huth     if (!dc->type_b) {
8610dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
862fcf5ef2aSThomas Huth         if (dc->ra == 0) {
863403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8640dc4af5cSEdgar E. Iglesias             return;
865fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
866403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8670dc4af5cSEdgar E. Iglesias             return;
868fcf5ef2aSThomas Huth         }
869fcf5ef2aSThomas Huth 
870fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8710e9033c8SEdgar E. Iglesias             stackprot = true;
872fcf5ef2aSThomas Huth         }
873fcf5ef2aSThomas Huth 
874403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
875403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
876403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
877403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
878fcf5ef2aSThomas Huth 
879fcf5ef2aSThomas Huth         if (stackprot) {
8800a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
881fcf5ef2aSThomas Huth         }
8820dc4af5cSEdgar E. Iglesias         return;
883fcf5ef2aSThomas Huth     }
884fcf5ef2aSThomas Huth     /* Immediate.  */
885403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
886fcf5ef2aSThomas Huth     if (!extimm) {
887fcf5ef2aSThomas Huth         if (dc->imm == 0) {
888403322eaSEdgar E. Iglesias             tcg_gen_mov_i32(t32, cpu_R[dc->ra]);
889fcf5ef2aSThomas Huth         } else {
890403322eaSEdgar E. Iglesias             tcg_gen_movi_i32(t32, (int32_t)((int16_t)dc->imm));
891403322eaSEdgar E. Iglesias             tcg_gen_add_i32(t32, cpu_R[dc->ra], t32);
892fcf5ef2aSThomas Huth         }
893403322eaSEdgar E. Iglesias     } else {
894403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
895403322eaSEdgar E. Iglesias     }
896403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
897403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
898fcf5ef2aSThomas Huth 
899fcf5ef2aSThomas Huth     if (stackprot) {
9000a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
901fcf5ef2aSThomas Huth     }
9020dc4af5cSEdgar E. Iglesias     return;
903fcf5ef2aSThomas Huth }
904fcf5ef2aSThomas Huth 
905fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
906fcf5ef2aSThomas Huth {
907403322eaSEdgar E. Iglesias     TCGv_i32 v;
908403322eaSEdgar E. Iglesias     TCGv addr;
9098534063aSEdgar E. Iglesias     unsigned int size;
9108534063aSEdgar E. Iglesias     bool rev = false, ex = false;
911fcf5ef2aSThomas Huth     TCGMemOp mop;
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
914fcf5ef2aSThomas Huth     size = 1 << mop;
915fcf5ef2aSThomas Huth     if (!dc->type_b) {
9168534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9178534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
918fcf5ef2aSThomas Huth     }
919fcf5ef2aSThomas Huth     mop |= MO_TE;
920fcf5ef2aSThomas Huth     if (rev) {
921fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
922fcf5ef2aSThomas Huth     }
923fcf5ef2aSThomas Huth 
924fcf5ef2aSThomas Huth     if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
925fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
926cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
927fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
928fcf5ef2aSThomas Huth         return;
929fcf5ef2aSThomas Huth     }
930fcf5ef2aSThomas Huth 
931fcf5ef2aSThomas Huth     LOG_DIS("l%d%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
932fcf5ef2aSThomas Huth                                                         ex ? "x" : "");
933fcf5ef2aSThomas Huth 
934fcf5ef2aSThomas Huth     t_sync_flags(dc);
935403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
9360a87e691SEdgar E. Iglesias     compute_ldst_addr(dc, addr);
937fcf5ef2aSThomas Huth 
938fcf5ef2aSThomas Huth     /*
939fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
940fcf5ef2aSThomas Huth      *
941fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
942fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
943fcf5ef2aSThomas Huth      */
944fcf5ef2aSThomas Huth     if (rev && size != 4) {
945fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
946fcf5ef2aSThomas Huth         switch (size) {
947fcf5ef2aSThomas Huth             case 1:
948fcf5ef2aSThomas Huth             {
949fcf5ef2aSThomas Huth                 /* 00 -> 11
950fcf5ef2aSThomas Huth                    01 -> 10
951fcf5ef2aSThomas Huth                    10 -> 10
952fcf5ef2aSThomas Huth                    11 -> 00 */
953403322eaSEdgar E. Iglesias                 TCGv low = tcg_temp_new();
954fcf5ef2aSThomas Huth 
955403322eaSEdgar E. Iglesias                 tcg_gen_andi_tl(low, addr, 3);
956403322eaSEdgar E. Iglesias                 tcg_gen_sub_tl(low, tcg_const_tl(3), low);
957403322eaSEdgar E. Iglesias                 tcg_gen_andi_tl(addr, addr, ~3);
958403322eaSEdgar E. Iglesias                 tcg_gen_or_tl(addr, addr, low);
959403322eaSEdgar E. Iglesias                 tcg_temp_free(low);
960fcf5ef2aSThomas Huth                 break;
961fcf5ef2aSThomas Huth             }
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth             case 2:
964fcf5ef2aSThomas Huth                 /* 00 -> 10
965fcf5ef2aSThomas Huth                    10 -> 00.  */
966403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
967fcf5ef2aSThomas Huth                 break;
968fcf5ef2aSThomas Huth             default:
969fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
970fcf5ef2aSThomas Huth                 break;
971fcf5ef2aSThomas Huth         }
972fcf5ef2aSThomas Huth     }
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
975fcf5ef2aSThomas Huth     if (ex) {
976403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
977fcf5ef2aSThomas Huth     }
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
980fcf5ef2aSThomas Huth     sync_jmpstate(dc);
981fcf5ef2aSThomas Huth 
982fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
983fcf5ef2aSThomas Huth     /*
984fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
985fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
986fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
987fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
988fcf5ef2aSThomas Huth      */
989cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
9900dc4af5cSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, cpu_mmu_index(&dc->cpu->env, false), mop);
991fcf5ef2aSThomas Huth 
992fcf5ef2aSThomas Huth     if ((dc->cpu->env.pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
993cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc);
9940dc4af5cSEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, tcg_const_i32(dc->rd),
995cfeea807SEdgar E. Iglesias                             tcg_const_i32(0), tcg_const_i32(size - 1));
996fcf5ef2aSThomas Huth     }
997fcf5ef2aSThomas Huth 
998fcf5ef2aSThomas Huth     if (ex) {
999403322eaSEdgar E. Iglesias         tcg_gen_mov_tl(env_res_addr, addr);
1000cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(env_res_val, v);
1001fcf5ef2aSThomas Huth     }
1002fcf5ef2aSThomas Huth     if (dc->rd) {
1003cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1004fcf5ef2aSThomas Huth     }
1005cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1006fcf5ef2aSThomas Huth 
1007fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1008fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
1009fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1010fcf5ef2aSThomas Huth     }
1011fcf5ef2aSThomas Huth 
1012403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1013fcf5ef2aSThomas Huth }
1014fcf5ef2aSThomas Huth 
1015fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1016fcf5ef2aSThomas Huth {
1017403322eaSEdgar E. Iglesias     TCGv addr;
1018fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1019b51b3d43SEdgar E. Iglesias     unsigned int size;
1020b51b3d43SEdgar E. Iglesias     bool rev = false, ex = false;
1021fcf5ef2aSThomas Huth     TCGMemOp mop;
1022fcf5ef2aSThomas Huth 
1023fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1024fcf5ef2aSThomas Huth     size = 1 << mop;
1025fcf5ef2aSThomas Huth     if (!dc->type_b) {
1026b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1027b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1028fcf5ef2aSThomas Huth     }
1029fcf5ef2aSThomas Huth     mop |= MO_TE;
1030fcf5ef2aSThomas Huth     if (rev) {
1031fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1032fcf5ef2aSThomas Huth     }
1033fcf5ef2aSThomas Huth 
1034fcf5ef2aSThomas Huth     if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
1035fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
1036cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1037fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1038fcf5ef2aSThomas Huth         return;
1039fcf5ef2aSThomas Huth     }
1040fcf5ef2aSThomas Huth 
1041fcf5ef2aSThomas Huth     LOG_DIS("s%d%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1042fcf5ef2aSThomas Huth                                                         ex ? "x" : "");
1043fcf5ef2aSThomas Huth     t_sync_flags(dc);
1044fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1045fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10460dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1047403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
10480a87e691SEdgar E. Iglesias     compute_ldst_addr(dc, addr);
1049fcf5ef2aSThomas Huth 
1050fcf5ef2aSThomas Huth     if (ex) { /* swx */
1051cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1052fcf5ef2aSThomas Huth 
1053fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1054403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth         write_carryi(dc, 1);
1057fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
1058403322eaSEdgar E. Iglesias         tcg_gen_brcond_tl(TCG_COND_NE, env_res_addr, addr, swx_skip);
1059fcf5ef2aSThomas Huth 
1060fcf5ef2aSThomas Huth         /* Compare the value loaded at lwx with current contents of
1061fcf5ef2aSThomas Huth            the reserved location.
1062fcf5ef2aSThomas Huth            FIXME: This only works for system emulation where we can expect
1063fcf5ef2aSThomas Huth            this compare and the following write to be atomic. For user
1064fcf5ef2aSThomas Huth            emulation we need to add atomicity between threads.  */
1065cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
10660dc4af5cSEdgar E. Iglesias         tcg_gen_qemu_ld_i32(tval, addr, cpu_mmu_index(&dc->cpu->env, false),
1067fcf5ef2aSThomas Huth                             MO_TEUL);
1068cfeea807SEdgar E. Iglesias         tcg_gen_brcond_i32(TCG_COND_NE, env_res_val, tval, swx_skip);
1069fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1070cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1071fcf5ef2aSThomas Huth     }
1072fcf5ef2aSThomas Huth 
1073fcf5ef2aSThomas Huth     if (rev && size != 4) {
1074fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1075fcf5ef2aSThomas Huth         switch (size) {
1076fcf5ef2aSThomas Huth             case 1:
1077fcf5ef2aSThomas Huth             {
1078fcf5ef2aSThomas Huth                 /* 00 -> 11
1079fcf5ef2aSThomas Huth                    01 -> 10
1080fcf5ef2aSThomas Huth                    10 -> 10
1081fcf5ef2aSThomas Huth                    11 -> 00 */
1082403322eaSEdgar E. Iglesias                 TCGv low = tcg_temp_new();
1083fcf5ef2aSThomas Huth 
1084403322eaSEdgar E. Iglesias                 tcg_gen_andi_tl(low, addr, 3);
1085403322eaSEdgar E. Iglesias                 tcg_gen_sub_tl(low, tcg_const_tl(3), low);
1086403322eaSEdgar E. Iglesias                 tcg_gen_andi_tl(addr, addr, ~3);
1087403322eaSEdgar E. Iglesias                 tcg_gen_or_tl(addr, addr, low);
1088403322eaSEdgar E. Iglesias                 tcg_temp_free(low);
1089fcf5ef2aSThomas Huth                 break;
1090fcf5ef2aSThomas Huth             }
1091fcf5ef2aSThomas Huth 
1092fcf5ef2aSThomas Huth             case 2:
1093fcf5ef2aSThomas Huth                 /* 00 -> 10
1094fcf5ef2aSThomas Huth                    10 -> 00.  */
1095fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1096403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1097fcf5ef2aSThomas Huth                 break;
1098fcf5ef2aSThomas Huth             default:
1099fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1100fcf5ef2aSThomas Huth                 break;
1101fcf5ef2aSThomas Huth         }
1102fcf5ef2aSThomas Huth     }
11030dc4af5cSEdgar E. Iglesias     tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr,
1104cfeea807SEdgar E. Iglesias                         cpu_mmu_index(&dc->cpu->env, false), mop);
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
1107fcf5ef2aSThomas Huth     if ((dc->cpu->env.pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
1108cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc);
1109fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1110fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1111fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1112fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1113fcf5ef2aSThomas Huth          *        access.
1114fcf5ef2aSThomas Huth          */
11150dc4af5cSEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, tcg_const_i32(dc->rd),
1116cfeea807SEdgar E. Iglesias                             tcg_const_i32(1), tcg_const_i32(size - 1));
1117fcf5ef2aSThomas Huth     }
1118fcf5ef2aSThomas Huth 
1119fcf5ef2aSThomas Huth     if (ex) {
1120fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1121fcf5ef2aSThomas Huth     }
1122fcf5ef2aSThomas Huth 
1123403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1124fcf5ef2aSThomas Huth }
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
1127cfeea807SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a, TCGv_i32 b)
1128fcf5ef2aSThomas Huth {
1129fcf5ef2aSThomas Huth     switch (cc) {
1130fcf5ef2aSThomas Huth         case CC_EQ:
1131cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_EQ, d, a, b);
1132fcf5ef2aSThomas Huth             break;
1133fcf5ef2aSThomas Huth         case CC_NE:
1134cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_NE, d, a, b);
1135fcf5ef2aSThomas Huth             break;
1136fcf5ef2aSThomas Huth         case CC_LT:
1137cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_LT, d, a, b);
1138fcf5ef2aSThomas Huth             break;
1139fcf5ef2aSThomas Huth         case CC_LE:
1140cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_LE, d, a, b);
1141fcf5ef2aSThomas Huth             break;
1142fcf5ef2aSThomas Huth         case CC_GE:
1143cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_GE, d, a, b);
1144fcf5ef2aSThomas Huth             break;
1145fcf5ef2aSThomas Huth         case CC_GT:
1146cfeea807SEdgar E. Iglesias             tcg_gen_setcond_i32(TCG_COND_GT, d, a, b);
1147fcf5ef2aSThomas Huth             break;
1148fcf5ef2aSThomas Huth         default:
1149fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1150fcf5ef2aSThomas Huth             break;
1151fcf5ef2aSThomas Huth     }
1152fcf5ef2aSThomas Huth }
1153fcf5ef2aSThomas Huth 
1154cfeea807SEdgar E. Iglesias static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1155fcf5ef2aSThomas Huth {
1156fcf5ef2aSThomas Huth     TCGLabel *l1 = gen_new_label();
1157fcf5ef2aSThomas Huth     /* Conditional jmp.  */
1158cfeea807SEdgar E. Iglesias     tcg_gen_mov_i32(cpu_SR[SR_PC], pc_false);
1159cfeea807SEdgar E. Iglesias     tcg_gen_brcondi_i32(TCG_COND_EQ, env_btaken, 0, l1);
1160cfeea807SEdgar E. Iglesias     tcg_gen_mov_i32(cpu_SR[SR_PC], pc_true);
1161fcf5ef2aSThomas Huth     gen_set_label(l1);
1162fcf5ef2aSThomas Huth }
1163fcf5ef2aSThomas Huth 
1164fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1165fcf5ef2aSThomas Huth {
1166fcf5ef2aSThomas Huth     unsigned int cc;
1167fcf5ef2aSThomas Huth     unsigned int dslot;
1168fcf5ef2aSThomas Huth 
1169fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1170fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1171fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1172fcf5ef2aSThomas Huth 
1173fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1174fcf5ef2aSThomas Huth     if (dslot) {
1175fcf5ef2aSThomas Huth         dc->delayed_branch = 2;
1176fcf5ef2aSThomas Huth         dc->tb_flags |= D_FLAG;
1177cfeea807SEdgar E. Iglesias         tcg_gen_st_i32(tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1178fcf5ef2aSThomas Huth                       cpu_env, offsetof(CPUMBState, bimm));
1179fcf5ef2aSThomas Huth     }
1180fcf5ef2aSThomas Huth 
1181fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1182fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1183fcf5ef2aSThomas Huth 
1184cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btarget, dc->pc + offset);
1185fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1186fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1187fcf5ef2aSThomas Huth     } else {
1188fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1189cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btarget, dc->pc);
1190cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
1191fcf5ef2aSThomas Huth     }
1192cfeea807SEdgar E. Iglesias     eval_cc(dc, cc, env_btaken, cpu_R[dc->ra], tcg_const_i32(0));
1193fcf5ef2aSThomas Huth }
1194fcf5ef2aSThomas Huth 
1195fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1196fcf5ef2aSThomas Huth {
1197fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1198fcf5ef2aSThomas Huth 
1199fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1200fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1201fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1202fcf5ef2aSThomas Huth 
1203fcf5ef2aSThomas Huth     /* Memory barrier.  */
1204fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1205fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1206fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1207fcf5ef2aSThomas Huth         if (dc->rd & 16) {
1208fcf5ef2aSThomas Huth             TCGv_i32 tmp_hlt = tcg_const_i32(EXCP_HLT);
1209fcf5ef2aSThomas Huth             TCGv_i32 tmp_1 = tcg_const_i32(1);
1210fcf5ef2aSThomas Huth 
1211fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1212fcf5ef2aSThomas Huth 
1213fcf5ef2aSThomas Huth             t_sync_flags(dc);
1214fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1215fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1216fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1217cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc + 4);
1218fcf5ef2aSThomas Huth             gen_helper_raise_exception(cpu_env, tmp_hlt);
1219fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_hlt);
1220fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
1221fcf5ef2aSThomas Huth             return;
1222fcf5ef2aSThomas Huth         }
1223fcf5ef2aSThomas Huth         LOG_DIS("mbar %d\n", dc->rd);
1224fcf5ef2aSThomas Huth         /* Break the TB.  */
1225fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1226fcf5ef2aSThomas Huth         return;
1227fcf5ef2aSThomas Huth     }
1228fcf5ef2aSThomas Huth 
1229fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1230fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1231fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1232fcf5ef2aSThomas Huth              dc->imm);
1233fcf5ef2aSThomas Huth 
1234fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1235fcf5ef2aSThomas Huth     if (dslot) {
1236fcf5ef2aSThomas Huth         dc->delayed_branch = 2;
1237fcf5ef2aSThomas Huth         dc->tb_flags |= D_FLAG;
1238cfeea807SEdgar E. Iglesias         tcg_gen_st_i32(tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1239fcf5ef2aSThomas Huth                       cpu_env, offsetof(CPUMBState, bimm));
1240fcf5ef2aSThomas Huth     }
1241fcf5ef2aSThomas Huth     if (link && dc->rd)
1242cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1243fcf5ef2aSThomas Huth 
1244fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1245fcf5ef2aSThomas Huth     if (abs) {
1246cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btaken, 1);
1247cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(env_btarget, *(dec_alu_op_b(dc)));
1248fcf5ef2aSThomas Huth         if (link && !dslot) {
1249fcf5ef2aSThomas Huth             if (!(dc->tb_flags & IMM_FLAG) && (dc->imm == 8 || dc->imm == 0x18))
1250fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_BREAK);
1251fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1252*bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1253fcf5ef2aSThomas Huth                     return;
1254fcf5ef2aSThomas Huth                 }
1255fcf5ef2aSThomas Huth 
1256fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_DEBUG);
1257fcf5ef2aSThomas Huth             }
1258fcf5ef2aSThomas Huth         }
1259fcf5ef2aSThomas Huth     } else {
1260fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1261fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1262fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1263fcf5ef2aSThomas Huth         } else {
1264cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
1265cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btarget, dc->pc);
1266cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
1267fcf5ef2aSThomas Huth         }
1268fcf5ef2aSThomas Huth     }
1269fcf5ef2aSThomas Huth }
1270fcf5ef2aSThomas Huth 
1271fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1272fcf5ef2aSThomas Huth {
1273cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1274cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1275cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1276cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, cpu_SR[SR_MSR], 1);
1277cfeea807SEdgar E. Iglesias     tcg_gen_ori_i32(t1, cpu_SR[SR_MSR], MSR_IE);
1278cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1279fcf5ef2aSThomas Huth 
1280cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1281cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1282fcf5ef2aSThomas Huth     msr_write(dc, t1);
1283cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1284cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1285fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1286fcf5ef2aSThomas Huth }
1287fcf5ef2aSThomas Huth 
1288fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1289fcf5ef2aSThomas Huth {
1290cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1291cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1292cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1293cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, cpu_SR[SR_MSR], ~MSR_BIP);
1294cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1295cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1296fcf5ef2aSThomas Huth 
1297cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1298cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1299fcf5ef2aSThomas Huth     msr_write(dc, t1);
1300cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1301cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1302fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1303fcf5ef2aSThomas Huth }
1304fcf5ef2aSThomas Huth 
1305fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1306fcf5ef2aSThomas Huth {
1307cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1308cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1309cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1310fcf5ef2aSThomas Huth 
1311cfeea807SEdgar E. Iglesias     tcg_gen_ori_i32(t1, cpu_SR[SR_MSR], MSR_EE);
1312cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1313cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1314cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1315fcf5ef2aSThomas Huth 
1316cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1317cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1318fcf5ef2aSThomas Huth     msr_write(dc, t1);
1319cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1320cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1321fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1322fcf5ef2aSThomas Huth }
1323fcf5ef2aSThomas Huth 
1324fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1325fcf5ef2aSThomas Huth {
1326fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1327fcf5ef2aSThomas Huth 
1328fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1329fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1330fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1331fcf5ef2aSThomas Huth 
1332*bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1333*bdfc1e88SEdgar E. Iglesias         return;
1334*bdfc1e88SEdgar E. Iglesias     }
1335*bdfc1e88SEdgar E. Iglesias 
1336fcf5ef2aSThomas Huth     dc->delayed_branch = 2;
1337fcf5ef2aSThomas Huth     dc->tb_flags |= D_FLAG;
1338cfeea807SEdgar E. Iglesias     tcg_gen_st_i32(tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1339fcf5ef2aSThomas Huth                   cpu_env, offsetof(CPUMBState, bimm));
1340fcf5ef2aSThomas Huth 
1341fcf5ef2aSThomas Huth     if (i_bit) {
1342fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1343fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1344fcf5ef2aSThomas Huth     } else if (b_bit) {
1345fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1346fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1347fcf5ef2aSThomas Huth     } else if (e_bit) {
1348fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1349fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1350fcf5ef2aSThomas Huth     } else
1351fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1352fcf5ef2aSThomas Huth 
1353fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1354cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_btaken, 1);
1355cfeea807SEdgar E. Iglesias     tcg_gen_add_i32(env_btarget, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
1356fcf5ef2aSThomas Huth }
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1359fcf5ef2aSThomas Huth {
1360fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
1361cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_FPU);
1362fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1363fcf5ef2aSThomas Huth     }
1364fcf5ef2aSThomas Huth     return (dc->cpu->cfg.use_fpu == 2) ? 0 : PVR2_USE_FPU2_MASK;
1365fcf5ef2aSThomas Huth }
1366fcf5ef2aSThomas Huth 
1367fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1368fcf5ef2aSThomas Huth {
1369fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1370fcf5ef2aSThomas Huth 
1371fcf5ef2aSThomas Huth     if ((dc->tb_flags & MSR_EE_FLAG)
1372fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
13735153bb89SEdgar E. Iglesias           && !dc->cpu->cfg.use_fpu) {
1374cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1375fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1376fcf5ef2aSThomas Huth         return;
1377fcf5ef2aSThomas Huth     }
1378fcf5ef2aSThomas Huth 
1379fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1380fcf5ef2aSThomas Huth 
1381fcf5ef2aSThomas Huth     switch (fpu_insn) {
1382fcf5ef2aSThomas Huth         case 0:
1383fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1384fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1385fcf5ef2aSThomas Huth             break;
1386fcf5ef2aSThomas Huth 
1387fcf5ef2aSThomas Huth         case 1:
1388fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1389fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1390fcf5ef2aSThomas Huth             break;
1391fcf5ef2aSThomas Huth 
1392fcf5ef2aSThomas Huth         case 2:
1393fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1394fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1395fcf5ef2aSThomas Huth             break;
1396fcf5ef2aSThomas Huth 
1397fcf5ef2aSThomas Huth         case 3:
1398fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1399fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1400fcf5ef2aSThomas Huth             break;
1401fcf5ef2aSThomas Huth 
1402fcf5ef2aSThomas Huth         case 4:
1403fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1404fcf5ef2aSThomas Huth                 case 0:
1405fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1406fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1407fcf5ef2aSThomas Huth                     break;
1408fcf5ef2aSThomas Huth                 case 1:
1409fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1410fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1411fcf5ef2aSThomas Huth                     break;
1412fcf5ef2aSThomas Huth                 case 2:
1413fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1414fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1415fcf5ef2aSThomas Huth                     break;
1416fcf5ef2aSThomas Huth                 case 3:
1417fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1418fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1419fcf5ef2aSThomas Huth                     break;
1420fcf5ef2aSThomas Huth                 case 4:
1421fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1422fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1423fcf5ef2aSThomas Huth                     break;
1424fcf5ef2aSThomas Huth                 case 5:
1425fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1426fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1427fcf5ef2aSThomas Huth                     break;
1428fcf5ef2aSThomas Huth                 case 6:
1429fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1430fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1431fcf5ef2aSThomas Huth                     break;
1432fcf5ef2aSThomas Huth                 default:
1433fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1434fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1435fcf5ef2aSThomas Huth                                   " opc=%x\n",
1436fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1437fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1438fcf5ef2aSThomas Huth                     break;
1439fcf5ef2aSThomas Huth             }
1440fcf5ef2aSThomas Huth             break;
1441fcf5ef2aSThomas Huth 
1442fcf5ef2aSThomas Huth         case 5:
1443fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1444fcf5ef2aSThomas Huth                 return;
1445fcf5ef2aSThomas Huth             }
1446fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1447fcf5ef2aSThomas Huth             break;
1448fcf5ef2aSThomas Huth 
1449fcf5ef2aSThomas Huth         case 6:
1450fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1451fcf5ef2aSThomas Huth                 return;
1452fcf5ef2aSThomas Huth             }
1453fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1454fcf5ef2aSThomas Huth             break;
1455fcf5ef2aSThomas Huth 
1456fcf5ef2aSThomas Huth         case 7:
1457fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1458fcf5ef2aSThomas Huth                 return;
1459fcf5ef2aSThomas Huth             }
1460fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1461fcf5ef2aSThomas Huth             break;
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth         default:
1464fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1465fcf5ef2aSThomas Huth                           " opc=%x\n",
1466fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1467fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1468fcf5ef2aSThomas Huth             break;
1469fcf5ef2aSThomas Huth     }
1470fcf5ef2aSThomas Huth }
1471fcf5ef2aSThomas Huth 
1472fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1473fcf5ef2aSThomas Huth {
1474fcf5ef2aSThomas Huth     if ((dc->tb_flags & MSR_EE_FLAG)
1475fcf5ef2aSThomas Huth           && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
1476cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1477fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1478fcf5ef2aSThomas Huth         return;
1479fcf5ef2aSThomas Huth     }
1480fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1481fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1482fcf5ef2aSThomas Huth }
1483fcf5ef2aSThomas Huth 
1484fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1485fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1486fcf5ef2aSThomas Huth {
1487fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1488fcf5ef2aSThomas Huth     int ctrl;
1489fcf5ef2aSThomas Huth 
1490fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1491fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1492fcf5ef2aSThomas Huth 
1493*bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1494fcf5ef2aSThomas Huth         return;
1495fcf5ef2aSThomas Huth     }
1496fcf5ef2aSThomas Huth 
1497cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1498fcf5ef2aSThomas Huth     if (dc->type_b) {
1499cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1500fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1501fcf5ef2aSThomas Huth     } else {
1502cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1503fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1504fcf5ef2aSThomas Huth     }
1505fcf5ef2aSThomas Huth 
1506cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1507fcf5ef2aSThomas Huth 
1508fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1509fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1510fcf5ef2aSThomas Huth     } else {
1511fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1512fcf5ef2aSThomas Huth     }
1513cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1514cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1515fcf5ef2aSThomas Huth }
1516fcf5ef2aSThomas Huth 
1517fcf5ef2aSThomas Huth static struct decoder_info {
1518fcf5ef2aSThomas Huth     struct {
1519fcf5ef2aSThomas Huth         uint32_t bits;
1520fcf5ef2aSThomas Huth         uint32_t mask;
1521fcf5ef2aSThomas Huth     };
1522fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1523fcf5ef2aSThomas Huth } decinfo[] = {
1524fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1525fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1526fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1527fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1528fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1529fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1530fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1531fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1532fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1533fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1534fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1535fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1536fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1537fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1538fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1539fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1540fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1541fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1542fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1543fcf5ef2aSThomas Huth };
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1546fcf5ef2aSThomas Huth {
1547fcf5ef2aSThomas Huth     int i;
1548fcf5ef2aSThomas Huth 
1549fcf5ef2aSThomas Huth     dc->ir = ir;
1550fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1551fcf5ef2aSThomas Huth 
1552fcf5ef2aSThomas Huth     if (dc->ir)
1553fcf5ef2aSThomas Huth         dc->nr_nops = 0;
1554fcf5ef2aSThomas Huth     else {
1555fcf5ef2aSThomas Huth         if ((dc->tb_flags & MSR_EE_FLAG)
1556fcf5ef2aSThomas Huth               && (dc->cpu->env.pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
1557fcf5ef2aSThomas Huth               && (dc->cpu->env.pvr.regs[2] & PVR2_OPCODE_0x0_ILL_MASK)) {
1558cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1559fcf5ef2aSThomas Huth             t_gen_raise_exception(dc, EXCP_HW_EXCP);
1560fcf5ef2aSThomas Huth             return;
1561fcf5ef2aSThomas Huth         }
1562fcf5ef2aSThomas Huth 
1563fcf5ef2aSThomas Huth         LOG_DIS("nr_nops=%d\t", dc->nr_nops);
1564fcf5ef2aSThomas Huth         dc->nr_nops++;
1565fcf5ef2aSThomas Huth         if (dc->nr_nops > 4) {
1566fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "fetching nop sequence\n");
1567fcf5ef2aSThomas Huth         }
1568fcf5ef2aSThomas Huth     }
1569fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1570fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1571fcf5ef2aSThomas Huth 
1572fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1573fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1574fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1575fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1576fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1577fcf5ef2aSThomas Huth 
1578fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1579fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1580fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1581fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1582fcf5ef2aSThomas Huth             break;
1583fcf5ef2aSThomas Huth         }
1584fcf5ef2aSThomas Huth     }
1585fcf5ef2aSThomas Huth }
1586fcf5ef2aSThomas Huth 
1587fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
15889c489ea6SLluís Vilanova void gen_intermediate_code(CPUState *cs, struct TranslationBlock *tb)
1589fcf5ef2aSThomas Huth {
15909c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1591fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = mb_env_get_cpu(env);
1592fcf5ef2aSThomas Huth     uint32_t pc_start;
1593fcf5ef2aSThomas Huth     struct DisasContext ctx;
1594fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
159556371527SEmilio G. Cota     uint32_t page_start, org_flags;
1596cfeea807SEdgar E. Iglesias     uint32_t npc;
1597fcf5ef2aSThomas Huth     int num_insns;
1598fcf5ef2aSThomas Huth     int max_insns;
1599fcf5ef2aSThomas Huth 
1600fcf5ef2aSThomas Huth     pc_start = tb->pc;
1601fcf5ef2aSThomas Huth     dc->cpu = cpu;
1602fcf5ef2aSThomas Huth     dc->tb = tb;
1603fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1604fcf5ef2aSThomas Huth 
1605fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1606fcf5ef2aSThomas Huth     dc->jmp = 0;
1607fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1608fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1609fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1610fcf5ef2aSThomas Huth     }
1611fcf5ef2aSThomas Huth     dc->pc = pc_start;
1612fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1613fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1614fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1615fcf5ef2aSThomas Huth     dc->nr_nops = 0;
1616fcf5ef2aSThomas Huth 
1617fcf5ef2aSThomas Huth     if (pc_start & 3) {
1618fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1619fcf5ef2aSThomas Huth     }
1620fcf5ef2aSThomas Huth 
162156371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1622fcf5ef2aSThomas Huth     num_insns = 0;
1623c5a49c63SEmilio G. Cota     max_insns = tb_cflags(tb) & CF_COUNT_MASK;
1624fcf5ef2aSThomas Huth     if (max_insns == 0) {
1625fcf5ef2aSThomas Huth         max_insns = CF_COUNT_MASK;
1626fcf5ef2aSThomas Huth     }
1627fcf5ef2aSThomas Huth     if (max_insns > TCG_MAX_INSNS) {
1628fcf5ef2aSThomas Huth         max_insns = TCG_MAX_INSNS;
1629fcf5ef2aSThomas Huth     }
1630fcf5ef2aSThomas Huth 
1631fcf5ef2aSThomas Huth     gen_tb_start(tb);
1632fcf5ef2aSThomas Huth     do
1633fcf5ef2aSThomas Huth     {
1634fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1635fcf5ef2aSThomas Huth         num_insns++;
1636fcf5ef2aSThomas Huth 
1637fcf5ef2aSThomas Huth #if SIM_COMPAT
1638fcf5ef2aSThomas Huth         if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1639cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(cpu_SR[SR_PC], dc->pc);
1640fcf5ef2aSThomas Huth             gen_helper_debug();
1641fcf5ef2aSThomas Huth         }
1642fcf5ef2aSThomas Huth #endif
1643fcf5ef2aSThomas Huth 
1644fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
1645fcf5ef2aSThomas Huth             t_gen_raise_exception(dc, EXCP_DEBUG);
1646fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
1647fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1648fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1649fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1650fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1651fcf5ef2aSThomas Huth             dc->pc += 4;
1652fcf5ef2aSThomas Huth             break;
1653fcf5ef2aSThomas Huth         }
1654fcf5ef2aSThomas Huth 
1655fcf5ef2aSThomas Huth         /* Pretty disas.  */
1656fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1657fcf5ef2aSThomas Huth 
1658c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1659fcf5ef2aSThomas Huth             gen_io_start();
1660fcf5ef2aSThomas Huth         }
1661fcf5ef2aSThomas Huth 
1662fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1663fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1664fcf5ef2aSThomas Huth         if (dc->clear_imm)
1665fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1666fcf5ef2aSThomas Huth         dc->pc += 4;
1667fcf5ef2aSThomas Huth 
1668fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1669fcf5ef2aSThomas Huth             dc->delayed_branch--;
1670fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1671fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1672fcf5ef2aSThomas Huth                     do_rti(dc);
1673fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1674fcf5ef2aSThomas Huth                     do_rtb(dc);
1675fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1676fcf5ef2aSThomas Huth                     do_rte(dc);
1677fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1678fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1679fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1680fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
1681cfeea807SEdgar E. Iglesias                     eval_cond_jmp(dc, env_btarget, tcg_const_i32(dc->pc));
1682fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1683fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1684fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1685fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1686fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1687fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1688fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1689fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1690fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
1691cfeea807SEdgar E. Iglesias                     tcg_gen_brcondi_i32(TCG_COND_NE, env_btaken, 0, l1);
1692fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1693fcf5ef2aSThomas Huth                     gen_set_label(l1);
1694fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1695fcf5ef2aSThomas Huth 
1696fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1697fcf5ef2aSThomas Huth                 }
1698fcf5ef2aSThomas Huth                 break;
1699fcf5ef2aSThomas Huth             }
1700fcf5ef2aSThomas Huth         }
1701fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1702fcf5ef2aSThomas Huth             break;
1703fcf5ef2aSThomas Huth         }
1704fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1705fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1706fcf5ef2aSThomas Huth              && !singlestep
170756371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1708fcf5ef2aSThomas Huth              && num_insns < max_insns);
1709fcf5ef2aSThomas Huth 
1710fcf5ef2aSThomas Huth     npc = dc->pc;
1711fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1712fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1713fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
1714cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(cpu_SR[SR_PC], npc);
1715fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1716fcf5ef2aSThomas Huth         } else
1717fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1718fcf5ef2aSThomas Huth     }
1719fcf5ef2aSThomas Huth 
1720c5a49c63SEmilio G. Cota     if (tb_cflags(tb) & CF_LAST_IO)
1721fcf5ef2aSThomas Huth         gen_io_end();
1722fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1723fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1724fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1725fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
1726cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_SR[SR_PC], npc);
1727fcf5ef2aSThomas Huth     }
1728fcf5ef2aSThomas Huth     t_sync_flags(dc);
1729fcf5ef2aSThomas Huth 
1730fcf5ef2aSThomas Huth     if (unlikely(cs->singlestep_enabled)) {
1731fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1732fcf5ef2aSThomas Huth 
1733fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
1734cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(cpu_SR[SR_PC], npc);
1735fcf5ef2aSThomas Huth         }
1736fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1737fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1738fcf5ef2aSThomas Huth     } else {
1739fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1740fcf5ef2aSThomas Huth             case DISAS_NEXT:
1741fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1742fcf5ef2aSThomas Huth                 break;
1743fcf5ef2aSThomas Huth             default:
1744fcf5ef2aSThomas Huth             case DISAS_JUMP:
1745fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1746fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1747fcf5ef2aSThomas Huth                    to find the next TB */
1748fcf5ef2aSThomas Huth                 tcg_gen_exit_tb(0);
1749fcf5ef2aSThomas Huth                 break;
1750fcf5ef2aSThomas Huth             case DISAS_TB_JUMP:
1751fcf5ef2aSThomas Huth                 /* nothing more to generate */
1752fcf5ef2aSThomas Huth                 break;
1753fcf5ef2aSThomas Huth         }
1754fcf5ef2aSThomas Huth     }
1755fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1756fcf5ef2aSThomas Huth 
1757fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1758fcf5ef2aSThomas Huth     tb->icount = num_insns;
1759fcf5ef2aSThomas Huth 
1760fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1761fcf5ef2aSThomas Huth #if !SIM_COMPAT
1762fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1763fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1764fcf5ef2aSThomas Huth         qemu_log_lock();
1765fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17661d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1767fcf5ef2aSThomas Huth         qemu_log_unlock();
1768fcf5ef2aSThomas Huth     }
1769fcf5ef2aSThomas Huth #endif
1770fcf5ef2aSThomas Huth #endif
1771fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1772fcf5ef2aSThomas Huth }
1773fcf5ef2aSThomas Huth 
1774fcf5ef2aSThomas Huth void mb_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
1775fcf5ef2aSThomas Huth                        int flags)
1776fcf5ef2aSThomas Huth {
1777fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1778fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1779fcf5ef2aSThomas Huth     int i;
1780fcf5ef2aSThomas Huth 
1781fcf5ef2aSThomas Huth     if (!env || !f)
1782fcf5ef2aSThomas Huth         return;
1783fcf5ef2aSThomas Huth 
1784fcf5ef2aSThomas Huth     cpu_fprintf(f, "IN: PC=%x %s\n",
1785fcf5ef2aSThomas Huth                 env->sregs[SR_PC], lookup_symbol(env->sregs[SR_PC]));
1786fcf5ef2aSThomas Huth     cpu_fprintf(f, "rmsr=%x resr=%x rear=%x debug=%x imm=%x iflags=%x fsr=%x\n",
1787fcf5ef2aSThomas Huth              env->sregs[SR_MSR], env->sregs[SR_ESR], env->sregs[SR_EAR],
1788fcf5ef2aSThomas Huth              env->debug, env->imm, env->iflags, env->sregs[SR_FSR]);
1789fcf5ef2aSThomas Huth     cpu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1790fcf5ef2aSThomas Huth              env->btaken, env->btarget,
1791fcf5ef2aSThomas Huth              (env->sregs[SR_MSR] & MSR_UM) ? "user" : "kernel",
1792fcf5ef2aSThomas Huth              (env->sregs[SR_MSR] & MSR_UMS) ? "user" : "kernel",
1793fcf5ef2aSThomas Huth              (env->sregs[SR_MSR] & MSR_EIP),
1794fcf5ef2aSThomas Huth              (env->sregs[SR_MSR] & MSR_IE));
1795fcf5ef2aSThomas Huth 
1796fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
1797fcf5ef2aSThomas Huth         cpu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1798fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
1799fcf5ef2aSThomas Huth             cpu_fprintf(f, "\n");
1800fcf5ef2aSThomas Huth         }
1801fcf5ef2aSThomas Huth     cpu_fprintf(f, "\n\n");
1802fcf5ef2aSThomas Huth }
1803fcf5ef2aSThomas Huth 
1804fcf5ef2aSThomas Huth void mb_tcg_init(void)
1805fcf5ef2aSThomas Huth {
1806fcf5ef2aSThomas Huth     int i;
1807fcf5ef2aSThomas Huth 
1808cfeea807SEdgar E. Iglesias     env_debug = tcg_global_mem_new_i32(cpu_env,
1809fcf5ef2aSThomas Huth                     offsetof(CPUMBState, debug),
1810fcf5ef2aSThomas Huth                     "debug0");
1811cfeea807SEdgar E. Iglesias     env_iflags = tcg_global_mem_new_i32(cpu_env,
1812fcf5ef2aSThomas Huth                     offsetof(CPUMBState, iflags),
1813fcf5ef2aSThomas Huth                     "iflags");
1814cfeea807SEdgar E. Iglesias     env_imm = tcg_global_mem_new_i32(cpu_env,
1815fcf5ef2aSThomas Huth                     offsetof(CPUMBState, imm),
1816fcf5ef2aSThomas Huth                     "imm");
1817cfeea807SEdgar E. Iglesias     env_btarget = tcg_global_mem_new_i32(cpu_env,
1818fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btarget),
1819fcf5ef2aSThomas Huth                      "btarget");
1820cfeea807SEdgar E. Iglesias     env_btaken = tcg_global_mem_new_i32(cpu_env,
1821fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btaken),
1822fcf5ef2aSThomas Huth                      "btaken");
1823403322eaSEdgar E. Iglesias     env_res_addr = tcg_global_mem_new(cpu_env,
1824fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_addr),
1825fcf5ef2aSThomas Huth                      "res_addr");
1826cfeea807SEdgar E. Iglesias     env_res_val = tcg_global_mem_new_i32(cpu_env,
1827fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_val),
1828fcf5ef2aSThomas Huth                      "res_val");
1829fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1830cfeea807SEdgar E. Iglesias         cpu_R[i] = tcg_global_mem_new_i32(cpu_env,
1831fcf5ef2aSThomas Huth                           offsetof(CPUMBState, regs[i]),
1832fcf5ef2aSThomas Huth                           regnames[i]);
1833fcf5ef2aSThomas Huth     }
1834fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(cpu_SR); i++) {
1835cfeea807SEdgar E. Iglesias         cpu_SR[i] = tcg_global_mem_new_i32(cpu_env,
1836fcf5ef2aSThomas Huth                           offsetof(CPUMBState, sregs[i]),
1837fcf5ef2aSThomas Huth                           special_regnames[i]);
1838fcf5ef2aSThomas Huth     }
1839fcf5ef2aSThomas Huth }
1840fcf5ef2aSThomas Huth 
1841fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1842fcf5ef2aSThomas Huth                           target_ulong *data)
1843fcf5ef2aSThomas Huth {
1844fcf5ef2aSThomas Huth     env->sregs[SR_PC] = data[0];
1845fcf5ef2aSThomas Huth }
1846