nr_dlsch.c 10.8 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"
Guy De Souza's avatar
Guy De Souza committed
37

Guy De Souza's avatar
Guy De Souza committed
38
//#define DEBUG_DLSCH
Guy De Souza's avatar
Guy De Souza committed
39
//#define DEBUG_DLSCH_MAPPING
Guy De Souza's avatar
Guy De Souza committed
40

Guy De Souza's avatar
Guy De Souza committed
41
void nr_pdsch_codeword_scrambling(uint8_t *in,
42 43 44 45 46
                                  uint32_t size,
                                  uint8_t q,
                                  uint32_t Nid,
                                  uint32_t n_RNTI,
                                  uint32_t* out) {
47

48
  uint8_t reset, b_idx;
49 50 51 52 53 54
  uint32_t x1, x2, s=0;

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

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

}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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);
    //    printf("in[%d] %x => %x\n",i,in32,out[i]);
    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
109

110
uint8_t nr_generate_pdsch(NR_gNB_DLSCH_t *dlsch,
Guy De Souza's avatar
Guy De Souza committed
111
                          uint32_t ***pdsch_dmrs,
112 113
                          int32_t** txdataF,
                          int16_t amp,
114
                          int     frame,
115
                          uint8_t slot,
116
                          NR_DL_FRAME_PARMS *frame_parms,
Raymond Knopp's avatar
Raymond Knopp committed
117
			  int xOverhead,
118 119
                          time_stats_t *dlsch_encoding_stats,
                          time_stats_t *dlsch_scrambling_stats,
120 121 122 123 124 125 126 127
                          time_stats_t *dlsch_modulation_stats,
			  time_stats_t *tinput,
			  time_stats_t *tprep,
			  time_stats_t *tparity,
			  time_stats_t *toutput,
			  time_stats_t *dlsch_rate_matching_stats,
			  time_stats_t *dlsch_interleaving_stats,
			  time_stats_t *dlsch_segmentation_stats) {
128

129 130
  int harq_pid = 0;
  NR_DL_gNB_HARQ_t *harq = dlsch->harq_processes[harq_pid];
Raymond Knopp's avatar
Raymond Knopp committed
131
  nfapi_nr_dl_tti_pdsch_pdu_rel15_t *rel15 = &harq->pdsch_pdu.pdsch_pdu_rel15;
Guy De Souza's avatar
Guy De Souza committed
132
  uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
133 134
  int16_t **mod_symbs = (int16_t**)dlsch->mod_symbs;
  int16_t **tx_layers = (int16_t**)dlsch->txdataF;
135
  int8_t Wf[2], Wt[2], l0, l_prime[2], delta;
Guy De Souza's avatar
Guy De Souza committed
136

137 138 139 140 141 142 143 144
  uint8_t dmrs_Type = rel15->dmrsConfigType;
  int nb_re_dmrs = (dmrs_Type== NFAPI_NR_DMRS_TYPE1) ? 6:4;
  uint16_t n_dmrs = ((rel15->rbSize+rel15->rbStart)*nb_re_dmrs)<<1;
  int16_t mod_dmrs[n_dmrs<<1];

  uint16_t nb_re = ((12*rel15->NrOfSymbols)-nb_re_dmrs-xOverhead)*rel15->rbSize*rel15->NrOfCodewords;
  uint8_t Qm = rel15->qamModOrder[0];
  uint32_t encoded_length = nb_re*Qm;
145

Guy De Souza's avatar
Guy De Souza committed
146
  /// CRC, coding, interleaving and rate matching
147
  AssertFatal(harq->pdu!=NULL,"harq->pdu is null\n");
148
  start_meas(dlsch_encoding_stats);
149 150 151 152
  nr_dlsch_encoding(harq->pdu, frame, slot, dlsch, frame_parms,tinput,tprep,tparity,toutput,
		    dlsch_rate_matching_stats,
		    dlsch_interleaving_stats,
		    dlsch_segmentation_stats);
153
  stop_meas(dlsch_encoding_stats);
Guy De Souza's avatar
Guy De Souza committed
154
#ifdef DEBUG_DLSCH
Raymond Knopp's avatar
Raymond Knopp committed
155 156 157 158 159 160 161 162 163 164 165 166
  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");
  }
Guy De Souza's avatar
Guy De Souza committed
167 168
  printf("\n");
#endif
Guy De Souza's avatar
Guy De Souza committed
169

170 171


Guy De Souza's avatar
Guy De Souza committed
172
  /// scrambling
173
  start_meas(dlsch_scrambling_stats);
Raymond Knopp's avatar
Raymond Knopp committed
174
  for (int q=0; q<rel15->NrOfCodewords; q++)
Guy De Souza's avatar
Guy De Souza committed
175
    memset((void*)scrambled_output[q], 0, (encoded_length>>5)*sizeof(uint32_t));
Raymond Knopp's avatar
Raymond Knopp committed
176
  for (int q=0; q<rel15->NrOfCodewords; q++)
177 178 179 180 181 182 183
    nr_pdsch_codeword_scrambling_optim(harq->f,
				       encoded_length,
				       q,
				       rel15->dlDmrsScramblingId,
				       rel15->rnti,
				       scrambled_output[q]);
  
184
  stop_meas(dlsch_scrambling_stats);
Guy De Souza's avatar
Guy De Souza committed
185
#ifdef DEBUG_DLSCH
Raymond Knopp's avatar
Raymond Knopp committed
186 187 188 189 190 191
  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");
  }
Guy De Souza's avatar
Guy De Souza committed
192
#endif
Guy De Souza's avatar
Guy De Souza committed
193 194
 
  /// Modulation
195
  start_meas(dlsch_modulation_stats);
Raymond Knopp's avatar
Raymond Knopp committed
196
  for (int q=0; q<rel15->NrOfCodewords; q++)
197
    nr_modulation(scrambled_output[q],
198
                         encoded_length,
199
                         Qm,
Guy De Souza's avatar
Guy De Souza committed
200
                         mod_symbs[q]);
201
  stop_meas(dlsch_modulation_stats);
Guy De Souza's avatar
Guy De Souza committed
202
#ifdef DEBUG_DLSCH
203 204
  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
205 206 207 208
    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");
Guy De Souza's avatar
Guy De Souza committed
209 210 211
  }
#endif

Guy De Souza's avatar
Guy De Souza committed
212 213

  /// Layer mapping
214
  nr_layer_mapping(mod_symbs,
Raymond Knopp's avatar
Raymond Knopp committed
215
		   rel15->nrOfLayers,
216
		   nb_re,
Raymond Knopp's avatar
Raymond Knopp committed
217
		   tx_layers);
Guy De Souza's avatar
Guy De Souza committed
218
#ifdef DEBUG_DLSCH
Raymond Knopp's avatar
Raymond Knopp committed
219 220
  printf("Layer mapping (%d layers):\n", rel15->nrOfLayers);
  for (int l=0; l<rel15->nrOfLayers; l++)
221 222
    for (int i=0; i<(nb_re/rel15->nrOfLayers)>>3; i++) {
      printf("layer %d, Re %d..%d : ",l,i<<3,(i<<3)+7);
Raymond Knopp's avatar
Raymond Knopp committed
223
      for (int j=0; j<8; j++) {
224
	printf("l%d %d\t", tx_layers[l][((i<<3)+j)<<1], tx_layers[l][(((i<<3)+j)<<1)+1]);
Raymond Knopp's avatar
Raymond Knopp committed
225 226
      }
      printf("\n");
Guy De Souza's avatar
Guy De Souza committed
227 228
    }
#endif
Guy De Souza's avatar
Guy De Souza committed
229

Guy De Souza's avatar
Guy De Souza committed
230
  /// Antenna port mapping
231 232
  //to be moved to init phase potentially, for now tx_layers 1-8 are mapped on antenna ports 1000-1007
  
Guy De Souza's avatar
Guy De Souza committed
233
  /// DMRS QPSK modulation
234 235 236 237 238
  
  
  l0 = get_l0(rel15->dlDmrsSymbPos);
  nr_modulation(pdsch_dmrs[l0][0], n_dmrs, DMRS_MOD_ORDER, mod_dmrs); // currently only codeword 0 is modulated. Qm = 2 as DMRS is QPSK modulated
  
239
#ifdef DEBUG_DLSCH
240 241 242 243 244 245
  printf("DMRS modulation (single symbol %d, %d symbols, type %d):\n", l0, n_dmrs>>1, 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");
Guy De Souza's avatar
Guy De Souza committed
246
  }
247
#endif
248 249
  
  
250
  /// Resource mapping
251
  
252
  // Non interleaved VRB to PRB mapping
Raymond Knopp's avatar
Raymond Knopp committed
253
  uint16_t start_sc = frame_parms->first_carrier_offset + rel15->rbStart*NR_NB_SC_PER_RB;
254 255
  if (start_sc >= frame_parms->ofdm_symbol_size)
    start_sc -= frame_parms->ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
256

Guy De Souza's avatar
Guy De Souza committed
257
#ifdef DEBUG_DLSCH_MAPPING
258 259
  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
260
#endif
Raymond Knopp's avatar
Raymond Knopp committed
261
  for (int ap=0; ap<rel15->nrOfLayers; ap++) {
Guy De Souza's avatar
Guy De Souza committed
262 263

    // DMRS params for this ap
Raymond Knopp's avatar
Raymond Knopp committed
264 265 266
    get_Wt(Wt, ap, dmrs_Type);
    get_Wf(Wf, ap, dmrs_Type);
    delta = get_delta(ap, dmrs_Type);
267
    l_prime[0] = 0; // single symbol ap 0
Guy De Souza's avatar
Guy De Souza committed
268
    uint8_t dmrs_symbol = l0+l_prime[0];
Guy De Souza's avatar
Guy De Souza committed
269
#ifdef DEBUG_DLSCH_MAPPING
270 271
    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",
	   1+dmrs_Type,ap, Wt[0], Wt[1], Wf[0], Wf[1], delta, l_prime[0], l0, dmrs_symbol);
Guy De Souza's avatar
Guy De Souza committed
272
#endif
Guy De Souza's avatar
Guy De Souza committed
273
    uint8_t k_prime=0;
274
    uint16_t m=0, n=0, dmrs_idx=0, k=0;
275

276
    int txdataF_offset = (slot%2)*frame_parms->samples_per_slot_wCP;
Raymond Knopp's avatar
Raymond Knopp committed
277 278
    if (dmrs_Type == NFAPI_NR_DMRS_TYPE1) // another if condition to be included to check pdsch config type (reference of k)
      dmrs_idx = rel15->rbStart*6;
279
    else
Raymond Knopp's avatar
Raymond Knopp committed
280
      dmrs_idx = rel15->rbStart*4;
281

Raymond Knopp's avatar
Raymond Knopp committed
282
    for (int l=rel15->StartSymbolIndex; l<rel15->StartSymbolIndex+rel15->NrOfSymbols; l++) {
283
      k = start_sc;
Raymond Knopp's avatar
Raymond Knopp committed
284 285
      for (int i=0; i<rel15->rbSize*NR_NB_SC_PER_RB; i++) {
        if ((l == dmrs_symbol) && (k == ((start_sc+get_dmrs_freq_idx(n, k_prime, delta, dmrs_Type))%(frame_parms->ofdm_symbol_size)))) {
286 287
          ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)] = (Wt[l_prime[0]]*Wf[k_prime]*amp*mod_dmrs[dmrs_idx<<1]) >> 15;
          ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)] = (Wt[l_prime[0]]*Wf[k_prime]*amp*mod_dmrs[(dmrs_idx<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
288
#ifdef DEBUG_DLSCH_MAPPING
289 290 291
	  printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
		 dmrs_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)]);
Guy De Souza's avatar
Guy De Souza committed
292
#endif
293 294 295
          dmrs_idx++;
          k_prime++;
          k_prime&=1;
Guy De Souza's avatar
Guy De Souza committed
296
          n+=(k_prime)?0:1;
Guy De Souza's avatar
Guy De Souza committed
297 298
        }

Guy De Souza's avatar
Guy De Souza committed
299 300
        else {

301 302
          ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)] = (amp * tx_layers[ap][m<<1]) >> 15;
          ((int16_t*)txdataF[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
303
#ifdef DEBUG_DLSCH_MAPPING
304
	  printf("m %d\t l %d \t k %d \t txdataF: %d %d\n",
305
m, l, k, ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + (2*txdataF_offset)],
306
		 ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1 + (2*txdataF_offset)]);
Guy De Souza's avatar
Guy De Souza committed
307
#endif
Guy De Souza's avatar
Guy De Souza committed
308 309
          m++;
        }
310 311
        if (++k >= frame_parms->ofdm_symbol_size)
          k -= frame_parms->ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
312
      }
313
    }
Guy De Souza's avatar
Guy De Souza committed
314
  }
315 316
  return 0;
}