From a8741674ce32152454d7a57a2ec1ab94af2a8fcd Mon Sep 17 00:00:00 2001 From: radius Date: Sat, 3 Feb 2018 11:28:35 -0500 Subject: [PATCH] better savestate code --- libretro/libretro.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/libretro/libretro.c b/libretro/libretro.c index d40d3c0..50c4406 100644 --- a/libretro/libretro.c +++ b/libretro/libretro.c @@ -735,7 +735,10 @@ 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(&gameboy[0]); + if (emulated_devices == 2) + return GB_get_save_state_size(&gameboy[0]) + GB_get_save_state_size(&gameboy[1]); + else + return GB_get_save_state_size(&gameboy[0]); } bool retro_serialize(void *data, size_t size) @@ -744,11 +747,14 @@ bool retro_serialize(void *data, size_t size) return false; void* save_data[2]; + size_t state_size[2]; + for (int i = 0; i < emulated_devices; i++) { - save_data[i] = (uint8_t*)malloc(size * sizeof(uint8_t) / emulated_devices); + state_size[i] = GB_get_save_state_size(&gameboy[i]); + save_data[i] = (uint8_t*)malloc(state_size[i]); GB_save_state_to_buffer(&gameboy[i], (uint8_t*) save_data[i]); - memcpy(data + (size * i / emulated_devices), save_data[i], size / emulated_devices); + memcpy(data + (state_size[i] * i), save_data[i], state_size[i]); free(save_data[i]); } @@ -761,13 +767,15 @@ bool retro_serialize(void *data, size_t size) bool retro_unserialize(const void *data, size_t size) { void* save_data[2]; + size_t state_size[2]; int ret; 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(&gameboy[i], save_data[i], size / emulated_devices); + state_size[i] = GB_get_save_state_size(&gameboy[i]); + save_data[i] = (uint8_t*)malloc(state_size[i]); + memcpy (save_data[i], data + (state_size[i] * i), state_size[i]); + ret = GB_load_state_from_buffer(&gameboy[i], save_data[i], state_size[i]); free(save_data[i]); if (ret != 0)