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.

170 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. if (to_ux01(data(i)) = '1') then
  75. v_return := v_return + 1;
  76. end if;
  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 (to_ux01(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 >= uint_bitsize(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. function uint_bitsize(data : in natural) return natural is
  113. variable v_nlz : natural := 0;
  114. variable v_data : unsigned(30 downto 0) := to_unsigned(data, 31);
  115. begin
  116. if (data = 0) then
  117. return 1;
  118. end if;
  119. for i in 30 downto 0 loop
  120. if(v_data(i) /= '0') then
  121. exit;
  122. else
  123. v_nlz := v_nlz + 1;
  124. end if;
  125. end loop;
  126. return 31 - v_nlz;
  127. end function uint_bitsize;
  128. end package body UtilsP;