
//Jian Lu================================================
#include <stdio.h>
#include <system.h>
#include <io.h>
#include "functions.h"
#include <sys/alt_irq.h> // the irq functions

#define random(x) (rand()%x)


int hex_to_ten(int hex)
{
    char buf[100];
    sprintf(buf,"%d",hex);
    return atoi(buf);
}

int mole(int num){
    if (num==1) return 100000000;
    else if (num==2) return 010000000;
    else if (num==3) return 001000000;
    else if (num==4) return 000100000;
    else if (num==5) return 000010000;
    else if (num==6) return 000001000;
    else if (num==7) return 000000100;
    else if (num==8) return 000000010;
    else if (num==9) return 000000001;
    else return 000000000;
}

int touch(int state){
	int x;
	int y;
	x = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
	y = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	if(state == 0x4 || state == 0x5 || state == 0x6 || state == 0x7 )
	{
		if(x>0xA10 && x< 0xED0 && y> 0x430 && y< 0x7F0)
			return 0;
		else if(x>0xA10 && x< 0xED0 && y>0x7F1 && y< 0xBB0)
			return 1;
		else if(x>0xA10 && x< 0xED0 && y>0xBB0 && y< 0xF40)
			return 2;
		else if(x>0x530 && x< 0xA10 && y> 0x430 && y< 0x7F0)
			return 3;
		else if(x>0x530 && x< 0xA10 && y>0x7F1 && y< 0xBB0)
			return 4;
		else if(x>0x530 && x< 0xA10 && y>0xBB0 && y< 0xF40)
			return 5;
		else if(x>0x0C0 && x< 0x530 && y> 0x430 && y< 0x7F0)
			return 6;
		else if(x>0x0C0 && x< 0x530 && y>0x7F1 && y< 0xBB0)
			return 7;
		else if(x>0x0C0 && x< 0x530 && y>0xBB0 && y< 0xF40)
			return 8;
		else if(x>0x530 && x<0x790 && y>0x0 && y<0x430)
			return 9; //start key
		else if(x>0x2F0 && x< 0x530 && y>0x0 && y<0x430)
			return 0xA;//pause key
		else if(x>0x0 && x< 0x2F0 && y>0x0 && y<0x430)
			return 0xB; //quit key
		else
			return 0xC;
	}

	else if(state == 0x0 || state == 0x1 || state == 0x2 || state == 0x3)
	{
		if(x>0x810 && x< 0xA90 && y> 0x580 && y< 0xA10)
			return 0;// easy
		else if(x>0x610 && x< 0x810 && y> 0x580 && y< 0xA10)
			return 1; // normal
		else if(x>0x370 && x< 0x610 && y> 0x580 && y< 0xA10)
			return 2; // hard
		else if(x>0x0 && x< 0x350 && y> 0xCD0 && y< 0xF98)
			return 3; //go
		else
			return 4;
	}
}

int mole1(int num){
    if (num==0) return 0x001;
    if (num==1) return 0x002;
    if (num==2) return 0x004;
    if (num==3) return 0x008;
    if (num==4) return 0x010;
    if (num==5) return 0x020;
    if (num==6) return 0x040;
    if (num==7) return 0x080;
    if (num==8) return 0x100;
}

int i=0;
int j;
int m;
int n;
unsigned data_temp;

static void standbymusic(){
	printf("N\n");
	 for(j=0;j<256;j++)
	     {
		 IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,standby[i]);
		 i++;
		 if(i >24533 ){
		 			i = 0;
		 		}
		 if(i == 24523 ) printf("Y\n");
	     }
}

int state_sel(int state)
{
	switch(state)
	{
	case 0x0:
		if(touch(state) != 4)
		{
			if(touch(state)== 0)
				state = 0x0;
			else if(touch(state) == 1)
				state = 0x1;
			else if(touch(state) == 2)
				state = 0x2;
			else if(touch(state)==3)
				state = 0x4;
		}
		break;

	case 0x1:
		if(touch(state) != 4)
		{
			if(touch(state)== 0)
				state = 0;
			else if(touch(state) == 1)
				state = 0x1;
			else if(touch(state) == 2)
				state = 0x2;
			else if(touch(state)==3)
				state = 0x4;
		}
		break;

	case 0x2:
		if(touch(state) != 4)
		{
			if(touch(state)== 0)
				state = 0;
			else if(touch(state) == 1)
				state = 0x1;
			else if(touch(state) == 2)
				state = 0x2;
			else if(touch(state)==3)
				state = 0x4;
		}
		break;
	//unused state
	case 0x3:
		break;

	//PLAY-INIT
	case 0x4:
		if(touch(state) == 0xB)
			state = 0x0;
		else if(touch(state) == 0x9)
			state = 0x5;
		break;

	//PLAY- ONGOING
	case 0x5:
		if(touch(state) == 0xB)
			state = 0x0;
		else if(touch(state) == 0xA)
			state = 0x6;

		break;

	//PLAY- PAUSE
	case 0x6:
		if(touch(state) == 0xB)
			state = 0x0;
		else if(touch(state) == 0x9)
			state = 0x5;
		break;

	case 0x7:
		break;
	case 0x8:
		break;
	case 0x9:
		break;
	case 0xA:
		break;
	case 0xB:
		break;
	case 0xC:
		break;
	case 0xD:
		break;
	case 0xE:
		break;
	case 0xF:
		break;
	}
	return state;
}

int main (int argc, const char * argv[])
{
	init();
	int state = 0x0;
	int score0 = 0;
	int score1 = 0;
	int score2 = 0;
	int life = 0;
	long int panel_command;
	long int mole_command;
	int previousx;
	int previousy;
	int currentx;
	int currenty;
	int counter = 0;

//Music interrupt
	alt_irq_register(MUSICCONTROLLER_0_IRQ, NULL,(void*)standbymusic);


	previousx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
	previousy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	while(1)
	{
		currentx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
		currenty = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
		if(life == 0) state = 0x8;
		else
			if(currentx != previousx || currenty != previousy)
			{
				state = state_sel(state);
			}

		panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
		mole_command = 0x4;
		IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
		IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
		//printf("%x\n",touch(state));
	}
/*
//	init();
	int state = 1;
	int life = 5;
	int score0 = 0;
	int score1 = 0;
	int score2 = 0;
    int counter=0;
    int previousx=0;
    int previousy=0;
    int inputx = 0;
    int inputy = 0;
    int random_number = 0;
    long int panel_command;
  //  int mole_position=100000000;
    int mole_position=0;
    int touch_position= 0;

    IOWR_CONTROL(COMMAND_0_BASE,1,0x00000000);
	previousx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
    previousy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
    while(1)
    {
    	random_number = rand()%9;
    	panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
    	IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
    	//if(random_number == 9) random_number = 0;
    	mole_position=mole1(random_number);
    	printf("mole_position = 0x%x",mole_position);
        while(counter<100000){
            counter++;
        	inputx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
        	inputy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
        	touch_position = touch();
            if (inputx!=previousx || inputy != previousy)
            {
                if (touch_position==random_number)
                {	if(score0<9)
                		score0++;
                	else if(score1<9)
                	{
                		score0 = 0;
                		score1++;
                	}
                	else
                	{
						score0 = 0;
						score1 = 0;
						score2 ++;
                	}


                    break;
                }
                else
                {
                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
                }

            }
            else
            {
            //	printf("E");
            	IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
            }
        	previousx = inputx;
            previousy = inputy;
        }
      //  IOWR_LED_DATA(base,0,000000000);
        IOWR_CONTROL(COMMAND_0_BASE,1,0x00000000);
        counter = 0;
        //random_number++;
    }
*/
    return 0;
}










