2 * MIPS32 emulation for qemu: main translation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define MIPS_DEBUG_DISAS
32 //#define MIPS_SINGLE_STEP
35 #define DEF(s, n, copy_size) INDEX_op_ ## s,
41 static uint16_t *gen_opc_ptr;
42 static uint32_t *gen_opparam_ptr;
46 const unsigned char *regnames[] =
47 { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
48 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
49 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
50 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
52 /* Warning: no function for r0 register (hard wired to zero) */
53 #define GEN32(func, NAME) \
54 static GenOpFunc *NAME ## _table [32] = { \
55 NULL, NAME ## 1, NAME ## 2, NAME ## 3, \
56 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
57 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
58 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
59 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
60 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
61 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
62 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
64 static inline void func(int n) \
66 NAME ## _table[n](); \
69 /* General purpose registers moves */
70 GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
71 GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
72 GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
74 GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
75 GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
77 typedef struct DisasContext {
78 struct TranslationBlock *tb;
79 target_ulong pc, saved_pc;
81 /* Routine used to access memory */
83 uint32_t hflags, saved_hflags;
90 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
93 BS_STOP = 1, /* We want to stop translation for any reason */
94 BS_BRANCH = 2, /* We reached a branch condition */
95 BS_EXCP = 3, /* We reached an exception condition */
98 #if defined MIPS_DEBUG_DISAS
99 #define MIPS_DEBUG(fmt, args...) \
101 if (loglevel & CPU_LOG_TB_IN_ASM) { \
102 fprintf(logfile, "%08x: %08x " fmt "\n", \
103 ctx->pc, ctx->opcode , ##args); \
107 #define MIPS_DEBUG(fmt, args...) do { } while(0)
110 #define MIPS_INVAL(op) \
112 MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26, \
113 ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F)); \
116 #define GEN_LOAD_REG_TN(Tn, Rn) \
119 glue(gen_op_reset_, Tn)(); \
121 glue(gen_op_load_gpr_, Tn)(Rn); \
125 #define GEN_LOAD_IMM_TN(Tn, Imm) \
128 glue(gen_op_reset_, Tn)(); \
130 glue(gen_op_set_, Tn)(Imm); \
134 #define GEN_STORE_TN_REG(Rn, Tn) \
137 glue(glue(gen_op_store_, Tn),_gpr)(Rn); \
141 static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
143 #if defined MIPS_DEBUG_DISAS
144 if (loglevel & CPU_LOG_TB_IN_ASM) {
145 fprintf(logfile, "hflags %08x saved %08x\n",
146 ctx->hflags, ctx->saved_hflags);
149 if (do_save_pc && ctx->pc != ctx->saved_pc) {
150 gen_op_save_pc(ctx->pc);
151 ctx->saved_pc = ctx->pc;
153 if (ctx->hflags != ctx->saved_hflags) {
154 gen_op_save_state(ctx->hflags);
155 ctx->saved_hflags = ctx->hflags;
156 if (ctx->hflags & MIPS_HFLAG_BR) {
157 gen_op_save_breg_target();
158 } else if (ctx->hflags & MIPS_HFLAG_B) {
159 gen_op_save_btarget(ctx->btarget);
160 } else if (ctx->hflags & MIPS_HFLAG_BMASK) {
162 gen_op_save_btarget(ctx->btarget);
167 static inline void generate_exception (DisasContext *ctx, int excp)
169 #if defined MIPS_DEBUG_DISAS
170 if (loglevel & CPU_LOG_TB_IN_ASM)
171 fprintf(logfile, "%s: raise exception %d\n", __func__, excp);
173 save_cpu_state(ctx, 1);
174 gen_op_raise_exception(excp);
175 ctx->bstate = BS_EXCP;
178 #if defined(CONFIG_USER_ONLY)
179 #define op_ldst(name) gen_op_##name##_raw()
180 #define OP_LD_TABLE(width)
181 #define OP_ST_TABLE(width)
183 #define op_ldst(name) (*gen_op_##name[ctx->mem_idx])()
184 #define OP_LD_TABLE(width) \
185 static GenOpFunc *gen_op_l##width[] = { \
186 &gen_op_l##width##_user, \
187 &gen_op_l##width##_kernel, \
189 #define OP_ST_TABLE(width) \
190 static GenOpFunc *gen_op_s##width[] = { \
191 &gen_op_s##width##_user, \
192 &gen_op_s##width##_kernel, \
220 static void gen_ldst (DisasContext *ctx, uint16_t opc, int rt,
221 int base, int16_t offset)
223 const unsigned char *opn = "unk";
226 GEN_LOAD_IMM_TN(T0, offset);
227 } else if (offset == 0) {
228 gen_op_load_gpr_T0(base);
230 gen_op_load_gpr_T0(base);
231 gen_op_set_T1(offset);
234 /* Don't do NOP if destination is zero: we must perform the actual
238 #if defined(TARGET_MIPS64)
240 #if defined (MIPS_HAS_UNALIGNED_LS)
244 GEN_STORE_TN_REG(rt, T0);
248 #if defined (MIPS_HAS_UNALIGNED_LS)
251 GEN_LOAD_REG_TN(T1, rt);
257 GEN_STORE_TN_REG(rt, T0);
261 GEN_LOAD_REG_TN(T1, rt);
267 GEN_STORE_TN_REG(rt, T0);
271 GEN_LOAD_REG_TN(T1, rt);
277 #if defined (MIPS_HAS_UNALIGNED_LS)
281 GEN_STORE_TN_REG(rt, T0);
285 #if defined (MIPS_HAS_UNALIGNED_LS)
288 GEN_LOAD_REG_TN(T1, rt);
293 #if defined (MIPS_HAS_UNALIGNED_LS)
297 GEN_STORE_TN_REG(rt, T0);
301 #if defined (MIPS_HAS_UNALIGNED_LS)
304 GEN_LOAD_REG_TN(T1, rt);
309 #if defined (MIPS_HAS_UNALIGNED_LS)
313 GEN_STORE_TN_REG(rt, T0);
318 GEN_STORE_TN_REG(rt, T0);
322 GEN_LOAD_REG_TN(T1, rt);
328 GEN_STORE_TN_REG(rt, T0);
332 GEN_LOAD_REG_TN(T1, rt);
334 GEN_STORE_TN_REG(rt, T0);
338 GEN_LOAD_REG_TN(T1, rt);
343 GEN_LOAD_REG_TN(T1, rt);
345 GEN_STORE_TN_REG(rt, T0);
349 GEN_LOAD_REG_TN(T1, rt);
355 GEN_STORE_TN_REG(rt, T0);
359 GEN_LOAD_REG_TN(T1, rt);
361 GEN_STORE_TN_REG(rt, T0);
365 MIPS_INVAL("load/store");
366 generate_exception(ctx, EXCP_RI);
369 MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
372 /* Arithmetic with immediate operand */
373 static void gen_arith_imm (DisasContext *ctx, uint16_t opc, int rt,
377 const unsigned char *opn = "unk";
379 if (rt == 0 && opc != OPC_ADDI) {
380 /* if no destination, treat it as a NOP
381 * For addi, we must generate the overflow exception when needed.
386 if (opc == OPC_ADDI || opc == OPC_ADDIU ||
387 opc == OPC_SLTI || opc == OPC_SLTIU)
388 uimm = (int32_t)imm; /* Sign extent to 32 bits */
390 uimm = (uint16_t)imm;
391 if (opc != OPC_LUI) {
392 GEN_LOAD_REG_TN(T0, rs);
393 GEN_LOAD_IMM_TN(T1, uimm);
396 GEN_LOAD_IMM_TN(T0, uimm);
400 save_cpu_state(ctx, 1);
444 MIPS_INVAL("imm arith");
445 generate_exception(ctx, EXCP_RI);
448 GEN_STORE_TN_REG(rt, T0);
449 MIPS_DEBUG("%s %s, %s, %x", opn, regnames[rt], regnames[rs], uimm);
453 static void gen_arith (DisasContext *ctx, uint16_t opc,
454 int rd, int rs, int rt)
456 const unsigned char *opn = "unk";
458 if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB) {
459 /* if no destination, treat it as a NOP
460 * For add & sub, we must generate the overflow exception when needed.
465 GEN_LOAD_REG_TN(T0, rs);
466 GEN_LOAD_REG_TN(T1, rt);
469 save_cpu_state(ctx, 1);
478 save_cpu_state(ctx, 1);
536 generate_exception(ctx, EXCP_RI);
539 GEN_STORE_TN_REG(rd, T0);
541 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
544 /* Arithmetic on HI/LO registers */
545 static void gen_HILO (DisasContext *ctx, uint16_t opc, int reg)
547 const unsigned char *opn = "unk";
549 if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
557 GEN_STORE_TN_REG(reg, T0);
562 GEN_STORE_TN_REG(reg, T0);
566 GEN_LOAD_REG_TN(T0, reg);
571 GEN_LOAD_REG_TN(T0, reg);
577 generate_exception(ctx, EXCP_RI);
580 MIPS_DEBUG("%s %s", opn, regnames[reg]);
583 static void gen_muldiv (DisasContext *ctx, uint16_t opc,
586 const unsigned char *opn = "unk";
588 GEN_LOAD_REG_TN(T0, rs);
589 GEN_LOAD_REG_TN(T1, rt);
624 MIPS_INVAL("mul/div");
625 generate_exception(ctx, EXCP_RI);
628 MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
631 static void gen_cl (DisasContext *ctx, uint16_t opc,
634 const unsigned char *opn = "unk";
640 GEN_LOAD_REG_TN(T0, rs);
654 generate_exception(ctx, EXCP_RI);
657 gen_op_store_T0_gpr(rd);
658 MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
662 static void gen_trap (DisasContext *ctx, uint16_t opc,
663 int rs, int rt, int16_t imm)
668 /* Load needed operands */
676 /* Compare two registers */
678 GEN_LOAD_REG_TN(T0, rs);
679 GEN_LOAD_REG_TN(T1, rt);
688 /* Compare register to immediate */
689 if (rs != 0 || imm != 0) {
690 GEN_LOAD_REG_TN(T0, rs);
691 GEN_LOAD_IMM_TN(T1, (int32_t)imm);
698 case OPC_TEQ: /* rs == rs */
699 case OPC_TEQI: /* r0 == 0 */
700 case OPC_TGE: /* rs >= rs */
701 case OPC_TGEI: /* r0 >= 0 */
702 case OPC_TGEU: /* rs >= rs unsigned */
703 case OPC_TGEIU: /* r0 >= 0 unsigned */
707 case OPC_TLT: /* rs < rs */
708 case OPC_TLTI: /* r0 < 0 */
709 case OPC_TLTU: /* rs < rs unsigned */
710 case OPC_TLTIU: /* r0 < 0 unsigned */
711 case OPC_TNE: /* rs != rs */
712 case OPC_TNEI: /* r0 != 0 */
713 /* Never trap: treat as NOP */
717 generate_exception(ctx, EXCP_RI);
748 generate_exception(ctx, EXCP_RI);
752 save_cpu_state(ctx, 1);
754 ctx->bstate = BS_STOP;
757 /* Branches (before delay slot) */
758 static void gen_compute_branch (DisasContext *ctx, uint16_t opc,
759 int rs, int rt, int32_t offset)
761 target_ulong btarget;
767 /* Load needed operands */
773 /* Compare two registers */
775 GEN_LOAD_REG_TN(T0, rs);
776 GEN_LOAD_REG_TN(T1, rt);
779 btarget = ctx->pc + 4 + offset;
793 /* Compare to zero */
795 gen_op_load_gpr_T0(rs);
798 btarget = ctx->pc + 4 + offset;
802 /* Jump to immediate */
803 btarget = ((ctx->pc + 4) & 0xF0000000) | offset;
807 /* Jump to register */
809 /* Only hint = 0 is valid */
810 generate_exception(ctx, EXCP_RI);
813 GEN_LOAD_REG_TN(T2, rs);
816 MIPS_INVAL("branch/jump");
817 generate_exception(ctx, EXCP_RI);
821 /* No condition to be computed */
823 case OPC_BEQ: /* rx == rx */
824 case OPC_BEQL: /* rx == rx likely */
825 case OPC_BGEZ: /* 0 >= 0 */
826 case OPC_BGEZL: /* 0 >= 0 likely */
827 case OPC_BLEZ: /* 0 <= 0 */
828 case OPC_BLEZL: /* 0 <= 0 likely */
830 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
831 MIPS_DEBUG("balways");
833 case OPC_BGEZAL: /* 0 >= 0 */
834 case OPC_BGEZALL: /* 0 >= 0 likely */
835 /* Always take and link */
837 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
838 MIPS_DEBUG("balways and link");
840 case OPC_BNE: /* rx != rx */
841 case OPC_BGTZ: /* 0 > 0 */
842 case OPC_BLTZ: /* 0 < 0 */
843 case OPC_BLTZAL: /* 0 < 0 */
845 MIPS_DEBUG("bnever (NOP)");
847 case OPC_BNEL: /* rx != rx likely */
848 case OPC_BGTZL: /* 0 > 0 likely */
849 case OPC_BLTZALL: /* 0 < 0 likely */
850 case OPC_BLTZL: /* 0 < 0 likely */
851 /* Skip the instruction in the delay slot */
852 MIPS_DEBUG("bnever and skip");
853 gen_op_branch((long)ctx->tb, ctx->pc + 4);
856 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
857 MIPS_DEBUG("j %08x", btarget);
861 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
862 MIPS_DEBUG("jal %08x", btarget);
865 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
866 MIPS_DEBUG("jr %s", regnames[rs]);
870 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
871 MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
874 MIPS_INVAL("branch/jump");
875 generate_exception(ctx, EXCP_RI);
882 MIPS_DEBUG("beq %s, %s, %08x",
883 regnames[rs], regnames[rt], btarget);
887 MIPS_DEBUG("beql %s, %s, %08x",
888 regnames[rs], regnames[rt], btarget);
892 MIPS_DEBUG("bne %s, %s, %08x",
893 regnames[rs], regnames[rt], btarget);
897 MIPS_DEBUG("bnel %s, %s, %08x",
898 regnames[rs], regnames[rt], btarget);
902 MIPS_DEBUG("bgez %s, %08x", regnames[rs], btarget);
906 MIPS_DEBUG("bgezl %s, %08x", regnames[rs], btarget);
910 MIPS_DEBUG("bgezal %s, %08x", regnames[rs], btarget);
916 MIPS_DEBUG("bgezall %s, %08x", regnames[rs], btarget);
920 MIPS_DEBUG("bgtz %s, %08x", regnames[rs], btarget);
924 MIPS_DEBUG("bgtzl %s, %08x", regnames[rs], btarget);
928 MIPS_DEBUG("blez %s, %08x", regnames[rs], btarget);
932 MIPS_DEBUG("blezl %s, %08x", regnames[rs], btarget);
936 MIPS_DEBUG("bltz %s, %08x", regnames[rs], btarget);
940 MIPS_DEBUG("bltzl %s, %08x", regnames[rs], btarget);
945 MIPS_DEBUG("bltzal %s, %08x", regnames[rs], btarget);
947 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BC;
952 MIPS_DEBUG("bltzall %s, %08x", regnames[rs], btarget);
954 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BL;
959 MIPS_DEBUG("enter ds: link %d cond %02x target %08x",
960 blink, ctx->hflags, btarget);
961 ctx->btarget = btarget;
963 gen_op_set_T0(ctx->pc + 8);
964 gen_op_store_T0_gpr(blink);
969 /* CP0 (MMU and control) */
970 static void gen_cp0 (DisasContext *ctx, uint16_t opc, int rt, int rd)
972 const unsigned char *opn = "unk";
974 if (!(ctx->CP0_Status & (1 << CP0St_CU0))) {
975 if (loglevel & CPU_LOG_TB_IN_ASM) {
976 fprintf(logfile, "CP0 is not usable\n");
978 gen_op_raise_exception_err(EXCP_CpU, 0);
987 gen_op_mfc0(rd, ctx->opcode & 0x7);
988 gen_op_store_T0_gpr(rt);
992 /* If we get an exception, we want to restart at next instruction */
994 save_cpu_state(ctx, 1);
996 GEN_LOAD_REG_TN(T0, rt);
997 gen_op_mtc0(rd, ctx->opcode & 0x7);
998 /* Stop translation as we may have switched the execution mode */
999 ctx->bstate = BS_STOP;
1002 #if defined(MIPS_USES_R4K_TLB)
1022 save_cpu_state(ctx, 0);
1024 ctx->bstate = BS_EXCP;
1028 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1029 generate_exception(ctx, EXCP_RI);
1031 save_cpu_state(ctx, 0);
1033 ctx->bstate = BS_EXCP;
1036 /* XXX: TODO: WAIT */
1038 if (loglevel & CPU_LOG_TB_IN_ASM) {
1039 fprintf(logfile, "Invalid CP0 opcode: %08x %03x %03x %03x\n",
1040 ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
1041 ((ctx->opcode >> 16) & 0x1F));
1043 generate_exception(ctx, EXCP_RI);
1046 MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
1049 /* Coprocessor 1 (FPU) */
1051 /* ISA extensions */
1052 /* MIPS16 extension to MIPS32 */
1053 /* SmartMIPS extension to MIPS32 */
1055 #ifdef TARGET_MIPS64
1056 static void gen_arith64 (DisasContext *ctx, uint16_t opc)
1058 if (func == 0x02 && rd == 0) {
1062 if (rs == 0 || rt == 0) {
1066 gen_op_load_gpr_T0(rs);
1067 gen_op_load_gpr_T1(rt);
1080 /* Coprocessor 3 (FPU) */
1082 /* MDMX extension to MIPS64 */
1083 /* MIPS-3D extension to MIPS64 */
1087 static void decode_opc (DisasContext *ctx)
1094 if ((ctx->hflags & MIPS_HFLAG_DS) &&
1095 (ctx->hflags & MIPS_HFLAG_BL)) {
1096 /* Handle blikely not taken case */
1097 MIPS_DEBUG("blikely condition (%08x)", ctx->pc + 4);
1098 gen_op_blikely((long)ctx->tb, ctx->pc + 4,
1099 ctx->hflags & ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS));
1101 op = ctx->opcode >> 26;
1102 rs = ((ctx->opcode >> 21) & 0x1F);
1103 rt = ((ctx->opcode >> 16) & 0x1F);
1104 rd = ((ctx->opcode >> 11) & 0x1F);
1105 sa = ((ctx->opcode >> 6) & 0x1F);
1106 imm = (int16_t)ctx->opcode;
1108 case 0x00: /* Special opcode */
1109 op1 = ctx->opcode & 0x3F;
1111 case 0x00: /* Arithmetic with immediate */
1113 gen_arith_imm(ctx, op1 | EXT_SPECIAL, rd, rt, sa);
1115 case 0x04: /* Arithmetic */
1120 gen_arith(ctx, op1 | EXT_SPECIAL, rd, rs, rt);
1122 case 0x18 ... 0x1B: /* MULT / DIV */
1123 gen_muldiv(ctx, op1 | EXT_SPECIAL, rs, rt);
1125 case 0x08 ... 0x09: /* Jumps */
1126 gen_compute_branch(ctx, op1 | EXT_SPECIAL, rs, rd, sa);
1128 case 0x30 ... 0x34: /* Traps */
1130 gen_trap(ctx, op1 | EXT_SPECIAL, rs, rt, -1);
1132 case 0x10: /* Move from HI/LO */
1134 gen_HILO(ctx, op1 | EXT_SPECIAL, rd);
1137 case 0x13: /* Move to HI/LO */
1138 gen_HILO(ctx, op1 | EXT_SPECIAL, rs);
1140 case 0x0C: /* SYSCALL */
1141 generate_exception(ctx, EXCP_SYSCALL);
1143 case 0x0D: /* BREAK */
1144 generate_exception(ctx, EXCP_BREAK);
1146 case 0x0F: /* SYNC */
1147 /* Treat as a noop */
1149 case 0x05: /* Pmon entry point */
1150 gen_op_pmon((ctx->opcode >> 6) & 0x1F);
1152 #if defined (MIPS_HAS_MOVCI)
1153 case 0x01: /* MOVCI */
1155 #if defined (TARGET_MIPS64)
1156 case 0x14: /* MIPS64 specific opcodes */
1165 default: /* Invalid */
1166 MIPS_INVAL("special");
1167 generate_exception(ctx, EXCP_RI);
1171 case 0x1C: /* Special2 opcode */
1172 op1 = ctx->opcode & 0x3F;
1174 #if defined (MIPS_USES_R4K_EXT)
1175 /* Those instructions are not part of MIPS32 core */
1176 case 0x00 ... 0x01: /* Multiply and add/sub */
1178 gen_muldiv(ctx, op1 | EXT_SPECIAL2, rs, rt);
1180 case 0x02: /* MUL */
1181 gen_arith(ctx, op1 | EXT_SPECIAL2, rd, rs, rt);
1183 case 0x20 ... 0x21: /* CLO / CLZ */
1184 gen_cl(ctx, op1 | EXT_SPECIAL2, rd, rs);
1187 case 0x3F: /* SDBBP */
1188 /* XXX: not clear which exception should be raised
1189 * when in debug mode...
1191 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1192 generate_exception(ctx, EXCP_DBp);
1194 generate_exception(ctx, EXCP_DBp);
1196 /* Treat as a noop */
1198 default: /* Invalid */
1199 MIPS_INVAL("special2");
1200 generate_exception(ctx, EXCP_RI);
1204 case 0x01: /* B REGIMM opcode */
1205 op1 = ((ctx->opcode >> 16) & 0x1F);
1207 case 0x00 ... 0x03: /* REGIMM branches */
1209 gen_compute_branch(ctx, op1 | EXT_REGIMM, rs, -1, imm << 2);
1211 case 0x08 ... 0x0C: /* Traps */
1213 gen_trap(ctx, op1 | EXT_REGIMM, rs, -1, imm);
1215 default: /* Invalid */
1216 MIPS_INVAL("REGIMM");
1217 generate_exception(ctx, EXCP_RI);
1221 case 0x10: /* CP0 opcode */
1222 op1 = ((ctx->opcode >> 21) & 0x1F);
1226 gen_cp0(ctx, op1 | EXT_CP0, rt, rd);
1229 gen_cp0(ctx, (ctx->opcode & 0x1F) | EXT_CP0, rt, rd);
1233 case 0x08 ... 0x0F: /* Arithmetic with immediate opcode */
1234 gen_arith_imm(ctx, op, rt, rs, imm);
1236 case 0x02 ... 0x03: /* Jump */
1237 offset = (int32_t)(ctx->opcode & 0x03FFFFFF) << 2;
1238 gen_compute_branch(ctx, op, rs, rt, offset);
1240 case 0x04 ... 0x07: /* Branch */
1242 gen_compute_branch(ctx, op, rs, rt, imm << 2);
1244 case 0x20 ... 0x26: /* Load and stores */
1248 gen_ldst(ctx, op, rt, rs, imm);
1250 case 0x2F: /* Cache operation */
1251 /* Treat as a noop */
1253 case 0x33: /* Prefetch */
1254 /* Treat as a noop */
1256 case 0x3F: /* HACK */
1258 #if defined(MIPS_USES_FPU)
1259 case 0x31 ... 0x32: /* Floating point load/store */
1263 /* Not implemented */
1264 /* XXX: not correct */
1266 case 0x11: /* CP1 opcode */
1267 /* Not implemented */
1268 /* XXX: not correct */
1269 case 0x12: /* CP2 opcode */
1270 /* Not implemented */
1271 /* XXX: not correct */
1272 case 0x13: /* CP3 opcode */
1273 /* Not implemented */
1274 /* XXX: not correct */
1275 #if defined (TARGET_MIPS64)
1280 /* MIPS64 opcodes */
1282 #if defined (MIPS_HAS_JALX)
1284 /* JALX: not implemented */
1288 #if defined (MIPS_HAS_LSC)
1289 case 0x31: /* LWC1 */
1290 case 0x32: /* LWC2 */
1291 case 0x35: /* SDC1 */
1292 case 0x36: /* SDC2 */
1294 default: /* Invalid */
1296 generate_exception(ctx, EXCP_RI);
1299 if (ctx->hflags & MIPS_HFLAG_DS) {
1300 int hflags = ctx->hflags;
1301 /* Branches completion */
1302 ctx->hflags &= ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS);
1303 ctx->bstate = BS_BRANCH;
1304 save_cpu_state(ctx, 0);
1305 switch (hflags & MIPS_HFLAG_BMASK) {
1307 /* unconditional branch */
1308 MIPS_DEBUG("unconditional branch");
1309 gen_op_branch((long)ctx->tb, ctx->btarget);
1312 /* blikely taken case */
1313 MIPS_DEBUG("blikely branch taken");
1314 gen_op_branch((long)ctx->tb, ctx->btarget);
1317 /* Conditional branch */
1318 MIPS_DEBUG("conditional branch");
1319 gen_op_bcond((long)ctx->tb, ctx->btarget, ctx->pc + 4);
1322 /* unconditional branch to register */
1323 MIPS_DEBUG("branch to register");
1327 MIPS_DEBUG("unknown branch");
1333 int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
1336 DisasContext ctx, *ctxp = &ctx;
1337 target_ulong pc_start;
1338 uint16_t *gen_opc_end;
1342 gen_opc_ptr = gen_opc_buf;
1343 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
1344 gen_opparam_ptr = gen_opparam_buf;
1347 ctx.bstate = BS_NONE;
1348 /* Restore delay slot state */
1349 ctx.hflags = env->hflags;
1350 ctx.saved_hflags = ctx.hflags;
1351 if (ctx.hflags & MIPS_HFLAG_BR) {
1352 gen_op_restore_breg_target();
1353 } else if (ctx.hflags & MIPS_HFLAG_B) {
1354 ctx.btarget = env->btarget;
1355 } else if (ctx.hflags & MIPS_HFLAG_BMASK) {
1356 /* If we are in the delay slot of a conditional branch,
1357 * restore the branch condition from env->bcond to T2
1359 ctx.btarget = env->btarget;
1360 gen_op_restore_bcond();
1362 #if defined(CONFIG_USER_ONLY)
1365 ctx.mem_idx = (ctx.hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM ? 0 : 1;
1367 ctx.CP0_Status = env->CP0_Status;
1369 if (loglevel & CPU_LOG_TB_CPU) {
1370 fprintf(logfile, "------------------------------------------------\n");
1371 cpu_dump_state(env, logfile, fprintf, 0);
1374 #if defined MIPS_DEBUG_DISAS
1375 if (loglevel & CPU_LOG_TB_IN_ASM)
1376 fprintf(logfile, "\ntb %p super %d cond %04x %04x\n",
1377 tb, ctx.mem_idx, ctx.hflags, env->hflags);
1379 while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
1381 j = gen_opc_ptr - gen_opc_buf;
1382 save_cpu_state(ctxp, 1);
1386 gen_opc_instr_start[lj++] = 0;
1387 gen_opc_pc[lj] = ctx.pc;
1388 gen_opc_instr_start[lj] = 1;
1391 ctx.opcode = ldl_code(ctx.pc);
1394 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
1396 #if defined (MIPS_SINGLE_STEP)
1400 if (ctx.bstate != BS_BRANCH && ctx.bstate != BS_EXCP) {
1401 save_cpu_state(ctxp, 0);
1402 gen_op_branch((long)ctx.tb, ctx.pc);
1405 /* Generate the return instruction */
1407 *gen_opc_ptr = INDEX_op_end;
1409 j = gen_opc_ptr - gen_opc_buf;
1412 gen_opc_instr_start[lj++] = 0;
1415 tb->size = ctx.pc - pc_start;
1418 #if defined MIPS_DEBUG_DISAS
1419 if (loglevel & CPU_LOG_TB_IN_ASM)
1420 fprintf(logfile, "\n");
1422 if (loglevel & CPU_LOG_TB_IN_ASM) {
1423 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
1424 target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
1425 fprintf(logfile, "\n");
1427 if (loglevel & CPU_LOG_TB_OP) {
1428 fprintf(logfile, "OP:\n");
1429 dump_ops(gen_opc_buf, gen_opparam_buf);
1430 fprintf(logfile, "\n");
1432 if (loglevel & CPU_LOG_TB_CPU) {
1433 fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
1440 int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
1442 return gen_intermediate_code_internal(env, tb, 0);
1445 int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
1447 return gen_intermediate_code_internal(env, tb, 1);
1450 void cpu_dump_state (CPUState *env, FILE *f,
1451 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1456 cpu_fprintf(f, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
1457 env->PC, env->HI, env->LO, env->hflags, env->btarget, env->bcond);
1458 for (i = 0; i < 32; i++) {
1460 cpu_fprintf(f, "GPR%02d:", i);
1461 cpu_fprintf(f, " %s %08x", regnames[i], env->gpr[i]);
1463 cpu_fprintf(f, "\n");
1465 cpu_fprintf(f, "CP0 Status 0x%08x Cause 0x%08x EPC 0x%08x\n",
1466 env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
1467 cpu_fprintf(f, " Config0 0x%08x Config1 0x%08x LLAddr 0x%08x\n",
1468 env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
1471 CPUMIPSState *cpu_mips_init (void)
1476 env = qemu_mallocz(sizeof(CPUMIPSState));
1481 env->PC = 0xBFC00000;
1482 #if defined (MIPS_USES_R4K_TLB)
1483 env->CP0_random = MIPS_TLB_NB - 1;
1486 env->CP0_Config0 = MIPS_CONFIG0;
1487 #if defined (MIPS_CONFIG1)
1488 env->CP0_Config1 = MIPS_CONFIG1;
1490 #if defined (MIPS_CONFIG2)
1491 env->CP0_Config2 = MIPS_CONFIG2;
1493 #if defined (MIPS_CONFIG3)
1494 env->CP0_Config3 = MIPS_CONFIG3;
1496 env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
1497 env->CP0_WatchLo = 0;
1498 env->hflags = MIPS_HFLAG_ERL;
1499 /* Count register increments in debug mode, EJTAG version 1 */
1500 env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
1501 env->CP0_PRid = MIPS_CPU;
1502 env->exception_index = EXCP_NONE;
1504 cpu_single_env = env;