2 * TeensyTAS, TAS input player for MegaDrive
3 * Copyright (c) 2014 notaz
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include "teensy3/core_pins.h"
30 #include "teensy3/usb_seremu.h"
31 #include "teensy3/usb_rawhid.h"
34 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
36 #define noinline __attribute__((noinline))
39 #define STREAM_BUF_SIZE 512
40 #define STREAM_BUF_MASK (512 - 1)
42 /* ?0SA 00DU, ?1CB RLDU */
43 #define STREAM_EL_SZ 2
46 uint8_t stream_to[2][STREAM_BUF_SIZE][STREAM_EL_SZ];
47 uint8_t stream_from[STREAM_BUF_SIZE][STREAM_EL_SZ];
50 uint8_t fixed_state[4];
51 uint32_t fixed_state32;
54 uint8_t pending_state[4];
55 uint32_t pending_state32;
58 uint32_t stream_enable_to:1;
59 uint32_t stream_enable_from:1;
60 uint32_t stream_started:1;
61 uint32_t stream_ended:1;
63 uint32_t use_pending:1;
69 } pos_to_p[2], pos_from;
72 ssize_t _write(int fd, const void *buf, size_t nbyte)
77 if (fd != 1 && fd != 2) {
78 snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd);
79 usb_seremu_write(tbuf, strlen(tbuf));
82 ret = usb_seremu_write(buf, nbyte);
83 return ret < 0 ? ret : nbyte;
90 static noinline void choose_isrs_idle(void);
93 #define PL1_ISFR PORTD_ISFR
94 #define PL1_TH() ((CORE_PIN21_PINREG >> CORE_PIN21_BIT) & 1)
96 static void pl1th_isr_fixed(void)
104 GPIOD_PDOR = g.pl[0].fixed_state[th];
108 static noinline void do_to_step_pl1(void)
112 g.pos_to_p[0].o = (g.pos_to_p[0].o + 1) & STREAM_BUF_MASK;
113 if (g.pos_to_p[0].o == g.pos_to_p[0].i)
118 static void pl1th_isr_do_to_inc(void)
126 GPIOD_PDOR = g.stream_to[0][g.pos_to_p[0].o][th];
131 static void pl1th_isr_do_to(void)
139 GPIOD_PDOR = g.stream_to[0][g.pos_to_p[0].o][th];
144 #define PL2_ISFR PORTC_ISFR
145 #define PL2_TH() ((CORE_PIN15_PINREG >> CORE_PIN15_BIT) & 1)
146 #define PL2_ADJ(x) ((x) | ((x) << 12))
148 static void pl2th_isr_fixed(void)
150 uint32_t isfr, th, v;
156 v = g.pl[1].fixed_state[th];
157 GPIOB_PDOR = PL2_ADJ(v);
160 static noinline void do_to_step_pl2(void)
162 g.pos_to_p[1].o = (g.pos_to_p[1].o + 1) & STREAM_BUF_MASK;
163 if (g.pos_to_p[1].o == g.pos_to_p[1].i)
168 static void pl2th_isr_do_to_inc(void)
170 uint32_t isfr, th, v;
176 v = g.stream_to[1][g.pos_to_p[1].o][th];
177 GPIOB_PDOR = PL2_ADJ(v);
182 static void pl2th_isr_do_to_p1d(void)
184 uint32_t isfr, th, v;
190 v = g.stream_to[1][g.pos_to_p[0].o][th];
191 GPIOB_PDOR = PL2_ADJ(v);
193 g.pos_to_p[1].o = g.pos_to_p[0].o;
196 static void pl2th_isr_do_to_inc_pl1(void)
198 uint32_t isfr, th, v;
204 v = g.stream_to[1][g.pos_to_p[1].o][th];
205 GPIOB_PDOR = PL2_ADJ(v);
208 g.pos_to_p[1].o = g.pos_to_p[0].o;
213 #define VSYNC_ISFR PORTC_ISFR
215 static void vsync_isr_nop(void)
223 // /vsync starts at line 235/259 (ntsc/pal), just as vcounter jumps back
224 // we care when it comes out (/vsync goes high) after 3 lines at 238/262
225 static void vsync_isr_frameinc(void)
232 g.pos_to_p[0].o = (g.pos_to_p[0].o + 1) & STREAM_BUF_MASK;
233 g.pos_to_p[1].o = g.pos_to_p[0].o;
235 if (g.pos_to_p[0].o == g.pos_to_p[0].i)
240 /* "recording" data */
241 static noinline void do_from_step(void)
245 // should hopefully give atomic fixed_state read..
246 s = g.pl[0].fixed_state32;
247 g.pl[0].fixed_state32 = g.pl[0].pending_state32;
248 g.stream_from[g.pos_from.i][0] = s;
249 g.stream_from[g.pos_from.i][1] = s >> 8;
250 g.pos_from.i = (g.pos_from.i + 1) & STREAM_BUF_MASK;
253 static void pl1th_isr_fixed_do_from(void)
261 GPIOD_PDOR = g.pl[0].fixed_state[th];
267 static void vsync_isr_frameinc_do_from(void)
279 static void choose_isrs(void)
281 void (*pl1th_handler)(void) = pl1th_isr_fixed;
282 void (*pl2th_handler)(void) = pl2th_isr_fixed;
283 void (*vsync_handler)(void) = vsync_isr_nop;
285 if (g.stream_enable_to) {
286 switch (g.inc_mode) {
288 pl1th_handler = pl1th_isr_do_to;
289 pl2th_handler = pl2th_isr_do_to_p1d;
290 vsync_handler = vsync_isr_frameinc;
292 case INC_MODE_SHARED_PL1:
293 pl1th_handler = pl1th_isr_do_to_inc;
294 pl2th_handler = pl2th_isr_do_to_p1d;
296 case INC_MODE_SHARED_PL2:
297 pl1th_handler = pl1th_isr_do_to;
298 pl2th_handler = pl2th_isr_do_to_inc_pl1;
300 case INC_MODE_SEPARATE:
301 pl1th_handler = pl1th_isr_do_to_inc;
302 pl2th_handler = pl2th_isr_do_to_inc;
306 else if (g.stream_enable_from) {
308 switch (g.inc_mode) {
310 vsync_handler = vsync_isr_frameinc_do_from;
312 case INC_MODE_SHARED_PL1:
313 pl1th_handler = pl1th_isr_fixed_do_from;
315 case INC_MODE_SHARED_PL2:
316 case INC_MODE_SEPARATE:
322 attachInterruptVector(IRQ_PORTD, pl1th_handler);
323 attachInterruptVector(IRQ_PORTC, pl2th_handler);
324 attachInterruptVector(IRQ_PORTA, vsync_handler);
327 static noinline void choose_isrs_idle(void)
329 attachInterruptVector(IRQ_PORTD, pl1th_isr_fixed);
330 attachInterruptVector(IRQ_PORTC, pl2th_isr_fixed);
331 attachInterruptVector(IRQ_PORTA, vsync_isr_nop);
334 static void udelay(uint32_t us)
336 uint32_t start = micros();
338 while ((micros() - start) < us) {
339 asm volatile("nop; nop; nop; nop");
344 static void do_start_seq(void)
346 uint32_t edge_cnt_last;
348 uint32_t start, t1, t2;
352 edge_cnt = g.edge_cnt;
355 g.pl[0].fixed_state[0] =
356 g.pl[0].fixed_state[1] = 0x25;
358 for (tout = 10000; tout > 0; tout--) {
359 edge_cnt_last = edge_cnt;
361 edge_cnt = g.edge_cnt;
363 if (edge_cnt != edge_cnt_last)
369 g.pl[0].fixed_state[0] = 0x33;
370 g.pl[0].fixed_state[1] = 0x3f;
375 printf("start_seq timeout1, t=%u\n", t1 - start);
379 for (tout = 100000; tout > 0; tout--) {
388 printf("start_seq timeout2, t1=%u, t2=%u\n",
389 t1 - start, t2 - t1);
393 //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
395 if (g.stream_started) {
396 printf("got start_seq when already started\n");
400 if (!g.stream_enable_to && !g.stream_enable_from) {
401 printf("got start_seq, without enable from USB\n");
405 if (g.stream_enable_to && g.pos_to_p[0].i == g.pos_to_p[0].o) {
406 printf("got start_seq while stream_to is empty\n");
410 if (g.stream_enable_from && g.pos_from.i != g.pos_from.o) {
411 printf("got start_seq while stream_from is not empty\n");
417 g.stream_started = 1;
421 // callers must disable IRQs
422 static void clear_state(void)
426 g.stream_enable_to = 0;
427 g.stream_enable_from = 0;
428 g.stream_started = 0;
430 g.inc_mode = INC_MODE_VSYNC;
432 for (i = 0; i < ARRAY_SIZE(g.pos_to_p); i++)
433 g.pos_to_p[i].i = g.pos_to_p[i].o = 0;
434 g.pos_from.i = g.pos_from.o = 0;
436 memset(g.stream_to[1], 0x3f, sizeof(g.stream_to[1]));
440 static int get_space_to(int p)
442 return STREAM_BUF_SIZE - ((g.pos_to_p[p].i - g.pos_to_p[p].o)
446 static int get_used_from(void)
448 return (g.pos_from.i - g.pos_from.o) & STREAM_BUF_MASK;
451 static void do_usb(void *buf)
453 struct tas_pkt *pkt = buf;
454 uint32_t pos_to_i, i, p;
458 case PKT_FIXED_STATE:
459 memcpy(&i, pkt->data, sizeof(i));
461 g.pl[0].pending_state32 = i;
463 g.pl[0].fixed_state32 = i;
465 case PKT_STREAM_ENABLE:
468 /* wait for start from MD */
469 g.stream_enable_to = pkt->enable.stream_to;
470 g.stream_enable_from = pkt->enable.stream_from;
471 g.inc_mode = pkt->enable.inc_mode;
472 if (pkt->enable.no_start_seq) {
475 g.stream_started = 1;
479 case PKT_STREAM_ABORT:
486 printf("end of stream\n");
488 case PKT_STREAM_DATA_TO_P1:
489 case PKT_STREAM_DATA_TO_P2:
490 p = pkt->type == PKT_STREAM_DATA_TO_P1 ? 0 : 1;
491 pos_to_i = g.pos_to_p[p].i;
492 space = get_space_to(p);
493 if (space <= pkt->size / STREAM_EL_SZ) {
494 printf("got data pkt while space=%d\n", space);
497 for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) {
498 memcpy(&g.stream_to[p][pos_to_i++],
499 pkt->data + i * STREAM_EL_SZ,
501 pos_to_i &= STREAM_BUF_MASK;
503 g.pos_to_p[p].i = pos_to_i;
506 printf("got unknown pkt type: %04x\n", pkt->type);
511 static void check_get_data(int p)
517 if (get_space_to(p) <= sizeof(pkt.data) / STREAM_EL_SZ)
520 if (g.pos_to_p[p].i == g.pos_to_p[p].o && g.frame_cnt != 0) {
521 printf("underflow detected\n");
522 g.stream_enable_to = 0;
526 pkt.type = PKT_STREAM_REQ;
527 pkt.req.frame = g.frame_cnt;
530 ret = usb_rawhid_send(&pkt, 1000);
531 if (ret != sizeof(pkt)) {
532 printf("send STREAM_REQ/%d: %d\n", p, ret);
536 ret = usb_rawhid_recv(buf, 1000);
538 printf("usb_rawhid_recv/s: %d\n", ret);
545 uint32_t led_time = 0;
546 uint32_t scheck_time = 0;
547 uint32_t edge_cnt_last;
552 delay(1000); // wait for usb..
554 /* ?0SA 00DU, ?1CB RLDU */
555 for (i = 0; i < 2; i++) {
556 g.pl[i].fixed_state[0] = 0x33;
557 g.pl[i].fixed_state[1] = 0x3f;
560 printf("starting, rawhid: %d\n", usb_rawhid_available());
564 // md pin th tr tl r l d u vsync th tr tl r l d u
565 // 7 9 6 4 3 2 1 7 9 6 4 3 2 1
566 // md bit* 6 5 4 3 2 1 0 6 5 4 3 2 1 0
567 // t bit d6 d5 d4 d3 d2 d1 d0 a12 c0 b17 b16 b3 b2 b1 b0
568 // t pin 21 20 6 8 7 14 2 3 15 1 0 18 19 17 16
569 // * - note: tl/tr mixed in most docs
573 attachInterrupt(21, pl1th_isr_fixed, CHANGE);
574 NVIC_SET_PRIORITY(IRQ_PORTD, 0);
585 attachInterrupt(15, pl1th_isr_fixed, CHANGE);
586 NVIC_SET_PRIORITY(IRQ_PORTC, 0);
597 attachInterrupt(3, vsync_isr_nop, RISING);
598 NVIC_SET_PRIORITY(IRQ_PORTA, 16);
603 // lower other priorities
604 SCB_SHPR1 = SCB_SHPR2 = SCB_SHPR3 = 0x10101010;
606 // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16
607 printf("GPIOB PDDR, PDIR: %08x %08x\n", GPIOB_PDIR, GPIOB_PDDR);
608 printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR);
609 printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR);
610 printf("PORTB_PCR16: %08x\n", PORTB_PCR16);
611 printf("PORTC_PCR6: %08x\n", PORTC_PCR6);
612 printf("PORTD_PCR0: %08x\n", PORTD_PCR0);
614 asm("mrs %0, BASEPRI" : "=r"(ret));
615 printf("BASEPRI: %d, SHPR: %08x %08x %08x\n",
616 ret, SCB_SHPR1, SCB_SHPR2, SCB_SHPR3);
618 edge_cnt_last = g.edge_cnt;
624 if (g.stream_enable_to && !g.stream_ended) {
626 if (g.inc_mode == INC_MODE_SHARED_PL2
627 || g.inc_mode == INC_MODE_SEPARATE)
631 while (g.stream_enable_from && !g.stream_ended
632 && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ)
638 for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) {
639 memcpy(pkt.data + i, &g.stream_from[o++],
641 o &= STREAM_BUF_MASK;
645 pkt.type = PKT_STREAM_DATA_FROM;
648 ret = usb_rawhid_send(&pkt, 1000);
649 if (ret != sizeof(pkt)) {
650 printf("send DATA_FROM: %d\n", ret);
657 // start condition check
658 if (now - scheck_time > 1000) {
659 edge_cnt = g.edge_cnt;
660 //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
662 if ((g.stream_enable_to || g.stream_enable_from)
664 && edge_cnt - edge_cnt_last > 10000)
667 edge_cnt = g.edge_cnt;
669 edge_cnt_last = edge_cnt;
674 if (CORE_PIN13_PORTREG & CORE_PIN13_BITMASK) {
675 if ((int)(now - led_time) > 10)
676 CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
679 // something on rawhid?
680 if (usb_rawhid_available() > 0)
682 ret = usb_rawhid_recv(buf, 20);
685 CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
690 printf("usb_rawhid_recv: %d\n", ret);