| 1 |
-- Copyright (c) 2013 Nuand LLC |
| 2 |
-- |
| 3 |
-- Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
-- of this software and associated documentation files (the "Software"), to deal |
| 5 |
-- in the Software without restriction, including without limitation the rights |
| 6 |
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
-- copies of the Software, and to permit persons to whom the Software is |
| 8 |
-- furnished to do so, subject to the following conditions: |
| 9 |
-- |
| 10 |
-- The above copyright notice and this permission notice shall be included in |
| 11 |
-- all copies or substantial portions of the Software. |
| 12 |
-- |
| 13 |
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
-- THE SOFTWARE. |
| 20 |
|
| 21 |
library ieee ; |
| 22 |
use ieee.std_logic_1164.all ; |
| 23 |
use ieee.numeric_std.all ; |
| 24 |
|
| 25 |
entity uart_tb is |
| 26 |
end entity ; -- uart_tb |
| 27 |
|
| 28 |
architecture arch of uart_tb is |
| 29 |
|
| 30 |
signal clock : std_logic := '1' ; |
| 31 |
signal reset : std_logic := '1' ; |
| 32 |
signal enable : std_logic := '0' ; |
| 33 |
|
| 34 |
signal rs232_rxd : std_logic ; |
| 35 |
signal rs232_txd : std_logic := '1' ; |
| 36 |
|
| 37 |
signal txd_we : std_logic ; |
| 38 |
signal txd_full : std_logic := '0' ; |
| 39 |
signal txd_data : std_logic_vector(7 downto 0) ; |
| 40 |
|
| 41 |
signal rxd_re : std_logic ; |
| 42 |
signal rxd_empty : std_logic := '1' ; |
| 43 |
signal rxd_data : std_logic_vector(7 downto 0) := (others =>'0') ; |
| 44 |
|
| 45 |
procedure nop( signal clock : in std_logic ; count : in natural ) is |
| 46 |
begin |
| 47 |
for i in 1 to count loop |
| 48 |
wait until rising_edge(clock) ; |
| 49 |
end loop ; |
| 50 |
end procedure ; |
| 51 |
|
| 52 |
procedure uart_send( signal clock : in std_logic ; signal txd : out std_logic ; data : in std_logic_vector(7 downto 0) ; cpb : in natural ) is |
| 53 |
begin |
| 54 |
wait until rising_edge(clock) ; |
| 55 |
-- Send start bit |
| 56 |
txd <= '0' ; |
| 57 |
nop( clock, cpb ) ; |
| 58 |
|
| 59 |
-- Send data |
| 60 |
for i in 0 to data'high loop |
| 61 |
txd <= data(i) ; |
| 62 |
nop( clock, cpb ) ; |
| 63 |
end loop ; |
| 64 |
|
| 65 |
-- Send stop bit |
| 66 |
txd <= '1' ; |
| 67 |
nop( clock, cpb ) ; |
| 68 |
end procedure ; |
| 69 |
|
| 70 |
begin |
| 71 |
|
| 72 |
clock <= not clock after 1 ns ; |
| 73 |
|
| 74 |
U_uart : entity work.uart |
| 75 |
port map ( |
| 76 |
clock => clock, |
| 77 |
reset => reset, |
| 78 |
enable => enable, |
| 79 |
|
| 80 |
rs232_rxd => rs232_rxd, |
| 81 |
rs232_txd => rs232_txd, |
| 82 |
|
| 83 |
txd_we => txd_we, |
| 84 |
txd_full => txd_full, |
| 85 |
txd_data => txd_data, |
| 86 |
|
| 87 |
rxd_re => rxd_re, |
| 88 |
rxd_empty => rxd_empty, |
| 89 |
rxd_data => rxd_data |
| 90 |
) ; |
| 91 |
|
| 92 |
stimulate : process |
| 93 |
begin |
| 94 |
-- Reset |
| 95 |
reset <= '1' ; |
| 96 |
nop( clock, 10 ) ; |
| 97 |
|
| 98 |
-- Done with reset ... |
| 99 |
reset <= '0' ; |
| 100 |
nop( clock, 10 ) ; |
| 101 |
|
| 102 |
-- Make sure we don't do anything when we aren't enabled ... |
| 103 |
enable <= '0' ; |
| 104 |
nop( clock, 10 ) ; |
| 105 |
|
| 106 |
-- ... and now lets go! |
| 107 |
enable <= '1' ; |
| 108 |
nop( clock, 100000 ) ; |
| 109 |
|
| 110 |
-- Ding! |
| 111 |
report "-- End of Simulation -- " severity failure ; |
| 112 |
|
| 113 |
end process ; |
| 114 |
|
| 115 |
-- Just keep sending data in from the UART |
| 116 |
simulate_uart_sending : process |
| 117 |
variable val : natural := 0 ; |
| 118 |
begin |
| 119 |
rs232_txd <= '1' ; |
| 120 |
wait until enable = '1' ; |
| 121 |
nop( clock, 100 ) ; |
| 122 |
while true loop |
| 123 |
uart_send( clock, rs232_txd, std_logic_vector(to_unsigned(val,8)), 10 ) ; |
| 124 |
val := (val + 1) mod 256 ; |
| 125 |
--nop( clock, 5 ) ; |
| 126 |
end loop ; |
| 127 |
end process ; |
| 128 |
|
| 129 |
-- Feedback data continuously |
| 130 |
simulate_s2u_fifo : process |
| 131 |
begin |
| 132 |
rxd_empty <= '1' ; |
| 133 |
wait until enable = '1' ; |
| 134 |
nop( clock, 100 ) ; |
| 135 |
rxd_empty <= '0' ; |
| 136 |
rxd_data <= (others =>'0') ; |
| 137 |
while true loop |
| 138 |
wait until rising_edge( clock ) and rxd_re = '1' ; |
| 139 |
rxd_data <= std_logic_vector(unsigned(rxd_data)+1) ; |
| 140 |
end loop ; |
| 141 |
end process ; |
| 142 |
|
| 143 |
end architecture ; -- arch |