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

Subversion Repositories sha_core

[/] [sha_core/] [trunk/] [bench/] [test_sha256.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 2 marsgod
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  SHA-256                                                    ////
4
////  Secure Hash Algorithm (SHA-256)   testbench                ////
5
////                                                             ////
6
////  Author: marsgod                                            ////
7
////          marsgod@opencores.org                              ////
8
////                                                             ////
9
////                                                             ////
10
////  Downloaded from: http://www.opencores.org/cores/sha_core/  ////
11
////                                                             ////
12
/////////////////////////////////////////////////////////////////////
13
////                                                             ////
14
//// Copyright (C) 2002-2004 marsgod                             ////
15
////                         marsgod@opencores.org               ////
16
////                                                             ////
17
////                                                             ////
18
//// This source file may be used and distributed without        ////
19
//// restriction provided that this copyright statement is not   ////
20
//// removed from the file and that any derivative work contains ////
21
//// the original copyright notice and the associated disclaimer.////
22
////                                                             ////
23
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
24
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
25
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
26
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
27
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
28
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
29
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
30
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
31
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
32
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
33
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
34
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
35
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
36
////                                                             ////
37
/////////////////////////////////////////////////////////////////////
38
 
39
 
40
`timescale 1ns/10ps
41
 
42
`define SHA256_TEST             "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
43
`define SHA256_TEST_PADDING     {1'b1,63'b0,448'b0,64'd448}     // 448 bit
44
`define SHA256_TEST_RESULT      256'h248d6a61_d20638b8_e5c02693_0c3e6039_a33ce459_64ff2167_f6ecedd4_19db06c1
45
 
46
module test_sha;
47
 
48
reg clk,rst,cmd_w_i;
49
reg [31:0] text_i;
50
 
51
reg [2:0] cmd_i;
52
 
53
wire [31:0] text_o;
54
wire [3:0] cmd_o;
55
 
56
initial
57
begin
58
//      $sdf_annotate("syn/data/sha256.sdf",sha_core);
59
 
60
        clk = 1'b0;
61
        rst = 1'b0;
62
        cmd_w_i = 1'b0;
63
        cmd_i = 3'b0;
64
 
65
        #21;
66
        rst = 1'b1;
67
        #17;
68
        rst = 1'b0;
69
 
70
        test_SHA256;
71
        @(posedge clk);
72
        @(posedge clk);
73
        @(posedge clk);
74
        @(posedge clk);
75
        @(posedge clk);
76
 
77
        $finish;
78
end
79
 
80
 
81
always #5 clk = ~clk;
82
 
83
sha256 sha_core(
84
        .clk_i(clk),
85
        .rst_i(rst),
86
        .text_i(text_i),
87
        .text_o(text_o),
88
        .cmd_i(cmd_i),
89
        .cmd_w_i(cmd_w_i),
90
        .cmd_o(cmd_o)
91
        );
92
 
93
 
94
task test_SHA256;
95
integer i;
96
reg [1023:0] all_message;
97
reg [511:0] tmp_i;
98
reg [255:0] tmp_o;
99
reg [31:0] tmp;
100
begin
101
        all_message = {`SHA256_TEST,`SHA256_TEST_PADDING};
102
        tmp_i = all_message[1023:512];
103
        tmp_o = `SHA256_TEST_RESULT;
104
 
105
        #100;
106
 
107
 
108
        @(posedge clk);
109
        cmd_i = 3'b010;
110
        cmd_w_i = 1'b1;
111
 
112
        for (i=0;i<16;i=i+1)
113
        begin
114
                @(posedge clk);
115
                cmd_w_i = 1'b0;
116
                text_i = tmp_i[16*32-1:15*32];
117
                tmp_i = tmp_i << 32;
118
        end
119
 
120
 
121
        @(posedge clk);
122
        @(posedge clk);
123
        @(posedge clk);
124
        @(posedge clk);
125
        @(posedge clk);
126
 
127
        while (cmd_o[3])
128
                @(posedge clk);
129
 
130
        @(posedge clk);
131
        @(posedge clk);
132
        @(posedge clk);
133
        @(posedge clk);
134
        @(posedge clk);
135
 
136
        #100;
137
 
138
 
139
        tmp_i = all_message[511:0];
140
        @(posedge clk);
141
        cmd_i = 3'b110;
142
        cmd_w_i = 1'b1;
143
 
144
        for (i=0;i<16;i=i+1)
145
        begin
146
                @(posedge clk);
147
                cmd_w_i = 1'b0;
148
                text_i = tmp_i[16*32-1:15*32];
149
                tmp_i = tmp_i << 32;
150
        end
151
 
152
        @(posedge clk);
153
        @(posedge clk);
154
        @(posedge clk);
155
        @(posedge clk);
156
        @(posedge clk);
157
 
158
        while (cmd_o[3])
159
                @(posedge clk);
160
 
161
        @(posedge clk);
162
        @(posedge clk);
163
        @(posedge clk);
164
        @(posedge clk);
165
        @(posedge clk);
166
 
167
        cmd_i = 3'b001;
168
        cmd_w_i = 1'b1;
169
 
170
        @(posedge clk);
171
        cmd_w_i = 1'b0;
172
        for (i=0;i<8;i=i+1)
173
        begin
174
                @(posedge clk);
175
                #1;
176
                tmp = tmp_o[8*32-1:7*32];
177
                if (text_o !== tmp | (|text_o)===1'bx)
178
                begin
179
                        $display("ERROR(SHA-256-%02d) Expected %x, Got %x", i,tmp, text_o);
180
                end
181
                else
182
                begin
183
                        $display("OK(SHA-256-%02d),Expected %x, Got %x", i,tmp, text_o);
184
                end
185
                tmp_o = tmp_o << 32;
186
        end
187
        @(posedge clk);
188
        @(posedge clk);
189
        @(posedge clk);
190
        @(posedge clk);
191
        @(posedge clk);
192
        #100;
193
end
194
endtask
195
 
196
endmodule

powered by: WebSVN 2.1.0

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