正确答案:
module top_module (
input clk,
input slowena,
input reset,
output [3:0] q);
always @ (posedge clk)
begin
if(reset ==0)
begin
if(slowena ==1)
begin
if(q!=9) q <= q+1;
else q <=0;
end
else q <= q;
end
else q <=0;
end
endmodule
另一种代码:
module top_module (
input clk,
input slowena,
input reset,
output [3:0] q);
always @ (posedge clk)
begin
if(reset ==1) q <=0;
else if(slowena ==1 && q==9) q<=0;//删除此行会导致计数器无法正常归零
else if(slowena ==1 && q!=9) q <= q+1;
else q <= q;
end
endmodule
由于slowena的存在,不能单纯判断q==9时候归0,只有slowena==1且q==9时候才能归零。
如果删除上面后面有注释的一行代码,就会没有考虑到slowena==1,且q==9的情况,这时候应该置0,但上述代码会保持9。