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.

223 lines
7.2 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 osvvm.CoveragePkg.all;
  25. use std.env.all;
  26. entity UartT is
  27. end entity UartT;
  28. architecture sim of UartT is
  29. component UartTx is
  30. generic (
  31. DATA_LENGTH : positive range 5 to 9 := 8;
  32. PARITY : boolean := false;
  33. CLK_DIV : natural := 10
  34. );
  35. port (
  36. reset_n_i : in std_logic; -- async reset
  37. clk_i : in std_logic; -- clock
  38. data_i : in std_logic_vector(DATA_LENGTH-1 downto 0); -- data input
  39. valid_i : in std_logic; -- input data valid
  40. accept_o : out std_logic; -- inpit data accepted
  41. tx_o : out std_logic -- uart tx data output
  42. );
  43. end component UartTx;
  44. component UartRx is
  45. generic (
  46. DATA_LENGTH : positive range 5 to 9 := 8;
  47. PARITY : boolean := true;
  48. CLK_DIV : natural := 10
  49. );
  50. port (
  51. reset_n_i : in std_logic; -- async reset
  52. clk_i : in std_logic; -- clock
  53. data_o : out std_logic_vector(DATA_LENGTH-1 downto 0); -- data output
  54. error_o : out std_logic; -- rx error
  55. valid_o : out std_logic; -- output data valid
  56. accept_i : in std_logic; -- output data accepted
  57. rx_i : in std_logic -- uart rx input
  58. );
  59. end component UartRx;
  60. constant c_data_length : positive range 5 to 9 := 8;
  61. constant c_parity : boolean := true;
  62. constant c_clk_div : natural := 10;
  63. signal s_reset_n : std_logic := '0';
  64. signal s_clk : std_logic := '1';
  65. signal s_tx_data : std_logic_vector(c_data_length-1 downto 0);
  66. signal s_tx_valid : std_logic;
  67. signal s_tx_accept : std_logic;
  68. signal s_rx_data : std_logic_vector(c_data_length-1 downto 0);
  69. signal s_rx_error : std_logic;
  70. signal s_rx_valid : std_logic;
  71. signal s_rx_accept : std_logic;
  72. signal s_tx_uart : std_logic := '1';
  73. signal s_rx_uart : std_logic := '1';
  74. signal s_error_inject : boolean := false;
  75. signal s_error_injected : boolean := false;
  76. shared variable sv_uart_err_coverage : CovPType;
  77. procedure injectError (signal inject : out boolean) is
  78. variable v_injected : boolean;
  79. variable v_random : RandomPType;
  80. begin
  81. v_random.InitSeed(v_random'instance_name & to_string(now));
  82. loop
  83. -- Wait for new UART transmission
  84. v_injected := false;
  85. wait until s_tx_valid = '1' and s_tx_accept = '1';
  86. wait until falling_edge(s_tx_uart);
  87. -- Skip start bit
  88. for i in 0 to c_clk_div-1 loop
  89. wait until rising_edge(s_clk);
  90. end loop;
  91. -- Possibly distort one of the data bits
  92. -- and update coverage object
  93. for i in 0 to c_data_length-1 loop
  94. if (not v_injected and v_random.DistValInt(((0, 9), (1, 1))) = 1) then
  95. v_injected := true;
  96. inject <= true;
  97. sv_uart_err_coverage.ICover(i);
  98. report "Injected transmit error on bit #" & to_string(i);
  99. end if;
  100. for y in 0 to c_clk_div-1 loop
  101. wait until rising_edge(s_clk);
  102. end loop;
  103. inject <= false;
  104. end loop;
  105. end loop;
  106. wait;
  107. end procedure injectError;
  108. begin
  109. Dut_UartTx : UartTx
  110. generic map (
  111. DATA_LENGTH => c_data_length,
  112. PARITY => c_parity,
  113. CLK_DIV => c_clk_div
  114. )
  115. port map (
  116. reset_n_i => s_reset_n,
  117. clk_i => s_clk,
  118. data_i => s_tx_data,
  119. valid_i => s_tx_valid,
  120. accept_o => s_tx_accept,
  121. tx_o => s_tx_uart
  122. );
  123. -- Error injection based on random
  124. sv_uart_err_coverage.AddBins(GenBin(0, c_data_length-1));
  125. injectError(s_error_inject);
  126. s_rx_uart <= s_tx_uart when not s_error_inject else not(s_tx_uart);
  127. Dut_UartRx : UartRx
  128. generic map (
  129. DATA_LENGTH => c_data_length,
  130. PARITY => c_parity,
  131. CLK_DIV => c_clk_div
  132. )
  133. port map (
  134. reset_n_i => s_reset_n,
  135. clk_i => s_clk,
  136. data_o => s_rx_data,
  137. error_o => s_rx_error,
  138. valid_o => s_rx_valid,
  139. accept_i => s_rx_accept,
  140. rx_i => s_rx_uart
  141. );
  142. s_clk <= not s_clk after 5 ns;
  143. s_reset_n <= '1' after 20 ns;
  144. -- Store if an error was injected in the current frame
  145. s_error_injected <= true when rising_edge(s_clk) and s_error_inject else
  146. false when s_tx_valid = '1';
  147. TestP : process is
  148. variable v_data : std_logic_vector(c_data_length-1 downto 0);
  149. variable v_error : boolean := false;
  150. variable v_random : RandomPType;
  151. begin
  152. v_random.InitSeed(v_random'instance_name);
  153. s_tx_valid <= '0';
  154. s_rx_accept <= '0';
  155. s_tx_data <= (others => '0');
  156. wait until s_reset_n = '1';
  157. for i in 0 to 2**c_data_length-1 loop
  158. wait until rising_edge(s_clk);
  159. s_tx_valid <= '1';
  160. s_rx_accept <= '1';
  161. v_data := v_random.RandSlv(8);
  162. s_tx_data <= v_data;
  163. report "Testcase #" & to_string(i) & ": Transmit 0x" & to_hstring(v_data);
  164. wait until rising_edge(s_clk) and s_tx_accept = '1';
  165. s_tx_valid <= '0';
  166. wait until rising_edge(s_clk) and s_rx_valid = '1';
  167. if s_error_injected then
  168. assert s_rx_data /= v_data
  169. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  170. severity failure;
  171. assert s_rx_error = '1'
  172. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b1"
  173. severity failure;
  174. else
  175. assert s_rx_data = v_data
  176. report "Received data 0x" & to_hstring(s_rx_data) & ", expected 0x" & to_hstring(v_data)
  177. severity failure;
  178. assert s_rx_error = '0'
  179. report "Received error 0b" & to_string(s_rx_error) & ", expected 0b0"
  180. severity failure;
  181. end if;
  182. end loop;
  183. wait for 10 us;
  184. sv_uart_err_coverage.SetMessage("UART bit error coverage");
  185. sv_uart_err_coverage.WriteBin;
  186. finish(0);
  187. end process TestP;
  188. end architecture sim;