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.

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