2019-12-17 14:46:50 +00:00
|
|
|
#include <SDL.h>
|
2021-01-10 11:47:44 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2019-12-17 14:46:50 +00:00
|
|
|
#include <memory>
|
2021-02-06 10:02:58 +00:00
|
|
|
#include <iomanip>
|
2019-12-17 14:46:50 +00:00
|
|
|
|
2021-01-20 18:17:34 +00:00
|
|
|
#include "StdAfx.h"
|
2019-12-17 14:46:50 +00:00
|
|
|
#include "linux/interface.h"
|
2020-11-21 20:13:56 +00:00
|
|
|
#include "linux/benchmark.h"
|
2021-01-10 11:47:44 +00:00
|
|
|
#include "linux/context.h"
|
2021-04-30 20:10:30 +01:00
|
|
|
#include "linux/network/uthernet2.h"
|
2020-10-11 10:20:22 +01:00
|
|
|
|
2020-12-27 19:43:35 +00:00
|
|
|
#include "frontends/common2/fileregistry.h"
|
2020-10-09 19:13:42 +01:00
|
|
|
#include "frontends/common2/utils.h"
|
|
|
|
#include "frontends/common2/programoptions.h"
|
2020-11-13 15:37:10 +00:00
|
|
|
#include "frontends/common2/timer.h"
|
2020-12-23 19:25:25 +00:00
|
|
|
#include "frontends/sdl/gamepad.h"
|
|
|
|
#include "frontends/sdl/sdirectsound.h"
|
|
|
|
#include "frontends/sdl/utils.h"
|
2021-02-06 16:55:08 +00:00
|
|
|
|
|
|
|
#include "frontends/sdl/renderer/sdlrendererframe.h"
|
|
|
|
#include "frontends/sdl/imgui/sdlimguiframe.h"
|
2019-12-17 14:46:50 +00:00
|
|
|
|
2021-02-23 19:32:00 +00:00
|
|
|
#include "CardManager.h"
|
2020-11-27 13:48:53 +00:00
|
|
|
#include "Core.h"
|
2019-12-17 14:46:50 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "CPU.h"
|
|
|
|
#include "NTSC.h"
|
|
|
|
#include "SaveState.h"
|
2020-12-28 19:42:04 +00:00
|
|
|
#include "Interface.h"
|
2019-12-17 14:46:50 +00:00
|
|
|
|
2020-12-07 13:13:42 +00:00
|
|
|
// comment out to test / debug init / shutdown only
|
|
|
|
#define EMULATOR_RUN
|
2019-12-17 14:46:50 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-01-10 11:47:44 +00:00
|
|
|
|
2020-11-14 09:14:37 +00:00
|
|
|
int getRefreshRate()
|
2020-11-13 11:50:42 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-12 20:33:04 +00:00
|
|
|
struct Data
|
|
|
|
{
|
2021-03-14 10:02:19 +00:00
|
|
|
sa2::SDLFrame * frame;
|
2020-11-12 20:33:04 +00:00
|
|
|
SDL_mutex * mutex;
|
2021-02-25 16:04:50 +00:00
|
|
|
common2::Timer * timer;
|
2020-11-12 20:33:04 +00:00
|
|
|
};
|
|
|
|
|
2020-11-13 11:50:42 +00:00
|
|
|
Uint32 emulator_callback(Uint32 interval, void *param)
|
2020-11-12 20:33:04 +00:00
|
|
|
{
|
|
|
|
Data * data = static_cast<Data *>(param);
|
2020-11-13 11:50:42 +00:00
|
|
|
SDL_LockMutex(data->mutex);
|
2020-11-13 15:37:10 +00:00
|
|
|
|
|
|
|
data->timer->tic();
|
2021-03-14 10:02:19 +00:00
|
|
|
data->frame->ExecuteOneFrame(interval);
|
2020-11-13 15:37:10 +00:00
|
|
|
data->timer->toc();
|
|
|
|
|
2020-11-13 11:50:42 +00:00
|
|
|
SDL_UnlockMutex(data->mutex);
|
2020-11-12 20:33:04 +00:00
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:13:42 +01:00
|
|
|
void run_sdl(int argc, const char * argv [])
|
2019-12-17 14:46:50 +00:00
|
|
|
{
|
2021-02-06 10:02:58 +00:00
|
|
|
std::cerr << std::fixed << std::setprecision(2);
|
|
|
|
|
2021-02-25 16:04:50 +00:00
|
|
|
common2::EmulatorOptions options;
|
2021-02-09 16:02:36 +00:00
|
|
|
|
|
|
|
Video & video = GetVideo();
|
|
|
|
const int sw = video.GetFrameBufferBorderlessWidth();
|
|
|
|
const int sh = video.GetFrameBufferBorderlessHeight();
|
|
|
|
|
2021-03-18 19:40:30 +00:00
|
|
|
options.geometry.width = sw * 2;
|
|
|
|
options.geometry.height = sh * 2;
|
2021-02-20 15:21:53 +00:00
|
|
|
options.geometry.x = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
options.geometry.y = SDL_WINDOWPOS_UNDEFINED;
|
2020-10-09 19:13:42 +01:00
|
|
|
const bool run = getEmulatorOptions(argc, argv, "SDL2", options);
|
|
|
|
|
|
|
|
if (!run)
|
|
|
|
return;
|
2019-12-17 14:46:50 +00:00
|
|
|
|
2021-05-23 20:06:36 +01:00
|
|
|
const Logger logger(options.log);
|
2021-03-27 18:47:40 +00:00
|
|
|
g_nMemoryClearType = options.memclear;
|
2021-05-23 20:06:36 +01:00
|
|
|
InitializeFileRegistry(options);
|
2021-03-27 18:47:40 +00:00
|
|
|
|
2021-02-25 16:31:24 +00:00
|
|
|
std::shared_ptr<sa2::SDLFrame> frame;
|
2021-02-06 16:55:08 +00:00
|
|
|
if (options.imgui)
|
|
|
|
{
|
2021-02-25 16:31:24 +00:00
|
|
|
frame.reset(new sa2::SDLImGuiFrame(options));
|
2021-02-06 16:55:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-25 16:31:24 +00:00
|
|
|
frame.reset(new sa2::SDLRendererFrame(options));
|
2021-02-06 16:55:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 20:06:36 +01:00
|
|
|
Paddle::instance.reset(new sa2::Gamepad(0));
|
|
|
|
const Initialisation init(frame);
|
|
|
|
|
2021-02-13 13:09:55 +00:00
|
|
|
if (SDL_GL_SetSwapInterval(options.glSwapInterval))
|
|
|
|
{
|
|
|
|
throw std::runtime_error(SDL_GetError());
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:29:33 +01:00
|
|
|
applyOptions(options);
|
|
|
|
|
2020-11-14 09:14:37 +00:00
|
|
|
const int fps = getRefreshRate();
|
2020-11-14 20:09:12 +00:00
|
|
|
std::cerr << "Video refresh rate: " << fps << " Hz, " << 1000.0 / fps << " ms" << std::endl;
|
2020-10-09 11:50:59 +01:00
|
|
|
|
2020-12-07 13:13:42 +00:00
|
|
|
#ifdef EMULATOR_RUN
|
2020-11-21 20:13:56 +00:00
|
|
|
if (options.benchmark)
|
2020-11-13 11:50:42 +00:00
|
|
|
{
|
2020-11-28 10:01:49 +00:00
|
|
|
// we need to switch off vsync, otherwise FPS is limited to 60
|
|
|
|
// and it will take longer to run
|
2021-02-13 13:09:55 +00:00
|
|
|
if (SDL_GL_SetSwapInterval(0))
|
|
|
|
{
|
|
|
|
throw std::runtime_error(SDL_GetError());
|
|
|
|
}
|
2020-11-28 10:01:49 +00:00
|
|
|
|
2021-02-13 13:09:55 +00:00
|
|
|
const auto redraw = [&frame]{
|
2021-03-14 10:04:14 +00:00
|
|
|
frame->UpdateTexture();
|
|
|
|
frame->RenderPresent();
|
|
|
|
};
|
2020-11-21 20:13:56 +00:00
|
|
|
|
2020-12-28 19:42:04 +00:00
|
|
|
const auto refresh = [redraw, &video]{
|
2021-03-14 10:04:14 +00:00
|
|
|
NTSC_SetVideoMode( video.GetVideoMode() );
|
|
|
|
NTSC_VideoRedrawWholeScreen();
|
|
|
|
redraw();
|
|
|
|
};
|
2020-11-21 20:13:56 +00:00
|
|
|
|
|
|
|
VideoBenchmark(redraw, refresh);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-02-25 16:04:50 +00:00
|
|
|
common2::Timer global;
|
|
|
|
common2::Timer updateTextureTimer;
|
|
|
|
common2::Timer refreshScreenTimer;
|
|
|
|
common2::Timer cpuTimer;
|
|
|
|
common2::Timer eventTimer;
|
|
|
|
common2::Timer frameTimer;
|
2020-11-12 19:51:45 +00:00
|
|
|
|
2020-11-21 20:13:56 +00:00
|
|
|
const std::string globalTag = ". .";
|
|
|
|
std::string updateTextureTimerTag, refreshScreenTimerTag, cpuTimerTag, eventTimerTag;
|
2020-11-12 19:51:45 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
// it does not need to be exact
|
|
|
|
const size_t oneFrame = 1000 / fps;
|
2020-11-13 11:50:42 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
bool quit = false;
|
2020-11-13 20:12:51 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
do
|
2020-11-13 11:50:42 +00:00
|
|
|
{
|
2021-03-21 12:08:30 +00:00
|
|
|
frameTimer.tic();
|
2020-11-13 15:37:10 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
eventTimer.tic();
|
|
|
|
sa2::writeAudio();
|
2021-04-30 20:10:30 +01:00
|
|
|
processEventsUthernet2(5);
|
2021-03-21 12:08:30 +00:00
|
|
|
frame->ProcessEvents(quit);
|
|
|
|
eventTimer.toc();
|
2020-11-13 15:37:10 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
cpuTimer.tic();
|
|
|
|
frame->ExecuteOneFrame(oneFrame);
|
|
|
|
cpuTimer.toc();
|
2020-11-13 15:37:10 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
updateTextureTimer.tic();
|
|
|
|
frame->UpdateTexture();
|
|
|
|
updateTextureTimer.toc();
|
|
|
|
|
|
|
|
if (!options.headless)
|
2020-11-21 15:07:31 +00:00
|
|
|
{
|
2021-03-21 12:08:30 +00:00
|
|
|
refreshScreenTimer.tic();
|
|
|
|
frame->RenderPresent();
|
|
|
|
refreshScreenTimer.toc();
|
|
|
|
}
|
|
|
|
frameTimer.toc();
|
|
|
|
} while (!quit);
|
2020-11-12 19:51:45 +00:00
|
|
|
|
2020-11-21 20:13:56 +00:00
|
|
|
global.toc();
|
2020-11-13 15:37:10 +00:00
|
|
|
|
2021-03-21 12:08:30 +00:00
|
|
|
std::cerr << "Global: " << global << std::endl;
|
|
|
|
std::cerr << "Frame: " << frameTimer << std::endl;
|
|
|
|
std::cerr << "Screen: " << refreshScreenTimer << std::endl;
|
|
|
|
std::cerr << "Texture: " << updateTextureTimer << std::endl;
|
|
|
|
std::cerr << "Events: " << eventTimer << std::endl;
|
|
|
|
std::cerr << "CPU: " << cpuTimer << std::endl;
|
2020-11-13 20:12:51 +00:00
|
|
|
|
2020-11-21 20:13:56 +00:00
|
|
|
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;
|
2021-03-27 13:39:12 +00:00
|
|
|
sa2::stopAudio();
|
2020-11-21 20:13:56 +00:00
|
|
|
}
|
2020-12-07 13:13:42 +00:00
|
|
|
#endif
|
2019-12-17 14:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 19:13:42 +01:00
|
|
|
int main(int argc, const char * argv [])
|
2019-12-17 14:46:50 +00:00
|
|
|
{
|
|
|
|
//First we need to start up SDL, and make sure it went ok
|
2020-11-12 20:33:04 +00:00
|
|
|
const Uint32 flags = SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO | SDL_INIT_TIMER;
|
|
|
|
if (SDL_Init(flags) != 0)
|
2019-12-17 14:46:50 +00:00
|
|
|
{
|
|
|
|
std::cerr << "SDL_Init Error: " << SDL_GetError() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int exit = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-10-09 19:13:42 +01:00
|
|
|
run_sdl(argc, argv);
|
2019-12-17 14:46:50 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
exit = 2;
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
return exit;
|
|
|
|
}
|