From e981ebcf96a22eece9d2e21949d63266a859cba8 Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Thu, 24 Sep 2020 06:11:52 +0800 Subject: [PATCH 1/8] input.cpp: Formatting --- mednafen/pce_fast/input.cpp | 420 ++++++++++++++++++------------------ 1 file changed, 212 insertions(+), 208 deletions(-) diff --git a/mednafen/pce_fast/input.cpp b/mednafen/pce_fast/input.cpp index 0706164b..f8b33307 100644 --- a/mednafen/pce_fast/input.cpp +++ b/mednafen/pce_fast/input.cpp @@ -44,296 +44,300 @@ static void SyncSettings(void); void PCEINPUT_SettingChanged(const char *name) { - SyncSettings(); + SyncSettings(); } void PCEINPUT_Init(void) { - SyncSettings(); + SyncSettings(); } void PCEINPUT_SetInput(unsigned port, const char *type, uint8 *ptr) { - assert(port < 5); - - if(!strcmp(type, "gamepad")) - InputTypes[port] = 1; - else if(!strcmp(type, "mouse")) - InputTypes[port] = 2; - else - InputTypes[port] = 0; - data_ptr[port] = (uint8 *)ptr; + assert(port < 5); + + if (!strcmp(type, "gamepad")) + InputTypes[port] = 1; + else if (!strcmp(type, "mouse")) + InputTypes[port] = 2; + else + InputTypes[port] = 0; + data_ptr[port] = (uint8 *)ptr; } void INPUT_TransformInput(void) { - for(int x = 0; x < 5; x++) - { - if(InputTypes[x] == 1) - { - if(DisableSR) + for (int x = 0; x < 5; x++) { - uint16 tmp = MDFN_de16lsb(data_ptr[x]); - - if((tmp & 0xC) == 0xC) - tmp &= ~0xC; - - MDFN_en16lsb(data_ptr[x], tmp); + if (InputTypes[x] == 1) + { + if (DisableSR) + { + uint16 tmp = MDFN_de16lsb(data_ptr[x]); + + if ((tmp & 0xC) == 0xC) + tmp &= ~0xC; + + MDFN_en16lsb(data_ptr[x], tmp); + } + } } - } - } } void INPUT_Frame(void) { - for(int x = 0; x < 5; x++) - { - if(InputTypes[x] == 1) - { - uint16 new_data = data_ptr[x][0] | (data_ptr[x][1] << 8); - pce_jp_data[x] = new_data; - } - else if(InputTypes[x] == 2) - { - mouse_x[x] += (int16)MDFN_de16lsb(data_ptr[x] + 0); - mouse_y[x] += (int16)MDFN_de16lsb(data_ptr[x] + 2); - pce_mouse_button[x] = *(uint8 *)(data_ptr[x] + 4); - } - } + for (int x = 0; x < 5; x++) + { + if (InputTypes[x] == 1) + { + uint16 new_data = data_ptr[x][0] | (data_ptr[x][1] << 8); + pce_jp_data[x] = new_data; + } + else if (InputTypes[x] == 2) + { + mouse_x[x] += (int16)MDFN_de16lsb(data_ptr[x] + 0); + mouse_y[x] += (int16)MDFN_de16lsb(data_ptr[x] + 2); + pce_mouse_button[x] = *(uint8 *)(data_ptr[x] + 4); + } + } } void INPUT_FixTS(void) { - for(int x = 0; x < 5; x++) - { - if(InputTypes[x] == 2) - mouse_last_meow[x] -= HuCPU.timestamp; - } + for (int x = 0; x < 5; x++) + { + if (InputTypes[x] == 2) + mouse_last_meow[x] -= HuCPU.timestamp; + } } static INLINE bool CheckLM(int n) { - if((int64)HuCPU.timestamp - mouse_last_meow[n] > 10000) + if ((int64)HuCPU.timestamp - mouse_last_meow[n] > 10000) { - mouse_last_meow[n] = HuCPU.timestamp; + mouse_last_meow[n] = HuCPU.timestamp; - int32 rel_x = (int32)((0-mouse_x[n])); - int32 rel_y = (int32)((0-mouse_y[n])); + int32 rel_x = (int32)((0 - mouse_x[n])); + int32 rel_y = (int32)((0 - mouse_y[n])); - if(rel_x < -127) rel_x = -127; - if(rel_x > 127) rel_x = 127; - if(rel_y < -127) rel_y = -127; - if(rel_y > 127) rel_y = 127; + if (rel_x < -127) + rel_x = -127; + if (rel_x > 127) + rel_x = 127; + if (rel_y < -127) + rel_y = -127; + if (rel_y > 127) + rel_y = 127; - mouse_rel[n] = ((rel_x & 0xF0) >> 4) | ((rel_x & 0x0F) << 4); - mouse_rel[n] |= (((rel_y & 0xF0) >> 4) | ((rel_y & 0x0F) << 4)) << 8; + mouse_rel[n] = ((rel_x & 0xF0) >> 4) | ((rel_x & 0x0F) << 4); + mouse_rel[n] |= (((rel_y & 0xF0) >> 4) | ((rel_y & 0x0F) << 4)) << 8; - mouse_x[n] += (int32)(rel_x); - mouse_y[n] += (int32)(rel_y); + mouse_x[n] += (int32)(rel_x); + mouse_y[n] += (int32)(rel_y); - return(1); + return (1); } - return(0); + return (0); } uint8 INPUT_Read(unsigned int A) { - uint8 ret = 0xF; - int tmp_ri = read_index; - - if(tmp_ri > 4) - ret ^= 0xF; - else - { - if(!InputTypes[tmp_ri]) - ret ^= 0xF; - else if(InputTypes[tmp_ri] == 2) // Mouse - { - if(sel & 1) - { - CheckLM(tmp_ri); - ret ^= 0xF; - ret ^= mouse_rel[tmp_ri] & 0xF; + uint8 ret = 0xF; + int tmp_ri = read_index; - mouse_rel[tmp_ri] >>= 4; - } + if (tmp_ri > 4) + ret ^= 0xF; else - ret ^= pce_mouse_button[tmp_ri] & 0xF; - } - else - { - if(InputTypes[tmp_ri] == 1) // Gamepad { - if(AVPad6Which[tmp_ri] && (pce_jp_data[tmp_ri] & 0x1000)) - { - if(sel & 1) - ret ^= 0x0F; - else - ret ^= (pce_jp_data[tmp_ri] >> 8) & 0x0F; - } - else - { - if(sel & 1) - ret ^= (pce_jp_data[tmp_ri] >> 4) & 0x0F; - else - ret ^= pce_jp_data[tmp_ri] & 0x0F; - } - if(!(sel & 1)) - AVPad6Which[tmp_ri] = !AVPad6Which[tmp_ri]; + if (!InputTypes[tmp_ri]) + ret ^= 0xF; + else if (InputTypes[tmp_ri] == 2) // Mouse + { + if (sel & 1) + { + CheckLM(tmp_ri); + ret ^= 0xF; + ret ^= mouse_rel[tmp_ri] & 0xF; + + mouse_rel[tmp_ri] >>= 4; + } + else + ret ^= pce_mouse_button[tmp_ri] & 0xF; + } + else + { + if (InputTypes[tmp_ri] == 1) // Gamepad + { + if (AVPad6Which[tmp_ri] && (pce_jp_data[tmp_ri] & 0x1000)) + { + if (sel & 1) + ret ^= 0x0F; + else + ret ^= (pce_jp_data[tmp_ri] >> 8) & 0x0F; + } + else + { + if (sel & 1) + ret ^= (pce_jp_data[tmp_ri] >> 4) & 0x0F; + else + ret ^= pce_jp_data[tmp_ri] & 0x0F; + } + if (!(sel & 1)) + AVPad6Which[tmp_ri] = !AVPad6Which[tmp_ri]; + } + } } - } - } - if(!PCE_IsCD) - ret |= 0x80; // Set when CDROM is not attached + if (!PCE_IsCD) + ret |= 0x80; // Set when CDROM is not attached - //ret |= 0x40; // PC Engine if set, TG16 if clear. Let's leave it clear, PC Engine games don't seem to mind if it's clear, but TG16 games barf if it's set. + //ret |= 0x40; // PC Engine if set, TG16 if clear. Let's leave it clear, PC Engine games don't seem to mind if it's clear, but TG16 games barf if it's set. - ret |= 0x30; // Always-set? + ret |= 0x30; // Always-set? - return(ret); + return (ret); } void INPUT_Write(unsigned int A, uint8 V) { - if((V & 1) && !(sel & 2) && (V & 2)) - { - read_index = 0; - } - else if((V & 1) && !(sel & 1)) - { - if(read_index < 255) - read_index++; - } - sel = V & 3; + if ((V & 1) && !(sel & 2) && (V & 2)) + { + read_index = 0; + } + else if ((V & 1) && !(sel & 1)) + { + if (read_index < 255) + read_index++; + } + sel = V & 3; } int INPUT_StateAction(StateMem *sm, int load, int data_only) { - SFORMAT StateRegs[] = - { - // 0.8.A fix: - SFARRAYB(AVPad6Enabled, 5), - SFARRAYB(AVPad6Which, 5), - - SFVARN(mouse_last_meow[0], "mlm_0"), - SFVARN(mouse_last_meow[1], "mlm_1"), - SFVARN(mouse_last_meow[2], "mlm_2"), - SFVARN(mouse_last_meow[3], "mlm_3"), - SFVARN(mouse_last_meow[4], "mlm_4"), - - SFARRAY32(mouse_x, 5), - SFARRAY32(mouse_y, 5), - SFARRAY16(mouse_rel, 5), - SFARRAY(pce_mouse_button, 5), - SFARRAY(mouse_index, 5), - // end 0.8.A fix - - SFARRAY16(pce_jp_data, 5), - SFVAR(sel), - SFVAR(read_index), - SFEND - }; - int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "JOY", false); - - return(ret); + SFORMAT StateRegs[] = + { + // 0.8.A fix: + SFARRAYB(AVPad6Enabled, 5), + SFARRAYB(AVPad6Which, 5), + + SFVARN(mouse_last_meow[0], "mlm_0"), + SFVARN(mouse_last_meow[1], "mlm_1"), + SFVARN(mouse_last_meow[2], "mlm_2"), + SFVARN(mouse_last_meow[3], "mlm_3"), + SFVARN(mouse_last_meow[4], "mlm_4"), + + SFARRAY32(mouse_x, 5), + SFARRAY32(mouse_y, 5), + SFARRAY16(mouse_rel, 5), + SFARRAY(pce_mouse_button, 5), + SFARRAY(mouse_index, 5), + // end 0.8.A fix + + SFARRAY16(pce_jp_data, 5), + SFVAR(sel), + SFVAR(read_index), + SFEND + }; + int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "JOY", false); + + return (ret); } // GamepadIDII and GamepadIDII_DSR must be EXACTLY the same except for the RUN+SELECT exclusion in the latter. static const InputDeviceInputInfoStruct GamepadIDII[] = { - { "i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL }, - { "ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL }, - { "select", "SELECT", 4, IDIT_BUTTON, NULL }, - { "run", "RUN", 5, IDIT_BUTTON, NULL }, - { "up", "UP ↑", 0, IDIT_BUTTON, "down" }, - { "right", "RIGHT →", 3, IDIT_BUTTON, "left" }, - { "down", "DOWN ↓", 1, IDIT_BUTTON, "up" }, - { "left", "LEFT ←", 2, IDIT_BUTTON, "right" }, - { "iii", "III", 10, IDIT_BUTTON, NULL }, - { "iv", "IV", 7, IDIT_BUTTON, NULL }, - { "v", "V", 8, IDIT_BUTTON, NULL }, - { "vi", "VI", 9, IDIT_BUTTON, NULL }, - { "mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL }, + {"i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL}, + {"ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL}, + {"select", "SELECT", 4, IDIT_BUTTON, NULL}, + {"run", "RUN", 5, IDIT_BUTTON, NULL}, + {"up", "UP ↑", 0, IDIT_BUTTON, "down"}, + {"right", "RIGHT →", 3, IDIT_BUTTON, "left"}, + {"down", "DOWN ↓", 1, IDIT_BUTTON, "up"}, + {"left", "LEFT ←", 2, IDIT_BUTTON, "right"}, + {"iii", "III", 10, IDIT_BUTTON, NULL}, + {"iv", "IV", 7, IDIT_BUTTON, NULL}, + {"v", "V", 8, IDIT_BUTTON, NULL}, + {"vi", "VI", 9, IDIT_BUTTON, NULL}, + {"mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL}, }; static const InputDeviceInputInfoStruct GamepadIDII_DSR[] = { - { "i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL }, - { "ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL }, - { "select", "SELECT", 4, IDIT_BUTTON, "run" }, - { "run", "RUN", 5, IDIT_BUTTON, "select" }, - { "up", "UP ↑", 0, IDIT_BUTTON, "down" }, - { "right", "RIGHT →", 3, IDIT_BUTTON, "left" }, - { "down", "DOWN ↓", 1, IDIT_BUTTON, "up" }, - { "left", "LEFT ←", 2, IDIT_BUTTON, "right" }, - { "iii", "III", 10, IDIT_BUTTON, NULL }, - { "iv", "IV", 7, IDIT_BUTTON, NULL }, - { "v", "V", 8, IDIT_BUTTON, NULL }, - { "vi", "VI", 9, IDIT_BUTTON, NULL }, - { "mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL }, + {"i", "I", 12, IDIT_BUTTON_CAN_RAPID, NULL}, + {"ii", "II", 11, IDIT_BUTTON_CAN_RAPID, NULL}, + {"select", "SELECT", 4, IDIT_BUTTON, "run"}, + {"run", "RUN", 5, IDIT_BUTTON, "select"}, + {"up", "UP ↑", 0, IDIT_BUTTON, "down"}, + {"right", "RIGHT →", 3, IDIT_BUTTON, "left"}, + {"down", "DOWN ↓", 1, IDIT_BUTTON, "up"}, + {"left", "LEFT ←", 2, IDIT_BUTTON, "right"}, + {"iii", "III", 10, IDIT_BUTTON, NULL}, + {"iv", "IV", 7, IDIT_BUTTON, NULL}, + {"v", "V", 8, IDIT_BUTTON, NULL}, + {"vi", "VI", 9, IDIT_BUTTON, NULL}, + {"mode_select", "2/6 Mode Select", 6, IDIT_BUTTON, NULL}, }; static const InputDeviceInputInfoStruct MouseIDII[] = { - { "x_axis", "X Axis", -1, IDIT_X_AXIS_REL }, - { "y_axis", "Y Axis", -1, IDIT_Y_AXIS_REL }, - { "left", "Left Button", 0, IDIT_BUTTON, NULL }, - { "right", "Right Button", 1, IDIT_BUTTON, NULL }, + {"x_axis", "X Axis", -1, IDIT_X_AXIS_REL}, + {"y_axis", "Y Axis", -1, IDIT_Y_AXIS_REL}, + {"left", "Left Button", 0, IDIT_BUTTON, NULL}, + {"right", "Right Button", 1, IDIT_BUTTON, NULL}, }; // If we add more devices to this array, REMEMBER TO UPDATE the hackish array indexing in the SyncSettings() function // below. static InputDeviceInfoStruct InputDeviceInfo[] = { - // None - { - "none", - "none", - NULL, - NULL, - 0, - NULL - }, - - // Gamepad - { - "gamepad", - "Gamepad", - NULL, - NULL, - sizeof(GamepadIDII) / sizeof(InputDeviceInputInfoStruct), - GamepadIDII, - }, - - // Mouse - { - "mouse", - "Mouse", - NULL, - NULL, - sizeof(MouseIDII) / sizeof(InputDeviceInputInfoStruct), - MouseIDII, - }, + // None + { + "none", + "none", + NULL, + NULL, + 0, + NULL + }, + + // Gamepad + { + "gamepad", + "Gamepad", + NULL, + NULL, + sizeof(GamepadIDII) / sizeof(InputDeviceInputInfoStruct), + GamepadIDII, + }, + + // Mouse + { + "mouse", + "Mouse", + NULL, + NULL, + sizeof(MouseIDII) / sizeof(InputDeviceInputInfoStruct), + MouseIDII, + }, }; static const InputPortInfoStruct PortInfo[] = { - { "port1", "Port 1", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }, - { "port2", "Port 2", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }, - { "port3", "Port 3", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }, - { "port4", "Port 4", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }, - { "port5", "Port 5", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad" }, + {"port1", "Port 1", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad"}, + {"port2", "Port 2", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad"}, + {"port3", "Port 3", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad"}, + {"port4", "Port 4", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad"}, + {"port5", "Port 5", sizeof(InputDeviceInfo) / sizeof(InputDeviceInfoStruct), InputDeviceInfo, "gamepad"}, }; InputInfoStruct PCEInputInfo = { - sizeof(PortInfo) / sizeof(InputPortInfoStruct), - PortInfo + sizeof(PortInfo) / sizeof(InputPortInfoStruct), + PortInfo }; static void SyncSettings(void) { - MDFNGameInfo->mouse_sensitivity = MDFN_GetSettingF("pce_fast.mouse_sensitivity"); - DisableSR = MDFN_GetSettingB("pce_fast.disable_softreset"); + MDFNGameInfo->mouse_sensitivity = MDFN_GetSettingF("pce_fast.mouse_sensitivity"); + DisableSR = MDFN_GetSettingB("pce_fast.disable_softreset"); } From 94b4b64cfbed2c4a9e688e5107ba4ca29c47961f Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Thu, 24 Sep 2020 06:13:56 +0800 Subject: [PATCH 2/8] Turn some vars into static --- mednafen/pce_fast/input.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/mednafen/pce_fast/input.cpp b/mednafen/pce_fast/input.cpp index f8b33307..27d81621 100644 --- a/mednafen/pce_fast/input.cpp +++ b/mednafen/pce_fast/input.cpp @@ -19,21 +19,22 @@ #include "input.h" #include "../mednafen-endian.h" +bool AVPad6Enabled[5] = { 0 }; + static int InputTypes[5]; static uint8 *data_ptr[5]; static bool AVPad6Which[5]; // Lower(8 buttons) or higher(4 buttons). -bool AVPad6Enabled[5]; -uint16 pce_jp_data[5]; +static uint16 pce_jp_data[5]; static int64 mouse_last_meow[5]; static int32 mouse_x[5], mouse_y[5]; static uint16 mouse_rel[5]; -uint8 pce_mouse_button[5]; -uint8 mouse_index[5]; +static uint8 pce_mouse_button[5]; +static uint8 mouse_index[5]; static uint8 sel; static uint8 read_index = 0; From 6b6611245204d2be326a980767af0d3f9696fd18 Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Thu, 24 Sep 2020 13:25:09 +0800 Subject: [PATCH 3/8] input: Add option to disable turbotap (multitap) --- libretro.cpp | 19 ++++- libretro_core_options.h | 11 +++ mednafen/pce_fast/input.cpp | 140 ++++++++++++++++++++++++------------ mednafen/settings.cpp | 23 +++--- mednafen/settings.h | 3 +- 5 files changed, 137 insertions(+), 59 deletions(-) diff --git a/libretro.cpp b/libretro.cpp index a8ac6cc6..a0c450ec 100644 --- a/libretro.cpp +++ b/libretro.cpp @@ -1115,6 +1115,21 @@ static void check_variables(void) } } + var.key = "sgx_multitap"; + + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + bool oldval = setting_pce_fast_multitap; + + if (strcmp(var.value, "enabled") == 0) + setting_pce_fast_multitap = true; + else + setting_pce_fast_multitap = false; + + if (setting_pce_fast_multitap != oldval) + PCEINPUT_SettingChanged(NULL); + } + var.key = "sgx_turbo_delay"; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) @@ -1472,7 +1487,9 @@ static void update_input_turbo(int port, int &input_state, int input_data) static void update_input(void) { - for (unsigned port = 0; port < MAX_PLAYERS; port++) + unsigned max_port = setting_pce_fast_multitap ? MAX_PLAYERS : 1; + + for (unsigned port = 0; port < max_port; port++) { RETRO_DEVICE_INFO *cur_device = &(r_input.device[port]); diff --git a/libretro_core_options.h b/libretro_core_options.h index ac92cada..46d0fc55 100644 --- a/libretro_core_options.h +++ b/libretro_core_options.h @@ -373,6 +373,17 @@ struct retro_core_option_definition option_defs_us[] = { }, "1" }, + { + "sgx_multitap", + "Multitap", + "Enables up to 5-player multitap emulation. Leave it (enabled). Disabling it is only needed on some cases (Cho Aniki).", + { + { "enabled", NULL }, + { "disabled", NULL }, + { NULL, NULL}, + }, + "enabled" + }, { "sgx_turbo_delay", "Turbo Delay", diff --git a/mednafen/pce_fast/input.cpp b/mednafen/pce_fast/input.cpp index 27d81621..0ce25411 100644 --- a/mednafen/pce_fast/input.cpp +++ b/mednafen/pce_fast/input.cpp @@ -40,6 +40,7 @@ static uint8 sel; static uint8 read_index = 0; static bool DisableSR; +static bool EnableMultitap; static void SyncSettings(void); @@ -141,75 +142,121 @@ static INLINE bool CheckLM(int n) return (0); } -uint8 INPUT_Read(unsigned int A) +static uint8 ReadPortMouse(int n) { uint8 ret = 0xF; - int tmp_ri = read_index; - if (tmp_ri > 4) + if (sel & 1) + { + CheckLM(n); ret ^= 0xF; + ret ^= mouse_rel[n] & 0xF; + + mouse_rel[n] >>= 4; + } else - { - if (!InputTypes[tmp_ri]) - ret ^= 0xF; - else if (InputTypes[tmp_ri] == 2) // Mouse - { - if (sel & 1) - { - CheckLM(tmp_ri); - ret ^= 0xF; - ret ^= mouse_rel[tmp_ri] & 0xF; + ret ^= pce_mouse_button[n] & 0xF; + return (ret); +} - mouse_rel[tmp_ri] >>= 4; - } - else - ret ^= pce_mouse_button[tmp_ri] & 0xF; - } +static uint8 ReadPortGamepad(int n) +{ + uint8 ret = 0xF; + + if (AVPad6Which[n] && (pce_jp_data[n] & 0x1000)) + { + if (sel & 1) + ret ^= 0x0F; else - { - if (InputTypes[tmp_ri] == 1) // Gamepad - { - if (AVPad6Which[tmp_ri] && (pce_jp_data[tmp_ri] & 0x1000)) - { - if (sel & 1) - ret ^= 0x0F; - else - ret ^= (pce_jp_data[tmp_ri] >> 8) & 0x0F; - } - else - { - if (sel & 1) - ret ^= (pce_jp_data[tmp_ri] >> 4) & 0x0F; - else - ret ^= pce_jp_data[tmp_ri] & 0x0F; - } - if (!(sel & 1)) - AVPad6Which[tmp_ri] = !AVPad6Which[tmp_ri]; - } - } + ret ^= (pce_jp_data[n] >> 8) & 0x0F; + } + else + { + if (sel & 1) + ret ^= (pce_jp_data[n] >> 4) & 0x0F; + else + ret ^= pce_jp_data[n] & 0x0F; } - if (!PCE_IsCD) - ret |= 0x80; // Set when CDROM is not attached + return (ret); +} - //ret |= 0x40; // PC Engine if set, TG16 if clear. Let's leave it clear, PC Engine games don't seem to mind if it's clear, but TG16 games barf if it's set. +static uint8 ReadPort(int n) +{ + uint8 ret = 0xF; - ret |= 0x30; // Always-set? + if (!InputTypes[n]) + ret ^= 0xF; + else if (InputTypes[n] == 2) // Mouse + ret = ReadPortMouse(n); + else if (InputTypes[n] == 1) // Gamepad + ret = ReadPortGamepad(n); return (ret); } -void INPUT_Write(unsigned int A, uint8 V) +static void WritePort(int n, uint8 V) { - if ((V & 1) && !(sel & 2) && (V & 2)) + if (InputTypes[n] == 1) { - read_index = 0; + if (!(sel & 1) && (V & 1)) + AVPad6Which[n] = !AVPad6Which[n]; } +} + +static uint8 ReadPortMultitap(int n) +{ + uint8 ret = 0xF; + + if (n > 4) + ret ^= 0xF; + else + ret = ReadPort(n); + + return (ret); +} + +static void WritePortMultitap(uint8 V) +{ + for (int i = 0; i < 5; i++) + WritePort(i, V); + + if ((V & 1) && !(sel & 2) && (V & 2)) + read_index = 0; else if ((V & 1) && !(sel & 1)) { if (read_index < 255) read_index++; } +} + +uint8 INPUT_Read(unsigned int A) +{ + uint8 ret = 0xF; + int tmp_ri = read_index; + + if (EnableMultitap) + ret = ReadPortMultitap(tmp_ri); + else + ret = ReadPort(0); + + if (!PCE_IsCD) + ret |= 0x80; // Set when CDROM is not attached + + //ret |= 0x40; // PC Engine if set, TG16 if clear. Let's leave it clear, PC Engine games don't seem to mind if it's clear, but TG16 games barf if it's set. + + ret |= 0x30; // Always-set? + + return (ret); +} + +void INPUT_Write(unsigned int A, uint8 V) +{ + if (EnableMultitap) + WritePortMultitap(V); + else + WritePort(0, V); + sel = V & 3; } @@ -340,5 +387,6 @@ InputInfoStruct PCEInputInfo = static void SyncSettings(void) { MDFNGameInfo->mouse_sensitivity = MDFN_GetSettingF("pce_fast.mouse_sensitivity"); + EnableMultitap = MDFN_GetSettingB("pce_fast.input.multitap"); DisableSR = MDFN_GetSettingB("pce_fast.disable_softreset"); } diff --git a/mednafen/settings.cpp b/mednafen/settings.cpp index 59ed5c99..3806237c 100644 --- a/mednafen/settings.cpp +++ b/mednafen/settings.cpp @@ -21,17 +21,18 @@ #include #include "settings.h" -bool setting_pce_fast_gexpress = true; -bool setting_pce_fast_forcesgx = true; +bool setting_pce_fast_gexpress = true; +bool setting_pce_fast_forcesgx = true; bool setting_pce_fast_nospritelimit = false; -int setting_initial_scanline = 0; -int setting_last_scanline = 242; -int setting_pce_hoverscan = 352; -int setting_pce_overclocked = 1; -int setting_pce_fast_cddavolume = 100; -int setting_pce_fast_adpcmvolume = 100; -int setting_pce_fast_cdpsgvolume = 100; -uint32_t setting_pce_fast_cdspeed = 1; +bool setting_pce_fast_multitap = false; +int setting_initial_scanline = 0; +int setting_last_scanline = 242; +int setting_pce_hoverscan = 352; +int setting_pce_overclocked = 1; +int setting_pce_fast_cddavolume = 100; +int setting_pce_fast_adpcmvolume = 100; +int setting_pce_fast_cdpsgvolume = 100; +int setting_pce_fast_cdspeed = 1; std::string setting_pce_fast_cdbios = "syscard3.pce"; bool OrderOfGriffonFix = false; @@ -88,7 +89,7 @@ bool MDFN_GetSettingB(const char *name) if (!strcmp("libretro.cd_load_into_ram", name)) return 0; if (!strcmp("pce_fast.input.multitap", name)) - return 1; + return setting_pce_fast_multitap; if (!strcmp("pce_fast.arcadecard", name)) return 1; if (!strcmp("pce_fast.forcesgx", name)) diff --git a/mednafen/settings.h b/mednafen/settings.h index 7c7f7083..6351a02e 100644 --- a/mednafen/settings.h +++ b/mednafen/settings.h @@ -4,6 +4,7 @@ extern bool setting_pce_fast_gexpress; extern bool setting_pce_fast_forcesgx; extern bool setting_pce_fast_nospritelimit; +extern bool setting_pce_fast_multitap; extern int setting_initial_scanline; extern int setting_last_scanline; extern int setting_pce_overclocked; @@ -11,7 +12,7 @@ extern int setting_pce_hoverscan; extern int setting_pce_fast_cddavolume; extern int setting_pce_fast_adpcmvolume; extern int setting_pce_fast_cdpsgvolume; -extern uint32_t setting_pce_fast_cdspeed; +extern int setting_pce_fast_cdspeed; extern std::string setting_pce_fast_cdbios; extern bool OrderOfGriffonFix; From a9a11eabd32e2eb938cb1846109ce33b0a640a57 Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Thu, 24 Sep 2020 16:31:07 +0800 Subject: [PATCH 4/8] Move soft-reset handling to input.cpp --- libretro.cpp | 22 +++++++++++++--------- mednafen/pce_fast/input.h | 1 + mednafen/pce_fast/pce.cpp | 2 ++ mednafen/settings.cpp | 3 ++- mednafen/settings.h | 1 + 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libretro.cpp b/libretro.cpp index a0c450ec..a4d66eb0 100644 --- a/libretro.cpp +++ b/libretro.cpp @@ -862,7 +862,6 @@ struct RETRO_INPUT { bool turbo_toggle_alt; // system options - bool disable_softreset; bool up_down_allowed; RETRO_DEVICE_INFO device[MAX_PLAYERS]; @@ -942,6 +941,7 @@ bool retro_load_game_special(unsigned, const struct retro_game_info *, size_t) static void check_variables(void) { + bool input_changed = false; struct retro_variable var = { 0 }; var.key = "sgx_cdimagecache"; @@ -1127,7 +1127,7 @@ static void check_variables(void) setting_pce_fast_multitap = false; if (setting_pce_fast_multitap != oldval) - PCEINPUT_SettingChanged(NULL); + input_changed = true; } var.key = "sgx_turbo_delay"; @@ -1170,7 +1170,17 @@ static void check_variables(void) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - r_input.disable_softreset = (strcmp(var.value, "enabled") == 0); + bool oldval = setting_pce_fast_softreset; + + setting_pce_fast_softreset = (bool)(strcmp(var.value, "enabled") == 0); + + if (setting_pce_fast_softreset != oldval) + input_changed = true; + } + + if (input_changed) + { + PCEINPUT_SettingChanged(NULL); } var.key = "sgx_up_down_allowed"; @@ -1560,12 +1570,6 @@ static void update_input(void) else cur_device->AVPad6_toggle_down = 0; - if (r_input.disable_softreset == true) - { - if ((input_state & 0xC) == 0xC) - input_state &= ~0xC; - } - if (r_input.up_down_allowed == false) { if ((input_state & 0x50) == 0x50) diff --git a/mednafen/pce_fast/input.h b/mednafen/pce_fast/input.h index 7374b095..ddde1ad5 100644 --- a/mednafen/pce_fast/input.h +++ b/mednafen/pce_fast/input.h @@ -3,6 +3,7 @@ void PCEINPUT_Init(void); void PCEINPUT_SettingChanged(const char *name); +void INPUT_TransformInput(void); void PCEINPUT_SetInput(unsigned port, const char *type, uint8 *ptr); uint8 INPUT_Read(unsigned int A); void INPUT_Write(unsigned int A, uint8 V); diff --git a/mednafen/pce_fast/pce.cpp b/mednafen/pce_fast/pce.cpp index b7c608ca..195275e8 100644 --- a/mednafen/pce_fast/pce.cpp +++ b/mednafen/pce_fast/pce.cpp @@ -510,6 +510,8 @@ void CloseGame(void) void Emulate(EmulateSpecStruct *espec) { + INPUT_TransformInput(); + INPUT_Frame(); MDFNMP_ApplyPeriodicCheats(); diff --git a/mednafen/settings.cpp b/mednafen/settings.cpp index 3806237c..97161149 100644 --- a/mednafen/settings.cpp +++ b/mednafen/settings.cpp @@ -25,6 +25,7 @@ bool setting_pce_fast_gexpress = true; bool setting_pce_fast_forcesgx = true; bool setting_pce_fast_nospritelimit = false; bool setting_pce_fast_multitap = false; +bool setting_pce_fast_softreset = false; int setting_initial_scanline = 0; int setting_last_scanline = 242; int setting_pce_hoverscan = 352; @@ -99,7 +100,7 @@ bool MDFN_GetSettingB(const char *name) if (!strcmp("pce_fast.forcemono", name)) return 0; if (!strcmp("pce_fast.disable_softreset", name)) - return 0; + return setting_pce_fast_softreset; if (!strcmp("pce_fast.adpcmlp", name)) return 0; /* CDROM */ diff --git a/mednafen/settings.h b/mednafen/settings.h index 6351a02e..2944733d 100644 --- a/mednafen/settings.h +++ b/mednafen/settings.h @@ -5,6 +5,7 @@ extern bool setting_pce_fast_gexpress; extern bool setting_pce_fast_forcesgx; extern bool setting_pce_fast_nospritelimit; extern bool setting_pce_fast_multitap; +extern bool setting_pce_fast_softreset; extern int setting_initial_scanline; extern int setting_last_scanline; extern int setting_pce_overclocked; From d2223ffc6897d5fdb5c74458ee62223b1ce9139f Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Thu, 24 Sep 2020 18:32:41 +0800 Subject: [PATCH 5/8] Move 2/6 button mode switch back to input.cpp --- libretro.cpp | 17 +---------------- mednafen/pce_fast/input.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/libretro.cpp b/libretro.cpp index a4d66eb0..53427f4b 100644 --- a/libretro.cpp +++ b/libretro.cpp @@ -847,7 +847,6 @@ struct RETRO_DEVICE_INFO { // Array to keep track of each buttons turbo status int turbo_counter[MAX_BUTTONS]; int turbo_toggle_down[MAX_BUTTONS]; - int AVPad6_toggle_down; uint8_t data[5]; }; @@ -1549,27 +1548,13 @@ static void update_input(void) input_state |= JOY_V; if (ret & BIT(RETRO_DEVICE_ID_JOYPAD_R)) input_state |= JOY_VI; - if (AVPad6Enabled[port]) + if (ret & BIT(RETRO_DEVICE_ID_JOYPAD_L2)) input_state |= JOY_MODE; // process turbo buttons only when in 2-button mode if (r_input.turbo_toggle != 0 && !AVPad6Enabled[port]) update_input_turbo(port, input_state, ret); - // 2/6 buttom mode switching - if (ret & BIT(RETRO_DEVICE_ID_JOYPAD_L2)) - { - if (cur_device->AVPad6_toggle_down == 0) - { - cur_device->AVPad6_toggle_down = !cur_device->AVPad6_toggle_down; - AVPad6Enabled[port] = !AVPad6Enabled[port]; - MDFN_DispMessage("%d-button mode selected for pad %d", - AVPad6Enabled[port] ? 6 : 2, port + 1); - } - } - else - cur_device->AVPad6_toggle_down = 0; - if (r_input.up_down_allowed == false) { if ((input_state & 0x50) == 0x50) diff --git a/mednafen/pce_fast/input.cpp b/mednafen/pce_fast/input.cpp index 0ce25411..4ff65199 100644 --- a/mednafen/pce_fast/input.cpp +++ b/mednafen/pce_fast/input.cpp @@ -93,6 +93,14 @@ void INPUT_Frame(void) if (InputTypes[x] == 1) { uint16 new_data = data_ptr[x][0] | (data_ptr[x][1] << 8); + + if ((new_data & 0x1000) && !(pce_jp_data[x] & 0x1000)) + { + AVPad6Enabled[x] = !AVPad6Enabled[x]; + MDFN_DispMessage("%d-button mode selected for pad %d", + AVPad6Enabled[x] ? 6 : 2, x + 1); + } + pce_jp_data[x] = new_data; } else if (InputTypes[x] == 2) @@ -163,7 +171,7 @@ static uint8 ReadPortGamepad(int n) { uint8 ret = 0xF; - if (AVPad6Which[n] && (pce_jp_data[n] & 0x1000)) + if (AVPad6Which[n] && AVPad6Enabled[n]) { if (sel & 1) ret ^= 0x0F; From 7d6348ae67cf86d2f6fe8c80ed5924a9e0fa0020 Mon Sep 17 00:00:00 2001 From: negativeExponent Date: Fri, 25 Sep 2020 00:16:05 +0800 Subject: [PATCH 6/8] Update libretro-common - updates libretro-common, adds time/rtime --- Makefile.common | 3 +- libretro-common/cdrom/cdrom.c | 57 +- libretro-common/compat/compat_posix_string.c | 2 +- libretro-common/compat/compat_snprintf.c | 2 +- libretro-common/compat/compat_strcasestr.c | 2 +- libretro-common/compat/compat_strl.c | 2 +- libretro-common/compat/fopen_utf8.c | 2 +- libretro-common/encodings/encoding_utf.c | 124 +++-- libretro-common/file/file_path.c | 317 ++++------- libretro-common/file/retro_dirent.c | 46 +- libretro-common/include/boolean.h | 2 +- libretro-common/include/cdrom/cdrom.h | 8 +- libretro-common/include/compat/apple_compat.h | 2 +- libretro-common/include/compat/fnmatch.h | 2 +- libretro-common/include/compat/fopen_utf8.h | 2 +- libretro-common/include/compat/getopt.h | 2 +- libretro-common/include/compat/intrinsics.h | 4 +- libretro-common/include/compat/msvc.h | 13 +- libretro-common/include/compat/posix_string.h | 6 +- libretro-common/include/compat/strcasestr.h | 6 +- libretro-common/include/compat/strl.h | 2 +- libretro-common/include/encodings/utf.h | 4 +- libretro-common/include/file/file_path.h | 14 +- libretro-common/include/libretro.h | 288 +++++++++- libretro-common/include/lists/dir_list.h | 14 +- libretro-common/include/lists/string_list.h | 12 +- libretro-common/include/memalign.h | 2 +- libretro-common/include/memmap.h | 2 +- libretro-common/include/retro_assert.h | 2 +- libretro-common/include/retro_common.h | 2 +- libretro-common/include/retro_common_api.h | 6 +- libretro-common/include/retro_dirent.h | 2 +- libretro-common/include/retro_endianness.h | 524 ++++++++++++++---- libretro-common/include/retro_environment.h | 2 +- libretro-common/include/retro_inline.h | 2 +- libretro-common/include/retro_math.h | 2 +- libretro-common/include/retro_miscellaneous.h | 26 +- libretro-common/include/retro_timers.h | 34 +- libretro-common/include/streams/file_stream.h | 2 +- .../include/streams/file_stream_transforms.h | 2 +- libretro-common/include/string/stdstring.h | 72 ++- libretro-common/include/time/rtime.h | 48 ++ libretro-common/include/vfs/vfs.h | 26 +- .../include/vfs/vfs_implementation.h | 2 +- .../include/vfs/vfs_implementation_cdrom.h | 2 +- libretro-common/lists/dir_list.c | 64 ++- libretro-common/lists/string_list.c | 183 +++++- libretro-common/memmap/memalign.c | 2 +- libretro-common/streams/file_stream.c | 199 ++++--- .../streams/file_stream_transforms.c | 2 +- libretro-common/string/stdstring.c | 65 ++- libretro-common/time/rtime.c | 81 +++ libretro-common/vfs/vfs_implementation.c | 337 +++++------ .../vfs/vfs_implementation_cdrom.c | 268 +++++---- 54 files changed, 1924 insertions(+), 975 deletions(-) create mode 100644 libretro-common/include/time/rtime.h create mode 100644 libretro-common/time/rtime.c diff --git a/Makefile.common b/Makefile.common index 1ad64094..96fd2268 100644 --- a/Makefile.common +++ b/Makefile.common @@ -243,5 +243,6 @@ ifneq ($(STATIC_LINKING), 1) $(LIBRETRO_COMM_DIR)/encodings/encoding_utf.c \ $(LIBRETRO_COMM_DIR)/vfs/vfs_implementation.c \ $(LIBRETRO_COMM_DIR)/memmap/memalign.c \ - $(LIBRETRO_COMM_DIR)/string/stdstring.c + $(LIBRETRO_COMM_DIR)/string/stdstring.c \ + $(LIBRETRO_COMM_DIR)/time/rtime.c endif diff --git a/libretro-common/cdrom/cdrom.c b/libretro-common/cdrom/cdrom.c index cb5ff8c1..75c63e52 100644 --- a/libretro-common/cdrom/cdrom.c +++ b/libretro-common/cdrom/cdrom.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2019 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (cdrom.c). @@ -96,6 +96,7 @@ void increment_msf(unsigned char *min, unsigned char *sec, unsigned char *frame) *frame = (*frame < 74) ? (*frame + 1) : 0; } +#ifdef CDROM_DEBUG static void cdrom_print_sense_data(const unsigned char *sense, size_t len) { unsigned i; @@ -252,6 +253,7 @@ static void cdrom_print_sense_data(const unsigned char *sense, size_t len) fflush(stdout); } +#endif #if defined(_WIN32) && !defined(_XBOX) static int cdrom_send_command_win32(const libretro_vfs_implementation_file *stream, CDROM_CMD_Direction dir, void *buf, size_t len, unsigned char *cmd, size_t cmd_len, unsigned char *sense, size_t sense_len) @@ -519,7 +521,9 @@ static int cdrom_send_command(libretro_vfs_implementation_file *stream, CDROM_CM } else { +#ifdef CDROM_DEBUG cdrom_print_sense_data(sense, sizeof(sense)); +#endif /* INQUIRY/TEST/SENSE should never fail, don't retry. */ /* READ ATIP seems to fail outright on some drives with pressed discs, skip retries. */ @@ -672,7 +676,9 @@ int cdrom_get_sense(libretro_vfs_implementation_file *stream, unsigned char *sen if (rv) return 1; +#ifdef CDROM_DEBUG cdrom_print_sense_data(buf, sizeof(buf)); +#endif return 0; } @@ -1336,22 +1342,26 @@ struct string_list* cdrom_get_available_drives(void) for (i = 0; i < (int)dir_list->size; i++) { - if (strstr(dir_list->elems[i].data, "/dev/sg")) + if (string_starts_with_size(dir_list->elems[i].data, "/dev/sg", + STRLEN_CONST("/dev/sg"))) { - char drive_model[32] = {0}; - char drive_string[33] = {0}; - union string_list_elem_attr attr = {0}; - int dev_index = 0; - RFILE *file = filestream_open(dir_list->elems[i].data, RETRO_VFS_FILE_ACCESS_READ, 0); libretro_vfs_implementation_file *stream; - bool is_cdrom = false; + char drive_model[32] = {0}; + char drive_string[33] = {0}; + union string_list_elem_attr attr = {0}; + int dev_index = 0; + RFILE *file = filestream_open( + dir_list->elems[i].data, RETRO_VFS_FILE_ACCESS_READ, 0); + bool is_cdrom = false; found = true; if (!file) { +#ifdef CDROM_DEBUG printf("[CDROM] Could not open %s, please check permissions.\n", dir_list->elems[i].data); fflush(stdout); +#endif continue; } @@ -1362,10 +1372,11 @@ struct string_list* cdrom_get_available_drives(void) if (!is_cdrom) continue; - sscanf(dir_list->elems[i].data + strlen("/dev/sg"), "%d", &dev_index); + sscanf(dir_list->elems[i].data + STRLEN_CONST("/dev/sg"), + "%d", &dev_index); dev_index = '0' + dev_index; - attr.i = dev_index; + attr.i = dev_index; if (!string_is_empty(drive_model)) strlcat(drive_string, drive_model, sizeof(drive_string)); @@ -1378,29 +1389,34 @@ struct string_list* cdrom_get_available_drives(void) if (!found) { - char *buf = NULL; + char *buf = NULL; int64_t len = 0; if (filestream_read_file("/proc/modules", (void**)&buf, &len)) { - struct string_list *mods = string_split(buf, "\n"); - bool found = false; +#ifdef CDROM_DEBUG + bool found = false; +#endif + struct string_list mods = {0}; - if (mods) + string_list_initialize(&mods); + + if (string_split_noalloc(&mods, buf, "\n")) { - - for (i = 0; i < mods->size; i++) + for (i = 0; i < mods.size; i++) { - if (strcasestr(mods->elems[i].data, "sg ")) + if (strcasestr(mods.elems[i].data, "sg ")) { +#ifdef CDROM_DEBUG found = true; +#endif break; } } - - string_list_free(mods); } + string_list_deinitialize(&mods); +#ifdef CDROM_DEBUG if (found) { printf("[CDROM] No sg devices found but kernel module is loaded.\n"); @@ -1411,12 +1427,15 @@ struct string_list* cdrom_get_available_drives(void) printf("[CDROM] No sg devices found and sg kernel module is not loaded.\n"); fflush(stdout); } +#endif } +#ifdef CDROM_DEBUG else { printf("[CDROM] No sg devices found, could not check if sg kernel module is loaded.\n"); fflush(stdout); } +#endif } string_list_free(dir_list); diff --git a/libretro-common/compat/compat_posix_string.c b/libretro-common/compat/compat_posix_string.c index 33a30e57..6a2f07ee 100644 --- a/libretro-common/compat/compat_posix_string.c +++ b/libretro-common/compat/compat_posix_string.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_posix_string.c). diff --git a/libretro-common/compat/compat_snprintf.c b/libretro-common/compat/compat_snprintf.c index b69ad047..5b0a34e0 100644 --- a/libretro-common/compat/compat_snprintf.c +++ b/libretro-common/compat/compat_snprintf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_snprintf.c). diff --git a/libretro-common/compat/compat_strcasestr.c b/libretro-common/compat/compat_strcasestr.c index 54c93a48..4129dab2 100644 --- a/libretro-common/compat/compat_strcasestr.c +++ b/libretro-common/compat/compat_strcasestr.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_strcasestr.c). diff --git a/libretro-common/compat/compat_strl.c b/libretro-common/compat/compat_strl.c index 94cb39b6..31723107 100644 --- a/libretro-common/compat/compat_strl.c +++ b/libretro-common/compat/compat_strl.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (compat_strl.c). diff --git a/libretro-common/compat/fopen_utf8.c b/libretro-common/compat/fopen_utf8.c index 893afb83..ea169615 100644 --- a/libretro-common/compat/fopen_utf8.c +++ b/libretro-common/compat/fopen_utf8.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (fopen_utf8.c). diff --git a/libretro-common/encodings/encoding_utf.c b/libretro-common/encodings/encoding_utf.c index b6ad2f96..2760824d 100644 --- a/libretro-common/encodings/encoding_utf.c +++ b/libretro-common/encodings/encoding_utf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2018 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (encoding_utf.c). @@ -37,6 +37,8 @@ #include #endif +#define UTF8_WALKBYTE(string) (*((*(string))++)) + static unsigned leading_ones(uint8_t c) { unsigned ones = 0; @@ -89,13 +91,14 @@ size_t utf8_conv_utf32(uint32_t *out, size_t out_chars, bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, const uint16_t *in, size_t in_size) { - static uint8_t kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; - size_t out_pos = 0; - size_t in_pos = 0; + size_t out_pos = 0; + size_t in_pos = 0; + static const + uint8_t utf8_limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC }; for (;;) { - unsigned numAdds; + unsigned num_adds; uint32_t value; if (in_pos == in_size) @@ -124,21 +127,21 @@ bool utf16_conv_utf8(uint8_t *out, size_t *out_chars, value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000; } - for (numAdds = 1; numAdds < 5; numAdds++) - if (value < (((uint32_t)1) << (numAdds * 5 + 6))) + for (num_adds = 1; num_adds < 5; num_adds++) + if (value < (((uint32_t)1) << (num_adds * 5 + 6))) break; if (out) - out[out_pos] = (char)(kUtf8Limits[numAdds - 1] - + (value >> (6 * numAdds))); + out[out_pos] = (char)(utf8_limits[num_adds - 1] + + (value >> (6 * num_adds))); out_pos++; do { - numAdds--; + num_adds--; if (out) out[out_pos] = (char)(0x80 - + ((value >> (6 * numAdds)) & 0x3F)); + + ((value >> (6 * num_adds)) & 0x3F)); out_pos++; - }while (numAdds != 0); + }while (num_adds != 0); } *out_chars = out_pos; @@ -166,13 +169,15 @@ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) while (*sb && chars-- > 0) { sb++; - while ((*sb & 0xC0) == 0x80) sb++; + while ((*sb & 0xC0) == 0x80) + sb++; } if ((size_t)(sb - sb_org) > d_len-1 /* NUL */) { sb = sb_org + d_len-1; - while ((*sb & 0xC0) == 0x80) sb--; + while ((*sb & 0xC0) == 0x80) + sb--; } memcpy(d, sb_org, sb-sb_org); @@ -184,14 +189,18 @@ size_t utf8cpy(char *d, size_t d_len, const char *s, size_t chars) const char *utf8skip(const char *str, size_t chars) { const uint8_t *strb = (const uint8_t*)str; + if (!chars) return str; + do { strb++; - while ((*strb & 0xC0)==0x80) strb++; + while ((*strb & 0xC0)==0x80) + strb++; chars--; - } while(chars); + }while (chars); + return (const char*)strb; } @@ -211,24 +220,22 @@ size_t utf8len(const char *string) return ret; } -#define utf8_walkbyte(string) (*((*(string))++)) - /* Does not validate the input, returns garbage if it's not UTF-8. */ uint32_t utf8_walk(const char **string) { - uint8_t first = utf8_walkbyte(string); + uint8_t first = UTF8_WALKBYTE(string); uint32_t ret = 0; if (first < 128) return first; - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); if (first >= 0xE0) { - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); if (first >= 0xF0) { - ret = (ret << 6) | (utf8_walkbyte(string) & 0x3F); + ret = (ret << 6) | (UTF8_WALKBYTE(string) & 0x3F); return ret | (first & 7) << 18; } return ret | (first & 15) << 12; @@ -277,9 +284,7 @@ bool utf16_to_char_string(const uint16_t *in, char *s, size_t len) static char *mb_to_mb_string_alloc(const char *str, enum CodePage cp_in, enum CodePage cp_out) { - char *path_buf = NULL; wchar_t *path_buf_wide = NULL; - int path_buf_len = 0; int path_buf_wide_len = MultiByteToWideChar(cp_in, 0, str, -1, NULL, 0); /* Windows 95 will return 0 from these functions with @@ -292,54 +297,51 @@ static char *mb_to_mb_string_alloc(const char *str, * MultiByteToWideChar also supports CP_UTF7 and CP_UTF8. */ - if (path_buf_wide_len) + if (!path_buf_wide_len) + return strdup(str); + + path_buf_wide = (wchar_t*) + calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)); + + if (path_buf_wide) { - path_buf_wide = (wchar_t*) - calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)); + MultiByteToWideChar(cp_in, 0, + str, -1, path_buf_wide, path_buf_wide_len); - if (path_buf_wide) + if (*path_buf_wide) { - MultiByteToWideChar(cp_in, 0, - str, -1, path_buf_wide, path_buf_wide_len); + int path_buf_len = WideCharToMultiByte(cp_out, 0, + path_buf_wide, -1, NULL, 0, NULL, NULL); - if (*path_buf_wide) + if (path_buf_len) { - path_buf_len = WideCharToMultiByte(cp_out, 0, - path_buf_wide, -1, NULL, 0, NULL, NULL); + char *path_buf = (char*) + calloc(path_buf_len + sizeof(char), sizeof(char)); - if (path_buf_len) + if (path_buf) { - path_buf = (char*) - calloc(path_buf_len + sizeof(char), sizeof(char)); + WideCharToMultiByte(cp_out, 0, + path_buf_wide, -1, path_buf, + path_buf_len, NULL, NULL); - if (path_buf) - { - WideCharToMultiByte(cp_out, 0, - path_buf_wide, -1, path_buf, - path_buf_len, NULL, NULL); - - free(path_buf_wide); + free(path_buf_wide); - if (*path_buf) - return path_buf; + if (*path_buf) + return path_buf; - free(path_buf); - return NULL; - } - } - else - { - free(path_buf_wide); - return strdup(str); + free(path_buf); + return NULL; } } + else + { + free(path_buf_wide); + return strdup(str); + } } - } - else - return strdup(str); - if (path_buf_wide) free(path_buf_wide); + } return NULL; } @@ -379,13 +381,13 @@ char* local_to_utf8_string_alloc(const char *str) wchar_t* utf8_to_utf16_string_alloc(const char *str) { #ifdef _WIN32 - int len = 0; - int out_len = 0; + int len = 0; + int out_len = 0; #else - size_t len = 0; + size_t len = 0; size_t out_len = 0; #endif - wchar_t *buf = NULL; + wchar_t *buf = NULL; if (!str || !*str) return NULL; diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 65b8f5b9..3dd53c98 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2010-2019 The RetroArch team +/* Copyright (C) 2010-2020 The RetroArch team * * --------------------------------------------------------------------------------------- * The following license statement only applies to this file (file_path.c). @@ -32,8 +32,7 @@ #include #include #include -#define VFS_FRONTEND -#include +#include