X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=host%2Fmain.c;h=58fbcb9e007c91ed72c95c9864e5cf3feaea35e7;hb=96b9855aaad7a6aabb109b885ce5f81d44a85c1d;hp=9c8021aaf29b0b2f20108bf8125fbd18f601d332;hpb=4af6d4e58ab889ea4b1787d35d5611dabb5d4741;p=teensytas.git diff --git a/host/main.c b/host/main.c index 9c8021a..58fbcb9 100644 --- a/host/main.c +++ b/host/main.c @@ -261,9 +261,11 @@ static int enable_echo(int enable) // printf("lflag: 0%o\n", tty.c_lflag); if (enable) - tty.c_lflag |= ECHO; - else - tty.c_lflag &= ~ECHO; + 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) { @@ -278,11 +280,12 @@ out: return retval; } +static int g_exit; + static void signal_handler(int sig) { - enable_echo(1); + g_exit = 1; signal(sig, SIG_DFL); - raise(sig); } /* ?0SA 00DU, ?1CB RLDU */ @@ -405,6 +408,157 @@ struct gmv_tas { 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 submit_urb(int fd, struct usbdevfs_urb *urb, int ep, void *buf, size_t buf_size) { @@ -440,11 +594,10 @@ int main(int argc, char *argv[]) int evdev_fd_cnt = 0; int evdev_support; int wait_device = 0; - int dbg_in_sent = 0; - int data_in_sent = 0; + int pending_urbs = 0; fd_set rfds, wfds; - struct gmv_tas *gmv = NULL; const char *tasfn = NULL; + uint8_t *tas_data = NULL; int use_readinc = 0; // frame increment on read int tas_skip = 0; int enable_sent = 0; @@ -455,7 +608,7 @@ int main(int argc, char *argv[]) struct tas_pkt pkt_out; struct timeval *timeout = NULL; struct timeval tout; - int i, ret; + int i, ret = -1; int fd; for (i = 1; i < argc; i++) { @@ -505,9 +658,10 @@ int main(int argc, char *argv[]) } if (tasfn != NULL) { + const char *ext; long size; FILE *f; - + f = fopen(tasfn, "rb"); if (f == NULL) { fprintf(stderr, "fopen %s: ", tasfn); @@ -518,56 +672,31 @@ int main(int argc, char *argv[]) fseek(f, 0, SEEK_END); size = ftell(f); fseek(f, 0, SEEK_SET); - if (size < (long)sizeof(*gmv)) { - fprintf(stderr, "bad gmv size: %ld\n", size); - return 1; - } - gmv = malloc(size); - if (gmv == NULL) { - fprintf(stderr, "OOM?\n"); - return 1; - } - ret = fread(gmv, 1, size, f); - if (ret != size) { - fprintf(stderr, "fread %d/%ld: ", ret, size); - perror(""); + if (size <= 0) { + fprintf(stderr, "bad size: %ld\n", size); return 1; } - fclose(f); - 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); + ext = strrchr(tasfn, '.'); + if (ext == NULL) + ext = tasfn; + else + ext++; + + if (strcasecmp(ext, "gmv") == 0) + tas_data = import_gmv(f, size, &frame_count); + else if (strcasecmp(ext, "bkm") == 0) + tas_data = import_bkm(f, &frame_count); + else { + fprintf(stderr, "unknown movie type: '%s'\n", ext); return 1; } + fclose(f); - if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) { - fprintf(stderr, "bad GMV sig\n"); + if (tas_data == NULL) { + fprintf(stderr, "failed fo parse %s\n", tasfn); return 1; } - if (gmv->ctrl1 != '3') { - fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1); - //return 1; - } - if (gmv->ver >= 'A') { - if (gmv->flags & 0x40) { - fprintf(stderr, "unhandled flag: movie requires a savestate\n"); - return 1; - } - if (gmv->flags & 0x20) { - fprintf(stderr, "unhandled flag: 3-player movie\n"); - return 1; - } - 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); if (tas_skip != 0) { if (tas_skip >= frame_count || tas_skip <= -frame_count) { @@ -576,19 +705,19 @@ int main(int argc, char *argv[]) } if (tas_skip > 0) { frame_count -= tas_skip; - memmove(&gmv->data[0], &gmv->data[tas_skip], - sizeof(gmv->data[0]) * frame_count); + memmove(&tas_data[0], &tas_data[tas_skip], + sizeof(tas_data[0]) * frame_count); } else { - gmv = realloc(gmv, sizeof(*gmv) - + (frame_count - tas_skip) * sizeof(gmv->data[0])); - if (gmv == NULL) { + tas_data = realloc(tas_data, + (frame_count - tas_skip) * sizeof(tas_data[0])); + if (tas_data == NULL) { fprintf(stderr, "OOM?\n"); return 1; } - memmove(&gmv->data[-tas_skip], &gmv->data[0], - sizeof(gmv->data[0]) * frame_count); - memset(&gmv->data[0], 0xff, sizeof(gmv->data[0]) * -tas_skip); + memmove(&tas_data[-tas_skip], &tas_data[0], + sizeof(tas_data[0]) * frame_count); + memset(&tas_data[0], 0xff, sizeof(tas_data[0]) * -tas_skip); frame_count -= tas_skip; } } @@ -599,7 +728,7 @@ int main(int argc, char *argv[]) dev.fd = -1; - while (1) + while (!g_exit) { if (dev.fd == -1) { ret = find_device(&dev, 0x16C0, 0x0486); @@ -616,8 +745,7 @@ int main(int argc, char *argv[]) } wait_device = 0; - data_in_sent = 0; - dbg_in_sent = 0; + pending_urbs = 0; enable_sent = 0; frames_sent = 0; @@ -628,7 +756,7 @@ int main(int argc, char *argv[]) timeout = &tout; } - if (!data_in_sent) { + if (!(pending_urbs & (1 << URB_DATA_IN))) { memset(&pkt_in, 0, sizeof(pkt_in)); ret = submit_urb(dev.fd, &urb[URB_DATA_IN], dev.ifaces[0].ep_in, &pkt_in, sizeof(pkt_in)); @@ -637,9 +765,9 @@ int main(int argc, char *argv[]) break; } - data_in_sent = 1; + pending_urbs |= 1 << URB_DATA_IN; } - if (!dbg_in_sent) { + if (!(pending_urbs & (1 << URB_DBG_IN))) { ret = submit_urb(dev.fd, &urb[URB_DBG_IN], dev.ifaces[1].ep_in, buf_dbg, sizeof(buf_dbg) - 1); if (ret != 0) { @@ -647,10 +775,11 @@ int main(int argc, char *argv[]) break; } - dbg_in_sent = 1; + pending_urbs |= 1 << URB_DBG_IN; } FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); for (i = 0; i < evdev_fd_cnt; i++) FD_SET(evdev_fds[i], &rfds); @@ -664,7 +793,23 @@ int main(int argc, char *argv[]) } timeout = NULL; + /* sometihng form stdin? */ /* something from input devices? */ + if (FD_ISSET(STDIN_FILENO, &rfds)) { + char c = 0; + ret = read(STDIN_FILENO, &c, 1); + if (ret <= 0) { + perror("read stdin"); + break; + } + + switch (c) { + case 'r': + enable_sent = 0; + break; + } + } + fixed_input_changed = 0; for (i = 0; i < evdev_fd_cnt; i++) { if (FD_ISSET(evdev_fds[i], &rfds)) { @@ -676,6 +821,8 @@ int main(int argc, char *argv[]) /* something from USB? */ if (FD_ISSET(dev.fd, &wfds)) { + unsigned int which_urb; + reaped_urb = NULL; ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); if (ret != 0) { @@ -684,13 +831,17 @@ int main(int argc, char *argv[]) perror("USBDEVFS_REAPURB"); break; } + which_urb = reaped_urb - urb; + if (which_urb < ARRAY_SIZE(urb)) + pending_urbs &= ~(1 << which_urb); + else { + fprintf(stderr, "reaped unknown urb: %p #%u", + reaped_urb, which_urb); + } if (reaped_urb != NULL && reaped_urb->status != 0) { errno = -reaped_urb->status; - if ((unsigned long)(reaped_urb - urb) < ARRAY_SIZE(urb)) - fprintf(stderr, "urb #%zu: ", reaped_urb - urb); - else - fprintf(stderr, "unknown urb: "); + fprintf(stderr, "urb #%u: ", which_urb); perror(""); if (reaped_urb->status == -EILSEQ) { /* this is usually a sign of disconnect.. */ @@ -698,18 +849,18 @@ int main(int argc, char *argv[]) goto dev_close; } } - - if (reaped_urb == &urb[URB_DATA_IN]) { + else if (reaped_urb == &urb[URB_DATA_IN]) + { /* some request from teensy */ int count; uint8_t b; switch (pkt_in.type) { case PKT_STREAM_REQ: - printf("%d/%d/%d\n", pkt_in.req.frame, + printf("req: %d/%d/%d\n", pkt_in.req.frame, frames_sent, frame_count); - for (i = 0; i < sizeof(pkt_out.data); i++) { + for (i = 0; i < sizeof(pkt_out.data) / 2; i++) { pkt_out.data[i * 2 + 0] = 0x33; pkt_out.data[i * 2 + 1] = 0x3f; } @@ -721,20 +872,11 @@ int main(int argc, char *argv[]) count = sizeof(pkt_out.data) / 2; for (i = 0; i < count; i++) { /* SCBA RLDU */ - b = gmv->data[frames_sent][0]; + b = tas_data[frames_sent]; /* ?0SA 00DU, ?1CB RLDU */ pkt_out.data[i * 2 + 0] = (b & 0x13) | ((b >> 2) & 0x20); pkt_out.data[i * 2 + 1] = (b & 0x0f) | ((b >> 1) & 0x30); - - if (gmv->data[frames_sent][1] != 0xff - || gmv->data[frames_sent][2] != 0xff) - { - fprintf(stderr, "f %d: unhandled byte(s) %02x %02x\n", - frames_sent, gmv->data[frames_sent][1], - gmv->data[frames_sent][2]); - } - frames_sent++; } } @@ -751,16 +893,15 @@ int main(int argc, char *argv[]) printf("host: got unknown pkt type: %04x\n", pkt_in.type); break; } - - data_in_sent = 0; } - else if (reaped_urb == &urb[URB_DATA_OUT]) { + else if (reaped_urb == &urb[URB_DATA_OUT]) + { } - else if (reaped_urb == &urb[URB_DBG_IN]) { + else if (reaped_urb == &urb[URB_DBG_IN]) + { /* debug text */ buf_dbg[reaped_urb->actual_length] = 0; printf("%s", buf_dbg); - dbg_in_sent = 0; } else { fprintf(stderr, "reaped unknown urb? %p #%zu\n", @@ -769,7 +910,11 @@ int main(int argc, char *argv[]) } /* something to send? */ - if (gmv != NULL && !enable_sent) { + if (pending_urbs & (1 << URB_DATA_OUT)) + // can't do that yet + continue; + + if (tas_data != NULL && !enable_sent) { memset(&pkt_out, 0, sizeof(pkt_out)); pkt_out.type = PKT_STREAM_ENABLE; pkt_out.start.use_readinc = use_readinc; @@ -781,8 +926,9 @@ int main(int argc, char *argv[]) continue; } enable_sent = 1; + frames_sent = 0; } - if (gmv == NULL && fixed_input_changed) { + if (tas_data == NULL && fixed_input_changed) { memset(&pkt_out, 0, sizeof(pkt_out)); pkt_out.type = PKT_FIXED_STATE; memcpy(pkt_out.data, fixed_input_state, sizeof(fixed_input_state)); @@ -793,6 +939,8 @@ int main(int argc, char *argv[]) perror("USBDEVFS_SUBMITURB URB_DATA_OUT"); break; } + pending_urbs |= 1 << URB_DATA_OUT; + continue; } continue; @@ -804,6 +952,23 @@ dev_close: enable_echo(1); + if (dev.fd != -1) { + /* deal with pending URBs */ + if (pending_urbs & (1 << URB_DATA_IN)) + ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DATA_IN]); + if (pending_urbs & (1 << URB_DBG_IN)) + ioctl(dev.fd, USBDEVFS_DISCARDURB, &urb[URB_DBG_IN]); + for (i = 0; i < URB_CNT; i++) { + if (pending_urbs & (1 << i)) { + ret = ioctl(dev.fd, USBDEVFS_REAPURB, &reaped_urb); + if (ret != 0) + perror("USBDEVFS_REAPURB"); + } + } + + close(dev.fd); + } + return ret; }