-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_loop.sv
executable file
·383 lines (329 loc) · 6.17 KB
/
main_loop.sv
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// main_loop
//
// the 64 iteration loop that integrates k_file, w_file and a~h registers
//
// @author: Yuhao Dong
//
module main_loop (input clk, Reset,
input init, next, done,
input [31:0] h0,h1,h2,h3,h4,h5,h6,h7,
input [31:0] k_i, w_i,
output logic finished,
output logic[31:0] a,b,c,d,e,f,g,h);
logic [31:0] T1,T2;
logic[31:0] a_next, e_next;
always_ff @ (posedge clk, negedge Reset)
begin
if(~Reset)
begin
a <= 32'b0;
b <= 32'b0;
c <= 32'b0;
d <= 32'b0;
e <= 32'b0;
f <= 32'b0;
g <= 32'b0;
h <= 32'b0;
finished <= 1'b0;
end
else if(init)
begin
a <= h0;
b <= h1;
c <= h2;
d <= h3;
e <= h4;
f <= h5;
g <= h6;
h <= h7;
end
else if(next)
begin
a <= a_next;
b <= a;
c <= b;
d <= c;
e <= e_next;
f <= e;
g <= f;
h <= g;
end
else if(done)
begin
a <= h0 + a;
b <= h1 + b;
c <= h2 + c;
d <= h3 + d;
e <= h4 + e;
f <= h5 + f;
g <= h6 + g;
h <= h7 + h;
finished <= 1'b1;
end
end
/////////////////////////////////////////
// ch
logic[31:0] ch_out;
ch ch_instance(.e, .f, .g, .ch_out);
// maj
logic[31:0] maj_out;
maj maj_instance(.a, .b, .c, .maj_out);
// S0
logic[31:0] S0_out;
S0 S0_instance(.a, .S0_out);
// S1
logic[31:0] S1_out;
S1 S1_instance(.e, .S1_out);
assign T1 = h + S1_out + ch_out + k_i + w_i;
assign T2 = S0_out + maj_out;
assign a_next = T1 + T2;
assign e_next = d + T1;
endmodule
//////////////////////////////////////////////////
//////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
module loop_count_controller (input clk, Reset, Add, Load,
output logic init, next, done,
output logic[6:0] count);
enum logic [3:0] {RESET, IDLE, LOAD2, NEXT2, DONE}
curr_state, next_state;
logic [6:0] count_next;
//
always_ff @ (posedge clk, negedge Reset)
begin
if (~Reset)
begin
curr_state <= RESET;
count <= 7'b0;
end
else
begin
curr_state <= next_state;
count <= count_next;
end
end
// next state declaration
always_comb
begin
next_state = curr_state;
unique case(curr_state)
RESET: begin
next_state = IDLE;
end
IDLE: begin
if (Load == 1'b0)
begin
next_state = LOAD2; //load1
end
else
if (Add == 1'b0)
begin
next_state = NEXT2; //next1
end
else
if (count == 7'b1000000)
begin
next_state = DONE;
end
else
begin
next_state = IDLE;
end
end
LOAD2: begin
next_state = IDLE;
end
NEXT2: begin
if (count != 7'b1000000) ///////////////////// 1000000 or 0111111
begin
next_state = NEXT2; //next1
end
else
begin
next_state = IDLE;
end
end
DONE: begin
next_state = IDLE;
end
endcase
end
// state behavior
always_comb
begin
init = 1'b0;
next = 1'b0;
done = 1'b0;
count_next = count;
unique case(curr_state)
RESET: begin
end
IDLE: begin
if (Add == 1'b0)
begin
count_next = count + 1;
end
end
// LOAD1: begin
// end
LOAD2: begin
init = 1'b1;
end
// NEXT1: begin
// end
NEXT2: begin
next = 1'b1;
if (count != 7'b1000000)
begin
count_next = count + 1;
end
end
DONE: begin
done = 1'b1;
count_next = count + 1;
end
endcase
end
endmodule
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
module hardware_button_state_machine (input clk, Reset, Add,
output add_soft);
enum logic [1:0] {RESET, IDLE, ADD1, ADD2}
curr_state, next_state;
always_ff @ (posedge clk, negedge Reset)
begin
if (~Reset)
begin
curr_state <= RESET;
end
else
begin
curr_state <= next_state;
end
end
// next state declaration
always_comb
begin
next_state = curr_state;
unique case(curr_state)
RESET: begin
next_state = IDLE;
end
IDLE: begin
if (Add == 1'b0)
begin
next_state = ADD1;
end
else
begin
next_state = IDLE;
end
end
ADD1: begin
if (Add == 1'b1)
begin
next_state = ADD2;
end
else
begin
next_state = ADD1;
end
end
ADD2: begin
next_state = IDLE;
end
endcase
end
// state behavior
always_comb
begin
add_soft = 1'b1;
unique case(curr_state)
RESET: begin
end
//
IDLE: begin
end
//
ADD1: begin
end
//
ADD2: begin
add_soft = 1'b0;
end
endcase
end
//
endmodule
//////////////////////////////////////////////////////////////////
//depreciated
/*
module main_loop_state_machine (input clk, Reset, m_init, m_next,
output logic init, next);
enum logic [1:0] {RESET, IDLE, LOAD, NEXT}
curr_state, next_state;
always_ff @ (posedge clk, negedge Reset)
begin
if (~Reset)
begin
curr_state <= RESET;
end
else
begin
curr_state <= next_state;
end
end
// next state declaration
always_comb
begin
next_state = curr_state;
unique case(curr_state)
RESET: begin
next_state = IDLE;
end
IDLE: begin
if (m_init == 1'b1)
begin
next_state = LOAD;
end
else
if (m_next == 1'b1)
begin
next_state = NEXT;
end
else
begin
next_state = IDLE;
end
end
LOAD: begin
next_state = IDLE;
end
NEXT: begin
next_state = IDLE;
end
endcase
end
// state behavior
always_comb
begin
init = 1'b0;
next = 1'b0;
unique case(curr_state)
RESET: begin
end
IDLE: begin
end
LOAD: begin
init = 1'b1;
end
NEXT: begin
next = 1'b1;
end
endcase
end
endmodule
*/