#include "Core/MIPS/MIPS.h"
#include "Core/MIPS/MIPSTables.h"
#include "Core/MIPS/MIPSCodeUtils.h"
#include "Core/MemMap.h"
namespace MIPSCodeUtils
{
#define OP_SYSCALL 0x0000000c
#define OP_SYSCALL_MASK 0xFC00003F
#define _RS ((op>>21) & 0x1F)
#define _RT ((op>>16) & 0x1F)
#define _IMM26 (op & 0x03FFFFFF)
#define TARGET16 ((int)(SignExtend16ToU32(op) << 2))
#define TARGET26 (_IMM26 << 2)
u32 GetJumpTarget(u32 addr) {
MIPSOpcode op = Memory::Read_Instruction(addr, true);
if (op != 0) {
MIPSInfo info = MIPSGetInfo(op);
if ((info & IS_JUMP) && (info & IN_IMM26))
return (addr & 0xF0000000) | TARGET26;
else
return INVALIDTARGET;
} else {
return INVALIDTARGET;
}
}
u32 GetBranchTarget(u32 addr) {
MIPSOpcode op = Memory::Read_Instruction(addr, true);
if (op != 0) {
MIPSInfo info = MIPSGetInfo(op);
if (info & IS_CONDBRANCH)
return addr + 4 + TARGET16;
else
return INVALIDTARGET;
} else {
return INVALIDTARGET;
}
}
u32 GetBranchTargetNoRA(u32 addr) {
MIPSOpcode op = Memory::Read_Instruction(addr, true);
return GetBranchTargetNoRA(addr, op);
}
u32 GetBranchTargetNoRA(u32 addr, MIPSOpcode op) {
if (op != 0) {
MIPSInfo info = MIPSGetInfo(op);
if ((info & IS_CONDBRANCH) && !(info & OUT_RA))
return addr + 4 + TARGET16;
else
return INVALIDTARGET;
} else {
return INVALIDTARGET;
}
}
u32 GetSureBranchTarget(u32 addr) {
MIPSOpcode op = Memory::Read_Instruction(addr, true);
if (op != 0) {
MIPSInfo info = MIPSGetInfo(op);
if ((info & IS_CONDBRANCH) && !(info & (IN_FPUFLAG | IS_VFPU))) {
bool sure;
bool takeBranch;
switch (info & CONDTYPE_MASK)
{
case CONDTYPE_EQ:
sure = _RS == _RT;
takeBranch = true;
break;
case CONDTYPE_NE:
sure = _RS == _RT;
takeBranch = false;
break;
case CONDTYPE_LEZ:
case CONDTYPE_GEZ:
sure = _RS == 0;
takeBranch = true;
break;
case CONDTYPE_LTZ:
case CONDTYPE_GTZ:
sure = _RS == 0;
takeBranch = false;
break;
default:
sure = false;
}
if (sure && takeBranch)
return addr + 4 + TARGET16;
else if (sure && !takeBranch)
return addr + 8;
else
return INVALIDTARGET;
} else {
return INVALIDTARGET;
}
} else {
return INVALIDTARGET;
}
}
bool IsVFPUBranch(MIPSOpcode op) {
return (MIPSGetInfo(op) & (IS_VFPU | IS_CONDBRANCH)) == (IS_VFPU | IS_CONDBRANCH);
}
bool IsBranch(MIPSOpcode op) {
return (MIPSGetInfo(op) & IS_CONDBRANCH) == IS_CONDBRANCH;
}
}