题解 | #使用握手信号实现跨时钟域数据传输#
使用握手信号实现跨时钟域数据传输
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 ); //用来计算接收完成后,5个clk后,data加1 reg [2:0] cnt; always@(posedge clk_a or negedge rst_n) if(!rst_n) cnt <= 3'd0; else if(data_ack) cnt <= 3'd0; else cnt <= cnt + 3'd1; //data 0-7循环 //每次循环前,都有响应信号代表进入下一数据的发送 always@(posedge clk_a or negedge rst_n) if(!rst_n) data <= 4'd0; else if(data==4'd7) data <=4'd0; else if(cnt==3'd4 && data_ack) data <= data + 4'd1; else data <= 4'd0; //输出有效信号在被接收成功前,一直拉高 always@(posedge clk_a or negedge rst_n) if(!rst_n) data_req<= 1'd0; else if(data_ack) data_req <= 1'd0; else if(cnt==3'd4) data_req <= 1'd1; endmodule module data_receiver( input clk_b, input rst_n, input [3:0] data, input data_req, output reg data_ack ); //接收响应信号只拉高一个周期 always@(posedge clk_b or negedge rst_n) if(!rst_n) data_ack <= 1'd0; else if(data_req) data_ack <= 1'd1; else data_ack <= 1'd0; endmodule