forked from audetto/AppleWin
/
main.cpp
203 lines (160 loc) · 4.94 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
#include <SDL.h>
#include <iostream>
#include <memory>
#include <iomanip>
#include "StdAfx.h"
#include "linux/benchmark.h"
#include "linux/context.h"
#include "frontends/common2/fileregistry.h"
#include "frontends/common2/utils.h"
#include "frontends/common2/programoptions.h"
#include "frontends/common2/timer.h"
#include "frontends/sdl/gamepad.h"
#include "frontends/sdl/sdirectsound.h"
#include "frontends/sdl/utils.h"
#include "frontends/sdl/renderer/sdlrendererframe.h"
#include "frontends/sdl/imgui/sdlimguiframe.h"
#include "CardManager.h"
#include "Core.h"
#include "Log.h"
#include "CPU.h"
#include "NTSC.h"
#include "SaveState.h"
#include "Interface.h"
// comment out to test / debug init / shutdown only
#define EMULATOR_RUN
namespace
{
int getRefreshRate()
{
SDL_DisplayMode current;
const int should_be_zero = SDL_GetCurrentDisplayMode(0, ¤t);
if (should_be_zero)
{
throw std::runtime_error(SDL_GetError());
}
return current.refresh_rate;
}
struct Data
{
sa2::SDLFrame * frame;
SDL_mutex * mutex;
common2::Timer * timer;
};
}
void run_sdl(int argc, const char * argv [])
{
std::cerr << std::fixed << std::setprecision(2);
common2::EmulatorOptions options;
Video & video = GetVideo();
const int sw = video.GetFrameBufferBorderlessWidth();
const int sh = video.GetFrameBufferBorderlessHeight();
options.geometry.empty = true;
options.geometry.width = sw * 2;
options.geometry.height = sh * 2;
options.geometry.x = SDL_WINDOWPOS_UNDEFINED;
options.geometry.y = SDL_WINDOWPOS_UNDEFINED;
const bool run = getEmulatorOptions(argc, argv, "SDL2", options);
if (!run)
return;
const LoggerContext logger(options.log);
const RegistryContext registryContext(CreateFileRegistry(options));
common2::loadGeometryFromRegistry("sa2", options.geometry);
std::shared_ptr<sa2::SDLFrame> frame;
if (options.imgui)
{
frame.reset(new sa2::SDLImGuiFrame(options));
}
else
{
frame.reset(new sa2::SDLRendererFrame(options));
}
std::shared_ptr<Paddle> paddle(new sa2::Gamepad(0));
const Initialisation init(frame, paddle);
applyOptions(options);
frame->Initialize(true);
std::cerr << "Default GL swap interval: " << SDL_GL_GetSwapInterval() << std::endl;
const int fps = getRefreshRate();
std::cerr << "Video refresh rate: " << fps << " Hz, " << 1000.0 / fps << " ms" << std::endl;
#ifdef EMULATOR_RUN
if (options.benchmark)
{
// we need to switch off vsync, otherwise FPS is limited to 60
// and it will take longer to run
sa2::SDLFrame::setGLSwapInterval(0);
const auto redraw = [&frame]{
frame->VideoPresentScreen();
};
const auto refresh = [redraw, &video]{
NTSC_SetVideoMode( video.GetVideoMode() );
NTSC_VideoRedrawWholeScreen();
redraw();
};
VideoBenchmark(redraw, refresh);
}
else
{
common2::Timer global;
common2::Timer refreshScreenTimer;
common2::Timer cpuTimer;
common2::Timer eventTimer;
common2::Timer frameTimer;
const std::string globalTag = ". .";
std::string updateTextureTimerTag, refreshScreenTimerTag, cpuTimerTag, eventTimerTag;
// it does not need to be exact
const size_t oneFrame = 1000 / fps;
bool quit = false;
do
{
frameTimer.tic();
eventTimer.tic();
sa2::writeAudio();
frame->ProcessEvents(quit);
eventTimer.toc();
cpuTimer.tic();
frame->ExecuteOneFrame(oneFrame);
cpuTimer.toc();
if (!options.headless)
{
refreshScreenTimer.tic();
frame->VideoPresentScreen();
refreshScreenTimer.toc();
}
frameTimer.toc();
} while (!quit);
global.toc();
std::cerr << "Global: " << global << std::endl;
std::cerr << "Frame: " << frameTimer << std::endl;
std::cerr << "Screen: " << refreshScreenTimer << std::endl;
std::cerr << "Events: " << eventTimer << std::endl;
std::cerr << "CPU: " << cpuTimer << std::endl;
const double timeInSeconds = global.getTimeInSeconds();
const double actualClock = g_nCumulativeCycles / timeInSeconds;
std::cerr << "Expected clock: " << g_fCurrentCLK6502 << " Hz, " << g_nCumulativeCycles / g_fCurrentCLK6502 << " s" << std::endl;
std::cerr << "Actual clock: " << actualClock << " Hz, " << timeInSeconds << " s" << std::endl;
sa2::stopAudio();
}
#endif
}
int main(int argc, const char * argv [])
{
//First we need to start up SDL, and make sure it went ok
const Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO | SDL_INIT_TIMER;
if (SDL_Init(flags) != 0)
{
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
int exit = 0;
try
{
run_sdl(argc, argv);
}
catch (const std::exception & e)
{
exit = 2;
std::cerr << e.what() << std::endl;
}
SDL_Quit();
return exit;
}