ulsch_decoding.c 50.1 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;
knopp's avatar
knopp committed
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];
knopp's avatar
knopp committed
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

gauthier's avatar
gauthier committed
291
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING,1);
292 293 294

  // 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
295

296
  //  harq_pid = (ulsch->RRCConnRequest_flag == 0) ? subframe2harq_pid_tdd(frame_parms->tdd_config,subframe) : 0;
knopp's avatar
knopp committed
297
  harq_pid = subframe2harq_pid(frame_parms,phy_vars_eNB->proc[sched_subframe].frame_rx,subframe);
knopp's avatar
knopp committed
298
  ulsch_harq = ulsch->harq_processes[harq_pid];
299 300

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

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

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

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

318

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


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

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


  sumKr = 0;
352 353

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

359 360
    sumKr += Kr;
  }
361

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

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

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

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

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

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

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

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

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

436

437

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

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

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

464
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
465
  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);
466 467 468 469 470 471 472
#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;
knopp's avatar
knopp committed
473 474
  memset(y,LTE_NULL,Q_m*Hpp);
  /*
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 504
  //  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);
  }
505

506
  // 1-bit ACK/NAK
knopp's avatar
knopp committed
507
  if (ulsch_harq->O_ACK == 1) {
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    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
533
  if (ulsch_harq->O_ACK == 2) {
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 569
    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;
570
      q_ACK[9] = PUSCH_x;
571 572 573 574 575 576 577 578 579 580 581 582 583 584
      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
585
  if (ulsch_harq->O_ACK > 2) {
586 587 588
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }
knopp's avatar
knopp committed
589 590


591
  // RI BITS
592

knopp's avatar
knopp committed
593
  //  memset(ytag2,0,Q_m*Hpp);
594

knopp's avatar
knopp committed
595
  */
596 597 598 599 600 601
  // 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;
602 603 604

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

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

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

  j=0;

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

    for (q=2; q<Q_m; q++)
628 629 630 631 632 633 634 635 636 637 638 639 640
      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;
641 642

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

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

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



  i=0;
671

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

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

690
    break;
691

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

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

    break;

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

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

732 733 734 735
    break;
  }


736 737 738 739 740 741 742 743
  //      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);
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 769
  // 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
  */
770 771 772
  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);

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

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

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

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

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

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

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

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

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


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

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

832
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
833 834
      //      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]);
835 836 837
#endif
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
838

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


843
  // RI BITS
844

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

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

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

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

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

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

  j=0;

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

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

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

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

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

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

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

926
      if (ys>127)
927
        ulsch_harq->q[q+(Q_m*i)] = 127;
928
      else if (ys<-128)
929 930 931 932 933
        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
934
      msg("ulsch_decoding.c: CQI %d, q %d, y[%d] %d\n",q+(Q_m*i),q,j2, q+j2,ys);
935 936 937
#endif
    }

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

  // j2=j*Q_m;

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

knopp's avatar
knopp committed
951 952
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
953 954
#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))]);
955
#endif
956

957
    }
958

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

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

knopp's avatar
knopp committed
969 970 971 972
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
      ulsch_harq->e[iprime++] = y[j2++];
973 974
#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))]);
975
#endif
976

977
    }
978

979
    break;
980

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

knopp's avatar
knopp committed
988 989 990 991 992 993
      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++];
994 995
#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))]);
996
#endif
997

998
    }
999

1000 1001 1002
    break;

  }
1003

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

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

1018
    if (i<Qprime_CQI) {
1019

1020
      for (q=0;q<Q_m;q++) {
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  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
1031
      }
1032
    }
1033 1034
    else {
      for (q=0;q<Q_m;q++) {
1035 1036 1037 1038 1039
  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
1040 1041 1042 1043 1044 1045 1046 1047
      }
    }
    j++;
    if (j==Cmux) {
      j=0;
      r++;
    }
  }
1048
  */
1049 1050 1051

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

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

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

1068
#ifdef DEBUG_ULSCH_DECODING
1069
    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]);
1070 1071
#endif
  }
1072

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

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

      break;
1082

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

      break;
1089

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

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

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

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


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

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

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

#ifdef DEBUG_ULSCH_DECODING
1127 1128

  for (i=0; i<ulsch_harq->O_ACK; i++)
knopp's avatar
knopp committed
1129
    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]);
1130

1131 1132 1133 1134
#endif

  // RI

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

1139 1140 1141
#ifdef DEBUG_ULSCH_DECODING

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

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

1151 1152 1153 1154 1155 1156
#endif


  // CQI

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

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


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

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

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

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

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

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

1198
    }
1199

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

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

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

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

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

  //  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);


  r_offset = 0;
1221 1222 1223

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

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

1230
    Kr_bytes = Kr>>3;
1231

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

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

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

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

    start_meas(&phy_vars_eNB->ulsch_rate_unmatching_stats);
knopp's avatar
knopp committed
1264

knopp's avatar
knopp committed
1265
    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
                                   ulsch->Mdlharq,
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
                                   get_Qm_ul(ulsch_harq->mcs),
                                   1,
                                   r,
                                   &E)==-1) {
1280 1281 1282
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
1283

1284 1285 1286 1287
    stop_meas(&phy_vars_eNB->ulsch_rate_unmatching_stats);
    r_offset += E;
    /*
    msg("Subblock deinterleaving, d %p w %p\n",
1288 1289
     ulsch_harq->d[r],
     ulsch_harq->w);
1290 1291
    */
    start_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
1292 1293 1294
    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);
1295
    stop_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
1296 1297
    /*
    #ifdef DEBUG_ULSCH_DECODING
1298 1299
    msg("decoder input(segment %d) :",r);
    for (i=0;i<(3*8*Kr_bytes)+12;i++)
knopp's avatar
knopp committed
1300
      msg("%d : %d\n",i,ulsch_harq->d[r][96+i]);
1301
    msg("\n");
1302
    #endif
knopp's avatar
knopp committed
1303
    */
1304 1305 1306
  }

#ifdef OMP
1307
  #pragma omp parallel private(r,ret) shared(ulsch,harq_pid,crc_type,Kr,f1f2mat_old,phy_vars_eNB,status,iind,)
1308
  {
1309
    #pragma omp for nowait
1310
#endif
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327

    for (r=0; r<ulsch_harq->C; r++) {
      //    msg("Clearing c, %p\n",ulsch_harq->c[r]);
      //    memset(ulsch_harq->c[r],0,16);//block_length);
      //    msg("done\n");
      if (ulsch_harq->C == 1)
        crc_type = CRC24_A;
      else
        crc_type = CRC24_B;

      /*
        msg("decoder input(segment %d)\n",r);
        for (i=0;i<(3*8*Kr_bytes)+12;i++)
        if ((ulsch_harq->d[r][96+i]>7) ||
        (ulsch_harq->d[r][96+i] < -8))
        msg("%d : %d\n",i,ulsch_harq->d[r][96+i]);
        msg("\n");
1328
      */
1329

1330
      start_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);
1331

knopp's avatar
knopp committed
1332
      ret = tc(&ulsch_harq->d[r][96],
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
               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,
               &phy_vars_eNB->ulsch_tc_init_stats,
               &phy_vars_eNB->ulsch_tc_alpha_stats,
               &phy_vars_eNB->ulsch_tc_beta_stats,
               &phy_vars_eNB->ulsch_tc_gamma_stats,
               &phy_vars_eNB->ulsch_tc_ext_stats,
               &phy_vars_eNB->ulsch_tc_intl1_stats,
               &phy_vars_eNB->ulsch_tc_intl2_stats);

1348 1349 1350
      stop_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);

      status[r] = ret;
gauthier's avatar