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.

212 lines
6.7 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. constant c_parity : boolean := true;
  61. constant c_clk_div : natural := 10;
  62. signal s_reset_n : std_logic := '0';
  63. signal s_clk : std_logic := '1';
  64. signal s_tx_data : std_logic_vector(c_data_length-1 downto 0);
  65. signal s_tx_valid : std_logic;
  66. signal s_tx_accept : std_logic;
  67. signal s_rx_data : std_logic_vector(c_data_length-1 downto 0);
  68. signal s_rx_error : std_logic;
  69. signal s_rx_valid : std_logic;
  70. signal s_rx_accept : std_logic;
  71. signal s_tx_uart : std_logic := '1';
  72. signal s_rx_uart : std_logic := '1';
  73. signal s_error_inject : boolean := false;
  74. signal s_error_injected : boolean := false;
  75. procedure injectError (signal inject : out boolean) is
  76. variable v_injected : boolean;
  77. variable v_random : RandomPType;
  78. begin
  79. v_random.InitSeed(v_random'instance_name & to_string(now));
  80. loop
  81. v_injected := false;
  82. wait until s_tx_valid = '1' and s_tx_accept = '1';
  83. wait until falling_edge(s_tx_uart);
  84. -- Skip start bit
  85. for i in 0 to c_clk_div-1 loop
  86. wait until rising_edge(s_clk);
  87. end loop;
  88. -- Possibly distort one of the data bits
  89. for i in 0 to c_data_length-1 loop
  90. if (not v_injected and v_random.DistValInt(((0, 9), (1, 1))) = 1) then
  91. v_injected := true;
  92. inject <= true;
  93. end if;
  94. for y in 0 to c_clk_div-1 loop
  95. wait until rising_edge(s_clk);
  96. end loop;
  97. inject <= false;
  98. end loop;
  99. end loop;
  100. wait;
  101. end procedure injectError;
  102. begin
  103. Dut_UartTx : UartTx
  104. generic map (
  105. DATA_LENGTH => c_data_length,
  106. PARITY => c_parity,
  107. CLK_DIV => c_clk_div
  108. )
  109. port map (
  110. reset_n_i => s_reset_n,
  111. clk_i => s_clk,
  112. data_i => s_tx_data,
  113. valid_i => s_tx_valid,
  114. accept_o => s_tx_accept,
  115. tx_o => s_tx_uart
  116. );
  117. -- Error injection based on random
  118. injectError(s_error_inject);
  119. s_rx_uart <= s_tx_uart when not s_error_inject else not(s_tx_uart);
  120. Dut_UartRx : UartRx
  121. generic map (
  122. DATA_LENGTH => c_data_length,
  123. PARITY => c_parity,
  124. CLK_DIV => c_clk_div
  125. )
  126. port map (
  127. reset_n_i => s_reset_n,
  128. clk_i => s_clk,
  129. data_o => s_rx_data,
  130. error_o => s_rx_error,
  131. valid_o => s_rx_valid,
  132. accept_i => s_rx_accept,
  133. rx_i => s_rx_uart
  134. );
  135. s_clk <= not s_clk after 5 ns;
  136. s_reset_n <= '1' after 20 ns;
  137. -- Store if an error was injected in the current frame
  138. s_error_injected <= true when rising_edge(s_clk) and s_error_inject else
  139. false when s_tx_valid = '1';
  140. TestP : process is
  141. variable v_data : std_logic_vector(c_data_length-1 downto 0);
  142. variable v_error : boolean := false;
  143. variable v_random : RandomPType;
  144. begin
  145. v_random.InitSeed(v_random'instance_name);
  146. s_tx_valid <= '0';
  147. s_rx_accept <= '0';
  148. s_tx_data <= (others => '0');
  149. wait until s_reset_n = '1';
  150. for i in 0 to 2**c_data_length-1 loop
  151. wait until rising_edge(s_clk);
  152. s_tx_valid <= '1';
  153. s_rx_accept <= '1';
  154. v_data := v_random.RandSlv(8);
  155. s_tx_data <= v_data;
  156. wait until rising_edge(s_clk) and s_tx_accept = '1';
  157. s_tx_valid <= '0';
  158. wait until rising_edge(s_clk) and s_rx_valid = '1';
  159. if s_error_injected then
  160. assert s_rx_data /= v_data
  161. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  162. severity failure;
  163. assert s_rx_error = '1'
  164. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b1"
  165. severity failure;
  166. else
  167. assert s_rx_data = v_data
  168. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  169. severity failure;
  170. assert s_rx_error = '0'
  171. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b0"
  172. severity failure;
  173. end if;
  174. end loop;
  175. wait for 10 us;
  176. stop(0);
  177. end process TestP;
  178. end architecture sim;