-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmodular_square_8_cycles.sv
803 lines (710 loc) · 30.8 KB
/
modular_square_8_cycles.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
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
/*******************************************************************************
Copyright 2019 Supranational LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************/
// Enable 26x17 bit multiplies (17x17 bit multiplies if commented out)
//`define DSP26BITS 1
module modular_square_8_cycles
#(
parameter int REDUNDANT_ELEMENTS = 2,
parameter int NONREDUNDANT_ELEMENTS = 8,
parameter int NUM_SEGMENTS = 4,
parameter int BIT_LEN = 17,
parameter int WORD_LEN = 16,
parameter int NUM_ELEMENTS = REDUNDANT_ELEMENTS +
NONREDUNDANT_ELEMENTS
)
(
input logic clk,
input logic reset,
input logic start,
input logic [BIT_LEN-1:0] sq_in[NUM_ELEMENTS],
output logic [BIT_LEN-1:0] sq_out[NUM_ELEMENTS],
output logic valid
);
localparam int SEGMENT_ELEMENTS = int'(NONREDUNDANT_ELEMENTS /
NUM_SEGMENTS);
localparam int MUL_NUM_ELEMENTS = REDUNDANT_ELEMENTS + SEGMENT_ELEMENTS;
localparam int EXTRA_ELEMENTS = 2;
localparam int ONE_SEGMENT = SEGMENT_ELEMENTS + EXTRA_ELEMENTS +
REDUNDANT_ELEMENTS;
localparam int TWO_SEGMENTS = (SEGMENT_ELEMENTS*2) + EXTRA_ELEMENTS +
REDUNDANT_ELEMENTS;
localparam int THREE_SEGMENTS = (SEGMENT_ELEMENTS*3) + EXTRA_ELEMENTS +
REDUNDANT_ELEMENTS;
localparam int NUM_MULTIPLIERS = 2;
`ifdef DSP26BITS
localparam int MUL_BIT_LEN = WORD_LEN + $clog2(MUL_NUM_ELEMENTS*2);
`else
localparam int EXTRA_MUL_TREE_BITS = (BIT_LEN > WORD_LEN) ?
$clog2(MUL_NUM_ELEMENTS) :
$clog2(MUL_NUM_ELEMENTS*2);
localparam int MUL_BIT_LEN = ((BIT_LEN*2) - WORD_LEN) +
EXTRA_MUL_TREE_BITS;
`endif
// Accumulator tree adds up to 9 values together of various lengths
// 1*BIT_LEN
// 4*WORD_LEN
// 4*(MUL_BIT_LEN - WORD_LEN)
// Brute force method here to calculate bits needed for sum
// Note this doesn't work for 64b and above
// TODO - need better method here, not using large conditionals though
localparam longint MAX_VALUE = ((2**BIT_LEN)-1) +
(((2**WORD_LEN)-1) << 2) +
(((2**(MUL_BIT_LEN-WORD_LEN))-1) << 2);
localparam int GRID_BIT_LEN = $clog2(MAX_VALUE);
localparam int GRID_PAD = GRID_BIT_LEN - WORD_LEN;
localparam int GRID_PAD_CARRY = GRID_BIT_LEN - (MUL_BIT_LEN - WORD_LEN);
localparam int GRID_PAD_C_SHIFT = GRID_PAD_CARRY - 1;
localparam int GRID_PAD_RESULT = GRID_BIT_LEN - BIT_LEN;
localparam int GRID_NUM_ELEMENTS = 9;
// TODO - The +1 is not really needed. Used in loops below for convenience
// Because there is a j+1 in setting carry over
`ifdef DSP26BITS
localparam int GRID_SIZE = (MUL_NUM_ELEMENTS*2) + 1 +
SEGMENT_ELEMENTS;
`else
localparam int GRID_SIZE = (MUL_NUM_ELEMENTS*2) + 1 +
(MUL_NUM_ELEMENTS - REDUNDANT_ELEMENTS);
`endif
localparam int LOOK_UP_WIDTH = int'(WORD_LEN / 2);
localparam int LUT_SIZE = 2**LOOK_UP_WIDTH;
localparam int LUT_MASK = (2**LOOK_UP_WIDTH)-1;
localparam int LUT_WIDTH = WORD_LEN * NONREDUNDANT_ELEMENTS;
localparam int ACC_ELEMENTS = TWO_SEGMENTS;
localparam int ACC_EXTRA_ELEMENTS = 3; // Prev, V3, V2V0
localparam int ACC_EXTRA_BIT_LEN = $clog2(ACC_ELEMENTS+ACC_EXTRA_ELEMENTS);
localparam int ACC_BIT_LEN = BIT_LEN + ACC_EXTRA_BIT_LEN;
localparam int IDLE = 0,
CYCLE_0 = 1,
CYCLE_1 = 2,
CYCLE_2 = 3,
CYCLE_3 = 4,
CYCLE_4 = 5,
CYCLE_5 = 6,
CYCLE_6 = 7,
CYCLE_7 = 8,
CYCLE_8 = 9,
CYCLE_9 = 10,
NUM_CYCLES = 11;
// Flop incoming data from external source
logic [BIT_LEN-1:0] sq_in_d1[NUM_ELEMENTS];
logic start_d1;
// Flop the lower half of sq_out -> sq_in
logic [BIT_LEN-1:0] sq_out_d1[NONREDUNDANT_ELEMENTS/2];
// Input to square (start of phase 1)
logic [BIT_LEN-1:0] curr_sq_in[NUM_ELEMENTS];
// Cycle number state machine
logic [NUM_CYCLES-1:0] next_cycle;
logic [NUM_CYCLES-1:0] curr_cycle;
// Multiplier selects in/out and values
logic mul_result_shift[NUM_MULTIPLIERS];
logic mul1_first_select;
logic [1:0] mul_A_select[NUM_MULTIPLIERS];
logic [1:0] mul_B_select[NUM_MULTIPLIERS];
logic [BIT_LEN-1:0] mul_A[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS];
logic [BIT_LEN-1:0] mul_B[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS];
`ifdef DSP26BITS
logic [MUL_BIT_LEN-1:0] mul_cout[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS*2+1];
logic [MUL_BIT_LEN-1:0] mul_s[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS*2+1];
`else
logic [MUL_BIT_LEN-1:0] mul_cout[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS*2];
logic [MUL_BIT_LEN-1:0] mul_s[NUM_MULTIPLIERS][MUL_NUM_ELEMENTS*2];
`endif
logic [GRID_BIT_LEN-1:0] grid[GRID_SIZE][GRID_NUM_ELEMENTS];
logic [GRID_BIT_LEN-1:0] C[GRID_SIZE];
logic [GRID_BIT_LEN-1:0] S[GRID_SIZE];
logic [GRID_BIT_LEN:0] grid_sum[GRID_SIZE];
logic [BIT_LEN-1:0] reduced_grid_sum[GRID_SIZE];
logic [BIT_LEN-1:0] v7v6[ACC_ELEMENTS];
logic [BIT_LEN-1:0] v5_partial[SEGMENT_ELEMENTS];
logic [BIT_LEN-1:0] v5v4_partial[ACC_ELEMENTS];
logic [BIT_LEN-1:0] v5v4[ACC_ELEMENTS];
logic [BIT_LEN-1:0] v3_partial[SEGMENT_ELEMENTS];
// TODO - does v3 need to be ONE_SEGMENT
logic [BIT_LEN-1:0] v3[SEGMENT_ELEMENTS+REDUNDANT_ELEMENTS];
logic [BIT_LEN-1:0] v2_partial[SEGMENT_ELEMENTS];
logic [BIT_LEN-1:0] v2v0[THREE_SEGMENTS];
logic [BIT_LEN-1:0] curr_lookup_segment[ACC_ELEMENTS];
logic curr_lookup_shift;
logic curr_lookup_upper_table;
logic curr_lookup_check_overflow;
logic curr_overflow;
logic set_overflow;
logic v7v6_overflow;
logic v5v4_overflow;
logic [LOOK_UP_WIDTH:0] lut_addr[ACC_ELEMENTS];
logic [BIT_LEN-1:0] lut_data[NUM_ELEMENTS][ACC_ELEMENTS];
logic [ACC_BIT_LEN-1:0] acc_stack[NUM_ELEMENTS][ACC_ELEMENTS +
ACC_EXTRA_ELEMENTS];
logic [ACC_BIT_LEN-1:0] acc_C[NUM_ELEMENTS];
logic [ACC_BIT_LEN-1:0] acc_S[NUM_ELEMENTS];
logic [ACC_BIT_LEN:0] acc_sum[NUM_ELEMENTS];
logic [BIT_LEN-1:0] reduced_acc_sum[NUM_ELEMENTS];
logic out_valid;
// State machine setting values based on current cycle
always_comb begin
next_cycle = '0;
out_valid = 1'b0;
mul_A_select[0] = 2'b00;
mul_B_select[0] = 2'b00;
mul_A_select[1] = 2'b00;
mul_B_select[1] = 2'b00;
mul_result_shift[0] = 1'b0;
mul_result_shift[1] = 1'b0;
mul1_first_select = 1'b0;
if (reset) begin
next_cycle = '0;
next_cycle[IDLE] = 1'b1;
out_valid = 1'b0;
end
else begin
unique case(1'b1)
curr_cycle[IDLE]: begin
if (start) begin
next_cycle[CYCLE_0] = 1'b1;
end
else begin
next_cycle[IDLE] = 1'b1;
end
end
curr_cycle[CYCLE_0]: begin
mul_A_select[0] = 2'b11;
mul_B_select[0] = 2'b10;
mul_A_select[1] = 2'b11;
mul_B_select[1] = 2'b11;
next_cycle[CYCLE_1] = 1'b1;
end
curr_cycle[CYCLE_1]: begin
mul_A_select[0] = 2'b10;
mul_B_select[0] = 2'b10;
mul_A_select[1] = 2'b11;
mul_B_select[1] = 2'b01;
next_cycle[CYCLE_2] = 1'b1;
end
curr_cycle[CYCLE_2]: begin
mul_A_select[0] = 2'b11;
mul_B_select[0] = 2'b00;
mul_A_select[1] = 2'b10;
mul_B_select[1] = 2'b01;
mul_result_shift[0] = 1'b1;
mul_result_shift[1] = 1'b0;
mul1_first_select = 1'b0;
next_cycle[CYCLE_3] = 1'b1;
end
curr_cycle[CYCLE_3]: begin
mul_A_select[0] = 2'b10;
mul_B_select[0] = 2'b00;
mul_A_select[1] = 2'b01;
mul_B_select[1] = 2'b01;
mul_result_shift[0] = 1'b0;
mul_result_shift[1] = 1'b1;
mul1_first_select = 1'b1;
next_cycle[CYCLE_4] = 1'b1;
end
curr_cycle[CYCLE_4]: begin
mul_A_select[0] = 2'b00;
mul_B_select[0] = 2'b00;
mul_A_select[1] = 2'b01;
mul_B_select[1] = 2'b00;
mul_result_shift[0] = 1'b1;
mul_result_shift[1] = 1'b1;
mul1_first_select = 1'b1;
next_cycle[CYCLE_5] = 1'b1;
end
curr_cycle[CYCLE_5]: begin
mul_result_shift[0] = 1'b1;
mul_result_shift[1] = 1'b0;
mul1_first_select = 1'b1;
next_cycle[CYCLE_6] = 1'b1;
end
curr_cycle[CYCLE_6]: begin
mul_result_shift[0] = 1'b0;
mul_result_shift[1] = 1'b1;
mul1_first_select = 1'b0;
next_cycle[CYCLE_7] = 1'b1;
end
curr_cycle[CYCLE_7]: begin
if (v5v4_overflow || v7v6_overflow) begin
next_cycle[CYCLE_8] = 1'b1;
end
else begin
next_cycle[CYCLE_0] = 1'b1;
out_valid = 1'b1;
end
end
curr_cycle[CYCLE_8]: begin
if (v5v4_overflow && v7v6_overflow) begin
next_cycle[CYCLE_9] = 1'b1;
end
else begin
next_cycle[CYCLE_0] = 1'b1;
out_valid = 1'b1;
end
end
curr_cycle[CYCLE_9]: begin
next_cycle[CYCLE_0] = 1'b1;
out_valid = 1'b1;
end
endcase
end
end
// Drive output valid signal
// Flop incoming start signal and data
always_ff @(posedge clk) begin
if (reset) begin
valid <= 1'b0;
start_d1 <= 1'b0;
end
else begin
valid <= out_valid;
// Keep start high once set until sq_out is valid for loopback
start_d1 <= start || (start_d1 && ~out_valid);
end
curr_cycle <= next_cycle;
if (start) begin
for (int k=0; k<NUM_ELEMENTS; k=k+1) begin
sq_in_d1[k][BIT_LEN-1:0] <= sq_in[k][BIT_LEN-1:0];
end
end
end
always_ff @(posedge clk) begin
for (int k=0; k<NONREDUNDANT_ELEMENTS/2; k=k+1) begin
sq_out_d1[k] <= sq_out[k];
end
end
// Mux square input from external or loopback
// When looping back use the flopped lower half coefficients
always_comb begin
for (int k=0; k<NONREDUNDANT_ELEMENTS/2; k=k+1) begin
curr_sq_in[k][BIT_LEN-1:0] = sq_out_d1[k][BIT_LEN-1:0];
if (start_d1) begin
curr_sq_in[k][BIT_LEN-1:0] = sq_in_d1[k][BIT_LEN-1:0];
end
end
for (int k=NONREDUNDANT_ELEMENTS/2; k<NUM_ELEMENTS; k=k+1) begin
curr_sq_in[k][BIT_LEN-1:0] = sq_out[k][BIT_LEN-1:0];
if (start_d1) begin
curr_sq_in[k][BIT_LEN-1:0] = sq_in_d1[k][BIT_LEN-1:0];
end
end
end
always_comb begin
// Select multiplier input sources
for (int k=0; k<NUM_MULTIPLIERS; k=k+1) begin
for (int l=0; l<SEGMENT_ELEMENTS; l=l+1) begin
unique case(mul_A_select[k])
2'b00: begin
mul_A[k][l][BIT_LEN-1:0] =
curr_sq_in[l][BIT_LEN-1:0];
end
2'b01: begin
mul_A[k][l][BIT_LEN-1:0] =
curr_sq_in[SEGMENT_ELEMENTS+l][BIT_LEN-1:0];
end
2'b10: begin
mul_A[k][l][BIT_LEN-1:0] =
curr_sq_in[(SEGMENT_ELEMENTS*2)+l][BIT_LEN-1:0];
end
2'b11: begin
mul_A[k][l][BIT_LEN-1:0] =
curr_sq_in[(SEGMENT_ELEMENTS*3)+l][BIT_LEN-1:0];
end
endcase
unique case(mul_B_select[k])
2'b00: begin
mul_B[k][l][BIT_LEN-1:0] =
curr_sq_in[l][BIT_LEN-1:0];
end
2'b01: begin
mul_B[k][l][BIT_LEN-1:0] =
curr_sq_in[SEGMENT_ELEMENTS+l][BIT_LEN-1:0];
end
2'b10: begin
mul_B[k][l][BIT_LEN-1:0] =
curr_sq_in[(SEGMENT_ELEMENTS*2)+l][BIT_LEN-1:0];
end
2'b11: begin
mul_B[k][l][BIT_LEN-1:0] =
curr_sq_in[(SEGMENT_ELEMENTS*3)+l][BIT_LEN-1:0];
end
endcase
end
// Redundant elements are only used as extension to highest element
for (int l=REDUNDANT_ELEMENTS; l>0; l=l-1) begin
mul_A[k][MUL_NUM_ELEMENTS-l][BIT_LEN-1:0] = '0;
mul_B[k][MUL_NUM_ELEMENTS-l][BIT_LEN-1:0] = '0;
if (mul_A_select[k] == 2'b11) begin
mul_A[k][MUL_NUM_ELEMENTS-l][BIT_LEN-1:0] =
curr_sq_in[NUM_ELEMENTS-l][BIT_LEN-1:0];
end
if (mul_B_select[k] == 2'b11) begin
mul_B[k][MUL_NUM_ELEMENTS-l][BIT_LEN-1:0] =
curr_sq_in[NUM_ELEMENTS-l][BIT_LEN-1:0];
end
end
end
end
genvar i;
// Instantiate multipliers
generate
for (i=0; i<NUM_MULTIPLIERS; i=i+1) begin : mul
`ifdef DSP26BITS
multiply_diff_widths #(.NUM_ELEMENTS(MUL_NUM_ELEMENTS),
.LG_BIT_LEN(26),
.BIT_LEN(BIT_LEN),
.LG_WORD_LEN(25),
.WORD_LEN(WORD_LEN)
)
multiply (
.clk(clk),
.A(mul_A[i]),
.B(mul_B[i]),
.Cout(mul_cout[i]),
.S(mul_s[i])
);
`else
multiply #(.NUM_ELEMENTS(MUL_NUM_ELEMENTS),
.A_BIT_LEN(BIT_LEN),
.B_BIT_LEN(BIT_LEN),
.WORD_LEN(WORD_LEN)
)
multiply (
.clk(clk),
.A(mul_A[i]),
.B(mul_B[i]),
.Cout(mul_cout[i]),
.S(mul_s[i])
);
`endif
end
endgenerate
always_comb begin
// Initialize grid for accumulating multiplier results across columns
for (int k=0; k<GRID_SIZE; k=k+1) begin
for (int l=0; l<GRID_NUM_ELEMENTS; l=l+1) begin
grid[k][l] = '0;
end
end
// Place multiplier results into proper grid location
for (int k=0; k<(MUL_NUM_ELEMENTS*2); k=k+1) begin
if (mul_result_shift[0] == 1'b1) begin
grid[k][0] = {{GRID_PAD{1'b0}},
mul_cout[0][k][WORD_LEN-2:0] , 1'b0};
grid[k+1][1] = {{GRID_PAD_C_SHIFT{1'b0}},
mul_cout[0][k][MUL_BIT_LEN-1:WORD_LEN-1]};
grid[k][2] = {{GRID_PAD{1'b0}},
mul_s[0][k][WORD_LEN-2:0] , 1'b0};
grid[k+1][3] = {{GRID_PAD_C_SHIFT{1'b0}},
mul_s[0][k][MUL_BIT_LEN-1:WORD_LEN-1]};
end
else begin
grid[k][0] = {{GRID_PAD{1'b0}},
mul_cout[0][k][WORD_LEN-1:0]};
grid[k+1][1] = {{GRID_PAD_CARRY{1'b0}},
mul_cout[0][k][MUL_BIT_LEN-1:WORD_LEN]};
grid[k][2] = {{GRID_PAD{1'b0}},
mul_s[0][k][WORD_LEN-1:0]};
grid[k+1][3] = {{GRID_PAD_CARRY{1'b0}},
mul_s[0][k][MUL_BIT_LEN-1:WORD_LEN]};
end
if (mul1_first_select == 1'b1) begin
if (mul_result_shift[1] == 1'b1) begin
grid[k][4] = {{GRID_PAD{1'b0}},
mul_cout[1][k][WORD_LEN-2:0] , 1'b0};
grid[k+1][5] = {{GRID_PAD_C_SHIFT{1'b0}},
mul_cout[1][k][MUL_BIT_LEN-1:WORD_LEN-1]};
grid[k][6] = {{GRID_PAD{1'b0}},
mul_s[1][k][WORD_LEN-2:0] , 1'b0};
grid[k+1][7] = {{GRID_PAD_C_SHIFT{1'b0}},
mul_s[1][k][MUL_BIT_LEN-1:WORD_LEN-1]};
end
else begin
grid[k][4] = {{GRID_PAD{1'b0}},
mul_cout[1][k][WORD_LEN-1:0]};
grid[k+1][5] = {{GRID_PAD_CARRY{1'b0}},
mul_cout[1][k][MUL_BIT_LEN-1:WORD_LEN]};
grid[k][6] = {{GRID_PAD{1'b0}},
mul_s[1][k][WORD_LEN-1:0]};
grid[k+1][7] = {{GRID_PAD_CARRY{1'b0}},
mul_s[1][k][MUL_BIT_LEN-1:WORD_LEN]};
end
end
else begin
if (mul_result_shift[1] == 1'b1) begin
grid[k+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][4] =
{{GRID_PAD{1'b0}}, mul_cout[1][k][WORD_LEN-2:0] , 1'b0};
grid[k+1+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][5] =
{{GRID_PAD_C_SHIFT{1'b0}},
mul_cout[1][k][MUL_BIT_LEN-1:WORD_LEN-1]};
grid[k+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][6] =
{{GRID_PAD{1'b0}}, mul_s[1][k][WORD_LEN-2:0] , 1'b0};
grid[k+1+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][7] =
{{GRID_PAD_C_SHIFT{1'b0}},
mul_s[1][k][MUL_BIT_LEN-1:WORD_LEN-1]};
end
else begin
grid[k+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][4] =
{{GRID_PAD{1'b0}}, mul_cout[1][k][WORD_LEN-1:0]};
grid[k+1+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][5] =
{{GRID_PAD_CARRY{1'b0}},
mul_cout[1][k][MUL_BIT_LEN-1:WORD_LEN]};
grid[k+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][6] =
{{GRID_PAD{1'b0}}, mul_s[1][k][WORD_LEN-1:0]};
grid[k+1+(MUL_NUM_ELEMENTS-REDUNDANT_ELEMENTS)][7] =
{{GRID_PAD_CARRY{1'b0}},
mul_s[1][k][MUL_BIT_LEN-1:WORD_LEN]};
end
end
end
// Set last grid row based on cycle
if (curr_cycle[CYCLE_3]) begin
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
grid[k+SEGMENT_ELEMENTS][8] = {{GRID_PAD_RESULT{1'b0}},
v5_partial[k][BIT_LEN-1:0]};
end
end
if (curr_cycle[CYCLE_4]) begin
for (int k=0; k<(ACC_ELEMENTS); k=k+1) begin
grid[k+SEGMENT_ELEMENTS][8] = {{GRID_PAD_RESULT{1'b0}},
v5v4_partial[k][BIT_LEN-1:0]};
end
end
if (curr_cycle[CYCLE_5]) begin
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
grid[k+SEGMENT_ELEMENTS][8] = {{GRID_PAD_RESULT{1'b0}},
v3_partial[k][BIT_LEN-1:0]};
end
end
if (curr_cycle[CYCLE_6]) begin
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
grid[k+(SEGMENT_ELEMENTS*2)][8] = {{GRID_PAD_RESULT{1'b0}},
v2_partial[k][BIT_LEN-1:0]};
end
end
end
// Instantiate compressor trees to accumulate over grid columns
generate
for (i=0; i<GRID_SIZE; i=i+1) begin : grid_acc
compressor_tree_3_to_2 #(.NUM_ELEMENTS(GRID_NUM_ELEMENTS),
.BIT_LEN(GRID_BIT_LEN)
)
compressor_tree_3_to_2 (
.terms(grid[i]),
.C(C[i]),
.S(S[i])
);
end
endgenerate
// Carry propogate add each column in grid
// Partially reduce adding neighbor carries
always_comb begin
for (int k=0; k<GRID_SIZE; k=k+1) begin
grid_sum[k][GRID_BIT_LEN:0] = C[k][GRID_BIT_LEN-1:0] +
S[k][GRID_BIT_LEN-1:0];
end
reduced_grid_sum[0] = {{(BIT_LEN-WORD_LEN){1'b0}},
grid_sum[0][WORD_LEN-1:0]};
for (int k=1; k<GRID_SIZE-1; k=k+1) begin
reduced_grid_sum[k] = {{(BIT_LEN-WORD_LEN){1'b0}},
grid_sum[k][WORD_LEN-1:0]} +
{{(BIT_LEN-(GRID_BIT_LEN-WORD_LEN))-1{1'b0}},
grid_sum[k-1][GRID_BIT_LEN:WORD_LEN]};
end
reduced_grid_sum[GRID_SIZE-1] = grid_sum[GRID_SIZE-1][BIT_LEN-1:0] +
{{(BIT_LEN-(GRID_BIT_LEN-WORD_LEN))-1{1'b0}},
grid_sum[GRID_SIZE-2][GRID_BIT_LEN:WORD_LEN]};
end
// Flop segments out of grid accumulator based on cycle
always_ff @(posedge clk) begin
if (curr_cycle[CYCLE_2]) begin
for (int k=0; k<ACC_ELEMENTS; k=k+1) begin
v7v6[k] <= reduced_grid_sum[k + SEGMENT_ELEMENTS];
end
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
v5_partial[k] <= reduced_grid_sum[k];
end
end
if (curr_cycle[CYCLE_3]) begin
for (int k=0; k<ACC_ELEMENTS; k=k+1) begin
v5v4_partial[k] <= reduced_grid_sum[k];
end
end
if (curr_cycle[CYCLE_4]) begin
for (int k=0; k<ACC_ELEMENTS; k=k+1) begin
v5v4[k] <= reduced_grid_sum[k + SEGMENT_ELEMENTS];
end
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
v3_partial[k] <= reduced_grid_sum[k];
end
end
if (curr_cycle[CYCLE_5]) begin
for (int k=0; k<(SEGMENT_ELEMENTS+REDUNDANT_ELEMENTS); k=k+1) begin
v3[k] <= reduced_grid_sum[k + SEGMENT_ELEMENTS];
end
for (int k=0; k<SEGMENT_ELEMENTS; k=k+1) begin
v2_partial[k] <= reduced_grid_sum[k];
end
end
if (curr_cycle[CYCLE_6]) begin
for (int k=0; k<THREE_SEGMENTS; k=k+1) begin
v2v0[k] <= reduced_grid_sum[k];
end
end
end
// Set values for which segments to lookup in reduction LUTs
always_comb begin
set_overflow = 1'b0;
if (curr_cycle[CYCLE_3] || curr_cycle[CYCLE_4] ||
(curr_cycle[CYCLE_5] && v7v6_overflow)) begin
curr_lookup_segment = v7v6;
end
else begin
curr_lookup_segment = v5v4;
end
curr_lookup_shift = (curr_cycle[CYCLE_3] ||
(curr_cycle[CYCLE_5] && !v7v6_overflow) ||
(curr_cycle[CYCLE_6] && v7v6_overflow));
curr_lookup_check_overflow = (curr_cycle[CYCLE_4] ||
curr_cycle[CYCLE_6]);
curr_lookup_upper_table = ((curr_cycle[CYCLE_5] && !v7v6_overflow) ||
curr_cycle[CYCLE_6] ||
curr_cycle[CYCLE_7] ||
curr_cycle[CYCLE_8]);
curr_overflow = ((curr_cycle[CYCLE_5] && v7v6_overflow) ||
(curr_cycle[CYCLE_7] &&
(v5v4_overflow & !v7v6_overflow)) ||
curr_cycle[CYCLE_8]);
for (int k=0; k<ACC_ELEMENTS; k=k+1) begin
lut_addr[k][LOOK_UP_WIDTH-1:0] =
curr_lookup_segment[k][LOOK_UP_WIDTH-1:0];
// Use upper part of word
if (curr_lookup_shift) begin
lut_addr[k][LOOK_UP_WIDTH-1:0] =
curr_lookup_segment[k][(LOOK_UP_WIDTH*2)-1:LOOK_UP_WIDTH];
end
// Check if there is an overflow
if (curr_lookup_check_overflow) begin
set_overflow |= (curr_lookup_segment[k][BIT_LEN-1:WORD_LEN] > 0);
end
// If there was an overflow, use upper bit(s) beyond word
if (curr_overflow) begin
lut_addr[k][LOOK_UP_WIDTH-1:0] =
{{(LOOK_UP_WIDTH-(BIT_LEN-WORD_LEN)){1'b0}},
curr_lookup_segment[k][BIT_LEN-1:WORD_LEN]};
end
// Set the upper LUT address bit for V5V4
lut_addr[k][LOOK_UP_WIDTH] = curr_lookup_upper_table;
end
end
// Flop overflow values
always_ff @(posedge clk) begin
if (curr_cycle[CYCLE_4]) begin
v7v6_overflow <= set_overflow;
end
if (curr_cycle[CYCLE_6]) begin
v5v4_overflow <= set_overflow;
end
end
// Instantiate memory holding reduction LUTs
// TODO - remove reduction loading pins or drive them
/* verilator lint_off PINMISSING */
reduction_lut #(.REDUNDANT_ELEMENTS(REDUNDANT_ELEMENTS),
.NONREDUNDANT_ELEMENTS(NONREDUNDANT_ELEMENTS),
.NUM_SEGMENTS(NUM_SEGMENTS),
.WORD_LEN(WORD_LEN)
)
reduction_lut (
.clk(clk),
.shift_high(curr_lookup_shift),
.shift_overflow(curr_overflow),
.lut_addr(lut_addr),
.lut_data(lut_data),
.we(0)
);
/* verilator lint_on PINMISSING */
// Accumulate reduction lut values with running total
always_comb begin
for (int l=0; l<ACC_EXTRA_ELEMENTS; l=l+1) begin
for (int k=0; k<NUM_ELEMENTS; k=k+1) begin
acc_stack[k][l + ACC_ELEMENTS] = '0;
end
end
for (int l=0; l<ACC_ELEMENTS; l=l+1) begin
for (int k=0; k<NUM_ELEMENTS; k=k+1) begin
acc_stack[k][l][ACC_BIT_LEN-1:0] = {{ACC_EXTRA_BIT_LEN{1'b0}},
lut_data[k][l][BIT_LEN-1:0]};
end
end
// Add in V3 - V0
if (curr_cycle[CYCLE_7]) begin
for (int k=0; k<THREE_SEGMENTS; k=k+1) begin
acc_stack[k][ACC_ELEMENTS+1][ACC_BIT_LEN-1:0] =
{{ACC_EXTRA_BIT_LEN{1'b0}}, v2v0[k][BIT_LEN-1:0]};
end
for (int k=0; k<(SEGMENT_ELEMENTS+REDUNDANT_ELEMENTS); k=k+1) begin
acc_stack[k+(SEGMENT_ELEMENTS*3)][ACC_ELEMENTS+2][ACC_BIT_LEN-1:0] =
{{ACC_EXTRA_BIT_LEN{1'b0}}, v3[k][BIT_LEN-1:0]};
end
end
// Add the previous accumulation in
if (curr_cycle[CYCLE_5] || curr_cycle[CYCLE_6] || curr_cycle[CYCLE_7] ||
curr_cycle[CYCLE_8] || curr_cycle[CYCLE_9]) begin
for (int k=0; k<NUM_ELEMENTS; k=k+1) begin
acc_stack[k][ACC_ELEMENTS][ACC_BIT_LEN-1:0] =
{{ACC_EXTRA_BIT_LEN{1'b0}}, sq_out[k][BIT_LEN-1:0]};
end
end
end
// Instantiate compressor trees to accumulate over accumulator columns
generate
for (i=0; i<NUM_ELEMENTS; i=i+1) begin : final_acc
compressor_tree_3_to_2 #(.NUM_ELEMENTS(ACC_ELEMENTS +
ACC_EXTRA_ELEMENTS),
.BIT_LEN(ACC_BIT_LEN)
)
compressor_tree_3_to_2 (
.terms(acc_stack[i]),
.C(acc_C[i]),
.S(acc_S[i])
);
end
endgenerate
// Carry propogate add each column in accumulator result
// Partially reduce adding neighbor carries
always_comb begin
for (int k=0; k<NUM_ELEMENTS; k=k+1) begin
acc_sum[k][ACC_BIT_LEN:0] = acc_C[k][ACC_BIT_LEN-1:0] +
acc_S[k][ACC_BIT_LEN-1:0];
end
reduced_acc_sum[0] = {{(BIT_LEN-WORD_LEN){1'b0}},
acc_sum[0][WORD_LEN-1:0]};
for (int k=1; k<NUM_ELEMENTS-1; k=k+1) begin
reduced_acc_sum[k] = {{(BIT_LEN-WORD_LEN){1'b0}},
acc_sum[k][WORD_LEN-1:0]} +
{{(BIT_LEN-(ACC_BIT_LEN-WORD_LEN))-1{1'b0}},
acc_sum[k-1][ACC_BIT_LEN:WORD_LEN]};
end
reduced_acc_sum[NUM_ELEMENTS-1] = acc_sum[NUM_ELEMENTS-1][BIT_LEN-1:0] +
{{(BIT_LEN-(ACC_BIT_LEN-WORD_LEN))-1{1'b0}},
acc_sum[NUM_ELEMENTS-2][ACC_BIT_LEN:WORD_LEN]};
end
// Flop output
always_ff @(posedge clk) begin
if (curr_cycle[CYCLE_4] || curr_cycle[CYCLE_5] ||
curr_cycle[CYCLE_6] || curr_cycle[CYCLE_7] ||
curr_cycle[CYCLE_8] || curr_cycle[CYCLE_9]) begin
for (int k=0; k<(NUM_ELEMENTS); k=k+1) begin
sq_out[k][BIT_LEN-1:0] <= reduced_acc_sum[k][BIT_LEN-1:0];
end
end
end
endmodule