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 37

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 37 arniml
-- $Id: tb_elem.vhd,v 1.6 2005-03-09 19:48:04 arniml Exp $
7 4 arniml
--
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 35 arniml
      set_sel_n_i    : in  std_logic_vector(3 downto 0);
81 4 arniml
      spi_clk_o      : out std_logic;
82
      spi_cs_n_o     : out std_logic;
83
      spi_data_in_i  : in  std_logic;
84
      spi_data_out_o : out std_logic;
85
      start_i        : in  std_logic;
86
      mode_i         : in  std_logic;
87
      config_n_o     : out std_logic;
88
      cfg_init_n_i   : in  std_logic;
89
      cfg_done_i     : in  std_logic;
90
      dat_done_i     : in  std_logic;
91
      cfg_clk_o      : out std_logic;
92
      cfg_dat_o      : out std_logic
93
    );
94
  end component;
95
 
96
  component card
97
    generic (
98
      card_type_g  : string := "none";
99
      is_sd_card_g : integer := 1
100
    );
101
    port (
102
      spi_clk_i  : in  std_logic;
103
      spi_cs_n_i : in  std_logic;
104
      spi_data_i : in  std_logic;
105
      spi_data_o : out std_logic
106
    );
107
  end component;
108
 
109 35 arniml
  signal reset_s : std_logic;
110
 
111 4 arniml
  -- SPI interface signals
112
  signal spi_clk_s            : std_logic;
113
  signal spi_data_to_card_s   : std_logic;
114
  signal spi_data_from_card_s : std_logic;
115
  signal spi_cs_n_s           : std_logic;
116
 
117
  -- config related signals
118
  signal start_s      : std_logic;
119
  signal mode_s       : std_logic;
120
  signal config_n_s   : std_logic;
121
  signal cfg_init_n_s : std_logic;
122
  signal cfg_done_s   : std_logic;
123
  signal dat_done_s   : std_logic;
124
  signal cfg_clk_s    : std_logic;
125
  signal cfg_dat_s    : std_logic;
126 35 arniml
  signal data_s       : unsigned(7 downto 0);
127 4 arniml
 
128 35 arniml
  signal set_sel_n_s : std_logic_vector(3 downto 0);
129
 
130 37 arniml
  constant verbose_c : boolean := false;
131
 
132 4 arniml
begin
133
 
134 15 arniml
  -- weak pull-ups
135
  spi_clk_s          <= 'H';
136
  spi_cs_n_s         <= 'H';
137
  spi_data_to_card_s <= 'H';
138
 
139 4 arniml
  -----------------------------------------------------------------------------
140
  -- DUT
141
  -----------------------------------------------------------------------------
142
  dut_b : chip
143
    port map (
144
      clk_i          => clk_i,
145 35 arniml
      reset_i        => reset_s,
146
      set_sel_n_i    => set_sel_n_s,
147 4 arniml
      spi_clk_o      => spi_clk_s,
148
      spi_cs_n_o     => spi_cs_n_s,
149
      spi_data_in_i  => spi_data_from_card_s,
150
      spi_data_out_o => spi_data_to_card_s,
151
      start_i        => start_s,
152
      mode_i         => mode_s,
153
      config_n_o     => config_n_s,
154
      cfg_init_n_i   => cfg_init_n_s,
155
      cfg_done_i     => cfg_done_s,
156
      dat_done_i     => dat_done_s,
157
      cfg_clk_o      => cfg_clk_s,
158
      cfg_dat_o      => cfg_dat_s
159
    );
160
 
161
  card_b : card
162
    generic map (
163
      card_type_g  => chip_type_g,
164
      is_sd_card_g => has_sd_card_g
165
    )
166
    port map (
167
      spi_clk_i  => spi_clk_s,
168
      spi_cs_n_i => spi_cs_n_s,
169
      spi_data_i => spi_data_to_card_s,
170
      spi_data_o => spi_data_from_card_s
171
    );
172
 
173
 
174
  -----------------------------------------------------------------------------
175
  -- DUT Stimuli
176
  --
177
  stim: process
178
 
179
    procedure rise_cfg_clk(num : integer) is
180
    begin
181
      for i in 1 to num loop
182
        wait until cfg_clk_s'event and cfg_clk_s = '1';
183
      end loop;
184
    end rise_cfg_clk;
185
 
186
--     procedure fall_cfg_clk(num : integer) is
187
--     begin
188
--       for i in 1 to num loop
189
--         wait until cfg_clk_s'event and cfg_clk_s = '0';
190
--       end loop;
191
--     end fall_cfg_clk;
192
 
193
    procedure rise_clk(num : integer) is
194
    begin
195
      for i in 1 to num loop
196
        wait until clk_i'event and clk_i = '1';
197
      end loop;
198
    end rise_clk;
199
 
200
    procedure read_check_byte(ref : unsigned(7 downto 0)) is
201
      variable byte_v : unsigned(7 downto 0);
202
      variable dump_line : line;
203
    begin
204
      for bit in 7 downto 0 loop
205
        rise_cfg_clk(1);
206
        byte_v(bit) := cfg_dat_s;
207
      end loop;
208
      data_s <= byte_v;
209
 
210
      if byte_v /= ref then
211
        write(dump_line, chip_type_g);
212
        write(dump_line, string'(" at "));
213
        write(dump_line, now);
214
        write(dump_line, string'(": read_check_byte failed "));
215
        write(dump_line, to_integer(byte_v));
216
        write(dump_line, string'(" "));
217
        write(dump_line, to_integer(ref));
218
        writeline(output, dump_line);
219
      end if;
220
    end read_check_byte;
221
 
222
    variable dump_line : line;
223
    variable addr_v    : unsigned(31 downto 0);
224
    variable temp_v    : unsigned( 7 downto 0);
225 35 arniml
    variable set_sel_v : unsigned(3 downto 0);
226 4 arniml
 
227
  begin
228
    -- default assignments
229
    -- these defaults show the required pull resistors
230
    -- except start_i as this must be pulled high for automatic start
231
    start_s      <= '0';
232
    mode_s       <= '1';
233
    cfg_init_n_s <= '1';
234
    cfg_done_s   <= '0';
235 7 arniml
    dat_done_s   <= '1';
236 4 arniml
    data_s       <= (others => '1');
237
    addr_v       := (others => '0');
238
    eos_o        <= false;
239 35 arniml
    set_sel_n_s  <= (others => '1');
240
    reset_s      <= '0';
241 4 arniml
 
242 35 arniml
    -- loop through some sets
243
    for set in 0 to 3 loop
244
      set_sel_v := to_unsigned(set, 4);
245
      addr_v(23 downto 20) := set_sel_v;  -- must match num_bits_per_img_g
246
                                          -- plus width_img_cnt_g
247
      set_sel_n_s <= not std_logic_vector(set_sel_v);
248 4 arniml
 
249 37 arniml
      assert not verbose_c
250 35 arniml
        report chip_type_g & ": Processing set " & to_string(set)
251
        severity note;
252 4 arniml
 
253 35 arniml
      wait for 100 us;
254
      reset_s <= '1';
255 4 arniml
 
256 37 arniml
      assert not verbose_c
257 35 arniml
        report chip_type_g & ": Requesting image 0"
258
        severity note;
259 4 arniml
 
260 35 arniml
      -- signal start
261
      start_s    <= '1';
262
      mode_s     <= '1';
263
      cfg_done_s <= '0';
264
      addr_v(19 downto 0) := (others => '0');
265
      wait until config_n_s = '0';
266
      -- run through configuration sequence
267
      rise_clk(1);
268
      cfg_init_n_s <= '0';
269
      rise_clk(3);
270
      cfg_init_n_s <= '1';
271 4 arniml
 
272 35 arniml
      -- and receive 32 bytes from image 0
273
      for i in 1 to 32 loop
274
        temp_v := addr_v(0) & calc_crc(addr_v);
275
        read_check_byte(temp_v);
276
        addr_v := addr_v + 1;
277
      end loop;
278
      start_s    <= '0';
279
      cfg_done_s <= '1';
280 4 arniml
 
281 35 arniml
      rise_clk(10);
282 4 arniml
 
283 37 arniml
      assert not verbose_c
284 35 arniml
        report chip_type_g & ": Requesting image 1"
285
        severity note;
286 4 arniml
 
287 35 arniml
      -- request next image
288
      mode_s  <= '0';
289
      start_s <= '1';
290
      addr_v(17 downto  0) := (others => '0');
291
      addr_v(19 downto 18) := "01"; -- must match num_bits_per_img_g in chip-*-a.vhd
292
      dat_done_s <= '0';
293
 
294
      -- receive another 32 bytes from image 1
295
      for i in 1 to 32 loop
296
        temp_v := addr_v(0) & calc_crc(addr_v);
297
        read_check_byte(temp_v);
298
        addr_v := addr_v + 1;
299
      end loop;
300
      start_s    <= '0';
301
      dat_done_s <= '1';
302
 
303
 
304
      rise_clk(10);
305
 
306 37 arniml
      assert not verbose_c
307 35 arniml
        report chip_type_g & ": Requesting image 2"
308
        severity note;
309
 
310
      -- request next image
311
      mode_s  <= '1';
312
      start_s <= '1';
313
      addr_v(17 downto  0) := (others => '0');
314
      addr_v(19 downto 18) := "10"; -- must match num_bits_per_img_g in chip-*-a.vhd
315
 
316
      wait until config_n_s = '0';
317
      -- run through configuration sequence
318
      rise_clk(1);
319
      cfg_done_s   <= '0';
320
      cfg_init_n_s <= '0';
321
      rise_clk(3);
322
      cfg_init_n_s <= '1';
323
 
324
      -- receive another 32 bytes from image 2
325
      for i in 1 to 32 loop
326
        temp_v := addr_v(0) & calc_crc(addr_v);
327
        read_check_byte(temp_v);
328
        addr_v := addr_v + 1;
329
      end loop;
330
      start_s    <= '0';
331
      cfg_done_s <= '1';
332
 
333
      -- give dut a chance to stop current transfer
334
      wait until spi_cs_n_s = '1';
335
      rise_clk(10);
336
 
337
      reset_s <= '0';
338 4 arniml
    end loop;
339
 
340
    eos_o <= true;
341
    wait;
342
  end process stim;
343
  --
344
  -----------------------------------------------------------------------------
345
 
346
end behav;
347
 
348
 
349
-------------------------------------------------------------------------------
350
-- File History:
351
--
352
-- $Log: not supported by cvs2svn $
353 37 arniml
-- Revision 1.5  2005/03/08 22:06:21  arniml
354
-- added set selection
355
--
356 35 arniml
-- Revision 1.4  2005/02/17 18:59:23  arniml
357
-- clarify wording for images
358
--
359 16 arniml
-- Revision 1.3  2005/02/16 19:34:56  arniml
360
-- add weak pull-ups for SPI lines
361
--
362 15 arniml
-- Revision 1.2  2005/02/13 17:14:03  arniml
363
-- change dat_done handling
364
--
365 7 arniml
-- Revision 1.1  2005/02/08 21:09:20  arniml
366
-- initial check-in
367
--
368 4 arniml
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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