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

Subversion Repositories yacc

[/] [yacc/] [trunk/] [bench/] [c_src/] [reed solomon/] [rs_tak.c] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 tak.sugawa
//Reed Solomon Program
2
//This program is based on Phil Karn
3
//Rewritten for YACC CPU (has no C library) by Tak.Sugawara Apr.3.2005
4
//Consideration for embedded CPU
5
// 1) Has no C library. Ex. Not have printf/random...
6
// 2) Not have plenty of stack 
7
 
8
#define POLY 0x80000057
9
 
10
 
11
 
12
 
13
#define print_port 0x3ff0
14
#define print_char_port 0x3ff1
15
#define print_int_port 0x3ff2
16
#define print_long_port 0x3ff4
17
 
18
 
19
 
20
 
21
 
22
#define uart_port               0x03ffc //for 16KRAM
23
#define uart_wport uart_port
24
#define uart_rport uart_port
25
#define int_set_address 0x03ff8 //for 16KRAM
26
 
27
//#define PC
28
 
29
void print_uart(unsigned char* ptr)// 
30
{
31
        unsigned int uport;
32
        #define WRITE_BUSY 0x0100
33
 
34
 
35
        while (*ptr) {
36
 
37
                do {
38
                  uport=*(volatile unsigned*)   uart_port;
39
                } while (uport & WRITE_BUSY);
40
                *(volatile unsigned char*)uart_wport=*(ptr++);
41
        }
42
        //*(volatile unsigned char*)uart_wport=0x00;//Write Done
43
}
44
 
45
 
46
void putc_uart(unsigned char c)// 
47
{
48
        unsigned int uport;
49
 
50
 
51
        do {
52
                  uport=*(volatile unsigned*)   uart_port;
53
        } while (uport & WRITE_BUSY);
54
        *(volatile unsigned char*)uart_wport=c;
55
 
56
}
57
 
58
unsigned char read_uart()//Verilog Test Bench Use 
59
{
60
                unsigned uport;
61
                uport= *(volatile unsigned *)uart_rport;
62
                return uport;
63
}
64
 
65
void print(unsigned char* ptr)//Verilog Test Bench Use 
66
{
67
 
68
        while (*ptr) {
69
                #ifdef PC
70
                        putchar(*(ptr++));
71
                #else   
72
                *(volatile unsigned char*)print_port=*(ptr++);
73
                #endif
74
        }
75
        #ifndef PC
76
                *(volatile unsigned char*)print_port=0x00;//Write Done
77
        #endif
78
}
79
void print_char(unsigned char val)//Little Endian write out 16bit number 
80
{
81
        #ifdef PC
82
                putchar(val);
83
        #else   
84
                *(volatile unsigned char*)print_port=(unsigned char)val ;
85
        #endif
86
}
87
void print_uchar(unsigned char val)//Little Endian write out 16bit number 
88
{
89
        #ifdef PC
90
 
91
                printf("%x",val);
92
        #else
93
        *(volatile unsigned char*)print_char_port=(unsigned char)val ;
94
        #endif  
95
}
96
 
97
 
98
 
99
 
100
 
101
 
102
 
103
static unsigned lfsr_state=1;
104
 
105
unsigned random (void)
106
{
107
  if (lfsr_state & 0x1)
108
    {
109
      lfsr_state = (lfsr_state >> 1) ^ POLY;
110
    }
111
  else
112
    {
113
      lfsr_state = (lfsr_state >> 1);
114
    }
115
  return lfsr_state;
116
}
117
/*
118
void print(unsigned char* ptr)
119
{
120
        while(*(ptr)) putchar(*(ptr++));
121
 
122
}
123
*/
124
void print_num(unsigned long num)
125
{
126
   unsigned long digit,offset;
127
   for(offset=1000;offset;offset/=10) {
128
      digit=num/offset;
129
 
130
      print_char(digit+'0');//putchar(digit+'0');
131
      num-=digit*offset;
132
   }
133
}
134
 
135
void memcpy(unsigned char* dest,unsigned char* source,unsigned size)
136
{
137
        unsigned i;
138
        for (i=0;i< size;i++){
139
                *(dest++)=*(source++);
140
        }
141
}
142
 
143
unsigned memcmp(unsigned char* dest,unsigned char* source,unsigned size)
144
{
145
        unsigned i;
146
        for (i=0;i< size;i++){
147
                if (*(dest++)!=*(source++) ) return 1;
148
        }
149
        return 0;
150
}
151
/*
152
 * Reed-Solomon coding and decoding
153
 * Phil Karn (karn@ka9q.ampr.org) September 1996
154
 *
155
 * This file is derived from the program "new_rs_erasures.c" by Robert
156
 * Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy
157
 * (harit@spectra.eng.hawaii.edu), Aug 1995
158
 *
159
 * I've made changes to improve performance, clean up the code and make it
160
 * easier to follow. Data is now passed to the encoding and decoding functions
161
 * through arguments rather than in global arrays. The decode function returns
162
 * the number of corrected symbols, or -1 if the word is uncorrectable.
163
 *
164
 * This code supports a symbol size from 2 bits up to 16 bits,
165
 * implying a block size of 3 2-bit symbols (6 bits) up to 65535
166
 * 16-bit symbols (1,048,560 bits). The code parameters are set in rs.h.
167
 *
168
 * Note that if symbols larger than 8 bits are used, the type of each
169
 * data array element switches from unsigned char to unsigned int. The
170
 * caller must ensure that elements larger than the symbol range are
171
 * not passed to the encoder or decoder.
172
 *
173
 */
174
//#include <stdio.h>
175
#include "rs.h"
176
 
177
#if (KK >= NN)
178
#error "KK must be less than 2**MM - 1"
179
#endif
180
 
181
/* This defines the type used to store an element of the Galois Field
182
 * used by the code. Make sure this is something larger than a char if
183
 * if anything larger than GF(256) is used.
184
 *
185
 * Note: unsigned char will work up to GF(256) but int seems to run
186
 * faster on the Pentium.
187
 */
188
typedef int gf;
189
 
190
/* Primitive polynomials - see Lin & Costello, Appendix A,
191
 * and  Lee & Messerschmitt, p. 453.
192
 */
193
#if(MM == 2)/* Admittedly silly */
194
int Pp[MM+1] = { 1, 1, 1 };
195
 
196
#elif(MM == 3)
197
/* 1 + x + x^3 */
198
int Pp[MM+1] = { 1, 1, 0, 1 };
199
 
200
#elif(MM == 4)
201
/* 1 + x + x^4 */
202
int Pp[MM+1] = { 1, 1, 0, 0, 1 };
203
 
204
#elif(MM == 5)
205
/* 1 + x^2 + x^5 */
206
int Pp[MM+1] = { 1, 0, 1, 0, 0, 1 };
207
 
208
#elif(MM == 6)
209
/* 1 + x + x^6 */
210
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1 };
211
 
212
#elif(MM == 7)
213
/* 1 + x^3 + x^7 */
214
int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 1 };
215
 
216
#elif(MM == 8)
217
/* 1+x^2+x^3+x^4+x^8 */
218
int Pp[MM+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 };
219
 
220
#elif(MM == 9)
221
/* 1+x^4+x^9 */
222
int Pp[MM+1] = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
223
 
224
#elif(MM == 10)
225
/* 1+x^3+x^10 */
226
int Pp[MM+1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
227
 
228
#elif(MM == 11)
229
/* 1+x^2+x^11 */
230
int Pp[MM+1] = { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
231
 
232
#elif(MM == 12)
233
/* 1+x+x^4+x^6+x^12 */
234
int Pp[MM+1] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 };
235
 
236
#elif(MM == 13)
237
/* 1+x+x^3+x^4+x^13 */
238
int Pp[MM+1] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
239
 
240
#elif(MM == 14)
241
/* 1+x+x^6+x^10+x^14 */
242
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
243
 
244
#elif(MM == 15)
245
/* 1+x+x^15 */
246
int Pp[MM+1] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
247
 
248
#elif(MM == 16)
249
/* 1+x+x^3+x^12+x^16 */
250
int Pp[MM+1] = { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
251
 
252
#else
253
#error "MM must be in range 2-16"
254
#endif
255
 
256
/* Alpha exponent for the first root of the generator polynomial */
257
#define B0      1
258
 
259
/* index->polynomial form conversion table */
260
gf Alpha_to[NN + 1];
261
 
262
/* Polynomial->index form conversion table */
263
gf Index_of[NN + 1];
264
 
265
/* No legal value in index form represents zero, so
266
 * we need a special value for this purpose
267
 */
268
#define A0      (NN)
269
 
270
/* Generator polynomial g(x)
271
 * Degree of g(x) = 2*TT
272
 * has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
273
 */
274
gf Gg[NN - KK + 1];
275
 
276
/* Compute x % NN, where NN is 2**MM - 1,
277
 * without a slow divide
278
 */
279
static inline gf
280
modnn(int x)
281
{
282
//      print("modnn input="); print_num(x);
283
        while (x >= NN) {
284
                x -= NN;
285
                x = (x >> MM) + (x & NN);
286
        }
287
//      print("modnn output=");print_num(x);
288
//      print("\n");
289
        return x;
290
}
291
 
292
#define min(a,b)        ((a) < (b) ? (a) : (b))
293
 
294
#define CLEAR(a,n) {    int ci; for(ci=(n)-1;ci >=0;ci--)               (a)[ci] = 0;    }
295
 
296
#define COPY(a,b,n) {   int ci; for(ci=(n)-1;ci >=0;ci--)       (a)[ci] = (b)[ci];      }
297
#define COPYDOWN(a,b,n) {       int ci; for(ci=(n)-1;ci >=0;ci--) (a)[ci] = (b)[ci];    }
298
 
299
void init_rs(void)
300
{
301
        generate_gf();
302
        gen_poly();
303
}
304
 
305
/* generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
306
   lookup tables:  index->polynomial form   alpha_to[] contains j=alpha**i;
307
                   polynomial form -> index form  index_of[j=alpha**i] = i
308
   alpha=2 is the primitive element of GF(2**m)
309
   HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
310
        Let @ represent the primitive element commonly called "alpha" that
311
   is the root of the primitive polynomial p(x). Then in GF(2^m), for any
312
 
313
        @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
314
   where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
315
   of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
316
   example the polynomial representation of @^5 would be given by the binary
317
   representation of the integer "alpha_to[5]".
318
                   Similarily, index_of[] can be used as follows:
319
        As above, let @ represent the primitive element of GF(2^m) that is
320
   the root of the primitive polynomial p(x). In order to find the power
321
   of @ (alpha) that has the polynomial representation
322
        a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
323
   we consider the integer "i" whose binary representation with a(0) being LSB
324
   and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
325
   "index_of[i]". Now, @^index_of[i] is that element whose polynomial
326
    representation is (a(0),a(1),a(2),...,a(m-1)).
327
   NOTE:
328
        The element alpha_to[2^m-1] = 0 always signifying that the
329
   representation of "@^infinity" = 0 is (0,0,0,...,0).
330
        Similarily, the element index_of[0] = A0 always signifying
331
   that the power of alpha which has the polynomial representation
332
   (0,0,...,0) is "infinity".
333
 
334
*/
335
 
336
void
337
generate_gf(void)
338
{
339
        register int i, mask;
340
 
341
        mask = 1;
342
        Alpha_to[MM] = 0;
343
        for (i = 0; i < MM; i++) {
344
                Alpha_to[i] = mask;
345
                Index_of[Alpha_to[i]] = i;
346
                /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
347
                if (Pp[i] != 0)
348
                        Alpha_to[MM] ^= mask;   /* Bit-wise EXOR operation */
349
                mask <<= 1;     /* single left-shift */
350
        }
351
        Index_of[Alpha_to[MM]] = MM;
352
        /*
353
         * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
354
         * poly-repr of @^i shifted left one-bit and accounting for any @^MM
355
         * term that may occur when poly-repr of @^i is shifted.
356
         */
357
        mask >>= 1;
358
        for (i = MM + 1; i < NN; i++) {
359
                if (Alpha_to[i - 1] >= mask)
360
                        Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
361
                else
362
                        Alpha_to[i] = Alpha_to[i - 1] << 1;
363
                Index_of[Alpha_to[i]] = i;
364
        }
365
        Index_of[0] = A0;
366
        Alpha_to[NN] = 0;
367
        print("index dump\n");
368
    for (i=0;i<NN;i++){
369
                print_uchar(Index_of[i]);
370
                print(" ");
371
        }
372
        print("\n");
373
        print("Alpha_to dump\n");
374
    for (i=0;i<NN;i++){
375
                print_uchar(Alpha_to[i]);
376
                print(" ");
377
        }
378
        print("\n");
379
 
380
}
381
 
382
 
383
/*
384
 * Obtain the generator polynomial of the TT-error correcting, length
385
 * NN=(2**MM -1) Reed Solomon code from the product of (X+@**(B0+i)), i = 0,
386
 * ... ,(2*TT-1)
387
 *
388
 * Examples:
389
 *
390
 * If B0 = 1, TT = 1. deg(g(x)) = 2*TT = 2.
391
 * g(x) = (x+@) (x+@**2)
392
 *
393
 * If B0 = 0, TT = 2. deg(g(x)) = 2*TT = 4.
394
 * g(x) = (x+1) (x+@) (x+@**2) (x+@**3)
395
 */
396
void
397
gen_poly(void)
398
{
399
        register int i, j;
400
 
401
        Gg[0] = Alpha_to[B0];
402
        Gg[1] = 1;              /* g(x) = (X+@**B0) initially */
403
        for (i = 2; i <= NN - KK; i++) {
404
                Gg[i] = 1;
405
                /*
406
                 * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
407
                 * (@**(B0+i-1) + x)
408
                 */
409
                for (j = i - 1; j > 0; j--){
410
                        if (Gg[j] != 0)
411
                                Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)];
412
                        else
413
                                Gg[j] = Gg[j - 1];
414
 
415
                //      print("Gg[");print_num(j);print("]=");print_num(Gg[j]);print("\n");
416
                //      print("Gg[");print_num(j-1);print("]=");print_num(Gg[j-1]);print("\n");
417
                }
418
                /* Gg[0] can never be zero */
419
                Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)];
420
        }
421
        /* convert Gg[] to index form for quicker encoding */
422
        for (i = 0; i <= NN - KK; i++)
423
                Gg[i] = Index_of[Gg[i]];
424
 
425
        print("Gg dump\n");
426
        for (i=0;i<=NN-KK;i++){
427
                print_uchar(Gg[i]);
428
                print(" ");
429
        }
430
        print("\n");
431
}
432
 
433
 
434
/*
435
 * take the string of symbols in data[i], i=0..(k-1) and encode
436
 * systematically to produce NN-KK parity symbols in bb[0]..bb[NN-KK-1] data[]
437
 * is input and bb[] is output in polynomial form. Encoding is done by using
438
 * a feedback shift register with appropriate connections specified by the
439
 * elements of Gg[], which was generated above. Codeword is   c(X) =
440
 * data(X)*X**(NN-KK)+ b(X)
441
 */
442
int
443
encode_rs(dtype data[KK], dtype bb[NN-KK])
444
{
445
        register int i, j;
446
        gf feedback;
447
 
448
        CLEAR(bb,NN-KK);
449
        for (i = KK - 1; i >= 0; i--) {
450
#if (MM != 8)
451
                if(data[i] > NN)
452
                        return -1;      /* Illegal symbol */
453
#endif
454
                feedback = Index_of[data[i] ^ bb[NN - KK - 1]];
455
                if (feedback != A0) {   /* feedback term is non-zero */
456
                        for (j = NN - KK - 1; j > 0; j--)
457
                                if (Gg[j] != A0)
458
                                        bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)];
459
                                else
460
                                        bb[j] = bb[j - 1];
461
                        bb[0] = Alpha_to[modnn(Gg[0] + feedback)];
462
                } else {        /* feedback term is zero. encoder becomes a
463
                                 * single-byte shifter */
464
                        for (j = NN - KK - 1; j > 0; j--)
465
                                bb[j] = bb[j - 1];
466
                        bb[0] = 0;
467
                }
468
        }
469
        return 0;
470
}
471
 
472
/*
473
 * Performs ERRORS+ERASURES decoding of RS codes. If decoding is successful,
474
 * writes the codeword into data[] itself. Otherwise data[] is unaltered.
475
 *
476
 * Return number of symbols corrected, or -1 if codeword is illegal
477
 * or uncorrectable.
478
 *
479
 * First "no_eras" erasures are declared by the calling program. Then, the
480
 * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
481
 * If the number of channel errors is not greater than "t_after_eras" the
482
 * transmitted codeword will be recovered. Details of algorithm can be found
483
 * in R. Blahut's "Theory ... of Error-Correcting Codes".
484
 */
485
int
486
eras_dec_rs(dtype data[NN], int eras_pos[NN-KK], int no_eras)
487
{
488
        int deg_lambda, el, deg_omega;
489
        int i, j, r;
490
        gf u,q,tmp,num1,num2,den,discr_r;
491
        gf recd[NN];
492
        gf lambda[NN-KK + 1], s[NN-KK + 1];     /* Err+Eras Locator poly
493
                                                 * and syndrome poly */
494
        gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
495
        gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];
496
        int syn_error, count;
497
 
498
        /* data[] is in polynomial form, copy and convert to index form */
499
        for (i = NN-1; i >= 0; i--){
500
#if (MM != 8)
501
                if(data[i] > NN)
502
                        return -1;      /* Illegal symbol */
503
#endif
504
                recd[i] = Index_of[data[i]];
505
        }
506
        /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
507
         * namely @**(B0+i), i = 0, ... ,(NN-KK-1)
508
         */
509
        syn_error = 0;
510
        for (i = 1; i <= NN-KK; i++) {
511
                tmp = 0;
512
                for (j = 0; j < NN; j++)
513
                        if (recd[j] != A0)      /* recd[j] in index form */
514
                                tmp ^= Alpha_to[modnn(recd[j] + (B0+i-1)*j)];
515
                syn_error |= tmp;       /* set flag if non-zero syndrome =>
516
                                         * error */
517
                /* store syndrome in index form  */
518
                s[i] = Index_of[tmp];
519
        }
520
        if (!syn_error) {
521
                /*
522
                 * if syndrome is zero, data[] is a codeword and there are no
523
                 * errors to correct. So return data[] unmodified
524
                 */
525
                return 0;
526
        }
527
        CLEAR(&lambda[1],NN-KK);
528
        lambda[0] = 1;
529
        if (no_eras > 0) {
530
                /* Init lambda to be the erasure locator polynomial */
531
                lambda[1] = Alpha_to[eras_pos[0]];
532
                for (i = 1; i < no_eras; i++) {
533
                        u = eras_pos[i];
534
                        for (j = i+1; j > 0; j--) {
535
                                tmp = Index_of[lambda[j - 1]];
536
                                if(tmp != A0)
537
                                        lambda[j] ^= Alpha_to[modnn(u + tmp)];
538
                        }
539
                }
540
#ifdef ERASURE_DEBUG
541
                /* find roots of the erasure location polynomial */
542
                for(i=1;i<=no_eras;i++)
543
                        reg[i] = Index_of[lambda[i]];
544
                count = 0;
545
                for (i = 1; i <= NN; i++) {
546
                        q = 1;
547
                        for (j = 1; j <= no_eras; j++)
548
                                if (reg[j] != A0) {
549
                                        reg[j] = modnn(reg[j] + j);
550
                                        q ^= Alpha_to[reg[j]];
551
                                }
552
                        if (!q) {
553
                                /* store root and error location
554
                                 * number indices
555
                                 */
556
                                root[count] = i;
557
                                loc[count] = NN - i;
558
                                count++;
559
                        }
560
                }
561
                if (count != no_eras) {
562
                        print("\n lambda(x) is WRONG\n");
563
                        return -1;
564
                }
565
#ifndef NO_PRINT
566
                print("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
567
                for (i = 0; i < count; i++)
568
                        print_num(loc[i]);
569
                print("\n");
570
#endif
571
#endif
572
        }
573
        for(i=0;i<NN-KK+1;i++)
574
                b[i] = Index_of[lambda[i]];
575
 
576
        /*
577
         * Begin Berlekamp-Massey algorithm to determine error+erasure
578
         * locator polynomial
579
         */
580
        r = no_eras;
581
        el = no_eras;
582
        while (++r <= NN-KK) {  /* r is the step number */
583
                /* Compute discrepancy at the r-th step in poly-form */
584
                discr_r = 0;
585
                for (i = 0; i < r; i++){
586
                        if ((lambda[i] != 0) && (s[r - i] != A0)) {
587
                                discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
588
                        }
589
                }
590
                discr_r = Index_of[discr_r];    /* Index form */
591
                if (discr_r == A0) {
592
                        /* 2 lines below: B(x) <-- x*B(x) */
593
                        COPYDOWN(&b[1],b,NN-KK);
594
                        b[0] = A0;
595
                } else {
596
                        /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
597
                        t[0] = lambda[0];
598
                        for (i = 0 ; i < NN-KK; i++) {
599
                                if(b[i] != A0)
600
                                        t[i+1] = lambda[i+1] ^ Alpha_to[modnn(discr_r + b[i])];
601
                                else
602
                                        t[i+1] = lambda[i+1];
603
                        }
604
                        if (2 * el <= r + no_eras - 1) {
605
                                el = r + no_eras - el;
606
                                /*
607
                                 * 2 lines below: B(x) <-- inv(discr_r) *
608
                                 * lambda(x)
609
                                 */
610
                                for (i = 0; i <= NN-KK; i++)
611
                                        b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
612
                        } else {
613
                                /* 2 lines below: B(x) <-- x*B(x) */
614
                                COPYDOWN(&b[1],b,NN-KK);
615
                                b[0] = A0;
616
                        }
617
                        COPY(lambda,t,NN-KK+1);
618
                }
619
        }
620
 
621
        /* Convert lambda to index form and compute deg(lambda(x)) */
622
        deg_lambda = 0;
623
        for(i=0;i<NN-KK+1;i++){
624
                lambda[i] = Index_of[lambda[i]];
625
                if(lambda[i] != A0)
626
                        deg_lambda = i;
627
        }
628
        /*
629
         * Find roots of the error+erasure locator polynomial. By Chien
630
         * Search
631
         */
632
        COPY(&reg[1],&lambda[1],NN-KK);
633
        count = 0;               /* Number of roots of lambda(x) */
634
        for (i = 1; i <= NN; i++) {
635
                q = 1;
636
                for (j = deg_lambda; j > 0; j--)
637
                        if (reg[j] != A0) {
638
                                reg[j] = modnn(reg[j] + j);
639
                                q ^= Alpha_to[reg[j]];
640
                        }
641
                if (!q) {
642
                        /* store root (index-form) and error location number */
643
                        root[count] = i;
644
                        loc[count] = NN - i;
645
                        count++;
646
                }
647
        }
648
 
649
#ifdef DEBUG
650
        print("\n Final error positions:\t");
651
        for (i = 0; i < count; i++)
652
                print_num(loc[i]);
653
        print("\n");
654
#endif
655
        if (deg_lambda != count) {
656
                /*
657
                 * deg(lambda) unequal to number of roots => uncorrectable
658
                 * error detected
659
                 */
660
                return -1;
661
        }
662
        /*
663
         * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
664
         * x**(NN-KK)). in index form. Also find deg(omega).
665
         */
666
        deg_omega = 0;
667
        for (i = 0; i < NN-KK;i++){
668
                tmp = 0;
669
                j = (deg_lambda < i) ? deg_lambda : i;
670
                for(;j >= 0; j--){
671
                        if ((s[i + 1 - j] != A0) && (lambda[j] != A0))
672
                                tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
673
                }
674
                if(tmp != 0)
675
                        deg_omega = i;
676
                omega[i] = Index_of[tmp];
677
        }
678
        omega[NN-KK] = A0;
679
 
680
        /*
681
         * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
682
         * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
683
         */
684
        for (j = count-1; j >=0; j--) {
685
                num1 = 0;
686
                for (i = deg_omega; i >= 0; i--) {
687
                        if (omega[i] != A0)
688
                                num1  ^= Alpha_to[modnn(omega[i] + i * root[j])];
689
                }
690
                num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
691
                den = 0;
692
 
693
                /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
694
                for (i = min(deg_lambda,NN-KK-1) & ~1; i >= 0; i -=2) {
695
                        if(lambda[i+1] != A0)
696
                                den ^= Alpha_to[modnn(lambda[i+1] + i * root[j])];
697
                }
698
                if (den == 0) {
699
#ifdef DEBUG
700
                        print("\n ERROR: denominator = 0\n");
701
#endif
702
                        return -1;
703
                }
704
                /* Apply error to data */
705
                if (num1 != 0) {
706
                        data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN - Index_of[den])];
707
                }
708
        }
709
        return count;
710
}
711
 
712
void
713
fill_eras(int eras_pos[],int n)
714
{
715
        int i,j,t,work[NN];
716
 
717
        for(i=0;i<NN;i++)
718
                work[i] = i;
719
        for(j=NN-1;j>0;j--){
720
                i = random() % j;       /* not really uniform, I know */
721
                t = work[i];
722
                work[i] = work[j];
723
                work[j] = t;
724
        }
725
#ifdef notdef
726
        for(i=0;i<NN;i++)
727
          print_num(work[i]);
728
        print("\n");
729
#endif
730
        for(i=0;i<n;i++)
731
                eras_pos[i] = work[i];
732
}
733
 
734
/* Return non-zero random number in range 0 - NN (NN power of 2 minus 1) */
735
int
736
randomnz(void)
737
{
738
        int i;
739
 
740
        while((i = random() & NN) == 0)
741
                ;
742
        return i;
743
}
744
 
745
dtype data[NN];
746
dtype tdata[NN];
747
dtype ddata[NN];
748
int eras_pos[NN];
749
int
750
main(int argc,char *argv[])
751
{
752
 
753
 
754
        int i,trials;
755
        int nerrors,nerase,ntrials,verbose,timetest;
756
        int detfails,fails;
757
        extern char *optarg;
758
 
759
        nerrors = nerase = 0;
760
        timetest = verbose = 0;
761
        ntrials = 3;
762
        verbose = 1;
763
        nerrors=11;
764
        nerase=10;
765
//      while((i = getopt(argc,argv,"e:E:n:vt")) != EOF){
766
//              switch(i){
767
///             case 'e':       /* Number of errors per block */
768
//                      nerrors = atoi(optarg);
769
//                      break;
770
//              case 'E':       /* Number of erasures per block */
771
//                      nerase = atoi(optarg);
772
//                      break;
773
//              case 'n':       /* Number of trials */
774
//                      ntrials = atoi(optarg);
775
//                      break;
776
//              case 'v':       /* Be verbose */
777
//                      verbose = 1;
778
//                      break;
779
//              case 't':       /* Repeatedly decode the same block */
780
//                      timetest = 1;
781
//                      break;
782
//              default:
783
//                      printf("usage: %s [-v] [-t] [-e errors] [-E erasures] [-n ntrials]\n",argv[0]);
784
//                      exit(1);
785
//              }
786
//      }
787
        print("It takes very very long time for RTL Simulation.\n");
788
        print("Reed-Solomon code is ");
789
//      for (i=3;i>0;i--){ 
790
                print_num(NN), print(" "); print_num(KK); print("over GF(");
791
                print_num(NN+1);print(")\n");
792
        //      print("i=");print_num(i);print("\n");
793
//      }
794
        print("test erasures: ");print_num(nerase);print("errors ");print_num(nerrors);print("\n");
795
        if(2*nerrors + nerase > NN-KK){
796
                print("Warning: ");
797
                print_num(nerrors); print("errors and ");
798
                print_num(nerase); print("erasures exceeds the correction ability of the code\n");
799
        }
800
 
801
        init_rs();
802
        print("Init_RS Done");
803
 
804
        fails = detfails = 0;
805
        for(trials=0;trials < ntrials;trials++){
806
                if(verbose){
807
                        print(" Trial ");
808
                        print_num(trials);
809
                        print("\n");
810
                }
811
                print("Making Encode Data");
812
                for(i=0;i<KK;i++)
813
                        data[i] = random() & NN;
814
                encode_rs(data,&data[KK]);
815
                fill_eras(eras_pos,nerase+nerrors);
816
                if(verbose && nerase){
817
                        print("\n erasing:");
818
                        for(i=0;i<nerase;i++){
819
                                print(" ");print_num(eras_pos[i]);
820
 
821
                        }
822
                        print("\n");
823
                }
824
                if(verbose && nerrors){
825
                        print(" erroring:");
826
                        for(i=nerase;i<nerase+nerrors;i++){
827
                                print(" ");print_num(eras_pos[i]);
828
 
829
                        }
830
                        print("\n");
831
                }
832
                if(verbose){
833
                        for(i=0;i<NN;i++){
834
                                print_uchar(data[i]);
835
                            print(" ");
836
                        }
837
                        print("\n");
838
                }
839
                memcpy(ddata,data,sizeof(data));
840
                for(i=0;i<nerase+nerrors;i++)
841
                        ddata[eras_pos[i]] ^= randomnz();
842
 
843
                i = eras_dec_rs(ddata,eras_pos,nerase);
844
                if(verbose){
845
                        print("errs + erasures corrected: ");print_num(i);
846
                }
847
                if(i == -1){
848
                        detfails++;
849
                        print("RS decoder detected failure\n");
850
                } else if(memcmp(ddata,data,NN) != 0){
851
                        fails++;
852
                        print(" Undetected decoding failure!\n");
853
                }
854
        }
855
        print(" \n\nTrials: ");
856
        print_num(ntrials);
857
        print(" decoding failures: ");
858
        print_num(detfails); print(" not detected by decoder: ");
859
        print_num(fails); print("\n");
860
        print("$finish");
861
        return 0;
862
}
863
 

powered by: WebSVN 2.1.0

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