C749 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 49 · Writing Verilog

Describing hardware so a tool can build it

Lesson 48 read Verilog; this lesson writes it. The language looks like a programming language, but it describes hardware: every assign is a permanent network of gates, every always @(posedge clk) block is a bank of flip-flops, and the synthesis tool's job is to turn the description into exactly those circuits. Writing it well means writing descriptions whose hardware you can picture, and avoiding the few patterns (missing branches, mixed assignment styles) that produce circuits you didn't intend.

  • A 2-bit counter with an enable EN and a synchronous reset R, written the way a synthesis tool expects: each bit is a flip-flop, the reset takes priority, and a bit toggles when every lower bit is 1 and the counter is enabled.
  • The waveform shows it counting 0, 1, 2, 3, 0, ..., holding while EN is low, and clearing at the first clock edge after R goes high.

Predict

In a clocked block, a <= b; b <= a; runs on each edge. What happens to a and b?

Name it

Modules and structure

module counter #(parameter W = 8) (input clk, input rst, input en, output reg [W-1:0] q);
  always @(posedge clk)
    if (rst)     q <= 0;
    else if (en) q <= q + 1;
endmodule

A module has ports; #(parameter W = 8) makes its width adjustable; instances of modules inside others build hierarchy (counter #(.W(4)) c1 (.clk(clk), .rst(r), .en(1'b1), .q(count));). A vector [W-1:0] is W wires, and q + 1 on it synthesizes a W-bit incrementer.

Combinational logic

Use assign for simple expressions, or always @(*) with case for anything with branches:

always @(*)
  case (sel)
    2'b00: y = a;
    2'b01: y = b;
    2'b10: y = c;
    default: y = 0;
  endcase

Every output must be assigned on every path. If some branch leaves y unassigned, y must hold its old value, and the tool builds a latch: almost always a bug. A default (or assigning y before the case) prevents it.

Sequential logic and assignment styles

  • In always @(posedge clk) blocks use nonblocking <=: all flip-flops update together.
  • In always @(*) blocks use blocking =: statements behave in order, like gates feeding gates.
  • Never assign one signal from two blocks.

State machines

Three parts: a state register (always @(posedge clk) state <= next;), next-state logic (always @(*) case (state) ...), and output logic. Name states with localparam so the code reads like the diagram.

Testbenches

A testbench is a module with no ports that instantiates the design, drives its inputs with initial blocks and delays (#10 en = 1;), and checks outputs with $display or assertions. It runs in a simulator and is never synthesized.

Worked example

What hardware does this describe? always @(posedge clk) if (load) r <= d; else r <= {r[6:0], 1'b0}; with reg [7:0] r.

Show the solution
  1. The registers. Eight flip-flops with a 2:1 multiplexer in front of each.
  2. Load. When load is 1 they take d.
  3. Shift. Otherwise each takes its lower neighbor's value and bit 0 takes 0.

Answer a loadable shift register that shifts left one place per clock.

Worked example

This combinational block has a bug: always @(*) if (en) y = a;. What does it build, and how do you fix it?

Show the solution
  1. Find the missing path. When en is 0 nothing assigns y, so y must remember its value: the tool infers a latch enabled by en.
  2. Fix it. Add else y = 0; (or whatever y should be) so y is assigned on every path.

Answer a latch enabled by en; adding an else that assigns y removes it.

Worked example

Compare always @(posedge clk) begin b = a; c = b; end with the same block using <=. What hardware does each build?

Show the solution
  1. With blocking assignments. With =, the statements run in order within the clock edge: b takes a, then c takes the new b, which is a.
  2. What that builds. Both registers load a, so c is just a copy of b, one stage.
  3. With nonblocking assignments. With <=, every right-hand side is read before any register updates: b takes a, and c takes the old b.
  4. What this builds. Two flip-flops in a chain, a shift register.
  5. Which to use. Clocked blocks use <= so that the code means the hardware it looks like.

Answer with =, one stage (c is a copy of b); with <=, two flip-flops in a chain, a shift register.

Check

How many wires does a vector declared [7:0] have?

Check

A reg [4-1:0] q starts at 15 and runs q <= q + 1; on every clock. What does it hold after 12 clocks?

Check

How many flip-flops does reg [8-1:0] r; always @(posedge clk) r <= r + 1; create?

Check

In a clocked block, a = b; b = a; (blocking) runs on each edge. What happens?

Check

A combinational always @(*) block assigns y in the if branch but not in the else. What does synthesis build?

Check

What does assign y = s ? a : b; compute? Write it with AND, OR and NOT (inputs S, A, B).

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Check

always @(posedge clk) r <= {r[N-2:0], d}; with N = 8, and a second block that copies its input through k nonblocking registers in a chain.

(a)

After how many clocks does a bit of d reach r[N−1]?

(b)

How many flip-flops do the two blocks build together?

Check

The buggy block always @(*) if (en) y = a; infers a latch. Write its output y as a function of en, a, and the latch's stored value q.

Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.

Explain why

Why do clocked blocks use nonblocking assignments and combinational blocks use blocking ones?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.