+static void portb_isr_fixed(void)
+{
+ uint32_t isfr, th;
+
+ isfr = PORTB_ISFR;
+ PORTB_ISFR = isfr;
+ th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
+
+ GPIOD_PDOR = g.fixed_state[th];
+ g.edge_cnt++;
+}
+
+static void portb_isr_readinc(void)
+{
+ uint32_t isfr, th;
+
+ isfr = PORTB_ISFR;
+ PORTB_ISFR = isfr;
+ th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
+
+ GPIOD_PDOR = g.stream[g.o][th];
+ if (th) {
+ g.o = (g.o + 1) & STREAM_BUF_MASK;
+ if (g.o == g.i)
+ // done
+ attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+ g.frame_cnt++;
+ }
+ g.edge_cnt++;
+}
+
+static void portb_isr_read(void)
+{
+ uint32_t isfr, th;
+
+ isfr = PORTB_ISFR;
+ PORTB_ISFR = isfr;
+ th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
+
+ GPIOD_PDOR = g.stream[g.o][th];
+ g.edge_cnt++;
+}
+
+static void portc_isr_nop(void)
+{
+ uint32_t isfr;
+
+ isfr = PORTC_ISFR;
+ PORTC_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)
+{
+ uint32_t isfr;
+
+ isfr = PORTC_ISFR;
+ PORTC_ISFR = isfr;
+
+ g.o = (g.o + 1) & STREAM_BUF_MASK;
+ if (g.o == g.i) {
+ attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+ }
+ g.frame_cnt++;
+}
+
+static void udelay(uint32_t us)
+{
+ uint32_t start = micros();
+
+ while ((micros() - start) < us) {
+ asm volatile("nop; nop; nop; nop");
+ yield();
+ }
+}
+
+static void do_start_seq(void)
+{
+ uint32_t edge_cnt_last;
+ uint32_t edge_cnt;
+ uint32_t start, t1, t2;
+ int tout;
+
+ start = micros();
+ edge_cnt = g.edge_cnt;
+
+ /* magic value */
+ g.fixed_state[0] =
+ g.fixed_state[1] = 0x25;
+
+ for (tout = 10000; tout > 0; tout--) {
+ edge_cnt_last = edge_cnt;
+ udelay(100);
+ edge_cnt = g.edge_cnt;
+
+ if (edge_cnt != edge_cnt_last)
+ continue;
+ if (!(GPIOB_PDIR & CORE_PIN0_BITMASK))
+ break;
+ }
+
+ g.fixed_state[0] = 0x33;
+ g.fixed_state[1] = 0x3f;
+ GPIOD_PDOR = 0x33;
+
+ t1 = micros();
+ if (tout == 0) {
+ printf("start_seq timeout1, t=%u\n", t1 - start);
+ return;
+ }
+
+ for (tout = 100000; tout > 0; tout--) {
+ udelay(1);
+
+ if (GPIOB_PDIR & CORE_PIN0_BITMASK)
+ break;
+ }
+
+ t2 = micros();
+ if (tout == 0) {
+ printf("start_seq timeout2, t1=%u, t2=%u\n",
+ t1 - start, t2 - t1);
+ return;
+ }
+
+ //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
+
+ if (g.stream_started) {
+ printf("got start_seq when already started\n");
+ return;
+ }
+
+ if (!g.stream_enable) {
+ printf("got start_seq, without enable from USB\n");
+ return;
+ }
+
+ if (g.i == g.o) {
+ printf("got start_seq while buffer is empty\n");
+ return;
+ }
+
+ __disable_irq();
+ g.stream_started = 1;
+ if (g.use_readinc) {
+ attachInterruptVector(IRQ_PORTB, portb_isr_readinc);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+ }
+ else {
+ attachInterruptVector(IRQ_PORTB, portb_isr_read);
+ attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
+ }
+ __enable_irq();
+}
+
+static int get_space(void)
+{
+ return STREAM_BUF_SIZE - ((g.i - g.o) & STREAM_BUF_MASK);
+}
+
+static void do_usb(void *buf)
+{
+ struct tas_pkt *pkt = buf;
+ uint32_t i, g_i;
+ int space;
+
+ switch (pkt->type) {
+ case PKT_FIXED_STATE:
+ memcpy(g.fixed_state, pkt->data, sizeof(g.fixed_state));
+ break;
+ case PKT_STREAM_ENABLE:
+ __disable_irq();
+ /* wait for start from MD */
+ g.stream_enable = 1;
+ g.stream_started = 0;
+ g.stream_received = 0;
+ g.use_readinc = pkt->start.use_readinc;
+ g.i = g.o = 0;
+ g.frame_cnt = 0;
+ attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
+ attachInterruptVector(IRQ_PORTC, portc_isr_nop);
+ __enable_irq();
+ break;
+ case PKT_STREAM_END:
+ g.stream_received = 1;
+ printf("end of stream\n");
+ break;
+ case PKT_STREAM_DATA:
+ g_i = g.i;
+ space = get_space();
+ if (space <= sizeof(pkt->data) / STREAM_EL_SZ) {
+ printf("got data pkt while space=%d\n", space);
+ return;
+ }
+ for (i = 0; i < sizeof(pkt->data) / STREAM_EL_SZ; i++) {
+ memcpy(&g.stream[g_i++],
+ pkt->data + i * STREAM_EL_SZ,
+ STREAM_EL_SZ);
+ g_i &= STREAM_BUF_MASK;
+ }
+ g.i = g_i;
+ break;
+ default:
+ printf("got unknown pkt type: %04x\n", pkt->type);
+ break;
+ }
+}
+