From 1d55ba4813d9ab01844e490fafb3795428442366 Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Wed, 2 Nov 2022 14:26:33 +0100
Subject: [PATCH] Output raw and quantized subbands. Use quantized subbands for
 SST.

---
 .../sdp/src/vhdl/node_sdp_filterbank.vhd      | 163 ++++++++++++------
 1 file changed, 110 insertions(+), 53 deletions(-)

diff --git a/applications/lofar2/libraries/sdp/src/vhdl/node_sdp_filterbank.vhd b/applications/lofar2/libraries/sdp/src/vhdl/node_sdp_filterbank.vhd
index 0ca1f59c6d..5381775015 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/node_sdp_filterbank.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/node_sdp_filterbank.vhd
@@ -27,15 +27,32 @@
 -- Description:
 -- . The subband filterbank seperates the incoming timestamped ADC samples into
 --   512 frequency bands called subbands. 
--- . It implements a critically sampled poly-phase filterbank (PFB). The PFB consists of a 
---   poly-phase finite impulse response (PFIR) filter per real input and a 
---   complex fast fourier transform (FFT) per 2 real inputs. 
+-- . It implements a critically sampled poly-phase filterbank (PFB). The PFB
+--   consists of a poly-phase finite impulse response (PFIR) filter per real
+--   input and a  complex fast fourier transform (FFT) per 2 real inputs.
 -- . The number of points of the FFT is 1024.
+--
+-- . Subband widths:
+--   - raw_sosi   : c_subband_raw_dat_w bits
+--   - quant_sosi : c_subband_quant_dat_w = c_sdp_W_subband bits
+--   The quantized subbands are output and used for the SST.
+--   The raw subbands are weighted by the equalizer and are output to allow
+--   further weighting by the beamformer weights (in the BF) or to allow
+--   different rounding (to c_sdp_W_crosslet bits in the XSub).
+--    ____            __
+--   |    |          |  |
+--   |    |--raw---->|EQ|--raw----------------------------> fsub_raw_sosi_arr
+--   |WPFB|          |  |--quant--+-----------------------> fsub_quant_sosi_arr
+--   |    |          |__|         |    ___       ___
+--   |    |                       \-->|   |     |   |
+--   |    |--quant------------------->|sel|---->|SST|
+--   |____|                           |___|     |___|
+--
 -- Remark:
--- .
+--
 -------------------------------------------------------------------------------
 
-LIBRARY IEEE, common_lib, dp_lib, rTwoSDF_lib, wpfb_lib, filter_lib, si_lib, st_lib, mm_lib;
+LIBRARY IEEE, common_lib, dp_lib, rTwoSDF_lib, fft_lib, wpfb_lib, filter_lib, si_lib, st_lib, mm_lib;
 USE IEEE.STD_LOGIC_1164.ALL;
 USE common_lib.common_pkg.ALL;
 USE common_lib.common_mem_pkg.ALL;
@@ -43,6 +60,7 @@ USE common_lib.common_network_layers_pkg.ALL;
 USE dp_lib.dp_stream_pkg.ALL;
 USE rTwoSDF_lib.rTwoSDFPkg.ALL;
 USE filter_lib.fil_pkg.ALL;
+USE fft_lib.fft_pkg.ALL;
 USE wpfb_lib.wpfb_pkg.ALL;
 USE work.sdp_pkg.ALL;
 
@@ -60,11 +78,11 @@ ENTITY node_sdp_filterbank IS
     dp_bsn_source_restart      : IN STD_LOGIC;
     dp_bsn_source_new_interval : IN STD_LOGIC;
 
-    in_sosi_arr   : IN  t_dp_sosi_arr(c_sdp_S_pn-1 DOWNTO 0);
-    pfb_sosi_arr  : OUT t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0);
-    fsub_sosi_arr : OUT t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0);
-    sst_udp_sosi  : OUT t_dp_sosi;
-    sst_udp_siso  : IN  t_dp_siso := c_dp_siso_rst;
+    in_sosi_arr        : IN  t_dp_sosi_arr(c_sdp_S_pn-1 DOWNTO 0);  -- c_sdp_W_adc bits
+    fsub_quant_sosi_arr: OUT t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0);  -- c_sdp_W_subband bits
+    fsub_raw_sosi_arr  : OUT t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0);  -- c_sdp_W_subband + c_subband_raw_fraction_w bits
+    sst_udp_sosi       : OUT t_dp_sosi;
+    sst_udp_siso       : IN  t_dp_siso := c_dp_siso_rst;
 
     mm_rst             : IN  STD_LOGIC;
     mm_clk             : IN  STD_LOGIC;
@@ -102,7 +120,12 @@ ARCHITECTURE str OF node_sdp_filterbank IS
 
   CONSTANT c_subband_equalizer_latency : NATURAL := 5;
 
-  CONSTANT c_nof_masters : POSITIVE := 2;
+  CONSTANT c_nof_masters : POSITIVE := 2;  -- for M&C MM access and for statistics offload MM access
+
+  CONSTANT c_fft                    : t_fft := func_wpfb_map_wpfb_parameters_to_fft(g_wpfb);
+  CONSTANT c_subband_raw_dat_w      : NATURAL := func_fft_raw_dat_w(c_fft);
+  CONSTANT c_subband_raw_fraction_w : NATURAL := func_fft_raw_fraction_w(c_fft);
+  CONSTANT c_subband_quant_dat_w    : NATURAL := c_subband_raw_dat_w - c_subband_raw_fraction_w;
 
   CONSTANT c_si_pipeline : NATURAL := 1;
 
@@ -118,20 +141,36 @@ ARCHITECTURE str OF node_sdp_filterbank IS
   SIGNAL master_mosi_arr     : t_mem_mosi_arr(0 TO c_nof_masters-1) := (OTHERS=>c_mem_mosi_rst);
   SIGNAL master_miso_arr     : t_mem_miso_arr(0 TO c_nof_masters-1) := (OTHERS=>c_mem_miso_rst);
 
-  SIGNAL si_sosi_arr                    : t_dp_sosi_arr(c_sdp_S_pn-1 DOWNTO 0)  := (OTHERS => c_dp_sosi_rst);
-  SIGNAL wpfb_unit_in_sosi_arr          : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL wpfb_unit_fil_sosi_arr         : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL wpfb_unit_out_quant_sosi_arr   : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL wpfb_unit_out_raw_sosi_arr     : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL subband_equalizer_out_sosi_arr : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL dp_selector_out_sosi_arr       : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
-  SIGNAL scope_sosi_arr                 : t_dp_sosi_integer_arr(c_sdp_S_pn-1 DOWNTO 0);
+  SIGNAL si_sosi_arr                      : t_dp_sosi_arr(c_sdp_S_pn-1 DOWNTO 0)  := (OTHERS => c_dp_sosi_rst);
+
+  SIGNAL wpfb_unit_in_sosi_arr            : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+  SIGNAL wpfb_unit_fil_sosi_arr           : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+  SIGNAL wpfb_unit_out_raw_sosi_arr       : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+  SIGNAL wpfb_unit_out_quant_sosi_arr     : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+
+  SIGNAL subband_equalizer_raw_sosi_arr   : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+  SIGNAL subband_equalizer_quant_sosi_arr : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
+
+  SIGNAL scope_wpfb_unit_out_quant_sosi_arr : t_dp_sosi_integer_arr(c_sdp_S_pn-1 DOWNTO 0);
+  SIGNAL scope_equalizer_quant_sosi_arr     : t_dp_sosi_integer_arr(c_sdp_S_pn-1 DOWNTO 0);
+
+  SIGNAL dp_selector_quant_sosi_arr       : t_dp_sosi_arr(c_sdp_P_pfb-1 DOWNTO 0) := (OTHERS => c_dp_sosi_rst);
   
   SIGNAL selector_en                : STD_LOGIC;
   SIGNAL weighted_subbands_flag     : STD_LOGIC;
+
   SIGNAL dp_bsn_source_restart_pipe : STD_LOGIC;
 
+  -- debug signals to view parameters in Wave Window
+  signal dbg_g_wpfb                   : t_wpfb := g_wpfb;
+  signal dbg_c_subband_raw_dat_w      : natural := c_subband_raw_dat_w;
+  signal dbg_c_subband_raw_fraction_w : natural := c_subband_raw_fraction_w;
+  signal dbg_c_subband_quant_dat_w    : natural := c_subband_quant_dat_w;
+
 BEGIN
+
+  ASSERT c_subband_quant_dat_w = c_sdp_W_subband REPORT "FSub: Quantized subband width mismatch." SEVERITY FAILURE;
+
   ---------------------------------------------------------------
   -- SPECTRAL INVERSION 
   ---------------------------------------------------------------
@@ -206,23 +245,23 @@ BEGIN
     dp_bsn_source_restart => dp_bsn_source_restart_pipe
   );
   
-  -- Output PFB streams
-  pfb_sosi_arr <= wpfb_unit_out_quant_sosi_arr;
-
   ---------------------------------------------------------------
   -- SUBBAND EQUALIZER
   ---------------------------------------------------------------
   u_sdp_subband_equalizer : ENTITY work.sdp_subband_equalizer
     GENERIC MAP (
-      g_gains_file_name => c_gains_file_name
+      g_gains_file_name => c_gains_file_name,
+      g_raw_dat_w       => c_subband_raw_dat_w,
+      g_raw_fraction_w  => c_subband_raw_fraction_w
     )
     PORT MAP(
       dp_clk         => dp_clk,
       dp_rst         => dp_rst,
 
-      in_sosi_arr    => wpfb_unit_out_quant_sosi_arr,
-      out_sosi_arr   => subband_equalizer_out_sosi_arr,
-  
+      in_raw_sosi_arr    => wpfb_unit_out_raw_sosi_arr,
+      out_raw_sosi_arr   => subband_equalizer_raw_sosi_arr,
+      out_quant_sosi_arr => subband_equalizer_quant_sosi_arr,
+
       mm_rst         => mm_rst,
       mm_clk         => mm_clk,
   
@@ -231,10 +270,11 @@ BEGIN
     );
 
   -- Output fsub streams
-  fsub_sosi_arr <= subband_equalizer_out_sosi_arr; 
+  fsub_quant_sosi_arr <= subband_equalizer_quant_sosi_arr;
+  fsub_raw_sosi_arr <= subband_equalizer_raw_sosi_arr;
 
   ---------------------------------------------------------------
-  -- DP SELECTOR 
+  -- DP SELECTOR for SST input
   ---------------------------------------------------------------
   u_dp_selector_arr : ENTITY dp_lib.dp_selector_arr
     GENERIC MAP (
@@ -251,19 +291,54 @@ BEGIN
       reg_selector_miso => reg_selector_miso,
 
       pipe_sosi_arr  => wpfb_unit_out_quant_sosi_arr,
-      ref_sosi_arr   => subband_equalizer_out_sosi_arr,    
-      out_sosi_arr   => dp_selector_out_sosi_arr,
+      ref_sosi_arr   => subband_equalizer_quant_sosi_arr,
+      out_sosi_arr   => dp_selector_quant_sosi_arr,
 
       selector_en    => selector_en
     );
  
+  ---------------------------------------------------------------
+  -- SIGNAL SCOPE
+  ---------------------------------------------------------------
+  -- synthesis translate_off
+  u_sdp_scope_wpfb : ENTITY work.sdp_scope
+    GENERIC MAP (
+      g_sim            => g_sim,
+      g_selection      => g_scope_selected_subband,
+      g_nof_input      => c_sdp_P_pfb,
+      g_n_deinterleave => c_sdp_Q_fft,
+      g_dat_w          => c_sdp_W_subband
+    )
+    PORT MAP (
+      clk            => dp_clk,
+      rst            => dp_rst,
+      sp_sosi_arr    => wpfb_unit_out_quant_sosi_arr,
+      scope_sosi_arr => scope_wpfb_unit_out_quant_sosi_arr
+    );
+
+  u_sdp_scope_equalizer : ENTITY work.sdp_scope
+    GENERIC MAP (
+      g_sim            => g_sim,
+      g_selection      => g_scope_selected_subband,
+      g_nof_input      => c_sdp_P_pfb,
+      g_n_deinterleave => c_sdp_Q_fft,
+      g_dat_w          => c_sdp_W_subband
+    )
+    PORT MAP (
+      clk            => dp_clk,
+      rst            => dp_rst,
+      sp_sosi_arr    => subband_equalizer_quant_sosi_arr,
+      scope_sosi_arr => scope_equalizer_quant_sosi_arr
+    );
+  -- synthesis translate_on
+
   ---------------------------------------------------------------
   -- SUBBAND STATISTICS
   ---------------------------------------------------------------
   gen_stats_streams: FOR I IN 0 TO c_sdp_P_pfb-1 GENERATE
       u_subband_stats : ENTITY st_lib.st_sst
       GENERIC MAP(
-        g_nof_stat      => c_sdp_N_sub*c_sdp_Q_fft,
+        g_nof_stat      => c_sdp_N_sub * c_sdp_Q_fft,
         g_in_data_w     => c_sdp_W_subband,
         g_stat_data_w   => g_wpfb.stat_data_w,
         g_stat_data_sz  => g_wpfb.stat_data_sz,
@@ -274,7 +349,7 @@ BEGIN
         mm_clk          => mm_clk,
         dp_rst          => dp_rst,
         dp_clk          => dp_clk,
-        in_complex      => dp_selector_out_sosi_arr(I),
+        in_complex      => dp_selector_quant_sosi_arr(I),
         ram_st_sst_mosi => ram_st_sst_mosi_arr(I),
         ram_st_sst_miso => ram_st_sst_miso_arr(I)
       );
@@ -288,7 +363,7 @@ BEGIN
   u_mem_mux_sst : ENTITY common_lib.common_mem_mux
   GENERIC MAP (
     g_nof_mosi    => c_sdp_P_pfb,
-    g_mult_addr_w => ceil_log2(c_sdp_N_sub*c_sdp_Q_fft*g_wpfb.stat_data_sz)
+    g_mult_addr_w => ceil_log2(c_sdp_N_sub * c_sdp_Q_fft * g_wpfb.stat_data_sz)
   )
   PORT MAP (
     mosi     => master_mem_mux_mosi,
@@ -317,28 +392,10 @@ BEGIN
     mux_miso        => master_mem_mux_miso
   );
   
-  ---------------------------------------------------------------
-  -- SIGNAL SCOPE
-  ---------------------------------------------------------------
-  u_sdp_scope : ENTITY work.sdp_scope
-    GENERIC MAP (
-      g_sim            => g_sim,
-      g_selection      => g_scope_selected_subband,
-      g_nof_input      => c_sdp_P_pfb,
-      g_n_deinterleave => c_sdp_Q_fft,
-      g_dat_w          => c_sdp_W_subband
-    )
-    PORT MAP (
-      clk            => dp_clk,
-      rst            => dp_rst,
-      sp_sosi_arr    => subband_equalizer_out_sosi_arr,
-      scope_sosi_arr => scope_sosi_arr
-    );
-
   ---------------------------------------------------------------
   -- STATISTICS OFFLOAD
   ---------------------------------------------------------------
-  weighted_subbands_flag <= NOT selector_en;
+  weighted_subbands_flag <= NOT selector_en WHEN rising_edge(dp_clk);
 
   u_sdp_sst_udp_offload: ENTITY work.sdp_statistics_offload
   GENERIC MAP (
@@ -364,7 +421,7 @@ BEGIN
     reg_bsn_monitor_v2_offload_copi => reg_bsn_monitor_v2_sst_offload_copi,
     reg_bsn_monitor_v2_offload_cipo => reg_bsn_monitor_v2_sst_offload_cipo,
 
-    in_sosi      => dp_selector_out_sosi_arr(0),
+    in_sosi      => dp_selector_quant_sosi_arr(0),
     new_interval => dp_bsn_source_new_interval,
 
     out_sosi  => sst_udp_sosi,
-- 
GitLab