-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.cpp
More file actions
179 lines (151 loc) · 5.3 KB
/
Bus.cpp
File metadata and controls
179 lines (151 loc) · 5.3 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
/*
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::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 >= 0x4016 && addr <= 0x4017)
{
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 >= 0x4016 && addr <= 0x4017)
{
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;
}
void 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();
// 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)
{
cpu.clock();
}
// 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();
}
nSystemClockCounter++;
}