From fc9844d8efae21f78c75e4e4c25af7aa5b67f1ff Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Thu, 16 Dec 2021 15:16:52 +0100
Subject: [PATCH] Preserve XST Re,Im order, so use g_user_size = 2 for complex
 data.

---
 .../lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd   |  6 +++---
 .../sdp/src/vhdl/sdp_statistics_offload.vhd     | 17 +++++++----------
 .../sdp/tb/vhdl/tb_sdp_statistics_offload.vhd   | 10 +++++-----
 3 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
index ea3e9fd776..2264b966b7 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_pkg.vhd
@@ -582,9 +582,9 @@ PACKAGE BODY sdp_pkg IS
   FUNCTION func_sdp_get_stat_from_mm_user_size(g_statistics_type : STRING) RETURN NATURAL IS
   -- see sdp_statistics_offload.vhd for description
   BEGIN
-    RETURN sel_a_b(g_statistics_type="BST",                 c_sdp_W_statistic_sz,   -- = 2
-           sel_a_b(g_statistics_type="XST", c_nof_complex * c_sdp_W_statistic_sz,   -- = 4
-                                                            c_sdp_W_statistic_sz)); -- = 2, SST
+    RETURN sel_a_b(g_statistics_type="BST", c_sdp_W_statistic_sz,   -- = 2, so preserve X, Y order
+           sel_a_b(g_statistics_type="XST", c_sdp_W_statistic_sz,   -- = 2, so preserve Re, Im order
+                                            c_sdp_W_statistic_sz)); -- = 2, SST
   END func_sdp_get_stat_from_mm_user_size;
 
   FUNCTION func_sdp_get_stat_from_mm_data_size(g_statistics_type : STRING) RETURN NATURAL IS
diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd
index c9269c6a02..ee230cc31c 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd
@@ -67,23 +67,20 @@
 --   and 64bit big endian.
 --   The g_user_size defines the number of words that get reversed:
 --   . For the SST there is only one uint64 part, so g_user_size = 2.
---   . For the BST the X and Y polarization parts are treated as and array of
+--   . For the BST the X and Y polarization parts are treated as an array of
 --     [N_pol_bf], so index 0 = X is send first and therefore g_user_size = 2
---     to preserve the polarization order.
---   . For the XST the Re and Im complex parts are treated as a cint64, so
---     the imaginary part is send first and therefore g_user_size = 4 to also
---     reverse the Re an Im parts. This is similar as with cint16 values that
---     are packed as Im << 16 + Re in a 32bit word. However it differs from
---     two diminensional arrays of [N_complex], because for arrays index 0
---     is send first and index 0 corresponds to Re.
+--     to preserve the polarization X, Y order.
+--   . For the XST the Re and Im complex parts of cint64 are treated as an
+--     array of [N_complex], so index 0 = Re is send first and therefore
+--     g_user_size = 2 to preserve the complex Re, Im order.
 --
 --   The () show the parts that are contained in g_user_size and that got
 --   reversed by g_reverse_word_order = TRUE compared to the stored order:
 --
 --   Transport order:                   (g_user_size)
 --   SST           (Uh,  Ul),                2
---   BST           (Xh,  Xl), (Yh,  Yl),     2   keep parts order
---   XST           (Ih,  Il,   Rh,  Rl),     4   also reverse parts order
+--   BST           (Xh,  Xl), (Yh,  Yl),     2   keep X, Y parts order
+--   XST           (Rh,  Rl), (Ih,  Il),     2   keep Re, Im parts order
 --
 -------------------------------------------------------------------------------
 
diff --git a/applications/lofar2/libraries/sdp/tb/vhdl/tb_sdp_statistics_offload.vhd b/applications/lofar2/libraries/sdp/tb/vhdl/tb_sdp_statistics_offload.vhd
index 327fa6ce99..d6bc51aad2 100644
--- a/applications/lofar2/libraries/sdp/tb/vhdl/tb_sdp_statistics_offload.vhd
+++ b/applications/lofar2/libraries/sdp/tb/vhdl/tb_sdp_statistics_offload.vhd
@@ -533,7 +533,7 @@ BEGIN
           --     W:    0     1      2     3      4     5 ...  1948  1949   1950  1951
           --     S:    0            1            2       ...   974          975
           -- B = D:    0                         1       ...   487
-          --     I:    0     1      2     3      0       ...     0     1      2     3
+          --     I:    0     1      0     1      0       ...     0     1      0     1
           --  P:  Words values:
           --  0        0     1      2     3      4     5 ...  1948  1949   1950  1951
           --
@@ -564,7 +564,7 @@ BEGIN
           --      W:        0     1      2     3      4     5 ...   572   573   574   575
           --      S:        0            1            2       ...   286         287
           --  X = D:        0                         1       ...   143
-          --      I:        0     1      2     3      0       ...     0     1     2     3
+          --      I:        0     1      0     1      0       ...     0     1     0     1
           --   P: J: K: Word values:
           --   0  0  0      0     1      2     3      4     5 ...   572   573   574   575
           --   1  1       576                                 ...
@@ -586,16 +586,16 @@ BEGIN
           D := S / c_nof_complex;           -- range c_mm_nof_data = 144 Data values, because
                                             -- c_mm_data_size / c_sdp_W_statistic_sz = 2 = c_nof_complex
           X := D;                           -- range c_sdp_X_sq = 144 complex XST values
-          I := W MOD c_mm_user_size;        -- range c_mm_user_size = c_nof_complex * c_sdp_W_statistic_sz = 4 words
+          I := W MOD c_mm_user_size;        -- range c_mm_user_size = c_sdp_W_statistic_sz = 2 words
           P := rx_packet_cnt;               -- range c_mm_nof_packets
           J := P MOD g_nof_crosslets;       -- range g_nof_crosslets
           K := P / g_nof_crosslets;         -- range g_P_sq
 
-          v_exp_data := D * c_mm_user_size;
+          v_exp_data := S * c_mm_user_size;  -- c_mm_user_size = 2
           IF g_reverse_word_order = FALSE THEN
             v_exp_data := v_exp_data + I;
           ELSE
-            v_exp_data := v_exp_data - I + c_mm_user_size-1;  -- c_mm_user_size = 4
+            v_exp_data := v_exp_data - I + c_mm_user_size-1;
           END IF;
           v_exp_data := v_exp_data + J * c_packet_size;  -- c_packet_size = 576
           v_exp_data := v_exp_data + K * c_mm_Xsq_span;  -- c_mm_Xsq_span = 4096
-- 
GitLab