swdc: first segatools added

This commit is contained in:
2023-07-14 00:52:50 +02:00
parent 89195ed60b
commit ec072667b3
31 changed files with 2167 additions and 1 deletions

53
swdchook/config.c Normal file
View File

@ -0,0 +1,53 @@
#include <assert.h>
#include <stddef.h>
#include "board/config.h"
#include "board/sg-reader.h"
#include "hooklib/config.h"
#include "hooklib/dvd.h"
#include "swdchook/config.h"
#include "swdchook/swdc-dll.h"
#include "platform/config.h"
#include "platform/platform.h"
void swdc_dll_config_load(
struct swdc_dll_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
GetPrivateProfileStringW(
L"swdcio",
L"path",
L"",
cfg->path,
_countof(cfg->path),
filename);
}
void swdc_hook_config_load(
struct swdc_hook_config *cfg,
const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
platform_config_load(&cfg->platform, filename);
aime_config_load(&cfg->aime, filename);
swdc_dll_config_load(&cfg->dll, filename);
zinput_config_load(&cfg->zinput, filename);
dvd_config_load(&cfg->dvd, filename);
io4_config_load(&cfg->io4, filename);
}
void zinput_config_load(struct zinput_config *cfg, const wchar_t *filename)
{
assert(cfg != NULL);
assert(filename != NULL);
cfg->enable = GetPrivateProfileIntW(L"zinput", L"enable", 1, filename);
}

32
swdchook/config.h Normal file
View File

@ -0,0 +1,32 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include "board/config.h"
#include "hooklib/dvd.h"
#include "swdchook/swdc-dll.h"
#include "swdchook/zinput.h"
#include "platform/platform.h"
struct swdc_hook_config {
struct platform_config platform;
struct aime_config aime;
struct dvd_config dvd;
struct io4_config io4;
struct swdc_dll_config dll;
struct zinput_config zinput;
};
void swdc_dll_config_load(
struct swdc_dll_config *cfg,
const wchar_t *filename);
void swdc_hook_config_load(
struct swdc_hook_config *cfg,
const wchar_t *filename);
void zinput_config_load(struct zinput_config *cfg, const wchar_t *filename);

112
swdchook/dllmain.c Normal file
View File

@ -0,0 +1,112 @@
#include <windows.h>
#include <shlwapi.h>
#include <stdlib.h>
#include "board/sg-reader.h"
#include "board/io4.h"
#include "board/vfd.h"
#include "hook/process.h"
#include "hooklib/dvd.h"
#include "hooklib/serial.h"
#include "hooklib/spike.h"
#include "swdchook/config.h"
#include "swdchook/swdc-dll.h"
#include "swdchook/io4.h"
#include "swdchook/zinput.h"
#include "platform/platform.h"
#include "util/dprintf.h"
static HMODULE swdc_hook_mod;
static process_entry_t swdc_startup;
static struct swdc_hook_config swdc_hook_cfg;
static DWORD CALLBACK swdc_pre_startup(void)
{
HRESULT hr;
dprintf("--- Begin swdc_pre_startup ---\n");
/* Config load */
swdc_hook_config_load(&swdc_hook_cfg, L".\\segatools.ini");
/* Hook Win32 APIs */
serial_hook_init();
zinput_hook_init(&swdc_hook_cfg.zinput);
dvd_hook_init(&swdc_hook_cfg.dvd, swdc_hook_mod);
/* Initialize emulation hooks */
hr = platform_hook_init(
&swdc_hook_cfg.platform,
"SDDS",
"ACA4",
swdc_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = sg_reader_hook_init(&swdc_hook_cfg.aime, 3, swdc_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = vfd_hook_init(4);
if (FAILED(hr)) {
return hr;
}
hr = swdc_dll_init(&swdc_hook_cfg.dll, swdc_hook_mod);
if (FAILED(hr)) {
goto fail;
}
hr = swdc_io4_hook_init(&swdc_hook_cfg.io4);
if (FAILED(hr)) {
goto fail;
}
/* Initialize debug helpers */
spike_hook_init(L".\\segatools.ini");
dprintf("--- End swdc_pre_startup ---\n");
/* Jump to EXE start address */
return swdc_startup();
fail:
ExitProcess(EXIT_FAILURE);
}
BOOL WINAPI DllMain(HMODULE mod, DWORD cause, void *ctx)
{
HRESULT hr;
if (cause != DLL_PROCESS_ATTACH) {
return TRUE;
}
swdc_hook_mod = mod;
hr = process_hijack_startup(swdc_pre_startup, &swdc_startup);
if (!SUCCEEDED(hr)) {
dprintf("Failed to hijack process startup: %x\n", (int) hr);
}
return SUCCEEDED(hr);
}

137
swdchook/io4.c Normal file
View File

@ -0,0 +1,137 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "board/io4.h"
#include "swdchook/swdc-dll.h"
#include "util/dprintf.h"
static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state);
static uint16_t coins;
static const struct io4_ops swdc_io4_ops = {
.poll = swdc_io4_poll,
};
HRESULT swdc_io4_hook_init(const struct io4_config *cfg)
{
HRESULT hr;
assert(swdc_dll.init != NULL);
hr = io4_hook_init(cfg, &swdc_io4_ops, NULL);
if (FAILED(hr)) {
return hr;
}
return swdc_dll.init();
}
static HRESULT swdc_io4_poll(void *ctx, struct io4_state *state)
{
uint8_t opbtn;
uint16_t gamebtn;
struct swdc_io_analog_state analog_state;
HRESULT hr;
assert(swdc_dll.poll != NULL);
assert(swdc_dll.get_opbtns != NULL);
assert(swdc_dll.get_gamebtns != NULL);
assert(swdc_dll.get_analogs != NULL);
memset(state, 0, sizeof(*state));
memset(&analog_state, 0, sizeof(analog_state));
hr = swdc_dll.poll();
if (FAILED(hr)) {
return hr;
}
opbtn = 0;
gamebtn = 0;
swdc_dll.get_opbtns(&opbtn);
swdc_dll.get_gamebtns(&gamebtn);
swdc_dll.get_analogs(&analog_state);
if (opbtn & SWDC_IO_OPBTN_TEST) {
state->buttons[0] |= IO4_BUTTON_TEST;
}
if (opbtn & SWDC_IO_OPBTN_SERVICE) {
state->buttons[0] |= IO4_BUTTON_SERVICE;
}
if (opbtn & SWDC_IO_OPBTN_COIN) {
coins++;
}
state->chutes[0] = coins << 8;
/* Update Cabinet buttons */
if (gamebtn & SWDC_IO_GAMEBTN_START) {
state->buttons[0] |= 1 << 7;
}
if (gamebtn & SWDC_IO_GAMEBTN_VIEW_CHANGE) {
state->buttons[0] |= 1 << 1;
}
if (gamebtn & SWDC_IO_GAMEBTN_UP) {
state->buttons[0] |= 1 << 5;
}
if (gamebtn & SWDC_IO_GAMEBTN_DOWN) {
state->buttons[0] |= 1 << 4;
}
if (gamebtn & SWDC_IO_GAMEBTN_LEFT) {
state->buttons[0] |= 1 << 3;
}
if (gamebtn & SWDC_IO_GAMEBTN_RIGHT) {
state->buttons[0] |= 1 << 2;
}
/* Update steering wheel buttons */
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_BLUE) {
state->buttons[1] |= 1 << 15;
}
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_GREEN) {
state->buttons[1] |= 1 << 14;
}
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_RED) {
state->buttons[1] |= 1 << 13;
}
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_YELLOW) {
state->buttons[1] |= 1 << 12;
}
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_PADDLE_LEFT) {
state->buttons[1] |= 1 << 1;
}
if (gamebtn & SWDC_IO_GAMEBTN_STEERING_PADDLE_RIGHT) {
state->buttons[1] |= 1 << 0;
}
/* Steering wheel increases left-to-right.
Use 0x8000 as the center point. */
state->adcs[0] = 0x8000 + analog_state.wheel;
state->adcs[1] = analog_state.accel;
state->adcs[2] = analog_state.brake;
return S_OK;
}

7
swdchook/io4.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <windows.h>
#include "board/io4.h"
HRESULT swdc_io4_hook_init(const struct io4_config *cfg);

32
swdchook/meson.build Normal file
View File

@ -0,0 +1,32 @@
shared_library(
'swdchook',
name_prefix : '',
include_directories : inc,
implicit_include_directories : false,
vs_module_defs : 'swdchook.def',
c_pch : '../precompiled.h',
dependencies : [
capnhook.get_variable('hook_dep'),
capnhook.get_variable('hooklib_dep'),
xinput_lib,
],
link_with : [
aimeio_lib,
board_lib,
hooklib_lib,
swdcio_lib,
platform_lib,
util_lib,
],
sources : [
'config.c',
'config.h',
'dllmain.c',
'swdc-dll.c',
'swdc-dll.h',
'io4.c',
'io4.h',
'zinput.c',
'zinput.h',
],
)

112
swdchook/swdc-dll.c Normal file
View File

@ -0,0 +1,112 @@
#include <windows.h>
#include <assert.h>
#include <stdlib.h>
#include "swdchook/swdc-dll.h"
#include "util/dll-bind.h"
#include "util/dprintf.h"
const struct dll_bind_sym swdc_dll_syms[] = {
{
.sym = "swdc_io_init",
.off = offsetof(struct swdc_dll, init),
}, {
.sym = "swdc_io_poll",
.off = offsetof(struct swdc_dll, poll),
}, {
.sym = "swdc_io_get_opbtns",
.off = offsetof(struct swdc_dll, get_opbtns),
}, {
.sym = "swdc_io_get_gamebtns",
.off = offsetof(struct swdc_dll, get_gamebtns),
}, {
.sym = "swdc_io_get_analogs",
.off = offsetof(struct swdc_dll, get_analogs),
}
};
struct swdc_dll swdc_dll;
// Copypasta DLL binding and diagnostic message boilerplate.
// Not much of this lends itself to being easily factored out. Also there
// will be a lot of API-specific branching code here eventually as new API
// versions get defined, so even though these functions all look the same
// now this won't remain the case forever.
HRESULT swdc_dll_init(const struct swdc_dll_config *cfg, HINSTANCE self)
{
uint16_t (*get_api_version)(void);
const struct dll_bind_sym *sym;
HINSTANCE owned;
HINSTANCE src;
HRESULT hr;
assert(cfg != NULL);
assert(self != NULL);
if (cfg->path[0] != L'\0') {
owned = LoadLibraryW(cfg->path);
if (owned == NULL) {
hr = HRESULT_FROM_WIN32(GetLastError());
dprintf("SWDC IO: Failed to load IO DLL: %lx: %S\n",
hr,
cfg->path);
goto end;
}
dprintf("SWDC IO: Using custom IO DLL: %S\n", cfg->path);
src = owned;
} else {
owned = NULL;
src = self;
}
get_api_version = (void *) GetProcAddress(src, "swdc_io_get_api_version");
if (get_api_version != NULL) {
swdc_dll.api_version = get_api_version();
} else {
swdc_dll.api_version = 0x0100;
dprintf("Custom IO DLL does not expose swdc_io_get_api_version, "
"assuming API version 1.0.\n"
"Please ask the developer to update their DLL.\n");
}
if (swdc_dll.api_version >= 0x0200) {
hr = E_NOTIMPL;
dprintf("SWDC IO: Custom IO DLL implements an unsupported "
"API version (%#04x). Please update Segatools.\n",
swdc_dll.api_version);
goto end;
}
sym = swdc_dll_syms;
hr = dll_bind(&swdc_dll, src, &sym, _countof(swdc_dll_syms));
if (FAILED(hr)) {
if (src != self) {
dprintf("SWDC IO: Custom IO DLL does not provide function "
"\"%s\". Please contact your IO DLL's developer for "
"further assistance.\n",
sym->sym);
goto end;
} else {
dprintf("Internal error: could not reflect \"%s\"\n", sym->sym);
}
}
owned = NULL;
end:
if (owned != NULL) {
FreeLibrary(owned);
}
return hr;
}

22
swdchook/swdc-dll.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <windows.h>
#include "swdcio/swdcio.h"
struct swdc_dll {
uint16_t api_version;
HRESULT (*init)(void);
HRESULT (*poll)(void);
void (*get_opbtns)(uint8_t *opbtn);
void (*get_gamebtns)(uint16_t *gamebtn);
void (*get_analogs)(struct swdc_io_analog_state *out);
};
struct swdc_dll_config {
wchar_t path[MAX_PATH];
};
extern struct swdc_dll swdc_dll;
HRESULT swdc_dll_init(const struct swdc_dll_config *cfg, HINSTANCE self);

19
swdchook/swdchook.def Normal file
View File

@ -0,0 +1,19 @@
LIBRARY swdchook
EXPORTS
aime_io_get_api_version
aime_io_init
aime_io_led_set_color
aime_io_nfc_get_aime_id
aime_io_nfc_get_felica_id
aime_io_nfc_poll
amDllVideoClose @2
amDllVideoGetVBiosVersion @4
amDllVideoOpen @1
amDllVideoSetResolution @3
swdc_io_get_api_version
swdc_io_init
swdc_io_poll
swdc_io_get_opbtns
swdc_io_get_gamebtns
swdc_io_get_analogs

186
swdchook/zinput.c Normal file
View File

@ -0,0 +1,186 @@
#include <windows.h>
#include <dinput.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "swdchook/config.h"
#include "swdchook/zinput.h"
#include "hook/table.h"
#include "util/dprintf.h"
HRESULT WINAPI hook_DirectInput8Create(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPUNKNOWN punkOuter);
static unsigned long WINAPI hook_AddRef(IUnknown *self);
static unsigned long WINAPI hook_Release(IUnknown *self);
static HRESULT WINAPI hook_CreateDevice(
IDirectInput8W *self,
REFGUID rguid,
LPDIRECTINPUTDEVICE8W * lplpDirectInputDevice,
LPUNKNOWN pUnkOuter);
static HRESULT WINAPI hook_EnumDevices(
IDirectInput8W *self,
DWORD dwDevType,
LPDIENUMDEVICESCALLBACKW lpCallback,
LPVOID pvRef,
DWORD dwFlags);
static HRESULT WINAPI hook_SetDataFormat(
IDirectInputDevice8W *self,
LPCDIDATAFORMAT lpdf);
static HRESULT WINAPI hook_SetCooperativeLevel(
IDirectInputDevice8W *self,
HWND hwnd,
DWORD flags);
static HRESULT WINAPI hook_Acquire(IDirectInputDevice8W *self);
static HRESULT WINAPI hook_Unacquire(IDirectInputDevice8W *self);
static HRESULT WINAPI hook_GetDeviceState(
IDirectInputDevice8W *self,
DWORD cbData,
LPVOID lpvData);
static const IDirectInput8WVtbl api_vtbl = {
.AddRef = (void *) hook_AddRef,
.Release = (void *) hook_Release,
.CreateDevice = hook_CreateDevice,
.EnumDevices = hook_EnumDevices,
};
static const IDirectInput8W api = { (void *) &api_vtbl };
static const IDirectInputDevice8WVtbl dev_vtbl = {
.AddRef = (void *) hook_AddRef,
.Release = (void *) hook_Release,
.SetDataFormat = hook_SetDataFormat,
.SetCooperativeLevel= hook_SetCooperativeLevel,
.Acquire = hook_Acquire,
.Unacquire = hook_Unacquire,
.GetDeviceState = hook_GetDeviceState,
};
static const IDirectInputDevice8W dev = { (void *) &dev_vtbl };
static const struct hook_symbol zinput_hook_syms[] = {
{
.name = "DirectInput8Create",
.patch = hook_DirectInput8Create,
}
};
HRESULT zinput_hook_init(struct zinput_config *cfg)
{
assert(cfg != NULL);
if (!cfg->enable) {
return S_FALSE;
}
hook_table_apply(
NULL,
"dinput8.dll",
zinput_hook_syms,
_countof(zinput_hook_syms));
return S_OK;
}
HRESULT WINAPI hook_DirectInput8Create(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPUNKNOWN punkOuter)
{
dprintf("ZInput: Blocking built-in DirectInput support\n");
*ppvOut = (void *) &api;
return S_OK;
}
static unsigned long WINAPI hook_AddRef(IUnknown *self)
{
return 1;
}
static unsigned long WINAPI hook_Release(IUnknown *self)
{
return 1;
}
static HRESULT WINAPI hook_CreateDevice(
IDirectInput8W *self,
REFGUID rguid,
LPDIRECTINPUTDEVICE8W *lplpDirectInputDevice,
LPUNKNOWN pUnkOuter)
{
dprintf("ZInput: %s\n", __func__);
*lplpDirectInputDevice = (void *) &dev;
return S_OK;
}
static HRESULT WINAPI hook_EnumDevices(
IDirectInput8W *self,
DWORD dwDevType,
LPDIENUMDEVICESCALLBACKW lpCallback,
LPVOID pvRef,
DWORD dwFlags)
{
dprintf("ZInput: %s\n", __func__);
return S_OK;
}
static HRESULT WINAPI hook_SetDataFormat(
IDirectInputDevice8W *self,
LPCDIDATAFORMAT lpdf)
{
dprintf("ZInput: %s\n", __func__);
return S_OK;
}
static HRESULT WINAPI hook_SetCooperativeLevel(
IDirectInputDevice8W *self,
HWND hwnd,
DWORD flags)
{
dprintf("ZInput: %s\n", __func__);
return S_OK;
}
static HRESULT WINAPI hook_Acquire(IDirectInputDevice8W *self)
{
return S_OK;
}
static HRESULT WINAPI hook_Unacquire(IDirectInputDevice8W *self)
{
return S_OK;
}
static HRESULT WINAPI hook_GetDeviceState(
IDirectInputDevice8W *self,
DWORD cbData,
LPVOID lpvData)
{
memset(lpvData, 0, cbData);
return S_OK;
}

11
swdchook/zinput.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <windows.h>
#include <stdbool.h>
struct zinput_config {
bool enable;
};
HRESULT zinput_hook_init(struct zinput_config *cfg);