status_router2.v 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*!
 * <b>Module:</b>status_router2
 * @file status_router2.v
 * @date 2015-01-13  
 * @author Andrey Filippov     
 *
 * @brief 2:1 status data router/mux
 *
 * @copyright Copyright (c) 2015 Elphel, Inc.
 *
 * <b>License:</b>
12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * status_router2.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.
 *
 *  status_router2.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
 //TODO: make a 4-input mux too?
`timescale 1ns/1ps
41 42
`include "system_defines.vh" 
//`define DEBUG_FIFO 1
43 44 45
module  status_router2 #(
    parameter FIFO_TYPE = "ONE_CYCLE" // "TWO_CYCLE"
)(
46 47
    input        rst,
    input        clk,
Andrey Filippov's avatar
Andrey Filippov committed
48
    input        srst,      // sync reset
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    // 2 input channels 
    input [7:0]  db_in0,
    input        rq_in0,
    output       start_in0, // only for the first cycle, combinatorial
    input [7:0]  db_in1,
    input        rq_in1,
    output       start_in1, // only for the first cycle, combinatorial
    // output (multiplexed) channel
    output [7:0] db_out,
    output       rq_out,
    input        start_out  // only for the first cycle, combinatorial
);
    wire           [1:0] rq_in={rq_in1,rq_in0};
    wire           [1:0] start_rcv;
    reg            [1:0] rcv_rest_r; // receiving remaining (after first) bytes
    wire           [1:0] fifo_half_full;
    
    assign         start_in0=start_rcv[0];
    assign         start_in1=start_rcv[1];

    assign start_rcv=~fifo_half_full & ~rcv_rest_r & rq_in;
    wire   [7:0] fifo0_out;
    wire   [7:0] fifo1_out;
Andrey Filippov's avatar
Andrey Filippov committed
72 73 74
    wire   [1:0] fifo_last_byte; 
    wire   [1:0] fifo_nempty_pre; // pure fifo output
    wire   [1:0] fifo_nempty;  // safe version, zeroed for last byte
75 76 77 78 79 80 81 82
    wire   [1:0] fifo_re;
    reg          next_chn;
    reg          current_chn_r;
    reg          snd_rest_r;
    wire         snd_pre_start; 
    wire         snd_last_byte;
    wire         chn_sel_w;
    wire         early_chn;
Andrey Filippov's avatar
Andrey Filippov committed
83
    wire         set_other_only_w; // window to initiate other channel only, same channel must wait
84

Andrey Filippov's avatar
Andrey Filippov committed
85
    assign       chn_sel_w=(&fifo_nempty)?next_chn : fifo_nempty[1];
86 87
    assign       fifo_re=start_out?{chn_sel_w,~chn_sel_w}:(snd_rest_r?{current_chn_r,~current_chn_r}:2'b0);
    
Andrey Filippov's avatar
Andrey Filippov committed
88 89 90
//    assign snd_last_byte=current_chn_r?fifo_last_byte[1]:fifo_last_byte[0];
    assign snd_last_byte=current_chn_r?(fifo_nempty_pre[1] && fifo_last_byte[1]):(fifo_nempty_pre[0] && fifo_last_byte[0]);
    assign set_other_only_w=snd_last_byte && (current_chn_r? fifo_nempty[0]:fifo_nempty[1]);
91
    assign snd_pre_start=|fifo_nempty && (!snd_rest_r || snd_last_byte);
Andrey Filippov's avatar
Andrey Filippov committed
92 93 94
///    assign snd_pre_start=|fifo_nempty && !snd_rest_r && !start_out; // no channel change after 
//    assign rq_out=(snd_rest_r && !snd_last_byte) || |fifo_nempty;
    assign rq_out=(snd_rest_r || |fifo_nempty) && !snd_last_byte ;
95 96
//    assign early_chn= (snd_rest_r & ~snd_last_byte)?current_chn_r:chn_sel_w;
    assign early_chn= snd_rest_r? current_chn_r: chn_sel_w;
97
    assign db_out=early_chn?fifo1_out:fifo0_out;
Andrey Filippov's avatar
Andrey Filippov committed
98 99
    assign fifo_nempty=fifo_nempty_pre & ~fifo_last_byte;
    
100
    always @ (posedge rst or posedge clk) begin
Andrey Filippov's avatar
Andrey Filippov committed
101 102 103
        if      (rst)  rcv_rest_r<= 0;
        else if (srst) rcv_rest_r<= 0;
        else           rcv_rest_r <= (rcv_rest_r & rq_in) | start_rcv;
104
    
Andrey Filippov's avatar
Andrey Filippov committed
105 106
        if      (rst)      next_chn<= 0;
        else if (srst)     next_chn<= 0;
Andrey Filippov's avatar
Andrey Filippov committed
107 108 109
        else if (|fifo_re) next_chn <= fifo_re[0]; // just to be fair
        
        if      (rst)                         current_chn_r <= 0;
Andrey Filippov's avatar
Andrey Filippov committed
110
        else if (srst)                        current_chn_r <= 0;
111
        else if (set_other_only_w)            current_chn_r <= ~current_chn_r;
Andrey Filippov's avatar
Andrey Filippov committed
112 113 114
        else if (snd_pre_start)               current_chn_r <= chn_sel_w;
///        else if (|fifo_nempty && !snd_rest_r) current_chn_r <= chn_sel_w;
        //|fifo_nempty && (!snd_rest_r
115

Andrey Filippov's avatar
Andrey Filippov committed
116 117 118
        if (rst)       snd_rest_r<= 0;
        else if (srst) snd_rest_r<= 0;
        else           snd_rest_r <= (snd_rest_r & ~snd_last_byte) | start_out;
119 120 121
    end
    
/* fifo_same_clock has currently latency of 2 cycles, use smth. faster here? - fifo_1cycle (but it has unregistered data output) */
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    generate
        if (FIFO_TYPE == "ONE_CYCLE") begin
            fifo_1cycle #(
                .DATA_WIDTH(9),
                .DATA_DEPTH(4) // 16
            ) fifo_in0_i (
                .rst       (1'b0),                                // rst), // input
                .clk       (clk),                                 // input
                .sync_rst  (srst),                                // input
                .we        (start_rcv[0] || rcv_rest_r[0]),       // input
                .re        (fifo_re[0]),                          // input
                .data_in   ({rcv_rest_r[0] & ~rq_in[0], db_in0}), // input[8:0] MSB marks last byte
                .data_out  ({fifo_last_byte[0],fifo0_out}),       // output[8:0]
                .nempty    (fifo_nempty_pre[0]),                  // output reg
                .half_full (fifo_half_full[0])                    // output reg 
        `ifdef DEBUG_FIFO
                ,.under(),     // output reg 
                .over(),       // output reg 
                .wcount(),     // output[3:0] reg 
                .rcount(),     // output[3:0] reg 
                .num_in_fifo() // output[3:0]
        `endif         
            );
        
            fifo_1cycle #(
                .DATA_WIDTH(9),
                .DATA_DEPTH(4) // 16
            ) fifo_in1_i (
                .rst       (1'b0),                                // rst), // input
                .clk       (clk),                                 // input
                .sync_rst  (srst),                                // input
                .we        (start_rcv[1] || rcv_rest_r[1]),       // input
                .re        (fifo_re[1]),                          // input
                .data_in   ({rcv_rest_r[1] & ~rq_in[1], db_in1}), // input[8:0] MSB marks last byte
                .data_out  ({fifo_last_byte[1],fifo1_out}),       // output[8:0]
                .nempty    (fifo_nempty_pre[1]),                  // output reg
                .half_full (fifo_half_full[1])                    // output reg 
        `ifdef DEBUG_FIFO
                ,.under(),     // output reg 
                .over(),       // output reg 
                .wcount(),     // output[3:0] reg 
                .rcount(),     // output[3:0] reg 
                .num_in_fifo() // output[3:0]
        `endif         
            );
        end else begin
            fifo_same_clock #(
                .DATA_WIDTH(9),
                .DATA_DEPTH(4) // 16
            ) fifo_in0_i (
                .rst       (1'b0),                                // rst), // input
                .clk       (clk),                                 // input
                .sync_rst  (srst),                                // input
                .we        (start_rcv[0] || rcv_rest_r[0]),       // input
                .re        (fifo_re[0]),                          // input
                .data_in   ({rcv_rest_r[0] & ~rq_in[0], db_in0}), // input[8:0] MSB marks last byte
                .data_out  ({fifo_last_byte[0],fifo0_out}),       // output[8:0]
                .nempty    (fifo_nempty_pre[0]),                  // output reg
                .half_full (fifo_half_full[0])                    // output reg 
        `ifdef DEBUG_FIFO
                ,.under(),     // output reg 
                .over(),       // output reg 
                .wcount(),     // output[3:0] reg 
                .rcount(),     // output[3:0] reg 
                .num_in_fifo() // output[3:0]
        `endif         
            );
        
            fifo_same_clock #(
                .DATA_WIDTH(9),
                .DATA_DEPTH(4) // 16
            ) fifo_in1_i (
                .rst       (1'b0),                                // rst), // input
                .clk       (clk),                                 // input
                .sync_rst  (srst),                                // input
                .we        (start_rcv[1] || rcv_rest_r[1]),       // input
                .re        (fifo_re[1]),                          // input
                .data_in   ({rcv_rest_r[1] & ~rq_in[1], db_in1}), // input[8:0] MSB marks last byte
                .data_out  ({fifo_last_byte[1],fifo1_out}),       // output[8:0]
                .nempty    (fifo_nempty_pre[1]),                  // output reg
                .half_full (fifo_half_full[1])                    // output reg 
        `ifdef DEBUG_FIFO
                ,.under(),     // output reg 
                .over(),       // output reg 
                .wcount(),     // output[3:0] reg 
                .rcount(),     // output[3:0] reg 
                .num_in_fifo() // output[3:0]
        `endif         
            );
        end
    endgenerate
    
214 215 216 217 218
// one car per green (round robin priority)
// start sending out with  with one cycle latency - now 2 cycles because of the FIFO

endmodule