xref: /openbmc/qemu/target/microblaze/translate.c (revision 6fbf78f24a43c57925dc4e789dc236cdec443987)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Xilinx MicroBlaze emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Edgar E. Iglesias.
5fcf5ef2aSThomas Huth  *  Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "disas/disas.h"
24fcf5ef2aSThomas Huth #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "microblaze-decode.h"
28fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
29fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
3077fc6f5eSLluís Vilanova #include "exec/translator.h"
3190c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "trace-tcg.h"
34fcf5ef2aSThomas Huth #include "exec/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth #define SIM_COMPAT 0
38fcf5ef2aSThomas Huth #define DISAS_GNU 1
39fcf5ef2aSThomas Huth #define DISAS_MB 1
40fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
41fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42fcf5ef2aSThomas Huth #else
43fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
44fcf5ef2aSThomas Huth #endif
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth #define D(x)
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
49fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
50fcf5ef2aSThomas Huth 
5177fc6f5eSLluís Vilanova /* is_jmp field values */
5277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5477fc6f5eSLluís Vilanova #define DISAS_TB_JUMP DISAS_TARGET_2 /* only pc was modified statically */
5577fc6f5eSLluís Vilanova 
56cfeea807SEdgar E. Iglesias static TCGv_i32 env_debug;
57cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
580a22f8cfSEdgar E. Iglesias static TCGv_i64 cpu_SR[14];
59cfeea807SEdgar E. Iglesias static TCGv_i32 env_imm;
60cfeea807SEdgar E. Iglesias static TCGv_i32 env_btaken;
6143d318b2SEdgar E. Iglesias static TCGv_i64 env_btarget;
62cfeea807SEdgar E. Iglesias static TCGv_i32 env_iflags;
63403322eaSEdgar E. Iglesias static TCGv env_res_addr;
64cfeea807SEdgar E. Iglesias static TCGv_i32 env_res_val;
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth /* This is the state at translation time.  */
69fcf5ef2aSThomas Huth typedef struct DisasContext {
70fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
71cfeea807SEdgar E. Iglesias     uint32_t pc;
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth     /* Decoder.  */
74fcf5ef2aSThomas Huth     int type_b;
75fcf5ef2aSThomas Huth     uint32_t ir;
76fcf5ef2aSThomas Huth     uint8_t opcode;
77fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
78fcf5ef2aSThomas Huth     uint16_t imm;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
81fcf5ef2aSThomas Huth     unsigned int delayed_branch;
82fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
83fcf5ef2aSThomas Huth     unsigned int clear_imm;
84fcf5ef2aSThomas Huth     int is_jmp;
85fcf5ef2aSThomas Huth 
86fcf5ef2aSThomas Huth #define JMP_NOJMP     0
87fcf5ef2aSThomas Huth #define JMP_DIRECT    1
88fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
89fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
90fcf5ef2aSThomas Huth     unsigned int jmp;
91fcf5ef2aSThomas Huth     uint32_t jmp_pc;
92fcf5ef2aSThomas Huth 
93fcf5ef2aSThomas Huth     int abort_at_next_insn;
94fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
95fcf5ef2aSThomas Huth     int singlestep_enabled;
96fcf5ef2aSThomas Huth } DisasContext;
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth static const char *regnames[] =
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
101fcf5ef2aSThomas Huth     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
102fcf5ef2aSThomas Huth     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
103fcf5ef2aSThomas Huth     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
104fcf5ef2aSThomas Huth };
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth static const char *special_regnames[] =
107fcf5ef2aSThomas Huth {
1080031eef2SEdgar E. Iglesias     "rpc", "rmsr", "sr2", "rear", "sr4", "resr", "sr6", "rfsr",
1090031eef2SEdgar E. Iglesias     "sr8", "sr9", "sr10", "rbtr", "sr12", "redr"
110fcf5ef2aSThomas Huth };
111fcf5ef2aSThomas Huth 
112fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
113fcf5ef2aSThomas Huth {
114fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
115fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
116cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_iflags, dc->tb_flags);
117fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
118fcf5ef2aSThomas Huth     }
119fcf5ef2aSThomas Huth }
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
122fcf5ef2aSThomas Huth {
123fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth     t_sync_flags(dc);
1260a22f8cfSEdgar E. Iglesias     tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc);
127fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
128fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
129fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_UPDATE;
130fcf5ef2aSThomas Huth }
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
135fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
136fcf5ef2aSThomas Huth #else
137fcf5ef2aSThomas Huth     return true;
138fcf5ef2aSThomas Huth #endif
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
142fcf5ef2aSThomas Huth {
143fcf5ef2aSThomas Huth     if (use_goto_tb(dc, dest)) {
144fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1450a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], dest);
14607ea28b4SRichard Henderson         tcg_gen_exit_tb(dc->tb, n);
147fcf5ef2aSThomas Huth     } else {
1480a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], dest);
14907ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
150fcf5ef2aSThomas Huth     }
151fcf5ef2aSThomas Huth }
152fcf5ef2aSThomas Huth 
153cfeea807SEdgar E. Iglesias static void read_carry(DisasContext *dc, TCGv_i32 d)
154fcf5ef2aSThomas Huth {
1550a22f8cfSEdgar E. Iglesias     tcg_gen_extrl_i64_i32(d, cpu_SR[SR_MSR]);
1560a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(d, d, 31);
157fcf5ef2aSThomas Huth }
158fcf5ef2aSThomas Huth 
159fcf5ef2aSThomas Huth /*
160fcf5ef2aSThomas Huth  * write_carry sets the carry bits in MSR based on bit 0 of v.
161fcf5ef2aSThomas Huth  * v[31:1] are ignored.
162fcf5ef2aSThomas Huth  */
163cfeea807SEdgar E. Iglesias static void write_carry(DisasContext *dc, TCGv_i32 v)
164fcf5ef2aSThomas Huth {
1650a22f8cfSEdgar E. Iglesias     TCGv_i64 t0 = tcg_temp_new_i64();
1660a22f8cfSEdgar E. Iglesias     tcg_gen_extu_i32_i64(t0, v);
1670a22f8cfSEdgar E. Iglesias     /* Deposit bit 0 into MSR_C and the alias MSR_CC.  */
1680a22f8cfSEdgar E. Iglesias     tcg_gen_deposit_i64(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t0, 2, 1);
1690a22f8cfSEdgar E. Iglesias     tcg_gen_deposit_i64(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t0, 31, 1);
1700a22f8cfSEdgar E. Iglesias     tcg_temp_free_i64(t0);
171fcf5ef2aSThomas Huth }
172fcf5ef2aSThomas Huth 
173fcf5ef2aSThomas Huth static void write_carryi(DisasContext *dc, bool carry)
174fcf5ef2aSThomas Huth {
175cfeea807SEdgar E. Iglesias     TCGv_i32 t0 = tcg_temp_new_i32();
176cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(t0, carry);
177fcf5ef2aSThomas Huth     write_carry(dc, t0);
178cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
179fcf5ef2aSThomas Huth }
180fcf5ef2aSThomas Huth 
181bdfc1e88SEdgar E. Iglesias /*
1829ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1839ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1849ba8cd45SEdgar E. Iglesias  */
1859ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1869ba8cd45SEdgar E. Iglesias {
1879ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1885143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
1890a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1909ba8cd45SEdgar E. Iglesias         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1919ba8cd45SEdgar E. Iglesias     }
1929ba8cd45SEdgar E. Iglesias     return cond;
1939ba8cd45SEdgar E. Iglesias }
1949ba8cd45SEdgar E. Iglesias 
1959ba8cd45SEdgar E. Iglesias /*
196bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
197bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
198bdfc1e88SEdgar E. Iglesias  */
199bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
200bdfc1e88SEdgar E. Iglesias {
201bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
202bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
203bdfc1e88SEdgar E. Iglesias 
204bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
2050a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
206bdfc1e88SEdgar E. Iglesias         t_gen_raise_exception(dc, EXCP_HW_EXCP);
207bdfc1e88SEdgar E. Iglesias     }
208bdfc1e88SEdgar E. Iglesias     return cond_user;
209bdfc1e88SEdgar E. Iglesias }
210bdfc1e88SEdgar E. Iglesias 
211fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
212fcf5ef2aSThomas Huth    faster treatment.  */
213fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
214fcf5ef2aSThomas Huth {
215fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
216fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
217fcf5ef2aSThomas Huth }
218fcf5ef2aSThomas Huth 
219cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
220fcf5ef2aSThomas Huth {
221fcf5ef2aSThomas Huth     if (dc->type_b) {
222fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
223cfeea807SEdgar E. Iglesias             tcg_gen_ori_i32(env_imm, env_imm, dc->imm);
224fcf5ef2aSThomas Huth         else
225cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_imm, (int32_t)((int16_t)dc->imm));
226fcf5ef2aSThomas Huth         return &env_imm;
227fcf5ef2aSThomas Huth     } else
228fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
229fcf5ef2aSThomas Huth }
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
232fcf5ef2aSThomas Huth {
233fcf5ef2aSThomas Huth     unsigned int k, c;
234cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
235fcf5ef2aSThomas Huth 
236fcf5ef2aSThomas Huth     k = dc->opcode & 4;
237fcf5ef2aSThomas Huth     c = dc->opcode & 2;
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
240fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
241fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
244fcf5ef2aSThomas Huth     if (k) {
245fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
246fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
247fcf5ef2aSThomas Huth         if (dc->rd) {
248cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
249fcf5ef2aSThomas Huth 
250fcf5ef2aSThomas Huth             if (c) {
251fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
252cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
253fcf5ef2aSThomas Huth 
254fcf5ef2aSThomas Huth                 read_carry(dc, cf);
255cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
256cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
257fcf5ef2aSThomas Huth             }
258fcf5ef2aSThomas Huth         }
259fcf5ef2aSThomas Huth         return;
260fcf5ef2aSThomas Huth     }
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
263fcf5ef2aSThomas Huth     /* Extract carry.  */
264cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
265fcf5ef2aSThomas Huth     if (c) {
266fcf5ef2aSThomas Huth         read_carry(dc, cf);
267fcf5ef2aSThomas Huth     } else {
268cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
269fcf5ef2aSThomas Huth     }
270fcf5ef2aSThomas Huth 
271fcf5ef2aSThomas Huth     if (dc->rd) {
272cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
273fcf5ef2aSThomas Huth         gen_helper_carry(ncf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
274cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
275cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
276fcf5ef2aSThomas Huth         write_carry(dc, ncf);
277cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
278fcf5ef2aSThomas Huth     } else {
279fcf5ef2aSThomas Huth         gen_helper_carry(cf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
280fcf5ef2aSThomas Huth         write_carry(dc, cf);
281fcf5ef2aSThomas Huth     }
282cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
283fcf5ef2aSThomas Huth }
284fcf5ef2aSThomas Huth 
285fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
286fcf5ef2aSThomas Huth {
287fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
288cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth     u = dc->imm & 2;
291fcf5ef2aSThomas Huth     k = dc->opcode & 4;
292fcf5ef2aSThomas Huth     c = dc->opcode & 2;
293fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
294fcf5ef2aSThomas Huth 
295fcf5ef2aSThomas Huth     if (cmp) {
296fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
297fcf5ef2aSThomas Huth         if (dc->rd) {
298fcf5ef2aSThomas Huth             if (u)
299fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
300fcf5ef2aSThomas Huth             else
301fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
302fcf5ef2aSThomas Huth         }
303fcf5ef2aSThomas Huth         return;
304fcf5ef2aSThomas Huth     }
305fcf5ef2aSThomas Huth 
306fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
307fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
308fcf5ef2aSThomas Huth 
309fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
310fcf5ef2aSThomas Huth     if (k) {
311fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
312fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
313fcf5ef2aSThomas Huth         if (dc->rd) {
314cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth             if (c) {
317fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
318cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth                 read_carry(dc, cf);
321cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
322cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
323fcf5ef2aSThomas Huth             }
324fcf5ef2aSThomas Huth         }
325fcf5ef2aSThomas Huth         return;
326fcf5ef2aSThomas Huth     }
327fcf5ef2aSThomas Huth 
328fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
329fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
330cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
331cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
332fcf5ef2aSThomas Huth     if (c) {
333fcf5ef2aSThomas Huth         read_carry(dc, cf);
334fcf5ef2aSThomas Huth     } else {
335cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
336fcf5ef2aSThomas Huth     }
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
339cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
340fcf5ef2aSThomas Huth 
341fcf5ef2aSThomas Huth     if (dc->rd) {
342cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
343fcf5ef2aSThomas Huth         gen_helper_carry(ncf, na, *(dec_alu_op_b(dc)), cf);
344cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
345cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
346fcf5ef2aSThomas Huth         write_carry(dc, ncf);
347cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
348fcf5ef2aSThomas Huth     } else {
349fcf5ef2aSThomas Huth         gen_helper_carry(cf, na, *(dec_alu_op_b(dc)), cf);
350fcf5ef2aSThomas Huth         write_carry(dc, cf);
351fcf5ef2aSThomas Huth     }
352cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
353cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
354fcf5ef2aSThomas Huth }
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
357fcf5ef2aSThomas Huth {
358fcf5ef2aSThomas Huth     unsigned int mode;
359fcf5ef2aSThomas Huth 
3609ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3619ba8cd45SEdgar E. Iglesias         return;
362fcf5ef2aSThomas Huth     }
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
365fcf5ef2aSThomas Huth     switch (mode) {
366fcf5ef2aSThomas Huth         case 0:
367fcf5ef2aSThomas Huth             /* pcmpbf.  */
368fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
369fcf5ef2aSThomas Huth             if (dc->rd)
370fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
371fcf5ef2aSThomas Huth             break;
372fcf5ef2aSThomas Huth         case 2:
373fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
374fcf5ef2aSThomas Huth             if (dc->rd) {
375cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
376fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
377fcf5ef2aSThomas Huth             }
378fcf5ef2aSThomas Huth             break;
379fcf5ef2aSThomas Huth         case 3:
380fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
381fcf5ef2aSThomas Huth             if (dc->rd) {
382cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
383fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
384fcf5ef2aSThomas Huth             }
385fcf5ef2aSThomas Huth             break;
386fcf5ef2aSThomas Huth         default:
387fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
388fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
389fcf5ef2aSThomas Huth             break;
390fcf5ef2aSThomas Huth     }
391fcf5ef2aSThomas Huth }
392fcf5ef2aSThomas Huth 
393fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
394fcf5ef2aSThomas Huth {
395fcf5ef2aSThomas Huth     unsigned int not;
396fcf5ef2aSThomas Huth 
397fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
398fcf5ef2aSThomas Huth         dec_pattern(dc);
399fcf5ef2aSThomas Huth         return;
400fcf5ef2aSThomas Huth     }
401fcf5ef2aSThomas Huth 
402fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
403fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
404fcf5ef2aSThomas Huth 
405fcf5ef2aSThomas Huth     if (!dc->rd)
406fcf5ef2aSThomas Huth         return;
407fcf5ef2aSThomas Huth 
408fcf5ef2aSThomas Huth     if (not) {
409cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
410fcf5ef2aSThomas Huth     } else
411cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
412fcf5ef2aSThomas Huth }
413fcf5ef2aSThomas Huth 
414fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
415fcf5ef2aSThomas Huth {
416fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
417fcf5ef2aSThomas Huth         dec_pattern(dc);
418fcf5ef2aSThomas Huth         return;
419fcf5ef2aSThomas Huth     }
420fcf5ef2aSThomas Huth 
421fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
422fcf5ef2aSThomas Huth     if (dc->rd)
423cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
424fcf5ef2aSThomas Huth }
425fcf5ef2aSThomas Huth 
426fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
427fcf5ef2aSThomas Huth {
428fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
429fcf5ef2aSThomas Huth         dec_pattern(dc);
430fcf5ef2aSThomas Huth         return;
431fcf5ef2aSThomas Huth     }
432fcf5ef2aSThomas Huth 
433fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
434fcf5ef2aSThomas Huth     if (dc->rd)
435cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
436fcf5ef2aSThomas Huth }
437fcf5ef2aSThomas Huth 
438cfeea807SEdgar E. Iglesias static inline void msr_read(DisasContext *dc, TCGv_i32 d)
439fcf5ef2aSThomas Huth {
4400a22f8cfSEdgar E. Iglesias     tcg_gen_extrl_i64_i32(d, cpu_SR[SR_MSR]);
441fcf5ef2aSThomas Huth }
442fcf5ef2aSThomas Huth 
443cfeea807SEdgar E. Iglesias static inline void msr_write(DisasContext *dc, TCGv_i32 v)
444fcf5ef2aSThomas Huth {
4450a22f8cfSEdgar E. Iglesias     TCGv_i64 t;
446fcf5ef2aSThomas Huth 
4470a22f8cfSEdgar E. Iglesias     t = tcg_temp_new_i64();
448fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
449fcf5ef2aSThomas Huth     /* PVR bit is not writable.  */
4500a22f8cfSEdgar E. Iglesias     tcg_gen_extu_i32_i64(t, v);
4510a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i64(t, t, ~MSR_PVR);
4520a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i64(cpu_SR[SR_MSR], cpu_SR[SR_MSR], MSR_PVR);
4530a22f8cfSEdgar E. Iglesias     tcg_gen_or_i64(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t);
4540a22f8cfSEdgar E. Iglesias     tcg_temp_free_i64(t);
455fcf5ef2aSThomas Huth }
456fcf5ef2aSThomas Huth 
457fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
458fcf5ef2aSThomas Huth {
459fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
460cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4612023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
462f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
463fcf5ef2aSThomas Huth 
4642023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4652023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4662023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
467fcf5ef2aSThomas Huth     dc->type_b = 1;
4682023e9a3SEdgar E. Iglesias     if (to) {
469fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
470f0f7e7f7SEdgar E. Iglesias     }
471f0f7e7f7SEdgar E. Iglesias 
472f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
473f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
474f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
475f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
476f0f7e7f7SEdgar E. Iglesias 
477f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4782023e9a3SEdgar E. Iglesias     }
479fcf5ef2aSThomas Huth 
480fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4812023e9a3SEdgar E. Iglesias     if (clrset) {
4822023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
483fcf5ef2aSThomas Huth 
484fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
485fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
486fcf5ef2aSThomas Huth 
48756837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
488fcf5ef2aSThomas Huth             /* nop??? */
489fcf5ef2aSThomas Huth             return;
490fcf5ef2aSThomas Huth         }
491fcf5ef2aSThomas Huth 
492bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
493fcf5ef2aSThomas Huth             return;
494fcf5ef2aSThomas Huth         }
495fcf5ef2aSThomas Huth 
496fcf5ef2aSThomas Huth         if (dc->rd)
497fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
498fcf5ef2aSThomas Huth 
499cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
500cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
501fcf5ef2aSThomas Huth         msr_read(dc, t0);
502cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
503fcf5ef2aSThomas Huth 
504fcf5ef2aSThomas Huth         if (clr) {
505cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
506cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
507fcf5ef2aSThomas Huth         } else
508cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
509fcf5ef2aSThomas Huth         msr_write(dc, t0);
510cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
511cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
5120a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc + 4);
513fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
514fcf5ef2aSThomas Huth         return;
515fcf5ef2aSThomas Huth     }
516fcf5ef2aSThomas Huth 
517bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
518fcf5ef2aSThomas Huth         return;
519fcf5ef2aSThomas Huth     }
520fcf5ef2aSThomas Huth 
521fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
522fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
523fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
524f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
52505a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
52605a9a651SEdgar E. Iglesias 
527fcf5ef2aSThomas Huth         sr &= 7;
52805a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
529fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
53005a9a651SEdgar E. Iglesias         if (to) {
531f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
53205a9a651SEdgar E. Iglesias         } else {
533f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
53405a9a651SEdgar E. Iglesias         }
53505a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
536f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
537fcf5ef2aSThomas Huth         return;
538fcf5ef2aSThomas Huth     }
539fcf5ef2aSThomas Huth #endif
540fcf5ef2aSThomas Huth 
541fcf5ef2aSThomas Huth     if (to) {
542fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
543fcf5ef2aSThomas Huth         switch (sr) {
544fcf5ef2aSThomas Huth             case 0:
545fcf5ef2aSThomas Huth                 break;
546fcf5ef2aSThomas Huth             case 1:
547fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
548fcf5ef2aSThomas Huth                 break;
549351527b7SEdgar E. Iglesias             case SR_EAR:
550351527b7SEdgar E. Iglesias             case SR_ESR:
551ab6dd380SEdgar E. Iglesias             case SR_FSR:
5520a22f8cfSEdgar E. Iglesias                 tcg_gen_extu_i32_i64(cpu_SR[sr], cpu_R[dc->ra]);
553fcf5ef2aSThomas Huth                 break;
554fcf5ef2aSThomas Huth             case 0x800:
555cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
556cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
557fcf5ef2aSThomas Huth                 break;
558fcf5ef2aSThomas Huth             case 0x802:
559cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
560cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
561fcf5ef2aSThomas Huth                 break;
562fcf5ef2aSThomas Huth             default:
563fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
564fcf5ef2aSThomas Huth                 break;
565fcf5ef2aSThomas Huth         }
566fcf5ef2aSThomas Huth     } else {
567fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
568fcf5ef2aSThomas Huth 
569fcf5ef2aSThomas Huth         switch (sr) {
570fcf5ef2aSThomas Huth             case 0:
571cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
572fcf5ef2aSThomas Huth                 break;
573fcf5ef2aSThomas Huth             case 1:
574fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
575fcf5ef2aSThomas Huth                 break;
576351527b7SEdgar E. Iglesias             case SR_EAR:
577a1b48e3aSEdgar E. Iglesias                 if (extended) {
578a1b48e3aSEdgar E. Iglesias                     tcg_gen_extrh_i64_i32(cpu_R[dc->rd], cpu_SR[sr]);
579a1b48e3aSEdgar E. Iglesias                     break;
580a1b48e3aSEdgar E. Iglesias                 }
581351527b7SEdgar E. Iglesias             case SR_ESR:
582351527b7SEdgar E. Iglesias             case SR_FSR:
583351527b7SEdgar E. Iglesias             case SR_BTR:
5847cdae31dSTong Ho             case SR_EDR:
5850a22f8cfSEdgar E. Iglesias                 tcg_gen_extrl_i64_i32(cpu_R[dc->rd], cpu_SR[sr]);
586fcf5ef2aSThomas Huth                 break;
587fcf5ef2aSThomas Huth             case 0x800:
588cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
589cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
590fcf5ef2aSThomas Huth                 break;
591fcf5ef2aSThomas Huth             case 0x802:
592cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
593cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
594fcf5ef2aSThomas Huth                 break;
595351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
596fcf5ef2aSThomas Huth                 rn = sr & 0xf;
597cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
598fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
599fcf5ef2aSThomas Huth                 break;
600fcf5ef2aSThomas Huth             default:
601fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
602fcf5ef2aSThomas Huth                 break;
603fcf5ef2aSThomas Huth         }
604fcf5ef2aSThomas Huth     }
605fcf5ef2aSThomas Huth 
606fcf5ef2aSThomas Huth     if (dc->rd == 0) {
607cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
608fcf5ef2aSThomas Huth     }
609fcf5ef2aSThomas Huth }
610fcf5ef2aSThomas Huth 
611fcf5ef2aSThomas Huth /* Multiplier unit.  */
612fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
613fcf5ef2aSThomas Huth {
614cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
615fcf5ef2aSThomas Huth     unsigned int subcode;
616fcf5ef2aSThomas Huth 
6179ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
618fcf5ef2aSThomas Huth         return;
619fcf5ef2aSThomas Huth     }
620fcf5ef2aSThomas Huth 
621fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
622fcf5ef2aSThomas Huth 
623fcf5ef2aSThomas Huth     if (dc->type_b) {
624fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
625cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
626fcf5ef2aSThomas Huth         return;
627fcf5ef2aSThomas Huth     }
628fcf5ef2aSThomas Huth 
629fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6309b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
631fcf5ef2aSThomas Huth         /* nop??? */
632fcf5ef2aSThomas Huth     }
633fcf5ef2aSThomas Huth 
634cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
635fcf5ef2aSThomas Huth     switch (subcode) {
636fcf5ef2aSThomas Huth         case 0:
637fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
638cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
639fcf5ef2aSThomas Huth             break;
640fcf5ef2aSThomas Huth         case 1:
641fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
642cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
643cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
644fcf5ef2aSThomas Huth             break;
645fcf5ef2aSThomas Huth         case 2:
646fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
647cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
648cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
649fcf5ef2aSThomas Huth             break;
650fcf5ef2aSThomas Huth         case 3:
651fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
652cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
653fcf5ef2aSThomas Huth             break;
654fcf5ef2aSThomas Huth         default:
655fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
656fcf5ef2aSThomas Huth             break;
657fcf5ef2aSThomas Huth     }
658cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
659fcf5ef2aSThomas Huth }
660fcf5ef2aSThomas Huth 
661fcf5ef2aSThomas Huth /* Div unit.  */
662fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
663fcf5ef2aSThomas Huth {
664fcf5ef2aSThomas Huth     unsigned int u;
665fcf5ef2aSThomas Huth 
666fcf5ef2aSThomas Huth     u = dc->imm & 2;
667fcf5ef2aSThomas Huth     LOG_DIS("div\n");
668fcf5ef2aSThomas Huth 
6699ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6709ba8cd45SEdgar E. Iglesias         return;
671fcf5ef2aSThomas Huth     }
672fcf5ef2aSThomas Huth 
673fcf5ef2aSThomas Huth     if (u)
674fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
675fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
676fcf5ef2aSThomas Huth     else
677fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
678fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
679fcf5ef2aSThomas Huth     if (!dc->rd)
680cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
681fcf5ef2aSThomas Huth }
682fcf5ef2aSThomas Huth 
683fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
684fcf5ef2aSThomas Huth {
685cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
686faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
687d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
688fcf5ef2aSThomas Huth 
6899ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
690fcf5ef2aSThomas Huth         return;
691fcf5ef2aSThomas Huth     }
692fcf5ef2aSThomas Huth 
693faa48d74SEdgar E. Iglesias     if (dc->type_b) {
694faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
695d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
696faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
697faa48d74SEdgar E. Iglesias     }
698e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
699e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
700faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
701faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
702fcf5ef2aSThomas Huth 
703faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
704faa48d74SEdgar E. Iglesias             e ? "e" : "",
705fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
706fcf5ef2aSThomas Huth 
707faa48d74SEdgar E. Iglesias     if (e) {
708faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
709faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
710faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
711faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
712faa48d74SEdgar E. Iglesias         } else {
713faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
714faa48d74SEdgar E. Iglesias         }
715d09b2585SEdgar E. Iglesias     } else if (i) {
716d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
717d09b2585SEdgar E. Iglesias 
718d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
719d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
720d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
721d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
722d09b2585SEdgar E. Iglesias         } else {
723d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
724d09b2585SEdgar E. Iglesias                                 imm_s, width);
725d09b2585SEdgar E. Iglesias         }
726faa48d74SEdgar E. Iglesias     } else {
727cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
728fcf5ef2aSThomas Huth 
729cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
730cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
731fcf5ef2aSThomas Huth 
7322acf6d53SEdgar E. Iglesias         if (s) {
733cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7342acf6d53SEdgar E. Iglesias         } else {
7352acf6d53SEdgar E. Iglesias             if (t) {
736cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7372acf6d53SEdgar E. Iglesias             } else {
738cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
739fcf5ef2aSThomas Huth             }
740fcf5ef2aSThomas Huth         }
741cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7422acf6d53SEdgar E. Iglesias     }
743faa48d74SEdgar E. Iglesias }
744fcf5ef2aSThomas Huth 
745fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
746fcf5ef2aSThomas Huth {
747fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
748cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
749fcf5ef2aSThomas Huth     unsigned int op;
750fcf5ef2aSThomas Huth 
751fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
752fcf5ef2aSThomas Huth     switch (op) {
753fcf5ef2aSThomas Huth         case 0x21:
754fcf5ef2aSThomas Huth             /* src.  */
755cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
756fcf5ef2aSThomas Huth 
757fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7580a22f8cfSEdgar E. Iglesias             tcg_gen_extrl_i64_i32(t0, cpu_SR[SR_MSR]);
7590a22f8cfSEdgar E. Iglesias             tcg_gen_andi_i32(t0, t0, MSR_CC);
760fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
761fcf5ef2aSThomas Huth             if (dc->rd) {
762cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
763cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
764fcf5ef2aSThomas Huth             }
765cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
766fcf5ef2aSThomas Huth             break;
767fcf5ef2aSThomas Huth 
768fcf5ef2aSThomas Huth         case 0x1:
769fcf5ef2aSThomas Huth         case 0x41:
770fcf5ef2aSThomas Huth             /* srl.  */
771fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
772fcf5ef2aSThomas Huth 
773fcf5ef2aSThomas Huth             /* Update carry. Note that write carry only looks at the LSB.  */
774fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
775fcf5ef2aSThomas Huth             if (dc->rd) {
776fcf5ef2aSThomas Huth                 if (op == 0x41)
777cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
778fcf5ef2aSThomas Huth                 else
779cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
780fcf5ef2aSThomas Huth             }
781fcf5ef2aSThomas Huth             break;
782fcf5ef2aSThomas Huth         case 0x60:
783fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
784fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
785fcf5ef2aSThomas Huth             break;
786fcf5ef2aSThomas Huth         case 0x61:
787fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
788fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
789fcf5ef2aSThomas Huth             break;
790fcf5ef2aSThomas Huth         case 0x64:
791fcf5ef2aSThomas Huth         case 0x66:
792fcf5ef2aSThomas Huth         case 0x74:
793fcf5ef2aSThomas Huth         case 0x76:
794fcf5ef2aSThomas Huth             /* wdc.  */
795fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
796bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
797fcf5ef2aSThomas Huth             break;
798fcf5ef2aSThomas Huth         case 0x68:
799fcf5ef2aSThomas Huth             /* wic.  */
800fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
801bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
802fcf5ef2aSThomas Huth             break;
803fcf5ef2aSThomas Huth         case 0xe0:
8049ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
8059ba8cd45SEdgar E. Iglesias                 return;
806fcf5ef2aSThomas Huth             }
8078fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8085318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
809fcf5ef2aSThomas Huth             }
810fcf5ef2aSThomas Huth             break;
811fcf5ef2aSThomas Huth         case 0x1e0:
812fcf5ef2aSThomas Huth             /* swapb */
813fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
814fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
815fcf5ef2aSThomas Huth             break;
816fcf5ef2aSThomas Huth         case 0x1e2:
817fcf5ef2aSThomas Huth             /*swaph */
818fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
819fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
820fcf5ef2aSThomas Huth             break;
821fcf5ef2aSThomas Huth         default:
822fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
823fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
824fcf5ef2aSThomas Huth             break;
825fcf5ef2aSThomas Huth     }
826fcf5ef2aSThomas Huth }
827fcf5ef2aSThomas Huth 
828fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
829fcf5ef2aSThomas Huth {
830fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
831fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
832cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
833fcf5ef2aSThomas Huth         }
834fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
83543d318b2SEdgar E. Iglesias         tcg_gen_movi_i64(env_btarget, dc->jmp_pc);
836fcf5ef2aSThomas Huth     }
837fcf5ef2aSThomas Huth }
838fcf5ef2aSThomas Huth 
839fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
840fcf5ef2aSThomas Huth {
841fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
842cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_imm, (dc->imm << 16));
843fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
844fcf5ef2aSThomas Huth     dc->clear_imm = 0;
845fcf5ef2aSThomas Huth }
846fcf5ef2aSThomas Huth 
847d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
848fcf5ef2aSThomas Huth {
8490e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8500e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8510e9033c8SEdgar E. Iglesias     bool stackprot = false;
852403322eaSEdgar E. Iglesias     TCGv_i32 t32;
853fcf5ef2aSThomas Huth 
854fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
855fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8560e9033c8SEdgar E. Iglesias         stackprot = true;
857fcf5ef2aSThomas Huth     }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
860fcf5ef2aSThomas Huth     if (!dc->type_b) {
861d248e1beSEdgar E. Iglesias         if (ea) {
862d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
863d248e1beSEdgar E. Iglesias 
864d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
865d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
866d248e1beSEdgar E. Iglesias                 return;
867d248e1beSEdgar E. Iglesias             }
868d248e1beSEdgar E. Iglesias 
869d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
870d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
871d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
872d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
873d248e1beSEdgar E. Iglesias             }
874d248e1beSEdgar E. Iglesias             return;
875d248e1beSEdgar E. Iglesias         }
876d248e1beSEdgar E. Iglesias 
8770dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
878fcf5ef2aSThomas Huth         if (dc->ra == 0) {
879403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8800dc4af5cSEdgar E. Iglesias             return;
881fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
882403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8830dc4af5cSEdgar E. Iglesias             return;
884fcf5ef2aSThomas Huth         }
885fcf5ef2aSThomas Huth 
886fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8870e9033c8SEdgar E. Iglesias             stackprot = true;
888fcf5ef2aSThomas Huth         }
889fcf5ef2aSThomas Huth 
890403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
891403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
892403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
893403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
894fcf5ef2aSThomas Huth 
895fcf5ef2aSThomas Huth         if (stackprot) {
8960a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
897fcf5ef2aSThomas Huth         }
8980dc4af5cSEdgar E. Iglesias         return;
899fcf5ef2aSThomas Huth     }
900fcf5ef2aSThomas Huth     /* Immediate.  */
901403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
902fcf5ef2aSThomas Huth     if (!extimm) {
903f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
904403322eaSEdgar E. Iglesias     } else {
905403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
906403322eaSEdgar E. Iglesias     }
907403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
908403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth     if (stackprot) {
9110a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
912fcf5ef2aSThomas Huth     }
9130dc4af5cSEdgar E. Iglesias     return;
914fcf5ef2aSThomas Huth }
915fcf5ef2aSThomas Huth 
916fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
917fcf5ef2aSThomas Huth {
918403322eaSEdgar E. Iglesias     TCGv_i32 v;
919403322eaSEdgar E. Iglesias     TCGv addr;
9208534063aSEdgar E. Iglesias     unsigned int size;
921d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
922d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
92314776ab5STony Nguyen     MemOp mop;
924fcf5ef2aSThomas Huth 
925fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
926fcf5ef2aSThomas Huth     size = 1 << mop;
927fcf5ef2aSThomas Huth     if (!dc->type_b) {
928d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9298534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9308534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
931fcf5ef2aSThomas Huth     }
932fcf5ef2aSThomas Huth     mop |= MO_TE;
933fcf5ef2aSThomas Huth     if (rev) {
934fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
935fcf5ef2aSThomas Huth     }
936fcf5ef2aSThomas Huth 
9379ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
938fcf5ef2aSThomas Huth         return;
939fcf5ef2aSThomas Huth     }
940fcf5ef2aSThomas Huth 
941d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
942d248e1beSEdgar E. Iglesias         return;
943d248e1beSEdgar E. Iglesias     }
944d248e1beSEdgar E. Iglesias 
945d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
946d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
947d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
948fcf5ef2aSThomas Huth 
949fcf5ef2aSThomas Huth     t_sync_flags(dc);
950403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
951d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
952d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
953d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
954fcf5ef2aSThomas Huth 
955fcf5ef2aSThomas Huth     /*
956fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
957fcf5ef2aSThomas Huth      *
958fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
959fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
960fcf5ef2aSThomas Huth      */
961fcf5ef2aSThomas Huth     if (rev && size != 4) {
962fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
963fcf5ef2aSThomas Huth         switch (size) {
964fcf5ef2aSThomas Huth             case 1:
965fcf5ef2aSThomas Huth             {
966a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
967fcf5ef2aSThomas Huth                 break;
968fcf5ef2aSThomas Huth             }
969fcf5ef2aSThomas Huth 
970fcf5ef2aSThomas Huth             case 2:
971fcf5ef2aSThomas Huth                 /* 00 -> 10
972fcf5ef2aSThomas Huth                    10 -> 00.  */
973403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
974fcf5ef2aSThomas Huth                 break;
975fcf5ef2aSThomas Huth             default:
976fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
977fcf5ef2aSThomas Huth                 break;
978fcf5ef2aSThomas Huth         }
979fcf5ef2aSThomas Huth     }
980fcf5ef2aSThomas Huth 
981fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
982fcf5ef2aSThomas Huth     if (ex) {
983403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
984fcf5ef2aSThomas Huth     }
985fcf5ef2aSThomas Huth 
986fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
987fcf5ef2aSThomas Huth     sync_jmpstate(dc);
988fcf5ef2aSThomas Huth 
989fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
990fcf5ef2aSThomas Huth     /*
991fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
992fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
993fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
994fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
995fcf5ef2aSThomas Huth      */
996cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
997d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
998fcf5ef2aSThomas Huth 
9991507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1000a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
1001a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1002a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1003a6338015SEdgar E. Iglesias 
10040a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc);
1005a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
1006a6338015SEdgar E. Iglesias 
1007a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1008a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1009a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1010fcf5ef2aSThomas Huth     }
1011fcf5ef2aSThomas Huth 
1012fcf5ef2aSThomas Huth     if (ex) {
1013403322eaSEdgar E. Iglesias         tcg_gen_mov_tl(env_res_addr, addr);
1014cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(env_res_val, v);
1015fcf5ef2aSThomas Huth     }
1016fcf5ef2aSThomas Huth     if (dc->rd) {
1017cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1018fcf5ef2aSThomas Huth     }
1019cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1020fcf5ef2aSThomas Huth 
1021fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1022fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
1023fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1024fcf5ef2aSThomas Huth     }
1025fcf5ef2aSThomas Huth 
1026403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1027fcf5ef2aSThomas Huth }
1028fcf5ef2aSThomas Huth 
1029fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1030fcf5ef2aSThomas Huth {
1031403322eaSEdgar E. Iglesias     TCGv addr;
1032fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1033b51b3d43SEdgar E. Iglesias     unsigned int size;
1034d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1035d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
103614776ab5STony Nguyen     MemOp mop;
1037fcf5ef2aSThomas Huth 
1038fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1039fcf5ef2aSThomas Huth     size = 1 << mop;
1040fcf5ef2aSThomas Huth     if (!dc->type_b) {
1041d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1042b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1043b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1044fcf5ef2aSThomas Huth     }
1045fcf5ef2aSThomas Huth     mop |= MO_TE;
1046fcf5ef2aSThomas Huth     if (rev) {
1047fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1048fcf5ef2aSThomas Huth     }
1049fcf5ef2aSThomas Huth 
10509ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1051fcf5ef2aSThomas Huth         return;
1052fcf5ef2aSThomas Huth     }
1053fcf5ef2aSThomas Huth 
1054d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1055d248e1beSEdgar E. Iglesias 
1056d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1057d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1058d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1059fcf5ef2aSThomas Huth     t_sync_flags(dc);
1060fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1061fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10620dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1063403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1064d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1065d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1066d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth     if (ex) { /* swx */
1069cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1072403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1073fcf5ef2aSThomas Huth 
1074fcf5ef2aSThomas Huth         write_carryi(dc, 1);
1075fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
1076403322eaSEdgar E. Iglesias         tcg_gen_brcond_tl(TCG_COND_NE, env_res_addr, addr, swx_skip);
1077fcf5ef2aSThomas Huth 
1078071cdc67SEdgar E. Iglesias         /*
1079071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1080071cdc67SEdgar E. Iglesias          * the reserved location.
1081071cdc67SEdgar E. Iglesias          */
1082cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1083071cdc67SEdgar E. Iglesias 
1084071cdc67SEdgar E. Iglesias         tcg_gen_atomic_cmpxchg_i32(tval, addr, env_res_val,
1085071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1086071cdc67SEdgar E. Iglesias                                    mop);
1087071cdc67SEdgar E. Iglesias 
1088cfeea807SEdgar E. Iglesias         tcg_gen_brcond_i32(TCG_COND_NE, env_res_val, tval, swx_skip);
1089fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1090cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1091fcf5ef2aSThomas Huth     }
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth     if (rev && size != 4) {
1094fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1095fcf5ef2aSThomas Huth         switch (size) {
1096fcf5ef2aSThomas Huth             case 1:
1097fcf5ef2aSThomas Huth             {
1098a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1099fcf5ef2aSThomas Huth                 break;
1100fcf5ef2aSThomas Huth             }
1101fcf5ef2aSThomas Huth 
1102fcf5ef2aSThomas Huth             case 2:
1103fcf5ef2aSThomas Huth                 /* 00 -> 10
1104fcf5ef2aSThomas Huth                    10 -> 00.  */
1105fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1106403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1107fcf5ef2aSThomas Huth                 break;
1108fcf5ef2aSThomas Huth             default:
1109fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1110fcf5ef2aSThomas Huth                 break;
1111fcf5ef2aSThomas Huth         }
1112fcf5ef2aSThomas Huth     }
1113071cdc67SEdgar E. Iglesias 
1114071cdc67SEdgar E. Iglesias     if (!ex) {
1115d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1116071cdc67SEdgar E. Iglesias     }
1117fcf5ef2aSThomas Huth 
1118fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11191507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1120a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1121a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1122a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1123a6338015SEdgar E. Iglesias 
11240a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc);
1125fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1126fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1127fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1128fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1129fcf5ef2aSThomas Huth          *        access.
1130fcf5ef2aSThomas Huth          */
1131a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1132a6338015SEdgar E. Iglesias 
1133a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1134a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1135a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1136fcf5ef2aSThomas Huth     }
1137fcf5ef2aSThomas Huth 
1138fcf5ef2aSThomas Huth     if (ex) {
1139fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1140fcf5ef2aSThomas Huth     }
1141fcf5ef2aSThomas Huth 
1142403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1143fcf5ef2aSThomas Huth }
1144fcf5ef2aSThomas Huth 
1145fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11469e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1147fcf5ef2aSThomas Huth {
1148d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1149d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1150d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1151d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1152d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1153d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1154d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1155d89b86e9SEdgar E. Iglesias     };
1156d89b86e9SEdgar E. Iglesias 
1157fcf5ef2aSThomas Huth     switch (cc) {
1158fcf5ef2aSThomas Huth     case CC_EQ:
1159fcf5ef2aSThomas Huth     case CC_NE:
1160fcf5ef2aSThomas Huth     case CC_LT:
1161fcf5ef2aSThomas Huth     case CC_LE:
1162fcf5ef2aSThomas Huth     case CC_GE:
1163fcf5ef2aSThomas Huth     case CC_GT:
11649e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1165fcf5ef2aSThomas Huth         break;
1166fcf5ef2aSThomas Huth     default:
1167fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1168fcf5ef2aSThomas Huth         break;
1169fcf5ef2aSThomas Huth     }
1170fcf5ef2aSThomas Huth }
1171fcf5ef2aSThomas Huth 
117243d318b2SEdgar E. Iglesias static void eval_cond_jmp(DisasContext *dc, TCGv_i64 pc_true, TCGv_i64 pc_false)
1173fcf5ef2aSThomas Huth {
1174e956caf2SEdgar E. Iglesias     TCGv_i64 tmp_btaken = tcg_temp_new_i64();
1175e956caf2SEdgar E. Iglesias     TCGv_i64 tmp_zero = tcg_const_i64(0);
1176e956caf2SEdgar E. Iglesias 
1177e956caf2SEdgar E. Iglesias     tcg_gen_extu_i32_i64(tmp_btaken, env_btaken);
1178e956caf2SEdgar E. Iglesias     tcg_gen_movcond_i64(TCG_COND_NE, cpu_SR[SR_PC],
1179e956caf2SEdgar E. Iglesias                         tmp_btaken, tmp_zero,
1180e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1181e956caf2SEdgar E. Iglesias 
1182e956caf2SEdgar E. Iglesias     tcg_temp_free_i64(tmp_btaken);
1183e956caf2SEdgar E. Iglesias     tcg_temp_free_i64(tmp_zero);
1184fcf5ef2aSThomas Huth }
1185fcf5ef2aSThomas Huth 
1186f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1187f91c60f0SEdgar E. Iglesias {
1188f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1189f91c60f0SEdgar E. Iglesias 
1190f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1191f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1192f91c60f0SEdgar E. Iglesias 
1193f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1194f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1195f91c60f0SEdgar E. Iglesias }
1196f91c60f0SEdgar E. Iglesias 
1197fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1198fcf5ef2aSThomas Huth {
1199fcf5ef2aSThomas Huth     unsigned int cc;
1200fcf5ef2aSThomas Huth     unsigned int dslot;
1201fcf5ef2aSThomas Huth 
1202fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1203fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1204fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1205fcf5ef2aSThomas Huth 
1206fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1207fcf5ef2aSThomas Huth     if (dslot) {
1208f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1209fcf5ef2aSThomas Huth     }
1210fcf5ef2aSThomas Huth 
1211fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1212fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1213fcf5ef2aSThomas Huth 
121443d318b2SEdgar E. Iglesias         tcg_gen_movi_i64(env_btarget, dc->pc + offset);
1215fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1216fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1217fcf5ef2aSThomas Huth     } else {
1218fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
121943d318b2SEdgar E. Iglesias         tcg_gen_extu_i32_i64(env_btarget, *(dec_alu_op_b(dc)));
122043d318b2SEdgar E. Iglesias         tcg_gen_addi_i64(env_btarget, env_btarget, dc->pc);
122143d318b2SEdgar E. Iglesias         tcg_gen_andi_i64(env_btarget, env_btarget, UINT32_MAX);
1222fcf5ef2aSThomas Huth     }
12239e6e1828SEdgar E. Iglesias     eval_cc(dc, cc, env_btaken, cpu_R[dc->ra]);
1224fcf5ef2aSThomas Huth }
1225fcf5ef2aSThomas Huth 
1226fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1227fcf5ef2aSThomas Huth {
1228fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1229fcf5ef2aSThomas Huth 
1230fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1231fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1232fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1233fcf5ef2aSThomas Huth 
1234fcf5ef2aSThomas Huth     /* Memory barrier.  */
1235fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1236fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1237badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1238badcbf9dSEdgar E. Iglesias 
12396f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12406f3c458bSEdgar E. Iglesias 
12413f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12423f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12433f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12443f172744SEdgar E. Iglesias         }
12453f172744SEdgar E. Iglesias 
1246fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1247badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
1248fcf5ef2aSThomas Huth             TCGv_i32 tmp_hlt = tcg_const_i32(EXCP_HLT);
1249fcf5ef2aSThomas Huth             TCGv_i32 tmp_1 = tcg_const_i32(1);
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1252fcf5ef2aSThomas Huth 
1253b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1254b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1255b4919e7dSEdgar E. Iglesias                 return;
1256b4919e7dSEdgar E. Iglesias             }
1257b4919e7dSEdgar E. Iglesias 
1258fcf5ef2aSThomas Huth             t_sync_flags(dc);
1259fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1260fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1261fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
12620a22f8cfSEdgar E. Iglesias             tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc + 4);
1263fcf5ef2aSThomas Huth             gen_helper_raise_exception(cpu_env, tmp_hlt);
1264fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_hlt);
1265fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
1266fcf5ef2aSThomas Huth             return;
1267fcf5ef2aSThomas Huth         }
1268fcf5ef2aSThomas Huth         /* Break the TB.  */
1269fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1270fcf5ef2aSThomas Huth         return;
1271fcf5ef2aSThomas Huth     }
1272fcf5ef2aSThomas Huth 
1273fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1274fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1275fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1276fcf5ef2aSThomas Huth              dc->imm);
1277fcf5ef2aSThomas Huth 
1278fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1279fcf5ef2aSThomas Huth     if (dslot) {
1280f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1281fcf5ef2aSThomas Huth     }
1282fcf5ef2aSThomas Huth     if (link && dc->rd)
1283cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1284fcf5ef2aSThomas Huth 
1285fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1286fcf5ef2aSThomas Huth     if (abs) {
1287cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(env_btaken, 1);
128843d318b2SEdgar E. Iglesias         tcg_gen_extu_i32_i64(env_btarget, *(dec_alu_op_b(dc)));
1289fcf5ef2aSThomas Huth         if (link && !dslot) {
1290fcf5ef2aSThomas Huth             if (!(dc->tb_flags & IMM_FLAG) && (dc->imm == 8 || dc->imm == 0x18))
1291fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_BREAK);
1292fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1293bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1294fcf5ef2aSThomas Huth                     return;
1295fcf5ef2aSThomas Huth                 }
1296fcf5ef2aSThomas Huth 
1297fcf5ef2aSThomas Huth                 t_gen_raise_exception(dc, EXCP_DEBUG);
1298fcf5ef2aSThomas Huth             }
1299fcf5ef2aSThomas Huth         }
1300fcf5ef2aSThomas Huth     } else {
1301fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1302fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1303fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1304fcf5ef2aSThomas Huth         } else {
1305cfeea807SEdgar E. Iglesias             tcg_gen_movi_i32(env_btaken, 1);
130643d318b2SEdgar E. Iglesias             tcg_gen_extu_i32_i64(env_btarget, *(dec_alu_op_b(dc)));
130743d318b2SEdgar E. Iglesias             tcg_gen_addi_i64(env_btarget, env_btarget, dc->pc);
130843d318b2SEdgar E. Iglesias             tcg_gen_andi_i64(env_btarget, env_btarget, UINT32_MAX);
1309fcf5ef2aSThomas Huth         }
1310fcf5ef2aSThomas Huth     }
1311fcf5ef2aSThomas Huth }
1312fcf5ef2aSThomas Huth 
1313fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1314fcf5ef2aSThomas Huth {
1315cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1316cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1317cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13180a22f8cfSEdgar E. Iglesias     tcg_gen_extrl_i64_i32(t1, cpu_SR[SR_MSR]);
13190a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13200a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1321cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1322fcf5ef2aSThomas Huth 
1323cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1324cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1325fcf5ef2aSThomas Huth     msr_write(dc, t1);
1326cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1327cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1328fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1329fcf5ef2aSThomas Huth }
1330fcf5ef2aSThomas Huth 
1331fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1332fcf5ef2aSThomas Huth {
1333cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1334cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1335cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13360a22f8cfSEdgar E. Iglesias     tcg_gen_extrl_i64_i32(t1, cpu_SR[SR_MSR]);
13370a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1338cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1339cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1340fcf5ef2aSThomas Huth 
1341cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1342cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1343fcf5ef2aSThomas Huth     msr_write(dc, t1);
1344cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1345cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1346fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1347fcf5ef2aSThomas Huth }
1348fcf5ef2aSThomas Huth 
1349fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1350fcf5ef2aSThomas Huth {
1351cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1352cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1353cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1354fcf5ef2aSThomas Huth 
13550a22f8cfSEdgar E. Iglesias     tcg_gen_extrl_i64_i32(t1, cpu_SR[SR_MSR]);
13560a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1357cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1358cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1359cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1360fcf5ef2aSThomas Huth 
1361cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1362cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1363fcf5ef2aSThomas Huth     msr_write(dc, t1);
1364cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1365cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1366fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1367fcf5ef2aSThomas Huth }
1368fcf5ef2aSThomas Huth 
1369fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1370fcf5ef2aSThomas Huth {
1371fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
137243d318b2SEdgar E. Iglesias     TCGv_i64 tmp64;
1373fcf5ef2aSThomas Huth 
1374fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1375fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1376fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1377fcf5ef2aSThomas Huth 
1378bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1379bdfc1e88SEdgar E. Iglesias         return;
1380bdfc1e88SEdgar E. Iglesias     }
1381bdfc1e88SEdgar E. Iglesias 
1382f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth     if (i_bit) {
1385fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1386fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1387fcf5ef2aSThomas Huth     } else if (b_bit) {
1388fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1389fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1390fcf5ef2aSThomas Huth     } else if (e_bit) {
1391fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1392fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1393fcf5ef2aSThomas Huth     } else
1394fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1395fcf5ef2aSThomas Huth 
1396fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1397cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(env_btaken, 1);
139843d318b2SEdgar E. Iglesias 
139943d318b2SEdgar E. Iglesias     tmp64 = tcg_temp_new_i64();
140043d318b2SEdgar E. Iglesias     tcg_gen_extu_i32_i64(env_btarget, *(dec_alu_op_b(dc)));
140143d318b2SEdgar E. Iglesias     tcg_gen_extu_i32_i64(tmp64, cpu_R[dc->ra]);
140243d318b2SEdgar E. Iglesias     tcg_gen_add_i64(env_btarget, env_btarget, tmp64);
140343d318b2SEdgar E. Iglesias     tcg_gen_andi_i64(env_btarget, env_btarget, UINT32_MAX);
140443d318b2SEdgar E. Iglesias     tcg_temp_free_i64(tmp64);
1405fcf5ef2aSThomas Huth }
1406fcf5ef2aSThomas Huth 
1407fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1408fcf5ef2aSThomas Huth {
1409fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
14100a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_ESR], ESR_EC_FPU);
1411fcf5ef2aSThomas Huth         t_gen_raise_exception(dc, EXCP_HW_EXCP);
1412fcf5ef2aSThomas Huth     }
14132016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1414fcf5ef2aSThomas Huth }
1415fcf5ef2aSThomas Huth 
1416fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1417fcf5ef2aSThomas Huth {
1418fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1419fcf5ef2aSThomas Huth 
14209ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1421fcf5ef2aSThomas Huth         return;
1422fcf5ef2aSThomas Huth     }
1423fcf5ef2aSThomas Huth 
1424fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1425fcf5ef2aSThomas Huth 
1426fcf5ef2aSThomas Huth     switch (fpu_insn) {
1427fcf5ef2aSThomas Huth         case 0:
1428fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1429fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1430fcf5ef2aSThomas Huth             break;
1431fcf5ef2aSThomas Huth 
1432fcf5ef2aSThomas Huth         case 1:
1433fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1434fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1435fcf5ef2aSThomas Huth             break;
1436fcf5ef2aSThomas Huth 
1437fcf5ef2aSThomas Huth         case 2:
1438fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1439fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1440fcf5ef2aSThomas Huth             break;
1441fcf5ef2aSThomas Huth 
1442fcf5ef2aSThomas Huth         case 3:
1443fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1444fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1445fcf5ef2aSThomas Huth             break;
1446fcf5ef2aSThomas Huth 
1447fcf5ef2aSThomas Huth         case 4:
1448fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1449fcf5ef2aSThomas Huth                 case 0:
1450fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1451fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1452fcf5ef2aSThomas Huth                     break;
1453fcf5ef2aSThomas Huth                 case 1:
1454fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1455fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1456fcf5ef2aSThomas Huth                     break;
1457fcf5ef2aSThomas Huth                 case 2:
1458fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1459fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1460fcf5ef2aSThomas Huth                     break;
1461fcf5ef2aSThomas Huth                 case 3:
1462fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1463fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1464fcf5ef2aSThomas Huth                     break;
1465fcf5ef2aSThomas Huth                 case 4:
1466fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1467fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1468fcf5ef2aSThomas Huth                     break;
1469fcf5ef2aSThomas Huth                 case 5:
1470fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1471fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1472fcf5ef2aSThomas Huth                     break;
1473fcf5ef2aSThomas Huth                 case 6:
1474fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1475fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1476fcf5ef2aSThomas Huth                     break;
1477fcf5ef2aSThomas Huth                 default:
1478fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1479fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1480fcf5ef2aSThomas Huth                                   " opc=%x\n",
1481fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1482fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1483fcf5ef2aSThomas Huth                     break;
1484fcf5ef2aSThomas Huth             }
1485fcf5ef2aSThomas Huth             break;
1486fcf5ef2aSThomas Huth 
1487fcf5ef2aSThomas Huth         case 5:
1488fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1489fcf5ef2aSThomas Huth                 return;
1490fcf5ef2aSThomas Huth             }
1491fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1492fcf5ef2aSThomas Huth             break;
1493fcf5ef2aSThomas Huth 
1494fcf5ef2aSThomas Huth         case 6:
1495fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1496fcf5ef2aSThomas Huth                 return;
1497fcf5ef2aSThomas Huth             }
1498fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1499fcf5ef2aSThomas Huth             break;
1500fcf5ef2aSThomas Huth 
1501fcf5ef2aSThomas Huth         case 7:
1502fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1503fcf5ef2aSThomas Huth                 return;
1504fcf5ef2aSThomas Huth             }
1505fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1506fcf5ef2aSThomas Huth             break;
1507fcf5ef2aSThomas Huth 
1508fcf5ef2aSThomas Huth         default:
1509fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1510fcf5ef2aSThomas Huth                           " opc=%x\n",
1511fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1512fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1513fcf5ef2aSThomas Huth             break;
1514fcf5ef2aSThomas Huth     }
1515fcf5ef2aSThomas Huth }
1516fcf5ef2aSThomas Huth 
1517fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1518fcf5ef2aSThomas Huth {
15199ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1520fcf5ef2aSThomas Huth         return;
1521fcf5ef2aSThomas Huth     }
1522fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1523fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1524fcf5ef2aSThomas Huth }
1525fcf5ef2aSThomas Huth 
1526fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1527fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1528fcf5ef2aSThomas Huth {
1529fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1530fcf5ef2aSThomas Huth     int ctrl;
1531fcf5ef2aSThomas Huth 
1532fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1533fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1534fcf5ef2aSThomas Huth 
1535bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1536fcf5ef2aSThomas Huth         return;
1537fcf5ef2aSThomas Huth     }
1538fcf5ef2aSThomas Huth 
1539cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1540fcf5ef2aSThomas Huth     if (dc->type_b) {
1541cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1542fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1543fcf5ef2aSThomas Huth     } else {
1544cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1545fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1546fcf5ef2aSThomas Huth     }
1547fcf5ef2aSThomas Huth 
1548cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1549fcf5ef2aSThomas Huth 
1550fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1551fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1552fcf5ef2aSThomas Huth     } else {
1553fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1554fcf5ef2aSThomas Huth     }
1555cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1556cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1557fcf5ef2aSThomas Huth }
1558fcf5ef2aSThomas Huth 
1559fcf5ef2aSThomas Huth static struct decoder_info {
1560fcf5ef2aSThomas Huth     struct {
1561fcf5ef2aSThomas Huth         uint32_t bits;
1562fcf5ef2aSThomas Huth         uint32_t mask;
1563fcf5ef2aSThomas Huth     };
1564fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1565fcf5ef2aSThomas Huth } decinfo[] = {
1566fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1567fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1568fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1569fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1570fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1571fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1572fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1573fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1574fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1575fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1576fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1577fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1578fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1579fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1580fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1581fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1582fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1583fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1584fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1585fcf5ef2aSThomas Huth };
1586fcf5ef2aSThomas Huth 
1587fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1588fcf5ef2aSThomas Huth {
1589fcf5ef2aSThomas Huth     int i;
1590fcf5ef2aSThomas Huth 
1591fcf5ef2aSThomas Huth     dc->ir = ir;
1592fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1593fcf5ef2aSThomas Huth 
1594462c2544SEdgar E. Iglesias     if (ir == 0) {
15951ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1596462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1597462c2544SEdgar E. Iglesias         return;
1598462c2544SEdgar E. Iglesias     }
1599fcf5ef2aSThomas Huth 
1600fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1601fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1602fcf5ef2aSThomas Huth 
1603fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1604fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1605fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1606fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1607fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1608fcf5ef2aSThomas Huth 
1609fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1610fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1611fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1612fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1613fcf5ef2aSThomas Huth             break;
1614fcf5ef2aSThomas Huth         }
1615fcf5ef2aSThomas Huth     }
1616fcf5ef2aSThomas Huth }
1617fcf5ef2aSThomas Huth 
1618fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
16198b86d6d2SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1620fcf5ef2aSThomas Huth {
16219c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1622f5c7e93aSRichard Henderson     MicroBlazeCPU *cpu = env_archcpu(env);
1623fcf5ef2aSThomas Huth     uint32_t pc_start;
1624fcf5ef2aSThomas Huth     struct DisasContext ctx;
1625fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
162656371527SEmilio G. Cota     uint32_t page_start, org_flags;
1627cfeea807SEdgar E. Iglesias     uint32_t npc;
1628fcf5ef2aSThomas Huth     int num_insns;
1629fcf5ef2aSThomas Huth 
1630fcf5ef2aSThomas Huth     pc_start = tb->pc;
1631fcf5ef2aSThomas Huth     dc->cpu = cpu;
1632fcf5ef2aSThomas Huth     dc->tb = tb;
1633fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1634fcf5ef2aSThomas Huth 
1635fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1636fcf5ef2aSThomas Huth     dc->jmp = 0;
1637fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1638fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1639fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1640fcf5ef2aSThomas Huth     }
1641fcf5ef2aSThomas Huth     dc->pc = pc_start;
1642fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1643fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1644fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1645fcf5ef2aSThomas Huth 
1646fcf5ef2aSThomas Huth     if (pc_start & 3) {
1647fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1648fcf5ef2aSThomas Huth     }
1649fcf5ef2aSThomas Huth 
165056371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1651fcf5ef2aSThomas Huth     num_insns = 0;
1652fcf5ef2aSThomas Huth 
1653fcf5ef2aSThomas Huth     gen_tb_start(tb);
1654fcf5ef2aSThomas Huth     do
1655fcf5ef2aSThomas Huth     {
1656fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1657fcf5ef2aSThomas Huth         num_insns++;
1658fcf5ef2aSThomas Huth 
1659fcf5ef2aSThomas Huth #if SIM_COMPAT
1660fcf5ef2aSThomas Huth         if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
16610a22f8cfSEdgar E. Iglesias             tcg_gen_movi_i64(cpu_SR[SR_PC], dc->pc);
1662fcf5ef2aSThomas Huth             gen_helper_debug();
1663fcf5ef2aSThomas Huth         }
1664fcf5ef2aSThomas Huth #endif
1665fcf5ef2aSThomas Huth 
1666fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
1667fcf5ef2aSThomas Huth             t_gen_raise_exception(dc, EXCP_DEBUG);
1668fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
1669fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1670fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1671fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1672fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1673fcf5ef2aSThomas Huth             dc->pc += 4;
1674fcf5ef2aSThomas Huth             break;
1675fcf5ef2aSThomas Huth         }
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth         /* Pretty disas.  */
1678fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1679fcf5ef2aSThomas Huth 
1680c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1681fcf5ef2aSThomas Huth             gen_io_start();
1682fcf5ef2aSThomas Huth         }
1683fcf5ef2aSThomas Huth 
1684fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1685fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1686fcf5ef2aSThomas Huth         if (dc->clear_imm)
1687fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1688fcf5ef2aSThomas Huth         dc->pc += 4;
1689fcf5ef2aSThomas Huth 
1690fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1691fcf5ef2aSThomas Huth             dc->delayed_branch--;
1692fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1693fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1694fcf5ef2aSThomas Huth                     do_rti(dc);
1695fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1696fcf5ef2aSThomas Huth                     do_rtb(dc);
1697fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1698fcf5ef2aSThomas Huth                     do_rte(dc);
1699fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1700fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1701fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1702fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
1703c49a41b0SEdgar E. Iglesias                     TCGv_i64 tmp_pc = tcg_const_i64(dc->pc);
1704c49a41b0SEdgar E. Iglesias                     eval_cond_jmp(dc, env_btarget, tmp_pc);
1705c49a41b0SEdgar E. Iglesias                     tcg_temp_free_i64(tmp_pc);
1706c49a41b0SEdgar E. Iglesias 
1707fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1708fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1709fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1710fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1711fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1712fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1713fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1714fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1715fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
1716cfeea807SEdgar E. Iglesias                     tcg_gen_brcondi_i32(TCG_COND_NE, env_btaken, 0, l1);
1717fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1718fcf5ef2aSThomas Huth                     gen_set_label(l1);
1719fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1720fcf5ef2aSThomas Huth 
1721fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1722fcf5ef2aSThomas Huth                 }
1723fcf5ef2aSThomas Huth                 break;
1724fcf5ef2aSThomas Huth             }
1725fcf5ef2aSThomas Huth         }
1726fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1727fcf5ef2aSThomas Huth             break;
1728fcf5ef2aSThomas Huth         }
1729fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1730fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1731fcf5ef2aSThomas Huth              && !singlestep
173256371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1733fcf5ef2aSThomas Huth              && num_insns < max_insns);
1734fcf5ef2aSThomas Huth 
1735fcf5ef2aSThomas Huth     npc = dc->pc;
1736fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1737fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1738fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
17390a22f8cfSEdgar E. Iglesias             tcg_gen_movi_i64(cpu_SR[SR_PC], npc);
1740fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1741fcf5ef2aSThomas Huth         } else
1742fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1743fcf5ef2aSThomas Huth     }
1744fcf5ef2aSThomas Huth 
1745fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1746fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1747fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1748fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
17490a22f8cfSEdgar E. Iglesias         tcg_gen_movi_i64(cpu_SR[SR_PC], npc);
1750fcf5ef2aSThomas Huth     }
1751fcf5ef2aSThomas Huth     t_sync_flags(dc);
1752fcf5ef2aSThomas Huth 
1753fcf5ef2aSThomas Huth     if (unlikely(cs->singlestep_enabled)) {
1754fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1755fcf5ef2aSThomas Huth 
1756fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
17570a22f8cfSEdgar E. Iglesias             tcg_gen_movi_i64(cpu_SR[SR_PC], npc);
1758fcf5ef2aSThomas Huth         }
1759fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1760fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1761fcf5ef2aSThomas Huth     } else {
1762fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1763fcf5ef2aSThomas Huth             case DISAS_NEXT:
1764fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1765fcf5ef2aSThomas Huth                 break;
1766fcf5ef2aSThomas Huth             default:
1767fcf5ef2aSThomas Huth             case DISAS_JUMP:
1768fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1769fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1770fcf5ef2aSThomas Huth                    to find the next TB */
177107ea28b4SRichard Henderson                 tcg_gen_exit_tb(NULL, 0);
1772fcf5ef2aSThomas Huth                 break;
1773fcf5ef2aSThomas Huth             case DISAS_TB_JUMP:
1774fcf5ef2aSThomas Huth                 /* nothing more to generate */
1775fcf5ef2aSThomas Huth                 break;
1776fcf5ef2aSThomas Huth         }
1777fcf5ef2aSThomas Huth     }
1778fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1779fcf5ef2aSThomas Huth 
1780fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1781fcf5ef2aSThomas Huth     tb->icount = num_insns;
1782fcf5ef2aSThomas Huth 
1783fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1784fcf5ef2aSThomas Huth #if !SIM_COMPAT
1785fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1786fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1787fc59d2d8SRobert Foley         FILE *logfile = qemu_log_lock();
1788fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17891d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1790fc59d2d8SRobert Foley         qemu_log_unlock(logfile);
1791fcf5ef2aSThomas Huth     }
1792fcf5ef2aSThomas Huth #endif
1793fcf5ef2aSThomas Huth #endif
1794fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1795fcf5ef2aSThomas Huth }
1796fcf5ef2aSThomas Huth 
179790c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1798fcf5ef2aSThomas Huth {
1799fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1800fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1801fcf5ef2aSThomas Huth     int i;
1802fcf5ef2aSThomas Huth 
180390c84c56SMarkus Armbruster     if (!env) {
1804fcf5ef2aSThomas Huth         return;
180590c84c56SMarkus Armbruster     }
1806fcf5ef2aSThomas Huth 
180790c84c56SMarkus Armbruster     qemu_fprintf(f, "IN: PC=%" PRIx64 " %s\n",
180876e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
180990c84c56SMarkus Armbruster     qemu_fprintf(f, "rmsr=%" PRIx64 " resr=%" PRIx64 " rear=%" PRIx64 " "
18102ead1b18SJoe Komlodi                  "debug=%x imm=%x iflags=%x fsr=%" PRIx64 " "
18112ead1b18SJoe Komlodi                  "rbtr=%" PRIx64 "\n",
181278e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
18135a8e0136SRichard Henderson                  env->debug, env->imm, env->iflags, env->fsr,
1814*6fbf78f2SRichard Henderson                  env->btr);
181590c84c56SMarkus Armbruster     qemu_fprintf(f, "btaken=%d btarget=%" PRIx64 " mode=%s(saved=%s) "
181643d318b2SEdgar E. Iglesias                  "eip=%d ie=%d\n",
1817fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
18182e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18192e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18202e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18212e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18222ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
18232ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
18242ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18252ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18262ead1b18SJoe Komlodi         }
18272ead1b18SJoe Komlodi     }
1828fcf5ef2aSThomas Huth 
18292ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
18302ead1b18SJoe Komlodi     qemu_fprintf(f, "redr=%" PRIx64 " rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
18312ead1b18SJoe Komlodi                     "rtlblo=0 rtlbhi=0\n", env->sregs[SR_EDR]);
18322ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1833fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
183490c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1835fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
183690c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1837fcf5ef2aSThomas Huth         }
183890c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1839fcf5ef2aSThomas Huth }
1840fcf5ef2aSThomas Huth 
1841fcf5ef2aSThomas Huth void mb_tcg_init(void)
1842fcf5ef2aSThomas Huth {
1843fcf5ef2aSThomas Huth     int i;
1844fcf5ef2aSThomas Huth 
1845cfeea807SEdgar E. Iglesias     env_debug = tcg_global_mem_new_i32(cpu_env,
1846fcf5ef2aSThomas Huth                     offsetof(CPUMBState, debug),
1847fcf5ef2aSThomas Huth                     "debug0");
1848cfeea807SEdgar E. Iglesias     env_iflags = tcg_global_mem_new_i32(cpu_env,
1849fcf5ef2aSThomas Huth                     offsetof(CPUMBState, iflags),
1850fcf5ef2aSThomas Huth                     "iflags");
1851cfeea807SEdgar E. Iglesias     env_imm = tcg_global_mem_new_i32(cpu_env,
1852fcf5ef2aSThomas Huth                     offsetof(CPUMBState, imm),
1853fcf5ef2aSThomas Huth                     "imm");
185443d318b2SEdgar E. Iglesias     env_btarget = tcg_global_mem_new_i64(cpu_env,
1855fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btarget),
1856fcf5ef2aSThomas Huth                      "btarget");
1857cfeea807SEdgar E. Iglesias     env_btaken = tcg_global_mem_new_i32(cpu_env,
1858fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btaken),
1859fcf5ef2aSThomas Huth                      "btaken");
1860403322eaSEdgar E. Iglesias     env_res_addr = tcg_global_mem_new(cpu_env,
1861fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_addr),
1862fcf5ef2aSThomas Huth                      "res_addr");
1863cfeea807SEdgar E. Iglesias     env_res_val = tcg_global_mem_new_i32(cpu_env,
1864fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_val),
1865fcf5ef2aSThomas Huth                      "res_val");
1866fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1867cfeea807SEdgar E. Iglesias         cpu_R[i] = tcg_global_mem_new_i32(cpu_env,
1868fcf5ef2aSThomas Huth                           offsetof(CPUMBState, regs[i]),
1869fcf5ef2aSThomas Huth                           regnames[i]);
1870fcf5ef2aSThomas Huth     }
187176e8187dSRichard Henderson 
187276e8187dSRichard Henderson     cpu_SR[SR_PC] =
187376e8187dSRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, pc), "rpc");
18742e5282caSRichard Henderson     cpu_SR[SR_MSR] =
18752e5282caSRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, msr), "rmsr");
1876b2e80a3cSRichard Henderson     cpu_SR[SR_EAR] =
1877b2e80a3cSRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, ear), "rear");
187878e9caf2SRichard Henderson     cpu_SR[SR_ESR] =
187978e9caf2SRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, esr), "resr");
18805a8e0136SRichard Henderson     cpu_SR[SR_FSR] =
18815a8e0136SRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, fsr), "rfsr");
1882*6fbf78f2SRichard Henderson     cpu_SR[SR_BTR] =
1883*6fbf78f2SRichard Henderson         tcg_global_mem_new_i64(cpu_env, offsetof(CPUMBState, btr), "rbtr");
188476e8187dSRichard Henderson 
1885*6fbf78f2SRichard Henderson     for (i = SR_BTR + 1; i < ARRAY_SIZE(cpu_SR); i++) {
18860a22f8cfSEdgar E. Iglesias         cpu_SR[i] = tcg_global_mem_new_i64(cpu_env,
1887fcf5ef2aSThomas Huth                           offsetof(CPUMBState, sregs[i]),
1888fcf5ef2aSThomas Huth                           special_regnames[i]);
1889fcf5ef2aSThomas Huth     }
1890fcf5ef2aSThomas Huth }
1891fcf5ef2aSThomas Huth 
1892fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1893fcf5ef2aSThomas Huth                           target_ulong *data)
1894fcf5ef2aSThomas Huth {
189576e8187dSRichard Henderson     env->pc = data[0];
1896fcf5ef2aSThomas Huth }
1897