SameBoy/SDL/main.c

325 lines
8.9 KiB
C
Raw Normal View History

2016-03-30 20:07:55 +00:00
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
2016-07-17 19:43:23 +00:00
#include <signal.h>
2016-03-30 20:07:55 +00:00
#include <SDL/SDL.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 :( */
#define AUDIO_FREQUENCY 44100
#include <direct.h>
#include <windows.h>
#define snprintf _snprintf
#endif
2016-03-30 20:07:55 +00:00
#include "gb.h"
static bool running = false;
2016-07-18 11:37:06 +00:00
static char *filename;
static void replace_extension(const char *src, size_t length, char *dest, const char *ext);
GB_gameboy_t gb;
2016-08-20 14:51:17 +00:00
static void GB_update_keys_status(GB_gameboy_t *gb)
2016-03-30 20:07:55 +00:00
{
static bool ctrl = false;
2016-07-18 11:37:06 +00:00
static bool shift = false;
#ifdef __APPLE__
static bool cmd = false;
#endif
2016-03-30 20:07:55 +00:00
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch( event.type ){
case SDL_QUIT:
running = false;
2016-03-30 20:07:55 +00:00
case SDL_KEYDOWN:
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_RIGHT:
GB_set_key_state(gb, GB_KEY_RIGHT, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_LEFT:
GB_set_key_state(gb, GB_KEY_LEFT, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_UP:
GB_set_key_state(gb, GB_KEY_UP, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_DOWN:
GB_set_key_state(gb, GB_KEY_DOWN, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_x:
GB_set_key_state(gb, GB_KEY_A, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_z:
GB_set_key_state(gb, GB_KEY_B, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_BACKSPACE:
GB_set_key_state(gb, GB_KEY_SELECT, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_RETURN:
GB_set_key_state(gb, GB_KEY_START, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_SPACE:
GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN);
2016-03-30 20:07:55 +00:00
break;
case SDLK_LCTRL:
2016-07-18 11:37:06 +00:00
case SDLK_RCTRL:
2016-03-30 20:07:55 +00:00
ctrl = event.type == SDL_KEYDOWN;
break;
2016-07-18 11:37:06 +00:00
case SDLK_LSHIFT:
case SDLK_RSHIFT:
shift = event.type == SDL_KEYDOWN;
break;
#ifdef __APPLE__
case SDLK_LMETA:
case SDLK_RMETA:
cmd = event.type == SDL_KEYDOWN;
break;
#endif
2016-03-30 20:07:55 +00:00
case SDLK_c:
if (ctrl && event.type == SDL_KEYDOWN) {
ctrl = false;
GB_debugger_break(gb);
2016-03-30 20:07:55 +00:00
}
break;
default:
2016-07-18 11:37:06 +00:00
/* Save states */
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
#ifdef __APPLE__
if (cmd) {
#else
if (ctrl) {
#endif
char save_path[strlen(filename) + 4];
char save_extension[] =".s0";
save_extension[2] += event.key.keysym.sym - SDLK_0;
replace_extension(filename, strlen(filename), save_path, save_extension);
if (shift) {
GB_load_state(gb, save_path);
}
else {
GB_save_state(gb, save_path);
}
}
}
2016-03-30 20:07:55 +00:00
break;
}
break;
default:
break;
}
}
}
2016-08-20 14:51:17 +00:00
static void vblank(GB_gameboy_t *gb)
2016-03-30 20:07:55 +00:00
{
SDL_Surface *screen = GB_get_user_data(gb);
2016-03-30 20:07:55 +00:00
SDL_Flip(screen);
GB_update_keys_status(gb);
2016-03-30 20:07:55 +00:00
GB_set_pixels_output(gb, screen->pixels);
2016-03-30 20:07:55 +00:00
}
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
static const char *executable_folder(void)
{
static char path[1024] = {0,};
if (path[0]) {
return path;
}
/* Ugly unportable code! :( */
#ifdef __APPLE__
unsigned int length = sizeof(path) - 1;
_NSGetExecutablePath(&path[0], &length);
#else
#ifdef __linux__
ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1);
assert (length != -1);
#else
2016-08-20 14:51:17 +00:00
#ifdef _WIN32
HMODULE hModule = GetModuleHandle(NULL);
GetModuleFileName(hModule, path, sizeof(path) - 1);
#else
2016-03-30 20:07:55 +00:00
/* No OS-specific way, assume running from CWD */
getcwd(&path[0], sizeof(path) - 1);
return path;
#endif
2016-08-20 14:51:17 +00:00
#endif
2016-03-30 20:07:55 +00:00
#endif
size_t pos = strlen(path);
while (pos) {
pos--;
2016-08-20 14:51:17 +00:00
#ifdef _WIN32
if (path[pos] == '\\') {
#else
2016-03-30 20:07:55 +00:00
if (path[pos] == '/') {
2016-08-20 14:51:17 +00:00
#endif
2016-03-30 20:07:55 +00:00
path[pos] = 0;
break;
}
}
return path;
}
static char *executable_relative_path(const char *filename)
{
static char path[1024];
snprintf(path, sizeof(path), "%s/%s", executable_folder(), filename);
return path;
}
static SDL_Surface *screen = NULL;
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(screen->format, r, g, b);
}
static void debugger_interrupt(int ignore)
{
/* ^C twice to exit */
if (GB_debugger_is_stopped(&gb)) {
exit(0);
}
GB_debugger_break(&gb);
}
2016-05-23 19:22:09 +00:00
static void audio_callback(void *gb, Uint8 *stream, int len)
{
GB_apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t));
2016-05-23 19:22:09 +00:00
}
2016-07-17 20:08:07 +00:00
static void replace_extension(const char *src, size_t length, char *dest, const char *ext)
{
memcpy(dest, src, length);
dest[length] = 0;
/* Remove extension */
for (size_t i = length; i--;) {
if (dest[i] == '/') break;
if (dest[i] == '.') {
dest[i] = 0;
break;
}
}
/* Add new extension */
strcat(dest, ext);
}
2016-03-30 20:07:55 +00:00
#ifdef __APPLE__
extern void cocoa_disable_filtering(void);
#endif
int main(int argc, char **argv)
{
bool dmg = false;
2016-04-01 20:36:43 +00:00
#define str(x) #x
#define xstr(x) str(x)
fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
2016-03-30 20:07:55 +00:00
if (argc == 1 || argc > 3) {
usage:
fprintf(stderr, "Usage: %s [--dmg] rom\n", argv[0]);
exit(1);
}
if (argc == 3) {
if (strcmp(argv[1], "--dmg") == 0) {
dmg = true;
}
else {
goto usage;
}
}
if (dmg) {
GB_init(&gb);
if (GB_load_boot_rom(&gb, executable_relative_path("dmg_boot.bin"))) {
2016-03-30 20:07:55 +00:00
perror("Failed to load boot ROM");
exit(1);
}
}
else {
GB_init_cgb(&gb);
if (GB_load_boot_rom(&gb, executable_relative_path("cgb_boot.bin"))) {
2016-03-30 20:07:55 +00:00
perror("Failed to load boot ROM");
exit(1);
}
}
2016-07-18 11:37:06 +00:00
filename = argv[argc - 1];
if (GB_load_rom(&gb, filename)) {
2016-03-30 20:07:55 +00:00
perror("Failed to load ROM");
exit(1);
}
signal(SIGINT, debugger_interrupt);
2016-03-30 20:07:55 +00:00
SDL_Init( SDL_INIT_EVERYTHING );
screen = SDL_SetVideoMode(160, 144, 32, SDL_SWSURFACE );
2016-08-20 14:51:17 +00:00
SDL_WM_SetCaption("SameBoy v" xstr(VERSION), "SameBoy v" xstr(VERSION));
2016-03-30 20:07:55 +00:00
#ifdef __APPLE__
cocoa_disable_filtering();
#endif
2016-05-23 19:22:09 +00:00
/* Configure Screen */
2016-03-30 20:07:55 +00:00
SDL_LockSurface(screen);
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
GB_set_user_data(&gb, screen);
GB_set_pixels_output(&gb, screen->pixels);
GB_set_rgb_encode_callback(&gb, rgb_encode);
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");
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
/* Configure Audio */
SDL_AudioSpec want, have;
SDL_memset(&want, 0, sizeof(want));
2016-08-20 14:51:17 +00:00
want.freq = AUDIO_FREQUENCY;
2016-05-23 19:22:09 +00:00
want.format = AUDIO_S16SYS;
want.channels = 2;
2016-05-23 19:22:09 +00:00
want.samples = 512;
want.callback = audio_callback;
want.userdata = &gb;
SDL_OpenAudio(&want, &have);
2016-08-20 14:51:17 +00:00
GB_set_sample_rate(&gb, AUDIO_FREQUENCY);
2016-05-23 19:22:09 +00:00
/* Start Audio */
SDL_PauseAudio(0);
2016-05-23 19:22:09 +00:00
/* Run emulation */
running = true;
while (running) {
GB_run(&gb);
2016-03-30 20:07:55 +00:00
}
2016-05-23 19:22:09 +00:00
SDL_CloseAudio();
GB_save_battery(&gb, battery_save_path);
2016-03-30 20:07:55 +00:00
return 0;
}