From 40e4f93637a30a6fae68970af3016e7eba53ddd9 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Thu, 12 Oct 2017 22:06:01 +0300 Subject: [PATCH] Replaced libretro specific code with a generic API --- Core/gb.c | 28 +++++++++------------------- Core/gb.h | 6 ++---- Makefile.libretro | 2 +- libretro/libretro.c | 15 ++++++++++----- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/Core/gb.c b/Core/gb.c index ff92f2f..80f087c 100755 --- a/Core/gb.c +++ b/Core/gb.c @@ -153,25 +153,6 @@ void GB_free(GB_gameboy_t *gb) memset(gb, 0, sizeof(*gb)); } -#ifdef __LIBRETRO__ -extern const char dmg_boot[], cgb_boot[]; -extern const unsigned dmg_boot_length, cgb_boot_length; - -int GB_load_boot_rom_dmg(GB_gameboy_t *gb) -{ - memset(gb->boot_rom, 0xFF, sizeof(gb->boot_rom)); - memcpy(gb->boot_rom, dmg_boot, dmg_boot_length); - return 0; -} - -int GB_load_boot_rom_cgb(GB_gameboy_t *gb) -{ - memset(gb->boot_rom, 0xFF, sizeof(gb->boot_rom)); - memcpy(gb->boot_rom, cgb_boot, cgb_boot_length); - return 0; -} -#endif - int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) { FILE *f = fopen(path, "rb"); @@ -184,6 +165,15 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) return 0; } +void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size) +{ + if (size > sizeof(gb->boot_rom)) { + size = sizeof(gb->boot_rom); + } + memset(gb->boot_rom, 0xFF, sizeof(gb->boot_rom)); + memcpy(gb->boot_rom, buffer, size); +} + int GB_load_rom(GB_gameboy_t *gb, const char *path) { FILE *f = fopen(path, "rb"); diff --git a/Core/gb.h b/Core/gb.h index 1085f06..bfafd33 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -530,12 +530,10 @@ void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t * void *GB_get_user_data(GB_gameboy_t *gb); void GB_set_user_data(GB_gameboy_t *gb, void *data); -#ifdef __LIBRETRO__ -int GB_load_boot_rom_dmg(GB_gameboy_t *gb); -int GB_load_boot_rom_cgb(GB_gameboy_t *gb); -#endif + int GB_load_boot_rom(GB_gameboy_t *gb, const char *path); +void GB_load_boot_rom_from_buffer(GB_gameboy_t *gb, const unsigned char *buffer, size_t size); int GB_load_rom(GB_gameboy_t *gb, const char *path); int GB_save_battery(GB_gameboy_t *gb, const char *path); diff --git a/Makefile.libretro b/Makefile.libretro index acf773d..68af373 100644 --- a/Makefile.libretro +++ b/Makefile.libretro @@ -135,7 +135,7 @@ all: $(TARGET) $(CORE_DIR)/libretro/%_boot.c: $(CORE_DIR)/build/bin/BootROMs/%_boot.bin echo "/* AUTO-GENERATED */" > $@ - echo "const char $(notdir $(@:%.c=%))[] = {" >> $@ + echo "const unsigned char $(notdir $(@:%.c=%))[] = {" >> $@ cat $< | xxd -i >> $@ echo "};" >> $@ echo "const unsigned $(notdir $(@:%.c=%))_length = sizeof($(notdir $(@:%.c=%)));" >> $@ diff --git a/libretro/libretro.c b/libretro/libretro.c index 69cfd12..a084502 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -49,6 +49,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; static void fallback_log(enum retro_log_level level, const char *fmt, ...) { @@ -289,8 +291,10 @@ bool retro_load_game(const struct retro_game_info *info) log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); err = GB_load_boot_rom(&gb, buf); - if (err) - err = GB_load_boot_rom_dmg(&gb); + if (err) { + GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length); + err = 0; + } } else { @@ -299,10 +303,11 @@ bool retro_load_game(const struct retro_game_info *info) log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); err = GB_load_boot_rom(&gb, buf); - if (err) - err = GB_load_boot_rom_cgb(&gb); + 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;