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.

204 lines
5.8 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. --+ including vhdl 2008 libraries
  4. --+ These lines can be commented out when using
  5. --+ a simulator with built-in VHDL 2008 support
  6. library ieee_proposed;
  7. use ieee_proposed.standard_additions.all;
  8. use ieee_proposed.std_logic_1164_additions.all;
  9. package QueueP is
  10. -- simple queue interface
  11. type t_simple_queue is protected
  12. procedure push (data : in std_logic_vector);
  13. procedure pop (data : out std_logic_vector);
  14. impure function is_empty return boolean;
  15. impure function is_full return boolean;
  16. impure function fillstate return natural;
  17. impure function freeslots return natural;
  18. end protected t_simple_queue;
  19. -- linked list queue interface
  20. type t_list_queue is protected
  21. procedure push (data : in std_logic_vector);
  22. procedure pop (data : inout std_logic_vector);
  23. procedure init (depth : in natural; logging : in boolean := false);
  24. impure function is_empty return boolean;
  25. impure function is_full return boolean;
  26. impure function fillstate return natural;
  27. impure function freeslots return natural;
  28. end protected t_list_queue;
  29. end package QueueP;
  30. package body QueueP is
  31. -- simple queue implementation
  32. -- inspired by noasic article http://noasic.com/blog/a-simple-fifo-using-vhdl-protected-types/
  33. type t_simple_queue is protected body
  34. constant C_QUEUE_DEPTH : natural := 64;
  35. constant C_QUEUE_WIDTH : natural := 64;
  36. type t_queue_array is array (0 to C_QUEUE_DEPTH-1) of std_logic_vector(C_QUEUE_WIDTH-1 downto 0);
  37. variable v_queue : t_queue_array := (others => (others => '0'));
  38. variable v_count : natural range 0 to t_queue_array'length := 0;
  39. variable v_head : natural range 0 to t_queue_array'high := 0;
  40. variable v_tail : natural range 0 to t_queue_array'high := 0;
  41. -- write one entry into queue
  42. procedure push (data : in std_logic_vector) is
  43. begin
  44. assert not(is_full)
  45. report "push into full queue -> discarded"
  46. severity failure;
  47. v_queue(v_head) := data;
  48. v_head := (v_head + 1) mod t_queue_array'length;
  49. v_count := v_count + 1;
  50. end procedure push;
  51. -- read one entry from queue
  52. procedure pop (data : out std_logic_vector) is
  53. begin
  54. assert not(is_empty)
  55. report "pop from empty queue -> discarded"
  56. severity failure;
  57. data := v_queue(v_tail);
  58. v_tail := (v_tail + 1) mod t_queue_array'length;
  59. v_count := v_count - 1;
  60. end procedure pop;
  61. -- returns true if queue is empty, false otherwise
  62. impure function is_empty return boolean is
  63. begin
  64. return v_count = 0;
  65. end function is_empty;
  66. -- returns true if queue is full, false otherwise
  67. impure function is_full return boolean is
  68. begin
  69. return v_count = t_queue_array'length;
  70. end function is_full;
  71. -- returns number of filled slots in queue
  72. impure function fillstate return natural is
  73. begin
  74. return v_count;
  75. end function fillstate;
  76. -- returns number of free slots in queue
  77. impure function freeslots return natural is
  78. begin
  79. return t_queue_array'length - v_count;
  80. end function freeslots;
  81. end protected body t_simple_queue;
  82. -- linked liste queue implementation
  83. type t_list_queue is protected body
  84. variable v_queue_depth : natural := 0;
  85. constant C_QUEUE_WIDTH : natural := 8;
  86. type t_entry;
  87. type t_entry_ptr is access t_entry;
  88. type t_entry is record
  89. data : std_logic_vector(C_QUEUE_WIDTH-1 downto 0);
  90. last_entry : t_entry_ptr;
  91. next_entry : t_entry_ptr;
  92. end record t_entry;
  93. variable v_head : t_entry_ptr;
  94. variable v_tail : t_entry_ptr;
  95. variable v_count : natural := 0;
  96. variable v_logging : boolean := false;
  97. -- write one entry into queue by
  98. -- creating new entry at head of list
  99. procedure push (data : in std_logic_vector) is
  100. begin
  101. assert not(is_full)
  102. report "push into full queue -> discarded"
  103. severity failure;
  104. if (v_count /= 0) then
  105. v_head := new t_entry'(data, v_head, null);
  106. v_head.last_entry.next_entry := v_head;
  107. else
  108. v_head := new t_entry'(data, null, null);
  109. v_tail := v_head;
  110. end if;
  111. v_count := v_count + 1;
  112. if v_logging then
  113. report t_list_queue'instance_name & " pushed 0x" & to_hstring(data) & " into queue";
  114. end if;
  115. end procedure push;
  116. -- read one entry from queue at tail of list and
  117. -- delete that entry from list after read
  118. procedure pop (data : inout std_logic_vector) is
  119. variable v_entry : t_entry_ptr;
  120. begin
  121. assert not(is_empty)
  122. report "pop from empty queue -> discarded"
  123. severity failure;
  124. data := v_tail.data;
  125. v_entry := v_tail;
  126. v_tail := v_tail.next_entry;
  127. deallocate(v_entry);
  128. v_count := v_count - 1;
  129. if v_logging then
  130. report t_list_queue'instance_name & " popped 0x" & to_hstring(data) & " from queue";
  131. end if;
  132. end procedure pop;
  133. procedure init (depth : in natural; logging : in boolean := false) is
  134. begin
  135. v_queue_depth := depth;
  136. v_logging := logging;
  137. end procedure init;
  138. -- returns true if queue is empty, false otherwise
  139. impure function is_empty return boolean is
  140. begin
  141. return v_tail = null;
  142. end function is_empty;
  143. -- returns true if queue is full, false otherwise
  144. impure function is_full return boolean is
  145. begin
  146. return v_count = v_queue_depth;
  147. end function is_full;
  148. -- returns number of filled slots in queue
  149. impure function fillstate return natural is
  150. begin
  151. return v_count;
  152. end function fillstate;
  153. -- returns number of free slots in queue
  154. impure function freeslots return natural is
  155. begin
  156. return v_queue_depth - v_count;
  157. end function freeslots;
  158. end protected body t_list_queue;
  159. end package body QueueP;