From e4081c057ffa703a455315c3f1056a96edf0e278 Mon Sep 17 00:00:00 2001 From: Will Toohey Date: Sun, 4 Oct 2020 16:24:17 +1000 Subject: [PATCH] Add jbio-magicbox.dll --- GNUmakefile | 2 +- Module.mk | 1 + doc/jbhook/jbio-magicbox.md | 11 ++ src/imports/ch341.h | 12 ++ src/imports/import_32_indep_ch341.def | 7 ++ src/main/jbio-magicbox/Module.mk | 11 ++ src/main/jbio-magicbox/jbio-magicbox.def | 11 ++ src/main/jbio-magicbox/jbio.c | 139 +++++++++++++++++++++++ 8 files changed, 193 insertions(+), 1 deletion(-) create mode 100644 doc/jbhook/jbio-magicbox.md create mode 100644 src/imports/ch341.h create mode 100644 src/imports/import_32_indep_ch341.def create mode 100644 src/main/jbio-magicbox/Module.mk create mode 100644 src/main/jbio-magicbox/jbio-magicbox.def create mode 100644 src/main/jbio-magicbox/jbio.c diff --git a/GNUmakefile b/GNUmakefile index 8cc1cc4..99cc225 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -256,7 +256,7 @@ impdef_$1_$2_$3 ?= src/imports/import_$1_$2_$3.def $$(bindir_$1_$2)/lib$3.a: $$(impdef_$1_$2_$3) | $$(bindir_$1_$2) $(V)echo ... $$@ [dlltool] - $(V)$$(toolchain_$1)dlltool -l $$@ -d $$< + $(V)$$(toolchain_$1)dlltool --kill-at -l $$@ -d $$< endef diff --git a/Module.mk b/Module.mk index 09d2cf2..3625ff3 100644 --- a/Module.mk +++ b/Module.mk @@ -130,6 +130,7 @@ include src/main/iidxio-ezusb2/Module.mk include src/main/iidxiotest/Module.mk include src/main/inject/Module.mk include src/main/jbio/Module.mk +include src/main/jbio-magicbox/Module.mk include src/main/jbiotest/Module.mk include src/main/jbhook/Module.mk include src/main/jbhook1/Module.mk diff --git a/doc/jbhook/jbio-magicbox.md b/doc/jbhook/jbio-magicbox.md new file mode 100644 index 0000000..253c07e --- /dev/null +++ b/doc/jbhook/jbio-magicbox.md @@ -0,0 +1,11 @@ +This library talks to a MagicBox cab's IO and implements the jbio API of BT5. +Thus, it allows you to use a MagicBox cab with *any* version of jubeat that is supported by BT5. + +# Setup +* Rename jbio-magicbox.dll to jbio.dll. +* Ensure that your gamestart.bat actually injects the appropriate jbhook dll +for example: +``` +launcher -K jbhook.dll jubeat.dll ...* +``` +or that the application otherwise uses jbio.dll diff --git a/src/imports/ch341.h b/src/imports/ch341.h new file mode 100644 index 0000000..6115019 --- /dev/null +++ b/src/imports/ch341.h @@ -0,0 +1,12 @@ +#ifndef IMPORTS_CH341_H +#define IMPORTS_CH341_H + +// The bare minimum definitions to implement jbio-magicbox +// There is a full .h for this DLL floating around online, if you care + +HANDLE WINAPI CH341OpenDevice(ULONG iIndex); +VOID WINAPI CH341CloseDevice(ULONG iIndex); +BOOL WINAPI CH341EppReadData(ULONG iIndex, PVOID oBuffer, PULONG ioLength); +BOOL WINAPI CH341EppSetAddr(ULONG iIndex, UCHAR iAddr); + +#endif diff --git a/src/imports/import_32_indep_ch341.def b/src/imports/import_32_indep_ch341.def new file mode 100644 index 0000000..d84f5f8 --- /dev/null +++ b/src/imports/import_32_indep_ch341.def @@ -0,0 +1,7 @@ +LIBRARY ch341dll + +EXPORTS + CH341OpenDevice@4=CH341OpenDevice + CH341CloseDevice@4=CH341CloseDevice + CH341EppReadData@12=CH341EppReadData + CH341EppSetAddr@8=CH341EppSetAddr diff --git a/src/main/jbio-magicbox/Module.mk b/src/main/jbio-magicbox/Module.mk new file mode 100644 index 0000000..1d24eb4 --- /dev/null +++ b/src/main/jbio-magicbox/Module.mk @@ -0,0 +1,11 @@ +dlls += jbio-magicbox +imps += ch341 + +deplibs_jbio-magicbox := \ + ch341 \ + +src_jbio-magicbox := \ + jbio.c \ + +libs_jbio-magicbox := \ + util \ diff --git a/src/main/jbio-magicbox/jbio-magicbox.def b/src/main/jbio-magicbox/jbio-magicbox.def new file mode 100644 index 0000000..a23c2a1 --- /dev/null +++ b/src/main/jbio-magicbox/jbio-magicbox.def @@ -0,0 +1,11 @@ +LIBRARY jbio + +EXPORTS + jb_io_fini + jb_io_get_panel_inputs + jb_io_get_sys_inputs + jb_io_init + jb_io_read_inputs + jb_io_set_loggers + jb_io_set_rgb_led + jb_io_write_outputs diff --git a/src/main/jbio-magicbox/jbio.c b/src/main/jbio-magicbox/jbio.c new file mode 100644 index 0000000..b890953 --- /dev/null +++ b/src/main/jbio-magicbox/jbio.c @@ -0,0 +1,139 @@ +// This implementation is ported from the device.dll source graciously +// provided by zyp + +#include + +#include "bemanitools/jbio.h" +#include "imports/ch341.h" +#include "util/defs.h" + +static uint16_t jb_io_panels; +static uint8_t jb_io_sys_buttons; + +static bool is_initialized = false; + +log_formatter_t jb_io_log_misc; +log_formatter_t jb_io_log_info; +log_formatter_t jb_io_log_warning; +log_formatter_t jb_io_log_fatal; + +void jb_io_set_loggers( + log_formatter_t misc, + log_formatter_t info, + log_formatter_t warning, + log_formatter_t fatal) +{ + jb_io_log_misc = misc; + jb_io_log_info = info; + jb_io_log_warning = warning; + jb_io_log_fatal = fatal; +} + +bool jb_io_init( + thread_create_t thread_create, + thread_join_t thread_join, + thread_destroy_t thread_destroy) +{ + if(CH341OpenDevice(0) < 0) { + jb_io_log_warning("jbio", "Can't open CH341 device.\n"); + return false; + } + + is_initialized = true; + + return true; +} + +void jb_io_fini(void) +{ + CH341CloseDevice(0); +} + +static const uint32_t magic_panel_mappings[] = { + (1 << 0x08), + (1 << 0x0a), + (1 << 0x0c), + (1 << 0x0e), + (1 << 0x07), + (1 << 0x05), + (1 << 0x03), + (1 << 0x01), + (1 << 0x09), + (1 << 0x0b), + (1 << 0x0d), + (1 << 0x0f), + (1 << 0x06), + (1 << 0x04), + (1 << 0x02), + (1 << 0x00), +}; + +static const uint32_t magic_sys_mappings[] = { + (1 << 0x11), // TEST + (1 << 0x10), // SERVICE + (1 << 0x13), // COIN +}; + +bool jb_io_read_inputs(void) +{ + // Read IO board + unsigned long n; + union { + uint32_t dword; + uint8_t bytes[4]; + } input; + + input.dword = -1; + jb_io_panels = 0; + jb_io_sys_buttons = 0; + + CH341EppSetAddr(0, 255); + CH341EppSetAddr(0, 1); + CH341EppSetAddr(0, 255); + CH341EppSetAddr(0, 0); + n = 1; + CH341EppReadData(0, &input.bytes[0], &n); + CH341EppSetAddr(0, 255); + CH341EppSetAddr(0, 0); + n = 1; + CH341EppReadData(0, &input.bytes[1], &n); + CH341EppSetAddr(0, 255); + CH341EppSetAddr(0, 0); + n = 1; + CH341EppReadData(0, &input.bytes[2], &n); + CH341EppSetAddr(0, 255); + + for (uint8_t i = 0; i < lengthof(magic_panel_mappings); i++) { + if((input.dword & magic_panel_mappings[i]) == 0) { + jb_io_panels |= 1 << i; + } + } + for (uint8_t i = 0; i < lengthof(magic_sys_mappings); i++) { + if((input.dword & magic_sys_mappings[i]) == 0) { + jb_io_sys_buttons |= 1 << i; + } + } + + return true; +} + +bool jb_io_write_outputs(void) +{ + return true; +} + +uint8_t jb_io_get_sys_inputs(void) +{ + return jb_io_sys_buttons; +} + +uint16_t jb_io_get_panel_inputs(void) +{ + return jb_io_panels; +} + +void jb_io_set_rgb_led(enum jb_io_rgb_led unit, uint8_t r, uint8_t g, uint8_t b) +{ + // I mean I guess there's reactive LEDs on the sides? I'm not going to the + // effort to work out if they're controllable or not +}