From a86523e03d146014e5b5be1703a05df7908e16ac Mon Sep 17 00:00:00 2001 From: condret Date: Thu, 17 Oct 2024 05:34:56 +0200 Subject: [PATCH] Start implementing Joypad --- include/gb.h | 9 ++++++ io/joypad.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++ io/timers.c | 3 -- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 io/joypad.c diff --git a/include/gb.h b/include/gb.h index 0a08e93..39922d1 100644 --- a/include/gb.h +++ b/include/gb.h @@ -24,6 +24,15 @@ GBTimers *gb_timers_open(RIO *io); void gb_timers_close(RIO *io, GBTimers *timers); void gb_timers_update(GBTimers *timers, ut32 cycles); +typedef struct gb_joypad_t { + ut8 *keys; + int fd; + ut8 data; + ut8 odata; +} GBJoypad; + +GBJoypad *gb_joypad_open(RIO *io) + //int gb_mbc1_open(RIO *io, char *path); extern RIOPlugin r_io_plugin_gb_timers; diff --git a/io/joypad.c b/io/joypad.c new file mode 100644 index 0000000..e4c3c95 --- /dev/null +++ b/io/joypad.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + +static bool __check(RIO *io, const char *pathname, bool many) { + return r_str_startswith (pathname, "gb_joypad://"); +} + +static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { + if (!r_str_startswith (pathname, "gb_joypad://")) { + return NULL; + } + GBJoypad *joypad = NULL; + sscanf (pathname, "gb_joybad://%p", &joypad); + RIODesc *desc = r_io_desc_new (io, &r_io_plugin_gb_joypad, pathname, + R_PERM_RWX, mode, timers); + return desc; +} + +static int __read(RIO *io, RIODesc *desc, ut8 *buf, int len) { + GBJoypad *joypad = (GBJoypad *)desc->data; + if (!len || (joypad->odata & 0x10)) { + return 0; + } + joypad->data = buf[0] & 0xf; + joypad->odata |= 0x10; + return 1; +} + +static int __write(RIO *io, RIODesc *desc, const ut8 *buf, int len) { + GBJoypad *joypad = (GBJoypad *)desc->data; + if (!len || (joypad->odata & 0x10)) { + return 0; + } + joypad->odata |= 0x10; + return 1; +} + +static bool __close(RIODesc *desc) { + return true; +} + +RIOPlugin r_io_plugin_gb_joypad = { + .meta = { + .name = "gb_joypad", + .desc = "gb_joypad", + .license = "LGPL", + }, + .uris = "gb_joypad://", + .open = __open, + .close = __close, + .read = __read, + .check = __check, + .seek = __lseek, + .write = __write, +}; + +GBJoypad *gb_joypad_open (RIO *io) { + joypad = R_NEW0 (GBJoypad); + if (!joypad) { + return NULL; + } + char uri[64]; + memset (uri, 0x00, sizeof (char) * 64); + sprintf (uri, "gb_joypad://%p", joypad); + RIODesc *desc = r_io_desc_open_plugin (io, &r_io_plugin_gb_joypad, + uri, R_PERM_RWX, 0); + if (!desc) { + free (joypad); + return NULL; + } + joypad->fd = desc->fd; + joypad->keys = SDL_GetKeyboardState (NULL); + return joypad; +} diff --git a/io/timers.c b/io/timers.c index 6198abc..a0b44c9 100644 --- a/io/timers.c +++ b/io/timers.c @@ -38,9 +38,6 @@ static RIODesc *__open(RIO *io, const char *pathname, int rw, int mode) { sscanf (pathname, "gb_timers://%p", &timers); RIODesc *desc = r_io_desc_new (io, &r_io_plugin_gb_timers, pathname, R_PERM_RWX, mode, timers); - if (!desc) { - return NULL; - } return desc; }