Verilog 编程——事件/脉冲融合



引言

再来记录一道今年海康的实习机试题目,这个相对简单很多。关于多个不同时到达的脉冲信号融合的问题。

题目描述

eof 表示一件事情的结束脉冲,为表示两件不确定先后顺序的事情都已结束,需要将两个结束脉冲进行融合,输出一个脉冲,清完善以下代码: 

module event_merge(
input 				clk,
input 				rst_n,
input 				eof1,//脉冲信号,时长:1个clock
input 				eof2,//脉冲信号,时长:1个clock
output wire 		eof
    );

思路

最简单的思路就是对脉冲计数,当然也要考虑事件脉冲同时到达的情况,以免出现bug。如有其他思路,欢迎评论区交流~~

解题

设计代码:


// ========================================================================
// 功能描述:-1- 将两个事件脉冲融合为1个脉冲
// 作者:Xu Y. B.
// 时间:2023-05-08
// ========================================================================

`timescale 1ns / 1ps
module event_merge(
input 				clk,
input 				rst_n,
input 				eof1,//脉冲信号,时长:1个clock
input 				eof2,//脉冲信号,时长:1个clock
output wire 		eof
    );

reg [1:0] count;

always @ (posedge clk)
begin
	if(~rst_n)
	begin
		count <= 0;
	end
	else if(count == 2)
	begin
		count <= 0;
	end
	else if(&{eof1,eof2})
	begin
		count <= 2;
	end
	else if(|{eof1,eof2})
	begin
		count <= count + 1;
	end
end

assign eof = count == 2;
endmodule

仿真代码:


// ========================================================================
// 功能描述:-1- 仿真测试模块 event_merge 功能
// 作者:Xu Y. B.
// 时间:2023-05-08
// ========================================================================

`timescale 1ns / 1ps
module tb_event_merge();

reg 				clk;
reg 				rst_n;
reg 				eof1;//脉冲信号,时长:1个clock
reg 				eof2;//脉冲信号,时长:1个clock
wire 				eof;

initial clk = 0;
always #10 clk = ~clk;

initial
begin
	rst_n = 0;
	eof1 = 0;
	eof2 = 0;
	#103;
	@(posedge clk)
	rst_n <= 1'b1;

// case1  :  eof2 先到来 ,eof1 后到来 
	#1034;
	@(posedge clk)
	eof2 <= 1'b1;
	@(posedge clk)
	eof2 <= 1'b0;
	#2098;
	@(posedge clk)
	eof1 <= 1'b1;
	@(posedge clk)
	eof1 <= 1'b0;

// case2  :  eof1 先到来 ,eof2 后到来 
	#1034;
	@(posedge clk)
	eof1 <= 1'b1;
	@(posedge clk)
	eof1 <= 1'b0;
	#2098;
	@(posedge clk)
	eof2 <= 1'b1;
	@(posedge clk)
	eof2 <= 1'b0;

// case3  :  eof1 ,eof2 同时到来 
	#1034;
	@(posedge clk)
	eof1 <= 1'b1;
	eof2 <= 1'b1;
	@(posedge clk)
	eof1 <= 1'b0;
	eof2 <= 1'b0;
	#2309;
	$finish;
end

event_merge INST_event_merge (.clk(clk), .rst_n(rst_n), .eof1(eof1), .eof2(eof2), .eof(eof));

endmodule

仿真结果:



猜你喜欢

转载自blog.csdn.net/qq_43045275/article/details/130580840