From 943cb1b83b784b9fea21537950bcea7e7ea6cd9f Mon Sep 17 00:00:00 2001
From: Daniel van der Schuur <schuur@astron.nl>
Date: Fri, 5 Oct 2018 06:59:54 +0000
Subject: [PATCH] -Added support for range(0,1,1) = [0], before the lowest
 range supported was  range(0,2,1) = [0,1]. -Reverified in sim using
 tb_tb_dp_counter and tb_dp_counter_func.

---
 .../base/dp/src/vhdl/dp_counter_func.vhd      |  2 +-
 .../dp/src/vhdl/dp_counter_func_single.vhd    | 44 +++++++++++--------
 libraries/base/dp/tb/vhdl/tb_dp_counter.vhd   |  2 +-
 3 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/libraries/base/dp/src/vhdl/dp_counter_func.vhd b/libraries/base/dp/src/vhdl/dp_counter_func.vhd
index 68f51e6475..48674aca67 100644
--- a/libraries/base/dp/src/vhdl/dp_counter_func.vhd
+++ b/libraries/base/dp/src/vhdl/dp_counter_func.vhd
@@ -30,7 +30,7 @@
 -- . Faster changing dimensions carry over in slower changing dimensions 
 --   when dimension maximum is reached
 -- . The outputs are in sync with / apply to src_out.
--- . range(0,2,1) = [0, 1] is the smallest count range allowed
+-- . range(0,1,1) = [0] is the smallest count range
 -- Usage:
 -- . The count values themselves (c0..c4) are very useful to tag streaming
 --   data with corresponding ID indices.
diff --git a/libraries/base/dp/src/vhdl/dp_counter_func_single.vhd b/libraries/base/dp/src/vhdl/dp_counter_func_single.vhd
index 1b14612f8e..a76d3a585b 100644
--- a/libraries/base/dp/src/vhdl/dp_counter_func_single.vhd
+++ b/libraries/base/dp/src/vhdl/dp_counter_func_single.vhd
@@ -94,27 +94,33 @@ BEGIN
     v.count_max := '0';
     v.count_init := '0';
 
-    -- Start counting / init
-    IF r.count_en='0' AND count_en='1' THEN
-      v.count_en := '1';
-      v.count := TO_UVEC(g_range_start+count_offset, c_count_w);
-      v.count_min := '1';
-      v.count_init := '1';
-      
-    -- keep counting
-    ELSIF count_en='1' THEN
-      v.count := INCR_UVEC(r.count, g_range_step);
-      IF check_max='1' AND r.count = TO_UVEC(c_count_max-g_range_step, c_count_w) THEN -- count max almost reached
-        v.count_max := '1';
-      ELSIF r.count = TO_UVEC(c_count_max, c_count_w) THEN -- count max reached
-        -- Reset count to start value
-        v.count := TO_UVEC(g_range_start, c_count_w);
+    IF count_en='1' THEN
+
+      -- Start counting / init
+      IF r.count_en='0' THEN
+        v.count_en := '1';
+        v.count := TO_UVEC(g_range_start+count_offset, c_count_w);
         v.count_min := '1';
+        v.count_init := '1';     
+      -- keep counting
+      ELSE
+        v.count := INCR_UVEC(r.count, g_range_step);
+        IF c_count_max>0 AND check_max='1' AND r.count = TO_UVEC(c_count_max-g_range_step, c_count_w) THEN -- count max almost reached
+          v.count_max := '1';
+        ELSIF r.count = TO_UVEC(c_count_max, c_count_w) THEN -- count max reached
+          -- Reset count to start value
+          v.count := TO_UVEC(g_range_start, c_count_w);
+          v.count_min := '1';
+        END IF;
       END IF;
-    ELSIF check_max='1' THEN
-      IF r.count = TO_UVEC(c_count_max, c_count_w) THEN -- count max reached
-        v.count_max := '1';
-      END IF;
+
+      -- If the maximum count is 0, count_max is always high.
+      IF c_count_max=0 THEN
+        v.count_max := '1';       
+      END IF;   
+
+    ELSIF check_max='1' AND r.count = TO_UVEC(c_count_max, c_count_w) THEN -- count max reached
+      v.count_max := '1';
     END IF;
    
     IF rst = '1' THEN
diff --git a/libraries/base/dp/tb/vhdl/tb_dp_counter.vhd b/libraries/base/dp/tb/vhdl/tb_dp_counter.vhd
index 9598fd6c7c..517bb00692 100644
--- a/libraries/base/dp/tb/vhdl/tb_dp_counter.vhd
+++ b/libraries/base/dp/tb/vhdl/tb_dp_counter.vhd
@@ -52,7 +52,7 @@ ENTITY tb_dp_counter IS
     g_nof_counters     : NATURAL := 3;
     -- min range = [0,2,1] => (0,1) 'the Python way'
     g_range_start      : t_nat_natural_arr(9 DOWNTO 0) := (0,0,0,0,0,0,0, 1, 0, 0);
-    g_range_stop       : t_nat_natural_arr(9 DOWNTO 0) := (2,2,2,2,2,2,7,16,16,16);
+    g_range_stop       : t_nat_natural_arr(9 DOWNTO 0) := (2,2,2,2,2,2,7,16,16, 1);
     g_range_step       : t_nat_natural_arr(9 DOWNTO 0) := (1,1,1,1,1,1,2, 2, 2, 1)
   );
 END tb_dp_counter;
-- 
GitLab