-
Notifications
You must be signed in to change notification settings - Fork 0
/
gte.py
194 lines (156 loc) · 6.07 KB
/
gte.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import idaapi
import ida_ida
import ida_allins
import ida_idp
import ida_bytes
import ida_ua
ITYPE_START = ida_idp.CUSTOM_INSN_ITYPE + 0x100
MNEM_WIDTH = 13
class GTE_disassemble(idaapi.IDP_Hooks):
def __init__(self):
idaapi.IDP_Hooks.__init__(self)
class idef:
def __init__(self, opcode, name, cmt):
self.opcode = opcode
self.name = name
self.cmt = cmt
self.itable = [
# Coprocessor Calculation Instructions
idef(0x01, "RTPS", "Perspective Transformation single"),
idef(0x06, "NCLIP", "Normal clipping"),
idef(0x0C, "OP", "Outer product of 2 vectors"),
idef(0x10, "DPCS", "Depth Cueing single"),
idef(0x11, "INTPL", "Interpolation of a vector and far color vector"),
idef(0x12 , "MVMVA", "Multiply vector by matrix and add vector (see below)"),
idef(0x13, "NCDS", "Normal color depth cue single vector"),
idef(0x14, "CDP", "Color Depth Que"),
idef(0x16, "NCDT", "Normal color depth cue triple vectors"),
idef(0x1B, "NCCS", "Normal Color Color single vector"),
idef(0x1C, "CC", "Color Color"),
idef(0x1E, "NCS", "Normal color single"),
idef(0x20, "NCT", "Normal color triple"),
idef(0x28, "SQR", "Square of vector IR"),
idef(0x29, "DCPL", "Depth Cue Color light"),
idef(0x2A, "DPCT", "Depth Cueing triple (should be fake=08h, but isn't)"),
idef(0x2D, "AVSZ3", "Average of three Z values"),
idef(0x2E, "AVSZ4", "Average of four Z values"),
idef(0x30, "RTPT", "Perspective Transformation triple"),
idef(0x3D, "GPF", "General purpose interpolation"),
idef(0x3E, "GPL", "General purpose interpolation with base"),
idef(0x3F, "NCCT", "Normal Color Color triple vector"),
]
self.DATA_REG = 0
self.CTRL_REG = 1
def ev_ana_insn(self, insn):
dword = ida_bytes.get_wide_dword(insn.ea)
if (dword >> 0x19 == 0x25):
opcode = dword & 0x3F
for i in range(0, len(self.itable)):
if (self.itable[i].opcode == opcode):
insn.itype = ITYPE_START + i
insn.Op1.type = ida_ua.o_void
insn.size = 4
return insn.size
return 0
def ev_get_autocmt(self, insn):
if (insn.itype >= ITYPE_START and insn.itype < ITYPE_START + len(self.itable)):
print(self.itable[insn.itype-ITYPE_START].cmt)
return self.itable[insn.itype-ITYPE_START].cmt
#return 0
def ev_emu_insn(self, insn):
if (insn.itype >= ITYPE_START and insn.itype < ITYPE_START + len(self.itable)):
insn.add_cref(insn.ea + insn.size, 0, 21); # 21 Ordinary flow
return 1
return 0
def get_register(self, op, ctx):
ctrl_regs = ["RT11RT12", "RT13RT21", "RT22RT23", "RT31RT32", "RT33", "TRX", "TRY", "TRZ",
"LM11LM12", "LM13LM21", "LM22LM23", "LM31LM32", "LM33", "RBK", "BBK", "GBK",
"LR1LR2", "LR3LG1", "LG2LG3", "LB1LB2", "LB3", "RFC", "GFC", "BFC",
"OFX", "OFY", "H", "DQA", "DQB", "ZSF3", "ZSF4", "FLAG"]
data_regs = ["VXY0", "VZ0", "VXY1", "VZ1", "VXY2", "VZ2" ,"RGBC" ,"OTZ" ,"IR0", "IR1",
"IR2", "IR3", "SXY0", "SXY1", "SXY2", "SXYP", "SZ0", "SZ1", "SZ2", "SZ3",
"RGB0", "RGB1", "RGB2", "(RES1)", "MAC0", "MAC1", "MAC2",
"MAC3", "IRGB", "ORGB", "LZCS", "LZCR"]
if (op.specval == self.DATA_REG):
return data_regs[op.reg].lower()
elif (op.specval == self.CTRL_REG):
return ctrl_regs[op.reg].lower()
else:
return "UNK"
def decode_sf_lm(self, dword):
sf = (dword >> 19) & 1
lm = (dword >> 10) & 1
return "sf={:d}, lm={:d}".format(sf, lm)
def decode_mvmva_bits(self, dword):
sf = (dword >> 19) & 1
mm = (dword >> 17) & 3
mv = (dword >> 15) & 3
tv = (dword >> 13) & 3
lm = (dword >> 10) & 1
return "sf={:d}, mm={:d}, mv={:d}, tv={:d}, lm={:d}".format(sf, mm, mv, tv, lm)
def ev_out_operand(self, ctx, op):
if (ctx.insn.itype >= ITYPE_START and ctx.insn.itype < ITYPE_START + len(self.itable) and op.n == 0):
if (self.itable[ctx.insn.itype - ITYPE_START].name.lower() == "mvmva"):
s=self.decode_mvmva_bits(ida_bytes.get_wide_dword(ctx.insn.ea))
ctx.out_line(s, 4)
return 1
else:
s=self.decode_sf_lm(ida_bytes.get_wide_dword(ctx.insn.ea))
ctx.out_line(s, 4)
return 1
elif (op.type == ida_ua.o_idpspec1 and op.reg < 32):
# First we need to fix instructions (badly) disassembled by mips.dll
if (ctx.insn.itype == ida_allins.MIPS_cfc2 and op.n == 1):
op.specval = self.CTRL_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype == ida_allins.MIPS_ctc2 and op.n == 1):
op.specval = self.CTRL_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype == ida_allins.MIPS_mtc2 and op.n == 1):
op.specval = self.DATA_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype == ida_allins.MIPS_mfc2 and op.n == 1):
op.specval = self.DATA_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype == ida_allins.MIPS_lwc2 and op.n == 0):
op.specval = self.DATA_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype == ida_allins.MIPS_swc2 and op.n == 0):
op.specval = self.DATA_REG
ctx.out_register(self.get_register(op, ctx))
elif (ctx.insn.itype >= ITYPE_START and ctx.insn.itype < ITYPE_START + len(self.itable)):
ctx.out_register(self.get_register(op, ctx))
else:
return 0
return 1
return 0
def ev_out_mnem(self, ctx):
if (ctx.insn.itype >= ITYPE_START and ctx.insn.itype < ITYPE_START + len(self.itable)):
ctx.out_custom_mnem(self.itable[ctx.insn.itype - ITYPE_START].name.lower(), MNEM_WIDTH, "")
return 1
# We do this to fix width of other instructions
ctx.out_mnem(MNEM_WIDTH)
return 1
class gte_plugin_t(idaapi.plugin_t):
flags = idaapi.PLUGIN_UNL
comment = ""
help = ""
wanted_name = "PSX GTE CP2 instructions disassembler"
wanted_hotkey = ""
def __init__(self):
self.gte = None
def init(self):
if (ida_ida.inf_get_procname() == 'mipsl'):
self.gte = GTE_disassemble()
self.gte.hook()
print("PSX GTE CP2 instructions disassembler loaded.")
return idaapi.PLUGIN_KEEP
return idaapi.PLUGIN_SKIP
def run(self, arg):
pass
def term(self):
if (self.gte != None):
self.gte.unhook()
self.gte = None
def PLUGIN_ENTRY():
return gte_plugin_t()