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.

232 lines
7.3 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. use ieee.numeric_std.all;
  17. library osvvm;
  18. use osvvm.RandomPkg.all;
  19. library libvhdl;
  20. entity DictT is
  21. end entity DictT;
  22. architecture sim of DictT is
  23. type t_scoreboard is array (natural range <>) of std_logic_vector(7 downto 0);
  24. function to_string(d : string) return string is
  25. begin
  26. return d;
  27. end function to_string;
  28. package StringSlvDict is new libvhdl.DictP
  29. generic map (KEY_TYPE => string,
  30. VALUE_TYPE => std_logic_vector,
  31. key_to_string => to_string,
  32. value_to_string => to_hstring);
  33. use StringSlvDict.all;
  34. shared variable sv_dict : t_dict;
  35. shared variable sv_dact : t_dict;
  36. shared variable sv_duct : t_dict;
  37. begin
  38. DictInitP : process is
  39. begin
  40. sv_dict.init(false);
  41. sv_dact.init(false);
  42. sv_duct.init(false);
  43. wait;
  44. end process DictInitP;
  45. DictTestP : process is
  46. variable v_key : t_dict_key_ptr;
  47. variable v_last_key : t_dict_key_ptr;
  48. variable v_random : RandomPType;
  49. variable v_input : std_logic_vector(7 downto 0);
  50. variable v_output : std_logic_vector(7 downto 0);
  51. variable v_scoreboard : t_scoreboard(0 to 511);
  52. begin
  53. v_random.InitSeed(v_random'instance_name);
  54. -- check initial emptiness
  55. assert sv_dict.size = 0
  56. report "ERROR: Dict should be empty"
  57. severity failure;
  58. -- fill dictionary and check count
  59. report "INFO: Test 1: Fill dictionary";
  60. for i in 0 to 255 loop
  61. v_input := v_random.RandSlv(8);
  62. sv_dict.set(integer'image(i), v_input);
  63. v_scoreboard(i) := v_input;
  64. assert sv_dict.size = i+1
  65. report "ERROR: Dict should have " & to_string(i+1) & " entries"
  66. severity failure;
  67. end loop;
  68. report "INFO: Test successful";
  69. -- read all entries and check for correct data
  70. report "INFO: Test 2: Read dictionary";
  71. for i in 0 to 255 loop
  72. sv_dict.get(integer'image(i), v_output);
  73. assert v_output = v_scoreboard(i)
  74. report "ERROR: Got 0x" & to_hstring(v_output) & ", expected 0x" & to_hstring(v_scoreboard(i))
  75. severity failure;
  76. end loop;
  77. report "INFO: Test successful";
  78. -- overwrite a key/value pair
  79. report "INFO: Test 3: Overwrite a entry";
  80. v_input := v_random.RandSlv(8);
  81. sv_dict.set("128", v_input);
  82. v_scoreboard(128) := v_input;
  83. sv_dict.get("128", v_output);
  84. assert v_output = v_scoreboard(128)
  85. report "ERROR: Got 0x" & to_hstring(v_output) & ", expected 0x" & to_hstring(v_scoreboard(128))
  86. severity failure;
  87. report "INFO: Test successful";
  88. -- check for existing keys
  89. report "INFO: Test 4: Check hasKey() method";
  90. for i in 0 to 255 loop
  91. assert sv_dict.hasKey(integer'image(i))
  92. report "ERROR: Key" & integer'image(i) & " should exist in dictionary"
  93. severity failure;
  94. end loop;
  95. assert not(sv_dict.hasKey("AFFE"))
  96. report "ERROR: Key AFFE shouldn't exist in dictionary"
  97. severity failure;
  98. report "INFO: Test successful";
  99. -- iterate up over all entries
  100. report "INFO: Test 5: Iterate up over all entries";
  101. sv_dict.setIter;
  102. for i in 0 to 255 loop
  103. v_key := new string'(sv_dict.iter(UP));
  104. assert v_key.all = integer'image(i)
  105. report "ERROR: Got key " & v_key.all & ", expected " & integer'image(i)
  106. severity failure;
  107. sv_dict.get(v_key.all, v_output);
  108. assert v_key.all = integer'image(i) and v_output = v_scoreboard(i)
  109. report "ERROR: Got 0x" & to_hstring(v_output) & ", expected 0x" & to_hstring(v_scoreboard(i))
  110. severity failure;
  111. end loop;
  112. v_last_key := v_key;
  113. v_key := new string'(sv_dict.iter(UP));
  114. assert v_key.all = v_last_key.all
  115. report "ERROR: Got key " & v_key.all & ", expected key" & v_last_key.all
  116. severity failure;
  117. report "INFO: Test successful";
  118. -- iterate down over all entries
  119. report "INFO: Test 6: Iterate down over all entries";
  120. sv_dict.setIter(HEAD);
  121. for i in 255 downto 0 loop
  122. v_key := new string'(sv_dict.iter(DOWN));
  123. assert v_key.all = integer'image(i)
  124. report "ERROR: Got key " & v_key.all & ", expected " & integer'image(i)
  125. severity failure;
  126. sv_dict.get(v_key.all, v_output);
  127. assert v_key.all = integer'image(i) and v_output = v_scoreboard(i)
  128. report "ERROR: Got 0x" & to_hstring(v_output) & ", expected 0x" & to_hstring(v_scoreboard(i))
  129. severity failure;
  130. end loop;
  131. v_last_key := v_key;
  132. v_key := new string'(sv_dict.iter(DOWN));
  133. assert v_key.all = v_last_key.all
  134. report "ERROR: Got key " & v_key.all & ", expected key" & v_last_key.all
  135. severity failure;
  136. deallocate(v_key);
  137. report "INFO: Test successful";
  138. -- merge 2 dictionaries
  139. -- fill dictionary and check count
  140. report "INFO: Test 7: Merge dictionaries";
  141. for i in 256 to 511 loop
  142. v_input := v_random.RandSlv(8);
  143. sv_dact.set(integer'image(i), v_input);
  144. v_scoreboard(i) := v_input;
  145. assert sv_dact.size = i-255
  146. report "ERROR: Dict should have " & to_string(i-255) & " entries"
  147. severity failure;
  148. end loop;
  149. -- merge dictionaries
  150. merge(sv_dict, sv_dact, sv_duct);
  151. -- read all entries and check for correct data
  152. for i in 0 to 511 loop
  153. sv_duct.get(integer'image(i), v_output);
  154. assert v_output = v_scoreboard(i)
  155. report "ERROR: Got 0x" & to_hstring(v_output) & ", expected 0x" & to_hstring(v_scoreboard(i))
  156. severity failure;
  157. end loop;
  158. report "INFO: Test successful";
  159. -- Remove key/value pair from head of dictionary
  160. report "INFO: Test 8: Removing entry from head of dictionary";
  161. sv_dict.del("255");
  162. assert not(sv_dict.hasKey("255"))
  163. report "ERROR: Key 255 shouldn't exist in dictionary"
  164. severity failure;
  165. report "INFO: Test successful";
  166. -- Remove key/value pair from head of dictionary
  167. report "INFO: Test 9: Removing entry from middle of dictionary";
  168. sv_dict.del("127");
  169. assert not(sv_dict.hasKey("127"))
  170. report "ERROR: Key 127 shouldn't exist in dictionary"
  171. severity failure;
  172. report "INFO: Test successful";
  173. -- Remove key/value pair from head of dictionary
  174. report "INFO: Test 10: Removing entry from beginning of dictionary";
  175. sv_dict.del("0");
  176. assert not(sv_dict.hasKey("0"))
  177. report "ERROR: Key 0 shouldn't exist in dictionary"
  178. severity failure;
  179. report "INFO: Test successful";
  180. -- Remove key/value pair from head of dictionary
  181. report "INFO: Test 11: Clear all entries from dictionary";
  182. sv_dict.clear;
  183. assert sv_dict.size = 0
  184. report "ERROR: Dict should be empty"
  185. severity failure;
  186. report "INFO: Test successful";
  187. report "INFO: t_dict test finished successfully";
  188. wait;
  189. end process DictTestP;
  190. end architecture sim;