我想设计一个计数器,它计算到一些数字,让我们说它是3,为此,我编写了一个与“$ finish”配合使用而不是“禁用”的代码。
我想用这个计数器进行合成,所以我必须使用“禁用”语句.....
我附上了我的两个代码 - (1)$ finish完成后,可以轻松准确地停止
// Code with $finish
module counter(input wire clk);
reg [23:0]N=24'b0000_0000_0000_0000_0000_0000;
always @ (posedge clk)
begin
if (N == 24'b0000_0000_0000_0000_0000_0011)
begin
$display("Inside If N=%d in Time=",N,$time);
$finish;
end
else
begin
N <= N +1;
$display("Inside Else N=%d in Time=",N,$time);
end
end
endmodule
(2)禁用根本不停止..
// Code with disable that not stop
module counter(input wire clk);
reg [23:0]N=24'b0000_0000_0000_0000_0000_0000;
always @ (posedge clk)
begin :close
if (N == 24'b0000_0000_0000_0000_0000_0011)
begin
$display("Inside If N=%d in Time=",N,$time);
disable close;
end
else
begin
N <= N +1;
$display("Inside Else N=%d in Time=",N,$time);
end
end
endmodule
toolic和Greg都表示$ finish和disable的使用不正确,这只是为了添加这两个点并显示一个可能的解决方案,从可综合的RTL中分离测试。
module counter(input clk);
reg [23:0] N = 24'b0;
always @ (posedge clk) begin
if (N < 24'd3) begin
N <= N +1;
$display("Inside If N=%d in Time=",N,$time);
end
else begin
$display("Inside Else N=%d in Time=",N,$time);
end
end
endmodule
测试它:
module test;
//Create clk
reg clk;
initial begin
clk = 0 ;
forever begin
#5 clk = ~clk;
end
end
//instantiate DUT (Device Under Test)
counter counter_i0(
.clk( clk )
);
// The test
// Run for 6 clock cycles then end
initial begin
repeat (6) begin
@(posedge clk)
end
$finish;
end
endmodule
如果你的意思是停在计数3,我会使用十进制表示法24'd3作为常量,因为它给出了明确的意图。
如果使用==
来停止计数器,一个小故障可能会导致错过,你必须等待这个包围arround。或者使用小于比较器意味着计数目标可以在运行中进行调整,而不必担心跳过==
中的确切值并且必须等待很长时间才能使其环绕。
输入是隐式线路,不需要像这样定义它们,但如果你愿意,你可以。
把$finish
放在你的测试台的末端,而不是你的综合RTL。
$finish
停止所有正在运行的进程并结束模拟。 disable
停止一个进程及其子进程。在示例代码中,disable close
终止always块中的代码,它不会停止时钟。下一个上升沿时钟和始终模块将尝试再次运行。
有关IEEE std 1800-2012语句,请参阅$finish
§20.2;对于Disable语句,请参见第9.6.2节