countzero_tb.vhdl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. library work;
  5. use work.common.all;
  6. use work.glibc_random.all;
  7. entity countzero_tb is
  8. end countzero_tb;
  9. architecture behave of countzero_tb is
  10. constant clk_period: time := 10 ns;
  11. signal rs: std_ulogic_vector(63 downto 0);
  12. signal is_32bit, count_right: std_ulogic := '0';
  13. signal result: std_ulogic_vector(63 downto 0);
  14. signal randno: std_ulogic_vector(63 downto 0);
  15. signal clk: std_ulogic;
  16. begin
  17. zerocounter_0: entity work.zero_counter
  18. port map (
  19. clk => clk,
  20. rs => rs,
  21. result => result,
  22. count_right => count_right,
  23. is_32bit => is_32bit
  24. );
  25. clk_process: process
  26. begin
  27. clk <= '0';
  28. wait for clk_period/2;
  29. clk <= '1';
  30. wait for clk_period/2;
  31. end process;
  32. stim_process: process
  33. variable r: std_ulogic_vector(63 downto 0);
  34. begin
  35. -- test with input = 0
  36. report "test zero input";
  37. rs <= (others => '0');
  38. is_32bit <= '0';
  39. count_right <= '0';
  40. wait for clk_period;
  41. assert result = x"0000000000000040"
  42. report "bad cntlzd 0 = " & to_hstring(result);
  43. count_right <= '1';
  44. wait for clk_period;
  45. assert result = x"0000000000000040"
  46. report "bad cnttzd 0 = " & to_hstring(result);
  47. is_32bit <= '1';
  48. count_right <= '0';
  49. wait for clk_period;
  50. assert result = x"0000000000000020"
  51. report "bad cntlzw 0 = " & to_hstring(result);
  52. count_right <= '1';
  53. wait for clk_period;
  54. assert result = x"0000000000000020"
  55. report "bad cnttzw 0 = " & to_hstring(result);
  56. report "test cntlzd/w";
  57. count_right <= '0';
  58. for j in 0 to 100 loop
  59. r := pseudorand(64);
  60. r(63) := '1';
  61. for i in 0 to 63 loop
  62. rs <= r;
  63. is_32bit <= '0';
  64. wait for clk_period;
  65. assert to_integer(unsigned(result)) = i
  66. report "bad cntlzd " & to_hstring(rs) & " -> " & to_hstring(result);
  67. rs <= r(31 downto 0) & r(63 downto 32);
  68. is_32bit <= '1';
  69. wait for clk_period;
  70. if i < 32 then
  71. assert to_integer(unsigned(result)) = i
  72. report "bad cntlzw " & to_hstring(rs) & " -> " & to_hstring(result);
  73. else
  74. assert to_integer(unsigned(result)) = 32
  75. report "bad cntlzw " & to_hstring(rs) & " -> " & to_hstring(result);
  76. end if;
  77. r := '0' & r(63 downto 1);
  78. end loop;
  79. end loop;
  80. report "test cnttzd/w";
  81. count_right <= '1';
  82. for j in 0 to 100 loop
  83. r := pseudorand(64);
  84. r(0) := '1';
  85. for i in 0 to 63 loop
  86. rs <= r;
  87. is_32bit <= '0';
  88. wait for clk_period;
  89. assert to_integer(unsigned(result)) = i
  90. report "bad cnttzd " & to_hstring(rs) & " -> " & to_hstring(result);
  91. is_32bit <= '1';
  92. wait for clk_period;
  93. if i < 32 then
  94. assert to_integer(unsigned(result)) = i
  95. report "bad cnttzw " & to_hstring(rs) & " -> " & to_hstring(result);
  96. else
  97. assert to_integer(unsigned(result)) = 32
  98. report "bad cnttzw " & to_hstring(rs) & " -> " & to_hstring(result);
  99. end if;
  100. r := r(62 downto 0) & '0';
  101. end loop;
  102. end loop;
  103. std.env.finish;
  104. end process;
  105. end behave;