From 5e138304d80f8abce63cd604fb3143fac7ee96ff Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 7 May 2020 23:30:08 -0400 Subject: [PATCH 1/3] investigate TAS-ing this --- src/pc/controller/controller_recorded_tas.c | 24 +++++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/pc/controller/controller_recorded_tas.c b/src/pc/controller/controller_recorded_tas.c index 4f5c1bb..c55c533 100644 --- a/src/pc/controller/controller_recorded_tas.c +++ b/src/pc/controller/controller_recorded_tas.c @@ -1,25 +1,35 @@ #include #include +#include +#include #include "controller_api.h" -static FILE *fp; +static int fd; +static int counter; + +#define OFFSET 0x400 static void tas_init(void) { - fp = fopen("cont.m64", "rb"); - if (fp != NULL) { - uint8_t buf[0x400]; - fread(buf, 1, sizeof(buf), fp); + fd = open("cont.m64", O_RDONLY); + if (fd != 0) { + uint8_t buf[OFFSET]; + read(fd, buf, sizeof(buf)); + counter = 0; } } static void tas_read(OSContPad *pad) { - if (fp != NULL) { + if (fd != 0) { uint8_t bytes[4] = {0}; - fread(bytes, 1, 4, fp); + int result = read(fd, bytes, 4); + assert(result > 0); pad->button = (bytes[0] << 8) | bytes[1]; pad->stick_x = bytes[2]; pad->stick_y = bytes[3]; + counter+=4; + printf("%x called %04x %d %d\n", (counter + OFFSET), pad->button, bytes[2], bytes[3]); + fflush(stdout); } } From 0a4f693fdcd3b8bdcda3c2353207f2917d449136 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 8 May 2020 06:48:52 -0400 Subject: [PATCH 2/3] tas recorder is functional --- shell.nix | 1 + src/pc/controller/controller_entry_point.c | 7 +-- src/pc/controller/controller_recorded_tas.c | 5 +- src/pc/controller/controller_tas_recorder.c | 52 +++++++++++++++++++++ src/pc/controller/controller_tas_recorder.h | 8 ++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/pc/controller/controller_tas_recorder.c create mode 100644 src/pc/controller/controller_tas_recorder.h diff --git a/shell.nix b/shell.nix index c611f0b..2198fbb 100644 --- a/shell.nix +++ b/shell.nix @@ -16,5 +16,6 @@ in pkgs.mkShell { glfw libGL clang_10 + gdb ]; } diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index faf91f5..a99b5e5 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -3,13 +3,14 @@ #include "controller_recorded_tas.h" #include "controller_keyboard.h" - #include "controller_sdl.h" +#include "controller_tas_recorder.h" static struct ControllerAPI *controller_implementations[] = { &controller_recorded_tas, - &controller_sdl, - &controller_keyboard, + /* &controller_sdl, */ + /* &controller_keyboard, */ + /* &controller_tas_recorder, */ }; s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) { diff --git a/src/pc/controller/controller_recorded_tas.c b/src/pc/controller/controller_recorded_tas.c index c55c533..83b982b 100644 --- a/src/pc/controller/controller_recorded_tas.c +++ b/src/pc/controller/controller_recorded_tas.c @@ -23,7 +23,10 @@ static void tas_read(OSContPad *pad) { if (fd != 0) { uint8_t bytes[4] = {0}; int result = read(fd, bytes, 4); - assert(result > 0); + if (result < 0) { + return; + } + pad->button = (bytes[0] << 8) | bytes[1]; pad->stick_x = bytes[2]; pad->stick_y = bytes[3]; diff --git a/src/pc/controller/controller_tas_recorder.c b/src/pc/controller/controller_tas_recorder.c new file mode 100644 index 0000000..d1c95a3 --- /dev/null +++ b/src/pc/controller/controller_tas_recorder.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +#include "controller_api.h" + +static FILE *fout; +static int counter; + +#define OFFSET 0x400 +#define fname "rec.m64" + +static void tas_recorder_close(void) { + fclose(fout); +} + +static void tas_recorder_init(void) { + if (fname == NULL) { + fout = NULL; + return; + } + + unlink(fname); + printf("[tas_recorder] writing output to %s\n", fname); + fout = fopen(fname, "wb"); + assert(fout != NULL); + uint8_t buf[OFFSET]; + memset(buf, 0, sizeof(buf)); + fwrite(buf, 1, sizeof(buf), fout); + atexit(tas_recorder_close); +} + +static void tas_recorder_read(OSContPad *pad) { + if (fout == NULL) { + return; + } + + uint8_t bytes[4] = {0}; + int button1 = pad->button; + int button2 = pad->button; + bytes[0] = button1 >> 8; + bytes[1] = button2 & 0x00FF; + bytes[2] = pad->stick_x; + bytes[3] = pad->stick_y; + fwrite(bytes, 1, 4, fout); +} + +struct ControllerAPI controller_tas_recorder = { + tas_recorder_init, + tas_recorder_read +}; diff --git a/src/pc/controller/controller_tas_recorder.h b/src/pc/controller/controller_tas_recorder.h new file mode 100644 index 0000000..8ceccf8 --- /dev/null +++ b/src/pc/controller/controller_tas_recorder.h @@ -0,0 +1,8 @@ +#ifndef CONTROLLER_TAS_RECORDER_H +#define CONTROLLER_TAS_RECORDER_H + +#include "controller_api.h" + +extern struct ControllerAPI controller_tas_recorder; + +#endif From cf7c89e31f453ef0c48967f21375d6a0ae2d1620 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 8 May 2020 08:47:25 -0400 Subject: [PATCH 3/3] let tas playback and input coexist --- src/pc/controller/controller_entry_point.c | 6 +-- src/pc/controller/controller_recorded_tas.c | 47 ++++++++++++--------- src/pc/controller/controller_tas_recorder.c | 3 +- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/pc/controller/controller_entry_point.c b/src/pc/controller/controller_entry_point.c index a99b5e5..5daed3c 100644 --- a/src/pc/controller/controller_entry_point.c +++ b/src/pc/controller/controller_entry_point.c @@ -8,9 +8,9 @@ static struct ControllerAPI *controller_implementations[] = { &controller_recorded_tas, - /* &controller_sdl, */ - /* &controller_keyboard, */ - /* &controller_tas_recorder, */ + &controller_sdl, + &controller_keyboard, + &controller_tas_recorder, }; s32 osContInit(OSMesgQueue *mq, u8 *controllerBits, OSContStatus *status) { diff --git a/src/pc/controller/controller_recorded_tas.c b/src/pc/controller/controller_recorded_tas.c index 83b982b..af6bf43 100644 --- a/src/pc/controller/controller_recorded_tas.c +++ b/src/pc/controller/controller_recorded_tas.c @@ -5,35 +5,44 @@ #include "controller_api.h" -static int fd; +static FILE *fin; static int counter; #define OFFSET 0x400 +#define fname "cont.m64" static void tas_init(void) { - fd = open("cont.m64", O_RDONLY); - if (fd != 0) { - uint8_t buf[OFFSET]; - read(fd, buf, sizeof(buf)); - counter = 0; + fin = fopen(fname, "rb"); + if (fin == NULL) { + return; } + + printf("[tas_playback] loading %s\n", fname); + uint8_t buf[OFFSET]; + fread(buf, 1, sizeof(buf), fin); + counter = 0; } static void tas_read(OSContPad *pad) { - if (fd != 0) { - uint8_t bytes[4] = {0}; - int result = read(fd, bytes, 4); - if (result < 0) { - return; - } - - pad->button = (bytes[0] << 8) | bytes[1]; - pad->stick_x = bytes[2]; - pad->stick_y = bytes[3]; - counter+=4; - printf("%x called %04x %d %d\n", (counter + OFFSET), pad->button, bytes[2], bytes[3]); - fflush(stdout); + if (fin == NULL) { + return; } + + uint8_t bytes[4] = {0}; + int result = fread(bytes, 1, 4, fin); + if (feof(fin)) { + printf("[tas_playback] end of tas input\n"); + fclose(fin); + fin = NULL; + return; + } + + pad->button = (bytes[0] << 8) | bytes[1]; + pad->stick_x = bytes[2]; + pad->stick_y = bytes[3]; + counter+=4; + printf("[tas_playback] %08x called %04x %02x%02x\n", (counter + OFFSET), pad->button, bytes[2], bytes[3]); + fflush(stdout); } struct ControllerAPI controller_recorded_tas = { diff --git a/src/pc/controller/controller_tas_recorder.c b/src/pc/controller/controller_tas_recorder.c index d1c95a3..6bd1c99 100644 --- a/src/pc/controller/controller_tas_recorder.c +++ b/src/pc/controller/controller_tas_recorder.c @@ -12,7 +12,8 @@ static int counter; #define fname "rec.m64" static void tas_recorder_close(void) { - fclose(fout); + fclose(fout); + printf("[tas_recorder] saving tas data to %s\n", fname); } static void tas_recorder_init(void) {