From 12ccca72b6ce85d0fba84ee8f6d08789694773fc Mon Sep 17 00:00:00 2001
From: francescomani <email@francescomani.it>
Date: Mon, 20 Feb 2023 11:09:32 +0100
Subject: [PATCH] further improvements

---
 executables/nr-gnb.c                          | 14 +-----
 openair1/PHY/INIT/nr_init.c                   | 10 ++++
 .../PHY/NR_ESTIMATION/nr_measurements_gNB.c   | 15 +++---
 openair1/PHY/NR_ESTIMATION/nr_ul_estimation.h |  2 +-
 openair1/PHY/NR_TRANSPORT/nr_dlsch.c          |  6 ---
 openair1/PHY/NR_TRANSPORT/nr_dlsch.h          |  2 -
 .../PHY/NR_TRANSPORT/nr_transport_proto.h     |  3 ++
 openair1/PHY/NR_TRANSPORT/nr_ulsch.c          | 47 +++++++++++--------
 openair1/PHY/NR_TRANSPORT/nr_ulsch.h          |  3 +-
 openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c | 25 +++++-----
 .../PHY/NR_TRANSPORT/nr_ulsch_demodulation.c  |  2 +-
 openair1/PHY/defs_gNB.h                       |  6 +--
 openair1/SCHED_NR/phy_procedures_nr_gNB.c     | 16 +++----
 openair1/SIMULATION/NR_PHY/dlsim.c            |  2 -
 openair1/SIMULATION/NR_PHY/ulsim.c            |  8 ++--
 15 files changed, 78 insertions(+), 83 deletions(-)

diff --git a/executables/nr-gnb.c b/executables/nr-gnb.c
index 1220fae9240..6df583d7047 100644
--- a/executables/nr-gnb.c
+++ b/executables/nr-gnb.c
@@ -175,18 +175,8 @@ void rx_func(void *param)
 
   T(T_GNB_PHY_DL_TICK, T_INT(gNB->Mod_id), T_INT(frame_tx), T_INT(slot_tx));
 
-  // disactivate PHY stats if UE is inactive for a given number of frames
-  for (int i = 0; i < MAX_MOBILES_PER_GNB; i++) {
-    NR_gNB_PHY_STATS_t *stats = &gNB->phy_stats[i];
-    if(stats->active && (((frame_tx - stats->frame + 1024) % 1024) > NUMBER_FRAMES_PHY_UE_INACTIVE))
-      stats->active = false;
-  }
-  // disactivate ULSCH structure if it is inactive for a given number of frames
-  for (int i = 0; i < gNB->max_nb_pusch; i++) {
-    NR_gNB_ULSCH_t *ulsch = gNB->ulsch[i];
-    if (ulsch->active && (((frame_tx - ulsch->harq_process->frame + 1024) % 1024) > NUMBER_FRAMES_PHY_UE_INACTIVE))
-      ulsch->active = false;
-  }
+  reset_active_stats(gNB, frame_tx);
+  reset_active_ulsch(gNB, frame_tx);
 
   // RX processing
   int rx_slot_type = nr_slot_select(cfg,frame_rx,slot_rx);
diff --git a/openair1/PHY/INIT/nr_init.c b/openair1/PHY/INIT/nr_init.c
index 465618a9f45..f3cbc030658 100644
--- a/openair1/PHY/INIT/nr_init.c
+++ b/openair1/PHY/INIT/nr_init.c
@@ -99,6 +99,16 @@ NR_gNB_PHY_STATS_t *get_phy_stats(PHY_VARS_gNB *gNB, uint16_t rnti)
    return(stats);
 }
 
+void reset_active_stats(PHY_VARS_gNB *gNB, int frame)
+{
+  // disactivate PHY stats if UE is inactive for a given number of frames
+  for (int i = 0; i < MAX_MOBILES_PER_GNB; i++) {
+    NR_gNB_PHY_STATS_t *stats = &gNB->phy_stats[i];
+    if(stats->active && (((frame - stats->frame + 1024) % 1024) > NUMBER_FRAMES_PHY_UE_INACTIVE))
+      stats->active = false;
+  }
+}
+
 int init_codebook_gNB(PHY_VARS_gNB *gNB) {
 
   if(gNB->frame_parms.nb_antennas_tx>1){
diff --git a/openair1/PHY/NR_ESTIMATION/nr_measurements_gNB.c b/openair1/PHY/NR_ESTIMATION/nr_measurements_gNB.c
index ff90ca54fff..efd0493bb5b 100644
--- a/openair1/PHY/NR_ESTIMATION/nr_measurements_gNB.c
+++ b/openair1/PHY/NR_ESTIMATION/nr_measurements_gNB.c
@@ -208,7 +208,7 @@ void gNB_I0_measurements(PHY_VARS_gNB *gNB,int slot, int first_symb,int num_symb
 //
 // Todo:
 // - averaging IIR filter for RX power and noise
-void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq_pid, unsigned char symbol, uint8_t nrOfLayers)
+void nr_gnb_measurements(PHY_VARS_gNB *gNB, NR_gNB_ULSCH_t *ulsch, NR_gNB_PUSCH *pusch_vars, unsigned char symbol, uint8_t nrOfLayers)
 {
 
   int rx_power_tot = 0;
@@ -220,8 +220,8 @@ void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq
   PHY_MEASUREMENTS_gNB *meas = &gNB->measurements;
   NR_DL_FRAME_PARMS *fp = &gNB->frame_parms;
   int ch_offset = fp->ofdm_symbol_size * symbol;
-  int N_RB_UL = gNB->ulsch[ulsch_id]->harq_process->ulsch_pdu.rb_size;
-  ulsch_measurements_gNB *ulsch_measurements = &gNB->ulsch[ulsch_id]->ulsch_measurements;
+  int N_RB_UL = ulsch->harq_process->ulsch_pdu.rb_size;
+  ulsch_measurements_gNB *ulsch_measurements = &ulsch->ulsch_measurements;
 
   int rx_power[fp->nb_antennas_rx];
   for (int aarx = 0; aarx < fp->nb_antennas_rx; aarx++){
@@ -230,7 +230,7 @@ void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq
 
     for (int aatx = 0; aatx < nrOfLayers; aatx++){
 
-      ulsch_measurements->rx_spatial_power[aatx][aarx] = (signal_energy_nodc(&gNB->pusch_vars[ulsch_id]->ul_ch_estimates[aatx*fp->nb_antennas_rx+aarx][ch_offset], N_RB_UL * NR_NB_SC_PER_RB));
+      ulsch_measurements->rx_spatial_power[aatx][aarx] = (signal_energy_nodc(&pusch_vars->ul_ch_estimates[aatx*fp->nb_antennas_rx+aarx][ch_offset], N_RB_UL * NR_NB_SC_PER_RB));
 
       if (ulsch_measurements->rx_spatial_power[aatx][aarx] < 0) {
         ulsch_measurements->rx_spatial_power[aatx][aarx] = 0;
@@ -240,7 +240,7 @@ void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq
       rx_power[aarx] += ulsch_measurements->rx_spatial_power[aatx][aarx];
 
     }
-    LOG_D(PHY, "[ULSCH ID %d] RX power in antenna %d = %d\n", ulsch_id, aarx, rx_power[aarx]);
+    LOG_D(PHY, "[RNTI %04x] RX power in antenna %d = %d\n", ulsch->rnti, aarx, rx_power[aarx]);
 
     rx_power_tot += rx_power[aarx];
 
@@ -252,13 +252,12 @@ void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq
   ulsch_measurements->wideband_cqi_tot = dB_fixed2(rx_power_tot, meas->n0_power_tot);
   ulsch_measurements->rx_rssi_dBm = rx_power_avg_dB + 30 - 10 * log10(pow(2, 30)) - (rx_gain - rx_gain_offset) - dB_fixed(fp->ofdm_symbol_size);
 
-  LOG_D(PHY, "[ULSCH %d] RSSI %d dBm/RE, RSSI (digital) %d dB (N_RB_UL %d), WBand CQI tot %d dB, N0 Power tot %d, RX Power tot %d\n",
-    ulsch_id,
+  LOG_D(PHY, "[RNTI %04x] RSSI %d dBm/RE, RSSI (digital) %d dB (N_RB_UL %d), WBand CQI tot %d dB, N0 Power tot %d, RX Power tot %d\n",
+    ulsch->rnti,
     ulsch_measurements->rx_rssi_dBm,
     rx_power_avg_dB,
     N_RB_UL,
     ulsch_measurements->wideband_cqi_tot,
     meas->n0_power_tot,
     rx_power_tot);
-
 }
diff --git a/openair1/PHY/NR_ESTIMATION/nr_ul_estimation.h b/openair1/PHY/NR_ESTIMATION/nr_ul_estimation.h
index defd62fd8b2..ce369ef9aca 100644
--- a/openair1/PHY/NR_ESTIMATION/nr_ul_estimation.h
+++ b/openair1/PHY/NR_ESTIMATION/nr_ul_estimation.h
@@ -53,7 +53,7 @@ void dump_nr_I0_stats(FILE *fd,PHY_VARS_gNB *gNB);
 
 void gNB_I0_measurements(PHY_VARS_gNB *gNB,int slot,int first_symb,int num_symb);
 
-void nr_gnb_measurements(PHY_VARS_gNB *gNB, uint8_t ulsch_id, unsigned char harq_pid, unsigned char symbol, uint8_t nrOfLayers);
+void nr_gnb_measurements(PHY_VARS_gNB *gNB, NR_gNB_ULSCH_t *ulsch, NR_gNB_PUSCH *pusch_vars, unsigned char symbol, uint8_t nrOfLayers);
 
 void nr_est_timing_advance_pusch(const NR_DL_FRAME_PARMS *frame_parms, const int32_t *ul_ch_estimates_time, NR_ULSCH_delay_t *delay);
 
diff --git a/openair1/PHY/NR_TRANSPORT/nr_dlsch.c b/openair1/PHY/NR_TRANSPORT/nr_dlsch.c
index 17882077a81..983d51d66ee 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_dlsch.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_dlsch.c
@@ -580,9 +580,3 @@ void dump_pdsch_stats(FILE *fd,PHY_VARS_gNB *gNB) {
     }
   }
 }
-
-void clear_pdsch_stats(PHY_VARS_gNB *gNB)
-{
-  for (int i = 0; i < MAX_MOBILES_PER_GNB;i++)
-    memset((void*)&gNB->phy_stats[i].dlsch_stats,0,sizeof(gNB->phy_stats[i].dlsch_stats));
-}
diff --git a/openair1/PHY/NR_TRANSPORT/nr_dlsch.h b/openair1/PHY/NR_TRANSPORT/nr_dlsch.h
index 6e2210aab05..8ee2e1171fb 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_dlsch.h
+++ b/openair1/PHY/NR_TRANSPORT/nr_dlsch.h
@@ -75,6 +75,4 @@ void nr_emulate_dlsch_payload(uint8_t* payload, uint16_t size);
 
 void dump_pdsch_stats(FILE *fd,PHY_VARS_gNB *gNB);
 
-void clear_pdsch_stats(PHY_VARS_gNB *gNB);
-
 #endif
diff --git a/openair1/PHY/NR_TRANSPORT/nr_transport_proto.h b/openair1/PHY/NR_TRANSPORT/nr_transport_proto.h
index b0a575a0ac4..b4580281730 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_transport_proto.h
+++ b/openair1/PHY/NR_TRANSPORT/nr_transport_proto.h
@@ -270,6 +270,9 @@ void nr_ulsch_compute_llr(int32_t *rxdataF_comp,
                           uint8_t  symbol,
                           uint8_t  mod_order);
 
+void reset_active_stats(PHY_VARS_gNB *gNB, int frame);
+void reset_active_ulsch(PHY_VARS_gNB *gNB, int frame);
+
 void nr_fill_ulsch(PHY_VARS_gNB *gNB,
                    int frame,
                    int slot,
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
index 8aca638bfad..5d02718db9f 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch.c
@@ -34,14 +34,15 @@
 #include "PHY/NR_TRANSPORT/nr_transport_common_proto.h"
 #include "PHY/NR_TRANSPORT/nr_ulsch.h"
 
-int16_t find_nr_ulsch(PHY_VARS_gNB *gNB, uint16_t rnti, int pid, int frame)
+NR_gNB_ULSCH_t *find_nr_ulsch(PHY_VARS_gNB *gNB, uint16_t rnti, int pid)
 {
 
   int16_t first_free_index = -1;
   AssertFatal(gNB != NULL,"gNB is null\n");
+  NR_gNB_ULSCH_t  *ulsch = NULL;
 
   for (int i = 0; i < gNB->max_nb_pusch; i++) {
-    NR_gNB_ULSCH_t *ulsch = gNB->ulsch[i];
+    ulsch = gNB->ulsch[i];
     AssertFatal(ulsch != NULL, "gNB->ulsch[%d] is null\n", i);
     if(!ulsch->active) {
       if (first_free_index == -1)
@@ -50,10 +51,13 @@ int16_t find_nr_ulsch(PHY_VARS_gNB *gNB, uint16_t rnti, int pid, int frame)
     else {
       // if there is already an active ULSCH for this RNTI and HARQ_PID
       if ((ulsch->harq_pid == pid) && (ulsch->rnti == rnti))
-        return i;
+        return ulsch;
     }
   }
-  return first_free_index;
+  if (first_free_index != -1)
+    ulsch = gNB->ulsch[first_free_index];
+
+  return ulsch;
 }
 
 void nr_fill_ulsch(PHY_VARS_gNB *gNB,
@@ -63,23 +67,20 @@ void nr_fill_ulsch(PHY_VARS_gNB *gNB,
 {
  
   int harq_pid = ulsch_pdu->pusch_data.harq_process_id;
-  int ulsch_id = find_nr_ulsch(gNB, ulsch_pdu->rnti, harq_pid, frame);
-  AssertFatal((ulsch_id >= 0) && (ulsch_id < gNB->max_nb_pusch),
-              "illegal or no ulsch_id found!!! rnti %04x ulsch_id %d\n",ulsch_pdu->rnti,ulsch_id);
-
-  NR_gNB_ULSCH_t  *ulsch = gNB->ulsch[ulsch_id];
+  NR_gNB_ULSCH_t *ulsch = find_nr_ulsch(gNB, ulsch_pdu->rnti, harq_pid);
+  AssertFatal(ulsch, "No ulsch_id found for rnti %04x\n", ulsch_pdu->rnti);
 
   ulsch->rnti = ulsch_pdu->rnti;
   ulsch->harq_pid = harq_pid;
   ulsch->handled = 0;
   ulsch->active = true;
+  ulsch->frame = frame;
+  ulsch->slot = slot;
 
   NR_UL_gNB_HARQ_t *harq = ulsch->harq_process;
-  harq->frame = frame;
-  harq->slot = slot;
   harq->new_rx = ulsch_pdu->pusch_data.new_data_indicator;
-  LOG_D(PHY,"%d.%d ULSCH ID %d RNTI %x HARQ PID %d new data indicator %d\n",
-        frame, slot, ulsch_id, ulsch_pdu->rnti, harq_pid, ulsch_pdu->pusch_data.new_data_indicator);
+  LOG_D(PHY,"%d.%d RNTI %x HARQ PID %d new data indicator %d\n",
+        frame, slot, ulsch_pdu->rnti, harq_pid, ulsch_pdu->pusch_data.new_data_indicator);
   if (harq->new_rx)
     harq->round = 0;
   else
@@ -87,7 +88,19 @@ void nr_fill_ulsch(PHY_VARS_gNB *gNB,
 
   memcpy(&ulsch->harq_process->ulsch_pdu, ulsch_pdu, sizeof(ulsch->harq_process->ulsch_pdu));
 
-  LOG_D(PHY,"Initializing nFAPI for ULSCH, UE %d, harq_pid %d\n", ulsch_id, harq_pid);
+  LOG_D(PHY,"Initializing nFAPI for ULSCH, harq_pid %d\n", harq_pid);
+}
+
+void reset_active_ulsch(PHY_VARS_gNB *gNB, int frame)
+{
+  // disactivate ULSCH structure after a given number of frames
+  // no activity on this structure for NUMBER_FRAMES_PHY_UE_INACTIVE
+  // assuming UE disconnected or some other error occurred
+  for (int i = 0; i < gNB->max_nb_pusch; i++) {
+    NR_gNB_ULSCH_t *ulsch = gNB->ulsch[i];
+    if (ulsch->active && (((frame - ulsch->frame + 1024) % 1024) > NUMBER_FRAMES_PHY_UE_INACTIVE))
+      ulsch->active = false;
+  }
 }
 
 void nr_ulsch_unscrambling(int16_t* llr, uint32_t size, uint32_t Nid, uint32_t n_RNTI)
@@ -159,10 +172,4 @@ void dump_pusch_stats(FILE *fd, PHY_VARS_gNB *gNB)
   }
 }
 
-void clear_pusch_stats(PHY_VARS_gNB *gNB)
-{
-  for (int i = 0; i < MAX_MOBILES_PER_GNB; i++)
-    memset((void*)&gNB->phy_stats[i].ulsch_stats,0,sizeof(gNB->phy_stats[i].ulsch_stats));
-}
-
 
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch.h b/openair1/PHY/NR_TRANSPORT/nr_ulsch.h
index d3b8599aca5..2393830881b 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch.h
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch.h
@@ -83,11 +83,10 @@ void nr_ulsch_procedures(PHY_VARS_gNB *gNB,
                          int UE_id,
                          uint8_t harq_pid);
 
-int16_t find_nr_ulsch(PHY_VARS_gNB *gNB, uint16_t rnti, int pid, int frame);
+NR_gNB_ULSCH_t *find_nr_ulsch(PHY_VARS_gNB *gNB, uint16_t rnti, int pid);
 
 void dump_pusch_stats(FILE *fd,PHY_VARS_gNB *gNB);
 
 void dump_nr_I0_stats(FILE *fd,PHY_VARS_gNB *gNB);
-void clear_pusch_stats(PHY_VARS_gNB *gNB);
 
 NR_gNB_SCH_STATS_t *get_ulsch_stats(PHY_VARS_gNB *gNB,NR_gNB_ULSCH_t *ulsch);
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
index 60902a37831..6d7f1973570 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch_decoding.c
@@ -101,13 +101,14 @@ NR_gNB_ULSCH_t *new_gNB_ulsch(uint8_t max_ldpc_iterations, uint16_t N_RB_UL)
   ulsch->harq_pid = -1;
   ulsch->active = false;
 
-  ulsch->harq_process = malloc16_clear(sizeof(ulsch->harq_process));
-  ulsch->harq_process->b = malloc16_clear(ulsch_bytes);
-  ulsch->harq_process->c = malloc16_clear(a_segments*sizeof(uint8_t *));
-  ulsch->harq_process->d = malloc16_clear(a_segments*sizeof(int16_t *));
+  NR_UL_gNB_HARQ_t *harq = malloc16_clear(sizeof(harq));
+  ulsch->harq_process = harq;
+  harq->b = malloc16_clear(ulsch_bytes * sizeof(*harq->b));
+  harq->c = malloc16_clear(a_segments * sizeof(*harq->c));
+  harq->d = malloc16_clear(a_segments * sizeof(*harq->d));
   for (int r = 0; r < a_segments; r++) {
-    ulsch->harq_process->c[r] = malloc16_clear(8448*sizeof(uint8_t));
-    ulsch->harq_process->d[r] = malloc16_clear((68*384)*sizeof(int16_t));
+    harq->c[r] = malloc16_clear(8448 * sizeof(*harq->c[r]));
+    harq->d[r] = malloc16_clear(68 * 384 * sizeof(*harq->d[r]));
   }
   return(ulsch);
 }
@@ -538,20 +539,20 @@ uint32_t nr_ulsch_decoding(PHY_VARS_gNB *phy_vars_gNB,
 
     VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_PHY_gNB_ULSCH_DECODING, 0);
 
-    if (harq_process->processedSegments == (harq_process->C)) {
-      LOG_D(PHY, "[gNB %d] ULSCH: Setting ACK for slot %d TBS %d\n", phy_vars_gNB->Mod_id, harq_process->slot, harq_process->TBS);
+    if (harq_process->processedSegments == harq_process->C) {
+      LOG_D(PHY, "[gNB %d] ULSCH: Setting ACK for slot %d TBS %d\n", phy_vars_gNB->Mod_id, ulsch->slot, harq_process->TBS);
       ulsch->active = false;
       harq_process->round = 0;
 
       LOG_D(PHY, "ULSCH received ok \n");
-      nr_fill_indication(phy_vars_gNB, harq_process->frame, harq_process->slot, ULSCH_id, harq_pid, 0, 0);
+      nr_fill_indication(phy_vars_gNB, ulsch->frame, ulsch->slot, ULSCH_id, harq_pid, 0, 0);
 
     } else {
       LOG_D(PHY,
             "[gNB %d] ULSCH: Setting NAK for SFN/SF %d/%d (pid %d, status %d, round %d, TBS %d)\n",
             phy_vars_gNB->Mod_id,
-            harq_process->frame,
-            harq_process->slot,
+            ulsch->frame,
+            ulsch->slot,
             harq_pid,
             ulsch->active,
             harq_process->round,
@@ -559,7 +560,7 @@ uint32_t nr_ulsch_decoding(PHY_VARS_gNB *phy_vars_gNB,
       ulsch->handled = 1;
       no_iteration_ldpc = ulsch->max_ldpc_iterations + 1;
       LOG_D(PHY, "ULSCH %d in error\n", ULSCH_id);
-      nr_fill_indication(phy_vars_gNB, harq_process->frame, harq_process->slot, ULSCH_id, harq_pid, 1, 0);
+      nr_fill_indication(phy_vars_gNB, ulsch->frame, ulsch->slot, ULSCH_id, harq_pid, 1, 0);
     }
     ulsch->last_iteration_cnt = no_iteration_ldpc;
   }
diff --git a/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c b/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
index c96fc64866d..370ec4a9c9e 100644
--- a/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
+++ b/openair1/PHY/NR_TRANSPORT/nr_ulsch_demodulation.c
@@ -1885,7 +1885,7 @@ void nr_rx_pusch(PHY_VARS_gNB *gNB,
                                     &max_ch);
       }
 
-      nr_gnb_measurements(gNB, ulsch_id, harq_pid, symbol, rel15_ul->nrOfLayers);
+      nr_gnb_measurements(gNB, gNB->ulsch[ulsch_id], gNB->pusch_vars[ulsch_id], symbol, rel15_ul->nrOfLayers);
 
       for (aarx = 0; aarx < frame_parms->nb_antennas_rx; aarx++) {
         if (symbol == rel15_ul->start_symbol_index) {
diff --git a/openair1/PHY/defs_gNB.h b/openair1/PHY/defs_gNB.h
index cb8990de9c0..f52fe32d7fc 100644
--- a/openair1/PHY/defs_gNB.h
+++ b/openair1/PHY/defs_gNB.h
@@ -188,10 +188,6 @@ typedef struct {
 typedef struct {
   /// Nfapi ULSCH PDU
   nfapi_nr_pusch_pdu_t ulsch_pdu;
-  /// Frame where current HARQ round was sent
-  uint32_t frame;
-  /// Slot where current HARQ round was sent
-  uint32_t slot;
   /// Index of current HARQ round for this DLSCH
   uint8_t round;
   bool new_rx;
@@ -261,6 +257,8 @@ typedef struct {
 } NR_ULSCH_delay_t;
 
 typedef struct {
+  uint32_t frame;
+  uint32_t slot;
   /// Pointers to 16 HARQ processes for the ULSCH
   NR_UL_gNB_HARQ_t *harq_process;
   /// HARQ process mask, indicates which processes are currently active
diff --git a/openair1/SCHED_NR/phy_procedures_nr_gNB.c b/openair1/SCHED_NR/phy_procedures_nr_gNB.c
index 5388bccb2b3..89fdf4918bb 100644
--- a/openair1/SCHED_NR/phy_procedures_nr_gNB.c
+++ b/openair1/SCHED_NR/phy_procedures_nr_gNB.c
@@ -244,16 +244,16 @@ void nr_postDecode(PHY_VARS_gNB *gNB, notifiedFIFO_elt_t *req)
   if (rdata->nbSegments == ulsch_harq->processedSegments) {
     if (decodeSuccess && !gNB->pusch_vars[rdata->ulsch_id]->DTX) {
       LOG_D(PHY,"[gNB %d] ULSCH: Setting ACK for SFN/SF %d.%d (rnti %x, pid %d, ndi %d, status %d, round %d, TBS %d, Max interation (all seg) %d)\n",
-            gNB->Mod_id, ulsch_harq->frame, ulsch_harq->slot, ulsch->rnti, rdata->harq_pid, pusch_pdu->pusch_data.new_data_indicator,
+            gNB->Mod_id, ulsch->frame, ulsch->slot, ulsch->rnti, rdata->harq_pid, pusch_pdu->pusch_data.new_data_indicator,
             ulsch->active, ulsch_harq->round, ulsch_harq->TBS, rdata->decodeIterations);
       ulsch->active = false;
       ulsch_harq->round = 0;
       LOG_D(PHY, "ULSCH received ok \n");
-      nr_fill_indication(gNB, ulsch_harq->frame, ulsch_harq->slot, rdata->ulsch_id, rdata->harq_pid, 0,0);
+      nr_fill_indication(gNB, ulsch->frame, ulsch->slot, rdata->ulsch_id, rdata->harq_pid, 0,0);
       //dumpsig=1;
     } else {
       LOG_D(PHY,"[gNB %d] ULSCH: Setting NAK for SFN/SF %d/%d (pid %d, ndi %d, status %d, round %d, RV %d, prb_start %d, prb_size %d, TBS %d) r %d\n",
-            gNB->Mod_id, ulsch_harq->frame, ulsch_harq->slot,
+            gNB->Mod_id, ulsch->frame, ulsch->slot,
             rdata->harq_pid, pusch_pdu->pusch_data.new_data_indicator, ulsch->active,
             ulsch_harq->round,
             ulsch_harq->ulsch_pdu.pusch_data.rv_index,
@@ -263,7 +263,7 @@ void nr_postDecode(PHY_VARS_gNB *gNB, notifiedFIFO_elt_t *req)
             r);
       ulsch->handled = 1;
       LOG_D(PHY, "ULSCH %d in error\n",rdata->ulsch_id);
-      nr_fill_indication(gNB,ulsch_harq->frame, ulsch_harq->slot, rdata->ulsch_id, rdata->harq_pid, 1,0);
+      nr_fill_indication(gNB, ulsch->frame, ulsch->slot, rdata->ulsch_id, rdata->harq_pid, 1,0);
 //      dumpsig=1;
     }
 /*
@@ -558,8 +558,8 @@ void fill_ul_rb_mask(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx) {
     NR_UL_gNB_HARQ_t *ulsch_harq = ulsch->harq_process;
     AssertFatal(ulsch_harq != NULL,"harq_pid %d is not allocated\n",harq_pid);
     if ((ulsch->active == true) &&
-        (ulsch_harq->frame == frame_rx) &&
-        (ulsch_harq->slot == slot_rx) &&
+        (ulsch->frame == frame_rx) &&
+        (ulsch->slot == slot_rx) &&
         (ulsch->handled == 0)) {
       uint8_t symbol_start = ulsch_harq->ulsch_pdu.start_symbol_index;
       uint8_t symbol_end = symbol_start + ulsch_harq->ulsch_pdu.nr_of_symbols;
@@ -779,8 +779,8 @@ int phy_procedures_gNB_uespec_RX(PHY_VARS_gNB *gNB, int frame_rx, int slot_rx)
     AssertFatal(ulsch_harq != NULL, "harq_pid %d is not allocated\n", ulsch->harq_pid);
 
     if ((ulsch->active == true) &&
-        (ulsch_harq->frame == frame_rx) &&
-        (ulsch_harq->slot == slot_rx) &&
+        (ulsch->frame == frame_rx) &&
+        (ulsch->slot == slot_rx) &&
         (ulsch->handled == 0)) {
 
       LOG_D(PHY, "PUSCH ID %d with RNTI %x detection started in frame %d slot %d\n",
diff --git a/openair1/SIMULATION/NR_PHY/dlsim.c b/openair1/SIMULATION/NR_PHY/dlsim.c
index 80ff799ab06..13528aeb6f5 100644
--- a/openair1/SIMULATION/NR_PHY/dlsim.c
+++ b/openair1/SIMULATION/NR_PHY/dlsim.c
@@ -951,8 +951,6 @@ int main(int argc, char **argv)
     reset_meas(&gNB->tparity);
     reset_meas(&gNB->toutput);
 
-    clear_pdsch_stats(gNB);
-
     uint32_t errors_scrambling[16] = {0};
     int n_errors[16] = {0};
     int round_trials[16] = {0};
diff --git a/openair1/SIMULATION/NR_PHY/ulsim.c b/openair1/SIMULATION/NR_PHY/ulsim.c
index 1a02308e128..805352e3b9b 100644
--- a/openair1/SIMULATION/NR_PHY/ulsim.c
+++ b/openair1/SIMULATION/NR_PHY/ulsim.c
@@ -986,8 +986,6 @@ int main(int argc, char **argv)
     double blerStats[16] = {0};
     double berStats[16] = {0};
 
-    clear_pusch_stats(gNB);
-
     uint64_t sum_pusch_delay = 0;
     int min_pusch_delay = INT_MAX;
     int max_pusch_delay = INT_MIN;
@@ -1506,9 +1504,9 @@ int main(int argc, char **argv)
       if (!crc_status)
         effRate += ((double)TBS) / (double)round;
 
-      sum_pusch_delay += gNB->measurements.delay[UE_id].pusch_est_delay;
-      min_pusch_delay = gNB->measurements.delay[UE_id].pusch_est_delay < min_pusch_delay ? gNB->measurements.delay[UE_id].pusch_est_delay : min_pusch_delay;
-      max_pusch_delay = gNB->measurements.delay[UE_id].pusch_est_delay > max_pusch_delay ? gNB->measurements.delay[UE_id].pusch_est_delay : max_pusch_delay;
+      sum_pusch_delay += ulsch_gNB->delay.pusch_est_delay;
+      min_pusch_delay = ulsch_gNB->delay.pusch_est_delay < min_pusch_delay ? ulsch_gNB->delay.pusch_est_delay : min_pusch_delay;
+      max_pusch_delay = ulsch_gNB->delay.pusch_est_delay > max_pusch_delay ? ulsch_gNB->delay.pusch_est_delay : max_pusch_delay;
       delay_pusch_est_count++;
 
     } // trial loop
-- 
GitLab