-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
208 lines (167 loc) · 6.61 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
#include <iostream>
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include "SDL_FontCache.h"
#include "fmt/format.h"
#include "def.h"
#include "debug.h"
#include "gameboy.h"
const int SCREEN_W = PIXELS_W * 4;
const int SCREEN_H = PIXELS_H * 4;
const int FPS = 59.7;
const float MS_PER_FRAME = 1000.0 / FPS;
int main(int argc, char* args[])
{
//GameBoy gb("C:\\Users\\ruben\\Documents\\GitHub\\gameboy\\roms\\cpu_instrs.gb");
//GameBoy gb("C:\\Users\\ruben\\Documents\\GitHub\\gameboy\\roms\\01-special.gb");
GameBoy gb("C:\\Users\\ruben\\Documents\\GitHub\\gameboy\\roms\\Tetris (World) (Rev A).gb");
//GameBoy gb("C:\\Users\\ruben\\Documents\\GitHub\\gameboy\\roms\\Super Mario Land (World).gb");
//GameBoy gb("C:\\Users\\ruben\\Documents\\GitHub\\gameboy\\roms\\Dr. Mario (World).gb");
//GameBoy gb("roms/Dr. Mario (World).gb");
//GameBoy gb("C:/Users/Ruben/Documents/ROMs/GameBoy/cpu_instrs/cpu_instrs.gb");
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "Init error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Init(SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow(
"GameBoy",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_W+700,
SCREEN_H,
SDL_WINDOW_SHOWN);
if (window == NULL) {
std::cout << "Window creation error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
std::cout << "Renderer creation error: " << SDL_GetError() << std::endl;
return -1;
}
SDL_AudioSpec want, have;
SDL_zero(want);
want.freq = APU_SAMPLE_RATE;
want.format = AUDIO_F32;
want.channels = 1;
want.samples = APU_BUFFER_SIZE;
want.callback = NULL; // Don't need since we queue audio ourselves
SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (dev == 0)
std::cout << "Could not open audio device: " << SDL_GetError() << std::endl;
// Audio is initially paused, so unpause it
SDL_PauseAudioDevice(dev, 0);
//SDL_RenderSetLogicalSize(renderer, SCREEN_W, SCREEN_H);
Debug debug = Debug(&gb, renderer);
bool quit = false;
SDL_Event e;
int current_time = 0;
bool redraw = false;
bool stepping_mode = false;
u8 break_instr = 0;
u16 break_PC = 0;
const Uint8* keys = SDL_GetKeyboardState(NULL);
while (!quit) {
current_time = SDL_GetTicks();
while (SDL_PollEvent(&e) != 0) {
switch (e.type) {
case SDL_KEYDOWN:
switch (e.key.keysym.scancode) {
case SDL_SCANCODE_Q:
stepping_mode = !stepping_mode;
break;
case SDL_SCANCODE_RIGHT:
gb.cpu.debug_print();
gb.cycle();
break;
default:
break;
}
break;
case SDL_QUIT:
quit = true;
break;
}
}
gb.buttons[Button::Up] = keys[SDL_SCANCODE_W];
gb.buttons[Button::Down] = keys[SDL_SCANCODE_S];
gb.buttons[Button::Left] = keys[SDL_SCANCODE_A];
gb.buttons[Button::Right] = keys[SDL_SCANCODE_D];
gb.buttons[Button::Start] = keys[SDL_SCANCODE_RETURN];
gb.buttons[Button::Select] = keys[SDL_SCANCODE_SPACE];
gb.buttons[Button::A] = keys[SDL_SCANCODE_O];
gb.buttons[Button::B] = keys[SDL_SCANCODE_P];
/*
The gameboy loop is structured as follows:
1. Wait until any queued audio has finished playing
2. While screen is not ready to redraw:
Cycle the gameboy
Push audio into the queue for playback if there is a new buffer
3. Redraw the screen
4. Wait any additional time to regulate to 60 FPS
*/
// Wait until the audio queue is finished playing
int queued = SDL_GetQueuedAudioSize(dev);
while (queued > 0) {
queued = SDL_GetQueuedAudioSize(dev);
}
redraw = false;
bool sent_for_playback = false;
// Cycle the gameboy until it wants us to redraw the screen
while (!redraw && !stepping_mode) {
gb.cycle();
// The sample_queue_index can be 0 for multiple CPU cycles, so only
// send the audio for playback once
if (gb.apu.sample_queue_index == 0 && !sent_for_playback) {
SDL_QueueAudio(dev, &gb.apu.sample_queue[0], gb.apu.sample_queue.size()*4);
sent_for_playback = true;
}
// Check if a new audio buffer has started to be filled
if (gb.apu.sample_queue_index == 1) {
sent_for_playback = false;
}
if (break_instr != 0 && gb.cpu.current_opcode() == break_instr)
stepping_mode = true;
if (break_PC != 0 && gb.cpu.PC == break_PC)
stepping_mode = true;
redraw = gb.gpu.get_redraw();
}
// Clear the screen
SDL_RenderClear(renderer);
// Create an SDL_Texture from the gameboy screen buffer
// This is not scaled up yet
SDL_Surface* screen = SDL_CreateRGBSurfaceFrom(
gb.gpu.get_screen_buffer(),
PIXELS_W,
PIXELS_H,
32,
PIXELS_W * 4,
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, screen);
SDL_FreeSurface(screen);
// Render the gameboy texture onto the screen
SDL_Rect dst = {0, 0, SCREEN_W, SCREEN_H};
SDL_RenderCopy(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture);
// Debug info rendering
debug.draw(SCREEN_W, 0);
// Update the screen
SDL_RenderPresent(renderer);
// Regulate to 60 fps
int elapsed_time = SDL_GetTicks() - current_time;
if (elapsed_time < MS_PER_FRAME) {
debug.current_fps = 1000.0 / elapsed_time;
SDL_Delay(MS_PER_FRAME - elapsed_time);
}
}
SDL_CloseAudioDevice(dev);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
SDL_Quit();
return 0;
}