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

Subversion Repositories bluespec-reedsolomon

[/] [bluespec-reedsolomon/] [trunk/] [bsv-reedsolomon/] [mkReedSolomon.bsv] - 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
import GetPut::*;
29
import FIFO::*;
30
import GFTypes::*;
31
import GFArith::*;
32
import SyndromeParallel::*;
33
import Berlekamp::*;
34
import ChienSearch::*;
35
import ErrorMagnitude::*;
36
import ErrorCorrector::*;
37
 
38
// Uncomment line below which defines BUFFER_LENGTH if
39
// you get a compile error regarding BUFFER_LENGTH
40
 
41
`define BUFFER_LENGTH  255
42
 
43
// ---------------------------------------------------------
44
// Reed-Solomon interface
45
// ---------------------------------------------------------
46
interface IReedSolomon;
47
   interface Put#(Byte) rs_t_in;
48
   interface Put#(Byte) rs_k_in;
49
   interface Put#(Byte) rs_input;
50
   interface Get#(Byte) rs_output;
51
   interface Get#(Bool) rs_flag;
52
endinterface
53
 
54
// ---------------------------------------------------------
55
// Reed-Solomon module
56
// ---------------------------------------------------------
57
(* synthesize *)
58
module mkReedSolomon (IReedSolomon);
59
 
60
   ISyndrome         syndrome          <- mkSyndromeParallel;
61
   IBerlekamp        berl              <- mkBerlekamp;
62
   IChienSearch      chien_search      <- mkChienSearch;
63
   IErrorMagnitude   error_magnitude   <- mkErrorMagnitude;
64
   IErrorCorrector   error_corrector   <- mkErrorCorrector;
65
 
66
   // FIFOs
67
   FIFO#(Byte)               t_in                          <- mkSizedFIFO(2);
68
   FIFO#(Byte)               k_in                          <- mkSizedFIFO(2);
69
   FIFO#(Byte)               stream_in                     <- mkSizedFIFO(2);
70
   FIFO#(Byte)               stream_out                    <- mkSizedFIFO(2);
71
   FIFO#(Bool)               cant_correct_out              <- mkSizedFIFO(3);
72
 
73
   // FIFOs for input of syndrome module
74
   FIFO#(Byte)               ff_n_to_syndrome              <- mkSizedFIFO(1);
75
   FIFO#(Byte)               ff_r_to_syndrome              <- mkSizedFIFO(2);
76
 
77
   // FIFOs for input of berlekamp module
78
   FIFO#(Byte)               ff_t_to_berl             <- mkSizedFIFO(2);
79
   FIFO#(Syndrome#(TwoT))    ff_s_to_berl             <- mkSizedFIFO(1);
80
 
81
   // FIFOs for input of chien searach module
82
   FIFO#(Byte)               ff_t_to_chien                 <- mkSizedFIFO(3);
83
   FIFO#(Byte)               ff_k_to_chien                 <- mkSizedFIFO(3);
84
   FIFO#(Bool)               ff_no_error_flag_to_chien     <- mkSizedFIFO(1);
85
   FIFO#(Syndrome#(T))       ff_l_to_chien                 <- mkSizedFIFO(1);
86
 
87
   // FIFOs for input of error magnitude module
88
   FIFO#(Byte)               ff_k_to_errormag              <- mkSizedFIFO(4);
89
   FIFO#(Bool)               ff_no_error_flag_to_errormag  <- mkSizedFIFO(2);
90
   FIFO#(Maybe#(Byte))       ff_loc_to_errormag            <- mkSizedFIFO(2);
91
   FIFO#(Maybe#(Byte))       ff_alpha_inv_to_errormag      <- mkSizedFIFO(2);
92
   FIFO#(Syndrome#(T))       ff_l_to_errormag              <- mkSizedFIFO(1);
93
   FIFO#(Syndrome#(T))       ff_w_to_errormag              <- mkSizedFIFO(2);
94
 
95
   // FIFOs for input of error corrector module
96
   FIFO#(Byte)               ff_r_to_errorcor              <- mkSizedFIFO(`BUFFER_LENGTH);
97
   FIFO#(Byte)               ff_e_to_errorcor              <- mkSizedFIFO(2);
98
   FIFO#(Byte)               ff_k_to_errorcor              <- mkSizedFIFO(5);
99
   FIFO#(Bool)               ff_no_error_flag_to_errorcor  <- mkSizedFIFO(3);
100
 
101
   // Regs
102
   Reg#(Bool)     info_count_done      <- mkReg (True);
103
   Reg#(Bool)     parity_count_done    <- mkReg (True);
104
   Reg#(Byte)     state                <- mkReg (0);
105
   Reg#(Bit#(32)) cycle_count          <- mkReg (0);
106
   Reg#(Byte)     info_count           <- mkReg (0);
107
   Reg#(Byte)     parity_count         <- mkReg (0);
108
 
109
   // ----------------------------------
110
   rule init (state == 0);
111
      state <= 1;
112
   endrule
113
 
114
   // ----------------------------------
115
   rule read_mac (state == 1 && info_count_done == True && parity_count_done == True);
116
      let k = k_in.first();
117
      k_in.deq ();
118
      info_count <= k;
119
      // k = 0, means no info bytes, stupid special case!
120
      if (k == 0)
121
         info_count_done <= True;
122
      else
123
         info_count_done <= False;
124
      ff_k_to_chien.enq(k);
125
      ff_k_to_errormag.enq(k);
126
      ff_k_to_errorcor.enq(k);
127
 
128
      let t = t_in.first();
129
      t_in.deq();
130
      ff_t_to_berl.enq(t);
131
      ff_t_to_chien.enq(t);
132
 
133
      let n = k + 2 * t;
134
      ff_n_to_syndrome.enq(n);
135
 
136
      parity_count <= 2 * t ;
137
      if (t == 0)
138
         parity_count_done <= True;
139
      else
140
         parity_count_done <= False;
141
 
142
      $display ("  [reedsol] read_mac z = %d, k = %d, t = %d", 255 - k - 2*t, k, t);
143
   endrule
144
 
145
   rule read_input (state == 1 && info_count_done == False);
146
      let datum = stream_in.first ();
147
      $display ("  [reedsol]  read_input [%d] = %d", info_count, datum);
148
      stream_in.deq();
149
      ff_r_to_syndrome.enq(datum);
150
      ff_r_to_errorcor.enq(datum);
151
      if (info_count == 1)
152
         info_count_done <= True;
153
      info_count <= info_count - 1;
154
   endrule
155
 
156
   rule read_parity (state == 1 && info_count_done == True && parity_count_done == False);
157
      let datum = stream_in.first ();
158
      $display ("  [reedsol]  read_parity [%d] = %d", parity_count, datum);
159
      stream_in.deq();
160
      ff_r_to_syndrome.enq (datum);
161
      if (parity_count == 1)
162
         parity_count_done <= True;
163
      parity_count <= parity_count - 1;
164
   endrule
165
 
166
   // ----------------------------------
167
   // rule for syndrome
168
   rule n_to_syndrome (state == 1);
169
      // $display ("    > > [t to syndrome] cycle count: %d", cycle_count);
170
      ff_n_to_syndrome.deq();
171
      let datum = ff_n_to_syndrome.first();
172
      syndrome.n_in(datum);
173
   endrule
174
 
175
   rule r_to_syndrome (state == 1);
176
      // $display ("    > > [r to syndrome] cycle count: %d", cycle_count);
177
      ff_r_to_syndrome.deq();
178
      let datum = ff_r_to_syndrome.first();
179
      syndrome.r_in(datum);
180
   endrule
181
 
182
   rule s_from_syndrome (state == 1);
183
      // $display ("    > > [s from syndrome] cycle count: %d", cycle_count);
184
      let datum <- syndrome.s_out();
185
      ff_s_to_berl.enq(datum);
186
   endrule
187
 
188
   // ----------------------------------
189
   // rules for berlekamp
190
   rule s_to_berl (state == 1);
191
      // $display ("    > > [s to berlekamp] cycle count: %d", cycle_count);
192
      ff_s_to_berl.deq();
193
      let datum = ff_s_to_berl.first();
194
      berl.s_in(datum);
195
   endrule
196
 
197
   rule t_to_berl (state == 1);
198
      // $display ("    > > [t to berlekamp] cycle count: %d", cycle_count);
199
      ff_t_to_berl.deq();
200
      let datum = ff_t_to_berl.first();
201
      berl.t_in(datum);
202
   endrule
203
 
204
   rule flag_from_berl (state == 1);
205
      // $display ("    > > [no error flag from syndrome] cycle count: %d", cycle_count);
206
      let no_error <- berl.no_error_flag_out();
207
      ff_no_error_flag_to_chien.enq(no_error);
208
      ff_no_error_flag_to_errormag.enq(no_error);
209
      ff_no_error_flag_to_errorcor.enq(no_error);
210
   endrule
211
 
212
   rule l_from_berl (state == 1);
213
      // $display ("    > > [l from berlekamp] cycle count: %d", cycle_count);
214
      let datum <- berl.lambda_out();
215
      ff_l_to_chien.enq(datum);
216
   endrule
217
 
218
   rule w_from_berl(state == 1);
219
      // $display ("    > > [w from berlekamp] cycle count: %d", cycle_count);
220
      let datum <- berl.omega_out();
221
      ff_w_to_errormag.enq(datum);
222
   endrule
223
 
224
   // ----------------------------------
225
   // rules for chien_search
226
   rule t_to_chien (state == 1);
227
      // $display ("    > > [t to chien] cycle count: %d", cycle_count);
228
      ff_t_to_chien.deq();
229
      let datum = ff_t_to_chien.first();
230
      chien_search.t_in(datum);
231
   endrule
232
 
233
   rule k_to_chien (state == 1);
234
      ff_k_to_chien.deq ();
235
      let datum = ff_k_to_chien.first ();
236
      chien_search.k_in (datum);
237
   endrule
238
 
239
   rule l_to_chien (state == 1);
240
      // $display ("    > > [l to chien] cycle count: %d", cycle_count);
241
      ff_l_to_chien.deq();
242
      let datum = ff_l_to_chien.first();
243
      chien_search.lambda_in(datum);
244
   endrule
245
 
246
   rule no_error_flag_to_chien (state == 1);
247
      // $display ("    > > [no_error to chien] cycle count: %d", cycle_count);
248
      ff_no_error_flag_to_chien.deq ();
249
      let no_error = ff_no_error_flag_to_chien.first ();
250
      chien_search.no_error_flag_in (no_error);
251
   endrule
252
 
253
   rule flag_from_chien (state == 1);
254
      // $display ("    > > [flag from chien] cycle count: %d", cycle_count);
255
      let datum <- chien_search.cant_correct_flag_out();
256
      cant_correct_out.enq(datum);
257
   endrule
258
 
259
   rule loc_from_chien (state == 1);
260
      // $display ("    > > [loc from chien] cycle count: %d", cycle_count);
261
      let datum <- chien_search.loc_out();
262
      ff_loc_to_errormag.enq(datum);
263
   endrule
264
 
265
   rule alpha_inv_from_chien (state == 1);
266
      // $display ("    > > [alpha inv from chien] cycle count: %d", cycle_count);
267
      let datum <- chien_search.alpha_inv_out();
268
      ff_alpha_inv_to_errormag.enq(datum);
269
   endrule
270
 
271
   rule l_from_chien (state == 1);
272
      // $display ("    > > [l from berlekamp] cycle count: %d", cycle_count);
273
      let datum <- chien_search.lambda_out();
274
      ff_l_to_errormag.enq(datum);
275
   endrule
276
 
277
   // ----------------------------------
278
   // rules for error_magnitude
279
   rule k_to_errormag (state == 1);
280
      ff_k_to_errormag.deq();
281
      let datum = ff_k_to_errormag.first();
282
      error_magnitude.k_in(datum);
283
   endrule
284
 
285
   rule no_error_flag_to_errormag (state == 1);
286
      ff_no_error_flag_to_errormag.deq();
287
      let datum = ff_no_error_flag_to_errormag.first();
288
      error_magnitude.no_error_flag_in(datum);
289
   endrule
290
 
291
   rule loc_to_errormag (state == 1);
292
      ff_loc_to_errormag.deq();
293
      let datum = ff_loc_to_errormag.first();
294
      error_magnitude.loc_in(datum);
295
   endrule
296
 
297
   rule alpha_inv_to_errormag (state == 1);
298
      ff_alpha_inv_to_errormag.deq();
299
      let datum = ff_alpha_inv_to_errormag.first();
300
      error_magnitude.alpha_inv_in(datum);
301
   endrule
302
 
303
   rule l_to_errormag (state == 1);
304
      ff_l_to_errormag.deq();
305
      let datum = ff_l_to_errormag.first();
306
      error_magnitude.lambda_in(datum);
307
   endrule
308
 
309
   rule w_to_errormag (state == 1);
310
      // $display ("    > > [w to chien] cycle count: %d", cycle_count);
311
      ff_w_to_errormag.deq();
312
      let datum = ff_w_to_errormag.first();
313
      error_magnitude.omega_in(datum);
314
   endrule
315
 
316
   rule e_from_errormag (state == 1);
317
      // $display ("    > > [e from chien] cycle count: %d", cycle_count);
318
      let datum <- error_magnitude.error_out();
319
      ff_e_to_errorcor.enq(datum);
320
   endrule
321
 
322
   // ----------------------------------
323
   // rules for error_corrector
324
   rule k_to_error_corrector (state == 1);
325
      // $display ("    > > [t to error_corrector] cycle count: %d", cycle_count);
326
      ff_k_to_errorcor.deq ();
327
      let datum = ff_k_to_errorcor.first ();
328
      error_corrector.k_in (datum);
329
   endrule
330
 
331
   rule no_error_flag_to_error_corrector (state == 1);
332
      // $display ("    > > [no_error to error_corrector] cycle count: %d", cycle_count);
333
      ff_no_error_flag_to_errorcor.deq();
334
      let no_error = ff_no_error_flag_to_errorcor.first();
335
      error_corrector.no_error_flag_in(no_error);
336
   endrule
337
 
338
   rule r_to_error_corrector (state == 1);
339
      // $display ("    > > [r to error corrector] cycle count: %d", cycle_count);
340
      ff_r_to_errorcor.deq ();
341
      let datum = ff_r_to_errorcor.first ();
342
      error_corrector.r_in (datum);
343
   endrule
344
 
345
   rule e_to_error_corrector (state == 1);
346
      // $display ("    > > [e to error corector] cycle count: %d", cycle_count);
347
      ff_e_to_errorcor.deq ();
348
      let error = ff_e_to_errorcor.first ();
349
      error_corrector.e_in (error);
350
   endrule
351
 
352
   rule d_from_error_corrector (state == 1);
353
      // $display ("    > > [d from error corector] cycle count: %d", cycle_count);
354
      let corrected_datum <- error_corrector.d_out ();
355
      stream_out.enq (corrected_datum);
356
   endrule
357
 
358
   // ----------------------------------
359
   rule cycle (state == 1);
360
      $display ("%d  -------------------------", cycle_count);
361
      cycle_count <= cycle_count + 1;
362
   endrule
363
 
364
   interface Put rs_t_in     = fifoToPut(t_in);
365
   interface Put rs_k_in     = fifoToPut(k_in);
366
   interface Put rs_input    = fifoToPut(stream_in);
367
   interface Get rs_output   = fifoToGet(stream_out);
368
   interface Get rs_flag     = fifoToGet(cant_correct_out);
369
 
370
endmodule
371
 

powered by: WebSVN 2.1.0

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