/******************************************************************
 * @file  receiver_Proc.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 "receiver_bit_func.h"

 /*!   
 * @brief:  Receiver_sync链路处理
 * @author:  HUOHUO
 * @Date:  2024年10月30日
 */
void Receiver_Bit_Proc(
    uint32_t *param_ptr,
    int32_t  *temp_dm0_ptr,
    int32_t  *temp_dm1_ptr,
    int32_t  *temp_dm2_ptr,
    int32_t  *temp_dm3_ptr
)
{
    //局部变量定义
    int32_t  *cfg_addr;
    uint32_t time_data_ddr_ptr;
    uint32_t time_data_length;
    uint32_t gain_test_ddr_ptr;
    uint32_t gain_test_length;
    uint32_t mpu_temp_dm0_ptr;
    uint32_t mpu_temp_dm1_ptr;
    uint32_t mpu_temp_dm2_ptr;
    uint32_t mpu_temp_dm3_ptr;
    uint32_t temp_u32;
    uint32_t time_data_dm0_ptr = ((((uint32_t)&temp_dm0_ptr[0] + 4095)>>12)<<12);
    uint32_t res_ptr = RECEIVER_OUT3;

    // 16QAM_LDPC-----------------------------------------------------------------------------------------------
    int i;
    int NumCB = 28;
    uint32_t gain_mmse_input = (uint32_t)(temp_dm1_ptr);
    uint32_t TempRNV = (uint32_t)(temp_dm1_ptr) + 8192;
    uint32_t TempQ = (uint32_t)(temp_dm3_ptr);
    uint32_t Qam16Out = (uint32_t)(temp_dm2_ptr);
    int Qm = 4;
    int ReNum = 2048;

    int front_nullen = 208 * 2;//=416byte,104word
    int32_t *ldpc_pre_data_ptr = (int32_t *)receiver_bit_config_dm3_ptr + g_receiver_bit_table_param.ByteCopy_CFG2_Offset;
    uint32_t bytecopy_out;
    int efect_len = 8192;
    uint32_t ByteShiftOut = (uint32_t)(temp_dm2_ptr);
    int totalCBlen = 14144;

    int32_t *de_ldpc_matrix_index_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT1_Offset;
    int32_t *de_ldpc_check_para0_addr = (int32_t *)receiver_bit_config_dm1_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT2_Offset;
    int32_t *de_ldpc_check_para1_addr = (int32_t *)receiver_bit_config_dm2_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_LUT3_Offset;
    int32_t *de_ldpc_lmn0_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG2_Offset;
    int32_t *de_ldpc_lmn1_addr = (int32_t *)receiver_bit_config_dm1_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG3_Offset;
    int32_t *de_ldpc_lmn2_addr = (int32_t *)receiver_bit_config_dm2_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG4_Offset;
    int32_t *de_ldpc_lmn3_addr = (int32_t *)receiver_bit_config_dm3_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG5_Offset;
//    uint32_t LdpcOut = (uint32_t)(temp_dm1_ptr);
    uint32_t LdpcOut;



    int out_len = 4576 / 8;
    for (i=0; i<ReNum; i++){
        time_data_ddr_ptr = (uint32_t)*param_ptr + 4 * i * ReNum;//addr->4;(ReNum*4)byte/4=(ReNum)word
        time_data_length = (uint32_t)*(param_ptr + 1) / NumCB;
        ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)time_data_ddr_ptr,
                                     (uint64_t)DM_TO_CSU_ADDR(time_data_dm0_ptr),
                                     time_data_length,
                                     DMA_TAG_G2L,
                                     0);
        ape_csu_task_lookup(DMA_TAG_G2L, 1);//等待之前的 DMA 传输完成
        gain_test_ddr_ptr = (uint32_t)*(param_ptr + 2) + 4 * i * ReNum / 2;//addr->4;(ReNum*2)byte/4=(ReNum/2)word
        gain_test_length = (uint32_t)*(param_ptr + 3) / NumCB;
        ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)gain_test_ddr_ptr,
                                        (uint64_t)DM_TO_CSU_ADDR(gain_mmse_input),
                                        gain_test_length,
                                        DMA_TAG_G2L,
                                        0);
        ape_csu_task_lookup(DMA_TAG_G2L, 1);
         cfg_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.LLRDemodulation_CFG1_Offset;
         LLRDemodulationSetConfig((int)cfg_addr,
                                 MPU_ADDR(time_data_dm0_ptr),
                                 MPU_ADDR(gain_mmse_input),
                                 MPU_ADDR(TempQ),
                                 MPU_ADDR(TempRNV),
                                 MPU_ADDR(Qam16Out),
                                 Qm, // Qm，16QAM
                                 ReNum); // ReNum，符号数，2048
         SVRReg[0] = MPU_ADDR(cfg_addr);
         LLRDemodulationAsm(SVRReg);
         WAIT_MPU_STOP;

        bytecopy_out = (uint32_t)ldpc_pre_data_ptr + front_nullen;
        cfg_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.ByteCopy_CFG1_Offset;
         ByteCopy((int)cfg_addr,
                 MPU_ADDR(Qam16Out),
                 MPU_ADDR(bytecopy_out),
                 efect_len);
         SVRReg[0] = MPU_ADDR(cfg_addr);
         ByteCopyAsm(SVRReg);
         WAIT_MPU_STOP;
         cfg_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.ByteShift_CFG1_Offset;
         ByteShift((int)cfg_addr,
                 MPU_ADDR((uint32_t)ldpc_pre_data_ptr),
                 MPU_ADDR(ByteShiftOut),
                 totalCBlen,
                 2);
         SVRReg[0] = MPU_ADDR(cfg_addr);
         ByteShiftAsm(SVRReg);
         WAIT_MPU_STOP;
         
         cfg_addr = (int32_t *)receiver_bit_config_dm0_ptr + g_receiver_bit_table_param.LdpcDecBG1AllZc_CFG1_Offset;
         LdpcDecBG1AllZc((int)cfg_addr,
                         0xd0, // Zc，208
                         0x08, // MaxIterNum，最好为8
                         0x0,  // ShiftBit，0，迭代移位值
                         0x62, // MulFactor，98，补偿因子
                         0x03, // code_rate，3，对应输入68*Zc
                         MPU_ADDR(ByteShiftOut), // data_in
                         MPU_ADDR((uint32_t)de_ldpc_matrix_index_addr), // BG1_Dec_Matrix，迭代所需的校验矩阵，配置ZcLatch
                         MPU_ADDR((uint32_t)de_ldpc_lmn0_addr), // temp_0，分别位于4个DM
                         MPU_ADDR((uint32_t)de_ldpc_lmn1_addr), // temp_1
                         MPU_ADDR((uint32_t)de_ldpc_lmn2_addr), // temp_2
                         MPU_ADDR((uint32_t)de_ldpc_lmn3_addr), // temp_3
                         MPU_ADDR((uint32_t)de_ldpc_check_para0_addr), // BG1_Check_Matrix_0，译码校验矩阵
                         MPU_ADDR((uint32_t)de_ldpc_check_para1_addr), // BG1_Check_Matrix_1，译码校验矩阵
//                         MPU_ADDR(LdpcOut)); // data_out
						 MPU_ADDR(time_data_dm0_ptr));
         SVRReg[0] = MPU_ADDR(cfg_addr);
         LdpcDecBG1AllZcAsm(SVRReg);
         WAIT_MPU_STOP;

         LdpcOut = res_ptr + out_len * i;
//          ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR(LdpcOut),
         ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR((uint32_t)time_data_dm0_ptr),
//                                 (uint64_t)res_ptr,
        		 	 	 	 	 (uint64_t)LdpcOut,
                                 out_len,
//								 2048,
                                 DMA_TAG_L2G,
                                 1);
          ape_csu_task_lookup(DMA_TAG_L2G, 1);
    };
    
//   ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR((uint32_t)receiver_bit_config_dm0_ptr),
//                                       (uint64_t)res_ptr,
//                                       37568,
//                                       DMA_TAG_L2G,
//                                       1);

    //data读入
    //获取源数据地址
    // time_data_ddr_ptr = (uint32_t)*param_ptr;
    // time_data_length = (uint32_t)((*(param_ptr + 1) +7)>>3);//计算byte数
    // ape_csu_task_lookup(DMA_TAG_G2L, 1);

    // ape_csu_dma_1D_G2L_ch0ch1_transfer((uint64_t)time_data_ddr_ptr,
    //                          (uint64_t)DM_TO_CSU_ADDR(time_data_dm0_ptr),
    //                          time_data_length,
    //                          DMA_TAG_G2L,
    //                          0);



    // //计算结果搬移到外存
    // temp_u32 = 1000;//计算byte数
    // WAIT_MPU_STOP;
    // ape_csu_task_lookup(DMA_TAG_G2L, 1);
    
    // ape_csu_dma_1D_L2G_ch0ch1_transfer((uint64_t)DM_TO_CSU_ADDR(time_data_dm0_ptr),
    //                         (uint64_t)res_ptr,
    //                         temp_u32,
    //                         DMA_TAG_L2G,
    //                         1);

    return;
}
