Browse Source

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)
pull/1/head
T. Meissner 9 years ago
parent
commit
98d6992020
1 changed files with 48 additions and 0 deletions
  1. +48
    -0
      common/UtilsP.vhd

+ 48
- 0
common/UtilsP.vhd View File

@ -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;

Loading…
Cancel
Save