
//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<0x43)
			return 9; //start key
		else if(x>0x2F0 && x< 0x530 && y>0x0 && y<0x43)
			return 0xA;
		else if(x>0x0 && x< 0x2F0 && y>0x0 && y<0x43)
			return 0xB;
	}

	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>0x810 && x< 0xA90 && y> 0x580 && y< 0xA10)
			return 1; // normal
		else if(x>0x810 && x< 0xA90 && 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 main (int argc, const char * argv[])
{
	init();
	int state = 0x0;
	alt_irq_register(MUSICCONTROLLER_0_IRQ, NULL,( void*)standbymusic);
	while(1)
	{
		switch(state)
		{
		case 0x0:
			if(touch(state)==)
			break;
		case 0x1:
			break;
		case 0x2:
			break;
		case 0x3:
			break;
		case 0x4:
			break;
		case 0x5:
			break;
		case 0x6:
			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;
		}

	}
/*
//	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;
}










///Linchuan???===========================================================================
//#include <stdio.h>
//#include <io.h>
//#include <system.h>
//#include <sys/alt_irq.h> // the irq functions
//
//
////static void audio_irqhandler (void * context, alt_u32 id)
////{
//    //printf("\n Audio irq!\n");
// //   IOWR_16DIRECT(MUSICCONTROLLER_0_BASE, 0, 0); // reset request
//
////}
//
//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");
//	     }
//}
///*
//static void backgroundmusic(){
//	 for(j=0;j<256;j++)
//	     {
//		 IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,duringplay[i]);
//		 i++;
//		 if(i >24533 ){
//		 			i = 0;
//		 		}
//	     }
//}
//
//static void victorymusic(){
//	 for(j=0;j<256;j++)
//	     {
//		 IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,victory[i]);
//		 i++;
//		 if(i >24533 ){
//		 			i = 0;
//		 		}
//	     }
//}
//
//static void losemusic(){
//	 for(j=0;j<256;j++)
//	     {
//		 IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,lose[i]);
//		 i++;
//		 if(i >24533 ){
//		 			i = 0;
//		 		}
//	     }
//}
//
//*/
//int main()
//{
//
//	alt_irq_register(   MUSICCONTROLLER_0_IRQ, NULL,( void*)standbymusic);
//	printf("x\n");
//
///*if (standby==1)
//{
//	alt_irq_register(   MUSICCONTROLLER_0_IRQ, NULL,( void*)standbymusic);
//}
//
//else if (playing==1)
//{
//	alt_irq_register(   MUSICCONTROLLER_0_IRQ, NULL,( void*)backgroundmusic);
//}
//
//else if (victory==1)
//{
//	alt_irq_register(   MUSICCONTROLLER_0_IRQ, NULL,( void*)victorymusic);
//}
//
//else if (victory==0)
//{
//	alt_irq_register(   MUSICCONTROLLER_0_IRQ, NULL,( void*)victorymusic);
//}
//
//
//*/
//
//  //printf("Hello from Nios II!\n");
//
//    // for(i=0;i<24533;i++)
//    // {
//	// IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,i,voice[i]);
//    // }
//
//	// if(count == 5200000)
//	 //{
//     //IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,0,voice[i]);
//     //i++;
//     //if(i==929)
//     //{
//    //	 i=0;
//    //	 printf("yayayayayayay");
//     //}
//	 //}
//	 //else
//	  //count=count+1;
//	 //if(count == 5200000)
//	//	 count = 0;
//
//
//  return 0;
//}








