Guest
Thanks everone for clearing few things for me. Really great info.
Thanks again
Thanks again
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
There are strong arguments the rule you cite. One it is simple.However, if you swap these two lines:
c <= a;
a = 1;
then races appear.
Since it is easy to make the "ordering mistake", one of verilog coding
guidelines is:
"Do not mix blocking and nonblocking assignments in the same always
block".
Regards,
-Alex
For me, it is very serious argument. The idea is to simplify the rules"Alex" <agnusin@gmail.com> writes:
There are strong arguments the rule you cite. One it is simple.
If I understand it correctly, you combine combinatorial and sequentialHowever, we use an alternate rule, all blocking assignments must
appear before all non-blocking assignments in any given always block.
Or in other words, after a non-blocking assignment, one cannot use a
blocking assignment. We have a tool that enforces this restriction.
It's a slightly more terse coding style than forcing blocking and
non-blocking assignments being in separate blocks and still avoids
races.
If one visualizes blocking assignments as representing combinatorial
code, and non-blocking assignments as state elements (flip-flops,
register files, etc.). Then putting all the blocking assignment
statements at the front of the block and the blocking assignments at
the back, effectively says there are some combinatorial elements whose
outputs are saved in state devices. The clock edge on the always
block tells you what the state devices synchronize on. It's a pretty
straight forward model of either a Mealey or a Moore machine, which
ever one has no gates on the output side of the flops. If you want
gated outputs from the flops, you put them in continuous assignments
or an always block with no clock.
From the original code, it looks like that was the style the first
poster was following.
Note, we also disallow variables assigned in a blocking assignment of
a clocked always block being used outside that block and disallow
cyclical references that don't go through a non-blocking assignment.
We also require all non-blocking assignments to be in a clocked always
block. Again, the tool enforces these restrictions. If you follow
these conventions, one can easily identify the state elements (and
their clocks) in the Verilog--they're assigned to in the non-blocking
assignments. Everything else is combinatorial.
-Chris
I don't see the race condition here. This first statement will scheduleHowever, if you swap these two lines:
c <= a;
a = 1;
then races appear.
Alex overstated the case.
I agree. The code
a = 1;
c <= a;
is race-free, since the first assignment blocks the execution of the
rest of statements (including nonblocking ones) within the same block.
However, if you swap these two lines:
c <= a;
a = 1;
then races appear.
Since it is easy to make the "ordering mistake", one of verilog coding
guidelines is:
"Do not mix blocking and nonblocking assignments in the same always
block".
Regards,
-Alex
In a single always block, blocking assignments are executed in order.
That's why they are called "blocking". They block the execution of
other statements within the same always block until the assignment
completes. The semantics of blocking assignments are designed to
mimic the semantics of other familiar languages like C.
-Alex
Chris F Clark wrote:
Alex writes:
so if we have something like this:
a = 1;
c <= a;
is that means we have race conditions. since now a is set and a is
evaluated for non-blocking in random order. sometimes c will set to 1
and sometimes it will not.
Yes, that's correct. As you pointed out, it results from the
non-specified ordering in Q1:
ankitks@yahoo.com writes:
if this leads to race condition, than why this FSM is not!
state <= next;
and next = CENT5 or..
Alex overstated the case.
In a single always block, blocking assignments are executed in order.
That's why they are called "blocking". They block the execution of
other statements within the same always block until the assignment
completes. The semantics of blocking assignments are designed to
mimic the semantics of other familiar languages like C.
Note, this does not hold between different always blocks or between
always blocks and continuous assignments. There are also other rules
about synchronization that are more subtle, but if you assume
non-determinism you will always be safe, because the deterministic
choice is always one of the non-deterministic choices.
Hope this helps,
-Chris
*****************************************************************************
Chris Clark Internet : compres@world.std.com
Compiler Resources, Inc. Web Site : http://world.std.com/~compres
23 Bailey Rd voice : (508) 435-5016
Berlin, MA 01503 USA fax : (978) 838-0263 (24 hours)
------------------------------------------------------------------------------
(A) - Evaluate RHS of all non-blocking assignmentsHowever, if you swap these two lines:
c <= a;
a = 1;
then races appear.
I don't see the race condition here. This first statement will schedule
a non-blocking update for "c" to whatever "a" was set to previously,
and then "a" will be set to 1 by the blocking assignment. Perhaps I'm
missing something.
From my previous post:
Q1 - (in any order) :
David Walker
Alex wrote:
Alex overstated the case.
I agree. The code
a = 1;
c <= a;
is race-free, since the first assignment blocks the execution of the
rest of statements (including nonblocking ones) within the same block.
However, if you swap these two lines:
c <= a;
a = 1;
then races appear.
Since it is easy to make the "ordering mistake", one of verilog coding
guidelines is:
"Do not mix blocking and nonblocking assignments in the same always
block".
Regards,
-Alex
In a single always block, blocking assignments are executed in order.
That's why they are called "blocking". They block the execution of
other statements within the same always block until the assignment
completes. The semantics of blocking assignments are designed to
mimic the semantics of other familiar languages like C.
-Alex
Chris F Clark wrote:
Alex writes:
so if we have something like this:
a = 1;
c <= a;
is that means we have race conditions. since now a is set and a is
evaluated for non-blocking in random order. sometimes c will set to 1
and sometimes it will not.
Yes, that's correct. As you pointed out, it results from the
non-specified ordering in Q1:
ankitks@yahoo.com writes:
if this leads to race condition, than why this FSM is not!
state <= next;
and next = CENT5 or..
Alex overstated the case.
In a single always block, blocking assignments are executed in order.
That's why they are called "blocking". They block the execution of
other statements within the same always block until the assignment
completes. The semantics of blocking assignments are designed to
mimic the semantics of other familiar languages like C.
Note, this does not hold between different always blocks or between
always blocks and continuous assignments. There are also other rules
about synchronization that are more subtle, but if you assume
non-determinism you will always be safe, because the deterministic
choice is always one of the non-deterministic choices.
Hope this helps,
-Chris
*****************************************************************************
Chris Clark Internet : compres@world.std.com
Compiler Resources, Inc. Web Site : http://world.std.com/~compres
23 Bailey Rd voice : (508) 435-5016
Berlin, MA 01503 USA fax : (978) 838-0263 (24 hours)
------------------------------------------------------------------------------
No. Mr. Walker is exactly right, there is no race here so longdbwalker0min@gmail.com wrote:
However, if you swap these two lines:
c <= a;
a = 1;
then races appear.
I don't see the race condition here. This first statement will schedule
a non-blocking update for "c" to whatever "a" was set to previously,
and then "a" will be set to 1 by the blocking assignment. Perhaps I'm
missing something.
From my previous post:
Q1 - (in any order) :
(A) - Evaluate RHS of all non-blocking assignments
(B) - Evaluate RHS and change LHS of all blocking assignments
c <= a; // (1)
a = 1; // (2)
Since (1) is non-blocking, (1) and (2) are in fact processed
simultaneously.
Agreed.However, if you swap these two lines:
c <= a;
a = 1;
then races appear.
I don't see the race condition here. This first statement will schedule
a non-blocking update for "c" to whatever "a" was set to previously,
and then "a" will be set to 1 by the blocking assignment.
I am trying to educate myself not from implementation (existing verilogVerilog execution order is nondeterministic in some cases, but some
people seem to be greatly exaggerating this.
The stratified event queue implies it. The non-blocking assignment(1) is indeed speaking about in-order execution of all statements in
the block - either blocking or non-blocking. However, it is not
speaking about the ordering of ASSIGNMENTS.
Then, (2) is asserting, that "non-blocking ASSIGNMENTS shall be
performed in the order the statements were executed".
Also, it is clear that blocking assignments execute in-order.
Is there any statement in the standard which clearly defines ordering
rules for intermixed blocking/nonblocking asignments within the same
block?
Here is the definition of stratified queue from Verilog standard:The stratified event queue implies it. The non-blocking assignment
values are placed into the nb-assign event queue. That queue is
not processed until all statements have advanced as far as they
can. Only after all the statements are blocked waiting on events
will the nb-assign queue start its work.