1:非阻塞赋值会延后一个周期,但是这在RTL仿真是显示不出来的,在门级仿真的波形中可以很明显的看出来
图1是RTL仿真图,图2是Gate level仿真图,有很大的不同
2:按键消抖原理,设置一个计数器,在计数器计数时间范围内要包含合理长度的抖动信号然后设置一个DY1和一个DY2,设置触发条件和逻辑关系并输出即可达到消抖目的
代码(逻辑设计方法):
module delay(
input iCLK,
input iRST_n,
input iTRIG,
output oTRIG
);
reg[31:0] Delay_cnt;
reg DY1,DY2;
parameter cnt_time=32'H9;
always@(posedge iCLK or negedge iRST_n) begin
if(!iRST_n)
DY1=0;
else if(iTRIG)
DY1=1;
else if(Delay_cnt>=cnt_time)
DY1=0;
end
always@(posedge iCLK or negedge iRST_n) begin
if(!iRST_n) begin
Delay_cnt<=0;//阻塞赋值 or 非阻塞赋值
end
else if(DY1==1) begin
Delay_cnt<=Delay_cnt+1;
end
else begin
Delay_cnt<=0;
end
end
always@(posedge iCLK or negedge iRST_n) begin
if(!iRST_n)
DY2<=0;
else
DY2<=DY1;
end
assign oTRIG=~DY1&DY2;
endmodule
`timescale 1ns/1ns
`define clock_period 20
module delay_tb();
//source define
reg clk;
reg rst_n;
reg in;
//peobe define
wire out;
//instant user module
delay u1(
.iCLK(clk),
.iRST_n(rst_n),
.iTRIG(in),
.oTRIG(out)
);
//generate clock
initial clk = 1;
always #(`clock_period/2) clk = ~clk;
initial begin
rst_n=0;
in=0;
#(`clock_period*2)
rst_n=1;
#(`clock_period)
in=1;
#(`clock_period*1.5)
in=0;
#(`clock_period*1.5)
in=1;
#(`clock_period*3.5)
in=0;
#(`clock_period*11.5)
in=1;
#(`clock_period*1.5)
in=0;
#(`clock_period*1.5)
in=1;
#(`clock_period*1.5)
in=0;
#(`clock_period)
in=1;
#(`clock_period*3.5)
in=0;
#(`clock_period*10)
$stop;
end
endmodule