From 19560e5f86bb4e1b3e22e41f29ed5c76f9afe6cf Mon Sep 17 00:00:00 2001 From: notaz Date: Tue, 18 Nov 2014 23:49:57 +0200 Subject: [PATCH] initial support for 2 players --- host/main.c | 174 +++++++++++++--------- main.c | 411 ++++++++++++++++++++++++++++++++++------------------ pkts.h | 25 +++- 3 files changed, 391 insertions(+), 219 deletions(-) diff --git a/host/main.c b/host/main.c index 9cc492c..b5a65b1 100644 --- a/host/main.c +++ b/host/main.c @@ -455,33 +455,33 @@ struct gmv_tas { uint8_t data[0][3]; }; -static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf) +static int import_gmv(FILE *f, long size, + uint8_t *out[2], int out_byte_count[2], 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; + out_byte_count[0] = out_byte_count[1] = 0; if (size < (long)sizeof(*gmv)) { fprintf(stderr, "bad gmv size: %ld\n", size); - return NULL; + return -1; } gmv = malloc(size); if (gmv == NULL) { fprintf(stderr, "OOM?\n"); - return NULL; + return -1; } ret = fread(gmv, 1, size, f); if (ret != size) { fprintf(stderr, "fread %d/%ld: ", ret, size); perror(""); - return NULL; + return -1; } frame_count = (size - sizeof(*gmv)) / sizeof(gmv->data[0]); @@ -489,25 +489,25 @@ static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf) /* check the GMV.. */ if (frame_count <= 0 || size != sizeof(*gmv) + frame_count * 3) { fprintf(stderr, "broken gmv? frames=%d\n", frame_count); - return NULL; + return -1; } if (strncmp(gmv->sig, "Gens Movie TEST", 15) != 0) { fprintf(stderr, "bad GMV sig\n"); - return NULL; + return -1; } if (gmv->ctrl1 != '3') { fprintf(stderr, "unhandled controlled config: '%c'\n", gmv->ctrl1); - //return NULL; + //return -1; } if (gmv->ver >= 'A') { if (gmv->flags & 0x40) { fprintf(stderr, "unhandled flag: movie requires a savestate\n"); - return NULL; + return -1; } if (gmv->flags & 0x20) { fprintf(stderr, "unhandled flag: 3-player movie\n"); - return NULL; + return -1; } if (gmv->flags & ~0x80) { //fprintf(stderr, "unhandled flag(s): %04x\n", gmv->flags); @@ -519,15 +519,15 @@ static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf) printf("%d frames, %u rerecords\n", frame_count, gmv->rerecord_count); - out = malloc(frame_count * MAX_INPUT_BYTES); - if (out == NULL) { + out[0] = malloc(frame_count * MAX_INPUT_BYTES); + if (out[0] == NULL) { fprintf(stderr, "OOM?\n"); - return NULL; + return -1; } 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); + count += tas_data_to_teensy(val, out[0] + count, logf); if (gmv->data[i][1] != 0xff || gmv->data[i][2] != 0xff) { @@ -536,8 +536,8 @@ static uint8_t *import_gmv(FILE *f, long size, int *byte_count, FILE *logf) } } - *byte_count = count; - return out; + out_byte_count[0] = count; + return 0; } static int do_bkm_char(char c, char expect, uint16_t *val, int bit) @@ -554,18 +554,21 @@ static int do_bkm_char(char c, char expect, uint16_t *val, int bit) return 1; } -static uint8_t *import_bkm(FILE *f, int *byte_count, FILE *logf) +static int import_bkm(FILE *f, uint8_t *out[2], int out_byte_count[2], + FILE *logf) { - uint8_t *out = NULL; - uint16_t val; + int teensy_bytes = 0; + int have_pl2 = 0; + int have_xyz = 0; int frames = 0; int count = 0; int alloc = 0; int line = 0; char buf[256]; const char *r; + uint16_t val; char *p; - int i; + int pl, i; while ((p = fgets(buf, sizeof(buf), f)) != NULL) { @@ -581,53 +584,78 @@ static uint8_t *import_bkm(FILE *f, int *byte_count, FILE *logf) 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; + for (pl = 0; pl < 2; pl++) { + out[pl] = realloc(out[pl], alloc * sizeof(out[0][0])); + if (out[pl] == NULL) { + fprintf(stderr, "OOM?\n"); + return -1; + } } } - 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)) + for (pl = 0; pl < 2; pl++) { + static const char ref[] = "UDLRABCSXYZM"; + + val = 0xfff; + for (r = ref, i = 0; *r != 0; p++, r++, i++) { + if (do_bkm_char(*p, *r, &val, i)) + goto unhandled_line; + } + + if (*p++ != '|') goto unhandled_line; + + teensy_bytes = tas_data_to_teensy(val, out[pl] + count, logf); + + if ((val & 0xf00) != 0xf00) + have_xyz = 1; + if (pl == 1) + have_pl2 |= (val != 0xfff); } + count += teensy_bytes; - if (strcmp(p, "....|............||") != 0) + 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; + return -1; } - printf("loaded bkm, %d frames, %d bytes\n", frames, count); - *byte_count = count; - return out; + printf("loaded bkm, %d players, %d frames, %d bytes, have_xyz=%d\n", + have_pl2 ? 2 : 1, frames, count, have_xyz); + out_byte_count[0] = count; + if (have_pl2) + out_byte_count[1] = count; + else { + free(out[1]); + out[1] = NULL; + } + + return 0; } -static uint8_t *import_raw(FILE *f, int *byte_count, FILE *logf) +static int import_raw(FILE *f, uint8_t *out[2], int out_byte_count[2], + FILE *logf) { - uint8_t *out = NULL, val; int count = 0; int alloc = 0; int line = 0; int first = 1; char buf[256]; + uint8_t val; char *p; int i; + out_byte_count[0] = out_byte_count[1] = 0; + while ((p = fgets(buf, sizeof(buf), f)) != NULL) { line++; @@ -655,27 +683,27 @@ static uint8_t *import_raw(FILE *f, int *byte_count, FILE *logf) if (count >= alloc) { alloc = alloc * 2 + 64; - out = realloc(out, alloc * sizeof(out[0])); - if (out == NULL) { + out[0] = realloc(out[0], alloc * sizeof(out[0][0])); + if (out[0] == NULL) { fprintf(stderr, "OOM?\n"); - return NULL; + return -1; } } if (logf) fwrite(&val, 1, 1, logf); - out[count++] = val & 0x3f; + out[0][count++] = val & 0x3f; continue; bad: fprintf(stderr, "bad raw line %d: '%s'\n", line, buf); - return NULL; + return -1; } printf("loaded raw, %d bytes\n", count); - *byte_count = count; - return out; + out_byte_count[0] = count; + return 0; } static int write_bkm_frame(FILE *f, const uint8_t *data) @@ -746,12 +774,11 @@ int main(int argc, char *argv[]) const char *tasfn = NULL; const char *outfn = NULL; const char *logfn = NULL; - uint8_t *tas_data = NULL; - int tas_data_size = 0; - int bytes_sent = 0; + uint8_t *tas_data[2] = { NULL, NULL }; + int tas_data_size[2] = { 0, 0 }; + int bytes_sent[2] = { 0, 0 }; int use_vsync = 0; // frame increment on vsync int no_start_seq = 0; - int tas_skip = 0; int enable_sent = 0; int abort_sent = 0; int frame_count = 0; @@ -858,11 +885,11 @@ int main(int argc, char *argv[]) ext++; if (strcasecmp(ext, "gmv") == 0) - tas_data = import_gmv(f, size, &tas_data_size, logf); + ret = import_gmv(f, size, tas_data, tas_data_size, logf); else if (strcasecmp(ext, "bkm") == 0) - tas_data = import_bkm(f, &tas_data_size, logf); + ret = import_bkm(f, tas_data, tas_data_size, logf); else if (strcasecmp(ext, "txt") == 0) - tas_data = import_raw(f, &tas_data_size, logf); + ret = import_raw(f, tas_data, tas_data_size, logf); else { fprintf(stderr, "unknown movie type: '%s'\n", ext); return 1; @@ -874,10 +901,14 @@ int main(int argc, char *argv[]) logf = NULL; } - if (tas_data == NULL) { + if (ret != 0 || tas_data[0] == NULL || tas_data_size[0] <= 0) { fprintf(stderr, "failed fo parse %s\n", tasfn); return 1; } + if (tas_data_size[1] != 0 && tas_data[1] == NULL) { + fprintf(stderr, "missing tas_data[1]\n"); + return 1; + } } if (outfn != NULL) { @@ -913,7 +944,8 @@ int main(int argc, char *argv[]) wait_device = 0; pending_urbs = 0; enable_sent = 0; - bytes_sent = 0; + bytes_sent[0] = 0; + bytes_sent[1] = 0; /* we wait first, then send commands, but if teensy * is started already, it won't send anything */ @@ -1016,21 +1048,25 @@ int main(int argc, char *argv[]) } else if (reaped_urb == &urb[URB_DATA_IN]) { + int p; + /* some request from teensy */ switch (pkt_in.type) { case PKT_STREAM_REQ: - printf("req: %d/%d/%d\n", pkt_in.req.frame * 2, - bytes_sent, tas_data_size); + p = pkt_in.req.is_p2 ? 1 : 0; + printf("req%d: %d/%d/%d\n", pkt_in.req.is_p2, + pkt_in.req.frame * 2, bytes_sent[p], tas_data_size[p]); pkt_out.size = 0; - if (bytes_sent < tas_data_size) { - pkt_out.type = PKT_STREAM_DATA_TO; + if (bytes_sent[p] < tas_data_size[p]) { + pkt_out.type = p ? PKT_STREAM_DATA_TO_P2 + : PKT_STREAM_DATA_TO_P1; - i = tas_data_size - bytes_sent; + i = tas_data_size[p] - bytes_sent[p]; if (i > sizeof(pkt_out.data)) i = sizeof(pkt_out.data); - memcpy(pkt_out.data, tas_data + bytes_sent, i); - bytes_sent += i; + memcpy(pkt_out.data, tas_data[p] + bytes_sent[p], i); + bytes_sent[p] += i; pkt_out.size = i; } else { @@ -1086,13 +1122,18 @@ int main(int argc, char *argv[]) // can't do that yet continue; - if ((tas_data != NULL || outf != NULL) && !enable_sent) { + if ((tas_data[0] != NULL || outf != NULL) && !enable_sent) { memset(&pkt_out, 0, sizeof(pkt_out)); pkt_out.type = PKT_STREAM_ENABLE; - pkt_out.enable.stream_to = (tas_data != NULL); + pkt_out.enable.stream_to = (tas_data[0] != NULL); pkt_out.enable.stream_from = (outf != NULL); - pkt_out.enable.use_readinc = !use_vsync; pkt_out.enable.no_start_seq = no_start_seq; + if (use_vsync) + pkt_out.enable.inc_mode = INC_MODE_VSYNC; + else if (tas_data_size[1] != 0) + pkt_out.enable.inc_mode = INC_MODE_SHARED_PL2; + else + pkt_out.enable.inc_mode = INC_MODE_SHARED_PL1; ret = submit_urb(dev.fd, &urb[URB_DATA_OUT], dev.ifaces[0].ep_out, &pkt_out, sizeof(pkt_out)); @@ -1102,10 +1143,11 @@ int main(int argc, char *argv[]) } pending_urbs |= 1 << URB_DATA_OUT; enable_sent = 1; - bytes_sent = 0; + bytes_sent[0] = 0; + bytes_sent[1] = 0; continue; } - if (tas_data == NULL && fixed_input_changed) { + if (tas_data[0] == 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)); diff --git a/main.c b/main.c index 96b895f..2aae65a 100644 --- a/main.c +++ b/main.c @@ -31,6 +31,8 @@ #include "teensy3/usb_rawhid.h" #include "pkts.h" +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + #define noinline __attribute__((noinline)) // use power of 2 @@ -41,28 +43,30 @@ #define STREAM_EL_SZ 2 static struct { - uint8_t stream_to[STREAM_BUF_SIZE][STREAM_EL_SZ]; + uint8_t stream_to[2][STREAM_BUF_SIZE][STREAM_EL_SZ]; uint8_t stream_from[STREAM_BUF_SIZE][STREAM_EL_SZ]; - union { - uint8_t fixed_state[4]; - uint32_t fixed_state32; - }; - union { - uint8_t pending_state[4]; - uint32_t pending_state32; - }; + struct { + union { + uint8_t fixed_state[4]; + uint32_t fixed_state32; + }; + union { + uint8_t pending_state[4]; + uint32_t pending_state32; + }; + } pl[2]; uint32_t stream_enable_to:1; uint32_t stream_enable_from:1; uint32_t stream_started:1; uint32_t stream_ended:1; - uint32_t use_readinc:1; + uint32_t inc_mode:2; uint32_t use_pending:1; uint32_t frame_cnt; uint32_t edge_cnt; - uint32_t t_i; - uint32_t t_o; - uint32_t f_i; - uint32_t f_o; + struct { + uint32_t i; + uint32_t o; + } pos_to_p[2], pos_from; } g; ssize_t _write(int fd, const void *buf, size_t nbyte) @@ -83,16 +87,21 @@ void yield(void) { } -/* portb handles TH */ -static void portb_isr_fixed(void) +static noinline void choose_isrs_idle(void); + +/* player1 TH */ +#define PL1_ISFR PORTD_ISFR +#define PL1_TH() ((CORE_PIN21_PINREG >> CORE_PIN21_BIT) & 1) + +static void pl1th_isr_fixed(void) { uint32_t isfr, th; - isfr = PORTB_ISFR; - PORTB_ISFR = isfr; - th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1; + isfr = PL1_ISFR; + PL1_ISFR = isfr; + th = PL1_TH(); - GPIOD_PDOR = g.fixed_state[th]; + GPIOD_PDOR = g.pl[0].fixed_state[th]; g.edge_cnt++; } @@ -100,59 +109,109 @@ static noinline void do_to_step(void) { g.frame_cnt++; - g.t_o = (g.t_o + 1) & STREAM_BUF_MASK; - if (g.t_o == g.t_i) + g.pos_to_p[0].o = (g.pos_to_p[0].o + 1) & STREAM_BUF_MASK; + if (g.pos_to_p[0].o == g.pos_to_p[0].i) // done - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); + choose_isrs_idle(); } -static void portb_isr_do_to_inc(void) +static void pl1th_isr_do_to_inc(void) { uint32_t isfr, th; - isfr = PORTB_ISFR; - PORTB_ISFR = isfr; - th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1; + isfr = PL1_ISFR; + PL1_ISFR = isfr; + th = PL1_TH(); - GPIOD_PDOR = g.stream_to[g.t_o][th]; + GPIOD_PDOR = g.stream_to[0][g.pos_to_p[0].o][th]; if (th) do_to_step(); } -static void portb_isr_do_to(void) +static void pl1th_isr_do_to(void) { uint32_t isfr, th; - isfr = PORTB_ISFR; - PORTB_ISFR = isfr; - th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1; + isfr = PL1_ISFR; + PL1_ISFR = isfr; + th = PL1_TH(); - GPIOD_PDOR = g.stream_to[g.t_o][th]; + GPIOD_PDOR = g.stream_to[0][g.pos_to_p[0].o][th]; g.edge_cnt++; } -static void portc_isr_nop(void) +/* player2 TH */ +#define PL2_ISFR PORTC_ISFR +#define PL2_TH() ((CORE_PIN15_PINREG >> CORE_PIN15_BIT) & 1) +#define PL2_ADJ(x) ((x) | ((x) << 12)) + +static void pl2th_isr_fixed(void) +{ + uint32_t isfr, th, v; + + isfr = PL2_ISFR; + PL2_ISFR = isfr; + th = PL2_TH(); + + v = g.pl[1].fixed_state[th]; + GPIOB_PDOR = PL2_ADJ(v); +} + +static void pl2th_isr_do_to(void) +{ + uint32_t isfr, th, v; + + isfr = PL2_ISFR; + PL2_ISFR = isfr; + th = PL2_TH(); + + v = g.stream_to[1][g.pos_to_p[0].o][th]; + GPIOB_PDOR = PL2_ADJ(v); + + g.pos_to_p[1].o = g.pos_to_p[0].o; +} + +static void pl2th_isr_do_to_inc_pl1(void) +{ + uint32_t isfr, th, v; + + isfr = PL2_ISFR; + PL2_ISFR = isfr; + th = PL2_TH(); + + v = g.stream_to[1][g.pos_to_p[1].o][th]; + GPIOB_PDOR = PL2_ADJ(v); + if (th) { + do_to_step(); + g.pos_to_p[1].o = g.pos_to_p[0].o; + } +} + +/* vsync handler */ +#define VSYNC_ISFR PORTC_ISFR + +static void vsync_isr_nop(void) { uint32_t isfr; - isfr = PORTC_ISFR; - PORTC_ISFR = isfr; + isfr = VSYNC_ISFR; + VSYNC_ISFR = isfr; } // /vsync starts at line 235/259 (ntsc/pal), just as vcounter jumps back // we care when it comes out (/vsync goes high) after 3 lines at 238/262 -static void portc_isr_frameinc(void) +static void vsync_isr_frameinc(void) { uint32_t isfr; - isfr = PORTC_ISFR; - PORTC_ISFR = isfr; + isfr = VSYNC_ISFR; + VSYNC_ISFR = isfr; - g.t_o = (g.t_o + 1) & STREAM_BUF_MASK; - if (g.t_o == g.t_i) { - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); - attachInterruptVector(IRQ_PORTC, portc_isr_nop); - } + g.pos_to_p[0].o = (g.pos_to_p[0].o + 1) & STREAM_BUF_MASK; + g.pos_to_p[1].o = g.pos_to_p[0].o; + + if (g.pos_to_p[0].o == g.pos_to_p[0].i) + choose_isrs_idle(); g.frame_cnt++; } @@ -162,63 +221,87 @@ static noinline void do_from_step(void) uint32_t s; // should hopefully give atomic fixed_state read.. - s = g.fixed_state32; - g.fixed_state32 = g.pending_state32; - g.stream_from[g.f_i][0] = s; - g.stream_from[g.f_i][1] = s >> 8; - g.f_i = (g.f_i + 1) & STREAM_BUF_MASK; + s = g.pl[0].fixed_state32; + g.pl[0].fixed_state32 = g.pl[0].pending_state32; + g.stream_from[g.pos_from.i][0] = s; + g.stream_from[g.pos_from.i][1] = s >> 8; + g.pos_from.i = (g.pos_from.i + 1) & STREAM_BUF_MASK; } -static void portb_isr_fixed_do_from(void) +static void pl1th_isr_fixed_do_from(void) { uint32_t isfr, th; - isfr = PORTB_ISFR; - PORTB_ISFR = isfr; - th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1; + isfr = PL1_ISFR; + PL1_ISFR = isfr; + th = PL1_TH(); - GPIOD_PDOR = g.fixed_state[th]; + GPIOD_PDOR = g.pl[0].fixed_state[th]; if (th) do_from_step(); g.edge_cnt++; } -static void portc_isr_frameinc_do_from(void) +static void vsync_isr_frameinc_do_from(void) { uint32_t isfr; - isfr = PORTC_ISFR; - PORTC_ISFR = isfr; + isfr = VSYNC_ISFR; + VSYNC_ISFR = isfr; do_from_step(); g.frame_cnt++; } +/* * */ static void choose_isrs(void) { + void (*pl1th_handler)(void) = pl1th_isr_fixed; + void (*pl2th_handler)(void) = pl2th_isr_fixed; + void (*vsync_handler)(void) = vsync_isr_nop; + if (g.stream_enable_to) { - if (g.use_readinc) { - attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc); - attachInterruptVector(IRQ_PORTC, portc_isr_nop); - } - else { - attachInterruptVector(IRQ_PORTB, portb_isr_do_to); - attachInterruptVector(IRQ_PORTC, portc_isr_frameinc); + switch (g.inc_mode) { + case INC_MODE_VSYNC: + pl1th_handler = pl1th_isr_do_to; + pl2th_handler = pl2th_isr_do_to; + vsync_handler = vsync_isr_frameinc; + break; + case INC_MODE_SHARED_PL1: + pl1th_handler = pl1th_isr_do_to_inc; + pl2th_handler = pl2th_isr_do_to; + break; + case INC_MODE_SHARED_PL2: + pl1th_handler = pl1th_isr_do_to; + pl2th_handler = pl2th_isr_do_to_inc_pl1; + break; } } else if (g.stream_enable_from) { g.use_pending = 1; - if (g.use_readinc) { - attachInterruptVector(IRQ_PORTB, - portb_isr_fixed_do_from); - attachInterruptVector(IRQ_PORTC, portc_isr_nop); - } - else { - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); - attachInterruptVector(IRQ_PORTC, - portc_isr_frameinc_do_from); + switch (g.inc_mode) { + case INC_MODE_VSYNC: + vsync_handler = vsync_isr_frameinc_do_from; + break; + case INC_MODE_SHARED_PL1: + pl1th_handler = pl1th_isr_fixed_do_from; + break; + case INC_MODE_SHARED_PL2: + /* TODO */ + break; } } + + attachInterruptVector(IRQ_PORTD, pl1th_handler); + attachInterruptVector(IRQ_PORTC, pl2th_handler); + attachInterruptVector(IRQ_PORTA, vsync_handler); +} + +static noinline void choose_isrs_idle(void) +{ + attachInterruptVector(IRQ_PORTD, pl1th_isr_fixed); + attachInterruptVector(IRQ_PORTC, pl2th_isr_fixed); + attachInterruptVector(IRQ_PORTA, vsync_isr_nop); } static void udelay(uint32_t us) @@ -242,8 +325,8 @@ static void do_start_seq(void) edge_cnt = g.edge_cnt; /* magic value */ - g.fixed_state[0] = - g.fixed_state[1] = 0x25; + g.pl[0].fixed_state[0] = + g.pl[0].fixed_state[1] = 0x25; for (tout = 10000; tout > 0; tout--) { edge_cnt_last = edge_cnt; @@ -252,12 +335,12 @@ static void do_start_seq(void) if (edge_cnt != edge_cnt_last) continue; - if (!(GPIOB_PDIR & CORE_PIN0_BITMASK)) + if (!PL1_TH()) break; } - g.fixed_state[0] = 0x33; - g.fixed_state[1] = 0x3f; + g.pl[0].fixed_state[0] = 0x33; + g.pl[0].fixed_state[1] = 0x3f; GPIOD_PDOR = 0x33; t1 = micros(); @@ -269,7 +352,7 @@ static void do_start_seq(void) for (tout = 100000; tout > 0; tout--) { udelay(1); - if (GPIOB_PDIR & CORE_PIN0_BITMASK) + if (PL1_TH()) break; } @@ -292,12 +375,12 @@ static void do_start_seq(void) return; } - if (g.stream_enable_to && g.t_i == g.t_o) { + if (g.stream_enable_to && g.pos_to_p[0].i == g.pos_to_p[0].o) { printf("got start_seq while stream_to is empty\n"); return; } - if (g.stream_enable_from && g.f_i != g.f_o) { + if (g.stream_enable_from && g.pos_from.i != g.pos_from.o) { printf("got start_seq while stream_from is not empty\n"); return; } @@ -311,42 +394,45 @@ static void do_start_seq(void) // callers must disable IRQs static void clear_state(void) { + int i; + g.stream_enable_to = 0; g.stream_enable_from = 0; g.stream_started = 0; g.stream_ended = 0; - g.use_readinc = 0; + g.inc_mode = INC_MODE_VSYNC; g.use_pending = 0; - g.t_i = g.t_o = 0; - g.f_i = g.f_o = 0; + for (i = 0; i < ARRAY_SIZE(g.pos_to_p); i++) + g.pos_to_p[i].i = g.pos_to_p[i].o = 0; + g.pos_from.i = g.pos_from.o = 0; g.frame_cnt = 0; - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); - attachInterruptVector(IRQ_PORTC, portc_isr_nop); + choose_isrs_idle(); } -static int get_space_to(void) +static int get_space_to(int p) { - return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK); + return STREAM_BUF_SIZE - ((g.pos_to_p[p].i - g.pos_to_p[p].o) + & STREAM_BUF_MASK); } static int get_used_from(void) { - return (g.f_i - g.f_o) & STREAM_BUF_MASK; + return (g.pos_from.i - g.pos_from.o) & STREAM_BUF_MASK; } static void do_usb(void *buf) { struct tas_pkt *pkt = buf; - uint32_t t_i, i; + uint32_t pos_to_i, i, p; int space; switch (pkt->type) { case PKT_FIXED_STATE: memcpy(&i, pkt->data, sizeof(i)); if (g.use_pending) - g.pending_state32 = i; + g.pl[0].pending_state32 = i; else - g.fixed_state32 = i; + g.pl[0].fixed_state32 = i; break; case PKT_STREAM_ENABLE: __disable_irq(); @@ -354,7 +440,7 @@ static void do_usb(void *buf) /* wait for start from MD */ g.stream_enable_to = pkt->enable.stream_to; g.stream_enable_from = pkt->enable.stream_from; - g.use_readinc = pkt->enable.use_readinc; + g.inc_mode = pkt->enable.inc_mode; if (pkt->enable.no_start_seq) { GPIOD_PDOR = 0x3f; choose_isrs(); @@ -371,20 +457,22 @@ static void do_usb(void *buf) g.stream_ended = 1; printf("end of stream\n"); break; - case PKT_STREAM_DATA_TO: - t_i = g.t_i; - space = get_space_to(); + case PKT_STREAM_DATA_TO_P1: + case PKT_STREAM_DATA_TO_P2: + p = pkt->type == PKT_STREAM_DATA_TO_P1 ? 0 : 1; + pos_to_i = g.pos_to_p[p].i; + space = get_space_to(p); if (space <= pkt->size / STREAM_EL_SZ) { printf("got data pkt while space=%d\n", space); return; } for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) { - memcpy(&g.stream_to[t_i++], + memcpy(&g.stream_to[p][pos_to_i++], pkt->data + i * STREAM_EL_SZ, STREAM_EL_SZ); - t_i &= STREAM_BUF_MASK; + pos_to_i &= STREAM_BUF_MASK; } - g.t_i = t_i; + g.pos_to_p[p].i = pos_to_i; break; default: printf("got unknown pkt type: %04x\n", pkt->type); @@ -392,6 +480,38 @@ static void do_usb(void *buf) } } +static void check_get_data(int p) +{ + struct tas_pkt pkt; + uint8_t buf[64]; + int ret; + + if (get_space_to(p) <= sizeof(pkt.data) / STREAM_EL_SZ) + return; + + if (g.pos_to_p[p].i == g.pos_to_p[p].o && g.frame_cnt != 0) { + printf("underflow detected\n"); + g.stream_enable_to = 0; + return; + } + + pkt.type = PKT_STREAM_REQ; + pkt.req.frame = g.frame_cnt; + pkt.req.is_p2 = p; + + ret = usb_rawhid_send(&pkt, 1000); + if (ret != sizeof(pkt)) { + printf("send STREAM_REQ/%d: %d\n", p, ret); + return; + } + + ret = usb_rawhid_recv(buf, 1000); + if (ret != 64) + printf("usb_rawhid_recv/s: %d\n", ret); + else + do_usb(buf); +} + int main(void) { uint32_t led_time = 0; @@ -399,31 +519,31 @@ int main(void) uint32_t edge_cnt_last; uint32_t edge_cnt; uint8_t buf[64]; - int ret; + int i, ret; delay(1000); // wait for usb.. /* ?0SA 00DU, ?1CB RLDU */ - g.fixed_state[0] = 0x33; - g.fixed_state[1] = 0x3f; + for (i = 0; i < 2; i++) { + g.pl[i].fixed_state[0] = 0x33; + g.pl[i].fixed_state[1] = 0x3f; + } printf("starting, rawhid: %d\n", usb_rawhid_available()); - // md pin th tr tl r l d u vsync - // md bit* 6 5 4 3 2 1 0 - // t bit b16 d5 d4 d3 d2 d1 d0 c6 - // t pin 0 20 6 8 7 14 2 11 + choose_isrs_idle(); + + // md pin th tr tl r l d u vsync th tr tl r l d u + // 7 9 6 4 3 2 1 7 9 6 4 3 2 1 + // md bit* 6 5 4 3 2 1 0 6 5 4 3 2 1 0 + // t bit d6 d5 d4 d3 d2 d1 d0 a12 c0 b17 b16 b3 b2 b1 b0 + // t pin 21 20 6 8 7 14 2 3 15 1 0 18 19 17 16 // * - note: tl/tr mixed in most docs - pinMode(0, INPUT); - attachInterrupt(0, portb_isr_fixed, CHANGE); - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); - pinMode(11, INPUT); - attachInterrupt(11, portc_isr_nop, RISING); - attachInterruptVector(IRQ_PORTC, portc_isr_nop); - - NVIC_SET_PRIORITY(IRQ_PORTB, 0); - NVIC_SET_PRIORITY(IRQ_PORTC, 16); - SCB_SHPR1 = SCB_SHPR2 = SCB_SHPR3 = 0x10101010; + + // player1 + pinMode(21, INPUT); + attachInterrupt(21, pl1th_isr_fixed, CHANGE); + NVIC_SET_PRIORITY(IRQ_PORTD, 0); pinMode( 2, OUTPUT); pinMode(14, OUTPUT); @@ -432,9 +552,29 @@ int main(void) pinMode( 6, OUTPUT); pinMode(20, OUTPUT); + // player2 + pinMode(15, INPUT); + attachInterrupt(15, pl1th_isr_fixed, CHANGE); + NVIC_SET_PRIORITY(IRQ_PORTC, 0); + + pinMode(16, OUTPUT); + pinMode(17, OUTPUT); + pinMode(19, OUTPUT); + pinMode(18, OUTPUT); + pinMode( 0, OUTPUT); + pinMode( 1, OUTPUT); + + // vsync line + pinMode(3, INPUT); + attachInterrupt(3, vsync_isr_nop, RISING); + NVIC_SET_PRIORITY(IRQ_PORTA, 16); + // led pinMode(13, OUTPUT); + // lower other priorities + SCB_SHPR1 = SCB_SHPR2 = SCB_SHPR3 = 0x10101010; + // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16 printf("GPIOB PDDR, PDIR: %08x %08x\n", GPIOB_PDIR, GPIOB_PDDR); printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR); @@ -453,44 +593,25 @@ int main(void) struct tas_pkt pkt; uint32_t now; - while (g.stream_enable_to && !g.stream_ended - && get_space_to() > sizeof(pkt.data) / STREAM_EL_SZ) - { - if (g.t_i == g.t_o && g.frame_cnt != 0) { - printf("underflow detected\n"); - g.stream_enable_to = 0; - break; - } - - pkt.type = PKT_STREAM_REQ; - pkt.req.frame = g.frame_cnt; - - ret = usb_rawhid_send(&pkt, 1000); - if (ret != sizeof(pkt)) { - printf("send STREAM_REQ: %d\n", ret); - break; - } - - ret = usb_rawhid_recv(buf, 1000); - if (ret != 64) - printf("usb_rawhid_recv/s: %d\n", ret); - else - do_usb(buf); + if (g.stream_enable_to && !g.stream_ended) { + check_get_data(0); + if (g.inc_mode == INC_MODE_SHARED_PL2) + check_get_data(1); } while (g.stream_enable_from && !g.stream_ended && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ) { - uint32_t f_o; + uint32_t o; int i; - f_o = g.f_o; + o = g.pos_from.o; for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) { - memcpy(pkt.data + i, &g.stream_from[f_o++], + memcpy(pkt.data + i, &g.stream_from[o++], STREAM_EL_SZ); - f_o &= STREAM_BUF_MASK; + o &= STREAM_BUF_MASK; } - g.f_o = f_o; + g.pos_from.o = o; pkt.type = PKT_STREAM_DATA_FROM; pkt.size = i; @@ -508,7 +629,7 @@ int main(void) if (now - scheck_time > 1000) { edge_cnt = g.edge_cnt; //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last, - // (GPIOB_PDIR >> CORE_PIN0_BIT) & 1); + // PL1_TH()); if ((g.stream_enable_to || g.stream_enable_from) && !g.stream_started && edge_cnt - edge_cnt_last > 10000) diff --git a/pkts.h b/pkts.h index 16877f6..4c25019 100644 --- a/pkts.h +++ b/pkts.h @@ -1,12 +1,13 @@ enum tas_pkt_type { - PKT_FIXED_STATE = 0xef01, - PKT_STREAM_ENABLE = 0xef02, - PKT_STREAM_REQ = 0xef03, - PKT_STREAM_DATA_TO = 0xef04, - PKT_STREAM_DATA_FROM = 0xef05, - PKT_STREAM_END = 0xef06, - PKT_STREAM_ABORT = 0xef07, + PKT_FIXED_STATE = 0xef01, + PKT_STREAM_ENABLE = 0xef02, + PKT_STREAM_REQ = 0xef03, + PKT_STREAM_DATA_TO_P1 = 0xef04, + PKT_STREAM_DATA_TO_P2 = 0xef05, + PKT_STREAM_DATA_FROM = 0xef06, + PKT_STREAM_END = 0xef07, + PKT_STREAM_ABORT = 0xef08, }; struct tas_pkt { @@ -16,13 +17,21 @@ struct tas_pkt { uint8_t data[60]; struct { uint32_t frame; // just fyi + uint8_t is_p2; } req; struct { uint8_t stream_to; uint8_t stream_from; // frame increment on read - uint8_t use_readinc; + uint8_t inc_mode; uint8_t no_start_seq; } enable; }; } __attribute__((packed)); + +enum inc_mode { + INC_MODE_VSYNC = 0, + // shared stream index incremented by pl1 or pl2 + INC_MODE_SHARED_PL1 = 1, + INC_MODE_SHARED_PL2 = 2, +}; -- 2.39.2