From 98d699202029133994102ed22ca7696ccdabf054 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Wed, 26 Aug 2015 22:44:38 +0200 Subject: [PATCH] Add various new helper functions * one_hot(): returns true when slv is one hot coded * is_unknown(): return true when slv contains bits with unknown value * uint_to_slv(): converts natural to slv (unsigned) * slv_to_uint(): converts slv to natural (unsigned) --- common/UtilsP.vhd | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/common/UtilsP.vhd b/common/UtilsP.vhd index 6bef32a..8b6925d 100644 --- a/common/UtilsP.vhd +++ b/common/UtilsP.vhd @@ -1,6 +1,7 @@ library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; + use ieee.math_real.all; @@ -20,6 +21,13 @@ package UtilsP is function count_ones (data : in std_logic_vector) return natural; + function one_hot (data : in std_logic_vector) return boolean; + + function is_unknown (data : in std_logic_vector) return boolean; + + function uint_to_slv (data: in natural; len : in positive) return std_logic_vector; + function slv_to_uint (data: in std_logic_vector) return natural; + end package UtilsP; @@ -97,7 +105,47 @@ package body UtilsP is for i in data'range loop v_return := v_return + 1; end loop; + return v_return; end function count_ones; + function one_hot (data : in std_logic_vector) return boolean is + begin + return count_ones(data) = 1; + end function one_hot; + + + function is_unknown (data : in std_logic_vector) return boolean is + begin + for i in data'range loop + if (data(i) = 'U') then + return true; + end if; + end loop; + end function is_unknown; + + + function uint_to_slv (data: in natural; len : in positive) return std_logic_vector is + begin + assert len > integer(trunc(log2(real(data)))) + report "Warning: std_logic_vector result truncated" + severity warning; + return std_logic_vector(to_unsigned(data, len)); + end function uint_to_slv; + + function slv_to_uint (data: in std_logic_vector) return natural is + begin + if data'ascending then + assert data'length <= 31 or or_reduce(data(data'left to data'right-31)) = '0' + report "WARNING: integer result overflow" + severity warning; + else + assert data'length <= 31 or or_reduce(data(data'left downto data'right+31)) = '0' + report "WARNING: integer result overflow" + severity warning; + end if; + return to_integer(unsigned(data)); + end function slv_to_uint; + + end package body UtilsP;