forked from freecores/bluespec-reedsolomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GFArith.bsv
105 lines (90 loc) · 3.62 KB
/
GFArith.bsv
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
//----------------------------------------------------------------------//
// The MIT License
//
// Copyright (c) 2008 Abhinav Agarwal, Alfred Man Cheuk Ng
// Contact: [email protected]
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------------------------------//
//**********************************************************************
// Galois field arithmetic
//----------------------------------------------------------------------
// $Id: GFArith.bsv
//
import Vector::*;
import GFTypes::*;
import RSParameters::*;
// -----------------------------------------------------------
//(* noinline *)
function Byte gf_mult(Byte left, Byte right);
Bit#(15) first = 15'b0;
Bit#(15) result = 15'b0;
// this function bring back higher degree values back to the field
function Bit#(15) getNewResult(Integer shift, Bit#(15) res);
Bit#(15) shiftPoly = zeroExtend(primitive_poly) << shift;
Bit#(15) newRes = res ^ ((res[8+shift] == 1'b1) ? shiftPoly : 0);
return newRes;
endfunction
for (Integer i = 0; i < 8; i = i + 1)
for (Integer j = 0; j < 8 ; j = j + 1)begin
if (first[i+j] == 0) // initialize result[i+j]
result[i+j] = (left[i] & right[j]);
else // accumulate
result[i+j] = result[i+j] ^ (left[i] & right[j]);
first[i+j] = 1; // only initialize each signal once
end
Vector#(7,Integer) shftAmntV = genVector;
Bit#(15) finalResult = foldr(getNewResult,result,shftAmntV);
return finalResult[7:0];
endfunction
(* noinline *)
function Byte gf_mult_inst(Byte x, Byte y);
return gf_mult(x,y);
endfunction
// -----------------------------------------------------------
function Byte gf_add(Byte left, Byte right);
return (left ^ right);
endfunction
(* noinline *)
function Byte gf_add_inst(Byte x, Byte y);
return gf_add(x,y);
endfunction
// -----------------------------------------------------------
//(* noinline *)
function Byte alpha_n(Byte n);
return times_alpha_n(1,n);
endfunction
// -----------------------------------------------------------
//(* noinline *)
function Byte times_alpha_n(Byte a, Byte n);
// Byte multVal = 1 << n;
// return gf_mult(primitive_poly,a,multVal);
Byte b=a;
for (Byte i = 0; i < n; i = i + 1)
b=times_alpha(b);
return b;
endfunction
// -----------------------------------------------------------
//(* noinline *)
function Byte times_alpha(Byte a);
// return gf_mult(primitive_poly, a, 2);
return (a<<1)^({a[7],a[7],a[7],a[7],a[7],a[7],a[7],a[7]} & primitive_poly);
endfunction