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.

168 lines
4.3 KiB

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