tabby/avr/main.c

127 lines
2.2 KiB
C
Raw Normal View History

2016-05-25 23:28:06 -05:00
#include <stdio.h>
#include <avr/interrupt.h>
2016-05-25 23:28:06 -05:00
#include <avr/sleep.h>
#include <tabby/clock.h>
#include <tabby/avr/uart.h>
2016-05-25 23:28:06 -05:00
static volatile uint8_t bits = 0;
static volatile uint8_t value_in = 0x00; /* Data coming in from Game Boy */
static volatile uint8_t value_out = 0x00; /* Data going out from host */
uint16_t timer_counter_intervals[4] = {
1953, 977, 61, 31
};
2016-05-25 23:28:06 -05:00
/*
* Internal clock source interrupt vector
*/
ISR(TIM0_COMPB_vect) {
/*
* Strobe the SCK pin
*/
PORTB |= (1 << PORTB1);
PORTB &= ~(1 << PORTB1);
value_in >>= 1;
if (PORTB & (1 << PORTB2)) {
value_in |= 0x80;
}
if (value_out & 0x80) {
PORTB |= (1 << PORTB3);
} else {
PORTB &= ~(1 << PORTB3);
}
value_out <<= 1;
if (--bits == 0) {
uart_putchar(value_in, NULL);
2016-05-25 23:28:06 -05:00
value_out = uart_getchar(NULL);
2016-05-25 23:28:06 -05:00
bits = 8;
}
}
static void clock_internal_setup(tabby_clock_speed speed) {
/*
* Configure MISO as output
*/
DDB3 |= (1 << PORTB3);
/*
* Configure MOSI as input
*/
DDB2 &= ~(1 << PORTB2);
/*
* Configure SCK pin as output
*/
DDB1 |= (1 << PORTB1);
/*
* Enable timer interrupt vector
*/
TIMSK = (1 << TOIE1);
/*
* Reset timer counter to zero
*/
TCNT1 = 0;
/*
* Set timer interval
*/
OCR1A = timer_counter_intervals[speed];
/*
* Set timer clock divider to 1/1
*/
TCCR1B = (1 << CS10);
}
2016-05-25 23:28:06 -05:00
static void clock_external_setup() {
/*
* Disable internal timer interrupts
*/
TCCR1B = 0;
OCR1A = 0;
TIMSK = 0;
}
2016-05-25 23:28:06 -05:00
static void snooze() {
set_sleep_mode(mode);
sleep_enable();
sleep_mode();
sleep_disable();
}
int main() {
/*
* Best turn on the serial UART!
*/
uart_init();
2016-05-25 23:28:06 -05:00
/*
* By default, the Game Boy link port is configured to monitor for external
* clock pulses, so we'll go ahead and do that in this case.
*/
clock_external_setup();
/*
* We do actually want to globally enable interrupts here, so here's that
* one assembly instruction to do so.
*/
sei();
while (1) {
snooze();
}
return 0;
}