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.

92 lines
2.5 KiB

  1. -- simple queue protected type
  2. -- inspired by noasic article http://noasic.com/blog/a-simple-fifo-using-vhdl-protected-types/
  3. library ieee;
  4. use ieee.std_logic_1164.all;
  5. package QueueP is
  6. type t_simple_queue is protected
  7. procedure push (data : in std_logic_vector);
  8. procedure pop (data : out std_logic_vector);
  9. impure function is_empty return boolean;
  10. impure function is_full return boolean;
  11. impure function fillstate return natural;
  12. impure function freeslots return natural;
  13. end protected t_simple_queue;
  14. end package QueueP;
  15. package body QueueP is
  16. type t_simple_queue is protected body
  17. constant C_QUEUE_DEPTH : natural := 64;
  18. constant C_QUEUE_WIDTH : natural := 64;
  19. type t_queue_array is array (0 to C_QUEUE_DEPTH-1) of std_logic_vector(C_QUEUE_WIDTH-1 downto 0);
  20. variable v_queue : t_queue_array := (others => (others => '0'));
  21. variable v_count : natural range 0 to t_queue_array'length := 0;
  22. variable v_head : natural range 0 to t_queue_array'high := 0;
  23. variable v_tail : natural range 0 to t_queue_array'high := 0;
  24. -- write one entry into queue
  25. procedure push (data : in std_logic_vector) is
  26. begin
  27. assert not(is_full)
  28. report "push into full queue -> discarded"
  29. severity failure;
  30. v_queue(v_head) := data;
  31. v_head := (v_head + 1) mod t_queue_array'length;
  32. v_count := v_count + 1;
  33. end procedure push;
  34. -- read one entry from queue
  35. procedure pop (data : out std_logic_vector) is
  36. begin
  37. assert not(is_empty)
  38. report "pop from empty queue -> discarded"
  39. severity failure;
  40. data := v_queue(v_tail);
  41. v_tail := (v_tail + 1) mod t_queue_array'length;
  42. v_count := v_count - 1;
  43. end procedure pop;
  44. -- returns true if queue is empty, false otherwise
  45. impure function is_empty return boolean is
  46. begin
  47. return v_count = 0;
  48. end function is_empty;
  49. -- returns true if queue is full, false otherwise
  50. impure function is_full return boolean is
  51. begin
  52. return v_count = t_queue_array'length;
  53. end function is_full;
  54. -- returns number of filled slots in queue
  55. impure function fillstate return natural is
  56. begin
  57. return v_count;
  58. end function fillstate;
  59. -- returns number of free slots in queue
  60. impure function freeslots return natural is
  61. begin
  62. return t_queue_array'length - v_count;
  63. end function freeslots;
  64. end protected body t_simple_queue;
  65. end package body QueueP;