nr_pbch.c 14.9 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
Guy De Souza's avatar
Guy De Souza committed
40
//#define DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
41

Guy De Souza's avatar
Guy De Souza committed
42
extern short nr_mod_table[NR_MOD_TABLE_SIZE_SHORT];
Guy De Souza's avatar
Guy De Souza committed
43
44
45
46
47

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
48
                          nfapi_nr_config_request_t* config,
Guy De Souza's avatar
Guy De Souza committed
49
50
                          NR_DL_FRAME_PARMS *frame_parms)
{
51
52
  int k,l;
  int16_t a;
53
54
  int16_t mod_dmrs[NR_PBCH_DMRS_LENGTH<<1];
  uint8_t idx=0;
Guy De Souza's avatar
Guy De Souza committed
55
  uint8_t nushift = config->sch_config.physical_cell_id.value &3;
Guy De Souza's avatar
Guy De Souza committed
56

57
  LOG_I(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
58

59
  /// QPSK modulation
60
  for (int m=0; m<NR_PBCH_DMRS_LENGTH; m++) {
61
62
63
    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
64
#ifdef DEBUG_PBCH_DMRS
65
66
  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
67
68
69
70
71
72
#endif
  }

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

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

76
77
78
    // 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
79
80
    l = ssb_start_symbol + 1;

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

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

93
94
      ///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
95
96
    l++;

97
    for (int m = 60; m < 84; m++) {
Guy De Souza's avatar
Guy De Souza committed
98
#ifdef DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
99
  printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
100
#endif
Guy De Souza's avatar
Guy De Souza committed
101
      ((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
102
      ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
103
      k+=(m==71)?148:4; // Jump from 44+nu to 192+nu
Guy De Souza's avatar
Guy De Souza committed
104
105
106
107

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

109
110
      ///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
111
112
    l++;

113
    for (int m = 84; m < NR_PBCH_DMRS_LENGTH; m++) {
Guy De Souza's avatar
Guy De Souza committed
114
#ifdef DEBUG_PBCH_DMRS
Guy De Souza's avatar
Guy De Souza committed
115
  printf("m %d at k %d of l %d\n", m, k, l);
Guy De Souza's avatar
Guy De Souza committed
116
#endif
Guy De Souza's avatar
Guy De Souza committed
117
      ((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
118
      ((int16_t*)txdataF[aa])[((l*frame_parms->ofdm_symbol_size + k)<<1) + 1] = (a * mod_dmrs[(m<<1) + 1]) >> 15;
Guy De Souza's avatar
Guy De Souza committed
119
120
121
122
123
124
      k+=4;

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

Guy De Souza's avatar
Guy De Souza committed
125
  }
Guy De Souza's avatar
Guy De Souza committed
126
127


Guy De Souza's avatar
Guy De Souza committed
128
129
#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
130
#endif
Guy De Souza's avatar
Guy De Souza committed
131
132
133
  return 0;
}

Guy De Souza's avatar
Guy De Souza committed
134
135
void nr_pbch_scrambling(NR_gNB_PBCH *pbch,
                        uint32_t Nid,
Guy De Souza's avatar
Guy De Souza committed
136
                        uint8_t nushift,
Guy De Souza's avatar
Guy De Souza committed
137
                        uint16_t M,
Guy De Souza's avatar
Guy De Souza committed
138
                        uint16_t length,
Guy De Souza's avatar
Guy De Souza committed
139
                        uint8_t encoded)
140
{
Guy De Souza's avatar
Guy De Souza committed
141
  uint8_t reset, offset;
142
  uint32_t x1, x2, s=0;
Guy De Souza's avatar
Guy De Souza committed
143
144
145
  uint8_t *pbch_e = pbch->pbch_e;
  uint32_t *pbch_a_prime = (uint32_t*)pbch->pbch_a_prime;
  uint32_t *pbch_a_interleaved = (uint32_t*)pbch->pbch_a_interleaved;
Guy De Souza's avatar
Guy De Souza committed
146
  uint32_t unscrambling_mask = 0x100006D;
147
148
149
150
151

  reset = 1;
  // x1 is set in lte_gold_generic
  x2 = Nid;

152
  // The Gold sequence is shifted by nushift* M, so we skip (nushift*M /32) double words
Guy De Souza's avatar
Guy De Souza committed
153
  for (int i=0; i<(uint16_t)ceil((nushift*M)/32); i++) {
154
155
156
    s = lte_gold_generic(&x1, &x2, reset);
    reset = 0;
  }
Guy De Souza's avatar
Guy De Souza committed
157
  // Scrambling is now done with offset (nushift*M)%32
Guy De Souza's avatar
Guy De Souza committed
158
  offset = (nushift*M)&0x1f;
Guy De Souza's avatar
Guy De Souza committed
159
160
161
#ifdef DEBUG_PBCH_ENCODING
  printf("Scrambling params: nushift %d M %d length %d bitwise %d offset %d\n", nushift, M, length, bitwise, offset);
#endif
162

163
  for (int i=0; i<length; i++) {
Guy De Souza's avatar
Guy De Souza committed
164
    if (((i+offset)&0x1f)==0) {
165
166
167
      s = lte_gold_generic(&x1, &x2, reset);
      reset = 0;
    }
Guy De Souza's avatar
Guy De Souza committed
168
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
169
  printf("s: %04x\t", s);
Guy De Souza's avatar
Guy De Souza committed
170
#endif
Guy De Souza's avatar
Guy De Souza committed
171
    if (!encoded) {
Guy De Souza's avatar
Guy De Souza committed
172
173
174
175
      (*pbch_a_prime) ^= ((unscrambling_mask>>i)&1)? (((*pbch_a_interleaved)>>i)&1)<<i : ((((*pbch_a_interleaved)>>i)&1) ^ ((s>>((i+offset)&0x1f))&1))<<i;      
    }

    else
Guy De Souza's avatar
Guy De Souza committed
176
      pbch_e[i>>5] ^= (((s>>((i+offset)&0x1f))&1)<<(i&0x1f));
177
178
179
  }
}

180
181
// This portion of code is temporarily kept until the optimized version is validated
uint8_t nr_pbch_payload_interleaving_pattern[32] = {16, 23, 18, 17, 8, 30, 10, 6, 24, 7, 0, 5, 3, 2, 1, 4,
Guy De Souza's avatar
Guy De Souza committed
182
183
184
                                                9, 11, 12, 13, 14, 15, 19, 20, 21, 22, 25, 26, 27, 28, 29, 31};

uint8_t nr_pbch_payload_interleaver(uint8_t i) {
185
  uint8_t j_sfn=6, j_hrf=10, j_ssb=11, j_other=14;
Guy De Souza's avatar
Guy De Souza committed
186

187
188
  if (24<=i && i<=27) //Sfn bits
    return nr_pbch_payload_interleaving_pattern[j_sfn + i -24];
Guy De Souza's avatar
Guy De Souza committed
189
  else if (i==28) // Hrf bit
Guy De Souza's avatar
Guy De Souza committed
190
    return nr_pbch_payload_interleaving_pattern[j_hrf];
Guy De Souza's avatar
Guy De Souza committed
191
  else if (29<=i) // Ssb bits
Guy De Souza's avatar
Guy De Souza committed
192
193
    return nr_pbch_payload_interleaving_pattern[j_ssb + (i-29)];
  else
194
195
    return nr_pbch_payload_interleaving_pattern[(j_other + i)&0x1f];
}
Guy De Souza's avatar
Guy De Souza committed
196
197

/*This pattern takes into account the adjustments for the field specific counters j_sfn, j_hrf, j_ssb and j_other*/
198
199
//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
200
201

int nr_generate_pbch(NR_gNB_PBCH *pbch,
Guy De Souza's avatar
Guy De Souza committed
202
                     t_nrPolar_paramsPtr polar_params,
Guy De Souza's avatar
Guy De Souza committed
203
204
205
206
                     uint8_t *pbch_pdu,
                     int32_t **txdataF,
                     int16_t amp,
                     uint8_t ssb_start_symbol,
207
                     uint8_t n_hf,
Guy De Souza's avatar
Guy De Souza committed
208
209
                     uint8_t Lmax,
                     uint8_t ssb_index,
210
                     int sfn,
ChenWeiTai's avatar
ChenWeiTai committed
211
                     nfapi_nr_config_request_t* config,
Guy De Souza's avatar
Guy De Souza committed
212
213
214
                     NR_DL_FRAME_PARMS *frame_parms)
{

Guy De Souza's avatar
Guy De Souza committed
215
  int k,l,m;
216
  int16_t a;
Guy De Souza's avatar
Guy De Souza committed
217
218
219
220
  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
221
222
  uint8_t *xbyte = pbch->pbch_a;
  memset((void*) xbyte, 0, 1);
223
  uint8_t pbch_a_b[32];
Guy De Souza's avatar
Guy De Souza committed
224
225
226

  LOG_I(PHY, "PBCH generation started\n");

227
228
  ///Payload generation
    // Fix byte endian
Guy De Souza's avatar
Guy De Souza committed
229
230
231
232
233
234
  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
235
    printf("pbch_a[%d]: 0x%02x\n", i, pbch->pbch_a[i]);
Guy De Souza's avatar
Guy De Souza committed
236
#endif
237

Guy De Souza's avatar
Guy De Souza committed
238
    // Extra byte generation
239
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
240
    (*xbyte) ^= ((sfn>>i)&1)<<i; // 4 lsb of sfn
241

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

Guy De Souza's avatar
Guy De Souza committed
244
245
246
247
248
  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
249
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
250
251
  printf("Extra byte:\n");
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
252
    printf("pbch_a[%d]: 0x%02x\n", i, pbch->pbch_a[i]);
Guy De Souza's avatar
Guy De Souza committed
253
254
255
#endif

    // Payload interleaving
Guy De Souza's avatar
Guy De Souza committed
256
257
258
259
  uint32_t in=0, out=0;
  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
260
  for (int i=0; i<32; i++) {
261
    out |= ((in>>i)&1)<<(nr_pbch_payload_interleaver(i));//nr_pbch_payload_interleaving_pattern[i]
Guy De Souza's avatar
Guy De Souza committed
262
#ifdef DEBUG_PBCH_ENCODING
263
  printf("i %d in 0x%08x out 0x%08x ilv %d (in>>i)&1) %d\n", i, in, out, nr_pbch_payload_interleaver(i), (in>>i)&1);
Guy De Souza's avatar
Guy De Souza committed
264
265
#endif
  }
Guy De Souza's avatar
Guy De Souza committed
266
267
268
269

  for (int i=0; i<NR_POLAR_PBCH_PAYLOAD_BITS>>3; i++)
    pbch->pbch_a_interleaved[i] = (uint8_t)((out>>(i<<3))&0xff);

Guy De Souza's avatar
Guy De Souza committed
270
271
272
#ifdef DEBUG_PBCH_ENCODING
  printf("Interleaving:\n");
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
273
    printf("pbch_a_interleaved[%d]: 0x%02x\n", i, pbch->pbch_a_interleaved[i]);
Guy De Souza's avatar
Guy De Souza committed
274
#endif
275
276

    // Scrambling
Guy De Souza's avatar
Guy De Souza committed
277
278
  M = (Lmax == 64)? (NR_POLAR_PBCH_PAYLOAD_BITS - 6) : (NR_POLAR_PBCH_PAYLOAD_BITS - 3);
  nushift = (((sfn>>2)&1)<<1) ^ ((sfn>>1)&1);
Guy De Souza's avatar
Guy De Souza committed
279
  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
280
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
281
282
  printf("Payload scrambling:\n");
  for (int i=0; i<4; i++)
Guy De Souza's avatar
Guy De Souza committed
283
    printf("pbch_a_prime[%d]: 0x%02x\n", i, pbch->pbch_a_prime[i]);
Guy De Souza's avatar
Guy De Souza committed
284
#endif
285

Guy De Souza's avatar
Guy De Souza committed
286
/*for (int m=0;m<32;m++){
Guy De Souza's avatar
Guy De Souza committed
287
pbch_a_b[m] = ((pbch->pbch_a_prime[m>>3]>>(m&7))&1);
288
//printf("pbch_a_b[%d] %d\n", m, pbch_a_b[m] );
Guy De Souza's avatar
Guy De Souza committed
289
}*/
Guy De Souza's avatar
Guy De Souza committed
290

291
  /// CRC, coding and rate matching
Guy De Souza's avatar
Guy De Souza committed
292
  polar_encoder (pbch->pbch_a_prime, pbch->pbch_e, polar_params);
Guy De Souza's avatar
Guy De Souza committed
293
#ifdef DEBUG_PBCH_ENCODING
Guy De Souza's avatar
Guy De Souza committed
294
  printf("Channel coding:\n");
Guy De Souza's avatar
Guy De Souza committed
295
  for (int i=0; i<NR_POLAR_PBCH_E>>5; i++)
Guy De Souza's avatar
Guy De Souza committed
296
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
Guy De Souza's avatar
Guy De Souza committed
297
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
298
#endif
299

Guy De Souza's avatar
Guy De Souza committed
300
301
  /// Scrambling
  M =  NR_POLAR_PBCH_E;
Guy De Souza's avatar
Guy De Souza committed
302
  nushift = (Lmax==4)? ssb_index&3 : ssb_index&7;
Guy De Souza's avatar
Guy De Souza committed
303
  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
304
305
#ifdef DEBUG_PBCH_ENCODING
  printf("Scrambling:\n");
Guy De Souza's avatar
Guy De Souza committed
306
  for (int i=0; i<NR_POLAR_PBCH_E>>5; i++)
Guy De Souza's avatar
Guy De Souza committed
307
    printf("pbch_e[%d]: 0x%08x\t", i, pbch->pbch_e[i]);
Guy De Souza's avatar
Guy De Souza committed
308
  printf("\n");
Guy De Souza's avatar
Guy De Souza committed
309
#endif
310
311

  /// QPSK modulation
Guy De Souza's avatar
Guy De Souza committed
312
  for (int i=0; i<NR_POLAR_PBCH_E>>1; i++){
Guy De Souza's avatar
Guy De Souza committed
313
    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
314
315
    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];
316
317

#ifdef DEBUG_PBCH
Guy De Souza's avatar
Guy De Souza committed
318
  printf("i %d idx %d  mod_pbch %d %d\n", i, idx, mod_pbch_e[2*i], mod_pbch_e[2*i+1]);
319
320
321
322
#endif
  }

  /// Resource mapping
Guy De Souza's avatar
Guy De Souza committed
323
  nushift = config->sch_config.physical_cell_id.value &3;
Guy De Souza's avatar
Guy De Souza committed
324
  a = (config->rf_config.tx_antenna_ports.value == 1) ? amp : (amp*ONE_OVER_SQRT2_Q15)>>15;
325
326
327
328

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

Guy De Souza's avatar
Guy De Souza committed
329
330
331
    // 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;
332
    l = ssb_start_symbol + 1;
Guy De Souza's avatar
Guy De Souza committed
333
334
335
    m = 0;

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

Guy De Souza's avatar
Guy De Souza committed
337
338
      if ((ssb_sc_idx&3) == nushift) {  //skip DMRS
        k++;
Guy De Souza's avatar
Guy De Souza committed
339
        continue;
Guy De Souza's avatar
Guy De Souza committed
340
341
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
342
343
344
#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
345
346
347
        ((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
348
        m++;
Guy De Souza's avatar
Guy De Souza committed
349
      }
350
351
352
353
354

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

Guy De Souza's avatar
Guy De Souza committed
355
356
      ///symbol 2  [0:47 ; 192:239] -- 72 mod symbols
    k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
357
    l++;
Guy De Souza's avatar
Guy De Souza committed
358
359
360
    m=180;

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

Guy De Souza's avatar
Guy De Souza committed
362
363
364
365
366
      if ((ssb_sc_idx&3) == nushift) {
        k++;
        continue;
      }
      else {
367
#ifdef DEBUG_PBCH
Guy De Souza's avatar
Guy De Souza committed
368
  printf("m %d ssb_sc_idx %d at k %d of l %d\n", m, ssb_sc_idx, k, l);
369
#endif
Guy De Souza's avatar
Guy De Souza committed
370
371
372
373
374
375
376
377
378
379
        ((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
380
381
382
383
    k += 144;
    if (k >= frame_parms->ofdm_symbol_size)
      k-=frame_parms->ofdm_symbol_size;

Guy De Souza's avatar
Guy De Souza committed
384
385
386
387
    m=216;

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

Guy De Souza's avatar
Guy De Souza committed
388
      if ((ssb_sc_idx&3) == nushift) {
Guy De Souza's avatar
Guy De Souza committed
389
390
        k++;
        continue;
Guy De Souza's avatar
Guy De Souza committed
391
392
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
393
394
395
#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
396
397
        ((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
398
399
        k++;
        m++;
Guy De Souza's avatar
Guy De Souza committed
400
      }
401
402
403
404
405

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

Guy De Souza's avatar
Guy De Souza committed
406
407
      ///symbol 3  [0:239] -- 180 mod symbols
    k = frame_parms->first_carrier_offset + frame_parms->ssb_start_subcarrier;
408
    l++;
Guy De Souza's avatar
Guy De Souza committed
409
410
411
    m=252;

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

Guy De Souza's avatar
Guy De Souza committed
413
414
      if ((ssb_sc_idx&3) == nushift) {
        k++;
Guy De Souza's avatar
Guy De Souza committed
415
        continue;
Guy De Souza's avatar
Guy De Souza committed
416
417
      }
      else {
Guy De Souza's avatar
Guy De Souza committed
418
419
420
#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
421
422
423
        ((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
424
        m++;
Guy De Souza's avatar
Guy De Souza committed
425
      }
426
427
428
429
430
431
432
433
434

      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
435
  write_output("txdataF_pbch.m", "txdataF_pbch", txdataF[0], frame_parms->samples_per_frame_wCP>>1, 1, 1);
436
437
#endif

Guy De Souza's avatar
Guy De Souza committed
438

Guy De Souza's avatar
Guy De Souza committed
439
  return 0;
Guy De Souza's avatar
Guy De Souza committed
440
}