Stop drawing and start describing: how every circuit in this course is written in Verilog, simulated, and turned into real hardware.
Why stop drawing
A modern chip has billions of transistors, and nobody draws those. Engineers write a hardware description language (HDL) and let tools work out the gates. Verilog is the most widely used. Here's the key thing to remember: it may look like a programming language, but you're describing hardware. Every line exists at once, and they all run in parallel.
> Loading the animation…
Modules and combinational logic
A module is one block with named inputs and outputs. Inside, assign describes combinational logic: the output continuously follows the expression, exactly like wires and gates.
module half_adder (
input wire a,
input wire b,
output wire sum,
output wire carry
);
assign sum = a ^ b;
assign carry = a & b;
endmodule
The half adder from Chapter 10. The two assign lines happen at the same time, and their order doesn't matter.
> Loading the animation…
For larger combinational logic, use always @(*). Its contents read like a program, but the result is still logic with no memory, and that holds only if every output gets a value on every path.
module mux4 (input wire [3:0] d, input wire [1:0] s, output reg y);
always @(*) begin
case (s)
2'b00: y = d[0];
2'b01: y = d[1];
2'b10: y = d[2];
2'b11: y = d[3];
endcase
end
endmodule
The 4:1 MUX from Chapter 11. reg here only means "assigned inside an always block". It doesn't mean a register exists.
always @(posedge clk) describes flip-flops: everything assigned in it updates at the clock edge. This is where one rule matters more than any other.
> Loading the animation…
> Loading the animation…
Describing
Use
Written
Combinational logic
Blocking
always @(*) with =
Flip-flops
Nonblocking
always @(posedge clk) with <=
Follow this one rule and a whole class of bugs goes away.
module counter4 (input wire clk, input wire rst, output reg [3:0] q);
always @(posedge clk) begin
if (rst) q <= 4'd0;
else q <= q + 4'd1;
end
endmodule
A 4-bit synchronous counter with reset, from Chapter 15. q + 1 gets you an adder, and you don't have to design it.
Practice
Which code describes a 3-bit shift register?
Common mistakes
You write
You get
Fix
if with no else in always @(*)
An accidental latch
Assign a default first, or cover every case
= in a clocked block
Order-dependent behaviour
Use <=
A signal driven from two blocks
Multiple drivers: X in simulation, or a synthesis error
Drive each signal from one place
A missing signal in always @(a)
Simulation differs from hardware
Use always @(*)
#10 delays in a design
Ignored by synthesis
Use a clock, and keep delays to testbenches
// An accidental latch: y must remember its value when en is 0
always @(*) if (en) y = d;
// Fixed: every path assigns y
always @(*) begin
y = 1'b0;
if (en) y = d;
end
The most common beginner bug. The synthesis tool warns you, so read its warnings.
Practice
What does always @(*) if (sel) y = a; create?
Testing with a testbench
A testbench is a Verilog module with no ports. It creates the design, drives its inputs and checks its outputs. It only ever runs in simulation, so it can use delays and printing, which real hardware can't.
module tb;
reg a = 0, b = 0;
wire sum, carry;
half_adder dut (.a(a), .b(b), .sum(sum), .carry(carry));
initial begin
a = 1; b = 1; #1;
if (sum !== 0 || carry !== 1) $display("FAIL");
else $display("PASS");
$finish;
end
endmodule
Verification teams don't check designs with if and $display alone. SystemVerilog adds three tools, and a testbench is judged by all three: assertions state rules that must always hold, constrained random stimulus tries inputs you wouldn't think of, and coverage measures how much of the design the tests actually reached.
// Concurrent assertion: checked at every clock edge
assert property (@(posedge clk) disable iff (rst) !(gnt[0] && gnt[1]))
else $error("both requesters granted");
// Coverage point: proves a path was reached
cover property (@(posedge clk) $past(state) == IDLE && state == BUSY);
// Constrained random stimulus
class Txn;
rand bit [7:0] a, b;
constraint edges { a dist { 0 := 1, 255 := 1, [1:254] :/ 6 }; }
endclass
An assertion fires the moment its rule breaks, with the exact time and line. A failed assertion is a bug found, with no FAIL print needed.
Coverage
Counts
A gap means
Line
Each statement that ran
Code no test reached
Branch
Each side of every if and case
A decision only ever went one way
Toggle
Each signal bit going 0 to 1 and 1 to 0
A bit that never changed, so a stuck bit would go unnoticed
Cover point
Each cover property that matched
A scenario you meant to test never happened
Watch out100% coverage doesn't mean the design is right: it means every part was exercised. You still need checks that notice a wrong answer. Coverage shows you where your checks never looked.
Practice
A 4-bit counter's testbench passes, but toggle coverage shows q[3] never changed. What does that tell you?
From code to chip
Simulate: check the behaviour with testbenches.
Synthesize: the tool turns your Verilog into a netlist of gates and flip-flops, and minimizes it, using methods like the ones in Chapters 7 and 8.
Place and route: decide where each cell goes and how to wire them.
Check timing: every path must meet the setup and hold rules from Chapter 17.
Program or manufacture: generate a bitstream for an FPGA, or masks for a chip.
TipEverything from this course shows up in that list. Synthesis uses K-maps' ideas, timing analysis is Chapter 17, and FPGA mapping fills the LUTs from Chapter 18.
Reading Verilog in exams and interviewsGATE: fine to skip at first
Questions usually ask what a snippet does. Work out the signal values for each clock edge in a table, using the old values for all <= assignments. Interviewers love the two cases in this chapter's animation: a swap with <=, and what goes wrong with =.
Practice · GATE-style
After one clock edge with a <= b; b <= a;, starting at a = 0 and b = 1, what are a and b?
Practice · GATE-style
After one clock edge with a = b; b = a;, starting at a = 0 and b = 1, what are a and b?
Practice · GATE-style
How many flip-flops does reg [7:0] count; create if assigned in always @(posedge clk)?
Practice · GATE-style
What does assign y = s ? a : b; describe?
Where to go from here
You've gone from a single bit to a complete view of digital design. The best way to make it stick is to build things. Here are some ways to keep going.