-----------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------
|
-- uart test bench
|
-- uart test bench
|
--
|
--
|
-----------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------
|
use std.textio.all;
|
use std.textio.all;
|
|
|
library ieee;
|
library ieee;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_unsigned.all;
|
|
use ieee.numeric_std.all;
|
|
use ieee.std_logic_textio.all;
|
|
|
|
library work;
|
library work;
|
use work.uart2BusTop_pkg.all;
|
use work.uart2BusTop_pkg.all;
|
use work.helpers_pkg.all;
|
use work.helpers_pkg.all;
|
|
|
-----------------------------------------------------------------------------------------
|
-----------------------------------------------------------------------------------------
|
-- test bench implementation
|
-- test bench implementation
|
entity uart2BusTop_txt_tb is
|
entity uart2BusTop_txt_tb is
|
end uart2BusTop_txt_tb;
|
end uart2BusTop_txt_tb;
|
|
|
architecture behavior of uart2BusTop_txt_tb is
|
architecture behavior of uart2BusTop_txt_tb is
|
|
|
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
|
|
|
|
variable shiftreg : std_logic_vector(7 downto 0);
|
|
variable bitTime : time;
|
|
|
|
begin
|
|
bitTime := 1000 ms / (baud + baud * baudError / 100.0);
|
|
shiftreg := std_logic_vector(to_unsigned(data, shiftreg'length));
|
|
txd <= '0';
|
|
wait for bitTime;
|
|
for index in 0 to bitnumber loop
|
|
txd <= shiftreg(index);
|
|
wait for bitTime;
|
|
end loop;
|
|
txd <= '1';
|
|
wait for stopbit * bitTime;
|
|
end procedure;
|
|
|
|
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
|
|
|
|
variable bitTime : time;
|
|
|
|
begin
|
|
bitTime := 1000 ms / (baud + baud * baudError / 100.0);
|
|
wait until (rxd = '0');
|
|
wait for bitTime / 2;
|
|
wait for bitTime;
|
|
for index in 0 to bitnumber loop
|
|
data <= rxd & data(7 downto 1);
|
|
wait for bitTime;
|
|
end loop;
|
|
wait for stopbit * bitTime;
|
|
end procedure;
|
|
|
|
-- Inputs
|
-- Inputs
|
signal clr : std_logic := '0';
|
signal clr : std_logic := '0';
|
signal clk : std_logic := '0';
|
signal clk : std_logic := '0';
|
signal serIn : std_logic := '0';
|
signal serIn : std_logic := '0';
|
signal intRdData : std_logic_vector(7 downto 0) := (others => '0');
|
signal intRdData : std_logic_vector(7 downto 0) := (others => '0');
|
|
|
-- Outputs
|
-- Outputs
|
signal serOut : std_logic;
|
signal serOut : std_logic;
|
signal intAddress : std_logic_vector(7 downto 0);
|
signal intAddress : std_logic_vector(7 downto 0);
|
signal intWrData : std_logic_vector(7 downto 0);
|
signal intWrData : std_logic_vector(7 downto 0);
|
signal intWrite : std_logic;
|
signal intWrite : std_logic;
|
signal intRead : std_logic;
|
signal intRead : std_logic;
|
signal recvData : std_logic_vector(7 downto 0);
|
signal recvData : std_logic_vector(7 downto 0);
|
signal newRxData : std_logic;
|
signal newRxData : std_logic;
|
signal intAccessReq : std_logic;
|
signal intAccessReq : std_logic;
|
signal intAccessGnt : std_logic;
|
signal intAccessGnt : std_logic;
|
signal counter : integer;
|
signal counter : integer;
|
|
|
constant BAUD_115200 : real := 115200.0;
|
constant BAUD_115200 : real := 115200.0;
|
constant BAUD_38400 : real := 38400.0;
|
constant BAUD_38400 : real := 38400.0;
|
constant BAUD_28800 : real := 28800.0;
|
constant BAUD_28800 : real := 28800.0;
|
constant BAUD_19200 : real := 19200.0;
|
constant BAUD_19200 : real := 19200.0;
|
constant BAUD_9600 : real := 9600.0;
|
constant BAUD_9600 : real := 9600.0;
|
constant BAUD_4800 : real := 4800.0;
|
constant BAUD_4800 : real := 4800.0;
|
constant BAUD_2400 : real := 2400.0;
|
constant BAUD_2400 : real := 2400.0;
|
constant BAUD_1200 : real := 1200.0;
|
constant BAUD_1200 : real := 1200.0;
|
|
|
constant NSTOPS_1 : real := 1.0;
|
constant NSTOPS_1 : real := 1.0;
|
constant NSTOPS_1_5 : real := 1.5;
|
constant NSTOPS_1_5 : real := 1.5;
|
constant NSTOPS_2 : real := 2.0;
|
constant NSTOPS_2 : real := 2.0;
|
|
|
constant PARITY_NONE : integer := 0;
|
constant PARITY_NONE : integer := 0;
|
constant PARITY_EVEN : integer := 1;
|
constant PARITY_EVEN : integer := 1;
|
constant PARITY_ODD : integer := 2;
|
constant PARITY_ODD : integer := 2;
|
constant PARITY_MARK : integer := 3;
|
constant PARITY_MARK : integer := 3;
|
constant PARITY_SPACE : integer := 4;
|
constant PARITY_SPACE : integer := 4;
|
|
|
constant NBITS_7 : integer := 6;
|
constant NBITS_7 : integer := 6;
|
constant NBITS_8 : integer := 7;
|
constant NBITS_8 : integer := 7;
|
|
|
begin
|
begin
|
-- Instantiate the Unit Under Test (UUT)
|
-- Instantiate the Unit Under Test (UUT)
|
uut : uart2BusTop
|
uut : uart2BusTop
|
port map
|
port map
|
(
|
(
|
clr => clr,
|
clr => clr,
|
clk => clk,
|
clk => clk,
|
serIn => serIn,
|
serIn => serIn,
|
serOut => serOut,
|
serOut => serOut,
|
intAccessReq => intAccessReq,
|
intAccessReq => intAccessReq,
|
intAccessGnt => intAccessGnt,
|
intAccessGnt => intAccessGnt,
|
intRdData => intRdData,
|
intRdData => intRdData,
|
intAddress => intAddress,
|
intAddress => intAddress,
|
intWrData => intWrData,
|
intWrData => intWrData,
|
intWrite => intWrite,
|
intWrite => intWrite,
|
intRead => intRead
|
intRead => intRead
|
);
|
);
|
|
|
rfm : regFileModel
|
rfm : regFileModel
|
port map
|
port map
|
(
|
(
|
clr => clr,
|
clr => clr,
|
clk => clk,
|
clk => clk,
|
intRdData => intRdData,
|
intRdData => intRdData,
|
intAddress => intAddress,
|
intAddress => intAddress,
|
intWrData => intWrData,
|
intWrData => intWrData,
|
intWrite => intWrite,
|
intWrite => intWrite,
|
intRead => intRead);
|
intRead => intRead);
|
|
|
-- just to create a delay similar to simulate a bus arbitrer
|
-- just to create a delay similar to simulate a bus arbitrer
|
process (clr, clk)
|
process (clr, clk)
|
begin
|
begin
|
if (clr = '1') then
|
if (clr = '1') then
|
intAccessGnt <= '0';
|
intAccessGnt <= '0';
|
counter <= 0;
|
counter <= 0;
|
elsif (rising_edge(clk)) then
|
elsif (rising_edge(clk)) then
|
if (counter = 0) then
|
if (counter = 0) then
|
if ((intAccessReq = '1') and (intAccessGnt = '0')) then
|
if ((intAccessReq = '1') and (intAccessGnt = '0')) then
|
counter <= 500;
|
counter <= 500;
|
end if;
|
end if;
|
intAccessGnt <= '0';
|
intAccessGnt <= '0';
|
elsif (counter = 1) then
|
elsif (counter = 1) then
|
counter <= counter - 1;
|
counter <= counter - 1;
|
intAccessGnt <= '1';
|
intAccessGnt <= '1';
|
else
|
else
|
counter <= counter - 1;
|
counter <= counter - 1;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
-- clock generator - 25MHz clock
|
-- clock generator - 25MHz clock
|
process
|
process
|
begin
|
begin
|
clk <= '0';
|
clk <= '0';
|
wait for 20 ns;
|
wait for 20 ns;
|
clk <= '1';
|
clk <= '1';
|
wait for 20 ns;
|
wait for 20 ns;
|
end process;
|
end process;
|
|
|
-- reset process definitions
|
-- reset process definitions
|
process
|
process
|
begin
|
begin
|
clr <= '1';
|
clr <= '1';
|
wait for 40 ns;
|
wait for 40 ns;
|
clr <= '0';
|
clr <= '0';
|
wait;
|
wait;
|
end process;
|
end process;
|
|
|
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
-- test bench receiver
|
-- test bench receiver
|
process
|
process
|
|
|
begin
|
begin
|
newRxData <= '0';
|
newRxData <= '0';
|
recvData <= (others => '0');
|
recvData <= (others => '0');
|
wait until (clr = '0');
|
wait until (clr = '0');
|
loop
|
loop
|
recvSerial(serOut, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, recvData);
|
recvSerial(serOut, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, recvData);
|
newRxData <= '1';
|
newRxData <= '1';
|
wait for 25 ns;
|
wait for 25 ns;
|
newRxData <= '0';
|
newRxData <= '0';
|
end loop;
|
end loop;
|
end process;
|
end process;
|
|
|
--------------------------------------------------------------------
|
--------------------------------------------------------------------
|
-- uart transmit - test bench control
|
-- uart transmit - test bench control
|
process
|
process
|
|
|
type dataFile is file of character;
|
type dataFile is file of character;
|
file testTextFile : dataFile open READ_MODE is "../test.txt";
|
file testTextFile : dataFile open READ_MODE is "../test.txt";
|
variable charBuf : character;
|
variable charBuf : character;
|
variable data : integer;
|
variable data : integer;
|
variable tempLine : line;
|
variable tempLine : line;
|
|
|
begin
|
begin
|
-- default value of serial output
|
-- default value of serial output
|
serIn <= '1';
|
serIn <= '1';
|
-- text mode simulation
|
-- text mode simulation
|
write(tempLine, string'("Starting text mode simulation"));
|
write(tempLine, string'("Starting text mode simulation"));
|
writeline(output, tempLine);
|
writeline(output, tempLine);
|
wait until (clr = '0');
|
wait until (clr = '0');
|
wait until (rising_edge(clk));
|
wait until (rising_edge(clk));
|
for index in 0 to 99 loop
|
for index in 0 to 99 loop
|
wait until (rising_edge(clk));
|
wait until (rising_edge(clk));
|
end loop;
|
end loop;
|
while not endfile(testTextFile) loop
|
while not endfile(testTextFile) loop
|
-- transmit the byte in the command file one by one
|
-- transmit the byte in the command file one by one
|
read(testTextFile, charBuf);
|
read(testTextFile, charBuf);
|
data := character'pos(charBuf);
|
data := character'pos(charBuf);
|
sendSerial(data, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, serIn);
|
sendSerial(data, BAUD_115200, PARITY_NONE, NSTOPS_1, NBITS_8, 0.0, serIn);
|
wait for 800 us;
|
wait for 800 us;
|
end loop;
|
end loop;
|
wait;
|
wait;
|
end process;
|
end process;
|
end;
|
end;
|
|
|