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

Subversion Repositories reedsolomon

[/] [reedsolomon/] [trunk/] [bluespec-source/] [ChienSearch.bsv] - 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
import FIFO::*;
29
import GFArith::*;
30
import GFTypes::*;
31
import MFIFO::*;
32
import UniqueWrappers::*;
33
import Vector::*;
34
 
35
typedef enum
36
{
37
   LOC_SEARCH,
38
   LOC_DONE
39
} Stage deriving (Bits, Eq);
40
 
41
// ---------------------------------------------------------
42
// Reed-Solomon Chien Error Magnitude computer interface
43
// ---------------------------------------------------------
44
interface IChienSearch;
45
   // input methods
46
   method Action                         t_in(Byte t_new);
47
   method Action                         k_in(Byte k_new);
48
   method Action                         no_error_flag_in(Bool no_error_new);
49
   method Action                         lambda_in(Syndrome#(T) lamdba_new);
50
 
51
   // output methods
52
   method ActionValue#(Maybe#(Byte))     loc_out();       // use Invalid to show new packet
53
   method ActionValue#(Maybe#(Byte))     alpha_inv_out(); // the alpha_inv of location that has error
54
   method ActionValue#(Bool)             cant_correct_flag_out();
55
   method ActionValue#(Syndrome#(T))     lambda_out();
56
endinterface
57
 
58
// ---------------------------------------------------------
59
// Auxiliary Function
60
// ---------------------------------------------------------
61
(* noinline *)
62
function Syndrome#(T) times_alpha_n_v(Syndrome#(T) lambda_a, Byte t);
63
   Syndrome#(T) lambda_a_new = lambda_a;
64
   for (Byte x = 0; x < fromInteger(valueOf(T)); x = x + 1)
65
      lambda_a_new[x] = times_alpha_n(lambda_a[x], x + 1) & ((x < t)? 8'hFF : 8'h00);
66
   return lambda_a_new;
67
endfunction
68
 
69
// ---------------------------------------------------------
70
// Reed-Solomon Chien Error Magnitude computer module
71
// ---------------------------------------------------------
72
(* synthesize *)
73
module mkChienSearch (IChienSearch);
74
 
75
   // comb. circuit sharing
76
   Wrapper2#(Syndrome#(T),Byte,
77
             Syndrome#(T))       times_alpha_n_vec   <- mkUniqueWrapper2(times_alpha_n_v);
78
 
79
   // input queues
80
   FIFO#(Bool)                   no_error_flag_q     <- mkSizedFIFO(1);
81
   FIFO#(Byte)                   t_q                 <- mkSizedFIFO(1);
82
   FIFO#(Byte)                   k_q                 <- mkSizedFIFO(1);
83
   FIFO#(Syndrome#(T))           lambda_q            <- mkSizedFIFO(1);
84
 
85
   // output queues
86
   FIFO#(Bool)                   cant_correct_flag_q <- mkSizedFIFO(1);
87
   FIFO#(Maybe#(Byte))           loc_q               <- mkSizedFIFO(2);
88
   FIFO#(Maybe#(Byte))           alpha_inv_q         <- mkSizedFIFO(2);
89
   MFIFO#(1,Syndrome#(T))        lambda_a_q          <- mkMFIFO1();
90
 
91
   // book-keep state
92
   Reg#(Byte)                    i                   <- mkRegU();
93
   Reg#(Byte)                    count_error         <- mkRegU();
94
   Reg#(Stage)                   stage               <- mkReg(LOC_DONE);
95
   Reg#(Byte)                    block_number        <- mkReg(0);
96
   Reg#(Byte)                    alpha_inv           <- mkRegU();
97
 
98
   // variables
99
   let no_error_flag = no_error_flag_q.first();
100
   let t = t_q.first();
101
   let k = k_q.first();
102
   Reg#(Syndrome#(T)) lambda_a = lambda_a_q.first;
103
 
104
   // ------------------------------------------------
105
   rule calc_loc (stage == LOC_SEARCH);
106
      $display ("  [chien %d]  calc_loc, i = %d", block_number, i);
107
 
108
      let zero_padding = (i >= k + 2 * t);
109
      let parity_bytes = (i < 2 * t);
110
      let process_error = ((i < k + 2 * t) && (i >= 2 * t));
111
      Byte result_location = 1;
112
 
113
      if (!no_error_flag)
114
         begin
115
            result_location = fold(gf_add, cons(1,lambda_a)); // lambda_a add up + 1
116
            alpha_inv <= times_alpha(alpha_inv);
117
 
118
            $display ("  [chien %d]  calc_loc, result location = %d", block_number, result_location);
119
         end
120
 
121
      let is_no_error = (result_location != 0);
122
 
123
      if (!is_no_error)
124
         count_error <= count_error + 1;
125
 
126
      if (i == 0)
127
         begin
128
            stage <= LOC_DONE;
129
            cant_correct_flag_q.enq(count_error == 0);
130
            t_q.deq();
131
            k_q.deq();
132
            no_error_flag_q.deq();
133
            if (!no_error_flag)
134
               begin
135
                  lambda_a_q.deq();
136
                  loc_q.enq(tagged Invalid);
137
                  alpha_inv_q.enq(tagged Invalid);
138
               end
139
         end
140
      else
141
         begin
142
            i <= i - 1;
143
            if (!no_error_flag)
144
               begin
145
                  let lambda_a_new <- times_alpha_n_vec.func(lambda_a,t);
146
                  lambda_a <= lambda_a_new;
147
 
148
                  //$display ("  [chien %d]  calc_loc, lambda_a = %d", block_number, lambda_a_new);
149
               end
150
            if (process_error)
151
               begin
152
                  if (!is_no_error) // enq loc_q an alpha_inv_q if there is error
153
                     begin
154
                        alpha_inv_q.enq(tagged Valid alpha_inv);
155
                        loc_q.enq(tagged Valid (k + 2 * t - i - 1)); // process range 1 - k
156
                     end
157
         end
158
 
159
         end
160
   endrule
161
 
162
   // ------------------------------------------------
163
   rule start_next_chien (stage == LOC_DONE);
164
      $display ("Start Next Chien ");
165
 
166
      stage <= LOC_SEARCH;
167
      i <= 254;
168
      count_error <= no_error_flag ? 1 : 0;
169
      block_number <= block_number + 1;
170
      if (!no_error_flag)
171
         begin
172
            let lambda_a_new <- times_alpha_n_vec.func(lambda_a,t);
173
            lambda_a <= lambda_a_new; // if correctable, initialize lambda_a
174
            alpha_inv <= 2; // = alpha^(1) = alpha^(-254)
175
         end
176
   endrule
177
 
178
   // ------------------------------------------------
179
   method Action no_error_flag_in (Bool no_error_new);
180
      $display ("  [chien %d]  no_error_in : %d", block_number, no_error_new);
181
 
182
      no_error_flag_q.enq(no_error_new);
183
   endmethod
184
 
185
   // ------------------------------------------------
186
   method Action t_in (Byte t_new);
187
      $display ("  [chien %d]  t_in : %d", block_number, t_new);
188
 
189
      t_q.enq(t_new);
190
   endmethod
191
 
192
   // ------------------------------------------------
193
   method Action k_in (Byte k_new);
194
      $display ("  [chien %d]  k_in : %d", block_number, k_new);
195
 
196
      k_q.enq(k_new);
197
   endmethod
198
 
199
   // ------------------------------------------------
200
   method Action lambda_in(Syndrome#(T) lambda_new);
201
      //$display ("  [chien %d]  lambda_in : %d", block_number, lambda_new);
202
 
203
      lambda_a_q.enq(lambda_new);
204
      lambda_q.enq(lambda_new);
205
   endmethod
206
 
207
   // ------------------------------------------------
208
   method ActionValue#(Maybe#(Byte)) loc_out();
209
      $display ("  [chien %d]  loc_out : %d, stage : %d", block_number, loc_q.first(), stage);
210
      loc_q.deq();
211
      $display ("No of Errors %d", count_error);
212
 
213
      return loc_q.first();
214
   endmethod
215
 
216
   // ------------------------------------------------
217
   method ActionValue#(Maybe#(Byte)) alpha_inv_out();
218
      $display ("  [chien %d]  alpha_inv_out : %d, stage : %d", block_number, alpha_inv_q.first(), stage);
219
      alpha_inv_q.deq();
220
      $display ("No of Errors %d", count_error);
221
 
222
      return alpha_inv_q.first();
223
   endmethod
224
 
225
   // ------------------------------------------------
226
   method ActionValue#(Bool) cant_correct_flag_out();
227
      $display ("  [chien %d]  Can't Correct Flag : %d", block_number, cant_correct_flag_q.first());
228
 
229
      cant_correct_flag_q.deq();
230
      return cant_correct_flag_q.first();
231
   endmethod
232
 
233
   // ------------------------------------------------
234
   method ActionValue#(Syndrome#(T)) lambda_out();
235
      //$display ("  [chien %d]  lambda_out : %d", block_number, lambda_q.first());
236
 
237
      lambda_q.deq();
238
      return lambda_q.first();
239
   endmethod
240
 
241
endmodule
242
 
243
 
244
 
245
 
246
 
247
 

powered by: WebSVN 2.1.0

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