Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

Commit

Permalink
Redo the Dualshock & Pad code.
Browse files Browse the repository at this point in the history
Sorry James but the bitwise mess was useless in the end
and more confusing.
  • Loading branch information
gameblabla committed Oct 7, 2019
1 parent bf1accd commit 198ebfc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 73 deletions.
62 changes: 32 additions & 30 deletions src/pad.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include "r3000a.h"

unsigned char CurPad = 0, CurCmd = 0;
unsigned char configmode = 0, padmode = 0;
unsigned char padid = 0x41;
unsigned char configmode[2] = {0, 0}, padmode[2] = {0, 0};
unsigned char padid[2] = {0x41, 0x41};

typedef struct tagGlobalData
{
Expand Down Expand Up @@ -85,18 +85,24 @@ unsigned char PAD1_poll(unsigned char value) {
uint32_t size_buf = 8;

if (g.CurByte1 == 0) {
uint64_t n;
uint16_t n;
g.CurByte1++;

n = pad_read(0);

g.CmdLen1 = 8;

// Don't enable Analog/Vibration for a standard pad
if (buf[0] == 0x41) {
if (player_controller[0].id == 0x41) {
CurCmd = CMD_READ_DATA_AND_VIBRATE;
} else {
g.CmdLen1 = 4;
}
else
{
CurCmd = value;
}

g.CmdLen1 = 8;


switch (CurCmd)
{
Expand Down Expand Up @@ -124,27 +130,27 @@ unsigned char PAD1_poll(unsigned char value) {
return 0xF3;

case CMD_CONFIG_MODE:
if (configmode) {
if (configmode[0]) {
memcpy(buf, stdcfg, 8);
return 0xF3;
}
// else FALLTHROUGH
case CMD_READ_DATA_AND_VIBRATE:
default:
if (buf[0] == 0x41)
{
g.CmdLen1 = 4;
size_buf = 4;
}
for(uint32_t i=0;i<size_buf;i=i+2)

buf[2] = n & 0xFF;
buf[3] = n >> 8;
if (padmode[0] == 1)
{
buf[i] = (n >> ((7-i-1) * 8)) & 0xFF;
buf[i+1] = (n >> ((7-i) * 8)) & 0xFF;
buf[4] = player_controller[0].joy_right_ax0;
buf[5] = player_controller[0].joy_right_ax1;
buf[6] = player_controller[0].joy_left_ax0;
buf[7] = player_controller[0].joy_left_ax1;
}
break;
}

return padid;
return padid[0];
}

if (g.CurByte1 >= g.CmdLen1)
Expand All @@ -155,13 +161,13 @@ unsigned char PAD1_poll(unsigned char value) {
switch (CurCmd)
{
case CMD_CONFIG_MODE:
configmode = value;
configmode[0] = value;
break;

case CMD_SET_MODE_AND_LOCK:
padmode = value;
padmode[0] = value;
/* Required to fix games that don't properly support the Dualshock */
padid = value ? 0x73 : 0x41;
padid[0] = value ? 0x73 : 0x41;
break;

case CMD_QUERY_ACT:
Expand All @@ -179,7 +185,6 @@ unsigned char PAD1_poll(unsigned char value) {
break;
}
break;

case CMD_QUERY_MODE:
switch (value) {
case 0: // mode 0 - digital mode
Expand All @@ -200,18 +205,15 @@ unsigned char PAD1_poll(unsigned char value) {
unsigned char PAD2_poll(unsigned char value) {
static uint8_t buf[8] = {0xFF, 0x5A, 0xFF, 0xFF, 0x80, 0x80, 0x80, 0x80};

if (g.CurByte2 == 0) {
uint64_t n;
if (g.CurByte2 == 0)
{
uint16_t n;
g.CurByte2++;

n = pad_read(1);
for(int i=0;i<8;i=i+2)
{
buf[i] = (n >> ((7-i-1) * 8)) & 0xFF;
buf[i+1] = (n >> ((7-i) * 8)) & 0xFF;
}
g.CmdLen2 = 8;
return buf[0];
buf[2] = n & 0xFF;
buf[3] = n >> 8;
g.CmdLen2 = 4;
return 0x41;
}

if (g.CurByte2 >= g.CmdLen2) return 0xFF;
Expand Down
88 changes: 46 additions & 42 deletions src/port/sdl/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,16 +541,17 @@ static struct
{ 0, 0 }
};

static uint64_t pad1 = 0xFF5Affffffffffff;
static uint64_t pad2 = 0x5AFFFFFF80808080;
static uint16_t pad1 = 0xFFFF;
static uint16_t pad2 = 0xFFFF;

static uint16_t pad1_buttons = 0xFFFF;

static unsigned short analog1 = 0,tmp_axis=0;
static uint16_t buttons = 0xFFFF;
static int menu_check = 0;
static int select_count = 0;
uint8_t use_speedup = 0;
static uint16_t id=0x5A41,joy_l = 0x8080,joy_r = 0x8080;
SDL_Joystick * sdl_joy1;
SDL_Joystick * sdl_joy2;
SDL_Joystick * sdl_joy[2];
#define joy_commit_range 2048
enum
{
Expand All @@ -560,12 +561,20 @@ enum
ANALOG_RIGHT = 8
};

struct ps1_controller player_controller[2];

void joy_init(void)
{
sdl_joy1 = SDL_JoystickOpen(0);
sdl_joy2 = SDL_JoystickOpen(1);
sdl_joy[0] = SDL_JoystickOpen(0);
sdl_joy[1] = SDL_JoystickOpen(1);
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_ENABLE);

player_controller[0].id = 0x41;
player_controller[0].joy_left_ax0 = 127;
player_controller[0].joy_left_ax1 = 127;
player_controller[0].joy_right_ax0 = 127;
player_controller[0].joy_right_ax1 = 127;
}

void pad_update(void)
Expand All @@ -577,16 +586,16 @@ void pad_update(void)
switch(Config.Analog_Mode)
{
/* Digital. Required for Ganbare Goemon: Ooedo Daikaiten i believe */
case 0:
id=0x5A41;
default:
player_controller[0].id=0x41;
break;
/* DualAnalog. Some games might misbehave with Dualshock like Descent so this is for those */
case 1:
id=0x5A53;
player_controller[0].id=0x53;
break;
/* DualShock, required for Ape Escape. */
case 2:
id=0x5A73;
player_controller[0].id=0x73;
break;
}

Expand Down Expand Up @@ -632,8 +641,7 @@ void pad_update(void)
analog1 |= ANALOG_LEFT;
}
} else {
tmp_axis = (axisval + 32768) / 256;
joy_l = (joy_l & 0xFF00) | tmp_axis;
player_controller[0].joy_left_ax0 = (axisval + 32768) / 256;
}
break;
case 1: /* Y axis*/
Expand All @@ -642,35 +650,31 @@ void pad_update(void)
analog1 &= ~(ANALOG_UP | ANALOG_DOWN);
if (axisval > joy_commit_range)
{
analog1 |= ANALOG_DOWN;
analog1 |= ANALOG_DOWN;
}
else if (axisval < -joy_commit_range)
{
analog1 |= ANALOG_UP;
analog1 |= ANALOG_UP;
}
} else {
tmp_axis = (axisval + 32768) / 256;
joy_l = (joy_l & 0x00FF) | (tmp_axis << 8);
player_controller[0].joy_left_ax1 = (axisval + 32768) / 256;
}
break;
case 2: /* X axis */
axisval = event.jaxis.value;
tmp_axis = (axisval + 32768) / 256;
joy_r = (joy_r & 0xFF00) | tmp_axis;

break;
axisval = event.jaxis.value;
player_controller[0].joy_right_ax0 = (axisval + 32768) / 256;
break;
case 3: /* Y axis*/
axisval = event.jaxis.value;
tmp_axis = (axisval + 32768) / 256;
joy_r = (joy_r & 0x00FF) | (tmp_axis << 8);
player_controller[0].joy_right_ax1 = (axisval + 32768) / 256;
break;
}
break;
case SDL_JOYBUTTONDOWN:
if(event.jbutton.which == 0) {
buttons |= (1 << DKEY_L3);
pad1_buttons |= (1 << DKEY_L3);
} else if(event.jbutton.which == 1) {
buttons |= (1 << DKEY_R3);
pad1_buttons |= (1 << DKEY_R3);
}
break;
default:
Expand All @@ -683,11 +687,11 @@ void pad_update(void)
{
if (keys[keymap[k].key])
{
buttons &= ~(1 << keymap[k].bit);
pad1_buttons &= ~(1 << keymap[k].bit);
}
else
{
buttons |= (1 << keymap[k].bit);
pad1_buttons |= (1 << keymap[k].bit);
}
k++;
}
Expand Down Expand Up @@ -758,29 +762,29 @@ void pad_update(void)
//
if (Config.AnalogArrow == 1)
{
buttons |= (1 << DKEY_SELECT);
pad1_buttons |= (1 << DKEY_SELECT);
// SELECT+B for psx's SELECT
if (keys[SDLK_ESCAPE] && keys[SDLK_LALT])
{
buttons &= ~(1 << DKEY_SELECT);
buttons |= (1 << DKEY_CROSS);
pad1_buttons &= ~(1 << DKEY_SELECT);
pad1_buttons |= (1 << DKEY_CROSS);
}

if ((buttons & (1 << DKEY_UP)) && (analog1 & ANALOG_UP))
if ((pad1_buttons & (1 << DKEY_UP)) && (analog1 & ANALOG_UP))
{
buttons &= ~(1 << DKEY_UP);
pad1_buttons &= ~(1 << DKEY_UP);
}
if ((buttons & (1 << DKEY_DOWN)) && (analog1 & ANALOG_DOWN))
if ((pad1_buttons & (1 << DKEY_DOWN)) && (analog1 & ANALOG_DOWN))
{
buttons &= ~(1 << DKEY_DOWN);
pad1_buttons &= ~(1 << DKEY_DOWN);
}
if ((buttons & (1 << DKEY_LEFT)) && (analog1 & ANALOG_LEFT))
if ((pad1_buttons & (1 << DKEY_LEFT)) && (analog1 & ANALOG_LEFT))
{
buttons &= ~(1 << DKEY_LEFT);
pad1_buttons &= ~(1 << DKEY_LEFT);
}
if ((buttons & (1 << DKEY_RIGHT)) && (analog1 & ANALOG_RIGHT))
if ((pad1_buttons & (1 << DKEY_RIGHT)) && (analog1 & ANALOG_RIGHT))
{
buttons &= ~(1 << DKEY_RIGHT);
pad1_buttons &= ~(1 << DKEY_RIGHT);
}
}

Expand All @@ -800,7 +804,7 @@ void pad_update(void)
use_speedup = 0;
menu_check = 0;
analog1 = 0;
buttons |= (1 << DKEY_START) | (1 << DKEY_CROSS) | (1 << DKEY_SELECT);
pad1_buttons |= (1 << DKEY_START) | (1 << DKEY_CROSS) | (1 << DKEY_SELECT);
video_clear();
video_flip();
video_clear();
Expand All @@ -812,10 +816,10 @@ void pad_update(void)
}
#endif

pad1 = (uint64_t)id<<48 | (uint64_t)buttons<<32 | (uint32_t) joy_r <<16 | (joy_l);
pad1 = pad1_buttons;
}

uint64_t pad_read(int num)
uint16_t pad_read(int num)
{
return (num == 0 ? pad1 : pad2);
}
Expand Down
12 changes: 11 additions & 1 deletion src/port/sdl/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#define PATH_MAX 2048
#endif

struct ps1_controller
{
uint8_t id;
uint8_t joy_right_ax0;
uint8_t joy_right_ax1;
uint8_t joy_left_ax0;
uint8_t joy_left_ax1;
};

extern struct ps1_controller player_controller[2];

///////////////////////////
// Windows compatibility //
Expand All @@ -34,7 +44,7 @@ static inline int fsync(int f)
unsigned get_ticks(void);
void wait_ticks(unsigned s);
void pad_update(void);
uint64_t pad_read(int num);
uint16_t pad_read(int num);

void video_flip(void);
#ifdef GPU_DFXVIDEO
Expand Down

0 comments on commit 198ebfc

Please sign in to comment.