//ICC-AVR application builder : 2007-05-17 ¿ÀÈÄ 5:29:04
// Target : M128
// Crystal: 16.000Mhz
//
//  E-mail: nyanya01@hanmail.net
//
#include <iom128v.h>
#include <macros.h>
#include <stdio.h>
#include <ktm128.h>
#include <ktmlcd.h>
#define dt 1
volatile int matrix[10],touch=0,speed=100;
volatile char level=1,linecnt=0;
struct _object{
	int s[4];
	int d[4];
};
struct _object start; 
struct _object object;
int sample[7][3][2]={{{-2,0},{-1,0},{1,0}},
					{{1,-1},{-1,0},{1,0}},
					{{1,1},{-1,0},{1,0}},
					{{0,-1},{0,1},{-1,0}},
					{{0,-1},{-1,0},{-1,-1}},
					{{1,0},{0,-1},{-1,-1}},
					{{1,0},{0,1},{-1,1}}};
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x00;
 PORTG = 0x00;
 DDRG  = 0x00;
}

//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
 UCSR0B = 0x00; //disable while setting baud rate
 UCSR0A = 0x00;
 UCSR0C = 0x06;
 UBRR0L = 0x67; //set baud rate lo
 UBRR0H = 0x00; //set baud rate hi
 UCSR0B = 0xD8;
}

#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
 //uart has received a character in UDR
}

#pragma interrupt_handler uart0_tx_isr:21
void uart0_tx_isr(void)
{
 //character has been transmitted
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 uart0_init();
 
 MCUCR = 0x80;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void matrix_output(){
	int i;
	for(i=0;i<10;i++){
		EX_DM_SEL=0x0001<<i;
		EX_DM_DATA=matrix[i];
		delay(dt);
	}
}
void object_output(){
	int i,a,b;
	for(i=0;i<4;i++){
		if((object.s[i]<=0)||(object.s[i]>10))a=0;
		else a=object.s[i];
		if((object.d[i]<=0)||(object.d[i]>10))b=0;
		else b=object.d[i];
		EX_DM_SEL=0x0001<<a-1;
		EX_DM_DATA=0x0200>>b-1;
		delay(dt);
	}
}

void make_ob(){
	int i,base=0;
//	srand(0);
	base=(rand())%7;
	start.s[0]=0;
	start.d[0]=5;
	for(i=1;i<4;i++){
		start.s[i]=start.s[0]+sample[base][i-1][0];
		start.d[i]=start.d[0]+sample[base][i-1][1];
	}
	object=start;
	speed=100;
}

void down(){
	int i;
	for(i=0;i<4;i++){
		object.s[i]++;
	}
}

void checktop(){
	int i;
	for(i=0;i<4;i++){
		if(matrix[object.s[i]]&(0x0200>>object.d[i]-1)){
		touch=1;
		}
		if(object.s[i]==10)touch=1;
	}
}

void merge(){
	int i;
	for(i=0;i<4;i++){
		matrix[object.s[i]-1]|=(0x0200>>(object.d[i]-1));
	}
}

void right(){
	int i;
	for(i=0;i<4;i++){
		if((object.d[i])>=10)return;
		if((matrix[object.s[i]-1])&(0x0200>>object.d[i]))return;		
	}
	for(i=0;i<4;i++){
		object.d[i]++;
	}
}
void left(){
	int i;
	for(i=0;i<4;i++){
		if((object.d[i])<=1)return;
		if((matrix[object.s[i]-1])&(0x0200>>object.d[i]-2))return;
	}
	for(i=0;i<4;i++){
		object.d[i]--;
	}
}
void turn(){
	int temp[2],i;
	for(i=1;i<4;i++){
		if(matrix[object.s[0]+object.d[i]-object.d[0]-1]
		&0x0200>>(object.d[0]-object.s[i]+object.s[0]-1))return;
	}
	for(i=1;i<4;i++){
		temp[1]=object.d[0]-object.s[i]+object.s[0];
		temp[0]=object.s[0]+object.d[i]-object.d[0];
		object.s[i]=temp[0];
		object.d[i]=temp[1];
		if(object.d[i]<1)right();
		if(object.d[i]>10)left();
	}
}
void fast(){
	speed=10;
}

void move(){
	int temp=0;
	if(PINB&0x01)right();
	else if(PINB&0x04)left();
	else if(PINB&0x02)turn();
	else if(PINB&0x08)fast();
/*	while(PINB){
		matrix_output();
		object_output();
	}*/
}

void linecheck(){
	int i,j,remerge=0;
	for(i=9;i>=0;i--){
		if(remerge){
			for(j=i;j>0;j--)matrix[j]=matrix[j-1];
			matrix[0]=0;
			remerge=0;
		}
		if(matrix[i]==0x3ff){
			i++;
			remerge=1;			
			linecnt++;
			lcd_puts(2,"L");
			lcd_putn2(level);
			lcd_putss(" score:");
			lcd_putn3(20*(level-1)+linecnt);					
		}
	}
}
void gameinit(){
	int i;
	for(i=0;i<10;i++)matrix[i]=0;
	for(i=0;i<4;i++){object.s[i]=0;object.d[i]=0;}
	touch=0;
	speed=100;
	level=1;
	linecnt=0;
	lcdClear();
	delay(dt*2);
	lcd_puts(1,"game start");
	lcd_puts(2,"L");
	lcd_putn2(level);
	lcd_putss(" score:");
	lcd_putn3(20*(level-1)+linecnt);
	make_ob();
}
	
void gameover(){
	if(matrix[0]&(0x0200>>4)){
	EX_LED^=0xff;
	lcdClear();
	delay(dt*2);
	lcd_puts(1,"game over");
	lcd_puts(2,"L");
	lcd_putn2(level);
	lcd_putss(" score:");
	lcd_putn3(20*(level-1)+linecnt);	
	while(!(PINB&0x80));
	gameinit();
	}
}
void complete(){
	lcdClear();
	delay(dt*2);
	lcd_puts(1,"game complete");
	lcd_puts(2,"L");
	lcd_putn2(level);
	lcd_putss(" score:");
	lcd_putn3(20*(level-1)+linecnt);
	while(!(PINB&0x80));
	gameinit();
}

void levelcheck(){
	if(linecnt>=20){
 		level++;
		linecnt=0;
		speed-=10;
		lcdRegWrite(0xc0);
		delay(dt);
		lcd_puts(2,"L");
		lcd_putn2(level);
		lcd_putss(" score:");
		lcd_putn3(20*(level-1)+linecnt);
	}
	if(level>=10){complete();}
}

void main(void)
{int cnt=0;
 char oldinput=0;
 init_devices();
 ktm128_init();
 lcdInit();
 delay(dt);
 gameinit();
// make_ob();

 while(1){
 	if(touch){
		merge();
	 	make_ob();
		touch=0;
	 }
	 if(!cnt)down();
	 if(cnt==speed-1)checktop();
	 linecheck();	 
	 levelcheck();
	 matrix_output();
	 object_output();
	 if(PINB&&(!oldinput)){
	 	move();
		oldinput=PINB;
	 }
	 else if((!PINB)&&oldinput)oldinput=PINB;
	 cnt++;
	 cnt%=speed;
	 gameover();
 	}
 //insert your functional code here...
}

