From 7ee063da28802eaa2539ec0b15f1bf43cedd4c4b Mon Sep 17 00:00:00 2001 From: radius Date: Wed, 31 Jan 2018 22:42:26 -0500 Subject: [PATCH] single mode works --- libretro/libretro.c | 382 +++++++++++++++++++++----------------------- 1 file changed, 183 insertions(+), 199 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index 2eaef49..2ca3ecb 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -61,7 +61,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; -static unsigned emulated_devices = 2; +static unsigned emulated_devices = 1; static unsigned layout = 0; signed short soundbuf[1024 * 2]; @@ -125,7 +125,6 @@ static void audio_callback(void *gb) } } - static void vblank1(GB_gameboy_t *gb) { vblank1_occurred = true; @@ -140,119 +139,6 @@ static void vblank2(GB_gameboy_t *gb) //audio_callback(gb); } -static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) -{ - return r<<16|g<<8|b; -} - -static retro_environment_t environ_cb; - -void retro_init(void) -{ - frame_buf = (uint32_t*)malloc(2 * 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; -} - -unsigned retro_api_version(void) -{ - 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); -} - -void retro_get_system_info(struct retro_system_info *info) -{ - memset(info, 0, sizeof(*info)); - info->library_name = "SameBoy"; -#ifdef GIT_VERSION - info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION; -#else - info->library_version = SAMEBOY_CORE_VERSION; -#endif - 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 * 2, VIDEO_WIDTH, VIDEO_HEIGHT * 2 ,160.0 / 288.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); -} - -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; -} - -void retro_set_input_poll(retro_input_poll_t cb) -{ - input_poll_cb = cb; -} - -void retro_set_input_state(retro_input_state_t cb) -{ - input_state_cb = cb; -} - -void retro_set_video_refresh(retro_video_refresh_t cb) -{ - video_cb = cb; -} - -void retro_reset(void) -{ - for (int i = 0; i < emulated_devices; i++) - GB_reset(&gb[i]); - -} - static uint8_t byte_to_send1 = 0xFF, byte_to_send2 = 0xFF; static void serial_start1(GB_gameboy_t *gb, uint8_t byte_received) @@ -279,6 +165,33 @@ static uint8_t serial_end2(GB_gameboy_t *gb) return ret; } +static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) +{ + return r<<16|g<<8|b; +} + +static retro_environment_t environ_cb; + +static const struct retro_variable vars[] = { + { "sameboy_link", "Link Cable; disabled|enabled" }, + { "sameboy_link_layout", "Screen Layout; top-down|left-right" }, + { "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 const struct retro_variable vars_link[] = { + { "sameboy_link", "Link Cable; disabled|enabled" }, + { "sameboy_link_layout", "Screen Layout; top-down|left-right" }, + { "sameboy_model_1", "Emulated Model for GB #1; Game Boy Color|Game Boy Advance|Game Boy" }, + { "sameboy_model_2", "Emulated Model for GB #2; Game Boy Color|Game Boy Advance|Game Boy" }, + { "sameboy_color_correction_mode_1", "Color Correction for GB #1; off|correct curves|emulate hardware|preserve brightness" }, + { "sameboy_color_correction_mode_2", "Color Correction for GB #2; off|correct curves|emulate hardware|preserve brightness" }, + { "sameboy_high_pass_filter_mode_1", "High Pass Filter for GB #1; off|accurate|remove dc offset" }, + { "sameboy_high_pass_filter_mode_2", "High Pass Filter for GB #2; off|accurate|remove dc offset" }, + { NULL } +}; + static void init_for_current_model(void) { enum model effective_model = model; @@ -307,12 +220,6 @@ static void init_for_current_model(void) log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); if (GB_load_boot_rom(&gb[i], buf)) GB_load_boot_rom_from_buffer(&gb[i], boot_code, boot_length); - } - - - - for (i = 0; i < emulated_devices; i++) - { GB_set_user_data(&gb[i], (void*)NULL); GB_set_pixels_output(&gb[i],(unsigned int*)(frame_buf + i * VIDEO_PIXELS)); GB_set_rgb_encode_callback(&gb[i], rgb_encode); @@ -321,11 +228,14 @@ static void init_for_current_model(void) /* todo: attempt to make these more generic */ GB_set_vblank_callback(&gb[0], (GB_vblank_callback_t) vblank1); - GB_set_vblank_callback(&gb[1], (GB_vblank_callback_t) vblank2); - GB_set_serial_transfer_start_callback(&gb[0], serial_start1); - GB_set_serial_transfer_end_callback(&gb[0], serial_end1); - GB_set_serial_transfer_start_callback(&gb[1], serial_start2); - GB_set_serial_transfer_end_callback(&gb[1], serial_end2); + if (emulated_devices == 2) + { + GB_set_vblank_callback(&gb[1], (GB_vblank_callback_t) vblank2); + GB_set_serial_transfer_start_callback(&gb[0], serial_start1); + GB_set_serial_transfer_end_callback(&gb[0], serial_end1); + GB_set_serial_transfer_start_callback(&gb[1], serial_start2); + GB_set_serial_transfer_end_callback(&gb[1], serial_end2); + } struct retro_memory_descriptor descs[7]; size_t size; @@ -382,25 +292,13 @@ static void check_variables(bool link) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb[0])) { if (strcmp(var.value, "off") == 0) - { GB_set_color_correction_mode(&gb[0], GB_COLOR_CORRECTION_DISABLED); - GB_set_color_correction_mode(&gb[1], GB_COLOR_CORRECTION_DISABLED); - } else if (strcmp(var.value, "correct curves") == 0) - { GB_set_color_correction_mode(&gb[0], GB_COLOR_CORRECTION_CORRECT_CURVES); - GB_set_color_correction_mode(&gb[1], GB_COLOR_CORRECTION_CORRECT_CURVES); - } else if (strcmp(var.value, "emulate hardware") == 0) - { GB_set_color_correction_mode(&gb[0], GB_COLOR_CORRECTION_EMULATE_HARDWARE); - GB_set_color_correction_mode(&gb[1], GB_COLOR_CORRECTION_EMULATE_HARDWARE); - } else if (strcmp(var.value, "preserve brightness") == 0) - { GB_set_color_correction_mode(&gb[0], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - GB_set_color_correction_mode(&gb[1], GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); - } } var.key = "sameboy_high_pass_filter_mode"; @@ -408,20 +306,11 @@ static void check_variables(bool link) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { if (strcmp(var.value, "off") == 0) - { GB_set_highpass_filter_mode(&gb[0], GB_HIGHPASS_OFF); - GB_set_highpass_filter_mode(&gb[1], GB_HIGHPASS_OFF); - } else if (strcmp(var.value, "accurate") == 0) - { GB_set_highpass_filter_mode(&gb[0], GB_HIGHPASS_ACCURATE); - GB_set_highpass_filter_mode(&gb[1], GB_HIGHPASS_ACCURATE); - } else if (strcmp(var.value, "remove dc offset") == 0) - { GB_set_highpass_filter_mode(&gb[0], GB_HIGHPASS_REMOVE_DC_OFFSET); - GB_set_highpass_filter_mode(&gb[1], GB_HIGHPASS_REMOVE_DC_OFFSET); - } } var.key = "sameboy_model"; @@ -441,10 +330,6 @@ static void check_variables(bool link) model[0] = new_model; init_for_current_model(); } - if (GB_is_inited(&gb[1]) && new_model != model[0]) { - model[0] = new_model; - init_for_current_model(); - } } } else @@ -544,7 +429,7 @@ static void check_variables(bool link) if (strcmp(var.value, "enabled") == 0) emulated_devices = 2; else - emulated_devices = 2; + emulated_devices = 1; } var.key = "sameboy_link_layout"; @@ -558,6 +443,115 @@ static void check_variables(bool link) } } +void retro_init(void) +{ + frame_buf = (uint32_t*)malloc(2 * 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", "."); + + environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, emulated_devices == 2 ? (void *)vars_link : (void *)vars); + check_variables(emulated_devices == 2 ? true : false); +} + +void retro_deinit(void) +{ + free(frame_buf); + frame_buf = NULL; +} + +unsigned retro_api_version(void) +{ + 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); +} + +void retro_get_system_info(struct retro_system_info *info) +{ + memset(info, 0, sizeof(*info)); + info->library_name = "SameBoy"; +#ifdef GIT_VERSION + info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION; +#else + info->library_version = SAMEBOY_CORE_VERSION; +#endif + 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 * 2, VIDEO_WIDTH, VIDEO_HEIGHT * 2 ,160.0 / 288.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); +} + +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; +} + +void retro_set_input_poll(retro_input_poll_t cb) +{ + input_poll_cb = cb; +} + +void retro_set_input_state(retro_input_state_t cb) +{ + input_state_cb = cb; +} + +void retro_set_video_refresh(retro_video_refresh_t cb) +{ + video_cb = cb; +} + +void retro_reset(void) +{ + for (int i = 0; i < emulated_devices; i++) + GB_reset(&gb[i]); + +} + void retro_run(void) { bool updated = false; @@ -570,39 +564,23 @@ void retro_run(void) vblank1_occurred = vblank2_occurred = false; signed delta = 0; + if (emulated_devices == 2) + { while (!vblank1_occurred || !vblank2_occurred) { - if (delta >= 0) { - delta -= GB_run(&gb[0]); - } - else { - delta += GB_run(&gb[1]); + if (delta >= 0) { + delta -= GB_run(&gb[0]); + } + else { + delta += GB_run(&gb[1]); + } } } + else + GB_run_frame(&gb[0]); video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT * 2, VIDEO_WIDTH * sizeof(uint32_t)); } -static const struct retro_variable vars[] = { - { "sameboy_link", "Link Cable; disabled|enabled" }, - { "sameboy_link_layout", "Screen Layout; top-down|left-right" }, - { "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 const struct retro_variable vars_link[] = { - { "sameboy_link", "Link Cable; disabled|enabled" }, - { "sameboy_link_layout", "Screen Layout; top-down|left-right" }, - { "sameboy_model_1", "Emulated Model for GB #1; Game Boy Color|Game Boy Advance|Game Boy" }, - { "sameboy_model_2", "Emulated Model for GB #2; Game Boy Color|Game Boy Advance|Game Boy" }, - { "sameboy_color_correction_mode_1", "Color Correction for GB #1; off|correct curves|emulate hardware|preserve brightness" }, - { "sameboy_color_correction_mode_2", "Color Correction for GB #2; off|correct curves|emulate hardware|preserve brightness" }, - { "sameboy_high_pass_filter_mode_1", "High Pass Filter for GB #1; off|accurate|remove dc offset" }, - { "sameboy_high_pass_filter_mode_2", "High Pass Filter for GB #2; off|accurate|remove dc offset" }, - - { NULL } -}; - bool retro_load_game(const struct retro_game_info *info) { struct retro_input_descriptor desc[] = { @@ -659,7 +637,6 @@ bool retro_load_game(const struct retro_game_info *info) environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, emulated_devices == 2 ? (void *)vars_link : (void *)vars); check_variables(emulated_devices == 2 ? true : false); - return true; } @@ -681,19 +658,23 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t retro_serialize_size(void) { - return 2 * GB_get_save_state_size(&gb[0]); + size_t size = 0; + for (int i = 0; i < emulated_devices; i++) + size += GB_get_save_state_size(&gb[i]); + + return size; } bool retro_serialize(void *data, size_t size) { - void* gb1_data = (uint8_t*)malloc(0.5 * size * sizeof(uint8_t)); - void* gb2_data = (uint8_t*)malloc(0.5 * size * sizeof(uint8_t)); - - GB_save_state_to_buffer(&gb[0], (uint8_t*) gb1_data); - GB_save_state_to_buffer(&gb[1], (uint8_t*) gb2_data); - - memcpy(data, gb1_data, size / 2); - memcpy(data + (size / 2), gb2_data, size / 2); + void* save_data[2]; + for (int i = 0; i < emulated_devices; i++) + { + save_data[i] = (uint8_t*)malloc(size * sizeof(uint8_t) / emulated_devices); + GB_save_state_to_buffer(&gb[i], (uint8_t*) save_data[i]); + memcpy(data + (size * i / emulated_devices), save_data[i], size / emulated_devices); + free(save_data[i]); + } if (data) return true; @@ -703,19 +684,22 @@ bool retro_serialize(void *data, size_t size) bool retro_unserialize(const void *data, size_t size) { - void* gb1_data = (uint8_t*)malloc(0.5 * size * sizeof(uint8_t)); - void* gb2_data = (uint8_t*)malloc(0.5 * size * sizeof(uint8_t)); + void* save_data[2]; + int ret; - memcpy (gb1_data, data, size / 2); - memcpy (gb2_data, data + (size / 2), size / 2); + for (int i = 0; i < emulated_devices; i++) + { + save_data[i] = (uint8_t*)malloc(size * sizeof(uint8_t)/ emulated_devices); + memcpy (save_data[i], data + (size * i / emulated_devices), size / emulated_devices); + ret = GB_load_state_from_buffer(&gb[i], save_data[i], size / emulated_devices); + free(save_data[i]); - int ret1 = GB_load_state_from_buffer(&gb[0], gb1_data, size / 2); - int ret2 = GB_load_state_from_buffer(&gb[1], gb2_data, size / 2); + if (ret != 0) + return false; + } + + return true; - if (ret1 == 0 && ret2 ==0) - return true; - else - return false; } void *retro_get_memory_data(unsigned type) @@ -731,7 +715,7 @@ void *retro_get_memory_data(unsigned type) { data = gb[0].mbc_ram; /* let's copy the save to gb[1] so it can save independently */ - memcpy(gb[1].mbc_ram, gb[0].mbc_ram, gb[0].mbc_ram_size); + //memcpy(gb[1].mbc_ram, gb[0].mbc_ram, gb[0].mbc_ram_size); } else data = NULL;