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

Subversion Repositories reed_solomon_codec_generator

[/] [reed_solomon_codec_generator/] [trunk/] [source/] [RsDecodeSyndrome.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 issei
//===================================================================
2
// Module Name : RsDecodeSyndrome
3
// File Name   : RsDecodeSyndrome.cpp
4
// Function    : RTL Decoder syndrome Module generation
5
// 
6
// Revision History:
7
// Date          By           Version    Change Description
8
//===================================================================
9
// 2009/02/03  Gael Sapience     1.0       Original
10
//
11
//===================================================================
12
// (C) COPYRIGHT 2009 SYSTEM LSI CO., Ltd.
13
//
14
#include <stdio.h>
15
#include <stdlib.h>
16
#include <iostream>
17
#include<windows.h>
18
#include<fstream>
19
#include <string.h>
20
using namespace std;
21
 
22
FILE  *OutFileDecodeSyndrome;
23
 
24
void RsGfMultiplier( int*, int*,int*, int, int);
25
void RsDecodeSyndrome(int DataSize, int TotalSize, int PrimPoly, int bitSymbol, int *MrefTab, int *PrefTab, int *coeffTab, int pathFlag, int lengthPath, char *rootFolderPath) {
26
 
27
 
28
   //---------------------------------------------------------------
29
   // c++ variables
30
   //---------------------------------------------------------------
31
   int syndromeLength;
32
   int ii,zz;
33
   int Pidx;
34
   int init;
35
   int idx1;
36
   int idx2;
37
   int tempNum;
38
   int countSize;
39
   int kk;
40
   int *bbTab;
41
   int *ppTab;
42
   int *ttTab;
43
   int *bidon;
44
   int mmTabSize = (bitSymbol*2) -1;
45
   char *strRsDecodeSyndrome;
46
 
47
 
48
   bbTab = new int[bitSymbol];
49
   ppTab = new int[bitSymbol];
50
   ttTab = new int[bitSymbol];
51
   bidon = new int[bitSymbol];
52
 
53
 
54
   syndromeLength = TotalSize - DataSize;
55
 
56
 
57
   //---------------------------------------------------------------
58
   // open file
59
   //---------------------------------------------------------------
60
   strRsDecodeSyndrome = (char *)calloc(lengthPath + 24,  sizeof(char));
61
   if (pathFlag == 0) {
62
        strRsDecodeSyndrome[0] = '.';
63
   }else{
64
      for(ii=0; ii<lengthPath; ii++){
65
         strRsDecodeSyndrome[ii] = rootFolderPath[ii];
66
      }
67
   }
68
   strcat(strRsDecodeSyndrome, "/rtl/RsDecodeSyndrome.v");
69
 
70
   OutFileDecodeSyndrome = fopen(strRsDecodeSyndrome,"w");
71
 
72
 
73
   //---------------------------------------------------------------
74
   // header file
75
   //---------------------------------------------------------------
76
   fprintf(OutFileDecodeSyndrome, "//===================================================================\n");
77
   fprintf(OutFileDecodeSyndrome, "// Module Name : RsDecodeSyndrome\n");
78
   fprintf(OutFileDecodeSyndrome, "// File Name   : RsDecodeSyndrome.v\n");
79
   fprintf(OutFileDecodeSyndrome, "// Function    : Rs Decoder syndrome calculation\n");
80
   fprintf(OutFileDecodeSyndrome, "// \n");
81
   fprintf(OutFileDecodeSyndrome, "// Revision History:\n");
82
   fprintf(OutFileDecodeSyndrome, "// Date          By           Version    Change Description\n");
83
   fprintf(OutFileDecodeSyndrome, "//===================================================================\n");
84
   fprintf(OutFileDecodeSyndrome, "// 2009/02/03  Gael Sapience     1.0       Original\n");
85
   fprintf(OutFileDecodeSyndrome, "//\n");
86
   fprintf(OutFileDecodeSyndrome, "//===================================================================\n");
87
   fprintf(OutFileDecodeSyndrome, "// (C) COPYRIGHT 2009 SYSTEM LSI CO., Ltd.\n");
88
   fprintf(OutFileDecodeSyndrome, "//\n\n\n");
89
 
90
 
91
 
92
   //---------------------------------------------------------------
93
   // Ports Declaration
94
   //---------------------------------------------------------------
95
   fprintf(OutFileDecodeSyndrome, "module RsDecodeSyndrome(\n");
96
   fprintf(OutFileDecodeSyndrome, "   CLK,           // system clock\n");
97
   fprintf(OutFileDecodeSyndrome, "   RESET,         // system reset\n");
98
   fprintf(OutFileDecodeSyndrome, "   enable,        // enable signal\n");
99
   fprintf(OutFileDecodeSyndrome, "   sync,          // sync signal\n");
100
   fprintf(OutFileDecodeSyndrome, "   dataIn,        // data input\n");
101
 
102
   for (ii=0; ii < syndromeLength; ii++) {
103
      if (ii < 10) {
104
         fprintf(OutFileDecodeSyndrome, "   syndrome_%d,    // syndrome polynom %d\n", ii, ii);
105
      }else{
106
        fprintf(OutFileDecodeSyndrome,  "   syndrome_%d,   // syndrome polynom %d\n",ii,ii);
107
      }
108
   }
109
 
110
   fprintf(OutFileDecodeSyndrome, "   done           // done signal\n");
111
   fprintf(OutFileDecodeSyndrome, ");\n\n\n");
112
   fprintf(OutFileDecodeSyndrome, "   input          CLK;           // system clock\n");
113
   fprintf(OutFileDecodeSyndrome, "   input          RESET;         // system reset\n");
114
   fprintf(OutFileDecodeSyndrome, "   input          enable;        // enable signal\n");
115
   fprintf(OutFileDecodeSyndrome, "   input          sync;          // sync signal\n");
116
   fprintf(OutFileDecodeSyndrome, "   input  [%d:0]   dataIn;        // data input\n", (bitSymbol -1));
117
 
118
   for (ii=0; ii<syndromeLength; ii++) {
119
      if (ii < 10) {
120
         fprintf(OutFileDecodeSyndrome, "   output [%d:0]   syndrome_%d;    // syndrome polynom %d\n",(bitSymbol -1),ii,ii);
121
      } else {
122
         fprintf(OutFileDecodeSyndrome, "   output [%d:0]   syndrome_%d;   // syndrome polynom %d\n",(bitSymbol -1),ii,ii);
123
      }
124
   }
125
   fprintf(OutFileDecodeSyndrome, "   output         done;          // done signal\n\n\n");
126
 
127
 
128
 
129
   //------------------------------------------------------------------------
130
   // + COUNT
131
   //- Counter
132
   //------------------------------------------------------------------------
133
   countSize = 0;
134
 
135
   if (TotalSize > 2047) {
136
      countSize = 12;
137
   } else{
138
      if (TotalSize > 1023) {
139
         countSize = 11;
140
      } else{
141
         if (TotalSize > 511) {
142
            countSize = 10;
143
         }else{
144
            if (TotalSize > 255) {
145
               countSize = 9;
146
            }else{
147
               if (TotalSize > 127) {
148
                  countSize = 8;
149
               }else{
150
                  if (TotalSize > 63) {
151
                     countSize = 7;
152
                  }else{
153
                     if (TotalSize > 31) {
154
                        countSize = 6;
155
                     }else{
156
                        if (TotalSize > 15) {
157
                           countSize = 5;
158
                        }else{
159
                           if (TotalSize > 7) {
160
                              countSize = 4;
161
                           }else{
162
                              if (TotalSize > 3) {
163
                                 countSize = 3;
164
                              }else{
165
                                 countSize = 2;
166
                              }
167
                           }
168
                        }
169
                     }
170
                  }
171
               }
172
            }
173
         }
174
      }
175
   }
176
 
177
 
178
 
179
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
180
   fprintf(OutFileDecodeSyndrome, "   // + count\n");
181
   fprintf(OutFileDecodeSyndrome, "   //- Counter\n");
182
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
183
//   fprintf(OutFileDecodeSyndrome, "   reg    [%d:0]   count;\n", bitSymbol);
184
   fprintf(OutFileDecodeSyndrome, "   reg    [%d:0]   count;\n", (countSize-1));
185
   fprintf(OutFileDecodeSyndrome, "   always @(posedge CLK or negedge RESET) begin\n");
186
   fprintf(OutFileDecodeSyndrome, "      if (~RESET) begin\n");
187
//   fprintf(OutFileDecodeSyndrome, "         count [%d:0] <= %d'd0;\n", bitSymbol, (bitSymbol+1));
188
   fprintf(OutFileDecodeSyndrome, "         count [%d:0] <= %d'd0;\n", (countSize-1), countSize);
189
   fprintf(OutFileDecodeSyndrome, "      end\n");
190
   fprintf(OutFileDecodeSyndrome, "      else if (enable == 1'b1) begin\n");
191
   fprintf(OutFileDecodeSyndrome, "         if (sync == 1'b1) begin\n");
192
   fprintf(OutFileDecodeSyndrome, "            count[%d:0] <= %d'd1;\n", (countSize-1), countSize);
193
   fprintf(OutFileDecodeSyndrome, "         end\n");
194
//   fprintf(OutFileDecodeSyndrome, "         else if ( (count[%d:0] ==%d'd0) || (count[%d:0] ==%d'd%d)) begin\n", bitSymbol, (bitSymbol+1), bitSymbol, (bitSymbol+1),TotalSize);
195
   fprintf(OutFileDecodeSyndrome, "         else if ( (count[%d:0] ==%d'd0) || (count[%d:0] ==%d'd%d)) begin\n", (countSize-1), countSize, (countSize-1), countSize,TotalSize);
196
//   fprintf(OutFileDecodeSyndrome, "            count[%d:0] <= %d'd0;\n", bitSymbol, (bitSymbol+1));
197
   fprintf(OutFileDecodeSyndrome, "            count[%d:0] <= %d'd0;\n", (countSize-1), countSize);
198
   fprintf(OutFileDecodeSyndrome, "         end\n");
199
   fprintf(OutFileDecodeSyndrome, "         else begin\n");
200
//   fprintf(OutFileDecodeSyndrome, "            count[%d:0] <= count[%d:0] + %d'd1;\n", bitSymbol, bitSymbol, (bitSymbol+1));
201
   fprintf(OutFileDecodeSyndrome, "            count[%d:0] <= count[%d:0] + %d'd1;\n", (countSize-1), (countSize-1), countSize);
202
   fprintf(OutFileDecodeSyndrome, "         end\n");
203
   fprintf(OutFileDecodeSyndrome, "      end\n");
204
   fprintf(OutFileDecodeSyndrome, "   end\n");
205
   fprintf(OutFileDecodeSyndrome, "\n\n\n");
206
 
207
 
208
   //------------------------------------------------------------------------
209
   // + done
210
   //------------------------------------------------------------------------
211
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
212
   fprintf(OutFileDecodeSyndrome, "   // + done\n");
213
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
214
   fprintf(OutFileDecodeSyndrome, "   reg         done;\n");
215
   fprintf(OutFileDecodeSyndrome, "   always @(count) begin\n");
216
//   fprintf(OutFileDecodeSyndrome, "      if (count ==%d'd%d) begin\n", (bitSymbol+1),TotalSize);
217
   fprintf(OutFileDecodeSyndrome, "      if (count ==%d'd%d) begin\n", countSize,TotalSize);
218
   fprintf(OutFileDecodeSyndrome, "         done = 1'b1;\n");
219
   fprintf(OutFileDecodeSyndrome, "      end\n");
220
   fprintf(OutFileDecodeSyndrome, "      else begin\n");
221
   fprintf(OutFileDecodeSyndrome, "         done = 1'b0;\n");
222
   fprintf(OutFileDecodeSyndrome, "      end\n");
223
   fprintf(OutFileDecodeSyndrome, "   end\n");
224
   fprintf(OutFileDecodeSyndrome, "\n\n");
225
 
226
 
227
   //------------------------------------------------------------------------
228
   // + product_0,..., product_xxx
229
   //- Syndrome Generator
230
   //------------------------------------------------------------------------
231
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
232
   fprintf(OutFileDecodeSyndrome, "   // + product_0,..., product_%d\n", (syndromeLength-1));
233
   fprintf(OutFileDecodeSyndrome, "   //- Syndrome Generator\n");
234
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
235
 
236
   for (ii=0; ii<syndromeLength;ii++) {
237
      fprintf(OutFileDecodeSyndrome, "   wire [%d:0]   product_%d;\n",(bitSymbol-1),ii);
238
   }
239
   fprintf(OutFileDecodeSyndrome, "\n");
240
 
241
   for (ii=0;ii<syndromeLength;ii++) {
242
      fprintf(OutFileDecodeSyndrome, "   reg  [%d:0]    reg_%d;\n",(bitSymbol-1), ii);
243
   }
244
   fprintf(OutFileDecodeSyndrome, "\n");
245
 
246
 
247
   //------------------------------------------------------------------------
248
   // + RsDecodeMakeMultCoeff Emulation
249
   //------------------------------------------------------------------------
250
   for (zz = 0; zz <syndromeLength;zz++){
251
      if (zz == 0) {
252
         for (kk = 0; kk <bitSymbol;kk++){
253
            fprintf(OutFileDecodeSyndrome, "   assign product_%d [%d] = reg_%d[%d];\n", zz, kk,zz,kk);
254
         }
255
      }else{
256
         ttTab[0] = 1;
257
 
258
         for (kk = 1; kk <bitSymbol;kk++){
259
            ttTab[kk] = 0;
260
         }
261
 
262
         bbTab[0] = 0;
263
         bbTab[1] = 1;
264
 
265
         for (kk = 2; kk <bitSymbol;kk++){
266
           bbTab[kk] = 0;
267
         }
268
 
269
         //------------------------------------------------------------------------
270
         //------------------------------------------------------------------------
271
         for(ii=1; ii<zz+1; ii++){
272
            //------------------------------------------------------------------------
273
            // ppTab = ttTab * bbTab
274
            //------------------------------------------------------------------------
275
            RsGfMultiplier(ppTab, ttTab, bbTab, PrimPoly, bitSymbol);
276
 
277
            //------------------------------------------------------------------------
278
            // reassign ttTab
279
            //------------------------------------------------------------------------
280
            for (kk = 0; kk <bitSymbol;kk++){
281
               ttTab[kk]        = ppTab[kk];
282
            }
283
 
284
 
285
            //------------------------------------------------------------------------
286
            // write P_OUT[0]
287
            //------------------------------------------------------------------------
288
            if (ii==zz) {
289
               for (Pidx=0; Pidx<bitSymbol; Pidx++){
290
                  fprintf(OutFileDecodeSyndrome, "   assign product_%d [%d] =", zz,Pidx);
291
                  init = 0;
292
 
293
                  for (idx2=0; idx2<bitSymbol;idx2++){
294
                     bidon [idx2] = 0;
295
                  }
296
                  for (idx1=0; idx1<mmTabSize;idx1++){
297
                     tempNum = PrefTab [Pidx*mmTabSize+idx1];
298
                     if (tempNum == 1) {
299
                        //------------------------------------------------------------------------
300
                        // search
301
                        //------------------------------------------------------------------------
302
                        for (idx2=0; idx2<bitSymbol;idx2++){
303
                           tempNum = MrefTab[idx1*bitSymbol+idx2];
304
                           if ((tempNum > 0) && (ttTab[tempNum-1] == 1)) {
305
                              if  (bidon [idx2] == 0) {
306
                                 bidon [idx2] = 1;
307
                              }
308
                              else {
309
                                 bidon [idx2] = 0;
310
                              }
311
                           }
312
                        }
313
                     }
314
                  }
315
                  //------------------------------------------------------------------------
316
                  // printf
317
                  //------------------------------------------------------------------------
318
                  for (idx2=0; idx2<bitSymbol; idx2++){
319
                     if (bidon[idx2] == 1) {
320
                        if (init == 0) {
321
                           fprintf(OutFileDecodeSyndrome, " reg_%d[%d]", zz,idx2);
322
                           init = 1;
323
                        }
324
                        else {
325
                           fprintf(OutFileDecodeSyndrome, " ^ reg_%d[%d]", zz,idx2);
326
                        }
327
                     }
328
                  }
329
                  fprintf(OutFileDecodeSyndrome, ";\n");
330
               }
331
            }
332
         }
333
      }
334
   }
335
 
336
   fprintf(OutFileDecodeSyndrome, "\n\n\n");
337
 
338
 
339
 
340
   //------------------------------------------------------------------------
341
   // + REG_0,..., REG_xxx
342
   //------------------------------------------------------------------------
343
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
344
   fprintf(OutFileDecodeSyndrome, "   // + REG_0,..., REG_%d\n", syndromeLength-1);
345
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
346
   fprintf(OutFileDecodeSyndrome, "   always @(posedge CLK or negedge RESET) begin\n");
347
   fprintf(OutFileDecodeSyndrome, "      if (~RESET) begin\n");
348
 
349
   for (ii=0; ii<syndromeLength;ii++) {
350
      if (ii < 10) {
351
         fprintf(OutFileDecodeSyndrome, "         reg_%d [%d:0]  <= %d'd0;\n", ii, bitSymbol-1, bitSymbol);
352
      }else{
353
        fprintf(OutFileDecodeSyndrome, "         reg_%d [%d:0] <= %d'd0;\n", ii, bitSymbol-1, bitSymbol);
354
      }
355
   }
356
 
357
 
358
   fprintf(OutFileDecodeSyndrome, "      end\n");
359
   fprintf(OutFileDecodeSyndrome, "      else if (enable == 1'b1) begin\n");
360
   fprintf(OutFileDecodeSyndrome, "         if (sync == 1'b1) begin\n");
361
 
362
 
363
   for (ii=0; ii<syndromeLength;ii++) {
364
      if (ii < 10) {
365
         fprintf(OutFileDecodeSyndrome, "            reg_%d [%d:0]  <= dataIn[%d:0];\n",ii, bitSymbol-1 ,bitSymbol-1);
366
      }else{
367
         fprintf(OutFileDecodeSyndrome, "            reg_%d [%d:0] <= dataIn[%d:0];\n",ii, bitSymbol-1 ,bitSymbol-1);
368
      }
369
   }
370
 
371
   fprintf(OutFileDecodeSyndrome, "         end\n");
372
   fprintf(OutFileDecodeSyndrome, "         else begin\n");
373
 
374
 
375
   for (ii=0; ii<syndromeLength; ii++) {
376
      if (ii < 10) {
377
        fprintf(OutFileDecodeSyndrome, "            reg_%d [%d:0]  <= dataIn [%d:0] ^ product_%d[%d:0];\n", ii,bitSymbol-1,bitSymbol-1, ii,bitSymbol-1);
378
      }else{
379
        fprintf(OutFileDecodeSyndrome, "            reg_%d [%d:0] <= dataIn [%d:0] ^ product_%d[%d:0];\n", ii,bitSymbol-1,bitSymbol-1, ii,bitSymbol-1);
380
      }
381
   }
382
 
383
   fprintf(OutFileDecodeSyndrome, "         end\n");
384
   fprintf(OutFileDecodeSyndrome, "      end\n");
385
   fprintf(OutFileDecodeSyndrome, "   end\n");
386
   fprintf(OutFileDecodeSyndrome, "\n\n\n");
387
 
388
 
389
   //------------------------------------------------------------------------
390
   //- Output Ports
391
   //------------------------------------------------------------------------
392
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
393
   fprintf(OutFileDecodeSyndrome, "   //- Output Ports\n");
394
   fprintf(OutFileDecodeSyndrome, "   //------------------------------------------------------------------------\n");
395
 
396
   for (ii=0;ii<syndromeLength;ii++){
397
      if (ii<10) {
398
        fprintf(OutFileDecodeSyndrome, "   assign   syndrome_%d[%d:0]  = reg_%d[%d:0];\n",ii,bitSymbol-1,ii,bitSymbol-1);
399
      }else{
400
        fprintf(OutFileDecodeSyndrome, "   assign   syndrome_%d[%d:0] = reg_%d[%d:0];\n",ii,bitSymbol-1,ii,bitSymbol-1);
401
      }
402
   }
403
 
404
   fprintf(OutFileDecodeSyndrome, "\n");
405
   fprintf(OutFileDecodeSyndrome, "endmodule\n");
406
 
407
 
408
   //---------------------------------------------------------------
409
   // close file
410
   //---------------------------------------------------------------
411
   fclose(OutFileDecodeSyndrome);
412
 
413
 
414
  //---------------------------------------------------------------
415
  // Free memory
416
  //---------------------------------------------------------------
417
   delete[] bbTab;
418
   delete[] ppTab;
419
   delete[] ttTab;
420
   delete[] bidon;
421
 
422
 
423
   //---------------------------------------------------------------
424
   // automatically convert Dos mode To Unix mode
425
   //---------------------------------------------------------------
426
        char ch;
427
        char temp[MAX_PATH]="\0";
428
 
429
        //Open the file for reading in binarymode.
430
        ifstream fp_read(strRsDecodeSyndrome, ios_base::in | ios_base::binary);
431
        sprintf(temp, "%s.temp", strRsDecodeSyndrome);
432
 
433
        //Create a temporary file for writing in the binary mode. This
434
        //file will be created in the same directory as the input file.
435
        ofstream fp_write(temp, ios_base::out | ios_base::trunc | ios_base::binary);
436
 
437
        while(fp_read.eof() != true)
438
        {
439
                fp_read.get(ch);
440
                //Check for CR (carriage return)
441
                if((int)ch == 0x0D)
442
                        continue;
443
                if (!fp_read.eof())fp_write.put(ch);
444
        }
445
 
446
        fp_read.close();
447
        fp_write.close();
448
        //Delete the existing input file.
449
        remove(strRsDecodeSyndrome);
450
        //Rename the temporary file to the input file.
451
        rename(temp, strRsDecodeSyndrome);
452
        //Delete the temporary file.
453
        remove(temp);
454
 
455
 
456
   //---------------------------------------------------------------
457
   // clean string
458
   //---------------------------------------------------------------
459
   free(strRsDecodeSyndrome);
460
 
461
 
462
}

powered by: WebSVN 2.1.0

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