EE / ECE students
Final-year and master's students preparing for VLSI internships and campus placements in design or verification.
Software engineers
Developers moving into hardware who want the foundations explained from first principles.
QA & test engineers
Test automation people heading into design verification — the same stimulus-and-check mindset applied to silicon.
FPGA & embedded engineers
Engineers who write some HDL already and want the rules and habits that verification teams expect.
RTL designers
Designers who want to move to verification and think like the person checking their code.
Career returners
Engineers refreshing fundamentals before interviews after time away from hands-on work.
Prerequisites
| You need | Level | Where to get it |
|---|---|---|
| Basic arithmetic | Comfortable | School maths |
| Powers of two | Know 1, 2, 4 … 256 | Module 1 recaps them |
| Any programming language | Helpful, not required | — |
| Command line | Can run a command | Install guide below |
Where this area sits in the chip flow — and the roles that use it
| Role | How you use this area on the job | Typical first title |
|---|---|---|
| Design verification (DV) | Predict expected values, read waveforms bit by bit, judge overflow and sign bugs | DV Engineer / Verification Engineer I |
| RTL design | Choose encodings, build datapaths from muxes and adders | ASIC Design Engineer / RTL Engineer |
| FPGA | Map logic onto LUTs and flip-flops | FPGA Engineer |
| DFT | Reason about scan flops and stuck-at faults on gates | DFT Engineer |
| Post-silicon validation | Decode register dumps and hex values from silicon | Validation Engineer |
- Convert between binary, hex and decimal and read two's complement values.
- Tell carry from signed overflow and compute both.
- Simplify logic with Boolean laws and Karnaugh maps.
- Describe mux, decoder, adder, comparator and ALU behaviour from their truth tables.
- Explain setup, hold and clock-to-Q, and what violating them does.
- Draw and trace a Moore state machine.
- Write a reference model and compare a design against it.
- Find the planted overflow bug in a 4-bit ALU.
| Tool | What it does | Used in this area for | Cost |
|---|---|---|---|
Icarus Verilog iverilog + vvp | Event-driven Verilog simulator: compiles, then runs | The ALU lab (make) and the first simulation | Free |
| Logisim-evolution or Digital | Draw gates and flip-flops and watch them switch | Trying circuits before writing HDL | Free |
| GTKWave or Surfer | Waveform viewers for .vcd / .fst dumps | Optional: watching ALU outputs change | Free |
| VS Code + Verilog extension | Editor with syntax highlighting and lint on save | Writing code | Free |
| EDA Playground | Browser-based simulators, including commercial ones, after a free sign-up | Zero-install option for the lab | Free |
| make and git | Run builds with one command; track your changes | Lab Makefile; saving your work | Free |
Commercial simulators you will meet at work
| Simulator | Vendor | Notes |
|---|---|---|
| VCS | Synopsys | Widely used for SystemVerilog/UVM regressions |
| Xcelium | Cadence | Successor of Incisive / NC-Sim |
| Questa | Siemens EDA | Successor of ModelSim; starter editions ship with FPGA tools |
The ALU you check in the lab is plain Verilog-2005: it runs unchanged on Icarus, Verilator and every commercial simulator.
Recommended: WSL 2 (Ubuntu inside Windows). You get the same commands as Linux and as most companies' compute farms.
- Turn on WSL and install UbuntuOpen PowerShell as Administrator, run the command, then restart.
wsl --install
After the restart an Ubuntu window opens: choose a user name and password. - Install the toolsIn the Ubuntu window:
sudo apt update sudo apt install -y iverilog verilator gtkwave make git
- Install VS Code and connect it to WSLInstall VS Code from code.visualstudio.com, add the WSL extension, then type
code .in Ubuntu to open the current folder. - Add Verilog supportInstall the Verilog-HDL/SystemVerilog/Bluespec SystemVerilog extension (publisher mshr-h).
- Check it works
iverilog -V | head -1 verilator --version
Install the native Icarus Verilog for Windows build (installer from bleyer.org/icarus, which also bundles GTKWave) and tick "Add executable folder to the user PATH". Use EDA Playground for Verilator.
- Install Apple's command-line tools
xcode-select --install
- Install HomebrewFollow the one-line command on brew.sh, then reopen Terminal.
- Install the simulators
brew install icarus-verilog verilator make git
- Install a waveform viewerDownload Surfer from surfer-project.org (native on Apple silicon), or try
brew install --cask gtkwave. - EditorInstall VS Code and the Verilog-HDL/SystemVerilog/Bluespec SystemVerilog extension.
- Check it works
iverilog -V | head -1 verilator --version
- Ubuntu / Debian
sudo apt update sudo apt install -y iverilog verilator gtkwave make git
- Fedora
sudo dnf install -y iverilog verilator gtkwave make git
- Arch
sudo pacman -S iverilog verilator gtkwave make git
- Check the versions
iverilog -V | head -1 # 11 or newer verilator --version # 5.x recommended
- Open EDA PlaygroundGo to edaplayground.com and create a free account (simulation needs a login).
- Paste the codeDesign on the right, testbench on the left.
- Choose a simulatorUnder Tools & Simulators pick Icarus Verilog; tick Open EPWave after run for waveforms.
- RunPress Run; the log shows your
$displayoutput.
- Make a folder and a file called
full_adder.vmodule full_adder (input a, b, cin, output sum, cout); assign sum = a ^ b ^ cin; assign cout = (a & b) | (cin & (a ^ b)); endmodule module tb; reg a, b, cin; wire sum, cout; full_adder dut (.a(a), .b(b), .cin(cin), .sum(sum), .cout(cout)); integer i; initial begin $display(" a b cin | cout sum"); for (i = 0; i < 8; i = i + 1) begin {a, b, cin} = i[2:0]; #1 $display(" %b %b %b | %b %b", a, b, cin, cout, sum); end end endmodule
- Compile it
iverilog -g2012 -o fa.vvp full_adder.v
- Run it
vvp fa.vvp
Expected output:a b cin | cout sum 0 0 0 | 0 0 0 0 1 | 0 1 0 1 0 | 0 1 0 1 1 | 1 0 1 0 0 | 0 1 1 0 1 | 1 0 1 1 0 | 1 0 1 1 1 | 1 1
- What to noticeThe loop drives all 8 input combinations — an exhaustive test.
#1lets the outputs settle before printing. Compare each row with the full adder table in Module 3.
| Session | Do this | Time |
|---|---|---|
| Day 1 | Install tools · first simulation · Module 1 (number systems, two's complement, carry vs overflow) | 3 h |
| Day 2 | Module 2 (gates, laws, K-maps); try the gate lab | 2.5 h |
| Day 3 | Module 3 (mux, decoder, adders, ALU) | 2 h |
| Day 4 | Module 4 (flip-flops, timing, FSMs); feed bits to the sequence detector | 2 h |
| Day 5 | Lab: complete the reference model, find the bug, reach PASS · area test | 2.5 h |
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
What “digital” means
A wire inside a chip is either at a low voltage (close to 0 V) or a high voltage (close to the supply). We call low 0 and high 1. Nothing in between counts.
A dimmer switch can sit at any brightness — that is analog. An ordinary light switch is only off or on — that is digital.
That single 0-or-1 value is a bit — short for binary digit.
Two levels are easy to tell apart even when the signal is noisy: a slightly wobbly high is still clearly high. That is why every value you will ever check in a testbench is built from bits.
A transistor is a switch
Something has to flip those 0s and 1s. That is a transistor: a switch controlled by another electrical signal instead of a finger.
A tap controlled by a lever: the lever (the gate) decides whether water (current) flows.
The control input is the gate. A modern chip has billions of these switches.
Every gate, adder and memory cell in this course is just transistor switches wired together — nothing more mysterious than that.
Counting with bits
Put four bits side by side and they can count. Each bit is one lamp: off is 0, on is 1.
A car odometer: the right wheel goes 0…9, rolls over to 0 and pushes the next wheel up. Binary is the same, but each wheel has only two values, so it rolls over after 1, not 9.
| Decimal | Bits | Decimal | Bits | Decimal | Bits | Decimal | Bits |
|---|---|---|---|---|---|---|---|
| 0 | 0000 | 4 | 0100 | 8 | 1000 | 12 | 1100 |
| 1 | 0001 | 5 | 0101 | 9 | 1001 | 13 | 1101 |
| 2 | 0010 | 6 | 0110 | 10 | 1010 | 14 | 1110 |
| 3 | 0011 | 7 | 0111 | 11 | 1011 | 15 | 1111 |
Watch 1 → 2: the right bit rolls over to 0 and the next bit turns on (0001 → 0010).
Four bits give 16 different patterns: 0 to 15.
Counting in binary is how you will read counters, addresses and state numbers in waveforms.
Place value: every position has a weight
In decimal, 345 means 3 hundreds + 4 tens + 5 ones. Each position is worth 10× the one to its right. In binary each position is worth 2× the one to its right.
Coins of 8, 4, 2 and 1: to pay any amount up to 15 you either use each coin once or not at all.
- Write the weights over the bits:
8 4 2 1. - Read
1011: one 8, no 4, one 2, one 1. - Add the weights where the bit is 1: 8 + 2 + 1 = 11.
- A byte continues the weights: 128 64 32 16 8 4 2 1. So
1011 0110= 128 + 32 + 16 + 4 + 2 = 182.
The weights are powers of two. The leftmost bit is the MSB (most significant), the rightmost the LSB.
To check any value in a waveform you add weights in your head. It becomes automatic within a week.
Decimal to binary: keep dividing by two
To turn a decimal number into binary, divide by 2 again and again and write down each remainder.
Sharing sweets between two people: each time, note whether one is left over.
| Step | Divide | Result | Remainder |
|---|---|---|---|
| 1 | 45 ÷ 2 | 22 | 1 |
| 2 | 22 ÷ 2 | 11 | 0 |
| 3 | 11 ÷ 2 | 5 | 1 |
| 4 | 5 ÷ 2 | 2 | 1 |
| 5 | 2 ÷ 2 | 1 | 0 |
| 6 | 1 ÷ 2 | 0 | 1 |
Read the remainders from the bottom up: 101101. Check with weights: 32 + 8 + 4 + 1 = 45 ✓
Repeated division by the radix (base) 2. The first remainder is the LSB.
You will often turn a number from a spec (a length of 45, an address of 200) into the bit pattern you expect on a bus.
Hexadecimal: four bits in one character
Long strings of 0s and 1s are hard to read. Hex is a shorthand with 16 digits: 0–9, then A=10, B=11, C=12, D=13, E=14, F=15.
Writing “1 dozen” instead of drawing 12 dots.
- Split the bits into groups of four from the right:
1011 0110. 1011= 11 = B;0110= 6 = 6.- So 182 = B6 in hex. In Verilog:
8'hB6— 8 bits, h for hex, value B6.
One hex digit = exactly four bits (a nibble).
Simulators, logs and register maps print values in hex. You must read 32'hDEAD_BEEF as fast as a phone number.
What is 8'h3C in binary and in decimal?
3 = 0011, C = 1100 → 0011 1100 = 32 + 16 + 8 + 4 = 60.
Fixed width and range
In hardware a number always has a fixed number of wires: 4, 8, 32 bits. With n bits you get 2n patterns.
An odometer at 999 rolls to 000 — there is no fourth wheel for the 1.
| Width | Patterns | Unsigned range |
|---|---|---|
| 4 bits | 16 | 0 … 15 |
| 8 bits | 256 | 0 … 255 |
| 16 bits | 65,536 | 0 … 65,535 |
| 32 bits | ≈ 4.3 billion | 0 … 4,294,967,295 |
8 bits: 255 + 1 → every bit rolls over → 0000_0000. The 1 that fell off the top is the carry.
The bit that falls off the top of an unsigned addition is the carry.
“Wrap-around at the maximum” is one of the most common corner cases you will test.
Negative numbers: two's complement
There is no minus sign on a wire. Hardware stores negatives by giving the leftmost bit a negative weight.
A clock face: going back 1 from 0 lands on 15 — the patterns wrap around a wheel.
- 4-bit weights become
−8 4 2 1. 1000= −8.1111= −8 + 4 + 2 + 1 = −1.0xxxpatterns are 0…7;1xxxpatterns are −8…−1.
The same bits mean different numbers depending on whether the design treats them as signed. Many real bugs are exactly that mix-up.
Negating: invert, then add one
To make a number negative: flip every bit, then add 1.
A two-step recipe you can do on paper in five seconds.
- 6 =
0110. - Invert every bit →
1001. - Add 1 →
1010= −8 + 2 = −6 ✓ - Add 6 + (−6) with an ordinary adder:
0110 + 1010 = 1 0000. Keep 4 bits →0000. The same adder handles both signs.
This is the two's complement negation.
It is why chips need only one adder for signed and unsigned maths — and why you check subtraction as “add the negation”.
Carry versus overflow
There are two different ways a result can “fall off the end”. Carry is about unsigned numbers; overflow is about signed numbers.
Carry: the odometer ran out of wheels. Overflow: the answer ran off the edge of the signed wheel, from +7 straight to −8.
- Unsigned 8-bit: 255 + 1 needs 9 bits → carry = 1.
- Signed 4-bit: 7 + 1 should be 8, but
1000reads as −8 → overflow = 1, carry = 0.
You check both flags separately — exactly what you will do in this area's ALU lab, where the planted bug is in a flag.
In 4 bits, what is 5 + 4 read as unsigned, and read as signed? Is there carry? Overflow?
The bits are 1001. Unsigned: 9, fits → no carry. Signed: −7, but the true answer is 9 → overflow, no carry.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Bit
- One binary digit, 0 or 1 — one wire in hardware.
- Nibble / byte / word
- 4 bits / 8 bits / the natural width of a datapath (often 32 or 64).
- Radix (base)
- How many digit values a number system has: 2 (binary), 10 (decimal), 16 (hexadecimal).
- MSB / LSB
- Most / least significant bit: the leftmost bit has the largest weight.
- Two's complement
- The way hardware stores signed integers: the MSB has a negative weight, −2ⁿ⁻¹.
- Sign extension
- Widening a signed value by copying its MSB into the new upper bits.
- Carry
- The extra bit produced when an unsigned result needs one more bit.
- Overflow
- When a signed result falls outside the range the width can represent.
Positional notation: every bit has a weight
| Base | Name | Digits | Verilog literal | Example (decimal 45) |
|---|---|---|---|---|
| 2 | Binary | 0–1 | 8'b0010_1101 | 0010 1101 |
| 16 | Hexadecimal | 0–9, A–F | 8'h2D | 2D |
| 10 | Decimal | 0–9 | 8'd45 | 45 |
| 8 | Octal | 0–7 | 8'o55 | 55 (rare in hardware) |
Try it: number explorer
Converting by hand
| Conversion | Method | Worked example |
|---|---|---|
| Binary → decimal | Add the weights of the 1 bits | 1011_0110 = 128+32+16+4+2 = 182 |
| Decimal → binary | Subtract the largest power of two that fits, repeat | 45 = 32+8+4+1 → 0010_1101 |
| Binary ↔ hex | Group four bits from the right | 1011_0110 → B 6 → 8'hB6 |
| Negate (two's complement) | Invert every bit, add 1 | −10: 0000_1010 → 1111_0101 + 1 = 1111_0110 |
| Sign-extend 4 → 8 bits | Copy the MSB into the new bits | 1010 (−6) → 1111_1010 (still −6) |
The two's complement wheel
Carry versus overflow
| Operation (8-bit) | Unsigned view | Signed view | carry | overflow |
|---|---|---|---|---|
| 200 + 100 | 300 doesn't fit → 44 | −56 + 100 = 44 ✓ | 1 | 0 |
| 100 + 50 | 150 ✓ | +150 doesn't fit → −106 | 0 | 1 |
−1 + −1 (FF + FF) | 510 → 254 | −2 ✓ | 1 | 0 |
| −100 + −100 | 312 → 56 | −200 doesn't fit → +56 | 1 | 1 |
// Overflow rules (n-bit, result in sum / diff) add_overflow = (a[n-1] == b[n-1]) && (sum[n-1] != a[n-1]); // same signs in, different sign out sub_overflow = (a[n-1] != b[n-1]) && (diff[n-1] != a[n-1]); // different signs in, sign of a lost
A checker should calculate the expected result in a wider integer (or plain 32-bit int) so it can see the carry and the true signed value, then compare flags explicitly.
Mixing signed and unsigned operands in one Verilog expression (the whole expression becomes unsigned) · zero-extending a negative number · treating overflow and carry as the same flag · forgetting that hex digits map to exactly four bits.
Interview corner
Q. What is −1 in 8-bit two's complement? A. 8'hFF (all ones).
Q. How do you detect signed overflow in an adder? A. Both operands have the same sign and the result's sign is different; equivalently, carry into the MSB ≠ carry out of the MSB.
Key takeaways
- Hardware stores patterns; signed or unsigned is an interpretation.
- Two's complement: MSB weighs −2ⁿ⁻¹; negate by invert-and-add-one.
- Carry is about unsigned results, overflow about signed ones.
- Sign-extend signed values; zero-extend unsigned ones.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
From bits to decisions: the logic gate
A logic gate is a tiny circuit of a few transistors. It takes one or two bits in and gives one bit out, following a fixed rule.
A vending-machine rule: “if the coin is in AND the button is pressed, drop the snack”.
A logic gate. Every digital circuit — a calculator, a processor — is thousands or millions of gates wired together.
When you read RTL you are reading a description of gates. Knowing the few basic rules lets you predict any output.
Truth tables: the full specification
A truth table lists every combination of inputs and the output for each one. Nothing is left out.
A complete price list: every item, every price — no “ask at the counter”.
- 1 input → 2 rows (0, 1).
- 2 inputs → 4 rows (00, 01, 10, 11).
- 3 inputs → 8 rows. Each new input doubles the rows: 2n.
A truth table with 2n rows for n inputs.
For a verification engineer the truth table is the specification: if every row behaves, the block is correct.
How many rows does a truth table with four inputs need?
2, 4, 8, 16 — two to the power of four.
NOT: the opposite
NOT has one input, and its output is the opposite: 0 → 1, 1 → 0.
A “do not disturb” sign that flips: when you're in, the sign says out.
| a | y = ~a |
|---|---|
| 0 | 1 |
| 1 | 0 |
An inverter. Symbol: a triangle with a small circle at the tip — the circle always means invert. Verilog: y = ~a;
Active-low signals (like rst_n) are everywhere; you invert in your head all day.
AND: all inputs must be 1
AND outputs 1 only when every input is 1.
A bank vault that opens only when two people turn their keys at the same time. As switches: two switches in series.
| a b | a & b |
|---|---|
| 0 0 | 0 |
| 0 1 | 0 |
| 1 0 | 0 |
| 1 1 | 1 |
AND as a mask: 1011_0110 & 0000_1111 = 0000_0110 — the top four bits are forced to 0, the bottom four pass through.
Symbol: flat back, round front. Verilog: a & b.
Masks are how designs pick fields out of registers, and how you write checks on just the bits you care about.
OR: any input is enough
OR outputs 1 when at least one input is 1.
A car's interior light comes on if the driver's door OR the passenger's door is open. As switches: two switches in parallel.
| a b | a | b |
|---|---|
| 0 0 | 0 |
| 0 1 | 1 |
| 1 0 | 1 |
| 1 1 | 1 |
OR forces bits on: 0011_0110 | 1000_0000 = 1011_0110 — the top bit becomes 1 whatever it was.
Verilog: a | b.
Setting a flag bit in a register is an OR. Collecting “any error happened” is an OR of all error bits.
XOR: are the inputs different?
XOR outputs 1 when exactly one input is 1 — when the inputs differ.
A staircase light with a switch at the bottom and the top: flipping either one changes the light.
| a b | a ^ b |
|---|---|
| 0 0 | 0 |
| 0 1 | 1 |
| 1 0 | 1 |
| 1 1 | 0 |
Bit by bit: 1010 ^ 1100 = 0110 — 1 wherever the columns differ.
Exclusive OR. Verilog: a ^ b.
XOR is the sum bit of an adder (1 + 1 = 0, carry 1), the heart of parity and CRC checks, and the quickest way to compare two values bit by bit.
XOR bit by bit: 1010 and 1100?
1⊕1 = 0, 0⊕1 = 1, 1⊕0 = 1, 0⊕0 = 0 → 0110.
NAND and NOR: the universal gates
NAND is AND followed by NOT; NOR is OR followed by NOT.
LEGO bricks: with enough copies of one brick you can build any shape.
| a b | NAND | NOR |
|---|---|---|
| 0 0 | 1 | 1 |
| 0 1 | 1 | 0 |
| 1 0 | 1 | 0 |
| 1 1 | 0 | 0 |
Tie both NAND inputs together → NOT. NAND then NOT → AND. Add De Morgan → OR.
NAND and NOR are universal: either one alone can build any circuit.
Real silicon is built mostly from NAND/NOR cells; you will see them in gate-level netlists.
Combining gates
Gates connect: the output of one becomes the input of the next.
Plumbing: water from one pipe feeds the next junction.
- Function:
y = (a & b) | c. - Inputs a = 1, b = 0, c = 1.
- AND first: 1 & 0 = 0.
- Then OR: 0 | 1 = 1. So y = 1.
A combinational circuit: the output depends only on the inputs right now.
This is exactly how you compute the expected value of any RTL expression: evaluate from the inside out.
Same circuit, y = (a & b) | c, with a = 1, b = 1, c = 0. What is y?
AND: 1 & 1 = 1. OR: 1 | 0 = 1.
Simplifying logic
Many different gate circuits give the same truth table. Simple rules let you remove the extra gates.
Tidying a sentence: “the very very big” → “the big” — same meaning, fewer words.
- Rules:
a & 1 = a,a & 0 = 0,a | 0 = a,a | 1 = 1. - A Karnaugh map groups neighbouring 1s: f = 1 for m1, m3, m5, m6, m7.
- A group of four where c = 1 gives
c; a pair where a = b = 1 givesa & b. - Result:
f = c | (a & b).
Synthesis tools simplify for you, but you must be able to read a truth table and predict the output.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Boolean algebra
- Algebra on values 0 and 1 with AND (·), OR (+) and NOT (¯).
- Truth table
- Every input combination listed with its output: 2ⁿ rows for n inputs.
- Minterm
- A product term that is 1 for exactly one input combination (m0, m1, …).
- Sum of products (SOP)
- An OR of AND terms, e.g.
a·b + c— maps directly to two levels of gates. - Karnaugh map
- A truth table drawn in Gray order so that adjacent 1s can be grouped to simplify.
- Don't care (X)
- An input combination that never happens, so its output can be chosen freely.
- Universal gate
- NAND or NOR alone can build every other function.
- Hazard (glitch)
- A short wrong pulse on an output while inputs change through different path delays.
Try it: gate lab
Gates and their truth tables
| A B | AND a&b | OR a|b | XOR a^b | NAND | NOR | XNOR |
|---|---|---|---|---|---|---|
| 0 0 | 0 | 0 | 0 | 1 | 1 | 1 |
| 0 1 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 0 | 0 | 1 | 1 | 1 | 0 | 0 |
| 1 1 | 1 | 1 | 0 | 0 | 0 | 1 |
The laws you will actually use
| Law | Form | Use it to |
|---|---|---|
| Identity / null | a·1 = a, a+0 = a, a·0 = 0, a+1 = 1 | Remove constants |
| Complement | a·ā = 0, a+ā = 1 | Kill impossible terms |
| Absorption | a + a·b = a, a·(a+b) = a | Delete redundant terms |
| De Morgan | (a·b)‾ = ā + b̄, (a+b)‾ = ā·b̄ | Push inversions; build with NAND/NOR |
| Distributive | a·(b+c) = a·b + a·c, a + b·c = (a+b)(a+c) | Factor or expand |
| Consensus | a·b + ā·c + b·c = a·b + ā·c | Spot redundant (hazard-covering) terms |
Karnaugh maps: simplify by grouping
// The simplified function in Verilog — synthesis would find this too assign f = c | (a & b);
You rarely minimise logic by hand at work — synthesis does it. But reading a truth table, spotting a missing don't-care, or deriving the expected value of a decoder from its spec is daily verification work.
Grouping in non-power-of-two sizes · forgetting edges wrap around · drawing the map in binary order 00 01 10 11 instead of Gray order · treating a don't-care as 1 when it doesn't help a group.
Interview corner
Q. Implement an inverter using only a NAND gate. A. Tie both inputs together: (a·a)‾ = ā.
Q. What is a static-1 hazard? A. A momentary 0 on an output that should stay 1 when one input changes; covered by adding the consensus term.
Key takeaways
- A truth table fully specifies combinational logic.
- K-maps group adjacent 1s in Gray order to drop variables.
- NAND and NOR are universal; De Morgan converts between forms.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
The multiplexer: a selector
A mux passes one of several inputs to its output. A separate select input decides which.
A railway switch: the lever decides which track the train goes down.
- 2:1 mux: select = 0 → out = input 0; select = 1 → out = input 1.
- 4:1 mux: four inputs need two select bits, because 2 bits name 4 choices.
- select =
10(2) → out = input 2.
? : and every case that picks a value becomes one.Whenever a design chooses between values, there is a mux — and a select signal you must drive through every value.
The decoder: one code, one output
A decoder does the opposite: it takes a code and turns on exactly one of its outputs.
A hotel key card: the code opens exactly one door.
| code | out 3 | out 2 | out 1 | out 0 |
|---|---|---|---|---|
| 00 | 0 | 0 | 0 | 1 |
| 01 | 0 | 0 | 1 | 0 |
| 10 | 0 | 1 | 0 | 0 |
| 11 | 1 | 0 | 0 | 0 |
That is how a chip picks one register or one memory block out of many: address decoding. A check: “exactly one output is high”.
Adding two bits: the half adder
Adding bits is just counting: 0+0 = 0, 0+1 = 1, 1+0 = 1, and 1+1 = 2, which is 10 in binary — a sum of 0 with a carry of 1.
Column addition at school: 5 + 7 = 12 → write 2, carry 1.
| a b | carry | sum |
|---|---|---|
| 0 0 | 0 | 0 |
| 0 1 | 0 | 1 |
| 1 0 | 0 | 1 |
| 1 1 | 1 | 0 |
Look at the columns: sum is exactly XOR, carry is exactly AND.
A half adder: sum = a ^ b, carry = a & b.
You just built arithmetic from two gates. Every adder you will verify grows from this.
Full adders and the ripple carry
A full adder also adds a carry coming in from the right. Chain one per bit and the carry passes from bit to bit.
A line of people passing a bucket: each waits for the one before.
- 0111 (7) + 0001 (1), starting at the right.
- Bit 0: 1 + 1 = 0, carry 1.
- Bit 1: 1 + 0 + carry 1 = 0, carry 1.
- Bit 2: 1 + 0 + carry 1 = 0, carry 1.
- Bit 3: 0 + 0 + carry 1 = 1. Result
1000= 8 ✓
A ripple-carry adder: the carry ripples right to left.
The longest carry chain (like 0111 + 0001 or 1111 + 0001) is a classic corner case to include in tests.
The ALU: many operations, one block
An ALU (Arithmetic Logic Unit) computes several operations — ADD, AND, OR, XOR … — and an opcode chooses which result comes out. Inside, a mux picks one of the results.
A Swiss-army knife: many tools, you flip out one at a time.
| opcode | operation | a = 6 (0110), b = 3 (0011) | y |
|---|---|---|---|
| 000 | ADD | 6 + 3 | 1001 = 9 |
| 010 | AND | 0110 & 0011 | 0010 = 2 |
| 011 | OR | 0110 | 0011 | 0111 = 7 |
| 100 | XOR | 0110 ^ 0011 | 0101 = 5 |
The ALU also reports flags: carry, overflow and zero. For 6 + 3 in 4 bits: unsigned 9 fits (carry 0), but signed 9 is more than +7 → overflow = 1.
This 4-bit ALU is exactly what you verify in Lab 01 — with an exhaustive test of all 2,048 input combinations.
4-bit ALU, a = 5 (0101), b = 3 (0011). What do ADD, AND and XOR give?
ADD: 1000 = 8 (unsigned fits, signed overflows). AND: 0001 = 1. XOR: 0110 = 6.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Multiplexer (mux)
- Selects one of 2ⁿ inputs onto one output using an n-bit select.
- Decoder
- Turns an n-bit code into 2ⁿ one-hot outputs; used for address decoding.
- Encoder / priority encoder
- Reverse of a decoder; a priority encoder reports the highest active input.
- Half / full adder
- Adds two bits (half) or two bits plus carry-in (full).
- Ripple-carry adder
- Full adders chained so each carry feeds the next; simple but slow for wide words.
- Comparator
- Tells whether a = b, a < b or a > b; equality is an AND of XNORs.
- ALU
- Arithmetic Logic Unit: one block that performs the operation chosen by an opcode.
- Propagation delay
- Time from an input change to the output settling; sets how fast a circuit can run.
Three blocks you will meet in every design
assign y = sel[1] ? (sel[0] ? d3 : d2) : (sel[0] ? d1 : d0); // 4:1 mux assign dec = 4'b0001 << a; // 2:4 decoder assign {cout, sum} = a + b + cin; // adder with carry
Full adder truth table
| a b cin | cout sum | a b cin | cout sum |
|---|---|---|---|
| 0 0 0 | 0 0 | 1 0 0 | 0 1 |
| 0 0 1 | 0 1 | 1 0 1 | 1 0 |
| 0 1 0 | 0 1 | 1 1 0 | 1 0 |
| 0 1 1 | 1 0 | 1 1 1 | 1 1 |
sum = a ⊕ b ⊕ cin and cout = a·b + cin·(a ⊕ b). Chaining n full adders gives an n-bit adder; faster designs (carry-lookahead, carry-select) compute carries in parallel.
The ALU you will verify in the lab
| op | Name | y | carry | overflow |
|---|---|---|---|---|
| 000 | ADD | a + b | carry out | signed add overflow |
| 001 | SUB | a − b | borrow (a < b unsigned) | signed sub overflow |
| 010 | AND | a & b | 0 | 0 |
| 011 | OR | a | b | 0 | 0 |
| 100 | XOR | a ^ b | 0 | 0 |
| 101 | NOT | ~a | 0 | 0 |
| 110 | SHL | a << 1 | a[3] | 0 |
| 111 | SHR | a >> 1 | a[0] | 0 |
A 4-bit ALU has 4 + 4 + 3 = 11 input bits: 2,048 combinations. Testing all of them takes milliseconds, so the lab's testbench is exhaustive — no luck involved. Beyond about 20 input bits you switch to constrained-random stimulus (Area 05).
Letting the adder result truncate silently (declare it one bit wider) · forgetting that SUB's carry means borrow · shift amount and direction mixed up · comparing signed values with an unsigned comparator.
Interview corner
Q. How many 2:1 muxes make a 4:1 mux? A. Three: two on the first level, one on the second.
Q. Why is a ripple-carry adder slow? A. The carry may travel through every stage, so delay grows linearly with width.
Key takeaways
- Datapaths are built from mux, decoder, adder and comparator.
- An adder's result needs n+1 bits; the extra bit is the carry.
- Small blocks can be verified exhaustively.
Start from zero
New to this? Work through these steps first, in order. Already comfortable? Skim them and jump to the reference below.
Circuits that remember
Everything so far has no memory: change the inputs and the output changes at once. An adder can't remember the last sum; a gate can't count.
A calculator without a memory key versus one with it.
| Kind | Output depends on | Examples |
|---|---|---|
| Combinational | the inputs now | gates, mux, adder, ALU |
| Sequential | the inputs and the past | flip-flops, registers, counters, state machines |
Real chips must remember: a counter value, a received byte, which step of a protocol they are on. Most bugs live in that memory.
The clock: the chip's heartbeat
A clock is a signal that flips 0, 1, 0, 1 forever at a steady rate, so that every memory element updates together.
A drummer keeping time for a whole band.
The 0→1 moment is the rising edge. Time between rising edges is the period; period = 1 / frequency.
Almost everything in a chip happens on the rising edge. Testbenches drive and sample relative to it.
If the clock is 500 MHz, how long is one period?
Half the frequency of 1 GHz means double the time: 2 ns.
The D flip-flop: a camera for one bit
A D flip-flop has a data input D, a clock input and an output Q. On each rising edge, Q copies D. Between edges Q holds that value, whatever D does.
A camera: D is the scene, the rising edge is the shutter click, Q is the photo. The scene can change between clicks — the photo doesn't.
always @(posedge clk) q <= d; // <= : non-blocking, for every flip-flop
A D flip-flop: it samples D on the edge and holds it until the next edge.
Seeing Q change one cycle after D is the most important waveform-reading skill in verification.
Registers and counters
One flip-flop stores one bit. Four side by side, sharing one clock, store a 4-bit number: a register. Feed its output through a +1 adder back into its input and it counts.
A turnstile counter that clicks up by one each time someone passes.
A register and a counter.
Counters are everywhere (timers, addresses, FIFO pointers), and their wrap-around is a favourite place for bugs.
A 4-bit counter is at 15. What does it show after the next edge?
15 = 1111; + 1 = 1 0000; four bits keep 0000 → it wraps to 0.
Reset: start from a known value
At power-on, flip-flops wake up holding random values. A reset signal forces them to a known value, usually 0.
Zeroing a kitchen scale before you weigh anything.
always @(posedge clk) if (rst) q <= 0; // reset check comes first else q <= q + 1;
Forget the reset and simulation shows X (unknown) — and X spreads through every gate it touches.
“Every register has its reset value after reset” is the first check in almost every testbench.
Setup and hold: keep still while the shutter clicks
D must be stable for a short time before the edge and a short time after it.
If someone moves just as the shutter clicks, the photo is blurred.
- Setup time: D stable before the edge.
- Hold time: D stable after the edge.
- If D changes inside that window, the flip-flop may capture the wrong value.
Static timing analysis tools catch these in the design. In testbenches, you avoid them by driving inputs away from the clock edge.
Metastability and the two-flop synchronizer
In the worst case a violation leaves the output stuck halfway between 0 and 1 for a while. This is most likely when a signal comes from a button or another clock, because it can change at any moment.
A ball balanced on the top of a hill: it will roll to one side, but you don't know which or when.
Every signal that crosses from one clock to another must be synchronized — reviewers and checkers look for it.
State machines
A finite state machine (FSM) is always in exactly one state, and on a clock edge it moves to the next state according to its inputs.
A traffic light: green → yellow → red → green.
Protocol controllers, arbiters and handshakes are all FSMs. Verifying them means covering every state and every transition.
Tracing an FSM step by step
This FSM watches a stream of bits and raises found when it sees two 1s in a row.
Counting how many times in a row someone says “yes”.
| input | — | 0 | 1 | 1 | 0 |
|---|---|---|---|---|---|
| state after | S0 | S0 | S1 | S2 | S0 |
| found | 0 | 0 | 0 | 1 | 0 |
A Moore FSM: the output depends only on the state (found = 1 in S2).
Tracing by hand like this is how you write the expected sequence a checker compares against.
From S0, the inputs are 1, 1, 1. Which state are we in at the end?
1 → S1, 1 → S2, 1 → stays in S2; found stays on.
An FSM in Verilog: two blocks
Write an FSM as two blocks that match its two parts: one for the state register, one for the next-state logic.
A form with two sections: “where am I now” and “where do I go next”.
always @(posedge clk) // 1 · state register if (rst) state <= S0; else state <= next; always @(*) // 2 · next-state logic case (state) S0: next = in ? S1 : S0; S1: next = in ? S2 : S0; S2: next = in ? S2 : S0; endcase assign found = (state == S2);
The two-process FSM pattern.
It is what reviewers expect, and it keeps the flip-flops and the logic clearly apart — which makes bugs easier to find.
What verification checks in sequential logic
Three things, every time: reset, every transition, and the corners.
A building inspector: does the door lock (reset), does every room connect correctly (transitions), what happens in a fire (corners)?
- Reset: after reset, is every register at its known value?
- Every transition: for each state and each input, does it go to the right next state? (3 states × 2 inputs = 6 transitions above)
- Corners: the counter wrapping at 15, two events on the same edge, a reset in the middle of a sequence.
Bugs hide in transitions and corners, not on the happy path. That is where your tests should spend their time.
Going further: the reference
The same ideas at the next level: precise definitions, tables, rules and interview questions.
Key definitions
- Latch
- Level-sensitive: transparent while enable is high, holds while low.
- Flip-flop
- Edge-triggered: samples D only at the clock edge, holds Q in between.
- Setup time (t_su)
- How long D must be stable before the clock edge.
- Hold time (t_h)
- How long D must stay stable after the edge.
- Clock-to-Q (t_cq)
- Delay from the clock edge to Q changing.
- Metastability
- An unresolved, in-between state when setup or hold is violated; fixed with a 2-flop synchronizer.
- Moore machine
- Outputs depend only on the current state.
- Mealy machine
- Outputs depend on state and current inputs.
Timing words you must know
| Term | Meaning | If violated |
|---|---|---|
| Setup | D stable before the edge | Wrong value or metastability — fix by slowing the clock or shortening logic |
| Hold | D stable after the edge | Wrong value at any clock speed — fix by adding delay on the short path |
| Clock skew | Clock arrives at different flops at different times | Eats into setup or hold margin |
| Reset recovery/removal | Setup/hold for releasing asynchronous reset | Flops leave reset in different cycles |
Finite state machines
| State | in = 0 | in = 1 | Output y |
|---|---|---|---|
| S0 | S0 | S1 | 0 |
| S1 | S10 | S1 | 0 |
| S10 | S0 | S101 | 0 |
| S101 | S10 | S1 | 1 |
Resets
| Style | Code | Notes |
|---|---|---|
| Asynchronous | always @(posedge clk or negedge rst_n) | Acts immediately; release must be synchronised |
| Synchronous | always @(posedge clk) if (!rst_n) … | Acts on the clock edge; needs a running clock |
Every checker in this course follows the same recipe: write a reference model in plain integer arithmetic, drive inputs, compare every output with !== (so X is a failure), and print the first few mismatches then PASS n/n or FAIL.
Confusing latches with flip-flops · fixing a hold violation by slowing the clock (it doesn't help) · FSMs with unreachable or missing transitions · forgetting to reset the state register.
Interview corner
Q. What is metastability and how do you handle it? A. A flop sampling during its setup/hold window can hover between 0 and 1; pass asynchronous signals through two flops to give it a cycle to resolve.
Q. Moore or Mealy — which reacts faster? A. Mealy: its output can change in the same cycle as the input.
Key takeaways
- Flip-flops sample on an edge; latches are transparent on a level.
- Setup limits clock speed; hold is independent of it.
- An FSM is a state register plus next-state logic; draw it before you code it.
What is 8'hB6 as an unsigned and as a signed number?
Write −6 in 4-bit two's complement, then sign-extend it to 8 bits.
1010 → 1111_1010.200 + 100 in 8 bits: carry, overflow, result?
Simplify a + a·b.
a (absorption).Why does a K-map use Gray order?
How many input combinations does the 4-bit ALU have?
Which timing violation cannot be fixed by slowing the clock?
In the 101 detector, what state follows S101 on input 0?
Explain two's complement and why hardware uses it.
Difference between carry and overflow?
Build an XOR from NAND gates.
What is a glitch and when does it matter?
Latch vs flip-flop?
Why do we need synchronizers?
Design a counter that counts 0 → 9 and wraps.
always @(posedge clk) cnt <= (cnt == 9) ? 0 : cnt + 1; with a 4-bit register and reset.How would you test a 32-bit adder?
| You see | Why | Fix |
|---|---|---|
| Overflow flag looks random in the lab | Checking signed overflow with the add rule for SUB | Use: different signs in, and the result's sign differs from a |
| Mismatch only for op = SUB | Borrow vs carry confusion | For SUB, carry means borrow: 1 when a < b (unsigned) |
iverilog: command not found | Not installed or not on PATH | Re-run the install step; reopen the terminal |
| Simulation prints X | Inputs never assigned before the first print | Drive every input before #1 $display |
| Testbench reports PASS with 0 checks | Loop bounds or counters wrong | Print the number of checks; require it to equal 2,048 |
| Resource | Why read it |
|---|---|
| Harris & Harris, Digital Design and Computer Architecture | Number systems, Boolean algebra and sequential logic with HDL examples |
| M. Morris Mano, Digital Design | The classic reference for K-maps and combinational design |
| Charles Petzold, Code | A readable story of how bits become computers — good for newcomers |
| Icarus Verilog documentation | Command-line options for the lab |
Lab 01 · Build and self-check a 4-bit ALU
You get alu4.v (eight operations, one planted bug), tb_alu4.v with an exhaustive stimulus loop and a reference model with TODOs, and a Makefile.
- Run
make: with the TODOs empty you see mismatches for the operations the model doesn't know yet. - Fill in TODO 1–3 so the model covers all eight operations and their carry and overflow flags.
- Run again: the remaining mismatches are the design's bug — they share one operation and one flag.
- Fix
alu4.vand re-run.
Done when you see PASS: 2048/2048.