题解 | #使用握手信号实现跨时钟域数据传输#

使用握手信号实现跨时钟域数据传输

https://www.nowcoder.com/practice/2bf1b28a4e634d1ba447d3495134baac

`timescale 1ns/1ns
module data_driver(
	input clk_a,
	input rst_n,
	input data_ack,

	output reg [3:0]data,
	output reg data_req
	);

reg data_ack_1,data_ack_2;
reg [2:0]   cnt;

always@(posedge clk_a or negedge rst_n)
    if(!rst_n)
        begin
            data_ack_1  <=  1'b0;
            data_ack_2  <=  1'b0;
        end
    else
        begin
            data_ack_1  <=  data_ack;
            data_ack_2  <=  data_ack_1;
        end

always@(posedge clk_a or negedge rst_n)
    if (!rst_n)
           data <= 4'd0;
    else if(data_ack_1 && !data_ack_2 && data == 4'd7)
           data <= 4'd0;
    else if(data_ack_1 && !data_ack_2)
           data <= data +1'b1;
    else
           data <= data;

always@(posedge clk_a or negedge rst_n)
    if (!rst_n)
        cnt <=  3'd0;
    else    if(data_ack_1 && !data_ack_2)
        cnt <=  3'd0;
    else    if(data_req)
        cnt <=  cnt;
    else
        cnt <=  cnt + 1'b1;

always@(posedge clk_a or negedge rst_n)
    if (!rst_n)
        data_req    <=  1'b0;
    else    if(cnt==3'd4)
        data_req    <=  1'b1;
    else    if(data_ack_1 && !data_ack_2)
        data_req    <=  1'b0;
    else
        data_req    <=  data_req;




endmodule

module	data_receiver
(
	input   wire               clk_b,
	input   wire               rst_n,
    input   wire    [3:0]       data,
    input   wire            data_req,

    output  reg  data_ack
);

reg data_req_1,data_req_2;
reg data_reg;

always@(posedge clk_b or negedge rst_n)
    if(!rst_n)
        begin
            data_req_1  <=  1'b0;
            data_req_2  <=  1'b0;
        end
    else
        begin
            data_req_1  <=  data_req;
            data_req_2  <=  data_req_1;
        end

always@(posedge clk_b or negedge rst_n)
    if(!rst_n)
        data_ack    <=  1'b0;
    else    if(data_req_1)
        data_ack    <=  1'b1;
    else
        data_ack    <=  1'b0;

always@(posedge clk_b or negedge rst_n)
    if(!rst_n)
        data_reg    <=  4'd0;
    else    if(data_req_1 && !data_req_2)
        data_reg    <=  data;
    else
        data_reg    <=  data_reg;





endmodule

全部评论

相关推荐

11-28 17:58
门头沟学院 Java
美团 JAVA开发 n×15.5
牛客786276759号:百度现在晋升很难的 而且云这块的业务没美团好 你看百度股价都跌成啥样了
点赞 评论 收藏
分享
10-27 17:26
东北大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务