-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.lua
237 lines (204 loc) · 5.79 KB
/
memory.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
local memory = {}
local bit = require("bit")
local oo = require("oo")
local band = bit.band
local bor = bit.bor
local bnot = bit.bnot
local lshift = bit.lshift
local rshift = bit.rshift
local arshift = bit.arshift
local tobit = bit.tobit
function memory.read32(mem, addr)
local word = rshift(addr, 2)
local byte = band(addr, 3)
local val = mem[word]
if byte == 0 then
return tobit(val)
end
return bor(
lshift(memory.read16(mem, addr), 16),
memory.read16(mem, addr + 2))
end
function memory.read16(mem, addr)
local word = rshift(addr, 2)
local byte = band(addr, 3)
local val = mem[word]
if byte == 0 then
return rshift(val, 16)
elseif byte == 2 then
return band(val, 0xffff)
end
return bor(
lshift(memory.read8(mem, addr), 8),
memory.read8(mem, addr + 1))
end
function memory.read8(mem, addr)
local word = rshift(addr, 2)
local byte = band(addr, 3)
local val = mem[word]
if byte == 0 then
return rshift(val, 24)
elseif byte == 1 then
return band(rshift(val, 16), 0xff)
elseif byte == 2 then
return band(rshift(val, 8), 0xff)
else
return band(val, 0xff)
end
end
-- Sign-extend 8 bits -> 32 bits
function memory.sex8(val)
return arshift(lshift(val, 24), 24)
end
-- Sign-extend 8 bits -> 32 bits
function memory.sex16(val)
return arshift(lshift(val, 16), 16)
end
function memory.write32(mem, addr, val)
local word = rshift(addr, 2)
local byte = band(addr, 3)
if byte == 0 then
mem[word] = val
return
end
memory.write16(mem, addr, rshift(val, 16))
memory.write16(mem, addr + 2, band(val, 0xffff))
end
function memory.write16(mem, addr, val)
val = band(val, 0xffff)
local word = rshift(addr, 2)
local byte = band(addr, 3)
if byte == 0 then
mem[word] = bor(
lshift(val, 16),
band(mem[word], 0xffff))
return
elseif byte == 2 then
mem[word] = bor(
band(mem[word], 0xffff0000),
band(val, 0xffff))
return
end
memory.write8(mem, addr, rshift(val, 8))
memory.write8(mem, addr + 1, band(val, 0xff))
end
function memory.write8(mem, addr, val)
val = band(val, 0xff)
local word = rshift(addr, 2)
local byte = band(addr, 3)
if byte == 0 then
mem[word] = bor(
lshift(val, 24),
band(mem[word], bnot(0xff000000)))
elseif byte == 1 then
mem[word] = bor(
lshift(val, 16),
band(mem[word], bnot(0x00ff0000)))
elseif byte == 2 then
mem[word] = bor(
lshift(val, 8),
band(mem[word], bnot(0x0000ff00)))
else
mem[word] = bor(
val,
band(mem[word], bnot(0xff)))
end
end
memory.Reader = oo.Class()
function memory.Reader:init(mem, addr)
self._g = mem
self._addr = addr
end
function memory.Reader:addr()
return self._addr
end
function memory.Reader:setAddr(addr)
self._addr = addr
end
function memory.Reader:peek8()
return memory.read8(self._g, self._addr)
end
function memory.Reader:peek16()
return memory.read16(self._g, self._addr)
end
function memory.Reader:peek32()
return memory.read32(self._g, self._addr)
end
function memory.Reader:read8()
self._addr = self._addr + 1
return memory.read8(self._g, self._addr - 1)
end
function memory.Reader:read8s()
return memory.sex8(self:read8())
end
function memory.Reader:read16()
self._addr = self._addr + 2
return memory.read16(self._g, self._addr - 2)
end
function memory.Reader:read16s()
return memory.sex16(self:read16())
end
function memory.Reader:read32()
self._addr = self._addr + 4
return memory.read32(self._g, self._addr - 4)
end
-- Unit test
local mock = {
[0] = 0x00112233;
[1] = 0x44556677;
}
assert(memory.read32(mock, 0) == tobit(0x00112233))
assert(memory.read32(mock, 1) == tobit(0x11223344))
assert(memory.read32(mock, 2) == tobit(0x22334455))
assert(memory.read32(mock, 3) == tobit(0x33445566))
assert(memory.read32(mock, 4) == tobit(0x44556677))
assert(memory.read16(mock, 0) == tobit(0x0011))
assert(memory.read16(mock, 1) == tobit(0x1122))
assert(memory.read16(mock, 2) == tobit(0x2233))
assert(memory.read16(mock, 3) == tobit(0x3344))
assert(memory.read16(mock, 4) == tobit(0x4455))
assert(memory.read16(mock, 5) == tobit(0x5566))
assert(memory.read16(mock, 6) == tobit(0x6677))
assert(memory.read8(mock, 0) == tobit(0x00))
assert(memory.read8(mock, 1) == tobit(0x11))
assert(memory.read8(mock, 2) == tobit(0x22))
assert(memory.read8(mock, 3) == tobit(0x33))
assert(memory.read8(mock, 4) == tobit(0x44))
assert(memory.read8(mock, 5) == tobit(0x55))
assert(memory.read8(mock, 6) == tobit(0x66))
assert(memory.read8(mock, 7) == tobit(0x77))
local r = memory.Reader(mock, 0)
assert(r:addr() == 0)
assert(r:read8() == tobit(0x00))
assert(r:addr() == 1)
assert(r:peek8() == tobit(0x11))
assert(r:peek16() == tobit(0x1122))
assert(r:peek32() == tobit(0x11223344))
assert(r:read16() == tobit(0x1122))
assert(r:read32() == tobit(0x33445566))
assert(r:read8() == tobit(0x77))
assert(r:addr() == 8)
memory.write32(mock, 0, 0xa0a1a2a3)
assert(memory.read32(mock, 0) == tobit(0xa0a1a2a3))
assert(memory.read32(mock, 4) == tobit(0x44556677))
memory.write32(mock, 1, 0xb1b2b3b4)
assert(memory.read32(mock, 0) == tobit(0xa0b1b2b3))
assert(memory.read32(mock, 4) == tobit(0xb4556677))
memory.write32(mock, 2, 0xc2c3c4c5)
assert(memory.read32(mock, 0) == tobit(0xa0b1c2c3))
assert(memory.read32(mock, 4) == tobit(0xc4c56677))
memory.write32(mock, 3, 0xd3d4d5d6)
assert(memory.read32(mock, 0) == tobit(0xa0b1c2d3))
assert(memory.read32(mock, 4) == tobit(0xd4d5d677))
memory.write32(mock, 4, 0xe4e5e6e7)
assert(memory.read32(mock, 0) == tobit(0xa0b1c2d3))
assert(memory.read32(mock, 4) == tobit(0xe4e5e6e7))
assert(memory.sex8(0x00) == 0)
assert(memory.sex8(0x7f) == 127)
assert(memory.sex8(0x80) == -128)
assert(memory.sex8(0xff) == -1)
assert(memory.sex16(0x0000) == 0)
assert(memory.sex16(0x7fff) == 32767)
assert(memory.sex16(0x8000) == -32768)
assert(memory.sex16(0xffff) == -1)
return memory