nr_pbch.c 15.4 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
Guy De Souza's avatar
Guy De Souza committed
25
* \date 2018
Guy De Souza's avatar
Guy De Souza committed
26
27
* \version 0.1
* \company Eurecom
Guy De Souza's avatar
Guy De Souza committed
28
* \email: desouza@eurecom.fr
Guy De Souza's avatar
Guy De Souza committed
29
30
31
* \note
* \warning
*/
Guy De Souza's avatar
Guy De Souza committed
32

33
#include "PHY/defs_gNB.h"
Guy De Souza's avatar
Guy De Souza committed
34
#include "PHY/NR_TRANSPORT/nr_transport.h"
Guy De Souza's avatar
Guy De Souza committed
35
#include "PHY/LTE_REFSIG/lte_refsig.h"
Guy De Souza's avatar
Guy De Souza committed
36
37
#include "PHY/sse_intrin.h"

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

42
#include "PHY/NR_REFSIG/nr_mod_table.h"
Guy De Souza's avatar
Guy De Souza committed
43

Guy De Souza's avatar
Guy De Souza committed
44
45
46
uint8_t nr_pbch_payload_interleaving_pattern[32] = {16, 23, 18, 17, 8, 30, 10, 6, 24, 7, 0, 5, 3, 2, 1, 4,
                                                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
47
48
49
50
int nr_generate_pbch_dmrs(uint32_t *gold_pbch_dmrs,
                          int32_t **txdataF,
                          int16_t amp,
                          uint8_t ssb_start_symbol,
ChenWeiTai's avatar
ChenWeiTai committed
51
                          nfapi_nr_config_request_t* config,
Guy De Souza's avatar
Guy De Souza committed
52
53
                          NR_DL_FRAME_PARMS *frame_parms)
{
54
55
  int k,l;
  int16_t a;
56
57
  int16_t mod_dmrs[NR_PBCH_DMRS_LENGTH<<1];
  uint8_t idx=0;
Guy De Souza's avatar
Guy De Souza committed
58
  uint8_t nushift = config->sch_config.physical_cell_id.value &3;
Guy De Souza's avatar
Guy De Souza committed
59

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++) {
64
65
66
    idx = ((((gold_pbch_dmrs[(m<<1)>>5])>>((m<<1)&0x1f))&1)<<1) ^ (((gold_pbch_dmrs[((m<<1)+1)>>5])>>(((m<<1)+1)&0x1f))&1);
    mod_dmrs[m<<1] = nr_mod_table[(NR_MOD_TABLE_QPSK_OFFSET + idx)<<1];
    mod_dmrs[(m<<1)+1] = nr_mod_table[((NR_MOD_TABLE_QPSK_OFFSET + 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
74
75
#endif
  }

  /// Resource mapping
  a = (config->rf_config.tx_antenna_ports.value == 1) ? amp : (amp*ONE_OVER_SQRT2_Q15)>>15;

76
  for (int aa = 0; aa < config->rf_config.tx_antenna_ports.value; aa++)
Guy De Souza's avatar
Guy De Souza committed
77
78
  {

79
80
81
    // 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;
Guy De Souza's avatar
Guy De Souza committed
82
83
    l = ssb_start_symbol + 1;

84
    for (int m = 0; m < 60; m++) {
Guy De Souza's avatar
Guy De Souza committed
85
#ifdef DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
86
  printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
87
#endif
Guy De Souza's avatar
Guy De Souza committed
88
      ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_dmrs[m<<1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
89
      ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[(m<<1) + 1]) >> 15;
knopp's avatar
knopp committed
90
91
92
93
94
#ifdef DEBUG_PBCH_DMRS
      printf("(%d,%d)\n",
	((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1],
	((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
#endif
Guy De Souza's avatar
Guy De Souza committed
95
96
97
98
99
100
      k+=4;

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

101
102
      ///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;
Guy De Souza's avatar
Guy De Souza committed
103
104
    l++;

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

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }
Guy De Souza's avatar
Guy De Souza committed
121

122
123
      ///symbol 3  [0+nushift:4:236+nushift] -- 60 mod symbols
    k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier + nushift;
Guy De Souza's avatar
Guy De Souza committed
124
125
    l++;

126
    for (int m = 84; m < NR_PBCH_DMRS_LENGTH; m++) {
Guy De Souza's avatar
Guy De Souza committed
127
#ifdef DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
128
  printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
129
#endif
Guy De Souza's avatar
Guy De Souza committed
130
      ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_dmrs[m<<1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
131
      ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[(m<<1) + 1]) >> 15;
knopp's avatar
knopp committed
132
133
134
135
136
#ifdef DEBUG_PBCH_DMRS
      printf("(%d,%d)\n",
	((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1],
	((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1)+1]);
#endif
Guy De Souza's avatar
Guy De Souza committed
137
138
139
140
141
142
      k+=4;

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

Guy De Souza's avatar
Guy De Souza committed
143
  }
Guy De Souza's avatar
Guy De Souza committed
144
145


Guy De Souza's avatar
Guy De Souza committed
146
147
#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
148
#endif
Guy De Souza's avatar
Guy De Souza committed
149
150
151
  return 0;
}

Guy De Souza's avatar
Guy De Souza committed
152
153
void nr_pbch_scrambling(NR_gNB_PBCH *pbch,
                        uint32_t Nid,
Guy De Souza's avatar
Guy De Souza committed
154
                        uint8_t nushift,
Guy De Souza's avatar
Guy De Souza committed
155
                        uint16_t M,
Guy De Souza's avatar
Guy De Souza committed
156
                        uint16_t length,
Guy De Souza's avatar
Guy De Souza committed
157
                        uint8_t encoded)
158
{
Guy De Souza's avatar
Guy De Souza committed
159
  uint8_t reset, offset;
160
  uint32_t x1, x2, s=0;
Hongzhi's avatar
Hongzhi committed
161
  uint32_t *pbch_e = pbch->pbch_e;
Guy De Souza's avatar
Guy De Souza committed
162
  uint32_t unscrambling_mask = 0x100006D;
163
164
165
  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = Nid;
166
  // The Gold sequence is shifted by nushift* M, so we skip (nushift*M /32) double words
knopp's avatar
knopp committed
167
  for (int i=0; i<(uint16_t)ceil(((float)nushift*M)/32); i++) {
Guy De Souza's avatar
Guy De Souza committed
168
    s = lte_gold_generic(&x1, &x2, reset);
169
170
    reset = 0;
  }
Guy De Souza's avatar
Guy De Souza committed
171
  // Scrambling is now done with offset (nushift*M)%32
Guy De Souza's avatar
Guy De Souza committed
172
  offset = (nushift*M)&0x1f;
Guy De Souza's avatar
Guy De Souza committed
173
#ifdef DEBUG_PBCH_ENCODING
Hongzhi's avatar
Hongzhi committed
174
  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
175
#endif
176

177
  for (int i=0; i<length; i++) {
Guy De Souza's avatar
Guy De Souza committed
178
    if (((i+offset)&0x1f)==0) {
179
180
181
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }
Guy De Souza's avatar
Guy De Souza committed
182
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
183
  printf("s: %04x\t", s);
Guy De Souza's avatar
Guy De Souza committed
184
#endif
knopp's avatar
knopp committed
185
186
    if (!encoded)
      pbch->pbch_a_prime ^= ((unscrambling_mask>>i)&1)? ((pbch->pbch_a_interleaved>>i)&1)<<i : (((pbch->pbch_a_interleaved>>i)&1) ^ ((s>>((i+offset)&0x1f))&1))<<i;      
Guy De Souza's avatar
Guy De Souza committed
187
188

    else
Guy De Souza's avatar
Guy De Souza committed
189
      pbch_e[i>>5] ^= (((s>>((i+offset)&0x1f))&1)<<(i&0x1f));
190
191
192
  }
}

Guy De Souza's avatar
Guy De Souza committed
193
uint8_t nr_init_pbch_interleaver(uint8_t *interleaver) {
Guy De Souza's avatar
Guy De Souza committed
194
195
196
  uint8_t j_sfn=0, j_hrf=10, j_ssb=11, j_other=14;
  memset((void*)interleaver,0, NR_POLAR_PBCH_PAYLOAD_BITS);

Guy De Souza's avatar
Guy De Souza committed
197
198
199
200
201
202
203
  for (uint8_t i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS; i++)
    if (i<6) //Sfn bits:6
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_sfn++);
    else if (i<24) // other
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_other++);
    else if (i<28) // Sfn:4
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_sfn++);
Guy De Souza's avatar
Guy De Souza committed
204
    else if (i==28) // Hrf bit
Guy De Souza's avatar
Guy De Souza committed
205
206
207
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_hrf);
    else // Ssb bits
      *(interleaver+i) = *(nr_pbch_payload_interleaving_pattern+j_ssb++);
208
}
Guy De Souza's avatar
Guy De Souza committed
209
210

/*This pattern takes into account the adjustments for the field specific counters j_sfn, j_hrf, j_ssb and j_other*/
211
212
//uint8_t nr_pbch_payload_interleaving_pattern[32] = {1,4,9,11,12,13,14,15,19,20,21,22,25,26,27,28,
//                                                    29,31,16,23,18,17,8,30,10,6,24,7,0,5,3,2};
Guy De Souza's avatar
Guy De Souza committed
213
214

int nr_generate_pbch(NR_gNB_PBCH *pbch,
Guy De Souza's avatar
Guy De Souza committed
215
                     t_nrPolar_paramsPtr polar_params,
Guy De Souza's avatar
Guy De Souza committed
216
                     uint8_t *pbch_pdu,
Guy De Souza's avatar
Guy De Souza committed
217
                     uint8_t *interleaver,
Guy De Souza's avatar
Guy De Souza committed
218
219
220
                     int32_t **txdataF,
                     int16_t amp,
                     uint8_t ssb_start_symbol,
221
                     uint8_t n_hf,
Guy De Souza's avatar
Guy De Souza committed
222
223
                     uint8_t Lmax,
                     uint8_t ssb_index,
224
                     int sfn,
ChenWeiTai's avatar
ChenWeiTai committed
225
                     nfapi_nr_config_request_t* config,
Guy De Souza's avatar
Guy De Souza committed
226
227
228
                     NR_DL_FRAME_PARMS *frame_parms)
{

Guy De Souza's avatar
Guy De Souza committed
229
  int k,l,m;
230
  int16_t a;
Guy De Souza's avatar
Guy De Souza committed
231
232
233
234
  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
235
236
  uint8_t *xbyte = pbch->pbch_a;
  memset((void*) xbyte, 0, 1);
knopp's avatar
knopp committed
237
  //uint8_t pbch_a_b[32];
Guy De Souza's avatar
Guy De Souza committed
238

239
  //  LOG_D(PHY, "PBCH generation started\n");
Guy De Souza's avatar
Guy De Souza committed
240

knopp's avatar
knopp committed
241
  memset((void*)pbch, 0, sizeof(NR_gNB_PBCH));
242
243
  ///Payload generation
    // Fix byte endian
Guy De Souza's avatar
Guy De Souza committed
244
245
246
247
248
249
  if (!(sfn&7))
    for (int i=0; i<(NR_PBCH_PDU_BITS>>3); i++)
      pbch->pbch_a[(NR_POLAR_PBCH_PAYLOAD_BITS>>3)-i-1] = pbch_pdu[i];
#ifdef DEBUG_PBCH_ENCODING
  printf("Byte endian fix:\n");
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
250
    printf("pbch_a[%d]: 0x%02x\n", i, pbch->pbch_a[i]);
Guy De Souza's avatar
Guy De Souza committed
251
#endif
252

Guy De Souza's avatar
Guy De Souza committed
253
    // Extra byte generation
254
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
255
    (*xbyte) ^= ((sfn>>i)&1)<<i; // 4 lsb of sfn
256

Guy De Souza's avatar
Guy De Souza committed
257
  (*xbyte) ^= n_hf<<4; // half frame index bit
258

Guy De Souza's avatar
Guy De Souza committed
259
260
261
262
263
  if (Lmax == 64)
    for (int i=0; i<3; i++)
      (*xbyte) ^= ((ssb_index>>(3+i))&1)<<(5+i); // resp. 4th, 5th and 6th bits of ssb_index
  else
    (*xbyte) ^= ((config->sch_config.ssb_subcarrier_offset.value>>5)&1)<<5; //MSB of k_SSB
Guy De Souza's avatar
Guy De Souza committed
264
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
265
266
  printf("Extra byte:\n");
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
267
    printf("pbch_a[%d]: 0x%02x\n", i, pbch->pbch_a[i]);
Guy De Souza's avatar
Guy De Souza committed
268
269
270
#endif

    // Payload interleaving
271
  uint32_t in=0;
Guy De Souza's avatar
Guy De Souza committed
272
273
274
  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS>>3; i++)
    in |= (uint32_t)(pbch->pbch_a[i]<<((3-i)<<3));

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

Guy De Souza's avatar
Guy De Souza committed
282
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
283
  printf("Interleaving:\n");
knopp's avatar
knopp committed
284
  printf("pbch_a_interleaved: 0x%08x\n", pbch->pbch_a_interleaved);
Guy De Souza's avatar
Guy De Souza committed
285
#endif
286
287

    // Scrambling
Guy De Souza's avatar
Guy De Souza committed
288
289
  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
290
  pbch->pbch_a_prime = 0;
Guy De Souza's avatar
Guy De Souza committed
291
  nr_pbch_scrambling(pbch, (uint32_t)config->sch_config.physical_cell_id.value, nushift, M, NR_POLAR_PBCH_PAYLOAD_BITS, 0);
Guy De Souza's avatar
Guy De Souza committed
292
#ifdef DEBUG_PBCH_ENCODING
knopp's avatar
knopp committed
293
294
  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
295
#endif
296
297

  /// CRC, coding and rate matching
knopp's avatar
knopp committed
298
  polar_encoder (&pbch->pbch_a_prime, pbch->pbch_e, polar_params);
Guy De Souza's avatar
Guy De Souza committed
299
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
300
  printf("Channel coding:\n");
Hongzhi's avatar
Hongzhi committed
301
  for (int i=0; i<NR_POLAR_PBCH_E_DWORD; i++)
Guy De Souza's avatar
Guy De Souza committed
302
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
Guy De Souza's avatar
Guy De Souza committed
303
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
304
#endif
305

Guy De Souza's avatar
Guy De Souza committed
306
307
  /// Scrambling
  M =  NR_POLAR_PBCH_E;
Guy De Souza's avatar
Guy De Souza committed
308
  nushift = (Lmax==4)? ssb_index&3 : ssb_index&7;
Guy De Souza's avatar
Guy De Souza committed
309
  nr_pbch_scrambling(pbch, (uint32_t)config->sch_config.physical_cell_id.value, nushift, M, NR_POLAR_PBCH_E, 1);
Guy De Souza's avatar
Guy De Souza committed
310
311
#ifdef DEBUG_PBCH_ENCODING
  printf("Scrambling:\n");
Hongzhi's avatar
Hongzhi committed
312
  for (int i=0; i<NR_POLAR_PBCH_E_DWORD; i++)
Guy De Souza's avatar
Guy De Souza committed
313
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
Guy De Souza's avatar
Guy De Souza committed
314
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
315
#endif
316
317

  /// QPSK modulation
Guy De Souza's avatar
Guy De Souza committed
318
  for (int i=0; i<NR_POLAR_PBCH_E>>1; i++){
Guy De Souza's avatar
Guy De Souza committed
319
    idx = (((pbch->pbch_e[(i<<1)>>5]>>((i<<1)&0x1f))&1)<<1) ^ ((pbch->pbch_e[((i<<1)+1)>>5]>>(((i<<1)+1)&0x1f))&1);
Guy De Souza's avatar
Guy De Souza committed
320
321
    mod_pbch_e[i<<1] = nr_mod_table[(NR_MOD_TABLE_QPSK_OFFSET + idx)<<1];
    mod_pbch_e[(i<<1)+1] = nr_mod_table[((NR_MOD_TABLE_QPSK_OFFSET + idx)<<1)+1];
322
323

#ifdef DEBUG_PBCH
Guy De Souza's avatar
Guy De Souza committed
324
  printf("i %d idx %d  mod_pbch %d %d\n", i, idx, mod_pbch_e[2*i], mod_pbch_e[2*i+1]);
325
326
327
328
#endif
  }

  /// Resource mapping
Guy De Souza's avatar
Guy De Souza committed
329
  nushift = config->sch_config.physical_cell_id.value &3;
Guy De Souza's avatar
Guy De Souza committed
330
  a = (config->rf_config.tx_antenna_ports.value == 1) ? amp : (amp*ONE_OVER_SQRT2_Q15)>>15;
331
332
333
334

  for (int aa = 0; aa < config->rf_config.tx_antenna_ports.value; aa++)
  {

Guy De Souza's avatar
Guy De Souza committed
335
336
337
    // 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;
338
    l = ssb_start_symbol + 1;
Guy De Souza's avatar
Guy De Souza committed
339
340
341
    m = 0;

    for (int ssb_sc_idx = 0; ssb_sc_idx < 240; ssb_sc_idx++) {
342

Guy De Souza's avatar
Guy De Souza committed
343
344
      if ((ssb_sc_idx&3) == nushift) {  //skip DMRS
        k++;
Guy De Souza's avatar
Guy De Souza committed
345
        continue;
Guy De Souza's avatar
Guy De Souza committed
346
347
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
348
349
350
#ifdef DEBUG_PBCH
  printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
#endif
Guy De Souza's avatar
Guy De Souza committed
351
352
353
        ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_pbch_e[m<<1]) >> 15;
        ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_pbch_e[(m<<1) + 1]) >> 15;
        k++;
Guy De Souza's avatar
Guy De Souza committed
354
        m++;
Guy De Souza's avatar
Guy De Souza committed
355
      }
356
357
358
359
360

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

Guy De Souza's avatar
Guy De Souza committed
361
362
      ///symbol 2  [0:47 ; 192:239] -- 72 mod symbols
    k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
363
    l++;
Guy De Souza's avatar
Guy De Souza committed
364
365
366
    m=180;

    for (int ssb_sc_idx = 0; ssb_sc_idx < 48; ssb_sc_idx++) {
367

Guy De Souza's avatar
Guy De Souza committed
368
369
370
371
372
      if ((ssb_sc_idx&3) == nushift) {
        k++;
        continue;
      }
      else {
373
#ifdef DEBUG_PBCH
Guy De Souza's avatar
Guy De Souza committed
374
  printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
375
#endif
Guy De Souza's avatar
Guy De Souza committed
376
377
378
379
380
381
382
383
384
385
        ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_pbch_e[m<<1]) >> 15;
        ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_pbch_e[(m<<1) + 1]) >> 15;
        k++;
        m++;
      }

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

Guy De Souza's avatar
Guy De Souza committed
386
387
388
389
    k += 144;
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;

Guy De Souza's avatar
Guy De Souza committed
390
391
392
393
    m=216;

    for (int ssb_sc_idx = 192; ssb_sc_idx < 240; ssb_sc_idx++) {

Guy De Souza's avatar
Guy De Souza committed
394
      if ((ssb_sc_idx&3) == nushift) {
Guy De Souza's avatar
Guy De Souza committed
395
396
        k++;
        continue;
Guy De Souza's avatar
Guy De Souza committed
397
398
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
399
400
401
#ifdef DEBUG_PBCH
  printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
#endif
Guy De Souza's avatar
Guy De Souza committed
402
403
        ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_pbch_e[m<<1]) >> 15;
        ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_pbch_e[(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
404
405
        k++;
        m++;
Guy De Souza's avatar
Guy De Souza committed
406
      }
407
408
409
410
411

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

Guy De Souza's avatar
Guy De Souza committed
412
413
      ///symbol 3  [0:239] -- 180 mod symbols
    k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
414
    l++;
Guy De Souza's avatar
Guy De Souza committed
415
416
417
    m=252;

    for (int ssb_sc_idx = 0; ssb_sc_idx < 240; ssb_sc_idx++) {
418

Guy De Souza's avatar
Guy De Souza committed
419
420
      if ((ssb_sc_idx&3) == nushift) {
        k++;
Guy De Souza's avatar
Guy De Souza committed
421
        continue;
Guy De Souza's avatar
Guy De Souza committed
422
423
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
424
425
426
#ifdef DEBUG_PBCH
  printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
#endif
Guy De Souza's avatar
Guy De Souza committed
427
428
429
        ((int16_t*)txdataF[aa])[(l*frame_parms->ofdm_symbol_size + k)<<1] = (a * mod_pbch_e[m<<1]) >> 15;
        ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_pbch_e[(m<<1) + 1]) >> 15;
        k++;
Guy De Souza's avatar
Guy De Souza committed
430
        m++;
Guy De Souza's avatar
Guy De Souza committed
431
      }
432
433
434
435
436
437
438
439
440

      if (k >= frame_parms->ofdm_symbol_size)
        k-=frame_parms->ofdm_symbol_size;
    }

  }


#ifdef DEBUG_PBCH
Guy De Souza's avatar
Guy De Souza committed
441
  write_output("txdataF_pbch.m", "txdataF_pbch", txdataF[0], frame_parms->samples_per_frame_wCP>>1, 1, 1);
442
443
#endif

Guy De Souza's avatar
Guy De Souza committed
444

Guy De Souza's avatar
Guy De Souza committed
445
  return 0;
Guy De Souza's avatar
Guy De Souza committed
446
}