1
0
mirror of https://gitea.tendokyu.moe/Hay1tsme/segatools.git synced 2024-11-25 01:40:11 +01:00
segatools-hay1tsme/chunihook/jvs.c

137 lines
2.9 KiB
C
Raw Normal View History

#include <windows.h>
2018-11-23 20:37:42 +01:00
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "amex/jvs.h"
#include "board/io3.h"
2021-05-31 18:58:22 +02:00
#include "chunihook/chuni-dll.h"
2019-05-03 04:12:06 +02:00
#include "jvs/jvs-bus.h"
#include "util/dprintf.h"
2019-05-03 04:12:06 +02:00
struct chunithm_jvs_ir_mask {
uint16_t p1;
uint16_t p2;
};
static void chunithm_jvs_read_switches(void *ctx, struct io3_switch_state *out);
static void chunithm_jvs_read_coin_counter(
void *ctx,
uint8_t slot_no,
uint16_t *out);
static const struct io3_ops chunithm_jvs_io3_ops = {
.read_switches = chunithm_jvs_read_switches,
.read_coin_counter = chunithm_jvs_read_coin_counter,
};
2021-05-31 19:00:22 +02:00
// Incorrect IR beam mappings retained for backward compatibility
static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks_v1[] = {
2019-05-03 04:12:06 +02:00
{ 0x0000, 0x0020 },
{ 0x0020, 0x0000 },
{ 0x0000, 0x0010 },
{ 0x0010, 0x0000 },
{ 0x0000, 0x0008 },
{ 0x0008, 0x0000 },
};
2021-05-31 19:00:22 +02:00
static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks[] = {
{ 0x0020, 0x0000 },
{ 0x0000, 0x0020 },
{ 0x0010, 0x0000 },
{ 0x0000, 0x0010 },
{ 0x0008, 0x0000 },
{ 0x0000, 0x0008 },
};
static struct io3 chunithm_jvs_io3;
2019-11-03 19:01:03 +01:00
HRESULT chunithm_jvs_init(struct jvs_node **out)
{
2019-11-03 19:01:03 +01:00
HRESULT hr;
assert(out != NULL);
2021-05-31 18:58:22 +02:00
assert(chuni_dll.jvs_init != NULL);
2019-11-03 19:01:03 +01:00
2021-05-31 18:58:22 +02:00
dprintf("JVS I/O: Starting IO backend\n");
hr = chuni_dll.jvs_init();
2019-11-03 19:01:03 +01:00
if (FAILED(hr)) {
dprintf("JVS I/O: Backend error, I/O disconnected: %x\n", (int) hr);
return hr;
}
io3_init(&chunithm_jvs_io3, NULL, &chunithm_jvs_io3_ops, NULL);
2019-11-03 19:01:03 +01:00
*out = io3_to_jvs_node(&chunithm_jvs_io3);
return S_OK;
}
static void chunithm_jvs_read_switches(void *ctx, struct io3_switch_state *out)
{
2021-05-31 19:00:22 +02:00
const struct chunithm_jvs_ir_mask *masks;
2019-05-03 04:12:06 +02:00
uint8_t opbtn;
uint8_t beams;
size_t i;
2019-05-03 04:12:06 +02:00
assert(out != NULL);
2021-05-31 18:58:22 +02:00
assert(chuni_dll.jvs_poll != NULL);
2021-05-31 19:00:22 +02:00
if (chuni_dll.api_version >= 0x0101) {
// Use correct mapping
masks = chunithm_jvs_ir_masks;
} else {
// Use backwards-compatible incorrect mapping
masks = chunithm_jvs_ir_masks_v1;
}
2019-05-03 04:12:06 +02:00
opbtn = 0;
beams = 0;
2021-05-31 18:58:22 +02:00
chuni_dll.jvs_poll(&opbtn, &beams);
2019-05-03 04:12:06 +02:00
out->system = 0x00;
out->p1 = 0x0000;
out->p2 = 0x0000;
2019-05-03 04:12:06 +02:00
if (opbtn & 0x01) {
out->system = 0x80;
} else {
2019-05-03 04:12:06 +02:00
out->system = 0x00;
}
2019-05-03 04:12:06 +02:00
if (opbtn & 0x02) {
out->p1 |= 0x4000;
}
2019-05-03 04:12:06 +02:00
for (i = 0 ; i < 6 ; i++) {
/* Beam "press" is active-low hence the ~ */
if (~beams & (1 << i)) {
2021-05-31 19:00:22 +02:00
out->p1 |= masks[i].p1;
out->p2 |= masks[i].p2;
2019-05-03 04:12:06 +02:00
}
}
}
static void chunithm_jvs_read_coin_counter(
void *ctx,
uint8_t slot_no,
uint16_t *out)
{
assert(out != NULL);
2021-05-31 18:58:22 +02:00
assert(chuni_dll.jvs_read_coin_counter != NULL);
if (slot_no > 0) {
return;
}
2021-05-31 18:58:22 +02:00
chuni_dll.jvs_read_coin_counter(out);
}