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

Subversion Repositories spi_boot

[/] [spi_boot/] [trunk/] [bench/] [vhdl/] [tb_elem.vhd] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 arniml
-------------------------------------------------------------------------------
2
--
3
-- SD/MMC Bootloader
4
-- Generic testbench element for a specific feature set
5
--
6
-- $Id: tb_elem.vhd,v 1.1 2005-02-08 21:09:20 arniml Exp $
7
--
8
-- Copyright (c) 2005, Arnim Laeuger (arniml@opencores.org)
9
--
10
-- All rights reserved, see COPYING.
11
--
12
-- Redistribution and use in source and synthezised forms, with or without
13
-- modification, are permitted provided that the following conditions are met:
14
--
15
-- Redistributions of source code must retain the above copyright notice,
16
-- this list of conditions and the following disclaimer.
17
--
18
-- Redistributions in synthesized form must reproduce the above copyright
19
-- notice, this list of conditions and the following disclaimer in the
20
-- documentation and/or other materials provided with the distribution.
21
--
22
-- Neither the name of the author nor the names of other contributors may
23
-- be used to endorse or promote products derived from this software without
24
-- specific prior written permission.
25
--
26
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
30
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
-- POSSIBILITY OF SUCH DAMAGE.
37
--
38
-- Please report bugs to the author, but before you do so, please
39
-- make sure that this is not a derivative work and that
40
-- you have the latest version of this file.
41
--
42
-- The latest version of this file can be found at:
43
--      http://www.opencores.org/projects.cgi/web/spi_boot/overview
44
--
45
-------------------------------------------------------------------------------
46
 
47
library ieee;
48
use ieee.std_logic_1164.all;
49
 
50
 
51
entity tb_elem is
52
 
53
  generic (
54
    chip_type_g   : string := "none";
55
    has_sd_card_g : integer := 1
56
  );
57
  port (
58
    clk_i   : in  std_logic;
59
    reset_i : in  std_logic;
60
    eos_o   : out boolean
61
  );
62
 
63
end tb_elem;
64
 
65
 
66
library ieee;
67
use ieee.numeric_std.all;
68
library std;
69
use std.textio.all;
70
 
71
use work.spi_boot_pack.all;
72
use work.tb_pack.all;
73
 
74
architecture behav of tb_elem is
75
 
76
  component chip
77
    port (
78
      clk_i          : in  std_logic;
79
      reset_i        : in  std_logic;
80
      spi_clk_o      : out std_logic;
81
      spi_cs_n_o     : out std_logic;
82
      spi_data_in_i  : in  std_logic;
83
      spi_data_out_o : out std_logic;
84
      start_i        : in  std_logic;
85
      mode_i         : in  std_logic;
86
      config_n_o     : out std_logic;
87
      cfg_init_n_i   : in  std_logic;
88
      cfg_done_i     : in  std_logic;
89
      dat_done_i     : in  std_logic;
90
      cfg_clk_o      : out std_logic;
91
      cfg_dat_o      : out std_logic
92
    );
93
  end component;
94
 
95
  component card
96
    generic (
97
      card_type_g  : string := "none";
98
      is_sd_card_g : integer := 1
99
    );
100
    port (
101
      spi_clk_i  : in  std_logic;
102
      spi_cs_n_i : in  std_logic;
103
      spi_data_i : in  std_logic;
104
      spi_data_o : out std_logic
105
    );
106
  end component;
107
 
108
  -- SPI interface signals
109
  signal spi_clk_s            : std_logic;
110
  signal spi_data_to_card_s   : std_logic;
111
  signal spi_data_from_card_s : std_logic;
112
  signal spi_cs_n_s           : std_logic;
113
 
114
  -- config related signals
115
  signal start_s      : std_logic;
116
  signal mode_s       : std_logic;
117
  signal config_n_s   : std_logic;
118
  signal cfg_init_n_s : std_logic;
119
  signal cfg_done_s   : std_logic;
120
  signal dat_done_s   : std_logic;
121
  signal cfg_clk_s    : std_logic;
122
  signal cfg_dat_s    : std_logic;
123
  signal data_s       : unsigned( 7 downto 0);
124
 
125
begin
126
 
127
  -----------------------------------------------------------------------------
128
  -- DUT
129
  -----------------------------------------------------------------------------
130
  dut_b : chip
131
    port map (
132
      clk_i          => clk_i,
133
      reset_i        => reset_i,
134
      spi_clk_o      => spi_clk_s,
135
      spi_cs_n_o     => spi_cs_n_s,
136
      spi_data_in_i  => spi_data_from_card_s,
137
      spi_data_out_o => spi_data_to_card_s,
138
      start_i        => start_s,
139
      mode_i         => mode_s,
140
      config_n_o     => config_n_s,
141
      cfg_init_n_i   => cfg_init_n_s,
142
      cfg_done_i     => cfg_done_s,
143
      dat_done_i     => dat_done_s,
144
      cfg_clk_o      => cfg_clk_s,
145
      cfg_dat_o      => cfg_dat_s
146
    );
147
 
148
  card_b : card
149
    generic map (
150
      card_type_g  => chip_type_g,
151
      is_sd_card_g => has_sd_card_g
152
    )
153
    port map (
154
      spi_clk_i  => spi_clk_s,
155
      spi_cs_n_i => spi_cs_n_s,
156
      spi_data_i => spi_data_to_card_s,
157
      spi_data_o => spi_data_from_card_s
158
    );
159
 
160
 
161
  -----------------------------------------------------------------------------
162
  -- DUT Stimuli
163
  --
164
  stim: process
165
 
166
    procedure rise_cfg_clk(num : integer) is
167
    begin
168
      for i in 1 to num loop
169
        wait until cfg_clk_s'event and cfg_clk_s = '1';
170
      end loop;
171
    end rise_cfg_clk;
172
 
173
--     procedure fall_cfg_clk(num : integer) is
174
--     begin
175
--       for i in 1 to num loop
176
--         wait until cfg_clk_s'event and cfg_clk_s = '0';
177
--       end loop;
178
--     end fall_cfg_clk;
179
 
180
    procedure rise_clk(num : integer) is
181
    begin
182
      for i in 1 to num loop
183
        wait until clk_i'event and clk_i = '1';
184
      end loop;
185
    end rise_clk;
186
 
187
    procedure read_check_byte(ref : unsigned(7 downto 0)) is
188
      variable byte_v : unsigned(7 downto 0);
189
      variable dump_line : line;
190
    begin
191
      for bit in 7 downto 0 loop
192
        rise_cfg_clk(1);
193
        byte_v(bit) := cfg_dat_s;
194
      end loop;
195
      data_s <= byte_v;
196
 
197
      if byte_v /= ref then
198
        write(dump_line, chip_type_g);
199
        write(dump_line, string'(" at "));
200
        write(dump_line, now);
201
        write(dump_line, string'(": read_check_byte failed "));
202
        write(dump_line, to_integer(byte_v));
203
        write(dump_line, string'(" "));
204
        write(dump_line, to_integer(ref));
205
        writeline(output, dump_line);
206
      end if;
207
    end read_check_byte;
208
 
209
    variable dump_line : line;
210
    variable addr_v    : unsigned(31 downto 0);
211
    variable temp_v    : unsigned( 7 downto 0);
212
 
213
  begin
214
    -- default assignments
215
    -- these defaults show the required pull resistors
216
    -- except start_i as this must be pulled high for automatic start
217
    start_s      <= '0';
218
    mode_s       <= '1';
219
    cfg_init_n_s <= '1';
220
    cfg_done_s   <= '0';
221
    dat_done_s   <= '0';
222
    data_s       <= (others => '1');
223
    addr_v       := (others => '0');
224
    eos_o        <= false;
225
 
226
    wait for 100 us;
227
    -- signal start
228
    start_s <= '1';
229
    wait until config_n_s = '0';
230
    -- run through configuration sequence
231
    rise_clk(1);
232
    cfg_init_n_s <= '0';
233
    rise_clk(3);
234
    cfg_init_n_s <= '1';
235
 
236
    -- and receive 32 bytes from set 0
237
    for i in 1 to 32 loop
238
      temp_v := addr_v(0) & calc_crc(addr_v);
239
      read_check_byte(temp_v);
240
      addr_v := addr_v + 1;
241
    end loop;
242
    start_s    <= '0';
243
    cfg_done_s <= '1';
244
 
245
    rise_clk(10);
246
 
247
    -- request next set
248
    mode_s  <= '0';
249
    start_s <= '1';
250
    addr_v  := (others => '0');
251
    addr_v(19 downto 18) := "01"; -- must match num_bits_per_set_g in chip-*-a.vhd
252
 
253
    -- receive another 32 bytes from set 1
254
    for i in 1 to 32 loop
255
      temp_v := addr_v(0) & calc_crc(addr_v);
256
      read_check_byte(temp_v);
257
      addr_v := addr_v + 1;
258
    end loop;
259
    start_s    <= '0';
260
    dat_done_s <= '1';
261
 
262
 
263
    rise_clk(10);
264
 
265
    -- request next set
266
    mode_s  <= '1';
267
    start_s <= '1';
268
    addr_v  := (others => '0');
269
    addr_v(19 downto 18) := "10"; -- must match num_bits_per_set_g in chip-*-a.vhd
270
 
271
    wait until config_n_s = '0';
272
    -- run through configuration sequence
273
    rise_clk(1);
274
    cfg_done_s   <= '0';
275
    cfg_init_n_s <= '0';
276
    rise_clk(3);
277
    cfg_init_n_s <= '1';
278
 
279
    -- receive another 32 bytes from set 2
280
    for i in 1 to 32 loop
281
      temp_v := addr_v(0) & calc_crc(addr_v);
282
      read_check_byte(temp_v);
283
      addr_v := addr_v + 1;
284
    end loop;
285
    start_s    <= '0';
286
    cfg_done_s <= '1';
287
 
288
    -- give dut a chance to stop current transfer
289
    wait until spi_cs_n_s = '1';
290
    rise_clk(10);
291
    eos_o <= true;
292
    wait;
293
  end process stim;
294
  --
295
  -----------------------------------------------------------------------------
296
 
297
end behav;
298
 
299
 
300
-------------------------------------------------------------------------------
301
-- File History:
302
--
303
-- $Log: not supported by cvs2svn $
304
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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