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>
|
|
|
|
|
|
|
|
#include "gb.h"
|
2016-07-17 20:08:07 +00:00
|
|
|
#include "debugger.h"
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-10 12:48:40 +00:00
|
|
|
static bool running = false;
|
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
void GB_update_keys_status(GB_gameboy_t *gb)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
static bool ctrl = false;
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
|
|
|
switch( event.type ){
|
|
|
|
case SDL_QUIT:
|
2016-06-10 12:48:40 +00:00
|
|
|
running = false;
|
2016-03-30 20:07:55 +00:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
case SDL_KEYUP:
|
|
|
|
gb->stopped = false;
|
|
|
|
switch (event.key.keysym.sym) {
|
|
|
|
case SDLK_RIGHT:
|
|
|
|
gb->keys[0] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_LEFT:
|
|
|
|
gb->keys[1] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_UP:
|
|
|
|
gb->keys[2] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_DOWN:
|
|
|
|
gb->keys[3] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_x:
|
|
|
|
gb->keys[4] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_z:
|
|
|
|
gb->keys[5] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_BACKSPACE:
|
|
|
|
gb->keys[6] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_RETURN:
|
|
|
|
gb->keys[7] = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_SPACE:
|
|
|
|
gb->turbo = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_LCTRL:
|
|
|
|
ctrl = event.type == SDL_KEYDOWN;
|
|
|
|
break;
|
|
|
|
case SDLK_c:
|
|
|
|
if (ctrl && event.type == SDL_KEYDOWN) {
|
|
|
|
ctrl = false;
|
|
|
|
gb->debug_stopped = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vblank(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
SDL_Surface *screen = gb->user_data;
|
|
|
|
SDL_Flip(screen);
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_update_keys_status(gb);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-18 17:29:11 +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
|
|
|
|
#warning Unsupported OS: sameboy will only run from CWD
|
|
|
|
/* No OS-specific way, assume running from CWD */
|
|
|
|
getcwd(&path[0], sizeof(path) - 1);
|
|
|
|
return path;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
size_t pos = strlen(path);
|
|
|
|
while (pos) {
|
|
|
|
pos--;
|
|
|
|
if (path[pos] == '/') {
|
|
|
|
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;
|
2016-06-18 17:29:11 +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(screen->format, r, g, b);
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:58:30 +00:00
|
|
|
GB_gameboy_t gb;
|
|
|
|
|
|
|
|
static void debugger_interrupt(int ignore)
|
|
|
|
{
|
2016-07-18 10:10:19 +00:00
|
|
|
/* ^C twice to exit */
|
|
|
|
if (gb.debug_stopped) {
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-04-06 19:58:30 +00:00
|
|
|
gb.debug_stopped = true;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
|
|
|
|
static void audio_callback(void *gb, Uint8 *stream, int len)
|
|
|
|
{
|
2016-06-18 17:29:11 +00:00
|
|
|
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) {
|
2016-06-18 17:29:11 +00:00
|
|
|
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 {
|
2016-06-18 17:29:11 +00:00
|
|
|
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-06-18 17:29:11 +00:00
|
|
|
if (GB_load_rom(&gb, argv[argc - 1])) {
|
2016-03-30 20:07:55 +00:00
|
|
|
perror("Failed to load ROM");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:58:30 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
#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);
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
2016-03-30 20:07:55 +00:00
|
|
|
gb.user_data = screen;
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_set_pixels_output(&gb, screen->pixels);
|
|
|
|
GB_set_rgb_encode_callback(&gb, rgb_encode);
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-06-10 12:48:40 +00:00
|
|
|
size_t path_length = strlen(argv[argc - 1]);
|
|
|
|
|
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 */
|
|
|
|
replace_extension(argv[argc - 1], path_length, battery_save_path, ".sav");
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_load_battery(&gb, battery_save_path);
|
2016-06-10 12:48:40 +00:00
|
|
|
|
2016-07-17 20:08:07 +00:00
|
|
|
/* Configure symbols */
|
|
|
|
char symbols_path[path_length + 5];
|
|
|
|
replace_extension(argv[argc - 1], path_length, symbols_path, ".sym");
|
|
|
|
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));
|
|
|
|
want.freq = 96000;
|
|
|
|
want.format = AUDIO_S16SYS;
|
2016-06-10 12:28:50 +00:00
|
|
|
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-06-18 17:29:11 +00:00
|
|
|
GB_set_sample_rate(&gb, 96000);
|
2016-05-23 19:22:09 +00:00
|
|
|
|
|
|
|
/* Start Audio */
|
|
|
|
SDL_PauseAudio(0);
|
2016-04-08 09:37:09 +00:00
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
/* Run emulation */
|
2016-06-10 12:48:40 +00:00
|
|
|
running = true;
|
|
|
|
while (running) {
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_run(&gb);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2016-05-23 19:22:09 +00:00
|
|
|
SDL_CloseAudio();
|
2016-06-10 12:48:40 +00:00
|
|
|
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_save_battery(&gb, battery_save_path);
|
2016-03-30 20:07:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|