2 * MIPS32 emulation for qemu: main translation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
5 * Copyright (c) 2006 Marius Groeger (FPU operations)
6 * Copyright (c) 2006 Thiemo Seufer (MIPS32R2 support)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 //#define MIPS_DEBUG_DISAS
34 //#define MIPS_SINGLE_STEP
36 #ifdef USE_DIRECT_JUMP
39 #define TBPARAM(x) (long)(x)
43 #define DEF(s, n, copy_size) INDEX_op_ ## s,
49 static uint16_t *gen_opc_ptr;
50 static uint32_t *gen_opparam_ptr;
54 /* MIPS major opcodes */
55 #define MASK_OP_MAJOR(op) (op & (0x3F << 26))
58 /* indirect opcode tables */
59 OPC_SPECIAL = (0x00 << 26),
60 OPC_REGIMM = (0x01 << 26),
61 OPC_CP0 = (0x10 << 26),
62 OPC_CP1 = (0x11 << 26),
63 OPC_CP2 = (0x12 << 26),
64 OPC_CP3 = (0x13 << 26),
65 OPC_SPECIAL2 = (0x1C << 26),
66 OPC_SPECIAL3 = (0x1F << 26),
67 /* arithmetic with immediate */
68 OPC_ADDI = (0x08 << 26),
69 OPC_ADDIU = (0x09 << 26),
70 OPC_SLTI = (0x0A << 26),
71 OPC_SLTIU = (0x0B << 26),
72 OPC_ANDI = (0x0C << 26),
73 OPC_ORI = (0x0D << 26),
74 OPC_XORI = (0x0E << 26),
75 OPC_LUI = (0x0F << 26),
76 OPC_DADDI = (0x18 << 26),
77 OPC_DADDIU = (0x19 << 26),
78 /* Jump and branches */
80 OPC_JAL = (0x03 << 26),
81 OPC_BEQ = (0x04 << 26), /* Unconditional if rs = rt = 0 (B) */
82 OPC_BEQL = (0x14 << 26),
83 OPC_BNE = (0x05 << 26),
84 OPC_BNEL = (0x15 << 26),
85 OPC_BLEZ = (0x06 << 26),
86 OPC_BLEZL = (0x16 << 26),
87 OPC_BGTZ = (0x07 << 26),
88 OPC_BGTZL = (0x17 << 26),
89 OPC_JALX = (0x1D << 26), /* MIPS 16 only */
91 OPC_LDL = (0x1A << 26),
92 OPC_LDR = (0x1B << 26),
93 OPC_LB = (0x20 << 26),
94 OPC_LH = (0x21 << 26),
95 OPC_LWL = (0x22 << 26),
96 OPC_LW = (0x23 << 26),
97 OPC_LBU = (0x24 << 26),
98 OPC_LHU = (0x25 << 26),
99 OPC_LWR = (0x26 << 26),
100 OPC_LWU = (0x27 << 26),
101 OPC_SB = (0x28 << 26),
102 OPC_SH = (0x29 << 26),
103 OPC_SWL = (0x2A << 26),
104 OPC_SW = (0x2B << 26),
105 OPC_SDL = (0x2C << 26),
106 OPC_SDR = (0x2D << 26),
107 OPC_SWR = (0x2E << 26),
108 OPC_LL = (0x30 << 26),
109 OPC_LLD = (0x34 << 26),
110 OPC_LD = (0x37 << 26),
111 OPC_SC = (0x38 << 26),
112 OPC_SCD = (0x3C << 26),
113 OPC_SD = (0x3F << 26),
114 /* Floating point load/store */
115 OPC_LWC1 = (0x31 << 26),
116 OPC_LWC2 = (0x32 << 26),
117 OPC_LDC1 = (0x35 << 26),
118 OPC_LDC2 = (0x36 << 26),
119 OPC_SWC1 = (0x39 << 26),
120 OPC_SWC2 = (0x3A << 26),
121 OPC_SDC1 = (0x3D << 26),
122 OPC_SDC2 = (0x3E << 26),
123 /* MDMX ASE specific */
124 OPC_MDMX = (0x1E << 26),
125 /* Cache and prefetch */
126 OPC_CACHE = (0x2F << 26),
127 OPC_PREF = (0x33 << 26),
128 /* Reserved major opcode */
129 OPC_MAJOR3B_RESERVED = (0x3B << 26),
132 /* MIPS special opcodes */
133 #define MASK_SPECIAL(op) MASK_OP_MAJOR(op) | (op & 0x3F)
137 OPC_SLL = 0x00 | OPC_SPECIAL,
138 /* NOP is SLL r0, r0, 0 */
139 /* SSNOP is SLL r0, r0, 1 */
140 /* EHB is SLL r0, r0, 3 */
141 OPC_SRL = 0x02 | OPC_SPECIAL, /* also ROTR */
142 OPC_SRA = 0x03 | OPC_SPECIAL,
143 OPC_SLLV = 0x04 | OPC_SPECIAL,
144 OPC_SRLV = 0x06 | OPC_SPECIAL,
145 OPC_SRAV = 0x07 | OPC_SPECIAL,
146 OPC_DSLLV = 0x14 | OPC_SPECIAL,
147 OPC_DSRLV = 0x16 | OPC_SPECIAL, /* also DROTRV */
148 OPC_DSRAV = 0x17 | OPC_SPECIAL,
149 OPC_DSLL = 0x38 | OPC_SPECIAL,
150 OPC_DSRL = 0x3A | OPC_SPECIAL, /* also DROTR */
151 OPC_DSRA = 0x3B | OPC_SPECIAL,
152 OPC_DSLL32 = 0x3C | OPC_SPECIAL,
153 OPC_DSRL32 = 0x3E | OPC_SPECIAL, /* also DROTR32 */
154 OPC_DSRA32 = 0x3F | OPC_SPECIAL,
155 /* Multiplication / division */
156 OPC_MULT = 0x18 | OPC_SPECIAL,
157 OPC_MULTU = 0x19 | OPC_SPECIAL,
158 OPC_DIV = 0x1A | OPC_SPECIAL,
159 OPC_DIVU = 0x1B | OPC_SPECIAL,
160 OPC_DMULT = 0x1C | OPC_SPECIAL,
161 OPC_DMULTU = 0x1D | OPC_SPECIAL,
162 OPC_DDIV = 0x1E | OPC_SPECIAL,
163 OPC_DDIVU = 0x1F | OPC_SPECIAL,
164 /* 2 registers arithmetic / logic */
165 OPC_ADD = 0x20 | OPC_SPECIAL,
166 OPC_ADDU = 0x21 | OPC_SPECIAL,
167 OPC_SUB = 0x22 | OPC_SPECIAL,
168 OPC_SUBU = 0x23 | OPC_SPECIAL,
169 OPC_AND = 0x24 | OPC_SPECIAL,
170 OPC_OR = 0x25 | OPC_SPECIAL,
171 OPC_XOR = 0x26 | OPC_SPECIAL,
172 OPC_NOR = 0x27 | OPC_SPECIAL,
173 OPC_SLT = 0x2A | OPC_SPECIAL,
174 OPC_SLTU = 0x2B | OPC_SPECIAL,
175 OPC_DADD = 0x2C | OPC_SPECIAL,
176 OPC_DADDU = 0x2D | OPC_SPECIAL,
177 OPC_DSUB = 0x2E | OPC_SPECIAL,
178 OPC_DSUBU = 0x2F | OPC_SPECIAL,
180 OPC_JR = 0x08 | OPC_SPECIAL, /* Also JR.HB */
181 OPC_JALR = 0x09 | OPC_SPECIAL, /* Also JALR.HB */
183 OPC_TGE = 0x30 | OPC_SPECIAL,
184 OPC_TGEU = 0x31 | OPC_SPECIAL,
185 OPC_TLT = 0x32 | OPC_SPECIAL,
186 OPC_TLTU = 0x33 | OPC_SPECIAL,
187 OPC_TEQ = 0x34 | OPC_SPECIAL,
188 OPC_TNE = 0x36 | OPC_SPECIAL,
189 /* HI / LO registers load & stores */
190 OPC_MFHI = 0x10 | OPC_SPECIAL,
191 OPC_MTHI = 0x11 | OPC_SPECIAL,
192 OPC_MFLO = 0x12 | OPC_SPECIAL,
193 OPC_MTLO = 0x13 | OPC_SPECIAL,
194 /* Conditional moves */
195 OPC_MOVZ = 0x0A | OPC_SPECIAL,
196 OPC_MOVN = 0x0B | OPC_SPECIAL,
198 OPC_MOVCI = 0x01 | OPC_SPECIAL,
201 OPC_PMON = 0x05 | OPC_SPECIAL, /* inofficial */
202 OPC_SYSCALL = 0x0C | OPC_SPECIAL,
203 OPC_BREAK = 0x0D | OPC_SPECIAL,
204 OPC_SPIM = 0x0E | OPC_SPECIAL, /* inofficial */
205 OPC_SYNC = 0x0F | OPC_SPECIAL,
207 OPC_SPECIAL15_RESERVED = 0x15 | OPC_SPECIAL,
208 OPC_SPECIAL28_RESERVED = 0x28 | OPC_SPECIAL,
209 OPC_SPECIAL29_RESERVED = 0x29 | OPC_SPECIAL,
210 OPC_SPECIAL35_RESERVED = 0x35 | OPC_SPECIAL,
211 OPC_SPECIAL37_RESERVED = 0x37 | OPC_SPECIAL,
212 OPC_SPECIAL39_RESERVED = 0x39 | OPC_SPECIAL,
213 OPC_SPECIAL3D_RESERVED = 0x3D | OPC_SPECIAL,
216 /* REGIMM (rt field) opcodes */
217 #define MASK_REGIMM(op) MASK_OP_MAJOR(op) | (op & (0x1F << 16))
220 OPC_BLTZ = (0x00 << 16) | OPC_REGIMM,
221 OPC_BLTZL = (0x02 << 16) | OPC_REGIMM,
222 OPC_BGEZ = (0x01 << 16) | OPC_REGIMM,
223 OPC_BGEZL = (0x03 << 16) | OPC_REGIMM,
224 OPC_BLTZAL = (0x10 << 16) | OPC_REGIMM,
225 OPC_BLTZALL = (0x12 << 16) | OPC_REGIMM,
226 OPC_BGEZAL = (0x11 << 16) | OPC_REGIMM,
227 OPC_BGEZALL = (0x13 << 16) | OPC_REGIMM,
228 OPC_TGEI = (0x08 << 16) | OPC_REGIMM,
229 OPC_TGEIU = (0x09 << 16) | OPC_REGIMM,
230 OPC_TLTI = (0x0A << 16) | OPC_REGIMM,
231 OPC_TLTIU = (0x0B << 16) | OPC_REGIMM,
232 OPC_TEQI = (0x0C << 16) | OPC_REGIMM,
233 OPC_TNEI = (0x0E << 16) | OPC_REGIMM,
234 OPC_SYNCI = (0x1F << 16) | OPC_REGIMM,
237 /* Special2 opcodes */
238 #define MASK_SPECIAL2(op) MASK_OP_MAJOR(op) | (op & 0x3F)
241 /* Multiply & xxx operations */
242 OPC_MADD = 0x00 | OPC_SPECIAL2,
243 OPC_MADDU = 0x01 | OPC_SPECIAL2,
244 OPC_MUL = 0x02 | OPC_SPECIAL2,
245 OPC_MSUB = 0x04 | OPC_SPECIAL2,
246 OPC_MSUBU = 0x05 | OPC_SPECIAL2,
248 OPC_CLZ = 0x20 | OPC_SPECIAL2,
249 OPC_CLO = 0x21 | OPC_SPECIAL2,
250 OPC_DCLZ = 0x24 | OPC_SPECIAL2,
251 OPC_DCLO = 0x25 | OPC_SPECIAL2,
253 OPC_SDBBP = 0x3F | OPC_SPECIAL2,
256 /* Special3 opcodes */
257 #define MASK_SPECIAL3(op) MASK_OP_MAJOR(op) | (op & 0x3F)
260 OPC_EXT = 0x00 | OPC_SPECIAL3,
261 OPC_DEXTM = 0x01 | OPC_SPECIAL3,
262 OPC_DEXTU = 0x02 | OPC_SPECIAL3,
263 OPC_DEXT = 0x03 | OPC_SPECIAL3,
264 OPC_INS = 0x04 | OPC_SPECIAL3,
265 OPC_DINSM = 0x05 | OPC_SPECIAL3,
266 OPC_DINSU = 0x06 | OPC_SPECIAL3,
267 OPC_DINS = 0x07 | OPC_SPECIAL3,
268 OPC_BSHFL = 0x20 | OPC_SPECIAL3,
269 OPC_DBSHFL = 0x24 | OPC_SPECIAL3,
270 OPC_RDHWR = 0x3B | OPC_SPECIAL3,
274 #define MASK_BSHFL(op) MASK_SPECIAL3(op) | (op & (0x1F << 6))
277 OPC_WSBH = (0x02 << 6) | OPC_BSHFL,
278 OPC_SEB = (0x10 << 6) | OPC_BSHFL,
279 OPC_SEH = (0x18 << 6) | OPC_BSHFL,
283 #define MASK_DBSHFL(op) MASK_SPECIAL3(op) | (op & (0x1F << 6))
286 OPC_DSBH = (0x02 << 6) | OPC_DBSHFL,
287 OPC_DSHD = (0x05 << 6) | OPC_DBSHFL,
290 /* Coprocessor 0 (rs field) */
291 #define MASK_CP0(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
294 OPC_MFC0 = (0x00 << 21) | OPC_CP0,
295 OPC_DMFC0 = (0x01 << 21) | OPC_CP0,
296 OPC_MTC0 = (0x04 << 21) | OPC_CP0,
297 OPC_DMTC0 = (0x05 << 21) | OPC_CP0,
298 OPC_RDPGPR = (0x0A << 21) | OPC_CP0,
299 OPC_MFMC0 = (0x0B << 21) | OPC_CP0,
300 OPC_WRPGPR = (0x0E << 21) | OPC_CP0,
301 OPC_C0 = (0x10 << 21) | OPC_CP0,
302 OPC_C0_FIRST = (0x10 << 21) | OPC_CP0,
303 OPC_C0_LAST = (0x1F << 21) | OPC_CP0,
307 #define MASK_MFMC0(op) MASK_CP0(op) | (op & ((0x0C << 11) | (1 << 5)))
310 OPC_DI = (0 << 5) | (0x0C << 11) | OPC_MFMC0,
311 OPC_EI = (1 << 5) | (0x0C << 11) | OPC_MFMC0,
314 /* Coprocessor 0 (with rs == C0) */
315 #define MASK_C0(op) MASK_CP0(op) | (op & 0x3F)
318 OPC_TLBR = 0x01 | OPC_C0,
319 OPC_TLBWI = 0x02 | OPC_C0,
320 OPC_TLBWR = 0x06 | OPC_C0,
321 OPC_TLBP = 0x08 | OPC_C0,
322 OPC_RFE = 0x10 | OPC_C0,
323 OPC_ERET = 0x18 | OPC_C0,
324 OPC_DERET = 0x1F | OPC_C0,
325 OPC_WAIT = 0x20 | OPC_C0,
328 /* Coprocessor 1 (rs field) */
329 #define MASK_CP1(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
332 OPC_MFC1 = (0x00 << 21) | OPC_CP1,
333 OPC_DMFC1 = (0x01 << 21) | OPC_CP1,
334 OPC_CFC1 = (0x02 << 21) | OPC_CP1,
335 OPC_MFHCI = (0x03 << 21) | OPC_CP1,
336 OPC_MTC1 = (0x04 << 21) | OPC_CP1,
337 OPC_DMTC1 = (0x05 << 21) | OPC_CP1,
338 OPC_CTC1 = (0x06 << 21) | OPC_CP1,
339 OPC_MTHCI = (0x07 << 21) | OPC_CP1,
340 OPC_BC1 = (0x08 << 21) | OPC_CP1, /* bc */
341 OPC_S_FMT = (0x10 << 21) | OPC_CP1, /* 16: fmt=single fp */
342 OPC_D_FMT = (0x11 << 21) | OPC_CP1, /* 17: fmt=double fp */
343 OPC_E_FMT = (0x12 << 21) | OPC_CP1, /* 18: fmt=extended fp */
344 OPC_Q_FMT = (0x13 << 21) | OPC_CP1, /* 19: fmt=quad fp */
345 OPC_W_FMT = (0x14 << 21) | OPC_CP1, /* 20: fmt=32bit fixed */
346 OPC_L_FMT = (0x15 << 21) | OPC_CP1, /* 21: fmt=64bit fixed */
350 OPC_BC1F = (0x00 << 16) | OPC_BC1,
351 OPC_BC1T = (0x01 << 16) | OPC_BC1,
352 OPC_BC1FL = (0x02 << 16) | OPC_BC1,
353 OPC_BC1TL = (0x03 << 16) | OPC_BC1,
356 #define MASK_CP1_BCOND(op) MASK_CP1(op) | (op & (0x3 << 16))
357 #define MASK_CP1_FUNC(op) MASK_CP1(op) | (op & 0x3F)
359 #define MASK_CP2(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
360 #define MASK_CP3(op) MASK_OP_MAJOR(op) | (op & (0x1F << 21))
362 const unsigned char *regnames[] =
363 { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
364 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
365 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
366 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
368 /* Warning: no function for r0 register (hard wired to zero) */
369 #define GEN32(func, NAME) \
370 static GenOpFunc *NAME ## _table [32] = { \
371 NULL, NAME ## 1, NAME ## 2, NAME ## 3, \
372 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
373 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
374 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
375 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
376 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
377 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
378 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
380 static inline void func(int n) \
382 NAME ## _table[n](); \
385 /* General purpose registers moves */
386 GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
387 GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
388 GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
390 GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
391 GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
393 static const char *fregnames[] =
394 { "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
395 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
396 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
397 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31", };
399 # define SFGEN32(func, NAME) \
400 static GenOpFunc *NAME ## _table [32] = { \
401 NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3, \
402 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
403 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
404 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
405 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
406 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
407 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
408 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
410 static inline void func(int n) \
412 NAME ## _table[n](); \
415 # define DFGEN32(func, NAME) \
416 static GenOpFunc *NAME ## _table [32] = { \
417 NAME ## 0, 0, NAME ## 2, 0, \
418 NAME ## 4, 0, NAME ## 6, 0, \
419 NAME ## 8, 0, NAME ## 10, 0, \
420 NAME ## 12, 0, NAME ## 14, 0, \
421 NAME ## 16, 0, NAME ## 18, 0, \
422 NAME ## 20, 0, NAME ## 22, 0, \
423 NAME ## 24, 0, NAME ## 26, 0, \
424 NAME ## 28, 0, NAME ## 30, 0, \
426 static inline void func(int n) \
428 NAME ## _table[n](); \
431 SFGEN32(gen_op_load_fpr_WT0, gen_op_load_fpr_WT0_fpr);
432 SFGEN32(gen_op_store_fpr_WT0, gen_op_store_fpr_WT0_fpr);
434 SFGEN32(gen_op_load_fpr_WT1, gen_op_load_fpr_WT1_fpr);
435 SFGEN32(gen_op_store_fpr_WT1, gen_op_store_fpr_WT1_fpr);
437 SFGEN32(gen_op_load_fpr_WT2, gen_op_load_fpr_WT2_fpr);
438 SFGEN32(gen_op_store_fpr_WT2, gen_op_store_fpr_WT2_fpr);
440 DFGEN32(gen_op_load_fpr_DT0, gen_op_load_fpr_DT0_fpr);
441 DFGEN32(gen_op_store_fpr_DT0, gen_op_store_fpr_DT0_fpr);
443 DFGEN32(gen_op_load_fpr_DT1, gen_op_load_fpr_DT1_fpr);
444 DFGEN32(gen_op_store_fpr_DT1, gen_op_store_fpr_DT1_fpr);
446 DFGEN32(gen_op_load_fpr_DT2, gen_op_load_fpr_DT2_fpr);
447 DFGEN32(gen_op_store_fpr_DT2, gen_op_store_fpr_DT2_fpr);
449 #define FOP_CONDS(fmt) \
450 static GenOpFunc * cond_ ## fmt ## _table[16] = { \
451 gen_op_cmp_ ## fmt ## _f, \
452 gen_op_cmp_ ## fmt ## _un, \
453 gen_op_cmp_ ## fmt ## _eq, \
454 gen_op_cmp_ ## fmt ## _ueq, \
455 gen_op_cmp_ ## fmt ## _olt, \
456 gen_op_cmp_ ## fmt ## _ult, \
457 gen_op_cmp_ ## fmt ## _ole, \
458 gen_op_cmp_ ## fmt ## _ule, \
459 gen_op_cmp_ ## fmt ## _sf, \
460 gen_op_cmp_ ## fmt ## _ngle, \
461 gen_op_cmp_ ## fmt ## _seq, \
462 gen_op_cmp_ ## fmt ## _ngl, \
463 gen_op_cmp_ ## fmt ## _lt, \
464 gen_op_cmp_ ## fmt ## _nge, \
465 gen_op_cmp_ ## fmt ## _le, \
466 gen_op_cmp_ ## fmt ## _ngt, \
468 static inline void gen_cmp_ ## fmt(int n) \
470 cond_ ## fmt ## _table[n](); \
476 typedef struct DisasContext {
477 struct TranslationBlock *tb;
478 target_ulong pc, saved_pc;
480 /* Routine used to access memory */
482 uint32_t hflags, saved_hflags;
485 target_ulong btarget;
489 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
490 * exception condition
492 BS_STOP = 1, /* We want to stop translation for any reason */
493 BS_BRANCH = 2, /* We reached a branch condition */
494 BS_EXCP = 3, /* We reached an exception condition */
497 #if defined MIPS_DEBUG_DISAS
498 #define MIPS_DEBUG(fmt, args...) \
500 if (loglevel & CPU_LOG_TB_IN_ASM) { \
501 fprintf(logfile, "%08x: %08x " fmt "\n", \
502 ctx->pc, ctx->opcode , ##args); \
506 #define MIPS_DEBUG(fmt, args...) do { } while(0)
509 #define MIPS_INVAL(op) \
511 MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26, \
512 ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F)); \
515 #define GEN_LOAD_REG_TN(Tn, Rn) \
518 glue(gen_op_reset_, Tn)(); \
520 glue(gen_op_load_gpr_, Tn)(Rn); \
524 #define GEN_LOAD_IMM_TN(Tn, Imm) \
527 glue(gen_op_reset_, Tn)(); \
529 glue(gen_op_set_, Tn)(Imm); \
533 #define GEN_STORE_TN_REG(Rn, Tn) \
536 glue(glue(gen_op_store_, Tn),_gpr)(Rn); \
540 #define GEN_LOAD_FREG_FTN(FTn, Fn) \
542 glue(gen_op_load_fpr_, FTn)(Fn); \
545 #define GEN_STORE_FTN_FREG(Fn, FTn) \
547 glue(gen_op_store_fpr_, FTn)(Fn); \
550 static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
552 #if defined MIPS_DEBUG_DISAS
553 if (loglevel & CPU_LOG_TB_IN_ASM) {
554 fprintf(logfile, "hflags %08x saved %08x\n",
555 ctx->hflags, ctx->saved_hflags);
558 if (do_save_pc && ctx->pc != ctx->saved_pc) {
559 gen_op_save_pc(ctx->pc);
560 ctx->saved_pc = ctx->pc;
562 if (ctx->hflags != ctx->saved_hflags) {
563 gen_op_save_state(ctx->hflags);
564 ctx->saved_hflags = ctx->hflags;
565 if (ctx->hflags & MIPS_HFLAG_BR) {
566 gen_op_save_breg_target();
567 } else if (ctx->hflags & MIPS_HFLAG_B) {
568 gen_op_save_btarget(ctx->btarget);
569 } else if (ctx->hflags & MIPS_HFLAG_BMASK) {
571 gen_op_save_btarget(ctx->btarget);
576 static inline void generate_exception_err (DisasContext *ctx, int excp, int err)
578 #if defined MIPS_DEBUG_DISAS
579 if (loglevel & CPU_LOG_TB_IN_ASM)
580 fprintf(logfile, "%s: raise exception %d\n", __func__, excp);
582 save_cpu_state(ctx, 1);
584 gen_op_raise_exception(excp);
586 gen_op_raise_exception_err(excp, err);
587 ctx->bstate = BS_EXCP;
590 static inline void generate_exception (DisasContext *ctx, int excp)
592 generate_exception_err (ctx, excp, 0);
595 #if defined(CONFIG_USER_ONLY)
596 #define op_ldst(name) gen_op_##name##_raw()
597 #define OP_LD_TABLE(width)
598 #define OP_ST_TABLE(width)
600 #define op_ldst(name) (*gen_op_##name[ctx->mem_idx])()
601 #define OP_LD_TABLE(width) \
602 static GenOpFunc *gen_op_l##width[] = { \
603 &gen_op_l##width##_user, \
604 &gen_op_l##width##_kernel, \
606 #define OP_ST_TABLE(width) \
607 static GenOpFunc *gen_op_s##width[] = { \
608 &gen_op_s##width##_user, \
609 &gen_op_s##width##_kernel, \
613 #ifdef MIPS_HAS_MIPS64
642 static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,
643 int base, int16_t offset)
645 const char *opn = "unk";
648 GEN_LOAD_IMM_TN(T0, offset);
649 } else if (offset == 0) {
650 gen_op_load_gpr_T0(base);
652 gen_op_load_gpr_T0(base);
653 gen_op_set_T1(offset);
656 /* Don't do NOP if destination is zero: we must perform the actual
660 #ifdef MIPS_HAS_MIPS64
663 GEN_STORE_TN_REG(rt, T0);
668 GEN_STORE_TN_REG(rt, T0);
672 GEN_LOAD_REG_TN(T1, rt);
677 GEN_LOAD_REG_TN(T1, rt);
683 GEN_STORE_TN_REG(rt, T0);
687 GEN_LOAD_REG_TN(T1, rt);
693 GEN_STORE_TN_REG(rt, T0);
697 GEN_LOAD_REG_TN(T1, rt);
704 GEN_STORE_TN_REG(rt, T0);
709 GEN_STORE_TN_REG(rt, T0);
713 GEN_LOAD_REG_TN(T1, rt);
719 GEN_STORE_TN_REG(rt, T0);
723 GEN_LOAD_REG_TN(T1, rt);
729 GEN_STORE_TN_REG(rt, T0);
734 GEN_STORE_TN_REG(rt, T0);
738 GEN_LOAD_REG_TN(T1, rt);
744 GEN_STORE_TN_REG(rt, T0);
748 GEN_LOAD_REG_TN(T1, rt);
750 GEN_STORE_TN_REG(rt, T0);
754 GEN_LOAD_REG_TN(T1, rt);
759 GEN_LOAD_REG_TN(T1, rt);
761 GEN_STORE_TN_REG(rt, T0);
765 GEN_LOAD_REG_TN(T1, rt);
771 GEN_STORE_TN_REG(rt, T0);
775 GEN_LOAD_REG_TN(T1, rt);
777 GEN_STORE_TN_REG(rt, T0);
781 MIPS_INVAL("load/store");
782 generate_exception(ctx, EXCP_RI);
785 MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
789 static void gen_flt_ldst (DisasContext *ctx, uint32_t opc, int ft,
790 int base, int16_t offset)
792 const char *opn = "unk";
795 GEN_LOAD_IMM_TN(T0, offset);
796 } else if (offset == 0) {
797 gen_op_load_gpr_T0(base);
799 gen_op_load_gpr_T0(base);
800 gen_op_set_T1(offset);
803 /* Don't do NOP if destination is zero: we must perform the actual
809 GEN_STORE_FTN_FREG(ft, WT0);
813 GEN_LOAD_FREG_FTN(WT0, ft);
819 GEN_STORE_FTN_FREG(ft, DT0);
823 GEN_LOAD_FREG_FTN(DT0, ft);
828 MIPS_INVAL("float load/store");
829 generate_exception_err(ctx, EXCP_CpU, 1);
832 MIPS_DEBUG("%s %s, %d(%s)", opn, fregnames[ft], offset, regnames[base]);
835 /* Arithmetic with immediate operand */
836 static void gen_arith_imm (DisasContext *ctx, uint32_t opc, int rt,
840 const char *opn = "unk";
842 if (rt == 0 && opc != OPC_ADDI && opc != OPC_DADDI) {
843 /* if no destination, treat it as a NOP
844 * For addi, we must generate the overflow exception when needed.
849 if (opc == OPC_ADDI || opc == OPC_ADDIU ||
850 opc == OPC_DADDI || opc == OPC_DADDIU ||
851 opc == OPC_SLTI || opc == OPC_SLTIU)
852 uimm = (int32_t)imm; /* Sign extend to 32 bits */
854 uimm = (uint16_t)imm;
855 if (opc != OPC_LUI) {
856 GEN_LOAD_REG_TN(T0, rs);
857 GEN_LOAD_IMM_TN(T1, uimm);
860 GEN_LOAD_IMM_TN(T0, uimm);
864 save_cpu_state(ctx, 1);
872 #ifdef MIPS_HAS_MIPS64
874 save_cpu_state(ctx, 1);
915 if ((ctx->opcode >> 21) & 1) {
923 #ifdef MIPS_HAS_MIPS64
933 if ((ctx->opcode >> 21) & 1) {
950 if ((ctx->opcode >> 21) & 1) {
960 MIPS_INVAL("imm arith");
961 generate_exception(ctx, EXCP_RI);
964 GEN_STORE_TN_REG(rt, T0);
965 MIPS_DEBUG("%s %s, %s, %x", opn, regnames[rt], regnames[rs], uimm);
969 static void gen_arith (DisasContext *ctx, uint32_t opc,
970 int rd, int rs, int rt)
972 const char *opn = "unk";
974 if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB
975 && opc != OPC_DADD && opc != OPC_DSUB) {
976 /* if no destination, treat it as a NOP
977 * For add & sub, we must generate the overflow exception when needed.
982 GEN_LOAD_REG_TN(T0, rs);
983 GEN_LOAD_REG_TN(T1, rt);
986 save_cpu_state(ctx, 1);
995 save_cpu_state(ctx, 1);
1003 #ifdef MIPS_HAS_MIPS64
1005 save_cpu_state(ctx, 1);
1014 save_cpu_state(ctx, 1);
1068 if ((ctx->opcode >> 6) & 1) {
1076 #ifdef MIPS_HAS_MIPS64
1086 if ((ctx->opcode >> 6) & 1) {
1096 MIPS_INVAL("arith");
1097 generate_exception(ctx, EXCP_RI);
1100 GEN_STORE_TN_REG(rd, T0);
1102 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
1105 /* Arithmetic on HI/LO registers */
1106 static void gen_HILO (DisasContext *ctx, uint32_t opc, int reg)
1108 const char *opn = "unk";
1110 if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
1111 /* Treat as a NOP */
1118 GEN_STORE_TN_REG(reg, T0);
1123 GEN_STORE_TN_REG(reg, T0);
1127 GEN_LOAD_REG_TN(T0, reg);
1132 GEN_LOAD_REG_TN(T0, reg);
1138 generate_exception(ctx, EXCP_RI);
1141 MIPS_DEBUG("%s %s", opn, regnames[reg]);
1144 static void gen_muldiv (DisasContext *ctx, uint32_t opc,
1147 const char *opn = "unk";
1149 GEN_LOAD_REG_TN(T0, rs);
1150 GEN_LOAD_REG_TN(T1, rt);
1168 #ifdef MIPS_HAS_MIPS64
1203 MIPS_INVAL("mul/div");
1204 generate_exception(ctx, EXCP_RI);
1207 MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
1210 static void gen_cl (DisasContext *ctx, uint32_t opc,
1213 const char *opn = "unk";
1215 /* Treat as a NOP */
1219 GEN_LOAD_REG_TN(T0, rs);
1229 #ifdef MIPS_HAS_MIPS64
1241 generate_exception(ctx, EXCP_RI);
1244 gen_op_store_T0_gpr(rd);
1245 MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
1249 static void gen_trap (DisasContext *ctx, uint32_t opc,
1250 int rs, int rt, int16_t imm)
1255 /* Load needed operands */
1263 /* Compare two registers */
1265 GEN_LOAD_REG_TN(T0, rs);
1266 GEN_LOAD_REG_TN(T1, rt);
1275 /* Compare register to immediate */
1276 if (rs != 0 || imm != 0) {
1277 GEN_LOAD_REG_TN(T0, rs);
1278 GEN_LOAD_IMM_TN(T1, (int32_t)imm);
1285 case OPC_TEQ: /* rs == rs */
1286 case OPC_TEQI: /* r0 == 0 */
1287 case OPC_TGE: /* rs >= rs */
1288 case OPC_TGEI: /* r0 >= 0 */
1289 case OPC_TGEU: /* rs >= rs unsigned */
1290 case OPC_TGEIU: /* r0 >= 0 unsigned */
1294 case OPC_TLT: /* rs < rs */
1295 case OPC_TLTI: /* r0 < 0 */
1296 case OPC_TLTU: /* rs < rs unsigned */
1297 case OPC_TLTIU: /* r0 < 0 unsigned */
1298 case OPC_TNE: /* rs != rs */
1299 case OPC_TNEI: /* r0 != 0 */
1300 /* Never trap: treat as NOP */
1304 generate_exception(ctx, EXCP_RI);
1335 generate_exception(ctx, EXCP_RI);
1339 save_cpu_state(ctx, 1);
1341 ctx->bstate = BS_STOP;
1344 static inline void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
1346 TranslationBlock *tb;
1348 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
1350 gen_op_goto_tb0(TBPARAM(tb));
1352 gen_op_goto_tb1(TBPARAM(tb));
1353 gen_op_save_pc(dest);
1354 gen_op_set_T0((long)tb + n);
1357 gen_op_save_pc(dest);
1363 /* Branches (before delay slot) */
1364 static void gen_compute_branch (DisasContext *ctx, uint32_t opc,
1365 int rs, int rt, int32_t offset)
1367 target_ulong btarget;
1373 /* Load needed operands */
1379 /* Compare two registers */
1381 GEN_LOAD_REG_TN(T0, rs);
1382 GEN_LOAD_REG_TN(T1, rt);
1385 btarget = ctx->pc + 4 + offset;
1399 /* Compare to zero */
1401 gen_op_load_gpr_T0(rs);
1404 btarget = ctx->pc + 4 + offset;
1408 /* Jump to immediate */
1409 btarget = ((ctx->pc + 4) & 0xF0000000) | offset;
1413 /* Jump to register */
1414 if (offset != 0 && offset != 16) {
1415 /* Hint = 0 is JR/JALR, hint 16 is JR.HB/JALR.HB, the
1416 others are reserved. */
1417 generate_exception(ctx, EXCP_RI);
1420 GEN_LOAD_REG_TN(T2, rs);
1423 MIPS_INVAL("branch/jump");
1424 generate_exception(ctx, EXCP_RI);
1428 /* No condition to be computed */
1430 case OPC_BEQ: /* rx == rx */
1431 case OPC_BEQL: /* rx == rx likely */
1432 case OPC_BGEZ: /* 0 >= 0 */
1433 case OPC_BGEZL: /* 0 >= 0 likely */
1434 case OPC_BLEZ: /* 0 <= 0 */
1435 case OPC_BLEZL: /* 0 <= 0 likely */
1437 ctx->hflags |= MIPS_HFLAG_B;
1438 MIPS_DEBUG("balways");
1440 case OPC_BGEZAL: /* 0 >= 0 */
1441 case OPC_BGEZALL: /* 0 >= 0 likely */
1442 /* Always take and link */
1444 ctx->hflags |= MIPS_HFLAG_B;
1445 MIPS_DEBUG("balways and link");
1447 case OPC_BNE: /* rx != rx */
1448 case OPC_BGTZ: /* 0 > 0 */
1449 case OPC_BLTZ: /* 0 < 0 */
1450 /* Treated as NOP */
1451 MIPS_DEBUG("bnever (NOP)");
1453 case OPC_BLTZAL: /* 0 < 0 */
1454 gen_op_set_T0(ctx->pc + 8);
1455 gen_op_store_T0_gpr(31);
1457 case OPC_BLTZALL: /* 0 < 0 likely */
1458 gen_op_set_T0(ctx->pc + 8);
1459 gen_op_store_T0_gpr(31);
1460 gen_goto_tb(ctx, 0, ctx->pc + 4);
1462 case OPC_BNEL: /* rx != rx likely */
1463 case OPC_BGTZL: /* 0 > 0 likely */
1464 case OPC_BLTZL: /* 0 < 0 likely */
1465 /* Skip the instruction in the delay slot */
1466 MIPS_DEBUG("bnever and skip");
1467 gen_goto_tb(ctx, 0, ctx->pc + 4);
1470 ctx->hflags |= MIPS_HFLAG_B;
1471 MIPS_DEBUG("j %08x", btarget);
1475 ctx->hflags |= MIPS_HFLAG_B;
1476 MIPS_DEBUG("jal %08x", btarget);
1479 ctx->hflags |= MIPS_HFLAG_BR;
1480 MIPS_DEBUG("jr %s", regnames[rs]);
1484 ctx->hflags |= MIPS_HFLAG_BR;
1485 MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
1488 MIPS_INVAL("branch/jump");
1489 generate_exception(ctx, EXCP_RI);
1496 MIPS_DEBUG("beq %s, %s, %08x",
1497 regnames[rs], regnames[rt], btarget);
1501 MIPS_DEBUG("beql %s, %s, %08x",
1502 regnames[rs], regnames[rt], btarget);
1506 MIPS_DEBUG("bne %s, %s, %08x",
1507 regnames[rs], regnames[rt], btarget);
1511 MIPS_DEBUG("bnel %s, %s, %08x",
1512 regnames[rs], regnames[rt], btarget);
1516 MIPS_DEBUG("bgez %s, %08x", regnames[rs], btarget);
1520 MIPS_DEBUG("bgezl %s, %08x", regnames[rs], btarget);
1524 MIPS_DEBUG("bgezal %s, %08x", regnames[rs], btarget);
1530 MIPS_DEBUG("bgezall %s, %08x", regnames[rs], btarget);
1534 MIPS_DEBUG("bgtz %s, %08x", regnames[rs], btarget);
1538 MIPS_DEBUG("bgtzl %s, %08x", regnames[rs], btarget);
1542 MIPS_DEBUG("blez %s, %08x", regnames[rs], btarget);
1546 MIPS_DEBUG("blezl %s, %08x", regnames[rs], btarget);
1550 MIPS_DEBUG("bltz %s, %08x", regnames[rs], btarget);
1554 MIPS_DEBUG("bltzl %s, %08x", regnames[rs], btarget);
1559 MIPS_DEBUG("bltzal %s, %08x", regnames[rs], btarget);
1561 ctx->hflags |= MIPS_HFLAG_BC;
1566 MIPS_DEBUG("bltzall %s, %08x", regnames[rs], btarget);
1568 ctx->hflags |= MIPS_HFLAG_BL;
1573 MIPS_DEBUG("enter ds: link %d cond %02x target %08x",
1574 blink, ctx->hflags, btarget);
1575 ctx->btarget = btarget;
1577 gen_op_set_T0(ctx->pc + 8);
1578 gen_op_store_T0_gpr(blink);
1583 /* special3 bitfield operations */
1584 static void gen_bitops (DisasContext *ctx, uint32_t opc, int rt,
1585 int rs, int lsb, int msb)
1587 GEN_LOAD_REG_TN(T1, rs);
1592 gen_op_ext(lsb, msb + 1);
1597 gen_op_ext(lsb, msb + 1 + 32);
1602 gen_op_ext(lsb + 32, msb + 1);
1605 gen_op_ext(lsb, msb + 1);
1610 GEN_LOAD_REG_TN(T2, rt);
1611 gen_op_ins(lsb, msb - lsb + 1);
1616 GEN_LOAD_REG_TN(T2, rt);
1617 gen_op_ins(lsb, msb - lsb + 1 + 32);
1622 GEN_LOAD_REG_TN(T2, rt);
1623 gen_op_ins(lsb + 32, msb - lsb + 1);
1628 GEN_LOAD_REG_TN(T2, rt);
1629 gen_op_ins(lsb, msb - lsb + 1);
1633 MIPS_INVAL("bitops");
1634 generate_exception(ctx, EXCP_RI);
1637 GEN_STORE_TN_REG(rt, T0);
1640 /* CP0 (MMU and control) */
1641 static void gen_mfc0 (DisasContext *ctx, int reg, int sel)
1643 const char *rn = "invalid";
1649 gen_op_mfc0_index();
1653 // gen_op_mfc0_mvpcontrol(); /* MT ASE */
1657 // gen_op_mfc0_mvpconf0(); /* MT ASE */
1661 // gen_op_mfc0_mvpconf1(); /* MT ASE */
1671 gen_op_mfc0_random();
1675 // gen_op_mfc0_vpecontrol(); /* MT ASE */
1679 // gen_op_mfc0_vpeconf0(); /* MT ASE */
1683 // gen_op_mfc0_vpeconf1(); /* MT ASE */
1687 // gen_op_mfc0_YQMask(); /* MT ASE */
1691 // gen_op_mfc0_vpeschedule(); /* MT ASE */
1695 // gen_op_mfc0_vpeschefback(); /* MT ASE */
1696 rn = "VPEScheFBack";
1699 // gen_op_mfc0_vpeopt(); /* MT ASE */
1709 gen_op_mfc0_entrylo0();
1713 // gen_op_mfc0_tcstatus(); /* MT ASE */
1717 // gen_op_mfc0_tcbind(); /* MT ASE */
1721 // gen_op_mfc0_tcrestart(); /* MT ASE */
1725 // gen_op_mfc0_tchalt(); /* MT ASE */
1729 // gen_op_mfc0_tccontext(); /* MT ASE */
1733 // gen_op_mfc0_tcschedule(); /* MT ASE */
1737 // gen_op_mfc0_tcschefback(); /* MT ASE */
1747 gen_op_mfc0_entrylo1();
1757 gen_op_mfc0_context();
1761 // gen_op_mfc0_contextconfig(); /* SmartMIPS ASE */
1762 rn = "ContextConfig";
1771 gen_op_mfc0_pagemask();
1775 gen_op_mfc0_pagegrain();
1785 gen_op_mfc0_wired();
1789 // gen_op_mfc0_srsconf0(); /* shadow registers */
1793 // gen_op_mfc0_srsconf1(); /* shadow registers */
1797 // gen_op_mfc0_srsconf2(); /* shadow registers */
1801 // gen_op_mfc0_srsconf3(); /* shadow registers */
1805 // gen_op_mfc0_srsconf4(); /* shadow registers */
1815 gen_op_mfc0_hwrena();
1825 gen_op_mfc0_badvaddr();
1835 gen_op_mfc0_count();
1838 /* 6,7 are implementation dependent */
1846 gen_op_mfc0_entryhi();
1856 gen_op_mfc0_compare();
1859 /* 6,7 are implementation dependent */
1867 gen_op_mfc0_status();
1871 gen_op_mfc0_intctl();
1875 gen_op_mfc0_srsctl();
1879 // gen_op_mfc0_srsmap(); /* shadow registers */
1889 gen_op_mfc0_cause();
1913 gen_op_mfc0_ebase();
1923 gen_op_mfc0_config0();
1927 gen_op_mfc0_config1();
1931 gen_op_mfc0_config2();
1935 gen_op_mfc0_config3();
1938 /* 6,7 are implementation dependent */
1946 gen_op_mfc0_lladdr();
1956 gen_op_mfc0_watchlo0();
1960 // gen_op_mfc0_watchlo1();
1964 // gen_op_mfc0_watchlo2();
1968 // gen_op_mfc0_watchlo3();
1972 // gen_op_mfc0_watchlo4();
1976 // gen_op_mfc0_watchlo5();
1980 // gen_op_mfc0_watchlo6();
1984 // gen_op_mfc0_watchlo7();
1994 gen_op_mfc0_watchhi0();
1998 // gen_op_mfc0_watchhi1();
2002 // gen_op_mfc0_watchhi2();
2006 // gen_op_mfc0_watchhi3();
2010 // gen_op_mfc0_watchhi4();
2014 // gen_op_mfc0_watchhi5();
2018 // gen_op_mfc0_watchhi6();
2022 // gen_op_mfc0_watchhi7();
2032 /* 64 bit MMU only */
2033 gen_op_mfc0_xcontext();
2041 /* Officially reserved, but sel 0 is used for R1x000 framemask */
2044 gen_op_mfc0_framemask();
2053 rn = "'Diagnostic"; /* implementation dependent */
2058 gen_op_mfc0_debug(); /* EJTAG support */
2062 // gen_op_mfc0_tracecontrol(); /* PDtrace support */
2063 rn = "TraceControl";
2066 // gen_op_mfc0_tracecontrol2(); /* PDtrace support */
2067 rn = "TraceControl2";
2070 // gen_op_mfc0_usertracedata(); /* PDtrace support */
2071 rn = "UserTraceData";
2074 // gen_op_mfc0_debug(); /* PDtrace support */
2084 gen_op_mfc0_depc(); /* EJTAG support */
2094 gen_op_mfc0_performance0();
2095 rn = "Performance0";
2098 // gen_op_mfc0_performance1();
2099 rn = "Performance1";
2102 // gen_op_mfc0_performance2();
2103 rn = "Performance2";
2106 // gen_op_mfc0_performance3();
2107 rn = "Performance3";
2110 // gen_op_mfc0_performance4();
2111 rn = "Performance4";
2114 // gen_op_mfc0_performance5();
2115 rn = "Performance5";
2118 // gen_op_mfc0_performance6();
2119 rn = "Performance6";
2122 // gen_op_mfc0_performance7();
2123 rn = "Performance7";
2148 gen_op_mfc0_taglo();
2155 gen_op_mfc0_datalo();
2168 gen_op_mfc0_taghi();
2175 gen_op_mfc0_datahi();
2185 gen_op_mfc0_errorepc();
2195 gen_op_mfc0_desave(); /* EJTAG support */
2205 #if defined MIPS_DEBUG_DISAS
2206 if (loglevel & CPU_LOG_TB_IN_ASM) {
2207 fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
2214 #if defined MIPS_DEBUG_DISAS
2215 if (loglevel & CPU_LOG_TB_IN_ASM) {
2216 fprintf(logfile, "mfc0 %s (reg %d sel %d)\n",
2220 generate_exception(ctx, EXCP_RI);
2223 static void gen_mtc0 (DisasContext *ctx, int reg, int sel)
2225 const char *rn = "invalid";
2231 gen_op_mtc0_index();
2235 // gen_op_mtc0_mvpcontrol(); /* MT ASE */
2239 // gen_op_mtc0_mvpconf0(); /* MT ASE */
2243 // gen_op_mtc0_mvpconf1(); /* MT ASE */
2257 // gen_op_mtc0_vpecontrol(); /* MT ASE */
2261 // gen_op_mtc0_vpeconf0(); /* MT ASE */
2265 // gen_op_mtc0_vpeconf1(); /* MT ASE */
2269 // gen_op_mtc0_YQMask(); /* MT ASE */
2273 // gen_op_mtc0_vpeschedule(); /* MT ASE */
2277 // gen_op_mtc0_vpeschefback(); /* MT ASE */
2278 rn = "VPEScheFBack";
2281 // gen_op_mtc0_vpeopt(); /* MT ASE */
2291 gen_op_mtc0_entrylo0();
2295 // gen_op_mtc0_tcstatus(); /* MT ASE */
2299 // gen_op_mtc0_tcbind(); /* MT ASE */
2303 // gen_op_mtc0_tcrestart(); /* MT ASE */
2307 // gen_op_mtc0_tchalt(); /* MT ASE */
2311 // gen_op_mtc0_tccontext(); /* MT ASE */
2315 // gen_op_mtc0_tcschedule(); /* MT ASE */
2319 // gen_op_mtc0_tcschefback(); /* MT ASE */
2329 gen_op_mtc0_entrylo1();
2339 gen_op_mtc0_context();
2343 // gen_op_mtc0_contextconfig(); /* SmartMIPS ASE */
2344 rn = "ContextConfig";
2353 gen_op_mtc0_pagemask();
2357 gen_op_mtc0_pagegrain();
2367 gen_op_mtc0_wired();
2371 // gen_op_mtc0_srsconf0(); /* shadow registers */
2375 // gen_op_mtc0_srsconf1(); /* shadow registers */
2379 // gen_op_mtc0_srsconf2(); /* shadow registers */
2383 // gen_op_mtc0_srsconf3(); /* shadow registers */
2387 // gen_op_mtc0_srsconf4(); /* shadow registers */
2397 gen_op_mtc0_hwrena();
2411 gen_op_mtc0_count();
2414 /* 6,7 are implementation dependent */
2418 /* Stop translation as we may have switched the execution mode */
2419 ctx->bstate = BS_STOP;
2424 gen_op_mtc0_entryhi();
2434 gen_op_mtc0_compare();
2437 /* 6,7 are implementation dependent */
2441 /* Stop translation as we may have switched the execution mode */
2442 ctx->bstate = BS_STOP;
2447 gen_op_mtc0_status();
2451 gen_op_mtc0_intctl();
2455 gen_op_mtc0_srsctl();
2459 // gen_op_mtc0_srsmap(); /* shadow registers */
2465 /* Stop translation as we may have switched the execution mode */
2466 ctx->bstate = BS_STOP;
2471 gen_op_mtc0_cause();
2477 /* Stop translation as we may have switched the execution mode */
2478 ctx->bstate = BS_STOP;
2497 gen_op_mtc0_ebase();
2507 gen_op_mtc0_config0();
2515 gen_op_mtc0_config2();
2522 /* 6,7 are implementation dependent */
2524 rn = "Invalid config selector";
2527 /* Stop translation as we may have switched the execution mode */
2528 ctx->bstate = BS_STOP;
2543 gen_op_mtc0_watchlo0();
2547 // gen_op_mtc0_watchlo1();
2551 // gen_op_mtc0_watchlo2();
2555 // gen_op_mtc0_watchlo3();
2559 // gen_op_mtc0_watchlo4();
2563 // gen_op_mtc0_watchlo5();
2567 // gen_op_mtc0_watchlo6();
2571 // gen_op_mtc0_watchlo7();
2581 gen_op_mtc0_watchhi0();
2585 // gen_op_mtc0_watchhi1();
2589 // gen_op_mtc0_watchhi2();
2593 // gen_op_mtc0_watchhi3();
2597 // gen_op_mtc0_watchhi4();
2601 // gen_op_mtc0_watchhi5();
2605 // gen_op_mtc0_watchhi6();
2609 // gen_op_mtc0_watchhi7();
2619 /* 64 bit MMU only */
2620 gen_op_mtc0_xcontext();
2628 /* Officially reserved, but sel 0 is used for R1x000 framemask */
2631 gen_op_mtc0_framemask();
2640 rn = "Diagnostic"; /* implementation dependent */
2645 gen_op_mtc0_debug(); /* EJTAG support */
2649 // gen_op_mtc0_tracecontrol(); /* PDtrace support */
2650 rn = "TraceControl";
2653 // gen_op_mtc0_tracecontrol2(); /* PDtrace support */
2654 rn = "TraceControl2";
2657 // gen_op_mtc0_usertracedata(); /* PDtrace support */
2658 rn = "UserTraceData";
2661 // gen_op_mtc0_debug(); /* PDtrace support */
2667 /* Stop translation as we may have switched the execution mode */
2668 ctx->bstate = BS_STOP;
2673 gen_op_mtc0_depc(); /* EJTAG support */
2683 gen_op_mtc0_performance0();
2684 rn = "Performance0";
2687 // gen_op_mtc0_performance1();
2688 rn = "Performance1";
2691 // gen_op_mtc0_performance2();
2692 rn = "Performance2";
2695 // gen_op_mtc0_performance3();
2696 rn = "Performance3";
2699 // gen_op_mtc0_performance4();
2700 rn = "Performance4";
2703 // gen_op_mtc0_performance5();
2704 rn = "Performance5";
2707 // gen_op_mtc0_performance6();
2708 rn = "Performance6";
2711 // gen_op_mtc0_performance7();
2712 rn = "Performance7";
2738 gen_op_mtc0_taglo();
2745 gen_op_mtc0_datalo();
2758 gen_op_mtc0_taghi();
2765 gen_op_mtc0_datahi();
2776 gen_op_mtc0_errorepc();
2786 gen_op_mtc0_desave(); /* EJTAG support */
2792 /* Stop translation as we may have switched the execution mode */
2793 ctx->bstate = BS_STOP;
2798 #if defined MIPS_DEBUG_DISAS
2799 if (loglevel & CPU_LOG_TB_IN_ASM) {
2800 fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
2807 #if defined MIPS_DEBUG_DISAS
2808 if (loglevel & CPU_LOG_TB_IN_ASM) {
2809 fprintf(logfile, "mtc0 %s (reg %d sel %d)\n",
2813 generate_exception(ctx, EXCP_RI);
2816 static void gen_cp0 (DisasContext *ctx, uint32_t opc, int rt, int rd)
2818 const char *opn = "unk";
2820 if ((!ctx->CP0_Status & (1 << CP0St_CU0) &&
2821 (ctx->hflags & MIPS_HFLAG_UM)) &&
2822 !(ctx->hflags & MIPS_HFLAG_ERL) &&
2823 !(ctx->hflags & MIPS_HFLAG_EXL)) {
2824 if (loglevel & CPU_LOG_TB_IN_ASM) {
2825 fprintf(logfile, "CP0 is not usable\n");
2827 generate_exception (ctx, EXCP_CpU);
2837 gen_mfc0(ctx, rd, ctx->opcode & 0x7);
2838 gen_op_store_T0_gpr(rt);
2842 /* If we get an exception, we want to restart at next instruction */
2843 /* XXX: breaks for mtc in delay slot */
2845 save_cpu_state(ctx, 1);
2847 GEN_LOAD_REG_TN(T0, rt);
2848 gen_mtc0(ctx, rd, ctx->opcode & 0x7);
2851 #if defined(MIPS_USES_R4K_TLB)
2871 save_cpu_state(ctx, 0);
2873 ctx->bstate = BS_EXCP;
2877 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
2878 generate_exception(ctx, EXCP_RI);
2880 save_cpu_state(ctx, 0);
2882 ctx->bstate = BS_EXCP;
2887 /* If we get an exception, we want to restart at next instruction */
2889 save_cpu_state(ctx, 1);
2892 ctx->bstate = BS_EXCP;
2895 if (loglevel & CPU_LOG_TB_IN_ASM) {
2896 fprintf(logfile, "Invalid CP0 opcode: %08x %03x %03x %03x\n",
2897 ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
2898 ((ctx->opcode >> 16) & 0x1F));
2900 generate_exception(ctx, EXCP_RI);
2903 MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
2906 /* CP1 Branches (before delay slot) */
2907 static void gen_compute_branch1 (DisasContext *ctx, uint32_t op,
2910 target_ulong btarget;
2912 btarget = ctx->pc + 4 + offset;
2917 MIPS_DEBUG("bc1f %08x", btarget);
2921 MIPS_DEBUG("bc1fl %08x", btarget);
2925 MIPS_DEBUG("bc1t %08x", btarget);
2927 ctx->hflags |= MIPS_HFLAG_BC;
2931 MIPS_DEBUG("bc1tl %08x", btarget);
2933 ctx->hflags |= MIPS_HFLAG_BL;
2936 MIPS_INVAL("cp1 branch/jump");
2937 generate_exception_err (ctx, EXCP_RI, 1);
2942 MIPS_DEBUG("enter ds: cond %02x target %08x",
2943 ctx->hflags, btarget);
2944 ctx->btarget = btarget;
2949 /* Coprocessor 1 (FPU) */
2950 static void gen_cp1 (DisasContext *ctx, uint32_t opc, int rt, int fs)
2952 const char *opn = "unk";
2956 GEN_LOAD_FREG_FTN(WT0, fs);
2958 GEN_STORE_TN_REG(rt, T0);
2962 GEN_LOAD_REG_TN(T0, rt);
2964 GEN_STORE_FTN_FREG(fs, WT0);
2968 if (fs != 0 && fs != 31) {
2969 MIPS_INVAL("cfc1 freg");
2970 generate_exception_err (ctx, EXCP_RI, 1);
2973 GEN_LOAD_IMM_TN(T1, fs);
2975 GEN_STORE_TN_REG(rt, T0);
2979 if (fs != 0 && fs != 31) {
2980 MIPS_INVAL("ctc1 freg");
2981 generate_exception_err (ctx, EXCP_RI, 1);
2984 GEN_LOAD_IMM_TN(T1, fs);
2985 GEN_LOAD_REG_TN(T0, rt);
2990 if (loglevel & CPU_LOG_TB_IN_ASM) {
2991 fprintf(logfile, "Invalid CP1 opcode: %08x %03x %03x %03x\n",
2992 ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
2993 ((ctx->opcode >> 16) & 0x1F));
2995 generate_exception_err (ctx, EXCP_RI, 1);
2998 MIPS_DEBUG("%s %s %s", opn, regnames[rt], fregnames[fs]);
3001 /* verify if floating point register is valid; an operation is not defined
3002 * if bit 0 of any register specification is set and the FR bit in the
3003 * Status register equals zero, since the register numbers specify an
3004 * even-odd pair of adjacent coprocessor general registers. When the FR bit
3005 * in the Status register equals one, both even and odd register numbers
3008 * Multiple float registers can be checked by calling
3009 * CHECK_FR(ctx, freg1 | freg2 | ... | fregN);
3011 #define CHECK_FR(ctx, freg) do { \
3012 if (!((ctx)->CP0_Status & (1<<CP0St_FR)) && ((freg) & 1)) { \
3013 generate_exception_err (ctx, EXCP_RI, 1); \
3018 #define FOP(func, fmt) (((fmt) << 21) | (func))
3020 static void gen_farith (DisasContext *ctx, uint32_t op1, int ft, int fs, int fd)
3022 const char *opn = "unk";
3023 const char *condnames[] = {
3042 uint32_t func = ctx->opcode & 0x3f;
3044 switch (ctx->opcode & FOP(0x3f, 0x1f)) {
3046 CHECK_FR(ctx, fs | ft | fd);
3047 GEN_LOAD_FREG_FTN(DT0, fs);
3048 GEN_LOAD_FREG_FTN(DT1, ft);
3049 gen_op_float_add_d();
3050 GEN_STORE_FTN_FREG(fd, DT2);
3055 CHECK_FR(ctx, fs | ft | fd);
3056 GEN_LOAD_FREG_FTN(DT0, fs);
3057 GEN_LOAD_FREG_FTN(DT1, ft);
3058 gen_op_float_sub_d();
3059 GEN_STORE_FTN_FREG(fd, DT2);
3064 CHECK_FR(ctx, fs | ft | fd);
3065 GEN_LOAD_FREG_FTN(DT0, fs);
3066 GEN_LOAD_FREG_FTN(DT1, ft);
3067 gen_op_float_mul_d();
3068 GEN_STORE_FTN_FREG(fd, DT2);
3073 CHECK_FR(ctx, fs | ft | fd);
3074 GEN_LOAD_FREG_FTN(DT0, fs);
3075 GEN_LOAD_FREG_FTN(DT1, ft);
3076 gen_op_float_div_d();
3077 GEN_STORE_FTN_FREG(fd, DT2);
3082 CHECK_FR(ctx, fs | fd);
3083 GEN_LOAD_FREG_FTN(DT0, fs);
3084 gen_op_float_sqrt_d();
3085 GEN_STORE_FTN_FREG(fd, DT2);
3089 CHECK_FR(ctx, fs | fd);
3090 GEN_LOAD_FREG_FTN(DT0, fs);
3091 gen_op_float_abs_d();
3092 GEN_STORE_FTN_FREG(fd, DT2);
3096 CHECK_FR(ctx, fs | fd);
3097 GEN_LOAD_FREG_FTN(DT0, fs);
3098 gen_op_float_mov_d();
3099 GEN_STORE_FTN_FREG(fd, DT2);
3103 CHECK_FR(ctx, fs | fd);
3104 GEN_LOAD_FREG_FTN(DT0, fs);
3105 gen_op_float_chs_d();
3106 GEN_STORE_FTN_FREG(fd, DT2);
3114 CHECK_FR(ctx, fs | fd);
3115 GEN_LOAD_FREG_FTN(DT0, fs);
3116 gen_op_float_roundw_d();
3117 GEN_STORE_FTN_FREG(fd, WT2);
3121 CHECK_FR(ctx, fs | fd);
3122 GEN_LOAD_FREG_FTN(DT0, fs);
3123 gen_op_float_truncw_d();
3124 GEN_STORE_FTN_FREG(fd, WT2);
3128 CHECK_FR(ctx, fs | fd);
3129 GEN_LOAD_FREG_FTN(DT0, fs);
3130 gen_op_float_ceilw_d();
3131 GEN_STORE_FTN_FREG(fd, WT2);
3135 CHECK_FR(ctx, fs | fd);
3136 GEN_LOAD_FREG_FTN(DT0, fs);
3137 gen_op_float_floorw_d();
3138 GEN_STORE_FTN_FREG(fd, WT2);
3141 case FOP(33, 16): /* cvt.d.s */
3142 CHECK_FR(ctx, fs | fd);
3143 GEN_LOAD_FREG_FTN(WT0, fs);
3144 gen_op_float_cvtd_s();
3145 GEN_STORE_FTN_FREG(fd, DT2);
3148 case FOP(33, 20): /* cvt.d.w */
3149 CHECK_FR(ctx, fs | fd);
3150 GEN_LOAD_FREG_FTN(WT0, fs);
3151 gen_op_float_cvtd_w();
3152 GEN_STORE_FTN_FREG(fd, DT2);
3171 CHECK_FR(ctx, fs | ft);
3172 GEN_LOAD_FREG_FTN(DT0, fs);
3173 GEN_LOAD_FREG_FTN(DT1, ft);
3175 opn = condnames[func-48];
3178 CHECK_FR(ctx, fs | ft | fd);
3179 GEN_LOAD_FREG_FTN(WT0, fs);
3180 GEN_LOAD_FREG_FTN(WT1, ft);
3181 gen_op_float_add_s();
3182 GEN_STORE_FTN_FREG(fd, WT2);
3187 CHECK_FR(ctx, fs | ft | fd);
3188 GEN_LOAD_FREG_FTN(WT0, fs);
3189 GEN_LOAD_FREG_FTN(WT1, ft);
3190 gen_op_float_sub_s();
3191 GEN_STORE_FTN_FREG(fd, WT2);
3196 CHECK_FR(ctx, fs | ft | fd);
3197 GEN_LOAD_FREG_FTN(WT0, fs);
3198 GEN_LOAD_FREG_FTN(WT1, ft);
3199 gen_op_float_mul_s();
3200 GEN_STORE_FTN_FREG(fd, WT2);
3205 CHECK_FR(ctx, fs | ft | fd);
3206 GEN_LOAD_FREG_FTN(WT0, fs);
3207 GEN_LOAD_FREG_FTN(WT1, ft);
3208 gen_op_float_div_s();
3209 GEN_STORE_FTN_FREG(fd, WT2);
3214 CHECK_FR(ctx, fs | fd);
3215 GEN_LOAD_FREG_FTN(WT0, fs);
3216 gen_op_float_sqrt_s();
3217 GEN_STORE_FTN_FREG(fd, WT2);
3221 CHECK_FR(ctx, fs | fd);
3222 GEN_LOAD_FREG_FTN(WT0, fs);
3223 gen_op_float_abs_s();
3224 GEN_STORE_FTN_FREG(fd, WT2);
3228 CHECK_FR(ctx, fs | fd);
3229 GEN_LOAD_FREG_FTN(WT0, fs);
3230 gen_op_float_mov_s();
3231 GEN_STORE_FTN_FREG(fd, WT2);
3235 CHECK_FR(ctx, fs | fd);
3236 GEN_LOAD_FREG_FTN(WT0, fs);
3237 gen_op_float_chs_s();
3238 GEN_STORE_FTN_FREG(fd, WT2);
3242 CHECK_FR(ctx, fs | fd);
3243 GEN_LOAD_FREG_FTN(WT0, fs);
3244 gen_op_float_roundw_s();
3245 GEN_STORE_FTN_FREG(fd, WT2);
3249 CHECK_FR(ctx, fs | fd);
3250 GEN_LOAD_FREG_FTN(WT0, fs);
3251 gen_op_float_truncw_s();
3252 GEN_STORE_FTN_FREG(fd, WT2);
3255 case FOP(32, 17): /* cvt.s.d */
3256 CHECK_FR(ctx, fs | fd);
3257 GEN_LOAD_FREG_FTN(DT0, fs);
3258 gen_op_float_cvts_d();
3259 GEN_STORE_FTN_FREG(fd, WT2);
3262 case FOP(32, 20): /* cvt.s.w */
3263 CHECK_FR(ctx, fs | fd);
3264 GEN_LOAD_FREG_FTN(WT0, fs);
3265 gen_op_float_cvts_w();
3266 GEN_STORE_FTN_FREG(fd, WT2);
3269 case FOP(36, 16): /* cvt.w.s */
3270 CHECK_FR(ctx, fs | fd);
3271 GEN_LOAD_FREG_FTN(WT0, fs);
3272 gen_op_float_cvtw_s();
3273 GEN_STORE_FTN_FREG(fd, WT2);
3276 case FOP(36, 17): /* cvt.w.d */
3277 CHECK_FR(ctx, fs | fd);
3278 GEN_LOAD_FREG_FTN(DT0, fs);
3279 gen_op_float_cvtw_d();
3280 GEN_STORE_FTN_FREG(fd, WT2);
3299 CHECK_FR(ctx, fs | ft);
3300 GEN_LOAD_FREG_FTN(WT0, fs);
3301 GEN_LOAD_FREG_FTN(WT1, ft);
3303 opn = condnames[func-48];
3306 if (loglevel & CPU_LOG_TB_IN_ASM) {
3307 fprintf(logfile, "Invalid FP arith function: %08x %03x %03x %03x\n",
3308 ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
3309 ((ctx->opcode >> 16) & 0x1F));
3311 generate_exception_err (ctx, EXCP_RI, 1);
3315 MIPS_DEBUG("%s %s, %s, %s", opn, fregnames[fd], fregnames[fs], fregnames[ft]);
3317 MIPS_DEBUG("%s %s,%s", opn, fregnames[fd], fregnames[fs]);
3320 static void gen_movci (DisasContext *ctx, int rd, int rs, int cc, int tf)
3325 ccbit = 1 << (24 + cc);
3329 gen_op_movf(ccbit, rd, rs);
3331 gen_op_movt(ccbit, rd, rs);
3334 /* ISA extensions (ASEs) */
3335 /* MIPS16 extension to MIPS32 */
3336 /* SmartMIPS extension to MIPS32 */
3338 #ifdef MIPS_HAS_MIPS64
3339 static void gen_arith64 (DisasContext *ctx, uint32_t opc)
3341 if (func == 0x02 && rd == 0) {
3345 if (rs == 0 || rt == 0) {
3349 gen_op_load_gpr_T0(rs);
3350 gen_op_load_gpr_T1(rt);
3363 /* Coprocessor 3 (FPU) */
3365 /* MDMX extension to MIPS64 */
3366 /* MIPS-3D extension to MIPS64 */
3370 static void gen_blikely(DisasContext *ctx)
3373 l1 = gen_new_label();
3375 gen_op_save_state(ctx->hflags & ~MIPS_HFLAG_BMASK);
3376 gen_goto_tb(ctx, 1, ctx->pc + 4);
3380 static void decode_opc (DisasContext *ctx)
3384 uint32_t op, op1, op2;
3387 /* make sure instructions are on a word boundary */
3388 if (ctx->pc & 0x3) {
3389 generate_exception(ctx, EXCP_AdEL);
3393 if ((ctx->hflags & MIPS_HFLAG_BMASK) == MIPS_HFLAG_BL) {
3394 /* Handle blikely not taken case */
3395 MIPS_DEBUG("blikely condition (%08x)", ctx->pc + 4);
3398 op = MASK_OP_MAJOR(ctx->opcode);
3399 rs = (ctx->opcode >> 21) & 0x1f;
3400 rt = (ctx->opcode >> 16) & 0x1f;
3401 rd = (ctx->opcode >> 11) & 0x1f;
3402 sa = (ctx->opcode >> 6) & 0x1f;
3403 imm = (int16_t)ctx->opcode;
3406 op1 = MASK_SPECIAL(ctx->opcode);
3408 case OPC_SLL: /* Arithmetic with immediate */
3409 case OPC_SRL ... OPC_SRA:
3410 gen_arith_imm(ctx, op1, rd, rt, sa);
3412 case OPC_SLLV: /* Arithmetic */
3413 case OPC_SRLV ... OPC_SRAV:
3414 case OPC_MOVZ ... OPC_MOVN:
3415 case OPC_ADD ... OPC_NOR:
3416 case OPC_SLT ... OPC_SLTU:
3417 gen_arith(ctx, op1, rd, rs, rt);
3419 case OPC_MULT ... OPC_DIVU:
3420 gen_muldiv(ctx, op1, rs, rt);
3422 case OPC_JR ... OPC_JALR:
3423 gen_compute_branch(ctx, op1, rs, rd, sa);
3425 case OPC_TGE ... OPC_TEQ: /* Traps */
3427 gen_trap(ctx, op1, rs, rt, -1);
3429 case OPC_MFHI: /* Move from HI/LO */
3431 gen_HILO(ctx, op1, rd);
3434 case OPC_MTLO: /* Move to HI/LO */
3435 gen_HILO(ctx, op1, rs);
3437 case OPC_PMON: /* Pmon entry point */
3441 generate_exception(ctx, EXCP_SYSCALL);
3442 ctx->bstate = BS_EXCP;
3445 generate_exception(ctx, EXCP_BREAK);
3447 case OPC_SPIM: /* SPIM ? */
3448 /* Implemented as RI exception for now. */
3449 MIPS_INVAL("spim (unofficial)");
3450 generate_exception(ctx, EXCP_RI);
3453 /* Treat as a noop. */
3457 gen_op_cp1_enabled();
3458 gen_movci(ctx, rd, rs, (ctx->opcode >> 18) & 0x7,
3459 (ctx->opcode >> 16) & 1);
3462 #ifdef MIPS_HAS_MIPS64
3463 /* MIPS64 specific opcodes */
3465 case OPC_DSRL ... OPC_DSRA:
3467 case OPC_DSRL32 ... OPC_DSRA32:
3468 gen_arith_imm(ctx, op1, rd, rt, sa);
3471 case OPC_DSRLV ... OPC_DSRAV:
3472 case OPC_DADD ... OPC_DSUBU:
3473 gen_arith(ctx, op1, rd, rs, rt);
3475 case OPC_DMULT ... OPC_DDIVU:
3476 gen_muldiv(ctx, op1, rs, rt);
3479 default: /* Invalid */
3480 MIPS_INVAL("special");
3481 generate_exception(ctx, EXCP_RI);
3486 op1 = MASK_SPECIAL2(ctx->opcode);
3488 case OPC_MADD ... OPC_MADDU: /* Multiply and add/sub */
3489 case OPC_MSUB ... OPC_MSUBU:
3490 gen_muldiv(ctx, op1, rs, rt);
3493 gen_arith(ctx, op1, rd, rs, rt);
3495 case OPC_CLZ ... OPC_CLO:
3496 gen_cl(ctx, op1, rd, rs);
3499 /* XXX: not clear which exception should be raised
3500 * when in debug mode...
3502 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
3503 generate_exception(ctx, EXCP_DBp);
3505 generate_exception(ctx, EXCP_DBp);
3507 /* Treat as a noop */
3509 #ifdef MIPS_HAS_MIPS64
3510 case OPC_DCLZ ... OPC_DCLO:
3511 gen_cl(ctx, op1, rd, rs);
3514 default: /* Invalid */
3515 MIPS_INVAL("special2");
3516 generate_exception(ctx, EXCP_RI);
3521 op1 = MASK_SPECIAL3(ctx->opcode);
3525 gen_bitops(ctx, op1, rt, rs, sa, rd);
3528 op2 = MASK_BSHFL(ctx->opcode);
3531 GEN_LOAD_REG_TN(T1, rt);
3535 GEN_LOAD_REG_TN(T1, rt);
3539 GEN_LOAD_REG_TN(T1, rt);
3542 default: /* Invalid */
3543 MIPS_INVAL("bshfl");
3544 generate_exception(ctx, EXCP_RI);
3547 GEN_STORE_TN_REG(rd, T0);
3552 gen_op_rdhwr_cpunum();
3555 gen_op_rdhwr_synci_step();
3561 gen_op_rdhwr_ccres();
3563 default: /* Invalid */
3564 MIPS_INVAL("rdhwr");
3565 generate_exception(ctx, EXCP_RI);
3568 GEN_STORE_TN_REG(rt, T0);
3570 #ifdef MIPS_HAS_MIPS64
3571 case OPC_DEXTM ... OPC_DEXT:
3572 case OPC_DINSM ... OPC_DINS:
3573 gen_bitops(ctx, op1, rt, rs, sa, rd);
3576 op2 = MASK_DBSHFL(ctx->opcode);
3579 GEN_LOAD_REG_TN(T1, rt);
3583 GEN_LOAD_REG_TN(T1, rt);
3586 default: /* Invalid */
3587 MIPS_INVAL("dbshfl");
3588 generate_exception(ctx, EXCP_RI);
3591 GEN_STORE_TN_REG(rd, T0);
3593 default: /* Invalid */
3594 MIPS_INVAL("special3");
3595 generate_exception(ctx, EXCP_RI);
3600 op1 = MASK_REGIMM(ctx->opcode);
3602 case OPC_BLTZ ... OPC_BGEZL: /* REGIMM branches */
3603 case OPC_BLTZAL ... OPC_BGEZALL:
3604 gen_compute_branch(ctx, op1, rs, -1, imm << 2);
3606 case OPC_TGEI ... OPC_TEQI: /* REGIMM traps */
3608 gen_trap(ctx, op1, rs, -1, imm);
3613 default: /* Invalid */
3614 MIPS_INVAL("REGIMM");
3615 generate_exception(ctx, EXCP_RI);
3620 op1 = MASK_CP0(ctx->opcode);
3624 #ifdef MIPS_HAS_MIPS64
3628 gen_cp0(ctx, op1, rt, rd);
3630 case OPC_C0_FIRST ... OPC_C0_LAST:
3631 gen_cp0(ctx, MASK_C0(ctx->opcode), rt, rd);
3634 op2 = MASK_MFMC0(ctx->opcode);
3638 /* Stop translation as we may have switched the execution mode */
3639 ctx->bstate = BS_STOP;
3643 /* Stop translation as we may have switched the execution mode */
3644 ctx->bstate = BS_STOP;
3646 default: /* Invalid */
3647 MIPS_INVAL("MFMC0");
3648 generate_exception(ctx, EXCP_RI);
3651 GEN_STORE_TN_REG(rt, T0);
3653 /* Shadow registers (not implemented). */
3657 generate_exception(ctx, EXCP_RI);
3661 case OPC_ADDI ... OPC_LUI: /* Arithmetic with immediate opcode */
3662 gen_arith_imm(ctx, op, rt, rs, imm);
3664 case OPC_J ... OPC_JAL: /* Jump */
3665 offset = (int32_t)(ctx->opcode & 0x3FFFFFF) << 2;
3666 gen_compute_branch(ctx, op, rs, rt, offset);
3668 case OPC_BEQ ... OPC_BGTZ: /* Branch */
3669 case OPC_BEQL ... OPC_BGTZL:
3670 gen_compute_branch(ctx, op, rs, rt, imm << 2);
3672 case OPC_LB ... OPC_LWR: /* Load and stores */
3673 case OPC_SB ... OPC_SW:
3677 gen_ldst(ctx, op, rt, rs, imm);
3680 /* Treat as a noop */
3683 /* Treat as a noop */
3686 /* Floating point. */
3691 #if defined(MIPS_USES_FPU)
3692 save_cpu_state(ctx, 1);
3693 gen_op_cp1_enabled();
3694 gen_flt_ldst(ctx, op, rt, rs, imm);
3696 generate_exception_err(ctx, EXCP_CpU, 1);
3701 #if defined(MIPS_USES_FPU)
3702 save_cpu_state(ctx, 1);
3703 gen_op_cp1_enabled();
3704 op1 = MASK_CP1(ctx->opcode);
3710 gen_cp1(ctx, op1, rt, rd);
3713 gen_compute_branch1(ctx, MASK_CP1_BCOND(ctx->opcode), imm << 2);
3719 gen_farith(ctx, MASK_CP1_FUNC(ctx->opcode), rt, rd, sa);
3722 generate_exception_err(ctx, EXCP_RI, 1);
3726 generate_exception_err(ctx, EXCP_CpU, 1);
3736 /* COP2: Not implemented. */
3737 generate_exception_err(ctx, EXCP_CpU, 2);
3741 gen_op_cp1_enabled();
3742 op1 = MASK_CP3(ctx->opcode);
3744 /* Not implemented */
3746 generate_exception_err(ctx, EXCP_RI, 1);
3751 #ifdef MIPS_HAS_MIPS64
3752 /* MIPS64 opcodes */
3754 case OPC_LDL ... OPC_LDR:
3755 case OPC_SDL ... OPC_SDR:
3760 gen_ldst(ctx, op, rt, rs, imm);
3762 case OPC_DADDI ... OPC_DADDIU:
3763 gen_arith_imm(ctx, op, rt, rs, imm);
3766 #ifdef MIPS_HAS_MIPS16
3768 /* MIPS16: Not implemented. */
3770 #ifdef MIPS_HAS_MDMX
3772 /* MDMX: Not implemented. */
3774 default: /* Invalid */
3776 generate_exception(ctx, EXCP_RI);
3779 if (ctx->hflags & MIPS_HFLAG_BMASK) {
3780 int hflags = ctx->hflags;
3781 /* Branches completion */
3782 ctx->hflags &= ~MIPS_HFLAG_BMASK;
3783 ctx->bstate = BS_BRANCH;
3784 save_cpu_state(ctx, 0);
3785 switch (hflags & MIPS_HFLAG_BMASK) {
3787 /* unconditional branch */
3788 MIPS_DEBUG("unconditional branch");
3789 gen_goto_tb(ctx, 0, ctx->btarget);
3792 /* blikely taken case */
3793 MIPS_DEBUG("blikely branch taken");
3794 gen_goto_tb(ctx, 0, ctx->btarget);
3797 /* Conditional branch */
3798 MIPS_DEBUG("conditional branch");
3801 l1 = gen_new_label();
3803 gen_goto_tb(ctx, 1, ctx->pc + 4);
3805 gen_goto_tb(ctx, 0, ctx->btarget);
3809 /* unconditional branch to register */
3810 MIPS_DEBUG("branch to register");
3814 MIPS_DEBUG("unknown branch");
3820 int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
3823 DisasContext ctx, *ctxp = &ctx;
3824 target_ulong pc_start;
3825 uint16_t *gen_opc_end;
3828 if (search_pc && loglevel)
3829 fprintf (logfile, "search pc %d\n", search_pc);
3832 gen_opc_ptr = gen_opc_buf;
3833 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
3834 gen_opparam_ptr = gen_opparam_buf;
3839 ctx.bstate = BS_NONE;
3840 /* Restore delay slot state from the tb context. */
3841 ctx.hflags = tb->flags;
3842 ctx.saved_hflags = ctx.hflags;
3843 if (ctx.hflags & MIPS_HFLAG_BR) {
3844 gen_op_restore_breg_target();
3845 } else if (ctx.hflags & MIPS_HFLAG_B) {
3846 ctx.btarget = env->btarget;
3847 } else if (ctx.hflags & MIPS_HFLAG_BMASK) {
3848 /* If we are in the delay slot of a conditional branch,
3849 * restore the branch condition from env->bcond to T2
3851 ctx.btarget = env->btarget;
3852 gen_op_restore_bcond();
3854 #if defined(CONFIG_USER_ONLY)
3857 ctx.mem_idx = !((ctx.hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM);
3859 ctx.CP0_Status = env->CP0_Status;
3861 if (loglevel & CPU_LOG_TB_CPU) {
3862 fprintf(logfile, "------------------------------------------------\n");
3863 /* FIXME: This may print out stale hflags from env... */
3864 cpu_dump_state(env, logfile, fprintf, 0);
3867 #if defined MIPS_DEBUG_DISAS
3868 if (loglevel & CPU_LOG_TB_IN_ASM)
3869 fprintf(logfile, "\ntb %p super %d cond %04x\n",
3870 tb, ctx.mem_idx, ctx.hflags);
3872 while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
3873 if (env->nb_breakpoints > 0) {
3874 for(j = 0; j < env->nb_breakpoints; j++) {
3875 if (env->breakpoints[j] == ctx.pc) {
3876 save_cpu_state(ctxp, 1);
3877 ctx.bstate = BS_BRANCH;
3879 goto done_generating;
3885 j = gen_opc_ptr - gen_opc_buf;
3889 gen_opc_instr_start[lj++] = 0;
3891 gen_opc_pc[lj] = ctx.pc;
3892 gen_opc_hflags[lj] = ctx.hflags & MIPS_HFLAG_BMASK;
3893 gen_opc_instr_start[lj] = 1;
3895 ctx.opcode = ldl_code(ctx.pc);
3899 if (env->singlestep_enabled)
3902 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
3905 #if defined (MIPS_SINGLE_STEP)
3909 if (env->singlestep_enabled) {
3910 save_cpu_state(ctxp, ctx.bstate == BS_NONE);
3912 goto done_generating;
3914 else if (ctx.bstate != BS_BRANCH && ctx.bstate != BS_EXCP) {
3915 save_cpu_state(ctxp, 0);
3916 gen_goto_tb(&ctx, 0, ctx.pc);
3919 /* Generate the return instruction */
3922 *gen_opc_ptr = INDEX_op_end;
3924 j = gen_opc_ptr - gen_opc_buf;
3927 gen_opc_instr_start[lj++] = 0;
3930 tb->size = ctx.pc - pc_start;
3933 #if defined MIPS_DEBUG_DISAS
3934 if (loglevel & CPU_LOG_TB_IN_ASM)
3935 fprintf(logfile, "\n");
3937 if (loglevel & CPU_LOG_TB_IN_ASM) {
3938 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
3939 target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
3940 fprintf(logfile, "\n");
3942 if (loglevel & CPU_LOG_TB_OP) {
3943 fprintf(logfile, "OP:\n");
3944 dump_ops(gen_opc_buf, gen_opparam_buf);
3945 fprintf(logfile, "\n");
3947 if (loglevel & CPU_LOG_TB_CPU) {
3948 fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
3955 int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3957 return gen_intermediate_code_internal(env, tb, 0);
3960 int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3962 return gen_intermediate_code_internal(env, tb, 1);
3965 void fpu_dump_state(CPUState *env, FILE *f,
3966 int (*fpu_fprintf)(FILE *f, const char *fmt, ...),
3971 # define printfpr(fp) do { \
3972 fpu_fprintf(f, "w:%08x d:%08lx%08lx fd:%g fs:%g\n", \
3973 (fp)->w[FP_ENDIAN_IDX], (fp)->w[0], (fp)->w[1], (fp)->fd, (fp)->fs[FP_ENDIAN_IDX]); \
3976 fpu_fprintf(f, "CP1 FCR0 0x%08x FCR31 0x%08x SR.FR %d\n",
3977 env->fcr0, env->fcr31,
3978 (env->CP0_Status & (1 << CP0St_FR)) != 0);
3979 fpu_fprintf(f, "FT0: "); printfpr(&env->ft0);
3980 fpu_fprintf(f, "FT1: "); printfpr(&env->ft1);
3981 fpu_fprintf(f, "FT2: "); printfpr(&env->ft2);
3982 for(i = 0; i < 32; i += 2) {
3983 fpu_fprintf(f, "%s: ", fregnames[i]);
3984 printfpr(FPR(env, i));
3990 void dump_fpu (CPUState *env)
3993 fprintf(logfile, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
3994 env->PC, env->HI, env->LO, env->hflags, env->btarget, env->bcond);
3995 fpu_dump_state(env, logfile, fprintf, 0);
3999 void cpu_dump_state (CPUState *env, FILE *f,
4000 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
4006 cpu_fprintf(f, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
4007 env->PC, env->HI, env->LO, env->hflags, env->btarget, env->bcond);
4008 for (i = 0; i < 32; i++) {
4010 cpu_fprintf(f, "GPR%02d:", i);
4011 cpu_fprintf(f, " %s %08x", regnames[i], env->gpr[i]);
4013 cpu_fprintf(f, "\n");
4016 c0_status = env->CP0_Status;
4017 if (env->hflags & MIPS_HFLAG_UM)
4018 c0_status |= (1 << CP0St_UM);
4019 if (env->hflags & MIPS_HFLAG_ERL)
4020 c0_status |= (1 << CP0St_ERL);
4021 if (env->hflags & MIPS_HFLAG_EXL)
4022 c0_status |= (1 << CP0St_EXL);
4024 cpu_fprintf(f, "CP0 Status 0x%08x Cause 0x%08x EPC 0x%08x\n",
4025 c0_status, env->CP0_Cause, env->CP0_EPC);
4026 cpu_fprintf(f, " Config0 0x%08x Config1 0x%08x LLAddr 0x%08x\n",
4027 env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
4028 if (c0_status & (1 << CP0St_CU1))
4029 fpu_dump_state(env, f, cpu_fprintf, flags);
4032 CPUMIPSState *cpu_mips_init (void)
4036 env = qemu_mallocz(sizeof(CPUMIPSState));
4044 void cpu_reset (CPUMIPSState *env)
4046 memset(env, 0, offsetof(CPUMIPSState, breakpoints));
4051 if (env->hflags & MIPS_HFLAG_BMASK) {
4052 /* If the exception was raised from a delay slot,
4053 * come back to the jump. */
4054 env->CP0_ErrorEPC = env->PC - 4;
4055 env->hflags &= ~MIPS_HFLAG_BMASK;
4057 env->CP0_ErrorEPC = env->PC;
4059 env->PC = 0xBFC00000;
4060 #if defined (MIPS_USES_R4K_TLB)
4061 env->CP0_random = MIPS_TLB_NB - 1;
4062 env->tlb_in_use = MIPS_TLB_NB;
4065 /* SMP not implemented */
4066 env->CP0_EBase = 0x80000000;
4067 env->CP0_Config0 = MIPS_CONFIG0;
4068 env->CP0_Config1 = MIPS_CONFIG1;
4069 env->CP0_Config2 = MIPS_CONFIG2;
4070 env->CP0_Config3 = MIPS_CONFIG3;
4071 env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
4072 env->CP0_WatchLo = 0;
4073 env->hflags = MIPS_HFLAG_ERL;
4074 /* Count register increments in debug mode, EJTAG version 1 */
4075 env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
4076 env->CP0_PRid = MIPS_CPU;
4077 env->exception_index = EXCP_NONE;
4078 #if defined(CONFIG_USER_ONLY)
4079 env->hflags |= MIPS_HFLAG_UM;
4081 #ifdef MIPS_USES_FPU
4082 env->fcr0 = MIPS_FCR0;
4084 /* XXX some guesswork here, values are CPU specific */
4085 env->SYNCI_Step = 16;