nr_dlsch.c 26.5 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 26 27 28 29 30 31
/*! \file nr_dlsch.c
 * \brief Top-level routines for transmission of the PDSCH 38211 v 15.2.0
 * \author Guy De Souza
 * \date 2018
 * \version 0.1
 * \company Eurecom
 * \email: desouza@eurecom.fr
 * \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 "common/utils/nr/nr_common.h"
41
#include "executables/softmodem-common.h"
Guy De Souza's avatar
Guy De Souza committed
42

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

Guy De Souza's avatar
Guy De Souza committed
46
void nr_pdsch_codeword_scrambling(uint8_t *in,
47 48 49 50
                                  uint32_t size,
                                  uint8_t q,
                                  uint32_t Nid,
                                  uint32_t n_RNTI,
51 52 53
                                  uint32_t* out)
{
  nr_codeword_scrambling(in, size, q, Nid, n_RNTI, out);
54 55
}

56 57 58
void nr_generate_pdsch(processingData_L1tx_t *msgTx,
                       int frame,
                       int slot) {
Guy De Souza's avatar
Guy De Souza committed
59

60
  PHY_VARS_gNB *gNB = msgTx->gNB;
61
  NR_gNB_DLSCH_t *dlsch;
62
  c16_t** txdataF = gNB->common_vars.txdataF;
63
  int16_t amp = gNB->TX_AMP;
64 65 66 67 68 69 70 71 72 73 74 75
  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;
76

77
  for (int dlsch_id=0; dlsch_id<msgTx->num_pdsch_slot; dlsch_id++) {
francescomani's avatar
francescomani committed
78
    dlsch = &msgTx->dlsch[dlsch_id][0];
79

80
    NR_DL_gNB_HARQ_t *harq = &dlsch->harq_process;
81 82 83
    nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
    int16_t **mod_symbs = (int16_t**)dlsch->mod_symbs;
    int16_t **tx_layers = (int16_t**)dlsch->txdataF;
84
    int8_t Wf[2], Wt[2], l0, l_prime, l_overline, delta;
85 86 87
    uint8_t dmrs_Type = rel15->dmrsConfigType;
    int nb_re_dmrs;
    uint16_t n_dmrs;
88 89
    LOG_D(PHY,"pdsch: BWPStart %d, BWPSize %d, rbStart %d, rbsize %d\n",
          rel15->BWPStart,rel15->BWPSize,rel15->rbStart,rel15->rbSize);
90 91 92 93 94 95
    if (rel15->dmrsConfigType==NFAPI_NR_DMRS_TYPE1) {
      nb_re_dmrs = 6*rel15->numDmrsCdmGrpsNoData;
    }
    else {
      nb_re_dmrs = 4*rel15->numDmrsCdmGrpsNoData;
    }
96
    n_dmrs = (rel15->BWPStart+rel15->rbStart+rel15->rbSize)*nb_re_dmrs;
97

98 99 100 101 102 103
    if(rel15->dlDmrsScramblingId != gNB->pdsch_gold_init[rel15->SCID])  {
      gNB->pdsch_gold_init[rel15->SCID] = rel15->dlDmrsScramblingId;
      nr_init_pdsch_dmrs(gNB, rel15->SCID, rel15->dlDmrsScramblingId);
    }

    uint32_t ***pdsch_dmrs = gNB->nr_gold_pdsch_dmrs[slot];
104 105
    uint16_t dmrs_symbol_map = rel15->dlDmrsSymbPos;//single DMRS: 010000100 Double DMRS 110001100
    uint8_t dmrs_len = get_num_dmrs(rel15->dlDmrsSymbPos);
106
    uint32_t nb_re = ((12*rel15->NrOfSymbols)-nb_re_dmrs*dmrs_len-xOverhead)*rel15->rbSize*rel15->nrOfLayers;
107 108
    uint8_t Qm = rel15->qamModOrder[0];
    uint32_t encoded_length = nb_re*Qm;
109
    int16_t mod_dmrs[n_dmrs<<1] __attribute__ ((aligned(16)));
110 111 112 113 114 115 116 117

    /* 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;
118
    if(rel15->pduBitmap & 0x1) {
119 120 121 122 123 124 125
      set_ptrs_symb_idx(&dlPtrsSymPos,
                          rel15->NrOfSymbols,
                          rel15->StartSymbolIndex,
                          1<<rel15->PTRSTimeDensity,
                          rel15->dlDmrsSymbPos);
      n_ptrs = (rel15->rbSize + rel15->PTRSFreqDensity - 1)/rel15->PTRSFreqDensity;
    }
126

127 128
    /// CRC, coding, interleaving and rate matching
    AssertFatal(harq->pdu!=NULL,"harq->pdu is null\n");
129 130
    unsigned char output[rel15->rbSize * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * Qm * rel15->nrOfLayers] __attribute__((aligned(32)));
    bzero(output,rel15->rbSize * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * Qm * rel15->nrOfLayers);
131
    start_meas(dlsch_encoding_stats);
Laurent Thomas's avatar
Laurent Thomas committed
132

133
    if (nr_dlsch_encoding(gNB,
134
                          frame, slot, harq, frame_parms,output,tinput,tprep,tparity,toutput,
135 136 137 138
                          dlsch_rate_matching_stats,
                          dlsch_interleaving_stats,
                          dlsch_segmentation_stats) == -1)
      return;
139
    stop_meas(dlsch_encoding_stats);
Guy De Souza's avatar
Guy De Souza committed
140
#ifdef DEBUG_DLSCH
141 142 143 144 145 146 147 148 149
    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++)
150
	printf("%d", output[(i<<3)+j]);
151 152
      printf("\t");
    }
Raymond Knopp's avatar
Raymond Knopp committed
153
    printf("\n");
Guy De Souza's avatar
Guy De Souza committed
154
#endif
155

156 157 158
    if (IS_SOFTMODEM_DLSIM)
      memcpy(harq->f, output, encoded_length);

159
    for (int q=0; q<rel15->NrOfCodewords; q++) {
160 161 162 163 164
      /// scrambling
      start_meas(dlsch_scrambling_stats);
      uint32_t scrambled_output[(encoded_length>>5)+4]; // modulator acces by 4 bytes in some cases
      memset(scrambled_output, 0, sizeof(scrambled_output));
      if ( encoded_length > rel15->rbSize * NR_SYMBOLS_PER_SLOT * NR_NB_SC_PER_RB * Qm * rel15->nrOfLayers) abort();
165 166 167 168 169
      nr_pdsch_codeword_scrambling(output,
                                   encoded_length,
                                   q,
                                   rel15->dataScramblingId,
                                   rel15->rnti,
170
                                   scrambled_output);
171

Guy De Souza's avatar
Guy De Souza committed
172
#ifdef DEBUG_DLSCH
173 174 175 176 177 178
      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[(i<<3)+j]);
        printf("\n");
      }
Guy De Souza's avatar
Guy De Souza committed
179
#endif
180

181 182 183 184 185 186 187 188 189
      stop_meas(dlsch_scrambling_stats);
      /// Modulation
      start_meas(dlsch_modulation_stats);
      nr_modulation(scrambled_output,
                    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
190
#ifdef DEBUG_DLSCH
191
      printf("PDSCH Modulation: Qm %d(%u)\n", Qm, nb_re);
192 193 194 195 196
      for (int i=0; i<nb_re>>3; i++) {
        for (int j=0; j<8; j++) {
          printf("%d %d\t", mod_symbs[0][((i<<3)+j)<<1], mod_symbs[0][(((i<<3)+j)<<1)+1]);
        }
        printf("\n");
Raymond Knopp's avatar
Raymond Knopp committed
197
      }
Guy De Souza's avatar
Guy De Souza committed
198
#endif
199
    }
200
    
201
    start_meas(&gNB->dlsch_layer_mapping_stats); 
202 203
    /// Layer mapping
    nr_layer_mapping(mod_symbs,
204 205 206
                     rel15->nrOfLayers,
                     nb_re,
                     tx_layers);
207 208 209 210
#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++) {
211 212 213 214 215
        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");
216 217
      }
#endif
218

219
    stop_meas(&gNB->dlsch_layer_mapping_stats); 
rmagueta's avatar
rmagueta committed
220

221 222 223
    /// Resource mapping
    
    // Non interleaved VRB to PRB mapping
224
    uint16_t start_sc = frame_parms->first_carrier_offset + (rel15->rbStart+rel15->BWPStart)*NR_NB_SC_PER_RB;
225 226
    if (start_sc >= frame_parms->ofdm_symbol_size)
      start_sc -= frame_parms->ofdm_symbol_size;
227

228
    int txdataF_offset = slot*frame_parms->samples_per_slot_wCP;
francescomani's avatar
francescomani committed
229 230
    int16_t **txdataF_precoding = (int16_t **)malloc16(rel15->nrOfLayers*sizeof(int16_t *));
    for (int layer = 0; layer<rel15->nrOfLayers; layer++)
231
      txdataF_precoding[layer] = (int16_t *)malloc16(2*14*frame_parms->ofdm_symbol_size*sizeof(int16_t));
232

Guy De Souza's avatar
Guy De Souza committed
233
#ifdef DEBUG_DLSCH_MAPPING
234
    printf("PDSCH resource mapping started (start SC %d\tstart symbol %d\tN_PRB %d\tnb_re %u,nb_layers %d)\n",
235
           start_sc, rel15->StartSymbolIndex, rel15->rbSize, nb_re,rel15->nrOfLayers);
Guy De Souza's avatar
Guy De Souza committed
236
#endif
237

238
    start_meas(&gNB->dlsch_resource_mapping_stats);
francescomani's avatar
francescomani committed
239
    for (int nl=0; nl<rel15->nrOfLayers; nl++) {
240

241
      int dmrs_port = get_dmrs_port(nl,rel15->dmrsPorts);
rmagueta's avatar
rmagueta committed
242

243
      // DMRS params for this dmrs port
244 245 246
      get_Wt(Wt, dmrs_port, dmrs_Type);
      get_Wf(Wf, dmrs_port, dmrs_Type);
      delta = get_delta(dmrs_port, dmrs_Type);
francescomani's avatar
francescomani committed
247
      l_prime = 0; // single symbol nl 0
248
      l0 = get_l0(rel15->dlDmrsSymbPos);
249 250
      l_overline = l0;

Guy De Souza's avatar
Guy De Souza committed
251
#ifdef DEBUG_DLSCH_MAPPING
252
      uint8_t dmrs_symbol = l0+l_prime;
francescomani's avatar
francescomani committed
253 254
      printf("DMRS Type %d params for nl %d: Wt %d %d \t Wf %d %d \t delta %d \t l_prime %d \t l0 %d\tDMRS symbol %d\n",
	     1+dmrs_Type,nl, Wt[0], Wt[1], Wf[0], Wf[1], delta, l_prime, l0, dmrs_symbol);
Guy De Souza's avatar
Guy De Souza committed
255
#endif
256

257
      uint32_t m=0, dmrs_idx=0;
258

259
      // Loop Over OFDM symbols:
260
      for (int l=rel15->StartSymbolIndex; l<rel15->StartSymbolIndex+rel15->NrOfSymbols; l++) {
261 262 263
        /// DMRS QPSK modulation
        uint8_t k_prime=0;
        uint16_t n=0;
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

        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;
            }
          }
282 283 284 285 286
          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;
287
          }
288
          /// DMRS QPSK modulation
289
          nr_modulation(pdsch_dmrs[l][rel15->SCID], n_dmrs*2, DMRS_MOD_ORDER, mod_dmrs); // Qm = 2 as DMRS is QPSK modulated
290 291 292 293 294 295 296 297 298 299 300 301

#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
        }

302 303
        /* calculate if current symbol is PTRS symbols */
        ptrs_idx = 0;
laurent's avatar
laurent committed
304
        int16_t *mod_ptrs = NULL;
305
        if(rel15->pduBitmap & 0x1) {
306
          ptrs_symbol = is_ptrs_symbol(l,dlPtrsSymPos);
307
          if(ptrs_symbol) {
308
            /* PTRS QPSK Modulation for each OFDM symbol in a slot */
309
            LOG_D(PHY,"Doing ptrs modulation for symbol %d, n_ptrs %d\n",l,n_ptrs);
laurent's avatar
laurent committed
310 311
            int16_t mod_ptrsBuf[n_ptrs<<1] __attribute__ ((aligned(16)));
            mod_ptrs =mod_ptrsBuf;
312
            nr_modulation(pdsch_dmrs[l][rel15->SCID], (n_ptrs<<1), DMRS_MOD_ORDER, mod_ptrs);
313 314
          }
        }
315
        uint16_t k = start_sc;
316 317 318 319 320 321
        if (ptrs_symbol || dmrs_symbol_map & (1 << l)) {

          // Loop Over SCs:
          for (int i=0; i<rel15->rbSize*NR_NB_SC_PER_RB; i++) {
            /* check if cuurent RE is PTRS RE*/
            is_ptrs_re = 0;
322 323
            /* check for PTRS symbol and set flag for PTRS RE */
            if(ptrs_symbol){
324 325
              is_ptrs_re = is_ptrs_subcarrier(k,
                                              rel15->rnti,
326
                                              nl,
327 328 329 330 331 332
                                              rel15->dmrsConfigType,
                                              rel15->PTRSFreqDensity,
                                              rel15->rbSize,
                                              rel15->PTRSReOffset,
                                              start_sc,
                                              frame_parms->ofdm_symbol_size);
333
            }
334 335 336
            /* 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)))) {
337 338
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)     ] = (Wt[l_prime]*Wf[k_prime]*amp*mod_dmrs[dmrs_idx<<1]) >> 15;
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 ] = (Wt[l_prime]*Wf[k_prime]*amp*mod_dmrs[(dmrs_idx<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
339
#ifdef DEBUG_DLSCH_MAPPING
340
              printf("dmrs_idx %u\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
341 342
                     dmrs_idx, l, k, k_prime, n, txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)],
                     txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1]);
Guy De Souza's avatar
Guy De Souza committed
343
#endif
344 345 346 347 348
              dmrs_idx++;
              k_prime++;
              k_prime&=1;
              n+=(k_prime)?0:1;
            }
349
            /* Map PTRS Symbol */
350
            else if(is_ptrs_re){
351 352
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)    ] = (beta_ptrs*amp*mod_ptrs[ptrs_idx<<1]) >> 15;
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (beta_ptrs*amp*mod_ptrs[(ptrs_idx<<1) + 1])>> 15;
353
#ifdef DEBUG_DLSCH_MAPPING
Raymond Knopp's avatar
Raymond Knopp committed
354
              printf("ptrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d, mod_ptrs: %d %d\n",
355 356
                     ptrs_idx, l, k, k_prime, n, ((int16_t*)txdataF_precoding[nl])[((l*frame_parms->ofdm_symbol_size + k)<<1)],
                     ((int16_t*)txdataF_precoding[nl])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1],mod_ptrs[ptrs_idx<<1],mod_ptrs[(ptrs_idx<<1)+1]);
357
#endif
358 359
              ptrs_idx++;
            }
360
          /* Map DATA Symbol */
Raymond Knopp's avatar
Raymond Knopp committed
361
            else if( ptrs_symbol || allowed_xlsch_re_in_dmrs_symbol(k,start_sc,frame_parms->ofdm_symbol_size,rel15->numDmrsCdmGrpsNoData,dmrs_Type)) {
362 363
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)    ] = (amp * tx_layers[nl][m<<1]) >> 15;
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * tx_layers[nl][(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
364
#ifdef DEBUG_DLSCH_MAPPING
365
              printf("m %u\t l %d \t k %d \t txdataF: %d %d\n",
366 367
                     m, l, k, txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)],
                     txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1]);
Guy De Souza's avatar
Guy De Souza committed
368
#endif
369 370 371 372
              m++;
            }
            /* mute RE */
            else {
373 374
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)    ] = 0;
              txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = 0;
375 376 377 378 379 380 381 382 383 384 385 386
            }
            if (++k >= frame_parms->ofdm_symbol_size)
              k -= frame_parms->ofdm_symbol_size;
          } //RE loop
        }
        else { // no PTRS or DMRS in this symbol
          // Loop Over SCs:
          int upper_limit=rel15->rbSize*NR_NB_SC_PER_RB;
          int remaining_re = 0;
          if (start_sc + upper_limit > frame_parms->ofdm_symbol_size) {
            remaining_re = upper_limit + start_sc - frame_parms->ofdm_symbol_size;
            upper_limit = frame_parms->ofdm_symbol_size - start_sc;
387
          }
388 389
          // fix the alignment issues later, use 64-bit SIMD below instead of 128.
          if (0/*(frame_parms->N_RB_DL&1)==0*/) {
390
            simde__m128i *txF=(simde__m128i*)&txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size+start_sc)<<1)];
391

392 393
            simde__m128i *txl = (simde__m128i*)&tx_layers[nl][m<<1];
            simde__m128i amp128=simde_mm_set1_epi16(amp);
394
            for (int i=0; i<(upper_limit>>2); i++) {
395
              txF[i] = simde_mm_mulhrs_epi16(amp128,txl[i]);
396 397 398
            } //RE loop, first part
            m+=upper_limit;
            if (remaining_re > 0) {
399 400
               txF = (simde__m128i*)&txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size)<<1)];
               txl = (simde__m128i*)&tx_layers[nl][m<<1];
401
               for (int i=0; i<(remaining_re>>2); i++) {
402
                 txF[i] = simde_mm_mulhrs_epi16(amp128,txl[i]);
403 404
               }
            }
405
          }
406
          else {
407
            simde__m128i *txF = (simde__m128i *)&txdataF_precoding[nl][((l * frame_parms->ofdm_symbol_size + start_sc) << 1)];
laurent's avatar
laurent committed
408

409 410
            simde__m128i *txl = (simde__m128i *)&tx_layers[nl][m << 1];
            simde__m128i amp64 = simde_mm_set1_epi16(amp);
laurent's avatar
laurent committed
411 412
            int i;
            for (i = 0; i < (upper_limit >> 2); i++) {
413 414
              const simde__m128i txL = simde_mm_loadu_si128(txl + i);
              simde_mm_storeu_si128(txF + i, simde_mm_mulhrs_epi16(amp64, txL));
415
#ifdef DEBUG_DLSCH_MAPPING
416
              if ((i&1) > 0)
417 418 419
                  printf("m %u\t l %d \t k %d \t txdataF: %d %d\n",
                         m, l, start_sc+(i>>1), txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + start_sc+(i>>1))<<1)],
                         txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + start_sc+(i>>1))<<1) + 1]);
420
#endif
421 422
              /* handle this, mute RE */
              /*else {
423 424
                txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) ] = 0;
                txdataF_precoding[anl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = 0;
laurent's avatar
laurent committed
425 426 427 428 429 430 431 432 433 434
                }*/
            }
            if (i * 4 != upper_limit) {
              c16_t *txFc = (c16_t *)&txdataF_precoding[nl][((l * frame_parms->ofdm_symbol_size + start_sc) << 1)];
              c16_t *txlc = (c16_t *)&tx_layers[nl][m << 1];
              for (i = (upper_limit >> 2) << 2; i < upper_limit; i++) {
                txFc[i].r = ((txlc[i].r * amp) >> 14) + 1;
                txFc[i].i = ((txlc[i].i * amp) >> 14) + 1;
              }
            }
435 436
            m+=upper_limit;
            if (remaining_re > 0) {
437 438
              txF = (simde__m128i *)&txdataF_precoding[nl][((l * frame_parms->ofdm_symbol_size) << 1)];
              txl = (simde__m128i *)&tx_layers[nl][m << 1];
laurent's avatar
laurent committed
439 440
              int i;
              for (i = 0; i < (remaining_re >> 2); i++) {
441 442 443
                const simde__m128i txL = simde_mm_loadu_si128(txl + i);
                simde_mm_storeu_si128(txF + i, simde_mm_mulhrs_epi16(amp64, txL));

444
#ifdef DEBUG_DLSCH_MAPPING
445 446 447 448
                 if ((i&1) > 0)
                   printf("m %u\t l %d \t k %d \t txdataF: %d %d\n",
                          m, l, i>>1, txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + (i>>1))<<1) ],
                          txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + (i>>1))<<1) + 1]);
449
#endif
laurent's avatar
laurent committed
450 451 452 453 454
                /* handle this, mute RE */
                /*else {
                  txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1)    ] = 0;
                  txdataF_precoding[nl][((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = 0;
                  }*/
laurent's avatar
laurent committed
455
              } // RE loop, second part
laurent's avatar
laurent committed
456 457 458 459 460 461 462 463
              if (i * 4 != remaining_re) {
                c16_t *txFc = (c16_t *)&txdataF_precoding[nl][((l * frame_parms->ofdm_symbol_size) << 1)];
                c16_t *txlc = (c16_t *)&tx_layers[nl][m << 1];
                for (i = (remaining_re >> 2) << 2; i < remaining_re; i++) {
                  txFc[i].r = ((txlc[i].r * amp) >> 14) + 1;
                  txFc[i].i = ((txlc[i].i * amp) >> 14) + 1;
                }
              }
464 465 466 467
            } // 
            m+=remaining_re;
          } // N_RB_DL even
        } // no DMRS/PTRS in symbol  
468 469
      } // symbol loop
    }// layer loop
470
    stop_meas(&gNB->dlsch_resource_mapping_stats);
471

472 473
    ///Layer Precoding and Antenna port mapping
    // tx_layers 1-8 are mapped on antenna ports 1000-1007
474 475 476
    // 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
477
    // The Precoding matrix:
sfn's avatar
sfn committed
478
    // The Codebook Type I
479 480
    start_meas(&gNB->dlsch_precoding_stats);

481
    for (int ap=0; ap<frame_parms->nb_antennas_tx; ap++) {
482

483 484
      for (int l=rel15->StartSymbolIndex; l<rel15->StartSymbolIndex+rel15->NrOfSymbols; l++) {
        uint16_t k = start_sc;
485

486 487 488
        for (int rb=0; rb<rel15->rbSize; rb++) {
          //get pmi info
          uint8_t pmi;
489
          if (0 /*rel15->precodingAndBeamforming.prg_size > 0*/)
490
            pmi = rel15->precodingAndBeamforming.prgs_list[(int)rb/rel15->precodingAndBeamforming.prg_size].pm_idx;
491 492
          else
            pmi = 0;//no precoding
493

494
          if (pmi == 0) {//unitary Precoding
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
            if (k + NR_NB_SC_PER_RB <= frame_parms->ofdm_symbol_size) { // RB does not cross DC
              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 + k)],
                       NR_NB_SC_PER_RB*sizeof(int32_t));
              else
                memset((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset + k],
                       0,
                       NR_NB_SC_PER_RB*sizeof(int32_t));
            } else { // RB does cross DC
              int neg_length = frame_parms->ofdm_symbol_size - k;
              int pos_length = NR_NB_SC_PER_RB - neg_length;
              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 + k)],
                       neg_length*sizeof(int32_t));
                memcpy((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset],
                       (void*)&txdataF_precoding[ap][2*(l*frame_parms->ofdm_symbol_size)],
                       pos_length*sizeof(int32_t));
              } else {
                memset((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset + k],
                       0,
                       neg_length*sizeof(int32_t));
                memset((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset],
                       0,
                       pos_length*sizeof(int32_t));
              }
            }
523 524 525 526 527 528
            k += NR_NB_SC_PER_RB;
            if (k >= frame_parms->ofdm_symbol_size) {
              k -= frame_parms->ofdm_symbol_size;
            }
          }
          else {
529 530
            if(frame_parms->nb_antennas_tx==1){//no precoding matrix defined
              memcpy((void*)&txdataF[ap][l*frame_parms->ofdm_symbol_size + txdataF_offset + k],
531
                     (void*)&txdataF_precoding[ap][2*(l*frame_parms->ofdm_symbol_size + k)],
532 533 534
                     NR_NB_SC_PER_RB*sizeof(int32_t));
              k += NR_NB_SC_PER_RB;
              if (k >= frame_parms->ofdm_symbol_size) {
535
                k -= frame_parms->ofdm_symbol_size;
536 537
              }
            }
538 539 540 541 542 543 544 545 546 547
            else {
              //get the precoding matrix weights:
              int32_t **mat = gNB->nr_mimo_precoding_matrix[rel15->nrOfLayers-1];
              //i_row =0,...,dl_antenna_port
              //j_col =0,...,nrOfLayers
              //mat[pmi][i_rows*2+j_col]
              int *W_prec;
              W_prec = (int32_t *)&mat[pmi][ap*rel15->nrOfLayers];
              for (int i=0; i<NR_NB_SC_PER_RB; i++) {
                int32_t re_offset = l*frame_parms->ofdm_symbol_size + k;
548
                int32_t precodatatx_F = nr_layer_precoder_cm(txdataF_precoding, W_prec, rel15->nrOfLayers, re_offset);
549 550 551 552 553 554 555 556 557 558 559 560
                ((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];
  #ifdef DEBUG_DLSCH_MAPPING
                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)]);
  #endif
                if (++k >= frame_parms->ofdm_symbol_size) {
                  k -= frame_parms->ofdm_symbol_size;
                }
              }
            }
561 562 563 564
          }
        } //RB loop
      } // symbol loop
    }// port loop
565

566
    stop_meas(&gNB->dlsch_precoding_stats);
567 568 569 570 571 572 573 574 575

    // 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
576
    if (gNB->common_vars.beam_id[0][slot*frame_parms->symbols_per_slot]==255) {
577 578 579 580
      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
581
      LOG_D(PHY,"beam index for PDSCH allocation already taken\n");
582
    }
francescomani's avatar
francescomani committed
583 584 585
    for (int layer = 0; layer<rel15->nrOfLayers; layer++)
      free16(txdataF_precoding[layer],2*14*frame_parms->ofdm_symbol_size);
    free16(txdataF_precoding,rel15->nrOfLayers);
586
  }// dlsch loop
587
}
588

589
void dump_pdsch_stats(FILE *fd,PHY_VARS_gNB *gNB) {
590 591 592 593
  for (int i = 0; i < MAX_MOBILES_PER_GNB; i++) {
    NR_gNB_PHY_STATS_t *stats = &gNB->phy_stats[i];
    if (stats->active && stats->frame != stats->dlsch_stats.dump_frame) {
      stats->dlsch_stats.dump_frame = stats->frame;
francescomani's avatar
francescomani committed
594 595
      fprintf(fd,
              "DLSCH RNTI %x: current_Qm %d, current_RI %d, total_bytes TX %d\n",
596 597 598 599
              stats->rnti,
              stats->dlsch_stats.current_Qm,
              stats->dlsch_stats.current_RI,
              stats->dlsch_stats.total_bytes_tx);
600
    }
601
  }
602
}