-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.lua
executable file
·50 lines (49 loc) · 1.77 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
local memory = setmetatable({
memory_map = {},
ram = false, -- TODO very ugly, just for checking initialized memory
breakpoint = {
address = nil,
read = false,
write = false
},
connect = function(self, startAddress, module)
self.memory_map[{
startAddress,
module.size
}] = module
-- TODO ugly hack
if startAddress == 0 then
self.ram = module
end
end
}, {
__index = function(self, address)
if address == "initialized" then
return self.ram.initialized
end
if address == self.breakpoint.address and self.breakpoint.read then
self.cpu.pause = true
end
for addresses, module in pairs(self.memory_map) do
if address >= addresses[1] and address < addresses[1] + addresses[2] then
return module[address - addresses[1]]
end
end
--print("Attempted read at unmapped address " .. string.format("%04X", address))
return 0
end,
__newindex = function(self, address, newValue)
if address == self.breakpoint.address and self.breakpoint.write then
self.cpu.pause = true
end
for addresses, module in pairs(self.memory_map) do
if address >= addresses[1] and address < addresses[1] + addresses[2] then
module[address - addresses[1]] = newValue
return
end
end
--print("Attempted write at unmapped address " .. string.format("%04X", address) .. " with value " .. newValue)
end,
__len = function(self) return self.eprom.startAddress + self.eprom.size end -- LuaJIT doesn't support this? :(
})
return memory