eepro100: address pci todo's, use pci_set_xx
[qemu.git] / target-microblaze / translate.c
blobca54e2c30e8075c274a4885ab44fff38c8e874ea
1 /*
2 * Xilinx MicroBlaze emulation for qemu: main translation routines.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
27 #include "cpu.h"
28 #include "exec-all.h"
29 #include "disas.h"
30 #include "tcg-op.h"
31 #include "helper.h"
32 #include "microblaze-decode.h"
33 #include "qemu-common.h"
35 #define GEN_HELPER 1
36 #include "helper.h"
38 #define SIM_COMPAT 0
39 #define DISAS_GNU 1
40 #define DISAS_MB 1
41 #if DISAS_MB && !SIM_COMPAT
42 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
43 #else
44 # define LOG_DIS(...) do { } while (0)
45 #endif
47 #define D(x)
49 #define EXTRACT_FIELD(src, start, end) \
50 (((src) >> start) & ((1 << (end - start + 1)) - 1))
52 static TCGv env_debug;
53 static TCGv_ptr cpu_env;
54 static TCGv cpu_R[32];
55 static TCGv cpu_SR[18];
56 static TCGv env_imm;
57 static TCGv env_btaken;
58 static TCGv env_btarget;
59 static TCGv env_iflags;
61 #include "gen-icount.h"
63 /* This is the state at translation time. */
64 typedef struct DisasContext {
65 CPUState *env;
66 target_ulong pc, ppc;
67 target_ulong cache_pc;
69 /* Decoder. */
70 int type_b;
71 uint32_t ir;
72 uint8_t opcode;
73 uint8_t rd, ra, rb;
74 uint16_t imm;
76 unsigned int cpustate_changed;
77 unsigned int delayed_branch;
78 unsigned int tb_flags, synced_flags; /* tb dependent flags. */
79 unsigned int clear_imm;
80 int is_jmp;
82 #define JMP_NOJMP 0
83 #define JMP_DIRECT 1
84 #define JMP_INDIRECT 2
85 unsigned int jmp;
86 uint32_t jmp_pc;
88 int abort_at_next_insn;
89 int nr_nops;
90 struct TranslationBlock *tb;
91 int singlestep_enabled;
92 } DisasContext;
94 static const char *regnames[] =
96 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
97 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
98 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
99 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
102 static const char *special_regnames[] =
104 "rpc", "rmsr", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
105 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
106 "sr16", "sr17", "sr18"
109 /* Sign extend at translation time. */
110 static inline int sign_extend(unsigned int val, unsigned int width)
112 int sval;
114 /* LSL. */
115 val <<= 31 - width;
116 sval = val;
117 /* ASR. */
118 sval >>= 31 - width;
119 return sval;
122 static inline void t_sync_flags(DisasContext *dc)
124 /* Synch the tb dependant flags between translator and runtime. */
125 if (dc->tb_flags != dc->synced_flags) {
126 tcg_gen_movi_tl(env_iflags, dc->tb_flags);
127 dc->synced_flags = dc->tb_flags;
131 static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
133 TCGv_i32 tmp = tcg_const_i32(index);
135 t_sync_flags(dc);
136 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
137 gen_helper_raise_exception(tmp);
138 tcg_temp_free_i32(tmp);
139 dc->is_jmp = DISAS_UPDATE;
142 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
144 TranslationBlock *tb;
145 tb = dc->tb;
146 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
147 tcg_gen_goto_tb(n);
148 tcg_gen_movi_tl(cpu_SR[SR_PC], dest);
149 tcg_gen_exit_tb((long)tb + n);
150 } else {
151 tcg_gen_movi_tl(cpu_SR[SR_PC], dest);
152 tcg_gen_exit_tb(0);
156 static inline TCGv *dec_alu_op_b(DisasContext *dc)
158 if (dc->type_b) {
159 if (dc->tb_flags & IMM_FLAG)
160 tcg_gen_ori_tl(env_imm, env_imm, dc->imm);
161 else
162 tcg_gen_movi_tl(env_imm, (int32_t)((int16_t)dc->imm));
163 return &env_imm;
164 } else
165 return &cpu_R[dc->rb];
168 static void dec_add(DisasContext *dc)
170 unsigned int k, c;
172 k = dc->opcode & 4;
173 c = dc->opcode & 2;
175 LOG_DIS("add%s%s%s r%d r%d r%d\n",
176 dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
177 dc->rd, dc->ra, dc->rb);
179 if (k && !c && dc->rd)
180 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
181 else if (dc->rd)
182 gen_helper_addkc(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)),
183 tcg_const_tl(k), tcg_const_tl(c));
184 else {
185 TCGv d = tcg_temp_new();
186 gen_helper_addkc(d, cpu_R[dc->ra], *(dec_alu_op_b(dc)),
187 tcg_const_tl(k), tcg_const_tl(c));
188 tcg_temp_free(d);
192 static void dec_sub(DisasContext *dc)
194 unsigned int u, cmp, k, c;
196 u = dc->imm & 2;
197 k = dc->opcode & 4;
198 c = dc->opcode & 2;
199 cmp = (dc->imm & 1) && (!dc->type_b) && k;
201 if (cmp) {
202 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
203 if (dc->rd) {
204 if (u)
205 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
206 else
207 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
209 } else {
210 LOG_DIS("sub%s%s r%d, r%d r%d\n",
211 k ? "k" : "", c ? "c" : "", dc->rd, dc->ra, dc->rb);
213 if (!k || c) {
214 TCGv t;
215 t = tcg_temp_new();
216 if (dc->rd)
217 gen_helper_subkc(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)),
218 tcg_const_tl(k), tcg_const_tl(c));
219 else
220 gen_helper_subkc(t, cpu_R[dc->ra], *(dec_alu_op_b(dc)),
221 tcg_const_tl(k), tcg_const_tl(c));
222 tcg_temp_free(t);
224 else if (dc->rd)
225 tcg_gen_sub_tl(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
229 static void dec_pattern(DisasContext *dc)
231 unsigned int mode;
232 int l1;
234 if ((dc->tb_flags & MSR_EE_FLAG)
235 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
236 && !((dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR))) {
237 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
238 t_gen_raise_exception(dc, EXCP_HW_EXCP);
241 mode = dc->opcode & 3;
242 switch (mode) {
243 case 0:
244 /* pcmpbf. */
245 LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
246 if (dc->rd)
247 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
248 break;
249 case 2:
250 LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
251 if (dc->rd) {
252 TCGv t0 = tcg_temp_local_new();
253 l1 = gen_new_label();
254 tcg_gen_movi_tl(t0, 1);
255 tcg_gen_brcond_tl(TCG_COND_EQ,
256 cpu_R[dc->ra], cpu_R[dc->rb], l1);
257 tcg_gen_movi_tl(t0, 0);
258 gen_set_label(l1);
259 tcg_gen_mov_tl(cpu_R[dc->rd], t0);
260 tcg_temp_free(t0);
262 break;
263 case 3:
264 LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
265 l1 = gen_new_label();
266 if (dc->rd) {
267 TCGv t0 = tcg_temp_local_new();
268 tcg_gen_movi_tl(t0, 1);
269 tcg_gen_brcond_tl(TCG_COND_NE,
270 cpu_R[dc->ra], cpu_R[dc->rb], l1);
271 tcg_gen_movi_tl(t0, 0);
272 gen_set_label(l1);
273 tcg_gen_mov_tl(cpu_R[dc->rd], t0);
274 tcg_temp_free(t0);
276 break;
277 default:
278 cpu_abort(dc->env,
279 "unsupported pattern insn opcode=%x\n", dc->opcode);
280 break;
284 static void dec_and(DisasContext *dc)
286 unsigned int not;
288 if (!dc->type_b && (dc->imm & (1 << 10))) {
289 dec_pattern(dc);
290 return;
293 not = dc->opcode & (1 << 1);
294 LOG_DIS("and%s\n", not ? "n" : "");
296 if (!dc->rd)
297 return;
299 if (not) {
300 TCGv t = tcg_temp_new();
301 tcg_gen_not_tl(t, *(dec_alu_op_b(dc)));
302 tcg_gen_and_tl(cpu_R[dc->rd], cpu_R[dc->ra], t);
303 tcg_temp_free(t);
304 } else
305 tcg_gen_and_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
308 static void dec_or(DisasContext *dc)
310 if (!dc->type_b && (dc->imm & (1 << 10))) {
311 dec_pattern(dc);
312 return;
315 LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
316 if (dc->rd)
317 tcg_gen_or_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
320 static void dec_xor(DisasContext *dc)
322 if (!dc->type_b && (dc->imm & (1 << 10))) {
323 dec_pattern(dc);
324 return;
327 LOG_DIS("xor r%d\n", dc->rd);
328 if (dc->rd)
329 tcg_gen_xor_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
332 static void read_carry(DisasContext *dc, TCGv d)
334 tcg_gen_shri_tl(d, cpu_SR[SR_MSR], 31);
337 static void write_carry(DisasContext *dc, TCGv v)
339 TCGv t0 = tcg_temp_new();
340 tcg_gen_shli_tl(t0, v, 31);
341 tcg_gen_sari_tl(t0, t0, 31);
342 tcg_gen_mov_tl(env_debug, t0);
343 tcg_gen_andi_tl(t0, t0, (MSR_C | MSR_CC));
344 tcg_gen_andi_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR],
345 ~(MSR_C | MSR_CC));
346 tcg_gen_or_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t0);
347 tcg_temp_free(t0);
351 static inline void msr_read(DisasContext *dc, TCGv d)
353 tcg_gen_mov_tl(d, cpu_SR[SR_MSR]);
356 static inline void msr_write(DisasContext *dc, TCGv v)
358 dc->cpustate_changed = 1;
359 tcg_gen_mov_tl(cpu_SR[SR_MSR], v);
360 /* PVR, we have a processor version register. */
361 tcg_gen_ori_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR], (1 << 10));
364 static void dec_msr(DisasContext *dc)
366 TCGv t0, t1;
367 unsigned int sr, to, rn;
368 int mem_index = cpu_mmu_index(dc->env);
370 sr = dc->imm & ((1 << 14) - 1);
371 to = dc->imm & (1 << 14);
372 dc->type_b = 1;
373 if (to)
374 dc->cpustate_changed = 1;
376 /* msrclr and msrset. */
377 if (!(dc->imm & (1 << 15))) {
378 unsigned int clr = dc->ir & (1 << 16);
380 LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
381 dc->rd, dc->imm);
383 if (!(dc->env->pvr.regs[2] & PVR2_USE_MSR_INSTR)) {
384 /* nop??? */
385 return;
388 if ((dc->tb_flags & MSR_EE_FLAG)
389 && mem_index == MMU_USER_IDX && (dc->imm != 4 && dc->imm != 0)) {
390 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
391 t_gen_raise_exception(dc, EXCP_HW_EXCP);
392 return;
395 if (dc->rd)
396 msr_read(dc, cpu_R[dc->rd]);
398 t0 = tcg_temp_new();
399 t1 = tcg_temp_new();
400 msr_read(dc, t0);
401 tcg_gen_mov_tl(t1, *(dec_alu_op_b(dc)));
403 if (clr) {
404 tcg_gen_not_tl(t1, t1);
405 tcg_gen_and_tl(t0, t0, t1);
406 } else
407 tcg_gen_or_tl(t0, t0, t1);
408 msr_write(dc, t0);
409 tcg_temp_free(t0);
410 tcg_temp_free(t1);
411 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc + 4);
412 dc->is_jmp = DISAS_UPDATE;
413 return;
416 if (to) {
417 if ((dc->tb_flags & MSR_EE_FLAG)
418 && mem_index == MMU_USER_IDX) {
419 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
420 t_gen_raise_exception(dc, EXCP_HW_EXCP);
421 return;
425 #if !defined(CONFIG_USER_ONLY)
426 /* Catch read/writes to the mmu block. */
427 if ((sr & ~0xff) == 0x1000) {
428 sr &= 7;
429 LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
430 if (to)
431 gen_helper_mmu_write(tcg_const_tl(sr), cpu_R[dc->ra]);
432 else
433 gen_helper_mmu_read(cpu_R[dc->rd], tcg_const_tl(sr));
434 return;
436 #endif
438 if (to) {
439 LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
440 switch (sr) {
441 case 0:
442 break;
443 case 1:
444 msr_write(dc, cpu_R[dc->ra]);
445 break;
446 case 0x3:
447 tcg_gen_mov_tl(cpu_SR[SR_EAR], cpu_R[dc->ra]);
448 break;
449 case 0x5:
450 tcg_gen_mov_tl(cpu_SR[SR_ESR], cpu_R[dc->ra]);
451 break;
452 case 0x7:
453 /* Ignored at the moment. */
454 break;
455 default:
456 cpu_abort(dc->env, "unknown mts reg %x\n", sr);
457 break;
459 } else {
460 LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
462 switch (sr) {
463 case 0:
464 tcg_gen_movi_tl(cpu_R[dc->rd], dc->pc);
465 break;
466 case 1:
467 msr_read(dc, cpu_R[dc->rd]);
468 break;
469 case 0x3:
470 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_EAR]);
471 break;
472 case 0x5:
473 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_ESR]);
474 break;
475 case 0x7:
476 tcg_gen_movi_tl(cpu_R[dc->rd], 0);
477 break;
478 case 0xb:
479 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_BTR]);
480 break;
481 case 0x2000:
482 case 0x2001:
483 case 0x2002:
484 case 0x2003:
485 case 0x2004:
486 case 0x2005:
487 case 0x2006:
488 case 0x2007:
489 case 0x2008:
490 case 0x2009:
491 case 0x200a:
492 case 0x200b:
493 case 0x200c:
494 rn = sr & 0xf;
495 tcg_gen_ld_tl(cpu_R[dc->rd],
496 cpu_env, offsetof(CPUState, pvr.regs[rn]));
497 break;
498 default:
499 cpu_abort(dc->env, "unknown mfs reg %x\n", sr);
500 break;
504 if (dc->rd == 0) {
505 tcg_gen_movi_tl(cpu_R[0], 0);
509 /* 64-bit signed mul, lower result in d and upper in d2. */
510 static void t_gen_muls(TCGv d, TCGv d2, TCGv a, TCGv b)
512 TCGv_i64 t0, t1;
514 t0 = tcg_temp_new_i64();
515 t1 = tcg_temp_new_i64();
517 tcg_gen_ext_i32_i64(t0, a);
518 tcg_gen_ext_i32_i64(t1, b);
519 tcg_gen_mul_i64(t0, t0, t1);
521 tcg_gen_trunc_i64_i32(d, t0);
522 tcg_gen_shri_i64(t0, t0, 32);
523 tcg_gen_trunc_i64_i32(d2, t0);
525 tcg_temp_free_i64(t0);
526 tcg_temp_free_i64(t1);
529 /* 64-bit unsigned muls, lower result in d and upper in d2. */
530 static void t_gen_mulu(TCGv d, TCGv d2, TCGv a, TCGv b)
532 TCGv_i64 t0, t1;
534 t0 = tcg_temp_new_i64();
535 t1 = tcg_temp_new_i64();
537 tcg_gen_extu_i32_i64(t0, a);
538 tcg_gen_extu_i32_i64(t1, b);
539 tcg_gen_mul_i64(t0, t0, t1);
541 tcg_gen_trunc_i64_i32(d, t0);
542 tcg_gen_shri_i64(t0, t0, 32);
543 tcg_gen_trunc_i64_i32(d2, t0);
545 tcg_temp_free_i64(t0);
546 tcg_temp_free_i64(t1);
549 /* Multiplier unit. */
550 static void dec_mul(DisasContext *dc)
552 TCGv d[2];
553 unsigned int subcode;
555 if ((dc->tb_flags & MSR_EE_FLAG)
556 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
557 && !(dc->env->pvr.regs[0] & PVR0_USE_HW_MUL_MASK)) {
558 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
559 t_gen_raise_exception(dc, EXCP_HW_EXCP);
560 return;
563 subcode = dc->imm & 3;
564 d[0] = tcg_temp_new();
565 d[1] = tcg_temp_new();
567 if (dc->type_b) {
568 LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
569 t_gen_mulu(cpu_R[dc->rd], d[1], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
570 goto done;
573 /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2. */
574 if (subcode >= 1 && subcode <= 3
575 && !((dc->env->pvr.regs[2] & PVR2_USE_MUL64_MASK))) {
576 /* nop??? */
579 switch (subcode) {
580 case 0:
581 LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
582 t_gen_mulu(cpu_R[dc->rd], d[1], cpu_R[dc->ra], cpu_R[dc->rb]);
583 break;
584 case 1:
585 LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
586 t_gen_muls(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
587 break;
588 case 2:
589 LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
590 t_gen_muls(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
591 break;
592 case 3:
593 LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
594 t_gen_mulu(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
595 break;
596 default:
597 cpu_abort(dc->env, "unknown MUL insn %x\n", subcode);
598 break;
600 done:
601 tcg_temp_free(d[0]);
602 tcg_temp_free(d[1]);
605 /* Div unit. */
606 static void dec_div(DisasContext *dc)
608 unsigned int u;
610 u = dc->imm & 2;
611 LOG_DIS("div\n");
613 if ((dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
614 && !((dc->env->pvr.regs[0] & PVR0_USE_DIV_MASK))) {
615 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
616 t_gen_raise_exception(dc, EXCP_HW_EXCP);
619 if (u)
620 gen_helper_divu(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
621 else
622 gen_helper_divs(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
623 if (!dc->rd)
624 tcg_gen_movi_tl(cpu_R[dc->rd], 0);
627 static void dec_barrel(DisasContext *dc)
629 TCGv t0;
630 unsigned int s, t;
632 if ((dc->tb_flags & MSR_EE_FLAG)
633 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
634 && !(dc->env->pvr.regs[0] & PVR0_USE_BARREL_MASK)) {
635 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
636 t_gen_raise_exception(dc, EXCP_HW_EXCP);
637 return;
640 s = dc->imm & (1 << 10);
641 t = dc->imm & (1 << 9);
643 LOG_DIS("bs%s%s r%d r%d r%d\n",
644 s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
646 t0 = tcg_temp_new();
648 tcg_gen_mov_tl(t0, *(dec_alu_op_b(dc)));
649 tcg_gen_andi_tl(t0, t0, 31);
651 if (s)
652 tcg_gen_shl_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
653 else {
654 if (t)
655 tcg_gen_sar_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
656 else
657 tcg_gen_shr_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
661 static void dec_bit(DisasContext *dc)
663 TCGv t0, t1;
664 unsigned int op;
665 int mem_index = cpu_mmu_index(dc->env);
667 op = dc->ir & ((1 << 8) - 1);
668 switch (op) {
669 case 0x21:
670 /* src. */
671 t0 = tcg_temp_new();
673 LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
674 tcg_gen_andi_tl(t0, cpu_R[dc->ra], 1);
675 if (dc->rd) {
676 t1 = tcg_temp_new();
677 read_carry(dc, t1);
678 tcg_gen_shli_tl(t1, t1, 31);
680 tcg_gen_shri_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
681 tcg_gen_or_tl(cpu_R[dc->rd], cpu_R[dc->rd], t1);
682 tcg_temp_free(t1);
685 /* Update carry. */
686 write_carry(dc, t0);
687 tcg_temp_free(t0);
688 break;
690 case 0x1:
691 case 0x41:
692 /* srl. */
693 t0 = tcg_temp_new();
694 LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
696 /* Update carry. */
697 tcg_gen_andi_tl(t0, cpu_R[dc->ra], 1);
698 write_carry(dc, t0);
699 tcg_temp_free(t0);
700 if (dc->rd) {
701 if (op == 0x41)
702 tcg_gen_shri_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
703 else
704 tcg_gen_sari_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
706 break;
707 case 0x60:
708 LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
709 tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
710 break;
711 case 0x61:
712 LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
713 tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
714 break;
715 case 0x64:
716 case 0x66:
717 case 0x74:
718 case 0x76:
719 /* wdc. */
720 LOG_DIS("wdc r%d\n", dc->ra);
721 if ((dc->tb_flags & MSR_EE_FLAG)
722 && mem_index == MMU_USER_IDX) {
723 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
724 t_gen_raise_exception(dc, EXCP_HW_EXCP);
725 return;
727 break;
728 case 0x68:
729 /* wic. */
730 LOG_DIS("wic r%d\n", dc->ra);
731 if ((dc->tb_flags & MSR_EE_FLAG)
732 && mem_index == MMU_USER_IDX) {
733 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
734 t_gen_raise_exception(dc, EXCP_HW_EXCP);
735 return;
737 break;
738 default:
739 cpu_abort(dc->env, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
740 dc->pc, op, dc->rd, dc->ra, dc->rb);
741 break;
745 static inline void sync_jmpstate(DisasContext *dc)
747 if (dc->jmp == JMP_DIRECT) {
748 dc->jmp = JMP_INDIRECT;
749 tcg_gen_movi_tl(env_btaken, 1);
750 tcg_gen_movi_tl(env_btarget, dc->jmp_pc);
754 static void dec_imm(DisasContext *dc)
756 LOG_DIS("imm %x\n", dc->imm << 16);
757 tcg_gen_movi_tl(env_imm, (dc->imm << 16));
758 dc->tb_flags |= IMM_FLAG;
759 dc->clear_imm = 0;
762 static inline void gen_load(DisasContext *dc, TCGv dst, TCGv addr,
763 unsigned int size)
765 int mem_index = cpu_mmu_index(dc->env);
767 if (size == 1) {
768 tcg_gen_qemu_ld8u(dst, addr, mem_index);
769 } else if (size == 2) {
770 tcg_gen_qemu_ld16u(dst, addr, mem_index);
771 } else if (size == 4) {
772 tcg_gen_qemu_ld32u(dst, addr, mem_index);
773 } else
774 cpu_abort(dc->env, "Incorrect load size %d\n", size);
777 static inline TCGv *compute_ldst_addr(DisasContext *dc, TCGv *t)
779 unsigned int extimm = dc->tb_flags & IMM_FLAG;
781 /* Treat the fast cases first. */
782 if (!dc->type_b) {
783 *t = tcg_temp_new();
784 tcg_gen_add_tl(*t, cpu_R[dc->ra], cpu_R[dc->rb]);
785 return t;
787 /* Immediate. */
788 if (!extimm) {
789 if (dc->imm == 0) {
790 return &cpu_R[dc->ra];
792 *t = tcg_temp_new();
793 tcg_gen_movi_tl(*t, (int32_t)((int16_t)dc->imm));
794 tcg_gen_add_tl(*t, cpu_R[dc->ra], *t);
795 } else {
796 *t = tcg_temp_new();
797 tcg_gen_add_tl(*t, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
800 return t;
803 static void dec_load(DisasContext *dc)
805 TCGv t, *addr;
806 unsigned int size;
808 size = 1 << (dc->opcode & 3);
809 if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
810 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
811 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
812 t_gen_raise_exception(dc, EXCP_HW_EXCP);
813 return;
816 LOG_DIS("l %x %d\n", dc->opcode, size);
817 t_sync_flags(dc);
818 addr = compute_ldst_addr(dc, &t);
820 /* If we get a fault on a dslot, the jmpstate better be in sync. */
821 sync_jmpstate(dc);
823 /* Verify alignment if needed. */
824 if ((dc->env->pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
825 TCGv v = tcg_temp_new();
828 * Microblaze gives MMU faults priority over faults due to
829 * unaligned addresses. That's why we speculatively do the load
830 * into v. If the load succeeds, we verify alignment of the
831 * address and if that succeeds we write into the destination reg.
833 gen_load(dc, v, *addr, size);
835 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
836 gen_helper_memalign(*addr, tcg_const_tl(dc->rd),
837 tcg_const_tl(0), tcg_const_tl(size - 1));
838 if (dc->rd)
839 tcg_gen_mov_tl(cpu_R[dc->rd], v);
840 tcg_temp_free(v);
841 } else {
842 if (dc->rd) {
843 gen_load(dc, cpu_R[dc->rd], *addr, size);
844 } else {
845 gen_load(dc, env_imm, *addr, size);
849 if (addr == &t)
850 tcg_temp_free(t);
853 static void gen_store(DisasContext *dc, TCGv addr, TCGv val,
854 unsigned int size)
856 int mem_index = cpu_mmu_index(dc->env);
858 if (size == 1)
859 tcg_gen_qemu_st8(val, addr, mem_index);
860 else if (size == 2) {
861 tcg_gen_qemu_st16(val, addr, mem_index);
862 } else if (size == 4) {
863 tcg_gen_qemu_st32(val, addr, mem_index);
864 } else
865 cpu_abort(dc->env, "Incorrect store size %d\n", size);
868 static void dec_store(DisasContext *dc)
870 TCGv t, *addr;
871 unsigned int size;
873 size = 1 << (dc->opcode & 3);
875 if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
876 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
877 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
878 t_gen_raise_exception(dc, EXCP_HW_EXCP);
879 return;
882 LOG_DIS("s%d%s\n", size, dc->type_b ? "i" : "");
883 t_sync_flags(dc);
884 /* If we get a fault on a dslot, the jmpstate better be in sync. */
885 sync_jmpstate(dc);
886 addr = compute_ldst_addr(dc, &t);
888 gen_store(dc, *addr, cpu_R[dc->rd], size);
890 /* Verify alignment if needed. */
891 if ((dc->env->pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
892 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
893 /* FIXME: if the alignment is wrong, we should restore the value
894 * in memory.
896 gen_helper_memalign(*addr, tcg_const_tl(dc->rd),
897 tcg_const_tl(1), tcg_const_tl(size - 1));
900 if (addr == &t)
901 tcg_temp_free(t);
904 static inline void eval_cc(DisasContext *dc, unsigned int cc,
905 TCGv d, TCGv a, TCGv b)
907 int l1;
909 switch (cc) {
910 case CC_EQ:
911 l1 = gen_new_label();
912 tcg_gen_movi_tl(env_btaken, 1);
913 tcg_gen_brcond_tl(TCG_COND_EQ, a, b, l1);
914 tcg_gen_movi_tl(env_btaken, 0);
915 gen_set_label(l1);
916 break;
917 case CC_NE:
918 l1 = gen_new_label();
919 tcg_gen_movi_tl(env_btaken, 1);
920 tcg_gen_brcond_tl(TCG_COND_NE, a, b, l1);
921 tcg_gen_movi_tl(env_btaken, 0);
922 gen_set_label(l1);
923 break;
924 case CC_LT:
925 l1 = gen_new_label();
926 tcg_gen_movi_tl(env_btaken, 1);
927 tcg_gen_brcond_tl(TCG_COND_LT, a, b, l1);
928 tcg_gen_movi_tl(env_btaken, 0);
929 gen_set_label(l1);
930 break;
931 case CC_LE:
932 l1 = gen_new_label();
933 tcg_gen_movi_tl(env_btaken, 1);
934 tcg_gen_brcond_tl(TCG_COND_LE, a, b, l1);
935 tcg_gen_movi_tl(env_btaken, 0);
936 gen_set_label(l1);
937 break;
938 case CC_GE:
939 l1 = gen_new_label();
940 tcg_gen_movi_tl(env_btaken, 1);
941 tcg_gen_brcond_tl(TCG_COND_GE, a, b, l1);
942 tcg_gen_movi_tl(env_btaken, 0);
943 gen_set_label(l1);
944 break;
945 case CC_GT:
946 l1 = gen_new_label();
947 tcg_gen_movi_tl(env_btaken, 1);
948 tcg_gen_brcond_tl(TCG_COND_GT, a, b, l1);
949 tcg_gen_movi_tl(env_btaken, 0);
950 gen_set_label(l1);
951 break;
952 default:
953 cpu_abort(dc->env, "Unknown condition code %x.\n", cc);
954 break;
958 static void eval_cond_jmp(DisasContext *dc, TCGv pc_true, TCGv pc_false)
960 int l1;
962 l1 = gen_new_label();
963 /* Conditional jmp. */
964 tcg_gen_mov_tl(cpu_SR[SR_PC], pc_false);
965 tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, l1);
966 tcg_gen_mov_tl(cpu_SR[SR_PC], pc_true);
967 gen_set_label(l1);
970 static void dec_bcc(DisasContext *dc)
972 unsigned int cc;
973 unsigned int dslot;
975 cc = EXTRACT_FIELD(dc->ir, 21, 23);
976 dslot = dc->ir & (1 << 25);
977 LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
979 dc->delayed_branch = 1;
980 if (dslot) {
981 dc->delayed_branch = 2;
982 dc->tb_flags |= D_FLAG;
983 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
984 cpu_env, offsetof(CPUState, bimm));
987 tcg_gen_movi_tl(env_btarget, dc->pc);
988 tcg_gen_add_tl(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
989 eval_cc(dc, cc, env_btaken, cpu_R[dc->ra], tcg_const_tl(0));
990 dc->jmp = JMP_INDIRECT;
993 static void dec_br(DisasContext *dc)
995 unsigned int dslot, link, abs;
996 int mem_index = cpu_mmu_index(dc->env);
998 dslot = dc->ir & (1 << 20);
999 abs = dc->ir & (1 << 19);
1000 link = dc->ir & (1 << 18);
1001 LOG_DIS("br%s%s%s%s imm=%x\n",
1002 abs ? "a" : "", link ? "l" : "",
1003 dc->type_b ? "i" : "", dslot ? "d" : "",
1004 dc->imm);
1006 dc->delayed_branch = 1;
1007 if (dslot) {
1008 dc->delayed_branch = 2;
1009 dc->tb_flags |= D_FLAG;
1010 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1011 cpu_env, offsetof(CPUState, bimm));
1013 if (link && dc->rd)
1014 tcg_gen_movi_tl(cpu_R[dc->rd], dc->pc);
1016 dc->jmp = JMP_INDIRECT;
1017 if (abs) {
1018 tcg_gen_movi_tl(env_btaken, 1);
1019 tcg_gen_mov_tl(env_btarget, *(dec_alu_op_b(dc)));
1020 if (link && !dslot) {
1021 if (!(dc->tb_flags & IMM_FLAG) && (dc->imm == 8 || dc->imm == 0x18))
1022 t_gen_raise_exception(dc, EXCP_BREAK);
1023 if (dc->imm == 0) {
1024 if ((dc->tb_flags & MSR_EE_FLAG) && mem_index == MMU_USER_IDX) {
1025 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1026 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1027 return;
1030 t_gen_raise_exception(dc, EXCP_DEBUG);
1033 } else {
1034 if (!dc->type_b || (dc->tb_flags & IMM_FLAG)) {
1035 tcg_gen_movi_tl(env_btaken, 1);
1036 tcg_gen_movi_tl(env_btarget, dc->pc);
1037 tcg_gen_add_tl(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
1038 } else {
1039 dc->jmp = JMP_DIRECT;
1040 dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1045 static inline void do_rti(DisasContext *dc)
1047 TCGv t0, t1;
1048 t0 = tcg_temp_new();
1049 t1 = tcg_temp_new();
1050 tcg_gen_shri_tl(t0, cpu_SR[SR_MSR], 1);
1051 tcg_gen_ori_tl(t1, cpu_SR[SR_MSR], MSR_IE);
1052 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1054 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1055 tcg_gen_or_tl(t1, t1, t0);
1056 msr_write(dc, t1);
1057 tcg_temp_free(t1);
1058 tcg_temp_free(t0);
1059 dc->tb_flags &= ~DRTI_FLAG;
1062 static inline void do_rtb(DisasContext *dc)
1064 TCGv t0, t1;
1065 t0 = tcg_temp_new();
1066 t1 = tcg_temp_new();
1067 tcg_gen_andi_tl(t1, cpu_SR[SR_MSR], ~MSR_BIP);
1068 tcg_gen_shri_tl(t0, t1, 1);
1069 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1071 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1072 tcg_gen_or_tl(t1, t1, t0);
1073 msr_write(dc, t1);
1074 tcg_temp_free(t1);
1075 tcg_temp_free(t0);
1076 dc->tb_flags &= ~DRTB_FLAG;
1079 static inline void do_rte(DisasContext *dc)
1081 TCGv t0, t1;
1082 t0 = tcg_temp_new();
1083 t1 = tcg_temp_new();
1085 tcg_gen_ori_tl(t1, cpu_SR[SR_MSR], MSR_EE);
1086 tcg_gen_andi_tl(t1, t1, ~MSR_EIP);
1087 tcg_gen_shri_tl(t0, t1, 1);
1088 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1090 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1091 tcg_gen_or_tl(t1, t1, t0);
1092 msr_write(dc, t1);
1093 tcg_temp_free(t1);
1094 tcg_temp_free(t0);
1095 dc->tb_flags &= ~DRTE_FLAG;
1098 static void dec_rts(DisasContext *dc)
1100 unsigned int b_bit, i_bit, e_bit;
1101 int mem_index = cpu_mmu_index(dc->env);
1103 i_bit = dc->ir & (1 << 21);
1104 b_bit = dc->ir & (1 << 22);
1105 e_bit = dc->ir & (1 << 23);
1107 dc->delayed_branch = 2;
1108 dc->tb_flags |= D_FLAG;
1109 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1110 cpu_env, offsetof(CPUState, bimm));
1112 if (i_bit) {
1113 LOG_DIS("rtid ir=%x\n", dc->ir);
1114 if ((dc->tb_flags & MSR_EE_FLAG)
1115 && mem_index == MMU_USER_IDX) {
1116 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1117 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1119 dc->tb_flags |= DRTI_FLAG;
1120 } else if (b_bit) {
1121 LOG_DIS("rtbd ir=%x\n", dc->ir);
1122 if ((dc->tb_flags & MSR_EE_FLAG)
1123 && mem_index == MMU_USER_IDX) {
1124 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1125 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1127 dc->tb_flags |= DRTB_FLAG;
1128 } else if (e_bit) {
1129 LOG_DIS("rted ir=%x\n", dc->ir);
1130 if ((dc->tb_flags & MSR_EE_FLAG)
1131 && mem_index == MMU_USER_IDX) {
1132 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1133 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1135 dc->tb_flags |= DRTE_FLAG;
1136 } else
1137 LOG_DIS("rts ir=%x\n", dc->ir);
1139 tcg_gen_movi_tl(env_btaken, 1);
1140 tcg_gen_add_tl(env_btarget, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
1143 static void dec_fpu(DisasContext *dc)
1145 if ((dc->tb_flags & MSR_EE_FLAG)
1146 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
1147 && !((dc->env->pvr.regs[2] & PVR2_USE_FPU_MASK))) {
1148 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_FPU);
1149 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1150 return;
1153 qemu_log ("unimplemented FPU insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1154 dc->abort_at_next_insn = 1;
1157 static void dec_null(DisasContext *dc)
1159 if ((dc->tb_flags & MSR_EE_FLAG)
1160 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
1161 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1162 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1163 return;
1165 qemu_log ("unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1166 dc->abort_at_next_insn = 1;
1169 static struct decoder_info {
1170 struct {
1171 uint32_t bits;
1172 uint32_t mask;
1174 void (*dec)(DisasContext *dc);
1175 } decinfo[] = {
1176 {DEC_ADD, dec_add},
1177 {DEC_SUB, dec_sub},
1178 {DEC_AND, dec_and},
1179 {DEC_XOR, dec_xor},
1180 {DEC_OR, dec_or},
1181 {DEC_BIT, dec_bit},
1182 {DEC_BARREL, dec_barrel},
1183 {DEC_LD, dec_load},
1184 {DEC_ST, dec_store},
1185 {DEC_IMM, dec_imm},
1186 {DEC_BR, dec_br},
1187 {DEC_BCC, dec_bcc},
1188 {DEC_RTS, dec_rts},
1189 {DEC_FPU, dec_fpu},
1190 {DEC_MUL, dec_mul},
1191 {DEC_DIV, dec_div},
1192 {DEC_MSR, dec_msr},
1193 {{0, 0}, dec_null}
1196 static inline void decode(DisasContext *dc)
1198 uint32_t ir;
1199 int i;
1201 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)))
1202 tcg_gen_debug_insn_start(dc->pc);
1204 dc->ir = ir = ldl_code(dc->pc);
1205 LOG_DIS("%8.8x\t", dc->ir);
1207 if (dc->ir)
1208 dc->nr_nops = 0;
1209 else {
1210 if ((dc->tb_flags & MSR_EE_FLAG)
1211 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
1212 && (dc->env->pvr.regs[2] & PVR2_OPCODE_0x0_ILL_MASK)) {
1213 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1214 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1215 return;
1218 LOG_DIS("nr_nops=%d\t", dc->nr_nops);
1219 dc->nr_nops++;
1220 if (dc->nr_nops > 4)
1221 cpu_abort(dc->env, "fetching nop sequence\n");
1223 /* bit 2 seems to indicate insn type. */
1224 dc->type_b = ir & (1 << 29);
1226 dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1227 dc->rd = EXTRACT_FIELD(ir, 21, 25);
1228 dc->ra = EXTRACT_FIELD(ir, 16, 20);
1229 dc->rb = EXTRACT_FIELD(ir, 11, 15);
1230 dc->imm = EXTRACT_FIELD(ir, 0, 15);
1232 /* Large switch for all insns. */
1233 for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1234 if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1235 decinfo[i].dec(dc);
1236 break;
1241 static void check_breakpoint(CPUState *env, DisasContext *dc)
1243 CPUBreakpoint *bp;
1245 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1246 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1247 if (bp->pc == dc->pc) {
1248 t_gen_raise_exception(dc, EXCP_DEBUG);
1249 dc->is_jmp = DISAS_UPDATE;
1255 /* generate intermediate code for basic block 'tb'. */
1256 static void
1257 gen_intermediate_code_internal(CPUState *env, TranslationBlock *tb,
1258 int search_pc)
1260 uint16_t *gen_opc_end;
1261 uint32_t pc_start;
1262 int j, lj;
1263 struct DisasContext ctx;
1264 struct DisasContext *dc = &ctx;
1265 uint32_t next_page_start, org_flags;
1266 target_ulong npc;
1267 int num_insns;
1268 int max_insns;
1270 qemu_log_try_set_file(stderr);
1272 pc_start = tb->pc;
1273 dc->env = env;
1274 dc->tb = tb;
1275 org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1277 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
1279 dc->is_jmp = DISAS_NEXT;
1280 dc->jmp = 0;
1281 dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1282 dc->ppc = pc_start;
1283 dc->pc = pc_start;
1284 dc->cache_pc = -1;
1285 dc->singlestep_enabled = env->singlestep_enabled;
1286 dc->cpustate_changed = 0;
1287 dc->abort_at_next_insn = 0;
1288 dc->nr_nops = 0;
1290 if (pc_start & 3)
1291 cpu_abort(env, "Microblaze: unaligned PC=%x\n", pc_start);
1293 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1294 #if !SIM_COMPAT
1295 qemu_log("--------------\n");
1296 log_cpu_state(env, 0);
1297 #endif
1300 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1301 lj = -1;
1302 num_insns = 0;
1303 max_insns = tb->cflags & CF_COUNT_MASK;
1304 if (max_insns == 0)
1305 max_insns = CF_COUNT_MASK;
1307 gen_icount_start();
1310 #if SIM_COMPAT
1311 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1312 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
1313 gen_helper_debug();
1315 #endif
1316 check_breakpoint(env, dc);
1318 if (search_pc) {
1319 j = gen_opc_ptr - gen_opc_buf;
1320 if (lj < j) {
1321 lj++;
1322 while (lj < j)
1323 gen_opc_instr_start[lj++] = 0;
1325 gen_opc_pc[lj] = dc->pc;
1326 gen_opc_instr_start[lj] = 1;
1327 gen_opc_icount[lj] = num_insns;
1330 /* Pretty disas. */
1331 LOG_DIS("%8.8x:\t", dc->pc);
1333 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
1334 gen_io_start();
1336 dc->clear_imm = 1;
1337 decode(dc);
1338 if (dc->clear_imm)
1339 dc->tb_flags &= ~IMM_FLAG;
1340 dc->ppc = dc->pc;
1341 dc->pc += 4;
1342 num_insns++;
1344 if (dc->delayed_branch) {
1345 dc->delayed_branch--;
1346 if (!dc->delayed_branch) {
1347 if (dc->tb_flags & DRTI_FLAG)
1348 do_rti(dc);
1349 if (dc->tb_flags & DRTB_FLAG)
1350 do_rtb(dc);
1351 if (dc->tb_flags & DRTE_FLAG)
1352 do_rte(dc);
1353 /* Clear the delay slot flag. */
1354 dc->tb_flags &= ~D_FLAG;
1355 /* If it is a direct jump, try direct chaining. */
1356 if (dc->jmp != JMP_DIRECT) {
1357 eval_cond_jmp(dc, env_btarget, tcg_const_tl(dc->pc));
1358 dc->is_jmp = DISAS_JUMP;
1360 break;
1363 if (env->singlestep_enabled)
1364 break;
1365 } while (!dc->is_jmp && !dc->cpustate_changed
1366 && gen_opc_ptr < gen_opc_end
1367 && !singlestep
1368 && (dc->pc < next_page_start)
1369 && num_insns < max_insns);
1371 npc = dc->pc;
1372 if (dc->jmp == JMP_DIRECT) {
1373 if (dc->tb_flags & D_FLAG) {
1374 dc->is_jmp = DISAS_UPDATE;
1375 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1376 sync_jmpstate(dc);
1377 } else
1378 npc = dc->jmp_pc;
1381 if (tb->cflags & CF_LAST_IO)
1382 gen_io_end();
1383 /* Force an update if the per-tb cpu state has changed. */
1384 if (dc->is_jmp == DISAS_NEXT
1385 && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1386 dc->is_jmp = DISAS_UPDATE;
1387 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1389 t_sync_flags(dc);
1391 if (unlikely(env->singlestep_enabled)) {
1392 t_gen_raise_exception(dc, EXCP_DEBUG);
1393 if (dc->is_jmp == DISAS_NEXT)
1394 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1395 } else {
1396 switch(dc->is_jmp) {
1397 case DISAS_NEXT:
1398 gen_goto_tb(dc, 1, npc);
1399 break;
1400 default:
1401 case DISAS_JUMP:
1402 case DISAS_UPDATE:
1403 /* indicate that the hash table must be used
1404 to find the next TB */
1405 tcg_gen_exit_tb(0);
1406 break;
1407 case DISAS_TB_JUMP:
1408 /* nothing more to generate */
1409 break;
1412 gen_icount_end(tb, num_insns);
1413 *gen_opc_ptr = INDEX_op_end;
1414 if (search_pc) {
1415 j = gen_opc_ptr - gen_opc_buf;
1416 lj++;
1417 while (lj <= j)
1418 gen_opc_instr_start[lj++] = 0;
1419 } else {
1420 tb->size = dc->pc - pc_start;
1421 tb->icount = num_insns;
1424 #ifdef DEBUG_DISAS
1425 #if !SIM_COMPAT
1426 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1427 qemu_log("\n");
1428 #if DISAS_GNU
1429 log_target_disas(pc_start, dc->pc - pc_start, 0);
1430 #endif
1431 qemu_log("\nisize=%d osize=%zd\n",
1432 dc->pc - pc_start, gen_opc_ptr - gen_opc_buf);
1434 #endif
1435 #endif
1436 assert(!dc->abort_at_next_insn);
1439 void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
1441 gen_intermediate_code_internal(env, tb, 0);
1444 void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
1446 gen_intermediate_code_internal(env, tb, 1);
1449 void cpu_dump_state (CPUState *env, FILE *f,
1450 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1451 int flags)
1453 int i;
1455 if (!env || !f)
1456 return;
1458 cpu_fprintf(f, "IN: PC=%x %s\n",
1459 env->sregs[SR_PC], lookup_symbol(env->sregs[SR_PC]));
1460 cpu_fprintf(f, "rmsr=%x resr=%x rear=%x debug[%x] imm=%x iflags=%x\n",
1461 env->sregs[SR_MSR], env->sregs[SR_ESR], env->sregs[SR_EAR],
1462 env->debug, env->imm, env->iflags);
1463 cpu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1464 env->btaken, env->btarget,
1465 (env->sregs[SR_MSR] & MSR_UM) ? "user" : "kernel",
1466 (env->sregs[SR_MSR] & MSR_UMS) ? "user" : "kernel",
1467 (env->sregs[SR_MSR] & MSR_EIP),
1468 (env->sregs[SR_MSR] & MSR_IE));
1470 for (i = 0; i < 32; i++) {
1471 cpu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1472 if ((i + 1) % 4 == 0)
1473 cpu_fprintf(f, "\n");
1475 cpu_fprintf(f, "\n\n");
1478 CPUState *cpu_mb_init (const char *cpu_model)
1480 CPUState *env;
1481 static int tcg_initialized = 0;
1482 int i;
1484 env = qemu_mallocz(sizeof(CPUState));
1486 cpu_exec_init(env);
1487 cpu_reset(env);
1490 if (tcg_initialized)
1491 return env;
1493 tcg_initialized = 1;
1495 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
1497 env_debug = tcg_global_mem_new(TCG_AREG0,
1498 offsetof(CPUState, debug),
1499 "debug0");
1500 env_iflags = tcg_global_mem_new(TCG_AREG0,
1501 offsetof(CPUState, iflags),
1502 "iflags");
1503 env_imm = tcg_global_mem_new(TCG_AREG0,
1504 offsetof(CPUState, imm),
1505 "imm");
1506 env_btarget = tcg_global_mem_new(TCG_AREG0,
1507 offsetof(CPUState, btarget),
1508 "btarget");
1509 env_btaken = tcg_global_mem_new(TCG_AREG0,
1510 offsetof(CPUState, btaken),
1511 "btaken");
1512 for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1513 cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
1514 offsetof(CPUState, regs[i]),
1515 regnames[i]);
1517 for (i = 0; i < ARRAY_SIZE(cpu_SR); i++) {
1518 cpu_SR[i] = tcg_global_mem_new(TCG_AREG0,
1519 offsetof(CPUState, sregs[i]),
1520 special_regnames[i]);
1522 #define GEN_HELPER 2
1523 #include "helper.h"
1525 return env;
1528 void cpu_reset (CPUState *env)
1530 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
1531 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
1532 log_cpu_state(env, 0);
1535 memset(env, 0, offsetof(CPUMBState, breakpoints));
1536 tlb_flush(env, 1);
1538 env->pvr.regs[0] = PVR0_PVR_FULL_MASK \
1539 | PVR0_USE_BARREL_MASK \
1540 | PVR0_USE_DIV_MASK \
1541 | PVR0_USE_HW_MUL_MASK \
1542 | PVR0_USE_EXC_MASK \
1543 | PVR0_USE_ICACHE_MASK \
1544 | PVR0_USE_DCACHE_MASK \
1545 | PVR0_USE_MMU \
1546 | (0xb << 8);
1547 env->pvr.regs[2] = PVR2_D_OPB_MASK \
1548 | PVR2_D_LMB_MASK \
1549 | PVR2_I_OPB_MASK \
1550 | PVR2_I_LMB_MASK \
1551 | PVR2_USE_MSR_INSTR \
1552 | PVR2_USE_PCMP_INSTR \
1553 | PVR2_USE_BARREL_MASK \
1554 | PVR2_USE_DIV_MASK \
1555 | PVR2_USE_HW_MUL_MASK \
1556 | PVR2_USE_MUL64_MASK \
1557 | 0;
1558 env->pvr.regs[10] = 0x0c000000; /* Default to spartan 3a dsp family. */
1559 env->pvr.regs[11] = PVR11_USE_MMU | (16 << 17);
1561 env->sregs[SR_MSR] = 0;
1562 #if defined(CONFIG_USER_ONLY)
1563 /* start in user mode with interrupts enabled. */
1564 env->pvr.regs[10] = 0x0c000000; /* Spartan 3a dsp. */
1565 #else
1566 mmu_init(&env->mmu);
1567 env->mmu.c_mmu = 3;
1568 env->mmu.c_mmu_tlb_access = 3;
1569 env->mmu.c_mmu_zones = 16;
1570 #endif
1573 void gen_pc_load(CPUState *env, struct TranslationBlock *tb,
1574 unsigned long searched_pc, int pc_pos, void *puc)
1576 env->sregs[SR_PC] = gen_opc_pc[pc_pos];