+static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf)
+{
+ struct gmv_tas *gmv;
+ int frame_count;
+ int count = 0;
+ uint16_t val;
+ uint8_t *out;
+ int ret;
+ int i;
+
+ *byte_count = 0;
+
+ 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 * MAX_INPUT_BYTES);
+ if (out == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+
+ for (i = 0; i < frame_count; i++) {
+ val = gmv->data[i][0] | ((gmv->data[i][2] & 0x0f) << 8);
+ count += tas_data_to_teensy(val, out + count, logf);
+
+ 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]);
+ }
+ }
+
+ *byte_count = count;
+ return out;
+}
+
+static int do_bkm_char(char c, char expect, uint16_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 *byte_count, FILE *logf)
+{
+ uint8_t *out = NULL;
+ uint16_t val;
+ int frames = 0;
+ 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 - MAX_INPUT_BYTES) {
+ alloc = alloc * 2 + 64;
+ out = realloc(out, alloc * sizeof(out[0]));
+ if (out == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+ }
+
+ val = 0xfff;
+
+ 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;
+
+ count += tas_data_to_teensy(val, out + count, logf);
+ frames++;
+ continue;
+
+unhandled_line:
+ fprintf(stderr, "unhandled bkm line %d: '%s'\n", line, buf);
+ return NULL;
+ }
+
+ printf("loaded bkm, %d frames, %d bytes\n", frames, count);
+ *byte_count = count;
+ return out;
+}
+
+static uint8_t *import_raw(FILE *f, int *byte_count, FILE *logf)
+{
+ uint8_t *out = NULL, val;
+ int count = 0;
+ int alloc = 0;
+ int line = 0;
+ int first = 1;
+ char buf[256];
+ char *p;
+ int i;
+
+ while ((p = fgets(buf, sizeof(buf), f)) != NULL)
+ {
+ line++;
+ if (p[0] == '#')
+ continue;
+ if (p[0] != 'e') {
+ printf("skipping: %s", p);
+ continue;
+ }
+
+ val = 0;
+ p++;
+ for (i = 6; i >= 0; i--, p++) {
+ if (*p != '0' && *p != '1')
+ goto bad;
+ if (*p == '1')
+ val |= 1 << i;
+ }
+ if (*p != ' ')
+ goto bad;
+
+ if (first && (val & 0x40))
+ continue; // XXX..
+ first = 0;
+
+ if (count >= alloc) {
+ alloc = alloc * 2 + 64;
+ out = realloc(out, alloc * sizeof(out[0]));
+ if (out == NULL) {
+ fprintf(stderr, "OOM?\n");
+ return NULL;
+ }
+ }
+
+ if (logf)
+ fwrite(&val, 1, 1, logf);
+
+ out[count++] = val & 0x3f;
+ continue;
+
+bad:
+ fprintf(stderr, "bad raw line %d: '%s'\n", line, buf);
+ return NULL;
+ }
+
+ printf("loaded raw, %d bytes\n", count);
+ *byte_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;
+}
+