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.

214 lines
6.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. 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. report "Injected transmit error on bit #" & to_string(i);
  94. end if;
  95. for y in 0 to c_clk_div-1 loop
  96. wait until rising_edge(s_clk);
  97. end loop;
  98. inject <= false;
  99. end loop;
  100. end loop;
  101. wait;
  102. end procedure injectError;
  103. begin
  104. Dut_UartTx : UartTx
  105. generic map (
  106. DATA_LENGTH => c_data_length,
  107. PARITY => c_parity,
  108. CLK_DIV => c_clk_div
  109. )
  110. port map (
  111. reset_n_i => s_reset_n,
  112. clk_i => s_clk,
  113. data_i => s_tx_data,
  114. valid_i => s_tx_valid,
  115. accept_o => s_tx_accept,
  116. tx_o => s_tx_uart
  117. );
  118. -- Error injection based on random
  119. injectError(s_error_inject);
  120. s_rx_uart <= s_tx_uart when not s_error_inject else not(s_tx_uart);
  121. Dut_UartRx : UartRx
  122. generic map (
  123. DATA_LENGTH => c_data_length,
  124. PARITY => c_parity,
  125. CLK_DIV => c_clk_div
  126. )
  127. port map (
  128. reset_n_i => s_reset_n,
  129. clk_i => s_clk,
  130. data_o => s_rx_data,
  131. error_o => s_rx_error,
  132. valid_o => s_rx_valid,
  133. accept_i => s_rx_accept,
  134. rx_i => s_rx_uart
  135. );
  136. s_clk <= not s_clk after 5 ns;
  137. s_reset_n <= '1' after 20 ns;
  138. -- Store if an error was injected in the current frame
  139. s_error_injected <= true when rising_edge(s_clk) and s_error_inject else
  140. false when s_tx_valid = '1';
  141. TestP : process is
  142. variable v_data : std_logic_vector(c_data_length-1 downto 0);
  143. variable v_error : boolean := false;
  144. variable v_random : RandomPType;
  145. begin
  146. v_random.InitSeed(v_random'instance_name);
  147. s_tx_valid <= '0';
  148. s_rx_accept <= '0';
  149. s_tx_data <= (others => '0');
  150. wait until s_reset_n = '1';
  151. for i in 0 to 2**c_data_length-1 loop
  152. wait until rising_edge(s_clk);
  153. s_tx_valid <= '1';
  154. s_rx_accept <= '1';
  155. v_data := v_random.RandSlv(8);
  156. s_tx_data <= v_data;
  157. report "Testcase #" & to_string(i) & ": Transmit 0x" & to_hstring(v_data);
  158. wait until rising_edge(s_clk) and s_tx_accept = '1';
  159. s_tx_valid <= '0';
  160. wait until rising_edge(s_clk) and s_rx_valid = '1';
  161. if s_error_injected then
  162. assert s_rx_data /= v_data
  163. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  164. severity failure;
  165. assert s_rx_error = '1'
  166. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b1"
  167. severity failure;
  168. else
  169. assert s_rx_data = v_data
  170. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  171. severity failure;
  172. assert s_rx_error = '0'
  173. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b0"
  174. severity failure;
  175. end if;
  176. end loop;
  177. wait for 10 us;
  178. stop(0);
  179. end process TestP;
  180. end architecture sim;