ulsch_decoding.c 50 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 59 60 61 62 63 64

/*! \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 "MAC_INTERFACE/defs.h"
#include "MAC_INTERFACE/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 OMP
#include <omp.h>
#endif

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

66 67 68
#include "UTIL/LOG/vcd_signal_dumper.h"
//#define DEBUG_ULSCH_DECODING

69 70
void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch)
{
71 72 73 74

  int i,r;

  if (ulsch) {
75
    for (i=0; i<ulsch->Mdlharq; i++) {
76
      if (ulsch->harq_processes[i]) {
77 78 79 80 81 82
        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++) {
83 84 85
          free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 768);
          ulsch->harq_processes[i]->c[r] = NULL;
        }
86 87 88 89 90 91 92 93 94

        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;
95 96
      }
    }
97 98 99

    free16(ulsch,sizeof(LTE_eNB_ULSCH_t));
    ulsch = NULL;
100 101 102
  }
}

103 104
LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t Mdlharq,uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag)
{
105 106 107 108

  LTE_eNB_ULSCH_t *ulsch;
  uint8_t exit_flag = 0,i,r;
  unsigned char bw_scaling =1;
109 110 111

  switch (N_RB_UL) {
  case 6:
112 113
    bw_scaling =16;
    break;
114

115 116 117
  case 25:
    bw_scaling =4;
    break;
118 119

  case 50:
120 121
    bw_scaling =2;
    break;
122

123 124 125 126
  default:
    bw_scaling =1;
    break;
  }
127

128
  ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
129

130
  if (ulsch) {
131
    memset(ulsch,0,sizeof(LTE_eNB_ULSCH_t));
132 133 134
    ulsch->Mdlharq = Mdlharq;
    ulsch->max_turbo_iterations = max_turbo_iterations;

135
    for (i=0; i<Mdlharq; i++) {
136 137
      //      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));
138

139
      if (ulsch->harq_processes[i]) {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        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;
169 170 171 172 173 174
      }
    }

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

176 177 178 179 180 181
  LOG_E(PHY,"new_ue_ulsch: exit_flag = %d\n",exit_flag);
  free_eNB_ulsch(ulsch);

  return(NULL);
}

182
void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch)
183
{
184 185 186 187 188 189 190 191

  unsigned char Mdlharq;
  unsigned char i;

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

    for (i=0; i<Mdlharq; i++) {
194
      if (ulsch->harq_processes[i]) {
195 196 197 198 199 200
        //    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;
201 202 203 204 205 206 207
      }
    }

  }
}


208 209
uint8_t extract_cqi_crc(uint8_t *cqi,uint8_t CQI_LENGTH)
{
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

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

}



unsigned int  ulsch_decoding(PHY_VARS_eNB *phy_vars_eNB,
231 232 233 234 235 236
                             uint8_t UE_id,
                             uint8_t sched_subframe,
                             uint8_t control_only_flag,
                             uint8_t Nbundled,
                             uint8_t llr8_flag)
{
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257


  int16_t *ulsch_llr = phy_vars_eNB->lte_eNB_pusch_vars[UE_id]->llr;
  LTE_DL_FRAME_PARMS *frame_parms = &phy_vars_eNB->lte_frame_parms;
  LTE_eNB_ULSCH_t *ulsch = phy_vars_eNB->ulsch_eNB[UE_id];
  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;
  unsigned int Qprime_ACK,Qprime_CQI,Qprime_RI,len_ACK=0,len_RI=0;
258
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
259 260 261 262 263 264 265 266 267 268 269 270 271
  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];
272
  int subframe = phy_vars_eNB->proc[sched_subframe].subframe_rx;
knopp's avatar
knopp committed
273
  LTE_UL_eNB_HARQ_t *ulsch_harq;
274 275

  uint8_t (*tc)(int16_t *y,
276 277 278 279 280 281 282 283 284 285 286 287 288 289
                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 *);
290

291 292 293
  harq_pid = subframe2harq_pid(frame_parms,phy_vars_eNB->proc[sched_subframe].frame_rx,subframe);

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,1);
294 295 296

  // 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
297
  ulsch_harq = ulsch->harq_processes[harq_pid];
298 299

  if (harq_pid==255) {
300
    LOG_E(PHY, "FATAL ERROR: illegal harq_pid, returning\n");
301
    return -1;
302 303
  }

304 305
  if (ulsch_harq->Nsymb_pusch == 0) {
      LOG_E(PHY, "FATAL ERROR: harq_pid %d, Nsymb 0!\n",harq_pid);
306
      return 1+ulsch->max_turbo_iterations;
307
  }
308 309 310 311
  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;
312

knopp's avatar
knopp committed
313
  nb_rb = ulsch_harq->nb_rb;
314

knopp's avatar
knopp committed
315
  A = ulsch_harq->TBS;
316

317

knopp's avatar
knopp committed
318 319
  Q_m = get_Qm_ul(ulsch_harq->mcs);
  G = nb_rb * (12 * Q_m) * ulsch_harq->Nsymb_pusch;
320 321 322


#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
323
  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",
324
      frame_parms->Nid_cell,ulsch->rnti,x2,
knopp's avatar
knopp committed
325 326 327 328 329
      ulsch_harq->round,
      ulsch_harq->rvidx,
      ulsch_harq->mcs,
      ulsch_harq->O_RI,
      ulsch_harq->O_ACK,
330 331
      G,
      subframe);
332
#endif
333

knopp's avatar
knopp committed
334
  if (ulsch_harq->round == 0) {
335
    // This is a new packet, so compute quantities regarding segmentation
knopp's avatar
knopp committed
336
    ulsch_harq->B = A+24;
337
    lte_segmentation(NULL,
338 339 340 341 342 343 344 345
                     NULL,
                     ulsch_harq->B,
                     &ulsch_harq->C,
                     &ulsch_harq->Cplus,
                     &ulsch_harq->Cminus,
                     &ulsch_harq->Kplus,
                     &ulsch_harq->Kminus,
                     &ulsch_harq->F);
346 347 348 349 350
    //  CLEAR LLR's HERE for first packet in process
  }


  sumKr = 0;
351 352

  for (r=0; r<ulsch_harq->C; r++) {
knopp's avatar
knopp committed
353 354
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
355
    else
knopp's avatar
knopp committed
356
      Kr = ulsch_harq->Kplus;
357

358 359
    sumKr += Kr;
  }
360

361 362
  if (sumKr==0) {
    LOG_N(PHY,"[eNB %d] ulsch_decoding.c: FATAL sumKr is 0!\n",phy_vars_eNB->Mod_id);
363
    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",
364 365 366 367 368 369 370 371 372
          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
373
    mac_xface->macphy_exit("ulsch_decoding.c: FATAL sumKr is 0!");
374 375
    return(-1);
  }
376

377
  // Compute Q_ri
knopp's avatar
knopp committed
378
  Qprime = ulsch_harq->O_RI*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_ri_times8;
379 380 381 382 383 384

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

386 387 388 389 390 391 392 393 394 395
    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
396
  Qprime = ulsch_harq->O_ACK*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_harqack_times8;
397

398 399 400 401 402 403 404 405 406 407 408 409 410
  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
411 412
  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);
413
#endif
414

415
  // Compute Q_cqi
knopp's avatar
knopp committed
416
  if (ulsch_harq->Or1 < 12)
417
    L=0;
418
  else
419
    L=8;
420

knopp's avatar
knopp committed
421 422
  if (ulsch_harq->Or1 > 0)
    Qprime = (ulsch_harq->Or1 + L) * ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_cqi_times8;
423 424 425
  else
    Qprime=0;

426
  if (Qprime > 0) {
427 428 429 430 431 432
    if ((Qprime % (8*sumKr)) > 0)
      Qprime = 1+(Qprime/(8*sumKr));
    else
      Qprime = Qprime/(8*sumKr);
  }

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

435

436

knopp's avatar
knopp committed
437 438
  if (Qprime > (G - ulsch_harq->O_RI))
    Qprime = G - ulsch_harq->O_RI;
439 440

  Q_CQI = Q_m * Qprime;
knopp's avatar
knopp committed
441
#ifdef DEBUG_ULSCH_DECODING
442
  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
443
#endif
444 445 446 447 448 449 450 451 452 453 454 455 456 457
  Qprime_CQI = Qprime;

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

knopp's avatar
knopp committed
459
  Cmux       = ulsch_harq->Nsymb_pusch;
460 461
  //  Rmux       = Hpp*Q_m/Cmux;
  Rmux_prime = Hpp/Cmux;
462

463
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
464
  msg("ulsch_decoding.c: G raw %d (%d symb), Hpp %d, Cmux %d, Rmux_prime %d\n",G,ulsch_harq->Nsymb_pusch,Hpp,Cmux,Rmux_prime);
465 466 467 468 469 470 471
#endif
  // Clear "tag" interleaving matrix to allow for CQI/DATA identification
  memset(ytag,0,Cmux*Rmux_prime);

  start_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);

  i=0;
472 473
  memset(y,LTE_NULL,Q_m*Hpp);
  /*
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  //  Do RI coding
  if (ulsch->O_RI == 1) {
    switch (Q_m) {
    case 2:
      q_RI[0] = 0;
      q_RI[1] = PUSCH_y;
      len_RI=2;
      break;
    case 4:
      q_RI[0] = 0;
      q_RI[1] = PUSCH_y;//1;
      q_RI[2] = PUSCH_x;//o_RI[0];
      q_RI[3] = PUSCH_x;//1;
      len_RI=4;
      break;
    case 6:
      q_RI[0] = 0;
      q_RI[1] = PUSCH_y;//1;
      q_RI[2] = PUSCH_x;//1;
      q_RI[3] = PUSCH_x;//ulsch->o_RI[0];
      q_RI[4] = PUSCH_x;//1;
      q_RI[5] = PUSCH_x;//1;
      len_RI=6;
      break;
    }
  }
  else if (ulsch->O_RI > 1){
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }
504

505
  // 1-bit ACK/NAK
knopp's avatar
knopp committed
506
  if (ulsch_harq->O_ACK == 1) {
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    switch (Q_m) {
    case 2:
      q_ACK[0] = 0;
      q_ACK[1] = (ulsch->bundling==0)? PUSCH_y : 0;
      len_ACK = 2;
      break;
    case 4:
      q_ACK[0] = 0;
      q_ACK[1] = (ulsch->bundling==0)? PUSCH_y : 0;
      q_ACK[2] = PUSCH_x;
      q_ACK[3] = PUSCH_x;
      len_ACK = 4;
      break;
    case 6:
      q_ACK[0] = 0;
      q_ACK[1] = (ulsch->bundling==0)? PUSCH_y : 0;
      q_ACK[2] = PUSCH_x;
      q_ACK[3] = PUSCH_x;
      q_ACK[4] = PUSCH_x;
      q_ACK[6] = PUSCH_x;
      len_ACK = 6;
      break;
    }
  }
  // two-bit ACK/NAK
knopp's avatar
knopp committed
532
  if (ulsch_harq->O_ACK == 2) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    switch (Q_m) {
    case 2:
      q_ACK[0] = 0;
      q_ACK[1] = 0;
      q_ACK[2] = 0;
      q_ACK[3] = 0;
      q_ACK[4] = 0;
      q_ACK[5] = 0;
      len_ACK = 6;
      break;
    case 4:
      q_ACK[0]  = 0;
      q_ACK[1]  = 0;
      q_ACK[2]  = PUSCH_x;
      q_ACK[3]  = PUSCH_x;//1;
      q_ACK[4]  = 0;
      q_ACK[5]  = 0;
      q_ACK[6]  = PUSCH_x;
      q_ACK[7]  = PUSCH_x;//1;
      q_ACK[8]  = 0;
      q_ACK[9]  = 0;
      q_ACK[10] = PUSCH_x;
      q_ACK[11] = PUSCH_x;//1;
      len_ACK = 12;
      break;
    case 6:
      q_ACK[0] = 0;
      q_ACK[1] = 0;
      q_ACK[2] = PUSCH_x;
      q_ACK[3] = PUSCH_x;
      q_ACK[4] = PUSCH_x;
      q_ACK[5] = PUSCH_x;

      q_ACK[6] = 0;
      q_ACK[7] = 0;
      q_ACK[8] = PUSCH_x;
569
      q_ACK[9] = PUSCH_x;
570 571 572 573 574 575 576 577 578 579 580 581 582 583
      q_ACK[10] = PUSCH_x;
      q_ACK[11] = PUSCH_x;

      q_ACK[12] = 0;
      q_ACK[13] = 0;
      q_ACK[14] = PUSCH_x;
      q_ACK[15] = PUSCH_x;
      q_ACK[16] = PUSCH_x;
      q_ACK[17] = PUSCH_x;
      len_ACK = 18;

      break;
    }
  }
knopp's avatar
knopp committed
584
  if (ulsch_harq->O_ACK > 2) {
585 586 587
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }
588 589


590
  // RI BITS
591

592
  //  memset(ytag2,0,Q_m*Hpp);
593

594
  */
595 596 597 598 599 600
  // 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;
601 602 603

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

607 608 609 610 611 612 613
    s = lte_gold_generic(&x1, &x2, 0);
  }

  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
614 615 616 617

  j=0;

  for (i=0; i<Qprime_RI; i++) {
618
    r = Rmux_prime - 1 - (i>>2);
619 620
    /*
    for (q=0;q<Q_m;q++)
621 622
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
623
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));
624
    cseq[off+1] = cseq[off];  // PUSCH_y
625 626

    for (q=2; q<Q_m; q++)
627 628 629 630 631 632 633 634 635 636 637 638 639
      cseq[off+q] = -1;    // PUSCH_x

    j=(j+3)&3;

  }

  // HARQ-ACK Bits (Note these overwrite some bits)
  if (frame_parms->Ncp == 0)
    columnset = cs_ack_normal;
  else
    columnset = cs_ack_extended;

  j=0;
640 641

  for (i=0; i<Qprime_ACK; i++) {
642 643 644 645 646 647
    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];
    }
    */
648 649
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));

knopp's avatar
knopp committed
650
    if (ulsch_harq->O_ACK == 1) {
651
      if (ulsch->bundling==0)
652 653 654 655 656 657 658
        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
659
    }
660

661
#ifdef DEBUG_ULSCH_DECODING
662
    msg("ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]);
663 664 665 666 667 668 669
#endif
    j=(j+3)&3;
  }



  i=0;
670

671 672
  switch (Q_m) {
  case 2:
673
    for (j=0; j<Cmux; j++) {
674
      i2=j<<1;
675 676 677 678 679 680 681 682 683 684 685

      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);
686 687
      }
    }
688

689
    break;
690

691
  case 4:
692
    for (j=0; j<Cmux; j++) {
693
      i2=j<<2;
694 695 696 697 698 699 700 701 702 703 704

      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);
705 706
      }
    }
707 708 709

    break;

710
  case 6:
711
    for (j=0; j<Cmux; j++) {
712
      i2=j*6;
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727

      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);
728 729
      }
    }
730

731 732 733 734
    break;
  }


735 736 737 738 739 740 741 742
  //      for (q=0;q<Q_m;q++) {
  /*
  if ((i&0x1f)==0) {
    s = lte_gold_generic(&x1, &x2, reset);
    //    msg("lte_gold[%d]=%x\n",i,s);
    reset = 0;
  }
  c = (uint8_t)((s>>(i&0x1f))&1);
743 744 745



746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
  // if bits are tagged as placeholders (RI,ACK)

  if (ytag2[q+(Q_m*((r*Cmux)+j))] == PUSCH_y) {

    c=c_prev;
  }
  else if (ytag2[q+(Q_m*((r*Cmux)+j))] == PUSCH_x) {
    c = 0;
  #ifdef DEBUG_ULSCH_DECODING
    //    msg("ulsch_decoding.c: PUSCH_x in row %d, col %d: llr %d\n",r,j,ulsch_llr[i]);
  #endif
  }
  c_prev = c;
  #ifdef DEBUG_ULSCH_DECODING
  //  msg("llr[%d] = %d (c %d, ytag2 %d) ==> ",i,ulsch_llr[i],c,ytag2[q+(Q_m*((r*Cmux)+j))]);
  #endif
  // note flipped here for reverse polarity in 3GPP bit mapping
  y[q+(Q_m*((r*Cmux)+j))] = (c==0) ? -ulsch_llr[i] : ulsch_llr[i];
  i++;
  #ifdef DEBUG_ULSCH_DECODING
  //  msg("%d\n",y[q+(Q_m*((r*Cmux)+j))]);
  #endif
  */
769 770 771
  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);

  if (i!=(H+Q_RI))
772
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI);
773 774 775 776 777 778 779 780 781 782

  // 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
783
  if (ulsch_harq->O_ACK == 1) {
784 785 786 787
    switch (Q_m) {
    case 2:
      len_ACK = 2;
      break;
788

789 790 791
    case 4:
      len_ACK = 4;
      break;
792

793 794 795 796 797
    case 6:
      len_ACK = 6;
      break;
    }
  }
798

knopp's avatar
knopp committed
799
  if (ulsch_harq->O_ACK == 2) {
800 801 802 803
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
804

805 806 807
    case 4:
      len_ACK = 12;
      break;
808

809 810 811 812 813
    case 6:
      len_ACK = 18;
      break;
    }
  }
814

knopp's avatar
knopp committed
815
  if (ulsch_harq->O_ACK > 2) {
816 817 818 819
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }

820
  for (i=0; i<len_ACK; i++)
knopp's avatar
knopp committed
821
    ulsch_harq->q_ACK[i] = 0;
822 823


824
  for (i=0; i<Qprime_ACK; i++) {
825
    r = Rmux_prime -1 - (i>>2);
826 827

    for (q=0; q<Q_m; q++) {
828
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
829 830
        ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];

831
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
832 833
      //      LOG_D(PHY,"ACK %d => %d (%d,%d,%d)\n",(q+(Q_m*i))%len_ACK,ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK],q+(Q_m*((r*Cmux) + columnset[j])),r,columnset[j]);
      printf("ACK %d => %d (%d,%d,%d)\n",(q+(Q_m*i))%len_ACK,ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK],q+(Q_m*((r*Cmux) + columnset[j])),r,columnset[j]);
834 835 836
#endif
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
837

838 839 840 841
    j=(j+3)&3;
  }


842
  // RI BITS
843

knopp's avatar
knopp committed
844
  if (ulsch_harq->O_RI == 1) {
845 846
    switch (Q_m) {
    case 2:
847
      len_RI=2;
848
      break;
849

850 851 852
    case 4:
      len_RI=4;
      break;
853

854 855 856 857 858 859
    case 6:
      len_RI=6;
      break;
    }
  }

knopp's avatar
knopp committed
860
  if (ulsch_harq->O_RI > 1) {
861 862 863 864
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

865
  for (i=0; i<len_RI; i++)
knopp's avatar
knopp committed
866
    ulsch_harq->q_RI[i] = 0;
867

868 869 870 871
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
872 873 874 875

  j=0;

  for (i=0; i<Qprime_RI; i++) {
876
    r = Rmux_prime -1 - (i>>2);
877 878

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

881 882 883 884 885
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

  // CQI and Data bits
886 887 888
  j=0;
  j2=0;

889
  //  r=0;
890
  for (i=0; i<Qprime_CQI; i++) {
891 892 893

    /*
    while (ytag[(r*Cmux)+j]==LTE_NULL) {
894
    #ifdef DEBUG_ULSCH_DECODING
895
      msg("ulsch_decoding.c: r %d, j %d: LTE_NULL\n",r,j);
896
    #endif
897 898
      j++;
      if (j==Cmux) {
899 900
    j=0;
    r++;
901 902 903 904 905
      }
    }
    for (q=0;q<Q_m;q++) {
      ys = y[q+(Q_m*((r*Cmux)+j))];
      if (ys>127)
906
    ulsch_harq->q[q+(Q_m*i)] = 127;
907
      else if (ys<-128)
908 909 910 911
    ulsch_harq->q[q+(Q_m*i)] = -128;
      else
    ulsch_harq->q[q+(Q_m*i)] = ys;
    #ifdef DEBUG_ULSCH_DECODING
912
      msg("ulsch_decoding.c: CQI %d, r %d, j %d, y[%d] %d\n",q+(Q_m*i),r,j, q+(Q_m*((r*Cmux) + j)),ys);
913 914
    #endif
    }
915
    */
916 917 918 919
    while (ytag[j]==LTE_NULL) {
      j++;
      j2+=Q_m;
    }
920

921
    for (q=0; q<Q_m; q++) {
922 923
      //      ys = y[q+(Q_m*((r*Cmux)+j))];
      ys = y[q+j2];
924

925
      if (ys>127)
926
        ulsch_harq->q[q+(Q_m*i)] = 127;
927
      else if (ys<-128)
928 929 930 931 932
        ulsch_harq->q[q+(Q_m*i)] = -128;
      else
        ulsch_harq->q[q+(Q_m*i)] = ys;

#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
933
      msg("ulsch_decoding.c: CQI %d, q %d, y[%d] %d\n",q+(Q_m*i),q,j2, q+j2,ys);
934 935 936
#endif
    }

937 938 939 940 941 942 943
    j2+=Q_m;
  }

  // j2=j*Q_m;

  switch (Q_m) {
  case 2:
944 945 946 947 948
    for (iprime=0; iprime<(Hprime-Qprime_CQI)<<1;) {
      while (ytag[j]==LTE_NULL) {
        j++;
        j2+=2;
      }
949

knopp's avatar
knopp committed
950 951
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
952 953
#ifdef DEBUG_ULSCH_DECODING
      //  msg("ulsch_decoding.c: e %d, r %d, j %d, y[%d] %d\n",g,r,j,q+(Q_m*((r*Cmux) + j)),y[q+(Q_m*((r*Cmux)+j))]);
954
#endif
955

956
    }
957

958
    //    write_output("/tmp/ulsch_e.m","ulsch_e",ulsch->e,iprime,1,0);
959
    break;
960

961
  case 4:
962 963 964 965 966
    for (iprime=0; iprime<(Hprime-Qprime_CQI)<<2;) {
      while (ytag[j]==LTE_NULL) {
        j++;
        j2+=4;
      }
967

knopp's avatar
knopp committed
968 969 970 971
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
972 973
#ifdef DEBUG_ULSCH_DECODING
      //  msg("ulsch_decoding.c: e %d, r %d, j %d, y[%d] %d\n",g,r,j,q+(Q_m*((r*Cmux) + j)),y[q+(Q_m*((r*Cmux)+j))]);
974
#endif
975

976
    }
977

978
    break;
979

980
  case 6:
981 982 983 984 985
    for (iprime=0; iprime<(Hprime-Qprime_CQI)*6;) {
      while (ytag[j]==LTE_NULL) {
        j++;
        j2+=6;
      }
986

knopp's avatar
knopp committed
987 988 989 990 991 992
      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++];
993 994
#ifdef DEBUG_ULSCH_DECODING
      //  msg("ulsch_decoding.c: e %d, r %d, j %d, y[%d] %d\n",g,r,j,q+(Q_m*((r*Cmux) + j)),y[q+(Q_m*((r*Cmux)+j))]);
995
#endif
996

997
    }
998

999 1000 1001
    break;

  }
1002

1003 1004 1005 1006
  /*
  for (i=0,iprime=-Qprime_CQI;i<Hprime;i++,iprime++) {

    while (ytag[(r*Cmux)+j]==LTE_NULL) {
1007
  #ifdef DEBUG_ULSCH_DECODING
1008
      msg("ulsch_decoding.c: r %d, j %d: LTE_NULL\n",r,j);
1009
  #endif
1010 1011
      j++;
      if (j==Cmux) {
1012 1013
  j=0;
  r++;
1014 1015
      }
    }
1016

1017
    if (i<Qprime_CQI) {
1018

1019
      for (q=0;q<Q_m;q++) {
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  ys = y[q+(Q_m*((r*Cmux)+j))];
  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;
  #ifdef DEBUG_ULSCH_DECODING
  msg("ulsch_decoding.c: CQI %d, r %d, j %d, y[%d] %d\n",q+(Q_m*i),r,j, q+(Q_m*((r*Cmux) + j)),ys);
  #endif
1030
      }
1031
    }
1032 1033
    else {
      for (q=0;q<Q_m;q++) {
1034 1035 1036 1037 1038
  g = q+(Q_m*iprime);
  ulsch->e[g] = y[q+(Q_m*((r*Cmux)+j))];
  #ifdef DEBUG_ULSCH_DECODING
  //  msg("ulsch_decoding.c: e %d, r %d, j %d, y[%d] %d\n",g,r,j,q+(Q_m*((r*Cmux) + j)),y[q+(Q_m*((r*Cmux)+j))]);
  #endif
1039 1040 1041 1042 1043 1044 1045 1046
      }
    }
    j++;
    if (j==Cmux) {
      j=0;
      r++;
    }
  }
1047
  */
1048 1049 1050

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

1051
  // HARQ-ACK
1052 1053
  wACK_idx = (ulsch->bundling==0) ? 4 : ((Nbundled-1)&3);
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
1054
  msg("ulsch_decoding.c: Bundling %d, Nbundled %d, wACK_idx %d\n",
1055 1056
      ulsch->bundling,Nbundled,wACK_idx);
#endif
1057

knopp's avatar
knopp committed
1058
  if (ulsch_harq->O_ACK == 1) {
1059 1060 1061 1062 1063 1064 1065 1066
    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;

1067
#ifdef DEBUG_ULSCH_DECODING
1068
    msg("ulsch_decoding.c: ulsch_q_ACK[0] %d (%d,%d)\n",ulsch_harq->q_ACK[0],wACK_RX[wACK_idx][0],wACK_RX[wACK_idx][1]);
1069 1070
#endif
  }
1071

knopp's avatar
knopp committed
1072
  if (ulsch_harq->O_ACK == 2) {
1073 1074 1075
    switch (Q_m) {

    case 2:
knopp's avatar
knopp committed
1076 1077 1078
      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];
1079 1080

      break;
1081

1082
    case 4:
knopp's avatar
knopp committed
1083 1084 1085
      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];
1086 1087

      break;
1088

1089
    case 6:
knopp's avatar
knopp committed
1090
      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];
1091 1092
      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];
1093 1094
      break;
    }
1095

knopp's avatar
knopp committed
1096 1097 1098 1099
    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];
1100 1101

    if (metric_new > metric) {
knopp's avatar
knopp committed
1102 1103
      ulsch_harq->o_ACK[0]=0;
      ulsch_harq->o_ACK[1]=1;
1104 1105
      metric = metric_new;
    }
1106

knopp's avatar
knopp committed
1107
    metric_new = ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
1108 1109 1110


    if (metric_new > metric) {
knopp's avatar
knopp committed
1111 1112
      ulsch_harq->o_ACK[0] = 1;
      ulsch_harq->o_ACK[1] = 0;
1113 1114
      metric = metric_new;
    }
1115

knopp's avatar
knopp committed
1116
    metric_new = -ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
1117 1118

    if (metric_new > metric) {
knopp's avatar
knopp committed
1119 1120
      ulsch_harq->o_ACK[0] = 0;
      ulsch_harq->o_ACK[1] = 0;
1121 1122 1123 1124 1125
      metric = metric_new;
    }
  }

#ifdef DEBUG_ULSCH_DECODING
1126 1127

  for (i=0; i<ulsch_harq->O_ACK; i++)
knopp's avatar
knopp committed
1128
    msg("ulsch_decoding: O_ACK[%d] %d, q_ACK => (%d,%d,%d)\n",i,ulsch_harq->o_ACK[i],ulsch_harq->q_ACK[0],ulsch_harq->q_ACK[1],ulsch_harq->q_ACK[2]);
1129

1130 1131 1132 1133
#endif

  // RI

knopp's avatar
knopp committed
1134
  if ((ulsch_harq->O_RI == 1) && (Qprime_RI > 0)) {
1135
    ulsch_harq->o_RI[0] = ((ulsch_harq->q_RI[0] + ulsch_harq->q_RI[Q_m/2]) > 0) ? 0 : 1;
1136
  }
1137

1138 1139 1140
#ifdef DEBUG_ULSCH_DECODING

  if (Qprime_RI > 0) {
1141
    for (i=0; i<2*ulsch_harq->O_RI; i++)
knopp's avatar
knopp committed
1142
      msg("ulsch_decoding: q_RI[%d] %d\n",i,ulsch_harq->q_RI[i]);
1143
  }
1144

1145
  if (Qprime_CQI > 0) {
1146
    for (i=0; i<ulsch_harq->O_RI; i++)
knopp's avatar
knopp committed
1147
      LOG_D(PHY,"ulsch_decoding: O_RI[%d] %d\n",i,ulsch_harq->o_RI[i]);
1148
  }
1149

1150 1151 1152 1153 1154 1155
#endif


  // CQI

  if (Qprime_CQI>0) {
knopp's avatar
knopp committed
1156
    memset((void *)&dummy_w_cc[0],0,3*(ulsch_harq->Or1+8+32));
1157

knopp's avatar
knopp committed
1158
    O_RCC = generate_dummy_w_cc(ulsch_harq->Or1+8,
1159 1160 1161
                                &dummy_w_cc[0]);


1162
    lte_rate_matching_cc_rx(O_RCC,
1163 1164 1165 1166 1167
                            Q_CQI,
                            ulsch_harq->o_w,
                            dummy_w_cc,
                            ulsch_harq->q);

knopp's avatar
knopp committed
1168
    sub_block_deinterleaving_cc((unsigned int)(ulsch_harq->Or1+8),
1169 1170 1171
                                &ulsch_harq->o_d[96],
                                &ulsch_harq->o_w[0]);

knopp's avatar
knopp committed
1172 1173
    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);
1174

knopp's avatar
knopp committed
1175 1176
    if (extract_cqi_crc(o_flip,ulsch_harq->Or1) == (crc8(o_flip,ulsch_harq->Or1)>>24))
      ulsch_harq->cqi_crc_status = 1;
1177
    else
knopp's avatar
knopp committed
1178
      ulsch_harq->cqi_crc_status = 0;
1179 1180 1181

    //printf("crc(cqi) rx: %x\n",(crc8(o_flip,ulsch->Or1)>>24));

1182
    if (ulsch->harq_processes[harq_pid]->Or1<=32) {
knopp's avatar
knopp committed
1183 1184 1185
      ulsch_harq->o[3] = o_flip[0] ;
      ulsch_harq->o[2] = o_flip[1] ;
      ulsch_harq->o[1] = o_flip[2] ;
1186 1187
      ulsch_harq->o[0] = o_flip[3] ;
    } else {
knopp's avatar
knopp committed
1188 1189 1190
      ulsch_harq->o[7] = o_flip[0] ;
      ulsch_harq->o[6] = o_flip[1] ;
      ulsch_harq->o[5] = o_flip[2] ;
1191
      ulsch_harq->o[4] = o_flip[3] ;
knopp's avatar
knopp committed
1192 1193 1194
      ulsch_harq->o[3] = o_flip[4] ;
      ulsch_harq->o[2] = o_flip[5] ;
      ulsch_harq->o[1] = o_flip[6] ;
1195 1196
      ulsch_harq->o[0] = o_flip[7] ;

1197
    }
1198

1199
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
1200
    msg("ulsch_decoding: Or1=%d\n",ulsch_harq->Or1);
1201 1202

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

knopp's avatar
knopp committed
1205 1206
    if (ulsch_harq->cqi_crc_status == 1)
      msg("RX CQI CRC OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
1207
    else
knopp's avatar
knopp committed
1208
      msg("RX CQI CRC NOT OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
1209

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
#endif
  }

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

  //  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);


  r_offset = 0;
1220 1221 1222

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

1223
    // Get Turbo interleaver parameters
knopp's avatar
knopp committed
1224 1225
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
1226
    else
knopp's avatar
knopp committed
1227
      Kr = ulsch_harq->Kplus;
1228

1229
    Kr_bytes = Kr>>3;
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    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);
    }
1243 1244

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

1248
    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
1249 1250 1251
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);
1252

1253
#ifdef DEBUG_ULSCH_DECODING
1254
    msg("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
1255 1256 1257 1258 1259 1260
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif