【发布时间】:2022-01-17 12:50:01
【问题描述】:
我尝试使用 icarus verilog 在 EDA Playground 上制作 BCD 计数器进行模拟。在我的第一次尝试中,我在没有使用 begin 和 end 关键字的情况下编写了 always 块:
module bcdcounter(out, clock, reset);
output [3:0] out;
input clock, reset;
reg [3:0] outstate= 4'd0;
reg [1:0] state= 2'd0;
always@(posedge reset)
case(state)
2'd0: state <=2'd1;
2'd1: state <=2'd2;
2'd2: state <=2'd0;
endcase
always@(posedge clock or state)
if(!state)
outstate <= 4'd0;
if(state == 2'd1)
if(outstate != 4'd9)
outstate <= outstate +4'd1;
else
outstate <= 4'd0;
if(state == 2'd2)
outstate <= outstate;
assign out = outstate;
endmodule
我检查时生成了以下输出:
design.sv:21: syntax error
design.sv:21: error: Invalid module instantiation
design.sv:23: syntax error
design.sv:23: error: Invalid module instantiation
design.sv:25: syntax error
design.sv:25: error: Invalid module instantiation
Exit code expected: 0, received: 1
但是,一旦我添加了一些 begin 和 end 关键字,它确实可以正常工作:
module bcdcounter(out, clock, reset);
output [3:0] out;
input clock, reset;
reg [3:0] outstate= 4'd0;
reg [1:0] state= 2'd0;
always@(posedge reset)
case(state)
2'd0: state <=2'd1;
2'd1: state <=2'd2;
2'd2: state <=2'd0;
endcase
always@(posedge clock or state)
begin
if(!state)
outstate <=4'd0;
if(state==2'd1)
begin
if(outstate!=4'd9)
outstate<= outstate+4'd1;
else outstate<= 4'd0;
end
if(state==2'd2)
outstate<=outstate;
end
assign out = outstate;
endmodule
我们什么时候需要在设计模块中使用begin 和end 关键字?任何帮助将不胜感激。
【问题讨论】: