X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=main.c;h=794317b0f2af73e308c5a52fa27c106819cf860a;hb=8e400118814010a69c98b468be02a6c750f4d4b1;hp=0cae7e6e5e64dcf783e3711e777b9d303ba7b3de;hpb=e8e66d024473bcf0c9e535cc3cb8b71677257687;p=megadrive.git diff --git a/main.c b/main.c index 0cae7e6..794317b 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,28 @@ +/* + * TeensyTAS, TAS input player for MegaDrive + * Copyright (c) 2014 notaz + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + #include #include #include @@ -22,11 +47,16 @@ static struct { uint8_t fixed_state[4]; uint32_t fixed_state32; }; + union { + uint8_t pending_state[4]; + uint32_t pending_state32; + }; 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 use_pending:1; uint32_t frame_cnt; uint32_t edge_cnt; uint32_t t_i; @@ -53,6 +83,7 @@ void yield(void) { } +/* portb handles TH */ static void portb_isr_fixed(void) { uint32_t isfr, th; @@ -65,6 +96,16 @@ static void portb_isr_fixed(void) g.edge_cnt++; } +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) + // done + attachInterruptVector(IRQ_PORTB, portb_isr_fixed); +} + static void portb_isr_do_to_inc(void) { uint32_t isfr, th; @@ -74,14 +115,8 @@ static void portb_isr_do_to_inc(void) th = (GPIOB_PDIR >> CORE_PIN0_BIT) & 1; GPIOD_PDOR = g.stream_to[g.t_o][th]; - if (th) { - g.t_o = (g.t_o + 1) & STREAM_BUF_MASK; - if (g.t_o == g.t_i) - // done - attachInterruptVector(IRQ_PORTB, portb_isr_fixed); - g.frame_cnt++; - } - g.edge_cnt++; + if (th) + do_to_step(); } static void portb_isr_do_to(void) @@ -128,6 +163,7 @@ static noinline void do_from_step(void) // 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; @@ -252,6 +288,7 @@ static void do_start_seq(void) } } else if (g.stream_enable_from) { + g.use_pending = 1; if (g.use_readinc) { attachInterruptVector(IRQ_PORTB, portb_isr_fixed_do_from); @@ -262,9 +299,6 @@ static void do_start_seq(void) attachInterruptVector(IRQ_PORTC, portc_isr_frameinc_do_from); } - - // prep first frame already - do_from_step(); } __enable_irq(); } @@ -274,9 +308,10 @@ static void clear_state(void) { g.stream_enable_to = 0; g.stream_enable_from = 0; - g.use_readinc = 0; g.stream_started = 0; g.stream_ended = 0; + g.use_readinc = 0; + g.use_pending = 0; g.t_i = g.t_o = 0; g.f_i = g.f_o = 0; g.frame_cnt = 0; @@ -302,7 +337,11 @@ static void do_usb(void *buf) switch (pkt->type) { case PKT_FIXED_STATE: - memcpy(g.fixed_state, pkt->data, sizeof(g.fixed_state)); + memcpy(&i, pkt->data, sizeof(i)); + if (g.use_pending) + g.pending_state32 = i; + else + g.fixed_state32 = i; break; case PKT_STREAM_ENABLE: __disable_irq(); @@ -345,10 +384,11 @@ static void do_usb(void *buf) int main(void) { + uint32_t led_time = 0; + uint32_t scheck_time = 0; uint32_t edge_cnt_last; uint32_t edge_cnt; uint8_t buf[64]; - int timeout; int ret; delay(1000); // wait for usb.. @@ -373,6 +413,7 @@ int main(void) NVIC_SET_PRIORITY(IRQ_PORTB, 0); NVIC_SET_PRIORITY(IRQ_PORTC, 16); + SCB_SHPR1 = SCB_SHPR2 = SCB_SHPR3 = 0x10101010; pinMode( 2, OUTPUT); pinMode(14, OUTPUT); @@ -390,19 +431,27 @@ int main(void) printf("GPIOD PDDR, PDIR: %08x %08x\n", GPIOD_PDIR, GPIOD_PDDR); printf("PORTB_PCR16: %08x\n", PORTB_PCR16); printf("PORTC_PCR6: %08x\n", PORTC_PCR6); + printf("PORTD_PCR0: %08x\n", PORTD_PCR0); asm("mrs %0, BASEPRI" : "=r"(ret)); - printf("BASEPRI: %d\n", ret); + printf("BASEPRI: %d, SHPR: %08x %08x %08x\n", + ret, SCB_SHPR1, SCB_SHPR2, SCB_SHPR3); - timeout = 1000; edge_cnt_last = g.edge_cnt; while (1) { 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; @@ -443,31 +492,43 @@ int main(void) } } - if (timeout == 1000) { + now = millis(); + + // start condition check + 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); - if (edge_cnt - edge_cnt_last > 10000) { + if ((g.stream_enable_to || g.stream_enable_from) + && !g.stream_started + && edge_cnt - edge_cnt_last > 10000) + { do_start_seq(); edge_cnt = g.edge_cnt; } edge_cnt_last = edge_cnt; + scheck_time = now; } - ret = usb_rawhid_recv(buf, timeout); - if (ret == 64) { - CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; - - do_usb(buf); - timeout = 20; - } - else if (ret == 0) { - CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; - timeout = 1000; + // led? + if (CORE_PIN13_PORTREG & CORE_PIN13_BITMASK) { + if ((int)(now - led_time) > 10) + CORE_PIN13_PORTCLEAR = CORE_PIN13_BITMASK; } - else { - printf("usb_rawhid_recv: %d\n", ret); - timeout = 1000; + + // something on rawhid? + if (usb_rawhid_available() > 0) + { + ret = usb_rawhid_recv(buf, 20); + if (ret == 64) { + led_time = millis(); + CORE_PIN13_PORTSET = CORE_PIN13_BITMASK; + + do_usb(buf); + } + else { + printf("usb_rawhid_recv: %d\n", ret); + } } }