I tried making a BCD counter on EDA Playground using icarus verilog for simulation. In my first try, I coded the always block without using the begin and end keywords:
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
and the following output was generated while I checked:
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
But, once I added a few begin and end keywords, it did work without errors:
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
When do we need to use the begin and end keywords in the design modules? Any help would be appreciated.