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

Subversion Repositories jpegencode

[/] [jpegencode/] [trunk/] [ff_checker.v] - Blame information for rev 3

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

Line No. Rev Author Line
1 2 davidklun
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  JPEG Encoder Core - Verilog                                ////
4
////                                                             ////
5
////  Author: David Lundgren                                     ////
6
////          davidklun@gmail.com                                ////
7
////                                                             ////
8
/////////////////////////////////////////////////////////////////////
9
////                                                             ////
10
//// Copyright (C) 2009 David Lundgren                           ////
11
////                  davidklun@gmail.com                        ////
12
////                                                             ////
13
//// This source file may be used and distributed without        ////
14
//// restriction provided that this copyright statement is not   ////
15
//// removed from the file and that any derivative work contains ////
16
//// the original copyright notice and the associated disclaimer.////
17
////                                                             ////
18
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
19
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
20
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
21
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
22
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
23
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
24
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
25
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
26
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
27
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
28
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
29
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
30
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
31
////                                                             ////
32
/////////////////////////////////////////////////////////////////////
33
 
34
/*
35
This module takes the JPEG_bitstream as its input, and checks for any FF values in
36
the bitstream.  When it finds an FF in the bitstream, this module puts a 00 after the FF,
37
and then continues with the rest of the bitstream after the 00, per the JPEG standard.
38
*/
39
 
40
`timescale 1ns / 100ps
41
 
42
module ff_checker(clk, rst, end_of_file_signal, JPEG_in, data_ready_in, orc_reg_in,
43
JPEG_bitstream_1, data_ready_1, orc_reg, eof_data_partial_ready);
44
input           clk;
45
input           rst;
46
input           end_of_file_signal;
47
input  [31:0]  JPEG_in;
48
input           data_ready_in;
49
input   [4:0]    orc_reg_in;
50
output  [31:0]  JPEG_bitstream_1;
51
output          data_ready_1;
52
output  [4:0]    orc_reg;
53
output          eof_data_partial_ready;
54
 
55
 
56
reg     first_2bytes, second_2bytes, third_2bytes, fourth_2bytes;
57
reg first_2bytes_eof, second_2bytes_eof, third_2bytes_eof;
58
reg fourth_2bytes_eof, fifth_2bytes_eof, s2b, t2b;
59
reg [79:0]       JPEG_eof_6, JPEG_eof_7;
60
reg [63:0]       JPEG_5, JPEG_eof_5_1, JPEG_6, JPEG_7;
61
reg [55:0]       JPEG_4, JPEG_eof_3, JPEG_eof_4, JPEG_eof_5;
62
reg [47:0]       JPEG_3, JPEG_eof_2;
63
reg     [39:0]   JPEG_2, JPEG_eof_1;
64
reg     [31:0]   JPEG_1, JPEG_ro, JPEG_bitstream, JPEG_bitstream_1;
65
reg [31:0]       JPEG_eof, JPEG_eof_ro;
66
reg [31:0]       JPEG_bitstream_eof;
67
reg [15:0]       JPEG_eof_ro_ro;
68
reg     [87:0]   JPEG_out, JPEG_out_1, JPEG_pf;
69
reg [23:0]  JPEG_ro_ro;
70
reg     dr_in_1, dr_in_2, dr_in_3, dr_in_4, dr_in_5, dr_in_6;
71
reg     dr_in_7, dr_in_8;
72
reg rollover, rollover_1, rollover_2, rollover_3, rollover_4, rollover_5;
73
reg rollover_pf, rpf_1;
74
reg [1:0] FF_count, FF_count_1, FF_eof_shift;
75
reg [2:0] count_total, ct_1;
76
reg [1:0] ffc_1, ffc_2, ffc_3, ffc_4, ffc_5, ffc_6, ffc_7;
77
reg [1:0] ffc_postfifo, count_total_eof;
78
reg [4:0] orc_input;
79
reg [4:0] orc_reg;
80
reg [6:0] extra_bits_eof, extra_bits_eof_1;
81
wire [90:0] read_data;
82
wire [90:0] write_data = { JPEG_out_1, ffc_7, rollover_5 };
83
reg data_ready, data_ready_1, write_enable, read_req, rdv_1;
84
reg end_of_file_enable, eof_count_enable;
85
reg eof_data_partial_ready, eof_dpr_1, eof_dpr_2;
86
reg end_of_file_enable_hold, eof_data_ready;
87
reg eof_data_ready_1, eof_bits_1, eof_bits_2, eof_bits_3;
88
reg [8:0] eof_count;
89
wire fifo_empty, rdata_valid;
90
 
91
 sync_fifo_ff u18(.clk(clk), .rst(rst), .read_req(read_req), .write_data(write_data),
92
                .write_enable(write_enable), .rollover_write(rollover_5),
93
                .read_data(read_data), .fifo_empty(fifo_empty),
94
                .rdata_valid(rdata_valid));
95
 
96
        // sync_fifo is the FIFO for the bitstream.  A FIFO is needed because when a  
97
        // total of 4 FF's have been found, then there will be a rollover of the 
98
        // 32 bit set of JPEG bits.  The JPEG bitstream input will need to be stored 
99
        // for 1 clock cycle as the extra 00 put in after the FF will cause an
100
        // extra set of 32 bits to be put into the bitstream.
101
 
102
always @(posedge clk)
103
begin
104
        if (rst)
105
                eof_data_partial_ready <= 0;
106
        else if (eof_bits_1)
107
                eof_data_partial_ready <= (extra_bits_eof_1 > 0) && (extra_bits_eof_1 < 32);
108
        else
109
                eof_data_partial_ready <= eof_dpr_1;
110
end
111
 
112
always @(posedge clk)
113
begin
114
        if (rst)
115
                eof_dpr_1 <= 0;
116
        else if (eof_bits_1)
117
                eof_dpr_1 <= (extra_bits_eof_1 > 32) && (extra_bits_eof_1 < 64);
118
        else
119
                eof_dpr_1 <= eof_dpr_2;
120
end
121
 
122
always @(posedge clk)
123
begin
124
        if (rst)
125
                eof_dpr_2 <= 0;
126
        else if (eof_bits_1)
127
                eof_dpr_2 <= extra_bits_eof_1 > 64;
128
        else
129
                eof_dpr_2 <= 0;
130
end
131
 
132
always @(posedge clk)
133
begin
134
        if (rst)
135
                eof_data_ready_1 <= 0;
136
        else if (end_of_file_enable)
137
                eof_data_ready_1 <= (extra_bits_eof_1 > 31);
138
        else if (eof_bits_1 || eof_bits_2)
139
                eof_data_ready_1 <= eof_data_ready;
140
end
141
 
142
always @(posedge clk)
143
begin
144
        if (rst)
145
                eof_data_ready <= 0;
146
        else if (end_of_file_enable)
147
                eof_data_ready <= (extra_bits_eof_1 > 63);
148
        else if (eof_bits_1)
149
                eof_data_ready <= 0;
150
end
151
 
152
always @(posedge clk)
153
begin
154
        if (rst) begin
155
                eof_bits_1 <= 0; eof_bits_2 <= 0; eof_bits_3 <= 0;
156
        end
157
        else begin
158
                eof_bits_1 <= end_of_file_enable;
159
                eof_bits_2 <= eof_bits_1;
160
                eof_bits_3 <= eof_bits_2;
161
        end
162
end
163
 
164
always @(posedge clk)
165
begin
166
        if (rst) begin
167
                JPEG_bitstream_eof <= 0; JPEG_eof_ro <= 0;
168
        end
169
        else if (end_of_file_enable) begin
170
                JPEG_bitstream_eof <= JPEG_eof_7[79:48];
171
                JPEG_eof_ro <= JPEG_eof_7[47:16];
172
        end
173
        else if (eof_bits_1 | eof_bits_2) begin
174
                JPEG_bitstream_eof <= JPEG_eof_ro;
175
                JPEG_eof_ro <= { JPEG_eof_ro_ro, {16{1'b0}} };
176
        end
177
end
178
 
179
always @(posedge clk)
180
begin
181
        if (rst)
182
                JPEG_eof_ro_ro <= 0;
183
        else if (end_of_file_enable)
184
                JPEG_eof_ro_ro <= JPEG_eof_7[15:0];
185
end
186
 
187
always @(posedge clk)
188
begin // These registers combine the previous leftover bits with
189
// the end of file bits
190
        if (rst) begin
191
                JPEG_eof_7 <= 0; JPEG_eof_6 <= 0; JPEG_eof_5_1 <= 0;
192
                FF_eof_shift <= 0; FF_count_1 <= 0;
193
        end
194
        else begin
195
                JPEG_eof_7[79:72] <= (FF_count_1 > 0) ? JPEG_ro[31:24] : JPEG_eof_6[79:72];
196
                JPEG_eof_7[71:64] <= (FF_count_1 > 1) ? JPEG_ro[23:16] : JPEG_eof_6[71:64];
197
                JPEG_eof_7[63:56] <= (FF_count_1 > 2) ? JPEG_ro[15:8] : JPEG_eof_6[63:56];
198
                JPEG_eof_7[55:0] <= JPEG_eof_6[55:0];
199
                JPEG_eof_6 <= JPEG_eof_5_1 << { FF_eof_shift[1], 4'b0000 };
200
                JPEG_eof_5_1 <= JPEG_eof_5 << { FF_eof_shift[0], 3'b000 };
201
                FF_eof_shift <= 2'b11 - FF_count;
202
                FF_count_1 <= FF_count;
203
        end
204
end
205
 
206
 
207
always @(posedge clk)
208
begin // These registers generate the end of file bits
209
        if (rst) begin
210
                orc_reg <= 0; extra_bits_eof <= 0;
211
                extra_bits_eof_1 <= 0; count_total_eof <= 0;
212
                JPEG_eof_5 <= 0; fifth_2bytes_eof <= 0;
213
                JPEG_eof_4 <= 0; fourth_2bytes_eof <= 0;
214
                JPEG_eof_3 <= 0; third_2bytes_eof <= 0;
215
                JPEG_eof_2 <= 0;
216
                second_2bytes_eof <= 0; JPEG_eof_1 <= 0;
217
                first_2bytes_eof <= 0; s2b <= 0;
218
                t2b <= 0; orc_input <= 0;
219
                end
220
        else begin
221
                orc_reg <= extra_bits_eof_1[4:0];
222
                extra_bits_eof <= { 2'b00, orc_input } + { 2'b00, FF_count, 3'b000 };
223
                extra_bits_eof_1 <= extra_bits_eof + { 2'b00, count_total_eof, 3'b000 };
224
                count_total_eof <= first_2bytes_eof + s2b + t2b;
225
                JPEG_eof_5[55:16] <= JPEG_eof_4[55:16];
226
                JPEG_eof_5[15:8] <= fifth_2bytes_eof ? 8'b00000000 : JPEG_eof_4[15:8];
227
                JPEG_eof_5[7:0] <= fifth_2bytes_eof ? JPEG_eof_4[15:8] : JPEG_eof_4[7:0];
228
                fifth_2bytes_eof <= (JPEG_eof_4[23:16] == 8'b11111111);
229
                JPEG_eof_4[55:24] <= JPEG_eof_3[55:24];
230
                JPEG_eof_4[23:16] <= fourth_2bytes_eof ? 8'b00000000 : JPEG_eof_3[23:16];
231
                JPEG_eof_4[15:8] <= fourth_2bytes_eof ? JPEG_eof_3[23:16] : JPEG_eof_3[15:8];
232
                JPEG_eof_4[7:0] <= fourth_2bytes_eof ? JPEG_eof_3[15:8] : JPEG_eof_3[7:0];
233
                fourth_2bytes_eof <= (JPEG_eof_3[31:24] == 8'b11111111);
234
                JPEG_eof_3[55:32] <= JPEG_eof_2[47:24];
235
                JPEG_eof_3[31:24] <= third_2bytes_eof ? 8'b00000000 : JPEG_eof_2[23:16];
236
                JPEG_eof_3[23:16] <= third_2bytes_eof ? JPEG_eof_2[23:16] : JPEG_eof_2[15:8];
237
                JPEG_eof_3[15:8] <= third_2bytes_eof ? JPEG_eof_2[15:8] : JPEG_eof_2[7:0];
238
                JPEG_eof_3[7:0] <= third_2bytes_eof ? JPEG_eof_2[7:0] : 8'b00000000;
239
                third_2bytes_eof <= (JPEG_eof_2[31:24] == 8'b11111111);
240
                JPEG_eof_2[47:32] <= JPEG_eof_1[39:24];
241
                JPEG_eof_2[31:24] <= second_2bytes_eof ? 8'b00000000 : JPEG_eof_1[23:16];
242
                JPEG_eof_2[23:16] <= second_2bytes_eof ? JPEG_eof_1[23:16] : JPEG_eof_1[15:8];
243
                JPEG_eof_2[15:8] <= second_2bytes_eof ? JPEG_eof_1[15:8] : JPEG_eof_1[7:0];
244
                JPEG_eof_2[7:0] <= second_2bytes_eof ? JPEG_eof_1[7:0] : 8'b00000000;
245
                second_2bytes_eof <= (JPEG_eof_1[31:24] == 8'b11111111);
246
                JPEG_eof_1[39:32] <= JPEG_eof[31:24];
247
                JPEG_eof_1[31:24] <= first_2bytes_eof ? 8'b00000000 : JPEG_eof[23:16];
248
                JPEG_eof_1[23:16] <= first_2bytes_eof ? JPEG_eof[23:16] : JPEG_eof[15:8];
249
                JPEG_eof_1[15:8] <= first_2bytes_eof ? JPEG_eof[15:8] : JPEG_eof[7:0];
250
                JPEG_eof_1[7:0] <= first_2bytes_eof ? JPEG_eof[7:0] : 8'b00000000;
251
                first_2bytes_eof <= (JPEG_eof[31:24] == 8'b11111111);
252
                s2b <= (JPEG_eof[23:16] == 8'b11111111);
253
                t2b <= (JPEG_eof[15:8] == 8'b11111111);
254
                orc_input <= orc_reg_in;
255
                end
256
end
257
 
258
always @(posedge clk)
259
begin
260
        if (rst) begin
261
                JPEG_eof <= 0;
262
                end
263
        else begin
264
                JPEG_eof[31] <= (orc_reg_in > 5'b00000) ? JPEG_in[31] : 1'b0;
265
                JPEG_eof[30] <= (orc_reg_in > 5'b00001) ? JPEG_in[30] : 1'b0;
266
                JPEG_eof[29] <= (orc_reg_in > 5'b00010) ? JPEG_in[29] : 1'b0;
267
                JPEG_eof[28] <= (orc_reg_in > 5'b00011) ? JPEG_in[28] : 1'b0;
268
                JPEG_eof[27] <= (orc_reg_in > 5'b00100) ? JPEG_in[27] : 1'b0;
269
                JPEG_eof[26] <= (orc_reg_in > 5'b00101) ? JPEG_in[26] : 1'b0;
270
                JPEG_eof[25] <= (orc_reg_in > 5'b00110) ? JPEG_in[25] : 1'b0;
271
                JPEG_eof[24] <= (orc_reg_in > 5'b00111) ? JPEG_in[24] : 1'b0;
272
                JPEG_eof[23] <= (orc_reg_in > 5'b01000) ? JPEG_in[23] : 1'b0;
273
                JPEG_eof[22] <= (orc_reg_in > 5'b01001) ? JPEG_in[22] : 1'b0;
274
                JPEG_eof[21] <= (orc_reg_in > 5'b01010) ? JPEG_in[21] : 1'b0;
275
                JPEG_eof[20] <= (orc_reg_in > 5'b01011) ? JPEG_in[20] : 1'b0;
276
                JPEG_eof[19] <= (orc_reg_in > 5'b01100) ? JPEG_in[19] : 1'b0;
277
                JPEG_eof[18] <= (orc_reg_in > 5'b01101) ? JPEG_in[18] : 1'b0;
278
                JPEG_eof[17] <= (orc_reg_in > 5'b01110) ? JPEG_in[17] : 1'b0;
279
                JPEG_eof[16] <= (orc_reg_in > 5'b01111) ? JPEG_in[16] : 1'b0;
280
                JPEG_eof[15] <= (orc_reg_in > 5'b10000) ? JPEG_in[15] : 1'b0;
281
                JPEG_eof[14] <= (orc_reg_in > 5'b10001) ? JPEG_in[14] : 1'b0;
282
                JPEG_eof[13] <= (orc_reg_in > 5'b10010) ? JPEG_in[13] : 1'b0;
283
                JPEG_eof[12] <= (orc_reg_in > 5'b10011) ? JPEG_in[12] : 1'b0;
284
                JPEG_eof[11] <= (orc_reg_in > 5'b10100) ? JPEG_in[11] : 1'b0;
285
                JPEG_eof[10] <= (orc_reg_in > 5'b10101) ? JPEG_in[10] : 1'b0;
286
                JPEG_eof[9] <= (orc_reg_in > 5'b10110) ? JPEG_in[9] : 1'b0;
287
                JPEG_eof[8] <= (orc_reg_in > 5'b10111) ? JPEG_in[8] : 1'b0;
288
                JPEG_eof[7] <= (orc_reg_in > 5'b11000) ? JPEG_in[7] : 1'b0;
289
                JPEG_eof[6] <= (orc_reg_in > 5'b11001) ? JPEG_in[6] : 1'b0;
290
                JPEG_eof[5] <= (orc_reg_in > 5'b11010) ? JPEG_in[5] : 1'b0;
291
                JPEG_eof[4] <= (orc_reg_in > 5'b11011) ? JPEG_in[4] : 1'b0;
292
                JPEG_eof[3] <= (orc_reg_in > 5'b11100) ? JPEG_in[3] : 1'b0;
293
                JPEG_eof[2] <= (orc_reg_in > 5'b11101) ? JPEG_in[2] : 1'b0;
294
                JPEG_eof[1] <= (orc_reg_in > 5'b11110) ? JPEG_in[1] : 1'b0;
295
                JPEG_eof[0] <= 1'b0;
296
                end
297
end
298
 
299
always @(posedge clk)
300
begin
301
        if (rst)
302
                eof_count_enable <= 0;
303
        else if (end_of_file_enable_hold)
304
                eof_count_enable <= 0;
305
        else if (end_of_file_signal)
306
                eof_count_enable <= 1;
307
end
308
 
309
always @(posedge clk)
310
begin
311
        if (rst)
312
                eof_count <= 0;
313
        else if (!eof_count_enable)
314
                eof_count <= 0;
315
        else if (eof_count_enable)
316
                eof_count <= eof_count + 1;
317
end
318
 
319
always @(posedge clk)
320
begin
321
        if (rst)
322
                end_of_file_enable <= 0;
323
        else if (eof_count != 9'b011110000)
324
                end_of_file_enable <= 0;
325
        else if (eof_count == 9'b011110000)
326
                end_of_file_enable <= 1;
327
end
328
 
329
always @(posedge clk)
330
begin
331
        if (rst)
332
                end_of_file_enable_hold <= 0;
333
        else if (end_of_file_enable)
334
                end_of_file_enable_hold <= 1;
335
end
336
 
337
// This ends the section dealing with the end of file.
338
 
339
always @(posedge clk)
340
begin
341
        if (rst) begin
342
                data_ready_1 <= 0; JPEG_bitstream_1 <= 0;
343
                end
344
        else begin
345
                data_ready_1 <= data_ready || eof_data_ready_1;
346
                JPEG_bitstream_1 <= (eof_bits_1 || eof_bits_2 || eof_bits_3) ?
347
                                                        JPEG_bitstream_eof : JPEG_bitstream;
348
                end
349
end
350
 
351
always @(posedge clk)
352
begin
353
        if (rst) begin
354
                data_ready <= 0; rdv_1 <= 0; rpf_1 <= 0;
355
                end
356
        else begin
357
                data_ready <= rdv_1 || rpf_1;
358
                rdv_1 <= rdata_valid;
359
                rpf_1 <= rollover_pf & !rpf_1; // there can't be 2 rollover's in a row
360
                // because after the first rollover, the next fifo entry is dummy data
361
                end
362
end
363
 
364
always @(posedge clk)
365
begin
366
        if (rst)
367
                JPEG_bitstream[31:24] <= 0;
368
        else if (rdv_1 && ffc_postfifo == 0 && !rpf_1)
369
                JPEG_bitstream[31:24] <= JPEG_pf[87:80];
370
        else if (rpf_1 || (rdv_1 &&  ffc_postfifo > 0))
371
                JPEG_bitstream[31:24] <= JPEG_ro[31:24];
372
end
373
 
374
always @(posedge clk)
375
begin
376
        if (rst)
377
                JPEG_bitstream[23:16] <= 0;
378
        else if (rdv_1 && ffc_postfifo < 2 && !rpf_1)
379
                JPEG_bitstream[23:16] <= JPEG_pf[79:72];
380
        else if (rpf_1 || (rdv_1 && ffc_postfifo > 1))
381
                JPEG_bitstream[23:16] <= JPEG_ro[23:16];
382
end
383
 
384
always @(posedge clk)
385
begin
386
        if (rst)
387
                JPEG_bitstream[15:8] <= 0;
388
        else if (rdv_1 && ffc_postfifo < 3 && !rpf_1)
389
                JPEG_bitstream[15:8] <= JPEG_pf[71:64];
390
        else if (rpf_1 || (rdv_1 && ffc_postfifo == 3))
391
                JPEG_bitstream[15:8] <= JPEG_ro[15:8];
392
end
393
 
394
always @(posedge clk)
395
begin
396
        if (rst)
397
                JPEG_bitstream[7:0] <= 0;
398
        else if (rdv_1 && !rpf_1)
399
                JPEG_bitstream[7:0] <= JPEG_pf[63:56];
400
        else if (rpf_1)
401
                JPEG_bitstream[7:0] <= JPEG_ro[7:0];
402
end
403
 
404
always @(posedge clk)
405
begin
406
        if (rst)
407
                JPEG_ro <= 0;
408
        else if (rdv_1 && !rpf_1)
409
                JPEG_ro <= JPEG_pf[55:24];
410
        else if (rpf_1)
411
                JPEG_ro[31:8] <= JPEG_ro_ro;
412
end
413
 
414
always @(posedge clk)
415
begin
416
        if (rst) begin
417
                JPEG_ro_ro <= 0;
418
                end
419
        else if (rdv_1) begin
420
                JPEG_ro_ro <= JPEG_pf[23:0];
421
                end
422
end
423
 
424
 
425
always @(posedge clk)
426
begin
427
        if (fifo_empty)
428
                read_req <= 0;
429
        else if (!fifo_empty)
430
                read_req <= 1;
431
end
432
 
433
always @(posedge clk)
434
begin
435
        if (rst)
436
                rollover_pf <= 0;
437
        else if (!rdata_valid)
438
                rollover_pf <= 0;
439
        else if (rdata_valid)
440
                rollover_pf <= read_data[0];
441
end
442
 
443
always @(posedge clk)
444
begin
445
        if (rst) begin
446
                JPEG_pf <= 0; ffc_postfifo <= 0;
447
                end
448
        else if (rdata_valid) begin
449
                JPEG_pf <= read_data[90:3];
450
                ffc_postfifo <= read_data[2:1];
451
                // ffc_postfifo is the current count of how many FF's there have
452
                // been in the bitstream.  This determines how the bitstream from the
453
                // FIFO is adjusted as it is put into the output bitstream.
454
                end
455
end
456
 
457
always @(posedge clk)
458
begin
459
        if (!dr_in_8)
460
                write_enable <= 0;
461
        else if (dr_in_8)
462
                write_enable <= 1;
463
                // write_enable is the write enable to the FIFO
464
end
465
 
466
always @(posedge clk)
467
begin
468
        if (rst) begin
469
                JPEG_out_1 <= 0; ffc_7 <= 0;
470
                end
471
        else if (dr_in_8) begin
472
                JPEG_out_1 <= ffc_6[0] ? JPEG_out : JPEG_out << 8;
473
                ffc_7 <= ffc_6;
474
                end
475
end
476
 
477
always @(posedge clk)
478
begin
479
        if (rst) begin
480
                JPEG_out <= 0; ffc_6 <= 0;
481
                end
482
        else if (dr_in_7) begin
483
                JPEG_out <= ffc_5[1] ? JPEG_7 : JPEG_7 << 16;
484
                ffc_6 <= ffc_5;
485
                end
486
end
487
 
488
always @(posedge clk)
489
begin
490
        if (rst) begin
491
                JPEG_7 <= 0; ffc_5 <= 0;
492
                end
493
        else if (dr_in_6) begin
494
                JPEG_7[63:16] <= JPEG_6[63:16];
495
                JPEG_7[15:8] <= (JPEG_6[23:16] == 8'b11111111) ? 8'b00000000 : JPEG_6[15:8];
496
                JPEG_7[7:0] <= (JPEG_6[23:16] == 8'b11111111) ? JPEG_6[15:8] : JPEG_6[7:0];
497
                ffc_5 <= ffc_4;
498
                end
499
end
500
 
501
always @(posedge clk)
502
begin
503
        if (rst) begin
504
                JPEG_6 <= 0; ffc_4 <= 0;
505
                end
506
        else if (dr_in_5) begin
507
                JPEG_6[63:24] <= JPEG_5[63:24];
508
                JPEG_6[23:16] <= (JPEG_5[31:24] == 8'b11111111) ? 8'b00000000 : JPEG_5[23:16];
509
                JPEG_6[15:8] <= (JPEG_5[31:24] == 8'b11111111) ? JPEG_5[23:16] : JPEG_5[15:8];
510
                JPEG_6[7:0] <= (JPEG_5[31:24] == 8'b11111111) ? JPEG_5[15:8] : JPEG_5[7:0];
511
                ffc_4 <= ffc_3;
512
                end
513
end
514
 
515
always @(posedge clk)
516
begin
517
        if (rst) begin
518
                JPEG_5 <= 0; ffc_3 <= 0;
519
                end
520
        else if (dr_in_4) begin
521
                JPEG_5[63:32] <= JPEG_4[55:24];
522
                JPEG_5[31:24] <= (JPEG_4[31:24] == 8'b11111111) ? 8'b00000000 : JPEG_4[23:16];
523
                JPEG_5[23:16] <= (JPEG_4[31:24] == 8'b11111111) ? JPEG_4[23:16] : JPEG_4[15:8];
524
                JPEG_5[15:8] <= (JPEG_4[31:24] == 8'b11111111) ? JPEG_4[15:8] : JPEG_4[7:0];
525
                JPEG_5[7:0] <= (JPEG_4[31:24] == 8'b11111111) ? JPEG_4[7:0] : 8'b00000000;
526
                ffc_3 <= ffc_2;
527
                end
528
end
529
 
530
always @(posedge clk)
531
begin
532
        if (rst) begin
533
                JPEG_4 <= 0; ffc_2 <= 0;
534
                end
535
        else if (dr_in_3) begin
536
                JPEG_4[55:32] <= JPEG_3[47:24];
537
                JPEG_4[31:24] <= (JPEG_3[31:24] == 8'b11111111) ? 8'b00000000 : JPEG_3[23:16];
538
                JPEG_4[23:16] <= (JPEG_3[31:24] == 8'b11111111) ? JPEG_3[23:16] : JPEG_3[15:8];
539
                JPEG_4[15:8] <= (JPEG_3[31:24] == 8'b11111111) ? JPEG_3[15:8] : JPEG_3[7:0];
540
                JPEG_4[7:0] <= (JPEG_3[31:24] == 8'b11111111) ? JPEG_3[7:0] : 8'b00000000;
541
                ffc_2 <= ffc_1;
542
                end
543
end
544
 
545
always @(posedge clk)
546
begin
547
        if (rst) begin
548
                JPEG_3 <= 0; ct_1 <= 0; FF_count <= 0;
549
                ffc_1 <= 0;
550
                end
551
        else if (dr_in_2) begin
552
                JPEG_3[47:32] <= JPEG_2[39:24];
553
                JPEG_3[31:24] <= (JPEG_2[31:24] == 8'b11111111) ? 8'b00000000 : JPEG_2[23:16];
554
                JPEG_3[23:16] <= (JPEG_2[31:24] == 8'b11111111) ? JPEG_2[23:16] : JPEG_2[15:8];
555
                JPEG_3[15:8] <= (JPEG_2[31:24] == 8'b11111111) ? JPEG_2[15:8] : JPEG_2[7:0];
556
                JPEG_3[7:0] <= (JPEG_2[31:24] == 8'b11111111) ? JPEG_2[7:0] : 8'b00000000;
557
                ct_1 <= count_total;
558
                FF_count <= FF_count + count_total;
559
                ffc_1 <= FF_count;
560
                end
561
end
562
 
563
always @(posedge clk)
564
begin
565
        if (rst) begin
566
                JPEG_2 <= 0; count_total <= 0;
567
                end
568
        else if (dr_in_1) begin
569
                JPEG_2[39:32] <= JPEG_1[31:24];
570
                JPEG_2[31:24] <= first_2bytes ? 8'b00000000 : JPEG_1[23:16];
571
                JPEG_2[23:16] <= first_2bytes ? JPEG_1[23:16] : JPEG_1[15:8];
572
                JPEG_2[15:8] <= first_2bytes ? JPEG_1[15:8] : JPEG_1[7:0];
573
                JPEG_2[7:0] <= first_2bytes ? JPEG_1[7:0] : 8'b00000000;
574
                count_total <= first_2bytes + second_2bytes + third_2bytes + fourth_2bytes;
575
                end
576
end
577
 
578
always @(posedge clk)
579
begin
580
        if (rst) begin
581
                first_2bytes <= 0; second_2bytes <= 0;
582
                third_2bytes <= 0; fourth_2bytes <= 0;
583
                JPEG_1 <= 0;
584
                end
585
        else if (data_ready_in) begin
586
                first_2bytes <= JPEG_in[31:24] == 8'b11111111;
587
                second_2bytes <= JPEG_in[23:16] == 8'b11111111;
588
                third_2bytes <= JPEG_in[15:8] == 8'b11111111;
589
                fourth_2bytes <= JPEG_in[7:0] == 8'b11111111;
590
                JPEG_1 <= JPEG_in;
591
                end
592
end
593
 
594
always @(posedge clk)
595
begin
596
        if (rst) begin
597
                rollover_1 <= 0; rollover_2 <= 0; rollover_3 <= 0;
598
                rollover_4 <= 0; rollover_5 <= 0;
599
                end
600
        else begin
601
                rollover_1 <= rollover; rollover_2 <= rollover_1;
602
                rollover_3 <= rollover_2; rollover_4 <= rollover_3;
603
                rollover_5 <= rollover_4;
604
                end
605
end
606
 
607
always @(posedge clk)
608
begin
609
        if (rst)
610
                rollover <= 0;
611
        else if (!dr_in_3)
612
                rollover <= 0;
613
        else if (dr_in_3)
614
                rollover <= (FF_count < ffc_1) | (ct_1 == 3'b100);
615
        // A rollover occurs whenever the next count is less than the current count.
616
        // FF_count is the next count of how many FF's have been found in the bitstream
617
        // The count rolls over at 4.  A rollover could also occur if all 32 bits of the
618
        // are FF's, which would be 4 FF's in the bitstream.  This is the condition where
619
        // ct_1 == 3'b100.  This is highly unlikely, almost impossible, but it could 
620
        // theoretically happen.
621
        // In that case, the next count would equal the current count, and by 
622
        // comparing the two, you wouldn't get a rollover condition, so you need the extra
623
        // check of the condition ct_1 == 3'b100.
624
end
625
 
626
always @(posedge clk)
627
begin
628
        if (rst) begin
629
                dr_in_1 <= 0; dr_in_2 <= 0; dr_in_3 <= 0; dr_in_4 <= 0;
630
                dr_in_5 <= 0; dr_in_6 <= 0; dr_in_7 <= 0; dr_in_8 <= 0;
631
                end
632
        else begin
633
                dr_in_1 <= data_ready_in;
634
                dr_in_2 <= dr_in_1;
635
                dr_in_3 <= dr_in_2;
636
                dr_in_4 <= dr_in_3;
637
                dr_in_5 <= dr_in_4;
638
                dr_in_6 <= dr_in_5;
639
                dr_in_7 <= dr_in_6;
640
                dr_in_8 <= dr_in_7;
641
                end
642
end
643
 
644
endmodule

powered by: WebSVN 2.1.0

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