1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #pragma once
19 
20 #include "Common/CommonTypes.h"
21 #include "Common/Data/Convert/SmallDataConvert.h"
22 #include "Core/MIPS/MIPS.h"
23 
24 // Invalid branch target address
25 #define INVALIDTARGET 0xFFFFFFFF
26 
27 #define MIPS_MAKE_B(offs)   (0x10000000 | ((offs) & 0xFFFF))
28 #define MIPS_MAKE_J(addr)   (0x08000000 | ((addr)>>2))
29 #define MIPS_MAKE_JAL(addr) (0x0C000000 | ((addr)>>2))
30 #define MIPS_MAKE_JR_RA()   (0x03e00008)
31 #define MIPS_MAKE_NOP()     (0)
32 
33 #define MIPS_MAKE_ADDIU(dreg, sreg, immval) ((9 << 26) | ((dreg) << 16) | ((sreg) << 21) | (immval))
34 #define MIPS_MAKE_LUI(reg, immval) (0x3c000000 | ((reg) << 16) | (immval))
35 #define MIPS_MAKE_ORI(rt, rs, immval) (0x34000000 | ((rs) << 21) | ((rt) << 16) | (immval))
36 #define MIPS_MAKE_LW(rt, rs, immval) (0x8c000000 | ((rs) << 21) | ((rt) << 16) | (immval))
37 #define MIPS_MAKE_SYSCALL(module, function) GetSyscallOp(module, GetNibByName(module, function))
38 #define MIPS_MAKE_BREAK(n) (((n) << 6) | 13)  // ! :)
39 
40 #define MIPS_GET_OP(op)   ((op>>26) & 0x3F)
41 #define MIPS_GET_FUNC(op) (op & 0x3F)
42 #define MIPS_GET_SA(op)   ((op>>6) & 0x1F)
43 
44 #define MIPS_GET_RS(op) MIPSGPReg((op>>21) & 0x1F)
45 #define MIPS_GET_RT(op) MIPSGPReg((op>>16) & 0x1F)
46 #define MIPS_GET_RD(op) MIPSGPReg((op>>11) & 0x1F)
47 
48 #define MIPS_GET_FS(op) ((op>>11) & 0x1F)
49 #define MIPS_GET_FT(op) ((op>>16) & 0x1F)
50 #define MIPS_GET_FD(op) ((op>>6 ) & 0x1F)
51 
52 #define MIPS_GET_VD(op) (op & 0x7F)
53 #define MIPS_GET_VS(op) ((op>>8) & 0x7F)
54 #define MIPS_GET_VT(op) ((op>>16) & 0x7F)
55 
SignExtend8ToS32(MIPSOpcode op)56 inline int32_t SignExtend8ToS32(MIPSOpcode op) {
57 	return SignExtend8ToS32(op.encoding);
58 }
59 
SignExtend8ToU32(MIPSOpcode op)60 inline uint32_t SignExtend8ToU32(MIPSOpcode op) {
61 	return SignExtend8ToU32(op.encoding);
62 }
63 
SignExtend16ToS32(MIPSOpcode op)64 inline int32_t SignExtend16ToS32(MIPSOpcode op) {
65 	return SignExtend16ToS32(op.encoding);
66 }
67 
SignExtend16ToU32(MIPSOpcode op)68 inline uint32_t SignExtend16ToU32(MIPSOpcode op) {
69 	return SignExtend16ToU32(op.encoding);
70 }
71 
72 namespace MIPSCodeUtils
73 {
74 	u32 GetCallTarget(u32 addr);
75 	u32 GetBranchTarget(u32 addr);
76 	// Ignores bltzal/etc. instructions that change RA.
77 	u32 GetBranchTargetNoRA(u32 addr);
78 	u32 GetBranchTargetNoRA(u32 addr, MIPSOpcode op);
79 	u32 GetJumpTarget(u32 addr);
80 	u32 GetSureBranchTarget(u32 addr);
81 	bool IsVFPUBranch(MIPSOpcode op);
82 	bool IsBranch(MIPSOpcode op);
83 }
84