-
Notifications
You must be signed in to change notification settings - Fork 44
/
bch_encode.vh
52 lines (48 loc) · 1.19 KB
/
bch_encode.vh
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
/*
* BCH Encode/Decoder Modules
*
* Copyright 2014 - Russ Dill <[email protected]>
* Distributed under 2-clause BSD license as contained in COPYING file.
*/
/* Calculate least common multiple which has x^2t .. x as its roots */
function [`BCH_ECC_BITS(P)-1:0] encoder_poly;
input dummy;
integer nk;
integer i;
integer j;
integer a;
integer curr;
integer prev;
reg [(`BCH_ECC_BITS(P)+1)*`BCH_M(P)-1:0] poly;
reg [`BCH_N(P)-1:0] roots;
begin
/* Calculate the roots for this finite field */
roots = 0;
for (i = 0; i < `BCH_T(P); i = i + 1) begin
a = 2 * i + 1;
for (j = 0; j < `BCH_M(P); j = j + 1) begin
roots[a] = 1;
a = (2 * a) % `BCH_N(P);
end
end
nk = 0;
poly = 1;
a = lpow(`BCH_M(P), 0);
for (i = 0; i < `BCH_N(P); i = i + 1) begin
if (roots[i]) begin
prev = 0;
poly[(nk+1)*`BCH_M(P)+:`BCH_M(P)] = 1;
for (j = 0; j <= nk; j = j + 1) begin
curr = poly[j*`BCH_M(P)+:`BCH_M(P)];
poly[j*`BCH_M(P)+:`BCH_M(P)] = finite_mult(`BCH_M(P), curr, a) ^ prev;
prev = curr;
end
nk = nk + 1;
end
a = `BCH_MUL1(`BCH_M(P), a);
end
encoder_poly = 0;
for (i = 0; i < nk; i = i + 1)
encoder_poly[i] = poly[i*`BCH_M(P)+:`BCH_M(P)] ? 1 : 0;
end
endfunction