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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
local function rd(self, addr)
self.fin:seek(addr * 4)
return self.fin:readU32()
end
local function rdn(self)
local r = self:rd(self.PC)
self.PC = self.PC + 1
return r
end
local opcodes_names = {
-- 0x.0 0x.1 0x.2 0x.3 0x.4 0x.5 0x.6 0x.7 0x.8 0x.9 0x.a 0x.b 0x.c 0x.d 0x.e 0x.f
"SETV", "SETV", nil, nil, nil, nil, nil, nil, "MULG0","ADDG0","PSH", nil, nil, "PSH1?",nil, -- 0x0.
nil, nil, "PSH2?","PSHI", "CALL", "JMP", "JMPZ", "RET", "SETD", "PSH3?",nil, nil, nil, nil, nil, nil, -- 0x1.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x2.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x3.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x4.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x5.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x6.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0x7.
"STTRG",nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "SSTOP",nil, nil, nil, -- 0x8.
nil, nil, "TBOX", "TBOX2",nil, nil, nil, nil, "TBOX3",nil, nil, nil, nil, nil, "SETRM","CHGRM",-- 0x9.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xa.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xb.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xc.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xd.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xe.
nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, -- 0xf.
}
local opcodes_imms = {
-- 0 1 2 3 4 5 6 7 8 9 a b c d e f
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- 0
0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, -- 1
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, -- 2
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, -- 3
0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- 4
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- 5
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- 6
0, 2, 1, 2, 0, 1, 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0, 0, -- b
2, 0, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, -- c
0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 0, 0, 1, 0, 0, -- d
1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, -- e
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -- f
}
local opcodes_known = {
-- 0 1 2 3 4 5 6 7 8 9 a b c d e f
true, true, false, false, false, false, false, false, true, true, true, false, false, true, false, -- 0
false, false, true, true, true, true, true, true, true, true, true, false, false, false, false, false, -- 1
false, true, true, true, true, true, true, true, true, true, true, false, false, false, true, false, -- 2
false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, true, -- 3
false, true, true, true, true, true, false, false, false, true, false, false, false, false, false, false, -- 4
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 5
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 6
true, true, true, true, true, true, true, false, true, false, false, true, true, false, true, false, -- 7
true, true, true, true, false, true, true, true, true, true, true, false, true, true, true, true, -- 8
false, true, true, true, true, false, false, false, true, false, false, false, false, true, true, true, -- 9
true, true, true, true, false, false, false, false, true, true, false, true, false, false, false, false, -- a
true, false, true, false, false, false, false, false, true, false, false, true, false, false, true, false, -- b
true, false, true, false, false, false, false, false, false, true, false, false, false, false, false, false, -- c
false, false, false, false, false, false, false, false, false, true, true, false, false, true, true, false, -- d
true, false, false, false, false, false, true, false, false, false, false, true, false, false, false, false, -- e
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- f
}
local opcodes_fromstack = {
-- 0 1 2 3 4 5 6 7 8 9 a b c d e f
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 0
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 1
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 2
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 3
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 4
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 5
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- 6
false, true, true, true, false, true, false, false, false, false, false, false, false, false, false, false, -- 7
true, true, true, true, false, true, true, true, true, false, true, false, true, false, true, true, -- 8
false, false, true, true, true, false, false, false, true, false, false, false, false, true, true, true, -- 9
true, true, true, true, false, false, false, false, true, true, false, true, false, false, false, false, -- a
false, false, true, false, false, false, false, false, false, false, false, true, false, false, false, false, -- b
true, false, true, false, false, false, false, false, false, true, false, false, false, false, false, false, -- c
false, false, false, false, false, false, false, false, false, true, true, false, false, true, false, false, -- d
false, false, false, false, false, false, true, false, false, false, false, true, false, false, false, false, -- e
false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, -- f
}
local function disasm_write(self, ...)
if self.out then self.out:write(...) end
end
local function disasm_opcode(self)
self.oPC = self.PC
self.opcode = self:rdn()
self.code = shr(self.opcode, 24)
self.imm8 = andB(self.opcode, 0xff)
self.imm16 = andB(self.opcode, 0xffff)
self.imm23 = andB(self.opcode, 0x7fffff)
self.high8 = andB(shr(self.opcode, 8), 0xff)
self.idx2 = shr(shl(self.opcode, 9), 12)
self.flags = {}
self.flags.is_local = andB(self.opcode, 0x800000) ~= 0
self.flags.from_stack = andB(self.opcode, 0x100000) ~= 0
self:write(string.format("%08X %08X ", self.oPC, self.opcode))
if self.code == 0 then
self:write("OPCODE00?!")
return
end
local name = opcodes_names[self.code]
if name then
self:write(string.format("%-8s", name))
elseif opcodes_known[self.code] then
self:write(string.format("OP_%02X ", self.code))
else
self:write(string.format("UNK%02X ", self.code))
end
if self.flags.from_stack and opcodes_fromstack[self.code] then
for i = 1, opcodes_nparams[self.code] do
self:write("POP()")
if i ~= opcodes_nparams[self.code] then self:write(", ") end
end
else
local nargs = opcodes_nparams[self.code]
local sv = opcodes_self_vals[self.code]
if false then
-- 0 = nothing
-- 1 = low8
-- 2 = low16
-- 3 = low23
-- 4 = high8
-- 5 = var_idx
-- 6 = var_idx2
-- 7 = low8 + high8
elseif sv == 1 then
self:write(string.format("%02X", self.imm8))
nargs = nargs - 1
elseif sv == 2 then
self:write(string.format("%04X", self.imm16))
nargs = nargs - 1
elseif sv == 3 then
self:write(string.format("%08X", self.imm23))
nargs = nargs - 1
elseif sv == 4 then
self:write(string.format("%02X", self.high8))
nargs = nargs - 1
elseif sv == 5 then
self:write(string.format("%s[%04X]", self.flags.is_local and "locl" or "glbl", self.imm16))
nargs = nargs - 1
elseif sv == 6 then
self:write(string.format("%s[%04X]", self.flags.is_local and "locl" or "glbl", self.idx2))
nargs = nargs - 1
elseif sv == 7 then
self:write(string.format("%02X, %02X", self.imm8, self.high8))
nargs = nargs - 2
end
if sv ~= 0 and nargs ~= 0 then
self:write(", ")
end
for i = 1, opcodes_imms[self.code] do
self:write(string.format("%08X", self:rdn()))
if i ~= opcodes_imms[self.code] then self:write(", ") end
nargs = nargs - 1
end
if not opcodes_fromstack[self.code] then
for i = 1, nargs do
self:write("POP()")
if i ~= nargs then self:write(", ") end
end
end
end
if self.hooks[self.code] then self.hooks[self.code](self.oPC) end
end
local function disasm(self)
while self.PC ~= self.EPC do
self:disasm_opcode()
self:write("\n")
for padPC = self.oPC + 1, self.PC - 1 do
self:write(string.format(" %08X\n", self:rd(padPC)))
end
end
end
function logic_disasm(logic, o, hooks)
local dobj = { rd = rd, rdn = rdn, disasm_opcode = disasm_opcode, disasm = disasm, PC = 0, fin = logic, out = o, EPC = logic:getsize() / 4, hooks = hooks, write = disasm_write }
dobj:disasm()
end
|