C7 · Lesson 48 · Reading Verilog
Describing hardware in text
Nobody draws a million gates by hand. Chips are described in hardware description languages, Verilog and VHDL, and tools turn the text into gates and wires. Verilog looks like a programming language, but it isn't one: every line describes hardware that exists all at once, and an always block describes flip-flops that update together on a clock edge. This course's logic sims are written in a small part of Verilog, so the circuits you've been flipping switches on are exactly what the text says.
Play
- The sim below was built from this Verilog, gate by gate:
module blinker(input clk, input en, output led);
reg q;
always @(posedge clk)
if (en) q <= ~q;
assign led = q;
endmodule
- Turn
enon and off. What does theifturn into in the circuit? - How many flip-flops are there? Find the line that makes each one.
Predict
In the blinker, what does the if (en) become in hardware when en is 0?
A multiplexer that feeds the flip-flop's own output back to its input. Hardware can't "skip" a line: the flip-flop is there and clocked on every edge either way. An if without an else in an always @(posedge clk) block means "otherwise keep the old value", which is built as q <= en ? ~q : q: a mux in front of D. Look for it in the circuit.
Name it
Verilog, the parts you'll read most
module name(input a, input b, output y); // a block with its ports
wire t; // a connection
reg q; // a value set in an always block
assign t = a & ~b; // combinational logic: always true
assign y = s ? a : b; // a 2:1 multiplexer
always @(posedge clk) // flip-flops, updated on the edge
if (rst) q <= 0; else q <= t;
endmodule
Operators: & AND, | OR, ^ XOR, ~ NOT, ? : select, and for multi-bit values +, -, ==, << (shift).
Two kinds of description:
assigndescribes combinational logic. It isn't executed once; it's a wire permanently connected to gates, recomputed whenever an input changes.always @(posedge clk)describes sequential logic: every value assigned with<=inside it is a flip-flop (or a row of them), all loading together at the edge.
Nonblocking <=. All right-hand sides are read first, then all flip-flops update at once. So a <= b; b <= a; swaps them, exactly as two cross-wired flip-flops would. (The blocking = inside always blocks behaves sequentially and is used for combinational always blocks; mixing the two is a classic source of bugs.)
Vectors. wire [7:0] bus; is eight wires; count <= count + 1; on a reg [3:0] builds a 4-bit register and an adder. Synthesis tools turn all of this into gates from a library.
A multiplexer feeding a register: flip s, a and b and see y change at once, and q only at the clock's rising edge.
Worked example
What does always @(posedge clk) q <= ~q; build?
Show the solution
- The flip-flop. One flip-flop whose input is its own output inverted.
- Its output. It toggles on every clock edge, so q is a square wave at half the clock frequency.
Answer one flip-flop that toggles on every clock edge, making a square wave at half the clock frequency.
Worked example
What does assign y = (sel == 2'b10) ? a : b; build?
Show the solution
- The comparator. It checks the 2-bit sel against 10: one AND gate with an inverter, .
- The multiplexer. The comparator drives the select of a 2:1 multiplexer: y is a when sel is 2, b otherwise.
Answer a comparator, , driving the select of a 2:1 multiplexer.
Worked example
With 4-bit inputs a and b, what does assign {cout, s} = a + b + cin; build?
Show the solution
- Count the bits. The sum of two 4-bit numbers and a carry can reach 31, which needs five bits.
- The concatenation.
{cout, s}provides them: s takes the low four and cout the top one. - What the tool builds. A 4-bit adder with carry in and carry out, a ripple chain or a faster structure depending on the timing goals.
- What and how. One line of Verilog says what to compute; the synthesizer chooses how.
Answer a 4-bit adder with carry in and carry out.
Check
Write the Boolean function described by assign y = ~(a | b) & c;.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
With s = 0, a = 0 and b = 1, what does assign y = s ? b : a; give?
Check
An always block has reg [4-1:0] r; and always @(posedge clk) r <= d;. How many flip-flops does it make?
Check
In an always @(posedge clk) block, a <= b; b <= a;. What happens at each clock edge?
Check
What does assign y = a & b; describe?
Check
Write the logic of assign y = (sel == 2'b10) ? a : b;, with sel = {S1, S0}.
Write AND as · or & (or just AB), OR as +, XOR as ⊕, and NOT as ¬ or a trailing ': A·B + C' works.
Check
reg [N-1:0] c; always @(posedge clk) c <= c + 1; with N = 8 runs from a 10 MHz clock.
(a)
How many flip-flops does it build?
(b)
At what frequency does its top bit toggle?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Explain why
Why is Verilog called a hardware description language rather than a programming language?
A program is a list of instructions a processor carries out one after another. Verilog describes circuits: each assign is a set of gates that exist and operate continuously and in parallel, and each always block clocked by an edge describes flip-flops that all update at the same instant. The text is turned by synthesis tools into a netlist of gates and wires, not into instructions, so its lines describe structure that exists at once rather than steps that happen in order.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.