-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_tb.vhd
102 lines (82 loc) · 3.4 KB
/
uart_tb.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity uart_tb is
end entity uart_tb;
architecture simulate_uart of uart_tb is
-- Signals for the testbench
signal transmit_data : STD_LOGIC := '0'; -- Initial value for transmit_data
signal clk : STD_LOGIC := '0'; -- Initial value for clock
signal data_in: STD_LOGIC_VECTOR(7 downto 0); -- Data input signal
signal tx: STD_LOGIC; -- Transmit signal
-- constants
constant T : time := 10 ns; -- clock period (10ns clock period gives 100MHz frequency)
constant clock_frequency : integer := 100_000_000;
constant baud_rate : integer := 9600; -- baud rate is number of data bits to send per second
constant single_bit_period : time := 1 sec / baud_rate; -- Time required to transmit one-bit
constant ten_bits_period : time := 10 * single_bit_period; -- Time required to transmit ten-bits
begin
-- Declare and instantiate the UART module
uut: entity work.uart
generic map (
baud_rate => baud_rate,
clock_frequency => clock_frequency
)
port map (
transmit_data => transmit_data,
clk => clk,
data_in => data_in,
tx => tx
);
-- Generate a clock of 100MHz
process
begin
clk <= '0';
wait for T/2;
clk <= '1';
wait for T/2;
end process;
-- Stimulus process
-- Transmit the word "Hello" character by character
process
begin
-- Keep waiting in the beginning and do not send any data
transmit_data <= '0'; -- Do not transmit data
wait for single_bit_period;
-- Send first character of "Hello"
transmit_data <= '1'; -- Start transmission
data_in <= "01001000"; -- ASCII code for 'H'
wait for ten_bits_period; -- Wait until all 10-bits have been transferred
-- Stop transmission and wait some time before sending other characters
transmit_data <= '0'; -- Stop transmission
wait for 2 * single_bit_period;
-- Send second character
transmit_data <= '1'; -- Resume transmission
data_in <= "01100101"; -- ASCII code for 'e'
wait for ten_bits_period;
-- Stop transmission and wait some time
transmit_data <= '0'; -- Stop transmission
wait for 3 * single_bit_period;
-- Send third character
transmit_data <= '1'; -- Resume transmission
data_in <= "01101100"; -- ASCII code for 'l'
wait for ten_bits_period;
-- Stop transmission and wait some time
transmit_data <= '0'; -- Stop transmission
wait for single_bit_period;
-- Send fourth character
transmit_data <= '1'; -- Resume transmission
data_in <= "01101100"; -- ASCII code for 'l'
wait for ten_bits_period;
-- Stop transmission and wait some time
transmit_data <= '0'; -- Stop transmission
wait for 2 * single_bit_period;
-- Send fifth character
transmit_data <= '1'; -- Resume transmission
data_in <= "01101111"; -- ASCII code for 'o'
wait for ten_bits_period;
-- Stop transmission
transmit_data <= '0';
wait; -- End simulation
end process;
end simulate_uart;