Hoist slider from board into chunihook

Diva slider uses the quasi-JVS framing used by all other SEGA
boards, the Chunithm slider is a special case.
This commit is contained in:
Tau
2018-11-13 19:36:34 -05:00
parent 78e7de613f
commit 9f43f9f397
8 changed files with 11 additions and 10 deletions

View File

@ -9,8 +9,5 @@ board_lib = static_library(
sources : [
'io3.c',
'io3.h',
'slider-cmd.h',
'slider-frame.c',
'slider-frame.h',
],
)

View File

@ -1,36 +0,0 @@
#pragma once
#include "board/slider-frame.h"
enum {
SLIDER_CMD_AUTO_SCAN = 0x01,
SLIDER_CMD_SET_LED = 0x02,
SLIDER_CMD_AUTO_SCAN_START = 0x03,
SLIDER_CMD_AUTO_SCAN_STOP = 0x04,
SLIDER_CMD_RESET = 0x10,
SLIDER_CMD_GET_BOARD_INFO = 0xF0,
};
struct slider_req_set_led {
struct slider_hdr hdr;
struct {
uint8_t unk; /* 0x28, decimal 40. meaning unknown. */
uint8_t rgb[32][3];
} payload;
};
union slider_req_any {
struct slider_hdr hdr;
struct slider_req_set_led set_led;
uint8_t bytes[260];
};
struct slider_resp_get_board_info {
struct slider_hdr hdr;
char version[32];
};
struct slider_resp_auto_scan {
struct slider_hdr hdr;
uint8_t pressure[32];
};

View File

@ -1,179 +0,0 @@
#include <windows.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "board/slider-frame.h"
#include "hook/iobuf.h"
static void slider_frame_sync(struct iobuf *src);
static HRESULT slider_frame_accept(const struct iobuf *dest);
static HRESULT slider_frame_encode_byte(struct iobuf *dest, uint8_t byte);
/* Frame structure:
[0] Sync byte (0xFF)
[1] Command
[2] Payload size
... Payload
[n] Checksum: Negate the sum of all prior bytes (including sync byte!)
Byte stuffing:
0xFD is an escape byte. Un-escape the subsequent byte by adding 1. */
static void slider_frame_sync(struct iobuf *src)
{
size_t i;
for (i = 0 ; i < src->pos && src->bytes[i] != 0xFF ; i++);
src->pos -= i;
memmove(&src->bytes[0], &src->bytes[i], i);
}
static HRESULT slider_frame_accept(const struct iobuf *dest)
{
uint8_t checksum;
size_t i;
if (dest->pos < 2 || dest->pos != dest->bytes[2] + 4) {
return S_FALSE;
}
checksum = 0;
for (i = 0 ; i < dest->pos ; i++) {
checksum += dest->bytes[i];
}
if (checksum != 0) {
return HRESULT_FROM_WIN32(ERROR_CRC);
}
return S_OK;
}
HRESULT slider_frame_decode(struct iobuf *dest, struct iobuf *src)
{
uint8_t byte;
bool escape;
size_t i;
HRESULT hr;
assert(dest != NULL);
assert(dest->bytes != NULL || dest->nbytes == 0);
assert(dest->pos <= dest->nbytes);
assert(src != NULL);
assert(src->bytes != NULL || src->nbytes == 0);
assert(src->pos <= src->nbytes);
slider_frame_sync(src);
dest->pos = 0;
escape = false;
for (i = 0, hr = S_FALSE ; i < src->pos && hr == S_FALSE ; i++) {
/* Step the FSM to unstuff another byte */
byte = src->bytes[i];
if (dest->pos >= dest->nbytes) {
hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
} else if (i == 0) {
dest->bytes[dest->pos++] = byte;
} else if (byte == 0xFF) {
hr = E_FAIL;
} else if (byte == 0xFD) {
if (escape) {
hr = E_FAIL;
}
escape = true;
} else if (escape) {
dest->bytes[dest->pos++] = byte + 1;
escape = false;
} else {
dest->bytes[dest->pos++] = byte;
}
/* Try to accept the packet we've built up so far */
if (SUCCEEDED(hr)) {
hr = slider_frame_accept(dest);
}
}
/* Handle FSM terminal state */
if (hr != S_FALSE) {
/* Frame was either accepted or rejected, remove it from src */
memmove(&src->bytes[0], &src->bytes[i], src->pos - i);
src->pos -= i;
}
return hr;
}
HRESULT slider_frame_encode(
struct iobuf *dest,
const void *ptr,
size_t nbytes)
{
const uint8_t *src;
uint8_t checksum;
uint8_t byte;
size_t i;
HRESULT hr;
assert(dest != NULL);
assert(dest->bytes != NULL || dest->nbytes == 0);
assert(dest->pos <= dest->nbytes);
assert(ptr != NULL);
src = ptr;
assert(nbytes >= 2 && src[0] == 0xFF && src[2] + 3 == nbytes);
if (dest->pos >= dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = 0xFF;
checksum = 0xFF;
for (i = 1 ; i < nbytes ; i++) {
byte = src[i];
checksum += byte;
hr = slider_frame_encode_byte(dest, byte);
if (FAILED(hr)) {
return hr;
}
}
return slider_frame_encode_byte(dest, -checksum);
}
static HRESULT slider_frame_encode_byte(struct iobuf *dest, uint8_t byte)
{
if (byte == 0xFF || byte == 0xFD) {
if (dest->pos + 2 > dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = 0xFD;
dest->bytes[dest->pos++] = byte - 1;
} else {
if (dest->pos + 1 > dest->nbytes) {
return HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
}
dest->bytes[dest->pos++] = byte;
}
return S_OK;
}

View File

@ -1,25 +0,0 @@
#pragma once
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
#include "hook/iobuf.h"
enum {
SLIDER_FRAME_SYNC = 0xFF,
};
struct slider_hdr {
uint8_t sync;
uint8_t cmd;
uint8_t nbytes;
};
HRESULT slider_frame_decode(struct iobuf *dest, struct iobuf *src);
HRESULT slider_frame_encode(
struct iobuf *dest,
const void *ptr,
size_t nbytes);