BitFlow
← Academy·From Gates to Verilog
</>Core~40 min

From Gates to Verilog

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.

Loading the animation…

Write your first module →The Learning Path starts here, with instant feedback from a real simulator.

Flip-flops and blocking assignments

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…

DescribingUseWritten
Combinational logicBlockingalways @(*) with =
Flip-flopsNonblockingalways @(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 writeYou getFix
if with no else in always @(*)An accidental latchAssign a default first, or cover every case
= in a clocked blockOrder-dependent behaviourUse <=
A signal driven from two blocksMultiple drivers: X in simulation, or a synthesis errorDrive each signal from one place
A missing signal in always @(a)Simulation differs from hardwareUse always @(*)
#10 delays in a designIgnored by synthesisUse 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
A small self-checking testbench. !== compares exactly, so it also catches X and Z.
Open the Sandbox →Write Verilog and a testbench, simulate it with a real simulator, and see the waveform.

Assertions and coverage

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.
CoverageCountsA gap means
LineEach statement that ranCode no test reached
BranchEach side of every if and caseA decision only ever went one way
ToggleEach signal bit going 0 to 1 and 1 to 0A bit that never changed, so a stuck bit would go unnoticed
Cover pointEach cover property that matchedA 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

  1. Simulate: check the behaviour with testbenches.
  2. 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.
  3. Place and route: decide where each cell goes and how to wire them.
  4. Check timing: every path must meet the setup and hold rules from Chapter 17.
  5. 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.
Put a design on a real board →Synthesize in your browser and flash an FPGA over USB.

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.

Solve Verilog problems →Graded problems from a first wire up to full designs, with hidden tests.Build real hardware in the Board Lab →LEDs, sensors, serial, and sound, simulated or on a real FPGA board.Take the circuit challenges →Earn XP building circuits within a gate budget.See the VLSI career roadmap →RTL design, verification, DFT and physical design: what each job does, and how to prepare.
Was this chapter helpful?