#include 
#define F_CPU	8000000UL
#include 
#include 
#include 
#include 
#include 

/* Controller button/bit/byte mapping defines */
#define	NES_A			0	// nes_byte
#define	NES_B			1
#define	NES_SELECT		2
#define	NES_START		3
#define	NES_UP			4
#define	NES_DOWN		5
#define	NES_LEFT		6
#define NES_RIGHT		7

#define	PS_SELECT		0	// data[0] byte
#define	PS_L3			1
#define	PS_R3			2
#define	PS_START		3
#define	PS_UP			4
#define	PS_RIGHT		5
#define	PS_DOWN			6
#define	PS_LEFT			7

#define	PS_L2			0	// data[1] byte
#define	PS_R2			1
#define	PS_L1			2
#define	PS_R1			3
#define	PS_TRIANGLE		4
#define	PS_CIRCLE		5
#define	PS_CROSS		6
#define	PS_SQUARE		7

#define DSC_RJOY_X		dsc_data[2]	// byte in dsc_data array containing right joystick x-axis value
#define DSC_RJOY_Y		dsc_data[3]
#define DSC_LJOY_X		dsc_data[4]
#define DSC_LJOY_Y		dsc_data[5]

#define FINAL_RJOY_X		final_data[2]	// byte in final_data array containing right joystick x-axis value
#define FINAL_RJOY_Y		final_data[3]
#define FINAL_LJOY_X		final_data[4]
#define FINAL_LJOY_Y		final_data[5]

#define NUNCHUCK_Z		0
#define NUNCHUCK_C		1

#define NUNCHUCK_JOY_X		chuck_data[0]
#define NUNCHUCK_JOY_Y		chuck_data[1]
#define NUNCHUCK_TILT_X		chuck_data[2]
#define NUNCHUCK_TILT_Y		chuck_data[3]
#define NUNCHUCK_TILT_Z		chuck_data[4]
#define NUNCHUCK_BUTTON_BYTE	chuck_data[5]	// byte in chuck_data array which contains button states for the nunchuck

#define SAT_L			0
#define SAT_UP			1
#define SAT_RIGHT		2
#define SAT_DOWN		3
#define SAT_LEFT		4


#define SAT_R			0
#define SAT_Z			1
#define SAT_X			2
#define SAT_Y			3
#define SAT_C			4
#define SAT_A			5
#define SAT_B			6
#define SAT_START		7

/* NES controller pin defines */
#define NES_CLOCK		PC2
#define NES_LATCH		PC3
#define NES_DATA0		PC4
#define NES_DATA1		PC5
#define NES_DATA_PIN		PINC
#define NES_PORT		PORTC
#define NES_DDR			DDRC

/* saturn controller pin defines */
#define SAT_DATA0		PD2
#define SAT_DATA1		PD3
#define SAT_DATA2		PD4
#define SAT_DATA3		PD5
#define SAT_SELECT0		PD6
#define SAT_SELECT1		PD7
#define SAT_PORT		PORTD
#define SAT_DDR			DDRD
#define SAT_PIN			PIND

/* Bit-banged SPI defines AVR-DSC */
#define DATA			PA7
#define	COMMAND			PA6
#define CLOCK			PA4
#define ATTENTION		PA5
#define BB_PORT			PORTA
#define BB_PIN			PINA
#define BB_DDR			DDRA

/* interface button defines */
#define ANA_DIG_SELECT		PC6
#define ANA_DIG_SELECT_PORT	PORTC
#define ANA_DIG_SELECT_PIN	PINC
#define CONFIG_CYCLE		PC7
#define CONFIG_CYCLE_PORT	PORTC
#define CONFIG_CYCLE_PIN	PINC

/* Hardware SPI defines AVR-Ps2 */
#define MOSI			PB5	// SPI MOSI; receives commands from Ps2
#define MISO			PB6	// SPI MISO; sends outgoing data to Ps2
#define SCK			PB7	// SPI SCK; serial clock controlled by Ps2, data is read on rising edge
#define ACK			PB3	// ACKNOWLEDGE, brief low pulse after each byte transfer in packet, except after final byte
#define SS			PB4	// SPI slave select pin
#define SPI_PORT		PORTB
#define SPI_PIN			PINB
#define SPI_DDR			DDRB
#define ENABLE_SPI SPCR |= _BV(SPE)
#define DISABLE_SPI SPCR &= ~_BV(SPE)

/* TWI defines */
#define TWI_PORT		PORTC
#define TWI_DDR			DDRC
#define SCL			PC0		// TWI clock pin
#define SDA			PC1		// TWI data pin
#define NUNCHUCK_ADDRESS	0x52		// 7 bit TWI address for Wii nunchuck
#define ENABLE_TWI TWCR |= _BV(TWEN)
#define DISABLE_TWI	TWCR &= ~_BV(TWEN)
#define SEND_TWI_START TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN)	// master sends TWI start condition
#define SEND_TWI_STOP TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN)	// master sends TWI stop condition
#define TWI_STATUS_CHECK(x) ((TWSR & 0xF8) == x)	// checks TWI status register to ensure it contains expected value
/* write 1 to TOV2 bit of TIFR2 to reset flag; write 0x01 to TCNT2 to reset timer; waits for TWINT flag in TWI control register to be set or timer2 overflow flag to be set signifying either successful communication or timeout */
#define WAIT_FOR_TWINT(x) {\
	TIFR2 |= _BV(TOV2);\
	TCNT2 = x;\
	while((!(TWCR & _BV(TWINT))) && (!(TIFR2 & _BV(TOV2))));\
} 

/* shift register defines */
// shift register bit defines
#define SHIFT_REG_DATA0_BIT	0
#define SHIFT_REG_DATA1_BIT	1
#define SHIFT_REG_DATA2_BIT	2
#define SHIFT_REG_DATA3_BIT	3
#define SHIFT_REG_E_BIT		6
#define SHIFT_REG_RS_BIT	7
// shift register input pin defines
#define SHIFT_REG_DATA		PB0
#define SHIFT_REG_CLOCK		PB1
#define SHIFT_REG_LATCH		PB2
#define SHIFT_REG_PORT		PORTB
#define SHIFT_REG_DDR		DDRB
// Define LCD instructions
#define	BLINK_CURSOR		0x0F
#define	UNDERLINE_CURSOR	0x0E
#define	HIDE_CURSOR		0x0C
#define	FOUR_BIT_2_LINE		0x28
#define	CLEAR_SCREEN		0x01
#define	RETURN_HOME		0x02
#define DISPLAY_OFF		0x08
#define	MOVE_CURSOR_RIGHT	0x14
#define MOVE_CURSOR_LEFT	0x10
#define SCROLL_ALL_RIGHT	0x1E
#define SCROLL_ALL_LEFT		0x18
#define SECOND_LINE		0x40

/* General defines */
#define BAUDRATE		9600	// USART baudrate for RS232 communication
#define	STATUS_LED_MOTOR	PA3	// LED cathode on PA3; indicates vibration motor activity
#define STATUS_LED_GREEN	PA2	// Green LED cathode on PC6; indicates proper communication with DSC
#define	STATUS_LED_RED2		PA1	// Red LED cathode on PC4; indicates no PS/host or communication error	
#define STATUS_LED_GREEN2	PA0	// Green LED cathode on PC2; indicates proper communication with PS/host
#define LED_PORT		PORTA
#define LED_DDR			DDRA
#define TURBO_RATE		3
#define STANDARD_MODE		0
#define KATAMARI_MODE		1
#define RACING_MODE		2
#define ARCADE_MODE		3
#define TURBO_MODE		4
#define SERIAL_IN_MODE		5

/* command status register defines */
// command status register 0 "CSR0"
#define LOCK_BIT		0
#define ANALOG_MODE_BIT		1
#define CONFIG_MODE_BIT		2
#define COMM_OK_BIT		3
#define FIRST_MOTOR_BYTE_SMALL	4
#define FIRST_MOTOR_BYTE_LARGE	5
#define SECOND_MOTOR_BYTE_SMALL	6
#define SECOND_MOTOR_BYTE_LARGE	7
#define COMM_OK_CHECK CSR0 & _BV(COMM_OK_BIT)
#define COMM_IS_OK CSR0 |= _BV(COMM_OK_BIT)
#define COMM_IS_NOT_OK CSR0 &= ~_BV(COMM_OK_BIT)

/* game controller status register defines */
// game controller status register 0 "GCSR0"
#define DSC_CONNECT_STATUS_BIT		0
#define CHUCK_CONNECT_STATUS_BIT	1
#define SKIP_POLLING_BIT		2
#define BUTTON0_LOW_STATUS_BIT		3
#define BUTTON1_LOW_STATUS_BIT		4

/* macro status register defines */
// macro status register 0 "MSR0"
#define MACRO0_ACTIVE			0
#define MACRO0_TOGGLE			1

/* turbo status register defines */
// turbo status register 0 "TSR0"
#define TURBO0_ACTIVE			0
#define TURBO0_TOGGLE			1
#define TURBO1_ACTIVE			2
#define TURBO1_TOGGLE			3
#define TURBO2_ACTIVE			4
#define TURBO2_TOGGLE			5
#define TURBO3_ACTIVE			6
#define TURBO3_TOGGLE			7