huff_fifo393.v 4.79 KB
Newer Older
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
/*
** -----------------------------------------------------------------------------**
** huff_fifo393.v
**
** Part of Huffman encoder for JPEG compressor - FIFO for Huffman encoder
**
** Copyright (C) 2002-2015 Elphel, Inc
**
** -----------------------------------------------------------------------------**
**  huff_fifo393.v is free software - hardware description language (HDL) code.
** 
**  This program 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.
**
**  This program 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/>.
** -----------------------------------------------------------------------------**
**
*/
27

28 29 30 31 32 33
module huff_fifo393 (
    input             xclk,            // pixel clock, posedge
    input             xclk2x,          // twice frequency - uses negedge inside
    input             en,              // will reset if ==0 (sync to xclk)
    input      [15:0] di,              // data in (sync to xclk)
    input             ds,              // din valid (sync to xclk)
34 35 36
    input             want_read,       // will be and-ed with dav
    output            dav,             // FIFO output latch has data (fifo_or_full)
    output reg [15:0] q
37
    );        // output data
38 39 40 41

    reg     [9:0] wa;
    reg     [9:0] ra_r;
    wire   [15:0] fifo_o;
42
    reg           ds1;     // ds delayed by one xclk to give time to block ram to write data. Not needed likely.
43
    reg           synci;
44
    reg     [2:0] synco;
45
    reg           sync_we; // single xclk2x period pulse for each ds@xclk
46
    reg           en2x;    // en sync to xclk2x;
47 48

    reg     [9:0] diff_a;
49
    
50 51 52 53
    wire    [3:0] re;
    reg     [2:0] nempty_r; // output register and RAM registers not empty
    wire    [3:0] nempty;   // output register and RAM register and RAM internal are not empty
    wire          many;     
54

55 56 57 58 59 60 61 62 63 64 65 66 67
    assign dav =    nempty[0];
    assign nempty = {(|diff_a), nempty_r};
    assign many =   &(nempty); // memory and all the register chain are full
    assign re =    {4{many & want_read}} | {nempty[3] & ~nempty[2], // read memory location
                                            nempty[2] & ~nempty[1], // regen
                                            nempty[1] & ~nempty[0], // copy to q- register
                                            nempty[0] & want_read}; // external read when data is available
                                                      
    always @ (posedge xclk) begin // input stage, no overrun detection. TODO: propagate half-full?
        if (!en)      wa <= 0;
        else if (ds)  wa <= wa+1;
        
        ds1                   <= ds && en;
68 69 70
        if (!en)      synci   <= 1'b0;
        else if (ds1) synci   <= ~synci;
    end
71
    
72 73
    always @ (negedge xclk2x) begin
        en2x <= en;
74 75
        synco   <= {synco[1:0],synci};
        sync_we      <= en2x && (synco[1] != synco[2]);
76 77 78
    end
  
    always @ (negedge xclk2x) begin
79 80
        if      (!en2x)         nempty_r[0] <= 0;
        else if (re[1] ^ re[0]) nempty_r[0] <=re[1];
81
        
82 83 84 85 86 87 88 89
        if      (!en2x)         nempty_r[1] <= 0;
        else if (re[2] ^ re[1]) nempty_r[1] <=re[2];

        if      (!en2x)         nempty_r[2] <= 0;
        else if (re[3] ^ re[2]) nempty_r[2] <=re[3];

        if        (!en2x)       ra_r <= 0;
        else if   (re[3])       ra_r <= ra_r + 1;
90
        
91 92 93
        if (!en2x)                   diff_a <= 0;
        else if ( sync_we && !re[3]) diff_a <= diff_a + 1;
        else if (!sync_we &&  re[3]) diff_a <= diff_a - 1;
94
        
95 96
        if      (!en2x) q <= 0;                  
        else if (re[1]) q <= fifo_o;
97 98 99 100
        
    end

    ram18_var_w_var_r #(
101
        .REGISTERS    (1),
102 103 104 105 106
        .LOG2WIDTH_WR (4),
        .LOG2WIDTH_RD (4),
        .DUMMY        (0)
    ) i_fifo (
        .rclk         (xclk2x),        // input
107 108 109
        .raddr        (ra_r[9:0]),     // input[9:0] 
        .ren          (re[3]),         // input
        .regen        (re[2]),         // input
110 111 112 113 114 115 116 117 118
        .data_out     (fifo_o[15:0]),  // output[15:0] 
        .wclk         (xclk),          // input
        .waddr        (wa[9:0]),       // input[9:0] 
        .we           (ds),            // input
        .web          (4'hf),          // input[3:0] 
        .data_in      (di[15:0])       // input[15:0] 
    );
 
endmodule