@ -1,6 +1,7 @@
library ieee ;
library ieee ;
use ieee.std_logic_1164. all ;
use ieee.std_logic_1164. all ;
use ieee.numeric_std. 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 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 ;
end package UtilsP ;
@ -97,7 +105,47 @@ package body UtilsP is
for i in data 'range loop
for i in data 'range loop
v_return : = v_return + 1 ;
v_return : = v_return + 1 ;
end loop ;
end loop ;
return v_return ;
end function count_ones ;
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 ;
end package body UtilsP ;