-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
164 lines (141 loc) · 4.8 KB
/
main.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
#include <sstream>
#include <iostream>
#include "Bus.h"
#include "olcPixelGameEngine.h"
#include "olcPGEX_Sound.h"
#include "tinyfiledialogs.h"
class Emulator : public olc::PixelGameEngine
{
private:
int clockspeed = 1789770 * 3;
Bus bus;
float frame_timer = 0;
uint8_t selected_palette = 0;
uint8_t selected_nametable = 0;
bool realtime = true;
static Emulator *emulator_pointer;
public:
Emulator(std::string rom_path) : bus(Bus(rom_path))
{
sAppName = "NES Emulator";
}
private:
static float SoundOut(int nChannel, float fGlobalTime, float fTimeStep)
{
if (nChannel == 0)
{
while (!emulator_pointer->bus.clock())
{
};
return static_cast<float>(emulator_pointer->bus.apu.get_sample(fGlobalTime));
}
else
return 0.0f;
}
bool OnUserCreate() override
{
bus.cpu.reset();
emulator_pointer = this;
olc::SOUND::InitialiseAudio(44100, 1, 8, 512);
olc::SOUND::SetUserSynthFunction(SoundOut);
return true;
}
bool OnUserUpdate(float dt) override
{
// Controller inputs
bus.controller[0] = 0x00;
bus.controller[0] |= GetKey(olc::Key::X).bHeld ? 0x80 : 0x00;
bus.controller[0] |= GetKey(olc::Key::Z).bHeld ? 0x40 : 0x00;
bus.controller[0] |= GetKey(olc::Key::A).bHeld ? 0x20 : 0x00;
bus.controller[0] |= GetKey(olc::Key::S).bHeld ? 0x10 : 0x00;
bus.controller[0] |= GetKey(olc::Key::UP).bHeld ? 0x08 : 0x00;
bus.controller[0] |= GetKey(olc::Key::DOWN).bHeld ? 0x04 : 0x00;
bus.controller[0] |= GetKey(olc::Key::LEFT).bHeld ? 0x02 : 0x00;
bus.controller[0] |= GetKey(olc::Key::RIGHT).bHeld ? 0x01 : 0x00;
// Cycle pattern table palette
if (GetKey(olc::Key::P).bPressed)
(++selected_palette) &= 0x07;
// Cycle visible nametable
if (GetKey(olc::Key::TAB).bPressed)
selected_nametable = 1 - selected_nametable;
// Pause realtime execution
if (GetKey(olc::Key::SPACE).bPressed)
realtime = !realtime;
// clear the screen
Clear(olc::BLACK);
// if (realtime)
// {
// // run the correct number of clock cycles
// for (int i = 0; i < (clockspeed * dt); i++)
// {
// bus.clock();
// }
// }
// else
// {
// // Use enter key to step through instructions
// if (GetKey(olc::Key::ENTER).bPressed || GetKey(olc::Key::SHIFT).bHeld)
// {
// // perform PPU clocks
// bus.clock();
// bus.clock();
// bus.clock();
// while (bus.cpu_executing)
// bus.clock(); // wait for CPU to finish executing
// }
// if (GetKey(olc::Key::N).bPressed)
// bus.cpu.nmi(); // emit NMI
// }
if (!realtime)
DrawString(516, 0, "paused", olc::YELLOW);
DrawString(516, 10, bus.display(), olc::WHITE);
// Draw Pattern Tables
DrawString(516, 338, "Pattern Table 0:", olc::WHITE);
olc::Sprite sprite = olc::Sprite(128, 128);
bus.ppu.get_pattern_table(0, selected_palette, &sprite);
DrawSprite(516, 348, &sprite);
DrawString(648, 338, "Pattern Table 1:", olc::WHITE);
bus.ppu.get_pattern_table(1, selected_palette, &sprite);
DrawSprite(648, 348, &sprite);
for (uint8_t y = 0; y < 30; y++)
{
for (uint8_t x = 0; x < 32; x++)
{
uint8_t id = (u_int32_t)bus.ppu.nametables[selected_nametable][y * 32 + x];
DrawPartialSprite((x * 8) + 516, (y * 8) + 98, &sprite, (id & 0x0F) << 3, ((id >> 4) & 0x0F) << 3, 8, 8, 1, 0);
}
}
// Draw main screen
DrawSprite(0, 0, &bus.ppu.screen, 2);
return true;
}
bool OnUserDestroy() override
{
olc::SOUND::DestroyAudio();
return true;
}
};
Emulator *Emulator::emulator_pointer = nullptr;
int main(int argc, char const *argv[])
{
// Open file picker UI
char const *extensions[1] = {"*.nes"};
char const *selection = tinyfd_openFileDialog(
"Select ROM", // title
NULL, // optional initial directory
1, // number of allowed file extensions
extensions, // allowed file extensions
NULL, // optional filter description
0 // forbid multiple selections
);
if (selection != nullptr)
{
Emulator emulator = Emulator(selection);
if (emulator.Construct(780, 480, 2, 2))
{
std::cout << "Starting emulator\n";
emulator.Start();
}
}
return 0;
}