nr_dlsch_coding.c 15.7 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * 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.0  (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/LTE_TRANSPORT/dlsch_coding.c
* \brief Top-level routines for implementing LDPC-coded (DLSCH) transport channels from 38-212, 15.2
* \author H.Wang
* \date 2018
* \version 0.1
* \company Eurecom
* \email:
* \note
* \warning
*/

#include "PHY/defs_gNB.h"
#include "PHY/phy_extern.h"
#include "PHY/CODING/coding_extern.h"
#include "PHY/CODING/coding_defs.h"
#include "PHY/CODING/lte_interleaver_inline.h"
38
#include "PHY/CODING/nrLDPC_encoder/defs.h"
39 40
#include "PHY/NR_TRANSPORT/nr_transport.h"
#include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
41
#include "PHY/NR_TRANSPORT/nr_dlsch.h"
42 43
#include "SCHED_NR/sched_nr.h"
#include "defs.h"
44 45
#include "common/utils/LOG/vcd_signal_dumper.h"
#include "common/utils/LOG/log.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <syscall.h>

//#define DEBUG_DLSCH_CODING
//#define DEBUG_DLSCH_FREE 1

void free_gNB_dlsch(NR_gNB_DLSCH_t *dlsch)
{
  int i;
  int r;

  if (dlsch) {
#ifdef DEBUG_DLSCH_FREE
    printf("Freeing dlsch %p\n",dlsch);
#endif

    for (i=0; i<dlsch->Mdlharq; i++) {
#ifdef DEBUG_DLSCH_FREE
      printf("Freeing dlsch process %d\n",i);
#endif

      if (dlsch->harq_processes[i]) {
#ifdef DEBUG_DLSCH_FREE
        printf("Freeing dlsch process %d (%p)\n",i,dlsch->harq_processes[i]);
#endif

        if (dlsch->harq_processes[i]->b) {
72
          free16(dlsch->harq_processes[i]->b,MAX_DLSCH_PAYLOAD_BYTES); //this should be MAX_NR_DLSCH_PAYLOAD_BYTES
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
          dlsch->harq_processes[i]->b = NULL;
#ifdef DEBUG_DLSCH_FREE
          printf("Freeing dlsch process %d b (%p)\n",i,dlsch->harq_processes[i]->b);
#endif
        }

#ifdef DEBUG_DLSCH_FREE
        printf("Freeing dlsch process %d c (%p)\n",i,dlsch->harq_processes[i]->c);
#endif

        for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++) {

#ifdef DEBUG_DLSCH_FREE
          printf("Freeing dlsch process %d c[%d] (%p)\n",i,r,dlsch->harq_processes[i]->c[r]);
#endif

          if (dlsch->harq_processes[i]->c[r]) {
90
            free16(dlsch->harq_processes[i]->c[r],1056);
91 92 93
            dlsch->harq_processes[i]->c[r] = NULL;
          }
          if (dlsch->harq_processes[i]->d[r]) {
94
            free16(dlsch->harq_processes[i]->d[r],3*8448);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
            dlsch->harq_processes[i]->d[r] = NULL;
          }

	}
	free16(dlsch->harq_processes[i],sizeof(NR_DL_gNB_HARQ_t));
	dlsch->harq_processes[i] = NULL;
      }
    }

    free16(dlsch,sizeof(NR_gNB_DLSCH_t));
    dlsch = NULL;
    }

}

110 111 112 113 114 115
NR_gNB_DLSCH_t *new_gNB_dlsch(unsigned char Kmimo,
                              unsigned char Mdlharq,
                              uint32_t Nsoft,
                              uint8_t abstraction_flag,
                              NR_DL_FRAME_PARMS *frame_parms,
                              nfapi_nr_config_request_t *config)
116 117 118
{

  NR_gNB_DLSCH_t *dlsch;
119
  unsigned char exit_flag = 0,i,r,aa,layer;
120 121
  int re;
  unsigned char bw_scaling =1;
122
  uint16_t N_RB = config->rf_config.dl_carrier_bandwidth.value;
123

124
  switch (N_RB) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

  case 106:
    bw_scaling =2;
    break;

  default:
    bw_scaling =1;
    break;
  }

  dlsch = (NR_gNB_DLSCH_t *)malloc16(sizeof(NR_gNB_DLSCH_t));

  if (dlsch) {
    bzero(dlsch,sizeof(NR_gNB_DLSCH_t));
    dlsch->Kmimo = Kmimo;
    dlsch->Mdlharq = Mdlharq;
    dlsch->Mlimit = 4;
    dlsch->Nsoft = Nsoft;

144
    for (layer=0; layer<NR_MAX_NB_LAYERS; layer++) {
145
      dlsch->ue_spec_bf_weights[layer] = (int32_t**)malloc16(64*sizeof(int32_t*));
146

147
       for (aa=0; aa<64; aa++) {
148 149 150 151 152
         dlsch->ue_spec_bf_weights[layer][aa] = (int32_t *)malloc16(OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES*sizeof(int32_t));
         for (re=0;re<OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES; re++) {
           dlsch->ue_spec_bf_weights[layer][aa][re] = 0x00007fff;
         }
       }
153

154
      dlsch->txdataF[layer] = (int32_t *)malloc16((NR_MAX_PDSCH_ENCODED_LENGTH/NR_MAX_NB_LAYERS)*sizeof(int32_t)); // NR_MAX_NB_LAYERS is already included in NR_MAX_PDSCH_ENCODED_LENGTH
155
    }
156

157
    for (int q=0; q<NR_MAX_NB_CODEWORDS; q++)
158
      dlsch->mod_symbs[q] = (int32_t *)malloc16(NR_MAX_PDSCH_ENCODED_LENGTH*sizeof(int32_t));
159

160 161
     dlsch->calib_dl_ch_estimates = (int32_t**)malloc16(64*sizeof(int32_t*));
     for (aa=0; aa<64; aa++) {
162 163 164 165
       dlsch->calib_dl_ch_estimates[aa] = (int32_t *)malloc16(OFDM_SYMBOL_SIZE_COMPLEX_SAMPLES*sizeof(int32_t));

     }

166 167 168 169
    for (i=0; i<20; i++) {
      dlsch->harq_ids[0][i] = 0;
      dlsch->harq_ids[1][i] = 0;
    }
Guy De Souza's avatar
Guy De Souza committed
170

171 172 173
    for (i=0; i<Mdlharq; i++) {
      dlsch->harq_processes[i] = (NR_DL_gNB_HARQ_t *)malloc16(sizeof(NR_DL_gNB_HARQ_t));
      LOG_T(PHY, "Required mem size %d (bw scaling %d), dlsch->harq_processes[%d] %p\n",
174
    		  MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling,bw_scaling, i,dlsch->harq_processes[i]);
175 176 177 178

      if (dlsch->harq_processes[i]) {
        bzero(dlsch->harq_processes[i],sizeof(NR_DL_gNB_HARQ_t));
        //    dlsch->harq_processes[i]->first_tx=1;
179
        dlsch->harq_processes[i]->b = (unsigned char*)malloc16(MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling);
Guy De Souza's avatar
Guy De Souza committed
180 181 182
        dlsch->harq_processes[i]->pdu = (uint8_t*)malloc16(MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling);
        if (dlsch->harq_processes[i]->pdu) {
          bzero(dlsch->harq_processes[i]->pdu,MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling);
183
          nr_emulate_dlsch_payload(dlsch->harq_processes[i]->pdu, (MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling)>>3);
Guy De Souza's avatar
Guy De Souza committed
184 185 186 187
        } else {
          printf("Can't allocate PDU\n");
          exit_flag=1;
        }
188 189

        if (dlsch->harq_processes[i]->b) {
190
          bzero(dlsch->harq_processes[i]->b,MAX_NR_DLSCH_PAYLOAD_BYTES/bw_scaling);
191 192 193 194 195 196 197 198
        } else {
          printf("Can't get b\n");
          exit_flag=1;
        }

        if (abstraction_flag==0) {
          for (r=0; r<MAX_NUM_NR_DLSCH_SEGMENTS/bw_scaling; r++) {
            // account for filler in first segment and CRCs for multiple segment case
Ahmed's avatar
Ahmed committed
199 200 201
            // [hna] 8448 is the maximum CB size in NR
            //       68*348 = 68*(maximum size of Zc)
            //       In section 5.3.2 in 38.212, the for loop is up to N + 2*Zc (maximum size of N is 66*Zc, therefore 68*Zc)
202
            dlsch->harq_processes[i]->c[r] = (uint8_t*)malloc16(8448);
203
            dlsch->harq_processes[i]->d[r] = (uint8_t*)malloc16(68*384); //max size for coded output
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
            if (dlsch->harq_processes[i]->c[r]) {
              bzero(dlsch->harq_processes[i]->c[r],8448);
            } else {
              printf("Can't get c\n");
              exit_flag=2;
            }
            if (dlsch->harq_processes[i]->d[r]) {
              bzero(dlsch->harq_processes[i]->d[r],(3*8448));
            } else {
              printf("Can't get d\n");
              exit_flag=2;
            }
          }
        }
      } else {
        printf("Can't get harq_p %d\n",i);
        exit_flag=3;
      }
    }

    if (exit_flag==0) {
      for (i=0; i<Mdlharq; i++) {
        dlsch->harq_processes[i]->round=0;
      }

      return(dlsch);
    }
  }

233
  LOG_D(PHY,"new_gNB_dlsch exit flag %d, size of  %ld\n",
234 235
	exit_flag, sizeof(NR_gNB_DLSCH_t));
  free_gNB_dlsch(dlsch);
236
  return(NULL);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251


}

void clean_gNB_dlsch(NR_gNB_DLSCH_t *dlsch)
{

  unsigned char Mdlharq;
  unsigned char i,j,r;

  if (dlsch) {
    Mdlharq = dlsch->Mdlharq;
    dlsch->rnti = 0;
    dlsch->active = 0;

252 253 254 255
    for (i=0; i<10; i++) {
      dlsch->harq_ids[0][i] = Mdlharq;
      dlsch->harq_ids[1][i] = Mdlharq;
    }
256 257 258
    for (i=0; i<Mdlharq; i++) {
      if (dlsch->harq_processes[i]) {
        //  dlsch->harq_processes[i]->Ndi    = 0;
259
        //dlsch->harq_processes[i]->status = 0;
260 261 262 263 264 265 266 267 268 269 270 271
        dlsch->harq_processes[i]->round  = 0;

	for (j=0; j<96; j++)
	  for (r=0; r<MAX_NUM_DLSCH_SEGMENTS; r++)
	    if (dlsch->harq_processes[i]->d[r])
	      dlsch->harq_processes[i]->d[r][j] = NR_NULL;

      }
    }
  }
}

272
int nr_dlsch_encoding(unsigned char *a,int frame,
273
                     uint8_t slot,
Guy De Souza's avatar
Guy De Souza committed
274 275
                     NR_gNB_DLSCH_t *dlsch,
                     NR_DL_FRAME_PARMS* frame_parms)
276 277 278 279
{

  unsigned int G;
  unsigned int crc=1;
280 281
  uint8_t harq_pid = dlsch->harq_ids[frame&2][slot];
  AssertFatal(harq_pid<8 && harq_pid>=0,"illegal harq_pid %d\b",harq_pid);
Guy De Souza's avatar
Guy De Souza committed
282 283 284
  nfapi_nr_dl_config_dlsch_pdu_rel15_t rel15 = dlsch->harq_processes[harq_pid]->dlsch_pdu.dlsch_pdu_rel15;
  uint16_t nb_rb = rel15.n_prb;
  uint8_t nb_symb_sch = rel15.nb_symbols;
285
  uint32_t A, Z, F=0;
286
  uint32_t *pz = &Z;
Guy De Souza's avatar
Guy De Souza committed
287
  uint8_t mod_order = rel15.modulation_order;
288 289
  uint16_t Kr=0,r,r_offset=0;
  //uint8_t *d_tmp[MAX_NUM_DLSCH_SEGMENTS];
290
  uint8_t BG=1;
291 292
  uint32_t E;
  uint8_t Ilbrm = 0;
293
  uint32_t Tbslbrm = 950984; //max tbs
Guy De Souza's avatar
Guy De Souza committed
294
  uint8_t nb_re_dmrs = rel15.nb_re_dmrs;
295
  uint16_t length_dmrs = 1;
296
  float Coderate = 0.0;
297

298 299 300 301 302 303 304
  /*
  uint8_t *channel_input[MAX_NUM_DLSCH_SEGMENTS]; //unsigned char
  for(j=0;j<MAX_NUM_DLSCH_SEGMENTS;j++) {
    channel_input[j] = (unsigned char *)malloc16(sizeof(unsigned char) * 68*384);
  }
  */
  
305 306
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_IN);

Guy De Souza's avatar
Guy De Souza committed
307
  A = rel15.transport_block_size;
308

Guy De Souza's avatar
Guy De Souza committed
309
  G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs,mod_order,rel15.nb_layers);
310

311
  LOG_D(PHY,"dlsch coding A %d G %d mod_order %d\n", A,G, mod_order);
312
  
313

Guy De Souza's avatar
Guy De Souza committed
314
  Tbslbrm = nr_compute_tbs(28,nb_rb,frame_parms->symbols_per_slot,0,0, rel15.nb_layers);
315 316 317 318 319 320 321 322 323 324 325 326 327 328

  //  if (dlsch->harq_processes[harq_pid]->Ndi == 1) {  // this is a new packet
  if (dlsch->harq_processes[harq_pid]->round == 0) {  // this is a new packet
#ifdef DEBUG_DLSCH_CODING
  printf("encoding thinks this is a new packet \n");
#endif
    /*
    int i;
    printf("dlsch (tx): \n");
    for (i=0;i<(A>>3);i++)
      printf("%02x.",a[i]);
    printf("\n");
    */
    // Add 24-bit crc (polynomial A) to payload
329
    crc = crc24a(a,A)>>8;
330 331 332 333
    a[A>>3] = ((uint8_t*)&crc)[2];
    a[1+(A>>3)] = ((uint8_t*)&crc)[1];
    a[2+(A>>3)] = ((uint8_t*)&crc)[0];
    //printf("CRC %x (A %d)\n",crc,A);
334
    //printf("a0 %d a1 %d a2 %d\n", a[A>>3], a[1+(A>>3)], a[2+(A>>3)]);
335 336 337

    dlsch->harq_processes[harq_pid]->B = A+24;
    //    dlsch->harq_processes[harq_pid]->b = a;
338 339 340
   
    AssertFatal((A/8)+4 <= MAX_DLSCH_PAYLOAD_BYTES,"A %d is too big (A/8+4 = %d > %d)\n",A,(A/8)+4,MAX_DLSCH_PAYLOAD_BYTES);

341 342 343
    memcpy(dlsch->harq_processes[harq_pid]->b,a,(A/8)+4);

    nr_segmentation(dlsch->harq_processes[harq_pid]->b,
344 345 346 347
		    dlsch->harq_processes[harq_pid]->c,
		    dlsch->harq_processes[harq_pid]->B,
		    &dlsch->harq_processes[harq_pid]->C,
		    &dlsch->harq_processes[harq_pid]->K,
Ahmed's avatar
Ahmed committed
348
		    pz, // [hna] pz is Zc
349
		    &dlsch->harq_processes[harq_pid]->F);
350

351 352 353 354 355
    F = dlsch->harq_processes[harq_pid]->F;
    Coderate = (float) A /(float) G;

    if ((A <=292) || ((A<=3824) && (Coderate <= 0.6667)) || Coderate <= 0.25){
		BG = 2;
356 357
	}
	else{
358
		BG = 1;
359 360 361
	}

    Kr = dlsch->harq_processes[harq_pid]->K;
362 363
#ifdef DEBUG_DLSCH_CODING
    uint16_t Kr_bytes;
364
    Kr_bytes = Kr>>3;
365
#endif
366

367
    //printf("segment Z %d k %d Kr %d BG %d\n", *pz,dlsch->harq_processes[harq_pid]->K,Kr,BG);
Hongzhi Wang's avatar
Hongzhi Wang committed
368

369
    for (r=0; r<dlsch->harq_processes[harq_pid]->C; r++) {
370
      //d_tmp[r] = &dlsch->harq_processes[harq_pid]->d[r][0];
371
      //channel_input[r] = &dlsch->harq_processes[harq_pid]->d[r][0];
372
#ifdef DEBUG_DLSCH_CODING
373
      printf("Encoder: B %d F %d \n",dlsch->harq_processes[harq_pid]->B, dlsch->harq_processes[harq_pid]->F);
374 375
      printf("start ldpc encoder segment %d/%d\n",r,dlsch->harq_processes[harq_pid]->C);
      printf("input %d %d %d %d %d \n", dlsch->harq_processes[harq_pid]->c[r][0], dlsch->harq_processes[harq_pid]->c[r][1], dlsch->harq_processes[harq_pid]->c[r][2],dlsch->harq_processes[harq_pid]->c[r][3], dlsch->harq_processes[harq_pid]->c[r][4]);
376
      for (int cnt =0 ; cnt < 22*(*pz)/8; cnt ++){
377
      printf("%d ", dlsch->harq_processes[harq_pid]->c[r][cnt]);
378 379
      }
      printf("\n");
380 381

#endif
382 383
      //ldpc_encoder_orig((unsigned char*)dlsch->harq_processes[harq_pid]->c[r],dlsch->harq_processes[harq_pid]->d[r],Kr,BG,0);
      //ldpc_encoder_optim((unsigned char*)dlsch->harq_processes[harq_pid]->c[r],(unsigned char*)&dlsch->harq_processes[harq_pid]->d[r][0],Kr,BG,NULL,NULL,NULL,NULL);
384 385
    }

386 387
    //for (int i=0;i<68*384;i++)
      //        printf("channel_input[%d]=%d\n",i,channel_input[i]);
388 389


390 391 392 393 394 395 396

    /*printf("output %d %d %d %d %d \n", dlsch->harq_processes[harq_pid]->d[0][0], dlsch->harq_processes[harq_pid]->d[0][1], dlsch->harq_processes[harq_pid]->d[r][2],dlsch->harq_processes[harq_pid]->d[0][3], dlsch->harq_processes[harq_pid]->d[0][4]);
    	for (int cnt =0 ; cnt < 66*(*pz); cnt ++){
    	printf("%d \n",  dlsch->harq_processes[harq_pid]->d[0][cnt]);
    	}
    	printf("\n");*/

397 398
    //ldpc_encoder_optim_8seg(dlsch->harq_processes[harq_pid]->c,d_tmp,Kr,BG,dlsch->harq_processes[harq_pid]->C,NULL,NULL,NULL,NULL);
    ldpc_encoder_optim_8seg(dlsch->harq_processes[harq_pid]->c,dlsch->harq_processes[harq_pid]->d,Kr,BG,dlsch->harq_processes[harq_pid]->C,NULL,NULL,NULL,NULL);
399

400
    //printf("end ldpc encoder -- output\n");
401 402 403 404 405 406 407 408 409

#ifdef DEBUG_DLSCH_CODING
      write_output("enc_input0.m","enc_in0",&dlsch->harq_processes[harq_pid]->c[0][0],Kr_bytes,1,4);
      write_output("enc_output0.m","enc0",&dlsch->harq_processes[harq_pid]->d[0][0],(3*8*Kr_bytes)+12,1,4);
#endif

  }

  for (r=0; r<dlsch->harq_processes[harq_pid]->C; r++) {
410 411 412 413 414 415 416 417

	  	  if (dlsch->harq_processes[harq_pid]->F>0) {
	          for (int k=(Kr-F-2*(*pz)); k<Kr-2*(*pz); k++) {
	        	  dlsch->harq_processes[harq_pid]->d[r][k] = NR_NULL;
	        	  //if (k<(Kr-F+8))
	            //printf("r %d filler bits [%d] = %d \n", r,k, dlsch->harq_processes[harq_pid]->d[r][k]);
	          }
	  }
418 419 420 421 422 423 424 425 426
#ifdef DEBUG_DLSCH_CODING
    printf("Rate Matching, Code segment %d (coded bits (G) %d,unpunctured/repeated bits per code segment %d,mod_order %d, nb_rb %d)...\n",
        r,
        G,
        Kr*3,
        mod_order,nb_rb);
#endif

#ifdef DEBUG_DLSCH_CODING
427
  printf("rvidx in encoding = %d\n", rel15.redundancy_version);
428 429
#endif

430 431 432 433 434 435 436 437 438 439 440
    E = nr_get_E(G, dlsch->harq_processes[harq_pid]->C, mod_order, rel15.nb_layers, r);

    nr_rate_matching_ldpc(Ilbrm,
                          Tbslbrm,
                          BG,
                          *pz,
                          dlsch->harq_processes[harq_pid]->d[r],
                          dlsch->harq_processes[harq_pid]->e+r_offset,
                          dlsch->harq_processes[harq_pid]->C,
                          rel15.redundancy_version,
                          E);
441

442
    
443 444
#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
445
      printf("output ratematching e[%d]= %d r_offset %d\n", i,dlsch->harq_processes[harq_pid]->e[i+r_offset], r_offset);
446
#endif
447 448

	nr_interleaving_ldpc(E,
449
						mod_order,
450 451 452
						dlsch->harq_processes[harq_pid]->e+r_offset,
						dlsch->harq_processes[harq_pid]->f+r_offset);

453

454 455
#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
456
      printf("output interleaving f[%d]= %d r_offset %d\n", i,dlsch->harq_processes[harq_pid]->f[i+r_offset], r_offset);
457 458

    if (r==dlsch->harq_processes[harq_pid]->C-1)
459
      write_output("enc_output.m","enc",dlsch->harq_processes[harq_pid]->f,G,1,4);
460
#endif
461 462

    r_offset += E;
463 464 465 466
  }

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_OUT);

Khalid Ahmed's avatar
Khalid Ahmed committed
467
  return 0;
468
}