chuniio: Break out Chunithm IO DLL

This commit is contained in:
Tau
2019-05-02 22:12:06 -04:00
parent 23257f272e
commit 46ab6c3d96
9 changed files with 321 additions and 111 deletions

View File

@ -12,6 +12,8 @@
#include "chunihook/jvs.h"
#include "chunihook/slider-hook.h"
#include "chuniio/chuniio.h"
#include "hook/process.h"
#include "hooklib/serial.h"
@ -73,6 +75,10 @@ static DWORD CALLBACK chuni_pre_startup(void)
dprintf("--- End chuni_pre_startup ---\n");
/* Initialize IO DLL */
chuni_io_init();
/* Jump to EXE start address */
return chuni_startup();

View File

@ -9,10 +9,17 @@
#include "board/io3.h"
#include "chuniio/chuniio.h"
#include "jvs/jvs-bus.h"
#include "util/dprintf.h"
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 uint16_t chunithm_jvs_read_coin_counter(void *ctx, uint8_t slot_no);
@ -21,10 +28,16 @@ static const struct io3_ops chunithm_jvs_io3_ops = {
.read_coin_counter = chunithm_jvs_read_coin_counter,
};
static const struct chunithm_jvs_ir_mask chunithm_jvs_ir_masks[] = {
{ 0x0000, 0x0020 },
{ 0x0020, 0x0000 },
{ 0x0000, 0x0010 },
{ 0x0010, 0x0000 },
{ 0x0000, 0x0008 },
{ 0x0008, 0x0000 },
};
static struct io3 chunithm_jvs_io3;
static size_t chunithm_jvs_rise_pos;
static bool chunithm_jvs_coin;
static uint16_t chunithm_jvs_coins;
void chunithm_jvs_init(void)
{
@ -34,45 +47,37 @@ void chunithm_jvs_init(void)
static void chunithm_jvs_read_switches(void *ctx, struct io3_switch_state *out)
{
uint8_t opbtn;
uint8_t beams;
size_t i;
assert(out != NULL);
/* Update simulated raise/lower state */
opbtn = 0;
beams = 0;
if (GetAsyncKeyState(VK_SPACE)) {
if (chunithm_jvs_rise_pos < 6) {
chunithm_jvs_rise_pos++;
}
} else {
if (chunithm_jvs_rise_pos > 0) {
chunithm_jvs_rise_pos--;
}
}
chuni_io_jvs_poll(&opbtn, &beams);
/* Render the state. Every case falls through, this is intentional. */
out->system = 0x00;
out->p1 = 0x0000;
out->p2 = 0x0000;
out->p1 = 0;
out->p2 = 0;
switch (chunithm_jvs_rise_pos) {
case 0: out->p2 |= 0x0020;
case 1: out->p1 |= 0x0020;
case 2: out->p2 |= 0x0010;
case 3: out->p1 |= 0x0010;
case 4: out->p2 |= 0x0008;
case 5: out->p1 |= 0x0008;
}
/* Update test/service buttons */
if (GetAsyncKeyState('1')) {
if (opbtn & 0x01) {
out->system = 0x80;
} else {
out->system = 0;
out->system = 0x00;
}
if (GetAsyncKeyState('2')) {
if (opbtn & 0x02) {
out->p1 |= 0x4000;
}
for (i = 0 ; i < 6 ; i++) {
if (beams & (1 << i)) {
out->p1 |= chunithm_jvs_ir_masks[i].p1;
out->p2 |= chunithm_jvs_ir_masks[i].p2;
}
}
}
static uint16_t chunithm_jvs_read_coin_counter(void *ctx, uint8_t slot_no)
@ -81,15 +86,5 @@ static uint16_t chunithm_jvs_read_coin_counter(void *ctx, uint8_t slot_no)
return 0;
}
if (GetAsyncKeyState('3')) {
if (!chunithm_jvs_coin) {
dprintf("Chunithm JVS: Coin drop\n");
chunithm_jvs_coin = true;
chunithm_jvs_coins++;
}
} else {
chunithm_jvs_coin = false;
}
return chunithm_jvs_coins;
return chuni_io_jvs_read_coin_counter();
}

View File

@ -11,6 +11,7 @@ shared_library(
link_with : [
amex_lib,
board_lib,
chuniio_dll,
jvs_lib,
platform_lib,
util_lib,

View File

@ -11,6 +11,8 @@
#include "chunihook/slider-hook.h"
#include "chuniio/chuniio.h"
#include "hook/iobuf.h"
#include "hook/iohook.h"
@ -29,16 +31,13 @@ static HRESULT slider_req_auto_scan_start(void);
static HRESULT slider_req_auto_scan_stop(void);
static HRESULT slider_req_set_led(const struct slider_req_set_led *req);
static unsigned int __stdcall slider_thread_proc(void *ctx);
static void slider_res_auto_scan(const uint8_t *state);
static CRITICAL_SECTION slider_lock;
static struct uart slider_uart;
static uint8_t slider_written_bytes[520];
static uint8_t slider_readable_bytes[520];
static HANDLE slider_thread;
static bool slider_stop;
void slider_hook_init(void)
{
InitializeCriticalSection(&slider_lock);
@ -173,25 +172,8 @@ static HRESULT slider_req_get_board_info(void)
static HRESULT slider_req_auto_scan_start(void)
{
dprintf("Chunithm slider: Start slider thread\n");
if (slider_thread != NULL) {
dprintf("Thread is already running\n");
return S_OK;
}
slider_thread = (HANDLE) _beginthreadex(
NULL,
0,
slider_thread_proc,
NULL,
0,
NULL);
if (slider_thread == NULL) {
dprintf("Thread launch failed\n");
}
dprintf("Chunithm slider: Start slider notifications\n");
chuni_io_slider_start(slider_res_auto_scan);
/* This message is not acknowledged */
@ -202,21 +184,16 @@ static HRESULT slider_req_auto_scan_stop(void)
{
struct slider_hdr resp;
dprintf("Chunithm slider: Stop slider thread\n");
dprintf("Chunithm slider: Stop slider notifications\n");
if (slider_thread != NULL) {
slider_stop = true;
LeaveCriticalSection(&slider_lock);
/* IO DLL worker thread might attempt to invoke the callback (which needs
to take slider_lock, which we are currently holding) before noticing that
it needs to shut down. Unlock here so that we don't deadlock in that
situation. */
WaitForSingleObject(slider_thread, INFINITE);
CloseHandle(slider_thread);
slider_thread = NULL;
slider_stop = false;
dprintf("Chunithm slider: Thread has terminated\n");
EnterCriticalSection(&slider_lock);
}
LeaveCriticalSection(&slider_lock);
chuni_io_slider_stop();
EnterCriticalSection(&slider_lock);
resp.sync = SLIDER_FRAME_SYNC;
resp.cmd = SLIDER_CMD_AUTO_SCAN_STOP;
@ -227,45 +204,23 @@ static HRESULT slider_req_auto_scan_stop(void)
static HRESULT slider_req_set_led(const struct slider_req_set_led *req)
{
chuni_io_slider_set_leds(req->payload.rgb);
/* This message is not acknowledged */
return S_OK;
}
static unsigned int WINAPI slider_thread_proc(void *ctx)
static void slider_res_auto_scan(const uint8_t *state)
{
static const int keys[] = {
'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
};
struct slider_resp_auto_scan resp;
uint8_t pressure;
bool stop;
size_t i;
for (;;) {
resp.hdr.sync = SLIDER_FRAME_SYNC;
resp.hdr.cmd = SLIDER_CMD_AUTO_SCAN;
resp.hdr.nbytes = sizeof(resp.pressure);
resp.hdr.sync = SLIDER_FRAME_SYNC;
resp.hdr.cmd = SLIDER_CMD_AUTO_SCAN;
resp.hdr.nbytes = sizeof(resp.pressure);
memcpy(resp.pressure, state, sizeof(resp.pressure));
for (i = 0 ; i < 8 ; i++) {
pressure = GetAsyncKeyState(keys[i]) ? 20 : 0;
memset(&resp.pressure[28 - 4 * i], pressure, 4);
}
EnterCriticalSection(&slider_lock);
stop = slider_stop;
if (!stop) {
slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
}
LeaveCriticalSection(&slider_lock);
if (stop) {
return 0;
}
Sleep(1);
}
EnterCriticalSection(&slider_lock);
slider_frame_encode(&slider_uart.readable, &resp, sizeof(resp));
LeaveCriticalSection(&slider_lock);
}