1
/* Mednafen - Multi-system Emulator
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "../frontio.h"
20
#include "dualshock.h"
24
If we ever call Update() more than once per video frame(IE 50/60Hz), we'll need to add debounce logic to the analog mode button evaluation code.
29
Both DA and DS style rumblings work in both analog and digital modes.
31
Regarding getting Dual Shock style rumble working, Sony is evil and/or mean. The owl tells me to burn Sony with boiling oil.
33
To enable Dual Shock-style rumble, the game has to at least enter MAD MUNCHKINS MODE with command 0x43, and send the appropriate data(not the actual rumble type data per-se)
36
DualAnalog-style rumble support seems to be borked until power loss if MAD MUNCHKINS MODE is even entered once...investigate further.
38
Command 0x44 in MAD MUNCHKINS MODE can turn on/off analog mode(and the light with it).
40
Command 0x42 in MAD MUNCHKINS MODE will return the analog mode style gamepad data, even when analog mode is off. In combination with command 0x44, this could hypothetically
41
be used for using the light in the gamepad as some kind of game mechanic).
43
Dual Analog-style rumble notes(some of which may apply to DS too):
44
Rumble appears to stop if you hold DTR active(TODO: for how long? instant?). (TODO: investigate if it's still stopped even if a memory card device number is sent. It may be, since rumble may
45
cause excessive current draw in combination with memory card access)
47
Rumble will work even if you interrupt the communication process after you've sent the rumble data(via command 0x42).
48
Though if you interrupt it when you've only sent partial rumble data, dragons will eat you and I don't know(seems to have timing-dependent or random effects or something;
49
based on VERY ROUGH testing).
52
namespace MDFN_IEN_PSX
55
class InputDevice_DualShock : public InputDevice
59
InputDevice_DualShock(const std::string &arg_name);
60
virtual ~InputDevice_DualShock();
62
virtual void Power(void);
63
virtual void Update(const pscpu_timestamp_t timestamp);
64
virtual void ResetTS(void);
65
virtual void UpdateInput(const void *data);
67
virtual void SetAMCT(bool enabled);
71
virtual void SetDTR(bool new_dtr);
72
virtual bool GetDSR(void);
73
virtual bool Clock(bool TxD, int32 &dsr_pulse_delay);
77
void CheckManualAnaModeChange(void);
81
bool cur_ana_button_state;
82
bool prev_ana_button_state;
83
int64 combo_anatoggle_counter;
86
bool da_rumble_compat;
89
bool analog_mode_locked;
92
uint8 rumble_magic[6];
94
uint8 rumble_param[2];
103
uint8 receive_buffer;
107
uint8 transmit_buffer[8];
109
uint32 transmit_count;
117
pscpu_timestamp_t lastts;
124
InputDevice_DualShock::InputDevice_DualShock(const std::string &name)
128
am_prev_info = analog_mode;
129
aml_prev_info = analog_mode_locked;
130
amct_enabled = false;
133
InputDevice_DualShock::~InputDevice_DualShock()
138
void InputDevice_DualShock::Update(const pscpu_timestamp_t timestamp)
143
void InputDevice_DualShock::ResetTS(void)
145
//printf("%lld\n", combo_anatoggle_counter);
146
if(combo_anatoggle_counter >= 0)
147
combo_anatoggle_counter += lastts;
151
void InputDevice_DualShock::SetAMCT(bool enabled)
153
amct_enabled = enabled;
157
// This simulates the behavior of the actual DualShock(analog toggle button evaluation is suspended while DTR is active).
158
// Call in Update(), and whenever dtr goes inactive in the port access code.
159
void InputDevice_DualShock::CheckManualAnaModeChange(void)
163
bool need_mode_toggle = false;
167
if(buttons[0] == 0x09 && buttons[1] == 0x0f)
169
if(combo_anatoggle_counter == -1)
170
combo_anatoggle_counter = 0;
171
else if(combo_anatoggle_counter >= (44100 * 768))
173
need_mode_toggle = true;
174
combo_anatoggle_counter = -2;
178
combo_anatoggle_counter = -1;
182
combo_anatoggle_counter = -1;
183
if(cur_ana_button_state && (cur_ana_button_state != prev_ana_button_state))
185
need_mode_toggle = true;
191
if(analog_mode_locked)
193
MDFN_DispMessage(_("%s: Analog mode is locked %s."), gp_name.c_str(), analog_mode ? _("on") : _("off"));
196
analog_mode = !analog_mode;
199
prev_ana_button_state = cur_ana_button_state; // Don't move this outside of the if(!dtr) block!
203
void InputDevice_DualShock::Power(void)
205
combo_anatoggle_counter = -2;
212
buttons[0] = buttons[1] = 0;
222
memset(transmit_buffer, 0, sizeof(transmit_buffer));
228
analog_mode_locked = false;
230
mad_munchkins = false;
231
memset(rumble_magic, 0, sizeof(rumble_magic));
232
memset(rumble_param, 0, sizeof(rumble_param));
234
da_rumble_compat = true;
236
prev_ana_button_state = false;
239
void InputDevice_DualShock::UpdateInput(const void *data)
241
uint8 *d8 = (uint8 *)data;
245
cur_ana_button_state = d8[2] & 0x01;
247
for(int stick = 0; stick < 2; stick++)
249
for(int axis = 0; axis < 2; axis++)
253
tmp = 32768 + MDFN_de32lsb((const uint8 *)data + stick * 16 + axis * 8 + 4) - ((int32)MDFN_de32lsb((const uint8 *)data + stick * 16 + axis * 8 + 8) * 32768 / 32767);
256
axes[stick][axis] = tmp;
260
if(da_rumble_compat == false)
262
uint8 sneaky_weaky = 0;
264
if(rumble_param[0] == 0x01)
267
MDFN_en32lsb(&d8[4 + 32 + 0], (sneaky_weaky << 0) | (rumble_param[1] << 8));
271
uint8 sneaky_weaky = 0;
273
if(((rumble_param[0] & 0xC0) == 0x40) && ((rumble_param[1] & 0x01) == 0x01))
276
MDFN_en32lsb(&d8[4 + 32 + 0], sneaky_weaky << 0);
279
//printf("%d %d %d %d\n", axes[0][0], axes[0][1], axes[1][0], axes[1][1]);
284
CheckManualAnaModeChange();
286
if(am_prev_info != analog_mode || aml_prev_info != analog_mode_locked)
288
MDFN_DispMessage(_("%s: Analog mode is %s(%s)."), gp_name.c_str(), analog_mode ? _("on") : _("off"), analog_mode_locked ? _("locked") : _("unlocked"));
290
am_prev_info = analog_mode;
291
aml_prev_info = analog_mode_locked;
295
void InputDevice_DualShock::SetDTR(bool new_dtr)
297
const bool old_dtr = dtr;
298
dtr = new_dtr; // Set it to new state before we call CheckManualAnaModeChange().
307
else if(old_dtr && !dtr)
309
CheckManualAnaModeChange();
310
//if(bitpos || transmit_count)
311
// printf("[PAD] Abort communication!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
315
bool InputDevice_DualShock::GetDSR(void)
320
if(!bitpos && transmit_count)
326
bool InputDevice_DualShock::Clock(bool TxD, int32 &dsr_pulse_delay)
336
ret = (transmit_buffer[transmit_pos] >> bitpos) & 1;
338
receive_buffer &= ~(1 << bitpos);
339
receive_buffer |= TxD << bitpos;
340
bitpos = (bitpos + 1) & 0x7;
344
//if(command == 0x44)
345
//if(mad_munchkins || command == 0x43)
346
// fprintf(stderr, "[PAD] Receive: %02x -- command=%02x, command_phase=%d, transmit_pos=%d\n", receive_buffer, command, command_phase, transmit_pos);
354
switch(command_phase)
357
if(receive_buffer != 0x01)
363
transmit_buffer[0] = 0xF3;
370
transmit_buffer[0] = analog_mode ? 0x73 : 0x41;
379
command = receive_buffer;
382
transmit_buffer[0] = 0x5A;
384
//fprintf(stderr, "Gamepad command: 0x%02x\n", command);
385
//if(command != 0x42 && command != 0x43)
386
// fprintf(stderr, "Gamepad unhandled command: 0x%02x\n", command);
390
transmit_buffer[0] = 0x5A;
393
command_phase = (command << 8) | 0x00;
395
else if(command == 0x43)
400
transmit_buffer[1] = 0xFF ^ buttons[0];
401
transmit_buffer[2] = 0xFF ^ buttons[1];
402
transmit_buffer[3] = axes[0][0];
403
transmit_buffer[4] = axes[0][1];
404
transmit_buffer[5] = axes[1][0];
405
transmit_buffer[6] = axes[1][1];
410
transmit_buffer[1] = 0xFF ^ buttons[0];
411
transmit_buffer[2] = 0xFF ^ buttons[1];
418
transmit_buffer[1] = 0;
419
transmit_buffer[2] = 0;
427
if(command == 0x43 && transmit_pos == 2 && (receive_buffer == 0x01))
429
//fprintf(stderr, "Mad Munchkins mode entered!\n");
430
mad_munchkins = true;
436
da_rumble_compat = false;
444
command = receive_buffer;
446
//fprintf(stderr, "Mad Munchkins DualShock command: 0x%02x\n", command);
448
if(command >= 0x40 && command <= 0x4F)
450
transmit_buffer[0] = 0x5A;
453
command_phase = (command << 8) | 0x00;
462
/************************/
463
/* MMMode 1, Command 0x40 */
464
/************************/
466
if(receive_buffer == 0x00)
468
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
476
transmit_buffer[0] = 0x00;
477
transmit_buffer[1] = 0x00;
478
transmit_buffer[2] = 0x00;
479
transmit_buffer[3] = 0x00;
480
transmit_buffer[4] = 0x00;
487
/************************/
488
/* MMMode 1, Command 0x41 */
489
/************************/
491
if(receive_buffer == 0x00)
493
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
501
transmit_buffer[0] = 0x00;
502
transmit_buffer[1] = 0x00;
503
transmit_buffer[2] = 0x00;
504
transmit_buffer[3] = 0x00;
505
transmit_buffer[4] = 0x00;
511
/**************************/
512
/* MMMode 0&1, Command 0x42 */
513
/**************************/
516
if(analog_mode || mad_munchkins)
518
transmit_buffer[0] = 0xFF ^ buttons[0];
519
transmit_buffer[1] = 0xFF ^ buttons[1];
520
transmit_buffer[2] = axes[0][0];
521
transmit_buffer[3] = axes[0][1];
522
transmit_buffer[4] = axes[1][0];
523
transmit_buffer[5] = axes[1][1];
528
transmit_buffer[0] = 0xFF ^ buttons[0];
529
transmit_buffer[1] = 0xFF ^ buttons[1];
535
case 0x4201: // Weak(in DS mode)
536
if(da_rumble_compat || (rumble_magic[0] == 0x00 && rumble_magic[2] != 0x00 && rumble_magic[3] != 0x00 && rumble_magic[4] != 0x00 && rumble_magic[5] != 0x00))
537
rumble_param[0] = receive_buffer;
541
case 0x4202: // Strong(in DS mode)
542
if(da_rumble_compat || rumble_magic[1] == 0x01)
543
rumble_param[1] = receive_buffer;
544
command_phase++; // Nowhere here we come!
547
/************************/
548
/* MMMode 1, Command 0x43 */
549
/************************/
551
if(receive_buffer == 0x00)
553
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
561
if(receive_buffer == 0x00)
563
//fprintf(stderr, "Mad Munchkins mode left!\n");
564
mad_munchkins = false;
566
transmit_buffer[0] = 0x00;
567
transmit_buffer[1] = 0x00;
568
transmit_buffer[2] = 0x00;
569
transmit_buffer[3] = 0x00;
570
transmit_buffer[4] = 0x00;
576
/************************/
577
/* MMMode 1, Command 0x44 */
578
/************************/
580
if(receive_buffer == 0x00)
582
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
590
transmit_buffer[0] = 0x00;
591
transmit_buffer[1] = 0x00;
592
transmit_buffer[2] = 0x00;
593
transmit_buffer[3] = 0x00;
594
transmit_buffer[4] = 0x00;
599
// Ignores locking state.
600
switch(receive_buffer)
604
//fprintf(stderr, "Analog mode disabled\n");
609
//fprintf(stderr, "Analog mode enabled\n");
615
switch(receive_buffer)
618
analog_mode_locked = false;
622
analog_mode_locked = true;
628
/************************/
629
/* MMMode 1, Command 0x45 */
630
/************************/
632
if(receive_buffer == 0x00)
634
transmit_buffer[0] = 0x01; /**/ transmit_pos = 0; transmit_count = 1; /**/
642
transmit_buffer[0] = 0x02;
643
transmit_buffer[1] = analog_mode ? 0x01 : 0x00;
644
transmit_buffer[2] = 0x02;
645
transmit_buffer[3] = 0x01;
646
transmit_buffer[4] = 0x00;
653
/************************/
654
/* MMMode 1, Command 0x46 */
655
/************************/
657
if(receive_buffer == 0x00)
659
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
667
if(receive_buffer == 0x00)
669
transmit_buffer[0] = 0x00;
670
transmit_buffer[1] = 0x01;
671
transmit_buffer[2] = 0x02;
672
transmit_buffer[3] = 0x00;
673
transmit_buffer[4] = 0x0A;
675
else if(receive_buffer == 0x01)
677
transmit_buffer[0] = 0x00;
678
transmit_buffer[1] = 0x01;
679
transmit_buffer[2] = 0x01;
680
transmit_buffer[3] = 0x01;
681
transmit_buffer[4] = 0x14;
685
transmit_buffer[0] = 0x00;
686
transmit_buffer[1] = 0x00;
687
transmit_buffer[2] = 0x00;
688
transmit_buffer[3] = 0x00;
689
transmit_buffer[4] = 0x00;
696
/************************/
697
/* MMMode 1, Command 0x47 */
698
/************************/
700
if(receive_buffer == 0x00)
702
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
710
if(receive_buffer == 0x00)
712
transmit_buffer[0] = 0x00;
713
transmit_buffer[1] = 0x02;
714
transmit_buffer[2] = 0x00;
715
transmit_buffer[3] = 0x01;
716
transmit_buffer[4] = 0x00;
720
transmit_buffer[0] = 0x00;
721
transmit_buffer[1] = 0x00;
722
transmit_buffer[2] = 0x00;
723
transmit_buffer[3] = 0x00;
724
transmit_buffer[4] = 0x00;
731
/************************/
732
/* MMMode 1, Command 0x48 */
733
/************************/
735
if(receive_buffer == 0x00)
737
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
745
if(receive_buffer == 0x00)
747
transmit_buffer[0] = 0x00;
748
transmit_buffer[1] = 0x00;
749
transmit_buffer[2] = 0x00;
750
transmit_buffer[3] = 0x01;
751
transmit_buffer[4] = rumble_param[0];
753
else if(receive_buffer == 0x01)
755
transmit_buffer[0] = 0x00;
756
transmit_buffer[1] = 0x00;
757
transmit_buffer[2] = 0x00;
758
transmit_buffer[3] = 0x01;
759
transmit_buffer[4] = rumble_param[1];
763
transmit_buffer[0] = 0x00;
764
transmit_buffer[1] = 0x00;
765
transmit_buffer[2] = 0x00;
766
transmit_buffer[3] = 0x00;
767
transmit_buffer[4] = 0x00;
774
/************************/
775
/* MMMode 1, Command 0x49 */
776
/************************/
778
if(receive_buffer == 0x00)
780
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
788
transmit_buffer[0] = 0x00;
789
transmit_buffer[1] = 0x00;
790
transmit_buffer[2] = 0x00;
791
transmit_buffer[3] = 0x00;
792
transmit_buffer[4] = 0x00;
798
/************************/
799
/* MMMode 1, Command 0x4A */
800
/************************/
802
if(receive_buffer == 0x00)
804
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
812
transmit_buffer[0] = 0x00;
813
transmit_buffer[1] = 0x00;
814
transmit_buffer[2] = 0x00;
815
transmit_buffer[3] = 0x00;
816
transmit_buffer[4] = 0x00;
822
/************************/
823
/* MMMode 1, Command 0x4B */
824
/************************/
826
if(receive_buffer == 0x00)
828
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
836
transmit_buffer[0] = 0x00;
837
transmit_buffer[1] = 0x00;
838
transmit_buffer[2] = 0x00;
839
transmit_buffer[3] = 0x00;
840
transmit_buffer[4] = 0x00;
846
/************************/
847
/* MMMode 1, Command 0x4C */
848
/************************/
850
if(receive_buffer == 0x00)
852
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
860
if(receive_buffer == 0x00)
862
transmit_buffer[0] = 0x00;
863
transmit_buffer[1] = 0x00;
864
transmit_buffer[2] = 0x04;
865
transmit_buffer[3] = 0x00;
866
transmit_buffer[4] = 0x00;
868
else if(receive_buffer == 0x01)
870
transmit_buffer[0] = 0x00;
871
transmit_buffer[1] = 0x00;
872
transmit_buffer[2] = 0x07;
873
transmit_buffer[3] = 0x00;
874
transmit_buffer[4] = 0x00;
878
transmit_buffer[0] = 0x00;
879
transmit_buffer[1] = 0x00;
880
transmit_buffer[2] = 0x00;
881
transmit_buffer[3] = 0x00;
882
transmit_buffer[4] = 0x00;
890
/************************/
891
/* MMMode 1, Command 0x4D */
892
/************************/
894
if(receive_buffer == 0x00)
896
transmit_buffer[0] = rumble_magic[0]; /**/ transmit_pos = 0; transmit_count = 1; /**/
910
unsigned index = command_phase - 0x4D01;
914
transmit_buffer[0] = rumble_magic[1 + index];
922
rumble_magic[index] = receive_buffer;
926
/************************/
927
/* MMMode 1, Command 0x4E */
928
/************************/
930
if(receive_buffer == 0x00)
932
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
940
transmit_buffer[0] = 0x00;
941
transmit_buffer[1] = 0x00;
942
transmit_buffer[2] = 0x00;
943
transmit_buffer[3] = 0x00;
944
transmit_buffer[4] = 0x00;
951
/************************/
952
/* MMMode 1, Command 0x4F */
953
/************************/
955
if(receive_buffer == 0x00)
957
transmit_buffer[0] = 0; /**/ transmit_pos = 0; transmit_count = 1; /**/
965
transmit_buffer[0] = 0x00;
966
transmit_buffer[1] = 0x00;
967
transmit_buffer[2] = 0x00;
968
transmit_buffer[3] = 0x00;
969
transmit_buffer[4] = 0x00;
977
if(!bitpos && transmit_count)
978
dsr_pulse_delay = 0x40; //0x100;
983
InputDevice *Device_DualShock_Create(const std::string &name)
985
return new InputDevice_DualShock(name);
989
InputDeviceInputInfoStruct Device_DualShock_IDII[26] =
991
{ "select", "SELECT", 4, IDIT_BUTTON, NULL },
992
{ "l3", "Left Stick, Button(L3)", 18, IDIT_BUTTON, NULL },
993
{ "r3", "Right stick, Button(R3)", 23, IDIT_BUTTON, NULL },
994
{ "start", "START", 5, IDIT_BUTTON, NULL },
995
{ "up", "D-Pad UP ↑", 0, IDIT_BUTTON, "down" },
996
{ "right", "D-Pad RIGHT →", 3, IDIT_BUTTON, "left" },
997
{ "down", "D-Pad DOWN ↓", 1, IDIT_BUTTON, "up" },
998
{ "left", "D-Pad LEFT ←", 2, IDIT_BUTTON, "right" },
1000
{ "l2", "L2 (rear left shoulder)", 11, IDIT_BUTTON, NULL },
1001
{ "r2", "R2 (rear right shoulder)", 13, IDIT_BUTTON, NULL },
1002
{ "l1", "L1 (front left shoulder)", 10, IDIT_BUTTON, NULL },
1003
{ "r1", "R1 (front right shoulder)", 12, IDIT_BUTTON, NULL },
1005
{ "triangle", "△ (upper)", 6, IDIT_BUTTON_CAN_RAPID, NULL },
1006
{ "circle", "○ (right)", 9, IDIT_BUTTON_CAN_RAPID, NULL },
1007
{ "cross", "x (lower)", 7, IDIT_BUTTON_CAN_RAPID, NULL },
1008
{ "square", "□ (left)", 8, IDIT_BUTTON_CAN_RAPID, NULL },
1010
{ "analog", "Analog(mode toggle)", 24, IDIT_BUTTON, NULL },
1012
{ "rstick_right", "Right Stick RIGHT →", 22, IDIT_BUTTON_ANALOG },
1013
{ "rstick_left", "Right Stick LEFT ←", 21, IDIT_BUTTON_ANALOG },
1014
{ "rstick_down", "Right Stick DOWN ↓", 20, IDIT_BUTTON_ANALOG },
1015
{ "rstick_up", "Right Stick UP ↑", 19, IDIT_BUTTON_ANALOG },
1017
{ "lstick_right", "Left Stick RIGHT →", 17, IDIT_BUTTON_ANALOG },
1018
{ "lstick_left", "Left Stick LEFT ←", 16, IDIT_BUTTON_ANALOG },
1019
{ "lstick_down", "Left Stick DOWN ↓", 15, IDIT_BUTTON_ANALOG },
1020
{ "lstick_up", "Left Stick UP ↑", 14, IDIT_BUTTON_ANALOG },
1022
{ "rumble", "RUMBLE MONSTER RUMBA", 100, IDIT_RUMBLE },