SameBoy/SDL/main.c

549 lines
19 KiB
C
Raw Normal View History

2016-03-30 20:07:55 +00:00
#include <stdbool.h>
#include <stdio.h>
2016-07-17 19:43:23 +00:00
#include <signal.h>
#include <SDL2/SDL.h>
2017-10-12 21:02:02 +00:00
#include <Core/gb.h>
2017-05-24 18:44:43 +00:00
#include "utils.h"
2017-10-04 09:43:31 +00:00
#include "gui.h"
2017-12-23 15:29:42 +00:00
#include "shader.h"
2016-08-20 14:51:17 +00:00
#ifndef _WIN32
#define AUDIO_FREQUENCY 96000
#else
/* Windows (well, at least my VM) can't handle 96KHz sound well :( */
/* felsqualle says: For SDL 2.0.6+ using the WASAPI driver, the highest freq.
we can get is 48000. 96000 also works, but always has some faint crackling in
the audio, no matter how high or low I set the buffer length...
Not quite satisfied with that solution, because acc. to SDL2 docs,
96k + WASAPI *should* work. */
#define AUDIO_FREQUENCY 48000
2016-08-20 14:51:17 +00:00
#endif
2016-03-30 20:07:55 +00:00
2017-05-24 18:44:43 +00:00
GB_gameboy_t gb;
static bool paused = false;
2017-12-23 15:41:47 +00:00
static uint32_t pixel_buffer_1[160*144], pixel_buffer_2[160*144];
static uint32_t *active_pixel_buffer = pixel_buffer_1, *previous_pixel_buffer = pixel_buffer_2;
static char *filename = NULL;
static bool should_free_filename = false;
static char *battery_save_path_ptr;
SDL_AudioDeviceID device_id;
static const GB_model_t sdl_to_internal_model[] =
{
[MODEL_DMG] = GB_MODEL_DMG_B,
[MODEL_CGB] = GB_MODEL_CGB_E,
[MODEL_AGB] = GB_MODEL_AGB
};
2017-10-04 09:43:31 +00:00
void set_filename(const char *new_filename, bool new_should_free)
{
if (filename && should_free_filename) {
2017-10-04 10:14:37 +00:00
SDL_free(filename);
2017-10-04 09:43:31 +00:00
}
filename = (char *) new_filename;
should_free_filename = new_should_free;
}
static SDL_AudioSpec want_aspec, have_aspec;
static char *captured_log = NULL;
static void log_capture_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes)
{
size_t current_len = strlen(captured_log);
size_t len_to_add = strlen(string);
captured_log = realloc(captured_log, current_len + len_to_add + 1);
memcpy(captured_log + current_len, string, len_to_add);
captured_log[current_len + len_to_add] = 0;
}
static void start_capturing_logs(void)
{
if (captured_log != NULL) {
free(captured_log);
}
captured_log = malloc(1);
captured_log[0] = 0;
GB_set_log_callback(&gb, log_capture_callback);
}
static const char *end_capturing_logs(bool show_popup, bool should_exit)
{
GB_set_log_callback(&gb, NULL);
if (captured_log[0] == 0) {
free(captured_log);
captured_log = NULL;
}
else {
if (show_popup) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", captured_log, window);
}
if (should_exit) {
exit(1);
}
}
return captured_log;
}
static void handle_events(GB_gameboy_t *gb)
2016-03-30 20:07:55 +00:00
{
2016-07-18 11:37:06 +00:00
#ifdef __APPLE__
#define MODIFIER KMOD_GUI
#else
#define MODIFIER KMOD_CTRL
2016-07-18 11:37:06 +00:00
#endif
2016-03-30 20:07:55 +00:00
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type) {
2016-03-30 20:07:55 +00:00
case SDL_QUIT:
pending_command = GB_SDL_QUIT_COMMAND;
break;
case SDL_DROPFILE: {
2017-10-04 10:14:37 +00:00
set_filename(event.drop.file, true);
pending_command = GB_SDL_NEW_FILE_COMMAND;
break;
}
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
update_viewport();
}
2017-12-22 22:39:04 +00:00
break;
}
2017-12-22 22:39:04 +00:00
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
event.jbutton.button = fix_joypad_button(event.jbutton.button);
if (event.jbutton.button < 4) {
GB_set_key_state(gb, (event.jbutton.button & 1) ? GB_KEY_A : GB_KEY_B,
event.type == SDL_JOYBUTTONDOWN);
}
else if (event.jbutton.button == 8) {
GB_set_key_state(gb, GB_KEY_SELECT, event.type == SDL_JOYBUTTONDOWN);
}
else if (event.jbutton.button == 9) {
GB_set_key_state(gb, GB_KEY_START, event.type == SDL_JOYBUTTONDOWN);
}
2017-12-28 18:22:54 +00:00
else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP) {
GB_set_key_state(gb, GB_KEY_UP, event.type == SDL_JOYBUTTONDOWN);
}
else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN) {
GB_set_key_state(gb, GB_KEY_DOWN, event.type == SDL_JOYBUTTONDOWN);
}
else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT) {
GB_set_key_state(gb, GB_KEY_LEFT, event.type == SDL_JOYBUTTONDOWN);
}
else if (event.jbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT) {
GB_set_key_state(gb, GB_KEY_RIGHT, event.type == SDL_JOYBUTTONDOWN);
}
2017-12-22 22:39:04 +00:00
else if (event.jbutton.button & 1) {
GB_set_turbo_mode(gb, event.type == SDL_JOYBUTTONDOWN, false);
}
2017-12-28 18:22:54 +00:00
2017-12-22 22:39:04 +00:00
else {
bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
2017-12-23 19:11:44 +00:00
if (audio_playing) {
SDL_PauseAudioDevice(device_id, 1);
2017-12-23 19:11:44 +00:00
}
2017-12-22 22:39:04 +00:00
run_gui(true);
if (!audio_playing) {
SDL_PauseAudioDevice(device_id, 0);
2017-12-23 19:11:44 +00:00
}
2017-12-22 22:39:04 +00:00
GB_set_color_correction_mode(gb, configuration.color_correction_mode);
2017-12-23 19:11:44 +00:00
GB_set_highpass_filter_mode(gb, configuration.highpass_mode);
2017-12-22 22:39:04 +00:00
}
break;
case SDL_JOYAXISMOTION:
2017-12-28 18:22:54 +00:00
event.jaxis.axis = fix_joypad_axis(event.jaxis.axis);
if (event.jaxis.axis == 1) {
2017-12-22 22:39:04 +00:00
GB_set_key_state(gb, GB_KEY_DOWN, event.jaxis.value > 0x4000);
GB_set_key_state(gb, GB_KEY_UP, event.jaxis.value < -0x4000);
}
2017-12-28 18:22:54 +00:00
else if (event.jaxis.axis == 0) {
2017-12-22 22:39:04 +00:00
GB_set_key_state(gb, GB_KEY_RIGHT, event.jaxis.value > 0x4000);
GB_set_key_state(gb, GB_KEY_LEFT, event.jaxis.value < -0x4000);
}
break;
case SDL_JOYHATMOTION:
{
uint8_t value = event.jhat.value;
int8_t updown =
value == SDL_HAT_LEFTUP || value == SDL_HAT_UP || value == SDL_HAT_RIGHTUP ? -1 : (value == SDL_HAT_LEFTDOWN || value == SDL_HAT_DOWN || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
int8_t leftright =
value == SDL_HAT_LEFTUP || value == SDL_HAT_LEFT || value == SDL_HAT_LEFTDOWN ? -1 : (value == SDL_HAT_RIGHTUP || value == SDL_HAT_RIGHT || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
GB_set_key_state(gb, GB_KEY_LEFT, leftright == -1);
GB_set_key_state(gb, GB_KEY_RIGHT, leftright == 1);
GB_set_key_state(gb, GB_KEY_UP, updown == -1);
GB_set_key_state(gb, GB_KEY_DOWN, updown == 1);
break;
};
2017-12-22 22:39:04 +00:00
2016-03-30 20:07:55 +00:00
case SDL_KEYDOWN:
2017-10-14 17:24:13 +00:00
switch (event.key.keysym.scancode) {
2017-12-23 19:11:44 +00:00
case SDL_SCANCODE_ESCAPE: {
bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
2017-12-23 19:11:44 +00:00
if (audio_playing) {
SDL_PauseAudioDevice(device_id, 1);
2017-12-23 19:11:44 +00:00
}
2017-10-13 22:41:51 +00:00
run_gui(true);
if (!audio_playing) {
SDL_PauseAudioDevice(device_id, 0);
2017-12-23 19:11:44 +00:00
}
2017-12-22 20:25:53 +00:00
GB_set_color_correction_mode(gb, configuration.color_correction_mode);
2017-12-23 19:11:44 +00:00
GB_set_highpass_filter_mode(gb, configuration.highpass_mode);
2017-10-13 22:41:51 +00:00
break;
2017-12-23 19:11:44 +00:00
}
2017-10-14 17:24:13 +00:00
case SDL_SCANCODE_C:
if (event.type == SDL_KEYDOWN && (event.key.keysym.mod & KMOD_CTRL)) {
GB_debugger_break(gb);
}
break;
2017-10-14 17:24:13 +00:00
case SDL_SCANCODE_R:
if (event.key.keysym.mod & MODIFIER) {
pending_command = GB_SDL_RESET_COMMAND;
}
break;
2017-10-14 17:24:13 +00:00
case SDL_SCANCODE_P:
if (event.key.keysym.mod & MODIFIER) {
paused = !paused;
}
break;
2017-10-14 17:24:13 +00:00
case SDL_SCANCODE_M:
if (event.key.keysym.mod & MODIFIER) {
#ifdef __APPLE__
// Can't override CMD+M (Minimize) in SDL
if (!(event.key.keysym.mod & KMOD_SHIFT)) {
break;
}
#endif
bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
if (audio_playing) {
SDL_PauseAudioDevice(device_id, 1);
}
else if (!audio_playing) {
SDL_PauseAudioDevice(device_id, 0);
}
}
break;
2018-04-18 17:22:08 +00:00
case SDL_SCANCODE_F:
if (event.key.keysym.mod & MODIFIER) {
if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) {
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else {
SDL_SetWindowFullscreen(window, 0);
}
}
break;
default:
/* Save states */
if (event.key.keysym.scancode >= SDL_SCANCODE_1 && event.key.keysym.scancode <= SDL_SCANCODE_0) {
if (event.key.keysym.mod & MODIFIER) {
command_parameter = (event.key.keysym.scancode - SDL_SCANCODE_1 + 1) % 10;
if (event.key.keysym.mod & KMOD_SHIFT) {
pending_command = GB_SDL_LOAD_STATE_COMMAND;
}
else {
pending_command = GB_SDL_SAVE_STATE_COMMAND;
}
}
}
break;
}
case SDL_KEYUP: // Fallthrough
2017-12-22 20:25:53 +00:00
if (event.key.keysym.scancode == configuration.keys[8]) {
GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN, false);
}
else {
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
if (event.key.keysym.scancode == configuration.keys[i]) {
GB_set_key_state(gb, i, event.type == SDL_KEYDOWN);
}
}
2016-03-30 20:07:55 +00:00
}
break;
default:
break;
}
2016-03-30 20:07:55 +00:00
}
}
2016-08-20 14:51:17 +00:00
static void vblank(GB_gameboy_t *gb)
2016-03-30 20:07:55 +00:00
{
2017-12-23 15:41:47 +00:00
if (configuration.blend_frames) {
render_texture(active_pixel_buffer, previous_pixel_buffer);
uint32_t *temp = active_pixel_buffer;
active_pixel_buffer = previous_pixel_buffer;
previous_pixel_buffer = temp;
GB_set_pixels_output(gb, active_pixel_buffer);
}
else {
render_texture(active_pixel_buffer, NULL);
}
handle_events(gb);
2016-03-30 20:07:55 +00:00
}
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
2016-03-30 20:07:55 +00:00
{
return SDL_MapRGB(pixel_format, r, g, b);
2016-03-30 20:07:55 +00:00
}
static void debugger_interrupt(int ignore)
{
if (!GB_is_inited(&gb)) return;
/* ^C twice to exit */
if (GB_debugger_is_stopped(&gb)) {
GB_save_battery(&gb, battery_save_path_ptr);
exit(0);
}
GB_debugger_break(&gb);
}
2016-05-23 19:22:09 +00:00
static void audio_callback(void *gb, Uint8 *stream, int len)
{
if (GB_is_inited(gb)) {
GB_apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t));
}
else {
memset(stream, 0, len);
}
2016-05-23 19:22:09 +00:00
}
2017-05-24 18:44:43 +00:00
static bool handle_pending_command(void)
2016-07-17 20:08:07 +00:00
{
2017-05-24 18:44:43 +00:00
switch (pending_command) {
case GB_SDL_LOAD_STATE_COMMAND:
case GB_SDL_SAVE_STATE_COMMAND: {
char save_path[strlen(filename) + 4];
char save_extension[] = ".s0";
save_extension[2] += command_parameter;
replace_extension(filename, strlen(filename), save_path, save_extension);
start_capturing_logs();
2017-05-24 18:44:43 +00:00
if (pending_command == GB_SDL_LOAD_STATE_COMMAND) {
GB_load_state(&gb, save_path);
}
else {
GB_save_state(&gb, save_path);
}
end_capturing_logs(true, false);
2017-05-24 18:44:43 +00:00
return false;
2016-07-17 20:08:07 +00:00
}
2017-05-24 18:44:43 +00:00
case GB_SDL_RESET_COMMAND:
GB_save_battery(&gb, battery_save_path_ptr);
return true;
2017-05-24 18:44:43 +00:00
case GB_SDL_NO_COMMAND:
return false;
case GB_SDL_NEW_FILE_COMMAND:
return true;
case GB_SDL_QUIT_COMMAND:
GB_save_battery(&gb, battery_save_path_ptr);
exit(0);
2016-07-17 20:08:07 +00:00
}
2017-05-24 18:44:43 +00:00
return false;
2016-07-17 20:08:07 +00:00
}
2017-05-24 18:44:43 +00:00
static void run(void)
2016-03-30 20:07:55 +00:00
{
2017-10-13 22:41:51 +00:00
pending_command = GB_SDL_NO_COMMAND;
restart:
if (GB_is_inited(&gb)) {
GB_switch_model_and_reset(&gb, sdl_to_internal_model[configuration.model]);
2016-03-30 20:07:55 +00:00
}
else {
GB_init(&gb, sdl_to_internal_model[configuration.model]);
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
2017-12-23 15:41:47 +00:00
GB_set_pixels_output(&gb, active_pixel_buffer);
GB_set_rgb_encode_callback(&gb, rgb_encode);
GB_set_sample_rate(&gb, have_aspec.freq);
2017-12-22 20:25:53 +00:00
GB_set_color_correction_mode(&gb, configuration.color_correction_mode);
2017-12-23 19:11:44 +00:00
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
2016-03-30 20:07:55 +00:00
}
bool error = false;
start_capturing_logs();
const char * const boot_roms[] = {"dmg_boot.bin", "cgb_boot.bin", "agb_boot.bin"};
error = GB_load_boot_rom(&gb, executable_relative_path(boot_roms[configuration.model]));
end_capturing_logs(true, error);
start_capturing_logs();
error = GB_load_rom(&gb, filename);
end_capturing_logs(true, error);
2016-03-30 20:07:55 +00:00
2016-07-18 11:37:06 +00:00
size_t path_length = strlen(filename);
2016-07-17 20:08:07 +00:00
/* Configure battery */
char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */
2016-07-18 11:37:06 +00:00
replace_extension(filename, path_length, battery_save_path, ".sav");
battery_save_path_ptr = battery_save_path;
GB_load_battery(&gb, battery_save_path);
2016-07-17 20:08:07 +00:00
/* Configure symbols */
2017-02-20 12:37:15 +00:00
GB_debugger_load_symbol_file(&gb, executable_relative_path("registers.sym"));
2016-07-17 20:08:07 +00:00
char symbols_path[path_length + 5];
2016-07-18 11:37:06 +00:00
replace_extension(filename, path_length, symbols_path, ".sym");
2016-07-17 20:08:07 +00:00
GB_debugger_load_symbol_file(&gb, symbols_path);
2016-05-23 19:22:09 +00:00
/* Run emulation */
while (true) {
if (paused) {
SDL_WaitEvent(NULL);
handle_events(&gb);
}
else {
GB_run(&gb);
}
2017-05-24 18:44:43 +00:00
/* These commands can't run in the handle_event function, because they're not safe in a vblank context. */
if (handle_pending_command()) {
pending_command = GB_SDL_NO_COMMAND;
goto restart;
}
pending_command = GB_SDL_NO_COMMAND;
2016-03-30 20:07:55 +00:00
}
}
2017-12-23 15:29:42 +00:00
2017-12-28 19:46:59 +00:00
static char prefs_path[1024] = {0, };
static void save_configuration(void)
{
FILE *prefs_file = fopen(prefs_path, "wb");
if (prefs_file) {
fwrite(&configuration, 1, sizeof(configuration), prefs_file);
fclose(prefs_file);
}
}
int main(int argc, char **argv)
{
#define str(x) #x
#define xstr(x) str(x)
fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
if (argc > 2) {
fprintf(stderr, "Usage: %s [rom]\n", argv[0]);
exit(1);
}
if (argc == 2) {
2018-01-22 20:25:27 +00:00
filename = argv[1];
}
2016-03-30 20:07:55 +00:00
signal(SIGINT, debugger_interrupt);
SDL_Init(SDL_INIT_EVERYTHING);
2017-12-23 15:29:42 +00:00
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
2017-12-23 15:29:42 +00:00
160 * 2, 144 * 2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_SetWindowMinimumSize(window, 160, 144);
2017-12-23 15:29:42 +00:00
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
2017-12-30 15:01:23 +00:00
GLint major = 0, minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
if (major * 0x100 + minor < 0x302) {
SDL_GL_DeleteContext(gl_context);
gl_context = NULL;
}
2017-12-23 15:29:42 +00:00
if (gl_context == NULL) {
renderer = SDL_CreateRenderer(window, -1, 0);
texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING, 160, 144);
pixel_format = SDL_AllocFormat(SDL_GetWindowPixelFormat(window));
}
else {
pixel_format = SDL_AllocFormat(SDL_PIXELFORMAT_ABGR8888);
}
/* Configure Audio */
memset(&want_aspec, 0, sizeof(want_aspec));
want_aspec.freq = AUDIO_FREQUENCY;
want_aspec.format = AUDIO_S16SYS;
want_aspec.channels = 2;
#if SDL_COMPILEDVERSION >= 2005 && defined(__APPLE__)
/* SDL 2.0.5 on macOS introduced a bug where certain combinations of buffer lengths and frequencies
fail to produce audio correctly. */
want_aspec.samples = 2048;
#else
want_aspec.samples = 512;
#endif
2018-04-22 14:20:11 +00:00
#if SDL_COMPILEDVERSION >= 2006 && defined(_WIN32)
/* SDL 2.0.6 offers WASAPI support which allows for much lower audio buffer lengths which at least
theoretically reduces lagging. */
want_aspec.samples = 32;
#endif
#if SDL_COMPILEDVERSION <= 2005 && defined(_WIN32)
/* Since WASAPI audio was introduced in SDL 2.0.6, we have to lower the audio frequency
to 44100 because otherwise we would get garbled audio output.*/
want_aspec.freq = 44100;
#endif
want_aspec.callback = audio_callback;
want_aspec.userdata = &gb;
device_id = SDL_OpenAudioDevice(0, 0, &want_aspec, &have_aspec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
/* Start Audio */
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
2017-12-28 19:46:59 +00:00
char *prefs_dir = SDL_GetPrefPath("", "SameBoy");
snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir);
SDL_free(prefs_dir);
FILE *prefs_file = fopen(prefs_path, "rb");
if (prefs_file) {
fread(&configuration, 1, sizeof(configuration), prefs_file);
fclose(prefs_file);
}
if (configuration.model >= MODEL_MAX) {
configuration.model = MODEL_CGB;
}
2017-12-28 19:46:59 +00:00
atexit(save_configuration);
2017-12-23 15:29:42 +00:00
if (!init_shader_with_name(&shader, configuration.filter)) {
init_shader_with_name(&shader, "NearestNeighbor");
}
update_viewport();
if (filename == NULL) {
2017-10-13 22:41:51 +00:00
run_gui(false);
}
SDL_PauseAudioDevice(device_id, 0);
run(); // Never returns
return 0;
}