Skip to content

Commit

Permalink
Added controller handling for new N64 options (MiSTer-devel#838)
Browse files Browse the repository at this point in the history
* Added controller handling for new N64 options

* Move to N64 folder and add per stick ranges

* Improve latency
  • Loading branch information
trbocode authored Oct 15, 2023
1 parent 222d617 commit 78edf5f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
21 changes: 11 additions & 10 deletions input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1199,9 +1199,9 @@ typedef struct
char id[80];
char name[128];
char sysfs[512];
int ss_range;
int max_cardinal;
float max_range;
int ss_range[2];
int max_cardinal[2];
float max_range[2];
} devInput;

static devInput input[NUMDEV] = {};
Expand Down Expand Up @@ -2089,27 +2089,28 @@ static void joy_analog(int dev, int axis, int offset, int stick = 0)
pos[stick][num][axis] = offset;
int x = pos[stick][num][0];
int y = pos[stick][num][1];
if (is_n64() && stick == 0)
if (is_n64())
{
// Update maximum observed cardinal distance
const int abs_x = abs(x);
const int abs_y = abs(y);

if (abs_x > input[dev].max_cardinal) input[dev].max_cardinal = abs_x;
if (abs_y > input[dev].max_cardinal) input[dev].max_cardinal = abs_y;
if (abs_x > input[dev].max_cardinal[stick]) input[dev].max_cardinal[stick] = abs_x;
if (abs_y > input[dev].max_cardinal[stick]) input[dev].max_cardinal[stick] = abs_y;

// Update maximum observed diag
// Use sum of squares and only calc sqrt() when necessary
const int ss_range_curr = x*x + y*y;
// compare to max ss_range and update if larger
if ((ss_range_curr > input[dev].ss_range) & (abs(abs_x - abs_y) <= 3))
if ((ss_range_curr > input[dev].ss_range[stick]) & (abs(abs_x - abs_y) <= 3))
{
input[dev].ss_range = ss_range_curr;
input[dev].max_range = sqrt(ss_range_curr);
input[dev].ss_range[stick] = ss_range_curr;
input[dev].max_range[stick] = sqrt(ss_range_curr);
}

// emulate n64 joystick range and shape for regular -127-+127 controllers
n64_joy_emu(x, y, &x, &y, input[dev].max_cardinal, input[dev].max_range);
n64_joy_emu(x, y, &x, &y, input[dev].max_cardinal[stick], input[dev].max_range[stick]);
stick_swap(num,stick,&num,&stick);
}
if(stick) user_io_r_analog_joystick(num, (char)x, (char)y);
else user_io_l_analog_joystick(num, (char)x, (char)y);
Expand Down
45 changes: 44 additions & 1 deletion support/n64/n64_joy_emu.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include "../../user_io.h"

#define N64_MAX_DIAG 69
#define N64_MAX_DIST sqrt(N64_MAX_DIAG * N64_MAX_DIAG * 2)
#define N64_MAX_CARDINAL 85
#define OUTER_DEADZONE 2.0f
#define WEDGE_BOUNDARY (N64_MAX_CARDINAL - 69.0f) / 69.0f



void stick_swap(int num, int stick, int *num2, int *stick2)
{
int get=user_io_status_get("TV",1);
int p2=get%2;
int p3=get&2;
int swap=get&4;
if(swap) //reverse sticks
{
stick=1-stick;
}
if(p3) //p1 right stick -> p3
{
if (stick && num<2)
{
num+=2;
stick=0;
}
else if(!stick && 2<num && num<5) //swap sticks to minimize conflict
{
num-=2;
stick=1;
}
}
if(p2) //p1 right stick -> p2
{
if (stick && ( (num==0) | (num==2)))
{
num++;
stick=0;
}
else if(!stick && num%2==1)
{
num--;
stick=1;
}
}
*num2=num;
*stick2=stick;
}

void n64_joy_emu(int x, int y, int* x2, int* y2, int max_cardinal, float max_range)
{
// Move to top right quadrant to standardize solutions
Expand Down Expand Up @@ -66,4 +109,4 @@ void n64_joy_emu(int x, int y, int* x2, int* y2, int max_cardinal, float max_ran
#undef N64_MAX_DIST
#undef N64_MAX_CARDINAL
#undef OUTER_DEADZONE
#undef WEDGE_BOUNDARY
#undef WEDGE_BOUNDARY
1 change: 1 addition & 0 deletions support/n64/n64_joy_emu.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
void stick_swap(int num, int stick, int *num2, int *stick2);
void n64_joy_emu(int x, int y, int* x2, int* y2, int max_cardinal, float max_range);

0 comments on commit 78edf5f

Please sign in to comment.