
#include "ucps2.h"
#include "ucpm2.h"
#include "freOffComp.h"
#include "freOffCompImpl.h"
#include "cordicSC.h"
#include "ape_common.h"
#include "common.h"
#include "trace.h"
//v16u32 KI = {2,4,6};

void freOffCompImpl(int* ConfigAddr_comp, int* ConfigAddr_cordic, int *freEstOutAddr,int* data_ptr_ddr,int* res_ptr_ddr, int *ava_ptr_dm2, int *ava_ptr_dm3){


	volatile int a = 1;
	int count = 7680;
	freEstOutAddr[0] = freEstOutAddr[0]>>10;
	int increment = freEstOutAddr[0];
	uint32_t time0, time1;

	for(int i=0;i<1;i++)
	{

		int res_ptr_offset = i*count;

		int *input_data_ptr = ava_ptr_dm3;
		int *fre_comp_exp_ptr = ava_ptr_dm2;
		int *output_data_ptr = ava_ptr_dm3;
		time0 = Time_offset(0);
	    int time_data_length = count;
	    ape_csu_task_lookup(DMA_TAG_G2L, 1);
	    ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)data_ptr_ddr,
	                             (uint64_t)DM_TO_CSU_ADDR(input_data_ptr),
	                             time_data_length*4,
	                             DMA_TAG_G2L,
	                             1);
		time1 = Time_offset(0);
		TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 9, time1-time0);
		time0 =time1;

	    cordicSC(ConfigAddr_cordic,MPU_ADDR(freEstOutAddr),MPU_ADDR(fre_comp_exp_ptr),increment,count);
		WAIT_MPU_STOP;
	    SVRReg[0] = MPU_ADDR(ConfigAddr_cordic);
	    cordicSCAsm(SVRReg);

		WAIT_MPU_STOP;
		time1 = Time_offset(0);
		TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 10, time1-time0);
		time0 =time1;

		freOffComp(ConfigAddr_comp,MPU_ADDR(input_data_ptr),MPU_ADDR(fre_comp_exp_ptr),MPU_ADDR(output_data_ptr));
		ape_csu_task_lookup(DMA_TAG_G2L, 1);
		WAIT_MPU_STOP;
		SVRReg[0] = MPU_ADDR(ConfigAddr_comp);
		freOffCompAsm(SVRReg);
		
		WAIT_MPU_STOP;

		time1 = Time_offset(0);
		TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 11, time1-time0);
		time0 =time1;

    	ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR((uint32_t)output_data_ptr),
    	                            (uint64_t)(res_ptr_ddr + res_ptr_offset),
									time_data_length*4 ,
    	                            DMA_TAG_L2G,
    	                            1);
    	time1 = Time_offset(0);
		TRACE_MAX(TRACE_RECEIVER_SYMB_ADDR, 12, time1-time0);
		time0 =time1;

	
	}


	return ;

}



