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.

160 lines
4.4 KiB

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. package DictP is
  4. type t_dict is protected
  5. procedure set (key : in string; data : in std_logic_vector);
  6. procedure get (key : in string; data : out std_logic_vector);
  7. procedure del (key : in string);
  8. procedure init (logging : in boolean := false);
  9. procedure clear;
  10. impure function hasKey (key : string) return boolean;
  11. impure function size return natural;
  12. end protected t_dict;
  13. end package DictP;
  14. package body DictP is
  15. type t_dict is protected body
  16. type t_entry;
  17. type t_entry_ptr is access t_entry;
  18. type t_key_ptr is access string;
  19. type t_data_ptr is access std_logic_vector;
  20. type t_entry is record
  21. key : t_key_ptr;
  22. data : t_data_ptr;
  23. last_entry : t_entry_ptr;
  24. next_entry : t_entry_ptr;
  25. end record t_entry;
  26. variable v_head : t_entry_ptr := null;
  27. variable v_size : natural := 0;
  28. variable v_logging : boolean := false;
  29. impure function find (key : string) return t_entry_ptr;
  30. procedure set (key : in string; data : in std_logic_vector) is
  31. variable v_entry : t_entry_ptr := find(key);
  32. begin
  33. if (v_entry = null) then
  34. if (v_head /= null) then
  35. v_entry := new t_entry;
  36. v_entry.key := new string'(key);
  37. v_entry.data := new std_logic_vector'(data);
  38. v_entry.last_entry := v_head;
  39. v_entry.next_entry := v_entry;
  40. v_head := v_entry;
  41. v_head.last_entry.next_entry := v_head;
  42. else
  43. v_head := new t_entry;
  44. v_head.key := new string'(key);
  45. v_head.data := new std_logic_vector'(data);
  46. v_head.last_entry := null;
  47. v_head.next_entry := v_entry;
  48. end if;
  49. if (v_logging) then
  50. report t_dict'instance_name & ": Add key " & key & " with data 0x" & to_hstring(data);
  51. end if;
  52. v_size := v_size + 1;
  53. else
  54. v_entry.data.all := data;
  55. if (v_logging) then
  56. report t_dict'instance_name & ": Set key " & key & " to 0x" & to_hstring(data);
  57. end if;
  58. end if;
  59. end procedure set;
  60. procedure get (key : in string; data : out std_logic_vector) is
  61. variable v_entry : t_entry_ptr := find(key);
  62. begin
  63. if(v_entry /= null) then
  64. data := v_entry.data.all;
  65. if v_logging then
  66. report t_dict'instance_name & ": Got key " & key & " with data 0x" & to_hstring(v_entry.data.all);
  67. end if;
  68. return;
  69. end if;
  70. assert false;
  71. end procedure get;
  72. procedure del (key : in string) is
  73. variable v_entry : t_entry_ptr := find(key);
  74. begin
  75. if (v_entry /= null) then
  76. -- remove head entry
  77. if(v_entry.next_entry = null) then
  78. v_entry.last_entry.next_entry := null;
  79. v_head := v_entry.last_entry;
  80. -- remove start entry
  81. elsif(v_entry.last_entry = null) then
  82. v_entry.next_entry.last_entry := null;
  83. -- remove entry between
  84. else
  85. v_entry.last_entry.next_entry := v_entry.next_entry;
  86. v_entry.next_entry.last_entry := v_entry.last_entry;
  87. end if;
  88. deallocate(v_entry.key);
  89. deallocate(v_entry.data);
  90. deallocate(v_entry);
  91. v_size := v_size - 1;
  92. end if;
  93. end procedure del;
  94. impure function find (key : string) return t_entry_ptr is
  95. variable v_entry : t_entry_ptr := v_head;
  96. begin
  97. while (v_entry /= null) loop
  98. if(v_entry.key.all = key) then
  99. return v_entry;
  100. end if;
  101. v_entry := v_entry.last_entry;
  102. end loop;
  103. return null;
  104. end function find;
  105. procedure clear is
  106. variable v_entry : t_entry_ptr := v_head;
  107. variable v_entry_d : t_entry_ptr;
  108. begin
  109. while (v_entry /= null) loop
  110. v_entry_d := v_entry;
  111. del(v_entry_d.key.all);
  112. v_entry := v_entry.last_entry;
  113. end loop;
  114. end procedure clear;
  115. impure function hasKey (key : string) return boolean is
  116. begin
  117. return find(key) /= null;
  118. end function hasKey;
  119. impure function size return natural is
  120. begin
  121. return v_size;
  122. end function size;
  123. procedure init (logging : in boolean := false) is
  124. begin
  125. v_logging := logging;
  126. end procedure init;
  127. end protected body t_dict;
  128. end package body DictP;