ahci_dma_rd_fifo.v 11.9 KB
Newer Older
1 2 3
/*******************************************************************************
 * Module: ahci_dma_rd_fifo
 * Date:2016-01-01  
4
 * Author: Andrey Filippov
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Description: cross clocks,  word-realign, 64->32
 * Convertion from x64 QWORD-aligned AXI data @hclk to
 * 32-bit word-aligned data at mclk
 *
 * Copyright (c) 2016 Elphel, Inc .
 * ahci_dma_rd_fifo.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.
 *
 *  ahci_dma_rd_fifo.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/> .
22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * 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"
 * files and/or simulating the code, the copyright holders of this Program give
 * 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
 * charge, and there is no dependence on any encrypted modules for simulating of
 * 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.
35 36 37 38
 *******************************************************************************/
`timescale 1ns/1ps

module  ahci_dma_rd_fifo#(
Andrey Filippov's avatar
Andrey Filippov committed
39 40
    parameter WCNT_BITS    = 21,
    parameter ADDRESS_BITS = 3
41 42 43 44 45
)(
    input                 mrst,
    input                 hrst,
    input                 mclk,
    input                 hclk,
Andrey Filippov's avatar
Andrey Filippov committed
46 47 48 49
    // hclk domain
    input [WCNT_BITS-1:0] wcnt,  // decrementing word counter, 0- based (0 need 1, 1 - need 2, ...) valid @ start
    input           [1:0] woffs, // 2 LSBs of the initial word address - valid @ start
    input                 start, // start transfer
50 51 52
    input          [63:0] din,
    input                 din_av,
    input                 din_av_many,
53
    input                 last_prd, // last prd, flush partial dword if there were odd number of words transferred. valid @ start
Andrey Filippov's avatar
Andrey Filippov committed
54
    // Or maybe use "last_prd"?
55
    output                din_re,
56 57 58
    output reg            done,        // this PRD data sent to cross-clock FIFO (may result in only half-dword sent out),
                                       // OK to fetch another PRD (if it was not the last) 
    output                done_flush,  // finished last PRD (indicated by last_prd @ start), data left module
Andrey Filippov's avatar
Andrey Filippov committed
59
    // mclk domain
60
    output         [31:0] dout,
Andrey Filippov's avatar
Andrey Filippov committed
61
    output                dout_vld,
62
    input                 dout_re,
63
    output                last_DW      // dout contains last DW
Andrey Filippov's avatar
Andrey Filippov committed
64 65
    ,output [31:0] debug_dma_h2d
    
66
);
Andrey Filippov's avatar
Andrey Filippov committed
67 68
    localparam ADDRESS_NUM = (1<<ADDRESS_BITS); // 8 for ADDRESS_BITS==3
    reg   [ADDRESS_BITS : 0] waddr; // 1 extra bit       
69 70 71
//    reg   [ADDRESS_BITS+1:0] raddr; // 1 extra bit       
    reg   [ADDRESS_BITS+1:0] raddr_r; // 1 extra bit       
    wire  [ADDRESS_BITS+1:0] raddr_w; // 1 extra bit       
Andrey Filippov's avatar
Andrey Filippov committed
72 73 74 75 76
    reg              [63:16] din_prev; // only 48 bits are needed
    reg      [WCNT_BITS-3:0] qwcntr;
    reg                      busy;
    wire               [2:0] end_offs = wcnt[1:0] + woffs;
    
77 78
    reg               [63:0] fifo_ram  [0: ADDRESS_NUM - 1];
    reg                [3:0] vld_ram   [0: ADDRESS_NUM - 1];
Andrey Filippov's avatar
Andrey Filippov committed
79 80 81 82
    reg [(1<<ADDRESS_BITS)-1:0] fifo_full;  // set in write clock domain
    reg [(1<<ADDRESS_BITS)-1:0] fifo_nempty;// set in read clock domain
    wire                     fifo_wr;
    wire                     fifo_rd;
83 84 85 86
    reg                [1:0] fifo_rd_r;
    reg                      mrst_hclk;
    
    wire [(1<<ADDRESS_BITS)-1:0] fifo_full2 =       {~fifo_full[0],fifo_full[ADDRESS_NUM-1:1]};
Andrey Filippov's avatar
Andrey Filippov committed
87
    reg                      fifo_dav;  // @mclk
88
    wire                     fifo_dav2_w;   
Andrey Filippov's avatar
Andrey Filippov committed
89
    reg                      fifo_dav2; // @mclk
90
    
91
    reg                      fifo_half_hclk; // Half Fifo is empty, OK to write
Andrey Filippov's avatar
Andrey Filippov committed
92 93 94 95
    reg                [1:0] woffs_r;
    
    wire              [63:0] fifo_di= woffs_r[1]?(woffs_r[0] ? {din[47:0],din_prev[63:48]} : {din[31:0],din_prev[63:32]}):
                                                 (woffs_r[0] ? {din[15:0],din_prev[63:16]} : din[63:0]);
96
    wire               [3:0] fifo_di_vld;                                             
97 98 99 100
//    wire              [63:0] fifo_do =       fifo_ram [raddr[ADDRESS_BITS:1]];
//    wire               [3:0] fifo_do_vld =   vld_ram  [raddr[ADDRESS_BITS:1]];
    reg               [63:0] fifo_do_r;
    reg                [3:0] fifo_do_vld_r;
101 102 103 104
    reg                      din_av_safe_r;
    reg                      en_fifo_wr;
    reg                [3:0] last_mask;
    wire                     done_flush_mclk;
105 106 107 108
    reg                      flushing_hclk; // flushing data, ends when confirmed from mclk domain
    reg                      flushing_mclk; // just registered flushing_hclk @mclk                     
    
    wire                     last_fifo_wr;
109 110 111
    
    assign din_re =  busy && fifo_half_hclk && din_av_safe_r;
    assign fifo_wr = en_fifo_wr && fifo_half_hclk && (din_av_safe_r || !busy);
112 113 114 115
    assign fifo_di_vld =    last_fifo_wr? last_mask : 4'hf;

    
    wire [2:0] debug_waddr = waddr[2:0];
116 117 118 119 120 121 122 123 124 125 126 127
    wire [2:0] debug_raddr = raddr_r[3:1];
     // just for gtkwave - same names
    wire  [ADDRESS_BITS+1:0] raddr = raddr_r;       
    wire              [63:0] fifo_do =       fifo_do_r;
    wire               [3:0] fifo_do_vld =   fifo_do_vld_r;
    
    
//    assign fifo_dav2_w = fifo_full2[raddr[ADDRESS_BITS:1]] ^ raddr[ADDRESS_BITS+1];
///    assign fifo_dav2_w = fifo_full2[raddr_r[ADDRESS_BITS:1]] ^ raddr_r[ADDRESS_BITS+1];
    assign fifo_dav2_w = fifo_full2[raddr_w[ADDRESS_BITS:1]] ^ raddr_w[ADDRESS_BITS+1];
    
    
128 129
    
    assign last_fifo_wr = !busy || ((qwcntr == 0) && ((woffs == 0) || end_offs[2])); //            ((qwcntr != 0) || ((woffs != 0) && last_prd));
130
    
Andrey Filippov's avatar
Andrey Filippov committed
131 132
    
    always @ (posedge hclk) begin
133 134 135 136
        if      (hrst)                      mrst_hclk <= 0;
        else                                mrst_hclk <= mrst;
    
        if      (mrst_hclk)                 busy <= 0;
137 138 139 140 141
        else if (start)                     busy <= 1;
        else if (din_re && (qwcntr == 0))   busy <= 0;
        
        done <= busy && din_re && (qwcntr == 0);
        
142 143 144 145 146
        if      (mrst_hclk)                 en_fifo_wr <= 0;
        else if (start)                     en_fifo_wr <= (woffs == 0);
        else if (din_re || fifo_wr)         en_fifo_wr <= busy && ((qwcntr != 0) || ((woffs != 0) && !end_offs[2]));
        
        if       (start) qwcntr <= wcnt[WCNT_BITS-1:2] + end_offs[2];
Andrey Filippov's avatar
Andrey Filippov committed
147 148 149 150 151
        else if (din_re) qwcntr <= qwcntr - 1;
        

        if (start) woffs_r <= woffs;
        
152 153
        if    (mrst_hclk) fifo_full <= 0;
        else if (fifo_wr) fifo_full <= {fifo_full[ADDRESS_NUM-2:0],~waddr[ADDRESS_BITS]};
Andrey Filippov's avatar
Andrey Filippov committed
154

155
        if    (mrst_hclk) waddr <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
156 157 158 159 160 161 162 163
        else if (fifo_wr) waddr <= waddr+1;
        
        fifo_half_hclk <= fifo_nempty [waddr[ADDRESS_BITS-1:0]] ^ waddr[ADDRESS_BITS];
        
        if (din_re) din_prev[63:16] <= din[63:16];
        
        if (fifo_wr) fifo_ram[waddr[ADDRESS_BITS-1:0]] <= fifo_di;
        if (fifo_wr) vld_ram [waddr[ADDRESS_BITS-1:0]] <= fifo_di_vld;
164
//        if (fifo_wr) flush_ram[waddr[ADDRESS_BITS-1:0]] <= fifo_di_flush;
165
        
166 167
        if (mrst_hclk) din_av_safe_r <= 0;
        else           din_av_safe_r <= din_av && (din_av_many || !din_re);
168
        
169
        if (start) last_mask <= {&wcnt[1:0], wcnt[1], |wcnt[1:0], 1'b1}; 
170
        
171 172 173
        if      (mrst_hclk || done_flush)                                                          flushing_hclk <= 0;
        else if (fifo_wr && last_prd && (((qwcntr == 0) && ((woffs == 0) || !last_prd)) || !busy)) flushing_hclk <= 1;
        
Andrey Filippov's avatar
Andrey Filippov committed
174 175
    end
    
176 177
    assign raddr_w = mrst ? 0 : (raddr_r + fifo_rd);
    
178
    always @ (posedge mclk) begin
179
    
180
        fifo_rd_r <= {fifo_rd_r[0],fifo_rd};
181 182 183 184
//        if      (mrst)                raddr <= 0;
//        else if (fifo_rd)             raddr <= raddr + 1; 

        raddr_r <= raddr_w;
185

186
        if      (mrst)                fifo_nempty <= {{(ADDRESS_NUM>>1){1'b0}},{(ADDRESS_NUM>>1){1'b1}}};// 8'b00001111
187 188 189 190 191 192 193
//        else if (fifo_rd && raddr[0]) fifo_nempty <= {fifo_nempty[ADDRESS_NUM-2:0], ~raddr[ADDRESS_BITS+1] ^ raddr[ADDRESS_BITS]};
        else if (fifo_rd && raddr_r[0]) fifo_nempty <= {fifo_nempty[ADDRESS_NUM-2:0], ~raddr_r[ADDRESS_BITS+1] ^ raddr_r[ADDRESS_BITS]};
        
//        fifo_dav <=  fifo_full [raddr[ADDRESS_BITS:1]] ^ raddr[ADDRESS_BITS+1];
///        fifo_dav <=  fifo_full [raddr_r[ADDRESS_BITS:1]] ^ raddr_r[ADDRESS_BITS+1];
        fifo_dav <=  fifo_full [raddr_w[ADDRESS_BITS:1]] ^ raddr_w[ADDRESS_BITS+1];
        
Andrey Filippov's avatar
Andrey Filippov committed
194
        
195 196 197 198
        fifo_dav2 <= fifo_dav2_w; // fifo_full2[raddr[ADDRESS_BITS:1]] ^ raddr[ADDRESS_BITS+1];
        
        if      (mrst)   flushing_mclk <= 0;
        else             flushing_mclk <= flushing_hclk;
199 200 201 202
        
        fifo_do_r <=       fifo_ram [raddr_w[ADDRESS_BITS:1]];
        fifo_do_vld_r <=   vld_ram  [raddr_w[ADDRESS_BITS:1]];
        
Andrey Filippov's avatar
Andrey Filippov committed
203 204 205
    end
    
    ahci_dma_rd_stuff ahci_dma_rd_stuff_i (
206 207 208
        .rst      (mrst),                                       // input
        .clk      (mclk),                                       // input
        .din_av   (fifo_dav),                                   // input
209
        .din_avm_w(fifo_dav2_w),                                // input
210
        .din_avm  (fifo_dav2),                                  // input
211
        .flushing (flushing_mclk),                              // input
212 213 214 215
//        .din      (raddr[0]?fifo_do[63:32]:  fifo_do[31:0]),    // input[31:0] 
//        .dm       (raddr[0]?fifo_do_vld[3:2]:fifo_do_vld[1:0]), // input[1:0] 
        .din      (raddr_r[0]?fifo_do_r[63:32]:  fifo_do_r[31:0]),    // input[31:0] 
        .dm       (raddr_r[0]?fifo_do_vld_r[3:2]:fifo_do_vld_r[1:0]), // input[1:0] 
216 217 218 219
        .din_re   (fifo_rd),                                    // output
        .flushed  (done_flush_mclk),                            // output reg: flush (end of last PRD is finished - data left module)
        .dout     (dout),                                       // output[31:0] reg 
        .dout_vld (dout_vld),                                   // output
Andrey Filippov's avatar
Andrey Filippov committed
220 221
        .dout_re  (dout_re),                                    // input
        .last_DW  (last_DW)                                     // output
Andrey Filippov's avatar
Andrey Filippov committed
222
    );
223

224 225 226
    pulse_cross_clock #(
        .EXTRA_DLY(0)
    ) done_flush_i (
227 228 229
        .rst       (mrst),                               // input
        .src_clk   (mclk),                               // input
        .dst_clk   (hclk),                               // input
230
        .in_pulse  (done_flush_mclk),                    // input
231 232 233
        .out_pulse (done_flush),                         // output
        .busy()                                          // output
    );
Andrey Filippov's avatar
Andrey Filippov committed
234 235 236 237
    
    assign debug_dma_h2d = {
                            14'b0,
                            fifo_rd,
238 239
                            raddr_r[4:0],
                            fifo_do_vld_r[3:0],
Andrey Filippov's avatar
Andrey Filippov committed
240 241 242 243 244 245 246 247 248 249 250
    
                            fifo_dav,
                            fifo_dav2_w,
                            fifo_dav2,
                            flushing_mclk,
                            
                            done_flush_mclk,
                            dout_vld,
                            dout_re,
                            last_DW
    };
251
endmodule