-2
\$\begingroup\$

I got two errors when using Vivado.

module top; 
 reg i_clk;
 always
 begin
 for (u=0;u<3;u=u+1){$display(" %d ",i_clk)}
 end
ERRROR: syntax error near ';'

And:

module top; 
 reg i_clk;
 integer u;
 begin
 for (u=0;u<3;u=u+1)
 $display(" %d ",i_clk);
 end
ERROR:u is not instant.

I am learning Verilog 2005 standard. I have no idea how to fix these two errors.

Must a for loop be within an initial,always block?

Velvet
4,9085 gold badges18 silver badges32 bronze badges
asked May 30 at 20:56
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

You're getting your syntax wong. Verilog is not C - it doesn't use {} for blocks, it uses begin-end.

So your first code should be:

always begin
 for (u=0;u<3;u=u+1) begin
 $display(" %d ",i_clk)
 end
end

Although I don't know what you are hoping to achieve. The code will immediately print the value of i_clk constantly, most likely causing the simulator to lock up - always begin just runs constantly with no trigger. The for loop also executes in zero time.


A for loop must be inside either a procedural block (initial/begin), a task or function, or a generate block. So the second example is again not a valid syntax.

answered May 30 at 21:19
\$\endgroup\$
2
  • \$\begingroup\$ Does begin end can be omitted when there's only one sentence? \$\endgroup\$ Commented May 30 at 21:26
  • 2
    \$\begingroup\$ @kittygirl It can be, but I prefer to always include them as it makes it easier to maintain the code. \$\endgroup\$ Commented May 30 at 22:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.