异步跨时钟域握手处理(Verilog)

RTL代码:

module req_ack(
	input clk_a,
	input rst_n,
	input pulse_a,
	input clk_b,
	output pulse_b
);
	reg req;		//请求信号
	reg [2:0]req_b;
	reg ack;		//应答信号
	reg [2:0]ack_a;
	
	always@(posedge clk_a or negedge rst_n)begin
		if(!rst_n)
			req <= 1'b0;
		else if(pulse_a)
			req <= 1'b1;
		else if(~ack_a[2] & ack_a[1])
			req <= 1'b0;
		else
			req <= req;
	end
	
	//时钟域b同步时钟域a的req信号
	always@(posedge clk_b or negedge rst_n)begin
		if(!rst_n)
			req_b <= 'd0;
		else
			req_b <= {req_b[1:0],req};
	end 
	
	always@(posedge clk_b or negedge rst_n)begin
		if(!rst_n)
			ack <= 1'b0;
		else if(~req_b[2] & req_b[1])
			ack <= 1'b1;
		else if(~req_b[1] & req_b[2])
			ack <= 1'b0;
		else 
			ack <= ack;
	end 
	
	//时钟域a同步时钟域b的ack信号
	always@(posedge clk_a or negedge rst_n)begin
		if(!rst_n)
			ack_a <= 'd0;
		else
			ack_a <= {ack_a[1:0],ack};
	end
	
	assign pulse_b = req_b[1] & (~req_b[2]);
	

endmodule


仿真代码:

`timescale 1ns/1ns
module req_ack_tb;
    
    reg clk_a;
    reg clk_b;
    reg pulse_a;
    reg rst_n;
    wire pulse_b;
    
    req_ack req_ack_inst(
	   .clk_a      (clk_a),
	   .rst_n      (rst_n),
	   .pulse_a    (pulse_a),
       .clk_b      (clk_b),
	   .pulse_b    (pulse_b)
);
    
    initial clk_a = 0;
    always#10 clk_a = ~clk_a;
    
    initial clk_b = 0;
    always#50 clk_b = ~clk_b;
    
    initial begin
        rst_n = 0;
        pulse_a = 0;
        #200;
        rst_n = 1;
        #200;
        pulse_a = 1;
        #20;
        pulse_a = 0;
        #5000;
    end
    
endmodule

仿真波形:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值