diff --git a/gg.c b/gg.c index 2dcb875..624c0b5 100644 --- a/gg.c +++ b/gg.c @@ -1,3 +1,6 @@ +#include + +#include "mmc.h" void timer_init_mega16(void) { @@ -49,3 +52,54 @@ void timer_init_tiny26(void) TIMSK = 1 << TOIE1;/* interrupt on overflow */ } + + +uint8_t length[4];/* remaining samples */ +uint8_t mmc_position[4];/* current reading position on mmc */ + +uint8_t overflows = 1;/* remaining counter overflows until next sample */ +#define SAMPLE_BITS 11 + +#define BUFFER_SIZE 92 +#define REFRESH_AMOUNT 64 +#define REFRESH_SIZE (BUFFER_SIZE - REFRESH_AMOUNT) +uint8_t buffer[BUFFER_SIZE];/* buffer for mmc data */ + +uint8_t pos = 0;/* current playing position */ +uint8_t refresh_buffer = 0;/* position to start buffer refreshing */ + +timerinterrupt() +{ + //if(!(TIFR & (1 << TOV1))) {continue;}/* interrupt flag polling */ + //TIFR |= (1 << TOV1);/* done by hardware */ + + overflows--; + if(!overflows) + { + overflows = 1 << (SAMPLE_BITS - 8); + pos = (pos + 1) % BUFFER_SIZE; + } + + /* TODO: a-law decoding */ + /* TODO: 11bit PWM emulation */ + OCR1A = buffer[pos];/* play next sample */ +} + + + +void main(void) +{ + if(mmc_init() != 0) {/* mmc fail */;} + timer_init_tiny26(); + sei(); + + for(;;) + { + if(pos == refresh_buffer)/* refresh buffer if running low */ + { + mmc_read(mmc_position, buffer + (ref + REFRESH_SIZE) % BUFFER_SIZE, BUFFER_SIZE - ref, buffer, REFRESH_AMOUNT - (BUFFER_SIZE - ref)); + /* pos on sd , first buffer address , 1st buffer size ,2nd buf, 2nd buffer size */ + refresh_buffer = (refresh_buffer + REFRESH_AMOUNT) % BUFFER_SIZE; + } + } +}