From 42b5818564e2b4cba4eaa1c23eb735897422a24a Mon Sep 17 00:00:00 2001 From: Duckey77 Date: Sun, 27 May 2018 21:30:31 -0700 Subject: [PATCH 1/5] Analog support int Mednafen 1.21.x for PSX and Saturn --- MednafenGameCore.mm | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/MednafenGameCore.mm b/MednafenGameCore.mm index a1a7bb5..29461ae 100644 --- a/MednafenGameCore.mm +++ b/MednafenGameCore.mm @@ -3287,9 +3287,9 @@ - (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError const int NGPMap[] = { 0, 1, 2, 3, 4, 5, 6 }; const int PCEMap[] = { 4, 6, 7, 5, 0, 1, 8, 9, 10, 11, 3, 2, 12 }; const int PCFXMap[] = { 8, 10, 11, 9, 0, 1, 2, 3, 4, 5, 7, 6 }; -const int PSXMap[] = { 4, 6, 7, 5, 12, 13, 14, 15, 10, 8, 1, 11, 9, 2, 3, 0, 16, 24, 23, 22, 21, 20, 19, 18, 17 }; +const int PSXMap[] = { 4, 6, 7, 5, 12, 13, 14, 15, 10, 8, 1, 11, 9, 2, 3, 0, 16, 23, 23, 21, 21, 19, 19, 17, 17 }; const int SSMap[] = { 4, 5, 6, 7, 10, 8, 9, 2, 1, 0, 15, 3, 11 }; -const int SS3DMap[] = { 0, 1, 2, 3, 6, 4, 5, 10, 9, 8, 18, 17, 7, 12, 15, 16, 13, 14, 18, 17}; +const int SS3DMap[] = { 0, 1, 2, 3, 6, 4, 5, 10, 9, 8, 18, 17, 7, 12, 15, 15, 13, 13, 17, 17}; const int VBMap[] = { 9, 8, 7, 6, 4, 13, 12, 5, 3, 2, 0, 1, 10, 11 }; const int WSMap[] = { 0, 2, 3, 1, 4, 6, 7, 5, 9, 10, 8, 11 }; @@ -3418,22 +3418,27 @@ - (oneway void)didMovePSXJoystickDirection:(OEPSXButton)button withValue:(CGFloa // Fix the analog circle-to-square axis range conversion by scaling between a value of 1.00 and 1.50 // We cannot use MDFNI_SetSetting("psx.input.port1.dualshock.axis_scale", "1.33") directly. // Background: https://mednafen.github.io/documentation/psx.html#Section_analog_range - value *= 32767; // de-normalize - double scaledValue = MIN(floor(0.5 + value * 1.33), 32767); // 30712 / cos(2*pi/8) / 32767 = 1.33 + value *= 32767 ; // de-normalize + double scaledValue = MIN(floor(0.5 + value * 1.33), 32767); // 30712 / cos(2*pi/8) / 32767 = 1.33 + + if (button == OEPSXLeftAnalogLeft || button == OEPSXLeftAnalogUp || button == OEPSXRightAnalogLeft || button == OEPSXRightAnalogUp) + scaledValue *= -1; + int analogNumber = PSXMap[button] - 17; uint8_t *buf = (uint8_t *)_inputBuffer[player-1]; - MDFN_en16lsb(&buf[3 + analogNumber * 2], scaledValue); - MDFN_en16lsb(&buf[3 + (analogNumber ^ 1) * 2], 0); + MDFN_en16lsb(&buf[3 + analogNumber], scaledValue + 32767); } - (oneway void)didMoveSaturnJoystickDirection:(OESaturnButton)button withValue:(CGFloat)value forPlayer:(NSUInteger)player { int analogNumber = SS3DMap[button] - 13; + if (button == OESaturnLeftAnalogLeft || button == OESaturnLeftAnalogUp || button == OESaturnAnalogL) + value *= -1; + uint8_t *buf = (uint8_t *)_inputBuffer[player-1]; - MDFN_en16lsb(&buf[2 + analogNumber * 2], 32767 * value); - MDFN_en16lsb(&buf[2 + (analogNumber ^ 1) * 2], 0); + MDFN_en16lsb(&buf[2 + analogNumber], 32767 * value + 32767); } - (void)changeDisplayMode From 07592856762872e0f799cc5ad1a991d518b72257 Mon Sep 17 00:00:00 2001 From: Duckey77 Date: Mon, 28 May 2018 10:53:13 -0700 Subject: [PATCH 2/5] Center the analog inputs on creation for the new input api --- MednafenGameCore.mm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/MednafenGameCore.mm b/MednafenGameCore.mm index 29461ae..9cd1e06 100644 --- a/MednafenGameCore.mm +++ b/MednafenGameCore.mm @@ -3081,7 +3081,16 @@ - (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error } for(unsigned i = 0; i < _multiTapPlayerCount; i++) + { game->SetInput(i, "dualshock", (uint8_t *)_inputBuffer[i]); + + //Center the analog inputs + uint8_t *buf = (uint8_t *)_inputBuffer[i]; + MDFN_en16lsb(&buf[3], 32767); + MDFN_en16lsb(&buf[5], 32767); + MDFN_en16lsb(&buf[7], 32767); + MDFN_en16lsb(&buf[9], 32767); + } } else if ([_mednafenCoreModule isEqualToString:@"ss"]) { @@ -3110,7 +3119,15 @@ - (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error //_inputBuffer[i][0] |= 1 << SS3DMap[OESaturnButtonAnalogMode]; //} else + { game->SetInput(i, "gamepad", (uint8_t *)_inputBuffer[i]); + + //Center the analog inputs + uint8_t *buf = (uint8_t *)_inputBuffer[i]; + MDFN_en16lsb(&buf[2], 32767); + MDFN_en16lsb(&buf[4], 32767); + MDFN_en16lsb(&buf[5], 32767); + } } game->SetInput(12, "builtin", (uint8_t *)_inputBuffer[12]); // reset button status From c6d6bee54de739d33edead1055d81dcdc694e8e1 Mon Sep 17 00:00:00 2001 From: Duckey77 Date: Mon, 28 May 2018 11:55:42 -0700 Subject: [PATCH 3/5] Fixed typo in Saturn analog centering --- MednafenGameCore.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MednafenGameCore.mm b/MednafenGameCore.mm index 9cd1e06..f52bff0 100644 --- a/MednafenGameCore.mm +++ b/MednafenGameCore.mm @@ -3126,7 +3126,7 @@ - (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error uint8_t *buf = (uint8_t *)_inputBuffer[i]; MDFN_en16lsb(&buf[2], 32767); MDFN_en16lsb(&buf[4], 32767); - MDFN_en16lsb(&buf[5], 32767); + MDFN_en16lsb(&buf[6], 32767); } } From 22d884d35ce6c78c07ba60674362aa20eeb1154a Mon Sep 17 00:00:00 2001 From: Duckey77 Date: Sun, 10 Jun 2018 22:39:30 -0700 Subject: [PATCH 4/5] Fix the button mapping and Analog input for Saturn Emulation --- MednafenGameCore.mm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/MednafenGameCore.mm b/MednafenGameCore.mm index f52bff0..0571dec 100644 --- a/MednafenGameCore.mm +++ b/MednafenGameCore.mm @@ -3306,7 +3306,7 @@ - (BOOL)deserializeState:(NSData *)state withError:(NSError **)outError const int PCFXMap[] = { 8, 10, 11, 9, 0, 1, 2, 3, 4, 5, 7, 6 }; const int PSXMap[] = { 4, 6, 7, 5, 12, 13, 14, 15, 10, 8, 1, 11, 9, 2, 3, 0, 16, 23, 23, 21, 21, 19, 19, 17, 17 }; const int SSMap[] = { 4, 5, 6, 7, 10, 8, 9, 2, 1, 0, 15, 3, 11 }; -const int SS3DMap[] = { 0, 1, 2, 3, 6, 4, 5, 10, 9, 8, 18, 17, 7, 12, 15, 15, 13, 13, 17, 17}; +const int SS3DMap[] = { 0, 1, 2, 3, 6, 4, 5, 10, 9, 8, 18, 20, 7, 12, 15, 15, 13, 13, 18, 20}; const int VBMap[] = { 9, 8, 7, 6, 4, 13, 12, 5, 3, 2, 0, 1, 10, 11 }; const int WSMap[] = { 0, 2, 3, 1, 4, 6, 7, 5, 9, 10, 8, 11 }; @@ -3449,13 +3449,20 @@ - (oneway void)didMovePSXJoystickDirection:(OEPSXButton)button withValue:(CGFloa - (oneway void)didMoveSaturnJoystickDirection:(OESaturnButton)button withValue:(CGFloat)value forPlayer:(NSUInteger)player { + value *= 32767 ; // de-normalize + int analogNumber = SS3DMap[button] - 13; - - if (button == OESaturnLeftAnalogLeft || button == OESaturnLeftAnalogUp || button == OESaturnAnalogL) + + if (button == OESaturnLeftAnalogLeft || button == OESaturnLeftAnalogUp) value *= -1; + if (!(button == OESaturnAnalogL || button == OESaturnAnalogR)) { + //Adjust all axis but Trigger buttons + value += 32767; + } + uint8_t *buf = (uint8_t *)_inputBuffer[player-1]; - MDFN_en16lsb(&buf[2 + analogNumber], 32767 * value + 32767); + MDFN_en16lsb(&buf[2 + analogNumber], value); } - (void)changeDisplayMode From 7134736a08dc8d1f44120a7893d51b4dcc11b01b Mon Sep 17 00:00:00 2001 From: Duckey77 Date: Sun, 10 Jun 2018 22:56:56 -0700 Subject: [PATCH 5/5] Put the Center analog Axis input in the correct place --- MednafenGameCore.mm | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/MednafenGameCore.mm b/MednafenGameCore.mm index 0571dec..de9ffa3 100644 --- a/MednafenGameCore.mm +++ b/MednafenGameCore.mm @@ -3112,21 +3112,20 @@ - (BOOL)loadFileAtPath:(NSString *)path error:(NSError **)error for(unsigned i = 0; i < _multiTapPlayerCount; i++) { if(_isSS3DControlPadSupportedGame) - //{ + { game->SetInput(i, "3dpad", (uint8_t *)_inputBuffer[i]); // Toggle default position of analog mode switch to Analog(○) // "Analog mode is not compatible with all games. For some compatible games, analog mode reportedly must be enabled before the game boots up for the game to recognize it properly." //_inputBuffer[i][0] |= 1 << SS3DMap[OESaturnButtonAnalogMode]; - //} - else - { - game->SetInput(i, "gamepad", (uint8_t *)_inputBuffer[i]); - - //Center the analog inputs + + //Center the analog axis inputs uint8_t *buf = (uint8_t *)_inputBuffer[i]; MDFN_en16lsb(&buf[2], 32767); MDFN_en16lsb(&buf[4], 32767); - MDFN_en16lsb(&buf[6], 32767); + } + else + { + game->SetInput(i, "gamepad", (uint8_t *)_inputBuffer[i]); } }