-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRAM.lua
61 lines (47 loc) · 1.48 KB
/
RAM.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
-- This is the NES's memory
-- The NES has hardware mapped IO
-- Mapper
require("Mapper")
-- APU
require("APU")
-- PPU
require("PPU")
-- The RAM
RAM = {}
-- Program Loaded Status
-- Initializes the system memory to have no data
function initializeMemory()
for i = 1, 2049 do
RAM[i] = tonumber("0xFF")
end
end
-- Maps memory reads to the correct locations
function readMemory(address)
if tonumber(address) > tonumber("0x4018") then
return readCart(tonumber(address))
elseif tonumber(address) <= tonumber("0x1FFF") then
return RAM[tonumber(address) and tonumber("0x7FF")]
elseif tonumber(address) <= tonumber("0x3FFF") then
return readPPU(tonumber(address) and 7)
elseif tonumber("0x4000") <= tonumber(address) then
if tonumber(address) <= tonumber("0x4018") then
return readAPU((tonumber(address) - tonumber("0x4000")))
end
else
-- Open bus
end
end
-- Maps memory writes into correct locations
function writeMemory(address, data)
if tonumber(address) > tonumber("0x4018") then
writeCart(tonumber(address), tonumber(data));
elseif tonumber(addr) <= tonumber("0x1FFF") then
RAM[tonumber(address) and tonumber("0x7FF")] = data;
elseif tonumber(addr) <= tonumber("0x3FFF") then
writePPU(tonumber(address) and 7, data);
elseif tonumber("0x4000") <= tonumber(addr) then
if tonumber(addr) <= tonumber("0x4018") then
writeAPU(tonumber(address) - tonumber("0x4000"), tonumber(data));
end
end
end