nr_init_ue.c 28.7 KB
Newer Older
Hongzhi Wang's avatar
Hongzhi Wang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

#include "phy_init.h"
#include "SCHED_UE/sched_UE.h"
#include "PHY/phy_extern_nr_ue.h"
25
//#include "SIMULATION/TOOLS/sim.h"
26
/*#include "RadioResourceConfigCommonSIB.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
27 28
#include "RadioResourceConfigDedicated.h"
#include "TDD-Config.h"
29 30
#include "MBSFN-SubframeConfigList.h"*/
#include "openair1/PHY/defs_RU.h"
31
#include "openair1/PHY/impl_defs_nr.h"
32
#include "common/utils/LOG/vcd_signal_dumper.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
33 34 35 36
#include "assertions.h"
#include <math.h>
#include "PHY/NR_UE_TRANSPORT/nr_transport_ue.h"
#include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
37
//#include "PHY/LTE_REFSIG/lte_refsig.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
38
#include "PHY/CODING/nrPolar_tools/nr_polar_pbch_defs.h"
hongzhi wang's avatar
hongzhi wang committed
39 40
#include "PHY/INIT/phy_init.h"
#include "PHY/NR_REFSIG/pss_nr.h"
41 42
#include "PHY/NR_REFSIG/ul_ref_seq_nr.h"
#include "PHY/NR_REFSIG/refsig_defs_ue.h"
Sakthivel Velumani's avatar
Sakthivel Velumani committed
43
#include "PHY/NR_REFSIG/nr_refsig.h"
Hongzhi Wang's avatar
Hongzhi Wang committed
44

45
#if 0
46
void phy_config_harq_ue(module_id_t Mod_id,
47
                        int CC_id,
48
                        uint8_t gNB_id,
49
                        uint16_t max_harq_tx) {
50
  int num_of_threads,num_of_code_words;
hongzhi wang's avatar
hongzhi wang committed
51
  PHY_VARS_NR_UE *phy_vars_ue = PHY_vars_UE_g[Mod_id][CC_id];
52

53 54
  for (num_of_threads=0; num_of_threads<RX_NB_TH_MAX; num_of_threads++)
    for (num_of_code_words=0; num_of_code_words<NR_MAX_NB_CODEWORDS; num_of_code_words++)
55
      phy_vars_ue->ulsch[num_of_threads][gNB_id][num_of_code_words]->Mlimit = max_harq_tx;
Hongzhi Wang's avatar
Hongzhi Wang committed
56
}
57
#endif
Hongzhi Wang's avatar
Hongzhi Wang committed
58 59 60 61 62 63 64 65

extern uint16_t beta_cqi[16];

/*! \brief Helper function to allocate memory for DLSCH data structures.
 * \param[out] pdsch Pointer to the LTE_UE_PDSCH structure to initialize.
 * \param[in] frame_parms LTE_DL_FRAME_PARMS structure.
 * \note This function is optimistic in that it expects malloc() to succeed.
 */
66 67
void phy_init_nr_ue__PDSCH(NR_UE_PDSCH *const pdsch,
                           const NR_DL_FRAME_PARMS *const fp) {
Hongzhi Wang's avatar
Hongzhi Wang committed
68
  AssertFatal( pdsch, "pdsch==0" );
69 70 71 72
  pdsch->pmi_ext = (uint8_t *)malloc16_clear( fp->N_RB_DL );
  pdsch->llr[0] = (int16_t *)malloc16_clear( (8*(3*8*6144))*sizeof(int16_t) );
  pdsch->layer_llr[0] = (int16_t *)malloc16_clear( (8*(3*8*6144))*sizeof(int16_t) );
  pdsch->llr128 = (int16_t **)malloc16_clear( sizeof(int16_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
73 74
  // FIXME! no further allocation for (int16_t*)pdsch->llr128 !!! expect SIGSEGV
  // FK, 11-3-2015: this is only as a temporary pointer, no memory is stored there
75 76 77
  pdsch->rxdataF_ext            = (int32_t **)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->rxdataF_uespec_pilots  = (int32_t **)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->rxdataF_comp0          = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
78
  pdsch->rho                    = (int32_t ***)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t **) );
79 80 81 82
  pdsch->dl_ch_estimates        = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_ch_estimates_ext    = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_bf_ch_estimates     = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_bf_ch_estimates_ext = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
83 84
  //pdsch->dl_ch_rho_ext          = (int32_t**)malloc16_clear( 8*sizeof(int32_t*) );
  //pdsch->dl_ch_rho2_ext         = (int32_t**)malloc16_clear( 8*sizeof(int32_t*) );
85 86 87 88 89 90
  pdsch->dl_ch_mag0             = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_ch_magb0            = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_ch_magr0            = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->ptrs_phase_per_slot    = (int32_t **)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->ptrs_re_per_slot       = (int32_t **)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t *) );
  pdsch->dl_ch_ptrs_estimates_ext = (int32_t **)malloc16_clear( fp->nb_antennas_rx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
91
  // the allocated memory size is fixed:
92
  AssertFatal( fp->nb_antennas_rx <= 4, "nb_antennas_rx > 4" );//Extend the max number of UE Rx antennas to 4
Hongzhi Wang's avatar
Hongzhi Wang committed
93

94
  const size_t num = 7*2*fp->N_RB_DL*12;
Hongzhi Wang's avatar
Hongzhi Wang committed
95
  for (int i=0; i<fp->nb_antennas_rx; i++) {
96 97 98 99 100 101
    pdsch->rxdataF_ext[i]              = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
    pdsch->rxdataF_uespec_pilots[i]    = (int32_t *)malloc16_clear( sizeof(int32_t) * fp->N_RB_DL*12);
    pdsch->ptrs_phase_per_slot[i]      = (int32_t *)malloc16_clear( sizeof(int32_t) * 14 );
    pdsch->ptrs_re_per_slot[i]         = (int32_t *)malloc16_clear( sizeof(int32_t) * 14);
    pdsch->dl_ch_ptrs_estimates_ext[i] = (int32_t *)malloc16_clear( sizeof(int32_t) * num);
    pdsch->rho[i]                      = (int32_t **)malloc16_clear( NR_MAX_NB_LAYERS*NR_MAX_NB_LAYERS*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
102

103
    for (int j=0; j<NR_MAX_NB_LAYERS; j++) {
104
      const int idx = (j*fp->nb_antennas_rx)+i;
105
      for (int k=0; k<NR_MAX_NB_LAYERS; k++) {
106
        pdsch->rho[i][j*NR_MAX_NB_LAYERS+k] = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
107
      }
108 109 110 111 112
      pdsch->rxdataF_comp0[idx]           = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
      pdsch->dl_ch_estimates[idx]         = (int32_t *)malloc16_clear( sizeof(int32_t) * fp->ofdm_symbol_size*7*2);
      pdsch->dl_ch_estimates_ext[idx]     = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
      pdsch->dl_bf_ch_estimates[idx]      = (int32_t *)malloc16_clear( sizeof(int32_t) * fp->ofdm_symbol_size*7*2);
      pdsch->dl_bf_ch_estimates_ext[idx]  = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
Hongzhi Wang's avatar
Hongzhi Wang committed
113 114
      //pdsch->dl_ch_rho_ext[idx]           = (int32_t*)malloc16_clear( sizeof(int32_t) * num );
      //pdsch->dl_ch_rho2_ext[idx]          = (int32_t*)malloc16_clear( sizeof(int32_t) * num );
115 116
      pdsch->dl_ch_mag0[idx]              = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
      pdsch->dl_ch_magb0[idx]             = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
117
      pdsch->dl_ch_magr0[idx]             = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
Hongzhi Wang's avatar
Hongzhi Wang committed
118 119 120 121
    }
  }
}

122 123
void phy_init_nr_ue_PUSCH(NR_UE_PUSCH *const pusch,
                          const NR_DL_FRAME_PARMS *const fp) {
124 125
  AssertFatal( pusch, "pusch==0" );

126 127
  for (int i=0; i<NR_MAX_NB_LAYERS; i++) {
    pusch->txdataF_layers[i] = (int32_t *)malloc16_clear((NR_MAX_PUSCH_ENCODED_LENGTH)*sizeof(int32_t *));
128
  }
129 130
}

Hongzhi Wang's avatar
Hongzhi Wang committed
131
int init_nr_ue_signal(PHY_VARS_NR_UE *ue,
132
                      int nb_connected_gNB,
133
                      uint8_t abstraction_flag) {
Hongzhi Wang's avatar
Hongzhi Wang committed
134
  // create shortcuts
135 136 137 138
  NR_DL_FRAME_PARMS *const fp            = &ue->frame_parms;
  NR_UE_COMMON *const common_vars        = &ue->common_vars;
  NR_UE_PBCH  **const pbch_vars          = ue->pbch_vars;
  NR_UE_PRACH **const prach_vars         = ue->prach_vars;
139
  int i,j,k,l,slot,symb,q;
140
  int gNB_id;
Hongzhi Wang's avatar
Hongzhi Wang committed
141
  int th_id;
142
  uint32_t ****pusch_dmrs;
143
  uint16_t N_n_scid[2] = {0,1}; // [HOTFIX] This is a temporary implementation of scramblingID0 and scramblingID1 which are given by DMRS-UplinkConfig
144
  int n_scid;
Hongzhi Wang's avatar
Hongzhi Wang committed
145
  abstraction_flag = 0;
146
  // dmrs_UplinkConfig_t *dmrs_Uplink_Config = &ue->pusch_config.dmrs_UplinkConfig;
147
  // ptrs_UplinkConfig_t *ptrs_Uplink_Config = &ue->pusch_config.dmrs_UplinkConfig.ptrs_UplinkConfig;
cig's avatar
cig committed
148
  LOG_I(PHY, "Initializing UE vars (abstraction %u) for gNB TXant %u, UE RXant %u\n", abstraction_flag, fp->nb_antennas_tx, fp->nb_antennas_rx);
149
  //LOG_D(PHY,"[MSC_NEW][FRAME 00000][PHY_UE][MOD %02u][]\n", ue->Mod_id+NB_gNB_INST);
hongzhi wang's avatar
hongzhi wang committed
150
  phy_init_nr_top(ue);
Hongzhi Wang's avatar
Hongzhi Wang committed
151
  // many memory allocation sizes are hard coded
152 153
  AssertFatal( fp->nb_antennas_rx <= 4, "hard coded allocation for ue_common_vars->dl_ch_estimates[gNB_id]" );
  AssertFatal( nb_connected_gNB <= NUMBER_OF_CONNECTED_gNB_MAX, "n_connected_gNB is too large" );
Hongzhi Wang's avatar
Hongzhi Wang committed
154 155 156 157 158 159 160 161
  // init phy_vars_ue

  for (i=0; i<4; i++) {
    ue->rx_gain_max[i] = 135;
    ue->rx_gain_med[i] = 128;
    ue->rx_gain_byp[i] = 120;
  }

162
  ue->n_connected_gNB = nb_connected_gNB;
Hongzhi Wang's avatar
Hongzhi Wang committed
163

164 165 166 167 168
  for(gNB_id = 0; gNB_id < ue->n_connected_gNB; gNB_id++) {
    ue->total_TBS[gNB_id] = 0;
    ue->total_TBS_last[gNB_id] = 0;
    ue->bitrate[gNB_id] = 0;
    ue->total_received_bits[gNB_id] = 0;
169

170 171 172
    ue->ul_time_alignment[gNB_id].apply_ta = 0;
    ue->ul_time_alignment[gNB_id].ta_frame = -1;
    ue->ul_time_alignment[gNB_id].ta_slot  = -1;
Hongzhi Wang's avatar
Hongzhi Wang committed
173
  }
Sakthivel Velumani's avatar
Sakthivel Velumani committed
174 175
  // init NR modulation lookup tables
  nr_generate_modulation_table();
Hongzhi Wang's avatar
Hongzhi Wang committed
176

177 178 179
  /////////////////////////PUSCH init/////////////////////////
  ///////////
  for (th_id = 0; th_id < RX_NB_TH_MAX; th_id++) {
180 181 182
    for (gNB_id = 0; gNB_id < ue->n_connected_gNB; gNB_id++) {
      ue->pusch_vars[th_id][gNB_id] = (NR_UE_PUSCH *)malloc16(sizeof(NR_UE_PUSCH));
      phy_init_nr_ue_PUSCH( ue->pusch_vars[th_id][gNB_id], fp );
183 184 185
    }
  }

186 187 188 189 190 191 192 193 194 195
  /////////////////////////PUCCH init/////////////////////////
  ///////////
  for (th_id = 0; th_id < RX_NB_TH_MAX; th_id++) {
    for (gNB_id = 0; gNB_id < ue->n_connected_gNB; gNB_id++) {
      ue->pucch_vars[th_id][gNB_id] = (NR_UE_PUCCH *)malloc16(sizeof(NR_UE_PUCCH));
      for (i=0; i<2; i++)
        ue->pucch_vars[th_id][gNB_id]->active[i] = false;
    }
  }

196 197
  ///////////
  ////////////////////////////////////////////////////////////////////////////////////////////
198

199 200
  /////////////////////////PUSCH DMRS init/////////////////////////
  ///////////
201 202

  // default values until overwritten by RRCConnectionReconfiguration
Khalid Ahmed's avatar
Khalid Ahmed committed
203

204
  for (i=0; i<MAX_NR_OF_UL_ALLOCATIONS; i++) {
Khalid Ahmed's avatar
Khalid Ahmed committed
205
    ue->pusch_config.pusch_TimeDomainResourceAllocation[i] = (PUSCH_TimeDomainResourceAllocation_t *)malloc16(sizeof(PUSCH_TimeDomainResourceAllocation_t));
206
    ue->pusch_config.pusch_TimeDomainResourceAllocation[i]->mappingType = typeB;
Khalid Ahmed's avatar
Khalid Ahmed committed
207 208
  }

209
  for (i=0;i<MAX_NR_OF_DL_ALLOCATIONS;i++){
210
    ue->PDSCH_Config.pdsch_TimeDomainResourceAllocation[i] = (PDSCH_TimeDomainResourceAllocation_t *)malloc16(sizeof(PDSCH_TimeDomainResourceAllocation_t));
211
    ue->PDSCH_Config.pdsch_TimeDomainResourceAllocation[i]->mappingType = typeA;
Khalid Ahmed's avatar
Khalid Ahmed committed
212 213
  }

214
  //------------- config DMRS parameters--------------//
215 216 217
  // dmrs_Uplink_Config->pusch_dmrs_type = pusch_dmrs_type1;
  // dmrs_Uplink_Config->pusch_dmrs_AdditionalPosition = pusch_dmrs_pos0;
  // dmrs_Uplink_Config->pusch_maxLength = pusch_len1;
218
  //-------------------------------------------------//
219
  ue->dmrs_DownlinkConfig.pdsch_dmrs_type = pdsch_dmrs_type1;
220 221 222 223
  ue->dmrs_DownlinkConfig.pdsch_dmrs_AdditionalPosition = pdsch_dmrs_pos0;
  ue->dmrs_DownlinkConfig.pdsch_maxLength = pdsch_len1;
  //-------------------------------------------------//

224 225 226
  ue->nr_gold_pusch_dmrs = (uint32_t ****)malloc16(fp->slots_per_frame*sizeof(uint32_t ***));
  pusch_dmrs             = ue->nr_gold_pusch_dmrs;
  n_scid = 0; // This quantity is indicated by higher layer parameter dmrs-SeqInitialization
227

228 229
  for (slot=0; slot<fp->slots_per_frame; slot++) {
    pusch_dmrs[slot] = (uint32_t ***)malloc16(fp->symbols_per_slot*sizeof(uint32_t **));
230
    AssertFatal(pusch_dmrs[slot]!=NULL, "init_nr_ue_signal: pusch_dmrs for slot %d - malloc failed\n", slot);
231 232 233

    for (symb=0; symb<fp->symbols_per_slot; symb++) {
      pusch_dmrs[slot][symb] = (uint32_t **)malloc16(NR_MAX_NB_CODEWORDS*sizeof(uint32_t *));
234
      AssertFatal(pusch_dmrs[slot][symb]!=NULL, "init_nr_ue_signal: pusch_dmrs for slot %d symbol %d - malloc failed\n", slot, symb);
235

236
      for (q=0; q<NR_MAX_NB_CODEWORDS; q++) {
237
        pusch_dmrs[slot][symb][q] = (uint32_t *)malloc16(NR_MAX_PDSCH_DMRS_INIT_LENGTH_DWORD*sizeof(uint32_t));
238 239 240 241 242 243
        AssertFatal(pusch_dmrs[slot][symb][q]!=NULL, "init_nr_ue_signal: pusch_dmrs for slot %d symbol %d codeword %d - malloc failed\n", slot, symb, q);
      }
    }
  }

  nr_init_pusch_dmrs(ue, N_n_scid, n_scid);
244 245
  ///////////
  ////////////////////////////////////////////////////////////////////////////////////////////
246

247 248 249 250
  /////////////////////////PUSCH PTRS init/////////////////////////
  ///////////

  //------------- config PTRS parameters--------------//
251 252 253 254 255 256
  // ptrs_Uplink_Config->timeDensity.ptrs_mcs1 = 2; // setting MCS values to 0 indicate abscence of time_density field in the configuration
  // ptrs_Uplink_Config->timeDensity.ptrs_mcs2 = 4;
  // ptrs_Uplink_Config->timeDensity.ptrs_mcs3 = 10;
  // ptrs_Uplink_Config->frequencyDensity.n_rb0 = 25;     // setting N_RB values to 0 indicate abscence of frequency_density field in the configuration
  // ptrs_Uplink_Config->frequencyDensity.n_rb1 = 75;
  // ptrs_Uplink_Config->resourceElementOffset = 0;
257 258 259 260 261
  //-------------------------------------------------//

  ///////////
  ////////////////////////////////////////////////////////////////////////////////////////////

262
  for (i=0; i<10; i++)
Hongzhi Wang's avatar
Hongzhi Wang committed
263 264 265 266
    ue->tx_power_dBm[i]=-127;

  if (abstraction_flag == 0) {
    // init TX buffers
267 268
    common_vars->txdata  = (int32_t **)malloc16( fp->nb_antennas_tx*sizeof(int32_t *) );
    common_vars->txdataF = (int32_t **)malloc16( fp->nb_antennas_tx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
269 270

    for (i=0; i<fp->nb_antennas_tx; i++) {
271
      common_vars->txdata[i]  = (int32_t *)malloc16_clear( fp->samples_per_subframe*10*sizeof(int32_t) );
272
      common_vars->txdataF[i] = (int32_t *)malloc16_clear( fp->samples_per_slot_wCP*sizeof(int32_t) );
Hongzhi Wang's avatar
Hongzhi Wang committed
273 274 275
    }

    // init RX buffers
276 277
    common_vars->rxdata   = (int32_t **)malloc16( fp->nb_antennas_rx*sizeof(int32_t *) );

Hongzhi Wang's avatar
Hongzhi Wang committed
278
    for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
279
      common_vars->common_vars_rx_data_per_thread[th_id].rxdataF  = (int32_t **)malloc16( fp->nb_antennas_rx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
280 281 282
    }

    for (i=0; i<fp->nb_antennas_rx; i++) {
283 284
      common_vars->rxdata[i] = (int32_t *) malloc16_clear( (2*(fp->samples_per_frame)+2048)*sizeof(int32_t) );

Hongzhi Wang's avatar
Hongzhi Wang committed
285
      for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
286
        common_vars->common_vars_rx_data_per_thread[th_id].rxdataF[i] = (int32_t *)malloc16_clear( sizeof(int32_t)*(fp->samples_per_slot_wCP) );
Hongzhi Wang's avatar
Hongzhi Wang committed
287 288 289 290
      }
    }
  }

291
  //PDCCH DMRS init (gNB offset = 0)
292 293 294 295 296 297 298 299 300 301 302 303 304 305
  ue->nr_gold_pdcch[0] = (uint32_t ***)malloc16(fp->slots_per_frame*sizeof(uint32_t **));
  uint32_t ***pdcch_dmrs = ue->nr_gold_pdcch[0];
  AssertFatal(pdcch_dmrs!=NULL, "NR init: pdcch_dmrs malloc failed\n");

  for (int slot=0; slot<fp->slots_per_frame; slot++) {
    pdcch_dmrs[slot] = (uint32_t **)malloc16(fp->symbols_per_slot*sizeof(uint32_t *));
    AssertFatal(pdcch_dmrs[slot]!=NULL, "NR init: pdcch_dmrs for slot %d - malloc failed\n", slot);

    for (int symb=0; symb<fp->symbols_per_slot; symb++) {
      pdcch_dmrs[slot][symb] = (uint32_t *)malloc16(NR_MAX_PDCCH_DMRS_INIT_LENGTH_DWORD*sizeof(uint32_t));
      AssertFatal(pdcch_dmrs[slot][symb]!=NULL, "NR init: pdcch_dmrs for slot %d symbol %d - malloc failed\n", slot, symb);
    }
  }

306 307 308
  ue->scramblingID_pdcch = fp->Nid_cell;
  nr_gold_pdcch(ue,fp->Nid_cell);

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  //PDSCH DMRS init (eNB offset = 0)
  ue->nr_gold_pdsch[0] = (uint32_t ****)malloc16(fp->slots_per_frame*sizeof(uint32_t ***));
  uint32_t ****pdsch_dmrs = ue->nr_gold_pdsch[0];

  for (int slot=0; slot<fp->slots_per_frame; slot++) {
    pdsch_dmrs[slot] = (uint32_t ***)malloc16(fp->symbols_per_slot*sizeof(uint32_t **));
    AssertFatal(pdsch_dmrs[slot]!=NULL, "NR init: pdsch_dmrs for slot %d - malloc failed\n", slot);

    for (int symb=0; symb<fp->symbols_per_slot; symb++) {
      pdsch_dmrs[slot][symb] = (uint32_t **)malloc16(NR_MAX_NB_CODEWORDS*sizeof(uint32_t *));
      AssertFatal(pdsch_dmrs[slot][symb]!=NULL, "NR init: pdsch_dmrs for slot %d symbol %d - malloc failed\n", slot, symb);

      for (int q=0; q<NR_MAX_NB_CODEWORDS; q++) {
        pdsch_dmrs[slot][symb][q] = (uint32_t *)malloc16(NR_MAX_PDSCH_DMRS_INIT_LENGTH_DWORD*sizeof(uint32_t));
        AssertFatal(pdsch_dmrs[slot][symb][q]!=NULL, "NR init: pdsch_dmrs for slot %d symbol %d codeword %d - malloc failed\n", slot, symb, q);
      }
    }
  }

328 329 330 331 332 333
  // initializing the scrambling IDs for PDSCH DMRS
  for (int i=0; i<2; i++)
    ue->scramblingID[i]=fp->Nid_cell;

  nr_gold_pdsch(ue,ue->scramblingID);

Hongzhi Wang's avatar
Hongzhi Wang committed
334
  // DLSCH
335
  for (gNB_id = 0; gNB_id < ue->n_connected_gNB; gNB_id++) {
Hongzhi Wang's avatar
Hongzhi Wang committed
336
    for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
337
      ue->pdsch_vars[th_id][gNB_id] = (NR_UE_PDSCH *)malloc16_clear(sizeof(NR_UE_PDSCH));
Hongzhi Wang's avatar
Hongzhi Wang committed
338 339 340
    }

    for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
341
      ue->pdcch_vars[th_id][gNB_id] = (NR_UE_PDCCH *)malloc16_clear(sizeof(NR_UE_PDCCH));
Hongzhi Wang's avatar
Hongzhi Wang committed
342 343
    }

344 345
    prach_vars[gNB_id] = (NR_UE_PRACH *)malloc16_clear(sizeof(NR_UE_PRACH));
    pbch_vars[gNB_id] = (NR_UE_PBCH *)malloc16_clear(sizeof(NR_UE_PBCH));
Hongzhi Wang's avatar
Hongzhi Wang committed
346 347 348

    if (abstraction_flag == 0) {
      for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
349
        phy_init_nr_ue__PDSCH( ue->pdsch_vars[th_id][gNB_id], fp );
Hongzhi Wang's avatar
Hongzhi Wang committed
350 351 352
      }

      for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
353 354 355 356 357
        ue->pdsch_vars[th_id][gNB_id]->llr_shifts          = (uint8_t *)malloc16_clear(7*2*fp->N_RB_DL*12);
        ue->pdsch_vars[th_id][gNB_id]->llr_shifts_p        = ue->pdsch_vars[0][gNB_id]->llr_shifts;
        ue->pdsch_vars[th_id][gNB_id]->llr[1]              = (int16_t *)malloc16_clear( (8*(3*8*8448))*sizeof(int16_t) );
        ue->pdsch_vars[th_id][gNB_id]->layer_llr[1]        = (int16_t *)malloc16_clear( (8*(3*8*8448))*sizeof(int16_t) );
        ue->pdsch_vars[th_id][gNB_id]->llr128_2ndstream    = (int16_t **)malloc16_clear( sizeof(int16_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
358 359 360 361
      }


      for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
362
        ue->pdsch_vars[th_id][gNB_id]->dl_ch_rho2_ext      = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
363 364 365 366
      }

      for (i=0; i<fp->nb_antennas_rx; i++)
        for (j=0; j<4; j++) {
367
          const int idx = (j*fp->nb_antennas_rx)+i;
Hongzhi Wang's avatar
Hongzhi Wang committed
368
          const size_t num = 7*2*fp->N_RB_DL*12+4;
369

Hongzhi Wang's avatar
Hongzhi Wang committed
370
          for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
371
            ue->pdsch_vars[th_id][gNB_id]->dl_ch_rho2_ext[idx] = (int32_t *)malloc16_clear( sizeof(int32_t) * num );
Hongzhi Wang's avatar
Hongzhi Wang committed
372 373 374 375
          }
        }

      //const size_t num = 7*2*fp->N_RB_DL*12+4;
376 377
      for (k=0; k<8; k++) { //harq_pid
        for (l=0; l<8; l++) { //round
Hongzhi Wang's avatar
Hongzhi Wang committed
378
          for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
379 380 381 382
            ue->pdsch_vars[th_id][gNB_id]->rxdataF_comp1[k][l] = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
            ue->pdsch_vars[th_id][gNB_id]->dl_ch_rho_ext[k][l] = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
            ue->pdsch_vars[th_id][gNB_id]->dl_ch_mag1[k][l]    = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
            ue->pdsch_vars[th_id][gNB_id]->dl_ch_magb1[k][l]   = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
Hongzhi Wang's avatar
Hongzhi Wang committed
383 384 385 386
          }

          for (int i=0; i<fp->nb_antennas_rx; i++)
            for (int j=0; j<4; j++) { //frame_parms->nb_antennas_tx; j++)
387
              const int idx = (j*fp->nb_antennas_rx)+i;
388

Hongzhi Wang's avatar
Hongzhi Wang committed
389
              for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
390 391 392 393
                ue->pdsch_vars[th_id][gNB_id]->dl_ch_rho_ext[k][l][idx] = (int32_t *)malloc16_clear(7*2*sizeof(int32_t)*(fp->N_RB_DL*12));
                ue->pdsch_vars[th_id][gNB_id]->rxdataF_comp1[k][l][idx] = (int32_t *)malloc16_clear(7*2*sizeof(int32_t)*(fp->N_RB_DL*12));
                ue->pdsch_vars[th_id][gNB_id]->dl_ch_mag1[k][l][idx]    = (int32_t *)malloc16_clear(7*2*sizeof(int32_t)*(fp->N_RB_DL*12));
                ue->pdsch_vars[th_id][gNB_id]->dl_ch_magb1[k][l][idx]   = (int32_t *)malloc16_clear(7*2*sizeof(int32_t)*(fp->N_RB_DL*12));
Hongzhi Wang's avatar
Hongzhi Wang committed
394
              }
395
            }
Hongzhi Wang's avatar
Hongzhi Wang committed
396 397
        }
      }
398

Hongzhi Wang's avatar
Hongzhi Wang committed
399 400
      // 100 PRBs * 12 REs/PRB * 4 PDCCH SYMBOLS * 2 LLRs/RE
      for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
401 402 403 404 405 406 407 408 409
        ue->pdcch_vars[th_id][gNB_id]->llr                 = (int16_t *)malloc16_clear( 2*4*100*12*sizeof(uint16_t) );
        ue->pdcch_vars[th_id][gNB_id]->llr16               = (int16_t *)malloc16_clear( 2*4*100*12*sizeof(uint16_t) );
        ue->pdcch_vars[th_id][gNB_id]->wbar                = (int16_t *)malloc16_clear( 2*4*100*12*sizeof(uint16_t) );
        ue->pdcch_vars[th_id][gNB_id]->e_rx                = (int16_t *)malloc16_clear( 4*2*100*12 );
        ue->pdcch_vars[th_id][gNB_id]->rxdataF_comp        = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
        ue->pdcch_vars[th_id][gNB_id]->dl_ch_rho_ext       = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
        ue->pdcch_vars[th_id][gNB_id]->rho                 = (int32_t **)malloc16( fp->nb_antennas_rx*sizeof(int32_t *) );
        ue->pdcch_vars[th_id][gNB_id]->rxdataF_ext         = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
        ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates_ext = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
410
        // Channel estimates
411 412
        ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates      = (int32_t **)malloc16_clear(4*fp->nb_antennas_rx*sizeof(int32_t *));
        ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates_time = (int32_t **)malloc16_clear(4*fp->nb_antennas_rx*sizeof(int32_t *));
413 414

        for (i=0; i<fp->nb_antennas_rx; i++) {
415
          ue->pdcch_vars[th_id][gNB_id]->rho[i] = (int32_t *)malloc16_clear( sizeof(int32_t)*(100*12*4));
416 417

          for (j=0; j<4; j++) {
418
            int idx = (j*fp->nb_antennas_rx)+i;
419 420
            ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates[idx] = (int32_t *)malloc16_clear( sizeof(int32_t)*fp->symbols_per_slot*(fp->ofdm_symbol_size+LTE_CE_FILTER_LENGTH) );
            ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates_time[idx] = (int32_t *)malloc16_clear( sizeof(int32_t)*fp->ofdm_symbol_size*2 );
421 422
            //  size_t num = 7*2*fp->N_RB_DL*12;
            size_t num = 4*273*12;  // 4 symbols, 100 PRBs, 12 REs per PRB
423 424 425 426
            ue->pdcch_vars[th_id][gNB_id]->rxdataF_comp[idx]        = (int32_t *)malloc16_clear(sizeof(int32_t) * num);
            ue->pdcch_vars[th_id][gNB_id]->dl_ch_rho_ext[idx]       = (int32_t *)malloc16_clear(sizeof(int32_t) * num);
            ue->pdcch_vars[th_id][gNB_id]->rxdataF_ext[idx]         = (int32_t *)malloc16_clear(sizeof(int32_t) * num);
            ue->pdcch_vars[th_id][gNB_id]->dl_ch_estimates_ext[idx] = (int32_t *)malloc16_clear(sizeof(int32_t) * num);
427 428
          }
        }
Hongzhi Wang's avatar
Hongzhi Wang committed
429 430 431
      }

      // PBCH
432 433 434 435 436 437 438 439
      pbch_vars[gNB_id]->rxdataF_ext         = (int32_t **)malloc16( fp->nb_antennas_rx*sizeof(int32_t *) );
      pbch_vars[gNB_id]->rxdataF_comp        = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
      pbch_vars[gNB_id]->dl_ch_estimates     = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
      pbch_vars[gNB_id]->dl_ch_estimates_ext = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
      pbch_vars[gNB_id]->dl_ch_estimates_time = (int32_t **)malloc16_clear( 4*fp->nb_antennas_rx*sizeof(int32_t *) );
      pbch_vars[gNB_id]->llr                 = (int16_t *)malloc16_clear( 1920 ); //
      prach_vars[gNB_id]->prachF             = (int16_t *)malloc16_clear( sizeof(int)*(7*2*sizeof(int)*(fp->ofdm_symbol_size*12)) );
      prach_vars[gNB_id]->prach              = (int16_t *)malloc16_clear( sizeof(int)*(7*2*sizeof(int)*(fp->ofdm_symbol_size*12)) );
Hongzhi Wang's avatar
Hongzhi Wang committed
440 441

      for (i=0; i<fp->nb_antennas_rx; i++) {
442
        pbch_vars[gNB_id]->rxdataF_ext[i]    = (int32_t *)malloc16_clear( sizeof(int32_t)*20*12*4 );
Hongzhi Wang's avatar
Hongzhi Wang committed
443 444

        for (j=0; j<4; j++) {//fp->nb_antennas_tx;j++) {
445
          int idx = (j*fp->nb_antennas_rx)+i;
446 447 448 449
          pbch_vars[gNB_id]->rxdataF_comp[idx]        = (int32_t *)malloc16_clear( sizeof(int32_t)*20*12*4 );
          pbch_vars[gNB_id]->dl_ch_estimates[idx]     = (int32_t *)malloc16_clear( sizeof(int32_t)*7*2*sizeof(int)*(fp->ofdm_symbol_size) );
          pbch_vars[gNB_id]->dl_ch_estimates_time[idx]= (int32_t *)malloc16_clear( sizeof(int32_t)*7*2*sizeof(int)*(fp->ofdm_symbol_size) );
          pbch_vars[gNB_id]->dl_ch_estimates_ext[idx] = (int32_t *)malloc16_clear( sizeof(int32_t)*20*12*4 );
Hongzhi Wang's avatar
Hongzhi Wang committed
450 451 452 453
        }
      }
    }

454
    pbch_vars[gNB_id]->decoded_output = (uint8_t *)malloc16_clear(64);
Hongzhi Wang's avatar
Hongzhi Wang committed
455 456 457 458
  }

  // initialization for the last instance of pdsch_vars (used for MU-MIMO)
  for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
459
    ue->pdsch_vars[th_id][gNB_id] = (NR_UE_PDSCH *)malloc16_clear(sizeof(NR_UE_PDSCH));
Hongzhi Wang's avatar
Hongzhi Wang committed
460 461 462 463
  }

  if (abstraction_flag == 0) {
    for (th_id=0; th_id<RX_NB_TH_MAX; th_id++) {
464 465 466
      //phy_init_lte_ue__PDSCH( ue->pdsch_vars[th_id][gNB_id], fp );
      ue->pdsch_vars[th_id][gNB_id]->llr[1] = (int16_t *)malloc16_clear((8*(3*8*8448))*sizeof(int16_t));
      ue->pdsch_vars[th_id][gNB_id]->layer_llr[1] = (int16_t *)malloc16_clear((8*(3*8*8448))*sizeof(int16_t));
Hongzhi Wang's avatar
Hongzhi Wang committed
467 468
    }
  } else { //abstraction == 1
469
    ue->sinr_dB = (double *) malloc16_clear( fp->N_RB_DL*12*sizeof(double) );
Hongzhi Wang's avatar
Hongzhi Wang committed
470 471
  }

472
  ue->sinr_CQI_dB = (double *) malloc16_clear( fp->N_RB_DL*12*sizeof(double) );
Hongzhi Wang's avatar
Hongzhi Wang committed
473
  ue->init_averaging = 1;
474

Hongzhi Wang's avatar
Hongzhi Wang committed
475
  // default value until overwritten by RRCConnectionReconfiguration
476
  if (fp->nb_antenna_ports_gNB==2)
Hongzhi Wang's avatar
Hongzhi Wang committed
477 478 479 480 481 482 483 484 485 486
    ue->pdsch_config_dedicated->p_a = dBm3;
  else
    ue->pdsch_config_dedicated->p_a = dB0;

  // set channel estimation to do linear interpolation in time
  ue->high_speed_flag = 1;
  ue->ch_est_alpha    = 24576;
  // enable MIB/SIB decoding by default
  ue->decode_MIB = 1;
  ue->decode_SIB = 1;
487 488 489

  init_nr_prach_tables(839);

Hongzhi Wang's avatar
Hongzhi Wang committed
490 491
  return 0;
}
hongzhi wang's avatar
hongzhi wang committed
492

yilmazt's avatar
yilmazt committed
493
void init_nr_ue_transport(PHY_VARS_NR_UE *ue,
494
                          int abstraction_flag) {
495
  for (int i = 0; i < NUMBER_OF_CONNECTED_gNB_MAX; i++) {
yilmazt's avatar
yilmazt committed
496 497
    for (int j=0; j<2; j++) {
      for (int k=0; k<RX_NB_TH_MAX; k++) {
498
        AssertFatal((ue->dlsch[k][i][j]  = new_nr_ue_dlsch(1,NR_MAX_DLSCH_HARQ_PROCESSES,NSOFT,MAX_LDPC_ITERATIONS,ue->frame_parms.N_RB_DL, abstraction_flag))!=NULL,"Can't get ue dlsch structures\n");
Khalid Ahmed's avatar
Khalid Ahmed committed
499
        LOG_D(PHY,"dlsch[%d][%d][%d] => %p\n",k,i,j,ue->dlsch[k][i][j]);
500
        AssertFatal((ue->ulsch[k][i][j]  = new_nr_ue_ulsch(ue->frame_parms.N_RB_UL, NR_MAX_ULSCH_HARQ_PROCESSES, abstraction_flag))!=NULL,"Can't get ue ulsch structures\n");
501
        LOG_D(PHY,"ulsch[%d][%d][%d] => %p\n",k,i,j,ue->ulsch[k][i][j]);
Hongzhi Wang's avatar
Hongzhi Wang committed
502 503 504
      }
    }

Hongzhi Wang's avatar
Hongzhi Wang committed
505 506
    ue->dlsch_SI[i]  = new_nr_ue_dlsch(1,1,NSOFT,MAX_LDPC_ITERATIONS,ue->frame_parms.N_RB_DL, abstraction_flag);
    ue->dlsch_ra[i]  = new_nr_ue_dlsch(1,1,NSOFT,MAX_LDPC_ITERATIONS,ue->frame_parms.N_RB_DL, abstraction_flag);
507
    ue->transmission_mode[i] = ue->frame_parms.nb_antenna_ports_gNB==1 ? 1 : 2;
Hongzhi Wang's avatar
Hongzhi Wang committed
508 509
  }

hongzhi wang's avatar
hongzhi wang committed
510
  //ue->frame_parms.pucch_config_common.deltaPUCCH_Shift = 1;
511
  ue->dlsch_MCH[0]  = new_nr_ue_dlsch(1,NR_MAX_DLSCH_HARQ_PROCESSES,NSOFT,MAX_LDPC_ITERATIONS_MBSFN,ue->frame_parms.N_RB_DL,0);
512 513 514

  for(int i=0; i<5; i++)
    ue->dl_stats[i] = 0;
Hongzhi Wang's avatar
Hongzhi Wang committed
515
}
hongzhi wang's avatar
hongzhi wang committed
516

517 518 519 520 521 522 523 524 525

void init_N_TA_offset(PHY_VARS_NR_UE *ue){

  NR_DL_FRAME_PARMS *fp = &ue->frame_parms;

  if (fp->frame_type == FDD) {
    ue->N_TA_offset = 0;
  } else {
    int N_TA_offset = fp->ul_CarrierFreq < 6e9 ? 400 : 431; // reference samples  for 25600Tc @ 30.72 Ms/s for FR1, same @ 61.44 Ms/s for FR2
526 527

    double factor = 1.0;
528 529 530
    switch (fp->numerology_index) {
      case 0: //15 kHz scs
        AssertFatal(N_TA_offset == 400, "scs_common 15kHz only for FR1\n");
531
        factor = fp->samples_per_subframe / 30720.0;
532 533 534
        break;
      case 1: //30 kHz sc
        AssertFatal(N_TA_offset == 400, "scs_common 30kHz only for FR1\n");
535
        factor = fp->samples_per_subframe / 30720.0;
536 537 538 539 540 541
        break;
      case 2: //60 kHz scs
        AssertFatal(1==0, "scs_common should not be 60 kHz\n");
        break;
      case 3: //120 kHz scs
        AssertFatal(N_TA_offset == 431, "scs_common 120kHz only for FR2\n");
542
        factor = fp->samples_per_subframe / 61440.0;
543 544
        break;
      case 4: //240 kHz scs
545 546
        AssertFatal(N_TA_offset == 431, "scs_common 240kHz only for FR2\n");
        factor = fp->samples_per_subframe / 61440.0;
547
        break;
548 549
      default:
        AssertFatal(1==0, "Invalid scs_common!\n");
550 551 552 553
    }

    ue->N_TA_offset = (int)(N_TA_offset * factor);

554
    LOG_I(PHY,"UE %d Setting N_TA_offset to %d samples (factor %f, UL Freq %lu, N_RB %d, mu %d)\n", ue->Mod_id, ue->N_TA_offset, factor, fp->ul_CarrierFreq, fp->N_RB_DL, fp->numerology_index);
555 556 557
  }
}

558
void phy_init_nr_top(PHY_VARS_NR_UE *ue) {
Raymond Knopp's avatar
Raymond Knopp committed
559
  NR_DL_FRAME_PARMS *frame_parms = &ue->frame_parms;
hongzhi wang's avatar
hongzhi wang committed
560
  crcTableInit();
frtabu's avatar
frtabu committed
561
  load_dftslib();
hongzhi wang's avatar
hongzhi wang committed
562 563
  init_context_synchro_nr(frame_parms);
  generate_ul_reference_signal_sequences(SHRT_MAX);
Hongzhi Wang's avatar
Hongzhi Wang committed
564
  // Polar encoder init for PBCH
hongzhi wang's avatar
hongzhi wang committed
565 566 567 568 569 570 571 572 573 574
  //lte_sync_time_init(frame_parms);
  //generate_ul_ref_sigs();
  //generate_ul_ref_sigs_rx();
  //generate_64qam_table();
  //generate_16qam_table();
  //generate_RIV_tables();
  //init_unscrambling_lut();
  //init_scrambling_lut();
  //set_taus_seed(1328);
}