From 59d4cb4bc2b562d8216099f10902e97cb06d1319 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jan 2018 13:02:07 +0200 Subject: [PATCH 1/9] Make menu key more obvious on the SDL port --- SDL/gui.c | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL/gui.c b/SDL/gui.c index ba2cb73..626dcea 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -853,6 +853,7 @@ void run_gui(bool is_running) switch (gui_state) { case SHOWING_DROP_MESSAGE: + draw_text_centered(pixels, 8, "Press ESC for menu", gui_palette_native[3], gui_palette_native[0], false); draw_text_centered(pixels, 116, "Drop a GB or GBC", gui_palette_native[3], gui_palette_native[0], false); draw_text_centered(pixels, 128, "file to play", gui_palette_native[3], gui_palette_native[0], false); break; From 2cd5c499350a67987f7fe8aff761c13cfc43135e Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jan 2018 13:02:31 +0200 Subject: [PATCH 2/9] Made CGB boot more accurate, added AGB boot --- BootROMs/agb_boot.asm | 2 ++ BootROMs/cgb_boot.asm | 23 ++++++++++++++++++++++- Makefile | 7 ++++--- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 BootROMs/agb_boot.asm diff --git a/BootROMs/agb_boot.asm b/BootROMs/agb_boot.asm new file mode 100644 index 0000000..95a2c78 --- /dev/null +++ b/BootROMs/agb_boot.asm @@ -0,0 +1,2 @@ +AGB EQU 1 +include "cgb_boot.asm" \ No newline at end of file diff --git a/BootROMs/cgb_boot.asm b/BootROMs/cgb_boot.asm index 92f0671..6b0d601 100644 --- a/BootROMs/cgb_boot.asm +++ b/BootROMs/cgb_boot.asm @@ -732,7 +732,28 @@ Preboot: ld a, [InputPalette] and a jr nz, .emulateDMGForCGBGame - ld a, $11 +IF DEF(AGB) + ; Set registers to match the original AGB-CGB boot + ld bc, $1100 + push bc + pop af + ld h, c + ld b, 1 + ld c, c + ld e, $08 + ld l, $7c +ELSE + ; Set registers to match the original CGB boot + ld bc, $1180 + push bc + pop af + ld c, 0 + ld h, c + ld b, c + ld c, c + ld e, $08 + ld l, $7c +ENDC ret .emulateDMGForCGBGame diff --git a/Makefile b/Makefile index 2969a3e..f416358 100755 --- a/Makefile +++ b/Makefile @@ -90,9 +90,9 @@ endif cocoa: $(BIN)/SameBoy.app quicklook: $(BIN)/SameBoy.qlgenerator -sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/background.bmp $(BIN)/SDL/Shaders -bootroms: $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin -tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin +sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/agb_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/background.bmp $(BIN)/SDL/Shaders +bootroms: $(BIN)/BootROMs/agb_boot.bin $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin +tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin $(BIN)/tester/agb_boot.bin all: cocoa sdl tester libretro # Get a list of our source files and their respective object file targets @@ -161,6 +161,7 @@ $(BIN)/SameBoy.app: $(BIN)/SameBoy.app/Contents/MacOS/SameBoy \ Misc/registers.sym \ $(BIN)/SameBoy.app/Contents/Resources/dmg_boot.bin \ $(BIN)/SameBoy.app/Contents/Resources/cgb_boot.bin \ + $(BIN)/SameBoy.app/Contents/Resources/agb_boot.bin \ $(patsubst %.xib,%.nib,$(addprefix $(BIN)/SameBoy.app/Contents/Resources/Base.lproj/,$(shell cd Cocoa;ls *.xib))) \ $(BIN)/SameBoy.qlgenerator \ Shaders From 69a712b07fd1f6ef5c00b7088c64698899b5fe3c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jan 2018 13:16:33 +0200 Subject: [PATCH 3/9] Added AGB as a model option in Cocoa --- Cocoa/Document.m | 33 ++++++++++++++++++++++++++------- Cocoa/MainMenu.xib | 10 ++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index e5fb9ae..6ec45c8 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -11,6 +11,13 @@ /* Todo: The general Objective-C coding style conflicts with SameBoy's. This file needs a cleanup. */ /* Todo: Split into category files! This is so messy!!! */ +enum model { + MODEL_NONE, + MODEL_DMG, + MODEL_CGB, + MODEL_AGB, +}; + @interface Document () { /* NSTextViews freeze the entire app if they're modified too often and too quickly. @@ -43,6 +50,7 @@ NSMutableString *capturedOutput; bool logToSideView; bool shouldClearSideView; + enum model current_model; } @property GBAudioClient *audioClient; @@ -126,6 +134,7 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, - (void) initDMG { + current_model = MODEL_DMG; GB_init(&gb); [self initCommon]; GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"dmg_boot" ofType:@"bin"] UTF8String]); @@ -135,7 +144,14 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, { GB_init_cgb(&gb); [self initCommon]; - GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"cgb_boot" ofType:@"bin"] UTF8String]); + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateAGB"]) { + current_model = MODEL_AGB; + GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"agb_boot" ofType:@"bin"] UTF8String]); + } + else { + current_model = MODEL_CGB; + GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"cgb_boot" ofType:@"bin"] UTF8String]); + } } - (void) initCommon @@ -214,17 +230,20 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, { [self stop]; - if ([sender tag] == 0) { + if ([sender tag] == MODEL_NONE) { GB_reset(&gb); } else { - GB_switch_model_and_reset(&gb, [sender tag] == 2); - GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:[sender tag] == 2? @"cgb_boot" : @"dmg_boot" ofType:@"bin"] UTF8String]); + current_model = (enum model)[sender tag]; + GB_switch_model_and_reset(&gb, current_model != MODEL_DMG); + static NSString * const boot_names[] = {@"dmg_boot", @"cgb_boot", @"agb_boot"}; + GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:boot_names[current_model - 1] ofType:@"bin"] UTF8String]); } if ([sender tag] != 0) { /* User explictly selected a model, save the preference */ - [[NSUserDefaults standardUserDefaults] setBool:[sender tag] == 1 forKey:@"EmulateDMG"]; + [[NSUserDefaults standardUserDefaults] setBool:current_model == MODEL_DMG forKey:@"EmulateDMG"]; + [[NSUserDefaults standardUserDefaults] setBool:current_model== MODEL_AGB forKey:@"EmulateAGB"]; } /* Reload the ROM, SAV and SYM files */ @@ -441,8 +460,8 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, [(NSMenuItem*)anItem setState:(!running) || (GB_debugger_is_stopped(&gb))]; return !GB_debugger_is_stopped(&gb); } - else if ([anItem action] == @selector(reset:) && anItem.tag != 0) { - [(NSMenuItem*)anItem setState:(anItem.tag == 1 && !GB_is_cgb(&gb)) || (anItem.tag == 2 && GB_is_cgb(&gb))]; + else if ([anItem action] == @selector(reset:) && anItem.tag != MODEL_NONE) { + [(NSMenuItem*)anItem setState:anItem.tag == current_model]; } else if ([anItem action] == @selector(toggleBlend:)) { [(NSMenuItem*)anItem setState:self.view.shouldBlendFrameWithPrevious]; diff --git a/Cocoa/MainMenu.xib b/Cocoa/MainMenu.xib index d43f107..18af33e 100644 --- a/Cocoa/MainMenu.xib +++ b/Cocoa/MainMenu.xib @@ -311,18 +311,24 @@ - + - + + + + + + + From 5c16d0e6562e1854beeebfeb85ed2255ec9d7072 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 13 Jan 2018 13:49:20 +0200 Subject: [PATCH 4/9] Added model selection GUI in the SDL port. Closes #24 --- SDL/gui.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++-------- SDL/gui.h | 7 ++++++- SDL/main.c | 46 ++++++++--------------------------------- 3 files changed, 66 insertions(+), 47 deletions(-) diff --git a/SDL/gui.c b/SDL/gui.c index 626dcea..5b04017 100644 --- a/SDL/gui.c +++ b/SDL/gui.c @@ -60,7 +60,8 @@ configuration_t configuration = .color_correction_mode = GB_COLOR_CORRECTION_EMULATE_HARDWARE, .highpass_mode = GB_HIGHPASS_ACCURATE, .scaling_mode = GB_SDL_SCALING_INTEGER_FACTOR, - .blend_frames = true + .blend_frames = true, + .model = MODEL_CGB }; @@ -72,7 +73,6 @@ static const char *help[] ={ " Open Menu: Escape\n" " Reset: " MODIFIER_NAME "+R\n" " Pause: " MODIFIER_NAME "+P\n" -" Toggle DMG/CGB: " MODIFIER_NAME "+T\n" " Save state: " MODIFIER_NAME "+(0-9)\n" " Load state: " MODIFIER_NAME "+" SHIFT_STRING "+(0-9)\n" #ifdef __APPLE__ @@ -225,6 +225,7 @@ static void item_help(unsigned index) gui_state = SHOWING_HELP; } +static void enter_emulation_menu(unsigned index); static void enter_graphics_menu(unsigned index); static void enter_controls_menu(unsigned index); static void enter_joypad_menu(unsigned index); @@ -232,6 +233,7 @@ static void enter_audio_menu(unsigned index); static const struct menu_item paused_menu[] = { {"Resume", NULL}, + {"Enumlation Options", enter_emulation_menu}, {"Graphic Options", enter_graphics_menu}, {"Audio Options", enter_audio_menu}, {"Keyboard", enter_controls_menu}, @@ -243,6 +245,49 @@ static const struct menu_item paused_menu[] = { static const struct menu_item *const nonpaused_menu = &paused_menu[1]; +static void return_to_root_menu(unsigned index) +{ + current_menu = root_menu; + current_selection = 0; +} + +static void cycle_model(unsigned index) +{ + + configuration.model++; + if (configuration.model == MODEL_MAX) { + configuration.model = 0; + } + pending_command = GB_SDL_RESET_COMMAND; +} + +static void cycle_model_backwards(unsigned index) +{ + if (configuration.model == 0) { + configuration.model = MODEL_MAX; + } + configuration.model--; + pending_command = GB_SDL_RESET_COMMAND; +} + +const char *current_model_string(unsigned index) +{ + return (const char *[]){"Game Boy", "Game Boy Color", "Game Boy Advance"} + [configuration.model]; +} + +static const struct menu_item emulation_menu[] = { + {"Emulated Model:", cycle_model, current_model_string, cycle_model_backwards}, + {"Back", return_to_root_menu}, + {NULL,} +}; + +static void enter_emulation_menu(unsigned index) +{ + current_menu = emulation_menu; + current_selection = 0; +} + const char *current_scaling_mode(unsigned index) { return (const char *[]){"Fill Entire Window", "Retain Aspect Ratio", "Retain Integer Factor"} @@ -374,12 +419,6 @@ const char *current_filter_name(unsigned index) return shaders[i].display_name; } -static void return_to_root_menu(unsigned index) -{ - current_menu = root_menu; - current_selection = 0; -} - static void toggle_blend_frames(unsigned index) { configuration.blend_frames ^= true; @@ -808,9 +847,12 @@ void run_gui(bool is_running) current_selection--; should_render = true; } - else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN) { + else if (event.key.keysym.scancode == SDL_SCANCODE_RETURN && !current_menu[current_selection].backwards_handler) { if (current_menu[current_selection].handler) { current_menu[current_selection].handler(current_selection); + if (pending_command == GB_SDL_RESET_COMMAND && !is_running) { + pending_command = GB_SDL_NO_COMMAND; + } if (pending_command) { if (!is_running && pending_command == GB_SDL_QUIT_COMMAND) { exit(0); diff --git a/SDL/gui.h b/SDL/gui.h index b68d0d1..2c99b9f 100644 --- a/SDL/gui.h +++ b/SDL/gui.h @@ -25,7 +25,6 @@ enum pending_command { GB_SDL_LOAD_STATE_COMMAND, GB_SDL_RESET_COMMAND, GB_SDL_NEW_FILE_COMMAND, - GB_SDL_TOGGLE_MODEL_COMMAND, GB_SDL_QUIT_COMMAND, }; @@ -44,6 +43,12 @@ typedef struct { bool swap_joysticks_bits_1_and_2; char filter[32]; + enum { + MODEL_DMG, + MODEL_CGB, + MODEL_AGB, + MODEL_MAX, + } model; } configuration_t; extern configuration_t configuration; diff --git a/SDL/main.c b/SDL/main.c index 7db2df6..b819c9d 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -14,7 +14,6 @@ #endif GB_gameboy_t gb; -static bool dmg = false; static bool paused = false; 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; @@ -184,12 +183,6 @@ static void handle_events(GB_gameboy_t *gb) pending_command = GB_SDL_RESET_COMMAND; } break; - - case SDL_SCANCODE_T: - if (event.key.keysym.mod & MODIFIER) { - pending_command = GB_SDL_TOGGLE_MODEL_COMMAND; - } - break; case SDL_SCANCODE_P: if (event.key.keysym.mod & MODIFIER) { @@ -308,8 +301,7 @@ static bool handle_pending_command(void) } case GB_SDL_RESET_COMMAND: - GB_reset(&gb); - return false; + return true; case GB_SDL_NO_COMMAND: return false; @@ -317,10 +309,6 @@ static bool handle_pending_command(void) case GB_SDL_NEW_FILE_COMMAND: return true; - case GB_SDL_TOGGLE_MODEL_COMMAND: - dmg = !dmg; - return true; - case GB_SDL_QUIT_COMMAND: GB_save_battery(&gb, battery_save_path_ptr); exit(0); @@ -333,10 +321,10 @@ static void run(void) pending_command = GB_SDL_NO_COMMAND; restart: if (GB_is_inited(&gb)) { - GB_switch_model_and_reset(&gb, !dmg); + GB_switch_model_and_reset(&gb, configuration.model != MODEL_DMG); } else { - if (dmg) { + if (configuration.model == MODEL_DMG) { GB_init(&gb); } else { @@ -353,12 +341,8 @@ restart: bool error = false; start_capturing_logs(); - if (dmg) { - error = GB_load_boot_rom(&gb, executable_relative_path("dmg_boot.bin")); - } - else { - error = GB_load_boot_rom(&gb, executable_relative_path("cgb_boot.bin")); - } + 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(); @@ -416,25 +400,13 @@ int main(int argc, char **argv) #define xstr(x) str(x) fprintf(stderr, "SameBoy v" xstr(VERSION) "\n"); - if (argc > 3) { -usage: - fprintf(stderr, "Usage: %s [--dmg] [rom]\n", argv[0]); + if (argc > 2) { + fprintf(stderr, "Usage: %s [rom]\n", argv[0]); exit(1); } - for (unsigned i = 1; i < argc; i++) { - if (strcmp(argv[i], "--dmg") == 0) { - if (dmg) { - goto usage; - } - dmg = true; - } - else if (!filename) { - filename = argv[i]; - } - else { - goto usage; - } + if (argc == 2) { + filename = argv[2]; } signal(SIGINT, debugger_interrupt); From 130c7c28c2325ca2bb01ef90c02ef38be9774f31 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Mon, 15 Jan 2018 22:23:20 +0200 Subject: [PATCH 5/9] Re-do the way the libretro port does audio. Audio is now sent to libretro at 384KHz, which is then resampled to whatever rate the user configured. --- libretro/libretro.c | 644 ++++++++++++++++++++++---------------------- 1 file changed, 322 insertions(+), 322 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index b21010e..6379909 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -6,7 +6,8 @@ #include #include -#define AUDIO_FREQUENCY 44100 +#define AUDIO_FREQUENCY 384000 +#define FRAME_RATE (0x400000 / 70224.0) #ifdef _WIN32 #include @@ -18,9 +19,9 @@ #include "libretro.h" #ifdef _WIN32 - char slash = '\\'; +char slash = '\\'; #else - char slash = '/'; +char slash = '/'; #endif #define VIDEO_WIDTH 160 @@ -39,7 +40,7 @@ static retro_audio_sample_batch_t audio_batch_cb; static retro_input_poll_t input_poll_cb; static retro_input_state_t input_state_cb; -signed short soundbuf[1024*2]; +signed short soundbuf[1024 * 2]; char retro_system_directory[4096]; char retro_save_directory[4096]; @@ -52,18 +53,18 @@ extern const unsigned dmg_boot_length, cgb_boot_length; static void fallback_log(enum retro_log_level level, const char *fmt, ...) { - (void)level; - va_list va; - va_start(va, fmt); - vfprintf(stderr, fmt, va); - va_end(va); + (void)level; + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); } 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; @@ -72,7 +73,7 @@ static void replace_extension(const char *src, size_t length, char *dest, const break; } } - + /* Add new extension */ strcat(dest, ext); } @@ -81,38 +82,47 @@ static struct retro_rumble_interface rumble; static void GB_update_keys_status(GB_gameboy_t *gb) { - - input_poll_cb(); - - GB_set_key_state(gb, GB_KEY_RIGHT,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)); - GB_set_key_state(gb, GB_KEY_LEFT, input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)); - GB_set_key_state(gb, GB_KEY_UP,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP) ); - GB_set_key_state(gb, GB_KEY_DOWN,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)); - GB_set_key_state(gb, GB_KEY_A,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A) ); - GB_set_key_state(gb, GB_KEY_B,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B) ); - GB_set_key_state(gb, GB_KEY_SELECT,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)); - GB_set_key_state(gb, GB_KEY_START,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START) ); - - if (gb->rumble_state) - rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535); - else - rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 0); - + + input_poll_cb(); + + GB_set_key_state(gb, GB_KEY_RIGHT,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT)); + GB_set_key_state(gb, GB_KEY_LEFT, input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT)); + GB_set_key_state(gb, GB_KEY_UP,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP) ); + GB_set_key_state(gb, GB_KEY_DOWN,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN)); + GB_set_key_state(gb, GB_KEY_A,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A) ); + GB_set_key_state(gb, GB_KEY_B,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B) ); + GB_set_key_state(gb, GB_KEY_SELECT,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT)); + GB_set_key_state(gb, GB_KEY_START,input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START) ); + + if (gb->rumble_state) + rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535); + else + rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 0); + } static void audio_callback(void *gb) { - GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, (float)AUDIO_FREQUENCY / 59.72); - audio_batch_cb(soundbuf, (float)AUDIO_FREQUENCY / 59.72); + size_t length = GB_apu_get_current_buffer_length(gb); + + while (length > sizeof(soundbuf) / 4) + { + GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, 1024); + audio_batch_cb(soundbuf, 1024); + length -= 1024; + } + if (length) { + GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, length); + audio_batch_cb(soundbuf, length); + } } static void vblank(GB_gameboy_t *gb) { - GB_update_keys_status(gb); - GB_set_pixels_output(gb, frame_buf); - audio_callback(gb); + GB_update_keys_status(gb); + audio_callback(gb); } static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) @@ -134,77 +144,77 @@ static retro_environment_t environ_cb; void retro_init(void) { - frame_buf = (uint32_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t)); - const char *dir = NULL; - - if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) - snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir); - else - snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", "."); - - if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) - snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir); - else - snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", "."); + frame_buf = (uint32_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t)); + const char *dir = NULL; + + if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) + snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir); + else + snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", "."); + + if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) + snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir); + else + snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", "."); } void retro_deinit(void) { - free(frame_buf); - frame_buf = NULL; + free(frame_buf); + frame_buf = NULL; } unsigned retro_api_version(void) { - return RETRO_API_VERSION; + return RETRO_API_VERSION; } void retro_set_controller_port_device(unsigned port, unsigned device) { - log_cb(RETRO_LOG_INFO, "Plugging device %u into port %u.\n", device, port); + log_cb(RETRO_LOG_INFO, "Plugging device %u into port %u.\n", device, port); } void retro_get_system_info(struct retro_system_info *info) { - memset(info, 0, sizeof(*info)); - info->library_name = "SameBoy"; + memset(info, 0, sizeof(*info)); + info->library_name = "SameBoy"; #ifdef GIT_VERSION - info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION; + info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION; #else - info->library_version = SAMEBOY_CORE_VERSION; + info->library_version = SAMEBOY_CORE_VERSION; #endif - info->need_fullpath = true; - info->valid_extensions = "gb|gbc"; + info->need_fullpath = true; + info->valid_extensions = "gb|gbc"; } void retro_get_system_av_info(struct retro_system_av_info *info) { - struct retro_game_geometry geom = { VIDEO_WIDTH, VIDEO_HEIGHT,VIDEO_WIDTH, VIDEO_HEIGHT ,160.0 / 144.0 }; - struct retro_system_timing timing = { 59.72, 44100.0 }; - - info->geometry = geom; - info->timing = timing; - + struct retro_game_geometry geom = { VIDEO_WIDTH, VIDEO_HEIGHT,VIDEO_WIDTH, VIDEO_HEIGHT ,160.0 / 144.0 }; + struct retro_system_timing timing = { FRAME_RATE, AUDIO_FREQUENCY }; + + info->geometry = geom; + info->timing = timing; + } void retro_set_environment(retro_environment_t cb) { - environ_cb = cb; - - if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) - log_cb = logging.log; - else - log_cb = fallback_log; - - static const struct retro_controller_description controllers[] = { - { "Nintendo Gameboy", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) }, - }; - - static const struct retro_controller_info ports[] = { - { controllers, 1 }, - { NULL, 0 }, - }; - cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); + environ_cb = cb; + + if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) + log_cb = logging.log; + else + log_cb = fallback_log; + + static const struct retro_controller_description controllers[] = { + { "Nintendo Gameboy", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) }, + }; + + static const struct retro_controller_info ports[] = { + { controllers, 1 }, + { NULL, 0 }, + }; + cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); } void retro_set_audio_sample(retro_audio_sample_t cb) @@ -213,316 +223,306 @@ void retro_set_audio_sample(retro_audio_sample_t cb) void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) { - audio_batch_cb = cb; + audio_batch_cb = cb; } void retro_set_input_poll(retro_input_poll_t cb) { - input_poll_cb = cb; + input_poll_cb = cb; } void retro_set_input_state(retro_input_state_t cb) { - input_state_cb = cb; + input_state_cb = cb; } void retro_set_video_refresh(retro_video_refresh_t cb) { - video_cb = cb; + video_cb = cb; } void retro_reset(void) { - GB_reset(&gb); + GB_reset(&gb); } static void check_variables(void) { - struct retro_variable var = {0}; - - var.key = "sameboy_color_correction_mode"; - var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb)) - { - if (strcmp(var.value, "off") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED); - else if (strcmp(var.value, "correct curves") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES); - else if (strcmp(var.value, "emulate hardware") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); - else if (strcmp(var.value, "preserve brightness") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - } - - var.key = "sameboy_high_pass_filter_mode"; - var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { - if (strcmp(var.value, "off") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF); - else if (strcmp(var.value, "accurate") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE); - else if (strcmp(var.value, "remove dc offset") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET); - } + struct retro_variable var = {0}; + + var.key = "sameboy_color_correction_mode"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb)) + { + if (strcmp(var.value, "off") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED); + else if (strcmp(var.value, "correct curves") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES); + else if (strcmp(var.value, "emulate hardware") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); + else if (strcmp(var.value, "preserve brightness") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + } + + var.key = "sameboy_high_pass_filter_mode"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + if (strcmp(var.value, "off") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF); + else if (strcmp(var.value, "accurate") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE); + else if (strcmp(var.value, "remove dc offset") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET); + } } void retro_run(void) { - static int frames; - size_t samples; - - bool updated = false; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) - check_variables(); - samples = GB_apu_get_current_buffer_length(&gb); - if (!(frames < (samples / 35112))) - { - GB_run_frame(&gb); - frames ++; - } - else - frames = 0; - - video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); + bool updated = false; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) + check_variables(); + GB_run_frame(&gb); + + video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); } bool retro_load_game(const struct retro_game_info *info) { - struct retro_input_descriptor desc[] = { - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, - { 0 }, - }; - - environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); - - enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; - if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) - { - log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n"); - return false; - } - - snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); - - char buf[256]; - int err = 0; - if (!strstr(info->path, "gbc")) - { - GB_init(&gb); - snprintf(buf, sizeof(buf), "%s%cdmg_boot.bin", retro_system_directory, slash); - log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - err = GB_load_boot_rom(&gb, buf); - - if (err) { - GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length); - err = 0; - } - } - else - { - GB_init_cgb(&gb); - snprintf(buf, sizeof(buf), "%s%ccgb_boot.bin", retro_system_directory, slash); - log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - err = GB_load_boot_rom(&gb, buf); - - if (err) { - GB_load_boot_rom_from_buffer(&gb, cgb_boot, cgb_boot_length); - err = 0; - } - } - if (err) - log_cb(RETRO_LOG_INFO, "Failed to load boot ROM %s %d\n", buf, err); - (void)info; - - if (GB_load_rom(&gb,info->path)) { + struct retro_input_descriptor desc[] = { + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, + { 0 }, + }; + + environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); + + enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; + if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) + { + log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n"); + return false; + } + + snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); + + char buf[256]; + int err = 0; + if (!strstr(info->path, "gbc")) + { + GB_init(&gb); + snprintf(buf, sizeof(buf), "%s%cdmg_boot.bin", retro_system_directory, slash); + log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); + err = GB_load_boot_rom(&gb, buf); + + if (err) { + GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length); + err = 0; + } + } + else + { + GB_init_cgb(&gb); + snprintf(buf, sizeof(buf), "%s%ccgb_boot.bin", retro_system_directory, slash); + log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); + err = GB_load_boot_rom(&gb, buf); + + if (err) { + GB_load_boot_rom_from_buffer(&gb, cgb_boot, cgb_boot_length); + err = 0; + } + } + if (err) + log_cb(RETRO_LOG_INFO, "Failed to load boot ROM %s %d\n", buf, err); + (void)info; + + if (GB_load_rom(&gb,info->path)) { perror("Failed to load ROM"); exit(1); - } - - GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); - GB_set_user_data(&gb, (void*)NULL); - GB_set_pixels_output(&gb,(unsigned int*)frame_buf); - GB_set_rgb_encode_callback(&gb, rgb_encode); - - size_t path_length = strlen(retro_game_path); - + } + + GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); + GB_set_user_data(&gb, (void*)NULL); + GB_set_pixels_output(&gb,(unsigned int*)frame_buf); + GB_set_rgb_encode_callback(&gb, rgb_encode); + + size_t path_length = strlen(retro_game_path); + #ifndef DISABLE_DEBUGGER - { - char TMPC[512]; - sprintf(TMPC,"%s/registers.sym",retro_system_directory); - GB_debugger_load_symbol_file(&gb, TMPC); - } + { + char TMPC[512]; + sprintf(TMPC,"%s/registers.sym",retro_system_directory); + GB_debugger_load_symbol_file(&gb, TMPC); + } #endif - - replace_extension(retro_game_path, path_length, symbols_path, ".sym"); - + + replace_extension(retro_game_path, path_length, symbols_path, ".sym"); + #ifndef DISABLE_DEBUGGER - GB_debugger_load_symbol_file(&gb, symbols_path); + GB_debugger_load_symbol_file(&gb, symbols_path); #endif - - GB_set_sample_rate(&gb, AUDIO_FREQUENCY); - - struct retro_memory_descriptor descs[7]; - size_t size; - uint16_t bank; - - memset(descs, 0, sizeof(descs)); - - descs[0].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_IE, &size, &bank); - descs[0].start = 0xFFFF; - descs[0].len = 1; - - descs[1].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_HRAM, &size, &bank); - descs[1].start = 0xFF80; - descs[1].len = 0x0080; - - descs[2].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_RAM, &size, &bank); - descs[2].start = 0xC000; - descs[2].len = 0x2000; - - descs[3].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank); - descs[3].start = 0xA000; - descs[3].len = 0x2000; - - descs[4].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_VRAM, &size, &bank); - descs[4].start = 0x8000; - descs[4].len = 0x2000; - - descs[5].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_ROM, &size, &bank); - descs[5].start = 0x0000; - descs[5].len = 0x4000; - descs[5].flags = RETRO_MEMDESC_CONST; - - descs[6].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_OAM, &size, &bank); - descs[6].start = 0xFE00; - descs[6].len = 0x00A0; - - struct retro_memory_map mmaps; - mmaps.descriptors = descs; - mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]); - environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); - - bool yes = true; - environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes); - - if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) - log_cb(RETRO_LOG_INFO, "Rumble environment supported.\n"); - else - log_cb(RETRO_LOG_INFO, "Rumble environment not supported.\n"); - - static struct retro_variable vars_cgb[] = { - { "sameboy_color_correction_mode", "Color Correction; off|correct curves|emulate hardware|preserve brightness" }, - { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, - { NULL } - }; - - static struct retro_variable vars_dmg[] = { - { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, - { NULL } - }; - - if (GB_is_cgb(&gb)) - environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_cgb); - else - environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_dmg); - check_variables(); - - return true; + + GB_set_sample_rate(&gb, AUDIO_FREQUENCY); + + struct retro_memory_descriptor descs[7]; + size_t size; + uint16_t bank; + + memset(descs, 0, sizeof(descs)); + + descs[0].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_IE, &size, &bank); + descs[0].start = 0xFFFF; + descs[0].len = 1; + + descs[1].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_HRAM, &size, &bank); + descs[1].start = 0xFF80; + descs[1].len = 0x0080; + + descs[2].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_RAM, &size, &bank); + descs[2].start = 0xC000; + descs[2].len = 0x2000; + + descs[3].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank); + descs[3].start = 0xA000; + descs[3].len = 0x2000; + + descs[4].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_VRAM, &size, &bank); + descs[4].start = 0x8000; + descs[4].len = 0x2000; + + descs[5].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_ROM, &size, &bank); + descs[5].start = 0x0000; + descs[5].len = 0x4000; + descs[5].flags = RETRO_MEMDESC_CONST; + + descs[6].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_OAM, &size, &bank); + descs[6].start = 0xFE00; + descs[6].len = 0x00A0; + + struct retro_memory_map mmaps; + mmaps.descriptors = descs; + mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]); + environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); + + bool yes = true; + environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes); + + if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) + log_cb(RETRO_LOG_INFO, "Rumble environment supported.\n"); + else + log_cb(RETRO_LOG_INFO, "Rumble environment not supported.\n"); + + static struct retro_variable vars_cgb[] = { + { "sameboy_color_correction_mode", "Color Correction; off|correct curves|emulate hardware|preserve brightness" }, + { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, + { NULL } + }; + + static struct retro_variable vars_dmg[] = { + { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, + { NULL } + }; + + if (GB_is_cgb(&gb)) + environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_cgb); + else + environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_dmg); + check_variables(); + + return true; } void retro_unload_game(void) { - GB_free(&gb); + GB_free(&gb); } unsigned retro_get_region(void) { - return RETRO_REGION_NTSC; + return RETRO_REGION_NTSC; } bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num) { - return false; + return false; } size_t retro_serialize_size(void) { - return GB_get_save_state_size(&gb); + return GB_get_save_state_size(&gb); } bool retro_serialize(void *data, size_t size) { - GB_save_state_to_buffer(&gb, (uint8_t*) data); - if (data) - return true; - else - return false; + GB_save_state_to_buffer(&gb, (uint8_t*) data); + if (data) + return true; + else + return false; } bool retro_unserialize(const void *data, size_t size) { - if (GB_load_state_from_buffer(&gb, (uint8_t*) data, size) == 0) - return true; - else - return false; + if (GB_load_state_from_buffer(&gb, (uint8_t*) data, size) == 0) + return true; + else + return false; } void *retro_get_memory_data(unsigned type) { - void* data; - switch(type) - { - case RETRO_MEMORY_SAVE_RAM: - if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) - data = gb.mbc_ram; - else + void* data; + switch(type) + { + case RETRO_MEMORY_SAVE_RAM: + if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) + data = gb.mbc_ram; + else + data = NULL; + break; + case RETRO_MEMORY_RTC: + if(gb.cartridge_type->has_battery) + data = &gb.rtc_real; + else + data = NULL; + break; + default: data = NULL; - break; - case RETRO_MEMORY_RTC: - if(gb.cartridge_type->has_battery) - data = &gb.rtc_real; - else - data = NULL; - break; - default: - data = NULL; - break; - } - - return data; + break; + } + + return data; } size_t retro_get_memory_size(unsigned type) { - size_t size; - switch(type) - { - case RETRO_MEMORY_SAVE_RAM: - if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) - size = gb.mbc_ram_size; - else + size_t size; + switch(type) + { + case RETRO_MEMORY_SAVE_RAM: + if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) + size = gb.mbc_ram_size; + else + size = 0; + break; + case RETRO_MEMORY_RTC: + if(gb.cartridge_type->has_battery) + size = sizeof (gb.rtc_real); + else + size = 0; + break; + default: size = 0; - break; - case RETRO_MEMORY_RTC: - if(gb.cartridge_type->has_battery) - size = sizeof (gb.rtc_real); - else - size = 0; - break; - default: - size = 0; - break; - } - - return size; + break; + } + + return size; } void retro_cheat_reset(void) @@ -530,8 +530,8 @@ void retro_cheat_reset(void) void retro_cheat_set(unsigned index, bool enabled, const char *code) { - (void)index; - (void)enabled; - (void)code; + (void)index; + (void)enabled; + (void)code; } From 37906bcd1f12d8b5199a9bb3fe11be3004e1036c Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jan 2018 00:47:46 +0200 Subject: [PATCH 6/9] Fixed sound pops in Super Mario Land 2. --- Core/apu.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Core/apu.c b/Core/apu.c index 8d5b3e7..363fb9b 100755 --- a/Core/apu.c +++ b/Core/apu.c @@ -173,7 +173,9 @@ void GB_apu_div_event(GB_gameboy_t *gb) gb->apu.square_channels[i].volume_countdown = nrx2 & 7; - update_square_sample(gb, i); + if (gb->apu.is_active[i]) { + update_square_sample(gb, i); + } } } } @@ -192,10 +194,12 @@ void GB_apu_div_event(GB_gameboy_t *gb) gb->apu.noise_channel.volume_countdown = nr42 & 7; - update_sample(gb, GB_NOISE, - (gb->apu.noise_channel.lfsr & 1) ? - gb->apu.noise_channel.current_volume : 0, - 0); + if (gb->apu.is_active[GB_NOISE]) { + update_sample(gb, GB_NOISE, + (gb->apu.noise_channel.lfsr & 1) ? + gb->apu.noise_channel.current_volume : 0, + 0); + } } } } @@ -748,8 +752,6 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) case GB_IO_NR44: { if (value & 0x80) { - gb->apu.noise_channel.lfsr = 0; - gb->apu.noise_channel.sample_countdown = (gb->apu.noise_channel.sample_length) * 2 + 6 - gb->apu.lf_div; /* I'm COMPLETELY unsure about this logic, but it passes all relevant tests. @@ -777,7 +779,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) gb->apu.noise_channel.current_volume : 0, 0); } - + gb->apu.noise_channel.lfsr = 0; gb->apu.noise_channel.volume_countdown = gb->io_registers[GB_IO_NR42] & 7; if ((gb->io_registers[GB_IO_NR42] & 0xF8) != 0) { @@ -821,7 +823,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) size_t GB_apu_get_current_buffer_length(GB_gameboy_t *gb) { - return gb->apu_output.buffer_position; + return gb->apu_output.buffer_position; } void GB_set_sample_rate(GB_gameboy_t *gb, unsigned int sample_rate) From 09dd47c6de9ee9d00728d06eb4cf8468300904bc Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 19 Jan 2018 19:56:39 +0200 Subject: [PATCH 7/9] =?UTF-8?q?Fixed=20unintentional=20delay=20in=20NR50?= =?UTF-8?q?=20and=20NR51=E2=80=99s=20effects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/apu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Core/apu.c b/Core/apu.c index 363fb9b..69bc5ea 100755 --- a/Core/apu.c +++ b/Core/apu.c @@ -469,6 +469,7 @@ void GB_apu_write(GB_gameboy_t *gb, uint8_t reg, uint8_t value) /* Globals */ case GB_IO_NR50: case GB_IO_NR51: + gb->io_registers[reg] = value; /* These registers affect the output of all 4 channels (but not the output of the PCM registers).*/ /* We call update_samples with the current value so the APU output is updated with the new outputs */ for (unsigned i = GB_N_CHANNELS; i--;) { From af143b0420ab6ccc34f907cc3b8120c678149c09 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Sat, 20 Jan 2018 00:06:33 +0200 Subject: [PATCH 8/9] Added model selection to libretro, including GBA. Closes #23. --- libretro/Makefile.common | 3 +- libretro/libretro.c | 275 +++++++++++++++++---------------------- 2 files changed, 125 insertions(+), 153 deletions(-) diff --git a/libretro/Makefile.common b/libretro/Makefile.common index 4ba7482..8e5af51 100644 --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -11,8 +11,9 @@ SOURCES_C := $(CORE_DIR)/Core/gb.c \ $(CORE_DIR)/Core/z80_cpu.c \ $(CORE_DIR)/Core/joypad.c \ $(CORE_DIR)/Core/save_state.c \ + $(CORE_DIR)/libretro/agb_boot.c \ $(CORE_DIR)/libretro/cgb_boot.c \ - $(CORE_DIR)/libretro/dmg_boot.c \ + $(CORE_DIR)/libretro/dmg_boot.c \ $(CORE_DIR)/libretro/libretro.c ifeq ($(HAVE_DEBUGGER), 1) diff --git a/libretro/libretro.c b/libretro/libretro.c index 6379909..5889b19 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -19,9 +19,9 @@ #include "libretro.h" #ifdef _WIN32 -char slash = '\\'; +static const char slash = '\\'; #else -char slash = '/'; +static const char slash = '/'; #endif #define VIDEO_WIDTH 160 @@ -31,6 +31,14 @@ char slash = '/'; char battery_save_path[512]; char symbols_path[512]; +enum model { + MODEL_DMG, + MODEL_CGB, + MODEL_AGB +}; + +static enum model model = MODEL_CGB; + static uint32_t *frame_buf; static struct retro_log_callback logging; static retro_log_printf_t log_cb; @@ -48,8 +56,8 @@ char retro_game_path[4096]; int RLOOP=1; GB_gameboy_t gb; -extern const unsigned char dmg_boot[], cgb_boot[]; -extern const unsigned dmg_boot_length, cgb_boot_length; +extern const unsigned char dmg_boot[], cgb_boot[], agb_boot[]; +extern const unsigned dmg_boot_length, cgb_boot_length, agb_boot_length; static void fallback_log(enum retro_log_level level, const char *fmt, ...) { @@ -60,24 +68,6 @@ static void fallback_log(enum retro_log_level level, const char *fmt, ...) va_end(va); } -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); -} - static struct retro_rumble_interface rumble; static void GB_update_keys_status(GB_gameboy_t *gb) @@ -130,16 +120,6 @@ static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) return r<<16|g<<8|b; } -#ifndef DISABLE_DEBUGGER -static void debugger_interrupt(int ignore) -{ - /* ^C twice to exit */ - if (GB_debugger_is_stopped(&gb)) - exit(0); - GB_debugger_break(&gb); -} -#endif - static retro_environment_t environ_cb; void retro_init(void) @@ -246,128 +226,35 @@ void retro_reset(void) GB_reset(&gb); } -static void check_variables(void) +static void init_for_current_model(void) { - struct retro_variable var = {0}; - - var.key = "sameboy_color_correction_mode"; - var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb)) - { - if (strcmp(var.value, "off") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED); - else if (strcmp(var.value, "correct curves") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES); - else if (strcmp(var.value, "emulate hardware") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); - else if (strcmp(var.value, "preserve brightness") == 0) - GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + if (GB_is_inited(&gb)) { + GB_switch_model_and_reset(&gb, model != MODEL_DMG); } - - var.key = "sameboy_high_pass_filter_mode"; - var.value = NULL; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) - { - if (strcmp(var.value, "off") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF); - else if (strcmp(var.value, "accurate") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE); - else if (strcmp(var.value, "remove dc offset") == 0) - GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET); + else { + if (model == MODEL_DMG) { + GB_init(&gb); + } + else { + GB_init_cgb(&gb); + } } -} - -void retro_run(void) -{ - bool updated = false; - if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) - check_variables(); - GB_run_frame(&gb); - - video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); -} - -bool retro_load_game(const struct retro_game_info *info) -{ - struct retro_input_descriptor desc[] = { - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, - { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, - { 0 }, - }; - - environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); - - enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; - if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) - { - log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n"); - return false; - } - - snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); + const char *model_name = (const char *[]){"dmg", "cgb", "agb"}[model]; + const unsigned char *boot_code = (const unsigned char *[]){dmg_boot, cgb_boot, agb_boot}[model]; + unsigned boot_length = (unsigned []){dmg_boot_length, cgb_boot_length, agb_boot_length}[model]; char buf[256]; - int err = 0; - if (!strstr(info->path, "gbc")) - { - GB_init(&gb); - snprintf(buf, sizeof(buf), "%s%cdmg_boot.bin", retro_system_directory, slash); - log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - err = GB_load_boot_rom(&gb, buf); - - if (err) { - GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length); - err = 0; - } - } - else - { - GB_init_cgb(&gb); - snprintf(buf, sizeof(buf), "%s%ccgb_boot.bin", retro_system_directory, slash); - log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - err = GB_load_boot_rom(&gb, buf); - - if (err) { - GB_load_boot_rom_from_buffer(&gb, cgb_boot, cgb_boot_length); - err = 0; - } - } - if (err) - log_cb(RETRO_LOG_INFO, "Failed to load boot ROM %s %d\n", buf, err); - (void)info; + snprintf(buf, sizeof(buf), "%s%c%s_boot.bin", retro_system_directory, slash, model_name); + log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); - if (GB_load_rom(&gb,info->path)) { - perror("Failed to load ROM"); - exit(1); + if (GB_load_boot_rom(&gb, buf)) { + GB_load_boot_rom_from_buffer(&gb, boot_code, boot_length); } GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_user_data(&gb, (void*)NULL); GB_set_pixels_output(&gb,(unsigned int*)frame_buf); GB_set_rgb_encode_callback(&gb, rgb_encode); - - size_t path_length = strlen(retro_game_path); - -#ifndef DISABLE_DEBUGGER - { - char TMPC[512]; - sprintf(TMPC,"%s/registers.sym",retro_system_directory); - GB_debugger_load_symbol_file(&gb, TMPC); - } -#endif - - replace_extension(retro_game_path, path_length, symbols_path, ".sym"); - -#ifndef DISABLE_DEBUGGER - GB_debugger_load_symbol_file(&gb, symbols_path); -#endif - GB_set_sample_rate(&gb, AUDIO_FREQUENCY); struct retro_memory_descriptor descs[7]; @@ -409,6 +296,97 @@ bool retro_load_game(const struct retro_game_info *info) mmaps.descriptors = descs; mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]); environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); +} + +static void check_variables(void) +{ + struct retro_variable var = {0}; + + var.key = "sameboy_color_correction_mode"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb)) + { + if (strcmp(var.value, "off") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED); + else if (strcmp(var.value, "correct curves") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES); + else if (strcmp(var.value, "emulate hardware") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); + else if (strcmp(var.value, "preserve brightness") == 0) + GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); + } + + var.key = "sameboy_high_pass_filter_mode"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + if (strcmp(var.value, "off") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF); + else if (strcmp(var.value, "accurate") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE); + else if (strcmp(var.value, "remove dc offset") == 0) + GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET); + } + + var.key = "sameboy_model"; + var.value = NULL; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + enum model new_model = model; + if (strcmp(var.value, "Game Boy") == 0) + new_model = MODEL_DMG; + else if (strcmp(var.value, "Game Boy Color") == 0) + new_model = MODEL_CGB; + else if (strcmp(var.value, "Game Boy Advance") == 0) + new_model = MODEL_AGB; + if (GB_is_inited(&gb) && new_model != model) { + model = new_model; + init_for_current_model(); + } + } +} + +void retro_run(void) +{ + bool updated = false; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated) + check_variables(); + GB_run_frame(&gb); + + video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t)); +} + +bool retro_load_game(const struct retro_game_info *info) +{ + struct retro_input_descriptor desc[] = { + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT, "Left" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Down" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B, "B" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT, "Select" }, + { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, + { 0 }, + }; + + environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); + + enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; + if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) + { + log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n"); + return false; + } + + snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); + + init_for_current_model(); + + if (GB_load_rom(&gb,info->path)) { + log_cb(RETRO_LOG_INFO, "Failed to load ROM\n"); + return false; + } bool yes = true; environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes); @@ -418,21 +396,14 @@ bool retro_load_game(const struct retro_game_info *info) else log_cb(RETRO_LOG_INFO, "Rumble environment not supported.\n"); - static struct retro_variable vars_cgb[] = { + static const struct retro_variable vars[] = { { "sameboy_color_correction_mode", "Color Correction; off|correct curves|emulate hardware|preserve brightness" }, { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, + { "sameboy_model", "Emulated Model; Game Boy Color|Game Boy Advance|Game Boy" }, { NULL } }; - static struct retro_variable vars_dmg[] = { - { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, - { NULL } - }; - - if (GB_is_cgb(&gb)) - environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_cgb); - else - environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_dmg); + environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, (void *)vars); check_variables(); return true; From ce31de47cb4ee5e0a461edea4499cab43d42e87b Mon Sep 17 00:00:00 2001 From: connor rigby Date: Mon, 22 Jan 2018 12:25:27 -0800 Subject: [PATCH 9/9] Fix loading game from command line. --- SDL/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDL/main.c b/SDL/main.c index b819c9d..4387b2d 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -406,7 +406,7 @@ int main(int argc, char **argv) } if (argc == 2) { - filename = argv[2]; + filename = argv[1]; } signal(SIGINT, debugger_interrupt);