-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEmulator.cpp
227 lines (192 loc) · 6.47 KB
/
Emulator.cpp
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
#include <iostream>
#include <sstream>
#include "olc6502.hpp"
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
class Demo_olc2C02 : public olc::PixelGameEngine
{
public:
Demo_olc2C02() { sAppName = "olc2C02 Demonstration"; }
private:
// The NES
Bus nes;
Cartridge *rom=nullptr;
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), 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 & olc6502::N ? olc::GREEN : olc::RED);
DrawString(x + 80, y , "V", nes.cpu->status & olc6502::V ? olc::GREEN : olc::RED);
DrawString(x + 96, y , "-", nes.cpu->status & olc6502::U ? olc::GREEN : olc::RED);
DrawString(x + 112, y , "B", nes.cpu->status & olc6502::B ? olc::GREEN : olc::RED);
DrawString(x + 128, y , "D", nes.cpu->status & olc6502::D ? olc::GREEN : olc::RED);
DrawString(x + 144, y , "I", nes.cpu->status & olc6502::I ? olc::GREEN : olc::RED);
DrawString(x + 160, y , "Z", nes.cpu->status & olc6502::Z ? olc::GREEN : olc::RED);
DrawString(x + 178, y , "C", nes.cpu->status & olc6502::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
std::string filename;
std::cout<<"Enter filename: ";
std::cin>>filename;
rom = new Cartridge(filename);
if (!rom->bImageValid){
std::cout<<"The ROM path is not valid";
return false;
}
// Insert into NES
nes.connect_ROM(rom);
// Extract dissassembly
// mapAsm = nes.cpu->disassemble(0x0000, 0xFFFF);
// Reset NES
nes.reset();
return true;
}
bool OnUserUpdate(float fElapsedTime)
{
Clear(olc::DARK_BLUE);
// Handle input for controller in port #1
nes.controller[0] = 0x00;
nes.controller[0] |= GetKey(olc::Key::UP).bHeld ? 0x80 : 0x00; // A Button
nes.controller[0] |= GetKey(olc::Key::CTRL).bHeld ? 0x40 : 0x00; // B Button
nes.controller[0] |= GetKey(olc::Key::A).bHeld ? 0x20 : 0x00; // Select
nes.controller[0] |= GetKey(olc::Key::S).bHeld ? 0x10 : 0x00; // Start
nes.controller[0] |= GetKey(olc::Key::X).bHeld ? 0x08 : 0x00;
nes.controller[0] |= GetKey(olc::Key::DOWN).bHeld ? 0x04 : 0x00;
nes.controller[0] |= GetKey(olc::Key::LEFT).bHeld ? 0x02 : 0x00;
nes.controller[0] |= GetKey(olc::Key::RIGHT).bHeld ? 0x01 : 0x00;
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 OAM Contents (first 26 out of 64) ======================================
for (int i = 0; i < 26; i++)
{
std::string s = hex(i, 2) + ": (" + std::to_string(nes.ppu.pOAM[i * 4 + 3])
+ ", " + std::to_string(nes.ppu.pOAM[i * 4 + 0]) + ") "
+ "ID: " + hex(nes.ppu.pOAM[i * 4 + 1], 2) +
+" AT: " + hex(nes.ppu.pOAM[i * 4 + 2], 2);
DrawString(516, 72 + i * 10, s);
}
// 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::WHITE);
// 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);
return true;
}
};
int main()
{
Demo_olc2C02 demo;
demo.Construct(780, 480, 2, 2);
demo.Start();
return 0;
}