ulsch_decoding.c 52.2 KB
Newer Older
1
/*******************************************************************************
ghaddab's avatar
ghaddab committed
2 3
    OpenAirInterface 
    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 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
   included in this distribution in the file called "COPYING". If not, 
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23 24 25
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
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 65 66 67 68 69 70 71 72 73 74 75

/*! \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

#include "UTIL/LOG/vcd_signal_dumper.h"
//#define DEBUG_ULSCH_DECODING

void free_eNB_ulsch(LTE_eNB_ULSCH_t *ulsch) {

  int i,r;

  if (ulsch) {
    for (i=0;i<ulsch->Mdlharq;i++) {
      if (ulsch->harq_processes[i]) {
76
	if (ulsch->harq_processes[i]->b) {
77
	  free16(ulsch->harq_processes[i]->b,MAX_ULSCH_PAYLOAD_BYTES);
78 79
	  ulsch->harq_processes[i]->b = NULL;
	}
80
	if (ulsch->harq_processes[i]->c) {
81
	  for (r=0;r<MAX_NUM_ULSCH_SEGMENTS;r++) {
82
	    free16(ulsch->harq_processes[i]->c[r],((r==0)?8:0) + 768);
83 84
	    ulsch->harq_processes[i]->c[r] = NULL;
	  }
85 86
	}
	for (r=0;r<MAX_NUM_ULSCH_SEGMENTS;r++)
87
	  if (ulsch->harq_processes[i]->d[r]) {
88
	    free16(ulsch->harq_processes[i]->d[r],((3*8*6144)+12+96)*sizeof(short));
89 90
	    ulsch->harq_processes[i]->d[r] = NULL;
	  }
91
	free16(ulsch->harq_processes[i],sizeof(LTE_UL_eNB_HARQ_t));
92
	ulsch->harq_processes[i] = NULL;
93 94 95
      }
    }
  free16(ulsch,sizeof(LTE_eNB_ULSCH_t));
96
  ulsch = NULL;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  }
}

LTE_eNB_ULSCH_t *new_eNB_ulsch(uint8_t Mdlharq,uint8_t max_turbo_iterations,uint8_t N_RB_UL, uint8_t abstraction_flag) {

  LTE_eNB_ULSCH_t *ulsch;
  uint8_t exit_flag = 0,i,r;
  unsigned char bw_scaling =1;
  
  switch (N_RB_UL){
  case 6: 
    bw_scaling =16;
    break;
  case 25:
    bw_scaling =4;
    break;
  case 50: 
    bw_scaling =2;
    break;
  default:
    bw_scaling =1;
    break;
  }
  
  ulsch = (LTE_eNB_ULSCH_t *)malloc16(sizeof(LTE_eNB_ULSCH_t));
  if (ulsch) {
123
    memset(ulsch,0,sizeof(LTE_eNB_ULSCH_t));
124 125 126 127 128 129 130
    ulsch->Mdlharq = Mdlharq;
    ulsch->max_turbo_iterations = max_turbo_iterations;

    for (i=0;i<Mdlharq;i++) {
      //      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));
      if (ulsch->harq_processes[i]) {
131
	memset(ulsch->harq_processes[i],0,sizeof(LTE_UL_eNB_HARQ_t));
132
	ulsch->harq_processes[i]->b = (uint8_t*)malloc16(MAX_ULSCH_PAYLOAD_BYTES/bw_scaling);
133 134 135
	if (ulsch->harq_processes[i]->b)
	  memset(ulsch->harq_processes[i]->b,0,MAX_ULSCH_PAYLOAD_BYTES/bw_scaling);
	else
136 137 138 139
	  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);	
140 141 142
	    if (ulsch->harq_processes[i]->c[r])
	      memset(ulsch->harq_processes[i]->c[r],0,((r==0)?8:0) + 3+768);
	    else
143 144
	      exit_flag=2;
	    ulsch->harq_processes[i]->d[r] = (short*)malloc16(((3*8*6144)+12+96)*sizeof(short));
145 146 147 148
	    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;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	  }
	  ulsch->harq_processes[i]->subframe_scheduling_flag = 0;
	}
      }	else {
	exit_flag=1;
      }
    }

    if (exit_flag==0)
      return(ulsch);
  }
  LOG_E(PHY,"new_ue_ulsch: exit_flag = %d\n",exit_flag);
  free_eNB_ulsch(ulsch);

  return(NULL);
}

void clean_eNb_ulsch(LTE_eNB_ULSCH_t *ulsch, uint8_t abstraction_flag) {

  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;
    for (i=0;i<Mdlharq;i++) {
      if (ulsch->harq_processes[i]) {
177
	//	  ulsch->harq_processes[i]->Ndi = 0;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	  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;
      }
    }

  }
}


uint8_t extract_cqi_crc(uint8_t *cqi,uint8_t CQI_LENGTH) {

  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,
			     uint8_t UE_id,
Raymond Knopp's avatar
 
Raymond Knopp committed
213
			     uint8_t sched_subframe,
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
			     uint8_t control_only_flag,
			     uint8_t Nbundled,
			     uint8_t llr8_flag) {


  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;
Raymond Knopp's avatar
 
Raymond Knopp committed
238
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
239 240 241 242 243 244 245 246 247 248 249 250 251
  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];
Raymond Knopp's avatar
 
Raymond Knopp committed
252
  int subframe = phy_vars_eNB->proc[sched_subframe].subframe_rx;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

  uint8_t (*tc)(int16_t *y,
		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 *);

  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING,1);

  // 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
  
  //  harq_pid = (ulsch->RRCConnRequest_flag == 0) ? subframe2harq_pid_tdd(frame_parms->tdd_config,subframe) : 0;
Raymond Knopp's avatar
 
Raymond Knopp committed
276
  harq_pid = subframe2harq_pid(frame_parms,phy_vars_eNB->proc[sched_subframe].frame_rx,subframe);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

  if (harq_pid==255) {
    LOG_E(PHY, "ulsch_decoding.c: FATAL ERROR: illegal harq_pid, returning\n");
    return(-1);
  }

  if (llr8_flag == 0)
    tc = phy_threegpplte_turbo_decoder16;
  else
    tc = phy_threegpplte_turbo_decoder8;
  
  nb_rb = ulsch->harq_processes[harq_pid]->nb_rb;

  A = ulsch->harq_processes[harq_pid]->TBS;

    
  Q_m = get_Qm_ul(ulsch->harq_processes[harq_pid]->mcs);
Raymond Knopp's avatar
 
Raymond Knopp committed
294
  G = nb_rb * (12 * Q_m) * ulsch->harq_processes[harq_pid]->Nsymb_pusch;
295 296 297


#ifdef DEBUG_ULSCH_DECODING
298
  LOG_D(PHY,"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",
299
      frame_parms->Nid_cell,ulsch->rnti,x2,
300
      ulsch->harq_processes[harq_pid]->round,
301 302
      ulsch->harq_processes[harq_pid]->rvidx,
      ulsch->harq_processes[harq_pid]->mcs,
Raymond Knopp's avatar
 
Raymond Knopp committed
303
      ulsch->harq_processes[harq_pid]->O_RI,
304 305 306 307 308
      ulsch->harq_processes[harq_pid]->O_ACK,
      G,
      subframe);
#endif  

309
  if (ulsch->harq_processes[harq_pid]->round == 0) {
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    // This is a new packet, so compute quantities regarding segmentation
    ulsch->harq_processes[harq_pid]->B = A+24;
    lte_segmentation(NULL,
		     NULL,
		     ulsch->harq_processes[harq_pid]->B,
		     &ulsch->harq_processes[harq_pid]->C,
		     &ulsch->harq_processes[harq_pid]->Cplus,
		     &ulsch->harq_processes[harq_pid]->Cminus,
		     &ulsch->harq_processes[harq_pid]->Kplus,
		     &ulsch->harq_processes[harq_pid]->Kminus,		     
		     &ulsch->harq_processes[harq_pid]->F);
    //  CLEAR LLR's HERE for first packet in process
  }


  sumKr = 0;
  for (r=0;r<ulsch->harq_processes[harq_pid]->C;r++) {
    if (r<ulsch->harq_processes[harq_pid]->Cminus)
      Kr = ulsch->harq_processes[harq_pid]->Kminus;
    else
      Kr = ulsch->harq_processes[harq_pid]->Kplus;
    sumKr += Kr;
  }
  if (sumKr==0) {
    LOG_N(PHY,"[eNB %d] ulsch_decoding.c: FATAL sumKr is 0!\n",phy_vars_eNB->Mod_id);
335
    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",
336 337
	frame_parms->Nid_cell,ulsch->rnti,x2,
	harq_pid,
338
	ulsch->harq_processes[harq_pid]->round,
339 340
	ulsch->harq_processes[harq_pid]->rvidx,
	ulsch->harq_processes[harq_pid]->mcs,
Raymond Knopp's avatar
 
Raymond Knopp committed
341
	ulsch->harq_processes[harq_pid]->O_RI,
342 343 344
	ulsch->harq_processes[harq_pid]->O_ACK,
	G,
	subframe);
Lionel Gauthier's avatar
Lionel Gauthier committed
345
    mac_xface->macphy_exit("ulsch_decoding.c: FATAL sumKr is 0!");
346 347 348 349
    return(-1);
  }
    
  // Compute Q_ri
Raymond Knopp's avatar
 
Raymond Knopp committed
350
  Qprime = ulsch->harq_processes[harq_pid]->O_RI*ulsch->harq_processes[harq_pid]->Msc_initial*ulsch->harq_processes[harq_pid]->Nsymb_initial * ulsch->beta_offset_ri_times8;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

  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_RI = Q_m*Qprime;
  Qprime_RI = Qprime;


  // Compute Q_ack

  Qprime = ulsch->harq_processes[harq_pid]->O_ACK*ulsch->harq_processes[harq_pid]->Msc_initial*ulsch->harq_processes[harq_pid]->Nsymb_initial * ulsch->beta_offset_harqack_times8;
  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
  LOG_D(PHY,"ulsch_decoding.c: Qprime_ACK %d, Msc_initial %d, Nsymb_initial %d, sumKr %d\n",
      Qprime_ACK,ulsch->harq_processes[harq_pid]->Msc_initial,ulsch->harq_processes[harq_pid]->Nsymb_initial,sumKr);
#endif
  // Compute Q_cqi
Raymond Knopp's avatar
 
Raymond Knopp committed
386
  if (ulsch->harq_processes[harq_pid]->Or1 < 12)
387 388 389
    L=0;
  else 
    L=8;
Raymond Knopp's avatar
 
Raymond Knopp committed
390 391
  if (ulsch->harq_processes[harq_pid]->Or1 > 0)
    Qprime = (ulsch->harq_processes[harq_pid]->Or1 + L) * ulsch->harq_processes[harq_pid]->Msc_initial*ulsch->harq_processes[harq_pid]->Nsymb_initial * ulsch->beta_offset_cqi_times8;
392 393 394 395 396 397 398 399 400 401
  else
    Qprime=0;

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

Raymond Knopp's avatar
 
Raymond Knopp committed
402
  G = nb_rb * (12 * Q_m) * (ulsch->harq_processes[harq_pid]->Nsymb_pusch);
403 404 405

 

Raymond Knopp's avatar
 
Raymond Knopp committed
406 407
  if (Qprime > (G - ulsch->harq_processes[harq_pid]->O_RI))
    Qprime = G - ulsch->harq_processes[harq_pid]->O_RI;
408 409

  Q_CQI = Q_m * Qprime;
Raymond Knopp's avatar
 
Raymond Knopp committed
410
  //#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
 
Raymond Knopp committed
411
    LOG_D(PHY,"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_processes[harq_pid]->Or1,ulsch->harq_processes[harq_pid]->O_ACK); 
Raymond Knopp's avatar
 
Raymond Knopp committed
412
  //#endif
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  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;
  
Raymond Knopp's avatar
 
Raymond Knopp committed
428
  Cmux       = ulsch->harq_processes[harq_pid]->Nsymb_pusch;
429 430 431 432 433 434 435 436 437 438 439 440
  //  Rmux       = Hpp*Q_m/Cmux;
  Rmux_prime = Hpp/Cmux;
  
#ifdef DEBUG_ULSCH_DECODING
  LOG_D(PHY,"ulsch_decoding.c: G raw %d (%d symb), Hpp %d, Cmux %d, Rmux_prime %d\n",G,ulsch->Nsymb_pusch,Hpp,Cmux,Rmux_prime);
#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;
Raymond Knopp's avatar
 
Raymond Knopp committed
441 442
  memset(y,LTE_NULL,Q_m*Hpp);
  /*
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  //  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);
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
473
 
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  // 1-bit ACK/NAK
  if (ulsch->harq_processes[harq_pid]->O_ACK == 1) {
    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
  if (ulsch->harq_processes[harq_pid]->O_ACK == 2) {
    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;
      q_ACK[9] = PUSCH_x; 
      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;
    }
  }
  if (ulsch->harq_processes[harq_pid]->O_ACK > 2) {
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }
Raymond Knopp's avatar
 
Raymond Knopp committed
557 558


559 560
  // RI BITS 

Raymond Knopp's avatar
 
Raymond Knopp committed
561
  //  memset(ytag2,0,Q_m*Hpp);
562

Raymond Knopp's avatar
 
Raymond Knopp committed
563
  */
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
  // 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;
  for (i=0;i<((Hpp*Q_m)>>5);i++) {
    for (j=0;j<32;j++) {
      cseq[i2++] = (int16_t)((((s>>j)&1)<<1)-1);
    }
    s = lte_gold_generic(&x1, &x2, 0);
  }

  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
  j=0;   
  for (i=0;i<Qprime_RI;i++) {
    r = Rmux_prime - 1 - (i>>2);
    /*    
    for (q=0;q<Q_m;q++) 
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m)); 
    cseq[off+1] = cseq[off];  // PUSCH_y
    for (q=2;q<Q_m;q++)
      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;
  for (i=0;i<Qprime_ACK;i++) {
    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];
    }
    */
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m)); 
    if (ulsch->harq_processes[harq_pid]->O_ACK == 1) {
      if (ulsch->bundling==0)
	cseq[off+1] = cseq[off];  // PUSCH_y
      for (q=2;q<Q_m;q++)
	cseq[off+q] = -1;    // PUSCH_x      
    }
    else if (ulsch->harq_processes[harq_pid]->O_ACK == 2) {
      for (q=2;q<Q_m;q++)
	cseq[off+q] = -1;    // PUSCH_x 
    }
#ifdef DEBUG_ULSCH_DECODING
    LOG_D(PHY,"ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]); 
#endif
    j=(j+3)&3;
  }



  i=0;
  switch (Q_m) {
  case 2:
    for (j=0;j<Cmux;j++) {
      i2=j<<1;
      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);
      }
    }
    break;
  case 4:
    for (j=0;j<Cmux;j++) {
      i2=j<<2;
      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);
      }
    }
    break; 
  case 6:
    for (j=0;j<Cmux;j++) {
      i2=j*6;
      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);
      }
    }
    break;
  }

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


	// 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
	*/
  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);

  if (i!=(H+Q_RI))
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI); 

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

  if (ulsch->harq_processes[harq_pid]->O_ACK == 1) {
    switch (Q_m) {
    case 2:
      len_ACK = 2;
      break;
    case 4:
      len_ACK = 4;
      break;
    case 6:
      len_ACK = 6;
      break;
    }
  }
  if (ulsch->harq_processes[harq_pid]->O_ACK == 2) {
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
    case 4:
      len_ACK = 12;
      break;
    case 6:
      len_ACK = 18;
      break;
    }
  }
  if (ulsch->harq_processes[harq_pid]->O_ACK > 2) {
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }

  for (i=0;i<len_ACK;i++)
Raymond Knopp's avatar
 
Raymond Knopp committed
767
    ulsch->harq_processes[harq_pid]->q_ACK[i] = 0;
768 769 770 771 772 773


  for (i=0;i<Qprime_ACK;i++) {
    r = Rmux_prime -1 - (i>>2);
    for (q=0;q<Q_m;q++) {
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
Raymond Knopp's avatar
 
Raymond Knopp committed
774
	ulsch->harq_processes[harq_pid]->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
775
#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
 
Raymond Knopp committed
776 777
      //      LOG_D(PHY,"ACK %d => %d (%d,%d,%d)\n",(q+(Q_m*i))%len_ACK,ulsch->harq_processes[harq_pid]->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_processes[harq_pid]->q_ACK[(q+(Q_m*i))%len_ACK],q+(Q_m*((r*Cmux) + columnset[j])),r,columnset[j]);
778 779 780 781 782 783 784 785 786
#endif
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
    j=(j+3)&3;
  }


  // RI BITS 

Raymond Knopp's avatar
 
Raymond Knopp committed
787
  if (ulsch->harq_processes[harq_pid]->O_RI == 1) {
788 789 790 791 792 793 794 795 796 797 798 799 800
    switch (Q_m) {
    case 2:
        len_RI=2;
      break;
    case 4:
      len_RI=4;
      break;
    case 6:
      len_RI=6;
      break;
    }
  }

Raymond Knopp's avatar
 
Raymond Knopp committed
801
  if (ulsch->harq_processes[harq_pid]->O_RI > 1) {
802 803 804 805 806
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

  for (i=0;i<len_RI;i++)
Raymond Knopp's avatar
 
Raymond Knopp committed
807
    ulsch->harq_processes[harq_pid]->q_RI[i] = 0;
808 809 810 811 812 813 814 815 816
 
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
  j=0;   
  for (i=0;i<Qprime_RI;i++) {
    r = Rmux_prime -1 - (i>>2);
    for (q=0;q<Q_m;q++) 
Raymond Knopp's avatar
 
Raymond Knopp committed
817
      ulsch->harq_processes[harq_pid]->q_RI[(q+(Q_m*i))%len_RI] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

  // CQI and Data bits
  j=0;j2=0;
  //  r=0;
  for (i=0;i<Qprime_CQI;i++) {

    /*
    while (ytag[(r*Cmux)+j]==LTE_NULL) {
#ifdef DEBUG_ULSCH_DECODING	
      msg("ulsch_decoding.c: r %d, j %d: LTE_NULL\n",r,j);
#endif
      j++;
      if (j==Cmux) {
	j=0;
	r++;
      }
    }
    for (q=0;q<Q_m;q++) {
      ys = y[q+(Q_m*((r*Cmux)+j))];
      if (ys>127)
Raymond Knopp's avatar
 
Raymond Knopp committed
841
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = 127;
842
      else if (ys<-128)
Raymond Knopp's avatar
 
Raymond Knopp committed
843
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = -128;
844
      else 
Raymond Knopp's avatar
 
Raymond Knopp committed
845
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = ys;
846 847 848 849 850 851 852 853 854 855 856
#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
    } 
    */
    while (ytag[j]==LTE_NULL){j++;j2+=Q_m;}

    for (q=0;q<Q_m;q++) {
      //      ys = y[q+(Q_m*((r*Cmux)+j))];
      ys = y[q+j2];
      if (ys>127)
Raymond Knopp's avatar
 
Raymond Knopp committed
857
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = 127;
858
      else if (ys<-128)
Raymond Knopp's avatar
 
Raymond Knopp committed
859
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = -128;
860
      else 
Raymond Knopp's avatar
 
Raymond Knopp committed
861
	ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = ys;
862 863 864 865 866 867 868 869 870 871 872 873 874 875
 #ifdef DEBUG_ULSCH_DECODING	
      LOG_D(PHY,"ulsch_decoding.c: CQI %d, q %d, y[%d] %d\n",q+(Q_m*i),q,j2, q+j2,ys);
 #endif
    } 
    j2+=Q_m;
  }

  // j2=j*Q_m;

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

Raymond Knopp's avatar
 
Raymond Knopp committed
876 877
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
878 879 880 881 882
#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
      
    }
883
    //    write_output("/tmp/ulsch_e.m","ulsch_e",ulsch->e,iprime,1,0);
884 885 886 887 888
    break;
  case 4:
    for (iprime=0;iprime<(Hprime-Qprime_CQI)<<2;) {
      while (ytag[j]==LTE_NULL) { j++;j2+=4; }

Raymond Knopp's avatar
 
Raymond Knopp committed
889 890 891 892
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
893 894 895 896 897 898 899 900 901 902
#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
      
    }
    break;
  case 6:
    for (iprime=0;iprime<(Hprime-Qprime_CQI)*6;) {
      while (ytag[j]==LTE_NULL) { j++;j2+=6; }

Raymond Knopp's avatar
 
Raymond Knopp committed
903 904 905 906 907 908
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
      ulsch->harq_processes[harq_pid]->e[iprime++] = y[j2++];
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
#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
      
    }
    break;

  }
  /*
  for (i=0,iprime=-Qprime_CQI;i<Hprime;i++,iprime++) {

    while (ytag[(r*Cmux)+j]==LTE_NULL) {
#ifdef DEBUG_ULSCH_DECODING	
      msg("ulsch_decoding.c: r %d, j %d: LTE_NULL\n",r,j);
#endif
      j++;
      if (j==Cmux) {
	j=0;
	r++;
      }
    }
    
    if (i<Qprime_CQI) {
      
      for (q=0;q<Q_m;q++) {
	ys = y[q+(Q_m*((r*Cmux)+j))];
	if (ys>127)
Raymond Knopp's avatar
 
Raymond Knopp committed
936
	  ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = 127;
937
	else if (ys<-128)
Raymond Knopp's avatar
 
Raymond Knopp committed
938
	  ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = -128;
939
	else 
Raymond Knopp's avatar
 
Raymond Knopp committed
940
	  ulsch->harq_processes[harq_pid]->q[q+(Q_m*i)] = ys;
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
#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
      }
    } 
    else {
      for (q=0;q<Q_m;q++) {
	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
      }
    }
    j++;
    if (j==Cmux) {
      j=0;
      r++;
    }
  }
*/

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

  // HARQ-ACK 
  wACK_idx = (ulsch->bundling==0) ? 4 : ((Nbundled-1)&3);
#ifdef DEBUG_ULSCH_DECODING
  LOG_D(PHY,"ulsch_decoding.c: Bundling %d, Nbundled %d, wACK_idx %d\n",
      ulsch->bundling,Nbundled,wACK_idx);
#endif
  if (ulsch->harq_processes[harq_pid]->O_ACK == 1) {
Raymond Knopp's avatar
 
Raymond Knopp committed
972 973
      ulsch->harq_processes[harq_pid]->q_ACK[0] *= wACK_RX[wACK_idx][0]; 
      ulsch->harq_processes[harq_pid]->q_ACK[0] += (ulsch->bundling==0) ? ulsch->harq_processes[harq_pid]->q_ACK[1]*wACK_RX[wACK_idx][0] : ulsch->harq_processes[harq_pid]->q_ACK[1]*wACK_RX[wACK_idx][1];
974
  
Raymond Knopp's avatar
 
Raymond Knopp committed
975 976
      if (ulsch->harq_processes[harq_pid]->q_ACK[0] < 0)
	ulsch->harq_processes[harq_pid]->o_ACK[0] = 0;
977
      else
Raymond Knopp's avatar
 
Raymond Knopp committed
978
	ulsch->harq_processes[harq_pid]->o_ACK[0] = 1;
979
#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
 
Raymond Knopp committed
980
      LOG_D(PHY,"ulsch_decoding.c: ulsch_q_ACK[0] %d (%d,%d)\n",ulsch->harq_processes[harq_pid]->q_ACK[0],wACK_RX[wACK_idx][0],wACK_RX[wACK_idx][1]);
981 982 983 984 985 986
#endif
  }
  if (ulsch->harq_processes[harq_pid]->O_ACK == 2) {
    switch (Q_m) {

    case 2:
Raymond Knopp's avatar
 
Raymond Knopp committed
987 988 989
      ulsch->harq_processes[harq_pid]->q_ACK[0] = ulsch->harq_processes[harq_pid]->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[3]*wACK_RX[wACK_idx][1];
      ulsch->harq_processes[harq_pid]->q_ACK[1] = ulsch->harq_processes[harq_pid]->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[4]*wACK_RX[wACK_idx][1];
      ulsch->harq_processes[harq_pid]->q_ACK[2] = ulsch->harq_processes[harq_pid]->q_ACK[2]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[5]*wACK_RX[wACK_idx][1];
990 991 992

      break;
    case 4:
Raymond Knopp's avatar
 
Raymond Knopp committed
993 994 995
      ulsch->harq_processes[harq_pid]->q_ACK[0] = ulsch->harq_processes[harq_pid]->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[5]*wACK_RX[wACK_idx][1];
      ulsch->harq_processes[harq_pid]->q_ACK[1] = ulsch->harq_processes[harq_pid]->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[8]*wACK_RX[wACK_idx][1];
      ulsch->harq_processes[harq_pid]->q_ACK[2] = ulsch->harq_processes[harq_pid]->q_ACK[4]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[9]*wACK_RX[wACK_idx][1];
996 997 998

      break;
    case 6:
Raymond Knopp's avatar
 
Raymond Knopp committed
999 1000 1001
      ulsch->harq_processes[harq_pid]->q_ACK[0] =  ulsch->harq_processes[harq_pid]->q_ACK[0]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[7]*wACK_RX[wACK_idx][1];
      ulsch->harq_processes[harq_pid]->q_ACK[1] =  ulsch->harq_processes[harq_pid]->q_ACK[1]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[12]*wACK_RX[wACK_idx][1]; 
      ulsch->harq_processes[harq_pid]->q_ACK[2] =  ulsch->harq_processes[harq_pid]->q_ACK[6]*wACK_RX[wACK_idx][0] + ulsch->harq_processes[harq_pid]->q_ACK[13]*wACK_RX[wACK_idx][1]; 
1002 1003
      break;
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1004 1005 1006 1007
    ulsch->harq_processes[harq_pid]->o_ACK[0] = 1;
    ulsch->harq_processes[harq_pid]->o_ACK[1] = 1;
    metric     = ulsch->harq_processes[harq_pid]->q_ACK[0]+ulsch->harq_processes[harq_pid]->q_ACK[1]-ulsch->harq_processes[harq_pid]->q_ACK[2];
    metric_new = -ulsch->harq_processes[harq_pid]->q_ACK[0]+ulsch->harq_processes[harq_pid]->q_ACK[1]+ulsch->harq_processes[harq_pid]->q_ACK[2];
1008 1009

    if (metric_new > metric) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1010 1011
      ulsch->harq_processes[harq_pid]->o_ACK[0]=0;
      ulsch->harq_processes[harq_pid]->o_ACK[1]=1;
1012 1013
      metric = metric_new;
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1014
    metric_new = ulsch->harq_processes[harq_pid]->q_ACK[0]-ulsch->harq_processes[harq_pid]->q_ACK[1]+ulsch->harq_processes[harq_pid]->q_ACK[2];
1015 1016 1017


    if (metric_new > metric) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1018 1019
      ulsch->harq_processes[harq_pid]->o_ACK[0] = 1;
      ulsch->harq_processes[harq_pid]->o_ACK[1] = 0;
1020 1021
      metric = metric_new;
    }
Raymond Knopp's avatar
 
Raymond Knopp committed
1022
    metric_new = -ulsch->harq_processes[harq_pid]->q_ACK[0]-ulsch->harq_processes[harq_pid]->q_ACK[1]-ulsch->harq_processes[harq_pid]->q_ACK[2];
1023 1024

    if (metric_new > metric) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1025 1026
      ulsch->harq_processes[harq_pid]->o_ACK[0] = 0;
      ulsch->harq_processes[harq_pid]->o_ACK[1] = 0;
1027 1028 1029 1030 1031
      metric = metric_new;
    }
  }

#ifdef DEBUG_ULSCH_DECODING
Raymond Knopp's avatar
 
Raymond Knopp committed
1032 1033
  for (i=0;i<ulsch->harq_processes[harq_pid]->harq_processes[harq_pid]->O_ACK;i++)
    LOG_D(PHY,"ulsch_decoding: O_ACK[%d] %d, q_ACK => (%d,%d,%d)\n",i,ulsch->harq_processes[harq_pid]->o_ACK[i],ulsch->harq_processes[harq_pid]->q_ACK[0],ulsch->harq_processes[harq_pid]->q_ACK[1],ulsch->harq_processes[harq_pid]->q_ACK[2]);
1034 1035 1036 1037
#endif

  // RI

Raymond Knopp's avatar
 
Raymond Knopp committed
1038 1039
  if ((ulsch->harq_processes[harq_pid]->O_RI == 1) && (Qprime_RI > 0)) {
    ulsch->harq_processes[harq_pid]->o_RI[0] = ((ulsch->harq_processes[harq_pid]->q_RI[0] + ulsch->harq_processes[harq_pid]->q_RI[Q_m/2]) > 0) ? 0 : 1; 
1040 1041 1042 1043
  }
#ifdef DEBUG_ULSCH_DECODING

  if (Qprime_RI > 0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1044 1045
    for (i=0;i<2*ulsch->harq_processes[harq_pid]->O_RI;i++)
      LOG_D(PHY,"ulsch_decoding: q_RI[%d] %d\n",i,ulsch->harq_processes[harq_pid]->q_RI[i]);
1046 1047 1048
  }
  
  if (Qprime_CQI > 0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1049 1050
    for (i=0;i<ulsch->harq_processes[harq_pid]->O_RI;i++)
      LOG_D(PHY,"ulsch_decoding: O_RI[%d] %d\n",i,ulsch->harq_processes[harq_pid]->o_RI[i]);
1051 1052 1053 1054 1055 1056 1057
  }
#endif


  // CQI

  if (Qprime_CQI>0) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1058
    memset((void *)&dummy_w_cc[0],0,3*(ulsch->harq_processes[harq_pid]->Or1+8+32));
1059
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1060
    O_RCC = generate_dummy_w_cc(ulsch->harq_processes[harq_pid]->Or1+8,
1061 1062 1063 1064 1065
				&dummy_w_cc[0]);
    
    
    lte_rate_matching_cc_rx(O_RCC,
			    Q_CQI,
Raymond Knopp's avatar
 
Raymond Knopp committed
1066
			    ulsch->harq_processes[harq_pid]->o_w,
1067
			    dummy_w_cc,
Raymond Knopp's avatar
 
Raymond Knopp committed
1068
			    ulsch->harq_processes[harq_pid]->q);
1069
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1070 1071 1072
    sub_block_deinterleaving_cc((unsigned int)(ulsch->harq_processes[harq_pid]->Or1+8),
				&ulsch->harq_processes[harq_pid]->o_d[96], 
				&ulsch->harq_processes[harq_pid]->o_w[0]); 
1073
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1074 1075
    memset(o_flip,0,1+((8+ulsch->harq_processes[harq_pid]->Or1)/8));
    phy_viterbi_lte_sse2(ulsch->harq_processes[harq_pid]->o_d+96,o_flip,8+ulsch->harq_processes[harq_pid]->Or1);
1076
    
Raymond Knopp's avatar
 
Raymond Knopp committed
1077 1078
    if (extract_cqi_crc(o_flip,ulsch->harq_processes[harq_pid]->Or1) == (crc8(o_flip,ulsch->harq_processes[harq_pid]->Or1)>>24))
      ulsch->harq_processes[harq_pid]->cqi_crc_status = 1;
1079
    else
Raymond Knopp's avatar
 
Raymond Knopp committed
1080
      ulsch->harq_processes[harq_pid]->cqi_crc_status = 0;
1081 1082 1083

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

Raymond Knopp's avatar
 
Raymond Knopp committed
1084 1085 1086 1087 1088
    if (ulsch->harq_processes[harq_pid]->Or1<=32) {
      ulsch->harq_processes[harq_pid]->o[3] = o_flip[0] ;
      ulsch->harq_processes[harq_pid]->o[2] = o_flip[1] ;
      ulsch->harq_processes[harq_pid]->o[1] = o_flip[2] ;
      ulsch->harq_processes[harq_pid]->o[0] = o_flip[3] ;   
1089 1090
    }
    else {
Raymond Knopp's avatar
 
Raymond Knopp committed
1091 1092 1093 1094 1095 1096 1097 1098
      ulsch->harq_processes[harq_pid]->o[7] = o_flip[0] ;
      ulsch->harq_processes[harq_pid]->o[6] = o_flip[1] ;
      ulsch->harq_processes[harq_pid]->o[5] = o_flip[2] ;
      ulsch->harq_processes[harq_pid]->o[4] = o_flip[3] ;   
      ulsch->harq_processes[harq_pid]->o[3] = o_flip[4] ;
      ulsch->harq_processes[harq_pid]->o[2] = o_flip[5] ;
      ulsch->harq_processes[harq_pid]->o[1] = o_flip[6] ;
      ulsch->harq_processes[harq_pid]->o[0] = o_flip[7] ;   
1099 1100 1101 1102 1103
      
    }
    
#ifdef DEBUG_ULSCH_DECODING
    LOG_D(PHY,"ulsch_decoding: Or1=%d\n",ulsch->Or1);
Raymond Knopp's avatar
 
Raymond Knopp committed
1104 1105 1106 1107
    for (i=0;i<1+((8+ulsch->harq_processes[harq_pid]->Or1)/8);i++)
      msg("ulsch_decoding: O[%d] %d\n",i,ulsch->harq_processes[harq_pid]->o[i]);
    if (ulsch->harq_processes[harq_pid]->cqi_crc_status == 1)
      msg("RX CQI CRC OK (%x)\n",extract_cqi_crc(o_flip,ulsch->harq_processes[harq_pid]->Or1));
1108
    else
Raymond Knopp's avatar
 
Raymond Knopp committed
1109
      msg("RX CQI CRC NOT OK (%x)\n",extract_cqi_crc(o_flip,ulsch->harq_processes[harq_pid]->Or1));
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
#endif
  }

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

  //  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);


  r_offset = 0;
  for (r=0;r<ulsch->harq_processes[harq_pid]->C;r++) {
    
    // Get Turbo interleaver parameters
    if (r<ulsch->harq_processes[harq_pid]->Cminus)
      Kr = ulsch->harq_processes[harq_pid]->Kminus;
    else
      Kr = ulsch->harq_processes[harq_pid]->Kplus;
    Kr_bytes = Kr>>3;
    
    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);
    }
    
#ifdef DEBUG_ULSCH_DECODING     
    msg("f1 %d, f2 %d, F %d\n",f1f2mat_old[2*iind],f1f2mat_old[1+(2*iind)],(r==0) ? ulsch->harq_processes[harq_pid]->F : 0);
#endif
    
    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
    ulsch->harq_processes[harq_pid]->RTC[r] = generate_dummy_w(4+(Kr_bytes*8), 
							       (uint8_t*)&dummy_w[r][0],
							       (r==0) ? ulsch->harq_processes[harq_pid]->F : 0);

#ifdef DEBUG_ULSCH_DECODING    
    msg("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
	   r, G,
	   Kr*3,
	   Q_m,
	   nb_rb,
	   ulsch->harq_processes[harq_pid]->Nl);
#endif    

    start_meas(&phy_vars_eNB->ulsch_rate_unmatching_stats);
Raymond Knopp's avatar
 
Raymond Knopp committed
1161

1162 1163 1164 1165
    if (lte_rate_matching_turbo_rx(ulsch->harq_processes[harq_pid]->RTC[r],
				   G,
				   ulsch->harq_processes[harq_pid]->w[r],
				   (uint8_t*) &dummy_w[r][0],
Raymond Knopp's avatar
 
Raymond Knopp committed
1166
				   ulsch->harq_processes[harq_pid]->e+r_offset,
1167 1168 1169 1170 1171
				   ulsch->harq_processes[harq_pid]->C,
				   NSOFT,
				   ulsch->Mdlharq,
				   1,
				   ulsch->harq_processes[harq_pid]->rvidx,
1172
				   (ulsch->harq_processes[harq_pid]->round==0)?1:0,  // clear
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
				   get_Qm_ul(ulsch->harq_processes[harq_pid]->mcs),
				   1,
				   r,
				   &E)==-1) {
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
    stop_meas(&phy_vars_eNB->ulsch_rate_unmatching_stats);
    r_offset += E;
    /*
    msg("Subblock deinterleaving, d %p w %p\n",
	   ulsch->harq_processes[harq_pid]->d[r],
	   ulsch->harq_processes[harq_pid]->w);
    */
    start_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
    sub_block_deinterleaving_turbo(4+Kr, 
				   &ulsch->harq_processes[harq_pid]->d[r][96], 
				   ulsch->harq_processes[harq_pid]->w[r]); 
    stop_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
Raymond Knopp's avatar
 
Raymond Knopp committed
1192 1193
    /*        
#ifdef DEBUG_ULSCH_DECODING    
1194 1195 1196 1197
    msg("decoder input(segment %d) :",r);
    for (i=0;i<(3*8*Kr_bytes)+12;i++)
      msg("%d : %d\n",i,ulsch->harq_processes[harq_pid]->d[r][96+i]);
    msg("\n");
Raymond Knopp's avatar
 
Raymond Knopp committed
1198 1199
#endif
    */
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 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 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
  }

#ifdef OMP
#pragma omp parallel private(r,ret) shared(ulsch,harq_pid,crc_type,Kr,f1f2mat_old,phy_vars_eNB,status,iind,)
  {
#pragma omp for nowait
#endif
    for (r=0;r<ulsch->harq_processes[harq_pid]->C;r++) {
    //    msg("Clearing c, %p\n",ulsch->harq_processes[harq_pid]->c[r]);
    //    memset(ulsch->harq_processes[harq_pid]->c[r],0,16);//block_length);
    //    msg("done\n");
      if (ulsch->harq_processes[harq_pid]->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_processes[harq_pid]->d[r][96+i]>7) || 
		    (ulsch->harq_processes[harq_pid]->d[r][96+i] < -8))
		    msg("%d : %d\n",i,ulsch->harq_processes[harq_pid]->d[r][96+i]);
		    msg("\n");
      */
      
      start_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);
      
      ret = tc(&ulsch->harq_processes[harq_pid]->d[r][96],
	       ulsch->harq_processes[harq_pid]->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_processes[harq_pid]->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);
      
      stop_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);

      status[r] = ret;
      if (ret==(1+ulsch->max_turbo_iterations)) {// a Code segment is in error so break;
#ifdef DEBUG_ULSCH_DECODING    
	msg("ULSCH harq_pid %d CRC failed\n",harq_pid);
#endif
	/*
	  for (i=0;i<Kr_bytes;i++)
	  printf("segment %d : byte %d => %d\n",r,i,ulsch->harq_processes[harq_pid]->c[r][i]);
	  return(ret);
	*/
      }
#ifdef DEBUG_ULSCH_DECODING    
      else
	msg("ULSCH harq_pid %d CRC OK : %d iterations\n",harq_pid, ret);
#endif

    }
#ifdef OMP
  }
#endif
  // Reassembly of Transport block here
  offset = 0;
  //  msg("F %d, Fbytes %d\n",ulsch->harq_processes[harq_pid]->F,ulsch->harq_processes[harq_pid]->F>>3);

  ret = 1;
  for (r=0;r<ulsch->harq_processes[harq_pid]->C;r++) {
    if (status[r] != (1+ulsch->max_turbo_iterations)) {
      if (r<ulsch->harq_processes[harq_pid]->Cminus)
	Kr = ulsch->harq_processes[harq_pid]->Kminus;
      else
	Kr = ulsch->harq_processes[harq_pid]->Kplus;
      
      Kr_bytes = Kr>>3;
      
      if (r==0) {
	memcpy(ulsch->harq_processes[harq_pid]->b,
	       &ulsch->harq_processes[harq_pid]->c[0][(ulsch->harq_processes[harq_pid]->F>>3)],
	       Kr_bytes - (ulsch->harq_processes[harq_pid]->F>>3) - ((ulsch->harq_processes[harq_pid]->C>1)?3:0));
	offset = Kr_bytes - (ulsch->harq_processes[harq_pid]->F>>3) - ((ulsch->harq_processes[harq_pid]->C>1)?3:0);
	//            msg("copied %d bytes to b sequence\n",
	//      	     Kr_bytes - (ulsch->harq_processes[harq_pid]->F>>3));
      }
      else {
	memcpy(ulsch->harq_processes[harq_pid]->b+offset,
	       ulsch->harq_processes[harq_pid]->c[r],
	       Kr_bytes - ((ulsch->harq_processes[harq_pid]->C>1)?3:0));
	offset += (Kr_bytes- ((ulsch->harq_processes[harq_pid]->C>1)?3:0));
      }
      if (ret != (1+ulsch->max_turbo_iterations))
	ret = status[r];
    }
    else {
      ret = 1+ulsch->max_turbo_iterations;
    }
    
  }
  vcd_signal_dumper_dump_function_by_name(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING,0);
  
  return(ret);
}

#ifdef PHY_ABSTRACTION

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

   int index,ii;
  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
  
   
  sinr_eff *= 10;
  sinr_eff = floor(sinr_eff);
  sinr_eff /= 10;
  
  LOG_D(PHY,"[ABSTRACTION] sinr_eff after rounding = %f\n",sinr_eff);
  for (index = 0; index < 16; index++) {
    if(index == 0) {
      if (sinr_eff < sinr_bler_map_up[mcs][0][index]) {
        bler = 1;
        break;
      }
    }
    if (sinr_eff == sinr_bler_map_up[mcs][0][index]) {
        bler = sinr_bler_map_up[mcs][1][index];
    }
  }
#ifdef USER_MODE // need to be adapted for the emulation in the kernel space 
   if (uniformrandom() < bler) {
     LOG_I(OCM,"abstraction_decoding failed (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(0);
  }
  else {
    LOG_I(OCM,"abstraction_decoding successful (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(1);
  }
#endif
}







int ulsch_abstraction_MIESM(double* sinr_dB,uint8_t TM, uint8_t mcs,uint16_t nrb, uint16_t frb) {
  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;
  for (offset = frb; offset <= (frb + nrb -1); offset++) {

      rb_count++;
     
      //we need to do the table lookups here for the mutual information corresponding to the certain sinr_dB. 
      
      sinr_db1 = sinr_dB[offset*2];
      sinr_db2 = sinr_dB[offset*2+1];

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

      //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][mcs]);
	     M +=2;
	    break;
	  }
	}
      
      //for sinr_db2
	for (index = 0; index < 162; index++) {
	    if (sinr_db2 < MI_map_4qam[0][0]) {
	      SI += (MI_map_4qam[1][0]/beta1_dlsch_MI[TM][mcs]);
	       M +=2;
	    break;
	    }
	     if (sinr_db2 > MI_map_4qam[0][161]) {
	       SI += (MI_map_4qam[1][161]/beta1_dlsch_MI[TM][mcs]);
	        M +=2;
	    break;
	    }
	  
	  if (sinr_db2 == MI_map_4qam[0][index]) {
	    SI += (MI_map_4qam[1][index]/beta1_dlsch_MI[TM][mcs]);
	     M +=2;
	    break;
	  }
	}
	
      }
      else if(mcs>9 && mcs<17)
	{
	  //for sinr_db1
	  for (index = 0; index < 197; index++) {
	    if (sinr_db1 < MI_map_16qam[0][0]) {
	      SI += (MI_map_16qam[1][0]/beta1_dlsch_MI[TM][mcs]);
	       M +=4;
	      break;
	    }
	    if (sinr_db1 > MI_map_16qam[0][196]) {
	      SI += (MI_map_16qam[1][196]/beta1_dlsch_MI[TM][mcs]);
	      M +=4;
	      break;
	    }
	    
	    if (sinr_db1 == MI_map_16qam[0][index]) {
	      SI += (MI_map_16qam[1][index]/beta1_dlsch_MI[TM][mcs]);
	      M +=4;
	    break;
	  }
	  }
	  
	  //for sinr_db2
	  for (index = 0; index < 197; index++) {
	    if (sinr_db2 < MI_map_16qam[0][0]) {
	      SI += (MI_map_16qam[1][0]/beta1_dlsch_MI[TM][mcs]);
	      M +=4;
	      break;
	    }
	    if (sinr_db2 > MI_map_16qam[0][196]) {
	      SI += (MI_map_16qam[1][196]/beta1_dlsch_MI[TM][mcs]);
	      M +=4;
	      break;
	    }
	    
	    if (sinr_db2 == MI_map_16qam[0][index]) {
	      SI += (MI_map_16qam[1][index]/beta1_dlsch_MI[TM][mcs]);
	      M +=4;
	    break;
	    }
	  }
	  
	}
      else if(mcs>16 && mcs<22)
	{
	  	//for sinr_db1
	for (index = 0; index < 227; index++) {
	    if (sinr_db1 < MI_map_64qam[0][0]) {
	      SI += (MI_map_64qam[1][0]/beta1_dlsch_MI[TM][mcs]);
	      M +=6;
	    break;
	    }
	     if (sinr_db1 > MI_map_64qam[0][226]) {
	       SI += (MI_map_64qam[1][226]/beta1_dlsch_MI[TM][mcs]);
	       M +=6;
	    break;
	    }
	  
	     if (sinr_db1 == MI_map_64qam[0][index]) {
	       SI += (MI_map_64qam[1][index]/beta1_dlsch_MI[TM][mcs]);
	       M +=6;
	       break;
	     }
	}
	
	//for sinr_db2
	for (index = 0; index < 227; index++) {
	  if (sinr_db2 < MI_map_64qam[0][0]) {
	    SI += (MI_map_64qam[1][0]/beta1_dlsch_MI[TM][mcs]);
	    M +=6;
	    break;
	  }
	  if (sinr_db2 > MI_map_64qam[0][226]) {
	    SI += (MI_map_64qam[1][226]/beta1_dlsch_MI[TM][mcs]);
	    M +=6;
	    break;
	  }
	  
	  if (sinr_db2 == MI_map_64qam[0][index]) {
	    SI += (MI_map_64qam[1][index]/beta1_dlsch_MI[TM][mcs]);
	    M +=6;
	    break;
	  }
	}
	}
    }
 // }

  RBIR = SI/M;
  
  //Now RBIR->SINR_effective Mapping
  //binary search method is performed here
  if(mcs<10){
    start = 0;
    end = 161;
    middle = end/2;
    if (RBIR <= MI_map_4qam[2][start])
      {
      sinr_eff =  MI_map_4qam[0][start];
      }
    else
      {
      if (RBIR >= MI_map_4qam[2][end])
	sinr_eff =  MI_map_4qam[0][end];
      else
	{//while((end-start > 1) && (RBIR >= MI_map_4qam[2])) 
	if (RBIR < MI_map_4qam[2][middle]){
	  end = middle;
	  middle = end/2;
	}
	else{ 
	    start = middle;
	  middle = (end-middle)/2;
	}
	}
    for (; end>start; end--){
      if ((RBIR < MI_map_4qam[2][end]) && (RBIR >  MI_map_4qam[2][end-2])){
      sinr_eff = MI_map_4qam[0][end-1];
      break;
      }
    }
      }
    sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs]; 
  }


  
  else
    if (mcs>9 && mcs<17)
      {
	
	start = 0;
	end = 196;
	middle = end/2;
	if (RBIR <= MI_map_16qam[2][start])
	  {
	  sinr_eff =  MI_map_16qam[0][start];
	  }
	else
	  {
	  if (RBIR >= MI_map_16qam[2][end])
	    sinr_eff =  MI_map_16qam[0][end];
	  else
	    {
	//while((end-start > 1) && (RBIR >= MI_map_4qam[2])) 
	if (RBIR < MI_map_16qam[2][middle]){
	  end = middle;
	  middle = end/2;
	}
	else{ 
	  start = middle;
	  middle = (end-middle)/2;
	}
	    }
	for (; end>start; end--){
	  if ((RBIR < MI_map_16qam[2][end]) && (RBIR >  MI_map_16qam[2][end-2])){
	    sinr_eff = MI_map_16qam[0][end-1];
	    break;
	  }
	}
      }
	sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs];
      } 
    else
      if (mcs>16)
	{
	  start = 0;
	  end = 226;
    middle = end/2;
    if (RBIR <= MI_map_64qam[2][start])
      {
      sinr_eff =  MI_map_64qam[0][start];
      }
    else
      {
      if (RBIR >= MI_map_64qam[2][end])
	sinr_eff =  MI_map_64qam[0][end];
      else
	{
	//while((end-start > 1) && (RBIR >= MI_map_4qam[2])) 
	if (RBIR < MI_map_64qam[2][middle]){
	  end = middle;
	  middle = end/2;
	}
	else{ 
	  start = middle;
	  middle = (end-middle)/2;
	}
	}
    for (; end>start; end--){
      if ((RBIR < MI_map_64qam[2][end]) && (RBIR >  MI_map_64qam[2][end-2])){
	sinr_eff = MI_map_64qam[0][end-1];
      break;
      }
    } 
      }
    sinr_eff = sinr_eff * beta2_dlsch_MI[TM][mcs]; 
	}

1635
  msg("SINR_Eff = %e\n",sinr_eff);
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672

 sinr_eff *= 10;
  sinr_eff = floor(sinr_eff);
  // if ((int)sinr_eff%2) {
  //   sinr_eff += 1;
  // }
  sinr_eff /= 10;
  msg("sinr_eff after rounding = %f\n",sinr_eff);

   for (index = 0; index < 16; index++) {
    if(index == 0) {
      if (sinr_eff < sinr_bler_map_up[mcs][0][index]) {
        bler = 1;
        break;
      }
    }
    if (sinr_eff == sinr_bler_map_up[mcs][0][index]) {
        bler = sinr_bler_map_up[mcs][1][index];
    }
   }
   
#ifdef USER_MODE // need to be adapted for the emulation in the kernel space 
   if (uniformrandom() < bler) {
    msg("abstraction_decoding failed (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(0);
  }
  else {
    msg("abstraction_decoding successful (mcs=%d, sinr_eff=%f, bler=%f)\n",mcs,sinr_eff,bler);
    return(1);
  }
#endif

}

#endif

uint32_t ulsch_decoding_emul(PHY_VARS_eNB *phy_vars_eNB,
Raymond Knopp's avatar
 
Raymond Knopp committed
1673
			     uint8_t sched_subframe,
1674 1675 1676 1677 1678
			     uint8_t UE_index,
			     uint16_t *crnti) {

  uint8_t UE_id;
  uint16_t rnti;
Raymond Knopp's avatar
 
Raymond Knopp committed
1679
  int subframe = phy_vars_eNB->proc[sched_subframe].subframe_rx;
Raymond Knopp's avatar
 
Raymond Knopp committed
1680
  uint8_t harq_pid;
Raymond Knopp's avatar
 
Raymond Knopp committed
1681
  uint8_t CC_id = phy_vars_eNB->CC_id;
Raymond Knopp's avatar
 
Raymond Knopp committed
1682

Raymond Knopp's avatar
 
Raymond Knopp committed
1683
  harq_pid = subframe2harq_pid(&phy_vars_eNB->lte_frame_parms,phy_vars_eNB->proc[sched_subframe].frame_rx,subframe);
1684 1685 1686 1687 1688 1689
  
  rnti = phy_vars_eNB->ulsch_eNB[UE_index]->rnti;
#ifdef DEBUG_PHY
  LOG_D(PHY,"[eNB %d] ulsch_decoding_emul : subframe %d UE_index %d harq_pid %d rnti %x\n",phy_vars_eNB->Mod_id,subframe,UE_index,harq_pid,rnti);
#endif
  for (UE_id=0;UE_id<NB_UE_INST;UE_id++) {
Raymond Knopp's avatar
 
Raymond Knopp committed
1690
    if (rnti == PHY_vars_UE_g[UE_id][CC_id]->lte_ue_pdcch_vars[0]->crnti)
1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
      break;
    /*
    msg("[PHY] EMUL eNB %d ulsch_decoding_emul : subframe ue id %d crnti %x nb ue %d\n",
	phy_vars_eNB->Mod_id,
	UE_id,
	PHY_vars_UE_g[UE_id]->lte_ue_pdcch_vars[0]->crnti,
	NB_UE_INST); 
    */
  }

  if (UE_id==NB_UE_INST) {
    LOG_W(PHY,"[eNB %d] ulsch_decoding_emul: FATAL, didn't find UE with rnti %x (UE index %d)\n",
	  phy_vars_eNB->Mod_id, rnti, UE_index);
    return(1+phy_vars_eNB->ulsch_eNB[UE_id]->max_turbo_iterations);
  }
  else {
    LOG_D(PHY,"[eNB %d] Found UE with rnti %x => UE_id %d\n",phy_vars_eNB->Mod_id, rnti, UE_id);
  }

Raymond Knopp's avatar
 
Raymond Knopp committed
1710
  if (PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->harq_processes[harq_pid]->status == CBA_ACTIVE){
1711
    *crnti = rnti;
Raymond Knopp's avatar
 
Raymond Knopp committed
1712
    PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->harq_processes[harq_pid]->status=IDLE;
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
  } else 
    *crnti = 0x0;
  
  // Do abstraction here to determine if packet it in error
 /* if (ulsch_abstraction_MIESM(phy_vars_eNB->sinr_dB_eNB,1, phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->mcs,phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->nb_rb, phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->first_rb) == 1) 
  flag = 1;
  else flag = 0;*/
  

  /*
 //SINRdbPost = phy_vars_eNB->sinr_dB_eNB;
 mcsPost = phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->mcs,
 nrbPost = phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->nb_rb;
 frbPost = phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->first_rb; 
 

 if(nrbPost > 0)
 {
 SINRdbPost = phy_vars_eNB->sinr_dB_eNB;
 ULflag1 = 1;
}
 else
 {
	 SINRdbPost = NULL  ;
	 ULflag1 = 0 ;
}*/

  //
  // write_output("postprocSINR.m","SINReNB",phy_vars_eNB->sinr_dB,301,1,7);
 
 
 //Yazdir buraya her frame icin 300 eNb 
 // fprintf(SINRrx,"%e,%e,%e,%e;\n",SINRdbPost);
 //fprintf(SINRrx,"%e\n",SINRdbPost);
 
 // fprintf(csv_fd,"%e+i*(%e),",channelx,channely);
  
 // if (ulsch_abstraction(phy_vars_eNB->sinr_dB,1, phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->mcs,phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->nb_rb, phy_vars_eNB->ulsch_eNB[UE_id]->harq_processes[harq_pid]->first_rb) == 1) {
  if (1) {
    LOG_D(PHY,"ulsch_decoding_emul abstraction successful\n");

    memcpy(phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->b,
Raymond Knopp's avatar
 
Raymond Knopp committed
1755
	   PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->harq_processes[harq_pid]->b,
1756 1757 1758 1759
	   phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->TBS>>3);
    // get local ue's ack 	 
    if ((UE_index >= oai_emulation.info.first_ue_local) ||(UE_index <(oai_emulation.info.first_ue_local+oai_emulation.info.nb_ue_local))){
      get_ack(&phy_vars_eNB->lte_frame_parms,
Raymond Knopp's avatar
 
Raymond Knopp committed
1760
	      PHY_vars_UE_g[UE_id][CC_id]->dlsch_ue[0][0]->harq_ack,
1761
	      subframe,
Raymond Knopp's avatar
 
Raymond Knopp committed
1762
	      phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_ACK);
1763
    }else { // get remote UEs' ack 
Raymond Knopp's avatar
 
Raymond Knopp committed
1764 1765
      phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_ACK[0] = PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->o_ACK[0];
      phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_ACK[1] = PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->o_ACK[1];
1766 1767 1768 1769 1770
    }

    // Do abstraction of PUSCH feedback
#ifdef DEBUG_PHY    
    LOG_D(PHY,"[eNB %d][EMUL] ue index %d UE_id %d: subframe %d : o_ACK (%d %d), cqi (val %d, len %d)\n",
Raymond Knopp's avatar
 
Raymond Knopp committed
1771 1772
	  phy_vars_eNB->Mod_id,UE_index, UE_id, subframe,phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_ACK[0],
	  phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_ACK[1],
Raymond Knopp's avatar
 
Raymond Knopp committed
1773 1774
	  ((HLC_subband_cqi_rank1_2A_5MHz *)PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->o)->cqi1,
	  PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->O); 
1775 1776
#endif 

Raymond Knopp's avatar
 
Raymond Knopp committed
1777 1778
    phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->Or1 = PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->O;
    phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->Or2 = PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->O;
1779
   
Raymond Knopp's avatar
 
Raymond Knopp committed
1780 1781 1782
    phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->uci_format = PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->uci_format;    
    memcpy(phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o,PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->o,MAX_CQI_BYTES); 
    memcpy(phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->o_RI,PHY_vars_UE_g[UE_id][CC_id]->ulsch_ue[0]->o_RI,2); 
1783

Raymond Knopp's avatar
 
Raymond Knopp committed
1784
    phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->cqi_crc_status = 1;
1785 1786 1787 1788 1789 1790
    
    return(1);   
  }
  else {
    LOG_W(PHY,"[eNB %d] ulsch_decoding_emul abstraction failed for UE %d\n",phy_vars_eNB->Mod_id,UE_index);

Raymond Knopp's avatar
 
Raymond Knopp committed
1791
    phy_vars_eNB->ulsch_eNB[UE_index]->harq_processes[harq_pid]->cqi_crc_status = 0;
1792 1793 1794 1795 1796 1797 1798

    // retransmission
    return(1+phy_vars_eNB->ulsch_eNB[UE_index]->max_turbo_iterations);
  }

}
#endif