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.

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