x393  1.0
FPGAcodeforElphelNC393camera
cmprs_cmd_decode.v
Go to the documentation of this file.
1 
39 `timescale 1ns/1ps
40 //From 353:
41 // [23] == 1 - set focus mode
42 // [22:21] 0 - none
43 // 1 - replace
44 // 2 - combine for all image
45 // 3 - combine in window only
46 // [20] == 1 - set Bayer shift
47 // [19:18] Bayer shift
48 // [17] == 1 - set compressor shift
49 // [16:14] compressor shift
50 // [13]==1 - enable color modes
51 // [12:9]== 0 - color, 4:2:0
52 // 1 - monochrome, 6/4 blocks (as 4:2:0)
53 // 2 - jp4, 6 blocks, original
54 // 3 - jp4, 6 blocks, dc -improved
55 // 4 - mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
56 // 5 - jp4, 4 blocks, dc-improved
57 // 6 - jp4, differential
58 // 7 - 15 - reserved
59 // [8:7] == 0,1 - NOP, 2 - disable, 3 - enable subtracting of average value (DC component), bypassing DCT
60 // [6] == 1 - enable quantization bank select, 0 - disregard bits [5:3]
61 // [5:3] = quantization page number (0..7)
62 // [2]== 1 - enable on/off control:
63 // [1:0]== 0 - reset compressor, stop immediately
64 // 1 - enable compressor, disable repetitive mode
65 // 2 - enable compressor, compress single frame
66 // 3 - enable compressor, enable repetitive mode
67 //
68 //Modified for 393:
69 // [23] == 1 - set focus mode
70 // [22:21] 0 - none
71 // 1 - replace
72 // 2 - combine for all image
73 // 3 - combine in window only
74 // [20] == 1 - set Bayer shift
75 // [19:18] Bayer shift
76 // [17:16] - unused
77 // [15] == 1 - set single/multi frame mode
78 // [14] 0 - multiframe (compare frame numbers for 'suspend' output)
79 // 1 - single frame buffer
80 // [13]== 1 - enable color modes
81 // [12:9]== 0 - color, 4:2:0
82 // 1 - monochrome, 6/4 blocks (as 4:2:0)
83 // 2 - jp4, 6 blocks, original
84 // 3 - jp4, 6 blocks, dc -improved
85 // 4 - mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
86 // 5 - jp4, 4 blocks, dc-improved
87 // 6 - jp4, differential
88 // 7 - 15 - reserved
89 // [8:7] == 0,1 - NOP, 2 - disable, 3 - enable subtracting of average value (DC component), bypassing DCT
90 // [6] == 1 - enable quantization bank select, 0 - disregard bits [5:3]
91 // [5:3] = quantization page number (0..7)
92 // [2]== 1 - enable compressor on/off control:
93 // [1:0]== 0 - reset compressor, stop immediately
94 // 1 - disable compression of the new frames, finish any already started
95 // 2 - enable compressor, compress single frame from memory (async)
96 // 3 - enable compressor, enable synchronous compression mode
97 
98 
99 
101  // Bit-fields in compressor control word
102  parameter CMPRS_CBIT_RUN = 2, // bit # to control compressor run modes
103  parameter CMPRS_CBIT_RUN_BITS = 2, // number of bits to control compressor run modes
104  parameter CMPRS_CBIT_QBANK = 6, // bit # to control quantization table page
105  parameter CMPRS_CBIT_QBANK_BITS = 3, // number of bits to control quantization table page
106  parameter CMPRS_CBIT_DCSUB = 8, // bit # to control extracting DC components bypassing DCT
107  parameter CMPRS_CBIT_DCSUB_BITS = 1, // bit # to control extracting DC components bypassing DCT
108  parameter CMPRS_CBIT_CMODE = 13, // bit # to control compressor color modes
109  parameter CMPRS_CBIT_CMODE_BITS = 4, // number of bits to control compressor color modes
110  parameter CMPRS_CBIT_FRAMES = 15, // bit # to control compressor multi/single frame buffer modes
111  parameter CMPRS_CBIT_FRAMES_BITS = 1, // number of bits to control compressor multi/single frame buffer modes
112  parameter CMPRS_CBIT_BAYER = 20, // bit # to control compressor Bayer shift mode
113  parameter CMPRS_CBIT_BAYER_BITS = 2, // number of bits to control compressor Bayer shift mode
114  parameter CMPRS_CBIT_FOCUS = 23, // bit # to control compressor focus display mode
115  parameter CMPRS_CBIT_FOCUS_BITS = 2, // number of bits to control compressor focus display mode
116  // compressor bit-fields decode
117  parameter CMPRS_CBIT_RUN_RST = 2'h0, // reset compressor, stop immediately
118 // parameter CMPRS_CBIT_RUN_DISABLE = 2'h1, // disable compression of the new frames, finish any already started
119  parameter CMPRS_CBIT_RUN_STANDALONE = 2'h2, // enable compressor, compress single frame from memory (async)
120  parameter CMPRS_CBIT_RUN_ENABLE = 2'h3, // enable compressor, enable synchronous compression mode
121  parameter CMPRS_CBIT_CMODE_JPEG18 = 4'h0, // color 4:2:0
122  parameter CMPRS_CBIT_CMODE_MONO6 = 4'h1, // mono 4:2:0 (6 blocks)
123  parameter CMPRS_CBIT_CMODE_JP46 = 4'h2, // jp4, 6 blocks, original
124  parameter CMPRS_CBIT_CMODE_JP46DC = 4'h3, // jp4, 6 blocks, dc -improved
125  parameter CMPRS_CBIT_CMODE_JPEG20 = 4'h4, // mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
126  parameter CMPRS_CBIT_CMODE_JP4 = 4'h5, // jp4, 4 blocks, dc-improved
127  parameter CMPRS_CBIT_CMODE_JP4DC = 4'h6, // jp4, 4 blocks, dc-improved
128  parameter CMPRS_CBIT_CMODE_JP4DIFF = 4'h7, // jp4, 4 blocks, differential
129  parameter CMPRS_CBIT_CMODE_JP4DIFFHDR = 4'h8, // jp4, 4 blocks, differential, hdr
130  parameter CMPRS_CBIT_CMODE_JP4DIFFDIV2 = 4'h9, // jp4, 4 blocks, differential, divide by 2
131  parameter CMPRS_CBIT_CMODE_JP4DIFFHDRDIV2 = 4'ha, // jp4, 4 blocks, differential, hdr,divide by 2
132  parameter CMPRS_CBIT_CMODE_MONO1 = 4'hb, // mono JPEG (not yet implemented)
133  parameter CMPRS_CBIT_CMODE_MONO4 = 4'he, // mono 4 blocks
134  parameter CMPRS_CBIT_FRAMES_SINGLE = 0, //1, // use a single-frame buffer for images
135 
136  parameter CMPRS_COLOR18 = 0, // JPEG 4:2:0 with 18x18 overlapping tiles for de-bayer
137  parameter CMPRS_COLOR20 = 1, // JPEG 4:2:0 with 18x18 overlapping tiles for de-bayer (not implemented)
138  parameter CMPRS_MONO16 = 2, // JPEG 4:2:0 with 16x16 non-overlapping tiles, color components zeroed
139  parameter CMPRS_JP4 = 3, // JP4 mode with 16x16 macroblocks
140  parameter CMPRS_JP4DIFF = 4, // JP4DIFF mode TODO: see if correct
141  parameter CMPRS_MONO8 = 7, // Regular JPEG monochrome with 8x8 macroblocks (not yet implemented)
142 
143  parameter CMPRS_FRMT_MBCM1 = 0, // bit # of number of macroblock columns minus 1 field in format word
144  parameter CMPRS_FRMT_MBCM1_BITS = 13, // number of bits in number of macroblock columns minus 1 field in format word
145  parameter CMPRS_FRMT_MBRM1 = 13, // bit # of number of macroblock rows minus 1 field in format word
146  parameter CMPRS_FRMT_MBRM1_BITS = 13, // number of bits in number of macroblock rows minus 1 field in format word
147  parameter CMPRS_FRMT_LMARG = 26, // bit # of left margin field in format word
148  parameter CMPRS_FRMT_LMARG_BITS = 5, // number of bits in left margin field in format word
149  parameter CMPRS_CSAT_CB = 0, // bit # of number of blue scale field in color saturation word
150  parameter CMPRS_CSAT_CB_BITS = 10, // number of bits in blue scale field in color saturation word
151  parameter CMPRS_CSAT_CR = 12, // bit # of number of red scale field in color saturation word
152  parameter CMPRS_CSAT_CR_BITS = 10, // number of bits in red scale field in color saturation word
153  parameter CMPRS_CORING_BITS = 3 // number of bits in coring mode
154 
155  //parameter CMPRS_STUFFER_NEG = 1 // stuffer runs @ negedge xclk2x
156 
157 
158 )(
159 // input rst,
160  input xclk, // global clock input, compressor single clock rate
161  input mclk, // global system/memory clock
162  input mrst, // @posedge mclk, sync reset
163  input ctrl_we, // input - @mclk control register write enable
164  input format_we, // input - @mclk write number of tiles and left margin
165  input color_sat_we, // input - @mclk write color saturation values
166  input coring_we, // input - @mclk write coring values
167 
168 // rs, // 0 - bit modes,
169 // // 1 - write ntiles;
170  input [31:0] di, // [15:0] data from CPU (sync to negedge sclk)
171 // cr_w, // data written to cr (1 cycle long) - just to reset legacy IRQ
172 // ntiles,//[17:0] - number of tiles in a frame to process
173  input frame_start, // @mclk
174  output frame_start_xclk, // frame start, parameters are copied at this pulse
175 
176  // outputs sync @ posedge mclk:
177  output cmprs_en_mclk, // @mclk 0 resets immediately
178  input cmprs_en_extend, // @mclk keep compressor enabled for graceful shutdown
179 
180  output reg cmprs_run_mclk, // @mclk enable propagation of vsync_late to frame_start_dst in bonded(sync to src) mode
181  output reg cmprs_standalone, // @mclk single-cycle: generate a single frame_start_dst in unbonded (not synchronized) mode.
182  // cmprs_run should be off
183  output reg sigle_frame_buf, // memory controller uses a single frame buffer (frame_number_* == 0), use other sync
184  // outputs sync @ posedge xclk:
185  output reg cmprs_en_xclk, // enable compressor, turn off immedaitely
186  output reg cmprs_en_late_xclk, // enable stuffer, extends control fields for graceful shutdown
187 // cmprs_start, // single cycle when single or constant compression is turned on
188 // cmprs_repeat,// high in repetitive mode
189  // outputs @posedge xclk, frozen when the new frame is requested
190  output reg [ 2:0] cmprs_qpage, // [2:0] - quantizator page number (0..7)
191  output reg cmprs_dcsub, // subtract dc level before DCT, restore later
192 // output reg [ 3:0] cmprs_mode, // [3:0] - compressor mode
193 // cmprs_shift, // tile shift from top left corner
194  output reg [ 1:0] cmprs_fmode, //[1:0] - focus mode
195  output reg [ 1:0] bayer_shift, // additional shift to bayer mosaic
196 
197  output reg ignore_color,
198  output reg four_blocks,
199  output reg jp4_dc_improved,
200  output reg [ 2:0] converter_type, // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
201  output reg scale_diff,
202  output reg hdr,
203 
204  output reg [CMPRS_FRMT_LMARG_BITS-1:0] left_marg, // left margin (for not-yet-implemented) mono JPEG (8 lines tile row) can need 7 bits (mod 32 - tile)
205  output reg [CMPRS_FRMT_MBCM1_BITS-1:0] n_blocks_in_row_m1, // number of macroblocks in a macroblock row minus 1
206  output reg [CMPRS_FRMT_MBRM1_BITS-1:0] n_block_rows_m1, // number of macroblock rows in a frame minus 1
207 
208  output reg [CMPRS_CSAT_CB_BITS-1:0] color_sat_cb, // scale for Cb color component (color saturation)
209  output reg [CMPRS_CSAT_CR_BITS-1:0] color_sat_cr, // scale for Cr color component (color saturation)
210 
211  output reg [CMPRS_CORING_BITS-1:0] coring // scale for Cb color component (color saturation)
212 
213  );
214 
215 // input is_compressing, // high from start of compressing till EOT (sync to posedge clk)
216 // abort_compress,
217  // input stuffer_done_mclk,
218 
219 // output reg force_flush); // abort compress - generate flush pulse, force end of image over DMA, update counter
220 
221  reg [30:0] di_r;
223 
227 
228 
230  wire ctrl_we_xclk; // single xclk pulse after ctrl_we_r (or use just ctrl_we, not ctrl_we_r)
231  wire format_we_xclk; // @xclk write number of tiles and left margin
232  wire color_sat_we_xclk; // @xclk write color saturation values
233  wire coring_we_xclk; // @xclk write coring values
234 
235 
236  reg [ 2:0] cmprs_qpage_mclk; // [2:0] - quantizator page number (0..7)
237  reg cmprs_dcsub_mclk; // subtract dc level before DCT, restore later
238  reg [ 3:0] cmprs_mode_mclk; // [3:0] - compressor mode
239  reg [ 1:0] cmprs_fmode_mclk; //[1:0] - focus mode
240  reg [ 1:0] bayer_shift_mclk; // additional shift to bayer mosaic
241 
242  reg [30:0] format_mclk; // left margin and macroblock rows/columns
243  reg [23:0] color_sat_mclk; // color saturation values (only 10 LSB in each 12 are used
244  reg [ 2:0] coring_mclk; // color saturation values (only 10 LSB in each 12 are used
245 
246  reg [ 2:0] cmprs_qpage_xclk; // [2:0] - quantizator page number (0..7)
247  reg cmprs_dcsub_xclk; // subtract dc level before DCT, restore later
248  reg [ 3:0] cmprs_mode_xclk; // [3:0] - compressor mode
249  reg [ 1:0] cmprs_fmode_xclk; //[1:0] - focus mode
250  reg [ 1:0] bayer_shift_xclk; // additional shift to bayer mosaic
251 
252  reg [30:0] format_xclk; // left margin and macroblock rows/columns
253  reg [23:0] color_sat_xclk; // color saturation values (only 10 LSB in each 12 are used
254  reg [ 2:0] coring_xclk; // color saturation values (only 10 LSB in each 12 are used
255 
256 // wire frame_start_xclk;
258 
259  always @ (posedge mclk) begin
260  if (mrst) ctrl_we_r <= 0;
261  else ctrl_we_r <= ctrl_we;
262 
263  if (mrst) format_we_r <= 0;
264  else format_we_r <= format_we;
265 
266  if (mrst) color_sat_we_r <= 0;
268 
269  if (mrst) coring_we_r <= 0;
270  else coring_we_r <= coring_we;
271 
272  if (mrst) di_r <= 0;
273  else if (ctrl_we || format_we || color_sat_we || coring_we) di_r <= di[30:0];
274 
275  if (mrst) cmprs_en_mclk_r <= 0;
277 
278  if (mrst) cmprs_run_mclk <= 0;
280 
281  if (mrst) cmprs_standalone <= 0;
282 // else if (ctrl_we_r) cmprs_standalone <= ctrl_we_r && di_r[CMPRS_CBIT_RUN] && (di_r[CMPRS_CBIT_RUN-1 -:CMPRS_CBIT_RUN_BITS] == CMPRS_CBIT_RUN_STANDALONE);
284 
285  if (mrst) sigle_frame_buf <= 0;
287 
288  if (mrst) cmprs_qpage_mclk <= 0;
290 
291  if (mrst) cmprs_dcsub_mclk <= 0;
293 
294  if (mrst) cmprs_mode_mclk <= 0;
296 
297  if (mrst) cmprs_fmode_mclk <= 0;
299 
300  if (mrst) bayer_shift_mclk <= 0;
302 
303 
304  if (mrst) format_mclk <= 0;
305  else if (format_we_r) format_mclk <= di_r[30:0];
306 
307  if (mrst) color_sat_mclk <= 'h0b6090; // 'h16c120 - saturation = 2
308  else if (color_sat_we_r) color_sat_mclk <= di_r[23:0];
309 
310  if (mrst) coring_mclk <= 0;
311  else if (coring_we_r) coring_mclk <= di_r[2:0];
312 
313  end
314 
315  // re-clock to compressor clock
316  always @ (posedge xclk) begin
319  end
320  always @ (posedge xclk) if (ctrl_we_xclk) begin
321 // cmprs_en_late_xclk <= cmprs_en_mclk_r || cmprs_en_extend;
327  end
328 
329  always @ (posedge xclk) begin
331  if (color_sat_we_xclk) color_sat_xclk <= color_sat_mclk; // SuppressThisWarning VivadoSynthesis
333  end
334 
335  always @ (posedge xclk) if (frame_start_xclk) begin
340 
344 
347 
348  coring <= coring_xclk;
349 
350 
351  // Will infer ROM
352  case (cmprs_mode_xclk[3:0])
353  CMPRS_CBIT_CMODE_MONO6: begin //monochrome, (4:2:0),
354  ignore_color <= 1;
355  four_blocks <= 0;
356  jp4_dc_improved <= 0;
357  converter_type[2:0] <= CMPRS_MONO16; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
358  scale_diff <=0;
359  hdr <=0;
360  end
361  CMPRS_CBIT_CMODE_JPEG18: begin //color, 4:2:0, 18x18(old)
362  ignore_color <= 0;
363  four_blocks <= 0;
364  jp4_dc_improved <= 0;
365  converter_type[2:0] <= CMPRS_COLOR18; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
366  scale_diff <=0;
367  hdr <=0;
368  end
369  CMPRS_CBIT_CMODE_JP46: begin // jp4, original (4:2:0),
370  ignore_color <= 1;
371  four_blocks <= 0;
372  jp4_dc_improved <= 0;
373  converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
374  scale_diff <=0;
375  hdr <=0;
376  end
377  CMPRS_CBIT_CMODE_JP46DC: begin // jp4, dc -improved (4:2:0),
378  ignore_color <= 1;
379  four_blocks <= 0;
380  jp4_dc_improved <= 1;
381  converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
382  scale_diff <=0;
383  hdr <=0;
384  end
385  CMPRS_CBIT_CMODE_JPEG20: begin // color, 4:2:0, 20x20, middle of the tile (not yet implemented)
386  ignore_color <= 0;
387  four_blocks <= 0;
388  jp4_dc_improved <= 0;
389  converter_type[2:0] <= CMPRS_COLOR20; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
390  scale_diff <=0;
391  hdr <=0;
392  end
393  CMPRS_CBIT_CMODE_JP4: begin // jp4, 4 blocks, (legacy)
394  ignore_color <= 1;
395  four_blocks <= 1;
396  jp4_dc_improved <= 0;
397  converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
398  scale_diff <=0;
399  hdr <=0;
400  end
401  CMPRS_CBIT_CMODE_JP4DC: begin // jp4, 4 blocks, dc -improved
402  ignore_color <= 1;
403  four_blocks <= 1;
404  jp4_dc_improved <= 1;
405  converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
406  scale_diff <=0;
407  hdr <=0;
408  end
409  CMPRS_CBIT_CMODE_JP4DIFF: begin // jp4, 4 blocks, differential
410  ignore_color <= 1;
411  four_blocks <= 1;
412  jp4_dc_improved <= 0;
413  converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
414  scale_diff <=0;
415  hdr <=0;
416  end
417  CMPRS_CBIT_CMODE_JP4DIFFHDR: begin // jp4, 4 blocks, differential, hdr
418  ignore_color <= 1;
419  four_blocks <= 1;
420  jp4_dc_improved <= 0;
421  converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
422  scale_diff <=0;
423  hdr <=1;
424  end
425  CMPRS_CBIT_CMODE_JP4DIFFDIV2: begin // jp4, 4 blocks, differential, divide diff by 2
426  ignore_color <= 1;
427  four_blocks <= 1;
428  jp4_dc_improved <= 0;
429  converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
430  scale_diff <=1;
431  hdr <=0;
432  end
433  CMPRS_CBIT_CMODE_JP4DIFFHDRDIV2: begin // jp4, 4 blocks, differential, hdr, divide diff by 2
434  ignore_color <= 1;
435  four_blocks <= 1;
436  jp4_dc_improved <= 0;
437  converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
438  scale_diff <=1;
439  hdr <=1;
440  end
441  CMPRS_CBIT_CMODE_MONO4: begin // mono, 4 blocks
442  ignore_color <= 1;
443  four_blocks <= 1;
444  jp4_dc_improved <= 0;
445  converter_type[2:0] <= CMPRS_MONO16; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
446  end
447  CMPRS_CBIT_CMODE_MONO1: begin // mono, 1 block
448  ignore_color <= 1;
449  four_blocks <= 1;
450  jp4_dc_improved <= 0;
451  converter_type[2:0] <= CMPRS_MONO8; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
452  end
453  default: begin //
454  ignore_color <= 'bx;
455  four_blocks <= 'bx;
456  jp4_dc_improved <= 'bx;
457  converter_type[2:0] <= 'bx;
458  end
459  endcase
460 
461 
462  end
463 //frame_start_xclk
468 
470 
471 endmodule
1571bayer_shift_mclkreg[1:0]
1577cmprs_mode_xclkreg[3:0]
reg [ 1:0] 1544bayer_shift
frame_start_xclk_i pulse_cross_clock
1502CMPRS_CBIT_CMODE_JP4DIFFDIV24'h9
reg [CMPRS_FRMT_MBCM1_BITS-1:0] 1552n_blocks_in_row_m1
1575cmprs_qpage_xclkreg[2:0]
reg [CMPRS_FRMT_LMARG_BITS-1:0] 1551left_marg
1580format_xclkreg[30:0]
1570cmprs_fmode_mclkreg[1:0]
reg [CMPRS_CORING_BITS-1:0] 1556coring
1578cmprs_fmode_xclkreg[1:0]
1569cmprs_mode_mclkreg[3:0]
reg [ 2:0] 1541cmprs_qpage
reg [ 1:0] 1543cmprs_fmode
1572format_mclkreg[30:0]
1581color_sat_xclkreg[23:0]
1567cmprs_qpage_mclkreg[2:0]
1503CMPRS_CBIT_CMODE_JP4DIFFHDRDIV24'ha
reg [CMPRS_FRMT_MBRM1_BITS-1:0] 1553n_block_rows_m1
reg [ 2:0] 1548converter_type
1573color_sat_mclkreg[23:0]
reg [CMPRS_CSAT_CB_BITS-1:0] 1554color_sat_cb
1501CMPRS_CBIT_CMODE_JP4DIFFHDR4'h8
reg [CMPRS_CSAT_CR_BITS-1:0] 1555color_sat_cr
1579bayer_shift_xclkreg[1:0]