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

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


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

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

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

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

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

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

//#include "defs.h"

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

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

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

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

  int i,r;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return(NULL);
}

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

  unsigned char i;

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

185
    for (i=0; i<8; i++) {
186
      if (ulsch->harq_processes[i]) {
187
188
189
190
191
192
        //    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;
193
194
195
196
197
198
199
      }
    }

  }
}


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

  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,
223
224
225
226
227
228
                             uint8_t UE_id,
                             uint8_t sched_subframe,
                             uint8_t control_only_flag,
                             uint8_t Nbundled,
                             uint8_t llr8_flag)
{
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248


  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;
249
  unsigned int Qprime_ACK,Qprime_RI,len_ACK=0,len_RI=0;
knopp's avatar
   
knopp committed
250
  //  uint8_t q_ACK[MAX_ACK_PAYLOAD],q_RI[MAX_RI_PAYLOAD];
251
252
253
254
255
256
257
258
259
260
261
262
263
  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
264
  int subframe = phy_vars_eNB->proc[sched_subframe].subframe_rx;
knopp's avatar
knopp committed
265
  LTE_UL_eNB_HARQ_t *ulsch_harq;
266
267

  uint8_t (*tc)(int16_t *y,
268
269
270
271
272
273
274
275
276
277
278
279
280
281
                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 *);
282

283
284
285
  harq_pid = subframe2harq_pid(frame_parms,phy_vars_eNB->proc[sched_subframe].frame_rx,subframe);

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,1);
286
287
288

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

  if (harq_pid==255) {
292
    LOG_E(PHY, "FATAL ERROR: illegal harq_pid, returning\n");
293
    return -1;
294
295
  }

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

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

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

309

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


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

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


  sumKr = 0;
343
344

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

350
351
    sumKr += Kr;
  }
352

353
354
  if (sumKr==0) {
    LOG_N(PHY,"[eNB %d] ulsch_decoding.c: FATAL sumKr is 0!\n",phy_vars_eNB->Mod_id);
355
    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",
356
357
358
359
360
361
362
363
364
          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
365
    mac_xface->macphy_exit("ulsch_decoding.c: FATAL sumKr is 0!");
366
367
    return(-1);
  }
368

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

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

378
379
380
381
382
383
384
385
386
387
    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
388
  Qprime = ulsch_harq->O_ACK*ulsch_harq->Msc_initial*ulsch_harq->Nsymb_initial * ulsch->beta_offset_harqack_times8;
389

390
391
392
393
394
395
396
397
398
399
400
401
402
  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
403
404
  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);
405
#endif
406

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

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

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

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

428

429
430

  Q_CQI = Q_m * Qprime;
knopp's avatar
knopp committed
431
#ifdef DEBUG_ULSCH_DECODING
432
  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
433
#endif
434
435
436
437
438
439
440
441
442
443
444
445
446

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

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

452
453
454
455
456
457
  // 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
458
  memset(y,LTE_NULL,Q_m*Hpp);
459
460
461
462
463
464
465

  // 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;
466
467
468

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

472
473
474
475
476
477
478
    s = lte_gold_generic(&x1, &x2, 0);
  }

  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
479
480
481
482

  j=0;

  for (i=0; i<Qprime_RI; i++) {
483
    r = Rmux_prime - 1 - (i>>2);
484
485
    /*
    for (q=0;q<Q_m;q++)
486
487
      ytag2[q+(Q_m*((r*Cmux) + columnset[j]))]  = q_RI[(q+(Q_m*i))%len_RI];
    */
488
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));
489
    cseq[off+1] = cseq[off];  // PUSCH_y
490
491

    for (q=2; q<Q_m; q++)
492
493
494
495
496
497
498
499
500
501
502
503
504
      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;
505
506

  for (i=0; i<Qprime_ACK; i++) {
507
508
509
510
511
512
    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];
    }
    */
513
514
    off =((Rmux_prime*Q_m*columnset[j])+(r*Q_m));

knopp's avatar
knopp committed
515
    if (ulsch_harq->O_ACK == 1) {
516
      if (ulsch->bundling==0)
517
518
519
520
521
522
523
        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
524
    }
525

526
#ifdef DEBUG_ULSCH_DECODING
527
    msg("ulsch_decoding.c: ACK i %d, r %d, j %d, ColumnSet[j] %d\n",i,r,j,columnset[j]);
528
529
530
531
532
533
534
#endif
    j=(j+3)&3;
  }



  i=0;
535

536
537
  switch (Q_m) {
  case 2:
538
    for (j=0; j<Cmux; j++) {
539
      i2=j<<1;
540
541
542
543
544
545
546
547
548
549
550

      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);
551
552
      }
    }
553

554
    break;
555

556
  case 4:
557
    for (j=0; j<Cmux; j++) {
558
      i2=j<<2;
559
560
561
562
563
564
565
566
567
568
569

      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);
570
571
      }
    }
572
573
574

    break;

575
  case 6:
576
    for (j=0; j<Cmux; j++) {
577
      i2=j*6;
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592

      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);
593
594
      }
    }
595

596
597
598
599
600
601
602
    break;
  }


  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);

  if (i!=(H+Q_RI))
603
    LOG_D(PHY,"ulsch_decoding.c: Error in input buffer length (j %d, H+Q_RI %d)\n",i,H+Q_RI);
604
605
606
607
608
609
610
611
612
613

  // 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
614
  if (ulsch_harq->O_ACK == 1) {
615
616
617
618
    switch (Q_m) {
    case 2:
      len_ACK = 2;
      break;
619

620
621
622
    case 4:
      len_ACK = 4;
      break;
623

624
625
626
627
628
    case 6:
      len_ACK = 6;
      break;
    }
  }
629

knopp's avatar
knopp committed
630
  if (ulsch_harq->O_ACK == 2) {
631
632
633
634
    switch (Q_m) {
    case 2:
      len_ACK = 6;
      break;
635

636
637
638
    case 4:
      len_ACK = 12;
      break;
639

640
641
642
643
644
    case 6:
      len_ACK = 18;
      break;
    }
  }
645

knopp's avatar
knopp committed
646
  if (ulsch_harq->O_ACK > 2) {
647
648
649
650
    LOG_E(PHY,"ulsch_decoding: FATAL, ACK cannot be more than 2 bits yet\n");
    return(-1);
  }

651
  for (i=0; i<len_ACK; i++)
knopp's avatar
knopp committed
652
    ulsch_harq->q_ACK[i] = 0;
653
654


655
  for (i=0; i<Qprime_ACK; i++) {
656
    r = Rmux_prime -1 - (i>>2);
657
658

    for (q=0; q<Q_m; q++) {
659
      if (y[q+(Q_m*((r*Cmux) + columnset[j]))]!=0)
660
        ulsch_harq->q_ACK[(q+(Q_m*i))%len_ACK] += y[q+(Q_m*((r*Cmux) + columnset[j]))];
661
662
      y[q+(Q_m*((r*Cmux) + columnset[j]))]=0;  // NULL LLRs in ACK positions
    }
663

664
665
666
667
    j=(j+3)&3;
  }


668
  // RI BITS
669

knopp's avatar
knopp committed
670
  if (ulsch_harq->O_RI == 1) {
671
672
    switch (Q_m) {
    case 2:
673
      len_RI=2;
674
      break;
675

676
677
678
    case 4:
      len_RI=4;
      break;
679

680
681
682
683
684
685
    case 6:
      len_RI=6;
      break;
    }
  }

knopp's avatar
knopp committed
686
  if (ulsch_harq->O_RI > 1) {
687
688
689
690
    LOG_E(PHY,"ulsch_decoding: FATAL, RI cannot be more than 1 bit yet\n");
    return(-1);
  }

691
  for (i=0; i<len_RI; i++)
knopp's avatar
knopp committed
692
    ulsch_harq->q_RI[i] = 0;
693

694
695
696
697
  if (frame_parms->Ncp == 0)
    columnset = cs_ri_normal;
  else
    columnset = cs_ri_extended;
698
699
700
701

  j=0;

  for (i=0; i<Qprime_RI; i++) {
702
    r = Rmux_prime -1 - (i>>2);
703
704

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

707
708
709
710
711
    ytag[(r*Cmux) + columnset[j]] = LTE_NULL;
    j=(j+3)&3;
  }

  // CQI and Data bits
712
713
714
  j=0;
  j2=0;

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

790
791
    }
  }
792
793
794
795
796
797
798
799
800
801
802
  else {
    for (i=0; i<(Q_CQI/Q_m); i++) {
      
      for (q=0; q<Q_m; q++) {
	ys = y[q+j2];
	if (ys>127)
	  ulsch_harq->q[q+(Q_m*i)] = 127;
	else if (ys<-128)
	  ulsch_harq->q[q+(Q_m*i)] = -128;
	else
	  ulsch_harq->q[q+(Q_m*i)] = ys;
803
      }
804
805
      
      j2+=Q_m;
806
    }
807
808
809
810
811
812
    
    switch (Q_m) {
    case 2:
      for (iprime=0; iprime<G<<1;) {
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
813
      }
814
815
816
817
818
819
820
      break;
    case 4:
      for (iprime=0; iprime<G<<2;) {
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
821
      }
822
823
824
825
826
827
828
829
830
831
832
      break;
    case 6:
      for (iprime=0; iprime<G*6;) {
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
	ulsch_harq->e[iprime++] = y[j2++];
      }
      break;
833
834
835
836
    }
  }
  // Do CQI/RI/HARQ-ACK Decoding first and pass to MAC

837
  // HARQ-ACK
838
  wACK_idx = (ulsch->bundling==0) ? 4 : ((Nbundled-1)&3);
839

knopp's avatar
knopp committed
840
  if (ulsch_harq->O_ACK == 1) {
841
842
843
844
845
846
847
    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;
848
  }
849

knopp's avatar
knopp committed
850
  if (ulsch_harq->O_ACK == 2) {
851
852
853
    switch (Q_m) {

    case 2:
knopp's avatar
knopp committed
854
855
856
      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];
857
858
      break;
    case 4:
knopp's avatar
knopp committed
859
860
861
      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];
862
863
      break;
    case 6:
knopp's avatar
knopp committed
864
      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];
865
866
      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];
867
868
      break;
    }
869

knopp's avatar
knopp committed
870
871
872
873
    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];
874
875

    if (metric_new > metric) {
knopp's avatar
knopp committed
876
877
      ulsch_harq->o_ACK[0]=0;
      ulsch_harq->o_ACK[1]=1;
878
879
      metric = metric_new;
    }
880

knopp's avatar
knopp committed
881
    metric_new = ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]+ulsch_harq->q_ACK[2];
882
883
884


    if (metric_new > metric) {
knopp's avatar
knopp committed
885
886
      ulsch_harq->o_ACK[0] = 1;
      ulsch_harq->o_ACK[1] = 0;
887
888
      metric = metric_new;
    }
889

knopp's avatar
knopp committed
890
    metric_new = -ulsch_harq->q_ACK[0]-ulsch_harq->q_ACK[1]-ulsch_harq->q_ACK[2];
891
892

    if (metric_new > metric) {
knopp's avatar
knopp committed
893
894
      ulsch_harq->o_ACK[0] = 0;
      ulsch_harq->o_ACK[1] = 0;
895
896
897
898
899
900
      metric = metric_new;
    }
  }

  // RI

knopp's avatar
knopp committed
901
  if ((ulsch_harq->O_RI == 1) && (Qprime_RI > 0)) {
902
    ulsch_harq->o_RI[0] = ((ulsch_harq->q_RI[0] + ulsch_harq->q_RI[Q_m/2]) > 0) ? 0 : 1;
903
  }
904

905
906
  // CQI

907
  if (Q_CQI>0) {
knopp's avatar
knopp committed
908
    memset((void *)&dummy_w_cc[0],0,3*(ulsch_harq->Or1+8+32));
909

knopp's avatar
knopp committed
910
    O_RCC = generate_dummy_w_cc(ulsch_harq->Or1+8,
911
912
913
                                &dummy_w_cc[0]);


914
    lte_rate_matching_cc_rx(O_RCC,
915
916
917
918
919
                            Q_CQI,
                            ulsch_harq->o_w,
                            dummy_w_cc,
                            ulsch_harq->q);

knopp's avatar
knopp committed
920
    sub_block_deinterleaving_cc((unsigned int)(ulsch_harq->Or1+8),
921
922
923
                                &ulsch_harq->o_d[96],
                                &ulsch_harq->o_w[0]);

knopp's avatar
knopp committed
924
925
    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);
926

knopp's avatar
knopp committed
927
928
    if (extract_cqi_crc(o_flip,ulsch_harq->Or1) == (crc8(o_flip,ulsch_harq->Or1)>>24))
      ulsch_harq->cqi_crc_status = 1;
929
    else
knopp's avatar
knopp committed
930
      ulsch_harq->cqi_crc_status = 0;
931
932


knopp's avatar
   
knopp committed
933
    if (ulsch->harq_processes[harq_pid]->Or1<=32) {
knopp's avatar
knopp committed
934
935
936
      ulsch_harq->o[3] = o_flip[0] ;
      ulsch_harq->o[2] = o_flip[1] ;
      ulsch_harq->o[1] = o_flip[2] ;
937
938
      ulsch_harq->o[0] = o_flip[3] ;
    } else {
knopp's avatar
knopp committed
939
940
941
      ulsch_harq->o[7] = o_flip[0] ;
      ulsch_harq->o[6] = o_flip[1] ;
      ulsch_harq->o[5] = o_flip[2] ;
942
      ulsch_harq->o[4] = o_flip[3] ;
knopp's avatar
knopp committed
943
944
945
      ulsch_harq->o[3] = o_flip[4] ;
      ulsch_harq->o[2] = o_flip[5] ;
      ulsch_harq->o[1] = o_flip[6] ;
946
947
      ulsch_harq->o[0] = o_flip[7] ;

948
    }
949

950
#ifdef DEBUG_ULSCH_DECODING
knopp's avatar
knopp committed
951
    msg("ulsch_decoding: Or1=%d\n",ulsch_harq->Or1);
952
953

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

knopp's avatar
knopp committed
956
957
    if (ulsch_harq->cqi_crc_status == 1)
      msg("RX CQI CRC OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
958
    else
knopp's avatar
knopp committed
959
      msg("RX CQI CRC NOT OK (%x)\n",extract_cqi_crc(o_flip,ulsch_harq->Or1));
960

961
962
963
964
965
966
967
968
969
970
#endif
  }

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

  //  stop_meas(&phy_vars_eNB->ulsch_demultiplexing_stats);


  r_offset = 0;
971
972
973

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

974
    // Get Turbo interleaver parameters
knopp's avatar
knopp committed
975
976
    if (r<ulsch_harq->Cminus)
      Kr = ulsch_harq->Kminus;
977
    else
knopp's avatar
knopp committed
978
      Kr = ulsch_harq->Kplus;
979

980
    Kr_bytes = Kr>>3;
981

982
983
984
985
986
987
988
989
990
991
992
993
    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);
    }
994
995

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

999
    memset(&dummy_w[r][0],0,3*(6144+64)*sizeof(short));
1000
1001
1002
    ulsch_harq->RTC[r] = generate_dummy_w(4+(Kr_bytes*8),
                                          (uint8_t*)&dummy_w[r][0],
                                          (r==0) ? ulsch_harq->F : 0);
1003

1004
#ifdef DEBUG_ULSCH_DECODING
1005
    msg("Rate Matching Segment %d (coded bits (G) %d,unpunctured/repeated bits %d, Q_m %d, nb_rb %d, Nl %d)...\n",
1006
1007
1008
1009
1010
1011
        r, G,
        Kr*3,
        Q_m,
        nb_rb,
        ulsch_harq->Nl);
#endif
1012
1013

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

knopp's avatar
knopp committed
1015
    if (lte_rate_matching_turbo_rx(ulsch_harq->RTC[r],
1016
1017
1018
1019
1020
1021
                                   G,
                                   ulsch_harq->w[r],
                                   (uint8_t*) &dummy_w[r][0],
                                   ulsch_harq->e+r_offset,
                                   ulsch_harq->C,
                                   NSOFT,
1022
                                   0,   //Uplink
1023
1024
1025
1026
1027
1028
1029
                                   1,
                                   ulsch_harq->rvidx,
                                   (ulsch_harq->round==0)?1:0,  // clear
                                   get_Qm_ul(ulsch_harq->mcs),
                                   1,
                                   r,
                                   &E)==-1) {
1030
1031
1032
      LOG_E(PHY,"ulsch_decoding.c: Problem in rate matching\n");
      return(-1);
    }
1033

1034
1035
    stop_meas(&phy_vars_eNB->ulsch_rate_unmatching_stats);
    r_offset += E;
1036

1037
    start_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
1038
1039
1040
    sub_block_deinterleaving_turbo(4+Kr,
                                   &ulsch_harq->d[r][96],
                                   ulsch_harq->w[r]);
1041
1042
1043
    stop_meas(&phy_vars_eNB->ulsch_deinterleaving_stats);
  }

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

1046
1047
1048
1049
1050
      if (ulsch_harq->C == 1)
        crc_type = CRC24_A;
      else
        crc_type = CRC24_B;

1051
      start_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);
1052

knopp's avatar
knopp committed
1053
      ret = tc(&ulsch_harq->d[r][96],
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
               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);

1069
1070
1071
      stop_meas(&phy_vars_eNB->ulsch_turbo_decoding_stats);

      status[r] = ret;
1072

1073
    }
1074

1075
1076
1077
1078
  // Reassembly of Transport block here
  offset = 0;

  ret = 1;
1079
1080

  for (r=0; r<ulsch_harq->C; r++) {
1081
    if (status[r] != (1+ulsch->max_turbo_iterations)) {
knopp's avatar
knopp committed
1082
      if (r<ulsch_harq->Cminus)
1083
        Kr = ulsch_harq->Kminus;
1084
      else
1085
1086
        Kr = ulsch_harq->Kplus;

1087
      Kr_bytes = Kr>>3;
1088

1089
      if (r==0) {
1090
1091
1092
1093
1094
1095
1096
1097
1098
        memcpy(ulsch_harq->b,
               &ulsch_harq->c[0][(ulsch_harq->F>>3)],
               Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0));
        offset = Kr_bytes - (ulsch_harq->F>>3) - ((ulsch_harq->C>1)?3:0);
      } else {
        memcpy(ulsch_harq->b+offset,
               ulsch_harq->c[r],
               Kr_bytes - ((ulsch_harq->C>1)?3:0));
        offset += (Kr_bytes- ((ulsch_harq->C>1)?3:0));
1099
      }
1100

1101
      if (ret != (1+ulsch->max_turbo_iterations))
1102
1103
        ret = status[r];
    } else {
1104
1105
      ret = 1+ulsch->max_turbo_iterations;
    }
1106

1107
  }
1108

1109
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_ENB_ULSCH_DECODING0+harq_pid,0);