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.

145 lines
4.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. package StackP is
  17. generic (
  18. type STACK_TYPE;
  19. MAX_LEN : natural := 64;
  20. function to_string(d : in STACK_TYPE) return string
  21. );
  22. -- linked list stack interface
  23. type t_stack is protected
  24. procedure push (data : in STACK_TYPE);
  25. procedure pop (data : inout STACK_TYPE);
  26. procedure init (logging : in boolean := false);
  27. impure function is_empty return boolean;
  28. impure function is_full return boolean;
  29. impure function fillstate return natural;
  30. end protected t_stack;
  31. end package StackP;
  32. package body StackP is
  33. -- linked list stack implementation
  34. type t_stack is protected body
  35. type t_entry;
  36. type t_entry_ptr is access t_entry;
  37. type t_data_ptr is access STACK_TYPE;
  38. type t_entry is record
  39. data : t_data_ptr;
  40. last_entry : t_entry_ptr;
  41. next_entry : t_entry_ptr;
  42. end record t_entry;
  43. variable v_head : t_entry_ptr := null;
  44. variable v_tail : t_entry_ptr := null;
  45. variable v_count : natural := 0;
  46. variable v_logging : boolean := false;
  47. -- write one entry into queue by
  48. -- creating new entry at head of list
  49. procedure push (data : in STACK_TYPE) is
  50. variable v_entry : t_entry_ptr;
  51. begin
  52. if (not(is_full)) then
  53. if (v_count /= 0) then
  54. v_entry := new t_entry;
  55. v_entry.data := new STACK_TYPE'(data);
  56. v_entry.last_entry := v_head;
  57. v_entry.next_entry := null;
  58. v_head := v_entry;
  59. v_head.last_entry.next_entry := v_head;
  60. else
  61. v_head := new t_entry;
  62. v_head.data := new STACK_TYPE'(data);
  63. v_head.last_entry := null;
  64. v_head.next_entry := null;
  65. v_tail := v_head;
  66. end if;
  67. v_count := v_count + 1;
  68. if v_logging then
  69. report t_stack'instance_name & " pushed 0x" & to_string(data) & " on stack";
  70. end if;
  71. else
  72. assert false
  73. report t_stack'instance_name & " push to full stack -> discared"
  74. severity warning;
  75. end if;
  76. end procedure push;
  77. -- read one entry from queue at tail of list and
  78. -- delete that entry from list after read
  79. procedure pop (data : inout STACK_TYPE) is
  80. variable v_entry : t_entry_ptr := v_head;
  81. begin
  82. assert not(is_empty)
  83. report "pop from empty queue -> discarded"
  84. severity failure;
  85. data := v_head.data.all;
  86. v_head := v_head.last_entry;
  87. deallocate(v_entry.data);
  88. deallocate(v_entry);
  89. v_count := v_count - 1;
  90. if v_logging then
  91. report t_stack'instance_name & " popped 0x" & to_string(data) & " from stack";
  92. end if;
  93. end procedure pop;
  94. procedure init (logging : in boolean := false) is
  95. begin
  96. v_logging := logging;
  97. end procedure init;
  98. -- returns true if queue is empty, false otherwise
  99. impure function is_empty return boolean is
  100. begin
  101. return v_head = null;
  102. end function is_empty;
  103. -- returns true if queue is full, false otherwise
  104. impure function is_full return boolean is
  105. begin
  106. return v_count = MAX_LEN;
  107. end function is_full;
  108. -- returns number of filled slots in queue
  109. impure function fillstate return natural is
  110. begin
  111. return v_count;
  112. end function fillstate;
  113. end protected body t_stack;
  114. end package body StackP;