nr_pbch.c 24.7 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 "SIMULATION/TOOLS/sim.h"
Wang's avatar
Wang committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

//#define DEBUG_PBCH 1
//#define DEBUG_PBCH_ENCODING

#ifdef OPENAIR2
//#include "PHY_INTERFACE/defs.h"
#endif

#define PBCH_A 24


uint16_t nr_pbch_extract(int **rxdataF,
                      int **dl_ch_estimates,
                      int **rxdataF_ext,
                      int **dl_ch_estimates_ext,
                      uint32_t symbol,
                      uint32_t high_speed_flag,
                      NR_DL_FRAME_PARMS *frame_parms)
{


  uint16_t rb;
  uint8_t i,j,aarx,aatx;
hongzhi wang's avatar
hongzhi wang committed
60
  int32_t *dl_ch0,*dl_ch0_ext,*rxF,*rxF_ext;
Wang's avatar
Wang committed
61

Hongzhi Wang's avatar
Hongzhi Wang committed
62
  int rx_offset = frame_parms->ofdm_symbol_size-10*12;
hongzhi wang's avatar
hongzhi wang committed
63
  int nushiftmod4 = frame_parms->nushift;
Wang's avatar
Wang committed
64
65

  for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
hongzhi wang's avatar
hongzhi wang committed
66
        
Wang's avatar
Wang committed
67
68
    rxF        = &rxdataF[aarx][(rx_offset + (symbol*(frame_parms->ofdm_symbol_size)))];
    rxF_ext    = &rxdataF_ext[aarx][symbol*(20*12)];
hongzhi wang's avatar
hongzhi wang committed
69
#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
70
71
     printf("extract_rbs (nushift %d): rx_offset=%d, symbol %d\n",frame_parms->nushift,
     (rx_offset + (symbol*(frame_parms->ofdm_symbol_size))),symbol);
hongzhi wang's avatar
hongzhi wang committed
72
     int16_t *p = (int16_t *)rxF;
hongzhi wang's avatar
hongzhi wang committed
73
     for (int i =0; i<8;i++){
hongzhi wang's avatar
hongzhi wang committed
74
        printf("rxF [%d]= %d\n",i,rxF[i]);
hongzhi wang's avatar
hongzhi wang committed
75
76
        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
77
78
     }
#endif
Wang's avatar
Wang committed
79
80

    for (rb=0; rb<20; rb++) {
hongzhi wang's avatar
hongzhi wang committed
81
82
83
      if (rb==10) {
        rxF       = &rxdataF[aarx][((symbol*(frame_parms->ofdm_symbol_size)))];
      }
Wang's avatar
Wang committed
84

hongzhi wang's avatar
hongzhi wang committed
85
      j=0;
hongzhi wang's avatar
hongzhi wang committed
86
      if ((symbol==5) || (symbol==7)) {
Wang's avatar
Wang committed
87
88
89
90
        for (i=0; i<12; i++) {
          if ((i!=nushiftmod4) &&
              (i!=(nushiftmod4+4)) &&
              (i!=(nushiftmod4+8))) {
hongzhi wang's avatar
hongzhi wang committed
91
92
93
            rxF_ext[j]=rxF[i];
	    //printf("rxF ext[%d] = %d rxF [%d]= %d\n",j,rxF_ext[j],i,rxF[i]);
	    j++;
Wang's avatar
Wang committed
94
95
96
97
          }
        }

        rxF+=12;
hongzhi wang's avatar
hongzhi wang committed
98
        rxF_ext+=9;
Wang's avatar
Wang committed
99
      } else { //symbol 2
hongzhi wang's avatar
hongzhi wang committed
100
    	if ((rb < 4) || (rb >15)){
Wang's avatar
Wang committed
101
102
103
104
    	  for (i=0; i<12; i++) {
        	if ((i!=nushiftmod4) &&
        	    (i!=(nushiftmod4+4)) &&
        	    (i!=(nushiftmod4+8))) {
hongzhi wang's avatar
hongzhi wang committed
105
        	  rxF_ext[j]=rxF[i];
hongzhi wang's avatar
hongzhi wang committed
106
		  //printf("symbol2 rxF ext[%d] = %d at %p\n",j,rxF_ext[j],&rxF[i]);
hongzhi wang's avatar
hongzhi wang committed
107
		  j++;
Wang's avatar
Wang committed
108
109
        	}
    	  }
hongzhi wang's avatar
hongzhi wang committed
110
	}
Wang's avatar
Wang committed
111
112

        rxF+=12;
hongzhi wang's avatar
hongzhi wang committed
113
        rxF_ext+=9;
Wang's avatar
Wang committed
114
115
116
      }
    }

hongzhi wang's avatar
hongzhi wang committed
117
    for (aatx=0; aatx<frame_parms->nb_antenna_ports_eNB;aatx++) {
Wang's avatar
Wang committed
118
      if (high_speed_flag == 1)
hongzhi wang's avatar
hongzhi wang committed
119
        dl_ch0     = &dl_ch_estimates[(aatx<<1)+aarx][(symbol*(frame_parms->ofdm_symbol_size))];
Wang's avatar
Wang committed
120
      else
hongzhi wang's avatar
hongzhi wang committed
121
122
        dl_ch0     = &dl_ch_estimates[(aatx<<1)+aarx][0];

hongzhi wang's avatar
hongzhi wang committed
123
      //printf("dl_ch0 addr %p\n",dl_ch0);
Wang's avatar
Wang committed
124
125
126
127

      dl_ch0_ext = &dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*(20*12)];

      for (rb=0; rb<20; rb++) {
hongzhi wang's avatar
hongzhi wang committed
128
	j=0;
hongzhi wang's avatar
hongzhi wang committed
129
        if ((symbol==5) || (symbol==7)) {
hongzhi wang's avatar
hongzhi wang committed
130
              	  for (i=0; i<12; i++) {
Wang's avatar
Wang committed
131
132
133
                    if ((i!=nushiftmod4) &&
                        (i!=(nushiftmod4+4)) &&
                        (i!=(nushiftmod4+8))) {
hongzhi wang's avatar
hongzhi wang committed
134
                           dl_ch0_ext[j]=dl_ch0[i];
hongzhi wang's avatar
hongzhi wang committed
135
136
			   if ((rb==0) && (i<2))
			     printf("dl ch0 ext[%d] = %d dl_ch0 [%d]= %d\n",j,dl_ch0_ext[j],i,dl_ch0[i]);
hongzhi wang's avatar
hongzhi wang committed
137
		           j++;
Wang's avatar
Wang committed
138
139
140
141
                    }
          	  }

          dl_ch0+=12;
hongzhi wang's avatar
hongzhi wang committed
142
          dl_ch0_ext+=9;
Wang's avatar
Wang committed
143
        }
hongzhi wang's avatar
hongzhi wang committed
144
        else { 
hongzhi wang's avatar
hongzhi wang committed
145
              if ((rb < 4) || (rb >15)){
Wang's avatar
Wang committed
146
              	  for (i=0; i<12; i++) {
hongzhi wang's avatar
hongzhi wang committed
147
148
149
150
                    if ((i!=nushiftmod4) &&
                        (i!=(nushiftmod4+4)) &&
                        (i!=(nushiftmod4+8))) {
		           dl_ch0_ext[j]=dl_ch0[i];
hongzhi wang's avatar
hongzhi wang committed
151
			   //printf("symbol2 dl ch0 ext[%d] = %d dl_ch0 [%d]= %d\n",j,dl_ch0_ext[j],i,dl_ch0[i]);
hongzhi wang's avatar
hongzhi wang committed
152
			   j++;
Wang's avatar
Wang committed
153
154
155
156
157
                       	}
               	  }
              }

              dl_ch0+=12;
hongzhi wang's avatar
hongzhi wang committed
158
              dl_ch0_ext+=9;
Wang's avatar
Wang committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

         }
      }
    }  //tx antenna loop

  }

  return(0);
}

//__m128i avg128;

//compute average channel_level on each (TX,RX) antenna pair
int nr_pbch_channel_level(int **dl_ch_estimates_ext,
                       NR_DL_FRAME_PARMS *frame_parms,
                       uint32_t symbol)
{

hongzhi wang's avatar
hongzhi wang committed
177
  int16_t rb, nb_rb=20;
Wang's avatar
Wang committed
178
179
180
181
182
183
184
185
186
187
188
  uint8_t aatx,aarx;

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

hongzhi wang's avatar
hongzhi wang committed
189
  for (aatx=0; aatx<frame_parms->nb_antenna_ports_eNB;aatx++)
Wang's avatar
Wang committed
190
191
192
193
194
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {
      //clear average level

#if defined(__x86_64__) || defined(__i386__)
      avg128 = _mm_setzero_si128();
hongzhi wang's avatar
hongzhi wang committed
195
      dl_ch128=(__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*20*12];
Wang's avatar
Wang committed
196
197
#elif defined(__arm__)
      avg128 = vdupq_n_s32(0);
hongzhi wang's avatar
hongzhi wang committed
198
      dl_ch128=(int16x8_t *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*20*12];
Wang's avatar
Wang committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

#endif
      for (rb=0; rb<nb_rb; rb++) {
#if defined(__x86_64__) || defined(__i386__)
        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]));
#elif defined(__arm__)
// to be filled in
#endif
        dl_ch128+=3;
        /*
          if (rb==0) {
          print_shorts("dl_ch128",&dl_ch128[0]);
          print_shorts("dl_ch128",&dl_ch128[1]);
          print_shorts("dl_ch128",&dl_ch128[2]);
          }
        */
      }

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

      if (avg1>avg2)
        avg2 = avg1;

      //msg("Channel level : %d, %d\n",avg1, avg2);
    }
#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
  return(avg2);

}

#if defined(__x86_64__) || defined(__i386__)
__m128i mmtmpP0,mmtmpP1,mmtmpP2,mmtmpP3;
#elif defined(__arm__)
int16x8_t mmtmpP0,mmtmpP1,mmtmpP2,mmtmpP3;
#endif
void nr_pbch_channel_compensation(int **rxdataF_ext,
                               int **dl_ch_estimates_ext,
                               int **rxdataF_comp,
                               NR_DL_FRAME_PARMS *frame_parms,
                               uint8_t symbol,
                               uint8_t output_shift)
{

hongzhi wang's avatar
hongzhi wang committed
250
  uint16_t rb,nb_rb=20;
Hongzhi Wang's avatar
Hongzhi Wang committed
251
  uint8_t aatx,aarx;
Wang's avatar
Wang committed
252
253
254
255
256
257
#if defined(__x86_64__) || defined(__i386__)
  __m128i *dl_ch128,*rxdataF128,*rxdataF_comp128;
#elif defined(__arm__)

#endif

hongzhi wang's avatar
hongzhi wang committed
258
  for (aatx=0; aatx<frame_parms->nb_antenna_ports_eNB;aatx++)
Wang's avatar
Wang committed
259
260
261
    for (aarx=0; aarx<frame_parms->nb_antennas_rx; aarx++) {

#if defined(__x86_64__) || defined(__i386__)
hongzhi wang's avatar
hongzhi wang committed
262
263
264
265
266
267
      dl_ch128          = (__m128i *)&dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*20*12];
      rxdataF128        = (__m128i *)&rxdataF_ext[aarx][symbol*20*12];
      rxdataF_comp128   = (__m128i *)&rxdataF_comp[(aatx<<1)+aarx][symbol*20*12];
      //printf("ch compensation dl_ch ext addr %p \n", &dl_ch_estimates_ext[(aatx<<1)+aarx][symbol*20*12]);
      //printf("rxdataf ext addr %p symbol %d\n", &rxdataF_ext[aarx][symbol*20*12], symbol);
      //printf("rxdataf_comp addr %p\n",&rxdataF_comp[(aatx<<1)+aarx][symbol*20*12]); 
Wang's avatar
Wang committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
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
335

#elif defined(__arm__)
// to be filled in
#endif

      for (rb=0; rb<nb_rb; rb++) {
        //printf("rb %d\n",rb);
#if defined(__x86_64__) || defined(__i386__)
        // multiply by conjugated channel
        mmtmpP0 = _mm_madd_epi16(dl_ch128[0],rxdataF128[0]);
        //  print_ints("re",&mmtmpP0);
        // mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
        mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[0],_MM_SHUFFLE(2,3,0,1));
        mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
        mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
        //  print_ints("im",&mmtmpP1);
        mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[0]);
        // mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
        mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
        //  print_ints("re(shift)",&mmtmpP0);
        mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
        //  print_ints("im(shift)",&mmtmpP1);
        mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
        mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
        //      print_ints("c0",&mmtmpP2);
        //  print_ints("c1",&mmtmpP3);
        rxdataF_comp128[0] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
        //  print_shorts("rx:",rxdataF128);
        //  print_shorts("ch:",dl_ch128);
        //  print_shorts("pack:",rxdataF_comp128);

        // multiply by conjugated channel
        mmtmpP0 = _mm_madd_epi16(dl_ch128[1],rxdataF128[1]);
        // mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
        mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[1],_MM_SHUFFLE(2,3,0,1));
        mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
        mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
        mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[1]);
        // mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
        mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
        mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
        mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
        mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
        rxdataF_comp128[1] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
        //  print_shorts("rx:",rxdataF128+1);
        //  print_shorts("ch:",dl_ch128+1);
        //  print_shorts("pack:",rxdataF_comp128+1);

          // multiply by conjugated channel
          mmtmpP0 = _mm_madd_epi16(dl_ch128[2],rxdataF128[2]);
          // mmtmpP0 contains real part of 4 consecutive outputs (32-bit)
          mmtmpP1 = _mm_shufflelo_epi16(dl_ch128[2],_MM_SHUFFLE(2,3,0,1));
          mmtmpP1 = _mm_shufflehi_epi16(mmtmpP1,_MM_SHUFFLE(2,3,0,1));
          mmtmpP1 = _mm_sign_epi16(mmtmpP1,*(__m128i*)&conjugate[0]);
          mmtmpP1 = _mm_madd_epi16(mmtmpP1,rxdataF128[2]);
          // mmtmpP1 contains imag part of 4 consecutive outputs (32-bit)
          mmtmpP0 = _mm_srai_epi32(mmtmpP0,output_shift);
          mmtmpP1 = _mm_srai_epi32(mmtmpP1,output_shift);
          mmtmpP2 = _mm_unpacklo_epi32(mmtmpP0,mmtmpP1);
          mmtmpP3 = _mm_unpackhi_epi32(mmtmpP0,mmtmpP1);
          rxdataF_comp128[2] = _mm_packs_epi32(mmtmpP2,mmtmpP3);
          //  print_shorts("rx:",rxdataF128+2);
          //  print_shorts("ch:",dl_ch128+2);
          //      print_shorts("pack:",rxdataF_comp128+2);

          dl_ch128+=3;
          rxdataF128+=3;
          rxdataF_comp128+=3;
hongzhi wang's avatar
hongzhi wang committed
336
        
Wang's avatar
Wang committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
386
387
388
#elif defined(__arm__)
// to be filled in
#endif
      }
    }
#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}

void nr_pbch_detection_mrc(NR_DL_FRAME_PARMS *frame_parms,
                        int **rxdataF_comp,
                        uint8_t symbol)
{

  uint8_t aatx, symbol_mod;
  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) {
    for (aatx=0; aatx<4; aatx++) { //frame_parms->nb_antenna_ports_eNB;aatx++) {
#if defined(__x86_64__) || defined(__i386__)
      rxdataF_comp128_0   = (__m128i *)&rxdataF_comp[(aatx<<1)][symbol_mod*6*12];
      rxdataF_comp128_1   = (__m128i *)&rxdataF_comp[(aatx<<1)+1][symbol_mod*6*12];
#elif defined(__arm__)
      rxdataF_comp128_0   = (int16x8_t *)&rxdataF_comp[(aatx<<1)][symbol_mod*6*12];
      rxdataF_comp128_1   = (int16x8_t *)&rxdataF_comp[(aatx<<1)+1][symbol_mod*6*12];

#endif
      // 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++) {
#if defined(__x86_64__) || defined(__i386__)
        rxdataF_comp128_0[i] = _mm_adds_epi16(_mm_srai_epi16(rxdataF_comp128_0[i],1),_mm_srai_epi16(rxdataF_comp128_1[i],1));
#elif defined(__arm__)
        rxdataF_comp128_0[i] = vhaddq_s16(rxdataF_comp128_0[i],rxdataF_comp128_1[i]);

#endif
      }
    }
  }
#if defined(__x86_64__) || defined(__i386__)
  _mm_empty();
  _m_empty();
#endif
}

389
390
391
392
393
394
void nr_pbch_unscrambling(NR_UE_PBCH *pbch,
					   uint16_t Nid,
					   uint8_t nushift,
					   uint16_t M,
					   uint16_t length,
					   uint8_t bitwise)
Wang's avatar
Wang committed
395
396
{
  int i;
397
  uint8_t reset, offset;
Wang's avatar
Wang committed
398
  uint32_t x1, x2, s=0;
hongzhi wang's avatar
hongzhi wang committed
399
  double *demod_pbch_e = pbch->demod_pbch_e;
400
401
402
  uint32_t *pbch_a_prime = (uint32_t*)pbch->pbch_a_prime;
  uint32_t *pbch_a_interleaved = (uint32_t*)pbch->pbch_a_interleaved;
  uint32_t unscrambling_mask = 0x100006D;
Wang's avatar
Wang committed
403

hongzhi wang's avatar
hongzhi wang committed
404
  printf("unscramb nid_cell %d\n",Nid);
hongzhi wang's avatar
hongzhi wang committed
405

Wang's avatar
Wang committed
406
407
  reset = 1;
  // x1 is set in first call to lte_gold_generic
408
  x2 = Nid; //this is c_init
Wang's avatar
Wang committed
409

410
411
412
413
414
415
416
417
418
419
420
421
422
423
  // The Gold sequence is shifted by nushift* M, so we skip (nushift*M /32) double words
  for (int i=0; i<(uint16_t)ceil((nushift*M)/32); i++) {
    s = lte_gold_generic(&x1, &x2, reset);
    reset = 0;
  }
  // Scrambling is now done with offset (nushift*M)%32
  offset = (nushift*M)&0x1f;

  for (int i=0; i<length; i++) {
      if (((i+offset)&0x1f)==0) {
        s = lte_gold_generic(&x1, &x2, reset);
        reset = 0;
      }
  #ifdef DEBUG_PBCH_ENCODING
hongzhi wang's avatar
hongzhi wang committed
424
      if (i<8)
425
426
427
428
429
    printf("s: %04x\t", s);
  #endif
      if (bitwise) {
        (*pbch_a_interleaved) ^= ((unscrambling_mask>>i)&1)? (((*pbch_a_prime)>>i)&1)<<i : ((((*pbch_a_prime)>>i)&1) ^ ((s>>((i+offset)&0x1f))&1))<<i;
      }
Wang's avatar
Wang committed
430

431
432
      else {
    	  if (((s>>((i+offset)&0x1f))&1)==1)
hongzhi wang's avatar
hongzhi wang committed
433
    		  demod_pbch_e[i] = -demod_pbch_e[i];
hongzhi wang's avatar
hongzhi wang committed
434
435
436
437
#ifdef DEBUG_PBCH_ENCODING
		if (i<8)
	printf("s %d demod_pbch_e[i] %d\n", ((s>>((i+offset)&0x1f))&1), demod_pbch_e[i]);
#endif
438
      }
Wang's avatar
Wang committed
439
  }
440

Wang's avatar
Wang committed
441
442
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
473
474
475
476
477
478
479
480
}

void nr_pbch_alamouti(NR_DL_FRAME_PARMS *frame_parms,
                   int **rxdataF_comp,
                   uint8_t symbol)
{


  int16_t *rxF0,*rxF1;
  //  __m128i *ch_mag0,*ch_mag1,*ch_mag0b,*ch_mag1b;
  uint8_t rb,re,symbol_mod;
  int jj;

  //  printf("Doing alamouti\n");
  symbol_mod = (symbol>=(7-frame_parms->Ncp)) ? symbol-(7-frame_parms->Ncp) : symbol;
  jj         = (symbol_mod*6*12);

  rxF0     = (int16_t*)&rxdataF_comp[0][jj];  //tx antenna 0  h0*y
  rxF1     = (int16_t*)&rxdataF_comp[2][jj];  //tx antenna 1  h1*y

  for (rb=0; rb<6; rb++) {

    for (re=0; re<12; re+=2) {

      // Alamouti RX combining

      rxF0[0] = rxF0[0] + rxF1[2];
      rxF0[1] = rxF0[1] - rxF1[3];

      rxF0[2] = rxF0[2] - rxF1[0];
      rxF0[3] = rxF0[3] + rxF1[1];

      rxF0+=4;
      rxF1+=4;
    }

  }

}

hongzhi wang's avatar
hongzhi wang committed
481
void nr_pbch_quantize(int8_t *pbch_llr8,
Wang's avatar
Wang committed
482
483
484
485
486
487
488
                   int16_t *pbch_llr,
                   uint16_t len)
{

  uint16_t i;

  for (i=0; i<len; i++) {
489
490
491
492
493
    if (pbch_llr[i]>127)
      pbch_llr8[i]=127;
    else if (pbch_llr[i]<-128)
      pbch_llr8[i]=-128;
    else
hongzhi wang's avatar
hongzhi wang committed
494
      pbch_llr8[i] = (char)(pbch_llr[i]);
Wang's avatar
Wang committed
495
496
497
498

  }
}

hongzhi wang's avatar
hongzhi wang committed
499
500
unsigned char sign(int8_t x) {
  return (unsigned char)x >> 7;
hongzhi wang's avatar
hongzhi wang committed
501
502
}

503
504
uint8_t pbch_deinterleaving_pattern[32] = {28,0,31,30,1,29,25,27,22,2,24,3,4,5,6,7,18,21,20,8,9,10,11,19,26,12,13,14,15,16,23,17};

Hongzhi Wang's avatar
Hongzhi Wang committed
505
int nr_rx_pbch( PHY_VARS_NR_UE *ue,
hongzhi wang's avatar
hongzhi wang committed
506
507
508
509
510
511
512
		     UE_nr_rxtx_proc_t *proc,
		     NR_UE_PBCH *nr_ue_pbch_vars,
		     NR_DL_FRAME_PARMS *frame_parms,
		     uint8_t eNB_id,
		     MIMO_mode_t mimo_mode,
		     uint32_t high_speed_flag,
		     uint8_t frame_mod4)
Wang's avatar
Wang committed
513
514
515
516
{

  NR_UE_COMMON *nr_ue_common_vars = &ue->common_vars;

Hongzhi Wang's avatar
Hongzhi Wang committed
517
  uint8_t log2_maxh;
Wang's avatar
Wang committed
518
519
520
  int max_h=0;

  int symbol,i;
hongzhi wang's avatar
hongzhi wang committed
521
  //uint8_t pbch_a[64];
522
523
  uint8_t *pbch_a = malloc(sizeof(uint8_t) * 32);
  uint8_t *pbch_a_prime;
hongzhi wang's avatar
hongzhi wang committed
524
  uint8_t *pbch_a_b = malloc(sizeof(uint8_t) *NR_POLAR_PBCH_PAYLOAD_BITS);
hongzhi wang's avatar
hongzhi wang committed
525
  int8_t *pbch_e_rx;
Wang's avatar
Wang committed
526
  uint8_t *decoded_output = nr_ue_pbch_vars->decoded_output;
527
528
529
530
  uint8_t nushift;
  uint16_t M;
  uint8_t Lmax=8; //to update
  uint8_t ssb_index=0;
hongzhi wang's avatar
hongzhi wang committed
531
  //uint16_t crc;
hongzhi wang's avatar
hongzhi wang committed
532
  //short nr_demod_table[8] = {0,0,0,1,1,0,1,1};
533
  double nr_demod_table[8] = {0.707,0.707,-0.707,0.707,0.707,-0.707,-0.707,-0.707};
hongzhi wang's avatar
hongzhi wang committed
534
535
  double *demod_pbch_e  = malloc (sizeof(double) * 864); 
  unsigned short idx_demod =0;
Hongzhi Wang's avatar
Hongzhi Wang committed
536
537
538
  int8_t decoderState=0;
  uint8_t decoderListSize = 8, pathMetricAppr = 0;
  double aPrioriArray[frame_parms->pbch_polar_params.payloadBits];  // assume no a priori knowledge available about the payload.
hongzhi wang's avatar
hongzhi wang committed
539

Hongzhi Wang's avatar
Hongzhi Wang committed
540
  memset(&pbch_a[0], 0, sizeof(uint8_t) * NR_POLAR_PBCH_PAYLOAD_BITS);
hongzhi wang's avatar
hongzhi wang committed
541

Hongzhi Wang's avatar
Hongzhi Wang committed
542
  //printf("nr_pbch_ue nid_cell %d\n",frame_parms->Nid_cell);
hongzhi wang's avatar
hongzhi wang committed
543

hongzhi wang's avatar
hongzhi wang committed
544
  for (int i=0; i<frame_parms->pbch_polar_params.payloadBits; i++) aPrioriArray[i] = NAN;
Wang's avatar
Wang committed
545
546
547

  int subframe_rx = proc->subframe_rx;

Hongzhi Wang's avatar
Hongzhi Wang committed
548
  pbch_e_rx = &nr_ue_pbch_vars->llr[0];
Wang's avatar
Wang committed
549
550

  // clear LLR buffer
Hongzhi Wang's avatar
Hongzhi Wang committed
551
  memset(nr_ue_pbch_vars->llr,0,NR_POLAR_PBCH_E);
Wang's avatar
Wang committed
552

Hongzhi Wang's avatar
Hongzhi Wang committed
553
  for (symbol=5; symbol<8; symbol++) {
Wang's avatar
Wang committed
554

hongzhi wang's avatar
hongzhi wang committed
555
    //printf("address dataf %p",nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[subframe_rx]].rxdataF);
hongzhi wang's avatar
hongzhi wang committed
556
    //write_output("rxdataF0_pbch.m","rxF0pbch",nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[subframe_rx]].rxdataF,frame_parms->ofdm_symbol_size*4,2,1);
hongzhi wang's avatar
hongzhi wang committed
557
  
Wang's avatar
Wang committed
558
559
560
561
562
563
564
565
    nr_pbch_extract(nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[subframe_rx]].rxdataF,
                 nr_ue_common_vars->common_vars_rx_data_per_thread[ue->current_thread_id[subframe_rx]].dl_ch_estimates[eNB_id],
                 nr_ue_pbch_vars->rxdataF_ext,
                 nr_ue_pbch_vars->dl_ch_estimates_ext,
                 symbol,
                 high_speed_flag,
                 frame_parms);
#ifdef DEBUG_PBCH
Hongzhi Wang's avatar
Hongzhi Wang committed
566
    msg("[PHY] PBCH Symbol %d ofdm size %d\n",symbol, frame_parms->ofdm_symbol_size );
Wang's avatar
Wang committed
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    msg("[PHY] PBCH starting channel_level\n");
#endif

    max_h = nr_pbch_channel_level(nr_ue_pbch_vars->dl_ch_estimates_ext,
                               frame_parms,
                               symbol);
    log2_maxh = 3+(log2_approx(max_h)/2);

#ifdef DEBUG_PBCH
    msg("[PHY] PBCH log2_maxh = %d (%d)\n",log2_maxh,max_h);
#endif

    nr_pbch_channel_compensation(nr_ue_pbch_vars->rxdataF_ext,
                              nr_ue_pbch_vars->dl_ch_estimates_ext,
                              nr_ue_pbch_vars->rxdataF_comp,
                              frame_parms,
                              symbol,
                              log2_maxh); // log2_maxh+I0_shift

hongzhi wang's avatar
hongzhi wang committed
586
587
    //write_output("rxdataF_comp.m","rxFcomp",nr_ue_pbch_vars->rxdataF_comp,180,2,1);
  
Wang's avatar
Wang committed
588
589
590
591
592
593
594
595
596
597
598
599
600
    /*if (frame_parms->nb_antennas_rx > 1)
      pbch_detection_mrc(frame_parms,
                         nr_ue_pbch_vars->rxdataF_comp,
                         symbol);*/


    if (mimo_mode == ALAMOUTI) {
      nr_pbch_alamouti(frame_parms,nr_ue_pbch_vars->rxdataF_comp,symbol);
    } else if (mimo_mode != SISO) {
      msg("[PBCH][RX] Unsupported MIMO mode\n");
      return(-1);
    }

hongzhi wang's avatar
hongzhi wang committed
601
    if (symbol==6) {
Wang's avatar
Wang committed
602
      nr_pbch_quantize(pbch_e_rx,
Hongzhi Wang's avatar
Hongzhi Wang committed
603
                    (short*)&(nr_ue_pbch_vars->rxdataF_comp[0][symbol*240]),
Hongzhi Wang's avatar
Hongzhi Wang committed
604
                    144);
Wang's avatar
Wang committed
605

Hongzhi Wang's avatar
Hongzhi Wang committed
606
      pbch_e_rx+=144;
Wang's avatar
Wang committed
607
608
    } else {
      nr_pbch_quantize(pbch_e_rx,
Hongzhi Wang's avatar
Hongzhi Wang committed
609
610
                    (short*)&(nr_ue_pbch_vars->rxdataF_comp[0][symbol*240]),
                    360);
Wang's avatar
Wang committed
611

Hongzhi Wang's avatar
Hongzhi Wang committed
612
      pbch_e_rx+=360;
Wang's avatar
Wang committed
613
614
615
616
617
618
    }


  }

  pbch_e_rx = nr_ue_pbch_vars->llr;
619
620
621
  demod_pbch_e = nr_ue_pbch_vars->demod_pbch_e;
  pbch_a = nr_ue_pbch_vars->pbch_a;
  pbch_a_prime = nr_ue_pbch_vars->pbch_a_prime;
Hongzhi Wang's avatar
Hongzhi Wang committed
622

623
//#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
624
  //pbch_e_rx = &nr_ue_pbch_vars->llr[0];
Wang's avatar
Wang committed
625

hongzhi wang's avatar
hongzhi wang committed
626
  short *p = (short *)&(nr_ue_pbch_vars->rxdataF_comp[0][5*20*12]);
hongzhi wang's avatar
hongzhi wang committed
627
628
  for (int cnt = 0; cnt < 8 ; cnt++)
    printf("pbch rx llr %d rxdata_comp %d addr %p\n",*(pbch_e_rx+cnt), p[cnt], &p[0]);
629
//#endif
Wang's avatar
Wang committed
630

Hongzhi Wang's avatar
Hongzhi Wang committed
631
  for (i=0; i<NR_POLAR_PBCH_E/2; i++){
hongzhi wang's avatar
hongzhi wang committed
632
633
634
    idx_demod = (sign(pbch_e_rx[i<<1])&1) ^ ((sign(pbch_e_rx[(i<<1)+1])&1)<<1);
    demod_pbch_e[i<<1] = nr_demod_table[(idx_demod)<<1];
    demod_pbch_e[(i<<1)+1] = nr_demod_table[((idx_demod)<<1)+1];
Hongzhi Wang's avatar
Hongzhi Wang committed
635
#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
636
637
638
    if (i<16){
    printf("idx[%d]= %d\n", i , idx_demod);
    printf("sign[%d]= %d sign[%d]= %d\n", i<<1 , sign(pbch_e_rx[i<<1]), (i<<1)+1 , sign(pbch_e_rx[(i<<1)+1]));
Hongzhi Wang's avatar
Hongzhi Wang committed
639
    printf("demod_pbch_e[%d] r = %2.3f i = %2.3f\n", i<<1 , demod_pbch_e[i<<1], demod_pbch_e[(i<<1)+1]);}
Hongzhi Wang's avatar
Hongzhi Wang committed
640
#endif
hongzhi wang's avatar
hongzhi wang committed
641
642
  }

643
644
645
646
  //un-scrambling
  M =  NR_POLAR_PBCH_E;
  nushift = (Lmax==4)? ssb_index&3 : ssb_index&7;
  nr_pbch_unscrambling(nr_ue_pbch_vars,frame_parms->Nid_cell,nushift,M,NR_POLAR_PBCH_E,0);
Hongzhi Wang's avatar
Hongzhi Wang committed
647

648
//#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
649
    for (i=0; i<16; i++){
Hongzhi Wang's avatar
Hongzhi Wang committed
650
    printf("unscrambling demod_pbch_e[%d] r = %2.3f i = %2.3f\n", i<<1 , demod_pbch_e[i<<1], demod_pbch_e[(i<<1)+1]);}
651
//#endif
hongzhi wang's avatar
hongzhi wang committed
652
		
Hongzhi Wang's avatar
Hongzhi Wang committed
653
  //polar decoding de-rate matching
hongzhi wang's avatar
hongzhi wang committed
654
  decoderState = polar_decoder(demod_pbch_e, pbch_a_b, &frame_parms->pbch_polar_params, decoderListSize, aPrioriArray, pathMetricAppr);
hongzhi wang's avatar
hongzhi wang committed
655
  printf("polar decoder state %d\n", decoderState);
656
657
  if(decoderState == -1)
  	return(decoderState);
Wang's avatar
Wang committed
658

hongzhi wang's avatar
hongzhi wang committed
659
  memset(&pbch_a_prime[0], 0, sizeof(uint8_t) * NR_POLAR_PBCH_PAYLOAD_BITS>>3);
660
  for (i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
hongzhi wang's avatar
hongzhi wang committed
661
662
663
664
    {
      pbch_a_prime[i/8] ^= (pbch_a_b[i]&1)<<(i&7);
      //printf("pbch_a_b[%d] = %u pbch_a_prime[i/8] 0x%02x \n", i,pbch_a_b[i],pbch_a_prime[i/8]);
    }
Wang's avatar
Wang committed
665

666
//#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
667
668
  for (i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS>>3; i++)
     printf("pbch_a_prime[%d] = 0x%02x\n", i,pbch_a_prime[i]);
669
//#endif
hongzhi wang's avatar
hongzhi wang committed
670
  
671
  //payload un-scrambling
hongzhi wang's avatar
hongzhi wang committed
672
  memset(nr_ue_pbch_vars->pbch_a_interleaved, 0, sizeof(uint8_t) * NR_POLAR_PBCH_PAYLOAD_BITS>>3);
673
  M = (Lmax == 64)? (NR_POLAR_PBCH_PAYLOAD_BITS - 6) : (NR_POLAR_PBCH_PAYLOAD_BITS - 3);
hongzhi wang's avatar
hongzhi wang committed
674
675
  nushift = ((pbch_a_prime[0]>>6)&1) ^ (((pbch_a_prime[3])&1)<<1);
  //printf("payload unscrambling nushift %d sfn3 %d sfn2 %d M %d\n",nushift, ((pbch_a_prime[0]>>6)&1),((pbch_a_prime[3])&1),M);
676
677
678
679
680
  nr_pbch_unscrambling(nr_ue_pbch_vars,frame_parms->Nid_cell,nushift,M,NR_POLAR_PBCH_PAYLOAD_BITS,1);

  //payload deinterleaving
  uint32_t in=0, out=0;
  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS>>3; i++)
hongzhi wang's avatar
hongzhi wang committed
681
    in |= (uint32_t)(nr_ue_pbch_vars->pbch_a_interleaved[i]<<(i<<3));
682
683
684

  for (int i=0; i<32; i++) {
    out |= ((in>>i)&1)<<(pbch_deinterleaving_pattern[i]);
685
#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
686
  printf("i %d in 0x%08x out 0x%08x ilv %d (in>>i)&1) 0x%08x\n", i, in, out, pbch_deinterleaving_pattern[i], (in>>i)&1);
687
#endif
688
689
690
  }

  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS>>3; i++)
hongzhi wang's avatar
hongzhi wang committed
691
	  pbch_a[i] = (uint8_t)((out>>(i<<3))&0xff);
hongzhi wang's avatar
hongzhi wang committed
692

Hongzhi Wang's avatar
Hongzhi Wang committed
693
  // Fix byte endian
694
695
  for (i=0; i<(NR_POLAR_PBCH_PAYLOAD_BITS>>3); i++)
     decoded_output[(NR_POLAR_PBCH_PAYLOAD_BITS>>3)-i-1] = pbch_a[i];
Wang's avatar
Wang committed
696

hongzhi wang's avatar
hongzhi wang committed
697
  //#ifdef DEBUG_PBCH
hongzhi wang's avatar
hongzhi wang committed
698
699
700
701
  for (i=0; i<(NR_POLAR_PBCH_PAYLOAD_BITS>>3); i++){
  	  printf("unscrambling pbch_a[%d] = %x \n", i,pbch_a[i]);
  	  printf("[PBCH] decoder_output[%d] = %x\n",i,decoded_output[i]);
  }
hongzhi wang's avatar
hongzhi wang committed
702
	  //#endif
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    ue->dl_indication.rx_ind = &ue->rx_ind; //  hang on rx_ind instance
    //ue->rx_ind.sfn_slot = 0;  //should be set by higher-1-layer, i.e. clean_and_set_if_instance()
    ue->rx_ind.number_pdus = ue->rx_ind.number_pdus + 1;
    ue->rx_ind.rx_request_body = (fapi_nr_rx_request_body_t *)malloc(sizeof(fapi_nr_rx_request_body_t));
    ue->rx_ind.rx_request_body->pdu_type = FAPI_NR_RX_PDU_TYPE_MIB;
    ue->rx_ind.rx_request_body->mib_pdu.pdu = &decoded_output[1];
    ue->rx_ind.rx_request_body->mib_pdu.additional_bits = decoded_output[0];
    ue->rx_ind.rx_request_body->mib_pdu.ssb_index = ssb_index;            //  confirm with TCL
    ue->rx_ind.rx_request_body->mib_pdu.ssb_length = Lmax;                //  confirm with TCL
    ue->rx_ind.rx_request_body->mib_pdu.cell_id = frame_parms->Nid_cell;  //  confirm with TCL

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

//  dci reception part, should be put into dci_nr.c ? TBD
#if 0
    ue->dl_indication.dci_ind = &ue->dci_ind; //  hang on rx_ind instance
    //ue->dci_ind.sfn_slot = 0;  //should be set by higher-1-layer, i.e. clean_and_set_if_instance()
    uint32_t num_dci = 1;
    uint32_t ii;

    ue->dci_ind.number_of_dcis = num_dci;
    ue->dci_ind.dci_list = (fapi_nr_dci_indication_pdu_t *)malloc(num_dci * sizeof(fapi_nr_dci_indication_pdu_t));
725
    
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
    for(ii=0; ii<num_dci; ++ii){
        //TODO check this part
        (ue->dci_ind.dci_list+ii)->rnti = 0x0000;
        (ue->dci_ind.dci_list+ii)->dci_type = 0;
        
        //  TODO to be fill with TCL
        //ue->dci_ind.dci_list[ii]->dci.

    }

    ue->if_inst->dl_indication(&ue->dl_indication);
#endif 


    return 0;    
Wang's avatar
Wang committed
741
}
742
743
744
745
746