/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */


//
//  main.c
//  WMT
//
//  Created by User on 14/04/2013.
//  Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//

// input signal x

#include <stdio.h>
#include <system.h>
#include <io.h>
#include "background.h"

//read x y coordinates, offset = 0 for x and offset = 1 for y
#define IORD_XY_DATA(base,offset) \
	IORD_16DIRECT(base,(offset)*2)
#define IOWR_SRAM_DATA(base,offset,data)\
	IOWR_16DIRECT(base,(offset)*2,data)
#define IORD_SRAM_DATA(base,offset)\
	IORD_16DIRECT(base,(offset)*2)

	// offset = 0 control panel 32bit
	// offset = 1 control mole 32bit
#define IOWR_CONTROL(base,offset,data) \
	IOWR_32DIRECT(base,(offset)*4,data)

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

//Initialization
void init()
{
	int x;
	int y;
	int z;
	int i;
	int data = 0;
	int counter = 0;
  printf("Hello from Nios II!\n");
  for(i=0;i<32000;i++)
  {
	  data = background[i];
	  IOWR_SRAM_DATA(SRAM_MUX_0_BASE,i,data);
  } //shifted by 64000

  for(i=0; i< 16000;i++)
  {
	  data = control_panel[i];
	  IOWR_SRAM_DATA(SRAM_MUX_0_BASE+64000,i,data); //shift 32000 from the base
  }

  for(i=0; i< 14400;i++)
  {
	  data = mole_shape[i];
	  IOWR_SRAM_DATA(SRAM_MUX_0_BASE+96000,i,data); //shift 48000 16bit from the base
  }
}

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 x;
	int y;
	x = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
	y = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	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;
}

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 main (int argc, const char * argv[])
{
	init();

//	init();
	int state = 0;
	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^2 + score1*16^3 + score2*16^3;
    	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(score1<9)
                		score1++;
                	else if(score2<9)
                	{
                		score1 = 0;
                		score2++;
                	}
                	else
                	{
						score1 = 0;
						score2 = 0;
						score3 ++;
                	}


                    break;
                }
                else
                {
                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
                }
            	previousx = inputx;
                previousy = inputy;
            }
            else
            {
            //	printf("E");
            	IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
            }

        }
      //  IOWR_LED_DATA(base,0,000000000);
        IOWR_CONTROL(COMMAND_0_BASE,1,0x00000000);
        counter = 0;
        //random_number++;
    }

    return 0;
}







