-
Notifications
You must be signed in to change notification settings - Fork 44
/
bch_syndrome.v
244 lines (212 loc) · 6.17 KB
/
bch_syndrome.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* BCH Encode/Decoder Modules
*
* Copyright 2014 - Russ Dill <[email protected]>
* Distributed under 2-clause BSD license as contained in COPYING file.
*/
`timescale 1ns / 1ps
`include "bch_defs.vh"
/* Calculate syndromes for S_j for 1 .. 2t-1 */
module bch_syndrome #(
parameter [`BCH_PARAM_SZ-1:0] P = `BCH_SANE,
parameter BITS = 1,
parameter REG_RATIO = 1,
parameter PIPELINE_STAGES = 0
) (
input clk,
input start, /* Accept first syndrome bit (assumes ce) */
input ce,
input [BITS-1:0] data_in,
output ready,
output [`BCH_SYNDROMES_SZ(P)-1:0] syndromes,
output reg done = 0
);
localparam M = `BCH_M(P);
localparam [`MAX_M*(1<<(`MAX_M-1))-1:0] TBL = syndrome_build_table(M, `BCH_T(P));
`include "bch_syndrome.vh"
localparam TCQ = 1;
genvar idx;
localparam CYCLES = PIPELINE_STAGES + (`BCH_CODE_BITS(P)+BITS-1) / BITS;
localparam DONE = lfsr_count(M, CYCLES - 2);
localparam REM = `BCH_CODE_BITS(P) % BITS;
localparam RUNT = BITS - REM;
localparam SYN_COUNT = TBL[0+:`MAX_M];
wire [M-1:0] count;
wire [BITS-1:0] data_pipelined;
wire [BITS-1:0] shifted_in;
wire [BITS-1:0] shifted_pipelined;
wire start_pipelined;
reg busy = 0;
if (CYCLES > 2) begin : COUNTER
lfsr_counter #(M) u_counter(
.clk(clk),
.reset(start && ce),
.ce(busy && ce),
.count(count)
);
end else
assign count = DONE;
assign ready = !busy;
always @(posedge clk) begin
if (ce) begin
if (start) begin
done <= #TCQ CYCLES == 1;
busy <= #TCQ CYCLES > 1;
end else if (busy && count == DONE) begin
done <= #TCQ 1;
busy <= #TCQ 0;
end else
done <= #TCQ 0;
end
end
/*
* Method 1 requires data to be aligned to the first transmitted bit,
* which is how input is received. Method 2 requires data to be
* aligned to the last received bit, so we may need to insert some
* zeros in the first word, and shift the remaining bits
*/
generate
if (REM) begin
reg [RUNT-1:0] runt = 0;
assign shifted_in = {start ? {RUNT{1'b0}} : runt, data_in[BITS-1:RUNT]};
always @(posedge clk)
if (ce)
runt <= #TCQ data_in;
end else
assign shifted_in = data_in;
endgenerate
/* Pipelined data for method1 */
pipeline_ce #(PIPELINE_STAGES > 1) u_data_pipeline [BITS-1:0] (
.clk(clk),
.ce(ce),
.i(data_in),
.o(data_pipelined)
);
/* Pipelined data for method2 */
pipeline_ce #(PIPELINE_STAGES > 0) u_shifted_pipeline [BITS-1:0] (
.clk(clk),
.ce(ce),
.i(shifted_in),
.o(shifted_pipelined)
);
pipeline_ce #(PIPELINE_STAGES > 1) u_start_pipeline (
.clk(clk),
.ce(ce),
.i(start),
.o(start_pipelined)
);
/* LFSR registers */
generate
for (idx = 0; idx < SYN_COUNT; idx = idx + 1) begin : SYNDROMES
localparam SYN = idx2syn(idx);
if (syndrome_method(`BCH_T(P), SYN) == 0) begin : METHOD1
dsynN_method1 #(P, SYN, BITS, REG_RATIO, PIPELINE_STAGES) u_syn1a(
.clk(clk),
.start(start),
.start_pipelined(start_pipelined),
.ce((busy || start) && ce),
.data_pipelined(data_pipelined),
.synN(syndromes[idx*M+:M])
);
end else begin : METHOD2
dsynN_method2 #(P, SYN, syndrome_degree(M, SYN), BITS, PIPELINE_STAGES) u_syn2a(
.clk(clk),
.start(start),
.start_pipelined(start_pipelined),
.ce((busy || start) && ce),
.data_in(shifted_in),
.data_pipelined(shifted_pipelined),
.synN(syndromes[idx*M+:M])
);
end
end
endgenerate
endmodule
/* Syndrome expansion/shuffling */
module bch_syndrome_shuffle #(
parameter [`BCH_PARAM_SZ-1:0] P = `BCH_SANE
) (
input clk,
input start, /* Accept first syndrome bit */
input ce, /* Shuffle cycle */
input [`BCH_SYNDROMES_SZ(P)-1:0] syndromes,
output reg [(2*`BCH_T(P)-1)*`BCH_M(P)-1:0] syn_shuffled = 0
);
localparam M = `BCH_M(P);
localparam [`MAX_M*(1<<(`MAX_M-1))-1:0] TBL = syndrome_build_table(M, `BCH_T(P));
`include "bch_syndrome.vh"
localparam TCQ = 1;
localparam T = `BCH_T(P);
genvar i;
wire [(2*T-1)*M-1:0] bypass_in_shifted;
wire [(2*T-1)*M-1:0] syndromes_pre_expand;
wire [(2*T-1)*M-1:0] expand_in;
wire [(2*T-1)*M-1:0] expand_in1;
wire [(2*T-1)*M-1:0] syn_expanded;
for (i = 0; i < 2 * T - 1; i = i + 1) begin : ASSIGN
assign syndromes_pre_expand[i*M+:M] = syndromes[dat2idx(i+1)*M+:M] & {M{start}};
end
/* Shuffle syndromes */
rotate_right #((2*T-1)*M, 3*M) u_rol_e(syndromes_pre_expand, expand_in1);
reverse_words #(M, 2*T-1) u_rev(expand_in1, expand_in);
rotate_left #((2*T-1)*M, 2*M) u_rol_b(syn_shuffled, bypass_in_shifted);
/*
* We need to combine syndrome expansion and shuffling into a single
* operation so we can optimize LUT usage for an XOR carry chain. It
* causes a little confusion as we need to select expansion method
* based on the pre-shuffled indexes as well as pass in the pre-
* shuffled index to the expand method.
*/
for (i = 0; i < 2 * T - 1; i = i + 1) begin : EXPAND
localparam PRE = (2 * T - 1 + 2 - i) % (2 * T - 1); /* Pre-shuffle value */
if (syndrome_method(T, dat2syn(PRE+1)) == 0) begin : METHOD1
syndrome_expand_method1 #(P) u_expand(
.in(expand_in[i*M+:M]),
.out(syn_expanded[i*M+:M])
);
end else begin : METHOD2
syndrome_expand_method2 #(P, PRE+1) u_expand(
.in(expand_in[i*M+:M]),
.out(syn_expanded[i*M+:M])
);
end
end
always @(posedge clk)
if (start || ce)
syn_shuffled <= #TCQ syn_expanded ^ ({(2*T-1)*M{!start}} & bypass_in_shifted);
endmodule
module bch_errors_present #(
parameter [`BCH_PARAM_SZ-1:0] P = `BCH_SANE,
parameter PIPELINE_STAGES = 0
) (
input clk,
input start,
input [`BCH_SYNDROMES_SZ(P)-1:0] syndromes,
output done,
output errors_present /* Valid during done cycle */
);
localparam M = `BCH_M(P);
genvar i;
wire [(`BCH_SYNDROMES_SZ(P)/M)-1:0] syndrome_zero;
wire [(`BCH_SYNDROMES_SZ(P)/M)-1:0] syndrome_zero_pipelined;
generate
for (i = 0; i < `BCH_SYNDROMES_SZ(P)/M; i = i + 1) begin : ZEROS
assign syndrome_zero[i] = |syndromes[i*M+:M];
end
endgenerate
pipeline #(PIPELINE_STAGES > 0) u_sz_pipeline [`BCH_SYNDROMES_SZ(P)/M-1:0] (
.clk(clk),
.i(syndrome_zero),
.o(syndrome_zero_pipelined)
);
pipeline #(PIPELINE_STAGES > 1) u_present_pipeline (
.clk(clk),
.i(|syndrome_zero_pipelined),
.o(errors_present)
);
pipeline #(PIPELINE_STAGES) u_done_pipeline (
.clk(clk),
.i(start),
.o(done)
);
endmodule