-
Notifications
You must be signed in to change notification settings - Fork 1
/
memory.prg
210 lines (190 loc) · 5.66 KB
/
memory.prg
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
#define XHB_BITOP // Habilita das operações | & ^^
#include "xhb.ch"
#include "common.ch"
#include "hbclass.ch"
//#define SHOWLOG
CREATE CLASS Memory
VAR console
METHOD New(console)
METHOD Read(address)
METHOD Write(address, value)
END CLASS
// CPU Memory Map
METHOD New(console) CLASS Memory
::console=console
return Self
METHOD Read(address) CLASS Memory
//address=(address & 0xFFFF)
#ifdef SHOWLOG
/*
ppi := 2
while ( !Empty(ProcName(ppi)) )
clog(ProcName(ppi)+"("+str(ProcLine(ppi))+") - "+procfile(ppi))
ppi++
end do
*/
//clog("IN.MEM: ",address)
#endif
do case
case address < 0x2000
#ifdef SHOWLOG
X=::console:RAM[(address % 0x0800)+1]
clog("MEM.Read: ",address," = ",x)
return x
#endif
return ::console:RAM[(address % 0x0800)+1]
case address < 0x4000
//alert("READ MEM:"+hb_numtohex(0x2000 + address % 8))
#ifdef SHOWLOG
X=::console:PPU:readRegister(0x2000 + address % 8)
clog("MEM.Read: ",address," - Value: ",x)
return x
#endif
return ::console:PPU:readRegister(0x2000 + address % 8)
case address == 0x4014
#ifdef SHOWLOG
X=::console:PPU:readRegister(address)
clog("MEM.Read: ",address," - Value: ",x)
return x
#endif
return ::console:PPU:readRegister(address)
case address == 0x4015
#ifdef SHOWLOG
X=::console:APU:readRegister(address)
clog("MEM.Read: ",address," - Value: ",x)
return x
#endif
return ::console:APU:readRegister(address)
case address == 0x4016
#ifdef SHOWLOG
X=::console:Controller1:Read()
clog("MEM.Read: ",address," - Value: ",x)
return x
#endif
return ::console:Controller1:Read()
case address == 0x4017
#ifdef SHOWLOG
X=::console:Controller2:Read()
clog("MEM.Read: ",address," - Value: ",x)
return x
#endif
return ::console:Controller2:Read()
case address < 0x6000
// TODO: I/O registers
case address >= 0x6000
#ifdef SHOWLOG
v=address
x=::console:Mapper:Read(address)
clog("Lendo do MAPPER: ",v," = ",x)
return x
#endif
return ::console:Mapper:Read(address)
default
? "unhandled cpu memory read at address: ", address
end case
return 0
METHOD Write(address, value) CLASS Memory
//? " -> Memory:Write: ",hb_numtohex(address,4),' -> ',hb_numtohex(value,2),' '
#ifdef SHOWLOG
clog("MEM.Write: ",address," = ",value)
#endif
do case
case address < 0x2000
//::console:RAM[(address % 0x0800)+1] = value
::console:RAM[address+1] = value
case address < 0x4000
::console:PPU:writeRegister(0x2000 + address % 8, value)
case address < 0x4014
::console:APU:writeRegister(address, value)
case address == 0x4014
::console:PPU:writeRegister(address, value)
case address == 0x4015
::console:APU:writeRegister(address, value)
case address == 0x4016
::console:Controller1:Write(value)
::console:Controller2:Write(value)
case address == 0x4017
::console:APU:writeRegister(address, value)
case address < 0x6000
// TODO: I/O registers
case address >= 0x6000
::console:Mapper:Write(address, value)
default
? "unhandled cpu memory write at address: ", address
end case
// PPU Memory Map
CREATE CLASS ppuMemory
VAR console
METHOD New(console)
METHOD Read(address)
METHOD Write(address, value)
END CLASS
METHOD New(console) CLASS ppuMemory
::console=console
return Self
METHOD Read(address) CLASS ppuMemory
//address = address % 0x4000
do case
case address < 0x2000
#ifdef SHOWLOG
x=::console:Mapper:Read(address)
clog("PPU.Read1: ",address," - Value: ",x)
return x
#endif
return ::console:Mapper:Read(address)
case address < 0x3F00
#ifdef SHOWLOG
mode := ::console:Cartridge:Mirror
clog("Mode: ",mode)
clog("Adress: ",MirrorAddress(mode, address) % 2048)
x=(::console:PPU:nameTableData[(MirrorAddress(mode, address) % 2048)+1])
clog("PPU.Read2: ",(MirrorAddress(mode, address) % 2048)," = ",x)
return x
#endif
mode := ::console:Cartridge:Mirror
return (::console:PPU:nameTableData[(MirrorAddress(mode, address) % 2048)+1])
case address < 0x4000
#ifdef SHOWLOG
x=::console:PPU:readPalette(address % 32)
clog("PPU.Read3: ",address % 32," = ",x)
return x
#endif
return ::console:PPU:readPalette(address % 32)
default
? "unhandled ppu memory read at address: ", address
end case
return 0
METHOD Write(address, value) CLASS ppuMemory
#ifdef SHOWLOG
clog("PPU.write: ",address," = ",value)
#endif
address = address % 0x4000
do case
case address < 0x2000
::console:Mapper:Write(address, value)
case address < 0x3F00
mode := ::console:Cartridge:Mirror
::console:PPU:nameTableData[(MirrorAddress(mode, address) % 2048)+1] = value
case address < 0x4000
::console:PPU:writePalette(address % 32, value)
default
? "unhandled ppu memory write at address: ", address
end case
// Mirroring Modes
/*const (
MirrorHorizontal = 0
MirrorVertical = 1
MirrorSingle0 = 2
MirrorSingle1 = 3
MirrorFour = 4
)*/
function MirrorAddress(mode, address)
Local MirrorLookup := {{0, 0, 1, 1},;
{0, 1, 0, 1},;
{0, 0, 0, 0},;
{1, 1, 1, 1},;
{0, 1, 2, 3}}
address = (address - 0x2000) % 0x1000
table := address / 0x0400
offset := address % 0x0400
return 0x2000 + MirrorLookup[mode+1][table+1] * 0x0400 + offset