tb.vhd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. file input_file : text;
  19. file output_ref_file : text;
  20. subtype addr is std_logic_vector(REG_BITS-1 downto 0);
  21. subtype data is std_logic_vector(DATA_WIDTH-1 downto 0);
  22. type data_arr is array(0 to 400) of MEM_DATA_TYPE;
  23. type input_t is
  24. record
  25. stall : std_logic;
  26. flush : std_logic;
  27. pc_src : std_logic;
  28. pc_in : PC_TYPE;
  29. end record;
  30. type output_t is
  31. record
  32. pc_out : PC_TYPE;
  33. instr : INSTR_TYPE;
  34. end record;
  35. signal inp : input_t := (
  36. '0',
  37. '0',
  38. '0',
  39. (others => '0')
  40. );
  41. signal outp : output_t;
  42. signal mem : data_arr;
  43. signal mem_busy : std_logic;
  44. signal memout : MEM_OUT_TYPE;
  45. signal memin : MEM_IN_TYPE := (busy => '0', others => (others =>'0'));
  46. impure function read_next_input(file f : text) return input_t is
  47. variable l : line;
  48. variable result : input_t;
  49. begin
  50. l := get_next_valid_line(f);
  51. l := get_next_valid_line(f);
  52. result.stall := str_to_sl(l(1));
  53. l := get_next_valid_line(f);
  54. result.flush := str_to_sl(l(1));
  55. l := get_next_valid_line(f);
  56. result.pc_src := str_to_sl(l(1));
  57. l := get_next_valid_line(f);
  58. result.pc_in := bin_to_slv(l.all, PC_TYPE'LENGTH);
  59. return result;
  60. end function;
  61. impure function read_next_output(file f : text) return output_t is
  62. variable l : line;
  63. variable result : output_t;
  64. begin
  65. l := get_next_valid_line(f);
  66. l := get_next_valid_line(f);
  67. result.pc_out := bin_to_slv(l.all, PC_TYPE'LENGTH);
  68. l := get_next_valid_line(f);
  69. result.instr := bin_to_slv(l.all, INSTR_TYPE'LENGTH);
  70. return result;
  71. end function;
  72. procedure check_output(output_ref : output_t) is
  73. variable passed : boolean;
  74. begin
  75. passed := (outp = output_ref);
  76. if passed then
  77. report " PASSED: "
  78. & "stall=" & to_string(inp.stall)
  79. & " flush=" & to_string(inp.flush)
  80. & " pc_src=" & to_string(inp.pc_src)
  81. & " pc_in=" & to_string(inp.pc_in)
  82. severity note;
  83. else
  84. report "FAILED: "
  85. & "stall=" & to_string(inp.stall)
  86. & " flush=" & to_string(inp.flush)
  87. & " pc_src=" & to_string(inp.pc_src)
  88. & " pc_in=" & to_string(inp.pc_in) & lf
  89. & "** expected: pc_out=" & to_string(output_ref.pc_out) & " instr=" & to_string(output_ref.instr) & lf
  90. & "** actual: pc_out=" & to_string(outp.pc_out) & " instr=" & to_string(outp.instr) & lf
  91. severity error;
  92. end if;
  93. end procedure;
  94. begin
  95. fetch_inst : entity work.fetch
  96. port map (
  97. clk => clk,
  98. res_n => res_n,
  99. stall => inp.stall,
  100. flush => inp.flush,
  101. mem_busy => mem_busy,
  102. pcsrc => inp.pc_src,
  103. pc_in => inp.pc_in,
  104. pc_out => outp.pc_out,
  105. instr => outp.instr,
  106. mem_out => memout,
  107. mem_in => memin
  108. );
  109. memcntrl : process(clk)
  110. begin
  111. if rising_edge(clk) then
  112. memin.rddata <= mem(to_integer(unsigned(memout.address)));
  113. end if;
  114. end process;
  115. stimulus : process
  116. variable fstatus: file_open_status;
  117. begin
  118. for i in 0 to 100 loop
  119. mem(i) <= std_logic_vector(to_unsigned(i, instr_type'LENGTH));
  120. end loop;
  121. res_n <= '0';
  122. wait until rising_edge(clk);
  123. res_n <= '1';
  124. file_open(fstatus, input_file, "testdata/input.txt", READ_MODE);
  125. while not endfile(input_file) loop
  126. inp <= read_next_input(input_file);
  127. timeout(1, CLK_PERIOD);
  128. end loop;
  129. wait;
  130. end process;
  131. output_checker : process
  132. variable fstatus: file_open_status;
  133. variable output_ref : output_t;
  134. begin
  135. file_open(fstatus, output_ref_file, "testdata/output.txt", READ_MODE);
  136. wait until res_n = '1';
  137. while not endfile(output_ref_file) loop
  138. output_ref := read_next_output(output_ref_file);
  139. wait until falling_edge(clk);
  140. check_output(output_ref);
  141. wait until rising_edge(clk);
  142. end loop;
  143. stop <= true;
  144. wait;
  145. end process;
  146. generate_clk : process
  147. begin
  148. clk_generate(clk, CLK_PERIOD, stop);
  149. wait;
  150. end process;
  151. end architecture;