3.5.4 · HinglishHDL & Digital Design Flow

Blocking vs non-blocking assignments

1,721 words8 min readRead in English

3.5.4 · Hardware › HDL & Digital Design Flow


YEH distinction exist kyun karti hai?

HAR EK KYA karta hai (scheduling rule):

Blocking = Non-blocking <=
RHS ka evaluation turant time step ke shuru mein sample hota hai
LHS ka update turant, agli statement se pehle time step ke end tak defer hota hai
Baad ki statements naya value dekhti hain? Haan Nahi (woh purana value dekhti hain)
Models combinational logic / sequential software-jaisi steps edge-triggered flip-flops

SCHEDULER actually isse kaise chalata hai (first principles)

Verilog simulation mein har time step par regions hote hain. Simplified:

  1. Active regionblocking assignments evaluate aur apply karo; non-blocking assignments ki RHS evaluate karo aur result stash karo.
  2. NBA (non-blocking) region — saare stashed non-blocking updates apply karo.

Figure — Blocking vs non-blocking assignments

Worked Example 1 — Do registers swap karna (classic)

always @(posedge clk) begin
    a <= b;
    b <= a;
end

Kya hota hai? Edge par, pehle RHS sample hota hai: a ki nayi value = purana b, b ki nayi value = purana a. Phir dono update hote hain. Result: values swap hoti hain. ✔

Yeh step kyun? Kyunki <= line 2 mein purana a use karta hai, woh value nahi jo line 1 ne abhi assign ki.

Ab blocking ke saath:

always @(posedge clk) begin
    a = b;   // a becomes old b
    b = a;   // a is ALREADY b, so b = b  -> NO swap!
end

Yeh step kyun? = line 1 turant apply karta hai, isliye line 2 naya a dekhti hai. Dono end mein purana b ke barabar ho jaate hain. Yeh ek single flop plus ek wire synthesize karta hai — swap nahi.


Worked Example 2 — Pipeline / shift register

Goal: q1 -> q2 -> q3 har clock par shift kare.

always @(posedge clk) begin
    q3 <= q2;
    q2 <= q1;
    q1 <= d;
end

Yeh order se independent kyun kaam karta hai? Saare RHS (q2, q1, d) pre-edge values se sample hote hain, phir apply hote hain. Toh q3 ko purana q2 milta hai, wagera — ek sach mein 3-stage shift register. Teeno lines ko reorder karne par same hardware milega.

Blocking = ke saath (yahan galat):

q1 = d; q2 = q1; q3 = q2;   // q3 = q2 = q1 = d ALL in one clock -> collapses!

Yeh kyun fail hota hai? Turant updates d ko ek hi cycle mein q3 tak ripple kar dete hain → aapne ek flip-flop banaya, teen nahi.


Worked Example 3 — Combinational logic (blocking sahi hai)

always @(*) begin
    tmp = a & b;    // intermediate
    y   = tmp | c;  // uses just-computed tmp
end

Yahan blocking kyun? Yeh ek data-flow hai: tmp ready hona chahiye pehle, phir y use kare, bilkul gates mein ripple ki tarah. <= use karne par y purana tmp use karega, jo simulation mein galat/latched behaviour produce karega.


Golden Rules (80/20 — inhe memorize karo)


Common Mistakes (Steel-manned)


Recall Feynman: 12-saal ke bachche ko samjhao

Socho ek class partner-swap dance kar rahi hai. Blocking = teacher bacche A ko kehti hai "ja, B ki jagah khada ho ja," aur A turant move karta hai; phir B ko kehti hai "ja, A ki jagah jao" — lekin A already move kar chuka hai, toh B, A ki nayi jagah copy karta hai. Dono ek hi jagah end hote hain — swap nahi! Non-blocking = teacher pehle ek photo leti hai, phir kehti hai "sab log, photo mein jahan tumhara partner tha, wahaan jao." Ab woh truly swap karte hain, kyunki sabne purani picture dekhi. Clocked chips photo version ki tarah kaam karti hain: har tick par, saare flip-flops "pehle wali" picture dekhte hain aur phir ek saath jump karte hain.


Active Recall

always block ke andar = (blocking) operator kya karta hai?
RHS evaluate karta hai aur LHS turant update karta hai, agli statement chalane se pehle (ordered, software-jaisa).
<= (non-blocking) kya karta hai?
Time step ke shuru mein saare RHS values sample karta hai, phir end mein saare LHS ek saath update karta hai — baad wali statements phir bhi purane values dekhti hain.
Clocked/sequential logic ke liye kaun sa assignment type?
Non-blocking <=.
Combinational logic (always @(*)) ke liye kaun sa assignment type?
Blocking =.
a<=b; b<=a; correctly swap kyun karta hai?
Dono RHS pre-edge (purani) values use karte hain, phir dono simultaneously update hote hain, toh a↔b exchange hota hai.
Blocking a=b; b=a; swap karne mein kyun fail karta hai?
a turant update hota hai, toh line 2 naya a use karta hai; dono purana b ban jaate hain.
Kya non-blocking statements ka order result affect karta hai?
Nahi — saare RHS kisi bhi LHS update se pehle sample hote hain, isliye order irrelevant hai.
Shift register mein = use karne se kaun sa hardware bug aata hai?
Data ek hi clock mein saare stages se ripple karta hai, bahut saare flip-flops ko ek mein collapse kar deta hai.
Clock aur operator ko link karne wala golden rule (mnemonic)?
"Clock? Non-blocK" — posedge blocks <= use karte hain.
Ek variable ke liye = aur <= mix karne se problems kyun aati hain?
Isse ordering/race ambiguity aur simulation–synthesis mismatch create hota hai.

Connections

Concept Map

uses

uses

RHS applied immediately

sample RHS then defer

synthesizes to

synthesizes to

modeled by

Active region

NBA region

reproduces

enables

breaks

Verilog always block

Blocking assignment =

Non-blocking assignment <=

Later stmts see new value

Later stmts see old value

Combinational logic chain

Parallel flip-flops

Scheduler regions

Sample and stash RHS

Apply all LHS together

Correct register swap