From 2278f839699ec2523be5c93d37d1f9497a41b97b Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Thu, 14 Oct 2021 16:56:49 +0200
Subject: [PATCH] Reorganized definition of width, magnitude and fraction for
 fixed point weights, no quantitive change.

---
 .../sdp/src/vhdl/sdp_beamformer_local.vhd     |  2 +-
 .../lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd | 24 ++++++++--------
 .../sdp/src/vhdl/sdp_subband_equalizer.vhd    |  2 +-
 .../model/lofar_station_firmware_model.py     | 28 +++++++++----------
 4 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_local.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_local.vhd
index 2f75f2f098..134b80aa33 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_local.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_local.vhd
@@ -228,7 +228,7 @@ BEGIN
   GENERIC MAP (               
     g_complex             => TRUE,  
     g_representation      => "SIGNED",           
-    g_lsb_w               => c_sdp_W_bf_fraction,
+    g_lsb_w               => c_sdp_W_bf_weight_fraction,
     g_lsb_round           => TRUE,       
     g_lsb_round_clip      => FALSE,      
     g_msb_clip            => FALSE,      
diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
index 547b1301ff..1aa14bfa2e 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
@@ -87,27 +87,29 @@ PACKAGE sdp_pkg is
   CONSTANT c_sdp_V_si_histogram  : NATURAL := 512;
   CONSTANT c_sdp_W_adc           : NATURAL := 14;
   CONSTANT c_sdp_W_adc_jesd      : NATURAL := 16;
-  CONSTANT c_sdp_W_beamlet       : NATURAL := 8;
-  CONSTANT c_sdp_W_beamlet_scale : NATURAL := 16;
-  CONSTANT c_sdp_W_beamlet_sum   : NATURAL := 18;
-  CONSTANT c_sdp_W_bf_magnitude  : NATURAL := 1;
-  CONSTANT c_sdp_W_bf_weight     : NATURAL := 16;
-  CONSTANT c_sdp_W_crosslet      : NATURAL := 16;
   CONSTANT c_sdp_W_fir_coef      : NATURAL := 16;
+  CONSTANT c_sdp_W_subband       : NATURAL := 18;
+  CONSTANT c_sdp_W_crosslet      : NATURAL := 16;
+  CONSTANT c_sdp_W_beamlet_sum   : NATURAL := 18;
+  CONSTANT c_sdp_W_beamlet       : NATURAL := 8;
   CONSTANT c_sdp_W_gn_id         : NATURAL := 5;
   CONSTANT c_sdp_W_statistic     : NATURAL := 64;
-  CONSTANT c_sdp_W_sub_magnitude : NATURAL := 2;
-  CONSTANT c_sdp_W_sub_weight    : NATURAL := 16;
-  CONSTANT c_sdp_W_subband       : NATURAL := 18;
+  CONSTANT c_sdp_W_sub_weight              : NATURAL := 16;  -- = w in s(w, p), s = signed
+  CONSTANT c_sdp_W_sub_weight_fraction     : NATURAL := 13;  -- = p in s(w, p)
+  CONSTANT c_sdp_W_sub_weight_magnitude    : NATURAL := c_sdp_W_sub_weight - c_sdp_W_sub_weight_fraction - 1;  -- = 2
+  CONSTANT c_sdp_W_beamlet_scale           : NATURAL := 16;  -- = w in u(w, p), u = unsigned
+  CONSTANT c_sdp_W_beamlet_scale_fraction  : NATURAL := 15;  -- = p in u(w, p)
+  CONSTANT c_sdp_W_beamlet_scale_magnitude : NATURAL := c_sdp_W_beamlet_scale - c_sdp_W_beamlet_scale_fraction;  -- = 1
+  CONSTANT c_sdp_W_bf_weight               : NATURAL := 16;  -- = w in s(w, p), s = signed
+  CONSTANT c_sdp_W_bf_weight_fraction      : NATURAL := 14;  -- = p in s(w, p)
+  CONSTANT c_sdp_W_bf_weight_magnitude     : NATURAL := c_sdp_W_bf_weight - c_sdp_W_bf_weight_fraction - 1;  -- = 1
 
   -- Derived constants
   CONSTANT c_sdp_FS_adc          : NATURAL := 2**(c_sdp_W_adc - 1); -- full scale FS corresponds to amplitude 1.0
   CONSTANT c_sdp_P_pfb           : NATURAL := c_sdp_S_pn / c_sdp_Q_fft;
   CONSTANT c_sdp_T_adc           : TIME    := (10**6 / c_sdp_f_adc_MHz) * 1 ps;
   CONSTANT c_sdp_T_sub           : TIME    := c_sdp_N_fft * c_sdp_T_adc;
-  CONSTANT c_sdp_W_bf_fraction   : NATURAL := c_sdp_W_bf_weight - c_sdp_W_bf_magnitude -1;
   CONSTANT c_sdp_W_bf_product    : NATURAL := c_sdp_W_subband + c_sdp_W_bf_weight -1;
-  CONSTANT c_sdp_W_sub_fraction  : NATURAL := c_sdp_W_sub_weight - c_sdp_W_sub_magnitude -1;
   CONSTANT c_sdp_X_sq            : NATURAL := c_sdp_S_pn * c_sdp_S_pn;
 
   -- 
diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_subband_equalizer.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_subband_equalizer.vhd
index fe03722c40..7ae6012f07 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_subband_equalizer.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_subband_equalizer.vhd
@@ -146,7 +146,7 @@ BEGIN
     GENERIC MAP (               
       g_complex             => TRUE,  
       g_representation      => "SIGNED",           
-      g_lsb_w               => c_sdp_W_sub_fraction,
+      g_lsb_w               => c_sdp_W_sub_weight_fraction,
       g_lsb_round           => TRUE,       
       g_lsb_round_clip      => FALSE,      
       g_msb_clip            => TRUE,      
diff --git a/applications/lofar2/model/lofar_station_firmware_model.py b/applications/lofar2/model/lofar_station_firmware_model.py
index 11d1ecea59..260a77962c 100644
--- a/applications/lofar2/model/lofar_station_firmware_model.py
+++ b/applications/lofar2/model/lofar_station_firmware_model.py
@@ -148,21 +148,21 @@ N_sub             = 512          # = N_fft / N_complex
 W_fsub_proc       = math.log(math.sqrt(N_sub), 2)    # = 4.5, processing gain of F_sub in number of bits
 
 N_int_sub         = 195312.5
-W_se_weight      = 16
-W_se_magnitude   = 1
-W_se_fraction    = 14
-c_se_unit_weight = 2**W_se_fraction
 
-W_bf_weight = 16
-W_bf_magnitude = 1
-W_bf_fraction  = 14
-c_bf_unit_weight = 2**W_sub_fraction
+W_sub_weight              = 16  # signed
+W_sub_weight_fraction     = 13
+W_sub_weight_magnitude    = W_sub_weight - W_sub_weight_fraction - 1
+c_sub_weight_unit         = 2**W_sub_weight_fraction
 
-W_beamlet_scale = 16
-W_beamlet_scale_magnitude = 1
-W_beamlet_scale_fraction  = 14
-c_beamlet_scale_unit = 2**W_beamlet_scale_fraction
+W_bf_weight               = 16  # signed
+W_bf_weight_fraction      = 14
+W_bf_weight_magnitude     = W_bf_weight - W_bf_weight_fraction - 1
+c_bf_weight_unit          = 2**W_bf_weight_fraction
 
+W_beamlet_scale           = 16  # unsigned
+W_beamlet_scale_fraction  = 15
+W_beamlet_scale_magnitude = W_beamlet_scale - W_beamlet_scale_fraction
+c_beamlet_scale_unit      = 2**W_beamlet_scale_fraction
 
 W_sst_fraction  =      4        # Extra fraction bits for SST subband input compared to W_adc
 W_sst_in        =     18        # Word width in number of bits of SST subband data input, = W_adc + W_sst_fraction
@@ -312,8 +312,8 @@ if 'dynamic' in models:
 # Derived gains
 
 # Aperif BF subband CB voltage weight
-subWeight = c_sub_unit_weight * subWeightAdjust      # = 16384 = 2**14 * 1.0
-bfWeight = c_bf_unit_weight * bfWeightAdjust         # = 16384 = 2**14 * 1.0
+subWeight = c_sub_weight_unit * subWeightAdjust      # = 16384 = 2**14 * 1.0
+bfWeight = c_bf_weight_unit * bfWeightAdjust         # = 16384 = 2**14 * 1.0
 if quantize:
     subWeight = np.round(subWeight)
     bfWeight = np.round(bfWeight)
-- 
GitLab