Library of reusable VHDL components
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.

158 lines
4.9 KiB

  1. -- ======================================================================
  2. -- UART testbench
  3. -- Copyright (C) 2020 Torsten Meissner
  4. -------------------------------------------------------------------------
  5. -- This program is free software; you can redistribute it and/or
  6. -- modify it under the terms of the GNU Lesser General Public
  7. -- License as published by the Free Software Foundation; either
  8. -- version 3 of the License, or (at your option) any later version.
  9. --
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. -- Lesser General Public License for more details.
  14. --
  15. -- You should have received a copy of the GNU Lesser General Public License
  16. -- along with this program; if not, write to the Free Software Foundation,
  17. -- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. -- ======================================================================
  19. library ieee;
  20. use ieee.std_logic_1164.all;
  21. use ieee.numeric_std.all;
  22. library osvvm;
  23. use osvvm.RandomPkg.all;
  24. use std.env.all;
  25. entity UartT is
  26. end entity UartT;
  27. architecture sim of UartT is
  28. component UartTx is
  29. generic (
  30. DATA_LENGTH : positive range 5 to 9 := 8;
  31. PARITY : boolean := false;
  32. CLK_DIV : natural := 10
  33. );
  34. port (
  35. reset_n_i : in std_logic; -- async reset
  36. clk_i : in std_logic; -- clock
  37. data_i : in std_logic_vector(DATA_LENGTH-1 downto 0); -- data input
  38. valid_i : in std_logic; -- input data valid
  39. accept_o : out std_logic; -- inpit data accepted
  40. tx_o : out std_logic -- uart tx data output
  41. );
  42. end component UartTx;
  43. component UartRx is
  44. generic (
  45. DATA_LENGTH : positive range 5 to 9 := 8;
  46. PARITY : boolean := true;
  47. CLK_DIV : natural := 10
  48. );
  49. port (
  50. reset_n_i : in std_logic; -- async reset
  51. clk_i : in std_logic; -- clock
  52. data_o : out std_logic_vector(DATA_LENGTH-1 downto 0); -- data output
  53. error_o : out std_logic; -- rx error
  54. valid_o : out std_logic; -- output data valid
  55. accept_i : in std_logic; -- output data accepted
  56. rx_i : in std_logic -- uart rx input
  57. );
  58. end component UartRx;
  59. constant c_data_length : positive range 5 to 8 := 8;
  60. signal s_reset_n : std_logic := '0';
  61. signal s_clk : std_logic := '1';
  62. signal s_tx_data : std_logic_vector(c_data_length-1 downto 0);
  63. signal s_tx_valid : std_logic;
  64. signal s_tx_accept : std_logic;
  65. signal s_rx_data : std_logic_vector(c_data_length-1 downto 0);
  66. signal s_rx_error : std_logic;
  67. signal s_rx_valid : std_logic;
  68. signal s_rx_accept : std_logic;
  69. signal s_uart : std_logic;
  70. begin
  71. Dut_UartTx : UartTx
  72. generic map (
  73. DATA_LENGTH => c_data_length,
  74. PARITY => true,
  75. CLK_DIV => 10
  76. )
  77. port map (
  78. reset_n_i => s_reset_n,
  79. clk_i => s_clk,
  80. data_i => s_tx_data,
  81. valid_i => s_tx_valid,
  82. accept_o => s_tx_accept,
  83. tx_o => s_uart
  84. );
  85. Dut_UartRx : UartRx
  86. generic map (
  87. DATA_LENGTH => c_data_length,
  88. PARITY => true,
  89. CLK_DIV => 10
  90. )
  91. port map (
  92. reset_n_i => s_reset_n,
  93. clk_i => s_clk,
  94. data_o => s_rx_data,
  95. error_o => s_rx_error,
  96. valid_o => s_rx_valid,
  97. accept_i => s_rx_accept,
  98. rx_i => s_uart
  99. );
  100. s_clk <= not s_clk after 5 ns;
  101. s_reset_n <= '1' after 20 ns;
  102. TestP : process is
  103. variable v_data : std_logic_vector(c_data_length-1 downto 0);
  104. variable v_random : RandomPType;
  105. begin
  106. v_random.InitSeed(v_random'instance_name);
  107. s_tx_valid <= '0';
  108. s_rx_accept <= '0';
  109. s_tx_data <= (others => '0');
  110. wait until s_reset_n = '1';
  111. for i in 0 to 2**c_data_length-1 loop
  112. wait until rising_edge(s_clk);
  113. s_tx_valid <= '1';
  114. s_rx_accept <= '1';
  115. v_data := v_random.RandSlv(8);
  116. s_tx_data <= v_data;
  117. wait until rising_edge(s_clk) and s_tx_accept = '1';
  118. s_tx_valid <= '0';
  119. wait until rising_edge(s_clk) and s_rx_valid = '1';
  120. assert s_rx_data = v_data
  121. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  122. severity failure;
  123. assert s_rx_error = '0'
  124. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b0"
  125. severity failure;
  126. end loop;
  127. wait for 10 us;
  128. stop(0);
  129. end process TestP;
  130. end architecture sim;