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.

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