nr_ulsch_coding.c 15.5 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
/*
 * 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"
39
#include "PHY/CODING/nrLDPC_extern.h"
40 41
#include "PHY/NR_UE_TRANSPORT/nr_transport_ue.h"
#include "common/utils/LOG/vcd_signal_dumper.h"
42
#include "LAYER2/NR_MAC_gNB/mac_proto.h"
43 44


45 46
void free_nr_ue_ulsch(NR_UE_ULSCH_t **ulschptr,unsigned char N_RB_UL)

47
{
yilmazt's avatar
yilmazt committed
48
  int i, r;
49
  NR_UE_ULSCH_t *ulsch = *ulschptr;
50 51 52 53 54 55

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

56 57 58 59 60 61 62 63 64
  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

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

        if (ulsch->harq_processes[i]->a) {
69
          free16(ulsch->harq_processes[i]->a,ulsch_bytes);
70 71
          ulsch->harq_processes[i]->a = NULL;
        }
72
        if (ulsch->harq_processes[i]->b) {
73
          free16(ulsch->harq_processes[i]->b,ulsch_bytes);
74 75
          ulsch->harq_processes[i]->b = NULL;
        }
76 77 78 79 80 81 82 83 84
        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++) {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
          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));
102
    *ulschptr = NULL;
103 104 105 106
  }

}

Ahmed's avatar
Ahmed committed
107

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

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

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

  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

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

134 135 136 137 138 139 140
    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));
141 142
        ulsch->harq_processes[i]->b = (uint8_t*)malloc16(ulsch_bytes);
        ulsch->harq_processes[i]->a = (unsigned char*)malloc16(ulsch_bytes);
143 144

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

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

        if (abstraction_flag==0) {
159
          for (r=0; r<a_segments; r++) {
160 161 162
            // 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
163

164 165 166 167 168 169 170 171 172 173 174 175 176
            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;
            }
          }
177 178 179 180 181 182 183 184 185 186 187 188 189 190
          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;
          }
191 192 193 194 195 196 197 198 199 200
        }

        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;
      }
    }

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

      return(ulsch);
207
    }
208 209 210
  }

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


215 216 217 218
}


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

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

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


/////////////////////////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
253
  nb_symb_sch = harq_process->number_of_symbols;
254 255
  A = harq_process->TBS;
  pz = &Z;
256
  mod_order = nr_get_Qm_ul(harq_process->mcs,0);
Sakthivel Velumani's avatar
Sakthivel Velumani committed
257
  R = nr_get_code_rate_ul(harq_process->mcs, 0);
258 259 260
  Kr=0;
  r_offset=0;
  BG = 1;
261
  F=0;
262 263 264
  Ilbrm = 0;
  Tbslbrm = 950984; //max tbs
  nb_re_dmrs = ulsch->nb_re_dmrs;
265
  length_dmrs = ulsch->length_dmrs;
266
  Coderate = 0.0;
267 268 269 270

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

271

272 273
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_IN);

274
  LOG_D(PHY,"ulsch coding nb_rb %d nb_symb_sch %d nb_re_dmrs %d, length_dmrs %d, harq_process->Nl = %d\n", nb_rb,nb_symb_sch, nb_re_dmrs,length_dmrs, harq_process->Nl);
275 276 277


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

  //  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;
290
    printf("ulsch (tx): \n");
291 292 293 294
    for (i=0;i<(A>>3);i++)
      printf("%02x.",a[i]);
    printf("\n");
    */
295 296 297 298 299 300 301 302
    if (A > 3824) {
      // 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)]);
303

304
      harq_process->B = A+24;
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      AssertFatal((A/8)+4 <= MAX_NR_ULSCH_PAYLOAD_BYTES,"A %d is too big (A/8+4 = %d > %d)\n",A,(A/8)+4,MAX_NR_ULSCH_PAYLOAD_BYTES);

      memcpy(harq_process->b,harq_process->a,(A/8)+4);
    }
    else {
      // Add 16-bit crc (polynomial A) to payload
      crc = crc16(harq_process->a,A)>>16;
      harq_process->a[A>>3] = ((uint8_t*)&crc)[1];
      harq_process->a[1+(A>>3)] = ((uint8_t*)&crc)[0];
      //printf("CRC %x (A %d)\n",crc,A);
      //printf("a0 %d a1 %d \n", a[A>>3], a[1+(A>>3)]);

      harq_process->B = A+16;

      AssertFatal((A/8)+3 <= MAX_NR_ULSCH_PAYLOAD_BYTES,"A %d is too big (A/8+3 = %d > %d)\n",A,(A/8)+3,MAX_NR_ULSCH_PAYLOAD_BYTES);

      memcpy(harq_process->b,harq_process->a,(A/8)+3);  // using 3 bytes to mimic the case of 24 bit crc
    }
324 325 326 327 328 329 330

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

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

Sakthivel Velumani's avatar
Sakthivel Velumani committed
331 332 333 334
    if (R<1024)
      Coderate = (float) R /(float) 1024;
    else
      Coderate = (float) R /(float) 2048;
335 336 337 338 339 340 341

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

Francesco Mani's avatar
Francesco Mani committed
343 344 345 346 347 348 349 350
    Kb=nr_segmentation(harq_process->b,
                       harq_process->c,
                       harq_process->B,
                       &harq_process->C,
                       &harq_process->K,
                       pz,
                       &harq_process->F,
                       BG);
351 352

    F = harq_process->F;
353
    Kr = harq_process->K;
354 355
#ifdef DEBUG_DLSCH_CODING
    uint16_t Kr_bytes;
356
    Kr_bytes = Kr>>3;
357
#endif
358 359 360 361

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

362
    //printf("segment Z %d k %d Kr %d BG %d\n", *pz,harq_process->K,Kr,BG);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

    //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]);

384 385 386 387 388 389
    int temp_opp = 0;

    if (opp_enabled) {
      opp_enabled = 0;
      temp_opp = 1;
    }
390 391 392 393 394 395 396


    /*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");*/
397 398 399
    encoder_implemparams_t impp;
    impp.n_segments=harq_process->C;
    impp.macro_num=0;
400

401
    nrLDPC_encoder(harq_process->c,harq_process->d,*pz,Kb,Kr,BG,&impp);
402 403 404 405

    //stop_meas(te_stats);
    //printf("end ldpc encoder -- output\n");
#ifdef DEBUG_DLSCH_CODING
406 407
    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);
408 409
#endif

410 411
    if (temp_opp) opp_enabled = 1;

412 413 414 415 416 417
///////////
///////////////////////////////////////////////////////////////////////////////

  }

  for (r=0; r<harq_process->C; r++) { // looping over C segments
418 419 420 421 422 423 424 425 426

    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]);
            }
    }

427
#ifdef DEBUG_DLSCH_CODING
yilmazt's avatar
yilmazt committed
428
    printf("Rate Matching, Code segment %d (coded bits (G) %u, unpunctured/repeated bits per code segment %d, mod_order %d, nb_rb %d)...\n",
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
        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);

445 446
    Tbslbrm = nr_compute_tbslbrm(0,nb_rb,harq_process->Nl,harq_process->C);

447 448 449 450 451 452 453
    nr_rate_matching_ldpc(Ilbrm,
                          Tbslbrm,
                          BG,
                          *pz,
                          harq_process->d[r],
                          harq_process->e+r_offset,
                          harq_process->C,
454 455
			  F,
                          Kr-F-2*(*pz),
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
                          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;

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

  }

498
  memcpy(ulsch->g,harq_process->f,G); // g is the concatenated code block
499 500 501 502 503

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_ENB_DLSCH_ENCODING, VCD_FUNCTION_OUT);

  return(0);
}