nr_ulsch_coding.c 14.6 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 38 39 40 41 42 43 44
/*
 * 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_UE_TRANSPORT/nr_ulsch_coding.c
* \brief Top-level routines for coding the ULSCH transport channel as described in 38.212 V15.4 2018-12
* \author Khalid Ahmed
* \date 2019
* \version 0.1
* \company Fraunhofer IIS
* \email: khalid.ahmed@iis.fraunhofer.de
* \note
* \warning
*/

#include "PHY/defs_UE.h"
#include "PHY/phy_extern_ue.h"
#include "PHY/NR_UE_TRANSPORT/nr_transport_proto_ue.h"
#include "PHY/CODING/coding_defs.h"
#include "PHY/CODING/coding_extern.h"
#include "PHY/CODING/lte_interleaver_inline.h"
#include "PHY/CODING/nrLDPC_encoder/defs.h"
#include "PHY/NR_UE_TRANSPORT/nr_transport_ue.h"
#include "common/utils/LOG/vcd_signal_dumper.h"



45
void free_nr_ue_ulsch(NR_UE_ULSCH_t *ulsch,unsigned char N_RB_UL)
46
{
yilmazt's avatar
yilmazt committed
47
  int i, r;
48 49 50 51 52 53

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

54 55 56 57 58 59 60 61 62
  uint16_t a_segments = MAX_NUM_NR_ULSCH_SEGMENTS;  //number of segments to be allocated

  if (N_RB_UL != 273) {
    a_segments = a_segments*N_RB_UL;
    a_segments = a_segments/273;
  }  

  uint16_t ulsch_bytes = a_segments*1056;  // allocated bytes per segment

63 64
    for (i=0; i<NR_MAX_ULSCH_HARQ_PROCESSES; i++) {
      if (ulsch->harq_processes[i]) {
65 66

        if (ulsch->harq_processes[i]->a) {
67
          free16(ulsch->harq_processes[i]->a,ulsch_bytes);
68 69
          ulsch->harq_processes[i]->a = NULL;
        }
70
        if (ulsch->harq_processes[i]->b) {
71
          free16(ulsch->harq_processes[i]->b,ulsch_bytes);
72 73
          ulsch->harq_processes[i]->b = NULL;
        }
74 75 76 77 78 79 80 81 82
        if (ulsch->harq_processes[i]->e) {
          free16(ulsch->harq_processes[i]->e,14*N_RB_UL*12*8);
          ulsch->harq_processes[i]->e = NULL;
        }
        if (ulsch->harq_processes[i]->f) {
          free16(ulsch->harq_processes[i]->f,14*N_RB_UL*12*8);
          ulsch->harq_processes[i]->f = NULL;
        }
        for (r=0; r<a_segments; r++) {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
          if (ulsch->harq_processes[i]->c[r]) {
            free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 3+768);
            ulsch->harq_processes[i]->c[r] = NULL;
          }

          if (ulsch->harq_processes[i]->d[r]) {
            free16(ulsch->harq_processes[i]->d[r],68*384);
            ulsch->harq_processes[i]->d[r] = NULL;
          }

        }

        free16(ulsch->harq_processes[i],sizeof(NR_UL_UE_HARQ_t));
        ulsch->harq_processes[i] = NULL;
      }
    }
    free16(ulsch,sizeof(NR_UE_ULSCH_t));
    ulsch = NULL;
  }

}

Ahmed's avatar
Ahmed committed
105

yilmazt's avatar
yilmazt committed
106 107 108
NR_UE_ULSCH_t *new_nr_ue_ulsch(unsigned char N_RB_UL,
                               int number_of_harq_pids,
                               uint8_t abstraction_flag)
109 110
{
  NR_UE_ULSCH_t *ulsch;
111
  unsigned char exit_flag = 0,i,r;
112
  uint16_t a_segments = MAX_NUM_NR_ULSCH_SEGMENTS;  //number of segments to be allocated
113

Francesco Mani's avatar
Francesco Mani committed
114 115
  if (N_RB_UL != 273) {
    a_segments = a_segments*N_RB_UL;
116 117
    a_segments = a_segments/273;
  }  
118

119
  uint16_t ulsch_bytes = a_segments*1056;  // allocated bytes per segment
120 121 122 123 124 125 126 127 128

  ulsch = (NR_UE_ULSCH_t *)malloc16(sizeof(NR_UE_ULSCH_t));

  if (ulsch) {
    memset(ulsch,0,sizeof(NR_UE_ULSCH_t));

    ulsch->number_harq_processes_for_pusch = NR_MAX_ULSCH_HARQ_PROCESSES;
    ulsch->Mlimit = 4; // maximum harq retransmissions

129 130 131
    //for (i=0; i<10; i++)
      //ulsch->harq_ids[i] = 0;

132 133 134 135 136 137 138
    for (i=0; i<number_of_harq_pids; i++) {

      ulsch->harq_processes[i] = (NR_UL_UE_HARQ_t *)malloc16(sizeof(NR_UL_UE_HARQ_t));

      //      printf("ulsch->harq_processes[%d] %p\n",i,ulsch->harq_processes[i]);
      if (ulsch->harq_processes[i]) {
        memset(ulsch->harq_processes[i], 0, sizeof(NR_UL_UE_HARQ_t));
139 140
        ulsch->harq_processes[i]->b = (uint8_t*)malloc16(ulsch_bytes);
        ulsch->harq_processes[i]->a = (unsigned char*)malloc16(ulsch_bytes);
141 142

        if (ulsch->harq_processes[i]->a) {
143
          bzero(ulsch->harq_processes[i]->a,ulsch_bytes);
144 145 146 147 148 149
        } else {
          printf("Can't allocate PDU\n");
          exit_flag=1;
        }

        if (ulsch->harq_processes[i]->b)
150
          bzero(ulsch->harq_processes[i]->b,ulsch_bytes);
151 152 153 154 155 156
        else {
          LOG_E(PHY,"Can't get b\n");
          exit_flag=1;
        }

        if (abstraction_flag==0) {
157
          for (r=0; r<a_segments; r++) {
158 159 160
            // account for filler in first segment and CRCs for multiple segment case
            ulsch->harq_processes[i]->c[r] = (uint8_t*)malloc16(8448);
            ulsch->harq_processes[i]->d[r] = (uint8_t*)malloc16(68*384); //max size for coded output
161

162 163 164 165 166 167 168 169 170 171 172 173 174
            if (ulsch->harq_processes[i]->c[r]) {
              bzero(ulsch->harq_processes[i]->c[r],8448);
            } else {
              printf("Can't get c\n");
              exit_flag=2;
            }
            if (ulsch->harq_processes[i]->d[r]) {
              bzero(ulsch->harq_processes[i]->d[r],(68*384));
            } else {
              printf("Can't get d\n");
              exit_flag=2;
            }
          }
175 176 177 178 179 180 181 182 183 184 185 186 187 188
          ulsch->harq_processes[i]->e = (uint8_t*)malloc16(14*N_RB_UL*12*8);
          if (ulsch->harq_processes[i]->e) {
            bzero(ulsch->harq_processes[i]->e,14*N_RB_UL*12*8);
          } else {
            printf("Can't get e\n");
            exit_flag=1;
          }
          ulsch->harq_processes[i]->f = (uint8_t*)malloc16(14*N_RB_UL*12*8);
          if (ulsch->harq_processes[i]->f) {
            bzero(ulsch->harq_processes[i]->f,14*N_RB_UL*12*8);
          } else {
            printf("Can't get f\n");
            exit_flag=1;
          }
189 190 191 192 193 194 195 196 197 198
        }

        ulsch->harq_processes[i]->subframe_scheduling_flag = 0;
        ulsch->harq_processes[i]->first_tx = 1;
      } else {
        LOG_E(PHY,"Can't get harq_p %d\n",i);
        exit_flag=3;
      }
    }

199 200 201 202
    if (exit_flag==0) {
      for (i=0; i<number_of_harq_pids; i++) {
        ulsch->harq_processes[i]->round=0;
      }
203 204

      return(ulsch);
205
    }
206 207 208
  }

  LOG_E(PHY,"new_ue_ulsch exit flag, size of  %d ,   %zu\n",exit_flag, sizeof(LTE_UE_ULSCH_t));
209
  free_nr_ue_ulsch(ulsch,N_RB_UL);
210 211 212
  return(NULL);


213 214 215 216
}


int nr_ulsch_encoding(NR_UE_ULSCH_t *ulsch,
yilmazt's avatar
yilmazt committed
217 218
                      NR_DL_FRAME_PARMS* frame_parms,
                      uint8_t harq_pid)
219 220 221 222 223 224 225 226
{
/////////////////////////parameters and variables declaration/////////////////////////
///////////

  unsigned int G,crc;
  NR_UL_UE_HARQ_t *harq_process; 
  uint16_t nb_rb ;
  uint8_t nb_symb_sch ;
227
  uint32_t A, Z, F;
228 229
  uint32_t *pz; 
  uint8_t mod_order; 
230
  uint16_t Kr,r,r_offset;
231
  uint8_t BG;
Francesco Mani's avatar
Francesco Mani committed
232
  uint32_t E,Kb;
233 234 235 236
  uint8_t Ilbrm; 
  uint32_t Tbslbrm; 
  uint8_t nb_re_dmrs; 
  uint16_t length_dmrs;
Sakthivel Velumani's avatar
Sakthivel Velumani committed
237
  uint16_t R;
238
  float Coderate;
239 240 241 242 243 244 245 246 247 248 249

///////////
///////////////////////////////////////////////////////////////////////////////////////


/////////////////////////parameters and variables initialization/////////////////////////
///////////

  crc = 1;
  harq_process = ulsch->harq_processes[harq_pid];
  nb_rb = harq_process->nb_rb;
Khalid Ahmed's avatar
Khalid Ahmed committed
250
  nb_symb_sch = harq_process->number_of_symbols;
251 252
  A = harq_process->TBS;
  pz = &Z;
253
  mod_order = nr_get_Qm_ul(harq_process->mcs,0);
Sakthivel Velumani's avatar
Sakthivel Velumani committed
254
  R = nr_get_code_rate_ul(harq_process->mcs, 0);
255 256 257
  Kr=0;
  r_offset=0;
  BG = 1;
258
  F=0;
259 260 261 262
  Ilbrm = 0;
  Tbslbrm = 950984; //max tbs
  nb_re_dmrs = ulsch->nb_re_dmrs;
  length_dmrs = 1;
263
  Coderate = 0.0;
264 265 266 267

///////////
/////////////////////////////////////////////////////////////////////////////////////////  

268

269 270
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_IN);

271
  LOG_D(PHY,"ulsch coding nb_rb %d nb_symb_sch %d nb_re_dmrs %d, length_dmrs %d\n", nb_rb,nb_symb_sch, nb_re_dmrs,length_dmrs);
272 273 274


  G = nr_get_G(nb_rb, nb_symb_sch, nb_re_dmrs, length_dmrs,mod_order,harq_process->Nl);
275
  LOG_D(PHY,"ulsch coding A %d G %d mod_order %d\n", A,G, mod_order);
276 277 278 279 280 281 282 283 284 285 286

  //  if (harq_process->Ndi == 1) {  // this is a new packet
  if (harq_process->round == 0) {  // this is a new packet
#ifdef DEBUG_DLSCH_CODING
  printf("encoding thinks this is a new packet \n");
#endif

///////////////////////// a---->| add CRC |---->b /////////////////////////
///////////
    /*
    int i;
287
    printf("ulsch (tx): \n");
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    for (i=0;i<(A>>3);i++)
      printf("%02x.",a[i]);
    printf("\n");
    */
    // Add 24-bit crc (polynomial A) to payload
    crc = crc24a(harq_process->a,A)>>8;
    harq_process->a[A>>3] = ((uint8_t*)&crc)[2];
    harq_process->a[1+(A>>3)] = ((uint8_t*)&crc)[1];
    harq_process->a[2+(A>>3)] = ((uint8_t*)&crc)[0];
    //printf("CRC %x (A %d)\n",crc,A);
    //printf("a0 %d a1 %d a2 %d\n", a[A>>3], a[1+(A>>3)], a[2+(A>>3)]);

    harq_process->B = A+24;

    memcpy(harq_process->b,harq_process->a,(A/8)+4);

///////////
///////////////////////////////////////////////////////////////////////////

///////////////////////// b---->| block segmentation |---->c /////////////////////////
///////////

Sakthivel Velumani's avatar
Sakthivel Velumani committed
310 311 312 313
    if (R<1024)
      Coderate = (float) R /(float) 1024;
    else
      Coderate = (float) R /(float) 2048;
314 315 316 317 318 319 320

    if ((A <=292) || ((A<=3824) && (Coderate <= 0.6667)) || Coderate <= 0.25){
      BG = 2;
    }
    else{
      BG = 1;
    }
321

Francesco Mani's avatar
Francesco Mani committed
322 323 324 325 326 327 328 329
    Kb=nr_segmentation(harq_process->b,
                       harq_process->c,
                       harq_process->B,
                       &harq_process->C,
                       &harq_process->K,
                       pz,
                       &harq_process->F,
                       BG);
330 331

    F = harq_process->F;
332
    Kr = harq_process->K;
333 334
#ifdef DEBUG_DLSCH_CODING
    uint16_t Kr_bytes;
335
    Kr_bytes = Kr>>3;
336
#endif
337 338 339 340

///////////
/////////////////////////////////////////////////////////////////////////////////////

341
opp_enabled=0;
342 343 344 345

///////////////////////// c---->| LDCP coding |---->d /////////////////////////
///////////

346
    //printf("segment Z %d k %d Kr %d BG %d\n", *pz,harq_process->K,Kr,BG);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

    //start_meas(te_stats);
    for (r=0; r<harq_process->C; r++) {
      //channel_input[r] = &harq_process->d[r][0];
#ifdef DEBUG_DLSCH_CODING
      printf("Encoder: B %d F %d \n",harq_process->B, harq_process->F);
      printf("start ldpc encoder segment %d/%d\n",r,harq_process->C);
      printf("input %d %d %d %d %d \n", harq_process->c[r][0], harq_process->c[r][1], harq_process->c[r][2],harq_process->c[r][3], harq_process->c[r][4]);
      for (int cnt =0 ; cnt < 22*(*pz)/8; cnt ++){
      printf("%d ", harq_process->c[r][cnt]);
      }
      printf("\n");

#endif
      //ldpc_encoder_orig((unsigned char*)harq_process->c[r],harq_process->d[r],Kr,BG,0);
      //ldpc_encoder_optim((unsigned char*)harq_process->c[r],(unsigned char*)&harq_process->d[r][0],Kr,BG,NULL,NULL,NULL,NULL);
    }

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



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

Francesco Mani's avatar
Francesco Mani committed
376
    ldpc_encoder_optim_8seg(harq_process->c,harq_process->d,*pz,Kb,Kr,BG,harq_process->C,NULL,NULL,NULL,NULL);
377 378 379 380 381 382 383 384 385 386 387 388 389 390

    //stop_meas(te_stats);
    //printf("end ldpc encoder -- output\n");
#ifdef DEBUG_DLSCH_CODING
      write_output("ulsch_enc_input0.m","enc_in0",&harq_process->c[0][0],Kr_bytes,1,4);
      write_output("ulsch_enc_output0.m","enc0",&harq_process->d[0][0],(3*8*Kr_bytes)+12,1,4);
#endif

///////////
///////////////////////////////////////////////////////////////////////////////

  }

  for (r=0; r<harq_process->C; r++) { // looping over C segments
391 392 393 394 395 396 397 398 399

    if (harq_process->F>0) {
            for (int k=(Kr-F-2*(*pz)); k<Kr-2*(*pz); k++) {
              harq_process->d[r][k] = NR_NULL;
              //if (k<(Kr-F+8))
              //printf("r %d filler bits [%d] = %d \n", r,k, harq_process->d[r][k]);
            }
    }

400
#ifdef DEBUG_DLSCH_CODING
yilmazt's avatar
yilmazt committed
401
    printf("Rate Matching, Code segment %d (coded bits (G) %u, unpunctured/repeated bits per code segment %d, mod_order %d, nb_rb %d)...\n",
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        r,
        G,
        Kr*3,
        mod_order,nb_rb);
#endif

    //start_meas(rm_stats);
#ifdef DEBUG_DLSCH_CODING
  printf("rvidx in encoding = %d\n", harq_process->rvidx);
#endif

///////////////////////// d---->| Rate matching bit selection |---->e /////////////////////////
///////////

    E = nr_get_E(G, harq_process->C, mod_order, harq_process->Nl, r);

418 419
    Tbslbrm = nr_compute_tbslbrm(0,nb_rb,harq_process->Nl,harq_process->C);

420 421 422 423 424 425 426
    nr_rate_matching_ldpc(Ilbrm,
                          Tbslbrm,
                          BG,
                          *pz,
                          harq_process->d[r],
                          harq_process->e+r_offset,
                          harq_process->C,
427 428
			  F,
                          Kr-F-2*(*pz),
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
                          harq_process->rvidx,
                          E);



#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
      printf("output ratematching e[%d]= %d r_offset %d\n", i,harq_process->e[i+r_offset], r_offset);
#endif

///////////
///////////////////////////////////////////////////////////////////////////////////////////////

    
///////////////////////// e---->| Rate matching bit interleaving |---->f /////////////////////////
///////////

    //stop_meas(rm_stats);

    //start_meas(i_stats);
  nr_interleaving_ldpc(E,
            mod_order,
            harq_process->e+r_offset,
            harq_process->f+r_offset);
    //stop_meas(i_stats);


#ifdef DEBUG_DLSCH_CODING
    for (int i =0; i<16; i++)
      printf("output interleaving f[%d]= %d r_offset %d\n", i,harq_process->f[i+r_offset], r_offset);

    if (r==harq_process->C-1)
      write_output("enc_output.m","enc",harq_process->f,G,1,4);
#endif

    r_offset += E;

///////////
///////////////////////////////////////////////////////////////////////////////////////////////

  }

471
  memcpy(ulsch->g,harq_process->f,G); // g is the concatenated code block
472 473 474 475 476

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_OUT);

  return(0);
}