📄 reedarith.bsv
字号:
//----------------------------------------------------------------------//// The MIT License // // Copyright (c) 2007 Alfred Man Cheuk Ng, mcn02@mit.edu // // 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: Arith.bsv// import ReedTypes::*; import Vector::*;// -----------------------------------------------------------// The primitive polynomial defines the Galois field in which // Reed-Solomon decoder operates, and all the following // arithmetic operations are defined under. Changing this // value cause the whole Reed-Solomon decoder to operate // under the new primitive polynomial.// primitive_polynomial[i] = Coefficient of x**i for i = 0:7// -----------------------------------------------------------Byte n_param = 8'd255;Byte t_param = 8'd16;// -----------------------------------------------------------//(* noinline *) function Byte gf_mult (Polynomial primitive_polynomial, 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 Byte getAddSymbol(Bool ctrl, Integer shift); Byte shiftPoly = primitive_polynomial << shift; Byte addSymbol = ctrl ? shiftPoly : 0; return addSymbol; 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,Bool) ctrlVec = unpack(result[14:8]); Byte finalResult = fold( \^ ,cons(result[7:0],zipWith(getAddSymbol,ctrlVec,genVector))); return finalResult;// result [0] = result [0] ^ (left [0] & right [0]);// result [1] = result [1] ^ (left [1] & right [0]) ^ (left [0] & right [1]);// result [2] = result [2] ^ (left [2] & right [0]) ^ (left [1] & right [1]) ^ (left [0] & right [2]);// result [3] = result [3] ^ (left [3] & right [0]) ^ (left [2] & right [1]) ^ (left [1] & right [2]) ^ (left [0] & right [3]);// result [4] = result [4] ^ (left [4] & right [0]) ^ (left [3] & right [1]) ^ (left [2] & right [2]) ^ (left [1] & right [3]) ^ (left [0] & right [4]);// result [5] = result [5] ^ (left [5] & right [0]) ^ (left [4] & right [1]) ^ (left [3] & right [2]) ^ (left [2] & right [3]) ^ (left [1] & right [4]) ^ (left [0] & right [5]);// result [6] = result [6] ^ (left [6] & right [0]) ^ (left [5] & right [1]) ^ (left [4] & right [2]) ^ (left [3] & right [3]) ^ (left [2] & right [4]) ^ (left [1] & right [5]) ^ (left [0] & right [6]);// result [7] = result [7] ^ (left [7] & right [0]) ^ (left [6] & right [1]) ^ (left [5] & right [2]) ^ (left [4] & right [3]) ^ (left [3] & right [4]) ^ (left [2] & right [5]) ^ (left [1] & right [6]) ^ (left [0] & right [7]);// result [8] = result [8] ^ (left [7] & right [1]) ^ (left [6] & right [2]) ^ (left [5] & right [3]) ^ (left [4] & right [4]) ^ (left [3] & right [5]) ^ (left [2] & right [6]) ^ (left [1] & right [7]);// result [9] = result [9] ^ (left [7] & right [2]) ^ (left [6] & right [3]) ^ (left [5] & right [4]) ^ (left [4] & right [5]) ^ (left [3] & right [6]) ^ (left [2] & right [7]);// result [10] = result [10] ^ (left [7] & right [3]) ^ (left [6] & right [4]) ^ (left [5] & right [5]) ^ (left [4] & right [6]) ^ (left [3] & right [7]);// result [11] = result [11] ^ (left [7] & right [4]) ^ (left [6] & right [5]) ^ (left [5] & right [6]) ^ (left [4] & right [7]);// result [12] = result [12] ^ (left [7] & right [5]) ^ (left [6] & right [6]) ^ (left [5] & right [7]);// result [13] = result [13] ^ (left [7] & right [6]) ^ (left [6] & right [7]);// result [14] = result [14] ^ (left [7] & right [7]);// if (result [14] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (6));// if (result [13] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (5));// if (result [12] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (4));// if (result [11] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (3));// if (result [10] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (2));// if (result [9] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (1));// if (result [8] == 1'b1)// result = result ^ ((zeroExtend (primitive_polynomial)) << (0));// return (result [7:0]);endfunction// -----------------------------------------------------------function Byte gf_add (Byte left, Byte right); return (left ^ right);endfunction// -----------------------------------------------------------//(* noinline *) function Byte alpha_n (Polynomial primitive_polynomial, Byte n); return times_alpha_n(primitive_polynomial, 1,n);endfunction// -----------------------------------------------------------//(* noinline *) function Byte times_alpha_n (Polynomial primitive_polynomial, Byte a, Byte n);// Byte b=a;// for (Byte i = 0; i < n; i = i + 1)// b=times_alpha(primitive_polynomial, b);// return b; Byte multVal = 1 << n; return gf_mult(primitive_polynomial,a,multVal);endfunction// -----------------------------------------------------------//(* noinline *) function Byte times_alpha (Polynomial primitive_polynomial, Byte a); return gf_mult(primitive_polynomial, a, 2);// return (a<<1)^({a[7],a[7],a[7],a[7],a[7],a[7],a[7],a[7]} & primitive_polynomial);endfunction// // -----------------------------------------------------------// function Byte gf_inv (Byte a);// case (a) matches// 0 : return 2;// 1 : return 1;// 2 : return 142;// 3 : return 244;// 4 : return 71;// 5 : return 167;// 6 : return 122;// 7 : return 186;// 8 : return 173;// 9 : return 157;// 10 : return 221;// 11 : return 152;// 12 : return 61;// 13 : return 170;// 14 : return 93;// 15 : return 150;// 16 : return 216;// 17 : return 114;// 18 : return 192;// 19 : return 88;// 20 : return 224;// 21 : return 62;// 22 : return 76;// 23 : return 102;// 24 : return 144;// 25 : return 222;// 26 : return 85;// 27 : return 128;// 28 : return 160;// 29 : return 131;// 30 : return 75;// 31 : return 42;// 32 : return 108;// 33 : return 237;// 34 : return 57;// 35 : return 81;// 36 : return 96;// 37 : return 86;// 38 : return 44;// 39 : return 138;// 40 : return 112;// 41 : return 208;// 42 : return 31;// 43 : return 74;// 44 : return 38;// 45 : return 139;// 46 : return 51;// 47 : return 110;// 48 : return 72;// 49 : return 137;// 50 : return 111;// 51 : return 46;// 52 : return 164;// 53 : return 195;// 54 : return 64;// 55 : return 94;// 56 : return 80;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -