-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmk.v
1772 lines (1560 loc) · 41.4 KB
/
pmk.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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
FPGA SHA1 and WPA2 PMK generators
Fully unrolled; avg. 7000 ALM per instance with throughput of 1 SHA/clock
Tested with Cyclone V 5CSEBA6U23I7 and 5CGTFD9E5F35C7
*****************************************************************************/
////
//
//
// Main PMK calculator module
//
//
/////
// synopsys translate_off
`timescale 1 ps / 1 ps
// synopsys translate_on
module tapsv2 (
clock,
enable,
shiftin,
shiftout,
lead_out);
parameter N=128;
parameter W=160;
parameter reg_in = 0;
parameter reg_out = 0;
input clock;
input enable;
input [W-1:0] shiftin;
output [W-1:0] shiftout;
output [W-1:0] lead_out;
wire [W-1:0] sub_wire0;
//wire [W-1:0] sub_wire1;
//wire [W-1:0] shiftout = sub_wire0[W-1:0];
//wire [W-1:0] taps = sub_wire1[W-1:0];
(* preserve, ramstyle="logic" *) reg[W-1:0] reg_input, last_reg;
altshift_taps ALTSHIFT_TAPS_component (
.clock (clock),
.shiftin (reg_in ? reg_input : shiftin),
.shiftout (sub_wire0),
.taps ()
// synopsys translate_off
,
.aclr (),
.clken (enable),
.sclr ()
// synopsys translate_on
);
defparam
ALTSHIFT_TAPS_component.intended_device_family = "Cyclone V",
ALTSHIFT_TAPS_component.lpm_hint = "RAM_BLOCK_TYPE=AUTO",
ALTSHIFT_TAPS_component.lpm_type = "altshift_taps",
ALTSHIFT_TAPS_component.number_of_taps = 1,
ALTSHIFT_TAPS_component.tap_distance = N-reg_in-reg_out,
ALTSHIFT_TAPS_component.width = W;
assign shiftout = reg_out ? last_reg : sub_wire0;
assign lead_out = sub_wire0;
always @(posedge clock)
begin
if(reg_in && enable)
reg_input<=shiftin;
if(reg_out && enable)
last_reg <= sub_wire0;
end
endmodule
module ring_buffer_taps(core_clk,
cycle, replace,
in_data,
out_data,
lead_out);
parameter Size=100;
parameter W=160;
parameter reg_in = 0;
parameter reg_out = 0;
input core_clk, cycle, replace;
input wire [W-1:0] in_data;
output wire [W-1:0] out_data;
output wire [W-1:0] lead_out;
wire[W-1:0] out_wire;
assign out_data = out_wire;
tapsv2 #(Size, W, reg_in, reg_out) tapobj(
.clock(core_clk),
.shiftin(replace ? in_data : out_wire),
.shiftout(out_wire),
.enable(cycle),
.lead_out(lead_out)
);
endmodule
module pmk_calc_daisy(input core_clk,
input [159:0] data_in,
input write_enable,
output [159:0] data_out,
input [1:0] ext_mode,
output done
);
parameter N=100;
parameter Niter=10;
parameter instance_id=32'hFFFFFFFF;
//parameter Niter=4096;
parameter L=81;
parameter NL=N-L;
(* maxfan = 32, dont_merge *) reg[1:0] status=0;
wire[159:0] out_ctx;
reg[31:0] I, J;
reg[159:0] data;
reg[159:0] pad_half_delayed;
wire[159:0] data_sha_input, data_bare;
reg pad_cycle=0, pad_replace=0;
wire [159:0] pad_ring_out_data;
wire [159:0] pad_ring_mid_data;
reg acc_cycle=0, acc_replace=0;
(* dont_merge *) reg[1:0] mode=0;
(* dont_merge *) reg mode_1 = 0;
(* dont_merge *) reg mode_0 = 0;
(* dont_merge *) reg[15:0] loop_counter=0;
(* dont_merge *) reg[10:0] inst_counter=0;
parameter A0W = 32;
wire[A0W-1:0] a0_buf_in, a0_buf_out, a0_buf_mid;
wire [159:0] acc_ring_out_data;
wire[159:0] acc_buf_lookahead;
ring_buffer_taps #(N, 160, 0, 1)
//ring_buffer_explicit #(N, 160)
acc_buf(
.core_clk(core_clk),
.replace(acc_replace),
.cycle(mode_1 ? acc_cycle : write_enable),
.in_data(mode_1 ? (acc_ring_out_data^data) : data_in),
.out_data(acc_ring_out_data),
.lead_out(acc_buf_lookahead)
);
reg[31:0] a0_part1, a0_part2;
wire[63:0] pad_pp;
pad_preprocess64 pp(acc_buf_lookahead, pad_pp);
assign a0_buf_in=a0_part1+a0_part2;
always @(posedge core_clk)
begin
if(mode_1 ? acc_cycle : write_enable)
{a0_part1,a0_part2}<=pad_pp;
end
parameter register_pad_taps = 1;
wire[191:0] wire_pad_in;
assign wire_pad_in = {a0_buf_in, acc_ring_out_data};
wire[191:0] wire_pad_out;
assign {a0_buf_out, pad_ring_out_data} = wire_pad_out;
wire[191:0] wire_pad_mid;
assign {a0_buf_mid, pad_ring_mid_data} = wire_pad_mid;
ring_buffer_taps #(N-2, 192, register_pad_taps, 0) pad_buf_part1(
.core_clk(core_clk),
.in_data(pad_replace ? wire_pad_in : wire_pad_out),
.out_data(wire_pad_mid),
.cycle(mode_1 ? pad_cycle : write_enable),
.replace(1'b1),
.lead_out()
);
ring_buffer_taps #(N+2, 192, register_pad_taps, 0) pad_buf_part2(
.core_clk(core_clk),
.in_data(wire_pad_mid),
.out_data(wire_pad_out),
.cycle(mode_1 ? pad_cycle : write_enable),
.replace(1'b1),
.lead_out()
);
/*
parameter register_pad_taps = 1;
ring_buffer_taps #(N-2, 160, register_pad_taps, 0) pad_buf_part1(
.core_clk(core_clk),
.in_data(pad_replace ? acc_ring_out_data : pad_ring_out_data),
.out_data(pad_ring_mid_data),
.cycle(mode_1 ? pad_cycle : write_enable),
.replace(1'b1),
.lead_out() );
ring_buffer_taps #(N+2, 160, register_pad_taps, 0) pad_buf_part2(
.core_clk(core_clk),
.in_data(pad_ring_mid_data),
.out_data(pad_ring_out_data),
.cycle(mode_1 ? pad_cycle : write_enable),
.replace(1'b1),
.lead_out()
);
parameter register_pad_a0 = 1;
ring_buffer_taps #(2*N, A0W, register_pad_a0) a0_buf (
.core_clk(core_clk),
.cycle(mode_1 ? pad_cycle : write_enable),
.replace(pad_replace),
.in_data(a0_buf_in),
.out_data(a0_buf_out),
.lead_out()
);
*/
reg data_cycle=0, data_replace=0;
ring_buffer_taps #(NL) data_buf (
.core_clk(core_clk),
.cycle(data_cycle),
.replace(data_replace),
.in_data(out_ctx),
.out_data(data_bare),
.lead_out()
);
reg first_iter=0, second_iter=0, last_iter=0, first_pass=0;
(* maxfan = 40 *) reg data_src_switch=0;
assign data_sha_input=(data_src_switch) ? data : acc_ring_out_data;
assign data_out = mode_0 ? pad_ring_out_data : acc_ring_out_data;
assign done = (status==2);
reg[6:0] timer=0;
SHA1_5x5_bare s55(core_clk, timer[6:0], a0_buf_out, pad_ring_out_data, data_sha_input, out_ctx);
reg[31:0] job_count=0;
always @ (posedge core_clk)
begin
timer<=timer+7'h1;
mode<=ext_mode;
mode_1 <= (ext_mode==1);
mode_0 <= (ext_mode==0);
if(mode==3)
status<=0;
if(status==0)
data_src_switch<=0;
else if(second_iter && first_pass)
data_src_switch<=1;
case(status)
0:
begin
if(mode==0) // still filling
begin
acc_replace<=1;
pad_replace<=1;
end
if(mode==1) // all cores filled, kicking off
begin
$display("Core %d job %d filled", instance_id, job_count);
//$display("%x %x %x", pad_buf.data[0], pad_buf.data[1], acc_buf.data[0]);
// counter <= N;
loop_counter <= 0;
inst_counter <= N;
job_count<=(job_count!=32'hffffffff)?job_count+1:0;
data_cycle<=1;
data_replace<=1;
status<=1;
acc_cycle<=0;
acc_replace<=0;
pad_replace<=0;
//data_src_switch<=0;
last_iter<=0;
first_iter<=1;
second_iter<=0;
end
if(mode==2) // something went wrong
begin
end
end
1:
begin
/*
if(instance_id==0 && (loop_counter==0 || !(inst_counter & 32'h3f)
|| (loop_counter==1 && inst_counter>=118 && inst_counter<=122)
|| (loop_counter==1 && inst_counter>=238)
|| (loop_counter==2 && inst_counter<=2)
)
)
// $display("%d %d %x %x", loop_counter, inst_counter, pad_ring_out_data[63:0], pad_ring_mid_data[63:0]);
$display("%d %d %x %x %x %x", loop_counter, inst_counter, acc_ring_out_data[63:0], pad_ring_mid_data[63:0], pad_half_delayed[63:0], acc_ring_out_data[63:0]^data[63:0]);
// $display("%d %d %x %x", loop_counter, inst_counter, acc_ring_out_data[63:0], pad_ring_out_data[63:0]);
*/
pad_cycle<=1;
pad_replace<=0;
if(last_iter && first_pass)// fires when loop_counter is Niter and inst_counter is 0
begin
acc_cycle<=0;
status<=2;
$display("Core %d finished", instance_id);
end
else
acc_cycle<=1;
//if(second_iter && first_pass)
// data_src_switch<=1;
acc_replace<=((!first_iter) && (inst_counter >= N))?1:0;
last_iter <= (loop_counter==Niter-1);
// counter<=counter+1;
if(inst_counter == 2*N-1)
begin
loop_counter <= loop_counter+1;
inst_counter <= 0;
second_iter<=first_iter;
first_iter<=0;
first_pass<=1;
end
else
begin
inst_counter <= inst_counter+1;
first_pass <= 0;
end
for(I=0; I<5; I=I+1)
data[I*32+:32]<=data_bare[I*32+:32]+pad_half_delayed[I*32+:32];
pad_half_delayed <= pad_ring_mid_data;
end
2:
begin
acc_replace<=1;
pad_replace<=1;
case(mode)
0: begin // all cores drained, reset
status<=0;
end
1: begin // some cores still working, sit tight
acc_cycle<=0;
pad_cycle<=0;
end
// 2: acc_replace<=1; // drain
endcase
end
endcase
end
endmodule
module ring_buffer_explicit(core_clk,
mode,
//cycle,
replace,
in_data,
out_data,
lead_out,
rden, wren, addr,
write_data, read_data, read_ready
);
parameter Size=100;
parameter W=160;
parameter RegWrite=0;
// parameter Mid=0;
input core_clk;
//input cycle;
input replace;
input [2:0] mode;
input wire [W-1:0] in_data;
output wire [W-1:0] out_data;
output wire [W-1:0] lead_out;
input rden, wren;
input [11:0] addr;
input [31:0] write_data;
output [31:0] read_data;
output read_ready;
parameter LSize = $clog2(Size);
parameter PaddedSize = 1<<LSize;
reg[31:0] cycle_count=0;
reg[31:0] I;
// reg[W-1:0] data[PaddedSize-1:0];
reg [31:0] reg_read_data=0;
reg reg_read_ready=0;
assign read_data = reg_read_data;
assign read_ready = reg_read_ready;
reg [LSize-1:0] counter=0, counter2=Size-2;
reg[W-1:0] lookaheadQueue[1:0];
assign out_data=lookaheadQueue[0];
assign lead_out=lookaheadQueue[1];
reg reg_wren=0, reg_rden=0;
reg [LSize-1:0] row;
reg [2:0] column;
reg [31:0] reg_write_data;
wire addr_valid;
parameter nCol = W/32;
wire[W-1:0] memory_return_line;
reg [W-1:0] memory_input_line_reg;
wire [W-1:0] memory_input_line_wire;
reg [nCol-1:0] mem_wren;
//wire[W-1:0] memory_input_line;
reg [LSize-1:0] mem_wraddr, mem_rdaddr;
assign memory_input_line_wire = mode[1] ? (replace ? in_data : lookaheadQueue[0]) : {5{reg_write_data}};
generate
genvar g;
for(g=0; g<nCol; g=g+1) begin: xx
my_syncram #(32,PaddedSize) mem (
core_clk,
RegWrite ? memory_input_line_reg[g*32+:32] : memory_input_line_wire[g*32+:32],
mem_rdaddr,
mem_wraddr,
mem_wren[g],
memory_return_line[g*32+:32]);
end
endgenerate
assign addr_valid = (addr[11:3]<Size && addr[2:0]<nCol);
reg[31:0] timer=0;
reg reg_rden_delay=0;
reg [2:0] column_delay=0;
reg cycle_delay=0;
always @(posedge core_clk)
begin
if(mode[0])
begin
counter<=RegWrite?4:3;
counter2<=0;//Size-1;
reg_rden_delay<=0;
end
reg_rden <= rden && addr_valid;
reg_wren <= wren && addr_valid;
reg_write_data <= write_data;
row <= addr[11:3];
column <= addr[2:0];
column_delay <= column;
reg_read_ready <= reg_rden_delay;
reg_rden_delay <= reg_rden;
mem_rdaddr<=mode[1] ? counter2 : addr[11:3];
mem_wraddr<=mode[1] ? counter : addr[11:3];
if(RegWrite)
begin
if(mode[1])
memory_input_line_reg<=replace ? in_data : lookaheadQueue[0];
else
memory_input_line_reg<={5{write_data}};
end
if(reg_rden_delay)
reg_read_data <= memory_return_line[column_delay*32+:32];
lookaheadQueue[1] <= memory_return_line;//[counter>=2 ? counter-2 : counter-2+Size];
lookaheadQueue[0] <= lookaheadQueue[1];
for(I=0; I<nCol; I=I+1)
mem_wren[I]<=(mode[2]) || (mode[0] && wren && addr[2:0]==I);
if(mode[1])
begin
if(counter!=0)
counter<=counter-1;
else
counter<=Size-1;
if(counter2!=0)
counter2<=counter2-1;
else
counter2<=Size-1;
end
end
endmodule
module ring_buffer_explicit_no_lead(core_clk,
mode,
//cycle,
replace,
in_data,
out_data,
// lead_out,
rden, wren, addr,
write_data, read_data, read_ready
);
parameter Size=100;
parameter W=160;
parameter RegWrite=0;
parameter RegRead=1;
// parameter Mid=0;
input core_clk;
//input cycle;
input replace;
input [2:0] mode;
input wire [W-1:0] in_data;
output wire [W-1:0] out_data;
// output wire [W-1:0] lead_out;
input rden, wren;
input [11:0] addr;
input [31:0] write_data;
output [31:0] read_data;
output read_ready;
parameter LSize = $clog2(Size);
parameter PaddedSize = 1<<LSize;
reg[31:0] cycle_count=0;
reg[31:0] I;
// reg[W-1:0] data[PaddedSize-1:0];
reg [31:0] reg_read_data=0;
reg reg_read_ready=0;
assign read_data = reg_read_data;
assign read_ready = reg_read_ready;
reg [LSize-1:0] counter=0, counter2=Size-2;
reg[W-1:0] lookaheadQueue;
wire[W-1:0] memory_return_line;
assign out_data=RegRead ? lookaheadQueue : memory_return_line;
reg reg_wren=0, reg_rden=0;
reg [LSize-1:0] row;
reg [2:0] column;
reg [31:0] reg_write_data;
wire addr_valid;
parameter nCol = W/32;
reg [W-1:0] memory_input_line_reg;
wire [W-1:0] memory_input_line_wire;
reg [nCol-1:0] mem_wren;
//wire[W-1:0] memory_input_line;
reg [LSize-1:0] mem_wraddr, mem_rdaddr;
assign memory_input_line_wire = mode[1] ? (replace ? in_data : out_data) : {5{reg_write_data}};
generate
genvar g;
for(g=0; g<nCol; g=g+1) begin: xx
my_syncram #(32,PaddedSize) mem (
core_clk,
RegWrite ? memory_input_line_reg[g*32+:32] : memory_input_line_wire[g*32+:32],
mem_rdaddr,
mem_wraddr,
mem_wren[g],
memory_return_line[g*32+:32]);
end
endgenerate
assign addr_valid = (addr[11:3]<Size && addr[2:0]<nCol);
reg[31:0] timer=0;
reg reg_rden_delay=0;
reg [2:0] column_delay=0;
reg cycle_delay=0;
always @(posedge core_clk)
begin
if(mode[0])
begin
counter<=RegWrite?4:3;
counter2<=RegRead?1:2;//Size-1;
reg_rden_delay<=0;
end
reg_rden <= rden && addr_valid;
reg_wren <= wren && addr_valid;
reg_write_data <= write_data;
row <= addr[11:3];
column <= addr[2:0];
column_delay <= column;
reg_read_ready <= reg_rden_delay;
reg_rden_delay <= reg_rden;
mem_rdaddr<=mode[1] ? counter2 : addr[11:3];
mem_wraddr<=mode[1] ? counter : addr[11:3];
if(RegWrite)
begin
if(mode[1])
memory_input_line_reg<=replace ? in_data : out_data;
else
memory_input_line_reg<={5{write_data}};
end
if(reg_rden_delay)
reg_read_data <= memory_return_line[column_delay*32+:32];
if(RegRead)
lookaheadQueue <= memory_return_line;//[counter>=2 ? counter-2 : counter-2+Size];
for(I=0; I<nCol; I=I+1)
mem_wren[I]<=mode[2] || (mode[0] && wren && addr[2:0]==I);
if(mode[1])
begin
if(counter!=0)
counter<=counter-1;
else
counter<=Size-1;
if(counter2!=0)
counter2<=counter2-1;
else
counter2<=Size-1;
end
end
endmodule
module ring_buffer_fulldpram(core_clk,
mode,
cycle,
out_data,
mid_data,
lead_out,
rden, wren, addr,
write_data, read_data, read_ready
);
parameter Size=100;
parameter W=160;
parameter Mid=0;
input core_clk, cycle;
input [1:0] mode;
output wire [W-1:0] out_data;
output wire [W-1:0] mid_data;
output wire [W-1:0] lead_out;
input rden, wren;
input [11:0] addr;
input [31:0] write_data;
output [31:0] read_data;
output read_ready;
reg[31:0] cycle_count=0;
reg[31:0] I;
reg[W-1:0] data[Size-1:0];
reg [31:0] reg_read_data=0;
reg reg_read_ready=0;
assign read_data = reg_read_data;
assign read_ready = reg_read_ready;
reg [9:0] counter=0, mid_counter=0;
reg[W-1:0] lookaheadQueue[1:0];
assign out_data=lookaheadQueue[0];
assign lead_out=lookaheadQueue[1];
reg[W-1:0] reg_mid_data;
assign mid_data=reg_mid_data;
reg reg_wren=0, reg_rden=0;
reg [8:0] row;
reg [2:0] column;
reg [31:0] reg_write_data;
wire addr_valid;
assign addr_valid = (addr[11:3]<Size && addr[2:0]<W/32);
always @(posedge core_clk)
begin
if(mode==0)
begin
counter<=0;
mid_counter<=Mid;
end
reg_rden <= rden && addr_valid;
reg_wren <= wren && addr_valid;
reg_write_data <= write_data;
row <= addr[11:3];
column <= addr[2:0];
reg_read_ready <= reg_rden;
if(cycle && (reg_rden || reg_wren))
$display("WARNING : ring buffer unexpected state");
if(reg_rden)
reg_read_data <= data[row][column*32+:32];
else if(cycle)
lookaheadQueue[1] <= data[counter>=2 ? counter-2 : counter-2+Size];
if(reg_wren)
data[row][column*32+:32]<=reg_write_data;
else
reg_mid_data<=data[mid_counter];
if(reg_wren)
begin
if(row==0)
lookaheadQueue[0][column*32+:32]<=reg_write_data;
if(row==Size-1)
lookaheadQueue[1][column*32+:32]<=reg_write_data;
end
if(cycle)
begin
lookaheadQueue[0] <= lookaheadQueue[1];
if(counter!=0)
counter<=counter-1;
else
counter<=Size-1;
if(mid_counter!=0)
mid_counter<=mid_counter-1;
else
mid_counter<=Size-1;
end
end
endmodule
module pmk_calc_direct_feed(input core_clk,
input [12:0] wire_addr,
input wire_rden,
input wire_wren,
input [31:0] wire_data_in,
output reg [31:0] data_out,
output reg readdatavalid,
input [1:0] ext_mode,
(* maxfan = 32, dont_merge *) output reg[1:0] status
);
parameter N=100;
parameter Niter=10;
parameter instance_id=32'hFFFFFFFF;
//parameter Niter=4096;
parameter L=81;
parameter NL=N-L;
initial begin
status<=0;
end
//(* maxfan = 32, dont_merge *) reg[1:0] status=0;
wire[159:0] out_ctx;
reg[31:0] I, J;
reg[159:0] data;
reg[159:0] pad_half_delayed;
wire[159:0] data_sha_input, data_bare;
wire [159:0] pad_ring_out_data;
wire [159:0] pad_ring_mid_data;
reg acc_replace=0;
(* dont_merge *) reg[1:0] mode=0;
(* dont_merge *) reg mode_1 = 0;
(* dont_merge *) reg mode_0 = 0;
(* dont_merge *) reg[15:0] loop_counter=0;
(* dont_merge *) reg[10:0] inst_counter=0;
wire[31:0] a0_buf_out, a0_buf_mid;
wire [159:0] acc_ring_out_data;
wire[159:0] pad_buf_lookahead;
(* dont_merge *) reg reg_rden_acc, reg_wren_acc,
reg_wren_pad1,
reg_wren_pad2;
(* dont_merge *) reg[31:0] reg_write_data;
(* dont_merge *) reg [11:0] reg_mm_addr;
reg [12:0] addr;
reg rden;
reg wren;
reg [31:0] data_in;
always @(posedge core_clk)
begin
addr <= wire_addr;
rden <= wire_rden;
wren <= wire_wren;
data_in <= wire_data_in;
reg_rden_acc <= rden && (addr[12]==0);
reg_wren_acc <= wren && (addr[12]==0);
`ifndef ONE_PAD_BUF
reg_wren_pad1 <= wren && addr[12] && (addr[11:3]<N-2);
reg_wren_pad2 <= wren && addr[12] && (addr[11:3]>=N-2);
reg_mm_addr <= (addr[12]==0 || addr[11:3]<N-2) ? addr[11:0] : (addr[11:0]-8*(N-2));
`else
reg_wren_pad1 <= wren && addr[12];
reg_wren_pad2 <= 0;
reg_mm_addr <= addr[11:0];
`endif
reg_write_data <= data_in;
end
(* maxfan = 32 *) reg[1:0] ring_status=0;
(* maxfan = 32, dont_merge *) reg[2:0] ring_status2_1=1;
(* maxfan = 32, dont_merge *) reg[2:0] ring_status2_2=1;
(* maxfan = 32, dont_merge *) reg[2:0] ring_status2_3=1;
//(* maxfan = 32, dont_merge *) reg[2:0] ring_status2_3=1;
//`define LEAD_ACC
`ifndef LEAD_ACC
ring_buffer_explicit_no_lead #(N, 160, 1, 1)
`else
wire[159:0] acc_ring_lead_out;
ring_buffer_explicit #(N, 160, 1)
`endif
acc_buf(
.core_clk(core_clk),
.mode(ring_status2_1),
.replace(acc_replace),
.in_data(acc_ring_out_data^data),
.out_data(acc_ring_out_data),
`ifdef LEAD_ACC
.lead_out(acc_ring_lead_out),
`endif
.rden(reg_rden_acc),
.wren(reg_wren_acc),
.addr(reg_mm_addr),
.write_data(reg_write_data),
.read_data(data_out),
.read_ready(readdatavalid)
);
reg[31:0] a0_part1, a0_part2;
wire[63:0] pad_pp;
pad_preprocess64 pp(pad_buf_lookahead[159:0], pad_pp);
wire [31:0] a0_buf_in;
assign a0_buf_in=a0_part1+a0_part2;
always @(posedge core_clk)
{a0_part1,a0_part2}<=pad_pp;
reg pad_replace=0;
`ifdef ONE_PAD_BUF
// not implemented
ring_buffer_fulldpram #(2*N, 160, N-3) pad_buf(
.core_clk(core_clk),
.mode(status),
.mid_data(pad_ring_mid_data),
.out_data(pad_ring_out_data),
.cycle(pad1_cycle),
.lead_out(pad_buf_lookahead),
.rden(1'b0),
.wren(reg_wren_pad1),
.addr(reg_mm_addr),
.write_data(reg_write_data),
.read_data(),
.read_ready()
);
`else
ring_buffer_explicit_no_lead #(N-2, 160, 0, 1) pad_buf_part1(
.core_clk(core_clk),
.mode(ring_status2_2),
.in_data(pad_ring_out_data),
.out_data(pad_ring_mid_data),
.replace(pad_replace),
//.lead_out(),
.rden(1'b0),
.wren(reg_wren_pad1),
.addr(reg_mm_addr),
.write_data(reg_write_data),
.read_data(),
.read_ready()
);
ring_buffer_explicit #(N+2, 160, 1) pad_buf_part2(
.core_clk(core_clk),
.mode(ring_status2_3),
.in_data(pad_ring_mid_data),
.out_data(pad_ring_out_data),
.replace(pad_replace),
.lead_out(pad_buf_lookahead),
.rden(1'b0),
.wren(reg_wren_pad2),
.addr(reg_mm_addr),
.write_data(reg_write_data),
.read_data(),
.read_ready()
);
`endif
reg data_cycle=0, data_replace=0;
(* altera_attribute = "-name QII_AUTO_PACKED_REGISTERS OFF" *)
ring_buffer_taps #(NL, 160, 0, 1) data_buf (
.core_clk(core_clk),
.cycle(data_cycle),
.replace(data_replace),
.in_data(out_ctx),
.out_data(data_bare),
.lead_out()
);
reg first_iter=0, second_iter=0, last_iter=0, first_pass=0, last_pass=0;
reg data_src_switch_lead = 0;
(* maxfan = 40 *) reg data_src_switch=0;
`ifdef LEAD_ACC
assign data_sha_input = data;
`else
assign data_sha_input=(data_src_switch) ? data : acc_ring_out_data;
`endif
//assign out_status=status;
(* dont_merge *) reg[6:0] timer=0;
//(* altera_attribute = "-name OPTIMIZE_HOLD_TIMING OFF" *)
`ifdef SHA_V2
SHA1_5x5_bare_v2
`else
SHA1_5x5_bare
`endif
s55(core_clk, timer[6:0], a0_buf_in, pad_ring_out_data, data_sha_input, out_ctx);
reg[31:0] job_count=0;
reg[2:0] ring_init_count;
reg[1:0] reg_ext_mode=0;
always @ (posedge core_clk)
begin
timer<=timer+7'h1;
reg_ext_mode<=ext_mode;
mode<=reg_ext_mode;
mode_1 <= (reg_ext_mode==1);
mode_0 <= (reg_ext_mode==0);
`ifdef LEAD_ACC
for(I=0; I<5; I=I+1)
data[I*32+:32]<=data_src_switch_lead ? data_bare[I*32+:32]+pad_half_delayed[I*32+:32] : acc_ring_lead_out[I*32+:32];
`else
for(I=0; I<5; I=I+1)
data[I*32+:32]<=data_bare[I*32+:32]+pad_half_delayed[I*32+:32];
`endif
pad_half_delayed <= pad_ring_mid_data;
//if(mode==3 || mode_0)
if(status==0 && mode_0)
begin
ring_status2_1<=1;
ring_status2_2<=1;
ring_status2_3<=1;
end
else if(status==0 && mode_1 && ring_status==0)
begin
ring_status2_1<=3'b010;
ring_status2_2<=3'b010;
ring_status2_3<=3'b010;
end
else if(status==0 && mode_1 && ring_status==1 && ring_init_count == 0)
begin
ring_status2_1<=3'b110;
ring_status2_2<=3'b110;
ring_status2_3<=3'b110;
end
else if(status==1 && last_iter && first_pass)// fires when loop_counter is Niter and inst_counter is 0
begin
ring_status2_1<=0;
ring_status2_2<=0;
ring_status2_3<=0;
end
if(mode==3 || mode==0)
begin
status<=0;
ring_status<=0;
end
if(status==0)
data_src_switch<=0;
else if(second_iter && first_pass)
data_src_switch<=1;
if(status==0)
data_src_switch_lead<=0;
else if(first_iter && last_pass)
data_src_switch_lead<=1;
if(status==0 && mode==1 && ring_status==0)
begin
ring_status<=1;
ring_init_count<=3;
end
if(status==0 && mode==1 && ring_status==1)
begin
ring_init_count <= ring_init_count-1;
if(ring_init_count == 0)
begin
$display("Core %d job %d filled", instance_id, job_count);
//$display("%x %x %x", pad_buf.data[0], pad_buf.data[1], acc_buf.data[0]);
// counter <= N;
loop_counter <= 0;
inst_counter <= N;
job_count<=(job_count!=32'hffffffff)?job_count+1:0;