segatools2/mai2hook/io4.c
2023-11-30 02:29:27 -05:00

148 lines
2.9 KiB
C

#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "board/io4.h"
#include "mai2hook/mai2-dll.h"
#include "util/dprintf.h"
bool mai2_io_coin = false;
uint16_t mai2_io_coins = 0;
static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state);
static const struct io4_ops mai2_io4_ops = {
.poll = mai2_io4_poll,
};
HRESULT mai2_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(mai2_dll.init != NULL);
hr = io4_hook_init(cfg, &mai2_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return mai2_dll.init();
}
static HRESULT mai2_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn = 0;
uint8_t player1 = 0;
uint8_t player2 = 0;
HRESULT hr;
assert(mai2_dll.poll != NULL);
hr = mai2_dll.poll(&opbtn, &player1, &player2);
if (FAILED(hr)) {
return hr;
}
if (opbtn & MAI2_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & MAI2_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & MAI2_IO_P1_START) {
state->buttons[0] |= 1 << 1;
}
if (opbtn & MAI2_IO_P2_START) {
state->buttons[1] |= 1 << 4;
}
if (!(player1 & MAI2_IO_GAMEBTN_1)) {
state->buttons[0] |= 1 << 2;
}
if (!(player1 & MAI2_IO_GAMEBTN_2)) {
state->buttons[0] |= 1 << 3;
}
if (!(player1 & MAI2_IO_GAMEBTN_3)) {
state->buttons[0] |= 1 << 0;
}
if (!(player1 & MAI2_IO_GAMEBTN_4)) {
state->buttons[0] |= 1 << 15;
}
if (!(player1 & MAI2_IO_GAMEBTN_5)) {
state->buttons[0] |= 1 << 14;
}
if (!(player1 & MAI2_IO_GAMEBTN_6)) {
state->buttons[0] |= 1 << 13;
}
if (!(player1 & MAI2_IO_GAMEBTN_7)) {
state->buttons[0] |= 1 << 12;
}
if (!(player1 & MAI2_IO_GAMEBTN_8)) {
state->buttons[0] |= 1 << 11;
}
if (!(player2 & MAI2_IO_GAMEBTN_1)) {
state->buttons[1] |= 1 << 2;
}
if (!(player2 & MAI2_IO_GAMEBTN_2)) {
state->buttons[1] |= 1 << 3;
}
if (!(player2 & MAI2_IO_GAMEBTN_3)) {
state->buttons[1] |= 1 << 0;
}
if (!(player2 & MAI2_IO_GAMEBTN_4)) {
state->buttons[1] |= 1 << 15;
}
if (!(player2 & MAI2_IO_GAMEBTN_5)) {
state->buttons[1] |= 1 << 14;
}
if (!(player2 & MAI2_IO_GAMEBTN_6)) {
state->buttons[1] |= 1 << 13;
}
if (!(player2 & MAI2_IO_GAMEBTN_7)) {
state->buttons[1] |= 1 << 12;
}
if (!(player2 & MAI2_IO_GAMEBTN_8)) {
state->buttons[1] |= 1 << 11;
}
if (opbtn & MAI2_IO_OPBTN_COIN) {
if (!mai2_io_coin) {
mai2_io_coin = true;
mai2_io_coins++;
}
}
else {
mai2_io_coin = false;
}
state->chutes[0] = 128 + 256 * mai2_io_coins;
return S_OK;
}