| 1 |
6 |
smuller |
-----------------------------------------------------------------------------------------
|
| 2 |
|
|
-- uart test bench
|
| 3 |
|
|
--
|
| 4 |
|
|
-----------------------------------------------------------------------------------------
|
| 5 |
|
|
use std.textio.all;
|
| 6 |
|
|
library ieee;
|
| 7 |
|
|
use ieee.std_logic_1164.all;
|
| 8 |
|
|
use ieee.std_logic_unsigned.all;
|
| 9 |
|
|
use ieee.numeric_std.all;
|
| 10 |
|
|
use ieee.std_logic_textio.all;
|
| 11 |
|
|
|
| 12 |
|
|
-----------------------------------------------------------------------------------------
|
| 13 |
|
|
-- test bench implementation
|
| 14 |
|
|
entity uart2BusTop_bin_tb is
|
| 15 |
|
|
end uart2BusTop_bin_tb;
|
| 16 |
|
|
|
| 17 |
|
|
architecture behavior of uart2BusTop_bin_tb is
|
| 18 |
|
|
|
| 19 |
|
|
procedure sendSerial(data : integer; baud : in real; parity : in integer; stopbit : in real; bitnumber : in integer; baudError : in real; signal txd : inout std_logic) is
|
| 20 |
|
|
|
| 21 |
|
|
variable shiftreg : std_logic_vector(7 downto 0);
|
| 22 |
|
|
variable bitTime : time;
|
| 23 |
|
|
|
| 24 |
|
|
begin
|
| 25 |
|
|
bitTime := 1000 ms / (baud + baud * baudError / 100.0);
|
| 26 |
|
|
shiftreg := std_logic_vector(to_unsigned(data, shiftreg'length));
|
| 27 |
|
|
txd <= '0';
|
| 28 |
|
|
wait for bitTime;
|
| 29 |
|
|
for index in 0 to bitnumber loop
|
| 30 |
|
|
txd <= shiftreg(index);
|
| 31 |
|
|
wait for bitTime;
|
| 32 |
|
|
end loop;
|
| 33 |
|
|
txd <= '1';
|
| 34 |
|
|
wait for stopbit * bitTime;
|
| 35 |
|
|
end procedure;
|
| 36 |
|
|
|
| 37 |
|
|
procedure recvSerial( signal rxd : in std_logic; baud : in real; parity : in integer; stopbit : in real; bitnumber : in integer; baudError : in real; signal data : inout std_logic_vector(7 downto 0)) is
|
| 38 |
|
|
|
| 39 |
|
|
variable bitTime : time;
|
| 40 |
|
|
|
| 41 |
|
|
begin
|
| 42 |
|
|
bitTime := 1000 ms / (baud + baud * baudError / 100.0);
|
| 43 |
|
|
wait until (rxd = '0');
|
| 44 |
|
|
wait for bitTime / 2;
|
| 45 |
|
|
wait for bitTime;
|
| 46 |
|
|
for index in 0 to bitnumber loop
|
| 47 |
|
|
data <= rxd & data(7 downto 1);
|
| 48 |
|
|
wait for bitTime;
|
| 49 |
|
|
end loop;
|
| 50 |
|
|
wait for stopbit * bitTime;
|
| 51 |
|
|
end procedure;
|
| 52 |
|
|
|
| 53 |
|
|
component uart2BusTop
|
| 54 |
|
|
generic
|
| 55 |
|
|
(
|
| 56 |
|
|
AW : integer := 8
|
| 57 |
|
|
);
|
| 58 |
|
|
port
|
| 59 |
|
|
(
|
| 60 |
|
|
clr : in std_logic;
|
| 61 |
|
|
clk : in std_logic;
|
| 62 |
|
|
serIn : in std_logic;
|
| 63 |
|
|
serOut : out std_logic;
|
| 64 |
|
|
intRdData : in std_logic_vector(7 downto 0);
|
| 65 |
|
|
intAddress : out std_logic_vector(AW - 1 downto 0);
|
| 66 |
|
|
intWrData : out std_logic_vector(7 downto 0);
|
| 67 |
|
|
intWrite : out std_logic;
|
| 68 |
|
|
intRead : out std_logic
|
| 69 |
|
|
);
|
| 70 |
|
|
end component;
|
| 71 |
|
|
|
| 72 |
|
|
component regFileModel
|
| 73 |
|
|
port
|
| 74 |
|
|
(
|
| 75 |
|
|
clr : in std_logic;
|
| 76 |
|
|
clk : in std_logic;
|
| 77 |
|
|
intAddress : in std_logic_vector(7 downto 0);
|
| 78 |
|
|
intWrData : in std_logic_vector(7 downto 0);
|
| 79 |
|
|
intWrite : in std_logic;
|
| 80 |
|
|
intRead : in std_logic;
|
| 81 |
|
|
intRdData : out std_logic_vector(7 downto 0));
|
| 82 |
|
|
end component;
|
| 83 |
|
|
|
| 84 |
|
|
-- Inputs
|
| 85 |
|
|
signal clr : std_logic := '0';
|
| 86 |
|
|
signal clk : std_logic := '0';
|
| 87 |
|
|
signal serIn : std_logic := '0';
|
| 88 |
|
|
signal intRdData : std_logic_vector(7 downto 0) := (others => '0');
|
| 89 |
|
|
|
| 90 |
|
|
-- Outputs
|
| 91 |
|
|
signal serOut : std_logic;
|
| 92 |
|
|
signal intAddress : std_logic_vector(7 downto 0);
|
| 93 |
|
|
signal intWrData : std_logic_vector(7 downto 0);
|
| 94 |
|
|
signal intWrite : std_logic;
|
| 95 |
|
|
signal intRead : std_logic;
|
| 96 |
|
|
signal recvData : std_logic_vector(7 downto 0);
|
| 97 |
|
|
signal newRxData : std_logic;
|
| 98 |
|
|
|
| 99 |
|
|
constant BAUD_115200 : real := 115200.0;
|
| 100 |
|
|
constant BAUD_38400 : real := 38400.0;
|
| 101 |
|
|
constant BAUD_28800 : real := 28800.0;
|
| 102 |
|
|
constant BAUD_19200 : real := 19200.0;
|
| 103 |
|
|
constant BAUD_9600 : real := 9600.0;
|
| 104 |
|
|
constant BAUD_4800 : real := 4800.0;
|
| 105 |
|
|
constant BAUD_2400 : real := 2400.0;
|
| 106 |
|
|
constant BAUD_1200 : real := 1200.0;
|
| 107 |
|
|
|
| 108 |
|
|
constant NSTOPS_1 : real := 1.0;
|
| 109 |
|
|
constant NSTOPS_1_5 : real := 1.5;
|
| 110 |
|
|
constant NSTOPS_2 : real := 2.0;
|
| 111 |
|
|
|
| 112 |
|
|
constant PARITY_NONE : integer := 0;
|
| 113 |
|
|
constant PARITY_EVEN : integer := 1;
|
| 114 |
|
|
constant PARITY_ODD : integer := 2;
|
| 115 |
|
|
constant PARITY_MARK : integer := 3;
|
| 116 |
|
|
constant PARITY_SPACE : integer := 4;
|
| 117 |
|
|
|
| 118 |
|
|
constant NBITS_7 : integer := 6;
|
| 119 |
|
|
constant NBITS_8 : integer := 7;
|
| 120 |
|
|
|
| 121 |
|
|
begin
|
| 122 |
|
|
-- Instantiate the Unit Under Test (UUT)
|
| 123 |
|
|
uut : uart2BusTop
|
| 124 |
|
|
port map
|
| 125 |
|
|
(
|
| 126 |
|
|
clr => clr,
|
| 127 |
|
|
clk => clk,
|
| 128 |
|
|
serIn => serIn,
|
| 129 |
|
|
serOut => serOut,
|
| 130 |
|
|
intRdData => intRdData,
|
| 131 |
|
|
intAddress => intAddress,
|
| 132 |
|
|
intWrData => intWrData,
|
| 133 |
|
|
intWrite => intWrite,
|
| 134 |
|
|
intRead => intRead
|
| 135 |
|
|
);
|
| 136 |
|
|
|
| 137 |
|
|
rfm : regFileModel
|
| 138 |
|
|
port map
|
| 139 |
|
|
(
|
| 140 |
|
|
clr => clr,
|
| 141 |
|
|
clk => clk,
|
| 142 |
|
|
intRdData => intRdData,
|
| 143 |
|
|
intAddress => intAddress,
|
| 144 |
|
|
intWrData => intWrData,
|
| 145 |
|
|
intWrite => intWrite,
|
| 146 |
|
|
intRead => intRead);
|
| 147 |
|
|
|
| 148 |
|
|
-- clock generator - 25MHz clock
|
| 149 |
|
|
process
|
| 150 |
|
|
begin
|
| 151 |
|
|
clk <= '0';
|
| 152 |
|
|
wait for 20 ns;
|
| 153 |
|
|
clk <= '1';
|
| 154 |
|
|
wait for 20 ns;
|
| 155 |
|
|
end process;
|
| 156 |
|
|
|
| 157 |
|
|
-- reset process definitions
|
| 158 |
|
|
process
|
| 159 |
|
|
begin
|
| 160 |
|
|
clr <= '1';
|
| 161 |
|
|
wait for 40 ns;
|
| 162 |
|
|
clr <= '0';
|
| 163 |
|
|
wait;
|
| 164 |
|
|
end process;
|
| 165 |
|
|
|
| 166 |
|
|
--------------------------------------------------------------------
|
| 167 |
|
|
-- test bench receiver
|
| 168 |
|
|
process
|
| 169 |
|
|
|
| 170 |
|
|
begin
|
| 171 |
|
|
newRxData <= '0';
|
| 172 |
|
|
recvData <= (others => '0');
|
| 173 |
|
|
wait until (clr = '0');
|
| 174 |
|
|
loop
|
| 175 |
|
|
recvSerial(serOut, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, recvData);
|
| 176 |
|
|
newRxData <= '1';
|
| 177 |
|
|
wait for 25 ns;
|
| 178 |
|
|
newRxData <= '0';
|
| 179 |
|
|
end loop;
|
| 180 |
|
|
end process;
|
| 181 |
|
|
|
| 182 |
|
|
--------------------------------------------------------------------
|
| 183 |
|
|
-- uart transmit - test bench control
|
| 184 |
|
|
process
|
| 185 |
|
|
|
| 186 |
|
|
type dataFile is file of character;
|
| 187 |
|
|
file testBinaryFile : dataFile open READ_MODE is "test.bin";
|
| 188 |
|
|
variable charBuf : character;
|
| 189 |
|
|
variable fileLength : integer;
|
| 190 |
|
|
variable byteIndex : integer;
|
| 191 |
|
|
variable txLength : integer;
|
| 192 |
|
|
variable rxLength : integer;
|
| 193 |
|
|
variable tempLine : line;
|
| 194 |
|
|
|
| 195 |
|
|
begin
|
| 196 |
|
|
-- default value of serial output
|
| 197 |
|
|
serIn <= '1';
|
| 198 |
|
|
-- binary mode simulation
|
| 199 |
|
|
write(tempLine, string'("Starting binary mode simulation"));
|
| 200 |
|
|
writeline(output, tempLine);
|
| 201 |
|
|
wait until (clr = '0');
|
| 202 |
|
|
wait until (rising_edge(clk));
|
| 203 |
|
|
for index in 0 to 99 loop
|
| 204 |
|
|
wait until (rising_edge(clk));
|
| 205 |
|
|
end loop;
|
| 206 |
|
|
-- in binary simulation mode the first two byte contain the file length (MSB first)
|
| 207 |
|
|
read(testBinaryFile, charBuf);
|
| 208 |
|
|
fileLength := character'pos(charBuf);
|
| 209 |
|
|
read(testBinaryFile, charBuf);
|
| 210 |
|
|
fileLength := 256 * fileLength + character'pos(charBuf);
|
| 211 |
|
|
write(tempLine, string'("File length: "));
|
| 212 |
|
|
write(tempLine, fileLength);
|
| 213 |
|
|
writeline(output, tempLine);
|
| 214 |
|
|
-- send entire file to uart
|
| 215 |
|
|
byteIndex := 0;
|
| 216 |
|
|
while (byteIndex < fileLength) loop
|
| 217 |
|
|
-- each "record" in the binary starts with two bytes: the first is the number
|
| 218 |
|
|
-- of bytes to transmit and the second is the number of received bytes to wait
|
| 219 |
|
|
-- for before transmitting the next command.
|
| 220 |
|
|
read(testBinaryFile, charBuf);
|
| 221 |
|
|
txLength := character'pos(charBuf);
|
| 222 |
|
|
read(testBinaryFile, charBuf);
|
| 223 |
|
|
rxLength := character'pos(charBuf);
|
| 224 |
|
|
write(tempLine, string'("Executing command with "));
|
| 225 |
|
|
write(tempLine, txLength);
|
| 226 |
|
|
write(tempLine, string'(" tx bytes and "));
|
| 227 |
|
|
write(tempLine, rxLength);
|
| 228 |
|
|
write(tempLine, string'(" rx bytes"));
|
| 229 |
|
|
writeline(output, tempLine);
|
| 230 |
|
|
byteIndex := byteIndex + 2;
|
| 231 |
|
|
-- transmit command
|
| 232 |
|
|
while (txLength > 0) loop
|
| 233 |
|
|
-- read next byte from file and transmit it
|
| 234 |
|
|
read(testBinaryFile, charBuf);
|
| 235 |
|
|
byteIndex := byteIndex + 1;
|
| 236 |
|
|
sendSerial(character'pos(charBuf), BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, serIn);
|
| 237 |
|
|
-- update tx_len
|
| 238 |
|
|
txLength := txLength - 1;
|
| 239 |
|
|
end loop;
|
| 240 |
|
|
-- wait for received bytes
|
| 241 |
|
|
while (rxLength > 0) loop
|
| 242 |
|
|
wait until (newRxData = '1');
|
| 243 |
|
|
wait until (newRxData = '0');
|
| 244 |
|
|
rxLength := rxLength - 1;
|
| 245 |
|
|
end loop;
|
| 246 |
|
|
write(tempLine, string'("Command finished"));
|
| 247 |
|
|
writeline(output, tempLine);
|
| 248 |
|
|
end loop;
|
| 249 |
|
|
wait;
|
| 250 |
|
|
end process;
|
| 251 |
|
|
end;
|