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