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.

216 lines
6.7 KiB

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