Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Difference between <= Relational operator and <= Non blocking assignment

Status
Not open for further replies.

debasish_deka

Newbie level 3
Joined
Jan 4, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangalore
Activity points
1,303
Hi All,
I am very new to verilog and got confused between the Relational operator <= (which is less than equal to) and the Non-blocking assignment operator <=.:bang:
I want to know how does the verilog compiler know that the variables on the either side of this operator means assignment operation not comparison or the vice versa.
 

TrickyDicky

Advanced Member level 5
Joined
Jun 7, 2010
Messages
7,076
Helped
2,078
Reputation
4,173
Reaction score
2,032
Trophy points
1,393
Activity points
39,231
It will depend on context. A simple

A <=b;

Is an assignment. But

If a <= b

Will be less than or equall
 

debasish_deka

Newbie level 3
Joined
Jan 4, 2011
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Bangalore
Activity points
1,303
Thanks Tricky Dicky,
Then how'll this code behave
Code:
if(a<=b)
    d<=a;
Actually I am from microcontroller programming background. To me it seems like (a<=b) evaluates to TRUE always and finally the value of b goes to d.
Am I right?
 

rberek

Full Member level 6
Joined
May 23, 2007
Messages
339
Helped
98
Reputation
202
Reaction score
83
Trophy points
1,308
Location
Canada
Activity points
4,095
No, you are not correct.

All that bit of code says is that "if a is greater than or equal to b, assign a to d." The value of b is not changed by the comparison, and nor would it be in the microcontroller world. There is also nothing in that code to indicate that a<=b would always evaluate to TRUE. You have given no values for any of a, b or d so no assumption can be made as to the result of that comparison.


r.b.
 
Last edited:

ads-ee

Super Moderator
Staff member
Joined
Sep 10, 2013
Messages
7,836
Helped
1,814
Reputation
3,638
Reaction score
1,776
Trophy points
1,393
Location
USA
Activity points
59,183
All that bit of code says is that "if a is greater than or equal to b, assign a to d." The value of b is not changed by the comparison, and nor would it be in the microcontroller world. There is also nothing in that code to indicate that a<=b would always evaluate to TRUE. You have given no values for any of a, b or d so no assumption can be made as to the result of that comparison.
rberek, you've missed the point the OP was making, the OP thinks the if (a<=b) is an assignment a gets assigned b and the if is checking the assignment operation which is always true as b did get assigned to a.

Code:
if(a<=b)
    d<=a;
To me it seems like (a<=b) evaluates to TRUE always and finally the value of b goes to d.
Am I right?
Don't know how you would think this would be true for C, C++, Verilog, etc, Perl perhaps but certainly not Verilog or C.

To reiterate what Tricky posted:
It depends on the context.

a <= b; // assignment (notice it's on a line by itself with no "if" in front of it and it ends with a ";"), stick the value of b into a.

If (a <= b) // comparison of a less than or equal to b when true execute the next statement

assign c = (a <= b) ? d : e; // comparison of a less than or equal to b being true assigns c with d

debasish_deka said:
Actually I am from microcontroller programming background.
Then don't use for loops, while loops, tasks, and functions until you understand how to write simple Verilog RTL that describes hardware using standard coding templates. This will save you a lot of grief and hopefully keep you from making the same mistakes so many others with a software background make when they try to learn what they think is the Verilog "software" language.

Regards
 
Last edited:

rberek

Full Member level 6
Joined
May 23, 2007
Messages
339
Helped
98
Reputation
202
Reaction score
83
Trophy points
1,308
Location
Canada
Activity points
4,095
To be honest I wasn't sure how/why the OP made those conclusions but I felt it made the best use of my time explaining the correct interpretation rather than fall down a rathole trying to figure out the incorrect one.

Also I misinterpreted the following sentence:

and finally the value of b goes to d

I now see they meant to say that d gets the value of b rather than the reverse.

r.b.
 
Last edited:

rberek

Full Member level 6
Joined
May 23, 2007
Messages
339
Helped
98
Reputation
202
Reaction score
83
Trophy points
1,308
Location
Canada
Activity points
4,095
I've used it a fair bit, I must just never have been confused by that. I have, however, been confused in lots of other ways!

r.b.
 

dave_59

Advanced Member level 3
Joined
Dec 15, 2011
Messages
817
Helped
361
Reputation
726
Reaction score
355
Trophy points
1,353
Location
Fremont, CA, USA
Activity points
6,671
It's very simple: the compiler uses the BNF to parse text token by token and recognizes what are called productions . A good example of this is the text

Code:
A <= B <= C

If you were to send just this text through a Verilog compiler, you would get the following error:
** Error: abc.v(1): near "A": syntax error, unexpected IDENTIFIER
That's because at the top level of a Verilog source file, there are only three productions available
Code:
source_text ::= { description }
description ::=
  module_declaration
| udp_declaration
| config_declaration
which basically means the first word in a Verilog source descriptions can only be one of module, primitive, or config. The compiler could not match any of the productions with the token "A", so it throws a syntax error.
Now lets send the following code to the compiler:
Code:
module test;
A <= B <= C
you would get the following error
** Error: abc.v(4): near "<=": syntax error, unexpected <=, expecting IDENTIFIER
Note the error is now on the "<=" token and not the "A". That's because inside a module_declaration, you are allowed any number of module_item productions, one of which could have been a module_instantiation like
Code:
A inst_name();
But there no module_item production that matches an identifier followed by '<='. So you get an error.

This process of parsing all your source text in productions keeps going until there is is no ambiguity left in what a token represents. Skipping ahead to a block of code that looks like
Code:
begin
  A <= B <= C;
end
The compiler is parsing a set of procedural statements, or statement_item productions. One of the production choices is
Code:
nonblocking_assignment ::=
  variable_lvalue <= [ delay_or_event_control ] expression
There is no expression production in a statement_itemso the first '<=' can only be matched as a nonblocking assignment construct. When the compiler parses the B <= C part of the statement, the only thing <= could match in an expression would be a less-than-or-equal operator.

Hope this helps.
 

mrflibble

Advanced Member level 5
Joined
Apr 19, 2010
Messages
2,724
Helped
679
Reputation
1,360
Reaction score
651
Trophy points
1,393
Activity points
19,551
Re: Difference between &lt;= Relational operator and &lt;= Non blocking assignment

Bonus points for having the patience to type what I thought. :) After 1 paragraph of typing I went "ah screw it, people just have to read the fine LRM" and pressed delete. ;)

- - - Updated - - -

And speaking of reading the fine LRM ... just google "verilog 2001 lrm" to find the standard for verilog 2001. And you'll run into the SystemVerilog documentation as well that way. Or grab it here:
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top