From aacbbc8a26fcaf865c039b3388b7572a2f1712ac Mon Sep 17 00:00:00 2001
From: Michael Boehmer <mboehmer@ph.tum.de>
Date: Tue, 19 Jul 2022 08:57:39 +0200
Subject: [PATCH] bugfixes for forwarder. seems to work now.

---
 gbe_trb/base/gbe_logic_wrapper.vhd            |  9 ++++---
 gbe_trb/base/gbe_wrapper_fifo.vhd             |  4 +--
 gbe_trb/base/trb_net16_gbe_main_control.vhd   | 24 +++++++++--------
 .../base/trb_net16_gbe_protocol_selector.vhd  | 27 ++++++++++---------
 ...net16_gbe_response_constructor_Forward.vhd | 20 +++++++++-----
 5 files changed, 49 insertions(+), 35 deletions(-)

diff --git a/gbe_trb/base/gbe_logic_wrapper.vhd b/gbe_trb/base/gbe_logic_wrapper.vhd
index b864b20..988af77 100644
--- a/gbe_trb/base/gbe_logic_wrapper.vhd
+++ b/gbe_trb/base/gbe_logic_wrapper.vhd
@@ -389,6 +389,9 @@ begin
       TSM_RX_STAT_VEC_IN            => (others => '0'), --mac_rx_stat_vec,
       TSM_RX_STAT_EN_IN             => '0', --mac_rx_stat_en,
 
+      DEBUG_OUT(31 downto 0)        => DEBUG_OUT, 
+      DEBUG_OUT(63 downto 32)       => open, 
+      
       MAC_READY_CONF_IN             => MAC_READY_CONF_IN,
       MAC_RECONF_OUT                => MAC_RECONF_OUT,
       MONITOR_SELECT_REC_OUT        => dbg_select_rec,
@@ -558,9 +561,9 @@ begin
       MONITOR_DROPPED_OUT     => monitor_dropped
     );
 
-  DEBUG_OUT(0) <= fr_rd_en;
-  DEBUG_OUT(1) <= fr_frame_valid;
-  DEBUG_OUT(2) <= fr_get_frame;
+--  DEBUG_OUT(0) <= fr_rd_en;
+--  DEBUG_OUT(1) <= fr_frame_valid;
+--  DEBUG_OUT(2) <= fr_get_frame;
     
   MONITOR_RX_FRAMES_OUT  <= monitor_rx_frames;
   MONITOR_RX_BYTES_OUT   <= monitor_rx_bytes;
diff --git a/gbe_trb/base/gbe_wrapper_fifo.vhd b/gbe_trb/base/gbe_wrapper_fifo.vhd
index 4e74f0a..7ec7ce6 100644
--- a/gbe_trb/base/gbe_wrapper_fifo.vhd
+++ b/gbe_trb/base/gbe_wrapper_fifo.vhd
@@ -237,7 +237,7 @@ begin
 
   -- debug(127 downto 64) are local
   -- debug(63 downto 0) are media interface
-  DEBUG_OUT <= monitor_gen_dbg(95 downto 64);
+--  DEBUG_OUT <= monitor_gen_dbg(95 downto 64);
 
   mac_0 <= MC_UNIQUE_ID_IN(15 downto 8) & MC_UNIQUE_ID_IN(23 downto 16) & MC_UNIQUE_ID_IN(31 downto 24) & x"0" & MC_UNIQUE_ID_IN(35 downto 32) & x"7ada";
 
@@ -353,7 +353,7 @@ begin
     MONITOR_DROPPED_OUT      => monitor_dropped,
     MONITOR_GEN_DBG_OUT      => monitor_gen_dbg,
     MAKE_RESET_OUT           => make_reset0,
-    DEBUG_OUT                => open --DEBUG_OUT
+    DEBUG_OUT                => DEBUG_OUT --open
   );
 
   BUS_IP_TX.ack  <= busip0.ack  when rising_edge(CLK_125_IN);
diff --git a/gbe_trb/base/trb_net16_gbe_main_control.vhd b/gbe_trb/base/trb_net16_gbe_main_control.vhd
index f18acb0..3f3c6ab 100644
--- a/gbe_trb/base/trb_net16_gbe_main_control.vhd
+++ b/gbe_trb/base/trb_net16_gbe_main_control.vhd
@@ -818,16 +818,18 @@ begin
   -- END OF TRI SPEED MAC CONTROLLER
   --***************
 
-  process(CLK)
-  begin
-    if rising_edge(CLK) then
-      DEBUG_OUT(3 downto 0)   <= redirect_state;
-      DEBUG_OUT(7 downto 4)   <= flow_state;
-      DEBUG_OUT(11 downto 8)  <= link_state;
-      DEBUG_OUT(31 downto 12) <= (others => '0');
-
-      DEBUG_OUT(63 downto 32) <= selector_debug(31 downto 0);
-    end if;
-  end process;
+--  process(CLK)
+--  begin
+--    if rising_edge(CLK) then
+--      DEBUG_OUT(3 downto 0)   <= redirect_state;
+--      DEBUG_OUT(7 downto 4)   <= flow_state;
+--      DEBUG_OUT(11 downto 8)  <= link_state;
+--      DEBUG_OUT(31 downto 12) <= (others => '0');
+--
+--      DEBUG_OUT(63 downto 32) <= selector_debug(31 downto 0);
+--    end if;
+--  end process;
+
+  DEBUG_OUT <= selector_debug;
 
 end trb_net16_gbe_main_control;
diff --git a/gbe_trb/base/trb_net16_gbe_protocol_selector.vhd b/gbe_trb/base/trb_net16_gbe_protocol_selector.vhd
index bd15e30..ac5aebe 100644
--- a/gbe_trb/base/trb_net16_gbe_protocol_selector.vhd
+++ b/gbe_trb/base/trb_net16_gbe_protocol_selector.vhd
@@ -612,17 +612,18 @@ begin
         FWD_READY_OUT          => FWD_READY_OUT,
         FWD_FULL_OUT           => FWD_FULL_OUT,
   
-
-
-        DEBUG_OUT              => open
+        DEBUG_OUT              => DEBUG_OUT(31 downto 0) --open
       -- END OF INTERFACE
       );
 
   end generate fwd_gen;
 
+  DEBUG_OUT(63 downto 32) <= (others => '0');
+  
   no_fwd_gen : if INCLUDE_FWD = '0' generate
     resp_ready(5) <= '0';
     busy(5) <= '0';
+    DEBUG_OUT(31 downto 0) <= (others => '0');
   end generate no_fwd_gen;
 
   PS_BUSY_OUT <= busy;
@@ -746,16 +747,16 @@ begin
     end if;
   end process SELECTOR_PROC;
 
-  process(CLK)
-  begin
-    if rising_edge(CLK) then
-      DEBUG_OUT(3 downto 0)   <= select_state;
-      DEBUG_OUT(11 downto 4)  <= std_logic_vector(to_unsigned(index, 8));
-      DEBUG_OUT(19 downto 12) <= "00" & resp_ready; -- 4:0
-      DEBUG_OUT(27 downto 20) <= "00" & busy; -- 4:0
-      DEBUG_OUT(63 downto 28) <= (others => '0');
-    end if;
-  end process;
+--  process(CLK)
+--  begin
+--    if rising_edge(CLK) then
+--      DEBUG_OUT(3 downto 0)   <= select_state;
+--      DEBUG_OUT(11 downto 4)  <= std_logic_vector(to_unsigned(index, 8));
+--      DEBUG_OUT(19 downto 12) <= "00" & resp_ready; -- 4:0
+--      DEBUG_OUT(27 downto 20) <= "00" & busy; -- 4:0
+--      DEBUG_OUT(63 downto 28) <= (others => '0');
+--    end if;
+--  end process;
 
 end trb_net16_gbe_protocol_selector;
 
diff --git a/gbe_trb/protocols/trb_net16_gbe_response_constructor_Forward.vhd b/gbe_trb/protocols/trb_net16_gbe_response_constructor_Forward.vhd
index bc69729..dbc79b9 100644
--- a/gbe_trb/protocols/trb_net16_gbe_response_constructor_Forward.vhd
+++ b/gbe_trb/protocols/trb_net16_gbe_response_constructor_Forward.vhd
@@ -14,6 +14,9 @@ use work.trb_net_gbe_components.all;
 -- Response Constructor which forwards received frame back creating a loopback 
 --
 
+-- WARNING: data latency was wrong by one cycle (first byte not sent, last byte duplicated), and
+--          size was calculated wrong (one off). Works now FOR ME. To be tested carefully!
+
 entity trb_net16_gbe_response_constructor_Forward is
   port (
     CLK                     : in  std_logic;  -- system clock
@@ -185,11 +188,13 @@ port map(
 
 ff_rd_en <= '1' when (TC_RD_EN_IN = '1' and PS_SELECTED_IN = '1') else '0';
 
-TC_DATA_OUT <= ff_q;
+--TC_DATA_OUT <= ff_q; -- BUG?!?
+TC_DATA_OUT <= ff_q when rising_edge(CLK);
 
 PS_RESPONSE_READY_OUT <= '1' when (dissect_current_state = LOAD) else '0';
 
-TC_FRAME_SIZE_OUT <= resp_bytes_ctr + x"1";
+--TC_FRAME_SIZE_OUT <= resp_bytes_ctr + x"1"; -- BUG?!?
+TC_FRAME_SIZE_OUT <= resp_bytes_ctr;
 
 TC_FRAME_TYPE_OUT <= x"0008";
 TC_DEST_MAC_OUT   <= FWD_DST_MAC_IN;
@@ -249,10 +254,13 @@ SENT_FRAMES_OUT     <= sent_frames;
 -- **** debug
 DEBUG_OUT(3 downto 0)   <= state;
 DEBUG_OUT(4)            <= ff_empty;
-DEBUG_OUT(7 downto 5)   <= "000";
-DEBUG_OUT(8)            <= ff_full;
-DEBUG_OUT(11 downto 9)  <= "000";
-DEBUG_OUT(31 downto 12) <= (others => '0');
+DEBUG_OUT(5)            <= ff_full;
+DEBUG_OUT(6)            <= ff_wr_en;
+DEBUG_OUT(7)            <= ff_rd_en;
+DEBUG_OUT(15 downto 8)  <= ff_q(7 downto 0);
+DEBUG_OUT(16)           <= ff_q(8);
+DEBUG_OUT(17)           <= local_eop;
+DEBUG_OUT(31 downto 18) <= (others => '0');
 -- ****
 
 end trb_net16_gbe_response_constructor_Forward;
-- 
2.43.0