-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDemo.cpp
More file actions
270 lines (220 loc) · 8.11 KB
/
Demo.cpp
File metadata and controls
270 lines (220 loc) · 8.11 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
/*
olc::NES - Part #4 - buses, Rams, Roms & Mappers
"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/-THeUXqR3zY
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 <iostream>
#include <sstream>
#include "bus.h"
#include "pineapple6502.h"
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
class Demo_olc2C02 : public olc::PixelGameEngine
{
public:
Demo_olc2C02() { sAppName = "olc2C02 Demonstration"; }
private:
// The NES
bus nes;
std::shared_ptr<cartridge> cart;
bool bEmulationRun = false;
float fResidualTime = 0.0f;
uint8_t nSelectedPalette = 0x00;
private:
// Support Utilities
std::map<uint16_t, std::string> mapAsm;
std::string hex(uint32_t n, uint8_t d)
{
std::string s(d, '0');
for (int i = d - 1; i >= 0; i--, n >>= 4)
s[i] = "0123456789ABCDEF"[n & 0xF];
return s;
};
void DrawRam(int x, int y, uint16_t nAddr, int nRows, int nColumns)
{
int nRamX = x, nRamY = y;
for (int row = 0; row < nRows; row++)
{
std::string sOffset = "$" + hex(nAddr, 4) + ":";
for (int col = 0; col < nColumns; col++)
{
sOffset += " " + hex(nes.cpuRead(nAddr, true), 2);
nAddr += 1;
}
DrawString(nRamX, nRamY, sOffset);
nRamY += 10;
}
}
void DrawCpu(int x, int y)
{
std::string status = "STATUS: ";
DrawString(x , y , "STATUS:", olc::WHITE);
DrawString(x + 64, y, "N", nes.cpu.status & pineapple6502::N ? olc::GREEN : olc::RED);
DrawString(x + 80, y , "V", nes.cpu.status & pineapple6502::V ? olc::GREEN : olc::RED);
DrawString(x + 96, y , "-", nes.cpu.status & pineapple6502::U ? olc::GREEN : olc::RED);
DrawString(x + 112, y , "B", nes.cpu.status & pineapple6502::B ? olc::GREEN : olc::RED);
DrawString(x + 128, y , "D", nes.cpu.status & pineapple6502::D ? olc::GREEN : olc::RED);
DrawString(x + 144, y , "I", nes.cpu.status & pineapple6502::I ? olc::GREEN : olc::RED);
DrawString(x + 160, y , "Z", nes.cpu.status & pineapple6502::Z ? olc::GREEN : olc::RED);
DrawString(x + 178, y , "C", nes.cpu.status & pineapple6502::C ? olc::GREEN : olc::RED);
DrawString(x , y + 10, "PC: $" + hex(nes.cpu.pc, 4));
DrawString(x , y + 20, "A: $" + hex(nes.cpu.a, 2) + " [" + std::to_string(nes.cpu.a) + "]");
DrawString(x , y + 30, "X: $" + hex(nes.cpu.x, 2) + " [" + std::to_string(nes.cpu.x) + "]");
DrawString(x , y + 40, "Y: $" + hex(nes.cpu.y, 2) + " [" + std::to_string(nes.cpu.y) + "]");
DrawString(x , y + 50, "Stack P: $" + hex(nes.cpu.stkp, 4));
}
void DrawCode(int x, int y, int nLines)
{
auto it_a = mapAsm.find(nes.cpu.pc);
int nLineY = (nLines >> 1) * 10 + y;
if (it_a != mapAsm.end())
{
DrawString(x, nLineY, (*it_a).second, olc::CYAN);
while (nLineY < (nLines * 10) + y)
{
nLineY += 10;
if (++it_a != mapAsm.end())
{
DrawString(x, nLineY, (*it_a).second);
}
}
}
it_a = mapAsm.find(nes.cpu.pc);
nLineY = (nLines >> 1) * 10 + y;
if (it_a != mapAsm.end())
{
while (nLineY > y)
{
nLineY -= 10;
if (--it_a != mapAsm.end())
{
DrawString(x, nLineY, (*it_a).second);
}
}
}
}
bool OnUserCreate()
{
// Load the cartridge
cart = std::make_shared<cartridge>("donkong.nes");
if (!cart->ImageValid())
return false;
// Insert into NES
nes.insertCartridge(cart);
// Extract dissassembly
mapAsm = nes.cpu.disassemble(0x0000, 0xFFFF);
// Reset NES
nes.reset();
return true;
}
bool OnUserUpdate(float fElapsedTime)
{
Clear(olc::DARK_BLUE);
// Sneaky peek of controller input in next video! ;P
if (GetKey(olc::Key::SPACE).bPressed) bEmulationRun = !bEmulationRun;
if (GetKey(olc::Key::R).bPressed) nes.reset();
if (GetKey(olc::Key::P).bPressed) (++nSelectedPalette) &= 0x07;
if (bEmulationRun)
{
if (fResidualTime > 0.0f)
fResidualTime -= fElapsedTime;
else
{
fResidualTime += (1.0f / 60.0f) - fElapsedTime;
do { nes.clock(); } while (!nes.ppu.frame_complete);
nes.ppu.frame_complete = false;
}
}
else
{
// Emulate code step-by-step
if (GetKey(olc::Key::C).bPressed)
{
// Clock enough times to execute a whole CPU instruction
do { nes.clock(); } while (!nes.cpu.complete());
// CPU clock runs slower than system clock, so it may be
// complete for additional system clock cycles. Drain
// those out
do { nes.clock(); } while (nes.cpu.complete());
}
// Emulate one whole frame
if (GetKey(olc::Key::F).bPressed)
{
// Clock enough times to draw a single frame
do { nes.clock(); } while (!nes.ppu.frame_complete);
// Use residual clock cycles to complete current instruction
do { nes.clock(); } while (!nes.cpu.complete());
// Reset frame completion flag
nes.ppu.frame_complete = false;
}
}
DrawCpu(516, 2);
DrawCode(516, 72, 26);
// Draw Palettes & Pattern Tables ==============================================
const int nSwatchSize = 6;
for (int p = 0; p < 8; p++) // For each palette
for(int s = 0; s < 4; s++) // For each index
FillRect(516 + p * (nSwatchSize * 5) + s * nSwatchSize, 340,
nSwatchSize, nSwatchSize, nes.ppu.GetColourFromPaletteRam(p, s));
// Draw selection reticule around selected palette
DrawRect(516 + nSelectedPalette * (nSwatchSize * 5) - 1, 339, (nSwatchSize * 4), nSwatchSize, olc::GREEN);
// Generate Pattern Tables
DrawSprite(516, 348, &nes.ppu.GetPatternTable(0, nSelectedPalette));
DrawSprite(648, 348, &nes.ppu.GetPatternTable(1, nSelectedPalette));
// Draw rendered output ========================================================
DrawSprite(0, 0, &nes.ppu.GetScreen(), 2);
// for (uint8_t y = 0; y<30; y++){
// for(uint8_t x = 0; x<32; x++){
// uint8_t id = (uint32_t)nes.ppu.tblName[0][y*32 +x];
// // DrawPartialSprite( x *16, y*16, &nes.ppu.GetPatternTable(0, nSelectedPalette), (id & 0x0F)<<3, ((id >> 4) & 0x0F) << 3, 8, 8, 2);
// }
// }
return true;
}
};
int main()
{
Demo_olc2C02 demo;
demo.Construct(780, 480, 2, 2);
demo.Start();
return 0;
}