nr_pbch.c 15.1 KB
Newer Older
Guy De Souza's avatar
Guy De Souza committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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.1  (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
 */

Guy De Souza's avatar
Guy De Souza committed
22
/*! \file PHY/NR_TRANSPORT/nr_pbch.c
Guy De Souza's avatar
Guy De Souza committed
23
* \brief Top-level routines for generating the PBCH/BCH physical/transport channel V15.1 03/2018
24
* \author Guy De Souza
25
* \thanks Special Thanks to Son Dang for helpful contributions and testing
Guy De Souza's avatar
Guy De Souza committed
26
* \date 2018
Guy De Souza's avatar
Guy De Souza committed
27
28
* \version 0.1
* \company Eurecom
Guy De Souza's avatar
Guy De Souza committed
29
* \email: desouza@eurecom.fr
Guy De Souza's avatar
Guy De Souza committed
30
31
32
* \note
* \warning
*/
Guy De Souza's avatar
Guy De Souza committed
33

34
#include "PHY/defs_gNB.h"
Guido Casati's avatar
Guido Casati committed
35
#include "PHY/NR_TRANSPORT/nr_transport_proto.h"
Guy De Souza's avatar
Guy De Souza committed
36
#include "PHY/LTE_REFSIG/lte_refsig.h"
Guy De Souza's avatar
Guy De Souza committed
37
38
#include "PHY/sse_intrin.h"

Guy De Souza's avatar
Guy De Souza committed
39
//#define DEBUG_PBCH
knopp's avatar
knopp committed
40
//#define DEBUG_PBCH_ENCODING
41
//#define DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
42

Sakthi's avatar
Sakthi committed
43
extern short nr_qpsk_mod_table[8];
Guy De Souza's avatar
Guy De Souza committed
44

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

Guy De Souza's avatar
Guy De Souza committed
49
int nr_generate_pbch_dmrs(uint32_t *gold_pbch_dmrs,
50
                          int32_t *txdataF,
Guy De Souza's avatar
Guy De Souza committed
51
52
                          int16_t amp,
                          uint8_t ssb_start_symbol,
53
                          nfapi_nr_config_request_scf_t *config,
54
                          NR_DL_FRAME_PARMS *frame_parms) {
55
  int k,l;
56
  //int16_t a;
57
58
  int16_t mod_dmrs[NR_PBCH_DMRS_LENGTH<<1];
  uint8_t idx=0;
59
  uint8_t nushift = config->cell_config.phy_cell_id.value &3;
60
  LOG_D(PHY, "PBCH DMRS mapping started at symbol %d shift %d\n", ssb_start_symbol+1, nushift);
Guy De Souza's avatar
Guy De Souza committed
61

62
  /// QPSK modulation
63
  for (int m=0; m<NR_PBCH_DMRS_LENGTH; m++) {
Sakthivel Velumani's avatar
Sakthivel Velumani committed
64
    idx = (((gold_pbch_dmrs[(m<<1)>>5])>>((m<<1)&0x1f))&3);
Sakthi's avatar
Sakthi committed
65
66
    mod_dmrs[m<<1] = nr_qpsk_mod_table[idx<<1];
    mod_dmrs[(m<<1)+1] = nr_qpsk_mod_table[(idx<<1) + 1];
Guy De Souza's avatar
Guy De Souza committed
67
#ifdef DEBUG_PBCH_DMRS
68
69
    printf("m %d idx %d gold seq %d b0-b1 %d-%d mod_dmrs %d %d\n", m, idx, gold_pbch_dmrs[(m<<1)>>5], (((gold_pbch_dmrs[(m<<1)>>5])>>((m<<1)&0x1f))&1),
           (((gold_pbch_dmrs[((m<<1)+1)>>5])>>(((m<<1)+1)&0x1f))&1), mod_dmrs[(m<<1)], mod_dmrs[(m<<1)+1]);
Guy De Souza's avatar
Guy De Souza committed
70
71
72
73
#endif
  }

  /// Resource mapping
74
75
76
77
  // PBCH DMRS are mapped  within the SSB block on every fourth subcarrier starting from nushift of symbols 1, 2, 3
  ///symbol 1  [0+nushift:4:236+nushift] -- 60 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier + nushift;
  l = ssb_start_symbol + 1;
Guy De Souza's avatar
Guy De Souza committed
78

79
  for (int m = 0; m < 60; m++) {
Guy De Souza's avatar
Guy De Souza committed
80
#ifdef DEBUG_PBCH_DMRS
81
    printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
82
#endif
83
84
    ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_dmrs[m<<1]) >> 15;
    ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_dmrs[(m<<1) + 1]) >> 15;
knopp's avatar
knopp committed
85
#ifdef DEBUG_PBCH_DMRS
86
87
88
    printf("(%d,%d)\n",
           ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1],
           ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
knopp's avatar
knopp committed
89
#endif
90
    k+=4;
Guy De Souza's avatar
Guy De Souza committed
91

92
93
94
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
Guy De Souza's avatar
Guy De Souza committed
95

96
97
98
  ///symbol 2  [0+u:4:44+nushift ; 192+nu:4:236+nushift] -- 24 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier + nushift;
  l++;
Guy De Souza's avatar
Guy De Souza committed
99

100
  for (int m = 60; m < 84; m++) {
Guy De Souza's avatar
Guy De Souza committed
101
#ifdef DEBUG_PBCH_DMRS
102
    printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
103
#endif
104
105
    ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_dmrs[m<<1]) >> 15;
    ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_dmrs[(m<<1) + 1]) >> 15;
knopp's avatar
knopp committed
106
#ifdef DEBUG_PBCH_DMRS
107
108
109
    printf("(%d,%d)\n",
           ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1],
           ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
knopp's avatar
knopp committed
110
#endif
111
    k+=(m==71)?148:4; // Jump from 44+nu to 192+nu
Guy De Souza's avatar
Guy De Souza committed
112

113
114
115
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
Guy De Souza's avatar
Guy De Souza committed
116

117
118
119
  ///symbol 3  [0+nushift:4:236+nushift] -- 60 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier + nushift;
  l++;
Guy De Souza's avatar
Guy De Souza committed
120

121
  for (int m = 84; m < NR_PBCH_DMRS_LENGTH; m++) {
Guy De Souza's avatar
Guy De Souza committed
122
#ifdef DEBUG_PBCH_DMRS
123
    printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
124
#endif
125
126
    ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_dmrs[m<<1]) >> 15;
    ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_dmrs[(m<<1) + 1]) >> 15;
knopp's avatar
knopp committed
127
#ifdef DEBUG_PBCH_DMRS
128
129
130
    printf("(%d,%d)\n",
           ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1],
           ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
knopp's avatar
knopp committed
131
#endif
132
    k+=4;
Guy De Souza's avatar
Guy De Souza committed
133

134
135
136
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
Guy De Souza's avatar
Guy De Souza committed
137

Guy De Souza's avatar
Guy De Souza committed
138
139
#ifdef DEBUG_PBCH_DMRS
  write_output("txdataF_pbch_dmrs.m", "txdataF_pbch_dmrs", txdataF[0], frame_parms->samples_per_frame_wCP>>1, 1, 1);
Guy De Souza's avatar
Guy De Souza committed
140
#endif
Guy De Souza's avatar
Guy De Souza committed
141
142
143
  return 0;
}

Thomas Laurent's avatar
Thomas Laurent committed
144
static void nr_pbch_scrambling(NR_gNB_PBCH *pbch,
Guy De Souza's avatar
Guy De Souza committed
145
                        uint32_t Nid,
Guy De Souza's avatar
Guy De Souza committed
146
                        uint8_t nushift,
Guy De Souza's avatar
Guy De Souza committed
147
                        uint16_t M,
Guy De Souza's avatar
Guy De Souza committed
148
                        uint16_t length,
Guy De Souza's avatar
Guy De Souza committed
149
150
                        uint8_t encoded,
                        uint32_t unscrambling_mask) {
Guy De Souza's avatar
Guy De Souza committed
151
  uint8_t reset, offset;
152
  uint32_t x1, x2, s=0;
Hongzhi's avatar
Hongzhi committed
153
  uint32_t *pbch_e = pbch->pbch_e;
154
155
156
  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = Nid;
Guy De Souza's avatar
Guy De Souza committed
157

158
  // The Gold sequence is shifted by nushift* M, so we skip (nushift*M /32) double words
knopp's avatar
knopp committed
159
  for (int i=0; i<(uint16_t)ceil(((float)nushift*M)/32); i++) {
Guy De Souza's avatar
Guy De Souza committed
160
    s = lte_gold_generic(&x1, &x2, reset);
161
162
    reset = 0;
  }
Guy De Souza's avatar
Guy De Souza committed
163

Guy De Souza's avatar
Guy De Souza committed
164
  // Scrambling is now done with offset (nushift*M)%32
Guy De Souza's avatar
Guy De Souza committed
165
  offset = (nushift*M)&0x1f;
Guy De Souza's avatar
Guy De Souza committed
166
#ifdef DEBUG_PBCH_ENCODING
Hongzhi's avatar
Hongzhi committed
167
  printf("Scrambling params: nushift %d M %d length %d encoded %d offset %d\n", nushift, M, length, encoded, offset);
Guy De Souza's avatar
Guy De Souza committed
168
169
#endif
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
170
  printf("s: %04x\t", s);
Guy De Souza's avatar
Guy De Souza committed
171
#endif
Guy De Souza's avatar
Guy De Souza committed
172
173
174
175
176
  int k = 0;

  if (!encoded) {
    /// 1st Scrambling
    for (int i = 0; i < length; ++i) {
177
178
      if ((unscrambling_mask>>i)&1)
        pbch->pbch_a_prime ^= ((pbch->pbch_a_interleaved>>i)&1)<<i;
Guy De Souza's avatar
Guy De Souza committed
179
180
181
182
183
      else {
        if (((k+offset)&0x1f)==0) {
          s = lte_gold_generic(&x1, &x2, reset);
          reset = 0;
        }
184

185
        pbch->pbch_a_prime ^= (((pbch->pbch_a_interleaved>>i)&1) ^ ((s>>((k+offset)&0x1f))&1))<<i;
Guy De Souza's avatar
Guy De Souza committed
186
187
188
189
190
191
192
193
194
195
        k++;                  /// k increase only when payload bit is not special bit
      }
    }
  } else {
    /// 2nd Scrambling
    for (int i = 0; i < length; ++i) {
      if (((i+offset)&0x1f)==0) {
        s = lte_gold_generic(&x1, &x2, reset);
        reset = 0;
      }
Guy De Souza's avatar
Guy De Souza committed
196

Guy De Souza's avatar
Guy De Souza committed
197
      pbch_e[i>>5] ^= (((s>>((i+offset)&0x1f))&1)<<(i&0x1f));
Guy De Souza's avatar
Guy De Souza committed
198
    }
199
200
201
  }
}

Guy De Souza's avatar
Guy De Souza committed
202

Guy De Souza's avatar
Guy De Souza committed
203
void nr_init_pbch_interleaver(uint8_t *interleaver) {
Guy De Souza's avatar
Guy De Souza committed
204
  uint8_t j_sfn=0, j_hrf=10, j_ssb=11, j_other=14;
205
  memset((void *)interleaver,0, NR_POLAR_PBCH_PAYLOAD_BITS);
Guy De Souza's avatar
Guy De Souza committed
206

Guy De Souza's avatar
Guy De Souza committed
207
  for (uint8_t i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
Guy De Souza's avatar
Guy De Souza committed
208
    if (!i) // choice bit:1
209
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_other++);
Guy De Souza's avatar
Guy De Souza committed
210
    else if (i<7) //Sfn bits:6
211
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_sfn++);
Guy De Souza's avatar
Guy De Souza committed
212
    else if (i<24) // other:17
213
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_other++);
Guy De Souza's avatar
Guy De Souza committed
214
    else if (i<28) // Sfn:4
215
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_sfn++);
Guy De Souza's avatar
Guy De Souza committed
216
    else if (i==28) // Hrf bit:1
217
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_hrf);
Guy De Souza's avatar
Guy De Souza committed
218
    else // Ssb bits:3
219
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_ssb++);
220
}
Guy De Souza's avatar
Guy De Souza committed
221

Guy De Souza's avatar
Guy De Souza committed
222
int nr_generate_pbch(NR_gNB_PBCH *pbch,
knopp's avatar
knopp committed
223
		     nfapi_nr_dl_tti_ssb_pdu *ssb_pdu,
Guy De Souza's avatar
Guy De Souza committed
224
                     uint8_t *interleaver,
225
                     int32_t *txdataF,
Guy De Souza's avatar
Guy De Souza committed
226
227
                     int16_t amp,
                     uint8_t ssb_start_symbol,
228
229
                     uint8_t n_hf,
                     int sfn,
230
                     nfapi_nr_config_request_scf_t *config,
231
                     NR_DL_FRAME_PARMS *frame_parms) {
Guy De Souza's avatar
Guy De Souza committed
232
  int k,l,m;
233
  //int16_t a;
Guy De Souza's avatar
Guy De Souza committed
234
235
236
237
  int16_t mod_pbch_e[NR_POLAR_PBCH_E];
  uint8_t idx=0;
  uint16_t M;
  uint8_t nushift;
Guy De Souza's avatar
Guy De Souza committed
238
  uint32_t unscrambling_mask;
Guy De Souza's avatar
Guy De Souza committed
239
  uint64_t a_reversed=0;
240
  LOG_D(PHY, "PBCH generation started\n");
241
  ///Payload generation
242
  memset((void *)pbch, 0, sizeof(NR_gNB_PBCH));
Guy De Souza's avatar
Guy De Souza committed
243
  pbch->pbch_a=0;
244
  uint8_t ssb_index = ssb_pdu->ssb_pdu_rel15.SsbBlockIndex;
knopp's avatar
knopp committed
245
  uint8_t *pbch_pdu = (uint8_t*)&ssb_pdu->ssb_pdu_rel15.bchPayload;
Francesco Mani's avatar
Francesco Mani committed
246
  uint8_t Lmax = frame_parms->Lmax;
Guy De Souza's avatar
Guy De Souza committed
247
  for (int i=0; i<NR_PBCH_PDU_BITS; i++)
Francesco Mani's avatar
Francesco Mani committed
248
    pbch->pbch_a |= ((pbch_pdu[i>>3]>>(7-(i&7)))&1)<<i;
249

Francesco Mani's avatar
Francesco Mani committed
250
  #ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
251
252
  for (int i=0; i<3; i++)
    printf("pbch_pdu[%d]: 0x%02x\n", i, pbch_pdu[i]);
253

Guy De Souza's avatar
Guy De Souza committed
254
  printf("PBCH payload = 0x%08x\n",pbch->pbch_a);
Francesco Mani's avatar
Francesco Mani committed
255
  #endif
256

257
  // Extra byte generation
258
  for (int i=0; i<4; i++)
259
    pbch->pbch_a |= ((sfn>>(3-i))&1)<<(24+i); // resp. 4th, 3rd, 2nd ans 1st lsb of sfn
260

261
  pbch->pbch_a |= n_hf<<28; // half frame index bit
262

Guy De Souza's avatar
Guy De Souza committed
263
264
  if (Lmax == 64)
    for (int i=0; i<3; i++)
265
      pbch->pbch_a |= ((ssb_index>>(5-i))&1)<<(29+i); // resp. 6th, 5th and 4th bits of ssb_index
Guy De Souza's avatar
Guy De Souza committed
266
  else
267
    pbch->pbch_a |= ((config->ssb_table.ssb_subcarrier_offset.value>>4)&1)<<29; //MSB of k_SSB (bit index 4)
Guy De Souza's avatar
Guy De Souza committed
268

269
  LOG_D(PHY,"After extra byte: pbch_a = 0x%08x\n",pbch->pbch_a);
Guy De Souza's avatar
Guy De Souza committed
270

271
272
  // Payload interleaving

Guy De Souza's avatar
Guy De Souza committed
273
  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++) {
Guy De Souza's avatar
Guy De Souza committed
274
    pbch->pbch_a_interleaved |= ((pbch->pbch_a>>i)&1)<<(*(interleaver+i));
Guy De Souza's avatar
Guy De Souza committed
275
#ifdef DEBUG_PBCH_ENCODING
276
    printf("i %d out 0x%08x ilv %d (in>>i)&1) %d\n", i, pbch->pbch_a_interleaved, *(interleaver+i), (pbch->pbch_a>>i)&1);
Guy De Souza's avatar
Guy De Souza committed
277
278
#endif
  }
Guy De Souza's avatar
Guy De Souza committed
279

Guy De Souza's avatar
Guy De Souza committed
280
281
#ifdef DEBUG_PBCH_ENCODING
  printf("Interleaving:\n");
knopp's avatar
knopp committed
282
  printf("pbch_a_interleaved: 0x%08x\n", pbch->pbch_a_interleaved);
Guy De Souza's avatar
Guy De Souza committed
283
#endif
284
  // Scrambling
285
  unscrambling_mask = (Lmax ==64)? 0x100006D:0x1000041;
Guy De Souza's avatar
Guy De Souza committed
286
287
  M = (Lmax == 64)? (NR_POLAR_PBCH_PAYLOAD_BITS - 6) : (NR_POLAR_PBCH_PAYLOAD_BITS - 3);
  nushift = (((sfn>>2)&1)<<1) ^ ((sfn>>1)&1);
knopp's avatar
knopp committed
288
  pbch->pbch_a_prime = 0;
289
  nr_pbch_scrambling(pbch, (uint32_t)config->cell_config.phy_cell_id.value, nushift, M, NR_POLAR_PBCH_PAYLOAD_BITS, 0, unscrambling_mask);
Guy De Souza's avatar
Guy De Souza committed
290
#ifdef DEBUG_PBCH_ENCODING
knopp's avatar
knopp committed
291
292
  printf("Payload scrambling: nushift %d M %d sfn3 %d sfn2 %d\n", nushift, M, (sfn>>2)&1, (sfn>>1)&1);
  printf("pbch_a_prime: 0x%08x\n", pbch->pbch_a_prime);
Guy De Souza's avatar
Guy De Souza committed
293
#endif
294

295
  // Encoder reversal
Guy De Souza's avatar
Guy De Souza committed
296
297
298
  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
    a_reversed |= (((uint64_t)pbch->pbch_a_prime>>i)&1)<<(31-i);

299
  /// CRC, coding and rate matching
Thomas Laurent's avatar
Thomas Laurent committed
300
  polar_encoder_fast (&a_reversed, (void*)pbch->pbch_e, 0, 0,
301
                      nr_polar_params( NR_POLAR_PBCH_MESSAGE_TYPE, NR_POLAR_PBCH_PAYLOAD_BITS, NR_POLAR_PBCH_AGGREGATION_LEVEL,0,NULL)
Thomas Laurent's avatar
Thomas Laurent committed
302
                     );
Guy De Souza's avatar
Guy De Souza committed
303
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
304
  printf("Channel coding:\n");
305

Hongzhi's avatar
Hongzhi committed
306
  for (int i=0; i<NR_POLAR_PBCH_E_DWORD; i++)
Guy De Souza's avatar
Guy De Souza committed
307
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
308

Guy De Souza's avatar
Guy De Souza committed
309
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
310
#endif
Guy De Souza's avatar
Guy De Souza committed
311
312
  /// Scrambling
  M =  NR_POLAR_PBCH_E;
Guy De Souza's avatar
Guy De Souza committed
313
  nushift = (Lmax==4)? ssb_index&3 : ssb_index&7;
314
  nr_pbch_scrambling(pbch, (uint32_t)config->cell_config.phy_cell_id.value, nushift, M, NR_POLAR_PBCH_E, 1, 0);
Guy De Souza's avatar
Guy De Souza committed
315
316
#ifdef DEBUG_PBCH_ENCODING
  printf("Scrambling:\n");
317

Hongzhi's avatar
Hongzhi committed
318
  for (int i=0; i<NR_POLAR_PBCH_E_DWORD; i++)
Guy De Souza's avatar
Guy De Souza committed
319
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
320

Guy De Souza's avatar
Guy De Souza committed
321
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
322
#endif
323
324

  /// QPSK modulation
325
  for (int i=0; i<NR_POLAR_PBCH_E>>1; i++) {
Sakthivel Velumani's avatar
Sakthivel Velumani committed
326
    idx = ((pbch->pbch_e[(i<<1)>>5]>>((i<<1)&0x1f))&3);
Sakthi's avatar
Sakthi committed
327
328
    mod_pbch_e[i<<1] = nr_qpsk_mod_table[idx<<1];
    mod_pbch_e[(i<<1)+1] = nr_qpsk_mod_table[(idx<<1)+1];
329
#ifdef DEBUG_PBCH
330
    printf("i %d idx %d  mod_pbch %d %d\n", i, idx, mod_pbch_e[2*i], mod_pbch_e[2*i+1]);
331
332
333
334
#endif
  }

  /// Resource mapping
335
  nushift = config->cell_config.phy_cell_id.value &3;
336
337
338
339
340
341
342
343
344
345
346
  // PBCH modulated symbols are mapped  within the SSB block on symbols 1, 2, 3 excluding the subcarriers used for the PBCH DMRS
  ///symbol 1  [0:239] -- 180 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
  l = ssb_start_symbol + 1;
  m = 0;

  for (int ssb_sc_idx = 0; ssb_sc_idx < 240; ssb_sc_idx++) {
    if ((ssb_sc_idx&3) == nushift) {  //skip DMRS
      k++;
      continue;
    } else {
Guy De Souza's avatar
Guy De Souza committed
347
#ifdef DEBUG_PBCH
348
      printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
Guy De Souza's avatar
Guy De Souza committed
349
#endif
350
351
352
353
      ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_pbch_e[m<<1]) >> 15;
      ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_pbch_e[(m<<1) + 1]) >> 15;
      k++;
      m++;
354
355
    }

356
357
358
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
Guy De Souza's avatar
Guy De Souza committed
359

360
361
362
363
  ///symbol 2  [0:47 ; 192:239] -- 72 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
  l++;
  m=180;
364

365
366
367
368
369
  for (int ssb_sc_idx = 0; ssb_sc_idx < 48; ssb_sc_idx++) {
    if ((ssb_sc_idx&3) == nushift) {
      k++;
      continue;
    } else {
370
#ifdef DEBUG_PBCH
371
      printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
372
#endif
373
374
375
376
      ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_pbch_e[m<<1]) >> 15;
      ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_pbch_e[(m<<1) + 1]) >> 15;
      k++;
      m++;
Guy De Souza's avatar
Guy De Souza committed
377
378
    }

Guy De Souza's avatar
Guy De Souza committed
379
380
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
381
  }
Guy De Souza's avatar
Guy De Souza committed
382

383
  k += 144;
Guy De Souza's avatar
Guy De Souza committed
384

385
386
  if (k >= frame_parms->ofdm_symbol_size)
    k-=frame_parms->ofdm_symbol_size;
Guy De Souza's avatar
Guy De Souza committed
387

388
389
390
391
392
393
394
  m=216;

  for (int ssb_sc_idx = 192; ssb_sc_idx < 240; ssb_sc_idx++) {
    if ((ssb_sc_idx&3) == nushift) {
      k++;
      continue;
    } else {
Guy De Souza's avatar
Guy De Souza committed
395
#ifdef DEBUG_PBCH
396
      printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
Guy De Souza's avatar
Guy De Souza committed
397
#endif
398
399
400
401
      ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_pbch_e[m<<1]) >> 15;
      ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_pbch_e[(m<<1) + 1]) >> 15;
      k++;
      m++;
402
403
    }

404
405
406
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
Guy De Souza's avatar
Guy De Souza committed
407

408
409
410
411
  ///symbol 3  [0:239] -- 180 mod symbols
  k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
  l++;
  m=252;
412

413
414
415
416
417
  for (int ssb_sc_idx = 0; ssb_sc_idx < 240; ssb_sc_idx++) {
    if ((ssb_sc_idx&3) == nushift) {
      k++;
      continue;
    } else {
Guy De Souza's avatar
Guy De Souza committed
418
#ifdef DEBUG_PBCH
419
      printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
Guy De Souza's avatar
Guy De Souza committed
420
#endif
421
422
423
424
      ((int16_t *)txdataF)[(l*frame_parms->ofdm_symbol_size + k)<<1]       = (amp * mod_pbch_e[m<<1]) >> 15;
      ((int16_t *)txdataF)[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (amp * mod_pbch_e[(m<<1) + 1]) >> 15;
      k++;
      m++;
425
426
    }

427

428
429
430
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;
  }
431

432

Guy De Souza's avatar
Guy De Souza committed
433
  return 0;
Guy De Souza's avatar
Guy De Souza committed
434
}