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

Subversion Repositories reedsolomon

[/] [reedsolomon/] [trunk/] [bluespec-source/] [preproc.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 abhiag
//----------------------------------------------------------------------//
2
// The MIT License 
3
// 
4
// Copyright (c) 2010 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 <cstring>
32
#include <cstdlib>
33
#include <regex.h>
34
using namespace std;
35
 
36
#define mm  8
37
#define nn  255
38
 
39
int pp [mm + 1];
40
int alpha_to [255 + 2];
41
int index_of [255 + 2];
42
 
43
// --------------------------------------------------------------------
44
void generate_gf (int* pp)
45
{
46
   int mask = 1;
47
 
48
   alpha_to [mm] = 0 ;
49
   for (int i = 0; i < mm; ++ i)
50
   {
51
      alpha_to [i] = mask;
52
      index_of [alpha_to [i]] = i;
53
      if (0 != pp [i])
54
         alpha_to[mm] ^= mask;
55
      mask <<= 1;
56
   }
57
   index_of [alpha_to [mm]] = mm;
58
   mask >>= 1;
59
   for (int i = mm + 1; i < nn; ++ i)
60
   {
61
      if (alpha_to [i - 1] >= mask)
62
         alpha_to [i] = alpha_to [mm] ^ ((alpha_to [i - 1] ^ mask) << 1);
63
      else
64
         alpha_to [i] = alpha_to [i - 1] << 1;
65
      index_of [alpha_to [i]] = i;
66
   }
67
   index_of [0] = -1;
68
}
69
 
70
 
71
// --------------------------------------------------------------------
72
unsigned char gfinv_lut (unsigned char a)
73
{
74
   unsigned char result = (nn - index_of [a]) % nn;
75
   return alpha_to[result];
76
}
77
 
78
 
79
// --------------------------------------------------------------------
80
void print (string& rLine)
81
{
82
   string sLine (rLine);
83
   string::size_type iLength = sLine.length ();
84
   for (string::size_type i = 0; i < iLength; ++ i)
85
      if ('\t' == sLine.at (i))
86
         sLine [i] = ' ';
87
 
88
   string::size_type iStart = sLine.find ("  ");
89
   while (string::npos != iStart)
90
   {
91
      string::size_type iEnd = iStart + 1;
92
      while ((iEnd < iLength) && (' ' == sLine.at (iEnd)))
93
         ++ iEnd;
94
 
95
      sLine.replace (iStart, iEnd - iStart, " ");
96
      iLength = sLine.length ();
97
      iStart = sLine.find ("  ");
98
   }
99
   if (' ' == sLine.at (0))
100
      sLine = sLine.substr (1);
101
   cout << sLine << endl;
102
}
103
 
104
// --------------------------------------------------------------------
105
int main (int argc, char* argv [])
106
{
107
   ifstream file;
108
   file.open (argv [1]);
109
   if (false == file.is_open ())
110
   {
111
      cerr << "Failed to open file '" << argv [1] << "'. Quitting." << endl;
112
      return 1;
113
   }
114
   cout << endl;
115
 
116
   string sPolynomial;
117
 
118
   list <string>   lstPotentialPolyDefs;
119
   //   while (false == file.eof ())
120
   //{
121
      char zLine [1000];
122
      file.getline (zLine, 1000);
123
      string sLine (zLine);
124
      if (0 != strstr (zLine, "Polynomial "))
125
      {
126
         // we've found a line that may have the primitive  
127
         // polynomial defined in it. Now we need to read   
128
         // till the end of the line (till a ;) and store   
129
         // all that for later...                           
130
         while (0 != strchr (zLine, ';'))
131
         {
132
            file.getline (zLine, 1000);
133
            sLine += zLine;
134
         }
135
         lstPotentialPolyDefs.push_back (sLine);
136
      }
137
//       else if (0 != strstr (zLine, "IReedSolomon "))
138
//       {
139
//          // we've found the line where the ReedSolomon      
140
//          // decoder is instantiated. The primitive          
141
//          // polynomial will be used here. We can use this   
142
//          // to extract it...                                
143
//          string sLine (zLine);
144
//       while (0 == strchr (zLine, ';'))
145
//          {
146
//             file.getline (zLine, 1000);
147
//             sLine += zLine;
148
//          }
149
//          cout << "Line with ReedSolomon decoder instantiation." << endl;
150
//          cout << endl << "\t";
151
//          print (sLine);
152
//          cout << endl;
153
//          // check that we have the correct line...          
154
//          if ((string::npos == sLine.find ("<-")) ||
155
//              (string::npos == sLine.find ("mkReedSolomon")))
156
//          {
157
//             cout << "Got the wrong line! Can't continue." << endl;
158
//             return 1;
159
//          }
160
 
161
//          // extract the polynomial used in the              
162
//          // mkReedSolomon line...                           
163
//          string::size_type iStart = sLine.find ("(");
164
//          string::size_type iEnd = sLine.rfind (")");
165
//          string sPolynomialVariable = sLine.substr (iStart + 1, iEnd - iStart - 1);
166
 
167
//          // check if the polynomial has been defined        
168
//          // in-place...                                     
169
//          regex_t regexp;
170
//          string sPattern = "[0-9]'[bdohBDOH][0-9]*";
171
//          int ret = regcomp (&regexp, sPattern.c_str (), REG_EXTENDED);
172
//          if (0 != ret)
173
//          {
174
//             char zError [1000];
175
//             regerror (ret, &regexp, zError, 1000);
176
//             cerr << "Regular expression " << sPattern 
177
//                  << " failed to compile. Error : " << zError << endl;
178
//             return 1;
179
//          }
180
//          if (0 == regexec (&regexp, sPolynomialVariable.c_str (), 0, NULL, 0))
181
//          {
182
//             // we have an in-line definition of the poly... 
183
//             sPolynomial = sPolynomialVariable;
184
//             cout << "Primitive polynomial found inline." << endl;
185
//             cout << "polynomial : " << sPolynomial << endl;
186
//             break;
187
//          }
188
//          else
189
//          {
190
            // we need to search through the lines we       
191
            // previously saved to find the location where  
192
            // the polynomial was defined...                
193
            cout << endl;
194
            cout << "ReedSolomon instantiation uses a polynomial declared elsewhere."
195
                 << endl << "Searching for the declaration." << endl << endl;
196
 
197
            bool bPolynomialFound = false;
198
            list <string>::iterator ite;
199
            for (ite = lstPotentialPolyDefs.begin ();
200
                ite != lstPotentialPolyDefs.end ();
201
                ++ ite)
202
            {
203
               string& rLine = *ite;
204
               cout << "\t";
205
               print (rLine);
206
               cout << endl;
207
//                if (string::npos != sLine.find ("primitive_polynomial"))
208
//                {
209
                  string::size_type iStart = rLine.find ("=");
210
                  string::size_type iEnd = rLine.rfind (";");
211
                  ++ iStart;
212
                  while (rLine.at (iStart) == ' ')
213
                     ++ iStart;
214
 
215
                  sPolynomial = rLine.substr (iStart, iEnd - iStart);
216
                  cout << endl;
217
                  cout << "Primitive polynomial found as a separate declaration." << endl;
218
                  cout << "polynomial : " << sPolynomial << endl;
219
                  bPolynomialFound = true;
220
                  break;
221
                  //               }
222
            }
223
            if (false == bPolynomialFound)
224
            {
225
               cerr << "Failed to find polynomial as a separate declaration, " << endl
226
                    << "and polynomial is not present inline. Cannot continue." << endl;
227
               return 1;
228
            }
229
            //         }
230
            //      }
231
            //}
232
 
233
 
234
   // process extracted polynomial...           
235
 
236
   if (string::npos == sPolynomial.find ("b"))
237
   {
238
      cerr << "Currently only polynomials declared in the binary format are supported."
239
           << "Quitting. " << endl;
240
      return 1;
241
   }
242
   int iLength = sPolynomial.length ();
243
   int j = 0;
244
   for (int i = iLength - 1; i > 2; -- i)
245
      pp [j++] = ('1' == sPolynomial.at (i))? 1 : 0;
246
   // The MSB of the primitive polynomial is always 1.
247
   // This bit is not included in the polynomial      
248
   // declared in mkReedSolomon.                      
249
   pp [j] = 1;
250
 
251
   generate_gf (pp);
252
 
253
 
254
   // generate the BlueSpec file with gf_inv... 
255
   cout << "Generating BlueSpec file with Galois field inversion code..." << endl;
256
   ofstream ofsGenerated (argv [2]);
257
   if (false == ofsGenerated.is_open ())
258
   {
259
      cerr << "Failed to open file '" << argv [2] << "' for writing. Quitting." << endl;
260
      return 1;
261
   }
262
 
263
//   ofsGenerated << "import GFTypes::*;" << endl;
264
//   ofsGenerated << endl << endl;
265
   ofsGenerated << "// ---------------------------------------------------------" << endl;
266
   ofsGenerated << "function Byte gf_inv (Byte a);" << endl;
267
   ofsGenerated << endl;
268
   ofsGenerated << "   case (a) matches" << endl;
269
 
270
   for (int i = 0; i < 256; ++ i)
271
   {
272
      unsigned char inv = gfinv_lut ((unsigned char) i);
273
      ofsGenerated << "        " << i << " : return " << (int) inv << ";" << endl;
274
   }
275
 
276
   ofsGenerated << "   endcase" << endl;
277
   ofsGenerated << "endfunction" << endl;
278
   ofsGenerated << endl;
279
   cout << "Done." << endl << endl;
280
}
281
 
282
 
283
 
284
 
285
 
286
 

powered by: WebSVN 2.1.0

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