From 34d60bde173cb41be5f6f65b4596a47af9080089 Mon Sep 17 00:00:00 2001
From: David Monk <david.gabriel.monk@cern.ch>
Date: Sat, 9 Sep 2023 19:04:43 +0200
Subject: [PATCH] Added tracking event rate monitor

---
 common/addr_table/link_aggregator.xml       |  1 +
 common/firmware/cfg/module.dep              |  1 +
 common/firmware/hdl/LinkAggregatorCore.vhd  | 50 ++++++++++++++++++---
 common/firmware/hdl/LinkAggregatorIPBus.vhd | 10 +++--
 common/firmware/hdl/tracking_constants.vhd  | 11 +++++
 5 files changed, 65 insertions(+), 8 deletions(-)
 create mode 100644 common/firmware/hdl/tracking_constants.vhd

diff --git a/common/addr_table/link_aggregator.xml b/common/addr_table/link_aggregator.xml
index 1026d7a3..1d4ee0bb 100644
--- a/common/addr_table/link_aggregator.xml
+++ b/common/addr_table/link_aggregator.xml
@@ -22,6 +22,7 @@
     </node>
     <node id="rate_monitoring" address="0x004" fwinfo="endpoint;width=2">
         <node id="bx_valid_count" address="0x0"/>
+        <node id="tracking_event_count" address="0x1"/>
     </node>
     <node id="monitoring" address="0x008" fwinfo="endpoint;width=3">
         <node id="non_empty_count_0" address="0x0"/>
diff --git a/common/firmware/cfg/module.dep b/common/firmware/cfg/module.dep
index 2846b5c3..2036591c 100644
--- a/common/firmware/cfg/module.dep
+++ b/common/firmware/cfg/module.dep
@@ -4,6 +4,7 @@ src fixed_pkg_2008.vhd
 
 src mprocessor_constants.vhd
 src ecal_zsf_type_pckg.vhd
+src tracking_constants.vhd
 
 src MProcessor.vhd
 src LinkAggregatorCore.vhd
diff --git a/common/firmware/hdl/LinkAggregatorCore.vhd b/common/firmware/hdl/LinkAggregatorCore.vhd
index c6544ff8..4456b4d3 100644
--- a/common/firmware/hdl/LinkAggregatorCore.vhd
+++ b/common/firmware/hdl/LinkAggregatorCore.vhd
@@ -3,6 +3,7 @@ use IEEE.STD_LOGIC_1164.ALL;
 use IEEE.NUMERIC_STD.ALL;
 use IEEE.STD_LOGIC_MISC.ALL;
 use work.emp_data_types.all;
+use work.tracking_constants.all;
 
 use work.ipbus.all;
 use work.ipbus_reg_types.all;
@@ -28,7 +29,7 @@ entity LinkAggregatorCore is
         readout_reset      : out std_logic := '0';
         --- Debug Ports ---
         status_bits        : out std_logic_vector(15 downto 0);
-        --- IPBus Ports ---
+        debug_tracking_event_count : out std_logic_vector(31 downto 0) := (others => '0')
         non_empty_counts   : out ipb_reg_v(8*N_LINKS - 1 downto 0);
         bx_valid_count_out : out ipb_reg_v(0 downto 0)
     );
@@ -129,7 +130,7 @@ architecture compressed of LinkAggregatorCore is
         output(31 downto 28) := (others => '0');
         return output;
     end function;
-
+    
     signal packet_start_buffered : std_logic := '0';
 
     signal output_overflow_array : std_logic_vector(cBoxcarBx - 1 downto 0) := (others => '0');
@@ -142,6 +143,13 @@ architecture compressed of LinkAggregatorCore is
     signal non_empty_fifo_count  : ipb_reg_v(8*N_LINKS - 1 downto 0)        := (others => (others => '0'));
     signal bx_valid_count        : ipb_reg_v(0 downto 0)                    := (others => (others => '0'));
     signal pBx_counter           : integer                                  := 0;
+    
+    signal readout_reset_signal : std_logic := '0';
+    
+    signal track_stubs : tTrackStubs := (others => LWORD_NULL);
+    signal single_stub_event : std_logic_vector(N_LINKS - 1 downto 0) := (others => '0');
+    signal event_valid_for_tracking : std_logic := '0';
+    signal tracking_event_count : unsigned(31 downto 0) := (others => '0');
 
 begin
 
@@ -215,7 +223,7 @@ begin
                     end if;
                     -- Check for non-empty FIFOs at reset
                     if reset = '1' then 
-                        non_empty_fifo_count(N_LINKS*j + i) <= (others => '0');
+                    non_empty_fifo_count(N_LINKS*j + i) <= (others => '0');
                     else
                         if route_reset = '1' then
                             if empty_route = '0' then
@@ -241,6 +249,23 @@ begin
                 end if;
             end process pCheckBxValidConditionPerLink;
             
+            --==============================--
+            genTrackingEventCheck : if j = 0 generate
+            --==============================--
+            begin
+                --==============================--
+                pCheckForTrackingEvent : process(clk_p)
+                --==============================--
+                begin
+                    if rising_edge(clk_p) then
+                        if unsigned(count_cache) = 1 then
+                            single_stub_event(i) <= '1';
+                        else
+                            single_stub_event(i) <= '0';
+                        end if;
+                    end if;
+                end process pCheckForTrackingEvent;
+            end generate;
             
             -- Enable writing of routing FIFO using the valid flag of the cache FIFO
             wr_en_route <= valid_cache;
@@ -429,9 +454,9 @@ begin
             end if;
             -- If counter is at the end of a packet, then start to readout the contents of the aggregated FIFOs
             if counter = 48 then
-                readout_reset <= '1';
+                readout_reset_signal <= '1';
             else
-                readout_reset <= '0';
+                readout_reset_signal <= '0';
             end if;
         end if;
     end process pReadoutReset;
@@ -463,6 +488,20 @@ begin
             end if;
         end if;
     end process pCountValidBits;
+    
+    
+    pExtractTrackingEvent : process(clk_p)
+    begin
+        if rising_edge(clk_p) then
+            event_valid_for_tracking <= and_reduce(single_stub_event);
+            
+            if readout_reset_signal = '1' and event_valid_for_tracking = '1' then
+                tracking_event_count <= tracking_event_count + 1;
+            end if;
+        end if;
+    end process pExtractTrackingEvent;
+    
+    readout_reset <= readout_reset_signal;
 
     --==============================--
     -- Debug
@@ -479,5 +518,6 @@ begin
     status_bits(9) <= route_overflow_array(0)(0); -- link_aggregator_route_0_0_overflow;
     non_empty_counts <= non_empty_fifo_count;
     bx_valid_count_out <= bx_valid_count;
+    debug_tracking_event_count <= std_logic_vector(tracking_event_count);
 
 end compressed;
diff --git a/common/firmware/hdl/LinkAggregatorIPBus.vhd b/common/firmware/hdl/LinkAggregatorIPBus.vhd
index 4f1664f1..4ff1f48d 100644
--- a/common/firmware/hdl/LinkAggregatorIPBus.vhd
+++ b/common/firmware/hdl/LinkAggregatorIPBus.vhd
@@ -43,13 +43,15 @@ architecture Behavorial of LinkAggregatorIPBus is
     -- signal link_aggregator_status_registers : ipb_reg_v(4 - 1 downto 0) := (others => (others => '0'));
     signal control_registers                : ipb_reg_v(1 downto 0)             := (others => (others => '0'));
     signal mprocessor_monitoring_registers  : ipb_reg_v(8*N_LINKS - 1 downto 0) := (others => (others => '0'));
-    signal rate_monitoring_registers        : ipb_reg_v(1 - 1 downto 0)         := (others => (others => '0'));
+    signal rate_monitoring_registers        : ipb_reg_v(2 - 1 downto 0)         := (others => (others => '0'));
     signal bx_valid_count                   : ipb_reg_v(1 - 1 downto 0)         := (others => (others => '0'));
 
     signal masked_links                     : ldata(N_LINKS - 1 downto 0)       := (others => LWORD_NULL);
     signal thresholds_l                     : tIntegerArray                     := (others => 0);
     signal thresholds_u                     : tIntegerArray                     := (others => 0);
 
+    signal tracking_event_count : std_logic_vector(31 downto 0) := (others => '0');
+
 begin
 
     --==============================--
@@ -107,7 +109,7 @@ begin
     --==============================--
     generic map(
         N_CTRL       => 0,
-        N_STAT       => 1
+        N_STAT       => 2
     )
     port map(
         clk          => ipb_clk,
@@ -219,9 +221,11 @@ begin
         --- Debug Ports ---
         -- status_bits      => link_aggregator_status_registers(3)(15 downto 0),
         non_empty_counts => mprocessor_monitoring_registers,
-        bx_valid_count_out => bx_valid_count
+        bx_valid_count_out => bx_valid_count,
+        debug_tracking_event_count => tracking_event_count
     );
     
     rate_monitoring_registers(0) <= bx_valid_count(0);
+    rate_monitoring_registers(1) <= tracking_event_count;
 
 end Behavorial;
diff --git a/common/firmware/hdl/tracking_constants.vhd b/common/firmware/hdl/tracking_constants.vhd
new file mode 100644
index 00000000..121f21cf
--- /dev/null
+++ b/common/firmware/hdl/tracking_constants.vhd
@@ -0,0 +1,11 @@
+library IEEE;
+use IEEE.STD_LOGIC_1164.ALL;
+use IEEE.NUMERIC_STD.ALL;
+
+use work.emp_data_types.all;
+
+package tracking_constants is
+    
+    type tTrackStubs is array (0 to 5) of lword;
+    
+end package tracking_constants;
\ No newline at end of file
-- 
GitLab