We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for
Did you mean:
Highlighted
Visitor
8,318 Views
Registered: ‎03-07-2016

## using both blocking and non blocking statement

I tried the following code on spartan 3e and tested using logicstart megawing:

always@(posedge clk)
begin
if(rst)
begin
s <= 3'b01;
t <= 3'b01;
p = s - 1'b1;
q = t - 1'b1;
chkrow <= s;
chkcol <= t;
chkp = p;
chkq = q;
end
if(en)
begin
t <= t + 1'b1;
q = t - 1'b1;
chkrow <= s;
chkcol <= t;
chkp = p;
chkq = q;
end
end

output

expected: when rst =1 , s=001,t=001,p=000,q=000

when en=1,s=001,t=010,p=001,q=001

obtained:when rst =1 , s=001,t=001,p=000,q=000

when en=1 s=001,t=111,p=000,q=111

can anyone please explain why this is happening and provide a way to get the expected output.

Tags (1)
1 Solution

Accepted Solutions
Professor
13,810 Views
Registered: ‎08-14-2007

## Re: using both blocking and non blocking statement

"i got the output in hardware."

"why is q always 2 less than the value of t? i want q to be t-1 how d i get that?"

This is how a clocked process works.  When you add one to t, that happens after the clock edge.  Anything in the process that uses t will use the value of t from before the clock edge.  The fact that q uses blocking assignments does not change that.  If you wanted to use the value of t after adding 1, then t should use blocking assignments like:

t = t + 1;  // t will still increment by one (hardware doesn't change for t)

q = t - 1; // q will now be the equivalent of (t + 1) - 1

in this instance q could use non-blocking assignments and it would give the same result in hardware.  The point is that if you want something to respond to the value just assigned, and not the value from before the process started, you need to use a blocking assignment.  Here the value you want for t is the value after you increment it.  So t (not q) needs to use blocking assignments so that when t is used further down in the process it will already have the new value.

-- Gabor
3 Replies
Professor
8,310 Views
Registered: ‎08-14-2007

## Re: using both blocking and non blocking statement

How did you get the "obtained" values?  In hardware?  In simulation?  I simulated the code, and what I saw is:

When rst = 1:

s = 1, t = 1, p = 0, q = 0

when en = 1:

s = 1, t counts from 0 to 7 and starts again, p = 0, q is always 2 less than t modulo 8

I don't see how p can ever be non-zero since there is no assignment that would make it non-zero.

-- Gabor
Visitor
8,270 Views
Registered: ‎03-07-2016

## Re: using both blocking and non blocking statement

i got the output in hardware. why is q always 2 less than the value of t? i want q to be t-1 how d i get that?

Professor
13,811 Views
Registered: ‎08-14-2007

## Re: using both blocking and non blocking statement

"i got the output in hardware."

"why is q always 2 less than the value of t? i want q to be t-1 how d i get that?"

This is how a clocked process works.  When you add one to t, that happens after the clock edge.  Anything in the process that uses t will use the value of t from before the clock edge.  The fact that q uses blocking assignments does not change that.  If you wanted to use the value of t after adding 1, then t should use blocking assignments like:

t = t + 1;  // t will still increment by one (hardware doesn't change for t)

q = t - 1; // q will now be the equivalent of (t + 1) - 1

in this instance q could use non-blocking assignments and it would give the same result in hardware.  The point is that if you want something to respond to the value just assigned, and not the value from before the process started, you need to use a blocking assignment.  Here the value you want for t is the value after you increment it.  So t (not q) needs to use blocking assignments so that when t is used further down in the process it will already have the new value.

-- Gabor