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_cmd_serial_host_tb.sv] - Blame information for rev 8

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_cmd_serial_host_tb.sv                                     ////
6
////                                                              ////
7
//// This file is part of the WISHBONE SD Card                    ////
8
//// Controller IP Core project                                   ////
9 8 rozpruwacz
//// http://opencores.org/project,sd_card_controller              ////
10 3 rozpruwacz
////                                                              ////
11
//// Description                                                  ////
12
//// testbench for sd_cmd_serial_host 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
module sd_cmd_serial_host_tb();
45
 
46
parameter SD_TCLK = 20; // 50 MHz -> timescale 1ns
47
parameter CMD_IDLE = 1'b1;
48
parameter CMD_START = 1'b0;
49
parameter CMD_END = 1'b1;
50
 
51
//---------------Input ports---------------
52
reg sd_clk;
53
reg rst;
54
reg [1:0] setting_i;
55
reg [39:0] cmd_i;
56
reg start_i;
57
reg cmd_dat_i;
58
//---------------Output ports---------------
59
wire cmd_dat_tri;
60
wire [119:0] response_o;
61
//wire ack_o;
62
wire finish_o;
63
wire crc_ok_o;
64
wire index_ok_o;
65
wire cmd_oe_o;
66
wire cmd_out_o;
67
 
68
wire [39:0] command = 40'h0123456786;
69
wire [127:0] response = 128'h0156789abcdef0123456789abcdef012;
70
 
71
function integer crc7;
72
    input integer crc_in;
73
    input bit inb;
74
    begin
75
        inb = inb ^ crc_in[0];
76
        crc7 = crc_in >> 1;
77
        crc7 = crc7 ^ (7'h48 & {7{inb}});
78
    end
79
endfunction
80
 
81
task sd_card_receive;
82
    integer crc, i;
83
    reg [39:0] cmd;
84
    reg [6:0] crc_in;
85
    begin
86
        crc = 0;
87
        //wait for transmission start
88
        wait (cmd_oe_o == 1);
89
        #(SD_TCLK/2);
90
        //get command bits
91
        for (i=39; i>=0; i--) begin
92
            cmd[i] = cmd_out_o;
93
            crc = crc7(crc, cmd_out_o);
94
            assert(cmd_oe_o == 1);
95
            #SD_TCLK;
96
        end
97
        assert(cmd == command);
98
        for (i=0; i<7; i++) begin
99
            crc_in[i] = cmd_out_o;
100
            assert(cmd_oe_o == 1);
101
            #SD_TCLK;
102
        end
103
        assert(crc_in == crc);
104
        assert(cmd_out_o == CMD_END);
105
        assert(cmd_oe_o == 1);
106
        #SD_TCLK;
107
        assert(cmd_oe_o == 0);
108
    end
109
endtask
110
 
111
task sd_card_send;
112
    input long_resp;
113
    integer crc, i, loop_end;
114
    begin
115
        crc = 0;
116
        if (long_resp) loop_end = 0;
117
        else loop_end = 127-39;
118
        for (i=127; i>=loop_end; i--) begin
119
            cmd_dat_i = response[i];
120
            if (!long_resp || i < 120)
121
                crc = crc7(crc, response[i]);
122
            assert(cmd_oe_o == 0);
123
            #SD_TCLK;
124
        end
125
        for (i=0; i<7; i++) begin
126
            cmd_dat_i = crc[i];
127
            assert(cmd_oe_o == 0);
128
            #SD_TCLK;
129
        end
130
        cmd_dat_i = CMD_END;
131
        assert(cmd_oe_o == 0);
132
        #SD_TCLK;
133
        cmd_dat_i = CMD_IDLE;
134
    end
135
endtask
136
 
137
 
138
sd_cmd_serial_host cmd_serial_host_dut(
139
                       .sd_clk     (sd_clk),
140
                       .rst        (rst),
141
                       .setting_i  (setting_i),
142
                       .cmd_i      (cmd_i),
143
                       .start_i      (start_i),
144
                       //.ack_i      (ack_i),
145
                       .finish_o (finish_o),
146
                       //.ack_o      (ack_o),
147
                       .response_o (response_o),
148
                       .crc_ok_o   (crc_ok_o),
149
                       .index_ok_o (index_ok_o),
150
                       .cmd_dat_i  (cmd_dat_tri),
151
                       .cmd_out_o  (cmd_out_o),
152
                       .cmd_oe_o   (cmd_oe_o)
153
                   );
154
 
155
assign cmd_dat_tri = cmd_oe_o ? cmd_out_o : cmd_dat_i;
156
// Generating WB_CLK_I clock
157
always
158
begin
159
    sd_clk=0;
160
    forever #(SD_TCLK/2) sd_clk = ~sd_clk;
161
end
162
 
163
initial
164
begin
165
    rst = 1;
166
    setting_i = 0;
167
    cmd_i = 1;
168
    start_i = 0;
169
    //ack_i = 0;
170
    cmd_dat_i = CMD_IDLE;
171
 
172
    $display("sd_cmd_serial_host_tb start ...");
173
 
174
    #(3*SD_TCLK);
175
    rst = 0;
176
    assert(finish_o == 0);
177
    //assert(ack_o == 0);
178
    assert(crc_ok_o == 0);
179
    assert(index_ok_o == 0);
180
    assert(cmd_out_o == 1);
181
    assert(cmd_oe_o == 1);
182
    #SD_TCLK;
183
    assert(finish_o == 0);
184
    //assert(ack_o == 0);
185
    assert(crc_ok_o == 0);
186
    assert(index_ok_o == 0);
187
    assert(cmd_out_o == 1);
188
    assert(cmd_oe_o == 1);
189
    #(65*SD_TCLK); //INIT_DELAY
190
    assert(finish_o == 0);
191
    //assert(ack_o == 1);
192
    assert(crc_ok_o == 0);
193
    assert(index_ok_o == 0);
194
    assert(cmd_oe_o == 0);
195
 
196
    //tests with normal response (check index, check crc)
197
    //tests with long response (check crc)
198
 
199
    //cmd without response
200
    setting_i = {2'h0};
201
    cmd_i <= command;
202
    start_i <= 1'b1;
203
    #SD_TCLK;
204
    setting_i = 0;
205
    cmd_i <= 0;
206
    start_i <= 0;
207
    fork
208
        sd_card_receive;
209
        begin
210
            wait(finish_o == 1);
211
            #(SD_TCLK/2);
212
            assert(crc_ok_o == 0);
213
            assert(index_ok_o == 0);
214
        end
215
    join
216
 
217
    //cmd with r1 response
218
    setting_i = {2'h1};
219
    cmd_i <= command;
220
    start_i <= 1'b1;
221
    #SD_TCLK;
222
    setting_i = 0;
223
    cmd_i <= 0;
224
    start_i <= 0;
225
 
226
    fork
227
        begin
228
            sd_card_receive;
229
            assert(finish_o == 0);
230
            sd_card_send(0);
231
        end
232
        begin
233
            wait(finish_o == 1);
234
            #(SD_TCLK/2);
235
            assert(response_o[119:88] == response[119:88]);
236
            assert(crc_ok_o == 1);
237
            assert(index_ok_o == 1);
238
        end
239
    join
240
 
241
    //cmd with r2 response
242
    setting_i = {2'h3};
243
    cmd_i <= command;
244
    start_i <= 1'b1;
245
    #SD_TCLK;
246
    setting_i = 0;
247
    cmd_i <= 0;
248
    start_i <= 0;
249
 
250
    fork
251
        begin
252
            sd_card_receive;
253
            assert(finish_o == 0);
254
            sd_card_send(1);
255
        end
256
        begin
257
            wait(finish_o == 1);
258
            #(SD_TCLK/2);
259
            assert(response_o == response[119:0]);
260
            assert(crc_ok_o == 1);
261
            assert(index_ok_o == 1);
262
        end
263
    join
264
 
265
    #(1000*SD_TCLK) $display("sd_cmd_serial_host_tb finish ...");
266
    $finish;
267
 
268
end
269
 
270
endmodule

powered by: WebSVN 2.1.0

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