#include <stdint.h>
#include "usf/usf.h"
#include "api/m64p_types.h"
#include "api/callbacks.h"
#include "memory/memory.h"
#include "main/main.h"
#include "osal/preproc.h"
#include "cached_interp.h"
#include "r4300.h"
#include "cp0.h"
#include "cp1.h"
#include "exception.h"
#include "interupt.h"
#include "tlb.h"
#include "usf/usf_internal.h"
static void InterpretOpcode(usf_state_t * state);
#define PCADDR state->interp_PC.addr
#define ADD_TO_PC(x) state->interp_PC.addr += x*4;
#define DECLARE_INSTRUCTION(name) static void name(usf_state_t * state, uint32_t op)
#define DECLARE_JUMP(name, destination, condition, link, likely, cop1) \
static void name(usf_state_t * state, uint32_t op) \
{ \
const int take_jump = (condition); \
const unsigned int jump_target = (destination); \
long long int *link_register = (link); \
if (cop1 && check_cop1_unusable(state)) return; \
if (link_register != &state->reg[0]) \
{ \
*link_register=state->interp_PC.addr + 8; \
sign_extended(*link_register); \
} \
if (!likely || take_jump) \
{ \
state->interp_PC.addr += 4; \
state->delay_slot=1; \
InterpretOpcode(state); \
update_count(state); \
state->delay_slot=0; \
if (take_jump && !state->skip_jump) \
{ \
state->interp_PC.addr = jump_target; \
} \
} \
else \
{ \
state->interp_PC.addr += 8; \
update_count(state); \
} \
state->last_addr = state->interp_PC.addr; \
if (state->next_interupt <= state->g_cp0_regs[CP0_COUNT_REG]) gen_interupt(state); \
} \
static void name##_IDLE(usf_state_t * state, uint32_t op) \
{ \
const int take_jump = (condition); \
int skip; \
if (cop1 && check_cop1_unusable(state)) return; \
if (take_jump) \
{ \
update_count(state); \
skip = state->next_interupt - state->g_cp0_regs[CP0_COUNT_REG]; \
if (skip > 3) state->g_cp0_regs[CP0_COUNT_REG] += (skip & 0xFFFFFFFC); \
else name(state, op); \
} \
else name(state, op); \
}
#define CHECK_MEMORY()
#define RD_OF(op) (((op) >> 11) & 0x1F)
#define RS_OF(op) (((op) >> 21) & 0x1F)
#define RT_OF(op) (((op) >> 16) & 0x1F)
#define SA_OF(op) (((op) >> 6) & 0x1F)
#define IMM16S_OF(op) ((int16_t) (op))
#define IMM16U_OF(op) ((uint16_t) (op))
#define FD_OF(op) (((op) >> 6) & 0x1F)
#define FS_OF(op) (((op) >> 11) & 0x1F)
#define FT_OF(op) (((op) >> 16) & 0x1F)
#define JUMP_OF(op) ((op) & UINT32_C(0x3FFFFFF))
#define IS_RELATIVE_IDLE_LOOP(op, addr) \
(IMM16S_OF(op) == -1 && *fast_mem_access(state, (addr) + 4) == 0)
#define IS_ABSOLUTE_IDLE_LOOP(op, addr) \
(JUMP_OF(op) == ((addr) & UINT32_C(0x0FFFFFFF)) >> 2 \
&& ((addr) & UINT32_C(0x0FFFFFFF)) != UINT32_C(0x0FFFFFFC) \
&& *fast_mem_access(state, (addr) + 4) == 0)
#define sign_extended(a) a = (int64_t) ((int32_t) (a))
#define sign_extendedb(a) a = (int64_t) ((int8_t) (a))
#define sign_extendedh(a) a = (int64_t) ((int16_t) (a))
#define rrt state->reg[RT_OF(op)]
#define rrd state->reg[RD_OF(op)]
#define rfs FS_OF(op)
#define rrs state->reg[RS_OF(op)]
#define rsa SA_OF(op)
#define irt state->reg[RT_OF(op)]
#define ioffset IMM16S_OF(op)
#define iimmediate IMM16S_OF(op)
#define irs state->reg[RS_OF(op)]
#define ibase state->reg[RS_OF(op)]
#define jinst_index JUMP_OF(op)
#define lfbase RS_OF(op)
#define lfft FT_OF(op)
#define lfoffset IMM16S_OF(op)
#define cfft FT_OF(op)
#define cffs FS_OF(op)
#define cffd FD_OF(op)
#ifndef M64P_BIG_ENDIAN
#define rrt32 *((int32_t*) &state->reg[RT_OF(op)])
#define rrd32 *((int32_t*) &state->reg[RD_OF(op)])
#define rrs32 *((int32_t*) &state->reg[RS_OF(op)])
#define irs32 *((int32_t*) &state->reg[RS_OF(op)])
#define irt32 *((int32_t*) &state->reg[RT_OF(op)])
#else
#define rrt32 *((int32_t*) &state->reg[RT_OF(op)] + 1)
#define rrd32 *((int32_t*) &state->reg[RD_OF(op)] + 1)
#define rrs32 *((int32_t*) &state->reg[RS_OF(op)] + 1)
#define irs32 *((int32_t*) &state->reg[RS_OF(op)] + 1)
#define irt32 *((int32_t*) &state->reg[RT_OF(op)] + 1)
#endif
#if defined(__GNUC__)
static void JR_IDLE(usf_state_t *, uint32_t) __attribute__((used));
static void JALR_IDLE(usf_state_t *, uint32_t) __attribute__((used));
#endif
#include "interpreter.def"
#include <stdio.h>
#include <inttypes.h>
#ifdef DEBUG_INFO
#include "debugger/dbg_decoder.h"
#include <string.h>
#endif
void InterpretOpcode(usf_state_t * state)
{
uint32_t op = *fast_mem_access(state, state->PC->addr);
#ifdef DEBUG_INFO
{
char instr[256];
char arguments[256];
r4300_decode_op(op, instr, arguments, state->PC->addr);
fprintf(state->debug_log, "%08x: %-16s %s\n", state->PC->addr, instr, arguments);
}
#endif
switch ((op >> 26) & 0x3F) {
case 0:
switch (op & 0x3F) {
case 0:
if (RD_OF(op) != 0) SLL(state, op);
else NOP(state, 0);
break;
case 2:
if (RD_OF(op) != 0) SRL(state, op);
else NOP(state, 0);
break;
case 3:
if (RD_OF(op) != 0) SRA(state, op);
else NOP(state, 0);
break;
case 4:
if (RD_OF(op) != 0) SLLV(state, op);
else NOP(state, 0);
break;
case 6:
if (RD_OF(op) != 0) SRLV(state, op);
else NOP(state, 0);
break;
case 7:
if (RD_OF(op) != 0) SRAV(state, op);
else NOP(state, 0);
break;
case 8: JR(state, op); break;
case 9:
JALR(state, op);
break;
case 12: SYSCALL(state, op); break;
case 13:
BREAK(state, op);
break;
case 15: SYNC(state, op); break;
case 16:
if (RD_OF(op) != 0) MFHI(state, op);
else NOP(state, 0);
break;
case 17: MTHI(state, op); break;
case 18:
if (RD_OF(op) != 0) MFLO(state, op);
else NOP(state, 0);
break;
case 19: MTLO(state, op); break;
case 20:
if (RD_OF(op) != 0) DSLLV(state, op);
else NOP(state, 0);
break;
case 22:
if (RD_OF(op) != 0) DSRLV(state, op);
else NOP(state, 0);
break;
case 23:
if (RD_OF(op) != 0) DSRAV(state, op);
else NOP(state, 0);
break;
case 24: MULT(state, op); break;
case 25: MULTU(state, op); break;
case 26: DIV(state, op); break;
case 27: DIVU(state, op); break;
case 28: DMULT(state, op); break;
case 29: DMULTU(state, op); break;
case 30: DDIV(state, op); break;
case 31: DDIVU(state, op); break;
case 32:
if (RD_OF(op) != 0) ADD(state, op);
else NOP(state, 0);
break;
case 33:
if (RD_OF(op) != 0) ADDU(state, op);
else NOP(state, 0);
break;
case 34:
if (RD_OF(op) != 0) SUB(state, op);
else NOP(state, 0);
break;
case 35:
if (RD_OF(op) != 0) SUBU(state, op);
else NOP(state, 0);
break;
case 36:
if (RD_OF(op) != 0) AND(state, op);
else NOP(state, 0);
break;
case 37:
if (RD_OF(op) != 0) OR(state, op);
else NOP(state, 0);
break;
case 38:
if (RD_OF(op) != 0) XOR(state, op);
else NOP(state, 0);
break;
case 39:
if (RD_OF(op) != 0) NOR(state, op);
else NOP(state, 0);
break;
case 42:
if (RD_OF(op) != 0) SLT(state, op);
else NOP(state, 0);
break;
case 43:
if (RD_OF(op) != 0) SLTU(state, op);
else NOP(state, 0);
break;
case 44:
if (RD_OF(op) != 0) DADD(state, op);
else NOP(state, 0);
break;
case 45:
if (RD_OF(op) != 0) DADDU(state, op);
else NOP(state, 0);
break;
case 46:
if (RD_OF(op) != 0) DSUB(state, op);
else NOP(state, 0);
break;
case 47:
if (RD_OF(op) != 0) DSUBU(state, op);
else NOP(state, 0);
break;
case 48:
case 49:
case 50:
case 51:
NI(state, op);
break;
case 52: TEQ(state, op); break;
case 54:
NI(state, op);
break;
case 56:
if (RD_OF(op) != 0) DSLL(state, op);
else NOP(state, 0);
break;
case 58:
if (RD_OF(op) != 0) DSRL(state, op);
else NOP(state, 0);
break;
case 59:
if (RD_OF(op) != 0) DSRA(state, op);
else NOP(state, 0);
break;
case 60:
if (RD_OF(op) != 0) DSLL32(state, op);
else NOP(state, 0);
break;
case 62:
if (RD_OF(op) != 0) DSRL32(state, op);
else NOP(state, 0);
break;
case 63:
if (RD_OF(op) != 0) DSRA32(state, op);
else NOP(state, 0);
break;
default:
RESERVED(state, op);
break;
}
break;
case 1:
switch ((op >> 16) & 0x1F) {
case 0:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLTZ_IDLE(state, op);
else BLTZ(state, op);
break;
case 1:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGEZ_IDLE(state, op);
else BGEZ(state, op);
break;
case 2:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLTZL_IDLE(state, op);
else BLTZL(state, op);
break;
case 3:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGEZL_IDLE(state, op);
else BGEZL(state, op);
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 14:
NI(state, op);
break;
case 16:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLTZAL_IDLE(state, op);
else BLTZAL(state, op);
break;
case 17:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGEZAL_IDLE(state, op);
else BGEZAL(state, op);
break;
case 18:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLTZALL_IDLE(state, op);
else BLTZALL(state, op);
break;
case 19:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGEZALL_IDLE(state, op);
else BGEZALL(state, op);
break;
default:
RESERVED(state, op);
break;
}
break;
case 2:
if (IS_ABSOLUTE_IDLE_LOOP(op, state->PC->addr)) J_IDLE(state, op);
else J(state, op);
break;
case 3:
if (IS_ABSOLUTE_IDLE_LOOP(op, state->PC->addr)) JAL_IDLE(state, op);
else JAL(state, op);
break;
case 4:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BEQ_IDLE(state, op);
else BEQ(state, op);
break;
case 5:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BNE_IDLE(state, op);
else BNE(state, op);
break;
case 6:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLEZ_IDLE(state, op);
else BLEZ(state, op);
break;
case 7:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGTZ_IDLE(state, op);
else BGTZ(state, op);
break;
case 8:
if (RT_OF(op) != 0) ADDI(state, op);
else NOP(state, 0);
break;
case 9:
if (RT_OF(op) != 0) ADDIU(state, op);
else NOP(state, 0);
break;
case 10:
if (RT_OF(op) != 0) SLTI(state, op);
else NOP(state, 0);
break;
case 11:
if (RT_OF(op) != 0) SLTIU(state, op);
else NOP(state, 0);
break;
case 12:
if (RT_OF(op) != 0) ANDI(state, op);
else NOP(state, 0);
break;
case 13:
if (RT_OF(op) != 0) ORI(state, op);
else NOP(state, 0);
break;
case 14:
if (RT_OF(op) != 0) XORI(state, op);
else NOP(state, 0);
break;
case 15:
if (RT_OF(op) != 0) LUI(state, op);
else NOP(state, 0);
break;
case 16:
switch ((op >> 21) & 0x1F) {
case 0:
if (RT_OF(op) != 0) MFC0(state, op);
else NOP(state, 0);
break;
case 4: MTC0(state, op); break;
case 16:
switch (op & 0x3F) {
case 1: TLBR(state, op); break;
case 2: TLBWI(state, op); break;
case 6: TLBWR(state, op); break;
case 8: TLBP(state, op); break;
case 24: ERET(state, op); break;
default:
RESERVED(state, op);
break;
}
break;
default:
RESERVED(state, op);
break;
}
break;
case 17:
switch ((op >> 21) & 0x1F) {
case 0:
if (RT_OF(op) != 0) MFC1(state, op);
else NOP(state, 0);
break;
case 1:
if (RT_OF(op) != 0) DMFC1(state, op);
else NOP(state, 0);
break;
case 2:
if (RT_OF(op) != 0) CFC1(state, op);
else NOP(state, 0);
break;
case 4: MTC1(state, op); break;
case 5: DMTC1(state, op); break;
case 6: CTC1(state, op); break;
case 8:
switch ((op >> 16) & 0x3) {
case 0:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BC1F_IDLE(state, op);
else BC1F(state, op);
break;
case 1:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BC1T_IDLE(state, op);
else BC1T(state, op);
break;
case 2:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BC1FL_IDLE(state, op);
else BC1FL(state, op);
break;
case 3:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BC1TL_IDLE(state, op);
else BC1TL(state, op);
break;
}
break;
case 16:
switch (op & 0x3F) {
case 0: ADD_S(state, op); break;
case 1: SUB_S(state, op); break;
case 2: MUL_S(state, op); break;
case 3: DIV_S(state, op); break;
case 4: SQRT_S(state, op); break;
case 5: ABS_S(state, op); break;
case 6: MOV_S(state, op); break;
case 7: NEG_S(state, op); break;
case 8: ROUND_L_S(state, op); break;
case 9: TRUNC_L_S(state, op); break;
case 10: CEIL_L_S(state, op); break;
case 11: FLOOR_L_S(state, op); break;
case 12: ROUND_W_S(state, op); break;
case 13: TRUNC_W_S(state, op); break;
case 14: CEIL_W_S(state, op); break;
case 15: FLOOR_W_S(state, op); break;
case 33: CVT_D_S(state, op); break;
case 36: CVT_W_S(state, op); break;
case 37: CVT_L_S(state, op); break;
case 48: C_F_S(state, op); break;
case 49: C_UN_S(state, op); break;
case 50: C_EQ_S(state, op); break;
case 51: C_UEQ_S(state, op); break;
case 52: C_OLT_S(state, op); break;
case 53: C_ULT_S(state, op); break;
case 54: C_OLE_S(state, op); break;
case 55: C_ULE_S(state, op); break;
case 56: C_SF_S(state, op); break;
case 57: C_NGLE_S(state, op); break;
case 58: C_SEQ_S(state, op); break;
case 59: C_NGL_S(state, op); break;
case 60: C_LT_S(state, op); break;
case 61: C_NGE_S(state, op); break;
case 62: C_LE_S(state, op); break;
case 63: C_NGT_S(state, op); break;
default:
RESERVED(state, op);
break;
}
break;
case 17:
switch (op & 0x3F) {
case 0: ADD_D(state, op); break;
case 1: SUB_D(state, op); break;
case 2: MUL_D(state, op); break;
case 3: DIV_D(state, op); break;
case 4: SQRT_D(state, op); break;
case 5: ABS_D(state, op); break;
case 6: MOV_D(state, op); break;
case 7: NEG_D(state, op); break;
case 8: ROUND_L_D(state, op); break;
case 9: TRUNC_L_D(state, op); break;
case 10: CEIL_L_D(state, op); break;
case 11: FLOOR_L_D(state, op); break;
case 12: ROUND_W_D(state, op); break;
case 13: TRUNC_W_D(state, op); break;
case 14: CEIL_W_D(state, op); break;
case 15: FLOOR_W_D(state, op); break;
case 32: CVT_S_D(state, op); break;
case 36: CVT_W_D(state, op); break;
case 37: CVT_L_D(state, op); break;
case 48: C_F_D(state, op); break;
case 49: C_UN_D(state, op); break;
case 50: C_EQ_D(state, op); break;
case 51: C_UEQ_D(state, op); break;
case 52: C_OLT_D(state, op); break;
case 53: C_ULT_D(state, op); break;
case 54: C_OLE_D(state, op); break;
case 55: C_ULE_D(state, op); break;
case 56: C_SF_D(state, op); break;
case 57: C_NGLE_D(state, op); break;
case 58: C_SEQ_D(state, op); break;
case 59: C_NGL_D(state, op); break;
case 60: C_LT_D(state, op); break;
case 61: C_NGE_D(state, op); break;
case 62: C_LE_D(state, op); break;
case 63: C_NGT_D(state, op); break;
default:
RESERVED(state, op);
break;
}
break;
case 20:
switch (op & 0x3F) {
case 32: CVT_S_W(state, op); break;
case 33: CVT_D_W(state, op); break;
default:
RESERVED(state, op);
break;
}
break;
case 21:
switch (op & 0x3F) {
case 32: CVT_S_L(state, op); break;
case 33: CVT_D_L(state, op); break;
default:
RESERVED(state, op);
break;
}
break;
default:
RESERVED(state, op);
break;
}
break;
case 20:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BEQL_IDLE(state, op);
else BEQL(state, op);
break;
case 21:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BNEL_IDLE(state, op);
else BNEL(state, op);
break;
case 22:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BLEZL_IDLE(state, op);
else BLEZL(state, op);
break;
case 23:
if (IS_RELATIVE_IDLE_LOOP(op, state->PC->addr)) BGTZL_IDLE(state, op);
else BGTZL(state, op);
break;
case 24:
if (RT_OF(op) != 0) DADDI(state, op);
else NOP(state, 0);
break;
case 25:
if (RT_OF(op) != 0) DADDIU(state, op);
else NOP(state, 0);
break;
case 26:
if (RT_OF(op) != 0) LDL(state, op);
else NOP(state, 0);
break;
case 27:
if (RT_OF(op) != 0) LDR(state, op);
else NOP(state, 0);
break;
case 32:
if (RT_OF(op) != 0) LB(state, op);
else NOP(state, 0);
break;
case 33:
if (RT_OF(op) != 0) LH(state, op);
else NOP(state, 0);
break;
case 34:
if (RT_OF(op) != 0) LWL(state, op);
else NOP(state, 0);
break;
case 35:
if (RT_OF(op) != 0) LW(state, op);
else NOP(state, 0);
break;
case 36:
if (RT_OF(op) != 0) LBU(state, op);
else NOP(state, 0);
break;
case 37:
if (RT_OF(op) != 0) LHU(state, op);
else NOP(state, 0);
break;
case 38:
if (RT_OF(op) != 0) LWR(state, op);
else NOP(state, 0);
break;
case 39:
if (RT_OF(op) != 0) LWU(state, op);
else NOP(state, 0);
break;
case 40: SB(state, op); break;
case 41: SH(state, op); break;
case 42: SWL(state, op); break;
case 43: SW(state, op); break;
case 44: SDL(state, op); break;
case 45: SDR(state, op); break;
case 46: SWR(state, op); break;
case 47: CACHE(state, op); break;
case 48:
if (RT_OF(op) != 0) LL(state, op);
else NOP(state, 0);
break;
case 49: LWC1(state, op); break;
case 52:
NI(state, op);
break;
case 53: LDC1(state, op); break;
case 55:
if (RT_OF(op) != 0) LD(state, op);
else NOP(state, 0);
break;
case 56:
if (RT_OF(op) != 0) SC(state, op);
else NOP(state, 0);
break;
case 57: SWC1(state, op); break;
case 60:
NI(state, op);
break;
case 61: SDC1(state, op); break;
case 63: SD(state, op); break;
default:
RESERVED(state, op);
break;
}
}
void pure_interpreter(usf_state_t * state)
{
state->stop = 0;
state->PC = &state->interp_PC;
while (!state->stop)
InterpretOpcode(state);
}