-
Notifications
You must be signed in to change notification settings - Fork 2
/
zpu_emus.lua
240 lines (205 loc) · 7.7 KB
/
zpu_emus.lua
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
-- ZPU Emulator: EMULATE implementations.
-- Quite nice speed up. Used to avoid needing crt0.5 and co.
-- Original by gamemanj, heavily tweaked/rewritten by vifino.
--
-- require this and pass the result in to the ZPU library's apply.
-- Example:
-- zpu:apply(require("zpu_emus")) -- now globally loaded.
--[[
The MIT License (MIT)
Copyright (c) 2016 Adrian Pistol
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]]
-- Debug config
-- For figuring out if there's something horribly wrong with the testcase.
local usage_trace = false
-- Place holders for bit functions
local band, bor, bxor, lshift, rshift
-- Localized functions
local mceil, mfloor = math.ceil, math.floor
-- Utils
local function a32(v)
return band(v, 0xFFFFFFFF)
end
local function sflip(v)
v = a32(v)
if band(v, 0x80000000) ~= 0 then
return v - 0x100000000
end
return v
end
local function mkbool(v)
return v and 1 or 0
end
local function advip(zpu_emu)
zpu_emu.rIP = a32(zpu_emu.rIP + 1)
end
-- getb and setb are the internal implementation of LOADB and STOREB
-- and are thus heavily endianess dependant
local function getb(zpu_emu, a)
local s = (24 - lshift(band(a, 3), 3))
local av = zpu_emu:get32(band(a, 0xFFFFFFFC))
return band(rshift(av, s), 0xFF)
end
local function setb(zpu_emu, a, v)
local s = (24 - lshift(band(a, 3), 3))
local b = bxor(lshift(0xFF, s), 0xFFFFFFFF)
local av = band(zpu_emu:get32(band(a, 0xFFFFFFFC)), b)
zpu_emu:set32(band( a, 0xFFFFFFFC), bor(av, lshift(band(v, 0xFF), s)))
end
-- geth and seth are the same but for halfwords.
-- This implementation will just mess up if it gets a certain kind of misalignment.
-- (I have no better ideas. There is no reliable way to error-escape.)
local function geth(zpu_emu, a)
local s = (24 - lshift(band(a, 3), 3))
local av = zpu_emu:get32(band(a, 0xFFFFFFFC))
return band(rshift(av, s), 0xFFFF)
end
local function seth(zpu_emu, a, v)
local s = (24 - lshift(band(a, 3), 3))
local b = bxor(lshift(0xFFFF, s), 0xFFFFFFFF)
local av = band(zpu_emu:get32(band(a, 0xFFFFFFFC)), b)
zpu_emu:set32(band(a, 0xFFFFFFFC), bor(av, lshift(band(v, 0xFFFF), s)))
end
local function eqbranch(zpu_emu, bcf)
local br = a32(zpu_emu.rIP + zpu_emu:v_pop())
if bcf(zpu_emu:v_pop()) then
zpu_emu.rIP = br
else
advip(zpu_emu)
end
end
-- Generic L/R shifter, logical-only.
local function gpi_shift(v, lShift)
if (lShift >= 32) or (lShift <= -32) then return 0 end
if lShift > 0 then return lshift(v, lShift) end
if lShift < 0 then return rshift(v, -lShift) end
end
-- Generic multifunction shifter. Should handle any case with ease.
local function gp_shift(v, lShift, arithmetic)
arithmetic = arithmetic and band(v, 0x80000000) ~= 0
v = gpi_shift(v, lShift)
if arithmetic and (lShift < 0) then
return bor(v, bxor(gpi_shift(0xFFFFFFFF, lShift), 0xFFFFFFFF))
end
return v
end
-- EMULATE building
local emulates = {}
local unused_emulates = 0
local function make_emu(id, name, code)
local unused = true
if usage_trace then
emulates[id] = {name, function(...)
if unused then
unused = false
io.stderr:write(name .. " used, " .. unused_emulates .. " to go\n")
unused_emulates = unused_emulates - 1
end
return code(...)
end}
else
emulates[id] = {name, code}
end
unused_emulates = unused_emulates + 1
end
local function make_pair(id, name, code)
make_emu(id, name, function(zpu_emu)
local a = zpu_emu:v_pop()
local b = zpu_emu:get32(zpu_emu.rSP)
zpu_emu:set32(zpu_emu.rSP, code(a, b))
advip(zpu_emu)
end)
end
-- Actual emulates!
-- Yay!
make_emu(2, "LOADH", function(zpu_emu) zpu_emu:set32(zpu_emu.rSP, geth(zpu_emu, zpu_emu:get32(zpu_emu.rSP))) advip(zpu_emu) end)
make_emu(3, "STOREH", function(zpu_emu) seth(zpu_emu, zpu_emu:v_pop(), zpu_emu:v_pop()) advip(zpu_emu) end)
make_pair(4, "LESSTHAN", function(a, b) return mkbool(sflip(a) < sflip(b)) end)
make_pair(5, "LESSTHANEQUAL", function(a, b) return mkbool(sflip(a) <= sflip(b)) end)
make_pair(6, "ULESSTHAN", function(a, b) return mkbool(a < b) end)
make_pair(7, "ULESSTHANEQUAL", function(a, b) return mkbool(a <= b) end)
make_pair(9, "SLOWMULT", function(a, b) return band(a * b, 0xFFFFFFFF) end)
make_pair(10, "LSHIFTRIGHT", function(a, b)
return gp_shift(b, -sflip(a), false)
end)
make_pair(11, "ASHIFTLEFT", function(a, b)
return gp_shift(b, sflip(a), true)
end)
make_pair(12, "ASHIFTRIGHT", function(a, b)
return gp_shift(b, -sflip(a), true)
end)
make_pair(14, "EQ", function(a, b) return mkbool(a == b) end)
make_pair(15, "NEQ", function(a, b) return mkbool(a ~= b) end)
make_emu(16, "NEQ", function(zpu_emu)
zpu_emu:set32(zpu_emu.rSP, a32(-sflip(zpu_emu:get32(zpu_emu.rSP))))
advip(zpu_emu)
end)
make_pair(17, "SUB", function(b, a) return band(a - b, 0xFFFFFFFF) end)
make_pair(18, "XOR", function(b, a) return band(bxor(a, b), 0xFFFFFFFF) end)
make_emu(19, "LOADB", function(zpu_emu) zpu_emu:set32(zpu_emu.rSP, getb(zpu_emu, zpu_emu:get32(zpu_emu.rSP))) advip(zpu_emu) end)
make_emu(20, "STOREB", function(zpu_emu)
setb(zpu_emu, zpu_emu:v_pop(), zpu_emu:v_pop())
advip(zpu_emu)
end)
local function rtz(v)
if v < 0 then return mceil(v) end
return mfloor(v)
end
local function cmod(a, b)
return a - (rtz(a / b) * b)
end
make_pair(21, "DIV", function (a, b) return a32(rtz(sflip(a) / sflip(b))) end)
make_pair(22, "MOD", function (a, b) return a32(cmod(sflip(a), sflip(b))) end)
make_emu(23, "EQBRANCH", function(zpu_emu) return eqbranch(zpu_emu, function(b) return b == 0 end) end)
make_emu(24, "NEQBRANCH", function(zpu_emu) return eqbranch(zpu_emu, function(b) return b ~= 0 end) end)
make_emu(25, "POPPCREL", function(zpu_emu) zpu_emu.rIP = band(zpu_emu.rIP + zpu_emu:v_pop(), 0xFFFFFFFF) end)
make_emu(29, "PUSHSPADD", function(zpu_emu)
zpu_emu:set32(zpu_emu.rSP, band(band(lshift(zpu_emu:get32(zpu_emu.rSP), 2), 0xFFFFFFFF) + zpu_emu.rSP, 0xFFFFFFFC))
advip(zpu_emu)
end)
make_emu(31, "CALLPCREL", function(zpu_emu)
local routine = band(zpu_emu.rIP + zpu_emu:get32(zpu_emu.rSP), 0xFFFFFFFF)
zpu_emu:set32(zpu_emu.rSP, band(zpu_emu.rIP + 1, 0xFFFFFFFF))
zpu_emu.rIP = routine
end)
-- Installation helper
local function assert_bitfn(bit, name)
assert(bit[name], "zpu_emus: Did not find function "..name.." in bitlib. We need it.")
end
local function install_bit(bl)
assert_bitfn(bl, "band") band = bl.band
assert_bitfn(bl, "bor") bor = bl.bor
assert_bitfn(bl, "bxor") bxor = bl.bxor
assert_bitfn(bl, "lshift") lshift = bl.lshift
assert_bitfn(bl, "rshift") rshift = bl.rshift
end
return function(zpu)
-- check bitlib, we need it too.
install_bit(zpu.bit32)
local old_emu = zpu.op_emulate
zpu.op_emulate = function(zpu_emu, op)
local emulate = emulates[op]
if emulate then
emulate[2](zpu_emu)
return emulate[1]
end
if usage_trace then io.stderr:write("zpu_emus: usage trace found "..op.." hasn't been written yet.") end
return old_emu(zpu_emu, op)
end
return true
end