+static int enable_echo(int enable)
+{
+ const char *portname = "/dev/tty";
+ struct termios tty;
+ int retval = -1;
+ int ret;
+ int fd;
+
+ memset(&tty, 0, sizeof(tty));
+
+ fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
+ if (fd < 0) {
+ fprintf(stderr, "open %s: ", portname);
+ perror("");
+ return 1;
+ }
+
+ ret = tcgetattr(fd, &tty);
+ if (ret != 0) {
+ perror("tcgetattr");
+ goto out;
+ }
+
+ // printf("lflag: 0%o\n", tty.c_lflag);
+ if (enable)
+ tty.c_lflag |= ECHO | ICANON;
+ else {
+ tty.c_lflag &= ~(ECHO | ICANON);
+ tty.c_cc[VMIN] = tty.c_cc[VTIME] = 0;
+ }
+
+ ret = tcsetattr(fd, TCSANOW, &tty);
+ if (ret != 0) {
+ perror("tcsetattr");
+ goto out;
+ }
+
+ retval = 0;
+out:
+ close(fd);
+
+ return retval;
+}
+
+static int g_exit;
+
+static void signal_handler(int sig)
+{
+ g_exit = 1;
+ signal(sig, SIG_DFL);
+}
+
+/* ?0SA 00DU, ?1CB RLDU */
+#define STATE_BYTES 2
+
+static uint8_t fixed_input_state[STATE_BYTES] = { 0x33, 0x3f };
+
+enum mdbtn {
+ MDBTN_UP = 1,
+ MDBTN_DOWN,
+ MDBTN_LEFT,
+ MDBTN_RIGHT,
+ MDBTN_A,
+ MDBTN_B,
+ MDBTN_C,
+ MDBTN_START,
+};
+
+static const enum mdbtn evdev_md_map[KEY_CNT] = {
+ [KEY_UP] = MDBTN_UP,
+ [KEY_DOWN] = MDBTN_DOWN,
+ [KEY_LEFT] = MDBTN_LEFT,
+ [KEY_RIGHT] = MDBTN_RIGHT,
+ [KEY_HOME] = MDBTN_A,
+ [KEY_PAGEDOWN] = MDBTN_B,
+ [KEY_END] = MDBTN_C,
+ [KEY_LEFTALT] = MDBTN_START,
+};
+
+int do_evdev_input(int fd)
+{
+ uint8_t old_state[STATE_BYTES];
+ uint8_t changed_bits[STATE_BYTES] = { 0, };
+ struct input_event ev;
+ enum mdbtn mdbtn;
+ int i, ret;
+
+ ret = read(fd, &ev, sizeof(ev));
+ if (ret != sizeof(ev)) {
+ fprintf(stderr, "evdev read %d/%zd: ", ret, sizeof(ev));
+ perror("");
+ return 0;
+ }
+
+ if (ev.type != EV_KEY)
+ return 0;
+
+ if (ev.value != 0 && ev.value != 1)
+ return 0;
+
+ if ((uint32_t)ev.code >= ARRAY_SIZE(evdev_md_map)) {
+ fprintf(stderr, "evdev read bad key: %u\n", ev.code);
+ return 0;
+ }
+
+ mdbtn = evdev_md_map[ev.code];
+ if (mdbtn == 0)
+ return 0;
+
+ memcpy(old_state, fixed_input_state, STATE_BYTES);
+
+ /* ?0SA 00DU, ?1CB RLDU */
+ switch (mdbtn) {
+ case MDBTN_UP:
+ changed_bits[0] = 0x01;
+ changed_bits[1] = 0x01;
+ break;
+ case MDBTN_DOWN:
+ changed_bits[0] = 0x02;
+ changed_bits[1] = 0x02;
+ break;
+ case MDBTN_LEFT:
+ changed_bits[0] = 0x00;
+ changed_bits[1] = 0x04;
+ break;
+ case MDBTN_RIGHT:
+ changed_bits[0] = 0x00;
+ changed_bits[1] = 0x08;
+ break;
+ case MDBTN_A:
+ changed_bits[0] = 0x10;
+ changed_bits[1] = 0x00;
+ break;
+ case MDBTN_B:
+ changed_bits[0] = 0x00;
+ changed_bits[1] = 0x10;
+ break;
+ case MDBTN_C:
+ changed_bits[0] = 0x00;
+ changed_bits[1] = 0x20;
+ break;
+ case MDBTN_START:
+ changed_bits[0] = 0x20;
+ changed_bits[1] = 0x00;
+ break;
+ }
+
+ if (ev.value) {
+ // key press
+ for (i = 0; i < STATE_BYTES; i++)
+ fixed_input_state[i] &= ~changed_bits[i];
+ }
+ else {
+ // key release
+ for (i = 0; i < STATE_BYTES; i++)
+ fixed_input_state[i] |= changed_bits[i];
+ }
+
+ return memcmp(old_state, fixed_input_state, STATE_BYTES) ? 1 : 0;
+}
+
+struct gmv_tas {
+ char sig[15];
+ char ver;
+ uint32_t rerecord_count;
+ char ctrl1;
+ char ctrl2;
+ uint16_t flags;
+ char name[40];
+ uint8_t data[0][3];
+};
+
+static uint8_t *import_gmv(FILE *f, long size, int *frame_count)
+{
+ struct gmv_tas *gmv;
+ uint8_t *out;
+ int ret;
+ int i;
+
+ if (size < (long)sizeof(*gmv)) {
+ fprintf(stderr, "bad gmv size: %ld\n", size);
+ return NULL;
+ }
+
+ gmv = malloc(size);
+ if (gmv == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+ ret = fread(gmv, 1, size, f);
+ if (ret != size) {
+ fprintf(stderr, "fread %d/%ld: ", ret, size);
+ perror("");
+ return NULL;
+ }
+
+ *frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]);
+
+ /* check the GMV.. */
+ if (*frame_count <= 0 || size != sizeof(*gmv) + *frame_count * 3) {
+ fprintf(stderr, "broken gmv? frames=%d\n", *frame_count);
+ return NULL;
+ }
+
+ if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) {
+ fprintf(stderr, "bad GMV sig\n");
+ return NULL;
+ }
+ if (gmv->ctrl1 != '3') {
+ fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1);
+ //return NULL;
+ }
+ if (gmv->ver >= 'A') {
+ if (gmv->flags & 0x40) {
+ fprintf(stderr, "unhandled flag: movie requires a savestate\n");
+ return NULL;
+ }
+ if (gmv->flags & 0x20) {
+ fprintf(stderr, "unhandled flag: 3-player movie\n");
+ return NULL;
+ }
+ if (gmv->flags & ~0x80) {
+ //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags);
+ //return 1;
+ }
+ }
+ gmv->name[39] = 0;
+ printf("loaded GMV: %s\n", gmv->name);
+ printf("%d frames, %u rerecords\n",
+ *frame_count, gmv->rerecord_count);
+
+ out = malloc(*frame_count * sizeof(out[0]));
+ if (out == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+
+ for (i = 0; i < *frame_count; i++) {
+ out[i] = gmv->data[i][0];
+
+ if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff)
+ {
+ fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n",
+ i, gmv->data[i][1], gmv->data[i][2]);
+ }
+ }
+
+ return out;
+}
+
+static int do_bkm_char(char c, char expect, uint8_t *val, int bit)
+{
+ if (c == expect) {
+ *val &= ~(1 << bit);
+ return 0;
+ }
+ if (c == '.')
+ return 0;
+
+ fprintf(stderr, "unexpected bkm char: '%c' instead of '%c'\n",
+ c, expect);
+ return 1;
+}
+
+static uint8_t *import_bkm(FILE *f, int *frame_count)
+{
+ uint8_t *out = NULL, val;
+ int count = 0;
+ int alloc = 0;
+ int line = 0;
+ char buf[256];
+ const char *r;
+ char *p;
+ int i;
+
+ while ((p = fgets(buf, sizeof(buf), f)) != NULL) {
+ line++;
+ if (p[0] != '|')
+ continue;
+
+ if (strlen(p) < 30)
+ goto unhandled_line;
+ if (p[30] != '\r' && p[30] != '\n')
+ goto unhandled_line;
+ p[30] = 0;
+
+ if (count >= alloc) {
+ alloc = alloc * 2 + 64;
+ out = realloc(out, alloc * sizeof(out[0]));
+ if (out == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+ }
+
+ val = 0xff;
+
+ if (strncmp(p, "|.|", 3) != 0)
+ goto unhandled_line;
+ p += 3;
+
+ const char ref[] = "UDLRABCS";
+ for (r = ref, i = 0; *r != 0; p++, r++, i++) {
+ if (do_bkm_char(*p, *r, &val, i))
+ goto unhandled_line;
+ }
+
+ if (strcmp(p, "....|............||") != 0)
+ goto unhandled_line;
+
+ out[count++] = val;
+ continue;
+
+unhandled_line:
+ fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf);
+ return NULL;
+ }
+
+ printf("loaded bkm, %d frames\n", count);
+ *frame_count = count;
+ return out;
+}
+
+static int write_bkm_frame(FILE *f, const uint8_t *data)
+{
+ /* ?0SA 00DU, ?1CB RLDU */
+ static const char ref[] = "UDLRABCSXYZM";
+ static const char bits[] = { 0,1,2,3, 12,4,5,13, 16,16,16,16 };
+ uint32_t idata[2];
+ int p, i;
+
+ if (f == NULL) {
+ fprintf(stderr, "%s called without outfile\n", __func__);
+ goto out;
+ }
+
+ idata[0] = 0x10000 | (data[0] << 8) | data[1];
+ idata[1] = ~0;
+
+ fprintf(f, "|.|");
+ for (p = 0; p < 2; p++) {
+ for (i = 0; i < 12; i++)
+ fprintf(f, "%c", (idata[p] & (1 << bits[i])) ? '.' : ref[i]);
+ fprintf(f, "|");
+ }
+ fprintf(f, "|\n");
+
+out:
+ return 2;
+}
+
+static int tas_data_to_teensy(uint8_t b, uint8_t *data)
+{
+ /* SCBA RLDU */
+ /* v */
+ /* ?0SA 00DU, ?1CB RLDU */
+ data[0] = (b & 0x13) | ((b >> 2) & 0x20);
+ data[1] = (b & 0x0f) | ((b >> 1) & 0x30);
+ return 2;
+}
+
+static int submit_urb(int fd, struct usbdevfs_urb *urb, int ep,
+ void *buf, size_t buf_size)
+{
+ memset(urb, 0, sizeof(*urb));
+ urb->type = USBDEVFS_URB_TYPE_INTERRUPT;
+ urb->endpoint = ep;
+ urb->buffer = buf;
+ urb->buffer_length = buf_size;
+
+ return ioctl(fd, USBDEVFS_SUBMITURB, urb);
+}
+