2016-03-30 20:07:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
|
|
|
|
#include "gb.h"
|
|
|
|
|
|
|
|
void update_keys_status(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
static bool ctrl = false;
|
|
|
|
SDL_Event event;
|
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
|
|
|
switch( event.type ){
|
|
|
|
case SDL_QUIT:
|
|
|
|
exit(0);
|
|
|
|
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);
|
|
|
|
update_keys_status(gb);
|
|
|
|
|
|
|
|
gb_set_pixels_output(gb, screen->pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
static uint32_t rgb_encode(GB_gameboy_t *gb, unsigned char r, unsigned char g, unsigned char b)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
gb.debug_stopped = true;
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
|
|
|
|
static void audio_callback(void *gb, Uint8 *stream, int len)
|
|
|
|
{
|
2016-06-10 12:28:50 +00:00
|
|
|
apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t));
|
2016-05-23 19:22:09 +00:00
|
|
|
}
|
|
|
|
|
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_bios(&gb, executable_relative_path("dmg_boot.bin"))) {
|
|
|
|
perror("Failed to load boot ROM");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
gb_init_cgb(&gb);
|
|
|
|
if (gb_load_bios(&gb, executable_relative_path("cgb_boot.bin"))) {
|
|
|
|
perror("Failed to load boot ROM");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gb_load_rom(&gb, argv[argc - 1])) {
|
|
|
|
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);
|
|
|
|
gb_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
|
|
|
gb.user_data = screen;
|
|
|
|
gb_set_pixels_output(&gb, screen->pixels);
|
|
|
|
gb_set_rgb_encode_callback(&gb, rgb_encode);
|
|
|
|
|
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-04-08 09:37:09 +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-03-30 20:07:55 +00:00
|
|
|
while (true) {
|
|
|
|
gb_run(&gb);
|
|
|
|
}
|
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
/* Won't run unless we change the condition for the above loop */
|
|
|
|
SDL_CloseAudio();
|
2016-03-30 20:07:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|