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

/*! \file PHY/NR_TRANSPORT/nr_dci.c
23
* \brief Implements DCI encoding/decoding and PDCCH TX/RX procedures (38.212/38.213/38.214). V15.2.0 2018-06.
24 25 26 27 28 29 30 31 32 33 34
* \author Guy De Souza
* \date 2018
* \version 0.1
* \company Eurecom
* \email: desouza@eurecom.fr
* \note
* \warning
*/

#include "nr_dci.h"

knopp's avatar
knopp committed
35 36 37
//#define DEBUG_PDCCH_DMRS
//#define DEBUG_DCI
//#define DEBUG_CHANNEL_CODING
38
#define PDCCH_TEST_POLAR_TEMP_FIX
39

40

41 42
extern short nr_mod_table[NR_MOD_TABLE_SIZE_SHORT];

yilmazt's avatar
yilmazt committed
43
uint16_t nr_get_dci_size(nfapi_nr_dci_format_e format,
Guy De Souza's avatar
Guy De Souza committed
44
                        nfapi_nr_rnti_type_e rnti_type,
Guy De Souza's avatar
Guy De Souza committed
45
                        uint16_t N_RB,
Guy De Souza's avatar
Guy De Souza committed
46
                        nfapi_nr_config_request_t* config)
47
{
yilmazt's avatar
yilmazt committed
48
  uint16_t size = 0;
49 50

  switch(format) {
Guy De Souza's avatar
Guy De Souza committed
51
/*Only sizes for 0_0 and 1_0 are correct at the moment*/
Guy De Souza's avatar
Guy De Souza committed
52
    case NFAPI_NR_UL_DCI_FORMAT_0_0:
Guy De Souza's avatar
Guy De Souza committed
53 54
      /// fixed: Format identifier 1, Hop flag 1, MCS 5, NDI 1, RV 2, HARQ PID 4, PUSCH TPC 2 Time Domain assgnmt 4 --20
      size += 20;
55
      size += (uint8_t)ceil( log2( (N_RB*(N_RB+1))>>1 ) ); // Freq domain assignment -- hopping scenario to be updated
Guy De Souza's avatar
Guy De Souza committed
56
      size += nr_get_dci_size(NFAPI_NR_DL_DCI_FORMAT_1_0, rnti_type, N_RB, config) - size; // Padding to match 1_0 size
Guy De Souza's avatar
Guy De Souza committed
57
      // UL/SUL indicator assumed to be 0
58 59
      break;

Guy De Souza's avatar
Guy De Souza committed
60
    case NFAPI_NR_UL_DCI_FORMAT_0_1:
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      /// fixed: Format identifier 1, MCS 5, NDI 1, RV 2, HARQ PID 4, PUSCH TPC 2, SRS request 2 --17
      size += 17;
      // Carrier indicator
      // UL/SUL indicator
      // BWP Indicator
      // Freq domain assignment
      // Time domain assignment
      // VRB to PRB mapping
      // Frequency Hopping flag
      // 1st DAI
      // 2nd DAI
      // SRS resource indicator
      // Precoding info and number of layers
      // Antenna ports
      // CSI request
      // CBGTI
      // PTRS - DMRS association
      // beta offset indicator
      // DMRS sequence init
      break;

Guy De Souza's avatar
Guy De Souza committed
82
    case NFAPI_NR_DL_DCI_FORMAT_1_0:
Guy De Souza's avatar
Guy De Souza committed
83 84
      /// fixed: Format identifier 1, VRB2PRB 1, MCS 5, NDI 1, RV 2, HARQ PID 4, DAI 2, PUCCH TPC 2, PUCCH RInd 3, PDSCH to HARQ TInd 3 Time Domain assgnmt 4 -- 28
      size += 28;
85
      size += (uint8_t)ceil( log2( (N_RB*(N_RB+1))>>1 ) ); // Freq domain assignment
Guy De Souza's avatar
Guy De Souza committed
86

87 88
      break;

Guy De Souza's avatar
Guy De Souza committed
89
    case NFAPI_NR_DL_DCI_FORMAT_1_1:
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      // Carrier indicator
      size += 1; // Format identifier
      // BWP Indicator
      // Freq domain assignment
      // Time domain assignment
      // VRB to PRB mapping
      // PRB bundling size indicator
      // Rate matching indicator
      // ZP CSI-RS trigger
        /// TB1- MCS 5, NDI 1, RV 2
      size += 8;
      // TB2
      size += 4 ;  // HARQ PID
      // DAI
      size += 2; // TPC PUCCH
      size += 3; // PUCCH resource indicator
      size += 3; // PDSCH to HARQ timing indicator
      // Antenna ports
      // Tx Config Indication
      size += 2; // SRS request
      // CBGTI
      // CBGFI
      size += 1; // DMRS sequence init
      
      break;

Guy De Souza's avatar
Guy De Souza committed
116
    case NFAPI_NR_DL_DCI_FORMAT_2_0:
117 118
      break;

Guy De Souza's avatar
Guy De Souza committed
119
    case NFAPI_NR_DL_DCI_FORMAT_2_1:
120 121
      break;

Guy De Souza's avatar
Guy De Souza committed
122
    case NFAPI_NR_DL_DCI_FORMAT_2_2:
123 124
      break;

Guy De Souza's avatar
Guy De Souza committed
125
    case NFAPI_NR_DL_DCI_FORMAT_2_3:
126 127 128 129 130 131 132 133 134
      break;

  default:
    AssertFatal(1==0, "Invalid NR DCI format %d\n", format);
  }

  return size;
}

135
void nr_pdcch_scrambling(uint32_t *in,
Hongzhi's avatar
Hongzhi committed
136
                         uint16_t size,
137 138
                         uint32_t Nid,
                         uint32_t n_RNTI,
139 140 141 142 143 144 145 146
                         uint32_t* out) {

  uint8_t reset;
  uint32_t x1, x2, s=0;

  reset = 1;
  x2 = (n_RNTI<<16) + Nid;

147
  for (int i=0; i<size; i++) {
148 149 150
    if ((i&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
Hongzhi's avatar
Hongzhi committed
151
      if (i){
152 153 154
	in++;
	out++;
      }
155
    }
Hongzhi's avatar
Hongzhi committed
156
    (*out) ^= ((((*in)>>(i&0x1f))&1) ^ ((s>>(i&0x1f))&1))<<(i&0x1f);
knopp's avatar
knopp committed
157
    //    printf("nr_pdcch_scrambling: in %d => out %d\n",((*in)>>(i&0x1f))&1,((*out)>>(i&0x1f))&1);
158
  }
159 160 161

}

162
uint8_t nr_generate_dci_top(NR_gNB_PDCCH pdcch_vars,
163
			    t_nrPolar_paramsPtr *nrPolar_params,
Guy De Souza's avatar
Guy De Souza committed
164
                            uint32_t **gold_pdcch_dmrs,
Guy De Souza's avatar
Guy De Souza committed
165 166
                            int32_t** txdataF,
                            int16_t amp,
167
                            NR_DL_FRAME_PARMS frame_parms,
168
                            nfapi_nr_config_request_t config)
169
{
170

171
  int16_t mod_dmrs[NR_MAX_CSET_DURATION][NR_MAX_PDCCH_DMRS_LENGTH>>1]; // 3 for the max coreset duration
Guy De Souza's avatar
Guy De Souza committed
172
  uint8_t idx=0;
173
  uint16_t a;
174
  int k,l,k_prime,dci_idx, dmrs_idx;
175
  nr_cce_t cce;
176 177
  nr_reg_t reg;
  nr_reg_t reg_mapping_list[NR_MAX_PDCCH_AGG_LEVEL*NR_NB_REG_PER_CCE];
Guy De Souza's avatar
Guy De Souza committed
178

179 180 181
  /*First iteration: single DCI*/
  NR_gNB_DCI_ALLOC_t dci_alloc = pdcch_vars.dci_alloc[0];
  nfapi_nr_dl_config_pdcch_parameters_rel15_t pdcch_params = dci_alloc.pdcch_params;
Guy De Souza's avatar
Guy De Souza committed
182 183
  uint16_t dmrs_length = dci_alloc.L*36; //2(QPSK)*3(per RB)*6(REG per CCE)
  uint16_t encoded_length = dci_alloc.L*108; //2(QPSK)*9(per RB)*6(REG per CCE)
184

Guy De Souza's avatar
Guy De Souza committed
185 186 187
  /*The coreset is initialised
  * in frequency: the first subcarrier is obtained by adding the first CRB overlapping the SSB and the rb_offset
  * in time: by its first slot and its first symbol*/
Guy De Souza's avatar
Guy De Souza committed
188
  uint16_t cset_start_sc = frame_parms.first_carrier_offset + ((int)floor(frame_parms.ssb_start_subcarrier/NR_NB_SC_PER_RB)+pdcch_params.rb_offset)*NR_NB_SC_PER_RB;
189 190
  //  uint8_t cset_start_symb = pdcch_params.first_slot*frame_parms.symbols_per_slot + pdcch_params.first_symbol;
  uint8_t cset_start_symb = pdcch_params.first_symbol;
Guy De Souza's avatar
Guy De Souza committed
191
  uint8_t cset_nsymb = pdcch_params.n_symb;
Guy De Souza's avatar
Guy De Souza committed
192 193
  dci_idx = 0;

194

Guy De Souza's avatar
Guy De Souza committed
195
  /// DMRS QPSK modulation
196 197
    /*There is a need to shift from which index the pregenerated DMRS sequence is used
     * see 38211 r15.2.0 section 7.4.1.3.2: assumption is the reference point for k refers to the DMRS sequence*/
198
  if (pdcch_params.config_type == NFAPI_NR_CSET_CONFIG_PDCCH_CONFIG)
199
    gold_pdcch_dmrs += ((int)floor(frame_parms.ssb_start_subcarrier/NR_NB_SC_PER_RB)+pdcch_params.rb_offset)*3/32;
200

Guy De Souza's avatar
Guy De Souza committed
201 202 203 204 205 206 207 208 209 210 211
  for (int symb=cset_start_symb; symb<cset_start_symb + pdcch_params.n_symb; symb++) {
    for (int i=0; i<dmrs_length>>1; i++) {
      idx = ((((gold_pdcch_dmrs[symb][(i<<1)>>5])>>((i<<1)&0x1f))&1)<<1) ^ (((gold_pdcch_dmrs[symb][((i<<1)+1)>>5])>>(((i<<1)+1)&0x1f))&1);
      mod_dmrs[symb][i<<1] = nr_mod_table[(NR_MOD_TABLE_QPSK_OFFSET + idx)<<1];
      mod_dmrs[symb][(i<<1)+1] = nr_mod_table[((NR_MOD_TABLE_QPSK_OFFSET + idx)<<1) + 1];
  #ifdef DEBUG_PDCCH_DMRS
    printf("symb %d i %d idx %d gold seq %u b0-b1 %d-%d mod_dmrs %d %d\n", symb, i, idx, gold_pdcch_dmrs[symb][(i<<1)>>5],
    (((gold_pdcch_dmrs[symb][(i<<1)>>5])>>((i<<1)&0x1f))&1), (((gold_pdcch_dmrs[symb][((i<<1)+1)>>5])>>(((i<<1)+1)&0x1f))&1),
     mod_dmrs[symb][(i<<1)], mod_dmrs[symb][(i<<1)+1]);
  #endif
    }
212 213
  }

214
  /// DCI payload processing
Guy De Souza's avatar
Guy De Souza committed
215
    // CRC attachment + Scrambling + Channel coding + Rate matching
yilmazt's avatar
yilmazt committed
216
  uint32_t encoder_output[NR_MAX_DCI_SIZE_DWORD];
Guy De Souza's avatar
Guy De Souza committed
217 218
  uint16_t n_RNTI = (pdcch_params.search_space_type == NFAPI_NR_SEARCH_SPACE_TYPE_UE_SPECIFIC)? ((pdcch_params.scrambling_id)?pdcch_params.rnti:0) : 0;
  uint16_t Nid = (pdcch_params.search_space_type == NFAPI_NR_SEARCH_SPACE_TYPE_UE_SPECIFIC)? pdcch_params.scrambling_id : config.sch_config.physical_cell_id.value;
Guy De Souza's avatar
Guy De Souza committed
219
#ifdef PDCCH_TEST_POLAR_TEMP_FIX
220 221 222 223
  t_nrPolar_paramsPtr currentPtr = NULL;//, polarParams = NULL;
  nr_polar_init(&currentPtr, NR_POLAR_DCI_MESSAGE_TYPE, dci_alloc.size, dci_alloc.L);
//  t_nrPolar_paramsPtr currentPtr = nr_polar_params(*nrPolar_params, NR_POLAR_DCI_MESSAGE_TYPE, dci_alloc.size, dci_alloc.L);
#else 
yilmazt's avatar
yilmazt committed
224
  nr_polar_init(nrPolar_params, NR_POLAR_DCI_MESSAGE_TYPE, dci_alloc.size, dci_alloc.L);
yilmazt's avatar
yilmazt committed
225
  t_nrPolar_paramsPtr currentPtr = nr_polar_params(*nrPolar_params, NR_POLAR_DCI_MESSAGE_TYPE, dci_alloc.size, dci_alloc.L);
226
#endif
Hongzhi's avatar
Hongzhi committed
227

228 229
  //polar_encoder_dci(dci_alloc.dci_pdu, encoder_output, currentPtr, pdcch_params.rnti);
  polar_encoder_fast(dci_alloc.dci_pdu, encoder_output, pdcch_params.rnti,currentPtr);
yilmazt's avatar
yilmazt committed
230

Guy De Souza's avatar
Guy De Souza committed
231
#ifdef DEBUG_CHANNEL_CODING
Hongzhi's avatar
Hongzhi committed
232 233 234
  printf("polar rnti %d\n",pdcch_params.rnti);
  for (int i=0;i<54;i++) printf("Encoded Payload: [%d]->0x%08x \n", i,encoder_output[i]);

235
  printf("DCI PDU: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
yilmazt's avatar
yilmazt committed
236 237
    		  dci_alloc.dci_pdu[0], dci_alloc.dci_pdu[1], dci_alloc.dci_pdu[2], dci_alloc.dci_pdu[3]);
  printf("Encoded Payload: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\n",
238
		  encoder_output[0], encoder_output[1], encoder_output[2], encoder_output[3]);
Guy De Souza's avatar
Guy De Souza committed
239
#endif
Guy De Souza's avatar
Guy De Souza committed
240 241

  /// Scrambling
Hongzhi's avatar
Hongzhi committed
242
  uint32_t scrambled_output[NR_MAX_DCI_SIZE_DWORD]={0};
Guy De Souza's avatar
Guy De Souza committed
243
  nr_pdcch_scrambling(encoder_output, encoded_length, Nid, n_RNTI, scrambled_output);
Hongzhi's avatar
Hongzhi committed
244 245 246 247 248 249
#ifdef DEBUG_CHANNEL_CODING
printf("scrambled output: [0]->0x%08x \t [1]->0x%08x \t [2]->0x%08x \t [3]->0x%08x\t [4]->0x%08x\t [5]->0x%08x\t \
[6]->0x%08x \t [7]->0x%08x \t [8]->0x%08x \t [9]->0x%08x\t [10]->0x%08x\t [11]->0x%08x\n",
		  scrambled_output[0], scrambled_output[1], scrambled_output[2], scrambled_output[3], scrambled_output[4],scrambled_output[5],
		  scrambled_output[6], scrambled_output[7], scrambled_output[8], scrambled_output[9], scrambled_output[10],scrambled_output[11] );	
#endif
yilmazt's avatar
yilmazt committed
250

251
    // QPSK modulation
Guy De Souza's avatar
Guy De Souza committed
252
  int16_t mod_dci[NR_MAX_DCI_SIZE>>1];
Guy De Souza's avatar
Guy De Souza committed
253
  for (int i=0; i<encoded_length>>1; i++) {
Guy De Souza's avatar
Guy De Souza committed
254
    idx = ((((scrambled_output[(i<<1)>>5])>>((i<<1)&0x1f))&1)<<1) ^ (((scrambled_output[((i<<1)+1)>>5])>>(((i<<1)+1)&0x1f))&1);
255 256
    mod_dci[i<<1] = nr_mod_table[(NR_MOD_TABLE_QPSK_OFFSET + idx)<<1];
    mod_dci[(i<<1)+1] = nr_mod_table[((NR_MOD_TABLE_QPSK_OFFSET + idx)<<1) + 1];
Guy De Souza's avatar
Guy De Souza committed
257
#ifdef DEBUG_DCI
Hongzhi's avatar
Hongzhi committed
258 259
  printf("i %d idx %d b0-b1 %d-%d mod_dci %d %d\n", i, idx, (((scrambled_output[(i<<1)>>5])>>((i<<1)&0x1f))&1),
  (((scrambled_output[((i<<1)+1)>>5])>>(((i<<1)+1)&0x1f))&1), mod_dci[(i<<1)], mod_dci[(i<<1)+1]);
Guy De Souza's avatar
Guy De Souza committed
260
#endif
261 262 263 264 265 266 267 268 269 270
  }

  /// Resource mapping
  a = (config.rf_config.tx_antenna_ports.value == 1) ? amp : (amp*ONE_OVER_SQRT2_Q15)>>15;

  for (int aa = 0; aa < config.rf_config.tx_antenna_ports.value; aa++)
  {
    if (cset_start_sc >= frame_parms.ofdm_symbol_size)
      cset_start_sc -= frame_parms.ofdm_symbol_size;

Guy De Souza's avatar
Guy De Souza committed
271 272 273 274 275 276 277 278 279 280

    /*Reorder REG list for a freq first mapping*/
    uint8_t symb_idx[NR_MAX_CSET_DURATION] = {0,0,0};
    uint8_t nb_regs = dci_alloc.L*NR_NB_REG_PER_CCE;
    uint8_t regs_per_symb = nb_regs/cset_nsymb;
    for (int cce_idx=0; cce_idx<dci_alloc.L; cce_idx++){
      cce = dci_alloc.cce_list[cce_idx];
      for (int reg_idx=0; reg_idx<NR_NB_REG_PER_CCE; reg_idx++) {
        reg = cce.reg_list[reg_idx];
        reg_mapping_list[reg.symb_idx*regs_per_symb + symb_idx[reg.symb_idx]++] = reg;
281
      }
Guy De Souza's avatar
Guy De Souza committed
282
    }
Guy De Souza's avatar
Guy De Souza committed
283
#ifdef DEBUG_DCI
284 285 286 287
    printf("\n Ordered REG list:\n");
    for (int i=0; i<nb_regs; i++)
      printf("%d\t",reg_mapping_list[i].reg_idx );
    printf("\n");
Guy De Souza's avatar
Guy De Souza committed
288
#endif
289
    
Guy De Souza's avatar
Guy De Souza committed
290 291 292 293 294 295 296 297
    if (pdcch_params.precoder_granularity == NFAPI_NR_CSET_ALL_CONTIGUOUS_RBS) {
    /*in this case the DMRS are mapped on all the coreset*/
      for (l=cset_start_symb; l<cset_start_symb+ cset_nsymb; l++) {
        dmrs_idx = 0;
        k = cset_start_sc + 1;
        while (dmrs_idx<3*pdcch_params.n_rb) {
          ((int16_t*)txdataF[aa])[(l*frame_parms.ofdm_symbol_size + k)<<1] = (a * mod_dmrs[l][dmrs_idx<<1]) >> 15;
          ((int16_t*)txdataF[aa])[((l*frame_parms.ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[l][(dmrs_idx<<1) + 1]) >> 15;
298 299 300
#ifdef DEBUG_PDCCH_DMRS
	  printf("symbol %d position %d => (%d,%d)\n",l,k,((int16_t*)txdataF[aa])[(l*frame_parms.ofdm_symbol_size + k)<<1] , ((int16_t*)txdataF[aa])[((l*frame_parms.ofdm_symbol_size + k)<<1)+1]); 
#endif
Guy De Souza's avatar
Guy De Souza committed
301 302
          k+=4;
          if (k >= frame_parms.ofdm_symbol_size)
303
            k -= frame_parms.ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
          dmrs_idx++;
        }
      }    
    }

    /*Now mapping the encoded DCI based on newly constructed REG list
     * and the DMRS for the precoder granularity same as REG bundle*/
    for (int reg_idx=0; reg_idx<nb_regs; reg_idx++) {
      reg = reg_mapping_list[reg_idx];
      k = cset_start_sc + reg.start_sc_idx;
      if (k >= frame_parms.ofdm_symbol_size)
          k -= frame_parms.ofdm_symbol_size;
      l = cset_start_symb + reg.symb_idx;
      dmrs_idx = (reg.reg_idx/cset_nsymb)*3;
      k_prime = 0;
      for (int m=0; m<NR_NB_SC_PER_RB; m++) {
        if ( m == (k_prime<<2)+1) { // DMRS if not already mapped
          if (pdcch_params.precoder_granularity == NFAPI_NR_CSET_SAME_AS_REG_BUNDLE) {
322 323
            ((int16_t*)txdataF[aa])[(l*frame_parms.ofdm_symbol_size + k)<<1] = (a * mod_dmrs[l][dmrs_idx<<1]) >> 15;
            ((int16_t*)txdataF[aa])[((l*frame_parms.ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[l][(dmrs_idx<<1) + 1]) >> 15;
324 325 326
#ifdef DEBUG_PDCCH_DMRS
	    printf("l %d position %d => (%d,%d)\n",l,k,((int16_t*)txdataF[aa])[(l*frame_parms.ofdm_symbol_size + k)<<1] , ((int16_t*)txdataF[aa])[((l*frame_parms.ofdm_symbol_size + k)<<1)+1]); 
#endif
327 328 329
            k_prime++;
            dmrs_idx++;
          }
330
        }
Guy De Souza's avatar
Guy De Souza committed
331 332 333
        else { // DCI payload
          ((int16_t*)txdataF[aa])[(l*frame_parms.ofdm_symbol_size + k)<<1] = (a * mod_dci[dci_idx<<1]) >> 15;
          ((int16_t*)txdataF[aa])[((l*frame_parms.ofdm_symbol_size + k)<<1) + 1] = (a * mod_dci[(dci_idx<<1) + 1]) >> 15;
Hongzhi's avatar
Hongzhi committed
334
          //printf("dci output %d %d\n",(a * mod_dci[dci_idx<<1]) >> 15, (a * mod_dci[(dci_idx<<1) + 1]) >> 15);
Guy De Souza's avatar
Guy De Souza committed
335 336 337 338 339
          dci_idx++;
        }
        k++;
        if (k >= frame_parms.ofdm_symbol_size)
          k -= frame_parms.ofdm_symbol_size;
340 341 342
      }
    }
  }
343

Guy De Souza's avatar
Guy De Souza committed
344 345 346 347
#ifdef DEBUG_DCI
  write_output("txdataF_dci.m", "txdataF_dci", txdataF[0], frame_parms.samples_per_frame_wCP>>1, 1, 1);
#endif

348 349
  return 0;
}