## Sunday, 8 July 2012

### Blocking vs Non Blocking Assignments

I did not initially plan on adding such topics to this blog but the reason I'm covering this is because I had a great deal of difficulty coding a FIFO buffer as I also was not clear regarding this concept. I was writing writing combinational logic in sequential blocks, which I read is not a good idea.

In an always block expressions and variables can be connected using either a blocking or non-blocking statement. Knowing when to use which will make the code work. As a general rule blocking assignments are used to describe combinational logic and non-blocking are used to describe sequential logic.

### Blocking Assignment:

Simply put this follows more of a C and C++ style, that is the lines of code are processed one after an other. The syntax for this blocking assignment is:

`(variable) = (expression);`

So when this line is executed the expression on the right hand side is calculated and the value is assigned to the variable on the left hand side. While this expression is being evaluated it will block the calculation of any other expression. That is untill this line is completed it will block the calculation of any other expression. That is why it is known as blocking assignment. It will only move to the next line when the expression has been calculated and the value assigned to the variable.

Consider the example:

```module blocking(
input x,y,z,
output c
);

always @ (*)
begin
c = x;        // c takes value of x
c = c & y;    // c takes the value of x & y
c = c & z;    // c takes the value of x & y & z
end

endmodule

```

So the above code will run in a C style, the expressions are evaluated step by step. Such that in the end we get a logical AND of all three inputs. The assignments of the variables on the left hand side are explained in the comments.

### Non Blocking Assignment:

The non blocking assignments are much different that the blocking ones. They are usually used in sequential always blocks. These assignments truly describe how hardware works. The syntax for non blocking assignment is:

`(variable) <= (expression);`

An always block using non blocking assignments can be thought of as hardware. When an always block with non blocking assignments is executed, the right hand sides of all the expressions contained within it  are calculated at the start of the block. And at the end of the this always block the calculated values of these expressions are assigned to the variables on the left hand side, all at the same time. These are called non blocking because the calculation of one expression does not prevent the calculation of the next. It is not a good idea to mix blocking and non blocking in the same always block.

Now consider the same example again, but this time for clarity suppose x has a value of 10.

```module blocking(
input x,y,z,
output c
);

always @ (*)
begin            // c will take its old/previous value, whatever it may be, for clarity suppose it is 0.
c <= x;        // at the end of this always block c will take the value of x
c <= c & y;    // at the end of this always block c will tale the value of c old value (0) AND'd with y
c <= c & z;    // at the end of this always block c will tale the value of c old value (0) AND'd with z
end

endmodule

```

In this case the first two lines are meaningless and the above code can be replaced by the last line i.e. c <= c & z;

Note that one method is not better than the other and both have their importance. One cannot complete some designs without using both assignments in their code. As can be seen from my next updates.