/******************************************************************
 * @file  Test_Func.s.c
 * @brief:    [file description] 
 * @author: HUOHUO
 * @Date 2024年4月20日
 * COPYRIGHT NOTICE: ITTC
 * Change_date         Owner         Change_content
 * 2024年4月20日        HUOHUO       create file

*****************************************************************/
#include "Test_Func.h"
#ifdef IDE_TEST
/*****************************************************************/
//Deofdm配置数据和时域数据
#if 0 
__VIRT_SM uint32_t g_deofdm_test_file[] = {
	#include "./../../Config/Transmitter_cfg_dm0.dat"
	#include "./../../Config/Transmitter_cfg_dm1.dat"
	#include "./../../Config/Transmitter_cfg_dm2.dat"
	#include "./../../Config/Transmitter_cfg_dm3.dat"
    #include "./../../Config/source_bit.dat"
};
#endif

__VIRT_SM uint32_t g_deofdm_test_file[] = {
	#include "./../../Config/Receiver_cfg_dm0.dat"
	#include "./../../Config/Receiver_cfg_dm1.dat"
	#include "./../../Config/Receiver_cfg_dm2.dat"
	#include "./../../Config/Receiver_cfg_dm3.dat"
    #include "./../../Config/timedata.dat"
};

//int32_t	get_tx_nr_slot_cycle(uint8_t scs)
//{
//	#ifdef IDE_TEST
//		return tick();
//	#else
//		return GET_CNT_VAL(get_core_id());
//	#endif
//}
//
int get_core_id()
{
	return 0;
}

int get_rx_nr_slot()
{
	return 1;
}
int get_rx_nr_sfn(uint8_t scs)
{
	return 1;
}

int32_t	get_rx_nr_slot_cycle(uint8_t scs)
{
#ifdef IDE_TEST
		return tick();
#else
		return GET_CNT_VAL(
		#ifndef IDE_TEST
		get_core_id()
		#endif
		);
#endif
}
//
//

void ucp_enter_critical(void)
{
	
}

//
void ucp_exit_critical(void)
{
	
}

int osp_printf(const char *fmt, ...)
{
	return 0;
}

void osp_sendLog(int level, char* pbuf, int size)
{
	
}


void osp_sendLog_print(int level, char* pbuf, int size)
{

}

int osp_strlen(const char* str)
{
	return 10;
}

char *g_ul_test_malloc_ddr = (char *)0x84000000;
char *osp_heap_malloc(uint32_t size)
{
	g_ul_test_malloc_ddr += 0x01000000;
	return (g_ul_test_malloc_ddr - 0x01000000);

}


int osp_send_msg(uint32_t msg_addr,
					  uint32_t msg_len,
					  uint8_t  msg_type,
					  uint8_t  src_core_id,
					  uint8_t  dst_core_id,
					  uint8_t  src_task_id,
					  uint8_t  dst_task_id)
{
	return 0;
}


void osp_timer_sync(int tti)
{

}

uint32_t get_ucp_port_id()
{
	return 0;
}

uint32_t msg_transfer_send_start(uint32_t port_id, uint32_t queue_num)
{
	#ifndef IDE_TEST
	return 0;
	#endif
}


uint32_t msg_transfer_send_msg(uint32_t port_id, uint32_t queue_num, uint8_t* msg_ptr, uint32_t msg_len)
{
	return 0;
}
uint32_t msg_transfer_send_end(uint32_t port_id, uint32_t queue_num)
{
	return 0;
}

uint32_t msg_transfer_free_msg(uint32_t port_id, uint32_t queue_num, uint32_t el_ind, char* buf)
{
	return 0;
}


int osp_get_cfgfile(char* name, uint32_t *pbuf, int* psize)
{
	int i;
	if(strlen(name) > 64)
		return -1;
	if(0 == strcmp(name,"Receiver_cfg_dm0.dat"))
	{
		*pbuf = (uint32_t)(RECEIVER_IDE_CONFIG_DM0_ADDR);
		*psize = RECEIVER_IDE_CONFIG_DM0_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Receiver_cfg_dm1.dat"))
	{
		*pbuf = (uint32_t)(RECEIVER_IDE_CONFIG_DM1_ADDR);
		*psize = RECEIVER_IDE_CONFIG_DM1_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Receiver_cfg_dm2.dat"))
	{
		*pbuf = (uint32_t)(RECEIVER_IDE_CONFIG_DM2_ADDR);
		*psize = RECEIVER_IDE_CONFIG_DM2_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Receiver_cfg_dm3.dat"))
	{
		*pbuf = (uint32_t)(RECEIVER_IDE_CONFIG_DM3_ADDR);
		*psize = RECEIVER_IDE_CONFIG_DM3_LEN;
		return 0;
	}
    	if(0 == strcmp(name,"timedata.dat"))
	{
		*pbuf = (uint32_t)(RECEIVER_IDE_TIME_DATA_ADDR);
		*psize = RECEIVER_IDE_TIME_DATA_LEN;
		return 0;
	}
	return -1;

#if 0
int osp_get_cfgfile(char* name, uint32_t *pbuf, int* psize)//
{
	int i;
	if(strlen(name) > 64)
		return -1;
	if(0 == strcmp(name,"Transmitter_cfg_dm0.dat"))
	{
		*pbuf = (uint32_t)(TRANSMITTER_IDE_CONFIG_DM0_ADDR);
		*psize = TRANSMITTER_IDE_CONFIG_DM0_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Transmitter_cfg_dm1.dat"))
	{
		*pbuf = (uint32_t)(TRANSMITTER_IDE_CONFIG_DM1_ADDR);
		*psize = TRANSMITTER_IDE_CONFIG_DM1_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Transmitter_cfg_dm2.dat"))
	{
		*pbuf = (uint32_t)(TRANSMITTER_IDE_CONFIG_DM2_ADDR);
		*psize = TRANSMITTER_IDE_CONFIG_DM2_LEN;
		return 0;
	}
		if(0 == strcmp(name,"Transmitter_cfg_dm3.dat"))
	{
		*pbuf = (uint32_t)(TRANSMITTER_IDE_CONFIG_DM3_ADDR);
		*psize = TRANSMITTER_IDE_CONFIG_DM3_LEN;
		return 0;
	}
    	if(0 == strcmp(name,"source_bit.dat"))
	{
		*pbuf = (uint32_t)(TRANSMITTER_IDE_SRC_BIT_ADDR);
		*psize = TRANSMITTER_IDE_SRC_BIT_LEN;
		return 0;
	}
	return -1;
}
#endif


}


int ape_csu_task_lookup(uint8_t task_tag, uint8_t isWait)
{
	return 0;
}

int ape_csu_dma_1D_G2L_ch0ch1_transfer(uint64_t addrSrc, uint64_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
	uint32_t addrDstLoc = addrDst;
	if((addrDst >= 0x100000) && (addrDst < 0x200000))
	{
		addrDstLoc = addrDst + 0x100000;
	}

	memcpy_ext((void *)addrDstLoc, (void *)addrSrc, dataLen);
	return 0;
}

int ape_csu_dma_1D_G2L_ch2ch3_transfer(uint64_t addrSrc, uint64_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
	uint32_t addrDstLoc = addrDst;
	if((addrDst >= 0x100000) && (addrDst < 0x200000))
	{
		addrDstLoc = addrDst + 0x100000;
	}

	memcpy_ext((void *)addrDstLoc, (void *)addrSrc, dataLen);
	return 0;
}

int ape_csu_dma_1D_L2G_ch0ch1_transfer(uint64_t addrSrc, uint64_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
		uint32_t addrSrcLoc = addrSrc;
	if((addrSrc >= 0x100000) && (addrSrc < 0x200000))
	{
		addrSrcLoc = addrSrc + 0x100000;
	}

	memcpy_ext((void *)addrDst, (void *)addrSrcLoc, dataLen);
	return 0;
}


int ape_csu_dma_1D_L2G_ch2ch3_transfer(uint64_t addrSrc, uint64_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
		uint32_t addrSrcLoc = addrSrc;
	if((addrSrc >= 0x100000) && (addrSrc < 0x200000))
	{
		addrSrcLoc = addrSrc + 0x100000;
	}

	memcpy_ext((void *)addrDst, (void *)addrSrcLoc, dataLen);
	return 0;

}


void ape_csu_dma_3Dto1D_G2L_ch6ch2_simp_transfer(uint32_t addrSrc, uint16_t xNumSrc, uint16_t yNumSrc, uint32_t yStepSrc, uint32_t 
zStepSrc, uint32_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
	void *dma_src_ptr = (void*)addrSrc;
	void *dma_dst_addr = (void *)addrDst;

	uint32_t x,y,z;
	uint32_t zNum = dataLen / (xNumSrc *yNumSrc);
	if ((addrDst >= 0x100000) && (addrDst < 0x200000))
	{
		dma_dst_addr = (void *)addrDst + 0x100000;
	}
	for (z = 0; z < zNum; z++)
	{
		for(y = 0; y < yNumSrc; y++)
		{
			memcpy_ext(dma_dst_addr, &dma_src_ptr[yStepSrc * y], xNumSrc);
			dma_dst_addr += xNumSrc;
		}
		dma_src_ptr += zStepSrc;
	}
	return;

}


void ape_csu_dma_3Dto1D_G2L_ch4ch0_simp_transfer(uint32_t addrSrc, uint16_t xNumSrc, uint16_t yNumSrc, uint32_t yStepSrc, uint32_t 
zStepSrc, uint32_t addrDst, uint32_t dataLen, uint8_t tag, uint8_t isWait)
{
	void *dma_src_ptr = (void*)addrSrc;
	void *dma_dst_addr = (void *)addrDst;

	uint32_t x,y,z;
	uint32_t zNum = dataLen / (xNumSrc *yNumSrc);
	if ((addrDst >= 0x100000) && (addrDst < 0x200000))
	{
		dma_dst_addr = (void *)addrDst + 0x100000;
	}
	for (z = 0; z < zNum; z++)
	{
		for(y = 0; y < yNumSrc; y++)
		{
			memcpy_ext(dma_dst_addr, &dma_src_ptr[yStepSrc * y], xNumSrc);
			dma_dst_addr += xNumSrc;
		}
		dma_src_ptr += zStepSrc;
	}
	return;

}


void dma_1l3d1s_chain_G2L_ch0ch1_simp_config_start(uint32_t *paramPtr,
	uint32_t paramCsuAddr,
	uint32_t numNodes,
	uint32_t dmaTag,
	uint32_t *xAddrSrcPtr,
	uint16_t xNum,
	uint32_t *allNumPtr,
	uint32_t addrDst,
	uint32_t allNumTotal,
	uint32_t waitFlag)
{
	uint32_t nodeIdx;
	uint32_t *currNodePtr = paramPtr;
	void *dma_src_ptr;
	void *dma_dst_addr = (void *)addrDst;
	uint32_t x, y, z;
	uint32_t yNum, zNum, yStep, zStep;
	if((addrDst >= 0X100000) && (addrDst < 0x200000))
	{
		dma_dst_addr =(void *)addrDst + 0x100000;
	}
	currNodePtr = paramPtr;
	for (nodeIdx =0; nodeIdx < numNodes; nodeIdx++)
	{
		dma_src_ptr = (void *)xAddrSrcPtr[nodeIdx];
		yNum = currNodePtr[APC_DMA_CHAIN_L1_3D_NXTADDRH_XADDRH_GRANSIZE_YNUM_WORD_OFFSET] & 0x0000FFFF;
		zNum = allNumPtr[nodeIdx] / (xNum * yNum);
		yStep = currNodePtr[APC_DMA_CHAIN_L1_3D_YSTEP_WORD_OFFSET];
		zStep = currNodePtr[APC_DMA_CHAIN_L1_3D_ZSTEP_WORD_OFFSET];
		for (z = 0; z < zNum; z++)
		{
			for (y = 0; y < yNum; y++)
			{
				memcpy_ext(dma_dst_addr, &dma_src_ptr[yStep * y], xNum);
				dma_src_ptr += zStep;
			}
			currNodePtr += APC_DMA_CHAIN_L1_3D_SIZE_WORD;
		}
		
	}
	return;
		
}


uint32_t g_qounit_findk_resValue;
void qounit_findK_config_start
	   (uint32_t bitSequenceAddr,
		uint32_t seqIndexAddr,
		uint32_t seqLength,
		uint32_t K0,
		uint32_t K1)
{
	uint32_t n, subBlockLen, KCnt = 0, Q_WCnt, idx, idxGroupNum, idxNResBits, bitValue, fullBitMask;
	uint16_t *Q_W = (uint16_t *)seqIndexAddr;
	uint32_t *Q_F_Tmp_ptr = (uint32_t *)bitSequenceAddr;
	if ((seqIndexAddr >= 0x100000) && (seqIndexAddr < 0x200000))
	{
		Q_W = (uint16_t *)(seqIndexAddr + 0x100000);
	}
	if ((bitSequenceAddr >= 0x100000) && (bitSequenceAddr < 0x200000))
	{
		Q_F_Tmp_ptr = (uint32_t *)(bitSequenceAddr + 0x100000);
	}
	n = 26 - __builtin_clz(seqLength);
	subBlockLen = 1 << n;
	if(32 == subBlockLen)
	{
		fullBitMask = 0xFFFFFFFF;
	}
	else
	{
		fullBitMask = (1 << subBlockLen) - 1;
	}
	g_qounit_findk_resValue = 0;
	for (Q_WCnt = 0; Q_WCnt < seqLength; Q_WCnt++)
	{
		idx = Q_W[Q_WCnt];
		idxGroupNum = idx >> 5;
		idxNResBits = idx & 31;
		bitValue = (Q_F_Tmp_ptr[idxGroupNum] >> idxNResBits) & 1;
		if (0 == bitValue)
		{
			KCnt++;
			if (KCnt == K0)
			{
				g_qounit_findk_resValue |= Q_WCnt;
			}
			if (KCnt == K1)
			{
				g_qounit_findk_resValue |= (Q_WCnt <<10);
				break;
			}
		}
	}
	return;
}


uint32_t qounit_wait_complete()
{
	return g_qounit_findk_resValue;
}
#endif