0
\$\begingroup\$

I was learning verilog hardware description language. I was confused a bit with the blocking and non-blocking statements. Can someone tell me what the following verilog codes realize and simple hints how

always@(posedge clk)
begin
 q1=d;
 q2<=q1;
 q3=q2;
end
always@(posedge clk)
begin
 q1<=d;
 q2<=q1;
 q3=q2;
end

These are two different codes implemented separately. I think FF comes into the picture because of edge detection. But I don't understand what it realizes as it has a intermix of blocking and non-blocking statements

asked Dec 12, 2013 at 9:34
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Let's start with d=0 q1=1 q2=2 q3=3.

always@(posedge clk)
begin
 q1=d;

q1 immediately gets the value of d=0.

 q2<=q1;

Non-blocking assignment: save the current value of q1 (which is currently the same as d=0)

 q3=q2;

q3 gets the unchanged value of q2, =2

end

At this point the nonblocking assignment happens and q2 gets the value 0.

I'm assuming the next always block is in a different module; putting it in the same module will just cause further confusion as you're overwriting the same variables again.

Start with d=0 q1=1 q2=2 q3=3 again.

always@(posedge clk)
begin
 q1<=d;
 q2<=q1;

Queue assignment of q1 to 0 and q2 to 1. These happen at the end of the block.

 q3=q2;

Set q3 to the current value of q2, ie 2.

end

Finally set q1=0 and q2=1.

Generally it's bad style to mix = and <= in the same block, and generally for synthesis you should always use <=. '=' can be helpful in testbenches where it behaves more like a conventional imperative programming language.

answered Dec 12, 2013 at 9:54
\$\endgroup\$
3
  • \$\begingroup\$ Actually this was a question asked by Freescale Semiconductor company under recruitment process but using its equivalent VHDL. Can you help me by giving the circuit that the above two codes realize \$\endgroup\$ Commented Dec 12, 2013 at 14:01
  • \$\begingroup\$ Can you clarify? You're applying for a job, and someone asked you this question? So you'll be turning this answer in to the interviewer? \$\endgroup\$ Commented Dec 12, 2013 at 17:04
  • \$\begingroup\$ In the process of interview I was posed with this question and I was not able to crack it. Now I was trying to overcome my mistakes \$\endgroup\$ Commented Dec 12, 2013 at 17:41

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.