0
\$\begingroup\$

I have the following verilog code that I came across and trying to find the sequence of evaluation. What value does 'A' have at the end of a cycle and after 5 cycles, when the value of A is 'x' at cycle 0.

always @ (posedge clk)

A <= 1;
A <= A+1;
B <= A+1;

I understand that the evaluation of RHS is done on the posedge and the assignment happens at the end of the cycle. What value would A have after the assignment? Is it '1' or 'X'?

Also does adding the delay to the statements act as blocking for the following code?

always @ (posedge clk)
begin
#5 A<=1;
#5 A<=A+1;
B <=A+1;
end
\$\endgroup\$
  • \$\begingroup\$ I edited your post to format the code blocks. Please check for correctness. I did remove two leading slashes on the lines starting with #, since I assume you put them there to avoid formatting the lines as headings, right? \$\endgroup\$ – Reinstate Monica Mar 4 at 21:44
  • \$\begingroup\$ You are missing th begin/end block. Is it intended? \$\endgroup\$ – Eugene Sh. Mar 4 at 21:46
  • \$\begingroup\$ Thanks @Justin for the edit, This interface is new to me. Missing begin/end block was intentional @Eugene Sh. \$\endgroup\$ – sxa144 Mar 4 at 22:20
0
\$\begingroup\$

I assume there is a begin/end around the three assigment statements. Otherwise the code is legal.

Because you are using non-blocking assignments (NBA) in your first example, A has the old value throughout the current time step. If you have 2 NBA to the same variable in the same timestep, the last one wins. So at the end of the timestep, A has the value A+1. After 5 cycles, it has the value A+5 Since you didn't specify how A gets initialized, that's the best answer you can get. Also realize that your code (enclosed by a single begin/end)is equivalent to

always @ (posedge clk) A <= 1;
always @ (posedge clk) A <= A+1;
always @ (posedge clk) B <= A+1;

In the second example when executing A<=A+1, A already is already updated to its new value, 1, so A would be 2. Note that blocking delays would not be allowed in synthesizable code.

\$\endgroup\$
  • \$\begingroup\$ Agree, So the value to B gets assigned before the blocking delay statements are evaluated or after ? \$\endgroup\$ – sxa144 Mar 4 at 22:57
  • \$\begingroup\$ There is no blocking delay between the second assignment to A and the assignment to B. You should try experimenting. \$\endgroup\$ – dave_59 Mar 4 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.