/
Main.cpp
278 lines (230 loc) · 7.69 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
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
#include "PCH.hpp"
#include <Emulator.hpp>
// 60 FPS or 16.67ms
const double TimePerFrame = 1.0 / 60.0;
// The number of CPU cycles per frame
const unsigned int CyclesPerFrame = 70224;
struct SDLWindowDeleter
{
void operator()(SDL_Window* window)
{
if (window != nullptr)
{
SDL_DestroyWindow(window);
}
}
};
struct SDLRendererDeleter
{
void operator()(SDL_Renderer* renderer)
{
if (renderer != nullptr)
{
SDL_DestroyRenderer(renderer);
}
}
};
struct SDLTextureDeleter
{
void operator()(SDL_Texture* texture)
{
if (texture != nullptr)
{
SDL_DestroyTexture(texture);
}
}
};
void Render(SDL_Renderer* pRenderer, SDL_Texture* pTexture, Emulator& emulator)
{
// Clear window
SDL_SetRenderDrawColor(pRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(pRenderer);
byte* pPixels;
int pitch = 0;
SDL_LockTexture(pTexture, nullptr, (void**)&pPixels, &pitch);
// Render Game
byte* pData = emulator.GetCurrentFrame();
memcpy(pPixels, pData, 160 * 144 * 4);
SDL_UnlockTexture(pTexture);
SDL_RenderCopy(pRenderer, pTexture, nullptr, nullptr);
// Update window
SDL_RenderPresent(pRenderer);
}
// TODO: refactor this
std::unique_ptr<SDL_Renderer, SDLRendererDeleter> spRenderer;
std::unique_ptr<SDL_Texture, SDLTextureDeleter> spTexture;
Emulator emulator;
// The emulator will call this whenever we hit VBlank
void VSyncCallback()
{
Render(spRenderer.get(), spTexture.get(), emulator);
}
void ProcessInput(Emulator& emulator)
{
SDL_PumpEvents();
const Uint8 *keys = SDL_GetKeyboardState(NULL);
byte input = JOYPAD_NONE;
byte buttons = JOYPAD_NONE;
if(keys[SDL_SCANCODE_W])
{
input |= JOYPAD_INPUT_UP;
}
if(keys[SDL_SCANCODE_A])
{
input |= JOYPAD_INPUT_LEFT;
}
if(keys[SDL_SCANCODE_S])
{
input |= JOYPAD_INPUT_DOWN;
}
if(keys[SDL_SCANCODE_D])
{
input |= JOYPAD_INPUT_RIGHT;
}
if(keys[SDL_SCANCODE_K])
{
buttons |= JOYPAD_BUTTONS_A;
}
if(keys[SDL_SCANCODE_L])
{
buttons |= JOYPAD_BUTTONS_B;
}
if(keys[SDL_SCANCODE_N])
{
buttons |= JOYPAD_BUTTONS_START;
}
if(keys[SDL_SCANCODE_M])
{
buttons |= JOYPAD_BUTTONS_SELECT;
}
emulator.SetInput(input, buttons);
}
int main(int argc, char** argv)
{
int windowWidth = 160;
int windowHeight = 144;
int windowScale = 2;
if(argc > 1)
{
windowScale = atoi(argv[1]);
}
std::string bootROM;
//std::string bootROM = "res/games/dmg_bios.bin";
//std::string bootROM = "res/games/gbc_bios.bin";
std::string romPath = "res/tests/cpu_instrs.gb"; // PASSED
//std::string romPath = "res/tests/01-special.gb"; // PASSED
//std::string romPath = "res/tests/02-interrupts.gb"; // PASSED
//std::string romPath = "res/tests/03-op sp,hl.gb"; // PASSED
//std::string romPath = "res/tests/04-op r,imm.gb"; // PASSED
//std::string romPath = "res/tests/05-op rp.gb"; // PASSED
//std::string romPath = "res/tests/06-ld r,r.gb"; // PASSED
//std::string romPath = "res/tests/07-jr,jp,call,ret,rst.gb"; // PASSED
//std::string romPath = "res/tests/08-misc instrs.gb"; // PASSED
//std::string romPath = "res/tests/09-op r,r.gb"; // PASSED
//std::string romPath = "res/tests/10-bit ops.gb"; // PASSED
//std::string romPath = "res/tests/11-op a,(hl).gb"; // PASSED
//std::string romPath = "res/tests/instr_timing.gb"; // PASSED
//std::string romPath = "res/tests/mem_timing.gb"; // FAILED
//std::string romPath = "res/tests/01-read_timing.gb"; // FAILED
//std::string romPath = "res/tests/02-write_timing.gb"; // FAILED
//std::string romPath = "res/tests/03-modify_timing.gb"; // FAILED
//std::string romPath = "res/tests/oam_bug.gb"; // FAILED
//std::string romPath = "res/games/Pokemon - Blue Version.gb";
//std::string romPath = "res/games/Tetris (World).gb";
//std::string romPath = "res/games/Super Mario Land (World).gb";
//std::string romPath = "res/games/Tamagotchi.gb";
//std::string romPath = "res/games/Battletoads.gb";
//std::string romPath = "res/games/Tetris.gb";
//std::string romPath = "res/games/Zelda.gb";
//std::string romPath = "res/games/plantboy.gb";
//std::string romPath = "res/games/Metroid.gb";
//std::string romPath = "res/games/Castlevania.gb";
// CGB Only
//std::string romPath = "res/games/Lemmings.gbc"; // Requires MBC5
//std::string romPath = "res/games/Mario2.gbc"; // Requires MBC5
if(argc > 2)
{
romPath = argv[2];
}
bool isRunning = true;
std::unique_ptr<SDL_Window, SDLWindowDeleter> spWindow;
SDL_Event event;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
Logger::LogError("SDL could not initialize! SDL error: '%s'", SDL_GetError());
return false;
}
// Create window
spWindow = std::unique_ptr<SDL_Window, SDLWindowDeleter>(
SDL_CreateWindow(
"GameLad",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
windowWidth * windowScale, // Original = 160
windowHeight * windowScale, // Original = 144
SDL_WINDOW_SHOWN));
if (spWindow == nullptr)
{
Logger::LogError("Window could not be created! SDL error: '%s'", SDL_GetError());
return false;
}
// Create renderer
spRenderer = std::unique_ptr<SDL_Renderer, SDLRendererDeleter>(
SDL_CreateRenderer(spWindow.get(), -1, SDL_RENDERER_ACCELERATED));
if (spRenderer == nullptr)
{
Logger::LogError("Renderer could not be created! SDL error: '%s'", SDL_GetError());
return false;
}
spTexture = std::unique_ptr<SDL_Texture, SDLTextureDeleter>(
SDL_CreateTexture(spRenderer.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 160, 144));
if (emulator.Initialize(bootROM.empty() ? nullptr : bootROM.data(), romPath.data()))
{
emulator.SetVSyncCallback(&VSyncCallback);
unsigned int cycles = 0;
Uint64 frameStart = SDL_GetPerformanceCounter();
while (isRunning)
{
// Poll for window input
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
isRunning = false;
emulator.SetVSyncCallback(nullptr);
}
}
if (!isRunning)
{
// Exit early if the app is closing
continue;
}
ProcessInput(emulator);
while (cycles < CyclesPerFrame)
{
cycles += emulator.Step();
}
cycles -= CyclesPerFrame;
Uint64 frameEnd = SDL_GetPerformanceCounter();
// Loop until we use up the rest of our frame time
while (true)
{
frameEnd = SDL_GetPerformanceCounter();
double frameElapsedInSec = (double)(frameEnd - frameStart) / SDL_GetPerformanceFrequency();
// Break out once we use up our time per frame
if (frameElapsedInSec >= TimePerFrame)
{
break;
}
}
frameStart = frameEnd;
}
}
emulator.Stop();
spTexture.reset();
spRenderer.reset();
spWindow.reset();
SDL_Quit();
return 0;
}