-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopSHA1.v
144 lines (128 loc) · 3.38 KB
/
topSHA1.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
`timescale 1ns / 1ps
module topSHA1( clk,
reset,
start_i,
msg_i,
is_first_i,
is_last_i,
hash_o,
busy_o,
hash_done_o
) ;
input clk;
input reset;
input start_i;
input [511:0] msg_i;
input is_first_i;
input is_last_i;
output [159:0] hash_o;
output busy_o;
output hash_done_o;
wire chunk_done_o;
wire chunk_busy;
reg [1:0] msg_cnt_in_w, msg_cnt_in_r; //for hash into first stage
reg [1:0] msg_cnt_out_w, msg_cnt_out_r; //for storing the output hash
reg busy_w, busy_r;
reg first_chunk_w, first_chunk_r;
reg last_chunk_w, last_chunk_r;
reg to_be_last_w, to_be_last_r;
reg start_i_delay;
assign hash_done_o = last_chunk_r & chunk_done_o;
assign busy_o = busy_w;
always @ (*)
begin
if( start_i_delay )
msg_cnt_in_w = msg_cnt_in_r + 1'b1;
else
msg_cnt_in_w = msg_cnt_in_r;
end
always @ (*)
begin
if( chunk_done_o )
msg_cnt_out_w = msg_cnt_out_r + 1'b1;
else
msg_cnt_out_w = msg_cnt_out_r;
end
always @ (*)
begin
if( ( msg_cnt_out_r == msg_cnt_in_r )&( !first_chunk_w|(first_chunk_w&last_chunk_r) ) )
begin
busy_w = chunk_busy|busy_r;
if( chunk_done_o & !chunk_busy )
begin
busy_w = 1'b0;
end
end
else
begin
busy_w = chunk_busy;
end
end
always @ (*)
begin
first_chunk_w = first_chunk_r;
if( is_first_i )
begin
first_chunk_w = 1'b1;
end
else if( msg_cnt_in_r == 2'd3 && start_i_delay )
begin
first_chunk_w = 1'b0;
end
end
always @ (*)
begin
to_be_last_w = to_be_last_r;
last_chunk_w = last_chunk_r;
if( is_last_i )
begin
to_be_last_w = 1'b1;
end
else if( to_be_last_r )
begin
if( msg_cnt_out_r == 2'd0 )
begin
last_chunk_w = 1'b1;
to_be_last_w = 1'b0;
end
end
else if( chunk_done_o == 1'b1 && msg_cnt_out_r == 2'd3 )
begin
last_chunk_w = 1'b0;
end
end
always @ ( posedge clk or posedge reset )
begin
if( reset )
begin
msg_cnt_in_r <= 2'd0;
msg_cnt_out_r <= 2'd0;
start_i_delay <= 1'b0;
busy_r <= 1'b0;
first_chunk_r <= 1'b0;
last_chunk_r <= 1'b0;
to_be_last_r <= 1'b0;
end
else
begin
msg_cnt_in_r <= msg_cnt_in_w;
msg_cnt_out_r <= msg_cnt_out_w;
start_i_delay <= start_i;
busy_r <= busy_w;
first_chunk_r <= first_chunk_w;
last_chunk_r <= last_chunk_w;
to_be_last_r <= to_be_last_w;
end
end
chunkSHA1 chunk_hash( .clk ( clk ),
.reset ( reset ),
.start ( start_i ),
.first_chunk ( first_chunk_w ),
.msgIn_cnt ( msg_cnt_in_r ),
.msgOut_cnt ( msg_cnt_out_r ),
.msg ( msg_i ),
.hash_out ( hash_o ),
.ready ( chunk_done_o ),
.busy ( chunk_busy )
);
endmodule