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.

186 lines
5.0 KiB

  1. -- Copyright (c) 2014 - 2022 by Torsten Meissner
  2. --
  3. -- Licensed under the Apache License, Version 2.0 (the "License");
  4. -- you may not use this file except in compliance with the License.
  5. -- You may obtain a copy of the License at
  6. --
  7. -- https://www.apache.org/licenses/LICENSE-2.0
  8. --
  9. -- Unless required by applicable law or agreed to in writing, software
  10. -- distributed under the License is distributed on an "AS IS" BASIS,
  11. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. -- See the License for the specific language governing permissions and
  13. -- limitations under the License.
  14. library ieee;
  15. use ieee.std_logic_1164.all;
  16. use ieee.numeric_std.all;
  17. package UtilsP is
  18. function and_reduce (data : in std_logic_vector) return std_logic;
  19. function and_reduce (data : in boolean_vector) return boolean;
  20. function or_reduce (data : in std_logic_vector) return std_logic;
  21. function or_reduce (data : in boolean_vector) return boolean;
  22. function xor_reduce (data : in std_logic_vector) return std_logic;
  23. function even_parity (data : in std_logic_vector) return std_logic;
  24. function odd_parity (data : in std_logic_vector) return std_logic;
  25. function count_ones (data : in std_logic_vector) return natural;
  26. function one_hot (data : in std_logic_vector) return boolean;
  27. function is_unknown (data : in std_logic_vector) return boolean;
  28. function uint_to_slv (data: in natural; len : in positive) return std_logic_vector;
  29. function slv_to_uint (data: in std_logic_vector) return natural;
  30. function uint_bitsize(data : in natural) return natural;
  31. end package UtilsP;
  32. package body UtilsP is
  33. function and_reduce (data : in std_logic_vector) return std_logic is
  34. variable v_return : std_logic := '1';
  35. begin
  36. for i in data'range loop
  37. v_return := v_return and data(i);
  38. end loop;
  39. return v_return;
  40. end function and_reduce;
  41. function and_reduce (data : in boolean_vector) return boolean is
  42. begin
  43. for i in data'range loop
  44. if (not(data(i))) then
  45. return false;
  46. end if;
  47. end loop;
  48. return true;
  49. end function and_reduce;
  50. function or_reduce (data : in std_logic_vector) return std_logic is
  51. variable v_return : std_logic := '0';
  52. begin
  53. for i in data'range loop
  54. v_return := v_return or data(i);
  55. end loop;
  56. return v_return;
  57. end function or_reduce;
  58. function or_reduce (data : in boolean_vector) return boolean is
  59. begin
  60. for i in data'range loop
  61. if data(i) then
  62. return true;
  63. end if;
  64. end loop;
  65. return false;
  66. end function or_reduce;
  67. function xor_reduce (data : in std_logic_vector) return std_logic is
  68. variable v_return : std_logic := '0';
  69. begin
  70. for i in data'range loop
  71. v_return := v_return xor data(i);
  72. end loop;
  73. return v_return;
  74. end function xor_reduce;
  75. function even_parity (data : in std_logic_vector) return std_logic is
  76. begin
  77. return xor_reduce(data);
  78. end function even_parity;
  79. function odd_parity (data : in std_logic_vector) return std_logic is
  80. begin
  81. return not(xor_reduce(data));
  82. end function odd_parity;
  83. function count_ones (data : in std_logic_vector) return natural is
  84. variable v_return : natural := 0;
  85. begin
  86. for i in data'range loop
  87. if (to_ux01(data(i)) = '1') then
  88. v_return := v_return + 1;
  89. end if;
  90. end loop;
  91. return v_return;
  92. end function count_ones;
  93. function one_hot (data : in std_logic_vector) return boolean is
  94. begin
  95. return count_ones(data) = 1;
  96. end function one_hot;
  97. function is_unknown (data : in std_logic_vector) return boolean is
  98. begin
  99. for i in data'range loop
  100. if (to_ux01(data(i)) = 'U') then
  101. return true;
  102. end if;
  103. end loop;
  104. end function is_unknown;
  105. function uint_to_slv (data: in natural; len : in positive) return std_logic_vector is
  106. begin
  107. assert len >= uint_bitsize(data)
  108. report "Warning: std_logic_vector result truncated"
  109. severity warning;
  110. return std_logic_vector(to_unsigned(data, len));
  111. end function uint_to_slv;
  112. function slv_to_uint (data: in std_logic_vector) return natural is
  113. begin
  114. if data'ascending then
  115. assert data'length <= 31 or or_reduce(data(data'left to data'right-31)) = '0'
  116. report "WARNING: integer result overflow"
  117. severity warning;
  118. else
  119. assert data'length <= 31 or or_reduce(data(data'left downto data'right+31)) = '0'
  120. report "WARNING: integer result overflow"
  121. severity warning;
  122. end if;
  123. return to_integer(unsigned(data));
  124. end function slv_to_uint;
  125. function uint_bitsize(data : in natural) return natural is
  126. variable v_nlz : natural := 0;
  127. variable v_data : unsigned(30 downto 0) := to_unsigned(data, 31);
  128. begin
  129. if (data = 0) then
  130. return 1;
  131. end if;
  132. for i in 30 downto 0 loop
  133. if(v_data(i) /= '0') then
  134. exit;
  135. else
  136. v_nlz := v_nlz + 1;
  137. end if;
  138. end loop;
  139. return 31 - v_nlz;
  140. end function uint_bitsize;
  141. end package body UtilsP;