| 1 |
2 |
hasw |
-- UART transactor
|
| 2 |
|
|
--
|
| 3 |
|
|
-- Author: Sebastian Witt
|
| 4 |
|
|
-- Date: 03.02.2008
|
| 5 |
|
|
-- Version: 1.0
|
| 6 |
|
|
--
|
| 7 |
|
|
-- This code is free software; you can redistribute it and/or
|
| 8 |
|
|
-- modify it under the terms of the GNU Lesser General Public
|
| 9 |
|
|
-- License as published by the Free Software Foundation; either
|
| 10 |
|
|
-- version 2.1 of the License, or (at your option) any later version.
|
| 11 |
|
|
--
|
| 12 |
|
|
-- This code is distributed in the hope that it will be useful,
|
| 13 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 15 |
|
|
-- Lesser General Public License for more details.
|
| 16 |
|
|
--
|
| 17 |
|
|
-- You should have received a copy of the GNU Lesser General Public
|
| 18 |
|
|
-- License along with this library; if not, write to the
|
| 19 |
|
|
-- Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
| 20 |
|
|
-- Boston, MA 02111-1307 USA
|
| 21 |
|
|
--
|
| 22 |
|
|
|
| 23 |
|
|
LIBRARY IEEE;
|
| 24 |
|
|
USE IEEE.std_logic_1164.all;
|
| 25 |
|
|
|
| 26 |
|
|
use std.textio.all;
|
| 27 |
|
|
use work.uart_package.all;
|
| 28 |
|
|
use work.txt_util.all;
|
| 29 |
|
|
|
| 30 |
|
|
entity uart_transactor is
|
| 31 |
|
|
generic (
|
| 32 |
25 |
hasw |
stim_file : string := "sim/uart_stim.dat"; -- Stimulus input file
|
| 33 |
|
|
log_file : string := "sim/uart_log.txt" -- Log file
|
| 34 |
2 |
hasw |
);
|
| 35 |
|
|
end uart_transactor;
|
| 36 |
|
|
|
| 37 |
|
|
architecture tb of uart_transactor is
|
| 38 |
|
|
file stimulus : TEXT open read_mode is stim_file; -- Open stimulus file for read
|
| 39 |
|
|
file log : TEXT open write_mode is log_file; -- Open log file for write
|
| 40 |
|
|
|
| 41 |
|
|
-- The DUT
|
| 42 |
|
|
component uart_16750 is
|
| 43 |
|
|
port (
|
| 44 |
|
|
CLK : in std_logic; -- Clock
|
| 45 |
|
|
RST : in std_logic; -- Reset
|
| 46 |
|
|
BAUDCE : in std_logic; -- Baudrate generator clock enable
|
| 47 |
|
|
CS : in std_logic; -- Chip select
|
| 48 |
|
|
WR : in std_logic; -- Write to UART
|
| 49 |
|
|
RD : in std_logic; -- Read from UART
|
| 50 |
|
|
A : in std_logic_vector(2 downto 0); -- Register select
|
| 51 |
|
|
DIN : in std_logic_vector(7 downto 0); -- Data bus input
|
| 52 |
|
|
DOUT : out std_logic_vector(7 downto 0); -- Data bus output
|
| 53 |
|
|
DDIS : out std_logic; -- Driver disable
|
| 54 |
|
|
INT : out std_logic; -- Interrupt output
|
| 55 |
|
|
OUT1N : out std_logic; -- Output 1
|
| 56 |
|
|
OUT2N : out std_logic; -- Output 2
|
| 57 |
|
|
RCLK : in std_logic; -- Receiver clock (16x baudrate)
|
| 58 |
|
|
BAUDOUTN : out std_logic; -- Baudrate generator output (16x baudrate)
|
| 59 |
|
|
RTSN : out std_logic; -- RTS output
|
| 60 |
|
|
DTRN : out std_logic; -- DTR output
|
| 61 |
|
|
CTSN : in std_logic; -- CTS input
|
| 62 |
|
|
DSRN : in std_logic; -- DSR input
|
| 63 |
|
|
DCDN : in std_logic; -- DCD input
|
| 64 |
|
|
RIN : in std_logic; -- RI input
|
| 65 |
|
|
SIN : in std_logic; -- Receiver input
|
| 66 |
|
|
SOUT : out std_logic -- Transmitter output
|
| 67 |
|
|
);
|
| 68 |
|
|
end component;
|
| 69 |
|
|
component slib_clock_div is
|
| 70 |
|
|
generic (
|
| 71 |
|
|
RATIO : integer := 18 -- Clock divider ratio
|
| 72 |
|
|
);
|
| 73 |
|
|
port (
|
| 74 |
|
|
CLK : in std_logic; -- Clock
|
| 75 |
|
|
RST : in std_logic; -- Reset
|
| 76 |
|
|
CE : in std_logic; -- Clock enable input
|
| 77 |
|
|
Q : out std_logic -- New clock enable output
|
| 78 |
|
|
);
|
| 79 |
|
|
end component;
|
| 80 |
|
|
|
| 81 |
|
|
|
| 82 |
|
|
-- DUT signals
|
| 83 |
|
|
signal clk, rst : std_logic;
|
| 84 |
|
|
signal uart_if : uart_interface;
|
| 85 |
|
|
signal dout : std_logic_vector (7 downto 0);
|
| 86 |
|
|
signal ddis, int : std_logic;
|
| 87 |
|
|
signal baudce, rclk, baudoutn : std_logic;
|
| 88 |
|
|
signal out1n, out2n, rtsn, dtrn, ctsn, dsrn, dcdn, rin, sin, sout : std_logic;
|
| 89 |
|
|
|
| 90 |
|
|
constant cycle : time := 30 ns;
|
| 91 |
|
|
|
| 92 |
|
|
begin
|
| 93 |
|
|
-- Main clock
|
| 94 |
|
|
CLOCK: process
|
| 95 |
|
|
begin
|
| 96 |
|
|
clk <= '0';
|
| 97 |
|
|
wait for cycle/2;
|
| 98 |
|
|
clk <= '1';
|
| 99 |
|
|
wait for cycle/2;
|
| 100 |
|
|
end process;
|
| 101 |
|
|
|
| 102 |
|
|
-- Baudrate generator clock enable
|
| 103 |
|
|
BGCE: slib_clock_div generic map (RATIO => 18) port map (clk, rst, '1', baudce);
|
| 104 |
|
|
|
| 105 |
|
|
rclk <= baudoutn;
|
| 106 |
|
|
|
| 107 |
|
|
-- Pull-up
|
| 108 |
|
|
--uart_if.data <= (others => 'H');
|
| 109 |
|
|
|
| 110 |
|
|
-- UART interface bus tri-state buffer
|
| 111 |
|
|
LPCBUF: process (ddis, dout)
|
| 112 |
|
|
begin
|
| 113 |
|
|
if (ddis = '0') then
|
| 114 |
|
|
uart_if.data <= dout;
|
| 115 |
|
|
else
|
| 116 |
|
|
uart_if.data <= (others => 'Z');
|
| 117 |
|
|
end if;
|
| 118 |
|
|
end process;
|
| 119 |
|
|
|
| 120 |
|
|
DUT: uart_16750 port map ( CLK => CLK,
|
| 121 |
|
|
RST => RST,
|
| 122 |
|
|
BAUDCE => BAUDCE,
|
| 123 |
|
|
CS => uart_if.cs,
|
| 124 |
|
|
WR => uart_if.wr,
|
| 125 |
|
|
RD => uart_if.rd,
|
| 126 |
|
|
A => uart_if.a,
|
| 127 |
|
|
DIN => uart_if.data,
|
| 128 |
|
|
DOUT => dout,
|
| 129 |
|
|
DDIS => ddis,
|
| 130 |
|
|
INT => int,
|
| 131 |
|
|
OUT1N => out1n,
|
| 132 |
|
|
OUT2N => out2n,
|
| 133 |
|
|
RCLK => rclk,
|
| 134 |
|
|
BAUDOUTN=> baudoutn,
|
| 135 |
|
|
RTSN => rtsn,
|
| 136 |
|
|
DTRN => dtrn,
|
| 137 |
|
|
CTSN => ctsn,
|
| 138 |
|
|
DSRN => dsrn,
|
| 139 |
|
|
DCDN => dcdn,
|
| 140 |
|
|
RIN => rin,
|
| 141 |
|
|
SIN => sin,
|
| 142 |
|
|
SOUT => sout
|
| 143 |
|
|
);
|
| 144 |
|
|
|
| 145 |
|
|
-- Main transaction process
|
| 146 |
|
|
TRANPROC: process
|
| 147 |
|
|
variable s : string(1 to 100);
|
| 148 |
|
|
variable address : std_logic_vector(2 downto 0);
|
| 149 |
|
|
variable data : std_logic_vector(7 downto 0);
|
| 150 |
|
|
variable data2 : std_logic_vector(7 downto 0);
|
| 151 |
|
|
begin
|
| 152 |
|
|
-- Default values
|
| 153 |
|
|
rst <= '1';
|
| 154 |
|
|
ctsn <= '1';
|
| 155 |
|
|
dsrn <= '1';
|
| 156 |
|
|
dcdn <= '1';
|
| 157 |
|
|
rin <= '1';
|
| 158 |
|
|
sin <= '1';
|
| 159 |
|
|
uart_if.a <= (others => '0');
|
| 160 |
|
|
uart_if.data <= (others => 'Z');
|
| 161 |
|
|
uart_if.cs <= '0';
|
| 162 |
|
|
uart_if.rd <= '0';
|
| 163 |
|
|
uart_if.wr <= '0';
|
| 164 |
|
|
|
| 165 |
|
|
wait until falling_edge(clk);
|
| 166 |
|
|
|
| 167 |
|
|
-- Get commands from stimulus file
|
| 168 |
|
|
while not endfile(stimulus) loop
|
| 169 |
|
|
str_read(stimulus, s); -- Read line into string
|
| 170 |
|
|
|
| 171 |
|
|
if (s(1 to 4) = "#SET") then -- Set values
|
| 172 |
|
|
-- Format: RSTN CTSN DSRN DCDN RIN
|
| 173 |
|
|
rst <= to_std_logic(s(6));
|
| 174 |
|
|
--CTSN <= to_std_logic(s(8));
|
| 175 |
|
|
--DSRN <= to_std_logic(s(10));
|
| 176 |
|
|
--DCDN <= to_std_logic(s(12));
|
| 177 |
|
|
--RIN <= to_std_logic(s(14));
|
| 178 |
|
|
elsif (s(1 to 5) = "#WAIT") then -- Wait n cycles
|
| 179 |
|
|
wait for integer'value(s(7 to 12))*cycle;
|
| 180 |
|
|
elsif (s(1 to 3) = "#RD") then -- Read from UART and compare
|
| 181 |
|
|
address := to_std_logic_vector(s(5 to 7));
|
| 182 |
|
|
data := to_std_logic_vector(s(9 to 16));
|
| 183 |
|
|
uart_read (uart_if, address, data2, log);
|
| 184 |
|
|
if (not compare(data, data2)) then
|
| 185 |
|
|
print (log, time'image(now) & ": " & "Failed: Expected 0x" & hstr(data) & " got 0x" & hstr(data2));
|
| 186 |
|
|
end if;
|
| 187 |
|
|
elsif (s(1 to 3) = "#WR") then -- Write to LPC
|
| 188 |
|
|
address := to_std_logic_vector(s(5 to 7));
|
| 189 |
|
|
data := to_std_logic_vector(s(9 to 16));
|
| 190 |
|
|
uart_write (uart_if, address, data, log);
|
| 191 |
|
|
elsif (s(1 to 4) = "#LOG") then -- Write message to log
|
| 192 |
|
|
print (log, time'image(now) & ": " & s(6 to 80));
|
| 193 |
|
|
elsif (s(1 to 4) = "#CUO") then -- Check UART outputs INT OUT1N OUT2N RTSN DTRN
|
| 194 |
|
|
data2(4 downto 0) := to_std_logic_vector(s(6 to 10));
|
| 195 |
|
|
data(4 downto 0) := INT & OUT1N & OUT2N & RTSN & DTRN;
|
| 196 |
|
|
if (not compare(data(3 downto 0), data2(3 downto 0))) then
|
| 197 |
|
|
print (log, time'image(now) & ": " & "UART outputs failed: Expected " &
|
| 198 |
|
|
str(data2(4 downto 0)) & " got " & str(data(4 downto 0)));
|
| 199 |
|
|
else
|
| 200 |
|
|
print (log, time'image(now) & ": " & "UART outputs: " & str(data(4 downto 0)));
|
| 201 |
|
|
end if;
|
| 202 |
25 |
hasw |
elsif (s(1 to 4) = "#EUS") then -- Send serial data from external UART to DUT
|
| 203 |
|
|
uart_send (SOUT, 8.68 us, integer'value (s(6 to 7)), integer'value (s(9 to 11)), log);
|
| 204 |
|
|
--uart_send (SOUT, 3.33 ms, 8, integer'value (s(8 to 10)), log);
|
| 205 |
2 |
hasw |
else
|
| 206 |
|
|
print ("Unknown command: " & s);
|
| 207 |
|
|
end if;
|
| 208 |
|
|
end loop;
|
| 209 |
|
|
|
| 210 |
|
|
wait;
|
| 211 |
|
|
end process;
|
| 212 |
|
|
|
| 213 |
|
|
end tb;
|
| 214 |
|
|
|