Motion FPGA
Motion FPGA for the MachX02-7000HE Breakout Board
spi_slave_tb.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 --! @file
3 --! @brief SPI Slave testbench module
4 -------------------------------------------------------------------------------
5 
6 --! Using IEEE library
7 LIBRARY ieee;
8 
9 --! Using IEEE standard logic components
10 USE ieee.std_logic_1164.ALL;
11 
12 --! @brief SPI Slave testbench module
13 ENTITY spi_slave_tb IS
14 END ENTITY spi_slave_tb;
15 
16 --! Architecture tb of spi_slave_tb entity
17 ARCHITECTURE tb OF spi_slave_tb IS
18 
19  --! Clock period
20  CONSTANT c_clk_period : time := 100 ns;
21 
22  -- Signals to unit under test
23  SIGNAL clk : std_logic; --! Clock
24  SIGNAL rst : std_logic; --! Asynchronous reset
25  SIGNAL spi_cs : std_logic; --! SPI chip select input to uut
26  SIGNAL spi_sclk : std_logic; --! SPI clock input to uut
27  SIGNAL spi_mosi : std_logic; --! SPI MOSI input to uut
28  SIGNAL spi_miso : std_logic; --! SPI MISO output from uut
29  SIGNAL dat_rd_reg : std_logic_vector(31 DOWNTO 0); --! Data read register input to uut
30  SIGNAL dat_wr_reg : std_logic_vector(31 DOWNTO 0); --! Data write register output from uut
31  SIGNAL dat_wr_done : std_logic; --! Data write done output from uut
32 
33  -- Test signals
34  SIGNAL mosi_wr : std_logic_vector(31 DOWNTO 0); --! SPI MOSI test pattern to drive into uut
35  SIGNAL miso_rd : std_logic_vector(31 DOWNTO 0); --! SPI MISO output pattern driven from uut
36 
37 BEGIN
38 
39  --! Instantiate spi_slave as unit under test
40  i_uut : ENTITY work.spi_slave(rtl)
41  GENERIC MAP (
42  size => 32
43  )
44  PORT MAP (
45  clk_in => clk,
46  rst_in => rst,
47  spi_cs_in => spi_cs,
54  );
55 
56  --! @brief Clock generation process
57  pr_clock : PROCESS IS
58  BEGIN
59 
60  -- Low for 1/2 clock
61  clk <= '0';
62  WAIT FOR c_clk_period / 2;
63 
64  -- High for 1/2 clock
65  clk <= '1';
66  WAIT FOR c_clk_period / 2;
67 
68  END PROCESS pr_clock;
69 
70  --! @brief Stimulus process
71  pr_stimulus : PROCESS IS
72  BEGIN
73 
74  -- Set SPI bus idle
75  spi_cs <= '1'; -- CS: high-idle
76  spi_sclk <= '0'; -- SCLK: low-idle
77  spi_mosi <= '0'; -- MOSI: idle
78 
79  -- Reset for 5 clocks
80  REPORT "Hold in Reset" SEVERITY note;
81  rst <= '1';
82  WAIT FOR c_clk_period * 5;
83  ASSERT (spi_miso = '0') REPORT "Expected spi_miso low while in reset" SEVERITY error;
84  ASSERT (dat_wr_done = '0') REPORT "Expected dat_wr_done low while in reset" SEVERITY error;
85 
86  -- Release reset for 5 clocks
87  REPORT "Take out of Reset" SEVERITY note;
88  rst <= '0';
89  WAIT FOR c_clk_period * 5;
90  ASSERT (spi_miso = '0') REPORT "Expected spi_miso low while idle" SEVERITY error;
91  ASSERT (dat_wr_done = '0') REPORT "Expected dat_wr_done low while idle" SEVERITY error;
92 
93  -- Provide test pattern
94  dat_rd_reg <= X"AA550011";
95  mosi_wr <= X"DEADBEEF";
96 
97  -- Start SPI transfer
98  REPORT "Start SPI Transfer" SEVERITY note;
99  spi_cs <= '0';
100  WAIT FOR c_clk_period;
101 
102  -- Wait for 5 clocks (CS-to-Start)
103  WAIT FOR c_clk_period * 5;
104 
105  -- Clock SPI bus
106  REPORT "Transfer 32 Bits" SEVERITY note;
107  FOR i IN 31 DOWNTO 0 LOOP
108  -- Drive data and first edge
109  spi_mosi <= mosi_wr(i);
110  spi_sclk <= '1';
111  WAIT FOR c_clk_period * 2;
112 
113  -- Capture data and drive second edge
114  miso_rd(i) <= spi_miso;
115  spi_sclk <= '0';
116  WAIT FOR c_clk_period * 2;
117  END LOOP;
118 
119  -- Wait for 5 clocks (End-to-CS)
120  WAIT FOR c_clk_period * 5;
121 
122  -- End SPI transfer
123  REPORT "End SPI Transfer" SEVERITY note;
124  spi_cs <= '1';
125  WAIT FOR c_clk_period;
126  ASSERT (dat_wr_done = '1') REPORT "Expected dat_wr_done high for transfer done" SEVERITY error;
127 
128  -- Wait for 10 clocks
129  WAIT FOR c_clk_period * 10;
130  ASSERT (dat_wr_done = '0') REPORT "Expected dat_wr_done low after transfer done" SEVERITY error;
131 
132  -- Verify test patterns
133  REPORT "Test Transfer Data" SEVERITY note;
134  ASSERT (dat_wr_reg = mosi_wr) REPORT "Expected dat_wr_reg matches mosi_wr" SEVERITY error;
135  ASSERT (miso_rd = dat_rd_reg) REPORT "Expected miso_rd matches dat_rd_reg" SEVERITY error;
136 
137  -- Log end of test
138  REPORT "Finished" SEVERITY note;
139 
140  -- Finish the simulation
141  std.env.finish;
142 
143  END PROCESS pr_stimulus;
144 
145 END ARCHITECTURE tb;
std_logic_vector( 31 DOWNTO 0) dat_wr_reg
Data write register output from uut.
std_logic_vector( 31 DOWNTO 0) miso_rd
SPI MISO output pattern driven from uut.
in spi_mosi_instd_logic
SPI MOSI.
Definition: spi_slave.vhd:34
std_logic spi_mosi
SPI MOSI input to uut.
std_logic dat_wr_done
Data write done output from uut.
out spi_miso_outstd_logic
SPI MISO.
Definition: spi_slave.vhd:35
in clk_instd_logic
Clock.
Definition: spi_slave.vhd:30
time := 100 ns c_clk_period
Clock period.
in spi_sclk_instd_logic
SPI Clock.
Definition: spi_slave.vhd:33
std_logic spi_cs
SPI chip select input to uut.
in dat_rd_reg_instd_logic_vector( size- 1 DOWNTO 0)
Data Read Register value.
Definition: spi_slave.vhd:36
SPI Slave module.
Definition: spi_slave.vhd:25
std_logic_vector( 31 DOWNTO 0) dat_rd_reg
Data read register input to uut.
std_logic spi_sclk
SPI clock input to uut.
SPI Slave testbench module.
std_logic clk
Clock.
sizenatural range 1 TO natural'high
Size of the SPI data.
Definition: spi_slave.vhd:28
out dat_wr_done_outstd_logic
Data Write Done flag.
Definition: spi_slave.vhd:39
std_logic rst
Asynchronous reset.
_library_ ieeeieee
Using IEEE library.
in rst_instd_logic
Asynchronous reset.
Definition: spi_slave.vhd:31
std_logic_vector( 31 DOWNTO 0) mosi_wr
SPI MOSI test pattern to drive into uut.
in spi_cs_instd_logic
SPI Chip-select.
Definition: spi_slave.vhd:32
out dat_wr_reg_outstd_logic_vector( size- 1 DOWNTO 0)
Data Write Register value.
Definition: spi_slave.vhd:37
std_logic spi_miso
SPI MISO output from uut.