1+ -- The MIT License (MIT)
2+ --
3+ -- Copyright (c) 2015 Jakub Cabal
4+ --
5+ -- Permission is hereby granted, free of charge, to any person obtaining a copy
6+ -- of this software and associated documentation files (the "Software"), to deal
7+ -- in the Software without restriction, including without limitation the rights
8+ -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ -- copies of the Software, and to permit persons to whom the Software is
10+ -- furnished to do so, subject to the following conditions:
11+ --
12+ -- The above copyright notice and this permission notice shall be included in
13+ -- all copies or substantial portions of the Software.
14+ --
15+ -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ -- SOFTWARE.
22+ --------------------------------------------------------------------------------
23+
24+ library IEEE;
25+ use IEEE.STD_LOGIC_1164.ALL ;
26+ use IEEE.STD_LOGIC_UNSIGNED.ALL ;
27+
28+ entity UART_TESTBENCH is
29+ end UART_TESTBENCH;
30+
31+ architecture FULL of UART_TESTBENCH is
32+
33+ signal CLK : std_logic := '0' ;
34+ signal RST : std_logic := '0' ;
35+ signal tx_uart : std_logic ;
36+ signal rx_uart : std_logic := '1' ;
37+ signal tx_valid : std_logic ;
38+ signal tx_data : std_logic_vector (7 downto 0 );
39+ signal rx_valid : std_logic ;
40+ signal rx_ready : std_logic ;
41+ signal rx_data : std_logic_vector (7 downto 0 );
42+
43+ constant clk_period : time := 20 ns ;
44+ constant uart_period : time := 8696 ns ;
45+ constant data_value : std_logic_vector (7 downto 0 ) := "10100111" ;
46+
47+ begin
48+
49+ utt: entity work.UART
50+ generic map (
51+ BAUD_RATE => 115200 , -- baud rate value, default is 115200
52+ DATA_BITS => 8 , -- legal values: 5,6,7,8, default is 8 dat bits
53+ CLK_FREQ => 50e6 -- set system clock frequency in Hz, default is 50 MHz
54+ )
55+ port map (
56+ CLK => CLK, -- system clock
57+ RST => RST, -- high active synchronous reset
58+ -- UART INTERFACE
59+ TX_UART => tx_uart,
60+ RX_UART => rx_uart,
61+ -- USER TX INTERFACE
62+ TX_DATA => tx_data,
63+ TX_VALID => tx_valid, -- when TX_VALID = 1, data on TX_DATA are valid
64+ -- USER RX INTERFACE
65+ RX_DATA => rx_data,
66+ RX_VALID => rx_valid, -- when RX_VALID = 1, data on RX_DATA are valid
67+ RX_READY => rx_ready -- when RX_READY = 1, you can set RX_VALID to 1
68+ );
69+
70+ clk_process : process
71+ begin
72+ CLK <= '0' ;
73+ wait for clk_period/ 2 ;
74+ CLK <= '1' ;
75+ wait for clk_period/ 2 ;
76+ end process ;
77+
78+ test_rx_uart : process
79+ begin
80+ rx_uart <= '1' ;
81+ RST <= '1' ;
82+ wait for 100 ns ;
83+ RST <= '0' ;
84+
85+ wait for uart_period;
86+
87+ rx_uart <= '0' ; -- start bit
88+ wait for uart_period;
89+
90+ for i in 0 to 7 loop
91+ rx_uart <= data_value(i); -- data bits
92+ wait for uart_period;
93+ end loop ;
94+
95+ rx_uart <= '1' ; -- stop bit
96+ wait for uart_period;
97+
98+ wait ;
99+
100+ end process ;
101+
102+ test_tx_uart : process
103+ begin
104+ rx_valid <= '0' ;
105+ RST <= '1' ;
106+ wait for 100 ns ;
107+ RST <= '0' ;
108+
109+ wait until rising_edge (CLK);
110+
111+ rx_valid <= '1' ;
112+ rx_data <= data_value;
113+
114+ wait until rising_edge (CLK);
115+
116+ rx_valid <= '0' ;
117+
118+ wait until rising_edge (CLK);
119+
120+ wait ;
121+
122+ end process ;
123+
124+ end FULL;
0 commit comments