From 3a58dec89ae3286b9af3f9796c248e69d229bb40 Mon Sep 17 00:00:00 2001
From: Eric Kooistra <kooistra@astron.nl>
Date: Wed, 16 Apr 2025 14:14:08 +0200
Subject: [PATCH] Use almost_equal(sum_of_bins, g_nof_data_per_sync,
 c_sum_delta) to work around missed counts due to block RAM
 mixed_port_feed_through_mode = DONT_CARE.

---
 libraries/dsp/st/tb/vhdl/tb_st_histogram.vhd | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libraries/dsp/st/tb/vhdl/tb_st_histogram.vhd b/libraries/dsp/st/tb/vhdl/tb_st_histogram.vhd
index df823cd06f..3f991dcef2 100644
--- a/libraries/dsp/st/tb/vhdl/tb_st_histogram.vhd
+++ b/libraries/dsp/st/tb/vhdl/tb_st_histogram.vhd
@@ -90,6 +90,7 @@ architecture tb of tb_st_histogram is
   constant c_nof_levels_per_bin           : natural := (2**g_data_w) / g_nof_bins;  -- e.g. 2 values per bin if g_data_w=9 (512 levels) and g_nof_bins=256
   constant c_ram_dat_w                    : natural := ceil_log2(g_nof_data_per_sync + 1);
   constant c_ram_adr_w                    : natural := ceil_log2(g_nof_bins);
+  constant c_sum_delta                    : natural := largest(g_nof_data_per_sync / 100, 1);  -- 1 % margin
 
   ---------------------------------------------------------------------------
   -- Clocks and resets
@@ -342,6 +343,9 @@ begin
       if i > 0 then  -- Skip sync 0 (histogram still all zeros)
         assert sum_of_bins = g_nof_data_per_sync
           report "Sum of bins not equal to g_nof_data_per_sync (expected " & integer'image(g_nof_data_per_sync) & ", actual " & integer'image(sum_of_bins) & ")"
+          severity WARNING;  -- Delta caused by block RAM mixed_port_feed_through_mode = DONT_CARE and wr and rd access at same address
+        assert almost_equal(sum_of_bins, g_nof_data_per_sync, c_sum_delta)
+          report "Sum of bins not within g_nof_data_per_sync +- c_sum_delta (expected " & integer'image(g_nof_data_per_sync) & ", actual " & integer'image(sum_of_bins) & ")"
           severity ERROR;
       end if;
     end loop;
-- 
GitLab