initial link cable implementation

This commit is contained in:
radius 2018-01-27 14:59:18 -05:00
parent ff4168b8f6
commit 3b858fb6af

View File

@ -31,7 +31,7 @@ static const char slash = '/';
#define VIDEO_WIDTH 160
#define VIDEO_HEIGHT 144
#define VIDEO_PIXELS VIDEO_WIDTH * VIDEO_HEIGHT
#define VIDEO_PIXELS (VIDEO_WIDTH * VIDEO_HEIGHT)
char battery_save_path[512];
char symbols_path[512];
@ -46,6 +46,8 @@ enum model {
static enum model model = MODEL_AUTO;
static enum model auto_model = MODEL_CGB;
static uint32_t *frame_buf;
static uint32_t *frame_buf = NULL;
static struct retro_log_callback logging;
static retro_log_printf_t log_cb;
@ -60,7 +62,8 @@ char retro_system_directory[4096];
char retro_save_directory[4096];
char retro_game_path[4096];
GB_gameboy_t gb;
GB_gameboy_t gb1;
GB_gameboy_t gb2;
extern const unsigned char dmg_boot[], cgb_boot[], agb_boot[];
extern const unsigned dmg_boot_length, cgb_boot_length, agb_boot_length;
@ -75,24 +78,24 @@ static void fallback_log(enum retro_log_level level, const char *fmt, ...)
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, unsigned port)
{
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) );
GB_set_key_state(gb, GB_KEY_RIGHT,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT));
GB_set_key_state(gb, GB_KEY_LEFT, input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT));
GB_set_key_state(gb, GB_KEY_UP,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP) );
GB_set_key_state(gb, GB_KEY_DOWN,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN));
GB_set_key_state(gb, GB_KEY_A,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_A) );
GB_set_key_state(gb, GB_KEY_B,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_B) );
GB_set_key_state(gb, GB_KEY_SELECT,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_SELECT));
GB_set_key_state(gb, GB_KEY_START,input_state_cb(port, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_START) );
if (gb->rumble_state)
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 65535);
rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 65535);
else
rumble.set_rumble_state(0, RETRO_RUMBLE_STRONG, 0);
rumble.set_rumble_state(port, RETRO_RUMBLE_STRONG, 0);
}
@ -114,11 +117,18 @@ static void audio_callback(void *gb)
}
static void vblank(GB_gameboy_t *gb)
static void vblank1(GB_gameboy_t *gb)
{
GB_update_keys_status(gb, 0);
audio_callback(gb);
}
static void vblank2(GB_gameboy_t *gb)
{
GB_update_keys_status(gb, 1);
//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;
@ -128,7 +138,7 @@ static retro_environment_t environ_cb;
void retro_init(void)
{
frame_buf = (uint32_t*)malloc(VIDEO_PIXELS * sizeof(uint32_t));
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)
@ -173,7 +183,7 @@ void retro_get_system_info(struct retro_system_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 * 2, VIDEO_WIDTH, VIDEO_HEIGHT * 2 ,160.0 / 288.0 };
struct retro_system_timing timing = { FRAME_RATE, AUDIO_FREQUENCY };
info->geometry = geom;
@ -227,7 +237,28 @@ void retro_set_video_refresh(retro_video_refresh_t cb)
void retro_reset(void)
{
GB_reset(&gb);
GB_reset(&gb1);
GB_reset(&gb2);
}
static void serial_start1(GB_gameboy_t *gb, uint8_t byte_received)
{
GB_serial_set_data(&gb2, byte_received);
}
static uint8_t serial_end1(GB_gameboy_t *gb)
{
return GB_serial_get_data(&gb2);
}
static void serial_start2(GB_gameboy_t *gb, uint8_t byte_received)
{
GB_serial_set_data(&gb1, byte_received);
}
static uint8_t serial_end2(GB_gameboy_t *gb)
{
return GB_serial_get_data(&gb1);
}
static void init_for_current_model(void)
@ -236,17 +267,29 @@ static void init_for_current_model(void)
if (effective_model == MODEL_AUTO) {
effective_model = auto_model;
}
if (GB_is_inited(&gb)) {
GB_switch_model_and_reset(&gb, effective_model != MODEL_DMG);
if (GB_is_inited(&gb1)) {
GB_switch_model_and_reset(&gb1, effective_model != MODEL_DMG);
}
else {
if (effective_model == MODEL_DMG) {
GB_init(&gb);
GB_init(&gb1);
}
else {
GB_init_cgb(&gb);
GB_init_cgb(&gb1);
}
}
if (GB_is_inited(&gb2)) {
GB_switch_model_and_reset(&gb2, effective_model != MODEL_DMG);
}
else {
if (effective_model == MODEL_DMG) {
GB_init(&gb2);
}
else {
GB_init_cgb(&gb2);
}
}
const char *model_name = (const char *[]){"dmg", "cgb", "agb"}[effective_model];
const unsigned char *boot_code = (const unsigned char *[]){dmg_boot, cgb_boot, agb_boot}[effective_model];
unsigned boot_length = (unsigned []){dmg_boot_length, cgb_boot_length, agb_boot_length}[effective_model];
@ -255,15 +298,27 @@ static void init_for_current_model(void)
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_boot_rom(&gb, buf)) {
GB_load_boot_rom_from_buffer(&gb, boot_code, boot_length);
if (GB_load_boot_rom(&gb1, buf)) {
GB_load_boot_rom_from_buffer(&gb1, boot_code, boot_length);
}
if (GB_load_boot_rom(&gb2, buf)) {
GB_load_boot_rom_from_buffer(&gb2, 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);
GB_set_sample_rate(&gb, AUDIO_FREQUENCY);
GB_set_vblank_callback(&gb1, (GB_vblank_callback_t) vblank1);
GB_set_vblank_callback(&gb2, (GB_vblank_callback_t) vblank2);
GB_set_user_data(&gb1, (void*)NULL);
GB_set_user_data(&gb2, (void*)NULL);
GB_set_pixels_output(&gb1,(unsigned int*)frame_buf);
GB_set_rgb_encode_callback(&gb1, rgb_encode);
GB_set_pixels_output(&gb2,(unsigned int*)(frame_buf + VIDEO_PIXELS));
GB_set_rgb_encode_callback(&gb2, rgb_encode);
GB_set_sample_rate(&gb1, AUDIO_FREQUENCY);
GB_set_sample_rate(&gb2, AUDIO_FREQUENCY);
GB_set_serial_transfer_start_callback(&gb1, serial_start1);
GB_set_serial_transfer_end_callback(&gb1, serial_end1);
GB_set_serial_transfer_start_callback(&gb2, serial_start2);
GB_set_serial_transfer_end_callback(&gb2, serial_end2);
struct retro_memory_descriptor descs[7];
size_t size;
@ -271,32 +326,32 @@ static void init_for_current_model(void)
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(&gb1, 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].ptr = GB_get_direct_access(&gb1, 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].ptr = GB_get_direct_access(&gb1, 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].ptr = GB_get_direct_access(&gb1, 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].ptr = GB_get_direct_access(&gb1, 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].ptr = GB_get_direct_access(&gb1, 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].ptr = GB_get_direct_access(&gb1, GB_DIRECT_ACCESS_OAM, &size, &bank);
descs[6].start = 0xFE00;
descs[6].len = 0x00A0;
@ -312,16 +367,28 @@ static void check_variables(void)
var.key = "sameboy_color_correction_mode";
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(&gb1))
{
if (strcmp(var.value, "off") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_DISABLED);
{
GB_set_color_correction_mode(&gb1, GB_COLOR_CORRECTION_DISABLED);
GB_set_color_correction_mode(&gb2, GB_COLOR_CORRECTION_DISABLED);
}
else if (strcmp(var.value, "correct curves") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_CORRECT_CURVES);
{
GB_set_color_correction_mode(&gb1, GB_COLOR_CORRECTION_CORRECT_CURVES);
GB_set_color_correction_mode(&gb2, GB_COLOR_CORRECTION_CORRECT_CURVES);
}
else if (strcmp(var.value, "emulate hardware") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_EMULATE_HARDWARE);
{
GB_set_color_correction_mode(&gb1, GB_COLOR_CORRECTION_EMULATE_HARDWARE);
GB_set_color_correction_mode(&gb2, GB_COLOR_CORRECTION_EMULATE_HARDWARE);
}
else if (strcmp(var.value, "preserve brightness") == 0)
GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS);
{
GB_set_color_correction_mode(&gb1, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS);
GB_set_color_correction_mode(&gb2, GB_COLOR_CORRECTION_PRESERVE_BRIGHTNESS);
}
}
var.key = "sameboy_high_pass_filter_mode";
@ -329,11 +396,20 @@ static void check_variables(void)
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);
{
GB_set_highpass_filter_mode(&gb1, GB_HIGHPASS_OFF);
GB_set_highpass_filter_mode(&gb2, GB_HIGHPASS_OFF);
}
else if (strcmp(var.value, "accurate") == 0)
GB_set_highpass_filter_mode(&gb, GB_HIGHPASS_ACCURATE);
{
GB_set_highpass_filter_mode(&gb1, GB_HIGHPASS_ACCURATE);
GB_set_highpass_filter_mode(&gb2, GB_HIGHPASS_ACCURATE);
}
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(&gb1, GB_HIGHPASS_REMOVE_DC_OFFSET);
GB_set_highpass_filter_mode(&gb2, GB_HIGHPASS_REMOVE_DC_OFFSET);
}
}
var.key = "sameboy_model";
@ -349,7 +425,11 @@ static void check_variables(void)
new_model = MODEL_AGB;
else if (strcmp(var.value, "Auto") == 0)
new_model = MODEL_AUTO;
if (GB_is_inited(&gb) && new_model != model) {
if (GB_is_inited(&gb1) && new_model != model) {
model = new_model;
init_for_current_model();
}
if (GB_is_inited(&gb2) && new_model != model) {
model = new_model;
init_for_current_model();
}
@ -359,15 +439,23 @@ static void check_variables(void)
}
}
int frames = 0;
void retro_run(void)
{
bool updated = false;
if (!frame_buf)
return;
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE, &updated) && updated)
check_variables();
GB_update_keys_status(&gb);
GB_run_frame(&gb);
video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * sizeof(uint32_t));
GB_run_frame(&gb1);
GB_run_frame(&gb2);
video_cb(frame_buf, VIDEO_WIDTH, VIDEO_HEIGHT * 2, VIDEO_WIDTH * sizeof(uint32_t));
frames++;
}
bool retro_load_game(const struct retro_game_info *info)
@ -398,11 +486,14 @@ bool retro_load_game(const struct retro_game_info *info)
auto_model = (info->path[strlen(info->path) - 1] & ~0x20) == 'C' ? MODEL_CGB : MODEL_DMG;
init_for_current_model();
if (GB_load_rom(&gb,info->path)) {
if (GB_load_rom(&gb1,info->path)) {
log_cb(RETRO_LOG_INFO, "Failed to load ROM\n");
return false;
}
if (GB_load_rom(&gb2,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);
@ -426,7 +517,8 @@ bool retro_load_game(const struct retro_game_info *info)
void retro_unload_game(void)
{
GB_free(&gb);
GB_free(&gb1);
GB_free(&gb2);
}
unsigned retro_get_region(void)
@ -441,12 +533,12 @@ bool retro_load_game_special(unsigned type, const struct retro_game_info *info,
size_t retro_serialize_size(void)
{
return GB_get_save_state_size(&gb);
return GB_get_save_state_size(&gb1);
}
bool retro_serialize(void *data, size_t size)
{
GB_save_state_to_buffer(&gb, (uint8_t*) data);
GB_save_state_to_buffer(&gb1, (uint8_t*) data);
if (data)
return true;
else
@ -455,7 +547,7 @@ bool retro_serialize(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(&gb1, (uint8_t*) data, size) == 0)
return true;
else
return false;
@ -467,28 +559,25 @@ void *retro_get_memory_data(unsigned type)
switch(type)
{
case RETRO_MEMORY_SYSTEM_RAM:
data = gb.ram;
data = gb1.ram;
break;
case RETRO_MEMORY_SAVE_RAM:
if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0)
data = gb.mbc_ram;
if (gb1.cartridge_type->has_battery && gb1.mbc_ram_size != 0)
data = gb1.mbc_ram;
else
data = NULL;
break;
case RETRO_MEMORY_VIDEO_RAM:
data = gb.vram;
data = gb1.vram;
break;
case RETRO_MEMORY_RTC:
if(gb.cartridge_type->has_battery)
data = &gb.rtc_real;
if(gb1.cartridge_type->has_battery)
data = &gb1.rtc_real;
else
data = NULL;
break;
default:
data = NULL;
break;
default:
data = NULL;
}
return data;
}
@ -499,20 +588,20 @@ size_t retro_get_memory_size(unsigned type)
switch(type)
{
case RETRO_MEMORY_SYSTEM_RAM:
size = gb.ram_size;
size = gb1.ram_size;
break;
case RETRO_MEMORY_SAVE_RAM:
if (gb.cartridge_type->has_battery && gb.mbc_ram_size != 0)
size = gb.mbc_ram_size;
if (gb1.cartridge_type->has_battery && gb1.mbc_ram_size != 0)
size = gb1.mbc_ram_size;
else
size = 0;
break;
case RETRO_MEMORY_VIDEO_RAM:
size = gb.vram_size;
size = gb1.vram_size;
break;
case RETRO_MEMORY_RTC:
if(gb.cartridge_type->has_battery)
size = sizeof (gb.rtc_real);
if(gb1.cartridge_type->has_battery)
size = sizeof (gb1.rtc_real);
else
size = 0;
break;
@ -520,6 +609,7 @@ size_t retro_get_memory_size(unsigned type)
size = 0;
break;
}
return size;
}