nr_dlsch.c 21.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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
 */

22 23 24 25
/*! \file PHY/NR_TRANSPORT/dlsch_decoding.c
* \brief Top-level routines for transmission of the PDSCH 38211 v 15.2.0
* \author Guy De Souza
* \date 2018
26 27
* \version 0.1
* \company Eurecom
28
* \email: desouza@eurecom.fr
29 30 31
* \note
* \warning
*/
Guy De Souza's avatar
Guy De Souza committed
32

33 34
#include "nr_dlsch.h"
#include "nr_dci.h"
Guy De Souza's avatar
Guy De Souza committed
35
#include "nr_sch_dmrs.h"
36
#include "PHY/MODULATION/nr_modulation.h"
37
#include "PHY/NR_REFSIG/dmrs_nr.h"
38
#include "PHY/NR_REFSIG/ptrs_nr.h"
Rakesh's avatar
Rakesh committed
39
#include "common/utils/LOG/vcd_signal_dumper.h"
40
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
Guy De Souza's avatar
Guy De Souza committed
41

Guy De Souza's avatar
Guy De Souza committed
42
//#define DEBUG_DLSCH
Guy De Souza's avatar
Guy De Souza committed
43
//#define DEBUG_DLSCH_MAPPING
Guy De Souza's avatar
Guy De Souza committed
44

Guy De Souza's avatar
Guy De Souza committed
45
void nr_pdsch_codeword_scrambling(uint8_t *in,
46 47 48 49 50
                                  uint32_t size,
                                  uint8_t q,
                                  uint32_t Nid,
                                  uint32_t n_RNTI,
                                  uint32_t* out) {
51

52
  uint8_t reset, b_idx;
53
  uint32_t x1, x2, s=0;
Rakesh's avatar
Rakesh committed
54
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PDSCH_CODEWORD_SCRAMBLING, 1);
55 56 57 58
  reset = 1;
  x2 = (n_RNTI<<15) + (q<<14) + Nid;

  for (int i=0; i<size; i++) {
59 60
    b_idx = i&0x1f;
    if (b_idx==0) {
61 62
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
63 64
      if (i)
        out++;
65
    }
66
    *out ^= (((in[i])&1) ^ ((s>>b_idx)&1))<<b_idx;
Guy De Souza's avatar
Guy De Souza committed
67
    //printf("i %d b_idx %d in %d s 0x%08x out 0x%08x\n", i, b_idx, in[i], s, *out);
68
  }
Rakesh's avatar
Rakesh committed
69
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PDSCH_CODEWORD_SCRAMBLING, 0);
70 71
}

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void nr_pdsch_codeword_scrambling_optim(uint8_t *in,
					uint32_t size,
					uint8_t q,
					uint32_t Nid,
					uint32_t n_RNTI,
					uint32_t* out) {
  
  uint32_t x1, x2, s=0,in32;

  x2 = (n_RNTI<<15) + (q<<14) + Nid;

  s=lte_gold_generic(&x1, &x2, 1);

#if defined(__AVX2__)
  for (int i=0; i<((size>>5)+((size&0x1f) > 0 ? 1 : 0)); i++) {
    in32=_mm256_movemask_epi8(_mm256_slli_epi16(((__m256i*)in)[i],7));
    out[i]=(in32^s);
89
    //printf("in[%d] %x => %x\n",i,in32,out[i]);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    s=lte_gold_generic(&x1, &x2, 0);
  }
#elif defined(__SSE4__)
  _m128i *in128;
  for (int i=0; i<((size>>5)+((size&0x1f) > 0 ? 1 : 0)); i++) {
    in128=&((__m128i*)in)[i<<1];
    ((uint16_t*)&in32)[0] = _mm128_movemask_epi8(_mm256_slli_epi16(in128[0],7));
    ((uint16_t*)&in32)[1] = _mm128_movemask_epi8(_mm256_slli_epi16(in128[1],7));
    out[i]=(in32^s);
    s=lte_gold_generic(&x1, &x2, 0);
  }
  //#elsif defined(__arm__) || defined(__aarch64)
  
#else 
  nr_pdsch_codeword_scrambling(in,
			       size,
			       q,
			       Nid,
			       n_RNTI,
			       out);
#endif
}

Guy De Souza's avatar
Guy De Souza committed
113

114 115 116
uint8_t nr_generate_pdsch(PHY_VARS_gNB *gNB,
			  int frame,
			  int slot) {
Guy De Souza's avatar
Guy De Souza committed
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  NR_gNB_DLSCH_t *dlsch;
  uint32_t ***pdsch_dmrs = gNB->nr_gold_pdsch_dmrs[slot];
  int32_t** txdataF = gNB->common_vars.txdataF;
  int16_t amp = AMP;
  int xOverhead = 0;
  NR_DL_FRAME_PARMS *frame_parms = &gNB->frame_parms;
  time_stats_t *dlsch_encoding_stats=&gNB->dlsch_encoding_stats;
  time_stats_t *dlsch_scrambling_stats=&gNB->dlsch_scrambling_stats;
  time_stats_t *dlsch_modulation_stats=&gNB->dlsch_modulation_stats;
  time_stats_t *tinput=&gNB->tinput;
  time_stats_t *tprep=&gNB->tprep;
  time_stats_t *tparity=&gNB->tparity;
  time_stats_t *toutput=&gNB->toutput;
  time_stats_t *dlsch_rate_matching_stats=&gNB->dlsch_rate_matching_stats;
  time_stats_t *dlsch_interleaving_stats=&gNB->dlsch_interleaving_stats;
  time_stats_t *dlsch_segmentation_stats=&gNB->dlsch_segmentation_stats;
134

135
  for (int dlsch_id=0;dlsch_id<gNB->number_of_nr_dlsch_max;dlsch_id++) {
136 137
    dlsch = gNB->dlsch[dlsch_id][0];
    if (dlsch->slot_tx[slot] == 0) continue;
138

139
    NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
140 141 142 143
    nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
    uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
    int16_t **mod_symbs = (int16_t**)dlsch->mod_symbs;
    int16_t **tx_layers = (int16_t**)dlsch->txdataF;
144
    int16_t **txdataF_precoding = (int16_t**)dlsch->txdataF_precoding;
145
    int8_t Wf[2], Wt[2], l0, l_prime, l_overline, delta;
146 147 148 149 150 151 152 153 154
    uint8_t dmrs_Type = rel15->dmrsConfigType;
    int nb_re_dmrs;
    uint16_t n_dmrs;
    if (rel15->dmrsConfigType==NFAPI_NR_DMRS_TYPE1) {
      nb_re_dmrs = 6*rel15->numDmrsCdmGrpsNoData;
    }
    else {
      nb_re_dmrs = 4*rel15->numDmrsCdmGrpsNoData;
    }
155
    n_dmrs = (rel15->BWPStart+rel15->rbStart+rel15->rbSize)*nb_re_dmrs;
156

157 158
    uint16_t dmrs_symbol_map = rel15->dlDmrsSymbPos;//single DMRS: 010000100 Double DMRS 110001100
    uint8_t dmrs_len = get_num_dmrs(rel15->dlDmrsSymbPos);
159
    uint16_t nb_re = ((12*rel15->NrOfSymbols)-nb_re_dmrs*dmrs_len-xOverhead)*rel15->rbSize*rel15->nrOfLayers;
160 161
    uint8_t Qm = rel15->qamModOrder[0];
    uint32_t encoded_length = nb_re*Qm;
162
    int16_t mod_dmrs[n_dmrs<<1] __attribute__ ((aligned(16)));
163 164 165 166 167 168 169 170

    /* PTRS */
    uint16_t beta_ptrs = 1;
    uint8_t ptrs_symbol = 0;
    uint16_t dlPtrsSymPos = 0;
    uint16_t n_ptrs = 0;
    uint16_t ptrs_idx = 0;
    uint8_t is_ptrs_re = 0;
171
    if(rel15->pduBitmap & 0x1) {
172 173 174 175 176 177 178 179
      set_ptrs_symb_idx(&dlPtrsSymPos,
                          rel15->NrOfSymbols,
                          rel15->StartSymbolIndex,
                          1<<rel15->PTRSTimeDensity,
                          rel15->dlDmrsSymbPos);
      n_ptrs = (rel15->rbSize + rel15->PTRSFreqDensity - 1)/rel15->PTRSFreqDensity;
    }
    int16_t mod_ptrs[n_ptrs<<1] __attribute__ ((aligned(16)));
180

181 182 183
    /// CRC, coding, interleaving and rate matching
    AssertFatal(harq->pdu!=NULL,"harq->pdu is null\n");
    start_meas(dlsch_encoding_stats);
184 185
    nr_dlsch_encoding(gNB,
		      harq->pdu, frame, slot, dlsch, frame_parms,tinput,tprep,tparity,toutput,
186 187 188 189
		      dlsch_rate_matching_stats,
		      dlsch_interleaving_stats,
		      dlsch_segmentation_stats);
    stop_meas(dlsch_encoding_stats);
Guy De Souza's avatar
Guy De Souza committed
190
#ifdef DEBUG_DLSCH
191 192 193 194 195 196 197 198 199 200 201 202
    printf("PDSCH encoding:\nPayload:\n");
    for (int i=0; i<harq->B>>7; i++) {
      for (int j=0; j<16; j++)
	printf("0x%02x\t", harq->pdu[(i<<4)+j]);
      printf("\n");
    }
    printf("\nEncoded payload:\n");
    for (int i=0; i<encoded_length>>3; i++) {
      for (int j=0; j<8; j++)
	printf("%d", harq->f[(i<<3)+j]);
      printf("\t");
    }
Raymond Knopp's avatar
Raymond Knopp committed
203
    printf("\n");
Guy De Souza's avatar
Guy De Souza committed
204
#endif
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    
    
    
    /// scrambling
    start_meas(dlsch_scrambling_stats);
    for (int q=0; q<rel15->NrOfCodewords; q++)
      memset((void*)scrambled_output[q], 0, (encoded_length>>5)*sizeof(uint32_t));
    for (int q=0; q<rel15->NrOfCodewords; q++)
      nr_pdsch_codeword_scrambling_optim(harq->f,
					 encoded_length,
					 q,
					 rel15->dlDmrsScramblingId,
					 rel15->rnti,
					 scrambled_output[q]);
    
    stop_meas(dlsch_scrambling_stats);
Guy De Souza's avatar
Guy De Souza committed
221
#ifdef DEBUG_DLSCH
222 223 224 225 226
    printf("PDSCH scrambling:\n");
    for (int i=0; i<encoded_length>>8; i++) {
      for (int j=0; j<8; j++)
	printf("0x%08x\t", scrambled_output[0][(i<<3)+j]);
      printf("\n");
Raymond Knopp's avatar
Raymond Knopp committed
227
    }
Guy De Souza's avatar
Guy De Souza committed
228
#endif
229 230 231 232 233 234 235 236 237 238 239
    
    /// Modulation
    start_meas(dlsch_modulation_stats);
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PDSCH_MODULATION, 1);
    for (int q=0; q<rel15->NrOfCodewords; q++)
      nr_modulation(scrambled_output[q],
		    encoded_length,
		    Qm,
		    mod_symbs[q]);
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_gNB_PDSCH_MODULATION, 0);
    stop_meas(dlsch_modulation_stats);
Guy De Souza's avatar
Guy De Souza committed
240
#ifdef DEBUG_DLSCH
241 242
    printf("PDSCH Modulation: Qm %d(%d)\n", Qm, nb_re);
    for (int i=0; i<nb_re>>3; i++) {
Raymond Knopp's avatar
Raymond Knopp committed
243
      for (int j=0; j<8; j++) {
244
	printf("%d %d\t", mod_symbs[0][((i<<3)+j)<<1], mod_symbs[0][(((i<<3)+j)<<1)+1]);
Raymond Knopp's avatar
Raymond Knopp committed
245 246
      }
      printf("\n");
Guy De Souza's avatar
Guy De Souza committed
247 248
    }
#endif
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    
    
    /// Layer mapping
    nr_layer_mapping(mod_symbs,
		     rel15->nrOfLayers,
		     nb_re,
		     tx_layers);
#ifdef DEBUG_DLSCH
    printf("Layer mapping (%d layers):\n", rel15->nrOfLayers);
    for (int l=0; l<rel15->nrOfLayers; l++)
      for (int i=0; i<(nb_re/rel15->nrOfLayers)>>3; i++) {
	printf("layer %d, Re %d..%d : ",l,i<<3,(i<<3)+7);
	for (int j=0; j<8; j++) {
	  printf("l%d %d\t", tx_layers[l][((i<<3)+j)<<1], tx_layers[l][(((i<<3)+j)<<1)+1]);
	}
	printf("\n");
      }
#endif
267

268 269 270
    /// Resource mapping
    
    // Non interleaved VRB to PRB mapping
271
    uint16_t start_sc = frame_parms->first_carrier_offset + (rel15->rbStart+rel15->BWPStart)*NR_NB_SC_PER_RB;
272 273
    if (start_sc >= frame_parms->ofdm_symbol_size)
      start_sc -= frame_parms->ofdm_symbol_size;
274 275 276

    int txdataF_offset = (slot%2)*frame_parms->samples_per_slot_wCP;

Guy De Souza's avatar
Guy De Souza committed
277
#ifdef DEBUG_DLSCH_MAPPING
278 279
    printf("PDSCH resource mapping started (start SC %d\tstart symbol %d\tN_PRB %d\tnb_re %d,nb_layers %d)\n",
	   start_sc, rel15->StartSymbolIndex, rel15->rbSize, nb_re,rel15->nrOfLayers);
Guy De Souza's avatar
Guy De Souza committed
280
#endif
281 282 283 284 285 286
    for (int ap=0; ap<rel15->nrOfLayers; ap++) {
      
      // DMRS params for this ap
      get_Wt(Wt, ap, dmrs_Type);
      get_Wf(Wf, ap, dmrs_Type);
      delta = get_delta(ap, dmrs_Type);
287 288
      l_prime = 0; // single symbol ap 0
      l0 = get_l0(rel15->dlDmrsSymbPos);
289 290
      l_overline = l0;

Guy De Souza's avatar
Guy De Souza committed
291
#ifdef DEBUG_DLSCH_MAPPING
292
      uint8_t dmrs_symbol = l0+l_prime;
293
      printf("DMRS Type %d params for ap %d: Wt %d %d \t Wf %d %d \t delta %d \t l_prime %d \t l0 %d\tDMRS symbol %d\n",
294
	     1+dmrs_Type,ap, Wt[0], Wt[1], Wf[0], Wf[1], delta, l_prime, l0, dmrs_symbol);
Guy De Souza's avatar
Guy De Souza committed
295
#endif
296

297
      uint16_t m=0, dmrs_idx=0;
298

299
      // Loop Over OFDM symbols:
300
      for (int l=rel15->StartSymbolIndex; l<rel15->StartSymbolIndex+rel15->NrOfSymbols; l++) {
301 302 303
        /// DMRS QPSK modulation
        uint8_t k_prime=0;
        uint16_t n=0;
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

        if ((dmrs_symbol_map & (1 << l))){ // DMRS time occasion
          // The reference point for is subcarrier 0 of the lowest-numbered resource block in CORESET 0 if the corresponding
          // PDCCH is associated with CORESET 0 and Type0-PDCCH common search space and is addressed to SI-RNTI
          // 3GPP TS 38.211 V15.8.0 Section 7.4.1.1.2 Mapping to physical resources
          if (rel15->rnti==SI_RNTI) {
            if (dmrs_Type==NFAPI_NR_DMRS_TYPE1) {
              dmrs_idx = rel15->rbStart*6;
            } else {
              dmrs_idx = rel15->rbStart*4;
            }
          } else {
            if (dmrs_Type == NFAPI_NR_DMRS_TYPE1) {
              dmrs_idx = (rel15->rbStart+rel15->BWPStart)*6;
            } else {
              dmrs_idx = (rel15->rbStart+rel15->BWPStart)*4;
            }
          }
322
        }
323

324 325 326 327 328 329 330
        // Update l_prime in the case of double DMRS config
        if ((dmrs_symbol_map & (1 << l))){ //DMRS time occasion
          if (l==(l_overline+1)) //take into account the double DMRS symbols
            l_prime = 1;
          else if (l>(l_overline+1)) {//new DMRS pair
            l_overline = l;
            l_prime = 0;
331 332
          }
        }
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
        /// DMRS QPSK modulation
        if (rel15->dlDmrsSymbPos & (1 << l)) {
          nr_modulation(pdsch_dmrs[l][0], n_dmrs*2, DMRS_MOD_ORDER, mod_dmrs); // currently only codeword 0 is modulated. Qm = 2 as DMRS is QPSK modulated

#ifdef DEBUG_DLSCH
          printf("DMRS modulation (symbol %d, %d symbols, type %d):\n", l, n_dmrs, dmrs_Type);
          for (int i=0; i<n_dmrs>>4; i++) {
            for (int j=0; j<8; j++) {
              printf("%d %d\t", mod_dmrs[((i<<3)+j)<<1], mod_dmrs[(((i<<3)+j)<<1)+1]);
            }
            printf("\n");
          }
#endif
        }

349 350 351
        /* calculate if current symbol is PTRS symbols */
        ptrs_idx = 0;

352
        if(rel15->pduBitmap & 0x1) {
353
          ptrs_symbol = is_ptrs_symbol(l,dlPtrsSymPos);
354
          if(ptrs_symbol) {
355 356 357 358
            /* PTRS QPSK Modulation for each OFDM symbol in a slot */
            nr_modulation(pdsch_dmrs[l][0], (n_ptrs<<1), DMRS_MOD_ORDER, mod_ptrs);
          }
        }
359
        uint16_t k = start_sc;
360
        // Loop Over SCs:
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        for (int i=0; i<rel15->rbSize*NR_NB_SC_PER_RB; i++) {
          /* check if cuurent RE is PTRS RE*/
          is_ptrs_re=0;
          /* check for PTRS symbol and set flag for PTRS RE */
          if(ptrs_symbol){
            is_ptrs_re = is_ptrs_subcarrier(k,
                                            rel15->rnti,
                                            ap,
                                            rel15->dmrsConfigType,
                                            rel15->PTRSFreqDensity,
                                            rel15->rbSize,
                                            rel15->PTRSReOffset,
                                            start_sc,
                                            frame_parms->ofdm_symbol_size);
          }
376

377 378
          /* Map DMRS Symbol */
          if ( ( dmrs_symbol_map & (1 << l) ) && (k == ((start_sc+get_dmrs_freq_idx(n, k_prime, delta, dmrs_Type))%(frame_parms->ofdm_symbol_size)))) {
379 380
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) +     (2*txdataF_offset)] = (Wt[l_prime]*Wf[k_prime]*amp*mod_dmrs[dmrs_idx<<1]) >> 15;
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)] = (Wt[l_prime]*Wf[k_prime]*amp*mod_dmrs[(dmrs_idx<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
381
#ifdef DEBUG_DLSCH_MAPPING
382
            printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
383 384
                   dmrs_idx, l, k, k_prime, n, txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)],
                   txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)]);
Guy De Souza's avatar
Guy De Souza committed
385
#endif
386 387 388 389
            dmrs_idx++;
            k_prime++;
            k_prime&=1;
            n+=(k_prime)?0:1;
390 391 392
          }
          /* Map PTRS Symbol */
          else if(is_ptrs_re){
393 394
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) +     (2*txdataF_offset)] = (beta_ptrs*amp*mod_ptrs[ptrs_idx<<1]) >> 15;
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)] = (beta_ptrs*amp*mod_ptrs[(ptrs_idx<<1) + 1])>> 15;
395 396 397 398 399 400 401 402
#ifdef DEBUG_DLSCH_MAPPING
            printf("ptrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
                   ptrs_idx, l, k, k_prime, n, ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)],
                   ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)]);
#endif
            ptrs_idx++;
          }
          /* Map DATA Symbol */
403 404 405
          else if( (!(dmrs_symbol_map & (1 << l))) || allowed_xlsch_re_in_dmrs_symbol(k,start_sc,frame_parms->ofdm_symbol_size,rel15->numDmrsCdmGrpsNoData,dmrs_Type)) {
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) +     (2*txdataF_offset)] = (amp * tx_layers[ap][m<<1]) >> 15;
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)] = (amp * tx_layers[ap][(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
406
#ifdef DEBUG_DLSCH_MAPPING
407 408 409
            printf("m %d\t l %d \t k %d \t txdataF: %d %d\n",
                   m, l, k, txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)],
                   txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)]);
Guy De Souza's avatar
Guy De Souza committed
410
#endif
411 412 413 414 415 416
            m++;
          }
          /* mute RE */
          else {
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) +     (2*txdataF_offset)] = 0;
            txdataF_precoding[ap][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)] = 0;
417 418 419 420
          }
          if (++k >= frame_parms->ofdm_symbol_size)
            k -= frame_parms->ofdm_symbol_size;
        } //RE loop
421 422
      } // symbol loop
    }// layer loop
423

424 425
    ///Layer Precoding and Antenna port mapping
    // tx_layers 1-8 are mapped on antenna ports 1000-1007
426 427 428
    // The precoding info is supported by nfapi such as num_prgs, prg_size, prgs_list and pm_idx
    // The same precoding matrix is applied on prg_size RBs, Thus
    //        pmi = prgs_list[rbidx/prg_size].pm_idx, rbidx =0,...,rbSize-1
sfn's avatar
sfn committed
429 430 431
    // The Precoding matrix:
    // The Codebook Type I and Type II are not supported yet.
    // We adopt the precoding matrices of PUSCH for 4 layers.
432
    for (int ap=0; ap<frame_parms->nb_antennas_tx; ap++) {
433

434 435
      for (int l=rel15->StartSymbolIndex; l<rel15->StartSymbolIndex+rel15->NrOfSymbols; l++) {
        uint16_t k = start_sc;
436

437 438 439
        for (int rb=0; rb<rel15->rbSize; rb++) {
          //get pmi info
          uint8_t pmi;
440 441
          if (rel15->precodingAndBeamforming.prg_size > 0)
            pmi = rel15->precodingAndBeamforming.prgs_list[(int)rb/rel15->precodingAndBeamforming.prg_size].pm_idx;
442 443
          else
            pmi = 0;//no precoding
444

445 446 447 448 449 450
          if (pmi == 0) {//unitary Precoding
            if(ap<rel15->nrOfLayers)
              memcpy((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset + k],
                     (void*)&txdataF_precoding[ap][2*(l*frame_parms->ofdm_symbol_size + txdataF_offset+ k)],
                     NR_NB_SC_PER_RB*sizeof(int32_t));
            else
sfn's avatar
sfn committed
451
              memset((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset + k],
452 453 454 455 456 457 458 459
                     0,
                     NR_NB_SC_PER_RB*sizeof(int32_t));
            k += NR_NB_SC_PER_RB;
            if (k >= frame_parms->ofdm_symbol_size) {
              k -= frame_parms->ofdm_symbol_size;
            }
          }
          else {
460 461 462 463 464
            //get the precoding matrix weights:
            char *W_prec;
            switch (frame_parms->nb_antennas_tx) {
              case 1://1 antenna port
                W_prec = nr_W_1l_2p[pmi][ap];
sfn's avatar
sfn committed
465
                break;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
              case 2://2 antenna ports
                if (rel15->nrOfLayers == 1)//1 layer
                  W_prec = nr_W_1l_2p[pmi][ap];
                else//2 layers
                  W_prec = nr_W_2l_2p[pmi][ap];
                break;
              case 4://4 antenna ports
                if (rel15->nrOfLayers == 1)//1 layer
                  W_prec = nr_W_1l_4p[pmi][ap];
                else if (rel15->nrOfLayers == 2)//2 layers
                  W_prec = nr_W_2l_4p[pmi][ap];
                else if (rel15->nrOfLayers == 3)//3 layers
                  W_prec = nr_W_3l_4p[pmi][ap];
                else//4 layers
                  W_prec = nr_W_4l_4p[pmi][ap];
                break;
              default:
                LOG_D(PHY,"Precoding 1,2, or 4 antenna ports are currently supported\n");
                W_prec = nr_W_1l_2p[pmi][ap];
                break;
sfn's avatar
sfn committed
486
            }
487 488 489 490 491
            for (int i=0; i<NR_NB_SC_PER_RB; i++) {
              int32_t re_offset = l*frame_parms->ofdm_symbol_size + k;
              int32_t precodatatx_F = nr_layer_precoder(txdataF_precoding, W_prec, rel15->nrOfLayers, re_offset+txdataF_offset);
              ((int16_t*)txdataF[ap])[(re_offset<<1) + (2*txdataF_offset)] = ((int16_t *) &precodatatx_F)[0];
              ((int16_t*)txdataF[ap])[(re_offset<<1) + 1 + (2*txdataF_offset)] = ((int16_t *) &precodatatx_F)[1];
492
#ifdef DEBUG_DLSCH_MAPPING
493 494 495
              printf("antenna %d\t l %d \t k %d \t txdataF: %d %d\n",
                     ap, l, k, ((int16_t*)txdataF[ap])[(re_offset<<1) + (2*txdataF_offset)],
                     ((int16_t*)txdataF[ap])[(re_offset<<1) + 1 + (2*txdataF_offset)]);
496
#endif
497 498
              if (++k >= frame_parms->ofdm_symbol_size) {
                k -= frame_parms->ofdm_symbol_size;
499 500
              }
            }
501 502 503 504
          }
        } //RB loop
      } // symbol loop
    }// port loop
505

506
    dlsch->slot_tx[slot]=0;
507 508 509 510 511 512 513 514 515

    // TODO: handle precoding
    // this maps the layers onto antenna ports
    
    // handle beamforming ID
    // each antenna port is assigned a beam_index
    // since PHY can only handle BF on slot basis we set the whole slot

    // first check if this slot has not already been allocated to another beam
Florian Kaltenberger's avatar
Florian Kaltenberger committed
516
    if (gNB->common_vars.beam_id[0][slot*frame_parms->symbols_per_slot]==255) {
517 518 519 520
      for (int j=0;j<frame_parms->symbols_per_slot;j++) 
	gNB->common_vars.beam_id[0][slot*frame_parms->symbols_per_slot+j] = rel15->precodingAndBeamforming.prgs_list[0].dig_bf_interface_list[0].beam_idx;
    }
    else {
rmagueta's avatar
rmagueta committed
521
      LOG_D(PHY,"beam index for PDSCH allocation already taken\n");
522
    }
523
  }// dlsch loop
524 525

  
526 527
  return 0;
}
528 529 530

void dump_pdsch_stats(PHY_VARS_gNB *gNB) {

531 532
  for (int i=0;i<NUMBER_OF_NR_SCH_STATS_MAX;i++)
    if (gNB->dlsch_stats[i].rnti > 0)
533
      LOG_D(PHY,"DLSCH RNTI %x: current_Qm %d, current_RI %d, total_bytes TX %d\n",
534 535 536 537
	    gNB->dlsch_stats[i].rnti,
	    gNB->dlsch_stats[i].current_Qm,
	    gNB->dlsch_stats[i].current_RI,
	    gNB->dlsch_stats[i].total_bytes_tx);
538 539 540 541 542

}

void clear_pdsch_stats(PHY_VARS_gNB *gNB) {

543
  for (int i=0;i<gNB->number_of_nr_dlsch_max;i++)
544
    memset((void*)&gNB->dlsch_stats[i],0,sizeof(gNB->dlsch_stats[i]));
545
}