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.

171 lines
4.3 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. function uint_bitsize(data : in natural) return natural;
  19. end package UtilsP;
  20. package body UtilsP is
  21. function and_reduce (data : in std_logic_vector) return std_logic is
  22. begin
  23. for i in data'range loop
  24. if data(i) = '0' then
  25. return '0';
  26. end if;
  27. end loop;
  28. return '1';
  29. end function and_reduce;
  30. function and_reduce (data : in boolean_vector) return boolean is
  31. begin
  32. for i in data'range loop
  33. if (not(data(i))) then
  34. return false;
  35. end if;
  36. end loop;
  37. return true;
  38. end function and_reduce;
  39. function or_reduce (data : in std_logic_vector) return std_logic is
  40. begin
  41. for i in data'range loop
  42. if data(i) = '1' then
  43. return '1';
  44. end if;
  45. end loop;
  46. return '0';
  47. end function or_reduce;
  48. function or_reduce (data : in boolean_vector) return boolean is
  49. begin
  50. for i in data'range loop
  51. if data(i) then
  52. return true;
  53. end if;
  54. end loop;
  55. return false;
  56. end function or_reduce;
  57. function xor_reduce (data : in std_logic_vector) return std_logic is
  58. variable v_return : std_logic := '0';
  59. begin
  60. for i in data'range loop
  61. v_return := v_return xor data(i);
  62. end loop;
  63. return v_return;
  64. end function xor_reduce;
  65. function even_parity (data : in std_logic_vector) return std_logic is
  66. begin
  67. return xor_reduce(data);
  68. end function even_parity;
  69. function odd_parity (data : in std_logic_vector) return std_logic is
  70. begin
  71. return not(xor_reduce(data));
  72. end function odd_parity;
  73. function count_ones (data : in std_logic_vector) return natural is
  74. variable v_return : natural := 0;
  75. begin
  76. for i in data'range loop
  77. v_return := v_return + 1;
  78. end loop;
  79. return v_return;
  80. end function count_ones;
  81. function one_hot (data : in std_logic_vector) return boolean is
  82. begin
  83. return count_ones(data) = 1;
  84. end function one_hot;
  85. function is_unknown (data : in std_logic_vector) return boolean is
  86. begin
  87. for i in data'range loop
  88. if (data(i) = 'U') then
  89. return true;
  90. end if;
  91. end loop;
  92. end function is_unknown;
  93. function uint_to_slv (data: in natural; len : in positive) return std_logic_vector is
  94. begin
  95. assert len >= uint_bitsize(data)
  96. report "Warning: std_logic_vector result truncated"
  97. severity warning;
  98. return std_logic_vector(to_unsigned(data, len));
  99. end function uint_to_slv;
  100. function slv_to_uint (data: in std_logic_vector) return natural is
  101. begin
  102. if data'ascending then
  103. assert data'length <= 31 or or_reduce(data(data'left to data'right-31)) = '0'
  104. report "WARNING: integer result overflow"
  105. severity warning;
  106. else
  107. assert data'length <= 31 or or_reduce(data(data'left downto data'right+31)) = '0'
  108. report "WARNING: integer result overflow"
  109. severity warning;
  110. end if;
  111. return to_integer(unsigned(data));
  112. end function slv_to_uint;
  113. function uint_bitsize(data : in natural) return natural is
  114. variable v_nlz : natural := 0;
  115. variable v_data : unsigned(30 downto 0) := to_unsigned(data, 31);
  116. begin
  117. if (data = 0) then
  118. return 1;
  119. end if;
  120. for i in 30 downto 0 loop
  121. if(v_data(i) /= '0') then
  122. exit;
  123. else
  124. v_nlz := v_nlz + 1;
  125. end if;
  126. end loop;
  127. return 31 - v_nlz;
  128. end function uint_bitsize;
  129. end package body UtilsP;