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

Subversion Repositories btcminer

[/] [btcminer/] [trunk/] [fpga/] [sha256_pipes2.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*!
2
   btcminer -- BTCMiner for ZTEX USB-FPGA Modules: HDL code: hash pipelines
3
   Copyright (C) 2011 ZTEX GmbH
4
   http://www.ztex.de
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License version 3 as
8
   published by the Free Software Foundation.
9
 
10
   This program is distributed in the hope that it will be useful, but
11
   WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
   General Public License for more details.
14
 
15
   You should have received a copy of the GNU General Public License
16
   along with this program; if not, see http://www.gnu.org/licenses/.
17
!*/
18
 
19
`define IDX(x) (((x)+1)*(32)-1):((x)*(32))
20
`define E0(x) ( {{x}[1:0],{x}[31:2]} ^ {{x}[12:0],{x}[31:13]} ^ {{x}[21:0],{x}[31:22]} )
21
`define E1(x) ( {{x}[5:0],{x}[31:6]} ^ {{x}[10:0],{x}[31:11]} ^ {{x}[24:0],{x}[31:25]} )
22
`define CH(x,y,z) ( (z) ^ ((x) & ((y) ^ (z))) )
23
`define MAJ(x,y,z) ( ((x) & (y)) | ((z) & ((x) | (y))) )
24
`define S0(x) ( { {x}[6:4] ^ {x}[17:15], {{x}[3:0], {x}[31:7]} ^ {{x}[14:0],{x}[31:18]} ^ {x}[31:3] } )
25
`define S1(x) ( { {x}[16:7] ^ {x}[18:9], {{x}[6:0], {x}[31:17]} ^ {{x}[8:0],{x}[31:19]} ^ {x}[31:10] } )
26
 
27
module sha256_pipe2_base ( clk, i_state, i_data, out );
28
 
29
        parameter STAGES = 64;
30
 
31
        input clk;
32
        input [255:0] i_state;
33
        input [511:0] i_data;
34
        output [255:0] out;
35
 
36
        localparam Ks = {
37
                32'h428a2f98, 32'h71374491, 32'hb5c0fbcf, 32'he9b5dba5,
38
                32'h3956c25b, 32'h59f111f1, 32'h923f82a4, 32'hab1c5ed5,
39
                32'hd807aa98, 32'h12835b01, 32'h243185be, 32'h550c7dc3,
40
                32'h72be5d74, 32'h80deb1fe, 32'h9bdc06a7, 32'hc19bf174,
41
                32'he49b69c1, 32'hefbe4786, 32'h0fc19dc6, 32'h240ca1cc,
42
                32'h2de92c6f, 32'h4a7484aa, 32'h5cb0a9dc, 32'h76f988da,
43
                32'h983e5152, 32'ha831c66d, 32'hb00327c8, 32'hbf597fc7,
44
                32'hc6e00bf3, 32'hd5a79147, 32'h06ca6351, 32'h14292967,
45
                32'h27b70a85, 32'h2e1b2138, 32'h4d2c6dfc, 32'h53380d13,
46
                32'h650a7354, 32'h766a0abb, 32'h81c2c92e, 32'h92722c85,
47
                32'ha2bfe8a1, 32'ha81a664b, 32'hc24b8b70, 32'hc76c51a3,
48
                32'hd192e819, 32'hd6990624, 32'hf40e3585, 32'h106aa070,
49
                32'h19a4c116, 32'h1e376c08, 32'h2748774c, 32'h34b0bcb5,
50
                32'h391c0cb3, 32'h4ed8aa4a, 32'h5b9cca4f, 32'h682e6ff3,
51
                32'h748f82ee, 32'h78a5636f, 32'h84c87814, 32'h8cc70208,
52
                32'h90befffa, 32'ha4506ceb, 32'hbef9a3f7, 32'hc67178f2
53
        };
54
 
55
        genvar i;
56
 
57
        generate
58
 
59
            for (i = 0; i <= STAGES; i = i + 1) begin : S
60
 
61
                reg [511:0] data;
62
                reg [223:0] state;
63
                reg [31:0] t1_p1;
64
 
65
                if(i == 0)
66
                begin
67
 
68
                    reg [223:0] o_state;
69
 
70
                    always @ (posedge clk)
71
                    begin
72
                        data <= i_data;
73
                        state <= i_state[223:0];
74
                        t1_p1 <= i_state[`IDX(7)] + i_data[`IDX(0)] + Ks[`IDX(63)];
75
                    end
76
 
77
                end else
78
                begin
79
 
80
                    reg [511:0] data_buf;
81
                    reg [223:0] state_buf;
82
                    reg [31:0] data15_p1, data15_p2, data15_p3, t1;
83
 
84
                    always @ (posedge clk)
85
                    begin
86
                        data_buf <= S[i-1].data;
87
                        data[479:0] <= data_buf[511:32];
88
 
89
                        data15_p1 <= `S1( S[i-1].data[`IDX(15)] );                                                                                      // 3
90
                        data15_p2 <= data15_p1;                                                                                                         // 1
91
                        data15_p3 <= ( ( i == 1 ) ? `S1( S[i-1].data[`IDX(14)] ) : S[i-1].data15_p2 ) + S[i-1].data[`IDX(9)] + S[i-1].data[`IDX(0)];     // 3
92
                        data[`IDX(15)] <= `S0( data_buf[`IDX(1)] ) + data15_p3;                                                                         // 4
93
 
94
                        state_buf <= S[i-1].state;                                                                                                      // 2
95
 
96
                        t1 <= `CH( S[i-1].state[`IDX(4)], S[i-1].state[`IDX(5)], S[i-1].state[`IDX(6)] ) + `E1( S[i-1].state[`IDX(4)] ) + S[i-1].t1_p1; // 6
97
 
98
                        state[`IDX(0)] <= `MAJ( state_buf[`IDX(0)], state_buf[`IDX(1)], state_buf[`IDX(2)] ) + `E0( state_buf[`IDX(0)] ) + t1;             // 7
99
                        state[`IDX(1)] <= state_buf[`IDX(0)];                                                                                            // 1
100
                        state[`IDX(2)] <= state_buf[`IDX(1)];                                                                                           // 1
101
                        state[`IDX(3)] <= state_buf[`IDX(2)];                                                                                           // 1
102
                        state[`IDX(4)] <= state_buf[`IDX(3)] + t1;                                                                                      // 2
103
                        state[`IDX(5)] <= state_buf[`IDX(4)];                                                                                           // 1
104
                        state[`IDX(6)] <= state_buf[`IDX(5)];                                                                                           // 1
105
 
106
                        t1_p1 <= state_buf[`IDX(6)] + data_buf[`IDX(1)] + Ks[`IDX((127-i) & 63)];                                                       // 2
107
                    end
108
 
109
                end
110
            end
111
 
112
        endgenerate
113
 
114
        reg [31:0] state7, state7_buf;
115
 
116
        always @ (posedge clk)
117
        begin
118
            state7_buf <= S[STAGES-1].state[`IDX(6)];
119
            state7 <= state7_buf;
120
        end
121
 
122
        assign out[223:0] = S[STAGES].state;
123
        assign out[255:224] = state7;
124
 
125
endmodule
126
 
127
 
128
module sha256_pipe130 ( clk, state, state2, data,  hash );
129
 
130
        input clk;
131
        input [255:0] state, state2;
132
        input [511:0] data;
133
        output reg [255:0] hash;
134
 
135
        wire [255:0] out;
136
 
137
        sha256_pipe2_base #( .STAGES(64) ) P (
138
            .clk(clk),
139
            .i_state(state),
140
            .i_data(data),
141
            .out(out)
142
        );
143
 
144
        always @ (posedge clk)
145
        begin
146
            hash[`IDX(0)] <= state2[`IDX(0)] + out[`IDX(0)];
147
            hash[`IDX(1)] <= state2[`IDX(1)] + out[`IDX(1)];
148
            hash[`IDX(2)] <= state2[`IDX(2)] + out[`IDX(2)];
149
            hash[`IDX(3)] <= state2[`IDX(3)] + out[`IDX(3)];
150
            hash[`IDX(4)] <= state2[`IDX(4)] + out[`IDX(4)];
151
            hash[`IDX(5)] <= state2[`IDX(5)] + out[`IDX(5)];
152
            hash[`IDX(6)] <= state2[`IDX(6)] + out[`IDX(6)];
153
            hash[`IDX(7)] <= state2[`IDX(7)] + out[`IDX(7)];
154
        end
155
 
156
endmodule
157
 
158
 
159
module sha256_pipe123 ( clk, data,  hash );
160
 
161
        parameter state = 256'h5be0cd191f83d9ab9b05688c510e527fa54ff53a3c6ef372bb67ae856a09e667;
162
 
163
        input clk;
164
        input [511:0] data;
165
        output [31:0] hash;
166
 
167
        wire [255:0] out;
168
 
169
        sha256_pipe2_base #( .STAGES(61) ) P (
170
            .clk(clk),
171
            .i_state(state),
172
            .i_data(data),
173
            .out(out)
174
        );
175
 
176
        assign hash = out[`IDX(4)];
177
 
178
endmodule
179
 
180
 
181
module sha256_pipe129 ( clk, state, data,  hash );
182
 
183
        input clk;
184
        input [255:0] state;
185
        input [511:0] data;
186
        output [255:0] hash;
187
 
188
        wire [255:0] out;
189
 
190
        sha256_pipe2_base #( .STAGES(64) ) P (
191
            .clk(clk),
192
            .i_state(state),
193
            .i_data(data),
194
            .out(out)
195
        );
196
 
197
        assign hash = out;
198
 
199
endmodule

powered by: WebSVN 2.1.0

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