bio2emu: refactor BIO2 emulation

This commit is contained in:
Will Xyen
2020-08-02 23:02:13 -07:00
parent c54a41410b
commit 350ba00549
12 changed files with 122 additions and 100 deletions
+1 -1
View File
@@ -15,6 +15,7 @@ libs_iidxhook8 := \
iidxhook-util \
acioemu \
bio2emu \
bio2emu-iidx \
camhook \
iidxio \
hook \
@@ -24,6 +25,5 @@ libs_iidxhook8 := \
eamio \
src_iidxhook8 := \
bi2a.c \
config-io.c \
dllmain.c \
-282
View File
@@ -1,282 +0,0 @@
#define LOG_MODULE "bio2emu-bi2a"
#include "iidxhook8/bi2a.h"
#include "bio2emu/emu.h"
#include <windows.h> /* for _BitScanForward */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "acioemu/emu.h"
#include "bemanitools/iidxio.h"
static int get_default_slider_valid(size_t idx);
static void bio2_emu_bi2a_cmd_send_version(
struct ac_io_emu *emu, const struct ac_io_message *req);
static void bio2_emu_bi2a_send_state(
struct ac_io_emu *emu, const struct ac_io_message *req);
static void bio2_emu_bi2a_send_empty(
struct ac_io_emu *emu, const struct ac_io_message *req);
static void bio2_emu_bi2a_send_status(
struct ac_io_emu *emu, const struct ac_io_message *req, uint8_t status);
static int default_sliders[5];
static bool poll_delay;
int get_default_slider_valid(size_t idx)
{
if (default_sliders[idx] >= 0 && default_sliders[idx] <= 15) {
return 1;
} else {
return 0;
}
}
void bio2_emu_bi2a_init(
struct bio2emu_port *bio2_emu, bool disable_poll_limiter)
{
bio2emu_port_init(bio2_emu);
poll_delay = !disable_poll_limiter;
if (!poll_delay) {
log_warning("bio2_emu_bi2a_init: poll_delay has been disabled");
}
for (size_t i = 0; i < 5; ++i) {
default_sliders[i] = -1;
}
FILE *f = fopen("vefx.txt", "r");
if (f) {
fscanf(
f,
"%d %d %d %d %d",
&default_sliders[0],
&default_sliders[1],
&default_sliders[2],
&default_sliders[3],
&default_sliders[4]);
fclose(f);
}
}
void bio2_emu_bi2a_dispatch_request(
struct bio2emu_port *bio2port, const struct ac_io_message *req)
{
uint16_t cmd_code;
cmd_code = ac_io_u16(req->cmd.code);
switch (cmd_code) {
case BIO2_BI2A_CMD_UNK_0100:
case BIO2_BI2A_CMD_UNK_0120:
log_misc("BIO2_BI2A_CMD_UNK_%04X(%d)", cmd_code, req->addr);
bio2_emu_bi2a_send_status(&bio2port->acio, req, 0x00);
break;
case BIO2_BI2A_CMD_POLL:
// log_misc("BIO2_BI2A_CMD_POLL");
bio2_emu_bi2a_send_state(&bio2port->acio, req);
break;
case AC_IO_CMD_GET_VERSION:
log_misc("BIO2_CMD_GET_VERSION(%d)", req->addr);
bio2_emu_bi2a_cmd_send_version(&bio2port->acio, req);
break;
case AC_IO_CMD_START_UP:
log_misc("BIO2_CMD_START_UP(%d)", req->addr);
bio2_emu_bi2a_send_status(&bio2port->acio, req, 0x00);
break;
case AC_IO_CMD_KEEPALIVE:
log_misc("BIO2_CMD_KEEPALIVE(%d)", req->addr);
bio2_emu_bi2a_send_empty(&bio2port->acio, req);
break;
default:
log_warning(
"Unknown BIO2 message %04x on BI2A node, addr=%d",
cmd_code,
req->addr);
break;
}
}
static void bio2_emu_bi2a_cmd_send_version(
struct ac_io_emu *emu, const struct ac_io_message *req)
{
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = sizeof(resp.cmd.version);
resp.cmd.version.type = ac_io_u32(AC_IO_NODE_TYPE_BI2A);
resp.cmd.version.flag = 0x00;
resp.cmd.version.major = 0x04;
resp.cmd.version.minor = 0x00;
resp.cmd.version.revision = 0x04;
memcpy(
resp.cmd.version.product_code,
"BI2A",
sizeof(resp.cmd.version.product_code));
strncpy(resp.cmd.version.date, __DATE__, sizeof(resp.cmd.version.date));
strncpy(resp.cmd.version.time, __TIME__, sizeof(resp.cmd.version.time));
ac_io_emu_response_push(emu, &resp, 0);
}
static void
bio2_emu_bi2a_send_empty(struct ac_io_emu *emu, const struct ac_io_message *req)
{
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = 0;
ac_io_emu_response_push(emu, &resp, 0);
}
static void bio2_emu_bi2a_send_status(
struct ac_io_emu *emu, const struct ac_io_message *req, uint8_t status)
{
struct ac_io_message resp;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = sizeof(resp.cmd.status);
resp.cmd.status = status;
ac_io_emu_response_push(emu, &resp, 0);
}
static void
bio2_emu_bi2a_send_state(struct ac_io_emu *emu, const struct ac_io_message *req)
{
struct ac_io_message resp;
struct bi2a_iidx_state_out *body;
struct bi2a_iidx_state_in *req_bi2a;
uint8_t input_sys = 0;
uint8_t input_panel = 0;
uint16_t input_keys = 0;
struct {
uint8_t panel_lights;
uint16_t deck_lights;
uint8_t top_lamps;
} packed_lights;
resp.addr = req->addr | AC_IO_RESPONSE_FLAG;
resp.cmd.code = req->cmd.code;
resp.cmd.seq_no = req->cmd.seq_no;
resp.cmd.nbytes = sizeof(struct bi2a_iidx_state_out);
req_bi2a = (struct bi2a_iidx_state_in *) req->cmd.raw;
packed_lights.panel_lights = 0;
for (size_t i = 0; i < 4; ++i) {
if (req_bi2a->PANEL[i].l_state) {
packed_lights.panel_lights |= (1 << i);
}
}
packed_lights.deck_lights = 0;
for (size_t i = 0; i < 14; ++i) {
if (req_bi2a->DECKSW[i].l_state) {
packed_lights.deck_lights |= (1 << i);
}
}
packed_lights.top_lamps = 0;
for (size_t i = 0; i < 4; ++i) {
if (req_bi2a->SPOTLIGHT1[i].l_state) {
packed_lights.top_lamps |= (1 << i);
}
}
for (size_t i = 0; i < 4; ++i) {
if (req_bi2a->SPOTLIGHT2[i].l_state) {
packed_lights.top_lamps |= (1 << (i + 4));
}
}
iidx_io_ep1_set_deck_lights(packed_lights.deck_lights);
iidx_io_ep1_set_panel_lights(packed_lights.panel_lights);
iidx_io_ep1_set_top_lamps(packed_lights.top_lamps);
iidx_io_ep1_set_top_neons(req_bi2a->NEONLAMP.l_state);
if (!iidx_io_ep1_send()) {
log_warning("BIO2: iidx_io_ep1_send error");
return bio2_emu_bi2a_send_status(emu, req, 0);
}
if (!iidx_io_ep3_write_16seg((const char *) req_bi2a->SEG16)) {
log_warning("BIO2: iidx_io_ep3_write_16seg error");
return bio2_emu_bi2a_send_status(emu, req, 0);
}
body = (struct bi2a_iidx_state_out *) &resp.cmd.raw;
memset(body, 0, sizeof(struct bi2a_iidx_state_out));
// IIDX25 polls really really fast, this limits it to 1000Hz
if (poll_delay) {
Sleep(1);
}
if (!iidx_io_ep2_recv()) {
log_warning("BIO2: iidx_io_ep2_recv error");
return bio2_emu_bi2a_send_status(emu, req, 0);
}
body->TURNTABLE1 = iidx_io_ep2_get_turntable(0);
body->TURNTABLE2 = iidx_io_ep2_get_turntable(1);
body->SLIDER1.s_val = get_default_slider_valid(0) ?
default_sliders[0] :
iidx_io_ep2_get_slider(0);
body->SLIDER2.s_val = get_default_slider_valid(1) ?
default_sliders[1] :
iidx_io_ep2_get_slider(1);
body->SLIDER3.s_val = get_default_slider_valid(2) ?
default_sliders[2] :
iidx_io_ep2_get_slider(2);
body->SLIDER4.s_val = get_default_slider_valid(3) ?
default_sliders[3] :
iidx_io_ep2_get_slider(3);
body->SLIDER5.s_val = get_default_slider_valid(4) ?
default_sliders[4] :
iidx_io_ep2_get_slider(4);
input_keys = iidx_io_ep2_get_keys();
input_sys = iidx_io_ep2_get_sys();
input_panel = iidx_io_ep2_get_panel();
body->P1SW1.b_val = (input_keys >> IIDX_IO_KEY_P1_1) & 1;
body->P1SW2.b_val = (input_keys >> IIDX_IO_KEY_P1_2) & 1;
body->P1SW3.b_val = (input_keys >> IIDX_IO_KEY_P1_3) & 1;
body->P1SW4.b_val = (input_keys >> IIDX_IO_KEY_P1_4) & 1;
body->P1SW5.b_val = (input_keys >> IIDX_IO_KEY_P1_5) & 1;
body->P1SW6.b_val = (input_keys >> IIDX_IO_KEY_P1_6) & 1;
body->P1SW7.b_val = (input_keys >> IIDX_IO_KEY_P1_7) & 1;
body->P2SW1.b_val = (input_keys >> IIDX_IO_KEY_P2_1) & 1;
body->P2SW2.b_val = (input_keys >> IIDX_IO_KEY_P2_2) & 1;
body->P2SW3.b_val = (input_keys >> IIDX_IO_KEY_P2_3) & 1;
body->P2SW4.b_val = (input_keys >> IIDX_IO_KEY_P2_4) & 1;
body->P2SW5.b_val = (input_keys >> IIDX_IO_KEY_P2_5) & 1;
body->P2SW6.b_val = (input_keys >> IIDX_IO_KEY_P2_6) & 1;
body->P2SW7.b_val = (input_keys >> IIDX_IO_KEY_P2_7) & 1;
body->PANEL.y_start1 = (input_panel >> IIDX_IO_PANEL_P1_START) & 1;
body->PANEL.y_start2 = (input_panel >> IIDX_IO_PANEL_P2_START) & 1;
body->PANEL.y_vefx = (input_panel >> IIDX_IO_PANEL_VEFX) & 1;
body->PANEL.y_effect = (input_panel >> IIDX_IO_PANEL_EFFECT) & 1;
body->SYSTEM.v_test = (input_sys >> IIDX_IO_SYS_TEST) & 1;
body->SYSTEM.v_service = (input_sys >> IIDX_IO_SYS_SERVICE) & 1;
body->SYSTEM.v_coin = (input_sys >> IIDX_IO_SYS_COIN) & 1;
ac_io_emu_response_push(emu, &resp, 0);
}
-121
View File
@@ -1,121 +0,0 @@
#ifndef IIDXHOOK_BI2A_H
#define IIDXHOOK_BI2A_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "bio2emu/emu.h"
enum bio2_iidx_cmd {
// Custom Stuff
BIO2_BI2A_CMD_UNK_0100 = 0x0100,
BIO2_BI2A_CMD_UNK_0120 = 0x0120,
BIO2_BI2A_CMD_POLL = 0x0152,
};
#pragma pack(push, 1)
struct bi2a_iidx_slider {
uint8_t s_unk : 4;
uint8_t s_val : 4;
};
struct bi2a_iidx_system {
uint8_t v_unk1 : 1;
uint8_t v_coin : 1;
uint8_t v_service : 1;
uint8_t v_test : 1;
uint8_t v_unk2 : 4;
};
struct bi2a_iidx_button {
uint8_t b_unk : 7;
uint8_t b_val : 1;
};
struct bi2a_iidx_panel {
uint8_t y_unk : 4;
uint8_t y_effect : 1;
uint8_t y_vefx : 1;
uint8_t y_start2 : 1;
uint8_t y_start1 : 1;
};
struct bi2a_iidx_state_out {
struct bi2a_iidx_slider SLIDER1;
struct bi2a_iidx_system SYSTEM;
struct bi2a_iidx_slider SLIDER2;
uint8_t UNK1;
struct bi2a_iidx_slider SLIDER3;
uint8_t UNK2;
struct bi2a_iidx_slider SLIDER4;
struct bi2a_iidx_slider SLIDER5;
uint8_t UNK3; // coin mech?
struct bi2a_iidx_panel PANEL;
uint8_t UNK4;
uint8_t UNK5;
uint8_t UNK6;
uint8_t UNK7;
uint8_t UNK8;
uint8_t UNK9;
uint8_t TURNTABLE1;
uint8_t TURNTABLE2;
struct bi2a_iidx_button P1SW1;
uint8_t UNK11;
struct bi2a_iidx_button P1SW2;
uint8_t UNK12;
struct bi2a_iidx_button P1SW3;
uint8_t UNK13;
struct bi2a_iidx_button P1SW4;
uint8_t UNK14;
struct bi2a_iidx_button P1SW5;
uint8_t UNK15;
struct bi2a_iidx_button P1SW6;
uint8_t UNK16;
struct bi2a_iidx_button P1SW7;
uint8_t UNK17;
struct bi2a_iidx_button P2SW1;
uint8_t UNK21;
struct bi2a_iidx_button P2SW2;
uint8_t UNK22;
struct bi2a_iidx_button P2SW3;
uint8_t UNK23;
struct bi2a_iidx_button P2SW4;
uint8_t UNK24;
struct bi2a_iidx_button P2SW5;
uint8_t UNK25;
struct bi2a_iidx_button P2SW6;
uint8_t UNK26;
struct bi2a_iidx_button P2SW7;
uint8_t UNK27;
};
struct bi2a_iidx_light {
uint8_t l_unk : 7;
uint8_t l_state : 1;
};
struct bi2a_iidx_state_in {
struct bi2a_iidx_light UNK1[3];
struct bi2a_iidx_light PANEL[4];
struct bi2a_iidx_light DECKSW[14];
uint8_t UNK2[2];
uint8_t SEG16[9];
struct bi2a_iidx_light SPOTLIGHT1[4];
struct bi2a_iidx_light NEONLAMP;
struct bi2a_iidx_light SPOTLIGHT2[4];
uint8_t UNK3[7];
};
_Static_assert(
sizeof(struct bi2a_iidx_state_in) == 48,
"bi2a_iidx_state_in is the wrong size");
_Static_assert(
sizeof(struct bi2a_iidx_state_out) == 46,
"bi2a_iidx_state_out is the wrong size");
#pragma pack(pop)
void bio2_emu_bi2a_init(struct bio2emu_port *in, bool disable_poll_limiter);
void bio2_emu_bi2a_dispatch_request(
struct bio2emu_port *bio2port, const struct ac_io_message *req);
#endif
+1 -1
View File
@@ -24,7 +24,7 @@
#include "iidxhook-util/log-server.h"
#include "bio2emu/emu.h"
#include "iidxhook8/bi2a.h"
#include "bio2emu-iidx/bi2a.h"
#include "camhook/cam.h"
#include "camhook/config-cam.h"