Winbagility 1 year ago
parent
commit
10157fc689
2 changed files with 368 additions and 0 deletions
  1. 0
    0
      startServer.bat
  2. 368
    0
      tools/disassemble.py

start.bat → startServer.bat View File


+ 368
- 0
tools/disassemble.py View File

@@ -0,0 +1,368 @@
1
+from struct import *
2
+
3
+
4
+
5
+condition_table_true = ["lt", "gt", "eq"]
6
+condition_table_false = ["ge", "le", "ne"]
7
+trap_condition_table = {
8
+    1: "lgt",
9
+    2: "llt",
10
+    4: "eq",
11
+    5: "lge",
12
+    8: "gt",
13
+    12: "ge",
14
+    16: "lt",
15
+    20: "le",
16
+    31: "u"
17
+}
18
+
19
+spr_table = {
20
+    8: "lr",
21
+    9: "ctr"
22
+}
23
+
24
+def decodeI(value):
25
+    return (value >> 2) & 0xFFFFFF, (value >> 1) & 1, value & 1
26
+
27
+def decodeB(value):
28
+    return (value >> 21) & 0x1F, (value >> 16) & 0x1F, (value >> 2) & 0x3FFF, (value >> 1) & 1, value & 1
29
+
30
+def decodeD(value):
31
+    return (value >> 21) & 0x1F, (value >> 16) & 0x1F, value & 0xFFFF
32
+
33
+def decodeX(value):
34
+    return (value >> 21) & 0x1F, (value >> 16) & 0x1F, (value >> 11) & 0x1F, (value >> 1) & 0x3FF, value & 1
35
+
36
+def extend_sign(value, bits=16):
37
+    if value & 1 << (bits - 1):
38
+        value -= 1 << bits
39
+    return value
40
+
41
+def ihex(value):
42
+    return "-" * (value < 0) + "0x" + hex(value).lstrip("-0x").rstrip("L").zfill(1).upper()
43
+
44
+def decodeCond(BO, BI):
45
+    #TODO: Better condition code
46
+    if BO == 20: return ""
47
+    if BO & 1: return "?"
48
+    if BI > 2: return "?"
49
+    if BO == 4: return condition_table_false[BI]
50
+    if BO == 12: return condition_table_true[BI]
51
+    return "?"
52
+
53
+def loadStore(value, regtype="r"):
54
+    D, A, d = decodeD(value)
55
+    d = extend_sign(d)
56
+    return "%s%i, %s(r%i)" %(regtype, D, ihex(d), A)
57
+
58
+def loadStoreX(D, A, B, pad):
59
+    if pad: return "<invalid>"
60
+    return "r%i, %s, r%i" %(D, ("r%i" %A) if A else "0", B)
61
+
62
+def add(D, A, B, Rc):
63
+    return "add%s" %("." * Rc), "r%i, r%i, r%i" %(D, A, B)
64
+
65
+def addi(value, addr):
66
+    D, A, SIMM = decodeD(value)
67
+    SIMM = extend_sign(SIMM)
68
+    if A == 0:
69
+        return "li", "r%i, %s" %(D, ihex(SIMM))
70
+    return "addi", "r%i, r%i, %s" %(D, A, ihex(SIMM))
71
+
72
+def addic(value, addr):
73
+    D, A, SIMM = decodeD(value)
74
+    SIMM = extend_sign(SIMM)
75
+    return "addic", "r%i, r%i, %s" %(D, A, ihex(SIMM))
76
+
77
+def addic_(value, addr):
78
+    D, A, SIMM = decodeD(value)
79
+    SIMM = extend_sign(SIMM)
80
+    return "addic.", "r%i, r%i, %s" %(D, A, ihex(SIMM))
81
+
82
+def addis(value, addr):
83
+    D, A, SIMM = decodeD(value)
84
+    SIMM = extend_sign(SIMM)
85
+    if A == 0:
86
+        return "lis", "r%i, %s" %(D, ihex(SIMM))
87
+    return "addis", "r%i, r%i, %s" %(D, A, ihex(SIMM))
88
+
89
+def and_(S, A, B, Rc):
90
+    return "and%s" % ("." * Rc), "r%i, r%i, r%i" % (A, S, B)
91
+
92
+def b(value, addr):
93
+    LI, AA, LK = decodeI(value)
94
+    LI = extend_sign(LI, 24) * 4
95
+    if AA:
96
+        dst = LI
97
+    else:
98
+        dst = addr + LI
99
+    return "b%s%s" %("l" * LK, "a" * AA), ihex(dst)
100
+
101
+def bc(value, addr):
102
+    BO, BI, BD, AA, LK = decodeB(value)
103
+    LI = extend_sign(LK, 14) * 4
104
+    instr = "b" + decodeCond(BO, BI)
105
+    if LK: instr += "l"
106
+    if AA:
107
+        instr += "a"
108
+        dst = LI
109
+    else:
110
+        dst = addr + LI
111
+    return instr, ihex(dst)
112
+
113
+def bcctr(BO, BI, pad, LK):
114
+    if pad: return "<invalid>"
115
+    instr = "b" + decodeCond(BO, BI) + "ctr"
116
+    if LK:
117
+        instr += "l"
118
+    return instr
119
+
120
+def bclr(BO, BI, pad, LK):
121
+    if pad: return "<invalid>"
122
+    instr = "b" + decodeCond(BO, BI) + "lr"
123
+    if LK:
124
+        instr += "l"
125
+    return instr
126
+
127
+def cmp(cr, A, B, pad):
128
+    if pad: return "<invalid>"
129
+    if cr & 3:
130
+        return "<invalid>"
131
+    return "cmp", "cr%i, r%i, r%i" %(cr >> 2, A, B)
132
+
133
+def cmpi(value, addr):
134
+    cr, A, SIMM = decodeD(value)
135
+    SIMM = extend_sign(SIMM)
136
+    if cr & 3:
137
+        return "<invalid>"
138
+    return "cmpwi", "cr%i, r%i, %s" %(cr >> 2, A, ihex(SIMM))
139
+
140
+def cmpl(cr, A, B, pad):
141
+    if pad: return "<invalid>"
142
+    if cr & 3:
143
+        return "<invalid>"
144
+    return "cmplw", "cr%i, r%i, r%i" %(cr >> 2, A, B)
145
+
146
+def cmpli(value, addr):
147
+    cr, A, UIMM = decodeD(value)
148
+    if cr & 3:
149
+        return "<invalid>"
150
+    return "cmplwi", "cr%i, r%i, %s" %(cr >> 2, A, ihex(UIMM))
151
+
152
+def cntlzw(S, A, pad, Rc):
153
+    if pad: return "<invalid>"
154
+    return "cntlzw%s" %("." * Rc), "r%i, r%i" %(A, S)
155
+
156
+def dcbst(pad1, A, B, pad2):
157
+    if pad1 or pad2: return "<invalid>"
158
+    return "dcbst", "r%i, r%i" %(A, B)
159
+
160
+def fmr(D, pad, B, Rc):
161
+    if pad: return "<invalid>"
162
+    return "fmr%s" %("." * Rc), "f%i, f%i" %(D, B)
163
+
164
+def fneg(D, pad, B, Rc):
165
+    if pad: return "<invalid>"
166
+    return "fneg%s" %("." * Rc), "f%i, f%i" %(D, B)
167
+
168
+def mfspr(D, sprLo, sprHi, pad):
169
+    if pad: return "<invalid>"
170
+    sprnum = (sprHi << 5) | sprLo
171
+    if sprnum not in spr_table:
172
+        spr = "?"
173
+    else:
174
+        spr = spr_table[sprnum]
175
+    return "mf%s" %spr, "r%i" %D
176
+
177
+def mtspr(S, sprLo, sprHi, pad):
178
+    if pad: return "<invalid>"
179
+    sprnum = (sprHi << 5) | sprLo
180
+    if sprnum not in spr_table:
181
+        spr = ihex(sprnum)
182
+    else:
183
+        spr = spr_table[sprnum]
184
+    return "mt%s" %spr, "r%i" %S
185
+
186
+def lbz(value, addr): return "lbz", loadStore(value)
187
+def lfd(value, addr): return "lfd", loadStore(value, "f")
188
+def lfs(value, addr): return "lfs", loadStore(value, "f")
189
+def lmw(value, addr): return "lmw", loadStore(value)
190
+def lwz(value, addr): return "lwz", loadStore(value)
191
+def lwzu(value, addr): return "lwzu", loadStore(value)
192
+def lwarx(D, A, B, pad): return "lwarx", loadStoreX(D, A, B, pad)
193
+def lwzx(D, A, B, pad): return "lwzx", loadStoreX(D, A, B, pad)
194
+
195
+def or_(S, A, B, Rc):
196
+    if S == B:
197
+        return "mr%s" %("." * Rc), "r%i, r%i" %(A, S)
198
+    return "or%s" %("." * Rc), "r%i, r%i, r%i" %(A, S, B)
199
+
200
+def ori(value, addr):
201
+    S, A, UIMM = decodeD(value)
202
+    if UIMM == 0:
203
+        return "nop"
204
+    return "ori", "r%s, r%s, %s" %(A, S, ihex(UIMM))
205
+
206
+def oris(value, addr):
207
+    S, A, UIMM = decodeD(value)
208
+    return "oris", "r%s, r%s, %s" %(A, S, ihex(UIMM))
209
+
210
+def rlwinm(value, addr):
211
+    S, A, SH, M, Rc = decodeX(value)
212
+    MB = M >> 5
213
+    ME = M & 0x1F
214
+    dot = "." * Rc
215
+    if SH == 0 and MB == 0 and ME == 31:
216
+        return "nop"
217
+    if MB == 0 and ME == 31 - SH:
218
+        return "slwi%s" %dot, "r%i, r%i, %i" %(A, S, SH)
219
+    if ME == 31 and SH == 32 - MB:
220
+        return "srwi%s" %dot, "r%i, r%i, %i" %(A, S, MB)
221
+    if MB == 0 and ME < 31:
222
+        return "extlwi%s" %dot, "r%i, r%i, %i,%i" %(A, S, ME + 1, SH)
223
+    #extrwi
224
+    if MB == 0 and ME == 31:
225
+        if SH >= 16:
226
+            return "rotlwi%s" %dot, "r%i, r%i, %i" %(A, S, SH)
227
+        return "rotrwi%s" %dot, "r%i, r%i, %i" %(A, S, 32 - SH)
228
+    if SH == 0 and ME == 31:
229
+        return "clrlwi%s" %dot, "r%i, r%i, %i" %(A, S, MB)
230
+    if SH == 0 and MB == 0:
231
+        return "clrrwi%s" %dot, "r%i, r%i, %i" %(A, S, 31 - ME)
232
+    #clrlslwi
233
+    return "rlwinm%s" %dot, "r%i, r%i, %i,%i,%i" %(A, S, SH, MB, ME)
234
+
235
+def sc(value, addr):
236
+    if value & 0x3FFFFFF != 2:
237
+        return "<invalid>"
238
+    return "sc"
239
+
240
+def stb(value, addr): return "stb", loadStore(value)
241
+def stfd(value, addr): return "stfd", loadStore(value, "f")
242
+def stfs(value, addr): return "stfs", loadStore(value, "f")
243
+def stfsu(value, addr): return "stfsu", loadStore(value, "f")
244
+def stmw(value, addr): return "stmw", loadStore(value)
245
+def stw(value, addr): return "stw", loadStore(value)
246
+def stwu(value, addr): return "stwu", loadStore(value)
247
+def stbx(S, A, B, pad): return "stbx", loadStoreX(S, A, B, pad)
248
+def stwx(S, A, B, pad): return "stwx", loadStoreX(S, A, B, pad)
249
+def stwcx(S, A, B, pad): return "stwcx", loadStoreX(S, A, B, pad ^ 1)
250
+
251
+def tw(TO, A, B, pad):
252
+    if pad: return "<invalid>"
253
+    if TO == 31 and A == 0 and B == 0:
254
+        return "trap"
255
+
256
+    if TO not in trap_condition_table:
257
+        condition = "?"
258
+    else:
259
+        condition = trap_condition_table[TO]
260
+    return "tw%s" %condition, "r%i, r%i" %(A, B)
261
+
262
+opcode_table_ext1 = {
263
+    16: bclr,
264
+    528: bcctr
265
+}
266
+
267
+opcode_table_ext2 = {
268
+    0: cmp,
269
+    4: tw,
270
+    20: lwarx,
271
+    23: lwzx,
272
+    26: cntlzw,
273
+    28: and_,
274
+    32: cmpl,
275
+    54: dcbst,
276
+    150: stwcx,
277
+    151: stwx,
278
+    215: stbx,
279
+    266: add,
280
+    339: mfspr,
281
+    444: or_,
282
+    467: mtspr
283
+}
284
+
285
+opcode_table_float_ext1 = {
286
+    40: fneg,
287
+    72: fmr
288
+}
289
+
290
+def ext1(value, addr):
291
+    DS, A, B, XO, Rc = decodeX(value)
292
+    if not XO in opcode_table_ext1:
293
+        return "ext1 - %s" %bin(XO)
294
+    return opcode_table_ext1[XO](DS, A, B, Rc)
295
+
296
+def ext2(value, addr):
297
+    DS, A, B, XO, Rc = decodeX(value)
298
+    if not XO in opcode_table_ext2:
299
+        return "ext2 - %s" %bin(XO)
300
+    return opcode_table_ext2[XO](DS, A, B, Rc)
301
+
302
+def float_ext1(value, addr):
303
+    D, A, B, XO, Rc = decodeX(value)
304
+    if not XO in opcode_table_float_ext1:
305
+        return "float_ext1 - %s" %bin(XO)
306
+    return opcode_table_float_ext1[XO](D, A, B, Rc)
307
+
308
+opcode_table = {
309
+    10: cmpli,
310
+    11: cmpi,
311
+    12: addic,
312
+    13: addic_,
313
+    14: addi,
314
+    15: addis,
315
+    16: bc,
316
+    17: sc,
317
+    18: b,
318
+    19: ext1,
319
+    21: rlwinm,
320
+    24: ori,
321
+    25: oris,
322
+    31: ext2,
323
+    32: lwz,
324
+    33: lwzu,
325
+    34: lbz,
326
+    36: stw,
327
+    37: stwu,
328
+    38: stb,
329
+    46: lmw,
330
+    47: stmw,
331
+    48: lfs,
332
+    50: lfd,
333
+    52: stfs,
334
+    53: stfsu,
335
+    54: stfd,
336
+    63: float_ext1
337
+}
338
+
339
+def disassemble(value, address):
340
+    opcode = value >> 26
341
+    if opcode not in opcode_table:
342
+        return "???"
343
+    instr = opcode_table[opcode](value, address)
344
+    if type(instr) == str:
345
+        return instr
346
+    return instr[0] + " " * (10 - len(instr[0])) + instr[1]
347
+    
348
+def disassembleFile(file):
349
+    f = open(file, "rb")
350
+    add = 0x01000000
351
+    old = ["", "", "","","",""]
352
+    while True:
353
+        opcode = unpack(">I", f.read(4))[0]
354
+        res = disassemble(opcode, add)
355
+        #print "%08x : " % (add),
356
+        #print res
357
+        for i in range(len(old)-1):
358
+            old[i] = old[i+1]
359
+        old[len(old)-1] = res
360
+        if res == "blr":
361
+            print "%08x : " % (add - (len(old)*4)),
362
+            for i in range(len(old)):
363
+                print "%s ; " % (old[i]),
364
+            print ""
365
+        add+=4
366
+    f.close()
367
+
368
+disassembleFile("DUMP0E.bin")