tb.vhd 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.std_logic_textio.all;
  5. library std; -- for Printing
  6. use std.textio.all;
  7. use work.mem_pkg.all;
  8. use work.op_pkg.all;
  9. use work.core_pkg.all;
  10. use work.tb_util_pkg.all;
  11. entity tb is
  12. end entity;
  13. architecture bench of tb is
  14. constant CLK_PERIOD : time := 10 ns;
  15. signal clk : std_logic;
  16. signal res_n : std_logic := '0';
  17. signal stop : boolean := false;
  18. signal start : std_logic := '0';
  19. file input_file : text;
  20. file output_ref_file : text;
  21. subtype addr is std_logic_vector(REG_BITS-1 downto 0);
  22. subtype data is std_logic_vector(DATA_WIDTH-1 downto 0);
  23. type data_arr is array(0 to 400) of MEM_DATA_TYPE;
  24. type input_t is
  25. record
  26. stall : std_logic;
  27. flush : std_logic;
  28. op : wb_op_type;
  29. aluresult : data_type;
  30. memresult : data_type;
  31. pc : pc_type;
  32. end record;
  33. type output_t is
  34. record
  35. reg_write : reg_write_type;
  36. end record;
  37. signal inp : input_t := (
  38. '0',
  39. '0',
  40. ((others => '0'), '0', WBS_ALU),
  41. (others => '0'),
  42. (others => '0'),
  43. (others => '0')
  44. );
  45. signal outp : output_t;
  46. signal reg_write : reg_write_type := (write => '0', reg => (others => '0'), data => (others => '0'));
  47. impure function read_next_input(file f : text) return input_t is
  48. variable l : line;
  49. variable result : input_t;
  50. begin
  51. l := get_next_valid_line(f);
  52. l := get_next_valid_line(f);
  53. result.stall := str_to_sl(l(1));
  54. l := get_next_valid_line(f);
  55. result.flush := str_to_sl(l(1));
  56. --wb_op
  57. l := get_next_valid_line(f);
  58. result.op.rd := bin_to_slv(l.all, REG_ADR_TYPE'LENGTH);
  59. l := get_next_valid_line(f);
  60. result.op.write := str_to_sl(l(1));
  61. l := get_next_valid_line(f);
  62. result.op.src := str_to_wbs_op(l.all);
  63. l := get_next_valid_line(f);
  64. result.aluresult := bin_to_slv(l.all, data_type'LENGTH);
  65. l := get_next_valid_line(f);
  66. result.memresult := bin_to_slv(l.all, data_type'LENGTH);
  67. l := get_next_valid_line(f);
  68. result.pc := bin_to_slv(l.all, pc_type'LENGTH);
  69. return result;
  70. end function;
  71. impure function read_next_output(file f : text) return output_t is
  72. variable l : line;
  73. variable result : output_t;
  74. begin
  75. l := get_next_valid_line(f);
  76. l := get_next_valid_line(f);
  77. result.reg_write.write := str_to_sl(l(1));
  78. l := get_next_valid_line(f);
  79. result.reg_write.reg := bin_to_slv(l.all, reg_adr_type'LENGTH);
  80. l := get_next_valid_line(f);
  81. result.reg_write.data := bin_to_slv(l.all, data_type'LENGTH);
  82. return result;
  83. end function;
  84. procedure check_output(output_ref : output_t) is
  85. variable passed : boolean;
  86. begin
  87. passed := (outp = output_ref);
  88. if passed then
  89. report " PASSED: "
  90. & "stall=" & to_string(inp.stall)
  91. & " flush=" & to_string(inp.flush)
  92. & " op.rd=" & to_string(inp.op.rd)
  93. & " op.write=" & to_string(inp.op.write)
  94. & " op.src=" & to_string(inp.op.src)
  95. & " aluresult=" & to_string(inp.aluresult)
  96. & " memresult=" & to_string(inp.memresult)
  97. & " pc=" & to_string(inp.pc)
  98. severity note;
  99. else
  100. report "FAILED: "
  101. & "stall=" & to_string(inp.stall)
  102. & " flush=" & to_string(inp.flush)
  103. & " op.rd=" & to_string(inp.op.rd)
  104. & " op.write=" & to_string(inp.op.write)
  105. & " op.src=" & to_string(inp.op.src)
  106. & " aluresult=" & to_string(inp.aluresult)
  107. & " memresult=" & to_string(inp.memresult)
  108. & " pc=" & to_string(inp.pc) & lf
  109. & "** expected: write= " & to_string(output_ref.reg_write.write) & " reg= " & to_string(output_ref.reg_write.reg) & " data= " & to_string(output_ref.reg_write.data) & lf
  110. & "** actual: write= " & to_string(outp.reg_write.write) & " reg= " & to_string(outp.reg_write.reg) & " data= " & to_string(outp.reg_write.data) & lf
  111. severity error;
  112. end if;
  113. end procedure;
  114. begin
  115. wb_inst : entity work.wb
  116. port map (
  117. clk => clk,
  118. res_n => res_n,
  119. stall => inp.stall,
  120. flush => inp.flush,
  121. op => inp.op,
  122. aluresult => inp.aluresult,
  123. memresult => inp.memresult,
  124. pc_old_in => inp.pc,
  125. reg_write => outp.reg_write
  126. );
  127. stimulus : process
  128. variable fstatus: file_open_status;
  129. begin
  130. res_n <= '0';
  131. wait until rising_edge(clk);
  132. res_n <= '1';
  133. file_open(fstatus, input_file, "testdata/input.txt", READ_MODE);
  134. wait until falling_edge(clk);
  135. wait for CLK_PERIOD/10;
  136. while not endfile(input_file) loop
  137. inp <= read_next_input(input_file);
  138. start <= '1';
  139. timeout(1, CLK_PERIOD);
  140. end loop;
  141. wait;
  142. end process;
  143. output_checker : process
  144. variable fstatus: file_open_status;
  145. variable output_ref : output_t;
  146. begin
  147. file_open(fstatus, output_ref_file, "testdata/output.txt", READ_MODE);
  148. wait until res_n = '1';
  149. wait until start = '1';
  150. while not endfile(output_ref_file) loop
  151. output_ref := read_next_output(output_ref_file);
  152. wait until falling_edge(clk);
  153. check_output(output_ref);
  154. wait until rising_edge(clk);
  155. end loop;
  156. stop <= true;
  157. wait;
  158. end process;
  159. generate_clk : process
  160. begin
  161. clk_generate(clk, CLK_PERIOD, stop);
  162. wait;
  163. end process;
  164. end architecture;