2019-03-05 20:34:43 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <xinput.h>
|
|
|
|
|
2019-04-18 22:55:00 +02:00
|
|
|
#include <math.h>
|
2019-03-05 20:34:43 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "amex/jvs.h"
|
|
|
|
|
|
|
|
#include "board/io3.h"
|
|
|
|
|
|
|
|
#include "idzhook/jvs.h"
|
|
|
|
|
|
|
|
#include "jvs/jvs-bus.h"
|
|
|
|
|
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
static void idz_jvs_read_analogs(
|
|
|
|
void *ctx,
|
|
|
|
uint16_t *analogs,
|
|
|
|
uint8_t nanalogs);
|
2019-03-05 20:34:43 +01:00
|
|
|
static void idz_jvs_read_switches(void *ctx, struct io3_switch_state *out);
|
2019-05-03 01:38:39 +02:00
|
|
|
static uint16_t idz_jvs_read_coin_counter(void *ctx, uint8_t slot_no);
|
2019-03-05 20:34:43 +01:00
|
|
|
|
|
|
|
static const struct io3_ops idz_jvs_io3_ops = {
|
2019-05-03 01:38:39 +02:00
|
|
|
.read_switches = idz_jvs_read_switches,
|
2019-05-03 21:57:12 +02:00
|
|
|
.read_analogs = idz_jvs_read_analogs,
|
2019-05-03 01:38:39 +02:00
|
|
|
.read_coin_counter = idz_jvs_read_coin_counter,
|
2019-03-05 20:34:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct io3 idz_jvs_io3;
|
|
|
|
static bool idz_jvs_coin;
|
2019-05-03 01:38:39 +02:00
|
|
|
static uint16_t idz_jvs_coins;
|
2019-04-08 20:25:31 +02:00
|
|
|
static bool idz_jvs_shifting;
|
|
|
|
static uint8_t idz_jvs_gear;
|
|
|
|
|
|
|
|
static const uint16_t idz_jvs_gear_signals[] = {
|
|
|
|
/* Neutral */
|
|
|
|
0x0000,
|
|
|
|
/* 1: Left|Up */
|
|
|
|
0x2800,
|
|
|
|
/* 2: Left|Down */
|
|
|
|
0x1800,
|
|
|
|
/* 3: Up */
|
|
|
|
0x2000,
|
|
|
|
/* 4: Down */
|
|
|
|
0x1000,
|
|
|
|
/* 5: Right|Up */
|
|
|
|
0x2400,
|
|
|
|
/* 6: Right|Down */
|
|
|
|
0x1400,
|
|
|
|
};
|
2019-03-05 20:34:43 +01:00
|
|
|
|
|
|
|
void idz_jvs_init(void)
|
|
|
|
{
|
|
|
|
io3_init(&idz_jvs_io3, NULL, &idz_jvs_io3_ops, NULL);
|
|
|
|
jvs_attach(&idz_jvs_io3.jvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void idz_jvs_read_switches(void *ctx, struct io3_switch_state *out)
|
|
|
|
{
|
2019-04-08 20:25:31 +02:00
|
|
|
bool shift_inc;
|
|
|
|
bool shift_dec;
|
2019-03-05 20:34:43 +01:00
|
|
|
XINPUT_STATE xi;
|
|
|
|
WORD xb;
|
|
|
|
|
|
|
|
assert(out != NULL);
|
|
|
|
|
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
xb = xi.Gamepad.wButtons;
|
|
|
|
|
2019-04-08 20:25:31 +02:00
|
|
|
/* Update gameplay buttons */
|
2019-03-05 20:34:43 +01:00
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_START) {
|
|
|
|
out->p1 |= 1 << 15;
|
2019-04-08 20:25:31 +02:00
|
|
|
idz_jvs_gear = 0; /* Reset to Neutral when start is pressed */
|
2019-03-05 20:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_UP) {
|
|
|
|
out->p1 |= 1 << 13;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_DOWN) {
|
|
|
|
out->p1 |= 1 << 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_LEFT) {
|
|
|
|
out->p1 |= 1 << 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_DPAD_RIGHT) {
|
|
|
|
out->p1 |= 1 << 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xb & XINPUT_GAMEPAD_BACK) {
|
|
|
|
out->p1 |= 1 << 9;
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:25:31 +02:00
|
|
|
/* Update simulated six-speed shifter */
|
|
|
|
|
2019-04-18 22:55:00 +02:00
|
|
|
shift_inc = xb & (XINPUT_GAMEPAD_X | XINPUT_GAMEPAD_RIGHT_SHOULDER);
|
|
|
|
shift_dec = xb & (XINPUT_GAMEPAD_Y | XINPUT_GAMEPAD_LEFT_SHOULDER);
|
2019-04-08 20:25:31 +02:00
|
|
|
|
|
|
|
if (!idz_jvs_shifting) {
|
|
|
|
if (shift_inc && idz_jvs_gear < 6) {
|
|
|
|
idz_jvs_gear++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shift_dec && idz_jvs_gear > 0) {
|
|
|
|
idz_jvs_gear--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idz_jvs_shifting = shift_inc || shift_dec;
|
|
|
|
out->p2 = idz_jvs_gear_signals[idz_jvs_gear];
|
|
|
|
|
2019-03-05 20:34:43 +01:00
|
|
|
/* Update test/service buttons */
|
|
|
|
|
|
|
|
if (GetAsyncKeyState('1')) {
|
|
|
|
out->system = 0x80;
|
|
|
|
} else {
|
|
|
|
out->system = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAsyncKeyState('2')) {
|
|
|
|
out->p1 |= 1 << 14;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
static void idz_jvs_read_analogs(
|
|
|
|
void *ctx,
|
|
|
|
uint16_t *analogs,
|
|
|
|
uint8_t nanalogs)
|
2019-03-05 20:34:43 +01:00
|
|
|
{
|
|
|
|
XINPUT_STATE xi;
|
2019-04-18 22:55:00 +02:00
|
|
|
int left;
|
|
|
|
int right;
|
2019-03-05 20:34:43 +01:00
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
assert(analogs != NULL);
|
2019-03-05 20:34:43 +01:00
|
|
|
|
|
|
|
memset(&xi, 0, sizeof(xi));
|
|
|
|
XInputGetState(0, &xi);
|
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
/* Wheel */
|
2019-03-05 20:34:43 +01:00
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
if (nanalogs > 0) {
|
2019-04-18 22:55:00 +02:00
|
|
|
left = xi.Gamepad.sThumbLX;
|
|
|
|
|
|
|
|
if (left < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
|
|
left += XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
|
|
|
|
} else if (left > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE) {
|
|
|
|
left -= XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
|
|
|
|
} else {
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
right = xi.Gamepad.sThumbRX;
|
|
|
|
|
|
|
|
if (right < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) {
|
|
|
|
right += XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
|
|
|
|
} else if (right > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) {
|
|
|
|
right -= XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE;
|
2019-03-05 20:34:43 +01:00
|
|
|
} else {
|
2019-04-18 22:55:00 +02:00
|
|
|
right = 0;
|
2019-03-05 20:34:43 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
analogs[0] = 0x8000 + (left + right) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accel */
|
2019-04-18 22:55:00 +02:00
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
if (nanalogs > 1) {
|
|
|
|
analogs[1] = xi.Gamepad.bRightTrigger << 8;
|
|
|
|
}
|
2019-03-05 20:34:43 +01:00
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
/* Brake */
|
2019-03-05 20:34:43 +01:00
|
|
|
|
2019-05-03 21:57:12 +02:00
|
|
|
if (nanalogs > 2) {
|
|
|
|
analogs[2] = xi.Gamepad.bLeftTrigger << 8;
|
2019-03-05 20:34:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 01:38:39 +02:00
|
|
|
static uint16_t idz_jvs_read_coin_counter(void *ctx, uint8_t slot_no)
|
2019-03-05 20:34:43 +01:00
|
|
|
{
|
|
|
|
if (slot_no > 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAsyncKeyState('3')) {
|
2019-05-03 01:38:39 +02:00
|
|
|
if (!idz_jvs_coin) {
|
2019-03-05 20:34:43 +01:00
|
|
|
dprintf("IDZero JVS: Coin drop\n");
|
|
|
|
idz_jvs_coin = true;
|
2019-05-03 01:38:39 +02:00
|
|
|
idz_jvs_coins++;
|
2019-03-05 20:34:43 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idz_jvs_coin = false;
|
|
|
|
}
|
2019-05-03 01:38:39 +02:00
|
|
|
|
|
|
|
return idz_jvs_coins;
|
2019-03-05 20:34:43 +01:00
|
|
|
}
|