简析

输入:data_in, data_en
输出:dataout
data_in:数据输入;data_en:输入数据有效;dataout数据输出。

输入数据暂存在data_reg中,使能信号data_en用打两拍的方式跨时钟域传输到时钟域B,最后data_out根据使能信号更新数据。

数据暂存

    reg [3:0] data_reg; 
    always@(posedge clk_a or negedge arstn) begin
        if(~arstn)
            data_reg <= 0;
        else
            data_reg <= data_in;
    end

使能信号打拍

data_en信号在A时钟域用一个D触发器暂存,然后打两拍传输到B时钟域。

	reg data_en_a, data_en_b0, data_en_b1;
    always@(posedge clk_a or negedge arstn) begin
        if(~arstn)
            data_en_a <= 0;
        else
            data_en_a <= data_en;
    end
     
    always@(posedge clk_b or negedge brstn) begin
        if(~brstn) begin
            data_en_b0 <= 0;
            data_en_b1 <= 0;
        end
        else begin
            data_en_b0 <= data_en_a;
            data_en_b1 <= data_en_b0;
        end
    end

MUX选择器

根据同步到B时钟域的使能信号data_en_b1,更新输出。

    always@(posedge clk_b or negedge brstn) begin
        if(~brstn)
            dataout <= 0;
        else
            dataout <= data_en_b1? data_reg: dataout;
    end

代码

module mux(
	input 				clk_a	, 
	input 				clk_b	,   
	input 				arstn	,
	input				brstn   ,
	input		[3:0]	data_in	,
	input               data_en ,

	output reg  [3:0] 	dataout
);
    reg [3:0] data_reg;
    reg       data_en_a, data_en_b0, data_en_b1;
    
    always@(posedge clk_a or negedge arstn) begin
        if(~arstn)
            data_reg <= 0;
        else
            data_reg <= data_in;
    end
    
    always@(posedge clk_a or negedge arstn) begin
        if(~arstn)
            data_en_a <= 0;
        else
            data_en_a <= data_en;
    end
    
    always@(posedge clk_b or negedge brstn) begin
        if(~brstn) begin
            data_en_b0 <= 0;
            data_en_b1 <= 0;
        end
        else begin
            data_en_b0 <= data_en_a;
            data_en_b1 <= data_en_b0;
        end
    end
    
    always@(posedge clk_b or negedge brstn) begin
        if(~brstn) 
            dataout <= 0;
        else 
            dataout <= data_en_b1? data_reg: dataout;
    end
endmodule