「Verilog学习笔记」使用握手信号实现跨时钟域数据传输

发布时间:2023年12月21日
专栏前言

本专栏的内容主要是记录本人学习Verilog过程中的一些知识点,刷题网站用的是牛客网

`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_reg_1, data_ack_reg_2 ; 
	reg [9:0] cnt ; 

	always @ (posedge clk_a or negedge rst_n) begin 
		if (~rst_n) begin 
			data_ack_reg_1 <= 0 ;
			data_ack_reg_2 <= 0 ; 
		end
		else begin 
			data_ack_reg_1 <= data_ack ; 
			data_ack_reg_2 <= data_ack_reg_1 ;
		end
	end

	always @ (posedge clk_a or negedge rst_n) begin 
		if (~rst_n) data <= 0 ; 
		else if (data_ack_reg_1 && !data_ack_reg_2) data <= data + 1 ; 
		else data <= data ; 
	end

	always @ (posedge clk_a or negedge rst_n) begin 
		if (~rst_n) cnt <= 0 ; 
		else if (data_ack_reg_1 && !data_ack_reg_2) cnt <= 0 ; 
		else if (data_req) cnt <= cnt ; 
		else cnt <= cnt + 1 ;
	end

	always @ (posedge clk_a or negedge rst_n) begin 
		if (~rst_n) data_req <= 0 ; 
		else if (cnt == 4) data_req <= 1 ; 
		else if (data_ack_reg_1 && !data_ack_reg_2) data_req <= 0 ; 
		else data_req <= data_req ; 
	end

endmodule

module data_receiver (
	input clk_b, 
	input rst_n,
	input data_req,
	input [3:0] data,
	output reg data_ack
);
	reg [3:0] data_in_reg ; 
	reg data_req_reg_1, data_req_reg_2 ; 
	always @ (posedge clk_b or negedge rst_n) begin 
		if (~rst_n) begin 
			data_req_reg_1 <= 0 ;
			data_req_reg_2 <= 0 ; 
		end
		else begin
			data_req_reg_1 <= data_req ; 
			data_req_reg_2 <= data_req_reg_1 ; 
		end
	end

	always @ (posedge clk_b or negedge rst_n) begin 
		if (~rst_n) data_ack <= 0 ;
		else if (data_req_reg_1) data_ack <= 1 ; 
		else data_ack <= 0 ;
	end

	always @ (posedge clk_b or negedge rst_n) begin 
		if (~rst_n) data_in_reg <= 0 ; 
		else if (data_req_reg_1 && !data_req_reg_2) data_in_reg <= data ; 
		else data_in_reg <= data_in_reg ;
	end

endmodule
文章来源:https://blog.csdn.net/m0_54689021/article/details/135126971
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。