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

Subversion Repositories sd_card_controller

[/] [sd_card_controller/] [trunk/] [bench/] [verilog/] [sd_data_master_tb.sv] - Blame information for rev 3

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

Line No. Rev Author Line
1 3 rozpruwacz
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// WISHBONE SD Card Controller IP Core                          ////
4
////                                                              ////
5
//// sd_data_master_tb.sv                                         ////
6
////                                                              ////
7
//// This file is part of the WISHBONE SD Card                    ////
8
//// Controller IP Core project                                   ////
9
//// http://www.opencores.org/cores/xxx/                          ////
10
////                                                              ////
11
//// Description                                                  ////
12
//// testbench for sd_data_master module                          ////
13
////                                                              ////
14
//// Author(s):                                                   ////
15
////     - Marek Czerski, ma.czerski@gmail.com                    ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2013 Authors                                   ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE. See the GNU Lesser General Public License for more  ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43
 
44
`include "sd_defines.h"
45
 
46
module sd_data_master_tb();
47
 
48
parameter SD_TCLK = 20; // 50 MHz -> timescale 1ns
49
 
50
reg sd_clk;
51
reg rst;
52
reg start_tx_i;
53
reg start_rx_i;
54
wire d_write_o;
55
wire d_read_o;
56
wire start_tx_fifo_o;
57
wire start_rx_fifo_o;
58
reg tx_fifo_empty_i;
59
reg tx_fifo_full_i;
60
reg rx_fifo_full_i;
61
reg xfr_complete_i;
62
reg crc_ok_i;
63
//2 - fifo un/ov
64
//1 - wrong crc
65
//0 - completed
66
wire [`INT_DATA_SIZE-1:0] int_status_o;
67
reg int_status_rst_i;
68
 
69
task test_read;
70
    input crc_ok;
71
    begin
72
        start_tx_i = 1; //start filling tx fifo
73
        #SD_TCLK;
74
        start_tx_i = 0;
75
        wait(start_tx_fifo_o == 1);
76
        #(SD_TCLK/2);
77
        assert(d_write_o == 0);
78
        assert(d_read_o == 0);
79
        assert(start_rx_fifo_o == 0);
80
        #(4*SD_TCLK);
81
        tx_fifo_full_i = 1; //tx fifo is full
82
        wait(d_write_o == 1);
83
        #(SD_TCLK/2);
84
        xfr_complete_i = 0; //serial_data_host starts its state machine
85
        assert(d_read_o == 0);
86
        assert(start_tx_fifo_o == 1);
87
        assert(start_rx_fifo_o == 0);
88
        #(2*SD_TCLK);
89
        //check if d_write_o went low
90
        assert(d_write_o == 0);
91
        #(10*SD_TCLK);
92
        tx_fifo_full_i = 0;
93
        xfr_complete_i = 1;
94
        crc_ok_i = crc_ok;
95
        #SD_TCLK;
96
        //xfr_complete_i = 0; //signl stays in logic 1 after transmission
97
        crc_ok_i = 0;
98
        assert(d_write_o == 0);
99
        assert(d_read_o == 0);
100
        wait(start_tx_fifo_o == 0);
101
        #(SD_TCLK/2);
102
        assert(start_rx_fifo_o == 0);
103
        if (crc_ok)
104
            assert(int_status_o == 1);
105
        else
106
            assert(int_status_o == 2);
107
 
108
        //reset int status
109
        int_status_rst_i = 1;
110
        #SD_TCLK;
111
        int_status_rst_i = 0;
112
        assert(int_status_o == 0);
113
    end
114
endtask
115
 
116
task test_write;
117
    input crc_ok;
118
    begin
119
        start_rx_i = 1; //start filling rx fifo and start sd_data_serial_host
120
        #SD_TCLK;
121
        start_rx_i = 0;
122
        wait(start_rx_fifo_o == 1 && d_read_o == 1);
123
        #(SD_TCLK/2);
124
        xfr_complete_i = 0; //serial_data_host starts its state machine
125
        assert(d_write_o == 0);
126
        assert(start_tx_fifo_o == 0);
127
        #(2*SD_TCLK);
128
        //check if d_read_o went low
129
        assert(d_read_o == 0);
130
        #(10*SD_TCLK);
131
        xfr_complete_i = 1;
132
        crc_ok_i = crc_ok;
133
        #SD_TCLK;
134
        //xfr_complete_i = 0; //signl stays in logic 1 after transmission
135
        crc_ok_i = 0;
136
        assert(d_write_o == 0);
137
        assert(d_read_o == 0);
138
        wait(start_rx_fifo_o == 0);
139
        #(SD_TCLK/2);
140
        assert(start_rx_fifo_o == 0);
141
        if (crc_ok)
142
            assert(int_status_o == 1);
143
        else
144
            assert(int_status_o == 2);
145
 
146
        //reset int status
147
        int_status_rst_i = 1;
148
        #SD_TCLK;
149
        int_status_rst_i = 0;
150
        assert(int_status_o == 0);
151
    end
152
endtask
153
 
154
sd_data_master sd_data_master_dut(
155
           .sd_clk(sd_clk),
156
           .rst(rst),
157
           .start_tx_i(start_tx_i),
158
           .start_rx_i(start_rx_i),
159
           .d_write_o(d_write_o),
160
           .d_read_o(d_read_o),
161
           .start_tx_fifo_o(start_tx_fifo_o),
162
           .start_rx_fifo_o(start_rx_fifo_o),
163
           .tx_fifo_empty_i(tx_fifo_empty_i),
164
           .tx_fifo_full_i(tx_fifo_full_i),
165
           .rx_fifo_full_i(rx_fifo_full_i),
166
           .xfr_complete_i(xfr_complete_i),
167
           .crc_ok_i(crc_ok_i),
168
           .int_status_o(int_status_o),
169
           .int_status_rst_i(int_status_rst_i)
170
       );
171
 
172
// Generating sd_clk clock
173
always
174
begin
175
    sd_clk=0;
176
    forever #(SD_TCLK/2) sd_clk = ~sd_clk;
177
end
178
 
179
initial
180
begin
181
    rst = 1;
182
    start_tx_i = 0;
183
    start_rx_i = 0;
184
    tx_fifo_empty_i = 0;
185
    tx_fifo_full_i = 0;
186
    rx_fifo_full_i = 0;
187
    xfr_complete_i = 1; //this signal is 0 only when serial_data_host is busy
188
    crc_ok_i = 0;
189
    int_status_rst_i = 0;
190
 
191
    $display("sd_data_master_tb start ...");
192
 
193
    #(3*SD_TCLK);
194
    rst = 0;
195
    assert(d_write_o == 0);
196
    assert(d_read_o == 0);
197
    assert(start_tx_fifo_o == 0);
198
    assert(start_rx_fifo_o == 0);
199
    assert(int_status_o == 0);
200
    #(3*SD_TCLK);
201
    assert(d_write_o == 0);
202
    assert(d_read_o == 0);
203
    assert(start_tx_fifo_o == 0);
204
    assert(start_rx_fifo_o == 0);
205
    assert(int_status_o == 0);
206
 
207
    //just check simple read and simple write, the rest is done by the sd_data_serial_host
208
    //////////////////////////////////////////////////////////////////////////////////////
209
    test_read(1);
210
 
211
    //write test
212
    /////////////////////////////////////////////////////////////////////
213
    test_write(1);
214
 
215
    //check fifo error during read / write
216
    //////////////////////////////////////////////////////////////////////
217
    start_tx_i = 1; //start filling tx fifo
218
    #SD_TCLK;
219
    start_tx_i = 0;
220
    wait(start_tx_fifo_o == 1);
221
    #(4.5*SD_TCLK);
222
    tx_fifo_full_i = 1; //tx fifo is full
223
    wait(d_write_o == 1);
224
    xfr_complete_i = 0;
225
    #(10.5*SD_TCLK);
226
    tx_fifo_full_i = 0;
227
    tx_fifo_empty_i = 1;
228
    #SD_TCLK;
229
    tx_fifo_empty_i = 0;
230
    assert(d_write_o == 1);
231
    assert(d_read_o == 1);
232
    wait(start_tx_fifo_o == 0);
233
    #(SD_TCLK/2);
234
    xfr_complete_i = 1;
235
    assert(start_rx_fifo_o == 0);
236
    assert(int_status_o == 4);
237
 
238
    //reset int status
239
    int_status_rst_i = 1;
240
    #SD_TCLK;
241
    int_status_rst_i = 0;
242
    assert(int_status_o == 0);
243
 
244
    //write test
245
    //////////////////////////////////////////////////////////////////////
246
    start_rx_i = 1; //start filling rx fifo and start sd_data_serial_host
247
    #SD_TCLK;
248
    start_rx_i = 0;
249
    wait(start_rx_fifo_o == 1 && d_read_o == 1);
250
    xfr_complete_i = 0;
251
    #(10.5*SD_TCLK);
252
    rx_fifo_full_i = 1;
253
    #SD_TCLK;
254
    rx_fifo_full_i = 0;
255
    assert(d_write_o == 1);
256
    assert(d_read_o == 1);
257
    wait(start_rx_fifo_o == 0);
258
    #(SD_TCLK/2);
259
    xfr_complete_i = 1;
260
    assert(start_rx_fifo_o == 0);
261
    assert(int_status_o == 4);
262
 
263
    //reset int status
264
    int_status_rst_i = 1;
265
    #SD_TCLK;
266
    int_status_rst_i = 0;
267
    assert(int_status_o == 0);
268
 
269
    //write crc not ok
270
    //////////////////////////////////////////////////////////////////////
271
    test_read(0);
272
 
273
    //read crc not ok
274
    //////////////////////////////////////////////////////////////////////
275
    test_write(0);
276
 
277
    #(10*SD_TCLK) $display("sd_data_master_tb finish ...");
278
    $finish;
279
 
280
end
281
 
282
endmodule

powered by: WebSVN 2.1.0

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