OpenCores
URL https://opencores.org/ocsvn/bluespec-reedsolomon/bluespec-reedsolomon/trunk

Subversion Repositories bluespec-reedsolomon

[/] [bluespec-reedsolomon/] [trunk/] [bsv-reedsolomon/] [preproc.cpp] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 abhiag
//----------------------------------------------------------------------//
2
// The MIT License 
3
// 
4
// Copyright (c) 2008 Abhinav Agarwal, Alfred Man Cheuk Ng
5
// Contact: abhiag@gmail.com
6
// 
7
// Permission is hereby granted, free of charge, to any person 
8
// obtaining a copy of this software and associated documentation 
9
// files (the "Software"), to deal in the Software without 
10
// restriction, including without limitation the rights to use,
11
// copy, modify, merge, publish, distribute, sublicense, and/or sell
12
// copies of the Software, and to permit persons to whom the
13
// Software is furnished to do so, subject to the following conditions:
14
// 
15
// The above copyright notice and this permission notice shall be
16
// included in all copies or substantial portions of the Software.
17
// 
18
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
// OTHER DEALINGS IN THE SOFTWARE.
26
//----------------------------------------------------------------------//
27
 
28
#include <iostream>
29
#include <fstream>
30
#include <list>
31
#include <string>
32
#include <regex.h>
33
using namespace std;
34
 
35
#define mm  8
36
#define nn  255
37
 
38
int pp [mm + 1];
39
int alpha_to [255 + 2];
40
int index_of [255 + 2];
41
 
42
// --------------------------------------------------------------------
43
void generate_gf (int* pp)
44
{
45
   int mask = 1;
46
 
47
   alpha_to [mm] = 0 ;
48
   for (int i = 0; i < mm; ++ i)
49
   {
50
      alpha_to [i] = mask;
51
      index_of [alpha_to [i]] = i;
52
      if (0 != pp [i])
53
         alpha_to[mm] ^= mask;
54
      mask <<= 1;
55
   }
56
   index_of [alpha_to [mm]] = mm;
57
   mask >>= 1;
58
   for (int i = mm + 1; i < nn; ++ i)
59
   {
60
      if (alpha_to [i - 1] >= mask)
61
         alpha_to [i] = alpha_to [mm] ^ ((alpha_to [i - 1] ^ mask) << 1);
62
      else
63
         alpha_to [i] = alpha_to [i - 1] << 1;
64
      index_of [alpha_to [i]] = i;
65
   }
66
   index_of [0] = -1;
67
}
68
 
69
 
70
// --------------------------------------------------------------------
71
unsigned char gfinv_lut (unsigned char a)
72
{
73
   unsigned char result = (nn - index_of [a]) % nn;
74
   return alpha_to[result];
75
}
76
 
77
 
78
// --------------------------------------------------------------------
79
void print (string& rLine)
80
{
81
   string sLine (rLine);
82
   string::size_type iLength = sLine.length ();
83
   for (string::size_type i = 0; i < iLength; ++ i)
84
      if ('\t' == sLine.at (i))
85
         sLine [i] = ' ';
86
 
87
   string::size_type iStart = sLine.find ("  ");
88
   while (string::npos != iStart)
89
   {
90
      string::size_type iEnd = iStart + 1;
91
      while ((iEnd < iLength) && (' ' == sLine.at (iEnd)))
92
         ++ iEnd;
93
 
94
      sLine.replace (iStart, iEnd - iStart, " ");
95
      iLength = sLine.length ();
96
      iStart = sLine.find ("  ");
97
   }
98
   if (' ' == sLine.at (0))
99
      sLine = sLine.substr (1);
100
   cout << sLine << endl;
101
}
102
 
103
// --------------------------------------------------------------------
104
int main (int argc, char* argv [])
105
{
106
   ifstream file;
107
   file.open (argv [1]);
108
   if (false == file.is_open ())
109
   {
110
      cerr << "Failed to open file '" << argv [1] << "'. Quitting." << endl;
111
      return 1;
112
   }
113
   cout << endl;
114
 
115
   string sPolynomial;
116
 
117
   list <string>   lstPotentialPolyDefs;
118
   //   while (false == file.eof ())
119
   //{
120
      char zLine [1000];
121
      file.getline (zLine, 1000);
122
      string sLine (zLine);
123
      if (0 != strstr (zLine, "Polynomial "))
124
      {
125
         // we've found a line that may have the primitive  
126
         // polynomial defined in it. Now we need to read   
127
         // till the end of the line (till a ;) and store   
128
         // all that for later...                           
129
         while (0 != strchr (zLine, ';'))
130
         {
131
            file.getline (zLine, 1000);
132
            sLine += zLine;
133
         }
134
         lstPotentialPolyDefs.push_back (sLine);
135
      }
136
//       else if (0 != strstr (zLine, "IReedSolomon "))
137
//       {
138
//          // we've found the line where the ReedSolomon      
139
//          // decoder is instantiated. The primitive          
140
//          // polynomial will be used here. We can use this   
141
//          // to extract it...                                
142
//          string sLine (zLine);
143
//       while (0 == strchr (zLine, ';'))
144
//          {
145
//             file.getline (zLine, 1000);
146
//             sLine += zLine;
147
//          }
148
//          cout << "Line with ReedSolomon decoder instantiation." << endl;
149
//          cout << endl << "\t";
150
//          print (sLine);
151
//          cout << endl;
152
//          // check that we have the correct line...          
153
//          if ((string::npos == sLine.find ("<-")) ||
154
//              (string::npos == sLine.find ("mkReedSolomon")))
155
//          {
156
//             cout << "Got the wrong line! Can't continue." << endl;
157
//             return 1;
158
//          }
159
 
160
//          // extract the polynomial used in the              
161
//          // mkReedSolomon line...                           
162
//          string::size_type iStart = sLine.find ("(");
163
//          string::size_type iEnd = sLine.rfind (")");
164
//          string sPolynomialVariable = sLine.substr (iStart + 1, iEnd - iStart - 1);
165
 
166
//          // check if the polynomial has been defined        
167
//          // in-place...                                     
168
//          regex_t regexp;
169
//          string sPattern = "[0-9]'[bdohBDOH][0-9]*";
170
//          int ret = regcomp (&regexp, sPattern.c_str (), REG_EXTENDED);
171
//          if (0 != ret)
172
//          {
173
//             char zError [1000];
174
//             regerror (ret, &regexp, zError, 1000);
175
//             cerr << "Regular expression " << sPattern 
176
//                  << " failed to compile. Error : " << zError << endl;
177
//             return 1;
178
//          }
179
//          if (0 == regexec (&regexp, sPolynomialVariable.c_str (), 0, NULL, 0))
180
//          {
181
//             // we have an in-line definition of the poly... 
182
//             sPolynomial = sPolynomialVariable;
183
//             cout << "Primitive polynomial found inline." << endl;
184
//             cout << "polynomial : " << sPolynomial << endl;
185
//             break;
186
//          }
187
//          else
188
//          {
189
            // we need to search through the lines we       
190
            // previously saved to find the location where  
191
            // the polynomial was defined...                
192
            cout << endl;
193
            cout << "ReedSolomon instantiation uses a polynomial declared elsewhere."
194
                 << endl << "Searching for the declaration." << endl << endl;
195
 
196
            bool bPolynomialFound = false;
197
            list <string>::iterator ite;
198
            for (ite = lstPotentialPolyDefs.begin ();
199
                ite != lstPotentialPolyDefs.end ();
200
                ++ ite)
201
            {
202
               string& rLine = *ite;
203
               cout << "\t";
204
               print (rLine);
205
               cout << endl;
206
//                if (string::npos != sLine.find ("primitive_polynomial"))
207
//                {
208
                  string::size_type iStart = rLine.find ("=");
209
                  string::size_type iEnd = rLine.rfind (";");
210
                  ++ iStart;
211
                  while (rLine.at (iStart) == ' ')
212
                     ++ iStart;
213
 
214
                  sPolynomial = rLine.substr (iStart, iEnd - iStart);
215
                  cout << endl;
216
                  cout << "Primitive polynomial found as a separate declaration." << endl;
217
                  cout << "polynomial : " << sPolynomial << endl;
218
                  bPolynomialFound = true;
219
                  break;
220
                  //               }
221
            }
222
            if (false == bPolynomialFound)
223
            {
224
               cerr << "Failed to find polynomial as a separate declaration, " << endl
225
                    << "and polynomial is not present inline. Cannot continue." << endl;
226
               return 1;
227
            }
228
            //         }
229
            //      }
230
            //}
231
 
232
 
233
   // process extracted polynomial...           
234
 
235
   if (string::npos == sPolynomial.find ("b"))
236
   {
237
      cerr << "Currently only polynomials declared in the binary format are supported."
238
           << "Quitting. " << endl;
239
      return 1;
240
   }
241
   int iLength = sPolynomial.length ();
242
   int j = 0;
243
   for (int i = iLength - 1; i > 2; -- i)
244
      pp [j++] = ('1' == sPolynomial.at (i))? 1 : 0;
245
   // The MSB of the primitive polynomial is always 1.
246
   // This bit is not included in the polynomial      
247
   // declared in mkReedSolomon.                      
248
   pp [j] = 1;
249
 
250
   generate_gf (pp);
251
 
252
 
253
   // generate the BlueSpec file with gf_inv... 
254
   cout << "Generating BlueSpec file with Galois field inversion code..." << endl;
255
   ofstream ofsGenerated (argv [2]);
256
   if (false == ofsGenerated.is_open ())
257
   {
258
      cerr << "Failed to open file '" << argv [2] << "' for writing. Quitting." << endl;
259
      return 1;
260
   }
261
 
262
//   ofsGenerated << "import GFTypes::*;" << endl;
263
//   ofsGenerated << endl << endl;
264
   ofsGenerated << "// ---------------------------------------------------------" << endl;
265
   ofsGenerated << "function Byte gf_inv (Byte a);" << endl;
266
   ofsGenerated << endl;
267
   ofsGenerated << "   case (a) matches" << endl;
268
 
269
   for (int i = 0; i < 256; ++ i)
270
   {
271
      unsigned char inv = gfinv_lut ((unsigned char) i);
272
      ofsGenerated << "        " << i << " : return " << (int) inv << ";" << endl;
273
   }
274
 
275
   ofsGenerated << "   endcase" << endl;
276
   ofsGenerated << "endfunction" << endl;
277
   ofsGenerated << endl;
278
   cout << "Done." << endl << endl;
279
}
280
 
281
 
282
 
283
 
284
 
285
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.