From c19d14058949564cdd8cbfa75ad2349c1ade7403 Mon Sep 17 00:00:00 2001 From: Tau Date: Sat, 4 May 2019 13:12:20 -0400 Subject: [PATCH] Wire up libamex config --- amex/amex.c | 44 ++++++++++++++++++++++++++++----- amex/amex.h | 6 ++++- amex/ds.c | 29 +++++++++++----------- amex/ds.h | 4 ++- amex/eeprom.c | 11 +++++++-- amex/eeprom.h | 4 ++- amex/gpio.c | 60 ++++++++++++++++++++++++++++++--------------- amex/gpio.h | 4 ++- amex/jvs.c | 25 ++++++++++++++++--- amex/jvs.h | 4 ++- amex/nvram.c | 2 +- amex/nvram.h | 2 +- amex/sram.c | 10 ++++++-- amex/sram.h | 4 ++- chunihook/dllmain.c | 10 ++++++-- divahook/dllmain.c | 11 +++++++-- idzhook/dllmain.c | 11 +++++++-- minihook/dllmain.c | 9 +++++-- 18 files changed, 186 insertions(+), 64 deletions(-) diff --git a/amex/amex.c b/amex/amex.c index f30195b..e7d33bc 100644 --- a/amex/amex.c +++ b/amex/amex.c @@ -1,3 +1,5 @@ +#include + #include "amex/amex.h" #include "amex/ds.h" #include "amex/eeprom.h" @@ -5,11 +7,41 @@ #include "amex/jvs.h" #include "amex/sram.h" -void amex_hook_init(void) +HRESULT amex_hook_init(const struct amex_config *cfg) { - ds_hook_init(); - eeprom_hook_init(); - gpio_hook_init(); - jvs_hook_init(); - sram_hook_init(); + HRESULT hr; + + assert(cfg != NULL); + + hr = ds_hook_init(&cfg->ds); + + if (FAILED(hr)) { + return hr; + } + + hr = eeprom_hook_init(&cfg->eeprom); + + if (FAILED(hr)) { + return hr; + } + + hr = gpio_hook_init(&cfg->gpio); + + if (FAILED(hr)) { + return hr; + } + + hr = jvs_hook_init(&cfg->jvs); + + if (FAILED(hr)) { + return hr; + } + + hr = sram_hook_init(&cfg->sram); + + if (FAILED(hr)) { + return hr; + } + + return S_OK; } diff --git a/amex/amex.h b/amex/amex.h index 63591e0..85dca7e 100644 --- a/amex/amex.h +++ b/amex/amex.h @@ -1,3 +1,7 @@ #pragma once -void amex_hook_init(void); +#include + +#include "amex/cfg.h" + +HRESULT amex_hook_init(const struct amex_config *cfg); diff --git a/amex/ds.c b/amex/ds.c index 916772d..4cbe365 100644 --- a/amex/ds.c +++ b/amex/ds.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include "amex/ds.h" @@ -46,29 +45,29 @@ static HRESULT ds_ioctl_get_geometry(struct irp *irp); static HRESULT ds_ioctl_setup(struct irp *irp); static HRESULT ds_ioctl_read_sector(struct irp *irp); -static const char ds_serial_file[] = "DEVICE/pcbid.txt"; static struct ds_eeprom ds_eeprom; static HANDLE ds_fd; -HRESULT ds_hook_init(void) +HRESULT ds_hook_init(const struct ds_config *cfg) { HRESULT hr; - int region; - FILE *f; - region = 0x01; /* Japan; use this default if ds.txt read fails */ - memset(&ds_eeprom, 0, sizeof(ds_eeprom)); + assert(cfg != NULL); - f = fopen(ds_serial_file, "r"); - - if (f != NULL) { - fscanf(f, "%16s %x", ds_eeprom.serial_no, ®ion); - fclose(f); - } else { - dprintf("Failed to open %s\n", ds_serial_file); + if (!cfg->enable) { + return S_FALSE; } - ds_eeprom.region = region; + memset(&ds_eeprom, 0, sizeof(ds_eeprom)); + + wcstombs_s( + NULL, + ds_eeprom.serial_no, + _countof(ds_eeprom.serial_no), + cfg->serial_no, + _countof(cfg->serial_no) - 1); + + ds_eeprom.region = cfg->region; ds_eeprom.crc32 = crc32(&ds_eeprom.unk_04, 0x1C, 0); hr = iohook_push_handler(ds_handle_irp); diff --git a/amex/ds.h b/amex/ds.h index 6bca367..33dd232 100644 --- a/amex/ds.h +++ b/amex/ds.h @@ -2,6 +2,8 @@ #include +#include "amex/cfg.h" + DEFINE_GUID( ds_guid, 0x279A9F67, @@ -9,4 +11,4 @@ DEFINE_GUID( 0x41C9, 0xA4, 0xC4, 0xDF, 0xDB, 0x8A, 0xE8, 0xE5, 0xE0); -HRESULT ds_hook_init(void); +HRESULT ds_hook_init(const struct ds_config *cfg); diff --git a/amex/eeprom.c b/amex/eeprom.c index da173cc..91b863f 100644 --- a/amex/eeprom.c +++ b/amex/eeprom.c @@ -10,6 +10,7 @@ #include +#include "amex/cfg.h" #include "amex/eeprom.h" #include "amex/nvram.h" @@ -29,11 +30,17 @@ static HRESULT eeprom_ioctl_get_geometry(struct irp *irp); static HANDLE eeprom_file; -HRESULT eeprom_hook_init(void) +HRESULT eeprom_hook_init(const struct eeprom_config *cfg) { HRESULT hr; - hr = nvram_open_file(&eeprom_file, L"DEVICE\\eeprom.bin", 0x2000); + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + hr = nvram_open_file(&eeprom_file, cfg->path, 0x2000); if (FAILED(hr)) { return hr; diff --git a/amex/eeprom.h b/amex/eeprom.h index 0d1802d..385ed7b 100644 --- a/amex/eeprom.h +++ b/amex/eeprom.h @@ -2,6 +2,8 @@ #include +#include "amex/cfg.h" + DEFINE_GUID( eeprom_guid, 0xB7970F0C, @@ -9,4 +11,4 @@ DEFINE_GUID( 0x45FF, 0x96, 0x18, 0x0A, 0x24, 0x00, 0x94, 0xB2, 0x71); -HRESULT eeprom_hook_init(void); +HRESULT eeprom_hook_init(const struct eeprom_config *cfg); diff --git a/amex/gpio.c b/amex/gpio.c index 0934e7b..96c4dc8 100644 --- a/amex/gpio.c +++ b/amex/gpio.c @@ -54,10 +54,6 @@ static HRESULT gpio_ioctl_get_dipsw(struct irp *irp); static HRESULT gpio_ioctl_describe(struct irp *irp); static HRESULT gpio_ioctl_set_leds(struct irp *irp); -static HANDLE gpio_fd; -static uint8_t gpio_dipsw; -static const char gpio_dipsw_file[] = "DEVICE/dipsw.txt"; - static const struct gpio_ports gpio_ports = { .ports = { { @@ -75,27 +71,35 @@ static const struct gpio_ports gpio_ports = { static_assert(sizeof(gpio_ports) == 129, "GPIO port map size"); -void gpio_hook_init(void) +static HANDLE gpio_fd; +static struct gpio_config gpio_config; + +HRESULT gpio_hook_init(const struct gpio_config *cfg) { - FILE *f; - int ival; + HRESULT hr; - f = fopen(gpio_dipsw_file, "r"); + assert(cfg != NULL); - if (f != NULL) { - ival = 0; - fscanf(f, "%02x", &ival); - gpio_dipsw = ival; - fclose(f); - - dprintf("Set DIPSW to %02x\n", gpio_dipsw); - } else { - dprintf("Failed to open %s\n", gpio_dipsw_file); + if (!cfg->enable) { + return S_FALSE; } + memcpy(&gpio_config, cfg, sizeof(*cfg)); + gpio_fd = iohook_open_dummy_fd(); - iohook_push_handler(gpio_handle_irp); - setupapi_add_phantom_dev(&gpio_guid, L"$gpio"); + hr = iohook_push_handler(gpio_handle_irp); + + if (FAILED(hr)) { + return hr; + } + + hr = setupapi_add_phantom_dev(&gpio_guid, L"$gpio"); + + if (FAILED(hr)) { + return hr; + } + + return S_OK; } static HRESULT gpio_handle_irp(struct irp *irp) @@ -161,8 +165,16 @@ static HRESULT gpio_handle_ioctl(struct irp *irp) static HRESULT gpio_ioctl_get_dipsw(struct irp *irp) { uint32_t dipsw; + size_t i; + + dipsw = 0; + + for (i = 0 ; i < 8 ; i++) { + if (gpio_config.dipsw[i]) { + dipsw |= 1 << i; + } + } - dipsw = gpio_dipsw; //dprintf("GPIO: Get dipsw %08x\n", dipsw); return iobuf_write_le32(&irp->read, dipsw); @@ -177,6 +189,14 @@ static HRESULT gpio_ioctl_get_psw(struct irp *irp) /* Bit 0 == SW1 == Alt. Test */ /* Bit 1 == SW2 == Alt. Service */ + if (gpio_config.vk_sw1 && (GetAsyncKeyState(gpio_config.vk_sw1) & 0x8000)) { + result |= 1 << 0; + } + + if (gpio_config.vk_sw2 && (GetAsyncKeyState(gpio_config.vk_sw2) & 0x8000)) { + result |= 1 << 1; + } + return iobuf_write_le32(&irp->read, result); } diff --git a/amex/gpio.h b/amex/gpio.h index e09da75..d108262 100644 --- a/amex/gpio.h +++ b/amex/gpio.h @@ -2,6 +2,8 @@ #include +#include "amex/cfg.h" + DEFINE_GUID( gpio_guid, 0xE9A26688, @@ -9,4 +11,4 @@ DEFINE_GUID( 0x44FA, 0xBF, 0xEE, 0x59, 0xDD, 0x16, 0x15, 0x56, 0x6C); -void gpio_hook_init(void); +HRESULT gpio_hook_init(const struct gpio_config *cfg); diff --git a/amex/jvs.c b/amex/jvs.c index f220bf7..f396962 100644 --- a/amex/jvs.c +++ b/amex/jvs.c @@ -37,11 +37,30 @@ static HRESULT jvs_ioctl_transact(struct irp *irp); static HANDLE jvs_fd; static struct jvs_node *jvs_root; -void jvs_hook_init(void) +HRESULT jvs_hook_init(const struct jvs_config *cfg) { + HRESULT hr; + + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + jvs_fd = iohook_open_dummy_fd(); - iohook_push_handler(jvs_handle_irp); - setupapi_add_phantom_dev(&jvs_guid, L"$jvs"); + hr = iohook_push_handler(jvs_handle_irp); + + if (FAILED(hr)) { + return hr; + } + + hr = setupapi_add_phantom_dev(&jvs_guid, L"$jvs"); + + if (FAILED(hr)) { + return hr; + } + + return S_OK; } void jvs_attach(struct jvs_node *root) diff --git a/amex/jvs.h b/amex/jvs.h index 2ab940b..0985e74 100644 --- a/amex/jvs.h +++ b/amex/jvs.h @@ -2,6 +2,8 @@ #include +#include "amex/cfg.h" + #include "jvs/jvs-bus.h" DEFINE_GUID( @@ -11,5 +13,5 @@ DEFINE_GUID( 0x4288, 0xAA, 0x00, 0x6C, 0x00, 0xD7, 0x67, 0xBD, 0xBF); -void jvs_hook_init(void); +HRESULT jvs_hook_init(const struct jvs_config *cfg); void jvs_attach(struct jvs_node *root); diff --git a/amex/nvram.c b/amex/nvram.c index 5e4286a..bbade62 100644 --- a/amex/nvram.c +++ b/amex/nvram.c @@ -8,7 +8,7 @@ #include "util/dprintf.h" -HRESULT nvram_open_file(HANDLE *out, wchar_t *path, size_t size) +HRESULT nvram_open_file(HANDLE *out, const wchar_t *path, size_t size) { LARGE_INTEGER cur_size; LARGE_INTEGER pos; diff --git a/amex/nvram.h b/amex/nvram.h index e54f5cc..ae6f532 100644 --- a/amex/nvram.h +++ b/amex/nvram.h @@ -4,4 +4,4 @@ #include -HRESULT nvram_open_file(HANDLE *out, wchar_t *path, size_t size); +HRESULT nvram_open_file(HANDLE *out, const wchar_t *path, size_t size); diff --git a/amex/sram.c b/amex/sram.c index c069c18..a9eb182 100644 --- a/amex/sram.c +++ b/amex/sram.c @@ -27,11 +27,17 @@ static HRESULT sram_ioctl_get_geometry(struct irp *irp); static HANDLE sram_file; -HRESULT sram_hook_init(void) +HRESULT sram_hook_init(const struct sram_config *cfg) { HRESULT hr; - hr = nvram_open_file(&sram_file, L"DEVICE\\sram.bin", 0x80000); + assert(cfg != NULL); + + if (!cfg->enable) { + return S_FALSE; + } + + hr = nvram_open_file(&sram_file, cfg->path, 0x80000); if (FAILED(hr)) { return hr; diff --git a/amex/sram.h b/amex/sram.h index f2c4c3b..f2763d2 100644 --- a/amex/sram.h +++ b/amex/sram.h @@ -2,6 +2,8 @@ #include +#include "amex/cfg.h" + DEFINE_GUID( sram_guid, 0x741B5FCA, @@ -9,4 +11,4 @@ DEFINE_GUID( 0x4443, 0xA7, 0xA0, 0x57, 0xCA, 0x7B, 0x50, 0x6A, 0x49); -HRESULT sram_hook_init(void); +HRESULT sram_hook_init(const struct sram_config *cfg); diff --git a/chunihook/dllmain.c b/chunihook/dllmain.c index 6f2dc89..dcbea40 100644 --- a/chunihook/dllmain.c +++ b/chunihook/dllmain.c @@ -4,6 +4,7 @@ #include #include "amex/amex.h" +#include "amex/cfg.h" #include "chunihook/jvs.h" #include "chunihook/slider.h" @@ -26,6 +27,7 @@ static process_entry_t chuni_startup; static DWORD CALLBACK chuni_pre_startup(void) { + struct amex_config amex_cfg; HMODULE d3dc; dprintf("--- Begin chuni_pre_startup ---\n"); @@ -53,12 +55,16 @@ static DWORD CALLBACK chuni_pre_startup(void) /* Initialize AMEX emulation */ - amex_hook_init(); + amex_config_load(&amex_cfg, L".\\segatools.ini"); + amex_hook_init(&amex_cfg); /* Initialize Chunithm board emulation */ + if (amex_cfg.jvs.enable) { + chunithm_jvs_init(); + } + slider_hook_init(); - chunithm_jvs_init(); /* Initialize debug helpers */ diff --git a/divahook/dllmain.c b/divahook/dllmain.c index 53bc9f1..77c8c03 100644 --- a/divahook/dllmain.c +++ b/divahook/dllmain.c @@ -4,6 +4,7 @@ #include #include "amex/amex.h" +#include "amex/cfg.h" #include "board/sg-reader.h" @@ -26,6 +27,8 @@ static process_entry_t diva_startup; static DWORD CALLBACK diva_pre_startup(void) { + struct amex_config amex_cfg; + dprintf("--- Begin diva_pre_startup ---\n"); /* Hook Win32 APIs */ @@ -40,11 +43,15 @@ static DWORD CALLBACK diva_pre_startup(void) /* Initialize AMEX emulation */ - amex_hook_init(); + amex_config_load(&amex_cfg, L".\\segatools.ini"); + amex_hook_init(&amex_cfg); /* Initialize Project Diva I/O board emulation */ - diva_jvs_init(); + if (amex_cfg.jvs.enable) { + diva_jvs_init(); + } + sg_reader_hook_init(10); slider_hook_init(); diff --git a/idzhook/dllmain.c b/idzhook/dllmain.c index e806c4c..ef53537 100644 --- a/idzhook/dllmain.c +++ b/idzhook/dllmain.c @@ -4,6 +4,7 @@ #include #include "amex/amex.h" +#include "amex/cfg.h" #include "board/sg-reader.h" @@ -25,6 +26,8 @@ static process_entry_t idz_startup; static DWORD CALLBACK idz_pre_startup(void) { + struct amex_config amex_cfg; + dprintf("--- Begin idz_pre_startup ---\n"); /* Hook Win32 APIs */ @@ -39,13 +42,17 @@ static DWORD CALLBACK idz_pre_startup(void) /* Initialize AMEX emulation */ - amex_hook_init(); + amex_config_load(&amex_cfg, L".\\segatools.ini"); + amex_hook_init(&amex_cfg); /* Initialize Initial D Zero I/O board emulation */ - idz_jvs_init(); sg_reader_hook_init(10); + if (amex_cfg.jvs.enable) { + idz_jvs_init(); + } + /* Initialize debug helpers */ spike_hook_init("idzspike.txt"); diff --git a/minihook/dllmain.c b/minihook/dllmain.c index 556f320..6ca7062 100644 --- a/minihook/dllmain.c +++ b/minihook/dllmain.c @@ -1,5 +1,6 @@ #include +#include "amex/cfg.h" #include "amex/ds.h" #include "hook/process.h" @@ -14,13 +15,17 @@ static process_entry_t app_startup; static DWORD CALLBACK app_pre_startup(void) { + struct ds_config ds_cfg; + dprintf("--- Begin %s ---\n", __func__); - spike_hook_init("minispike.txt"); + ds_config_load(&ds_cfg, L".\\segatools.ini"); clock_hook_init(); - ds_hook_init(); + ds_hook_init(&ds_cfg); nusec_hook_init(); + spike_hook_init("minispike.txt"); + dprintf("--- End %s ---\n", __func__); return app_startup();