ulsch_decoding.c 42.2 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
    Copyright(c) 1999 - 2014 Eurecom
4

ghaddab's avatar
ghaddab committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


ghaddab's avatar
ghaddab committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

ghaddab's avatar
ghaddab committed
16
    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

/*! \file PHY/LTE_TRANSPORT/ulsch_decoding.c
* \brief Top-level routines for decoding  the ULSCH transport channel from 36.212 V8.6 2009-03
* \author R. Knopp
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr
* \note
* \warning
*/

//#include "defs.h"

#include "PHY/defs.h"
#include "PHY/extern.h"
#include "PHY/CODING/extern.h"
#include "extern.h"
#include "SCHED/extern.h"
#ifdef OPENAIR2
#include "LAYER2/MAC/defs.h"
#include "LAYER2/MAC/extern.h"
#include "RRC/LITE/extern.h"
#include "PHY_INTERFACE/extern.h"
#endif

#ifdef PHY_ABSTRACTION
#include "UTIL/OCG/OCG.h"
#include "UTIL/OCG/OCG_extern.h"
#endif
59

60 61 62
#include "UTIL/LOG/vcd_signal_dumper.h"
//#define DEBUG_ULSCH_DECODING

63 64
void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch)
{
65 66 67 68

  int i,r;

  if (ulsch) {
69
    for (i=0; i<8; i++) {
70
      if (ulsch->harq_processes[i]) {
71 72 73 74 75 76
        if (ulsch->harq_processes[i]->b) {
          free16(ulsch->harq_processes[i]->b,MAX_ULSCH_PAYLOAD_BYTES);
          ulsch->harq_processes[i]->b = NULL;
        }

        for (r=0; r<MAX_NUM_ULSCH_SEGMENTS; r++) {
77 78 79
          free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 768);
          ulsch->harq_processes[i]->c[r] = NULL;
        }
80 81 82 83 84 85 86 87 88

        for (r=0; r<MAX_NUM_ULSCH_SEGMENTS; r++)
          if (ulsch->harq_processes[i]->d[r]) {
            free16(ulsch->harq_processes[i]->d[r],((3*8*6144)+12+96)*sizeof(short));
            ulsch->harq_processes[i]->d[r] = NULL;
          }

        free16(ulsch->harq_processes[i],sizeof(LTE_UL_eNB_HARQ_t));
        ulsch->harq_processes[i] = NULL;
89 90
      }
    }
91 92 93

    free16(ulsch,sizeof(LTE_eNB_ULSCH_t));
    ulsch = NULL;
94 95 96
  }
}

97
LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag)
98
{
99 100 101 102

  LTE_eNB_ULSCH_t *ulsch;
  uint8_t exit_flag = 0,i,r;
  unsigned char bw_scaling =1;
103 104 105

  switch (N_RB_UL) {
  case 6:
106 107
    bw_scaling =16;
    break;
108

109 110 111
  case 25:
    bw_scaling =4;
    break;
112 113

  case 50:
114 115
    bw_scaling =2;
    break;
116

117 118 119 120
  default:
    bw_scaling =1;
    break;
  }
121

122
  ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
123

124
  if (ulsch) {
125
    memset(ulsch,0,sizeof(LTE_eNB_ULSCH_t));
126
    ulsch->max_turbo_iterations = max_turbo_iterations;
127
    ulsch->Mlimit = 4;
128

129
    for (i=0; i<8; i++) {
130 131
      //      msg("new_ue_ulsch: Harq process %d\n",i);
      ulsch->harq_processes[i] = (LTE_UL_eNB_HARQ_t *)malloc16(sizeof(LTE_UL_eNB_HARQ_t));
132

133
      if (ulsch->harq_processes[i]) {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        memset(ulsch->harq_processes[i],0,sizeof(LTE_UL_eNB_HARQ_t));
        ulsch->harq_processes[i]->b = (uint8_t*)malloc16(MAX_ULSCH_PAYLOAD_BYTES/bw_scaling);

        if (ulsch->harq_processes[i]->b)
          memset(ulsch->harq_processes[i]->b,0,MAX_ULSCH_PAYLOAD_BYTES/bw_scaling);
        else
          exit_flag=3;

        if (abstraction_flag==0) {
          for (r=0; r<MAX_NUM_ULSCH_SEGMENTS/bw_scaling; r++) {
            ulsch->harq_processes[i]->c[r] = (uint8_t*)malloc16(((r==0)?8:0) + 3+768);
            if (ulsch->harq_processes[i]->c[r])
              memset(ulsch->harq_processes[i]->c[r],0,((r==0)?8:0) + 3+768);
            else
              exit_flag=2;

            ulsch->harq_processes[i]->d[r] = (short*)malloc16(((3*8*6144)+12+96)*sizeof(short));

            if (ulsch->harq_processes[i]->d[r])
              memset(ulsch->harq_processes[i]->d[r],0,((3*8*6144)+12+96)*sizeof(short));
            else
              exit_flag=2;
          }

          ulsch->harq_processes[i]->subframe_scheduling_flag = 0;
        }
      } else {
        exit_flag=1;
162 163 164 165 166 167
      }
    }

    if (exit_flag==0)
      return(ulsch);
  }
168

169 170 171 172 173 174
  LOG_E(PHY,"new_ue_ulsch: exit_flag = %d\n",exit_flag);
  free_eNB_ulsch(ulsch);

  return(NULL);
}

175
void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch)
176
{
177 178 179 180 181 182

  unsigned char i;

  //ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
  if (ulsch) {
    ulsch->rnti = 0;
183

184
    for (i=0; i<8; i++) {
185
      if (ulsch->harq_processes[i]) {
186 187 188 189 190 191
        //    ulsch->harq_processes[i]->Ndi = 0;
        ulsch->harq_processes[i]->status = 0;
        ulsch->harq_processes[i]->subframe_scheduling_flag = 0;
        //ulsch->harq_processes[i]->phich_active = 0; //this will be done later after transmission of PHICH
        ulsch->harq_processes[i]->phich_ACK = 0;
        ulsch->harq_processes[i]->round = 0;
192 193 194 195 196 197 198
      }
    }

  }
}


199 200
uint8_t extract_cqi_crc(uint8_t *cqi,uint8_t CQI_LENGTH)
{
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  uint8_t crc;

  crc = cqi[CQI_LENGTH>>3];
  //  msg("crc1: %x, shift %d\n",crc,CQI_LENGTH&0x7);
  crc = (crc<<(CQI_LENGTH&0x7));
  // clear crc bits
  //  ((char *)cqi)[CQI_LENGTH>>3] &= 0xff>>(8-(CQI_LENGTH&0x7));
  //  msg("crc2: %x, cqi0 %x\n",crc,cqi[1+(CQI_LENGTH>>3)]);
  crc |= (cqi[1+(CQI_LENGTH>>3)])>>(8-(CQI_LENGTH&0x7));
  // clear crc bits
  //(((char *)cqi)[1+(CQI_LENGTH>>3)]) = 0;

  //  printf("crc : %x\n",crc);
  return(crc);

}



knopp's avatar
knopp committed
221
unsigned int  ulsch_decoding(PHY_VARS_eNB *eNB,eNB_rxtx_proc_t *proc,
222 223 224 225 226
                             uint8_t UE_id,
                             uint8_t control_only_flag,
                             uint8_t Nbundled,
                             uint8_t llr8_flag)
{
227 228


229 230 231
  int16_t *ulsch_llr = eNB->pusch_vars[UE_id]->llr;
  LTE_DL_FRAME_PARMS *frame_parms = &eNB->frame_parms;
  LTE_eNB_ULSCH_t *ulsch = eNB->ulsch[UE_id];
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
  uint8_t harq_pid;
  unsigned short nb_rb;
  unsigned int A,E;
  uint8_t Q_m;
  unsigned int i,i2,q,j,j2;
  int iprime;
  unsigned int ret=0,offset;
  unsigned short iind;
  //  uint8_t dummy_channel_output[(3*8*block_length)+12];

  unsigned int r,r_offset=0,Kr,Kr_bytes;
  uint8_t crc_type;
  uint8_t *columnset;
  unsigned int sumKr=0;
  unsigned int Qprime,L,G,Q_CQI,Q_RI,H,Hprime,Hpp,Cmux,Rmux_prime,O_RCC;
247
  unsigned int Qprime_ACK,Qprime_RI,len_ACK=0,len_RI=0;
knopp's avatar
knopp committed
248
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
249 250 251 252 253 254 255 256 257 258 259 260 261
  int metric,metric_new;
  uint8_t o_flip[8];
  uint32_t x1, x2, s=0;
  int16_t ys,c;
  uint32_t wACK_idx;
  int16_t dummy_w[MAX_NUM_ULSCH_SEGMENTS][3*(6144+64)];
  uint8_t dummy_w_cc[3*(MAX_CQI_BITS+8+32)];
  int16_t y[6*14*1200];
  uint8_t ytag[14*1200];
  //  uint8_t ytag2[6*14*1200],*ytag2_ptr;
  int16_t cseq[6*14*1200];
  int off;
  int status[20];
knopp's avatar
knopp committed
262
  int subframe = proc->subframe_rx;
knopp's avatar
knopp committed
263
  LTE_UL_eNB_HARQ_t *ulsch_harq;
264 265

  uint8_t (*tc)(int16_t *y,
266 267 268 269 270 271 272 273 274 275 276 277 278 279
                uint8_t *,
                uint16_t,
                uint16_t,
                uint16_t,
                uint8_t,
                uint8_t,
                uint8_t,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *,
                time_stats_t *);
280

knopp's avatar
knopp committed
281
  harq_pid = subframe2harq_pid(frame_parms,proc->frame_rx,subframe);
282 283

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,1);
284 285 286

  // x1 is set in lte_gold_generic
  x2 = ((uint32_t)ulsch->rnti<<14) + ((uint32_t)subframe<<9) + frame_parms->Nid_cell; //this is c_init in 36.211 Sec 6.3.1
knopp's avatar
knopp committed
287
  ulsch_harq = ulsch->harq_processes[harq_pid];
288 289

  if (harq_pid==255) {
290
    LOG_E(PHY, "FATAL ERROR: illegal harq_pid, returning\n");
knopp's avatar
knopp committed
291
    VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0);
292
    return -1;
293 294
  }

295 296
  if (ulsch_harq->Nsymb_pusch == 0) {
      LOG_E(PHY, "FATAL ERROR: harq_pid %d, Nsymb 0!\n",harq_pid);
knopp's avatar
knopp committed
297
      VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0); 
298
      return 1+ulsch->max_turbo_iterations;
299
  }
300 301 302 303
  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;
304

knopp's avatar
knopp committed
305
  nb_rb = ulsch_harq->nb_rb;
306

knopp's avatar
knopp committed
307
  A = ulsch_harq->TBS;
308

309

knopp's avatar
knopp committed
310 311
  Q_m = get_Qm_ul(ulsch_harq->mcs);
  G = nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
312 313 314


#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
315
  msg("ulsch_decoding (Nid_cell %d, rnti %x, x2 %x): round %d, RV %d, mcs %d, O_RI %d, O_ACK %d, G %d, subframe %d\n",
316
      frame_parms->Nid_cell,ulsch->rnti,x2,
knopp's avatar
knopp committed
317 318 319 320 321
      ulsch_harq->round,
      ulsch_harq->rvidx,
      ulsch_harq->mcs,
      ulsch_harq->O_RI,
      ulsch_harq->O_ACK,
322 323
      G,
      subframe);
324
#endif
325

knopp's avatar
knopp committed
326
  if (ulsch_harq->round == 0) {
327
    // This is a new packet, so compute quantities regarding segmentation
knopp's avatar
knopp committed
328
    ulsch_harq->B = A+24;
329
    lte_segmentation(NULL,
330 331 332 333 334 335 336 337
                     NULL,
                     ulsch_harq->B,
                     &ulsch_harq->C,
                     &ulsch_harq->Cplus,
                     &ulsch_harq->Cminus,
                     &ulsch_harq->Kplus,
                     &ulsch_harq->Kminus,
                     &ulsch_harq->F);
338 339 340
    //  CLEAR LLR's HERE for first packet in process
  }

341
  //  printf("after segmentation c[%d] = %p\n",0,ulsch_harq->c[0]);
342 343

  sumKr = 0;
344 345

  for (r=0; r<ulsch_harq->C; r++) {
knopp's avatar
knopp committed
346 347
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
348
    else
knopp's avatar
knopp committed
349
      Kr = ulsch_harq->Kplus;
350

351 352
    sumKr += Kr;
  }
353

354
  if (sumKr==0) {
355
    LOG_N(PHY,"[eNB %d] ulsch_decoding.c: FATAL sumKr is 0!\n",eNB->Mod_id);
356
    LOG_D(PHY,"ulsch_decoding (Nid_cell %d, rnti %x, x2 %x): harq_pid %d round %d, RV %d, mcs %d, O_RI %d, O_ACK %d, G %d, subframe %d\n",
357 358 359 360 361 362 363 364 365
          frame_parms->Nid_cell,ulsch->rnti,x2,
          harq_pid,
          ulsch_harq->round,
          ulsch_harq->rvidx,
          ulsch_harq->mcs,
          ulsch_harq->O_RI,
          ulsch_harq->O_ACK,
          G,
          subframe);
gauthier's avatar
gauthier committed
366
    mac_xface->macphy_exit("ulsch_decoding.c: FATAL sumKr is 0!");
367 368
    return(-1);
  }
369

370
  // Compute Q_ri
knopp's avatar
knopp committed
371
  Qprime = ulsch_harq->O_RI*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_ri_times8;
372 373 374 375 376 377

  if (Qprime > 0 ) {
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
378

379 380 381 382 383 384 385 386 387 388
    if (Qprime > 4*nb_rb * 12)
      Qprime = 4*nb_rb * 12;
  }

  Q_RI = Q_m*Qprime;
  Qprime_RI = Qprime;


  // Compute Q_ack

knopp's avatar
knopp committed
389
  Qprime = ulsch_harq->O_ACK*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_harqack_times8;
390

391 392 393 394 395 396 397 398 399 400 401 402 403
  if (Qprime > 0) {
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);

    if (Qprime > (4*nb_rb * 12))
      Qprime = 4*nb_rb * 12;
  }

  //  Q_ACK = Qprime * Q_m;
  Qprime_ACK = Qprime;
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
404 405
  msg("ulsch_decoding.c: Qprime_ACK %d, Msc_initial %d, Nsymb_initial %d, sumKr %d\n",
      Qprime_ACK,ulsch_harq->Msc_initial,ulsch_harq->Nsymb_initial,sumKr);
406
#endif
407

408
  // Compute Q_cqi
knopp's avatar
knopp committed
409
  if (ulsch_harq->Or1 < 12)
410
    L=0;
411
  else
412
    L=8;
413

414
  // NOTE: we have to handle the case where we have a very small number of bits (condition on pg. 26 36.212)
knopp's avatar
knopp committed
415 416
  if (ulsch_harq->Or1 > 0)
    Qprime = (ulsch_harq->Or1 + L) * ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_cqi_times8;
417 418 419
  else
    Qprime=0;

420
  if (Qprime > 0) {  // check if ceiling is larger than floor in Q' expression
421 422 423 424 425 426
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
  }

knopp's avatar
knopp committed
427
  G = nb_rb * (12 * Q_m) * (ulsch_harq->Nsymb_pusch);
428

429

430 431

  Q_CQI = Q_m * Qprime;
knopp's avatar
knopp committed
432
#ifdef DEBUG_ULSCH_DECODING
433
  msg("ulsch_decoding: G %d, Q_RI %d, Q_CQI %d (L %d, Or1 %d) O_ACK %d\n",G,Q_RI,Q_CQI,L,ulsch_harq->Or1,ulsch_harq->O_ACK);
knopp's avatar
knopp committed
434
#endif
435 436 437 438 439 440 441 442 443 444 445 446 447

  G = G - Q_RI - Q_CQI;

  if ((int)G < 0) {
    LOG_E(PHY,"FATAL: ulsch_decoding.c G < 0 (%d) : Q_RI %d, Q_CQI %d\n",G,Q_RI,Q_CQI);
    return(-1);
  }

  H = G + Q_CQI;
  Hprime = H/Q_m;

  // Demultiplexing/Deinterleaving of PUSCH/ACK/RI/CQI
  Hpp = Hprime + Qprime_RI;
448

knopp's avatar
knopp committed
449
  Cmux       = ulsch_harq->Nsymb_pusch;
450 451
  //  Rmux       = Hpp*Q_m/Cmux;
  Rmux_prime = Hpp/Cmux;
452

453

454 455 456
  // Clear "tag" interleaving matrix to allow for CQI/DATA identification
  memset(ytag,0,Cmux*Rmux_prime);

457
  start_meas(&eNB->ulsch_demultiplexing_stats);
458 459

  i=0;
knopp's avatar
knopp committed
460
  memset(y,LTE_NULL,Q_m*Hpp);
461

462
  //  printf("before unscrambling c[%d] = %p\n",0,ulsch_harq->c[0]);
463 464 465 466 467 468
  // read in buffer and unscramble llrs for everything but placeholder bits
  // llrs stored per symbol correspond to columns of interleaving matrix


  s = lte_gold_generic(&x1, &x2, 1);
  i2=0;
469 470 471

  for (i=0; i<((Hpp*Q_m)>>5); i++) {
    for (j=0; j<32; j++) {
472 473
      cseq[i2++] = (int16_t)((((s>>j)&1)<<1)-1);
    }
474

475 476 477
    s = lte_gold_generic(&x1, &x2, 0);
  }

478 479
  //  printf("after unscrambling c[%d] = %p\n",0,ulsch_harq->c[0]);

480 481 482 483
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
484 485 486 487

  j=0;

  for (i=0; i<Qprime_RI; i++) {
488
    r = Rmux_prime - 1 - (i>>2);
489 490
    /*
    for (q=0;q<Q_m;q++)
491 492
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
493
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));
494
    cseq[off+1] = cseq[off];  // PUSCH_y
495 496

    for (q=2; q<Q_m; q++)
497 498 499 500 501 502
      cseq[off+q] = -1;    // PUSCH_x

    j=(j+3)&3;

  }

503 504
  //  printf("after RI c[%d] = %p\n",0,ulsch_harq->c[0]);

505 506 507 508 509 510 511
  // HARQ-ACK Bits (Note these overwrite some bits)
  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;
512 513

  for (i=0; i<Qprime_ACK; i++) {
514 515 516 517 518 519
    r = Rmux_prime - 1 - (i>>2);
    /*
    for (q=0;q<Q_m;q++) {
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_ACK[(q+(Q_m*i))%len_ACK];
    }
    */
520 521
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));

knopp's avatar
knopp committed
522
    if (ulsch_harq->O_ACK == 1) {
523
      if (ulsch->bundling==0)
524 525 526 527 528 529 530
        cseq[off+1] = cseq[off];  // PUSCH_y

      for (q=2; q<Q_m; q++)
        cseq[off+q] = -1;    // PUSCH_x
    } else if (ulsch_harq->O_ACK == 2) {
      for (q=2; q<Q_m; q++)
        cseq[off+q] = -1;    // PUSCH_x
531
    }
532

533
#ifdef DEBUG_ULSCH_DECODING
534
    msg("ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]);
535 536 537 538 539 540 541
#endif
    j=(j+3)&3;
  }



  i=0;
542

543 544
  switch (Q_m) {
  case 2:
545
    for (j=0; j<Cmux; j++) {
546
      i2=j<<1;
547 548 549 550 551 552 553 554 555 556 557

      for (r=0; r<Rmux_prime; r++) {
        c = cseq[i];
        //  printf("ulsch %d: %d * ",i,c);
        y[i2++] = c*ulsch_llr[i++];
        //  printf("%d\n",ulsch_llr[i-1]);
        c = cseq[i];
        //  printf("ulsch %d: %d * ",i,c);
        y[i2] = c*ulsch_llr[i++];
        //  printf("%d\n",ulsch_llr[i-1]);
        i2=(i2+(Cmux<<1)-1);
558 559
      }
    }
560

561
    break;
562

563
  case 4:
564
    for (j=0; j<Cmux; j++) {
565
      i2=j<<2;
566 567 568 569 570 571 572 573 574 575 576

      for (r=0; r<Rmux_prime; r++) {
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2] = c*ulsch_llr[i++];
        i2=(i2+(Cmux<<2)-3);
577 578
      }
    }
579 580 581

    break;

582
  case 6:
583
    for (j=0; j<Cmux; j++) {
584
      i2=j*6;
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599

      for (r=0; r<Rmux_prime; r++) {
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2++] = c*ulsch_llr[i++];
        c = cseq[i];
        y[i2] = c*ulsch_llr[i++];
        i2=(i2+(Cmux*6)-5);
600 601
      }
    }
602

603 604 605 606
    break;
  }


607
  stop_meas(&eNB->ulsch_demultiplexing_stats);
608 609

  if (i!=(H+Q_RI))
610
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI);
611 612 613 614 615 616 617 618 619 620

  // HARQ-ACK Bits (LLRs are nulled in overwritten bits after copying HARQ-ACK LLR)

  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;

knopp's avatar
knopp committed
621
  if (ulsch_harq->O_ACK == 1) {
622 623 624 625
    switch (Q_m) {
    case 2:
      len_ACK = 2;
      break;
626

627 628 629
    case 4:
      len_ACK = 4;
      break;
630

631 632 633 634 635
    case 6:
      len_ACK = 6;
      break;
    }
  }
636

knopp's avatar
knopp committed
637
  if (ulsch_harq->O_ACK == 2) {
638 639 640 641
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
642

643 644 645
    case 4:
      len_ACK = 12;
      break;
646

647 648 649 650 651
    case 6:
      len_ACK = 18;
      break;
    }
  }
652

knopp's avatar
knopp committed
653
  if (ulsch_harq->O_ACK > 2) {
654 655 656 657
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }

658
  for (i=0; i<len_ACK; i++)
knopp's avatar
knopp committed
659
    ulsch_harq->q_ACK[i] = 0;
660 661


662
  for (i=0; i<Qprime_ACK; i++) {
663
    r = Rmux_prime -1 - (i>>2);
664 665

    for (q=0; q<Q_m; q++) {
666
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
667
        ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
668 669
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
670

671 672 673
    j=(j+3)&3;
  }

674
  //  printf("after ACKNAK c[%d] = %p\n",0,ulsch_harq->c[0]);
675

676
  // RI BITS
677

knopp's avatar
knopp committed
678
  if (ulsch_harq->O_RI == 1) {
679 680
    switch (Q_m) {
    case 2:
681
      len_RI=2;
682
      break;
683

684 685 686
    case 4:
      len_RI=4;
      break;
687

688 689 690 691 692 693
    case 6:
      len_RI=6;
      break;
    }
  }

knopp's avatar
knopp committed
694
  if (ulsch_harq->O_RI > 1) {
695 696 697 698
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

699
  for (i=0; i<len_RI; i++)
knopp's avatar
knopp committed
700
    ulsch_harq->q_RI[i] = 0;
701

702 703 704 705
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
706 707 708 709

  j=0;

  for (i=0; i<Qprime_RI; i++) {
710
    r = Rmux_prime -1 - (i>>2);
711 712

    for (q=0; q<Q_m; q++)
knopp's avatar
knopp committed
713
      ulsch_harq->q_RI[(q+(Q_m*i))%len_RI] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
714

715 716 717 718
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

719 720
  //  printf("after RI2 c[%d] = %p\n",0,ulsch_harq->c[0]);

721
  // CQI and Data bits
722 723 724
  j=0;
  j2=0;

725
  //  r=0;
726 727 728 729 730 731
  if (Q_RI>0) {
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      while (ytag[j]==LTE_NULL) {
	j++;
	j2+=Q_m;
732
      }
733 734 735 736 737 738 739 740 741 742 743 744 745
      
      for (q=0; q<Q_m; q++) {
	//      ys = y[q+(Q_m*((r*Cmux)+j))];
	ys = y[q+j2];
	
	if (ys>127)
	  ulsch_harq->q[q+(Q_m*i)] = 127;
	else if (ys<-128)
	  ulsch_harq->q[q+(Q_m*i)] = -128;
	else
	  ulsch_harq->q[q+(Q_m*i)] = ys;
      }
      
746 747
      j2+=Q_m;
    }
748
    
749
        
750 751
    switch (Q_m) {
    case 2:
752
      for (iprime=0; iprime<G;) {
753 754 755 756 757 758 759 760
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=2;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
761
      }
762 763 764 765 766
      
      
      break;
      
    case 4:
767
      for (iprime=0; iprime<G;) {
768 769 770 771 772 773 774 775 776 777
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=4;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
778
      }
779 780 781 782
      
      break;
      
    case 6:
783
      for (iprime=0; iprime<G;) {
784 785 786 787 788 789 790 791 792 793 794 795
	while (ytag[j]==LTE_NULL) {
	  j++;
	  j2+=6;
	}
	
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	
796
      }
797 798
      
      break;
799

800
    }
801 802 803
    

  } // Q_RI>0
804
  else {
805

806 807 808 809 810 811 812 813 814 815
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      for (q=0; q<Q_m; q++) {
	ys = y[q+j2];
	if (ys>127)
	  ulsch_harq->q[q+(Q_m*i)] = 127;
	else if (ys<-128)
	  ulsch_harq->q[q+(Q_m*i)] = -128;
	else
	  ulsch_harq->q[q+(Q_m*i)] = ys;
816
      }
817 818
      
      j2+=Q_m;
819
    }
820
    //    printf("after CQI0 c[%d] = %p\n",0,ulsch_harq->c[0]);
821 822
    switch (Q_m) {
    case 2:
823
      for (iprime=0; iprime<G;) {
824 825
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
826
      }
827 828
      break;
    case 4:
829
      for (iprime=0; iprime<G;) {
830 831 832 833
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
834
      }
835 836
      break;
    case 6:
837
      for (iprime=0; iprime<G;) {
838 839 840 841 842 843 844 845
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
      }
      break;
846 847
    }
  }
848 849
  //  printf("after ACKNAK2 c[%d] = %p (iprime %d, G %d)\n",0,ulsch_harq->c[0],iprime,G);

850 851
  // Do CQI/RI/HARQ-ACK Decoding first and pass to MAC

852
  // HARQ-ACK
853
  wACK_idx = (ulsch->bundling==0) ? 4 : ((Nbundled-1)&3);
854

knopp's avatar
knopp committed
855
  if (ulsch_harq->O_ACK == 1) {
856 857 858 859 860 861 862
    ulsch_harq->q_ACK[0] *= wACK_RX[wACK_idx][0];
    ulsch_harq->q_ACK[0] += (ulsch->bundling==0) ? ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] : ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][1];

    if (ulsch_harq->q_ACK[0] < 0)
      ulsch_harq->o_ACK[0] = 0;
    else
      ulsch_harq->o_ACK[0] = 1;
863
  }
864

knopp's avatar
knopp committed
865
  if (ulsch_harq->O_ACK == 2) {
866 867 868
    switch (Q_m) {

    case 2:
knopp's avatar
knopp committed
869 870 871
      ulsch_harq->q_ACK[0] = ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[3]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[1] = ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[4]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] = ulsch_harq->q_ACK[2]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[5]*wACK_RX[wACK_idx][1];
872 873
      break;
    case 4:
knopp's avatar
knopp committed
874 875 876
      ulsch_harq->q_ACK[0] = ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[5]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[1] = ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[8]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] = ulsch_harq->q_ACK[4]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[9]*wACK_RX[wACK_idx][1];
877 878
      break;
    case 6:
knopp's avatar
knopp committed
879
      ulsch_harq->q_ACK[0] =  ulsch_harq->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[7]*wACK_RX[wACK_idx][1];
880 881
      ulsch_harq->q_ACK[1] =  ulsch_harq->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[12]*wACK_RX[wACK_idx][1];
      ulsch_harq->q_ACK[2] =  ulsch_harq->q_ACK[6]*wACK_RX[wACK_idx][0] + ulsch_harq->q_ACK[13]*wACK_RX[wACK_idx][1];
882 883
      break;
    }
884

knopp's avatar
knopp committed
885 886 887 888
    ulsch_harq->o_ACK[0] = 1;
    ulsch_harq->o_ACK[1] = 1;
    metric     = ulsch_harq->q_ACK[0]+ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
    metric_new = -ulsch_harq->q_ACK[0]+ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
889 890

    if (metric_new > metric) {
knopp's avatar
knopp committed
891 892
      ulsch_harq->o_ACK[0]=0;
      ulsch_harq->o_ACK[1]=1;
893 894
      metric = metric_new;
    }
895

knopp's avatar
knopp committed
896
    metric_new = ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
897 898 899


    if (metric_new > metric) {
knopp's avatar
knopp committed
900 901
      ulsch_harq->o_ACK[0] = 1;
      ulsch_harq->o_ACK[1] = 0;
902 903
      metric = metric_new;
    }
904

knopp's avatar
knopp committed
905
    metric_new = -ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
906 907

    if (metric_new > metric) {
knopp's avatar
knopp committed
908 909
      ulsch_harq->o_ACK[0] = 0;
      ulsch_harq->o_ACK[1] = 0;
910 911 912 913 914 915
      metric = metric_new;
    }
  }

  // RI

knopp's avatar
knopp committed
916
  if ((ulsch_harq->O_RI == 1) && (Qprime_RI > 0)) {
917
    ulsch_harq->o_RI[0] = ((ulsch_harq->q_RI[0] + ulsch_harq->q_RI[Q_m/2]) > 0) ? 0 : 1;
918
  }
919

920 921
  // CQI

922
  //  printf("before cqi c[%d] = %p\n",0,ulsch_harq->c[0]);
923
  if (Q_CQI>0) {
knopp's avatar
knopp committed
924
    memset((void *)&dummy_w_cc[0],0,3*(ulsch_harq->Or1+8+32));
925

knopp's avatar
knopp committed
926
    O_RCC = generate_dummy_w_cc(ulsch_harq->Or1+8,
927 928 929
                                &dummy_w_cc[0]);


930
    lte_rate_matching_cc_rx(O_RCC,
931 932 933 934 935
                            Q_CQI,
                            ulsch_harq->o_w,
                            dummy_w_cc,
                            ulsch_harq->q);

knopp's avatar
knopp committed
936
    sub_block_deinterleaving_cc((unsigned int)(ulsch_harq->Or1+8),
937 938 939
                                &ulsch_harq->o_d[96],
                                &ulsch_harq->o_w[0]);

knopp's avatar
knopp committed
940 941
    memset(o_flip,0,1+((8+ulsch_harq->Or1)/8));
    phy_viterbi_lte_sse2(ulsch_harq->o_d+96,o_flip,8+ulsch_harq->Or1);
942

knopp's avatar
knopp committed
943 944
    if (extract_cqi_crc(o_flip,ulsch_harq->Or1) == (crc8(o_flip,ulsch_harq->Or1)>>24))
      ulsch_harq->cqi_crc_status = 1;
945
    else
knopp's avatar
knopp committed
946
      ulsch_harq->cqi_crc_status = 0;
947 948


knopp's avatar
knopp committed
949
    if (ulsch->harq_processes[harq_pid]->Or1<=32) {
knopp's avatar
knopp committed
950 951 952
      ulsch_harq->o[3] = o_flip[0] ;
      ulsch_harq->o[2] = o_flip[1] ;
      ulsch_harq->o[1] = o_flip[2] ;
953 954
      ulsch_harq->o[0] = o_flip[3] ;
    } else {
knopp's avatar
knopp committed
955 956 957
      ulsch_harq->o[7] = o_flip[0] ;
      ulsch_harq->o[6] = o_flip[1] ;
      ulsch_harq->o[5] = o_flip[2] ;
958
      ulsch_harq->o[4] = o_flip[3] ;
knopp's avatar
knopp committed
959 960 961
      ulsch_harq->o[3] = o_flip[4] ;
      ulsch_harq->o[2] = o_flip[5] ;
      ulsch_harq->o[1] = o_flip[6] ;
962 963
      ulsch_harq->o[0] = o_flip[7] ;

964
    }
965

966
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
967
    msg("ulsch_decoding: Or1=%d\n",ulsch_harq->Or1);
968 969

    for (i=0; i<1+((8+ulsch_harq->Or1)/8); i++)
knopp's avatar
knopp committed
970
      msg("ulsch_decoding: O[%d] %d\n",i,ulsch_harq->o[i]);
971

knopp's avatar
knopp committed
972 973
    if (ulsch_harq->cqi_crc_status == 1)
      msg("RX CQI CRC OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
974
    else
knopp's avatar
knopp committed
975
      msg("RX CQI CRC NOT OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
976

977 978 979 980 981 982
#endif
  }

  //  return(0);
  // Do PUSCH Decoding

983
  //  stop_meas(&eNB->ulsch_demultiplexing_stats);
984 985 986


  r_offset = 0;
987 988 989

  for (r=0; r<ulsch_harq->C; r++) {

990
    //    printf("before subblock deinterleaving c[%d] = %p\n",r,ulsch_harq->c[r]);
991
    // Get Turbo interleaver parameters
knopp's avatar
knopp committed
992 993
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
994
    else
knopp's avatar
knopp committed
995
      Kr = ulsch_harq->Kplus;
996

997
    Kr_bytes = Kr>>3;
998

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    if (Kr_bytes<=64)
      iind = (Kr_bytes-5);
    else if (Kr_bytes <=128)
      iind = 59 + ((Kr_bytes-64)>>1);
    else if (Kr_bytes <= 256)
      iind = 91 + ((Kr_bytes-128)>>2);
    else if (Kr_bytes <= 768)
      iind = 123 + ((Kr_bytes-256)>>3);
    else {
      LOG_E(PHY,"ulsch_decoding: Illegal codeword size %d!!!\n",Kr_bytes);
      return(-1);
    }
1011 1012

#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
1013
    msg("f1 %d, f2 %d, F %d\n",f1f2mat_old[2*iind],f1f2mat_old[1+(2*iind)],(r==0) ? ulsch_harq->F : 0);
1014
#endif
1015

1016
    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
1017 1018 1019
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);
1020

1021
#ifdef DEBUG_ULSCH_DECODING
1022
    msg("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
1023 1024 1025 1026 1027 1028
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif
1029

1030
    start_meas(&eNB->ulsch_rate_unmatching_stats);
knopp's avatar
knopp committed
1031

knopp's avatar
knopp committed
1032
    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
1033 1034 1035 1036 1037 1038
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
1039
                                   0,   //Uplink
1040 1041 1042 1043 1044 1045 1046
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
                                   get_Qm_ul(ulsch_harq->mcs),
                                   1,
                                   r,
                                   &E)==-1) {
1047 1048 1049
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
1050

1051
    stop_meas(&eNB->ulsch_rate_unmatching_stats);
1052
    r_offset += E;
1053

1054
    start_meas(&eNB->ulsch_deinterleaving_stats);
1055 1056 1057
    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);
1058
    stop_meas(&eNB->ulsch_deinterleaving_stats);
1059 1060
  }

1061
    for (r=0; r<ulsch_harq->C; r++) {
1062

1063 1064 1065 1066
      /*      printf("c[%d] : %p\n",r,
	     ulsch_harq->c[r]);
      */

1067 1068 1069 1070 1071
      if (ulsch_harq->C == 1)
        crc_type = CRC24_A;
      else
        crc_type = CRC24_B;

1072
      start_meas(&eNB->ulsch_turbo_decoding_stats);
1073

knopp's avatar
knopp committed
1074
      ret = tc(&ulsch_harq->d[r][96],
1075 1076 1077 1078 1079 1080 1081
               ulsch_harq->c[r],
               Kr,
               f1f2mat_old[iind*2],
               f1f2mat_old[(iind*2)+1],
               ulsch->max_turbo_iterations,//MAX_TURBO_ITERATIONS,
               crc_type,
               (r==0) ? ulsch_harq->F : 0,
1082 1083 1084 1085 1086 1087 1088
               &eNB->ulsch_tc_init_stats,
               &eNB->ulsch_tc_alpha_stats,
               &eNB->ulsch_tc_beta_stats,
               &eNB->ulsch_tc_gamma_stats,
               &eNB->ulsch_tc_ext_stats,
               &eNB->ulsch_tc_intl1_stats,
               &eNB->ulsch_tc_intl2_stats);
1089

1090
      stop_meas(&eNB->ulsch_turbo_decoding_stats);
1091 1092

      status[r] = ret;
1093

1094
    }
1095

1096 1097 1098 1099
  // Reassembly of Transport block here
  offset = 0;

  ret = 1;
1100 1101

  for (r=0; r<ulsch_harq->C; r++) {
1102
    if (status[r] != (1+ulsch->max_turbo_iterations)) {
knopp's avatar
knopp committed
1103
      if (r<ulsch_harq->Cminus)
1104
        Kr = ulsch_harq->Kminus;
1105
      else
1106 1107
        Kr = ulsch_harq->Kplus;

1108
      Kr_bytes = Kr>>3;
1109

1110
      if (r==0) {
1111 1112 1113 1114 1115 1116 1117 1118 1119
        memcpy(ulsch_harq->b,
               &ulsch_harq->c[0][(ulsch_harq->F>>3)],
               Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0));
        offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
      } else {
        memcpy(ulsch_harq->b+offset,
               ulsch_harq->c[r],
               Kr_bytes - ((ulsch_harq->C>1)?3:0));
        offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
1120
      }
1121

1122
      if (ret != (1+ulsch->max_turbo_iterations))
1123 1124
        ret = status[r];
    } else {
1125 1126
      ret = 1+ulsch->max_turbo_iterations;
    }
1127

1128
  }
1129

1130
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0);
1131

1132 1133 1134 1135 1136 1137
  return(ret);
}

#ifdef PHY_ABSTRACTION

#ifdef PHY_ABSTRACTION_UL
1138 1139
int ulsch_abstraction(double* sinr_dB, uint8_t TM, uint8_t mcs,uint16_t nrb, uint16_t frb)
{
1140

1141
  int index,ii;
1142 1143 1144 1145 1146 1147
  double sinr_eff = 0;
  int rb_count = 0;
  int offset;
  double bler = 0;
  TM = TM-1;
  sinr_eff = sinr_dB[frb]; //the single sinr_eff value we calculated with MMSE FDE formula in init_snr_up function
1148 1149


1150 1151 1152
  sinr_eff *= 10;
  sinr_eff = floor(sinr_eff);
  sinr_eff /= 10;
1153

1154
  LOG_D(PHY,"[ABSTRACTION] sinr_eff after rounding = %f\n",sinr_eff);
1155

1156 1157 1158 1159 1160 1161 1162
  for (index = 0; index < 16; index++) {
    if(index == 0) {
      if (sinr_eff < sinr_bler_map_up[mcs][0][index]) {
        bler = 1;
        break;
      }
    }
1163

1164
    if (sinr_eff == sinr_bler_map_up[mcs][0][index]) {
1165
      bler = sinr_bler_map_up[mcs][1][index];
1166 1167
    }
  }
1168

1169
#ifdef USER_MODE // need to be adapted for the emulation in the kernel space 
1170 1171 1172

  if (uniformrandom() < bler) {
    LOG_I(OCM,"abstraction_decoding failed (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
1173
    return(0);
1174
  } else {
1175 1176 1177
    LOG_I(OCM,"abstraction_decoding successful (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(1);
  }
1178

1179 1180 1181 1182 1183 1184 1185 1186 1187
#endif
}







1188 1189
int ulsch_abstraction_MIESM(double* sinr_dB,uint8_t TM, uint8_t mcs,uint16_t nrb, uint16_t frb)
{
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
  int index;
  double sinr_eff = 0;
  double sinr_db1 = 0;
  double sinr_db2 = 0;
  double SI=0;
  double RBIR=0;
  int rb_count = 0;
  int offset, M=0;
  double bler = 0;
  int start,middle,end;
  TM = TM-1;
1201

1202 1203
  for (offset = frb; offset <= (frb + nrb -1); offset++) {

1204
    rb_count++;
1205

1206
    //we need to do the table lookups here for the mutual information corresponding to the certain sinr_dB.
1207

1208 1209
    sinr_db1 = sinr_dB[offset*2];
    sinr_db2 = sinr_dB[offset*2+1];
1210

1211
    msg("sinr_db1=%f\n,sinr_db2=%f\n",sinr_db1,sinr_db2);
1212

1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    //rounding up for the table lookup
    sinr_db1 *= 10;
    sinr_db2 *= 10;

    sinr_db1 = floor(sinr_db1);
    sinr_db2 = floor(sinr_db2);

    if ((int)sinr_db1%2) {
      sinr_db1 += 1;
    }

    if ((int)sinr_db2%2) {
      sinr_db2 += 1;
    }

    sinr_db1 /= 10;
    sinr_db2 /= 10;

    if(mcs<10) {
      //for sinr_db1
      for (index = 0; index < 162; index++) {
        if (sinr_db1 < MI_map_4qam[0][0]) {
          SI += (MI_map_4qam[1][0]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db1 > MI_map_4qam[0][161]) {
          SI += (MI_map_4qam[1][161]/beta1_dlsch_MI[TM][mcs]);
          M +=2;
          break;
        }

        if (sinr_db1 == MI_map_4qam[0][index]) {
          SI += (MI_map_4qam[1][index]/beta1_dlsch_MI[TM][