nr_dlsch.c 9.27 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,
Guy De Souza's avatar
Guy De Souza committed
42
                         uint16_t size,
43 44 45 46 47
                         uint8_t q,
                         uint32_t Nid,
                         uint32_t n_RNTI,
                         uint32_t* out) {

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
  }

}

Guy De Souza's avatar
Guy De Souza committed
68

69 70
uint8_t nr_generate_pdsch(NR_gNB_DLSCH_t *dlsch,
                          NR_gNB_DCI_ALLOC_t *dci_alloc,
Guy De Souza's avatar
Guy De Souza committed
71
                          uint32_t ***pdsch_dmrs,
72 73
                          int32_t** txdataF,
                          int16_t amp,
74
                          int     frame,
75
                          uint8_t slot,
76
                          NR_DL_FRAME_PARMS *frame_parms,
77 78 79 80
                          nfapi_nr_config_request_t *config,
                          time_stats_t *dlsch_encoding_stats,
                          time_stats_t *dlsch_scrambling_stats,
                          time_stats_t *dlsch_modulation_stats) {
81

82
  NR_DL_gNB_HARQ_t *harq = dlsch->harq_processes[dci_alloc->harq_pid];
Guy De Souza's avatar
Guy De Souza committed
83
  nfapi_nr_dl_config_dlsch_pdu_rel15_t *rel15 = &harq->dlsch_pdu.dlsch_pdu_rel15;
84
  nfapi_nr_dl_config_pdcch_parameters_rel15_t pdcch_params = dci_alloc->pdcch_params;
Guy De Souza's avatar
Guy De Souza committed
85
  uint32_t scrambled_output[NR_MAX_NB_CODEWORDS][NR_MAX_PDSCH_ENCODED_LENGTH>>5];
86 87
  int16_t **mod_symbs = (int16_t**)dlsch->mod_symbs;
  int16_t **tx_layers = (int16_t**)dlsch->txdataF;
88 89
  int8_t Wf[2], Wt[2], l0, l_prime[2], delta;
  uint16_t nb_symbols = rel15->nb_mod_symbols;
Guy De Souza's avatar
Guy De Souza committed
90
  uint8_t Qm = rel15->modulation_order;
91
  uint16_t encoded_length = nb_symbols*Qm;
Guy De Souza's avatar
Guy De Souza committed
92 93

  /// CRC, coding, interleaving and rate matching
94
  AssertFatal(harq->pdu!=NULL,"harq->pdu is null\n");
95
  start_meas(dlsch_encoding_stats);
96
  nr_dlsch_encoding(harq->pdu, frame, slot, dlsch, frame_parms);
97
  stop_meas(dlsch_encoding_stats);
Guy De Souza's avatar
Guy De Souza committed
98 99
#ifdef DEBUG_DLSCH
printf("PDSCH encoding:\nPayload:\n");
100
for (int i=0; i<harq->B>>7; i++) {
Guy De Souza's avatar
Guy De Souza committed
101 102 103 104 105
  for (int j=0; j<16; j++)
    printf("0x%02x\t", harq->pdu[(i<<4)+j]);
  printf("\n");
}
printf("\nEncoded payload:\n");
Guy De Souza's avatar
Guy De Souza committed
106 107 108 109
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
110
}
Guy De Souza's avatar
Guy De Souza committed
111
printf("\n");
Guy De Souza's avatar
Guy De Souza committed
112
#endif
Guy De Souza's avatar
Guy De Souza committed
113 114

  /// scrambling
115
  start_meas(dlsch_scrambling_stats);
Guy De Souza's avatar
Guy De Souza committed
116 117
  for (int q=0; q<rel15->nb_codewords; q++)
    memset((void*)scrambled_output[q], 0, (encoded_length>>5)*sizeof(uint32_t));
Guy De Souza's avatar
Guy De Souza committed
118
  uint16_t n_RNTI = (pdcch_params.search_space_type == NFAPI_NR_SEARCH_SPACE_TYPE_UE_SPECIFIC)? \
119
  ((pdcch_params.scrambling_id==0)?pdcch_params.rnti:0) : 0;
Guy De Souza's avatar
Guy De Souza committed
120
  uint16_t Nid = (pdcch_params.search_space_type == NFAPI_NR_SEARCH_SPACE_TYPE_UE_SPECIFIC)? \
121
  pdcch_params.scrambling_id : config->sch_config.physical_cell_id.value;
Guy De Souza's avatar
Guy De Souza committed
122
  for (int q=0; q<rel15->nb_codewords; q++)
Guy De Souza's avatar
Guy De Souza committed
123
    nr_pdsch_codeword_scrambling(harq->f,
124
                         encoded_length,
Guy De Souza's avatar
Guy De Souza committed
125 126 127 128
                         q,
                         Nid,
                         n_RNTI,
                         scrambled_output[q]);
129
  stop_meas(dlsch_scrambling_stats);
Guy De Souza's avatar
Guy De Souza committed
130
#ifdef DEBUG_DLSCH
Guy De Souza's avatar
Guy De Souza committed
131
printf("PDSCH scrambling:\n");
Guy De Souza's avatar
Guy De Souza committed
132
for (int i=0; i<encoded_length>>8; i++) {
Guy De Souza's avatar
Guy De Souza committed
133 134 135 136
  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
137
#endif
Guy De Souza's avatar
Guy De Souza committed
138 139
 
  /// Modulation
140
  start_meas(dlsch_modulation_stats);
Guy De Souza's avatar
Guy De Souza committed
141
  for (int q=0; q<rel15->nb_codewords; q++)
142
    nr_modulation(scrambled_output[q],
143
                         encoded_length,
144
                         Qm,
Guy De Souza's avatar
Guy De Souza committed
145
                         mod_symbs[q]);
146
  stop_meas(dlsch_modulation_stats);
Guy De Souza's avatar
Guy De Souza committed
147
#ifdef DEBUG_DLSCH
Guy De Souza's avatar
Guy De Souza committed
148
printf("PDSCH Modulation: Qm %d(%d)\n", Qm, nb_symbols);
149
for (int i=0; i<nb_symbols>>3; i++) {
Guy De Souza's avatar
Guy De Souza committed
150 151
  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]);
Guy De Souza's avatar
Guy De Souza committed
152
  }
Guy De Souza's avatar
Guy De Souza committed
153 154
  printf("\n");
}
Guy De Souza's avatar
Guy De Souza committed
155 156
#endif

Guy De Souza's avatar
Guy De Souza committed
157 158

  /// Layer mapping
159
  nr_layer_mapping(mod_symbs,
Guy De Souza's avatar
Guy De Souza committed
160
                         rel15->nb_layers,
161
                         nb_symbols,
162
                         tx_layers);
Guy De Souza's avatar
Guy De Souza committed
163 164 165
#ifdef DEBUG_DLSCH
printf("Layer mapping (%d layers):\n", rel15->nb_layers);
for (int l=0; l<rel15->nb_layers; l++)
Guy De Souza's avatar
Guy De Souza committed
166
  for (int i=0; i<(nb_symbols/rel15->nb_layers)>>3; i++) {
Guy De Souza's avatar
Guy De Souza committed
167 168 169 170 171 172
    for (int j=0; j<8; j++) {
      printf("%d %d\t", tx_layers[l][((i<<3)+j)<<1], tx_layers[l][(((i<<3)+j)<<1)+1]);
    }
    printf("\n");
  }
#endif
Guy De Souza's avatar
Guy De Souza committed
173

Guy De Souza's avatar
Guy De Souza committed
174 175 176 177
  /// Antenna port mapping
    //to be moved to init phase potentially, for now tx_layers 1-8 are mapped on antenna ports 1000-1007

  /// DMRS QPSK modulation
Guy De Souza's avatar
Guy De Souza committed
178
  uint16_t n_dmrs = (rel15->n_prb*rel15->nb_re_dmrs)<<1;
179
  int16_t mod_dmrs[n_dmrs<<1];
180 181
  uint8_t dmrs_type = config->pdsch_config.dmrs_type.value;
  uint8_t mapping_type = config->pdsch_config.mapping_type.value;
182

183
  l0 = get_l0(mapping_type, 2);//config->pdsch_config.dmrs_typeA_position.value);
Khalid Ahmed's avatar
Khalid Ahmed committed
184
  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
185

186
#ifdef DEBUG_DLSCH
187 188
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++) {
Guy De Souza's avatar
Guy De Souza committed
189 190 191 192 193
  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");
}
194
#endif
Guy De Souza's avatar
Guy De Souza committed
195

196

197
  /// Resource mapping
Guy De Souza's avatar
Guy De Souza committed
198

199 200 201 202
  // Non interleaved VRB to PRB mapping
  uint16_t start_sc = frame_parms->first_carrier_offset + rel15->start_prb*NR_NB_SC_PER_RB;
  if (start_sc >= frame_parms->ofdm_symbol_size)
    start_sc -= frame_parms->ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
203

Guy De Souza's avatar
Guy De Souza committed
204
#ifdef DEBUG_DLSCH_MAPPING
205 206
 printf("PDSCH resource mapping started (start SC %d\tstart symbol %d\tN_PRB %d\tnb_symbols %d)\n",
	start_sc, rel15->start_symbol, rel15->n_prb, rel15->nb_symbols);
Guy De Souza's avatar
Guy De Souza committed
207 208
#endif

Guy De Souza's avatar
Guy De Souza committed
209 210 211
  for (int ap=0; ap<rel15->nb_layers; ap++) {

    // DMRS params for this ap
212 213
    get_Wt(Wt, ap, dmrs_type);
    get_Wf(Wf, ap, dmrs_type);
Guy De Souza's avatar
Guy De Souza committed
214
    delta = get_delta(ap, dmrs_type);
215
    l_prime[0] = 0; // single symbol ap 0
Guy De Souza's avatar
Guy De Souza committed
216
    uint8_t dmrs_symbol = l0+l_prime[0];
Guy De Souza's avatar
Guy De Souza committed
217
#ifdef DEBUG_DLSCH_MAPPING
218 219
printf("DMRS 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",
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
220
#endif
Guy De Souza's avatar
Guy De Souza committed
221
    uint8_t k_prime=0;
222 223 224 225 226
    uint16_t m=0, n=0, dmrs_idx=0, k=0;

    for (int l=rel15->start_symbol; l<rel15->start_symbol+rel15->nb_symbols; l++) {
      k = start_sc;
      for (int i=0; i<rel15->n_prb*NR_NB_SC_PER_RB; i++) {
Khalid Ahmed's avatar
Khalid Ahmed committed
227
        if ((l == dmrs_symbol) && (k == ((start_sc+get_dmrs_freq_idx(n, k_prime, delta, dmrs_type))%(frame_parms->ofdm_symbol_size)))) {
228 229
          ((int16_t*)txdataF[ap])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (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] = (Wt[l_prime[0]]*Wf[k_prime]*amp*mod_dmrs[(dmrs_idx<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
230
#ifdef DEBUG_DLSCH_MAPPING
Guy De Souza's avatar
Guy De Souza committed
231
printf("dmrs_idx %d\t l %d \t k %d \t k_prime %d \t n %d \t txdataF: %d %d\n",
232 233
dmrs_idx, l, k, k_prime, n, ((int16_t*)txdataF[ap])[(l*frame_parms->ofdm_symbol_size + k)<<1],
((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1]);
Guy De Souza's avatar
Guy De Souza committed
234
#endif
235 236 237
          dmrs_idx++;
          k_prime++;
          k_prime&=1;
Guy De Souza's avatar
Guy De Souza committed
238
          n+=(k_prime)?0:1;
Guy De Souza's avatar
Guy De Souza committed
239 240
        }

Guy De Souza's avatar
Guy De Souza committed
241 242
        else {

243 244
          ((int16_t*)txdataF[ap])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (amp * tx_layers[ap][m<<1]) >> 15;
          ((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * tx_layers[ap][(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
245
#ifdef DEBUG_DLSCH_MAPPING
Guy De Souza's avatar
Guy De Souza committed
246
printf("m %d\t l %d \t k %d \t txdataF: %d %d\n",
247 248
m, l, k, ((int16_t*)txdataF[ap])[(l*frame_parms->ofdm_symbol_size + k)<<1],
((int16_t*)txdataF[ap])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1]);
Guy De Souza's avatar
Guy De Souza committed
249
#endif
Guy De Souza's avatar
Guy De Souza committed
250 251
          m++;
        }
252 253
        if (++k >= frame_parms->ofdm_symbol_size)
          k -= frame_parms->ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
254
      }
255
    }
Guy De Souza's avatar
Guy De Souza committed
256
  }
257 258
  return 0;
}