/*************************************************************************************
 * SDRAM
 *
 *Dexcel Electronics Designs PVT Ltd
 *
 * - File              : sdram.c
 *
 * Description		:  . contains the functions for verifying the Actel Nand Flash IP core.
 *					   . Ask for the Communication Port
 *					   . Ask for the Read / Write operation
 *					   . Ask for Bank selection
 *					   . Ask for the Data to be written if write operation selected
 *					   . Displays the Read data if read operation is selected
 *					   . Exit from the program
 *
 * (C) Copyright 2008 Dexcel Electronics Designs PVT Ltd.
 * $Revision: 1.4 $
 * $Date: Tuesday, December 08 2008 
 ************************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include <windows.h>

//#define DATA_LEN	0x200000
#define DATA_LEN	0x200
unsigned char ComPort[10];
unsigned long file_cnt;



/*********************************************************************************************
 MAIN ROUTINE 
*********************************************************************************************/
int main()
{
	int USB_HANDLE;
	unsigned char buffer[1000],flag_rcv,bank_count=0;
	unsigned char Data,fileflag=1,Flag_Complete=0;
	int port_choice=0,choice=0,data_choice;
	long int start_addr, end_addr;
	unsigned int ret,i,l,bank_no;
	unsigned short data_tx,count;
	static long int len=4096;
	unsigned char StartAddr1, StartAddr2, StartAddr3,EndAddr1,EndAddr2,EndAddr3;
	unsigned char Data1, Data2;
	unsigned char ch1, ch2,cnt;

	FILE *fp1, *fp2;

	printf("\n");
	printf("/***************************************************\\\n");
	printf("|                Actel Corporation                  |\n");
	printf("|                                                   |\n");
	printf("|                  www.actel.com                    |\n");
	printf("\\***************************************************/\n");
	printf("\n");

	printf("Please Enter COM Port Name (e.g: COM1 or COM2 etc)\n");
	scanf("%s",&ComPort);

	USB_HANDLE = OpenUsb(ComPort);
	while(1)
	{
		Flag_Complete=0;
		bank_count=0;
		printf("\n\n");
		printf("Please Select  Operation \n 1 for Write \n 2 for Read \n 3 for verify  \n");
		printf("Press Ctrl-C to exit\n");
		printf("\n");
		scanf("%d",&choice);
		UsbSend(USB_HANDLE,&choice,1);
		if(choice==1)						/*perform a Write function*/
		{
			fp2=fopen("Write_Data.txt","wb");
			if(!fp2)
				printf("Error in opening Write file \n");

			printf("\n");
			printf ("Select Bank \n 0 for Bank0 \n 1 for Bank1 \n 2 for Bank2 \n 3 for Bank3 \n \n");
			printf("Press Ctrl-C to exit\n");
			scanf("%d",&bank_no);
			UsbSend(USB_HANDLE,&bank_no,1);

			printf("Select \n 1. for fixed data\n 2. for counter data \n");
			printf("Press Ctrl-C to exit\n");
			scanf("%d", &data_choice);
			printf("\n");
			UsbSend(USB_HANDLE,&data_choice,1);

			if(data_choice==1)										/*write static data*/
			{		
				printf("Select Data between 0x00 to 0xFFFF to Download \n");
				printf("Press Ctrl-C to exit\n");
				scanf("%x", &data_tx);
				Data1 =(data_tx & 0xff);
				Data2 =((data_tx>>8) & 0xff);
				UsbSend(USB_HANDLE,&Data1,1);
				UsbSend(USB_HANDLE,&Data2,1);
				for(i=0;i<(DATA_LEN);i++)
				{
					fwrite(&Data1,1, 1, fp2);
					fwrite(&Data2,1, 1, fp2);
				}
			}
			else
			{
				count=0;										/*write counter value*/
				Data1=0x00;
				for(i=0;i<DATA_LEN;i++)
				{
					fwrite(&count,1, 2, fp2);
					count++;
					if(count==0xffff)
					count=0;
				}
			}
			printf("\n");
			printf("\n");
			printf(" Writing Data please wait ...........\n");
			printf("\n");
			printf("\n");
			ret=UsbRecv(USB_HANDLE,&flag_rcv,1);
			if(flag_rcv==0x0A)
				printf(" WRITE COMPLETED \n");
			printf("\n");
			printf("\n");
			fclose(fp2);
		}
		else
		if(choice==2)										/*perform Read function*/
		{
			printf ("Select Bank \n 0 for Bank0 \n 1 for Bank1 \n 2 for Bank2 \n 3 for Bank3 \n \n");
			printf("Press Ctrl-C to exit\n");
			scanf("%d",&bank_no);
			UsbSend(USB_HANDLE,&bank_no,1);
			fp1=fopen("Read_Data.txt","wb");
			if(!fp1)
				printf("File to write read data cannot be opened \n");

			printf("\n");
			printf("\n");
			printf(" Reading Data please wait .......................... \n");
			printf("\n");
			printf("\n");
			for(i=0;i<(DATA_LEN*2);i++)
			{
				ret=UsbRecv(USB_HANDLE,&buffer,1);
				fwrite(&buffer,1, 1, fp1);
			}
			ret=UsbRecv(USB_HANDLE,&flag_rcv,1);
			if(flag_rcv==0x0A)
				printf(" READ DATA COMPLETED  \n");
			printf("\n");
			printf("\n");
			fclose(fp1);
		}
		else
		if(choice==3)							/*perform verify function*/
		{
			fp1=fopen("Read_Data.txt","r");
				if(!fp1)
					printf("File Read_Data cannt be opened \n");
			fp2=fopen("Write_Data.txt","r");
			if(!fp2)
				printf("Write Read_Data cannt be opened \n");

			printf("\n");
			printf("\n");
			printf("Verification under progress . Please wait ..... \n");
			printf("\n");
			printf("\n");
			/*comparing 2 files*/
			while(!feof(fp1))
			{
				ch1 = fgetc(fp1);
				if(ferror(fp1)) 
				{
					printf("Error reading read file.\n");
					exit(1);
				}
				ch2 = fgetc(fp2);
				if(ferror(fp2))
				{
					printf("Error reading write file.\n");
					exit(1);
				}
				if(ch1 != ch2) 
				{
					printf("\n");
					printf("VERIFICATION COMPLETED \n");
					printf("Files are NOT same \n");
					fileflag=0;
					break;
				}
			}
			if(fileflag)
			{
				printf("\n");
				printf("VERIFICATION COMPLETED \n");
                printf("Files are same\n");
			}
			printf("\n");
			fileflag=1;
			fclose(fp1);
			fclose(fp2);
		}
	}
	CloseUsb(USB_HANDLE);
}


