Tu jab Python mein a + b likhta hai, tab actually lakhon transistors ek saath switch karte hain. Har transistor ek chota switch hai — ya to band hai (0) ya khula hai (1). Bas itna. Aur in switches ko combine karke bante hain logic gates, gates se bante hain adders, adders se bante hain ALU, aur ALU se banta hai pura CPU.
Ye course gates se datapath tak ka safar karaata hai. Tujhe A[i]=B[i]+C[i] ko Python se leke transistors tak trace karna aana chahiye. Aaj ka kaam: gates aur Boolean algebra.
Boolean algebra ek aisa math hai jahan sirf do values hote hain: 0 aur 1. George Boole ne 1847 mein ye system banaya tha for logical reasoning. Phir 1937 mein Claude Shannon ne prove kiya ki ye EXACTLY electronic circuits describe karta hai!
| Symbol | Hardware Meaning | Software Meaning | Physical Meaning |
|---|---|---|---|
| 1 | HIGH voltage (~3.3V) | TRUE | Switch ON |
| 0 | LOW voltage (~0V) | FALSE | Switch OFF |
Gate ek electrical circuit hai jo Boolean operation karta hai. Aaj 7 gates padho — yahi teri poori digital duniya hai.
Series mein do switches ki tarah. Dono ON hone chahiye tab hi output ON. Real example: "Alarm tab baje jab door bhi khuli HO aur motion bhi detect hua HO."
| A | B | Y = A·B | Explanation |
|---|---|---|---|
| 0 | 0 | 0 | Dono switch band → current nahi |
| 0 | 1 | 0 | Pehla band → current nahi |
| 1 | 0 | 0 | Doosra band → current nahi |
| 1 | 1 | 1 | Dono ON → current jaata hai! |
Parallel mein do switches ki tarah. Koi bhi ek ON ho tab output ON. Real example: "Light on karo jab presence sensor OR motion sensor trigger ho."
| A | B | Y = A+B | Explanation |
|---|---|---|---|
| 0 | 0 | 0 | Dono band → current nahi (SIRF yahi case 0 hai!) |
| 0 | 1 | 1 | B ON hai kafi hai |
| 1 | 0 | 1 | A ON hai kafi hai |
| 1 | 1 | 1 | Dono ON, definitely 1 |
Simplest gate. Ek input, ek output — bas flip karo. Real example: "Active-LOW LED: logic 0 pe jalti hai, 1 pe bujhti hai." Uses: complement banane ke liye, active-low signals ke liye.
| A | Y = A' |
|---|---|
| 0 | 1 |
| 1 | 0 |
Exclusive OR. Output 1 tab jab inputs alag hain. Output 0 tab jab inputs same hain. Real uses: adder ka SUM bit, parity checker, cryptography.
| A | B | Y = A⊕B | Why? |
|---|---|---|---|
| 0 | 0 | 0 | Same (both 0) → 0 |
| 0 | 1 | 1 | Different → 1 |
| 1 | 0 | 1 | Different → 1 |
| 1 | 1 | 0 | Same (both 1) → 0 (yahi "exclusive" hai!) |
| Gate | Formula | Meaning | 00 | 01 | 10 | 11 |
|---|---|---|---|---|---|---|
| NAND | ~(A·B) | NOT AND — AND ka ulta | 1 | 1 | 1 | 0 |
| NOR | ~(A+B) | NOT OR — OR ka ulta | 1 | 0 | 0 | 0 |
| XNOR | ~(A⊕B) | Same = 1, Different = 0 | 1 | 0 | 0 | 1 |
In laws se hum complex expressions simplify karte hain → kam gates = chhota chip, kam power, tez circuit.
| Law Name | AND Form | OR Form | Intuition |
|---|---|---|---|
| Identity | A · 1 = A | A + 0 = A | 1 se AND ya 0 se OR → kuch nahi badla |
| Null/Dominance | A · 0 = 0 | A + 1 = 1 | 0 se AND = always 0. 1 se OR = always 1 |
| Idempotent | A · A = A | A + A = A | Khud se operate = no change |
| Complement | A · A' = 0 | A + A' = 1 | Opposite combine = extreme result |
| Double Negation | (A')' = A | Do baar NOT = original | |
| Commutative | AB = BA | A+B = B+A | Order doesn't matter (addition ki tarah) |
| Associative | (AB)C = A(BC) | (A+B)+C = A+(B+C) | Grouping doesn't matter |
| Distributive | A(B+C) = AB+AC | A+(BC) = (A+B)(A+C) | AND distributes over OR, and vice versa! |
| Absorption | A(A+B) = A | A+AB = A | "A already included in AB" — redundant term drops! |
| Consensus | AB + A'C + BC = AB + A'C | BC is consensus term — always redundant! | |
F=0 wali rows mein MAXTERMS hote hain. Har maxterm mein: variable=1 → complement, variable=0 → as-is. Phir AND karo sab maxterms ko.
Har extra gate = extra silicon area, extra power, extra heat, aur extra delay. Ek mobile phone mein 10 billion transistors hain. Agar har function 2x zyada gates use kare → phone ka battery 2x tez khatam ho, aur chip 2x costly ho. Boolean minimization directly money aur performance se linked hai!
K-map ek grid hai jo truth table ka visual version hai. Adjacent cells mein single bit change hoti hai (Gray code!). Adjacent 1s ko group karke, combined Boolean term result mein kam variables hote hain.
| AB\C | C=0 | C=1 |
|---|---|---|
| AB=00 | m0 (ABC=000) | m1 (ABC=001) |
| AB=01 | m2 (ABC=010) | m3 (ABC=011) |
| AB=11 | m6 (ABC=110) | m7 (ABC=111) |
| AB=10 | m4 (ABC=100) | m5 (ABC=101) |
F(A,B,C,D) = Σm(0,2,5,7,8,10,13,15)
Bubble pushing = DeMorgan ka visual version. Gate ke output pe bubble lagaao → gate type flip karo aur sab inputs pe bubble lagaao.
Koi bhi CPU ek gate at a time design nahi karta. Verified blocks banao — MUX, decoder, adder — aur phir in blocks ko compose karo. Ek verified MUX = infinite trusted copies. Jaise LEGO bricks: individual bricks simple, but mila ke complex cheez banti hai.
MUX = "N inputs mein se exactly 1 choose karke output pe daal do." Select lines decide karti hain kaunsa. Real-world analogy: railway switch — do tracks mein se ek choose karta hai.
2n:1 MUX = ANY n-variable Boolean function! Feed variables into SELECT lines, feed truth table values as DATA inputs.
n-bit input → 2n outputs. Exactly ONE output HIGH at any time (one-hot output). Input = which output goes HIGH.
Har bit ki value = 2position. MSB se shuru:
| Bit | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
|---|---|---|---|---|---|---|---|---|
| Weight | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
42 ko binary mein convert karo:
Problem: bits sirf 0 ya 1 hoti hain. Negative numbers kaise?
| Representation | +5 (8-bit) | −5 (8-bit) | Range (8-bit) | Problem |
|---|---|---|---|---|
| Sign-Magnitude | 0000 0101 | 1000 0101 | −127 to +127 | Two zeros! (+0 and −0). Complex adder needed. |
| One's Complement | 0000 0101 | 1111 1010 | −127 to +127 | Two zeros again! End-around carry in addition. |
| Two's Complement ✔ | 0000 0101 | 1111 1011 | −128 to +127 | ONE zero. Regular binary adder works. WINNER! |
Yahi two's complement ka magic hai: same hardware for positive AND negative numbers.
Circuit mein memory kaise aati hai? Output ko input mein wapas connect karo = FEEDBACK LOOP.
Do stable states possible hain: "Q=0, Q'=1 stable" aur "Q=1, Q'=0 stable". Yahi memory hai!
Do NOR gates, cross-coupled. Har gate ka output doosre gate ka input feed karta hai.
| S | R | Q (next) | Action | Why? |
|---|---|---|---|---|
| 0 | 0 | Q (hold) | HOLD | Dono NOR inputs same, output maintains. Perfect memory! |
| 1 | 0 | 1 | SET | S=1 forces Q=1. Even after S goes back to 0, Q stays 1 (memory!) |
| 0 | 1 | 0 | RESET | R=1 forces Q=0. Memory again. |
| 1 | 1 | ??? | FORBIDDEN | Both outputs go to 0. Q = Q' = 0 → breaks Q ≠ Q' invariant! |
D latch = SR latch + enable gate + clever internal wiring: S = D·E and R = D'·E. Since D and D' are always opposite, S and R can NEVER both be 1. Forbidden state = impossible!
| Feature | D Latch | D Flip-Flop |
|---|---|---|
| Control signal | Enable (level) | Clock (edge) |
| When output updates | Entire HIGH window of Enable | Exactly ONE rising edge instant |
| Transparent? | YES — D wiggles = Q wiggles | NO — Q frozen between edges |
| Glitch propagation? | YES | NO |
| Used in modern CPUs? | Rarely (SRAM cells, time borrowing) | EVERYWHERE — all registers! |
At rising clock edge: Q ← D (sampled once). Between edges: Q is frozen, regardless of what D does.
| Type | Inputs | Char. Equation | Primary Use | Special |
|---|---|---|---|---|
| SR | S, R | Q+ = S + R'Q (if SR'=invalid) | Basic memory, latches | S=R=1 = forbidden! |
| D | D | Q+ = D | Registers, pipelines, state FFs. THE DEFAULT. | Simplest, no forbidden state |
| JK | J, K | Q+ = JQ' + K'Q | Universal — can do SR, D, T | J=K=1 → TOGGLE! No forbidden. |
| T | T | Q+ = T ⊕ Q | Counters (T=1 toggles every clock) | Built from JK (J=K=T) or D (D=T⊕Q) |
Flip-flop ek snapshot leta hai clock edge pe. Usi snapshot ke around D stable honi chahiye, warna blurry photo!
N D flip-flops, sab ek hi clock share karte hain. N bits store karta hai. All N bits update SIMULTANEOUSLY on same clock edge = atomic update.
Plain DFF grabs D on EVERY clock edge. But register should hold value for many cycles, update only when told. Solution: 2:1 MUX per bit at D input.
FFs chain karo: one FF's Q = next FF's D. Clock edge pe, har bit ek position aage shift hoti hai.
| Type | Input Mode | Output Mode | Key Application | Direction |
|---|---|---|---|---|
| SISO | Serial (1 bit) | Serial (1 bit) | Delay line, pipeline buffer | Data waits N clocks |
| SIPO | Serial (1 bit) | Parallel (all) | UART Receiver, SPI | Build up parallel word |
| PISO | Parallel (all) | Serial (1 bit) | UART Transmitter, SPI | Break down parallel word |
| PIPO | Parallel (all) | Parallel (all) | Normal register! | Load all, read all |
Bit-0 FF toggling every clock. Bit-1 FF clocked by Bit-0's output. Bit-2 clocked by Bit-1. Etc.
All FFs share same clock. Next-state logic computed in PARALLEL. All FFs update simultaneously.
Shift register + XOR feedback from specific taps → generates pseudo-random bit stream.
FSM = Finite State Machine. Any circuit ki general blueprint. States + transitions + outputs.
Register file = CPU ke andar multiple registers ka collection with efficient read/write mechanism.
Why async? Speed! In one clock cycle, CPU needs to: read 2 operands → ALU computes → write result. Reads must be instant (combinational) or there won't be time for ALU.
| Feature | Moore Machine | Mealy Machine |
|---|---|---|
| Output depends on | Current STATE only | State + Current INPUT |
| Where output written | INSIDE the state circle | ON the transition arrow |
| Output changes when | State changes (clock edge) | Input changes (immediate!) |
| Glitchy? | NO — output stable between edges | YES — can glitch when input glitches |
| Number of states | More (separate state for each output) | Fewer (output on edges) |
| Response speed | 1 cycle delay (output after state) | Same cycle (combinational) |
| Design preference | Safer, easier to verify | Faster response, fewer states |
| State | Meaning | x=0 → Next | x=1 → Next | Output Z | Why the transition? |
|---|---|---|---|---|---|
| S0 | Idle/Reset | S0 | S1 | 0 | x=1 starts our pattern; x=0 can't start 1011 |
| S1 | Got "1" | S2 | S1 | 0 | x=0 → "10" so far; x=1 → "11" = still have 1 (last 1 valid) |
| S2 | Got "10" | S0 | S3 | 0 | x=1 → "101"; x=0 → "100" = no prefix of 1011 → back to S0 |
| S3 | Got "101" | S2 | S4 | 0 | x=1 → "1011" done!; x=0 → "1010" = "10" is prefix → S2 |
| S4 | Got "1011" ✓ | S2 | S1 | 1 | x=0 → "10110" = "10" so far; x=1 → "10111" = last "1" could start new |
Verilog = Hardware Description Language (HDL). Ye ek programming language nahi hai — ye ek wiring diagram ka textual representation hai. Jab tu Verilog likhta hai, tu CPU ke andar wires aur gates describe kar raha hota hai, not steps.
assign y = a & b; ye ek line nahi hai jo "execute" hoti hai — ye ek WIRE hai jo a aur b ke beech ek AND gate se permanently connected hai. Jab a ya b change hote hain, y TURANT change hoti hai — koi delay nahi, koi sequence nahi.Verilog mein har circuit ek module hai. Module = black box with inputs and outputs. Andar wires aur logic. Bahar sirf ports.
| Op | Symbol | Example | Use | Result |
|---|---|---|---|---|
| AND | & | a & b | AND gate | 1 if both 1 |
| OR | | | a | b | OR gate | 1 if any 1 |
| NOT | ~ | ~a | Inverter | Flip |
| XOR | ^ | a ^ b | XOR gate | Different=1 |
| XNOR | ~^ | a ~^ b | XNOR gate | Same=1 |
| NAND | ~( & ) | ~(a&b) | NAND gate | NOT AND |
| NOR | ~( | ) | ~(a|b) | NOR gate | NOT OR |
| Ternary | ?: | s?b:a | 2:1 MUX | s=1→b, s=0→a |
| Concat | {} | {a,b,c} | Bit join | MSB first |
| Replicate | {n{x}} | {4{a[3]}} | Sign extend | 4 copies |
| Red-AND | &x | &q | All bits 1? | 1 if all bits=1 |
| Red-OR | |x | |q | Any bit 1? | 1 if any bit=1 |
Testbench ek special module hai jiska koi port nahi hota — ye sirf simulation ke liye hai. Andar DUT (Design Under Test) instantiate hoti hai aur stimulus diya jaata hai.
iverilog -o sim.vvp and_gate.v and_gate_tb.v — Compilevvp sim.vvp — Run simulationgtkwave and_tb.vcd — View waveformsCPU ke andar har jagah MUX aur DEMUX hain:
| S | Y | Meaning |
|---|---|---|
| 0 | I0 (=A) | A selected |
| 1 | I1 (=B) | B selected |
| S1 | S0 | Y |
|---|---|---|
| 0 | 0 | I0 |
| 0 | 1 | I1 |
| 1 | 0 | I2 |
| 1 | 1 | I3 |
DEMUX = Data Distributor. 1 input, routed to one of many outputs. Unselected outputs = 0.
| S | Y0 | Y1 |
|---|---|---|
| 0 | D | 0 |
| 1 | 0 | D |
| S1 | S0 | Y0 | Y1 | Y2 | Y3 |
|---|---|---|---|---|---|
| 0 | 0 | D | 0 | 0 | 0 |
| 0 | 1 | 0 | D | 0 | 0 |
| 1 | 0 | 0 | 0 | D | 0 |
| 1 | 1 | 0 | 0 | 0 | D |
| A | B | Sum | Cout | Binary |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0+0=00 |
| 0 | 1 | 1 | 0 | 0+1=01 |
| 1 | 0 | 1 | 0 | 1+0=01 |
| 1 | 1 | 0 | 1 | 1+1=10 ⚠ |
| A | B | Cin | Sum | Cout | Rule |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 1 | 0 | 1 |
| 0 | 1 | 0 | 1 | 0 | 1 |
| 0 | 1 | 1 | 0 | 1 | 2 |
| 1 | 0 | 0 | 1 | 0 | 1 |
| 1 | 0 | 1 | 0 | 1 | 2 |
| 1 | 1 | 0 | 0 | 1 | 2 |
| 1 | 1 | 1 | 1 | 1 | 3 |
Sum=1 when odd number of inputs are 1 (parity). Cout=1 when 2+ inputs are 1 (majority).
Idea: Chain 4 FAs. Bit-0 FA cout goes to bit-1 FA cin. Carry "ripples" stage by stage.
| Case | Inputs | Sum | CF | OF | Note |
|---|---|---|---|---|---|
| 1 | 0001+0001 | 0010 | 0 | 0 | 1+1=2, all fine |
| 2 | 1111+0001 | 0000 | 1 | 0 | Unsigned wrap, signed −1+1=0 ok |
| 3 | 0111+0001 | 1000 | 0 | 1 | +7+1 should be +8 but got −8! |
| 4 | 1000+1000 | 0000 | 1 | 1 | Both CF and OF! Unsigned 8+8=16, Signed −8+−8=−16 both wrong |
c1 & c2 instead of c1 | c2! AND requires BOTH carries=1 which never happensFeedback = wire an output back into an input. With the right topology, two stable states exist → the circuit can hold either → memory!
Made of 2 cross-coupled NOR gates. Each gate's output feeds the other's input. This mutual reference = storage.
Equations: Q = ~(R|Q') Q' = ~(S|Q) When S=R=0: Q=~Q', Q'=~Q (consistent → stable!)
| S | R | Q(next) | Action | Notes |
|---|---|---|---|---|
| 0 | 0 | Q_prev | HOLD | Both NORs in hold. Q keeps its value! |
| 1 | 0 | 1 | SET | S=1 forces Q=1 |
| 0 | 1 | 0 | RESET | R=1 forces Q=0 |
| 1 | 1 | ?? | FORBIDDEN! | Both outputs driven to 0. Q=Q'=0 breaks invariant. On release: race! |
D latch = SR latch + enable gate. Internally: S = D·E, R = D'·E. Since S=D·E and R=D'·E, they can never BOTH be 1 at same time (D and D' are always different).
| E | D | Q | Action |
|---|---|---|---|
| 0 | X | Q_prev | HOLD — D can change freely, Q frozen |
| 1 | 0 | 0 | TRANSPARENT: Q=D=0 |
| 1 | 1 | 1 | TRANSPARENT: Q=D=1 |
| Feature | D Latch (Level) | D Flip-Flop (Edge) |
|---|---|---|
| Trigger | Entire LEVEL (E=HIGH window) | Single EDGE (0→1 instant) |
| Transparent? | Yes during E=1 | No — opaque between edges |
| Glitches propagate? | Yes | No |
| Verilog | always @(*) if(en) | always @(posedge clk) |
| Output type | reg (or wire via assign) | reg (always) |
| Used in CPU? | Rarely (SRAM, time-borrow) | Everywhere! |
Samples D at exactly ONE instant (rising clock edge). Between edges: Q frozen no matter what D does.
Register = N D flip-flops sharing same clock. Stores N bits of state.
| Type | Input | Output | Use Case | Key Operation |
|---|---|---|---|---|
| SISO | Serial | Serial | Delay line | q <= {q[N-2:0], d_in} |
| SIPO | Serial | Parallel | UART receiver, data collection | Shift in, read all bits |
| PISO | Parallel | Serial | UART transmitter, data send | Load all, shift out one bit |
| PIPO | Parallel | Parallel | Normal register | Direct load |
| State | Meaning | Input=0 → Next | Input=1 → Next | Output |
|---|---|---|---|---|
| S0 | Idle | S0 | S1 | 0 |
| S1 | Got "1" | S2 | S1 | 0 |
| S2 | Got "10" | S0 | S3 | 0 |
| S3 | Got "101" | S2 | S4 | 0 |
| S4 | Got "1011" ! | S2 | S1 | 1 |
assign y = sel ? b : a;