forked from russdill/bch_verilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bch_math.v
758 lines (656 loc) · 17.8 KB
/
bch_math.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
/*
* BCH Encode/Decoder Modules
*
* Copyright 2014 - Russ Dill <[email protected]>
* Distributed under 2-clause BSD license as contained in COPYING file.
*/
`timescale 1ns / 1ps
`include "config.vh"
/*
* Bit-serial Berlekamp (mixed dual/standard basis) multiplier)
* Can multiply one dual basis input by N_INPUTS standard basis
* inputs in M cycles, producing one bit of each output per
* cycle
*/
module serial_mixed_multiplier #(
parameter M = 4,
parameter N_INPUT = 1
) (
input clk,
input start,
input [M-1:0] dual_in,
input [M*N_INPUT-1:0] standard_in,
output [N_INPUT-1:0] dual_out
);
`include "bch.vh"
localparam TCQ = 1;
reg [M-1:0] lfsr;
always @(posedge clk) begin
if (start)
lfsr <= #TCQ dual_in;
else
lfsr <= #TCQ {^(lfsr & `BCH_POLYNOMIAL(M)), lfsr[M-1:1]};
end
matrix_vector_multiply #(M, N_INPUT) u_mult(standard_in, lfsr, dual_out);
endmodule
/*
* Combine the serial mixed multiplier with a dual basis to standard basis
* conversion so it can feed the serial polynomial multiplier. The serial
* mixed multplier outputs a dual basis result LSB first, the serial polynomial
* multiplier accepts a standard basis input MSB first. An example of a dual
* to standard basis conversion matrix for a trinomial looks like this:
*
* 543210
* ------
* 000001 0
* 100000 1
* 010000 2
* 001000 3
* 000100 4
* 000010 5
*
* So we need the second output of the multiplier first, followed by the 3rd,
* 4th, 5th, 6th, and then the 1st output. We can preload the multiplier state
* register so that it outputs the 2nd bit first, and then load it with the
* original input at the appropriate time. Other trinomials are similar, but
* start with the 3rd, 4th, or 5th output and thus the preload calculation is
* slightly more complicated.
*
* Pentonomials offer a bit more of a challenge. It's easy to see that the
* conversion matrix is divided into a lower matrix and an upper matrix. The
* lower matrix gives an identical start to the trinomial state, but at some
* point, the original value must be added back in.
*
* At some point in time, a switch to the upper matrix is made. The value
* loaded is the combination of 2 or more values. Similar to the lower matrix,
* at certain points in time, values must be subtracted out.
*
* 109876543210
* ------------
* 000000001000 0
* 000000001100 1
* 000000000110 2
* 000000000011 3
* 100000100000 4
* 010000010000 5
* 001000000000 6
* 000100000000 7
* 000010000000 8
* 000001000000 9
* 000000100000 10
* 000000010000 11
*/
module serial_mixed_multiplier_dss #(
parameter M = 4,
parameter N_INPUT = 1
) (
input clk,
input start,
input [M-1:0] dual_in,
input [M*N_INPUT-1:0] standard_in,
output [N_INPUT-1:0] standard_out
);
`include "bch.vh"
localparam TCQ = 1;
/* Width/height of upper matrix */
localparam D = `BCH_DUALD(M);
/* Dual to standard conversion matrix lower */
localparam [M-1:0] DSL = dsl_vector(M);
/* Dual to standard conversion matrix upper */
localparam [M-1:0] DSU = dsu_vector(M);
localparam MAX_COUNT = M - 2;
localparam CB = log2(MAX_COUNT);
reg [M-1:0] lfsr = 0;
wire [M-1:0] lfsr_add;
/* Store original value, and also the upper matrix value for the lfsr */
reg [M-1:0] dual_stored = 0;
wire [CB-1:0] count;
reg change = 0;
wire [M*(D+1)-1:0] dual_dsu_part;
wire [M-1:0] dual_dsu;
wire [M-1:0] dual_dsl;
lfsr_counter #(CB) u_counter(
.clk(clk),
.reset(start),
.ce(1'b1),
.count(count)
);
/* Handle pentanomials */
genvar i;
generate
if (`CONFIG_CONST_OP) begin
/* Fast forward initial multiplier state */
parallel_mixed_multiplier_const_standard #(M, lpow(M, D + 1)) u_dmli1(
.dual_in(dual_in),
.dual_out(dual_dsl)
);
/* Initial LFSR state for DSU */
for (i = 1; i < D + 1; i = i + 1) begin : MULT
if (DSU[i])
parallel_mixed_multiplier_const_standard #(M, lpow(M, i)) u_dmli3(
.dual_in(dual_in),
.dual_out(dual_dsu_part[i*M+:M])
);
else
assign dual_dsu_part[i*M+:M] = 0;
end
end else begin
parallel_mixed_multiplier #(M) u_dmli1(
.dual_in(dual_in),
.standard_in(lpow(M, D + 1)),
.dual_out(dual_dsl)
);
for (i = 1; i < D + 1; i = i + 1) begin : MULT
if (DSU[i])
parallel_mixed_multiplier #(M) u_dmli3(
.dual_in(dual_in),
.standard_in(lpow(M, i)),
.dual_out(dual_dsu_part[i*M+:M])
);
else
assign dual_dsu_part[i*M+:M] = 0;
end
end
endgenerate
assign dual_dsu_part[0+:M] = dual_in;
finite_parallel_adder #(M, D+1) u_adder(dual_dsu_part, dual_dsu);
/*
* Determines when bits are shifting into DSL or out of DSU. This
* determines when to add/subtract out mod
*/
generate
if (`BCH_IS_PENTANOMIAL(M)) begin
/* Modify LFSR */
reg [M-1:0] mod = 0;
wire [M-3:0] vector_bits;
reg vector_bit = 0;
assign vector_bits[0] = DSL[M - D - 3] && start;
for (i = 0; i < M - D - 3; i = i + 1) begin : ASSIGN_DSL
assign vector_bits[i + 1] = DSL[M - D - 4 - i] &&
!start && count == lfsr_count(CB, i);
end
for (i = 0; i < D; i = i + 1) begin : ASSIGN_DSU
assign vector_bits[i + M - D - 2] = DSU[D - i] &&
!start &&
count == lfsr_count(CB, i + M - D - 2);
end
assign lfsr_add = {M{vector_bit}} & mod;
always @(posedge clk) begin
if (start)
/* Load the DSL/DSU shift in/out value */
mod <= #TCQ dual_dsl;
/* Do a shift out/in of mod next cycle */
vector_bit <= |vector_bits;
end
end else
assign lfsr_add = 0;
endgenerate
always @(posedge clk) begin
if (start)
/* Used to load the mod register with a DSU value*/
dual_stored <= #TCQ dual_dsu;
if (M - D == 2)
change <= #TCQ start;
else
change <= #TCQ count == lfsr_count(CB, M - D - 3) &&
!start;
if (start)
/* Start with DSL */
lfsr <= #TCQ dual_dsl;
else if (change)
/* Switch to DSU */
lfsr <= #TCQ dual_stored;
else
lfsr <= #TCQ {^(lfsr & `BCH_POLYNOMIAL(M)), lfsr[M-1:1]} ^
lfsr_add;
end
matrix_vector_multiply #(M, N_INPUT) u_mult(standard_in, lfsr, standard_out);
endmodule
/* Berlekamp bit-parallel dual-basis multiplier */
module parallel_mixed_multiplier #(
parameter M = 4
) (
input [M-1:0] dual_in,
input [M-1:0] standard_in,
output [M-1:0] dual_out
);
`include "bch.vh"
localparam [M-1:0] POLY = `BCH_POLYNOMIAL(M);
wire [M-2:0] aux;
wire [M*2-2:0] all;
assign all = {aux, dual_in};
/* Generate additional terms via an LFSR */
compact_matrix_vector_multiply #(M, M-1) u_lfsr(all[M*2-3:0], POLY, aux);
/* Perform matrix multiplication of terms */
compact_matrix_vector_multiply #(M) u_mult(all, standard_in, dual_out);
endmodule
module parallel_mixed_multiplier_const_dual #(
parameter M = 4,
parameter [M-1:0] DUAL_IN = 1
) (
input [M-1:0] standard_in,
output [M-1:0] dual_out
);
`include "bch.vh"
localparam [M-1:0] POLY = `BCH_POLYNOMIAL(M);
function [M*2-2:0] gen_matrix;
input dummy;
integer i;
begin
gen_matrix[0+:M] = DUAL_IN;
for (i = 0; i < M - 1; i = i + 1)
gen_matrix[i+M] = ^(gen_matrix[i+:M] & POLY);
end
endfunction
/* Perform matrix multiplication of terms */
compact_const_matrix_multiply #(.C(M), .MATRIX(gen_matrix(0))) u_mult(standard_in, dual_out);
endmodule
module parallel_mixed_multiplier_const_standard #(
parameter M = 4,
parameter [M-1:0] STANDARD_IN = 1
) (
input [M-1:0] dual_in,
output [M-1:0] dual_out
);
`include "bch.vh"
localparam [M-1:0] POLY = `BCH_POLYNOMIAL(M);
wire [M-2:0] aux;
wire [M*2-2:0] all;
assign all = {aux, dual_in};
/* Generate additional terms via an LFSR */
compact_matrix_vector_multiply #(M, M-1) u_lfsr(all[M*2-3:0], POLY, aux);
/* Perform matrix multiplication of terms */
compact_const_vector_multiply #(M, STANDARD_IN) u_mult(all, dual_out);
endmodule
/* Bit-parallel standard basis multiplier (PPBML) */
module parallel_standard_multiplier #(
parameter M = 4,
parameter N_INPUT = 1
) (
input [M-1:0] standard_in1,
input [M*N_INPUT-1:0] standard_in2,
output [M*N_INPUT-1:0] standard_out
);
function [M*M-1:0] gen_matrix;
input [M-1:0] in;
integer i;
begin
for (i = 0; i < M; i = i + 1)
gen_matrix[i*M+:M] = i ? `BCH_MUL1(M, gen_matrix[(i-1)*M+:M]) : in;
end
endfunction
matrix_vector_multiplyT #(M) u_mult [N_INPUT-1:0] (gen_matrix(standard_in1), standard_in2, standard_out);
endmodule
/* Bit-parallel standard basis multiplier (PPBML) */
module parallel_standard_multiplier_const1 #(
parameter M = 4,
parameter [M-1:0] STANDARD_IN1 = 0
) (
input [M-1:0] standard_in,
output [M-1:0] standard_out
);
`include "bch.vh"
function [M*M-1:0] gen_matrix;
input dummy;
integer i;
begin
for (i = 0; i < M; i = i + 1)
gen_matrix[i*M+:M] = i ? `BCH_MUL1(M, gen_matrix[(i-1)*M+:M]) : STANDARD_IN1;
end
endfunction
const_matrix_multiplyT #(.C(M), .MATRIX(gen_matrix(0))) u_mult(standard_in, standard_out);
endmodule
module parallel_standard_multiplier_const2 #(
parameter M = 4,
parameter [M-1:0] STANDARD_IN2 = 0
) (
input [M-1:0] standard_in,
output [M-1:0] standard_out
);
function [M*M-1:0] gen_matrix;
input [M-1:0] in;
integer i;
begin
for (i = 0; i < M; i = i + 1)
gen_matrix[i*M+:M] = i ? `BCH_MUL1(M, gen_matrix[(i-1)*M+:M]) : in;
end
endfunction
const_vector_multiplyT #(.C(M), .VECTOR(STANDARD_IN2)) u_mult(gen_matrix(standard_in), standard_out);
endmodule
/*
* Final portion of MSB first bit-serial standard basis multiplier (SPBMM)
* Input per cycle:
* M{a[M-1]} & b
* M{a[M-2]} & b
* ...
* M[a[0]} & b
* All products of input pairs are summed together.
* Takes M cycles
*/
module serial_standard_multiplier #(
parameter M = 4,
parameter N_INPUT = 1
) (
input clk,
input reset,
input ce,
input [M*N_INPUT-1:0] parallel_in,
input [N_INPUT-1:0] serial_in,
output reg [M-1:0] out = 0
);
`include "bch.vh"
localparam TCQ = 1;
wire [M*N_INPUT-1:0] z;
wire [M-1:0] in;
wire [M-1:0] out_mul1;
assign out_mul1 = `BCH_MUL1(M, out);
genvar i;
for (i = 0; i < N_INPUT; i = i + 1) begin : mult
assign z[i*M+:M] = {M{serial_in[i]}} & parallel_in[i*M+:M];
end
finite_parallel_adder #(M, N_INPUT+1) u_adder({z, out_mul1}, in);
always @(posedge clk) begin
if (reset)
out <= #TCQ 0;
else if (ce)
out <= #TCQ in;
end
endmodule
/* Raise standard basis input to a power */
module parallel_standard_power #(
parameter M = 4,
parameter P = 2
) (
input [M-1:0] standard_in,
output [M-1:0] standard_out
);
`include "bch.vh"
function [M*M-1:0] gen_matrix;
input dummy;
integer i;
begin
for (i = 0; i < M; i = i + 1)
gen_matrix[i*M+:M] = lpow(M, i * P);
end
endfunction
if (`CONFIG_CONST_OP)
const_matrix_multiplyT #(.C(M), .MATRIX(gen_matrix(0))) u_mult(standard_in, standard_out);
else
matrix_vector_multiplyT #(M) u_mult(gen_matrix(0), standard_in, standard_out);
endmodule
/*
* Divider, takes M clock cycles.
* Inverse of denominator is calculated by using fermat inverter:
* a^(-1) = a^(2^n-2) = (a^2)*(a^2^2)*(a^2^3)....*(a^2^(m-1))
* Wang, Charles C., et al. "VLSI architectures for computing multiplications
* and inverses in GF (2 m)." Computers, IEEE Transactions on 100.8 (1985):
* 709-717.
*
* Load denominator with start=1. If !busy (M cyles have passed), result is
* in dual_out. Numerator is not required until busy is low.
*/
module finite_divider #(
parameter M = 6
) (
input clk,
input start,
input [M-1:0] standard_numer,
input [M-1:0] standard_denom,
output [M-1:0] dual_out,
output reg busy = 0
);
`include "bch.vh"
localparam TCQ = 1;
localparam DONE = lfsr_count(log2(M), M - 2);
localparam INITIAL = `BCH_DUAL(M);
reg [M-1:0] standard_a = 0;
wire [M-1:0] standard_b;
reg [M-1:0] dual_c = INITIAL;
wire [M-1:0] dual_d;
wire [log2(M)-1:0] count;
assign dual_out = dual_d;
/* Square the input each cycle */
parallel_standard_power #(M, 2) u_dsq(
.standard_in(start ? standard_denom : standard_a),
.standard_out(standard_b)
);
/*
* Accumulate the term each cycle (Reuse for C = A*B^(-1) )
* Reuse multiplier to multiply by numerator
*/
parallel_mixed_multiplier #(M) u_parallel_mixed_multiplier(
.dual_in(dual_c),
.standard_in(busy ? standard_a : standard_numer),
.dual_out(dual_d)
);
lfsr_counter #(log2(M)) u_counter(
.clk(clk),
.reset(start),
.ce(busy),
.count(count)
);
always @(posedge clk) begin
if (start)
busy <= #TCQ 1;
else if (count == DONE)
busy <= #TCQ 0;
if (start)
dual_c <= #TCQ INITIAL;
else if (busy)
dual_c <= #TCQ dual_d;
if (start || busy)
standard_a <= #TCQ standard_b;
end
endmodule
/*
* Takes input for M clock cycles (MSB concurrent with start).
* Inverted input is available after an additional M clock cycles.
* Input is required to be 0 during second set of M clock cycles.
* Berlekamp-Massy algorithm performing gauss elimination. Documented in
* VLSI Aspects on Inversion in Finite Fields, Mikael Olofsson 2002.
*/
module berlekamp_inverter #(
parameter M = 4
) (
input clk,
input start,
input standard_in,
output [M-1:0] standard_out
);
localparam TCQ = 1;
reg [M-1:0] A = 0;
reg [M-2:0] B = 0;
reg [M-2:0] C = 0;
reg [$clog2(M+1)-1:0] r = 0;
reg [$clog2(M+1)-1:0] s = 0;
reg rs_next = 0; /* Set if r < s for next cycle */
reg ff = 0;
wire delta;
wire Tr;
assign Tr = ff ^ standard_in;
/* Note: delta is very wide, 2M + 2 */
assign delta = ^(B & C) ^ Tr;
assign standard_out = A;
always @(posedge clk) begin
if (start) begin
r <= #TCQ !standard_in;
s <= #TCQ standard_in;
rs_next <= #TCQ standard_in;
ff <= #TCQ 0;
A <= #TCQ (standard_in << (M - 1));
B <= #TCQ 0;
C <= #TCQ (standard_in << (M - 2));
end else begin
if (!delta || rs_next) begin
A <= #TCQ A >> 1;
r <= #TCQ r + 1'b1;
rs_next <= #TCQ r + 1 < s;
end else begin
A <= #TCQ {1'b1, B};
s <= #TCQ r + 1'b1;
r <= #TCQ s;
rs_next <= s < r + 1;
end
ff <= #TCQ ^(C & `BCH_POLYNOMIAL(M));
B <= #TCQ B ^ ({M-1{delta}} & (A >> 1));
C <= #TCQ (Tr << (M - 2)) | (C >> 1);
end
end
endmodule
/*
* Inverter, takes M clock cycles.
* Inverse of denominator is calculated by using fermat inverter:
* a^(-1) = a^(2^n-2) = (a^2)*(a^2^2)*(a^2^3)....*(a^2^(m-1))
* Wang, Charles C., et al. "VLSI architectures for computing multiplications
* and inverses in GF (2 m)." Computers, IEEE Transactions on 100.8 (1985):
* 709-717.
*/
module fermat_inverter #(
parameter M = 6
) (
input clk,
input start,
input [M-1:0] standard_in,
output [M-1:0] dual_out
);
`include "bch.vh"
localparam TCQ = 1;
localparam INITIAL = `BCH_DUAL(M);
reg [M-1:0] standard_a = 0;
wire [M-1:0] standard_b;
reg [M-1:0] dual_c = INITIAL;
wire [M-1:0] dual_d;
assign dual_out = dual_c;
/* Square the input each cycle */
parallel_standard_power #(M, 2) u_dsq(
.standard_in(start ? standard_in : standard_a),
.standard_out(standard_b)
);
/* Accumulate the term each cycle */
parallel_mixed_multiplier #(M) u_parallel_mixed_multiplier(
.dual_in(dual_c),
.standard_in(standard_a),
.dual_out(dual_d)
);
always @(posedge clk) begin
dual_c <= #TCQ start ? INITIAL : dual_d;
standard_a <= #TCQ standard_b;
end
endmodule
/* out = in^3 (standard basis). Saves space vs in^2 * in */
module pow3 #(
parameter M = 4
) (
input [M-1:0] in,
output [M-1:0] out
);
`include "bch.vh"
genvar i, j, k;
wire [M-1:0] ft_in;
wire [M*M-1:0] st_in;
generate
for (i = 0; i < M; i = i + 1) begin : FIRST_TERM
localparam BITS = lpow(M, 3 * i);
/* first_term = a_i * alpha^(3*i) */
assign ft_in[i] = in[i];
end
/* i = 0 to m - 2, j = i to m - 1 */
for (k = 0; k < M * M; k = k + 1) begin : SECOND_TERM
/* i = k / M, j = j % M */
/* second_term = a_i * a_j * (alpha^(2*i+j) + alpha^(2*i+j)) */
localparam BITS = (k/M < k%M) ? (lpow(M, 2*(k/M)+k%M) ^ lpow(M, 2*(k%M)+k/M)) : 0;
assign st_in[k] = (k/M < k%M) ? (in[k/M] & in[k%M]) : 0;
end
for (i = 0; i < M; i = i + 1) begin : CALC
wire [M-1:0] first_term;
wire [M*M-1:0] second_term;
/* Rearrange bits for multiplication */
for (j = 0; j < M; j = j + 1) begin : arrange1
assign first_term[j] = FIRST_TERM[j].BITS[i];
end
for (j = 0; j < M*M; j = j + 1) begin : arrange2
assign second_term[j] = SECOND_TERM[j].BITS[i];
end
/* a^3 = first_term + second_term*/
assign out[i] = ^(ft_in & first_term) ^ ^(st_in & second_term);
end
endgenerate
endmodule
/*
* Finite adder, xor each bit
* Note that for adders with more than 6 inputs, we can utilize the carry
* chain by passing the output of the carry chain xor through [A-D]MUX and
* back in [A-D]X and back into the carry chain.
*/
module finite_parallel_adder #(
parameter M = 4,
parameter N_INPUT = 2
) (
input [M*N_INPUT-1:0] in,
output [M-1:0] out
);
genvar i, j;
for (i = 0; i < M; i = i + 1) begin : add
wire [N_INPUT-1:0] z;
for (j = 0; j < N_INPUT; j = j + 1) begin : arrange
assign z[j] = in[j*M+i];
end
assign out[i] = ^z;
end
endmodule
module finite_serial_adder #(
parameter M = 4
) (
input clk,
input start,
input ce,
input [M-1:0] parallel_in,
input serial_in,
output reg [M-1:0] parallel_out = 0,
output serial_out
);
localparam TCQ = 1;
always @(posedge clk)
if (start)
parallel_out <= #TCQ {parallel_in[0+:M-1], parallel_in[M-1]};
else if (ce)
parallel_out <= #TCQ {parallel_out[0+:M-1], parallel_out[M-1] ^ serial_in};
assign serial_out = parallel_out[0];
endmodule
module lfsr_counter #(
parameter M = 4
) (
input clk,
input reset,
input ce,
output reg [M-1:0] count = lfsr_count(M, 0)
);
`include "bch.vh"
localparam TCQ = 1;
always @(posedge clk)
if (reset)
count <= #TCQ lfsr_count(M, 0);
else if (ce)
count <= #TCQ `BCH_MUL1(M, count);
endmodule
/* Generate an LFSR term for a series of input bits */
module lfsr_term #(
parameter M = 15,
parameter [M-1:0] POLY = 0,
parameter BITS = 1
) (
input [BITS-1:0] in,
output [M-1:0] out
);
wire [M*BITS-1:0] in_terms;
genvar j;
for (j = 0; j < BITS; j = j + 1) begin : lfsr_build
wire [M-1:0] poly;
assign poly = j ? `BCH_MUL_POLY(M, lfsr_build[j > 0 ? j-1 : 0].poly, POLY) : POLY;
assign in_terms[j*M+:M] = in[j] ? poly : 1'b0;
end
finite_parallel_adder #(M, BITS) u_adder(
.in(in_terms),
.out(out)
);
endmodule