-
Notifications
You must be signed in to change notification settings - Fork 2
/
memlib.lua
494 lines (423 loc) · 11.1 KB
/
memlib.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
-- Memlib
-- Composable memory blocks with different backends.
-- Basic
local _M = {
backend = {}
}
local bitops = require("bitops")
local band, bor, blshift, brshift = bitops.band, bitops.bor, bitops.lshift, bitops.rshift
local strbyte, strsub, strfind, strchar = string.byte, string.sub, string.find, string.char
-- Helpers.
local function and32(v, addr)
if addr then return band(v, 0xFFFFFFFC) end
return band(v, 0xFFFFFFFF)
end
local function comb4b_be(a, b, c, d)
return bor(bor(blshift(a, 24), blshift(b, 16)), bor(blshift(c, 8), d))
end
local function dis32(v)
local a = band(brshift(v, 24), 0xFF)
local b = band(brshift(v, 16), 0xFF)
local c = band(brshift(v, 8), 0xFF)
local d = band(v, 0xFF)
return a, b, c, d
end
-- Backend implementation "shortcuts"
local function get32_get(memory, i)
local a = memory:get(i)
local b = memory:get(i+1)
local c = memory:get(i+2)
local d = memory:get(i+3)
return comb4b_be(a, b, c, d)
end
local function get_get32(memory, i)
local a, _, _, _ = memory:get32(i)
return a
end
local function set32_set(memory, i, v)
local a, b, c, d = dis32(v)
memory:set(i, a)
memory:set(and32(i + 1), b)
memory:set(and32(i + 2), c)
memory:set(and32(i + 3), d)
end
local function set_set32(memory, i, v)
local a, b, c, d = memory:get32(i)
memory:set32(i, comb4b_be(v, b, c, d))
end
function strreplace(str, pos, r)
local n = #r
return strsub(str, 1, pos-n) .. r .. strsub(str, pos+n)
end
-- Public Helpers
function _M.new(driver, ...)
local drv = _M.backend[driver]
if drv then
return drv(...)
end
error("No such driver: "..tostring(driver), 1)
end
function _M.copy(mem1, mem2)
for i=0, mem1.size do
mem2:set(i, mem1:get(i))
end
end
function _M.copyto(memsrc, addroff, memdst)
for i=0, memsrc.size do
local v = memsrc:get(i)
memdst:set(addroff + i, v)
end
end
-- Memory "Backends", where things are actually stored.
-- Simplistic table backend.
-- Loads of overhead. But works. And is okay fast.
local function setifeanz(t, k, v)
if v == 0 then
if t[k] then
t[k] = nil
end
else
t[k] = v
end
end
local fns_tbackend = {
get32be = function(memory, i)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
local a = memory[i] or 0
local b = memory[and32(i + 1)] or 0
local c = memory[and32(i + 2)] or 0
local d = memory[and32(i + 3)] or 0
return comb4b_be(a, b, c, d)
end,
set32be = function(memory, i, v)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
local a = band(brshift(v, 24), 0xFF)
setifeanz(memory, i, a)
local b = band(brshift(v, 16), 0xFF)
setifeanz(memory, and32(i + 1), b)
local c = band(brshift(v, 8), 0xFF)
setifeanz(memory, and32(i + 2), c)
local d = band(v, 0xFF)
setifeanz(memory, and32(i + 3), d)
end,
get = function(memory, i)
if (memory.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
return memory[i] or 0
end,
set = function(memory, i, v)
if (memory.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
setifeanz(memory, i, v)
end
}
function _M.backend.table(memsz, prealloc)
local memory = {
get32be = fns_tbackend.get32be,
set32be = fns_tbackend.set32be,
set = fns_tbackend.set,
get = fns_tbackend.get,
size = memsz
}
if prealloc then
for i = 0, memsz do
memory[i] = 0
end
end
return memory
end
-- Simple read-only string backend.
-- Incapable of writing, but efficient for readonly operation!
local function rostr_werr()
error("rostring memory backend is incapable of writing.")
end
-- Returns two values:
-- 1. The last non-NULL memory index, or -1 if the memory is empty.
-- 2. The stripped string.
local function rostr_striptrailingnulls(str)
local l2, _ = strfind(str, "%z+$")
if not l2 then return str end
return strsub(str, 1, l2 - 1)
end
local fns_rostring = {
get = function(memory, i)
if (memory.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
return strbyte(memory.str, i + 1) or 0
end,
get32be = function(memory, i)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
local a, b, c, d = strbyte(memory.str, i + 1, i + 4)
return comb4b_be(a or 0, b or 0, c or 0, d or 0)
end,
}
function _M.backend.rostring(string, memsz)
local size = memsz or #string
string = rostr_striptrailingnulls(string)
return {
-- Don't go changing any ofthese.
str = string,
size = size,
get = fns_rostring.get,
get32be = fns_rostring.get32be,
-- Incapable of writing
set = rostr_werr,
set32be = rostr_werr,
}
end
-- Read/Write overlay for existing memory backend.
-- Useful for ROM/RAM.
local function rwovl_read(romem, ovlt, i)
local oval = ovlt[i]
if oval then return oval end
if romem.size >= i then return romem:get(i) end
return 0
end
local fns_rwovl = {
get32be = function(memory, i)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
local a = rwovl_read(memory.romem, memory, i)
local b = rwovl_read(memory.romem, memory, and32(i + 1))
local c = rwovl_read(memory.romem, memory, and32(i + 2))
local d = rwovl_read(memory.romem, memory, and32(i + 3))
return comb4b_be(a, b, c, d)
end,
set32be = function(memory, i, v)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
memory[i] = band(brshift(v, 24), 0xFF)
memory[and32(i + 1)] = band(brshift(v, 16), 0xFF)
memory[and32(i + 2)] = band(brshift(v, 8), 0xFF)
memory[and32(i + 3)] = band(v, 0xFF)
end,
get = function(memory, i)
if (memory.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
return rwovl_read(memory.romem, memory, i)
end,
set = function(memory, i, v)
if (memory.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
memory[i] = v
end
}
function _M.backend.rwoverlay(existing_mem, memsz)
return {
-- Don't go changing any of these.
romem = existing_mem,
get32be = fns_rwovl.get32be,
set32be = fns_rwovl.set32be,
set = fns_rwovl.set,
get = fns_rwovl.get,
size = memsz or existing_mem.size,
}
end
-- Read/Write overlay for existing memory backend. 32bit read version.
-- Useful for ROM/RAM.
local fns_rwovl32 = {
get32be = function(memory, i)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
local romem = memory.romem
local oval = memory[i / 4]
if oval then return oval end
if romem.size >= i then return romem:get32be(i) end
return 0
end,
set32be = function(memory, i, v)
if ((memory.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
memory[i / 4] = v
end,
errorout = function()
error("memlib: rwoverlay32 doesn't support byte accesses.")
end
}
function _M.backend.rwoverlay32(existing_mem, memsz)
return {
-- Don't go changing any of these.
romem = existing_mem,
get32be = fns_rwovl32.get32be,
set32be = fns_rwovl32.set32be,
set = fns_rwovl32.errorout,
get = fns_rwovl32.errorout,
size = memsz or existing_mem.size,
}
end
-- Rather simple cached file backend.
-- Little fancy, not much.
-- Warning: May not write back data unless one reads data after it.
local function file_getcached(s, i, n)
local blksz = s.blksz
local ind = i % blksz
local iblk = i - ind
local cblk = s.cblk
local cached = s.cached
local fh = s.fh
if s.didmod then
fh:seek("set", cblk)
fh:write(cached)
fh:flush()
s.didmod = false
end
if cblk == iblk then -- current page is the cached one
if not cached then
fh:seek("set", iblk)
cached = fh:read(blksz)
s.cached = cached
end
else -- current cache is for another block.
fh:seek("set", iblk)
cached = fh:read(blksz)
s.cached = cached
s.cblk = iblk
end
return strbyte(cached, ind+1)
end
local function file_setcached(s, i, v)
local blksz = s.blksz
local ind = i % blksz
local iblk = i - ind
local cblk = s.cblk
local cached = s.cached
local fh = s.fh
if cblk == iblk then -- current page is the cached one
fh:seek("set", iblk)
if not cached then
cached = fh:read(blksz)
s.cached = cached
end
else -- current cache is for another block.
if s.didmod then
fh:seek("set", cblk)
fh:write(cached)
fh:flush()
s.didmod = false
end
fh:seek("set", iblk)
cached = fh:read(blksz)
s.cached = cached
s.cblk = iblk
end
s.didmod = true
s.cached = strreplace(s.cached, ind+1, strchar(v))
end
local fns_file = {
get = function(self, i)
if (self.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
return file_getcached(self, i)
end,
get32be = function(self, i)
if ((self.size - 3) < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
return comb4b_be(file_getcached(self, i), file_getcached(self, i+1), file_getcached(self, i+2), file_getcached(self, i+3))
end,
set = function(self, i, v)
if (self.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
file_setcached(self, i, v)
end,
set32be = function(self, i, v)
if (self.size < i) or (0 > i) then
error("Bad Access (" .. string.format("%08x", i) .. ")")
end
fh_setcached(self, i, strchar(band(brshift(v, 24), 0xFF), band(brshift(v, 16), 0xFF), band(brshift(v, 8), 0xFF), band(v, 0xFF)))
end,
}
local function fh_size(fh)
local current = fh:seek()
local size = fh:seek("end")
fh:seek("set", current)
return size
end
function _M.backend.file(file, blksz)
local fh = file
if type(file) == "string" then
fh = assert(io.open(file, "r+b"))
end
local realsize = fh_size(fh)
return {
fh = fh,
size = realsize,
blksz = blksz or 128,
cblk = 0,
cached = nil,
didmod = false,
get = fns_file.get,
get32be = fns_file.get32be,
set = fns_file.set,
set32be = fns_file.set32be,
}
end
local function rofile_werr()
error("rofile memory backend is incapable of writing.")
end
function _M.backend.rofile(file, blksz)
local fh = file
if type(file) == "string" then
fh = assert(io.open(file, "rb"))
end
local realsize = fh_size(fh)
return {
fh = fh,
size = realsize,
blksz = blksz or 128,
cblk = 0,
cached = nil,
didmod = false,
get = fns_file.get,
get32be = fns_file.get32be,
set = rofile_werr,
set32be = rofile_werr,
}
end
-- Memory block composition.
local function run_handlers(self, method, i, v)
local addr_handler = self.addr_handlers[i]
if addr_handler then return addr_handler(self, method, i, v) end
local handlers = self.handlers
local hlen = #handlers
if hlen < 0 then
for i=1, hlen do
local res = handlers[i](self, method, i, v)
if res then return res end
end
end
return self.backend[method](self.backend, i, v)
end
function _M.compose(memory, addrhandlers, handlers)
local composed = {
backend = memory,
addr_handlers = addrhandlers or {},
handlers = handlers or {},
}
setmetatable(composed, {__index=function(this, name)
local fn = function(self, i, v)
return run_handlers(self, name, i, v)
end
rawset(this, name, fn)
return fn
end})
return composed
end
return _M