Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<= Assignment Operator in Verilog

Tags:

verilog

What does the <= do in Verilog?

For example:

always @(posedge Clock) begin
   if (Clear) begin
      BCD1 <= 0;
      BCD0 <= 0;
   end
end
like image 547
biw Avatar asked Nov 04 '14 04:11

biw


2 Answers

"<=" in Verilog is called non-blocking assignment which brings a whole lot of difference than "=" which is called as blocking assignment because of scheduling events in any vendor based simulators.

It is Recommended to use non-blocking assignment for sequential logic and blocking assignment for combinational logic, only then it infers correct hardware logic during synthesis.

Non-blocking statements in sequential block will infer flip flop in actual hardware.

Always remember do not mix blocking and non-blocking in any sequential or combinational block.

During scheduling process of simulator:

There are four regions and order of execution of commands as follows

1) Active region
     --Blocking assignments
     --Evaluation of RHS of non-blocking assignments(NBA)
     --Continuous assignment
     --$display command
     --Evaluate input and output of primitives
2) Inactive region
     --#0 blocking assignments
3) NBA(non-blocking assignment update)
     --update LHS of non-blocking assignments (NBA)
4) Postponed
     --$monitor command
     --$strobe command

Using of blocking assignment "=" for two variable at the same time slot causes race condition

eg: Verilog code with race condition,

always @(posedge Clock) 
   BCD0 = 0; // Usage of blocking statements should be avoided
always @(posedge Clock) 
   BCD1 = BCD0; 

In order to avoid race condition use non-blocking statement "<="

eg:

   always @(posedge Clock) 
       BCD0 <= 0; // Recommended to use NBA
    always @(posedge Clock) 
       BCD1 <= BCD0; 

When this block is executed, there will be two events added to the non blocking assign update queue. Hence, it does the updation of BCD1 from BCD0 at the end of the time step.

Using Non-blocking "<=" assignment in continuous assignment statement is not allowed according to verilog LRM and will result in compilation error.

eg:

assign BCD0 <= BCD1; //Results in compilation error

Only use NBA in procedural assignment statements,

 - initial and
 - always blocks
like image 50
Emman Avatar answered Sep 23 '22 09:09

Emman


This is called a 'non-blocking' assignment. The non-blocking assignment allows designers to describe a state-machine update without needing to declare and use temporary storage variables.

For example, in this code, when you're using a non-blocking assignment, its action won't be registered until the next clock cycle. This means that the order of the assignments is irrelevant and will produce the same result.

The other assignment operator, '=', is referred to as a blocking assignment. When '=' assignment is used, for the purposes of logic, the target variable is updated immediately.

The understand this more deeply, please look at this example (from Wikipedia):

module toplevel(clock,reset);
    input clock;
    input reset;

    reg flop1;
    reg flop2;

    always @ (posedge reset or posedge clock)
        if (reset)
        begin
            flop1 <= 0;
            flop2 <= 1;
        end
        else
        begin
            flop1 <= flop2;
            flop2 <= flop1;
        end
endmodule

In this example, flop1 <= flop2 and flop2 <= flop1 would swap the values of these two regs. But if we used blocking assignment, =, this wouldn't happen and the behavior would be wrong.

like image 32
Sadjad Avatar answered Sep 22 '22 09:09

Sadjad