Library of reusable VHDL components
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.8 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.math_real.all;
  5. package UtilsP is
  6. function and_reduce (data : in std_logic_vector) return std_logic;
  7. function and_reduce (data : in boolean_vector) return boolean;
  8. function or_reduce (data : in std_logic_vector) return std_logic;
  9. function or_reduce (data : in boolean_vector) return boolean;
  10. function xor_reduce (data : in std_logic_vector) return std_logic;
  11. function even_parity (data : in std_logic_vector) return std_logic;
  12. function odd_parity (data : in std_logic_vector) return std_logic;
  13. function count_ones (data : in std_logic_vector) return natural;
  14. function one_hot (data : in std_logic_vector) return boolean;
  15. function is_unknown (data : in std_logic_vector) return boolean;
  16. function uint_to_slv (data: in natural; len : in positive) return std_logic_vector;
  17. function slv_to_uint (data: in std_logic_vector) return natural;
  18. end package UtilsP;
  19. package body UtilsP is
  20. function and_reduce (data : in std_logic_vector) return std_logic is
  21. begin
  22. for i in data'range loop
  23. if data(i) = '0' then
  24. return '0';
  25. end if;
  26. end loop;
  27. return '1';
  28. end function and_reduce;
  29. function and_reduce (data : in boolean_vector) return boolean is
  30. begin
  31. for i in data'range loop
  32. if (not(data(i))) then
  33. return false;
  34. end if;
  35. end loop;
  36. return true;
  37. end function and_reduce;
  38. function or_reduce (data : in std_logic_vector) return std_logic is
  39. begin
  40. for i in data'range loop
  41. if data(i) = '1' then
  42. return '1';
  43. end if;
  44. end loop;
  45. return '0';
  46. end function or_reduce;
  47. function or_reduce (data : in boolean_vector) return boolean is
  48. begin
  49. for i in data'range loop
  50. if data(i) then
  51. return true;
  52. end if;
  53. end loop;
  54. return false;
  55. end function or_reduce;
  56. function xor_reduce (data : in std_logic_vector) return std_logic is
  57. variable v_return : std_logic := '0';
  58. begin
  59. for i in data'range loop
  60. v_return := v_return xor data(i);
  61. end loop;
  62. return v_return;
  63. end function xor_reduce;
  64. function even_parity (data : in std_logic_vector) return std_logic is
  65. begin
  66. return xor_reduce(data);
  67. end function even_parity;
  68. function odd_parity (data : in std_logic_vector) return std_logic is
  69. begin
  70. return not(xor_reduce(data));
  71. end function odd_parity;
  72. function count_ones (data : in std_logic_vector) return natural is
  73. variable v_return : natural := 0;
  74. begin
  75. for i in data'range loop
  76. v_return := v_return + 1;
  77. end loop;
  78. return v_return;
  79. end function count_ones;
  80. function one_hot (data : in std_logic_vector) return boolean is
  81. begin
  82. return count_ones(data) = 1;
  83. end function one_hot;
  84. function is_unknown (data : in std_logic_vector) return boolean is
  85. begin
  86. for i in data'range loop
  87. if (data(i) = 'U') then
  88. return true;
  89. end if;
  90. end loop;
  91. end function is_unknown;
  92. function uint_to_slv (data: in natural; len : in positive) return std_logic_vector is
  93. begin
  94. assert len > integer(trunc(log2(real(data))))
  95. report "Warning: std_logic_vector result truncated"
  96. severity warning;
  97. return std_logic_vector(to_unsigned(data, len));
  98. end function uint_to_slv;
  99. function slv_to_uint (data: in std_logic_vector) return natural is
  100. begin
  101. if data'ascending then
  102. assert data'length <= 31 or or_reduce(data(data'left to data'right-31)) = '0'
  103. report "WARNING: integer result overflow"
  104. severity warning;
  105. else
  106. assert data'length <= 31 or or_reduce(data(data'left downto data'right+31)) = '0'
  107. report "WARNING: integer result overflow"
  108. severity warning;
  109. end if;
  110. return to_integer(unsigned(data));
  111. end function slv_to_uint;
  112. end package body UtilsP;