
/*************************************************************************************
 * SDRAM
 *
 *Dexcel Electronics Designs PVT Ltd
 *
 * - File              : sdram.c
 *
 * Description		:  . contains routines for testing sdram ip core
 *
 * (C) Copyright 2008 Dexcel Electronics Designs PVT Ltd.
 * $Revision: 1.2 $
 * $Date: Tuesday, December 08 2008 
 ************************************************************************************/


#include<8051.h>

#define BASEADDRESS	0x0000 
//#define DATA_LEN	0x200000
#define DATA_LEN	0x200  
#define ADDR		0x200000

unsigned char xdata *BaseAddress = BASEADDRESS;
static unsigned long int Start_Addr, End_Addr,Len;
unsigned short Data_Read;
unsigned char Data_Type,Bank_No,Data_Rcv;

unsigned char CheckUART();


/**
  * Transmits  data through UART
  *
  * @param ch : Data to be transmitted.
  *
  * @return	 None.
  */
void Uart_Tx(unsigned char ch)
{
	 while(!(P3 & 0x01));
	 P0 = ch; 
  	 P3 = 0x00;
	 P3 = 0x01; 
}


/**
  * Reads Data from SDRAM
  *
  * @param 
  *			: Addr : Addr location where data to be read
  *
  * @return	 Zero.
  */
int ReadData(unsigned long Addr)
{
	unsigned char ch1, ch2;

	BaseAddress[1]=(Addr & 0xFF);
	BaseAddress[2]=((Addr >> 8) & 0xFF);
	BaseAddress[3]=((Addr >>16 ) & 0xFF);  

	BaseAddress[6]=0x04;
	
	ch1=BaseAddress[0];  
	ch2=BaseAddress[7];  
	
	Uart_Tx(ch1);
	Uart_Tx(ch2);
	return 0;
}



/**
  * Writes data to SDRAM
  *
  * @param 
  *			: Addr : Addr location where data to be written
  *			:Data_Wr: Data to be written
  *
  * @return	 none.
  */
int WriteData(unsigned long Addr, unsigned short Data_Wr)
{
	unsigned long i=0x00;

	BaseAddress[1]=	(Addr & 0xFF);
	BaseAddress[2]=	((Addr>>8) & 0xFF);
	BaseAddress[3]=	((Addr>>16) & 0xFF);
	BaseAddress[4]=(Data_Wr & 0xFF);
	BaseAddress[5]=((Data_Wr >>8) & 0xFF);

	return 0;
}


/**
  * Receives data from UART
  *
  * @param none.
  *
  * @return	 data_rx: Data received from UART
  */
unsigned char CheckUART()
{
	unsigned char data_rx;
	 while(!(P2 & 0x01));
	 P2 = 0x00;
	 P2 = 0x01;
	 data_rx=P1; 
	 return data_rx;
}


//void delay()
//{
//		int i=10; 
//		while(i)
//		i--;  
//}
/***************************************************************
 MAIN ROUTINE 
****************************************************************/
void main()
{
	unsigned char cnt=0,Data1, Data2;
	unsigned short count=0;
	unsigned long i=0;

//	P1=0x00;
//	delay();
//	P1=0x11; 
	
	while(1)
	{
		Data_Rcv=CheckUART();
		if(Data_Rcv==1)
		{
			Bank_No=CheckUART();
			Data_Type=CheckUART();
			if(Data_Type==1)
			{
				Data1=CheckUART();
				Data2=CheckUART();
				Data_Read=(Data1 + (Data2 <<8));
				Start_Addr=ADDR*Bank_No;
				for(i=0;i<DATA_LEN;i++)
				{
					WriteData(Start_Addr+i, Data_Read);
				}
			}
			else
			{
				Start_Addr=ADDR*Bank_No;
				count=0;
				for(i=0;i<DATA_LEN;i++)
				{
					WriteData(Start_Addr+i, count);
					count++;
					if(count==0x1000)
					count=0;
				}
			}
			Uart_Tx(0x0A);
		}
		else
		if(Data_Rcv==2)
		{
			Bank_No=CheckUART();
			Start_Addr=ADDR*Bank_No;
			for(i=0;i<DATA_LEN;i++)
			{
				ReadData(Start_Addr+i);
			}
			Uart_Tx(0x0A);
		}
	}
}
