-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.cpp
More file actions
278 lines (244 loc) · 7.5 KB
/
Copy pathBus.cpp
File metadata and controls
278 lines (244 loc) · 7.5 KB
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
/*
olc::NES - System Bus
"Thanks Dad for believing computers were gonna be a big deal..." - javidx9
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018-2019 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Relevant Video: https://youtu.be/xdzOvpYPmGE
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
https://www.youtube.com/javidx9extra
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Patreon: https://www.patreon.com/javidx9
Homepage: https://www.onelonecoder.com
Author
~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2019
*/
#include "Bus.h"
Bus::Bus()
{
// Connect CPU to communication bus
cpu.ConnectBus(this);
}
Bus::~Bus()
{
}
void Bus::SetSampleFrequency(uint32_t sample_rate)
{
dAudioTimePerSystemSample = 1.0 / (double)sample_rate;
dAudioTimePerNESClock = 1.0 / 5369318.0; // PPU Clock Frequency
}
void Bus::cpuWrite(uint16_t addr, uint8_t data)
{
if (cart->cpuWrite(addr, data))
{
// The cartridge "sees all" and has the facility to veto
// the propagation of the bus transaction if it requires.
// This allows the cartridge to map any address to some
// other data, including the facility to divert transactions
// with other physical devices. The NES does not do this
// but I figured it might be quite a flexible way of adding
// "custom" hardware to the NES in the future!
}
else if (addr >= 0x0000 && addr <= 0x1FFF)
{
// System RAM Address Range. The range covers 8KB, though
// there is only 2KB available. That 2KB is "mirrored"
// through this address range. Using bitwise AND to mask
// the bottom 11 bits is the same as addr % 2048.
cpuRam[addr & 0x07FF] = data;
}
else if (addr >= 0x2000 && addr <= 0x3FFF)
{
// PPU Address range. The PPU only has 8 primary registers
// and these are repeated throughout this range. We can
// use bitwise AND operation to mask the bottom 3 bits,
// which is the equivalent of addr % 8.
ppu.cpuWrite(addr & 0x0007, data);
}
else if ((addr >= 0x4000 && addr <= 0x4013) || addr == 0x4015 || addr == 0x4017) // NES APU
{
apu.cpuWrite(addr, data);
}
else if (addr == 0x4014)
{
// A write to this address initiates a DMA transfer
dma_page = data;
dma_addr = 0x00;
dma_transfer = true;
}
else if (addr >= 0x4016 && addr <= 0x4017)
{
// "Lock In" controller state at this time
controller_state[addr & 0x0001] = controller[addr & 0x0001];
}
}
uint8_t Bus::cpuRead(uint16_t addr, bool bReadOnly)
{
uint8_t data = 0x00;
if (cart->cpuRead(addr, data))
{
// Cartridge Address Range
}
else if (addr >= 0x0000 && addr <= 0x1FFF)
{
// System RAM Address Range, mirrored every 2048
data = cpuRam[addr & 0x07FF];
}
else if (addr >= 0x2000 && addr <= 0x3FFF)
{
// PPU Address range, mirrored every 8
data = ppu.cpuRead(addr & 0x0007, bReadOnly);
}
else if (addr == 0x4015)
{
// APU Read Status
data = apu.cpuRead(addr);
}
else if (addr >= 0x4016 && addr <= 0x4017)
{
// Read out the MSB of the controller status word
data = (controller_state[addr & 0x0001] & 0x80) > 0;
controller_state[addr & 0x0001] <<= 1;
}
return data;
}
void Bus::insertCartridge(const std::shared_ptr<Cartridge>& cartridge)
{
// Connects cartridge to both Main Bus and CPU Bus
this->cart = cartridge;
ppu.ConnectCartridge(cartridge);
}
void Bus::reset()
{
cart->reset();
cpu.reset();
ppu.reset();
nSystemClockCounter = 0;
dma_page = 0x00;
dma_addr = 0x00;
dma_data = 0x00;
dma_dummy = true;
dma_transfer = false;
}
bool Bus::clock()
{
// Clocking. The heart and soul of an emulator. The running
// frequency is controlled by whatever calls this function.
// So here we "divide" the clock as necessary and call
// the peripheral devices clock() function at the correct
// times.
// The fastest clock frequency the digital system cares
// about is equivalent to the PPU clock. So the PPU is clocked
// each time this function is called...
ppu.clock();
// ...also clock the APU
apu.clock();
// The CPU runs 3 times slower than the PPU so we only call its
// clock() function every 3 times this function is called. We
// have a global counter to keep track of this.
if (nSystemClockCounter % 3 == 0)
{
// Is the system performing a DMA transfer form CPU memory to
// OAM memory on PPU?...
if (dma_transfer)
{
// ...Yes! We need to wait until the next even CPU clock cycle
// before it starts...
if (dma_dummy)
{
// ...So hang around in here each clock until 1 or 2 cycles
// have elapsed...
if (nSystemClockCounter % 2 == 1)
{
// ...and finally allow DMA to start
dma_dummy = false;
}
}
else
{
// DMA can take place!
if (nSystemClockCounter % 2 == 0)
{
// On even clock cycles, read from CPU bus
dma_data = cpuRead(dma_page << 8 | dma_addr);
}
else
{
// On odd clock cycles, write to PPU OAM
ppu.pOAM[dma_addr] = dma_data;
// Increment the lo byte of the address
dma_addr++;
// If this wraps around, we know that 256
// bytes have been written, so end the DMA
// transfer, and proceed as normal
if (dma_addr == 0x00)
{
dma_transfer = false;
dma_dummy = true;
}
}
}
}
else
{
// No DMA happening, the CPU is in control of its
// own destiny. Go forth my friend and calculate
// awesomeness for many generations to come...
cpu.clock();
}
}
// Synchronising with Audio
bool bAudioSampleReady = false;
dAudioTime += dAudioTimePerNESClock;
if (dAudioTime >= dAudioTimePerSystemSample)
{
dAudioTime -= dAudioTimePerSystemSample;
dAudioSample = apu.GetOutputSample();
bAudioSampleReady = true;
}
// The PPU is capable of emitting an interrupt to indicate the
// vertical blanking period has been entered. If it has, we need
// to send that irq to the CPU.
if (ppu.nmi)
{
ppu.nmi = false;
cpu.nmi();
}
// Check if cartridge is requesting IRQ
if (cart->GetMapper()->irqState())
{
cart->GetMapper()->irqClear();
cpu.irq();
}
nSystemClockCounter++;
return bAudioSampleReady;
}