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.

This commit is contained in:
Lior Halphon 2018-01-15 22:23:20 +02:00
parent 5c16d0e656
commit 130c7c28c2

View File

@ -6,7 +6,8 @@
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#define AUDIO_FREQUENCY 44100 #define AUDIO_FREQUENCY 384000
#define FRAME_RATE (0x400000 / 70224.0)
#ifdef _WIN32 #ifdef _WIN32
#include <direct.h> #include <direct.h>
@ -18,9 +19,9 @@
#include "libretro.h" #include "libretro.h"
#ifdef _WIN32 #ifdef _WIN32
char slash = '\\'; char slash = '\\';
#else #else
char slash = '/'; char slash = '/';
#endif #endif
#define VIDEO_WIDTH 160 #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_poll_t input_poll_cb;
static retro_input_state_t input_state_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_system_directory[4096];
char retro_save_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, ...) static void fallback_log(enum retro_log_level level, const char *fmt, ...)
{ {
(void)level; (void)level;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
vfprintf(stderr, fmt, va); vfprintf(stderr, fmt, va);
va_end(va); va_end(va);
} }
static void replace_extension(const char *src, size_t length, char *dest, const char *ext) static void replace_extension(const char *src, size_t length, char *dest, const char *ext)
{ {
memcpy(dest, src, length); memcpy(dest, src, length);
dest[length] = 0; dest[length] = 0;
/* Remove extension */ /* Remove extension */
for (size_t i = length; i--;) { for (size_t i = length; i--;) {
if (dest[i] == '/') break; if (dest[i] == '/') break;
@ -72,7 +73,7 @@ static void replace_extension(const char *src, size_t length, char *dest, const
break; break;
} }
} }
/* Add new extension */ /* Add new extension */
strcat(dest, ext); strcat(dest, ext);
} }
@ -81,38 +82,47 @@ static struct retro_rumble_interface rumble;
static void GB_update_keys_status(GB_gameboy_t *gb) static void GB_update_keys_status(GB_gameboy_t *gb)
{ {
input_poll_cb(); 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_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_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_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_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_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_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_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) ); 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) if (gb->rumble_state)
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535); rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535);
else else
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 0); rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 0);
} }
static void audio_callback(void *gb) static void audio_callback(void *gb)
{ {
GB_apu_copy_buffer(gb, (GB_sample_t *) soundbuf, (float)AUDIO_FREQUENCY / 59.72); size_t length = GB_apu_get_current_buffer_length(gb);
audio_batch_cb(soundbuf, (float)AUDIO_FREQUENCY / 59.72);
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) static void vblank(GB_gameboy_t *gb)
{ {
GB_update_keys_status(gb); GB_update_keys_status(gb);
GB_set_pixels_output(gb, frame_buf); audio_callback(gb);
audio_callback(gb);
} }
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) 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) void retro_init(void)
{ {
frame_buf = (uint32_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t)); frame_buf = (uint32_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t));
const char *dir = NULL; const char *dir = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir) if (environ_cb(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &dir) && dir)
snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir); snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", dir);
else else
snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", "."); snprintf(retro_system_directory, sizeof(retro_system_directory), "%s", ".");
if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir) if (environ_cb(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &dir) && dir)
snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir); snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", dir);
else else
snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", "."); snprintf(retro_save_directory, sizeof(retro_save_directory), "%s", ".");
} }
void retro_deinit(void) void retro_deinit(void)
{ {
free(frame_buf); free(frame_buf);
frame_buf = NULL; frame_buf = NULL;
} }
unsigned retro_api_version(void) unsigned retro_api_version(void)
{ {
return RETRO_API_VERSION; return RETRO_API_VERSION;
} }
void retro_set_controller_port_device(unsigned port, unsigned device) 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) void retro_get_system_info(struct retro_system_info *info)
{ {
memset(info, 0, sizeof(*info)); memset(info, 0, sizeof(*info));
info->library_name = "SameBoy"; info->library_name = "SameBoy";
#ifdef GIT_VERSION #ifdef GIT_VERSION
info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION; info->library_version = SAMEBOY_CORE_VERSION GIT_VERSION;
#else #else
info->library_version = SAMEBOY_CORE_VERSION; info->library_version = SAMEBOY_CORE_VERSION;
#endif #endif
info->need_fullpath = true; info->need_fullpath = true;
info->valid_extensions = "gb|gbc"; info->valid_extensions = "gb|gbc";
} }
void retro_get_system_av_info(struct retro_system_av_info *info) 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_game_geometry geom = { VIDEO_WIDTH, VIDEO_HEIGHT,VIDEO_WIDTH, VIDEO_HEIGHT ,160.0 / 144.0 };
struct retro_system_timing timing = { 59.72, 44100.0 }; struct retro_system_timing timing = { FRAME_RATE, AUDIO_FREQUENCY };
info->geometry = geom; info->geometry = geom;
info->timing = timing; info->timing = timing;
} }
void retro_set_environment(retro_environment_t cb) void retro_set_environment(retro_environment_t cb)
{ {
environ_cb = cb; environ_cb = cb;
if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging)) if (cb(RETRO_ENVIRONMENT_GET_LOG_INTERFACE, &logging))
log_cb = logging.log; log_cb = logging.log;
else else
log_cb = fallback_log; log_cb = fallback_log;
static const struct retro_controller_description controllers[] = { static const struct retro_controller_description controllers[] = {
{ "Nintendo Gameboy", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) }, { "Nintendo Gameboy", RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_JOYPAD, 0) },
}; };
static const struct retro_controller_info ports[] = { static const struct retro_controller_info ports[] = {
{ controllers, 1 }, { controllers, 1 },
{ NULL, 0 }, { NULL, 0 },
}; };
cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports); cb(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO, (void*)ports);
} }
void retro_set_audio_sample(retro_audio_sample_t cb) 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) 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) 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) 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) void retro_set_video_refresh(retro_video_refresh_t cb)
{ {
video_cb = cb; video_cb = cb;
} }
void retro_reset(void) void retro_reset(void)
{ {
GB_reset(&gb); GB_reset(&gb);
} }
static void check_variables(void) static void check_variables(void)
{ {
struct retro_variable var = {0}; struct retro_variable var = {0};
var.key = "sameboy_color_correction_mode"; var.key = "sameboy_color_correction_mode";
var.value = NULL; var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb)) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value && GB_is_cgb(&gb))
{ {
if (strcmp(var.value, "off") == 0) if (strcmp(var.value, "off") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED); GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED);
else if (strcmp(var.value, "correct curves") == 0) else if (strcmp(var.value, "correct curves") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES); GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES);
else if (strcmp(var.value, "emulate hardware") == 0) else if (strcmp(var.value, "emulate hardware") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE); GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE);
else if (strcmp(var.value, "preserve brightness") == 0) else if (strcmp(var.value, "preserve brightness") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS); GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS);
} }
var.key = "sameboy_high_pass_filter_mode"; var.key = "sameboy_high_pass_filter_mode";
var.value = NULL; var.value = NULL;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{ {
if (strcmp(var.value, "off") == 0) if (strcmp(var.value, "off") == 0)
GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF); GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_OFF);
else if (strcmp(var.value, "accurate") == 0) else if (strcmp(var.value, "accurate") == 0)
GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE); GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE);
else if (strcmp(var.value, "remove dc offset") == 0) else if (strcmp(var.value, "remove dc offset") == 0)
GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET); GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_REMOVE_DC_OFFSET);
} }
} }
void retro_run(void) void retro_run(void)
{ {
static int frames; bool updated = false;
size_t samples; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
bool updated = false; GB_run_frame(&gb);
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables(); video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t));
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 retro_load_game(const struct retro_game_info *info) bool retro_load_game(const struct retro_game_info *info)
{ {
struct retro_input_descriptor desc[] = { 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_LEFT, "Left" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Up" }, { 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_DOWN, "Down" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Right" }, { 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_B, "B" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A, "A" }, { 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_SELECT, "Select" },
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" }, { 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START, "Start" },
{ 0 }, { 0 },
}; };
environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc); environ_cb(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS, desc);
enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888; enum retro_pixel_format fmt = RETRO_PIXEL_FORMAT_XRGB8888;
if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt)) if (!environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &fmt))
{ {
log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n"); log_cb(RETRO_LOG_INFO, "XRGB8888 is not supported.\n");
return false; return false;
} }
snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path); snprintf(retro_game_path, sizeof(retro_game_path), "%s", info->path);
char buf[256]; char buf[256];
int err = 0; int err = 0;
if (!strstr(info->path, "gbc")) if (!strstr(info->path, "gbc"))
{ {
GB_init(&gb); GB_init(&gb);
snprintf(buf, sizeof(buf), "%s%cdmg_boot.bin", retro_system_directory, slash); snprintf(buf, sizeof(buf), "%s%cdmg_boot.bin", retro_system_directory, slash);
log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf);
err = GB_load_boot_rom(&gb, buf); err = GB_load_boot_rom(&gb, buf);
if (err) { if (err) {
GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length); GB_load_boot_rom_from_buffer(&gb, dmg_boot, dmg_boot_length);
err = 0; err = 0;
} }
} }
else else
{ {
GB_init_cgb(&gb); GB_init_cgb(&gb);
snprintf(buf, sizeof(buf), "%s%ccgb_boot.bin", retro_system_directory, slash); snprintf(buf, sizeof(buf), "%s%ccgb_boot.bin", retro_system_directory, slash);
log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf); log_cb(RETRO_LOG_INFO, "Loading boot image: %s\n", buf);
err = GB_load_boot_rom(&gb, buf); err = GB_load_boot_rom(&gb, buf);
if (err) { if (err) {
GB_load_boot_rom_from_buffer(&gb, cgb_boot, cgb_boot_length); GB_load_boot_rom_from_buffer(&gb, cgb_boot, cgb_boot_length);
err = 0; err = 0;
} }
} }
if (err) if (err)
log_cb(RETRO_LOG_INFO, "Failed to load boot ROM %s %d\n", buf, err); log_cb(RETRO_LOG_INFO, "Failed to load boot ROM %s %d\n", buf, err);
(void)info; (void)info;
if (GB_load_rom(&gb,info->path)) { if (GB_load_rom(&gb,info->path)) {
perror("Failed to load ROM"); perror("Failed to load ROM");
exit(1); exit(1);
} }
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
GB_set_user_data(&gb, (void*)NULL); GB_set_user_data(&gb, (void*)NULL);
GB_set_pixels_output(&gb,(unsigned int*)frame_buf); GB_set_pixels_output(&gb,(unsigned int*)frame_buf);
GB_set_rgb_encode_callback(&gb, rgb_encode); GB_set_rgb_encode_callback(&gb, rgb_encode);
size_t path_length = strlen(retro_game_path); size_t path_length = strlen(retro_game_path);
#ifndef DISABLE_DEBUGGER #ifndef DISABLE_DEBUGGER
{ {
char TMPC[512]; char TMPC[512];
sprintf(TMPC,"%s/registers.sym",retro_system_directory); sprintf(TMPC,"%s/registers.sym",retro_system_directory);
GB_debugger_load_symbol_file(&gb, TMPC); GB_debugger_load_symbol_file(&gb, TMPC);
} }
#endif #endif
replace_extension(retro_game_path, path_length, symbols_path, ".sym"); replace_extension(retro_game_path, path_length, symbols_path, ".sym");
#ifndef DISABLE_DEBUGGER #ifndef DISABLE_DEBUGGER
GB_debugger_load_symbol_file(&gb, symbols_path); GB_debugger_load_symbol_file(&gb, symbols_path);
#endif #endif
GB_set_sample_rate(&gb, AUDIO_FREQUENCY); GB_set_sample_rate(&gb, AUDIO_FREQUENCY);
struct retro_memory_descriptor descs[7]; struct retro_memory_descriptor descs[7];
size_t size; size_t size;
uint16_t bank; uint16_t bank;
memset(descs, 0, sizeof(descs)); memset(descs, 0, sizeof(descs));
descs[0].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_IE, &size, &bank); descs[0].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_IE, &size, &bank);
descs[0].start = 0xFFFF; descs[0].start = 0xFFFF;
descs[0].len = 1; descs[0].len = 1;
descs[1].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_HRAM, &size, &bank); descs[1].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_HRAM, &size, &bank);
descs[1].start = 0xFF80; descs[1].start = 0xFF80;
descs[1].len = 0x0080; descs[1].len = 0x0080;
descs[2].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_RAM, &size, &bank); descs[2].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_RAM, &size, &bank);
descs[2].start = 0xC000; descs[2].start = 0xC000;
descs[2].len = 0x2000; descs[2].len = 0x2000;
descs[3].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank); descs[3].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_CART_RAM, &size, &bank);
descs[3].start = 0xA000; descs[3].start = 0xA000;
descs[3].len = 0x2000; descs[3].len = 0x2000;
descs[4].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_VRAM, &size, &bank); descs[4].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_VRAM, &size, &bank);
descs[4].start = 0x8000; descs[4].start = 0x8000;
descs[4].len = 0x2000; descs[4].len = 0x2000;
descs[5].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_ROM, &size, &bank); descs[5].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_ROM, &size, &bank);
descs[5].start = 0x0000; descs[5].start = 0x0000;
descs[5].len = 0x4000; descs[5].len = 0x4000;
descs[5].flags = RETRO_MEMDESC_CONST; descs[5].flags = RETRO_MEMDESC_CONST;
descs[6].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_OAM, &size, &bank); descs[6].ptr = GB_get_direct_access(&gb, GB_DIRECT_ACCESS_OAM, &size, &bank);
descs[6].start = 0xFE00; descs[6].start = 0xFE00;
descs[6].len = 0x00A0; descs[6].len = 0x00A0;
struct retro_memory_map mmaps; struct retro_memory_map mmaps;
mmaps.descriptors = descs; mmaps.descriptors = descs;
mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]); mmaps.num_descriptors = sizeof(descs) / sizeof(descs[0]);
environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps); environ_cb(RETRO_ENVIRONMENT_SET_MEMORY_MAPS, &mmaps);
bool yes = true; bool yes = true;
environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes); environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_ACHIEVEMENTS, &yes);
if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble)) if (environ_cb(RETRO_ENVIRONMENT_GET_RUMBLE_INTERFACE, &rumble))
log_cb(RETRO_LOG_INFO, "Rumble environment supported.\n"); log_cb(RETRO_LOG_INFO, "Rumble environment supported.\n");
else else
log_cb(RETRO_LOG_INFO, "Rumble environment not supported.\n"); log_cb(RETRO_LOG_INFO, "Rumble environment not supported.\n");
static struct retro_variable vars_cgb[] = { static struct retro_variable vars_cgb[] = {
{ "sameboy_color_correction_mode", "Color Correction; off|correct curves|emulate hardware|preserve brightness" }, { "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_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" },
{ NULL } { NULL }
}; };
static struct retro_variable vars_dmg[] = { static struct retro_variable vars_dmg[] = {
{ "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" }, { "sameboy_high_pass_filter_mode", "High Pass Filter; off|accurate|remove dc offset" },
{ NULL } { NULL }
}; };
if (GB_is_cgb(&gb)) if (GB_is_cgb(&gb))
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_cgb); environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_cgb);
else else
environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_dmg); environ_cb(RETRO_ENVIRONMENT_SET_VARIABLES, vars_dmg);
check_variables(); check_variables();
return true; return true;
} }
void retro_unload_game(void) void retro_unload_game(void)
{ {
GB_free(&gb); GB_free(&gb);
} }
unsigned retro_get_region(void) 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) 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) 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) bool retro_serialize(void *data, size_t size)
{ {
GB_save_state_to_buffer(&gb, (uint8_t*) data); GB_save_state_to_buffer(&gb, (uint8_t*) data);
if (data) if (data)
return true; return true;
else else
return false; return false;
} }
bool retro_unserialize(const void *data, size_t size) bool retro_unserialize(const void *data, size_t size)
{ {
if (GB_load_state_from_buffer(&gb, (uint8_t*) data, size) == 0) if (GB_load_state_from_buffer(&gb, (uint8_t*) data, size) == 0)
return true; return true;
else else
return false; return false;
} }
void *retro_get_memory_data(unsigned type) void *retro_get_memory_data(unsigned type)
{ {
void* data; void* data;
switch(type) switch(type)
{ {
case RETRO_MEMORY_SAVE_RAM: case RETRO_MEMORY_SAVE_RAM:
if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0)
data = gb.mbc_ram; data = gb.mbc_ram;
else 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; data = NULL;
break; break;
case RETRO_MEMORY_RTC: }
if(gb.cartridge_type->has_battery)
data = &gb.rtc_real; return data;
else
data = NULL;
break;
default:
data = NULL;
break;
}
return data;
} }
size_t retro_get_memory_size(unsigned type) size_t retro_get_memory_size(unsigned type)
{ {
size_t size; size_t size;
switch(type) switch(type)
{ {
case RETRO_MEMORY_SAVE_RAM: case RETRO_MEMORY_SAVE_RAM:
if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0) if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0)
size = gb.mbc_ram_size; size = gb.mbc_ram_size;
else 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; size = 0;
break; break;
case RETRO_MEMORY_RTC: }
if(gb.cartridge_type->has_battery)
size = sizeof (gb.rtc_real); return size;
else
size = 0;
break;
default:
size = 0;
break;
}
return size;
} }
void retro_cheat_reset(void) 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 retro_cheat_set(unsigned index, bool enabled, const char *code)
{ {
(void)index; (void)index;
(void)enabled; (void)enabled;
(void)code; (void)code;
} }