From 540b472eeaf89e39ca561528071ea37dde4b5a7c Mon Sep 17 00:00:00 2001
From: Sakthivel Velumani <velumani@eurecom.fr>
Date: Thu, 8 Oct 2020 12:08:06 +0200
Subject: [PATCH] modified dlsim to accept external signal

---
 openair1/SIMULATION/NR_PHY/dlsim.c            | 203 ++++++++++--------
 .../LAYER2/NR_MAC_gNB/gNB_scheduler_phytest.c |   8 +-
 .../NR_MAC_gNB/gNB_scheduler_primitives.c     |   1 +
 3 files changed, 116 insertions(+), 96 deletions(-)

diff --git a/openair1/SIMULATION/NR_PHY/dlsim.c b/openair1/SIMULATION/NR_PHY/dlsim.c
index 79b5884a13..4b7bf5f882 100644
--- a/openair1/SIMULATION/NR_PHY/dlsim.c
+++ b/openair1/SIMULATION/NR_PHY/dlsim.c
@@ -209,6 +209,7 @@ int main(int argc, char **argv)
   int pusch_tgt_snrx10 = 200;
   int pucch_tgt_snrx10 = 200;
   int loglvl=OAILOG_INFO;
+  int file_offset = 0;
 
   //float target_error_rate = 0.01;
   int css_flag=0;
@@ -226,7 +227,7 @@ int main(int argc, char **argv)
 
   FILE *scg_fd=NULL;
   
-  while ((c = getopt (argc, argv, "f:hA:pf:g:i:j:n:s:S:t:x:y:z:M:N:F:GR:dPIL:Ea:b:e:m:w")) != -1) {
+  while ((c = getopt (argc, argv, "f:hA:pf:g:i:j:n:s:S:t:x:y:z:M:N:F:G:R:dPIL:Ea:b:e:m:w")) != -1) {
     switch (c) {
     case 'f':
       scg_fd = fopen(optarg,"r");
@@ -362,9 +363,14 @@ int main(int argc, char **argv)
         printf("Problem with filename %s\n",optarg);
         exit(-1);
       }
+      num_rounds = 1;
 
       break;
 
+    case 'G':
+      file_offset = atoi(optarg);
+      break;
+
     case 'P':
       print_perf=1;
       opp_enabled=1;
@@ -677,7 +683,6 @@ int main(int argc, char **argv)
   estimated_output_bit = (unsigned char *) malloc16(sizeof(unsigned char) * 16 * 68 * 384);
   
   // generate signal
-  AssertFatal(input_fd==NULL,"Not ready for input signal file\n");
   gNB->pbch_configured = 1;
   gNB->ssb_pdu.ssb_pdu_rel15.bchPayload=0x001234;
   
@@ -714,6 +719,8 @@ int main(int argc, char **argv)
   NR_UE_info_t *UE_info = &RC.nrmac[0]->UE_info;
   //NR_COMMON_channels_t *cc = RC.nrmac[0]->common_channels;
   snrRun = 0;
+  int slot_offset = 0;
+  int slot_length = 0;
 
   for (SNR = snr0; SNR < snr1; SNR += .2) {
 
@@ -793,103 +800,111 @@ int main(int argc, char **argv)
         Sched_INFO.TX_req    = &gNB_mac->TX_req[0];
         nr_schedule_response(&Sched_INFO);
         
-        if (run_initial_sync)
-          nr_common_signal_procedures(gNB,frame,slot);
-        else
-          phy_procedures_gNB_TX(gNB,frame,slot,0);
-            
-        int txdataF_offset = (slot%2) * frame_parms->samples_per_slot_wCP;
-        
-        if (n_trials==1) {
-          LOG_M("txsigF0.m","txsF0", &gNB->common_vars.txdataF[0][txdataF_offset],frame_parms->samples_per_slot_wCP,1,1);
-          if (gNB->frame_parms.nb_antennas_tx>1)
-          LOG_M("txsigF1.m","txsF1", &gNB->common_vars.txdataF[1][txdataF_offset],frame_parms->samples_per_slot_wCP,1,1);
-        }
-        int tx_offset = frame_parms->get_samples_slot_timestamp(slot,frame_parms,0);
-        if (n_trials==1) printf("tx_offset %d, txdataF_offset %d \n", tx_offset,txdataF_offset);
-        
-        //TODO: loop over slots
-        for (aa=0; aa<gNB->frame_parms.nb_antennas_tx; aa++) {
-    
-          if (cyclic_prefix_type == 1) {
-            PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
-                         &txdata[aa][tx_offset],
-                         frame_parms->ofdm_symbol_size,
-                         12,
-                         frame_parms->nb_prefix_samples,
-                         CYCLIC_PREFIX);
-          } else {/*
-            nr_normal_prefix_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
-                                 &txdata[aa][tx_offset],
-                                 14,
-                                 frame_parms);
-		  */
-	    PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
-			 (int*)&txdata[aa][tx_offset],
-			 frame_parms->ofdm_symbol_size,
-			 1,
-			 frame_parms->nb_prefix_samples0,
-			 CYCLIC_PREFIX);
-	    	    
-	    apply_nr_rotation(frame_parms,
-			      (int16_t*)&txdata[aa][tx_offset],
-			      slot,
-			      0,
-			      1,
-			      frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples0);
-	    PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset+frame_parms->ofdm_symbol_size],
-			 (int*)&txdata[aa][tx_offset+frame_parms->nb_prefix_samples0+frame_parms->ofdm_symbol_size],
-			 frame_parms->ofdm_symbol_size,
-			 13,
-			 frame_parms->nb_prefix_samples,
-			 CYCLIC_PREFIX);
-	    apply_nr_rotation(frame_parms,
-			      (int16_t*)&txdata[aa][tx_offset+frame_parms->nb_prefix_samples0+frame_parms->ofdm_symbol_size],
-			      slot,
-			      1,
-			      13,
-			      frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
-	    
+        if (input_fd==NULL) {
+          if (run_initial_sync)
+            nr_common_signal_procedures(gNB,frame,slot);
+          else
+            phy_procedures_gNB_TX(gNB,frame,slot,0);
+              
+          int txdataF_offset = (slot%2) * frame_parms->samples_per_slot_wCP;
+          
+          if (n_trials==1) {
+            LOG_M("txsigF0.m","txsF0", &gNB->common_vars.txdataF[0][txdataF_offset],frame_parms->samples_per_slot_wCP,1,1);
+            if (gNB->frame_parms.nb_antennas_tx>1)
+            LOG_M("txsigF1.m","txsF1", &gNB->common_vars.txdataF[1][txdataF_offset],frame_parms->samples_per_slot_wCP,1,1);
           }
-        }
-       
-        if (n_trials==1) {
-          LOG_M("txsig0.m","txs0", &txdata[0][tx_offset],frame_parms->get_samples_slot_timestamp(slot,frame_parms,0),1,1);
-          if (gNB->frame_parms.nb_antennas_tx>1)
-            LOG_M("txsig1.m","txs1", &txdata[1][tx_offset],frame_parms->get_samples_slot_timestamp(slot,frame_parms,0),1,1);
-        }
-        if (output_fd) {
-          printf("writing txdata to binary file\n");
-          fwrite(txdata[0],sizeof(int32_t),frame_length_complex_samples,output_fd);
-        }
-
-        int txlev = signal_energy(&txdata[0][frame_parms->get_samples_slot_timestamp(slot,frame_parms,0)+5*frame_parms->ofdm_symbol_size + 4*frame_parms->nb_prefix_samples + frame_parms->nb_prefix_samples0], frame_parms->ofdm_symbol_size + frame_parms->nb_prefix_samples);
-        
-        //  if (n_trials==1) printf("txlev %d (%f)\n",txlev,10*log10((double)txlev));
+          int tx_offset = frame_parms->get_samples_slot_timestamp(slot,frame_parms,0);
+          if (n_trials==1) printf("tx_offset %d, txdataF_offset %d \n", tx_offset,txdataF_offset);
+          
+          //TODO: loop over slots
+          for (aa=0; aa<gNB->frame_parms.nb_antennas_tx; aa++) {
+      
+            if (cyclic_prefix_type == 1) {
+              PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
+                           &txdata[aa][tx_offset],
+                           frame_parms->ofdm_symbol_size,
+                           12,
+                           frame_parms->nb_prefix_samples,
+                           CYCLIC_PREFIX);
+            } else {/*
+              nr_normal_prefix_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
+                                   &txdata[aa][tx_offset],
+                                   14,
+                                   frame_parms);
+        */
+        PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset],
+         (int*)&txdata[aa][tx_offset],
+         frame_parms->ofdm_symbol_size,
+         1,
+         frame_parms->nb_prefix_samples0,
+         CYCLIC_PREFIX);
+              
+        apply_nr_rotation(frame_parms,
+              (int16_t*)&txdata[aa][tx_offset],
+              slot,
+              0,
+              1,
+              frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples0);
+        PHY_ofdm_mod(&gNB->common_vars.txdataF[aa][txdataF_offset+frame_parms->ofdm_symbol_size],
+         (int*)&txdata[aa][tx_offset+frame_parms->nb_prefix_samples0+frame_parms->ofdm_symbol_size],
+         frame_parms->ofdm_symbol_size,
+         13,
+         frame_parms->nb_prefix_samples,
+         CYCLIC_PREFIX);
+        apply_nr_rotation(frame_parms,
+              (int16_t*)&txdata[aa][tx_offset+frame_parms->nb_prefix_samples0+frame_parms->ofdm_symbol_size],
+              slot,
+              1,
+              13,
+              frame_parms->ofdm_symbol_size+frame_parms->nb_prefix_samples);
         
-        for (i=(frame_parms->get_samples_slot_timestamp(slot,frame_parms,0)); 
-             i<(frame_parms->get_samples_slot_timestamp(slot+1,frame_parms,0)); 
-             i++) {
-    
-          for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
-            r_re[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)]);
-            r_im[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)+1]);
+            }
+          }
+         
+          if (n_trials==1) {
+            LOG_M("txsig0.m","txs0", &txdata[0][tx_offset],frame_parms->get_samples_slot_timestamp(slot,frame_parms,0),1,1);
+            if (gNB->frame_parms.nb_antennas_tx>1)
+              LOG_M("txsig1.m","txs1", &txdata[1][tx_offset],frame_parms->get_samples_slot_timestamp(slot,frame_parms,0),1,1);
+          }
+          if (output_fd) {
+            printf("writing txdata to binary file\n");
+            fwrite(txdata[0],sizeof(int32_t),frame_length_complex_samples,output_fd);
           }
-        }
-        
-        //AWGN
-        sigma2_dB = 10 * log10((double)txlev * ((double)UE->frame_parms.ofdm_symbol_size/(12*rel15->rbSize))) - SNR;
-        sigma2    = pow(10, sigma2_dB/10);
-        if (n_trials==1) printf("sigma2 %f (%f dB), txlev %f (factor %f)\n",sigma2,sigma2_dB,10*log10((double)txlev),(double)(double)UE->frame_parms.ofdm_symbol_size/(12*rel15->rbSize));
-        
-        for (i=frame_parms->get_samples_slot_timestamp(slot,frame_parms,0); 
-             i<frame_parms->get_samples_slot_timestamp(slot+1,frame_parms,0);
-             i++) {
 
-          for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
-            ((short*) UE->common_vars.rxdata[aa])[2*i]   = (short) ((r_re[aa][i] + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
-            ((short*) UE->common_vars.rxdata[aa])[2*i+1] = (short) ((r_im[aa][i] + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
+          int txlev = signal_energy(&txdata[0][frame_parms->get_samples_slot_timestamp(slot,frame_parms,0)+5*frame_parms->ofdm_symbol_size + 4*frame_parms->nb_prefix_samples + frame_parms->nb_prefix_samples0], frame_parms->ofdm_symbol_size + frame_parms->nb_prefix_samples);
+          
+          //  if (n_trials==1) printf("txlev %d (%f)\n",txlev,10*log10((double)txlev));
+          
+          for (i=(frame_parms->get_samples_slot_timestamp(slot,frame_parms,0)); 
+               i<(frame_parms->get_samples_slot_timestamp(slot+1,frame_parms,0)); 
+               i++) {
+      
+            for (aa=0; aa<frame_parms->nb_antennas_tx; aa++) {
+              r_re[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)]);
+              r_im[aa][i] = ((double)(((short *)txdata[aa]))[(i<<1)+1]);
+            }
+          }
+          
+          //AWGN
+          sigma2_dB = 10 * log10((double)txlev * ((double)UE->frame_parms.ofdm_symbol_size/(12*rel15->rbSize))) - SNR;
+          sigma2    = pow(10, sigma2_dB/10);
+          if (n_trials==1) printf("sigma2 %f (%f dB), txlev %f (factor %f)\n",sigma2,sigma2_dB,10*log10((double)txlev),(double)(double)UE->frame_parms.ofdm_symbol_size/(12*rel15->rbSize));
+          
+          for (i=frame_parms->get_samples_slot_timestamp(slot,frame_parms,0); 
+               i<frame_parms->get_samples_slot_timestamp(slot+1,frame_parms,0);
+               i++) {
+
+            for (aa=0; aa<frame_parms->nb_antennas_rx; aa++) {
+              ((short*) UE->common_vars.rxdata[aa])[2*i]   = (short) ((r_re[aa][i] + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
+              ((short*) UE->common_vars.rxdata[aa])[2*i+1] = (short) ((r_im[aa][i] + sqrt(sigma2/2)*gaussdouble(0.0,1.0)));
+            }
           }
+        } else {
+          fseek(input_fd,file_offset*sizeof(int16_t)*2,SEEK_SET);
+          slot_offset = get_samples_slot_timestamp(slot,frame_parms,0);
+          slot_length = get_samples_per_slot(slot,frame_parms);
+          fread((void*)&UE->common_vars.rxdata[0][slot_offset],
+                sizeof(int16_t),slot_length<<1,input_fd);
         }
         
         nr_ue_dcireq(&dcireq); //to be replaced with function pointer later
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_phytest.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_phytest.c
index 812b9f317c..9e57ec1a06 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_phytest.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_phytest.c
@@ -382,7 +382,7 @@ int configure_fapi_dl_pdu_phytest(int Mod_idP,
   dci_pdu_rel15[0].antenna_ports.val = 0;  // nb of cdm groups w/o data 1 and dmrs port 0
   // dmrs sequence initialization
   dci_pdu_rel15[0].dmrs_sequence_initialization.val = pdsch_pdu_rel15->SCID;
-  LOG_D(MAC, "[gNB scheduler phytest] DCI type 1 payload: freq_alloc %d (%d,%d,%d), time_alloc %d, vrb to prb %d, mcs %d tb_scaling %d ndi %d rv %d\n",
+  LOG_D(MAC, "[gNB scheduler phytest] DCI type 1 payload: freq_alloc %d (%d,%d,%d), time_alloc %d, vrb to prb %d, mcs %d tb_scaling %d ndi %d rv %d dai %d tpc %d pucch_res_indi %d harq_timing_indi %d\n",
 	dci_pdu_rel15[0].frequency_domain_assignment.val,
 	pdsch_pdu_rel15->rbStart, 
 	pdsch_pdu_rel15->rbSize,	
@@ -392,7 +392,11 @@ int configure_fapi_dl_pdu_phytest(int Mod_idP,
 	dci_pdu_rel15[0].mcs,
 	dci_pdu_rel15[0].tb_scaling,
 	dci_pdu_rel15[0].ndi, 
-	dci_pdu_rel15[0].rv);
+	dci_pdu_rel15[0].rv,
+  dci_pdu_rel15[0].dai[0].val,
+  dci_pdu_rel15[0].tpc,
+  dci_pdu_rel15[0].pucch_resource_indicator,
+  dci_pdu_rel15[0].pdsch_to_harq_feedback_timing_indicator.val);
 
   NR_SearchSpace_t *ss;
   int target_ss = NR_SearchSpace__searchSpaceType_PR_ue_Specific;
diff --git a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
index 54b403f895..ecec7ea0ce 100644
--- a/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
+++ b/openair2/LAYER2/NR_MAC_gNB/gNB_scheduler_primitives.c
@@ -1002,6 +1002,7 @@ void fill_dci_pdu_rel15(NR_ServingCellConfigCommon_t *scc,
                         int bwp_id) {
 
   uint8_t fsize=0, pos=0;
+  LOG_D(MAC,"Number of DL DCIs %d\n",pdcch_pdu_rel15->numDlDci);
 
   for (int d=0;d<pdcch_pdu_rel15->numDlDci;d++) {
 
-- 
2.26.2