/* This file includes the initialization codes for FIR, IIR and FFT Accelerators */

#include "def21469.h"



#include "Cdef21469.h"

void Init_FFT();

/* Declaring the external buffers needed for FFT Accelerator*/
extern int FFT_IP_buff[];
extern int FFT_OP_buff[];
extern int FFT_VCF_buff[];
extern int FFT_HCF_buff[];
extern int FFT_IM_OP_buff[];
extern int FFT_SP_CF_buff[];


/*Adding the TCB for FFT Accelerator*/

/*N=2048, V=32, H=64 */

/************** PHASE I: V Point FFT of the original input****************/

/*Coefficient TCB for loading V cofficients */


	int FFT_VCF_TCB[6]={
				0,
				(int)FFT_VCF_buff,
				64,
				64,
				1,
				(int)FFT_VCF_buff
			};



/*TCB for loading 2*N-1 Input data points  cofficients (modifier = 2*H) */
	int FFT_VIP_TCB[6]={
				0,
				(int)FFT_IP_buff,
				4095,
				4095,
				128,
				(int)FFT_IP_buff
			};



/*TCB for loading 2*N-1 Input data points  cofficients (modifier = 2*H) */
	int FFT_VLASTIP_TCB[6]={
				0,
				(int)FFT_IP_buff,
				4096,
				1,
				1,
				(int)FFT_IP_buff+4095
			};


	int FFT_IMOP_TCB[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				4096,
				1,
				(int)FFT_IM_OP_buff
			};


	int FFT_PWCF_TCB1[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+0
			};


	int FFT_IMIP_TCB1[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+0
			};


	int FFT_IMOP_TCB1[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+0
			};


	int FFT_PWCF_TCB2[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+512
			};


	int FFT_IMIP_TCB2[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+256
			};


	int FFT_IMOP_TCB2[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+256
			};


	int FFT_PWCF_TCB3[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+1024
			};


	int FFT_IMIP_TCB3[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+512
			};


	int FFT_IMOP_TCB3[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+512
			};


	int FFT_PWCF_TCB4[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+1536
			};


	int FFT_IMIP_TCB4[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+768
			};


	int FFT_IMOP_TCB4[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+768
			};


	int FFT_PWCF_TCB5[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+2048
			};


	int FFT_IMIP_TCB5[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1024
			};


	int FFT_IMOP_TCB5[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1024
			};


	int FFT_PWCF_TCB6[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+2560
			};


	int FFT_IMIP_TCB6[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1280
			};


	int FFT_IMOP_TCB6[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1280
			};


	int FFT_PWCF_TCB7[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+3072
			};


	int FFT_IMIP_TCB7[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1536
			};


	int FFT_IMOP_TCB7[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1536
			};


	int FFT_PWCF_TCB8[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+3584
			};


	int FFT_IMIP_TCB8[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1792
			};


	int FFT_IMOP_TCB8[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+1792
			};


	int FFT_PWCF_TCB9[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+4096
			};


	int FFT_IMIP_TCB9[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2048
			};


	int FFT_IMOP_TCB9[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2048
			};


	int FFT_PWCF_TCB10[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+4608
			};


	int FFT_IMIP_TCB10[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2304
			};


	int FFT_IMOP_TCB10[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2304
			};


	int FFT_PWCF_TCB11[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+5120
			};


	int FFT_IMIP_TCB11[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2560
			};


	int FFT_IMOP_TCB11[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2560
			};


	int FFT_PWCF_TCB12[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+5632
			};


	int FFT_IMIP_TCB12[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2816
			};


	int FFT_IMOP_TCB12[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+2816
			};


	int FFT_PWCF_TCB13[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+6144
			};


	int FFT_IMIP_TCB13[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3072
			};


	int FFT_IMOP_TCB13[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3072
			};


	int FFT_PWCF_TCB14[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+6656
			};


	int FFT_IMIP_TCB14[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3328
			};


	int FFT_IMOP_TCB14[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3328
			};


	int FFT_PWCF_TCB15[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+7168
			};


	int FFT_IMIP_TCB15[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3584
			};


	int FFT_IMOP_TCB15[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3584
			};


	int FFT_PWCF_TCB16[6]={
				0,
				(int)FFT_SP_CF_buff,
				8192,
				512,
				1,
				(int)FFT_SP_CF_buff+7680
			};


	int FFT_IMIP_TCB16[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3840
			};


	int FFT_IMOP_TCB16[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				256,
				1,
				(int)FFT_IM_OP_buff+3840
			};




	int FFT_HCF_TCB[6]={
				0,
				(int)FFT_HCF_buff,
				128,
				128,
				1,
				(int)FFT_HCF_buff
			};


	int FFT_HIP_TCB[6]={
				0,
				(int)FFT_IM_OP_buff,
				4095,
				4095,
				64,
				(int)FFT_IM_OP_buff
			};


	int FFT_HLASTIP_TCB[6]={
				0,
				(int)FFT_IM_OP_buff,
				4096,
				1,
				1,
				(int)FFT_IM_OP_buff+4095
			};


	int FFT_OP_TCB[6]={
				0,
				(int)FFT_OP_buff,
				4095,
				4095,
				64,
				(int)FFT_OP_buff
			};


	int FFT_LASTOP_TCB[6]={
				0,
				(int)FFT_OP_buff,
				4096,
				1,
				1,
				(int)FFT_OP_buff+4095
			};



/* Adding the FFT Initialization Code Now */

void Init_FFT()

{

	int temp;

	//Mapping the FFT DMA interrupt
	temp=*pPICR0;
	temp&=~(P0I0|P0I1|P0I2|P0I3|P0I4);
	temp|=P0I0|P0I1|P0I3|P0I4;
	*pPICR0=temp;

	*pPMCTL1&=~(BIT_17|BIT_18);
	*pPMCTL1|=FFTACCSEL;

	//PMCTL1 effect latency
		asm("nop;nop;nop;nop;");

		//Clearing the FFTCTL registers
		*pFFTCTL2=0;

		*pFFTCTL1=FFT_RST;

		asm("nop;nop;nop;nop;");

		FFT_VCF_TCB[0]=(int)FFT_VIP_TCB+5-0x80000;

	FFT_VIP_TCB[0]=(int)FFT_VLASTIP_TCB+5-0x80000;

	temp=(int)FFT_PWCF_TCB1;
	FFT_VLASTIP_TCB[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB[0]=(int)FFT_IMOP_TCB1+5-0x80000;

	FFT_PWCF_TCB1[0]=(int)FFT_IMIP_TCB1+5-0x80000;

	temp=(int)FFT_PWCF_TCB2;
	FFT_IMIP_TCB1[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB1[0]=(int)FFT_IMOP_TCB2+5-0x80000;

	FFT_PWCF_TCB2[0]=(int)FFT_IMIP_TCB2+5-0x80000;

	temp=(int)FFT_PWCF_TCB3;
	FFT_IMIP_TCB2[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB2[0]=(int)FFT_IMOP_TCB3+5-0x80000;

	FFT_PWCF_TCB3[0]=(int)FFT_IMIP_TCB3+5-0x80000;

	temp=(int)FFT_PWCF_TCB4;
	FFT_IMIP_TCB3[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB3[0]=(int)FFT_IMOP_TCB4+5-0x80000;

	FFT_PWCF_TCB4[0]=(int)FFT_IMIP_TCB4+5-0x80000;

	temp=(int)FFT_PWCF_TCB5;
	FFT_IMIP_TCB4[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB4[0]=(int)FFT_IMOP_TCB5+5-0x80000;

	FFT_PWCF_TCB5[0]=(int)FFT_IMIP_TCB5+5-0x80000;

	temp=(int)FFT_PWCF_TCB6;
	FFT_IMIP_TCB5[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB5[0]=(int)FFT_IMOP_TCB6+5-0x80000;

	FFT_PWCF_TCB6[0]=(int)FFT_IMIP_TCB6+5-0x80000;

	temp=(int)FFT_PWCF_TCB7;
	FFT_IMIP_TCB6[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB6[0]=(int)FFT_IMOP_TCB7+5-0x80000;

	FFT_PWCF_TCB7[0]=(int)FFT_IMIP_TCB7+5-0x80000;

	temp=(int)FFT_PWCF_TCB8;
	FFT_IMIP_TCB7[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB7[0]=(int)FFT_IMOP_TCB8+5-0x80000;

	FFT_PWCF_TCB8[0]=(int)FFT_IMIP_TCB8+5-0x80000;

	temp=(int)FFT_PWCF_TCB9;
	FFT_IMIP_TCB8[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB8[0]=(int)FFT_IMOP_TCB9+5-0x80000;

	FFT_PWCF_TCB9[0]=(int)FFT_IMIP_TCB9+5-0x80000;

	temp=(int)FFT_PWCF_TCB10;
	FFT_IMIP_TCB9[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB9[0]=(int)FFT_IMOP_TCB10+5-0x80000;

	FFT_PWCF_TCB10[0]=(int)FFT_IMIP_TCB10+5-0x80000;

	temp=(int)FFT_PWCF_TCB11;
	FFT_IMIP_TCB10[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB10[0]=(int)FFT_IMOP_TCB11+5-0x80000;

	FFT_PWCF_TCB11[0]=(int)FFT_IMIP_TCB11+5-0x80000;

	temp=(int)FFT_PWCF_TCB12;
	FFT_IMIP_TCB11[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB11[0]=(int)FFT_IMOP_TCB12+5-0x80000;

	FFT_PWCF_TCB12[0]=(int)FFT_IMIP_TCB12+5-0x80000;

	temp=(int)FFT_PWCF_TCB13;
	FFT_IMIP_TCB12[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB12[0]=(int)FFT_IMOP_TCB13+5-0x80000;

	FFT_PWCF_TCB13[0]=(int)FFT_IMIP_TCB13+5-0x80000;

	temp=(int)FFT_PWCF_TCB14;
	FFT_IMIP_TCB13[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB13[0]=(int)FFT_IMOP_TCB14+5-0x80000;

	FFT_PWCF_TCB14[0]=(int)FFT_IMIP_TCB14+5-0x80000;

	temp=(int)FFT_PWCF_TCB15;
	FFT_IMIP_TCB14[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB14[0]=(int)FFT_IMOP_TCB15+5-0x80000;

	FFT_PWCF_TCB15[0]=(int)FFT_IMIP_TCB15+5-0x80000;

	temp=(int)FFT_PWCF_TCB16;
	FFT_IMIP_TCB15[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB15[0]=(int)FFT_IMOP_TCB16+5-0x80000;

	FFT_PWCF_TCB16[0]=(int)FFT_IMIP_TCB16+5-0x80000;

	temp=(int)FFT_HCF_TCB;
	FFT_IMIP_TCB16[0]=0x100000|temp+5-0x80000;

	FFT_IMOP_TCB16[0]=(int)FFT_OP_TCB+5-0x80000;

	FFT_HCF_TCB[0]=(int)FFT_HIP_TCB+5-0x80000;

	FFT_HIP_TCB[0]=(int)FFT_HLASTIP_TCB+5-0x80000;

	FFT_OP_TCB[0]=(int)FFT_LASTOP_TCB+5-0x80000;

	temp=(int)FFT_VCF_TCB;

	*pCPIFFT=0x100000|temp+5-0x80000;

	*pCPOFFT=(int)FFT_IMOP_TCB+5-0x80000;

	*pFFTCTL2=VDIMDIV16_2|FFT_LOG2VDIM_5|HDIMDIV16_4|FFT_LOG2HDIM_6|NOVER256_8|FFT_CPACKIN|FFT_CPACKOUT;

	*pFFTCTL1=FFT_EN|FFT_START|FFT_DMAEN;

};

