From c2b2d77332e6675ec2679522691f1fdcff37791a Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Wed, 16 Aug 2023 14:02:30 +0200
Subject: [PATCH] Repack beamlet data in two steps.

---
 .../sdp/src/vhdl/sdp_beamformer_output.vhd    | 94 ++++++++++++-------
 .../lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd |  1 +
 2 files changed, 61 insertions(+), 34 deletions(-)

diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_output.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_output.vhd
index 2ba6c2fb7a..29edc88476 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_output.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_beamformer_output.vhd
@@ -43,8 +43,7 @@ use work.sdp_pkg.all;
 entity sdp_beamformer_output is
   generic (
     g_beamset_id : natural := 0
-
-   );
+  );
   port (
     dp_clk   : in  std_logic;
     dp_rst   : in  std_logic;
@@ -89,21 +88,22 @@ architecture str of sdp_beamformer_output is
   constant c_fifo_fill      : natural := c_sdp_cep_payload_nof_longwords;  -- 976
   constant c_fifo_size      : natural := true_log_pow2(c_sdp_cep_payload_nof_longwords) * c_sdp_N_beamsets;  -- 2048
 
-  signal snk_in_concat             : t_dp_sosi;
-  signal dp_repack_data_src_out    : t_dp_sosi;
-  signal dp_packet_merge_src_out   : t_dp_sosi;
-  signal dp_fifo_merge_src_out     : t_dp_sosi;
-  signal dp_fifo_merge_src_in      : t_dp_siso;
-  signal dp_pipeline_src_out       : t_dp_sosi;
-  signal dp_pipeline_src_in        : t_dp_siso;
-  signal dp_offload_tx_src_out     : t_dp_sosi;
-  signal dp_offload_tx_src_in      : t_dp_siso;
-  signal ip_checksum_src_out       : t_dp_sosi;
-  signal ip_checksum_src_in        : t_dp_siso;
-  signal dp_pipeline_ready_src_out : t_dp_sosi;
-  signal dp_pipeline_ready_src_in  : t_dp_siso;
-
-  signal dbg_bsn_offset        : std_logic;
+  signal snk_in_concat              : t_dp_sosi;
+  signal dp_repack_beamlet_src_out  : t_dp_sosi;
+  signal dp_packet_merge_src_out    : t_dp_sosi;
+  signal dp_repack_longword_src_out : t_dp_sosi;
+  signal dp_fifo_fill_eop_src_out   : t_dp_sosi;
+  signal dp_fifo_fill_eop_src_in    : t_dp_siso;
+  signal dp_pipeline_src_out        : t_dp_sosi;
+  signal dp_pipeline_src_in         : t_dp_siso;
+  signal dp_offload_tx_src_out      : t_dp_sosi;
+  signal dp_offload_tx_src_in       : t_dp_siso;
+  signal ip_checksum_src_out        : t_dp_sosi;
+  signal ip_checksum_src_in         : t_dp_siso;
+  signal dp_pipeline_ready_src_out  : t_dp_sosi;
+  signal dp_pipeline_ready_src_in   : t_dp_siso;
+
+  signal dbg_bsn_offset     : std_logic;
   signal payload_err        : std_logic_vector(0 downto 0);
   signal station_info       : std_logic_vector(15 downto 0) := (others => '0');
 
@@ -156,14 +156,15 @@ begin
 
   -------------------------------------------------------------------------------
   -- dp_repack_data
-  -- . 16b -> 64b
-  -- . We don't need to flow control the source because we're going from 16b->64b
+  -- . Repack 16b -> 32b, to get dual polarization beamlets of N_pol_bf *
+  --   N_complex * W_beamlet = 2 * 2 * 8 = 32b words
+  -- . No need to flow control the source, because repack into wider words
   -------------------------------------------------------------------------------
-  u_dp_repack_data : entity dp_lib.dp_repack_data
+  u_dp_repack_data_beamlet : entity dp_lib.dp_repack_data
   generic map (
-    g_in_dat_w      => c_data_w,
-    g_in_nof_words  => 4,
-    g_out_dat_w     => c_longword_w,
+    g_in_dat_w      => c_data_w,  -- = 16b
+    g_in_nof_words  => c_sdp_N_pol_bf,  -- = 2
+    g_out_dat_w     => c_sdp_W_dual_pol_beamlet,  -- = 32b
     g_out_nof_words => 1
   )
   port map (
@@ -173,7 +174,7 @@ begin
     snk_in  => snk_in_concat,
     snk_out => OPEN,
 
-    src_out => dp_repack_data_src_out,
+    src_out => dp_repack_beamlet_src_out,
     src_in  => c_dp_siso_rdy
   );
 
@@ -190,17 +191,42 @@ begin
     clk     => dp_clk,
 
     snk_out => OPEN,
-    snk_in  => dp_repack_data_src_out,
+    snk_in  => dp_repack_beamlet_src_out,
 
     src_in  => c_dp_siso_rdy,
     src_out => dp_packet_merge_src_out
   );
 
+  -------------------------------------------------------------------------------
+  -- dp_repack_data
+  -- . Repack 32b -> 64b, to get 64b longwords for network packet data
+  -- . No need to flow control the source, because repack into wider words
+  -------------------------------------------------------------------------------
+  u_dp_repack_data_longword : entity dp_lib.dp_repack_data
+  generic map (
+    g_in_dat_w      => c_sdp_W_dual_pol_beamlet,  -- = 32b
+    g_in_nof_words  => 2,  -- = 2
+    g_out_dat_w     => c_longword_w,  -- = 64b
+    g_out_nof_words => 1
+  )
+  port map (
+    clk     => dp_clk,
+    rst     => dp_rst,
+
+    snk_in  => dp_packet_merge_src_out,
+    snk_out => OPEN,
+
+    src_out => dp_repack_longword_src_out,
+    src_in  => c_dp_siso_rdy
+  );
+
   -------------------------------------------------------------------------------
   -- FIFO
   -------------------------------------------------------------------------------
+  -- Pass on dp_repack_longword_src_out.err field (from u_dp_packet_merge) via
+  -- separate u_common_fifo_sc_err
   u_dp_fifo_fill_eop_sc : entity dp_lib.dp_fifo_fill_eop_sc
-  generic map (  -- pass on dp_packet_merge_src_out.err via u_common_fifo_sc_err
+  generic map (
     g_data_w         => c_longword_w,
     g_empty_w        => c_byte_w,
     g_use_empty      => true,
@@ -214,9 +240,9 @@ begin
   port map (
     clk     => dp_clk,
     rst     => dp_rst,
-    snk_in  => dp_packet_merge_src_out,
-    src_out => dp_fifo_merge_src_out,
-    src_in  => dp_fifo_merge_src_in
+    snk_in  => dp_repack_longword_src_out,
+    src_out => dp_fifo_fill_eop_src_out,
+    src_in  => dp_fifo_fill_eop_src_in
   );
 
   -- Simple fifo to store the payload error bit at eop of FIFO input to be used at sop of FIFO
@@ -232,10 +258,10 @@ begin
   port map (
     rst    => dp_rst,
     clk    => dp_clk,
-    wr_dat => dp_packet_merge_src_out.err(0 downto 0),
-    wr_req => dp_packet_merge_src_out.eop,
+    wr_dat => dp_repack_longword_src_out.err(0 downto 0),
+    wr_req => dp_repack_longword_src_out.eop,
     rd_dat => payload_err,
-    rd_req => dp_fifo_merge_src_out.sop
+    rd_req => dp_fifo_fill_eop_src_out.sop
   );
 
   -- Pipeline FIFO output to align payload_err at dp_pipeline_src_out.sop
@@ -247,8 +273,8 @@ begin
     rst        => dp_rst,
     clk        => dp_clk,
     -- ST sink
-    snk_out    => dp_fifo_merge_src_in,
-    snk_in     => dp_fifo_merge_src_out,
+    snk_out    => dp_fifo_fill_eop_src_in,
+    snk_in     => dp_fifo_fill_eop_src_out,
     -- ST source
     src_in     => dp_pipeline_src_in,
     src_out    => dp_pipeline_src_out
diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
index cae9604c18..d9b2420586 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
@@ -135,6 +135,7 @@ package sdp_pkg is
   constant c_sdp_X_sq                    : natural := c_sdp_S_pn * c_sdp_S_pn;  -- = 144
   constant c_sdp_block_period            : natural := c_sdp_N_fft * 1000 / c_sdp_f_adc_MHz;  -- = 5120 [ns]
   constant c_sdp_N_beamlets_sdp          : natural := c_sdp_N_beamsets * c_sdp_S_sub_bf;  -- = 976
+  constant c_sdp_W_dual_pol_beamlet      : natural := c_sdp_N_pol_bf * c_nof_complex * c_sdp_W_beamlet;  -- 2 * 2 * 8 = 32b
 
   -- . unit weights
   constant c_sdp_unit_sub_weight      : natural := 2**c_sdp_W_sub_weight_fraction;  -- 2**13, so range +-4.0 for 16 bit signed weight
-- 
GitLab