From 11aa6b8989c509258352a7d52ef5517c255169ed Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Mon, 22 Aug 2022 14:26:36 +0200
Subject: [PATCH] Use same g_ring_info_O_rn_w = 5 (<= c_byte_w) for both
 gn_index and ring_info.O_rn.

---
 .../src/vhdl/disturb2_unb2b_station.vhd            |  2 +-
 .../libraries/disturb/src/vhdl/disturb_station.vhd | 11 +++++------
 .../src/vhdl/lofar2_unb2b_ring.vhd                 |  1 +
 .../src/vhdl/lofar2_unb2c_ring.vhd                 |  1 +
 .../lofar2/libraries/sdp/src/vhdl/sdp_station.vhd  | 11 +++++------
 .../sdp/src/vhdl/sdp_statistics_offload.vhd        | 10 ++++++----
 libraries/base/ring/src/vhdl/ring_info.vhd         | 14 ++++++++++++--
 7 files changed, 31 insertions(+), 19 deletions(-)

diff --git a/applications/disturb2/designs/disturb2_unb2b_station/src/vhdl/disturb2_unb2b_station.vhd b/applications/disturb2/designs/disturb2_unb2b_station/src/vhdl/disturb2_unb2b_station.vhd
index c77bf9b435..c5296dcf39 100644
--- a/applications/disturb2/designs/disturb2_unb2b_station/src/vhdl/disturb2_unb2b_station.vhd
+++ b/applications/disturb2/designs/disturb2_unb2b_station/src/vhdl/disturb2_unb2b_station.vhd
@@ -799,7 +799,7 @@ BEGIN
     ram_st_xsq_cipo                              => ram_st_xsq_cipo 
   );
 
-  -- Use gn_id = ID MOD 32, so map ID to 0:31 range (c_sdp_W_gn_id = 5)
+  -- Use gn_id = ID MOD 32, so map ID to 0:31 range (c_disturb_W_gn_id = 5)
   gn_id <= ID(c_disturb_W_gn_id-1 DOWNTO 0);
 
   -----------------------------------------------------------------------------
diff --git a/applications/disturb2/libraries/disturb/src/vhdl/disturb_station.vhd b/applications/disturb2/libraries/disturb/src/vhdl/disturb_station.vhd
index 7e127caf57..76da089381 100644
--- a/applications/disturb2/libraries/disturb/src/vhdl/disturb_station.vhd
+++ b/applications/disturb2/libraries/disturb/src/vhdl/disturb_station.vhd
@@ -409,8 +409,7 @@ ARCHITECTURE str OF disturb_station IS
   CONSTANT c_ring_1_if_offset          : NATURAL := 2; -- RING_1 signals are indexed at c_nof_if * I + 2.
   CONSTANT c_nof_mac                   : NATURAL := 12; -- Using 9 out of 12 (this is NOT optimized away during synthesis), must match one of the MAC IP variations, e.g. 1, 3, 4, 12, 24, 48
 
-  SIGNAL gn_index : NATURAL := 0;
-  SIGNAL O_rn     : NATURAL := 0;
+  SIGNAL gn_index : NATURAL := 0;  -- range 0:31 (c_disturb_W_gn_id = 5)
   SIGNAL this_rn  : STD_LOGIC_VECTOR(c_byte_w-1 DOWNTO 0);
 
   ----------------------------------------------
@@ -585,6 +584,9 @@ BEGIN
   -- Ring info
   -----------------------------------------------------------------------------
   u_ring_info : ENTITY ring_lib.ring_info
+  GENERIC MAP  (
+    g_ring_info_O_rn_w => c_disturb_W_gn_id  -- wrap O_rn to gn_index range
+  )
   PORT MAP (
     mm_rst => mm_rst,
     mm_clk => mm_clk,
@@ -598,10 +600,7 @@ BEGIN
     ring_info => ring_info
   );
 
-  -- Map O_rn in gn_index range 0:31 (c_sdp_W_gn_id = 5), to determine RN
-  -- index of this_rn
-  O_rn <= TO_UINT(ring_info.O_rn(c_sdp_W_gn_id-1 DOWNTO 0));
-  this_rn <= TO_UVEC(gn_index - O_rn, c_byte_w) WHEN rising_edge(dp_clk);  -- Use register to ease timing closure.
+  this_rn <= TO_UVEC(gn_index - TO_UINT(ring_info.O_rn), c_byte_w) WHEN rising_edge(dp_clk); -- Using register to ease timing closure.
 
   -----------------------------------------------------------------------------
   -- node_adc_input_and_timing (AIT)
diff --git a/applications/lofar2/designs/lofar2_unb2b_ring/src/vhdl/lofar2_unb2b_ring.vhd b/applications/lofar2/designs/lofar2_unb2b_ring/src/vhdl/lofar2_unb2b_ring.vhd
index 8d69605e8d..d6d240b2e8 100644
--- a/applications/lofar2/designs/lofar2_unb2b_ring/src/vhdl/lofar2_unb2b_ring.vhd
+++ b/applications/lofar2/designs/lofar2_unb2b_ring/src/vhdl/lofar2_unb2b_ring.vhd
@@ -750,6 +750,7 @@ BEGIN
     ring_info => ring_info
   );
 
+  -- Use full c_byte_w range of ID for gn_index and ring_info.O_rn
   gn_index <= TO_UINT(ID);
   this_rn <= TO_UVEC(gn_index - TO_UINT(ring_info.O_rn), c_byte_w) WHEN rising_edge(dp_clk); -- Using register to ease timing closure.
 
diff --git a/applications/lofar2/designs/lofar2_unb2c_ring/src/vhdl/lofar2_unb2c_ring.vhd b/applications/lofar2/designs/lofar2_unb2c_ring/src/vhdl/lofar2_unb2c_ring.vhd
index ef5867134b..11260553a0 100644
--- a/applications/lofar2/designs/lofar2_unb2c_ring/src/vhdl/lofar2_unb2c_ring.vhd
+++ b/applications/lofar2/designs/lofar2_unb2c_ring/src/vhdl/lofar2_unb2c_ring.vhd
@@ -716,6 +716,7 @@ BEGIN
     ring_info => ring_info
   );
 
+  -- Use full c_byte_w range of ID for gn_index and ring_info.O_rn
   gn_index <= TO_UINT(ID);
   this_rn <= TO_UVEC(gn_index - TO_UINT(ring_info.O_rn), c_byte_w) WHEN rising_edge(dp_clk); -- Using register to ease timing closure.
 
diff --git a/applications/lofar2/libraries/sdp/src/vhdl/sdp_station.vhd b/applications/lofar2/libraries/sdp/src/vhdl/sdp_station.vhd
index 7f06d8f995..9260356e07 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_station.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_station.vhd
@@ -421,8 +421,7 @@ ARCHITECTURE str OF sdp_station IS
   CONSTANT c_ring_1_if_offset          : NATURAL := 2; -- RING_1 signals are indexed at c_nof_if * I + 2.
   CONSTANT c_ring_nof_mac              : NATURAL := 12; -- Using 9 out of 12 (this is NOT optimized away during synthesis), must match one of the MAC IP variations, e.g. 1, 3, 4, 12, 24, 48
 
-  SIGNAL gn_index                   : NATURAL := 0;
-  SIGNAL O_rn                       : NATURAL := 0;
+  SIGNAL gn_index                   : NATURAL := 0;  -- range 0:31 (c_sdp_W_gn_id = 5)
   SIGNAL this_rn                    : STD_LOGIC_VECTOR(c_byte_w-1 DOWNTO 0);
 
   SIGNAL sdp_info                   : t_sdp_info := c_sdp_info_rst;
@@ -600,6 +599,9 @@ BEGIN
   -- Ring info
   -----------------------------------------------------------------------------
   u_ring_info : ENTITY ring_lib.ring_info
+  GENERIC MAP  (
+    g_ring_info_O_rn_w => c_sdp_W_gn_id  -- wrap O_rn to gn_index range
+  )
   PORT MAP (
     mm_rst => mm_rst,
     mm_clk => mm_clk,
@@ -613,10 +615,7 @@ BEGIN
     ring_info => ring_info
   );
 
-  -- Map O_rn in gn_index range 0:31 (c_sdp_W_gn_id = 5), to determine RN
-  -- index of this_rn
-  O_rn <= TO_UINT(ring_info.O_rn(c_sdp_W_gn_id-1 DOWNTO 0));
-  this_rn <= TO_UVEC(gn_index - O_rn, c_byte_w) WHEN rising_edge(dp_clk);  -- Use register to ease timing closure.
+  this_rn <= TO_UVEC(gn_index - TO_UINT(ring_info.O_rn), c_byte_w) WHEN rising_edge(dp_clk); -- Using register to ease timing closure.
 
   -----------------------------------------------------------------------------
   -- node_adc_input_and_timing (AIT)
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 8883efbc9c..40a078a306 100644
--- a/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd
+++ b/applications/lofar2/libraries/sdp/src/vhdl/sdp_statistics_offload.vhd
@@ -333,17 +333,19 @@ BEGIN
   END PROCESS;
 
   -- Derive and pipeline dynamic parameters that depend on node id and MM, but are otherwise fixed
-  -- . c_sdp_offload_time = 600000 * 5 ns = 3 ms, so max gn_index = 31 starts
-  --   after 93 ms, to just fit within XST T_int min is 100 ms.
+  -- . gn_index and ring_info.O_rn are in 0:31 range defined by c_sdp_W_gn_id = 5
+  -- . O_rn is first GN index in ring, so O_rn <= gn_index
+  -- . c_sdp_offload_time = 600000 * 5 ns = 3 ms, so for max gn_index = 31 the
+  --   offload starts after 93 ms, to just fit within XST T_int min is 100 ms.
   p_reg_parameters : PROCESS(dp_clk)
   BEGIN
     IF rising_edge(dp_clk) THEN
-      p.gn_index         <= gn_index;  -- gn_index is in 0:31 range
+      p.gn_index         <= gn_index;
       p.pn_index         <= func_sdp_gn_index_to_pn_index(p.gn_index);
       p.offset_rn        <= TO_UINT(ring_info.O_rn);
       p.rn_index         <= p.gn_index - p.offset_rn;
       p.local_si_offset  <= p.pn_index * c_sdp_S_pn;
-      p.nof_cycles_dly   <= p.gn_index * g_offload_time;  -- max gn_index = 31
+      p.nof_cycles_dly   <= p.gn_index * g_offload_time;
       p.nof_rn           <= TO_UINT(ring_info.N_rn);
       p.nof_used_P_sq    <= smallest(p.nof_rn / 2 + 1, g_P_sq);
       p.remote_rn        <= func_ring_nof_hops_to_source_rn(r.instance_count, p.rn_index, p.nof_rn, g_crosslets_direction);
diff --git a/libraries/base/ring/src/vhdl/ring_info.vhd b/libraries/base/ring/src/vhdl/ring_info.vhd
index 47e8cdb9fd..e1fbd16e32 100644
--- a/libraries/base/ring/src/vhdl/ring_info.vhd
+++ b/libraries/base/ring/src/vhdl/ring_info.vhd
@@ -38,6 +38,10 @@ USE common_lib.common_field_pkg.ALL;
 USE work.ring_pkg.ALL;
 
 ENTITY ring_info IS
+  GENERIC (
+    -- Actual ring_info.O_rn field width can be less than a byte
+    g_ring_info_O_rn_w : NATURAL := c_byte_w
+  );
   PORT (
     -- Clocks and reset
     mm_rst     : IN  STD_LOGIC;   -- reset synchronous with mm_clk
@@ -58,6 +62,8 @@ ARCHITECTURE str OF ring_info IS
 
   SIGNAL mm_fields_out : STD_LOGIC_VECTOR(field_slv_out_len(c_ring_info_field_arr)-1 DOWNTO 0);
 
+  SIGNAL O_rn : STD_LOGIC_VECTOR(c_byte_w-1 DOWNTO 0);
+
 BEGIN
 
   u_mm_fields: ENTITY mm_lib.mm_fields
@@ -81,10 +87,14 @@ BEGIN
   );
 
   -- get "RW" fields from mm_fields
-  ring_info.O_rn                     <= mm_fields_out(field_hi(c_ring_info_field_arr, "O_rn") DOWNTO field_lo(c_ring_info_field_arr, "O_rn"));
-  ring_info.N_rn                     <= mm_fields_out(field_hi(c_ring_info_field_arr, "N_rn") DOWNTO field_lo(c_ring_info_field_arr, "N_rn"));
+  O_rn <= mm_fields_out(field_hi(c_ring_info_field_arr, "O_rn") DOWNTO field_lo(c_ring_info_field_arr, "O_rn"));
+
+  ring_info.O_rn <= RESIZE_UVEC(O_rn(g_ring_info_O_rn_w-1 DOWNTO 0), c_byte_w);
+  ring_info.N_rn <= mm_fields_out(field_hi(c_ring_info_field_arr, "N_rn") DOWNTO field_lo(c_ring_info_field_arr, "N_rn"));
+
   ring_info.use_cable_to_previous_rn <= sl(mm_fields_out(field_hi(c_ring_info_field_arr, "use_cable_to_previous_rn") DOWNTO field_lo(c_ring_info_field_arr, "use_cable_to_previous_rn")));
   ring_info.use_cable_to_next_rn     <= sl(mm_fields_out(field_hi(c_ring_info_field_arr, "use_cable_to_next_rn") DOWNTO field_lo(c_ring_info_field_arr, "use_cable_to_next_rn")));
+
 END str;
 
 
-- 
GitLab