OpenCores
URL https://opencores.org/ocsvn/spi_master_slave/spi_master_slave/trunk

Subversion Repositories spi_master_slave

[/] [spi_master_slave/] [trunk/] [rtl/] [spi_master_slave/] [spi_loopback_test.vhd] - Blame information for rev 24

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jdoin
--------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer:        Jonny Doin
4
--
5
-- Create Date:     22:59:18 04/25/2011
6
-- Design Name:     spi_master_slave
7
-- Module Name:     spi_master_slave/spi_loopback_test.vhd
8
-- Project Name:    SPI_interface
9
-- Target Device:   Spartan-6
10
-- Tool versions:   ISE 13.1
11
-- Description:     Testbench to simulate the master and slave SPI interfaces. Each module can be tested
12
--                  in a "real" environment, where the 'spi_master' exchanges data with the 'spi_slave'
13
--                  module, simulating the internal working of each design.
14
--                  In behavioral simulation, select a matching data width (N) and spi mode (CPOL, CPHA) for
15
--                  both modules, and also a different clock domain for each parallel interface.
16
--                  Different values for PREFETCH for each interface can be tested, to model the best value
17
--                  for the pipelined memory / bus that is attached to the di/do ports.
18
--                  To test the parallel interfaces, a simple ROM memory is simulated for each interface, with
19
--                  8 words of data to be sent, synchronous to each clock and flow control signals.
20
--
21
-- 
22
-- VHDL Test Bench Created by ISE for modules: 'spi_master' and 'spi_slave'
23
-- 
24
-- Dependencies:
25
-- 
26
-- Revision:
27
-- Revision 0.01 - File Created
28
-- Revision 0.10 - Implemented FIFO simulation for each interface.
29
-- Additional Comments:
30
--
31
-- Notes: 
32
-- This testbench has been automatically generated using types std_logic and
33
-- std_logic_vector for the ports of the unit under test.  Xilinx recommends
34
-- that these types always be used for the top-level I/O of a design in order
35
-- to guarantee that the testbench will bind correctly to the post-implementation 
36
-- simulation model.
37
--------------------------------------------------------------------------------
38
LIBRARY ieee;
39
USE ieee.std_logic_1164.ALL;
40
USE ieee.numeric_std.ALL;
41
 
42
library work;
43
use work.all;
44
 
45
ENTITY spi_loopback_test IS
46
    GENERIC (
47
        N : positive := 32;                                 -- 32bit serial word length is default
48
        CPOL : std_logic := '0';                            -- SPI mode selection (mode 0 default)
49
        CPHA : std_logic := '1';                            -- CPOL = clock polarity, CPHA = clock phase.
50
        PREFETCH : positive := 2                            -- prefetch lookahead cycles
51
    );
52
END spi_loopback_test;
53
 
54
ARCHITECTURE behavior OF spi_loopback_test IS
55
 
56
    --=========================================================
57
    -- Component declaration for the Unit Under Test (UUT)
58
    --=========================================================
59
 
60
        COMPONENT spi_loopback
61
        PORT(
62
                m_clk_i : IN std_logic;
63
                m_rst_i : IN std_logic;
64
                m_spi_miso_i : IN std_logic;
65
                m_di_i : IN std_logic_vector(31 downto 0);
66
                m_wren_i : IN std_logic;
67
                s_clk_i : IN std_logic;
68
                s_spi_ssel_i : IN std_logic;
69
                s_spi_sck_i : IN std_logic;
70
                s_spi_mosi_i : IN std_logic;
71
                s_di_i : IN std_logic_vector(31 downto 0);
72
                s_wren_i : IN std_logic;
73
                m_spi_ssel_o : OUT std_logic;
74
                m_spi_sck_o : OUT std_logic;
75
                m_spi_mosi_o : OUT std_logic;
76
                m_di_req_o : OUT std_logic;
77
                m_do_valid_o : OUT std_logic;
78
                m_do_o : OUT std_logic_vector(31 downto 0);
79
                m_do_transfer_o : OUT std_logic;
80
                m_wren_o : OUT std_logic;
81
                m_wren_ack_o : OUT std_logic;
82
                m_rx_bit_reg_o : OUT std_logic;
83
                m_state_dbg_o : OUT std_logic_vector(5 downto 0);
84
                m_core_clk_o : OUT std_logic;
85
                m_core_n_clk_o : OUT std_logic;
86
                m_sh_reg_dbg_o : OUT std_logic_vector(31 downto 0);
87
                s_spi_miso_o : OUT std_logic;
88
                s_di_req_o : OUT std_logic;
89
                s_do_valid_o : OUT std_logic;
90
                s_do_o : OUT std_logic_vector(31 downto 0);
91
                s_do_transfer_o : OUT std_logic;
92
                s_wren_o : OUT std_logic;
93
                s_wren_ack_o : OUT std_logic;
94
                s_rx_bit_reg_o : OUT std_logic;
95
                s_state_dbg_o : OUT std_logic_vector(5 downto 0)
96
                );
97
        END COMPONENT;
98
 
99
    --=========================================================
100
    -- constants
101
    --=========================================================
102
    constant fifo_memory_size : integer := 16;
103
 
104
    --=========================================================
105
    -- types
106
    --=========================================================
107
    type fifo_memory_type is array (0 to fifo_memory_size-1) of std_logic_vector (N-1 downto 0);
108
 
109
    --=========================================================
110
    -- signals to connect the instances
111
    --=========================================================
112
    -- internal clk and rst
113
    signal m_clk : std_logic := '0';                -- clock domain for the master parallel interface. Must be faster than spi bus sck.
114
    signal s_clk : std_logic := '0';                -- clock domain for the slave parallel interface. Must be faster than spi bus sck.
115
    signal rst : std_logic := 'U';
116
    -- spi bus wires
117
    signal spi_sck : std_logic;
118
    signal spi_ssel : std_logic;
119
    signal spi_miso : std_logic;
120
    signal spi_mosi : std_logic;
121
    -- master parallel interface
122
    signal di_m : std_logic_vector (N-1 downto 0) := (others => '0');
123
    signal do_m : std_logic_vector (N-1 downto 0) := (others => 'U');
124
    signal do_valid_m : std_logic;
125
    signal do_transfer_m : std_logic;
126
    signal di_req_m : std_logic;
127
    signal wren_m : std_logic := '0';
128
    signal wren_o_m : std_logic := 'U';
129
    signal wren_ack_o_m : std_logic := 'U';
130
    signal rx_bit_reg_m : std_logic;
131
    signal state_m : std_logic_vector (5 downto 0);
132
    signal core_clk_o_m : std_logic;
133
    signal core_n_clk_o_m : std_logic;
134
    signal sh_reg_m : std_logic_vector (N-1 downto 0) := (others => '0');
135
    -- slave parallel interface
136
    signal di_s : std_logic_vector (N-1 downto 0) := (others => '0');
137
    signal do_s : std_logic_vector (N-1 downto 0);
138
    signal do_valid_s : std_logic;
139
    signal do_transfer_s : std_logic;
140
    signal di_req_s : std_logic;
141
    signal wren_s : std_logic := '0';
142
    signal wren_o_s : std_logic := 'U';
143
    signal wren_ack_o_s : std_logic := 'U';
144
    signal rx_bit_reg_s : std_logic;
145
    signal state_s : std_logic_vector (5 downto 0);
146
--    signal sh_reg_s : std_logic_vector (N-1 downto 0);
147
 
148
    --=========================================================
149
    -- Clock period definitions
150
    --=========================================================
151
    constant m_clk_period : time := 10 ns;          -- 100MHz master parallel clock
152
    constant s_clk_period : time := 10 ns;          -- 100MHz slave parallel clock
153
 
154
BEGIN
155
 
156
    --=========================================================
157
    -- Component instantiation for the Unit Under Test (UUT)
158
    --=========================================================
159
 
160
    Inst_spi_loopback: spi_loopback
161
    port map(
162
        ----------------MASTER-----------------------
163
        m_clk_i => m_clk,
164
        m_rst_i => rst,
165
        m_spi_ssel_o => spi_ssel,
166
        m_spi_sck_o => spi_sck,
167
        m_spi_mosi_o => spi_mosi,
168
        m_spi_miso_i => spi_miso,
169
        m_di_req_o => di_req_m,
170
        m_di_i => di_m,
171
        m_wren_i => wren_m,
172
        m_do_valid_o => do_valid_m,
173
        m_do_o => do_m,
174
        ----- debug -----
175
        m_do_transfer_o => do_transfer_m,
176
        m_wren_o => wren_o_m,
177
        m_wren_ack_o => wren_ack_o_m,
178
        m_rx_bit_reg_o => rx_bit_reg_m,
179
        m_state_dbg_o => state_m,
180
        m_core_clk_o => core_clk_o_m,
181
        m_core_n_clk_o => core_n_clk_o_m,
182
        m_sh_reg_dbg_o => sh_reg_m,
183
        ----------------SLAVE-----------------------
184
        s_clk_i => s_clk,
185
        s_spi_ssel_i => spi_ssel,
186
        s_spi_sck_i => spi_sck,
187
        s_spi_mosi_i => spi_mosi,
188
        s_spi_miso_o => spi_miso,
189
        s_di_req_o => di_req_s,
190
        s_di_i => di_s,
191
        s_wren_i => wren_s,
192
        s_do_valid_o => do_valid_s,
193
        s_do_o => do_s,
194
        ----- debug -----
195
        s_do_transfer_o => do_transfer_s,
196
        s_wren_o => wren_o_s,
197
        s_wren_ack_o => wren_ack_o_s,
198
        s_rx_bit_reg_o => rx_bit_reg_s,
199
        s_state_dbg_o => state_s
200
--        s_sh_reg_dbg_o => sh_reg_s
201
    );
202
 
203
    --=========================================================
204
    -- Clock generator processes
205
    --=========================================================
206
    m_clk_process : process
207
    begin
208
        m_clk <= '0';
209
        wait for m_clk_period/2;
210
        m_clk <= '1';
211
        wait for m_clk_period/2;
212
    end process m_clk_process;
213
 
214
    s_clk_process : process
215
    begin
216
        s_clk <= '0';
217
        wait for s_clk_period/2;
218
        s_clk <= '1';
219
        wait for s_clk_period/2;
220
    end process s_clk_process;
221
 
222
    --=========================================================
223
    -- rst_i process
224
    --=========================================================
225
    rst <= '0', '1' after 20 ns, '0' after 100 ns;
226
 
227
    --=========================================================
228
    -- Master interface process
229
    --=========================================================
230
    master_tx_fifo_proc: process is
231
        variable fifo_memory : fifo_memory_type :=
232
            (X"87654321",X"abcdef01",X"faceb007",X"10203049",X"85a5a5a5",X"7aaa5551",X"7adecabe",X"57564789",
233
             X"12345678",X"beefbeef",X"fee1600d",X"f158ba17",X"5ee1a7e3",X"101096da",X"600ddeed",X"deaddead");
234
        variable fifo_head : integer range 0 to fifo_memory_size-1;
235
    begin
236
        -- synchronous rst_i
237
        wait until rst = '1';
238
        wait until m_clk'event and m_clk = '1';
239
        di_m <= (others => '0');
240
        wren_m <= '0';
241
        fifo_head := 0;
242
        wait until rst = '0';
243
        wait until di_req_m = '1';                          -- wait shift register request for data
244
        -- load next fifo contents into shift register
245
        for cnt in 0 to (fifo_memory_size/2)-1 loop
246
            fifo_head := cnt;                               -- pre-compute next pointer 
247
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
248
            di_m <= fifo_memory(fifo_head);                 -- place data into tx_data input bus
249
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
250
            wren_m <= '1';                                  -- write data into spi master
251
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
252
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
253
            wren_m <= '0';                                  -- remove write enable signal
254
            wait until di_req_m = '1';                      -- wait shift register request for data
255
        end loop;
256
        wait until spi_ssel = '1';
257
        wait for 2000 ns;
258
        for cnt in (fifo_memory_size/2) to fifo_memory_size-1 loop
259
            fifo_head := cnt;                               -- pre-compute next pointer 
260
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
261
            di_m <= fifo_memory(fifo_head);                 -- place data into tx_data input bus
262
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
263
            wren_m <= '1';                                  -- write data into spi master
264
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
265
            wait until m_clk'event and m_clk = '1';         -- sync fifo data load at next rising edge
266
            wren_m <= '0';                                  -- remove write enable signal
267
            wait until di_req_m = '1';                      -- wait shift register request for data
268
        end loop;
269
        wait;
270
    end process master_tx_fifo_proc;
271
 
272
 
273
    --=========================================================
274
    -- Slave interface process
275
    --=========================================================
276
    slave_tx_fifo_proc: process is
277
        variable fifo_memory : fifo_memory_type :=
278
            (X"90201031",X"97640231",X"ef55aaf1",X"babaca51",X"b00b1ee5",X"51525354",X"81828384",X"91929394",
279
             X"be575ec5",X"2fa57410",X"cafed0ce",X"afadab0a",X"bac7ed1a",X"f05fac75",X"2acbac7e",X"12345678");
280
        variable fifo_head : integer range 0 to fifo_memory_size-1;
281
    begin
282
        -- synchronous rst_i
283
        wait until rst = '1';
284
        wait until s_clk'event and s_clk = '1';
285
        di_s <= (others => '0');
286
        wren_s <= '0';
287
        fifo_head := 0;
288
        wait until rst = '0';
289
        wait until di_req_s = '1';                          -- wait shift register request for data
290
        -- load next fifo contents into shift register
291
        for cnt in 0 to fifo_memory_size-1 loop
292
            fifo_head := cnt;                               -- pre-compute next pointer 
293
            wait until s_clk'event and s_clk = '1';         -- sync fifo data load at next rising edge
294
            di_s <= fifo_memory(fifo_head);                 -- place data into tx_data input bus
295
            wait until s_clk'event and s_clk = '1';         -- sync fifo data load at next rising edge
296
            wren_s <= '1';                                  -- write data into shift register
297
            wait until s_clk'event and s_clk = '1';         -- sync fifo data load at next rising edge
298
            wait until s_clk'event and s_clk = '1';         -- sync fifo data load at next rising edge
299
            wren_s <= '0';                                  -- remove write enable signal
300
            wait until di_req_s = '1';                      -- wait shift register request for data
301
        end loop;
302
        wait;
303
    end process slave_tx_fifo_proc;
304
 
305
END ARCHITECTURE behavior;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.