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 noinline __attribute__((noinline))
37 #define STREAM_BUF_SIZE 512
38 #define STREAM_BUF_MASK (512 - 1)
40 /* ?0SA 00DU, ?1CB RLDU */
41 #define STREAM_EL_SZ 2
44 uint8_t stream_to[STREAM_BUF_SIZE][STREAM_EL_SZ];
45 uint8_t stream_from[STREAM_BUF_SIZE][STREAM_EL_SZ];
47 uint8_t fixed_state[4];
48 uint32_t fixed_state32;
51 uint8_t pending_state[4];
52 uint32_t pending_state32;
54 uint32_t stream_enable_to:1;
55 uint32_t stream_enable_from:1;
56 uint32_t stream_started:1;
57 uint32_t stream_ended:1;
58 uint32_t use_readinc:1;
59 uint32_t use_pending:1;
68 ssize_t _write(int fd, const void *buf, size_t nbyte)
73 if (fd != 1 && fd != 2) {
74 snprintf(tbuf, sizeof(tbuf), "write to fd %d\n", fd);
75 usb_seremu_write(tbuf, strlen(tbuf));
78 ret = usb_seremu_write(buf, nbyte);
79 return ret < 0 ? ret : nbyte;
86 /* portb handles TH */
87 static void portb_isr_fixed(void)
93 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
95 GPIOD_PDOR = g.fixed_state[th];
99 static noinline void do_to_step(void)
103 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
106 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
109 static void portb_isr_do_to_inc(void)
115 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
117 GPIOD_PDOR = g.stream_to[g.t_o][th];
122 static void portb_isr_do_to(void)
128 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
130 GPIOD_PDOR = g.stream_to[g.t_o][th];
134 static void portc_isr_nop(void)
142 // /vsync starts at line 235/259 (ntsc/pal), just as vcounter jumps back
143 // we care when it comes out (/vsync goes high) after 3 lines at 238/262
144 static void portc_isr_frameinc(void)
151 g.t_o = (g.t_o + 1) & STREAM_BUF_MASK;
152 if (g.t_o == g.t_i) {
153 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
154 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
159 /* "recording" data */
160 static noinline void do_from_step(void)
164 // should hopefully give atomic fixed_state read..
166 g.fixed_state32 = g.pending_state32;
167 g.stream_from[g.f_i][0] = s;
168 g.stream_from[g.f_i][1] = s >> 8;
169 g.f_i = (g.f_i + 1) & STREAM_BUF_MASK;
172 static void portb_isr_fixed_do_from(void)
178 th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1;
180 GPIOD_PDOR = g.fixed_state[th];
186 static void portc_isr_frameinc_do_from(void)
197 static void choose_isrs(void)
199 if (g.stream_enable_to) {
201 attachInterruptVector(IRQ_PORTB, portb_isr_do_to_inc);
202 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
205 attachInterruptVector(IRQ_PORTB, portb_isr_do_to);
206 attachInterruptVector(IRQ_PORTC, portc_isr_frameinc);
209 else if (g.stream_enable_from) {
212 attachInterruptVector(IRQ_PORTB,
213 portb_isr_fixed_do_from);
214 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
217 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
218 attachInterruptVector(IRQ_PORTC,
219 portc_isr_frameinc_do_from);
224 static void udelay(uint32_t us)
226 uint32_t start = micros();
228 while ((micros() - start) < us) {
229 asm volatile("nop; nop; nop; nop");
234 static void do_start_seq(void)
236 uint32_t edge_cnt_last;
238 uint32_t start, t1, t2;
242 edge_cnt = g.edge_cnt;
246 g.fixed_state[1] = 0x25;
248 for (tout = 10000; tout > 0; tout--) {
249 edge_cnt_last = edge_cnt;
251 edge_cnt = g.edge_cnt;
253 if (edge_cnt != edge_cnt_last)
255 if (!(GPIOB_PDIR & CORE_PIN0_BITMASK))
259 g.fixed_state[0] = 0x33;
260 g.fixed_state[1] = 0x3f;
265 printf("start_seq timeout1, t=%u\n", t1 - start);
269 for (tout = 100000; tout > 0; tout--) {
272 if (GPIOB_PDIR & CORE_PIN0_BITMASK)
278 printf("start_seq timeout2, t1=%u, t2=%u\n",
279 t1 - start, t2 - t1);
283 //printf(" t1=%u, t2=%u\n", t1 - start, t2 - t1);
285 if (g.stream_started) {
286 printf("got start_seq when already started\n");
290 if (!g.stream_enable_to && !g.stream_enable_from) {
291 printf("got start_seq, without enable from USB\n");
295 if (g.stream_enable_to && g.t_i == g.t_o) {
296 printf("got start_seq while stream_to is empty\n");
300 if (g.stream_enable_from && g.f_i != g.f_o) {
301 printf("got start_seq while stream_from is not empty\n");
307 g.stream_started = 1;
311 // callers must disable IRQs
312 static void clear_state(void)
314 g.stream_enable_to = 0;
315 g.stream_enable_from = 0;
316 g.stream_started = 0;
323 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
324 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
327 static int get_space_to(void)
329 return STREAM_BUF_SIZE - ((g.t_i - g.t_o) & STREAM_BUF_MASK);
332 static int get_used_from(void)
334 return (g.f_i - g.f_o) & STREAM_BUF_MASK;
337 static void do_usb(void *buf)
339 struct tas_pkt *pkt = buf;
344 case PKT_FIXED_STATE:
345 memcpy(&i, pkt->data, sizeof(i));
347 g.pending_state32 = i;
351 case PKT_STREAM_ENABLE:
354 /* wait for start from MD */
355 g.stream_enable_to = pkt->enable.stream_to;
356 g.stream_enable_from = pkt->enable.stream_from;
357 g.use_readinc = pkt->enable.use_readinc;
358 if (pkt->enable.no_start_seq) {
361 g.stream_started = 1;
365 case PKT_STREAM_ABORT:
372 printf("end of stream\n");
374 case PKT_STREAM_DATA_TO:
376 space = get_space_to();
377 if (space <= pkt->size / STREAM_EL_SZ) {
378 printf("got data pkt while space=%d\n", space);
381 for (i = 0; i < pkt->size / STREAM_EL_SZ; i++) {
382 memcpy(&g.stream_to[t_i++],
383 pkt->data + i * STREAM_EL_SZ,
385 t_i &= STREAM_BUF_MASK;
390 printf("got unknown pkt type: %04x\n", pkt->type);
397 uint32_t led_time = 0;
398 uint32_t scheck_time = 0;
399 uint32_t edge_cnt_last;
404 delay(1000); // wait for usb..
406 /* ?0SA 00DU, ?1CB RLDU */
407 g.fixed_state[0] = 0x33;
408 g.fixed_state[1] = 0x3f;
410 printf("starting, rawhid: %d\n", usb_rawhid_available());
412 // md pin th tr tl r l d u vsync
413 // md bit* 6 5 4 3 2 1 0
414 // t bit b16 d5 d4 d3 d2 d1 d0 c6
415 // t pin 0 20 6 8 7 14 2 11
416 // * - note: tl/tr mixed in most docs
418 attachInterrupt(0, portb_isr_fixed, CHANGE);
419 attachInterruptVector(IRQ_PORTB, portb_isr_fixed);
421 attachInterrupt(11, portc_isr_nop, RISING);
422 attachInterruptVector(IRQ_PORTC, portc_isr_nop);
424 NVIC_SET_PRIORITY(IRQ_PORTB, 0);
425 NVIC_SET_PRIORITY(IRQ_PORTC, 16);
426 SCB_SHPR1 = SCB_SHPR2 = SCB_SHPR3 = 0x10101010;
438 // CORE_PIN0_PORTSET CORE_PIN0_BITMASK PORTB_PCR16
439 printf("GPIOB PDDR, PDIR: %08x %08x\n", GPIOB_PDIR, GPIOB_PDDR);
440 printf("GPIOC PDDR, PDIR: %08x %08x\n", GPIOC_PDIR, GPIOC_PDDR);
441 printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR);
442 printf("PORTB_PCR16: %08x\n", PORTB_PCR16);
443 printf("PORTC_PCR6: %08x\n", PORTC_PCR6);
444 printf("PORTD_PCR0: %08x\n", PORTD_PCR0);
446 asm("mrs %0, BASEPRI" : "=r"(ret));
447 printf("BASEPRI: %d, SHPR: %08x %08x %08x\n",
448 ret, SCB_SHPR1, SCB_SHPR2, SCB_SHPR3);
450 edge_cnt_last = g.edge_cnt;
456 while (g.stream_enable_to && !g.stream_ended
457 && get_space_to() > sizeof(pkt.data) / STREAM_EL_SZ)
459 if (g.t_i == g.t_o && g.frame_cnt != 0) {
460 printf("underflow detected\n");
461 g.stream_enable_to = 0;
465 pkt.type = PKT_STREAM_REQ;
466 pkt.req.frame = g.frame_cnt;
468 ret = usb_rawhid_send(&pkt, 1000);
469 if (ret != sizeof(pkt)) {
470 printf("send STREAM_REQ: %d\n", ret);
474 ret = usb_rawhid_recv(buf, 1000);
476 printf("usb_rawhid_recv/s: %d\n", ret);
481 while (g.stream_enable_from && !g.stream_ended
482 && get_used_from() >= sizeof(pkt.data) / STREAM_EL_SZ)
488 for (i = 0; i < sizeof(pkt.data); i += STREAM_EL_SZ) {
489 memcpy(pkt.data + i, &g.stream_from[f_o++],
491 f_o &= STREAM_BUF_MASK;
495 pkt.type = PKT_STREAM_DATA_FROM;
498 ret = usb_rawhid_send(&pkt, 1000);
499 if (ret != sizeof(pkt)) {
500 printf("send DATA_FROM: %d\n", ret);
507 // start condition check
508 if (now - scheck_time > 1000) {
509 edge_cnt = g.edge_cnt;
510 //printf("e: %d th: %d\n", edge_cnt - edge_cnt_last,
511 // (GPIOB_PDIR >> CORE_PIN0_BIT) & 1);
512 if ((g.stream_enable_to || g.stream_enable_from)
514 && edge_cnt - edge_cnt_last > 10000)
517 edge_cnt = g.edge_cnt;
519 edge_cnt_last = edge_cnt;
524 if (CORE_PIN13_PORTREG & CORE_PIN13_BITMASK) {
525 if ((int)(now - led_time) > 10)
526 CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK;
529 // something on rawhid?
530 if (usb_rawhid_available() > 0)
532 ret = usb_rawhid_recv(buf, 20);
535 CORE_PIN13_PORTSET = CORE_PIN13_BITMASK;
540 printf("usb_rawhid_recv: %d\n", ret);