4 * Copyright (C) 2007 ziggy
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "rsp_recomp.h"
51 static opinfo_t opinfo[0x1000/4];
52 static int jumps[0x1000];
53 static unsigned int nb_branches;
54 static branch_t branches[256];
55 static unsigned int nb_labels;
56 static int labels[256];
58 #define OPI(pc) opinfo[(pc)>>2]
59 /*inline*/ void SETLABEL(int pc) {
62 assert(pc >= 0 && pc < 0x1000);
63 if (OPI(pc).labeled != curvisit) {
64 labels[nb_labels] = pc;
65 OPI(pc).label = nb_labels++;
66 assert(nb_labels < sizeof(labels)/sizeof(labels[0]));
67 OPI(pc).labeled = curvisit;
71 #define ABS(addr) (((addr) << 2) & 0xfff)
72 #define REL(offset) ((pc + ((offset) << 2)) & 0xfff)
74 static UINT32 prep_gen(int pc, UINT32 crc, int & len)
79 branches[nb_branches].start = pc;
83 if (OPI(pc).visit == curvisit) {
85 SETLABEL((pc+4)&0xfff);
89 OPI(pc).visit = curvisit;
92 crc = ((crc<<1)|(crc>>31))^op^pc;
98 case 0x00: /* SPECIAL */
105 case 0x09: /* JALR */
108 case 0x0d: /* BREAK */
115 case 0x01: /* REGIMM */
119 case 0x00: /* BLTZ */
120 case 0x01: /* BGEZ */
121 SETLABEL(REL(SIMM16));
123 case 0x11: /* BGEZAL */
131 SETLABEL(ABS(UIMM26));
136 case 0x06: /* BLEZ */
137 case 0x07: /* BGTZ */
138 SETLABEL(REL(SIMM16));
141 //SETLABEL(ABS(UIMM26));
148 branches[nb_branches++].end = pc;
149 assert(nb_branches < sizeof(branches)/sizeof(branches[0]));
154 static void rsp_gen(int pc)
160 // we looped, reset all visit counters
161 for (i=0; i<0x1000/4; i++) {
163 opinfo[i].labeled = 0;
172 UINT32 crc = prep_gen(pc, 0, len);
174 for (i=0; i<nb_labels; i++) {
175 if (OPI(labels[i]).visit != curvisit)
176 crc = prep_gen(labels[i], crc, len);
179 opinfo_t * opi = &OPI(pc);
181 for (i=0; i<opi->nbgen; i++)
182 if (opi->gentable[i].crc == crc) {
183 opi->curgen = opi->gentable + i;
187 if (opi->nbgen >= opi->szgen) {
192 opi->gentable = (gen_t *) realloc(opi->gentable, sizeof(gen_t)*(opi->szgen));
195 gen = opi->gentable + opi->nbgen++;
199 // convert to bytecode
200 unsigned int lbc = 0;
201 static rsp_bc_t bc[0x1000*2+10];
202 for (i=0; i<nb_branches; i++) {
206 rsp_opinfo_t delayed;
208 for (pc = branches[i].start; cont || delayed.op; pc = (pc+4)&0xfff) {
209 UINT32 op = ROPCODE(pc);
213 // rsp_dasm_one(s, realpc, op);
214 // printf("%d %3x\t%s\n", lbc, realpc, s);
217 rsp_get_opinfo(op, &info);
218 if ((info.flags & RSP_OPINFO_JUMP) && !cont)
226 if (RDREG == RTREG && SHIFT == 0)
234 bc[lbc].op2 = info.op2;
235 bc[lbc].flags = info.flags | (((pc&0xffc)<<5)-2) | (!cont? (1<<15):0);
238 loopc = (pc+4)&0xfff;
242 const UINT32 op = delayed.op;
243 switch (delayed.op2) {
250 addop = RSP_CONDJUMPLOCAL;
251 bc[lbc].flags = (pc + (SIMM16<<2))&0xfff; // address to be resolved later
254 addop = RSP_JUMPLOCAL;
255 bc[lbc].flags = (UIMM26<<2)&0xfff; // address to be resolved later
258 addop = RSP_CONDJUMP;
266 bc[lbc].op = delayed.op;
270 if (info.flags & RSP_OPINFO_JUMP) {
274 if (((pc + 4)&0xfff) == branches[i].end)
277 if (bc[lbc-1].op2 != RSP_JUMP &&
278 bc[lbc-1].op2 != RSP_JUMPLOCAL &&
279 bc[lbc-1].op2 != RSP_BREAK &&
280 bc[lbc-1].op2 != RSP_STOP) {
283 bc[lbc].op2 = RSP_LOOP;
284 bc[lbc].flags = loopc; // address to be resolved later
289 // resolve local jumps
290 for (i=0; i<lbc; i++) {
291 // printf("%d %x\n", i, bc[i].op2);
292 // if (bc[i].op2 < RSP_CONTROL_OFFS) {
293 // int realpc = (bc[i].flags>>3)&0xffc;
295 // rsp_dasm_one(s, realpc, bc[i].op);
296 // printf("%3x\t%s\n", realpc, s);
300 case RSP_CONDJUMPLOCAL:
304 // for (pc = 0; pc<lbc; pc++)
305 // if (bc[pc].op2 < RSP_CONTROL_OFFS &&
306 // !(bc[pc].flags & (1<<15)) &&
307 // ((bc[pc].flags>>5)<<2) == bc[i].flags)
310 // bc[i].flags = pc<<5;
311 bc[i].flags = jumps[bc[i].flags]<<5;
318 gen->bc = (rsp_bc_t *) malloc(sizeof(rsp_bc_t)*lbc);
319 memcpy(gen->bc, bc, sizeof(rsp_bc_t)*lbc);
322 void rsp_invalidate(int begin, int len)
324 //printf("invalidate %x %x\n", begin, len);
325 begin = 0; len = 0x1000;
326 assert(begin+len<=0x1000);
328 OPI(begin).curgen = 0;
335 inline void rsp_execute_one(RSP_REGS & rsp, const UINT32 op)
339 case 0x12: /* COP2 */
341 handle_vector_ops(op);
345 case 0x32: /* LWC2 */ handle_lwc2(op); break;
346 case 0x3a: /* SWC2 */ handle_swc2(op); break;
350 unimplemented_opcode(op);
357 static int run(RSP_REGS & rsp, gen_t * gen)
363 const rsp_bc_t & bc = gen->bc[pc];
364 const UINT32 op = bc.op;
365 const int op2 = bc.op2;
367 // if (op2 < RSP_CONTROL_OFFS) {
368 // int realpc = (bc.flags>>3)&0xffc;
370 // rsp_dasm_one(s, realpc, op);
371 // fprintf(stderr, "%3x\t%s\n", realpc, s);
380 case RSP_CONDJUMPLOCAL:
393 #define _LINK(l) rsp.r[l] = ((bc.flags >>3)+8)&0xffc
394 #define _JUMP_PC(a) { cond=1; rsp.nextpc = ((a) & 0xfff); }
395 #define _JUMP_PC_L(a, l) { _LINK(l); _JUMP_PC(a); }
396 #define _JUMP_REL(a) _JUMP_PC(((bc.flags >>3)+4+(a<<2))&0xffc)
397 #define _JUMP_REL_L(a, l) _JUMP_PC_L(((bc.flags >>3)+4+(a<<2))&0xffc, l)
399 case RSP_SLL: if (RDREG) RDVAL = (UINT32)RTVAL << SHIFT; break;
400 case RSP_SRL: if (RDREG) RDVAL = (UINT32)RTVAL >> SHIFT; break;
401 case RSP_SRA: if (RDREG) RDVAL = (INT32)RTVAL >> SHIFT; break;
402 case RSP_SLLV: if (RDREG) RDVAL = (UINT32)RTVAL << (RSVAL & 0x1f); break;
403 case RSP_SRLV: if (RDREG) RDVAL = (UINT32)RTVAL >> (RSVAL & 0x1f); break;
404 case RSP_SRAV: if (RDREG) RDVAL = (INT32)RTVAL >> (RSVAL & 0x1f); break;
405 case RSP_JR: _JUMP_PC(RSVAL); break;
406 case RSP_JALR: _JUMP_PC_L(RSVAL, RDREG); break;
409 *z64_rspinfo.SP_STATUS_REG |= (SP_STATUS_HALT | SP_STATUS_BROKE );
410 if ((*z64_rspinfo.SP_STATUS_REG & SP_STATUS_INTR_BREAK) != 0 ) {
411 *z64_rspinfo.MI_INTR_REG |= 1;
412 z64_rspinfo.CheckInterrupts();
416 case RSP_ADD: if (RDREG) RDVAL = (INT32)(RSVAL + RTVAL); break;
417 case RSP_ADDU: if (RDREG) RDVAL = (INT32)(RSVAL + RTVAL); break;
418 case RSP_SUB: if (RDREG) RDVAL = (INT32)(RSVAL - RTVAL); break;
419 case RSP_SUBU: if (RDREG) RDVAL = (INT32)(RSVAL - RTVAL); break;
420 case RSP_AND: if (RDREG) RDVAL = RSVAL & RTVAL; break;
421 case RSP_OR: if (RDREG) RDVAL = RSVAL | RTVAL; break;
422 case RSP_XOR: if (RDREG) RDVAL = RSVAL ^ RTVAL; break;
423 case RSP_NOR: if (RDREG) RDVAL = ~(RSVAL | RTVAL); break;
424 case RSP_SLT: if (RDREG) RDVAL = (INT32)RSVAL < (INT32)RTVAL; break;
425 case RSP_SLTU: if (RDREG) RDVAL = (UINT32)RSVAL < (UINT32)RTVAL; break;
426 case RSP_BLTZ: if ((INT32)(RSVAL) < 0) cond = 1; break;
427 case RSP_BGEZ: if ((INT32)(RSVAL) >= 0) cond = 1; break;
428 case RSP_BGEZAL: _LINK(31); if ((INT32)(RSVAL) >= 0) _JUMP_REL(SIMM16); break;
429 case RSP_J: cond = 1; break;
430 case RSP_JAL: _JUMP_PC_L(UIMM26<<2, 31); break;
431 case RSP_BEQ: if (RSVAL == RTVAL) cond = 1; break;
432 case RSP_BNE: if (RSVAL != RTVAL) cond = 1; break;
433 case RSP_BLEZ: if ((INT32)RSVAL <= 0) cond = 1; break;
434 case RSP_BGTZ: if ((INT32)RSVAL > 0) cond = 1; break;
435 case RSP_ADDI: if (RTREG) RTVAL = (INT32)(RSVAL + SIMM16); break;
436 case RSP_ADDIU: if (RTREG) RTVAL = (INT32)(RSVAL + SIMM16); break;
437 case RSP_SLTI: if (RTREG) RTVAL = (INT32)(RSVAL) < ((INT32)SIMM16); break;
438 case RSP_SLTIU: if (RTREG) RTVAL = (UINT32)(RSVAL) < (UINT32)((INT32)SIMM16); break;
439 case RSP_ANDI: if (RTREG) RTVAL = RSVAL & UIMM16; break;
440 case RSP_ORI: if (RTREG) RTVAL = RSVAL | UIMM16; break;
441 case RSP_XORI: if (RTREG) RTVAL = RSVAL ^ UIMM16; break;
442 case RSP_LUI: if (RTREG) RTVAL = UIMM16 << 16; break;
446 switch ((op >> 21) & 0x1f)
448 case 0x00: /* MFC0 */
450 RTVAL = get_cop0_reg(RDREG);
452 case 0x04: /* MTC0 */
453 set_cop0_reg(RDREG, RTVAL);
456 sp_pc = ((bc.flags >>3) + 4)&0xffc;
461 log(M64MSG_WARNING, "unimplemented cop0 %x (%x)\n", (op >> 21) & 0x1f, op);
469 // 31 25 20 15 10 6 0
470 // ---------------------------------------------------
471 // | 010010 | 00000 | TTTTT | DDDDD | IIII | 0000000 |
472 // ---------------------------------------------------
475 int el = (op >> 7) & 0xf;
476 UINT16 b1 = VREG_B(VS1REG, (el+0) & 0xf);
477 UINT16 b2 = VREG_B(VS1REG, (el+1) & 0xf);
478 if (RTREG) RTVAL = (INT32)(INT16)((b1 << 8) | (b2));
484 // ------------------------------------------------
485 // | 010010 | 00010 | TTTTT | DDDDD | 00000000000 |
486 // ------------------------------------------------
489 // VP to sign extend or to not sign extend ?
490 //if (RTREG) RTVAL = (INT16)rsp.flag[RDREG];
491 if (RTREG) RTVAL = rsp.flag[RDREG];
496 // 31 25 20 15 10 6 0
497 // ---------------------------------------------------
498 // | 010010 | 00100 | TTTTT | DDDDD | IIII | 0000000 |
499 // ---------------------------------------------------
502 int el = (op >> 7) & 0xf;
503 VREG_B(VS1REG, (el+0) & 0xf) = (RTVAL >> 8) & 0xff;
504 VREG_B(VS1REG, (el+1) & 0xf) = (RTVAL >> 0) & 0xff;
510 // ------------------------------------------------
511 // | 010010 | 00110 | TTTTT | DDDDD | 00000000000 |
512 // ------------------------------------------------
515 rsp.flag[RDREG] = RTVAL & 0xffff;
518 case RSP_LB: if (RTREG) RTVAL = (INT32)(INT8)READ8(RSVAL + SIMM16); break;
519 case RSP_LH: if (RTREG) RTVAL = (INT32)(INT16)READ16(RSVAL + SIMM16); break;
520 case RSP_LW: if (RTREG) RTVAL = READ32(RSVAL + SIMM16); break;
521 case RSP_LBU: if (RTREG) RTVAL = (UINT8)READ8(RSVAL + SIMM16); break;
522 case RSP_LHU: if (RTREG) RTVAL = (UINT16)READ16(RSVAL + SIMM16); break;
523 case RSP_SB: WRITE8(RSVAL + SIMM16, RTVAL); break;
524 case RSP_SH: WRITE16(RSVAL + SIMM16, RTVAL); break;
525 case RSP_SW: WRITE32(RSVAL + SIMM16, RTVAL); break;
530 case 0x12: /* COP2 */
531 handle_vector_ops(op);
533 case 0x32: /* LWC2 */
536 case 0x3a: /* SWC2 */
544 int rsp_gen_cache_hit;
545 int rsp_gen_cache_miss;
551 opinfo_t * opi = &OPI(pc);
552 gen_t * gen = opi->curgen;
555 rsp_gen_cache_miss++;
559 //fprintf(stderr, "rsp_jump %x (%s)\n", pc, gen->name);
561 int res = run(rsp, gen);
563 //fprintf(stderr, "r31 %x from %x nextpc %x pc %x res %d (%s)\n", rsp.r[31], pc, rsp.nextpc, sp_pc, res, gen->name);
564 if (rsp.nextpc != ~0U)
566 sp_pc = (rsp.nextpc & 0xfff);
571 //sp_pc = ((sp_pc+4)&0xfff);