cmprs_cmd_decode.v 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*!
 * <b>Module:</b>cmprs_cmd_decode
 * @file cmprs_cmd_decode.v
 * @date 2015-06-23  
 * @author Andrey Filippov     
 *
 * @brief Decode compressor command/modes, reclock some signals
 *
 * @copyright Copyright (c) 2015 Elphel, Inc.
 *
 * <b>License:</b>
12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * cmprs_cmd_decode.v is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  cmprs_cmd_decode.v is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/> .
25 26 27 28 29 30
 *
 * Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with independent modules provided by the FPGA vendor only (this permission
 * does not extend to any 3-rd party modules, "soft cores" or macros) under
 * different license terms solely for the purpose of generating binary "bitstream"
31
 * files and/or simulating the code, the copyright holders of this Program give
32 33
 * you the right to distribute the covered work without those independent modules
 * as long as the source code for them is available from the FPGA vendor free of
Andrey Filippov's avatar
Andrey Filippov committed
34
 * charge, and there is no dependence on any encrypted modules for simulating of
35 36 37
 * the combined code. This permission applies to you if the distributed code
 * contains all the components and scripts required to completely simulate it
 * with at least one of the Free Software programs.
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
`timescale 1ns/1ps
//From 353:
// [23] == 1 - set focus mode
// [22:21] 0 - none
//         1 - replace
//         2 - combine for all image
//         3 - combine in window only
// [20] == 1 - set Bayer shift
// [19:18] Bayer shift
// [17] == 1 - set compressor shift
// [16:14] compressor shift
// [13]==1 - enable color modes
// [12:9]== 0 - color, 4:2:0
//          1 - monochrome, 6/4 blocks (as 4:2:0)
//          2 - jp4, 6 blocks, original
//          3 - jp4, 6 blocks, dc -improved
//          4 - mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
//          5 - jp4,  4 blocks, dc-improved
//          6 - jp4,  differential
//          7 - 15 - reserved
// [8:7] == 0,1 - NOP, 2 -   disable, 3 - enable subtracting of average value (DC component), bypassing DCT
// [6] == 1 - enable quantization bank select, 0 - disregard bits [5:3]
// [5:3] = quantization page number (0..7)
// [2]== 1 - enable on/off control:
// [1:0]== 0 - reset compressor, stop immediately
//         1 - enable compressor, disable repetitive mode
//         2 - enable compressor, compress single frame
//         3 - enable compressor, enable repetitive mode
//
//Modified for 393:
// [23] == 1 - set focus mode
// [22:21] 0 - none
//         1 - replace
//         2 - combine for all image
//         3 - combine in window only
// [20] == 1 - set Bayer shift
// [19:18] Bayer shift
76 77 78 79 80
// WAS: [17:16] - unused

// [17] == 1 - set BE16 mode 
// [16]    0 - BE16 mode (1 - byte stream is little endian 16-bit, file is big endian 16 bits

81 82 83 84 85 86 87 88 89 90 91
// [15] == 1 - set single/multi frame mode
// [14]    0 - multiframe (compare frame numbers for 'suspend' output)
//         1 - single frame buffer
// [13]== 1 - enable color modes
// [12:9]== 0 - color, 4:2:0
//          1 - monochrome, 6/4 blocks (as 4:2:0)
//          2 - jp4, 6 blocks, original
//          3 - jp4, 6 blocks, dc -improved
//          4 - mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
//          5 - jp4,  4 blocks, dc-improved
//          6 - jp4,  differential
92 93 94 95 96 97 98 99 100
//          7 - jp4,  4 blocks, differential
//          8 - jp4,  4 blocks, differential, hdr
//          9 - jp4,  4 blocks, differential, divide by 2
//         10 - jp4,  4 blocks, differential, hdr,divide by 2
//         11 - mono JPEG (not yet implemented)
//         12-13 - RESERVED
//         14 - mono 4 blocks
//         15 - uncompressed

101 102 103 104 105 106 107 108 109
// [8:7] == 0,1 - NOP, 2 -   disable, 3 - enable subtracting of average value (DC component), bypassing DCT
// [6] == 1 - enable quantization bank select, 0 - disregard bits [5:3]
// [5:3] = quantization page number (0..7)
// [2]== 1 - enable compressor on/off control:
// [1:0]== 0 - reset compressor, stop immediately
//         1 - disable compression of the new frames, finish any already started
//         2 - enable compressor, compress single frame from memory (async)
//         3 - enable compressor, enable synchronous compression mode

110
// TODO WARNING: Switching to/from raw(uncompressed) mode requires stopping/restarting compressor!
111 112 113 114 115 116 117 118 119 120 121 122 123

module  cmprs_cmd_decode#(
        // Bit-fields in compressor control word
        parameter CMPRS_CBIT_RUN =            2, // bit # to control compressor run modes
        parameter CMPRS_CBIT_RUN_BITS =       2, // number of bits to control compressor run modes
        parameter CMPRS_CBIT_QBANK =          6, // bit # to control quantization table page
        parameter CMPRS_CBIT_QBANK_BITS =     3, // number of bits to control quantization table page
        parameter CMPRS_CBIT_DCSUB =          8, // bit # to control extracting DC components bypassing DCT
        parameter CMPRS_CBIT_DCSUB_BITS =     1, // bit # to control extracting DC components bypassing DCT
        parameter CMPRS_CBIT_CMODE =         13, // bit # to control compressor color modes
        parameter CMPRS_CBIT_CMODE_BITS =     4, // number of bits to control compressor color modes
        parameter CMPRS_CBIT_FRAMES =        15, // bit # to control compressor multi/single frame buffer modes
        parameter CMPRS_CBIT_FRAMES_BITS =    1, // number of bits to control compressor multi/single frame buffer modes
124 125 126 127
        
        parameter CMPRS_CBIT_BE16 =          17, // bit # to control compressor multi/single frame buffer modes
        parameter CMPRS_CBIT_BE16_BITS =      1, // number of bits to control compressor multi/single frame buffer modes
        
128 129 130 131 132 133 134 135 136
        parameter CMPRS_CBIT_BAYER =         20, // bit # to control compressor Bayer shift mode
        parameter CMPRS_CBIT_BAYER_BITS =     2, // number of bits to control compressor Bayer shift mode
        parameter CMPRS_CBIT_FOCUS =         23, // bit # to control compressor focus display mode
        parameter CMPRS_CBIT_FOCUS_BITS =     2, // number of bits to control compressor focus display mode
        // compressor bit-fields decode
        parameter CMPRS_CBIT_RUN_RST =        2'h0, // reset compressor, stop immediately
//      parameter CMPRS_CBIT_RUN_DISABLE =    2'h1, // disable compression of the new frames, finish any already started
        parameter CMPRS_CBIT_RUN_STANDALONE = 2'h2, // enable compressor, compress single frame from memory (async)
        parameter CMPRS_CBIT_RUN_ENABLE =     2'h3, // enable compressor, enable synchronous compression mode
137
        
138 139 140 141 142 143 144 145 146 147 148 149 150
        parameter CMPRS_CBIT_CMODE_JPEG18 =   4'h0, // color 4:2:0
        parameter CMPRS_CBIT_CMODE_MONO6 =    4'h1, // mono 4:2:0 (6 blocks)
        parameter CMPRS_CBIT_CMODE_JP46 =     4'h2, // jp4, 6 blocks, original
        parameter CMPRS_CBIT_CMODE_JP46DC =   4'h3, // jp4, 6 blocks, dc -improved
        parameter CMPRS_CBIT_CMODE_JPEG20 =   4'h4, // mono, 4 blocks (but still not actual monochrome JPEG as the blocks are scanned in 2x2 macroblocks)
        parameter CMPRS_CBIT_CMODE_JP4 =      4'h5, // jp4,  4 blocks, dc-improved
        parameter CMPRS_CBIT_CMODE_JP4DC =    4'h6, // jp4,  4 blocks, dc-improved
        parameter CMPRS_CBIT_CMODE_JP4DIFF =  4'h7, // jp4,  4 blocks, differential
        parameter CMPRS_CBIT_CMODE_JP4DIFFHDR =  4'h8, // jp4,  4 blocks, differential, hdr
        parameter CMPRS_CBIT_CMODE_JP4DIFFDIV2 = 4'h9, // jp4,  4 blocks, differential, divide by 2
        parameter CMPRS_CBIT_CMODE_JP4DIFFHDRDIV2 = 4'ha, // jp4,  4 blocks, differential, hdr,divide by 2
        parameter CMPRS_CBIT_CMODE_MONO1 =    4'hb, // mono JPEG (not yet implemented)
        parameter CMPRS_CBIT_CMODE_MONO4 =    4'he, // mono 4 blocks
151 152
        parameter CMPRS_CBIT_CMODE_RAW =      4'hf, // uncompressed
        
153 154 155 156 157 158 159
        parameter CMPRS_CBIT_FRAMES_SINGLE =  0, //1, // use a single-frame buffer for images

        parameter CMPRS_COLOR18 =             0, // JPEG 4:2:0 with 18x18 overlapping tiles for de-bayer
        parameter CMPRS_COLOR20 =             1, // JPEG 4:2:0 with 18x18 overlapping tiles for de-bayer (not implemented)
        parameter CMPRS_MONO16 =              2, // JPEG 4:2:0 with 16x16 non-overlapping tiles, color components zeroed
        parameter CMPRS_JP4 =                 3, // JP4 mode with 16x16 macroblocks
        parameter CMPRS_JP4DIFF =             4, // JP4DIFF mode TODO: see if correct
160 161
        parameter CMPRS_RAW =                 6, // Not comressed, raw data
        parameter CMPRS_MONO8 =               7, // Regular JPEG monochrome with 8x8 macroblocks (not yet implemented)
162 163 164 165 166 167 168 169 170 171 172
        
        parameter CMPRS_FRMT_MBCM1 =           0, // bit # of number of macroblock columns minus 1 field in format word
        parameter CMPRS_FRMT_MBCM1_BITS =     13, // number of bits in number of macroblock columns minus 1 field in format word
        parameter CMPRS_FRMT_MBRM1 =          13, // bit # of number of macroblock rows minus 1 field in format word
        parameter CMPRS_FRMT_MBRM1_BITS =     13, // number of bits in number of macroblock rows minus 1 field in format word
        parameter CMPRS_FRMT_LMARG =          26, // bit # of left margin field in format word
        parameter CMPRS_FRMT_LMARG_BITS =      5, // number of bits in left margin field in format word
        parameter CMPRS_CSAT_CB =              0, // bit # of number of blue scale field in color saturation word
        parameter CMPRS_CSAT_CB_BITS =        10, // number of bits in blue scale field in color saturation word
        parameter CMPRS_CSAT_CR =             12, // bit # of number of red scale field in color saturation word
        parameter CMPRS_CSAT_CR_BITS =        10, // number of bits in red scale field in color saturation word
Andrey Filippov's avatar
Andrey Filippov committed
173
        parameter CMPRS_CORING_BITS =          3 // number of bits in coring mode
174
        
175 176 177 178
    
)(
    input                         xclk,               // global clock input, compressor single clock rate
    input                         mclk,               // global system/memory clock
Andrey Filippov's avatar
Andrey Filippov committed
179
    input                         mrst,      // @posedge mclk, sync reset
180 181 182 183 184 185 186
    input                         ctrl_we,            // input - @mclk control register write enable
    input                         format_we,          // input - @mclk write number of tiles and left margin
    input                         color_sat_we,       // input - @mclk write color saturation values
    input                         coring_we,          // input - @mclk write coring values
    
    input                  [31:0] di,     // [15:0] data from CPU (sync to negedge sclk)
    input                         frame_start,        // @mclk
187 188
    output                        frame_start_xclk,   // frame start, parameters are copied at this pulse
    
189 190 191
                                  //  outputs sync @ posedge mclk:
    output                        cmprs_en_mclk,      // @mclk 0 resets immediately
    input                         cmprs_en_extend,    // @mclk keep compressor enabled for graceful shutdown
192
    input                         compressor_running,  // @xclk - reading frame or running stuffer/trailer
193 194 195 196 197 198
    
    output reg                    cmprs_run_mclk,     // @mclk enable propagation of vsync_late to frame_start_dst in bonded(sync to src) mode
    output reg                    cmprs_standalone,   // @mclk single-cycle: generate a single frame_start_dst in unbonded (not synchronized) mode.
                                                      // cmprs_run should be off
    output reg                    sigle_frame_buf,    // memory controller uses a single frame buffer (frame_number_* == 0), use other sync
                                  //  outputs sync @ posedge xclk:
199
    output reg                    cmprs_en_xclk,      // enable compressor, turn off immedaitely
200 201 202 203
    output reg                    cmprs_en_xclk_jp,      // enable compressor, turn off immedaitely
    output reg                    cmprs_en_xclk_raw,      // enable compressor, turn off immedaitely

    output reg                    cmprs_en_late_xclk, // enable stuffer, extends control fields for graceful shutdown (should be for raw also?)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                                  // outputs @posedge xclk, frozen when the new frame is requested
    output reg             [ 2:0] cmprs_qpage, // [2:0] - quantizator page number (0..7)
    output reg                    cmprs_dcsub, // subtract dc level before DCT, restore later
    output reg             [ 1:0] cmprs_fmode, //[1:0] - focus mode
    output reg             [ 1:0] bayer_shift, // additional shift to bayer mosaic
                                  
    output reg                    ignore_color,
    output reg                    four_blocks,
    output reg                    jp4_dc_improved,
    output reg             [ 2:0] converter_type, // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
    output reg                    scale_diff,
    output reg                    hdr,
    
    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)
    output reg [CMPRS_FRMT_MBCM1_BITS-1:0] n_blocks_in_row_m1, // number of macroblocks in a macroblock row minus 1
    output reg [CMPRS_FRMT_MBRM1_BITS-1:0] n_block_rows_m1,    // number of macroblock rows in a frame minus 1

    output reg [CMPRS_CSAT_CB_BITS-1:0] color_sat_cb,    // scale for Cb color component (color saturation)
    output reg [CMPRS_CSAT_CR_BITS-1:0] color_sat_cr,    // scale for Cr color component (color saturation)
    
224
    output reg [CMPRS_CORING_BITS-1:0]  coring,           // scale for Cb color component (color saturation)
225 226
    output reg                    compressed,      // late on, early off running compressor chain
    output reg                    uncompressed,    // late on, early off running raw chain
227
    output reg                    be16
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    );

    reg   [30:0]  di_r;
    reg           ctrl_we_r;
    
    reg           format_we_r;
    reg           color_sat_we_r;
    reg           coring_we_r;
    
    
    reg           cmprs_en_mclk_r;
    wire          ctrl_we_xclk;       // single xclk pulse after ctrl_we_r (or use just ctrl_we, not ctrl_we_r)
    wire          format_we_xclk;     // @xclk write number of tiles and left margin
    wire          color_sat_we_xclk;  // @xclk write color saturation values
    wire          coring_we_xclk;     // @xclk write coring values
    
    
    reg   [ 2:0] cmprs_qpage_mclk; // [2:0] - quantizator page number (0..7)
    reg          cmprs_dcsub_mclk; // subtract dc level before DCT, restore later
    reg   [ 3:0] cmprs_mode_mclk;  // [3:0] - compressor mode
    reg   [ 1:0] cmprs_fmode_mclk; //[1:0] - focus mode
    reg   [ 1:0] bayer_shift_mclk; // additional shift to bayer mosaic
    
    reg   [30:0] format_mclk; // left margin and macroblock rows/columns
    reg   [23:0] color_sat_mclk; // color saturation values (only 10 LSB in each 12 are used
    reg   [ 2:0] coring_mclk; // color saturation values (only 10 LSB in each 12 are used
    
    reg   [ 2:0] cmprs_qpage_xclk; // [2:0] - quantizator page number (0..7)
    reg          cmprs_dcsub_xclk; // subtract dc level before DCT, restore later
    reg   [ 3:0] cmprs_mode_xclk;  // [3:0] - compressor mode
    reg   [ 1:0] cmprs_fmode_xclk; //[1:0] - focus mode
    reg   [ 1:0] bayer_shift_xclk; // additional shift to bayer mosaic
    
    reg   [30:0] format_xclk; // left margin and macroblock rows/columns
    reg   [23:0] color_sat_xclk; // color saturation values (only 10 LSB in each 12 are used
    reg   [ 2:0] coring_xclk; // color saturation values (only 10 LSB in each 12 are used
264 265
    
    wire         uncompressed_mclk;
266
    wire         uncompressed_xclk;
267
    
268
    assign cmprs_en_mclk = cmprs_en_mclk_r;
269
    assign uncompressed_mclk = cmprs_mode_mclk[3:0] == CMPRS_CBIT_CMODE_RAW;
270
    assign uncompressed_xclk = cmprs_mode_xclk[3:0] == CMPRS_CBIT_CMODE_RAW;
271
    
Andrey Filippov's avatar
Andrey Filippov committed
272 273 274
    always @ (posedge mclk) begin
        if (mrst) ctrl_we_r <= 0;
        else      ctrl_we_r <= ctrl_we;
275
        
Andrey Filippov's avatar
Andrey Filippov committed
276 277
        if (mrst) format_we_r <= 0;
        else      format_we_r <= format_we;
278
        
Andrey Filippov's avatar
Andrey Filippov committed
279 280
        if (mrst) color_sat_we_r <= 0;
        else      color_sat_we_r <= color_sat_we;
281
        
Andrey Filippov's avatar
Andrey Filippov committed
282 283
        if (mrst) coring_we_r <= 0;
        else      coring_we_r <= coring_we;
284
        
Andrey Filippov's avatar
Andrey Filippov committed
285
        if (mrst)                                                      di_r <= 0;
286 287
        else if (ctrl_we || format_we || color_sat_we || coring_we)    di_r <= di[30:0];
    
Andrey Filippov's avatar
Andrey Filippov committed
288
        if      (mrst)                              cmprs_en_mclk_r <= 0;
289 290
        else if (ctrl_we_r && di_r[CMPRS_CBIT_RUN]) cmprs_en_mclk_r <= (di_r[CMPRS_CBIT_RUN-1 -:CMPRS_CBIT_RUN_BITS] != CMPRS_CBIT_RUN_RST);
        
Andrey Filippov's avatar
Andrey Filippov committed
291
        if      (mrst)                              cmprs_run_mclk <= 0;
292 293
        else if (ctrl_we_r && di_r[CMPRS_CBIT_RUN]) cmprs_run_mclk <= (di_r[CMPRS_CBIT_RUN-1 -:CMPRS_CBIT_RUN_BITS] == CMPRS_CBIT_RUN_ENABLE);
        
Andrey Filippov's avatar
Andrey Filippov committed
294
        if      (mrst)      cmprs_standalone <= 0;
295
        else cmprs_standalone <=  ctrl_we_r && di_r[CMPRS_CBIT_RUN] && (di_r[CMPRS_CBIT_RUN-1 -:CMPRS_CBIT_RUN_BITS] == CMPRS_CBIT_RUN_STANDALONE);
296

Andrey Filippov's avatar
Andrey Filippov committed
297
        if      (mrst)                                 sigle_frame_buf <= 0;
298 299
        else if (ctrl_we_r && di_r[CMPRS_CBIT_FRAMES]) sigle_frame_buf <= (di_r[CMPRS_CBIT_FRAMES-1 -:CMPRS_CBIT_FRAMES_BITS] == CMPRS_CBIT_FRAMES_SINGLE);

300 301 302
        if      (mrst)                                 be16 <= 0;
        else if (ctrl_we_r && di_r[CMPRS_CBIT_BE16])   be16 <= (di_r[CMPRS_CBIT_BE16-1 -:CMPRS_CBIT_BE16_BITS] == 1);

Andrey Filippov's avatar
Andrey Filippov committed
303
        if      (mrst)                                 cmprs_qpage_mclk <= 0;
304 305
        else if (ctrl_we_r && di_r[CMPRS_CBIT_QBANK])  cmprs_qpage_mclk <= di_r[CMPRS_CBIT_QBANK-1 -:CMPRS_CBIT_QBANK_BITS];

Andrey Filippov's avatar
Andrey Filippov committed
306
        if      (mrst)                                 cmprs_dcsub_mclk <= 0;
307 308
        else if (ctrl_we_r && di_r[CMPRS_CBIT_DCSUB])  cmprs_dcsub_mclk <= di_r[CMPRS_CBIT_DCSUB-1 -:CMPRS_CBIT_DCSUB_BITS];
        
Andrey Filippov's avatar
Andrey Filippov committed
309
        if      (mrst)                                 cmprs_mode_mclk <=  0;
310 311
        else if (ctrl_we_r && di_r[CMPRS_CBIT_CMODE])  cmprs_mode_mclk <=  di_r[CMPRS_CBIT_CMODE-1 -:CMPRS_CBIT_CMODE_BITS];
        
Andrey Filippov's avatar
Andrey Filippov committed
312
        if      (mrst)                                 cmprs_fmode_mclk <=  0;
313 314
        else if (ctrl_we_r && di_r[CMPRS_CBIT_FOCUS])  cmprs_fmode_mclk <=  di_r[CMPRS_CBIT_FOCUS-1 -:CMPRS_CBIT_FOCUS_BITS];
        
Andrey Filippov's avatar
Andrey Filippov committed
315
        if      (mrst)                                 bayer_shift_mclk <=  0;
316 317 318
        else if (ctrl_we_r && di_r[CMPRS_CBIT_BAYER])  bayer_shift_mclk <=  di_r[CMPRS_CBIT_BAYER-1 -:CMPRS_CBIT_BAYER_BITS];
        
        
Andrey Filippov's avatar
Andrey Filippov committed
319
        if      (mrst)           format_mclk <=  0;
320 321
        else if (format_we_r)    format_mclk <=  di_r[30:0];
        
322
        if      (mrst)           color_sat_mclk <=  'h0b6090; // 'h16c120 - saturation = 2
323 324
        else if (color_sat_we_r) color_sat_mclk <=  di_r[23:0];
        
Andrey Filippov's avatar
Andrey Filippov committed
325
        if      (mrst)           coring_mclk <=  0;
326 327 328 329 330
        else if (coring_we_r)    coring_mclk <=  di_r[2:0];

    end
    
    // re-clock to compressor clock
331
    always @ (posedge xclk)  begin
332 333
        cmprs_en_xclk   <=      cmprs_en_mclk_r;
        cmprs_en_late_xclk  <=  cmprs_en_mclk_r || cmprs_en_extend;
334 335 336 337 338 339
        if      (!cmprs_en_mclk_r) cmprs_en_xclk_jp <= 0;
        else if (ctrl_we_xclk)     cmprs_en_xclk_jp <= cmprs_en_mclk_r && !uncompressed_mclk;

        if      (!cmprs_en_mclk_r) cmprs_en_xclk_raw <= 0;
        else if (ctrl_we_xclk)     cmprs_en_xclk_raw <= cmprs_en_mclk_r && uncompressed_mclk;
        
340 341
    end    
    always @ (posedge xclk) if (ctrl_we_xclk) begin
342 343 344 345 346 347 348 349 350
        cmprs_qpage_xclk <=     cmprs_qpage_mclk;
        cmprs_dcsub_xclk <=     cmprs_dcsub_mclk;
        cmprs_mode_xclk <=      cmprs_mode_mclk;
        cmprs_fmode_xclk <=     cmprs_fmode_mclk;
        bayer_shift_xclk <=     bayer_shift_mclk;
    end
    
    always @ (posedge xclk) begin
         if (format_we_xclk)    format_xclk <=    format_mclk;
Andrey Filippov's avatar
Andrey Filippov committed
351
         if (color_sat_we_xclk) color_sat_xclk <= color_sat_mclk; // SuppressThisWarning VivadoSynthesis
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
         if (coring_we_xclk)    coring_xclk <=    coring_mclk;
    end

    always @ (posedge xclk) if (frame_start_xclk) begin
        cmprs_qpage <=        cmprs_qpage_xclk;
        cmprs_dcsub <=        cmprs_dcsub_xclk;
        cmprs_fmode <=        cmprs_fmode_xclk;
        bayer_shift <=        bayer_shift_xclk;
        
        left_marg <=          format_xclk[CMPRS_FRMT_LMARG +: CMPRS_FRMT_LMARG_BITS];
        n_block_rows_m1 <=    format_xclk[CMPRS_FRMT_MBRM1 +: CMPRS_FRMT_MBRM1_BITS];
        n_blocks_in_row_m1 <= format_xclk[CMPRS_FRMT_MBCM1 +: CMPRS_FRMT_MBCM1_BITS];
        
        color_sat_cr <=       color_sat_xclk[CMPRS_CSAT_CR +: CMPRS_CSAT_CR_BITS];
        color_sat_cb <=       color_sat_xclk[CMPRS_CSAT_CB +: CMPRS_CSAT_CB_BITS];
        
        coring <=             coring_xclk;
        
        
        // Will infer ROM
        case (cmprs_mode_xclk[3:0])
            CMPRS_CBIT_CMODE_MONO6: begin //monochrome, (4:2:0),
                ignore_color     <= 1;
                four_blocks      <= 0;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_MONO16; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JPEG18: begin //color, 4:2:0, 18x18(old)
                ignore_color     <= 0;
                four_blocks      <= 0;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_COLOR18; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP46: begin // jp4, original (4:2:0),
                ignore_color     <= 1;
                four_blocks      <= 0;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP46DC: begin // jp4, dc -improved (4:2:0),
                ignore_color     <= 1;
                four_blocks      <= 0;
                jp4_dc_improved  <= 1;
                converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JPEG20: begin // color, 4:2:0, 20x20, middle of the tile (not yet implemented)
                ignore_color     <= 0;
                four_blocks      <= 0;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_COLOR20; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP4: begin // jp4, 4 blocks, (legacy)
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP4DC: begin // jp4, 4 blocks, dc -improved
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 1;
                converter_type[2:0] <= CMPRS_JP4; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP4DIFF: begin // jp4, 4 blocks, differential
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP4DIFFHDR: begin // jp4, 4 blocks, differential, hdr
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=0;
                hdr              <=1;
              end
            CMPRS_CBIT_CMODE_JP4DIFFDIV2: begin // jp4, 4 blocks, differential, divide diff by 2
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=1;
                hdr              <=0;
              end
            CMPRS_CBIT_CMODE_JP4DIFFHDRDIV2: begin // jp4, 4 blocks, differential, hdr, divide diff by 2
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_JP4DIFF; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
                scale_diff       <=1;
                hdr              <=1;
              end
            CMPRS_CBIT_CMODE_MONO4: begin // mono, 4 blocks
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_MONO16; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
              end
            CMPRS_CBIT_CMODE_MONO1: begin // mono, 1 block
                ignore_color     <= 1;
                four_blocks      <= 1;
                jp4_dc_improved  <= 0;
                converter_type[2:0] <= CMPRS_MONO8; // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
              end
473 474 475 476 477

            CMPRS_CBIT_CMODE_RAW:   begin // uncompressed
                converter_type[2:0] <= CMPRS_RAW;   // 0 - color18, 1 - color20, 2 - mono, 3 - jp4, 4 - jp4-diff
              end

478 479 480 481 482 483 484
            default: begin // 
                ignore_color     <=    'bx;
                four_blocks      <=    'bx;
                jp4_dc_improved  <=    'bx;
                converter_type[2:0] <= 'bx;
              end
       endcase
485 486 487 488 489 490 491 492 493
//       uncompressed <= cmprs_mode_xclk[3:0] == CMPRS_CBIT_CMODE_RAW;
    end
    always @ (posedge xclk) begin
        if (!cmprs_en_mclk_r || (!uncompressed_xclk && !compressor_running)) uncompressed <= 0;
        else if (frame_start_xclk)                                           uncompressed <= uncompressed_xclk;

        if (!cmprs_en_mclk_r || ( uncompressed_xclk && !compressor_running)) compressed <= 0;
        else if (frame_start_xclk)                                           compressed <= !uncompressed_xclk;
    end
494 495 496
    
    
//frame_start_xclk
Andrey Filippov's avatar
Andrey Filippov committed
497 498 499 500
    pulse_cross_clock ctrl_we_xclk_i       (.rst(mrst), .src_clk(mclk), .dst_clk(xclk), .in_pulse(ctrl_we_r),      .out_pulse(ctrl_we_xclk),.busy());
    pulse_cross_clock format_we_xclk_i     (.rst(mrst), .src_clk(mclk), .dst_clk(xclk), .in_pulse(format_we_r),    .out_pulse(format_we_xclk),.busy());
    pulse_cross_clock color_sat_we_xclk_i  (.rst(mrst), .src_clk(mclk), .dst_clk(xclk), .in_pulse(color_sat_we_r), .out_pulse(color_sat_we_xclk),.busy());
    pulse_cross_clock coring__we_xclk_i    (.rst(mrst), .src_clk(mclk), .dst_clk(xclk), .in_pulse(coring_we_r),    .out_pulse(coring_we_xclk),.busy());
501
    
Andrey Filippov's avatar
Andrey Filippov committed
502
    pulse_cross_clock frame_start_xclk_i   (.rst(mrst), .src_clk(mclk), .dst_clk(xclk), .in_pulse(frame_start),    .out_pulse(frame_start_xclk),.busy());
503 504

endmodule