nr_pbch.c 21.2 KB
Newer Older
Wang's avatar
Wang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file PHY/LTE_TRANSPORT/pbch.c
* \brief Top-level routines for generating and decoding  the PBCH/BCH physical/transport channel V8.6 2009-03
* \author R. Knopp, F. Kaltenberger
* \date 2011
* \version 0.1
* \company Eurecom
* \email: knopp@eurecom.fr,florian.kaltenberger.fr
* \note
* \warning
*/
#include "PHY/defs_nr_UE.h"
#include "PHY/CODING/coding_extern.h"
#include "PHY/phy_extern_nr_ue.h"
#include "PHY/sse_intrin.h"
hongzhi wang's avatar
hongzhi wang committed
36
#include "PHY/LTE_REFSIG/lte_refsig.h"
37
#include "PHY/INIT/phy_init.h"
Wang's avatar
Wang committed
38

knopp's avatar
knopp committed
39
//#define DEBUG_PBCH 1
Wang's avatar
Wang committed
40
41
42
//#define DEBUG_PBCH_ENCODING

#ifdef OPENAIR2
43
  //#include "PHY_INTERFACE/defs.h"
Wang's avatar
Wang committed
44
45
46
47
#endif

#define PBCH_A 24

knopp's avatar
knopp committed
48
#define print_shorts(s,x) printf("%s : %d,%d,%d,%d,%d,%d,%d,%d\n",s,((int16_t*)x)[0],((int16_t*)x)[1],((int16_t*)x)[2],((int16_t*)x)[3],((int16_t*)x)[4],((int16_t*)x)[5],((int16_t*)x)[6],((int16_t*)x)[7])
Wang's avatar
Wang committed
49
50

uint16_t nr_pbch_extract(int **rxdataF,
51
52
53
54
55
56
57
                         int **dl_ch_estimates,
                         int **rxdataF_ext,
                         int **dl_ch_estimates_ext,
                         uint32_t symbol,
                         uint32_t s_offset,
                         uint32_t high_speed_flag,
                         NR_DL_FRAME_PARMS *frame_parms) {
Wang's avatar
Wang committed
58
  uint16_t rb;
knopp's avatar
knopp committed
59
  uint8_t i,j,aarx;
hongzhi wang's avatar
hongzhi wang committed
60
61
  int32_t *dl_ch0,*dl_ch0_ext,*rxF,*rxF_ext;
  int nushiftmod4 = frame_parms->nushift;
knopp's avatar
knopp committed
62
  unsigned int  rx_offset = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier; //and
63

knopp's avatar
knopp committed
64
  if (rx_offset>= frame_parms->ofdm_symbol_size) rx_offset-=frame_parms->ofdm_symbol_size;
65
66
67
68

  AssertFatal(symbol>=1 && symbol<5,
              "symbol %d illegal for PBCH extraction\n",
              symbol);
knopp's avatar
knopp committed
69

Wang's avatar
Wang committed
70
  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
71
    rxF        = &rxdataF[aarx][(symbol+s_offset)*frame_parms->ofdm_symbol_size];
72
    rxF_ext    = &rxdataF_ext[aarx][symbol*20*12];
hongzhi wang's avatar
hongzhi wang committed
73
#ifdef DEBUG_PBCH
74
75
76
77
78
79
80
81
82
83
    printf("extract_rbs (nushift %d): rx_offset=%d, symbol %d\n",frame_parms->nushift,
           (rx_offset + ((symbol+s_offset)*(frame_parms->ofdm_symbol_size))),symbol);
    int16_t *p = (int16_t *)rxF;

    for (int i =0; i<8; i++) {
      printf("rxF [%d]= %d\n",i,rxF[i]);
      printf("pbch extract rxF  %d %d addr %p\n", p[2*i], p[2*i+1], &p[2*i]);
      printf("rxF ext addr %p\n", &rxF_ext[i]);
    }

hongzhi wang's avatar
hongzhi wang committed
84
#endif
Wang's avatar
Wang committed
85
86

    for (rb=0; rb<20; rb++) {
hongzhi wang's avatar
hongzhi wang committed
87
      j=0;
knopp's avatar
knopp committed
88

89
      if (symbol==1 || symbol==3) {
Wang's avatar
Wang committed
90
91
92
93
        for (i=0; i<12; i++) {
          if ((i!=nushiftmod4) &&
              (i!=(nushiftmod4+4)) &&
              (i!=(nushiftmod4+8))) {
knopp's avatar
knopp committed
94
95
            rxF_ext[j]=rxF[rx_offset];
#ifdef DEBUG_PBCH
96
97
98
99
100
101
            printf("rxF ext[%d] = (%d,%d) rxF [%d]= (%d,%d)\n",(9*rb) + j,
                   ((int16_t *)&rxF_ext[j])[0],
                   ((int16_t *)&rxF_ext[j])[1],
                   rx_offset,
                   ((int16_t *)&rxF[rx_offset])[0],
                   ((int16_t *)&rxF[rx_offset])[1]);
knopp's avatar
knopp committed
102
#endif
103
            j++;
Wang's avatar
Wang committed
104
          }
105
106

          rx_offset=(rx_offset+1)&(frame_parms->ofdm_symbol_size-1);
Wang's avatar
Wang committed
107
        }
108

hongzhi wang's avatar
hongzhi wang committed
109
        rxF_ext+=9;
Wang's avatar
Wang committed
110
      } else { //symbol 2
111
112
113
114
115
116
        if ((rb < 4) || (rb >15)) {
          for (i=0; i<12; i++) {
            if ((i!=nushiftmod4) &&
                (i!=(nushiftmod4+4)) &&
                (i!=(nushiftmod4+8))) {
              rxF_ext[j]=rxF[rx_offset];
knopp's avatar
knopp committed
117
#ifdef DEBUG_PBCH
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
              printf("rxF ext[%d] = (%d,%d) rxF [%d]= (%d,%d)\n",(rb<4) ? (9*rb) + j : (9*(rb-12))+j,
                     ((int16_t *)&rxF_ext[j])[0],
                     ((int16_t *)&rxF_ext[j])[1],
                     rx_offset,
                     ((int16_t *)&rxF[rx_offset])[0],
                     ((int16_t *)&rxF[rx_offset])[1]);
#endif
              j++;
            }

            rx_offset=(rx_offset+1)&(frame_parms->ofdm_symbol_size-1);
          }

          rxF_ext+=9;
        } else rx_offset = (rx_offset+12)&(frame_parms->ofdm_symbol_size-1);
Wang's avatar
Wang committed
133
134
135
      }
    }

knopp's avatar
knopp committed
136
    if (high_speed_flag == 1)
137
      dl_ch0     = &dl_ch_estimates[aarx][((symbol+s_offset)*(frame_parms->ofdm_symbol_size))];
knopp's avatar
knopp committed
138
139
    else
      dl_ch0     = &dl_ch_estimates[aarx][0];
140

knopp's avatar
knopp committed
141
    //printf("dl_ch0 addr %p\n",dl_ch0);
142
    dl_ch0_ext = &dl_ch_estimates_ext[aarx][symbol*20*12];
143

knopp's avatar
knopp committed
144
145
    for (rb=0; rb<20; rb++) {
      j=0;
146

147
      if (symbol==1 || symbol==3) {
148
149
150
151
152
        for (i=0; i<12; i++) {
          if ((i!=nushiftmod4) &&
              (i!=(nushiftmod4+4)) &&
              (i!=(nushiftmod4+8))) {
            dl_ch0_ext[j]=dl_ch0[i];
knopp's avatar
knopp committed
153
#ifdef DEBUG_PBCH
154
155
156
157
158
159
160
161
162

            if ((rb==0) && (i<2))
              printf("dl ch0 ext[%d] = (%d,%d)  dl_ch0 [%d]= (%d,%d)\n",j,
                     ((int16_t *)&dl_ch0_ext[j])[0],
                     ((int16_t *)&dl_ch0_ext[j])[1],
                     i,
                     ((int16_t *)&dl_ch0[i])[0],
                     ((int16_t *)&dl_ch0[i])[1]);

knopp's avatar
knopp committed
163
#endif
164
165
166
167
168
169
170
171
172
173
174
175
176
            j++;
          }
        }

        dl_ch0+=12;
        dl_ch0_ext+=9;
      } else {
        if ((rb < 4) || (rb >15)) {
          for (i=0; i<12; i++) {
            if ((i!=nushiftmod4) &&
                (i!=(nushiftmod4+4)) &&
                (i!=(nushiftmod4+8))) {
              dl_ch0_ext[j]=dl_ch0[i];
knopp's avatar
knopp committed
177
#ifdef DEBUG_PBCH
178
179
180
181
182
183
              printf("dl ch0 ext[%d] = (%d,%d)  dl_ch0 [%d]= (%d,%d)\n",j,
                     ((int16_t *)&dl_ch0_ext[j])[0],
                     ((int16_t *)&dl_ch0_ext[j])[1],
                     i,
                     ((int16_t *)&dl_ch0[i])[0],
                     ((int16_t *)&dl_ch0[i])[1]);
knopp's avatar
knopp committed
184
#endif
185
186
187
188
189
190
191
192
              j++;
            }
          }

          dl_ch0_ext+=9;
        }

        dl_ch0+=12;
knopp's avatar
knopp committed
193
194
      }
    }
Wang's avatar
Wang committed
195
196
197
198
199
200
201
202
203
  }

  return(0);
}

//__m128i avg128;

//compute average channel_level on each (TX,RX) antenna pair
int nr_pbch_channel_level(int **dl_ch_estimates_ext,
204
205
                          NR_DL_FRAME_PARMS *frame_parms,
                          uint32_t symbol) {
hongzhi wang's avatar
hongzhi wang committed
206
  int16_t rb, nb_rb=20;
knopp's avatar
knopp committed
207
  uint8_t aarx;
Wang's avatar
Wang committed
208
209
210
211
212
213
214
215
216
#if defined(__x86_64__) || defined(__i386__)
  __m128i avg128;
  __m128i *dl_ch128;
#elif defined(__arm__)
  int32x4_t avg128;
  int16x8_t *dl_ch128;
#endif
  int avg1=0,avg2=0;

knopp's avatar
knopp committed
217
218
  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
    //clear average level
Wang's avatar
Wang committed
219
#if defined(__x86_64__) || defined(__i386__)
knopp's avatar
knopp committed
220
221
    avg128 = _mm_setzero_si128();
    dl_ch128=(__m128i *)&dl_ch_estimates_ext[aarx][symbol*20*12];
Wang's avatar
Wang committed
222
#elif defined(__arm__)
knopp's avatar
knopp committed
223
224
    avg128 = vdupq_n_s32(0);
    dl_ch128=(int16x8_t *)&dl_ch_estimates_ext[aarx][symbol*20*12];
Wang's avatar
Wang committed
225
#endif
226

knopp's avatar
knopp committed
227
    for (rb=0; rb<nb_rb; rb++) {
Wang's avatar
Wang committed
228
#if defined(__x86_64__) || defined(__i386__)
knopp's avatar
knopp committed
229
230
231
      avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[0],dl_ch128[0]));
      avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[1],dl_ch128[1]));
      avg128 = _mm_add_epi32(avg128,_mm_madd_epi16(dl_ch128[2],dl_ch128[2]));
Wang's avatar
Wang committed
232
#elif defined(__arm__)
233
      // to be filled in
Wang's avatar
Wang committed
234
#endif
knopp's avatar
knopp committed
235
236
237
      dl_ch128+=3;
      /*
      if (rb==0) {
238
239
240
241
      print_shorts("dl_ch128",&dl_ch128[0]);
      print_shorts("dl_ch128",&dl_ch128[1]);
      print_shorts("dl_ch128",&dl_ch128[2]);
      }*/
Wang's avatar
Wang committed
242
    }
243
244
245
246
247
248

    avg1 = (((int *)&avg128)[0] +
            ((int *)&avg128)[1] +
            ((int *)&avg128)[2] +
            ((int *)&avg128)[3])/(nb_rb*12);

knopp's avatar
knopp committed
249
250
    if (avg1>avg2)
      avg2 = avg1;
251

knopp's avatar
knopp committed
252
253
    //LOG_I(PHY,"Channel level : %d, %d\n",avg1, avg2);
  }
254

Wang's avatar
Wang committed
255
256
257
258
259
260
261
262
#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
  return(avg2);
}

void nr_pbch_channel_compensation(int **rxdataF_ext,
263
264
265
266
267
                                  int **dl_ch_estimates_ext,
                                  int **rxdataF_comp,
                                  NR_DL_FRAME_PARMS *frame_parms,
                                  uint32_t symbol,
                                  uint8_t output_shift) {
Thomas Laurent's avatar
Thomas Laurent committed
268
  const uint16_t nb_re=symbol == 2 ? 72 : 180;
269
  AssertFatal((symbol > 0 && symbol < 4),
270
271
              "symbol %d is illegal for PBCH DM-RS\n",
              symbol);
Wang's avatar
Wang committed
272

273
274
  //  printf("comp: symbol %d : nb_re %d\n",symbol,nb_re);

Thomas Laurent's avatar
Thomas Laurent committed
275
276
277
278
  for (int aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
    vect128 *dl_ch128          = (vect128 *)&dl_ch_estimates_ext[aarx][symbol*20*12];
    vect128 *rxdataF128        = (vect128 *)&rxdataF_ext[aarx][symbol*20*12];
    vect128 *rxdataF_comp128   = (vect128 *)&rxdataF_comp[aarx][symbol*20*12];
knopp's avatar
knopp committed
279
280
281
    /*
    printf("ch compensation dl_ch ext addr %p \n", &dl_ch_estimates_ext[aarx][symbol*20*12]);
    printf("rxdataf ext addr %p symbol %d\n", &rxdataF_ext[aarx][symbol*20*12], symbol);
282
    printf("rxdataf_comp addr %p\n",&rxdataF_comp[aarx][symbol*20*12]);
knopp's avatar
knopp committed
283
    */
Wang's avatar
Wang committed
284

knopp's avatar
knopp committed
285
    for (int re=0; re<nb_re; re+=12) {
Thomas Laurent's avatar
Thomas Laurent committed
286
287
288
      *rxdataF_comp128++ = mulByConjugate128(rxdataF128++, dl_ch128++, output_shift);
      *rxdataF_comp128++ = mulByConjugate128(rxdataF128++, dl_ch128++, output_shift);
      *rxdataF_comp128++ = mulByConjugate128(rxdataF128++, dl_ch128++, output_shift);
Wang's avatar
Wang committed
289
    }
knopp's avatar
knopp committed
290
  }
Wang's avatar
Wang committed
291
292
293
}

void nr_pbch_detection_mrc(NR_DL_FRAME_PARMS *frame_parms,
294
295
                           int **rxdataF_comp,
                           uint8_t symbol) {
knopp's avatar
knopp committed
296
  uint8_t symbol_mod;
Wang's avatar
Wang committed
297
298
299
300
301
302
303
304
305
306
  int i, nb_rb=6;
#if defined(__x86_64__) || defined(__i386__)
  __m128i *rxdataF_comp128_0,*rxdataF_comp128_1;
#elif defined(__arm__)
  int16x8_t *rxdataF_comp128_0,*rxdataF_comp128_1;
#endif
  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;

  if (frame_parms->nb_antennas_rx>1) {
#if defined(__x86_64__) || defined(__i386__)
307
308
    rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[0][symbol_mod*6*12];
    rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[1][symbol_mod*6*12];
Wang's avatar
Wang committed
309
#elif defined(__arm__)
310
311
    rxdataF_comp128_0   = (int16x8_t *)&rxdataF_comp[0][symbol_mod*6*12];
    rxdataF_comp128_1   = (int16x8_t *)&rxdataF_comp[1][symbol_mod*6*12];
Wang's avatar
Wang committed
312
#endif
313
314
315

    // MRC on each re of rb, both on MF output and magnitude (for 16QAM/64QAM llr computation)
    for (i=0; i<nb_rb*3; i++) {
Wang's avatar
Wang committed
316
#if defined(__x86_64__) || defined(__i386__)
317
      rxdataF_comp128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_0[i],1),_mm_srai_epi16(rxdataF_comp128_1[i],1));
Wang's avatar
Wang committed
318
#elif defined(__arm__)
319
      rxdataF_comp128_0[i] = vhaddq_s16(rxdataF_comp128_0[i],rxdataF_comp128_1[i]);
Wang's avatar
Wang committed
320
321
#endif
    }
322
  }
knopp's avatar
knopp committed
323

Wang's avatar
Wang committed
324
325
326
327
328
329
#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}

330
void nr_pbch_unscrambling(NR_UE_PBCH *pbch,
331
332
333
334
335
336
                          uint16_t Nid,
                          uint8_t nushift,
                          uint16_t M,
                          uint16_t length,
                          uint8_t bitwise,
                          uint32_t unscrambling_mask) {
337
  uint8_t reset, offset;
Wang's avatar
Wang committed
338
  uint32_t x1, x2, s=0;
Guy De Souza's avatar
Guy De Souza committed
339
  uint8_t k=0;
340
  int16_t *demod_pbch_e = pbch->llr;
Wang's avatar
Wang committed
341
342
  reset = 1;
  // x1 is set in first call to lte_gold_generic
343
  x2 = Nid; //this is c_init
Wang's avatar
Wang committed
344

345
  // The Gold sequence is shifted by nushift* M, so we skip (nushift*M /32) double words
346
  for (int i=0; i<(uint16_t)ceil(((float)nushift*M)/32); i++) {
347
348
349
    s = lte_gold_generic(&x1, &x2, reset);
    reset = 0;
  }
350

351
352
353
354
  // Scrambling is now done with offset (nushift*M)%32
  offset = (nushift*M)&0x1f;

  for (int i=0; i<length; i++) {
355
356
357
358
359
    /*if (((i+offset)&0x1f)==0) {
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }*/
    if (bitwise) {
360
      if (((k+offset)&0x1f)==0 && (!((unscrambling_mask>>i)&1))) {
361
362
363
        s = lte_gold_generic(&x1, &x2, reset);
        reset = 0;
      }
364
365
366

      (pbch->pbch_a_interleaved) ^= ((unscrambling_mask>>i)&1)? ((pbch->pbch_a_prime>>i)&1)<<i : (((pbch->pbch_a_prime>>i)&1) ^ ((s>>((k+offset)&0x1f))&1))<<i;
      k += (!((unscrambling_mask>>i)&1));
knopp's avatar
knopp committed
367
#ifdef DEBUG_PBCH_ENCODING
368
369
      printf("i %d k %d offset %d (unscrambling_mask>>i)&1) %d s: %08x\t  pbch_a_interleaved 0x%08x (!((unscrambling_mask>>i)&1)) %d\n", i, k, offset, (unscrambling_mask>>i)&1, s, pbch->pbch_a_interleaved,
             (!((unscrambling_mask>>i)&1)));
knopp's avatar
knopp committed
370
#endif
371
    } else {
372
373
374
375
      if (((i+offset)&0x1f)==0) {
        s = lte_gold_generic(&x1, &x2, reset);
        reset = 0;
      }
376
377
378
379

      if (((s>>((i+offset)&0x1f))&1)==1)
        demod_pbch_e[i] = -demod_pbch_e[i];

hongzhi wang's avatar
hongzhi wang committed
380
#ifdef DEBUG_PBCH_ENCODING
381
382
383
384

      if (i<8)
        printf("s %d demod_pbch_e[i] %d\n", ((s>>((i+offset)&0x1f))&1), demod_pbch_e[i]);

hongzhi wang's avatar
hongzhi wang committed
385
#endif
386
    }
Wang's avatar
Wang committed
387
388
389
  }
}

390
391
void nr_pbch_quantize(int16_t *pbch_llr8,
                      int16_t *pbch_llr,
392
                      uint16_t len) {
Wang's avatar
Wang committed
393
394
395
  uint16_t i;

  for (i=0; i<len; i++) {
396
397
398
399
    if (pbch_llr[i]>31)
      pbch_llr8[i]=32;
    else if (pbch_llr[i]<-31)
      pbch_llr8[i]=-32;
400
    else
hongzhi wang's avatar
hongzhi wang committed
401
      pbch_llr8[i] = (char)(pbch_llr[i]);
Wang's avatar
Wang committed
402
403
  }
}
404
/*
hongzhi wang's avatar
hongzhi wang committed
405
406
unsigned char sign(int8_t x) {
  return (unsigned char)x >> 7;
hongzhi wang's avatar
hongzhi wang committed
407
}
408
*/
hongzhi wang's avatar
hongzhi wang committed
409

Guy De Souza's avatar
Guy De Souza committed
410
uint8_t pbch_deinterleaving_pattern[32] = {28,0,31,30,7,29,25,27,5,8,24,9,10,11,12,13,1,4,3,14,15,16,17,2,26,18,19,20,21,22,6,23};
Wang's avatar
Wang committed
411

412
int nr_rx_pbch( PHY_VARS_NR_UE *ue,
Thomas Laurent's avatar
fixes    
Thomas Laurent committed
413
                UE_nr_rxtx_proc_t *proc,
414
415
416
                NR_UE_PBCH *nr_ue_pbch_vars,
                NR_DL_FRAME_PARMS *frame_parms,
                uint8_t eNB_id,
417
                uint8_t i_ssb,
418
419
                MIMO_mode_t mimo_mode,
                uint32_t high_speed_flag) {
420
421

  int Ns = proc->nr_tti_rx;
Wang's avatar
Wang committed
422
423
  NR_UE_COMMON *nr_ue_common_vars = &ue->common_vars;
  int max_h=0;
424
  int symbol;
hongzhi wang's avatar
hongzhi wang committed
425
  //uint8_t pbch_a[64];
426
  uint8_t *pbch_a = malloc(sizeof(uint8_t) * 32);
427
  //uint32_t pbch_a_prime;
knopp's avatar
knopp committed
428
  int16_t *pbch_e_rx;
Wang's avatar
Wang committed
429
  uint8_t *decoded_output = nr_ue_pbch_vars->decoded_output;
430
  uint8_t nushift,n_hf,ssb_index;
431
  uint16_t M;
432
  uint8_t Lmax=frame_parms->Lmax; 
hongzhi wang's avatar
hongzhi wang committed
433
  //uint16_t crc;
434
  //unsigned short idx_demod =0;
435
  uint32_t decoderState=0;
436
437
438
  //uint8_t decoderListSize = 8, pathMetricAppr = 0;
  //time_stats_t polar_decoder_init,polar_rate_matching,decoding,bit_extraction,deinterleaving;
  //time_stats_t path_metric,sorting,update_LLR;
Hongzhi Wang's avatar
Hongzhi Wang committed
439
440
  memset(&pbch_a[0], 0, sizeof(uint8_t) * NR_POLAR_PBCH_PAYLOAD_BITS);
  //printf("nr_pbch_ue nid_cell %d\n",frame_parms->Nid_cell);
Thomas Laurent's avatar
Thomas Laurent committed
441

Hongzhi Wang's avatar
Hongzhi Wang committed
442
  pbch_e_rx = &nr_ue_pbch_vars->llr[0];
Wang's avatar
Wang committed
443
  // clear LLR buffer
Hongzhi Wang's avatar
Hongzhi Wang committed
444
  memset(nr_ue_pbch_vars->llr,0,NR_POLAR_PBCH_E);
445
  int symbol_offset=1;
446

447
  if (ue->is_synchronized > 0)
448
    symbol_offset=(ue->symbol_offset)%(frame_parms->symbols_per_slot);
449
450
  else
    symbol_offset=0;
Wang's avatar
Wang committed
451

452

Florian Kaltenberger's avatar
WIP    
Florian Kaltenberger committed
453
#ifdef DEBUG_PBCH
454
  //printf("address dataf %p",nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns]].rxdataF);
455
  write_output("rxdataF0_pbch.m","rxF0pbch",
456
               &nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns]].rxdataF[0][(symbol_offset+1)*frame_parms->ofdm_symbol_size],frame_parms->ofdm_symbol_size*3,1,1);
Florian Kaltenberger's avatar
WIP    
Florian Kaltenberger committed
457
#endif
knopp's avatar
knopp committed
458

459
  // symbol refers to symbol within SSB. symbol_offset is the offset of the SSB wrt start of slot
460
  for (symbol=1; symbol<4; symbol++) {
461

462
    nr_pbch_extract(nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[Ns]].rxdataF,
Francesco Mani's avatar
Francesco Mani committed
463
                    nr_ue_pbch_vars->dl_ch_estimates,
464
465
466
467
468
469
                    nr_ue_pbch_vars->rxdataF_ext,
                    nr_ue_pbch_vars->dl_ch_estimates_ext,
                    symbol,
                    symbol_offset,
                    high_speed_flag,
                    frame_parms);
Wang's avatar
Wang committed
470
#ifdef DEBUG_PBCH
knopp's avatar
knopp committed
471
472
    LOG_I(PHY,"[PHY] PBCH Symbol %d ofdm size %d\n",symbol, frame_parms->ofdm_symbol_size );
    LOG_I(PHY,"[PHY] PBCH starting channel_level\n");
Wang's avatar
Wang committed
473
474
#endif

475
    if (symbol == 1) {
knopp's avatar
knopp committed
476
      max_h = nr_pbch_channel_level(nr_ue_pbch_vars->dl_ch_estimates_ext,
477
478
                                    frame_parms,
                                    symbol);
knopp's avatar
knopp committed
479
480
      nr_ue_pbch_vars->log2_maxh = 3+(log2_approx(max_h)/2);
    }
Wang's avatar
Wang committed
481
482

#ifdef DEBUG_PBCH
knopp's avatar
knopp committed
483
    LOG_I(PHY,"[PHY] PBCH log2_maxh = %d (%d)\n",nr_ue_pbch_vars->log2_maxh,max_h);
Wang's avatar
Wang committed
484
485
#endif
    nr_pbch_channel_compensation(nr_ue_pbch_vars->rxdataF_ext,
486
487
488
489
490
                                 nr_ue_pbch_vars->dl_ch_estimates_ext,
                                 nr_ue_pbch_vars->rxdataF_comp,
                                 frame_parms,
                                 symbol,
                                 nr_ue_pbch_vars->log2_maxh); // log2_maxh+I0_shift
Wang's avatar
Wang committed
491
492
493
494
495
496

    /*if (frame_parms->nb_antennas_rx > 1)
      pbch_detection_mrc(frame_parms,
                         nr_ue_pbch_vars->rxdataF_comp,
                         symbol);*/

497
498
499
500
501
502
503
504
    /*
        if (mimo_mode == ALAMOUTI) {
          nr_pbch_alamouti(frame_parms,nr_ue_pbch_vars->rxdataF_comp,symbol);
        } else if (mimo_mode != SISO) {
          LOG_I(PHY,"[PBCH][RX] Unsupported MIMO mode\n");
          return(-1);
        }
    */
505
    if (symbol==1) {
Wang's avatar
Wang committed
506
      nr_pbch_quantize(pbch_e_rx,
507
508
                       (short *)&(nr_ue_pbch_vars->rxdataF_comp[0][symbol*240]),
                       144);
Hongzhi Wang's avatar
Hongzhi Wang committed
509
      pbch_e_rx+=144;
Wang's avatar
Wang committed
510
511
    } else {
      nr_pbch_quantize(pbch_e_rx,
512
513
                       (short *)&(nr_ue_pbch_vars->rxdataF_comp[0][symbol*240]),
                       360);
Hongzhi Wang's avatar
Hongzhi Wang committed
514
      pbch_e_rx+=360;
Wang's avatar
Wang committed
515
516
517
    }
  }

knopp's avatar
knopp committed
518
#ifdef DEBUG_PBCH
519
  write_output("rxdataF_comp.m","rxFcomp",&nr_ue_pbch_vars->rxdataF_comp[0][240],240*3,1,1);
knopp's avatar
knopp committed
520
#endif
Wang's avatar
Wang committed
521
  pbch_e_rx = nr_ue_pbch_vars->llr;
522
  //demod_pbch_e = nr_ue_pbch_vars->demod_pbch_e;
523
  pbch_a = nr_ue_pbch_vars->pbch_a;
524
#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
525
  //pbch_e_rx = &nr_ue_pbch_vars->llr[0];
526
  short *p = (short *)&(nr_ue_pbch_vars->rxdataF_comp[0][20*12]);
527

knopp's avatar
knopp committed
528
529
  for (int cnt = 0; cnt < 864  ; cnt++)
    printf("pbch rx llr %d\n",*(pbch_e_rx+cnt));
Wang's avatar
Wang committed
530

Hongzhi Wang's avatar
Hongzhi Wang committed
531
#endif
532
533
  //un-scrambling
  M =  NR_POLAR_PBCH_E;
534
  nushift = (Lmax==4)? i_ssb&3 : i_ssb&7;
Guy De Souza's avatar
Guy De Souza committed
535
536
  uint32_t unscrambling_mask = (Lmax==64)?0x100006D:0x1000041;
  nr_pbch_unscrambling(nr_ue_pbch_vars,frame_parms->Nid_cell,nushift,M,NR_POLAR_PBCH_E,0,0);
Hongzhi Wang's avatar
Hongzhi Wang committed
537
  //polar decoding de-rate matching
Thomas Laurent's avatar
Thomas Laurent committed
538
  const t_nrPolar_params *currentPtr = nr_polar_params( NR_POLAR_PBCH_MESSAGE_TYPE, NR_POLAR_PBCH_PAYLOAD_BITS, NR_POLAR_PBCH_AGGREGATION_LEVEL);
539
  decoderState = polar_decoder_int16(pbch_e_rx,(uint64_t *)&nr_ue_pbch_vars->pbch_a_prime,currentPtr);
540

541
542
  if(decoderState) return(decoderState);

543
  //  printf("polar decoder output 0x%08x\n",nr_ue_pbch_vars->pbch_a_prime);
544
545
  // Decoder reversal
  uint32_t a_reversed=0;
546

547
548
549
550
  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
    a_reversed |= (((uint64_t)nr_ue_pbch_vars->pbch_a_prime>>i)&1)<<(31-i);

  nr_ue_pbch_vars->pbch_a_prime = a_reversed;
551
  //payload un-scrambling
552
  memset(&nr_ue_pbch_vars->pbch_a_interleaved, 0, sizeof(uint32_t) );
553
  M = (Lmax == 64)? (NR_POLAR_PBCH_PAYLOAD_BITS - 6) : (NR_POLAR_PBCH_PAYLOAD_BITS - 3);
Guy De Souza's avatar
Guy De Souza committed
554
555
  nushift = ((nr_ue_pbch_vars->pbch_a_prime>>24)&1) ^ (((nr_ue_pbch_vars->pbch_a_prime>>6)&1)<<1);
  nr_pbch_unscrambling(nr_ue_pbch_vars,frame_parms->Nid_cell,nushift,M,NR_POLAR_PBCH_PAYLOAD_BITS,1,unscrambling_mask);
556
  //printf("nushift %d sfn 3rd %d 2nd %d", nushift,((nr_ue_pbch_vars->pbch_a_prime>>6)&1), ((nr_ue_pbch_vars->pbch_a_prime>>24)&1) );
557
  //payload deinterleaving
558
559
  //uint32_t in=0;
  uint32_t out=0;
560

561
  for (int i=0; i<32; i++) {
562
    out |= ((nr_ue_pbch_vars->pbch_a_interleaved>>i)&1)<<(pbch_deinterleaving_pattern[i]);
563
#ifdef DEBUG_PBCH
564
    printf("i %d in 0x%08x out 0x%08x ilv %d (in>>i)&1) 0x%08x\n", i, nr_ue_pbch_vars->pbch_a_interleaved, out, pbch_deinterleaving_pattern[i], (nr_ue_pbch_vars->pbch_a_interleaved>>i)&1);
565
#endif
566
567
  }

568
569
570
  uint32_t payload = 0;
  uint8_t xtra_byte = 0;
  xtra_byte = (out>>24)&0xff;
Guy De Souza's avatar
Guy De Souza committed
571
572

  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
573
    payload |= ((out>>i)&1)<<(NR_POLAR_PBCH_PAYLOAD_BITS-i-1);
Guy De Souza's avatar
Guy De Souza committed
574

575
576
  for (int i=0; i<3; i++)
    decoded_output[i] = (uint8_t)((payload>>((3-i)<<3))&0xff);
hongzhi wang's avatar
hongzhi wang committed
577

578
579
580
581
582
583
584
585
586
587
  n_hf = ((xtra_byte>>4)&0x01); // computing the half frame index from the extra byte

  ssb_index = i_ssb;  // ssb index corresponds to i_ssb for Lmax = 4,8
  if (Lmax == 64) {   // for Lmax = 64 ssb index 4th,5th and 6th bits are in extra byte
    for (int i=0; i<3; i++)
      ssb_index += (((xtra_byte>>(7-i))&0x01)<<(3+i));
  }

  ue->symbol_offset = nr_get_ssb_start_symbol(frame_parms, ssb_index, n_hf);

588
#ifdef DEBUG_PBCH
589
  printf("xtra_byte %x payload %x\n", xtra_byte, payload);
590

591
592
593
594
595
  for (int i=0; i<(NR_POLAR_PBCH_PAYLOAD_BITS>>3); i++) {
    //     printf("unscrambling pbch_a[%d] = %x \n", i,pbch_a[i]);
    printf("[PBCH] decoder payload[%d] = %x\n",i,decoded_output[i]);
  }
#endif
596

597
  ue->dl_indication.rx_ind = &ue->rx_ind; //  hang on rx_ind instance
Thomas Laurent's avatar
fixes    
Thomas Laurent committed
598
  ue->dl_indication.proc=proc;
599
600
601
602
  //ue->rx_ind.sfn_slot = 0;  //should be set by higher-1-layer, i.e. clean_and_set_if_instance()
  ue->rx_ind.rx_indication_body[0].pdu_type = FAPI_NR_RX_PDU_TYPE_MIB;
  ue->rx_ind.rx_indication_body[0].mib_pdu.pdu = &decoded_output[0];
  ue->rx_ind.rx_indication_body[0].mib_pdu.additional_bits = xtra_byte;
603
  ue->rx_ind.rx_indication_body[0].mib_pdu.ssb_index = i_ssb;                //  confirm with TCL
604
605
606
607
608
609
610
611
  ue->rx_ind.rx_indication_body[0].mib_pdu.ssb_length = Lmax;                //  confirm with TCL
  ue->rx_ind.rx_indication_body[0].mib_pdu.cell_id = frame_parms->Nid_cell;  //  confirm with TCL
  ue->rx_ind.number_pdus = 1;

  if (ue->if_inst && ue->if_inst->dl_indication)
    ue->if_inst->dl_indication(&ue->dl_indication);

  return 0;
Wang's avatar
Wang committed
612
}