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.

80 lines
1.9 KiB

  1. library ieee ;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use std.env.all;
  5. entity tb_uart_reg is
  6. end entity tb_uart_reg;
  7. architecture sim of tb_uart_reg is
  8. signal s_clk : std_logic := '1';
  9. signal s_rst_n : std_logic := '0';
  10. signal s_uart_rx : std_logic := '1';
  11. signal s_uart_tx : std_logic;
  12. constant c_baudrate : natural := 9600;
  13. constant c_period_ns : time := 1000000000 / c_baudrate * ns;
  14. begin
  15. dut : entity work.uart_reg
  16. port map (
  17. clk_i => s_clk,
  18. rst_n_i => s_rst_n,
  19. uart_rx_i => s_uart_rx,
  20. uart_tx_o => s_uart_tx
  21. );
  22. s_rst_n <= '1' after 120 ns;
  23. s_clk <= not s_clk after 50 ns;
  24. SendP : process is
  25. variable v_data : std_logic_vector(7 downto 0);
  26. begin
  27. wait until s_rst_n;
  28. wait until rising_edge(s_clk);
  29. wait for 200 us;
  30. for tx in 0 to 255 loop
  31. v_data := std_logic_vector(to_unsigned(tx, 8));
  32. report "UART send: 0x" & to_hstring(v_data);
  33. s_uart_rx <= '0';
  34. wait for c_period_ns;
  35. for i in 0 to 7 loop
  36. s_uart_rx <= v_data(i);
  37. wait for c_period_ns;
  38. end loop;
  39. s_uart_rx <= '1';
  40. wait for c_period_ns;
  41. end loop;
  42. wait;
  43. end process;
  44. ReceiveP : process is
  45. variable v_data : std_logic_vector(7 downto 0);
  46. begin
  47. wait until s_rst_n;
  48. wait until rising_edge(s_clk);
  49. for rx in 0 to 255 loop
  50. wait until not s_uart_tx;
  51. wait for c_period_ns; -- Skip start bit
  52. wait for c_period_ns/2;
  53. for i in 0 to 7 loop
  54. v_data(i) := s_uart_tx;
  55. wait for c_period_ns;
  56. end loop;
  57. report "UART recv: 0x" & to_hstring(v_data);
  58. assert v_data = std_logic_vector(to_unsigned(rx, 8))
  59. report "UART receive error, got 0x" & to_hstring(v_data) & ", expected 0x" & to_hstring(v_data)
  60. severity failure;
  61. end loop;
  62. wait for 200 us;
  63. report "Simulation finished :-)";
  64. stop(0);
  65. end process;
  66. end architecture;