---
title: Reading Verilog
heading: Describing hardware in text
eyebrow: C7 · Lesson 48 · Reading Verilog
xp: 50
---

::: hook
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:

```verilog
module blinker(input clk, input en, output led);
  reg q;
  always @(posedge clk)
    if (en) q <= ~q;
  assign led = q;
endmodule
```

- Turn `en` on and off. What does the `if` turn into in the circuit?
- How many flip-flops are there? Find the line that makes each one.
:::
:::

```sim logic
module blinker(input clk, input en, output led);
  reg q;
  always @(posedge clk)
    if (en) q <= ~q;
  assign led = q;
endmodule
clock clk 6
set en = 1
wave: clk, en, q
time: 48
```

::: predict
## In the blinker, what does the `if (en)` become in hardware when `en` is 0?

- [ ] The clock stops
- [x] A multiplexer feeds q back into its own flip-flop, so it holds its value
- [ ] q resets to 0
- [ ] Nothing: the line is skipped

::: reveal
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.
:::
:::

::: nameit
## Verilog, the parts you'll read most

```verilog
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:**

- `assign` describes **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.
:::

```sim logic
module mux_reg(input s, input a, input b, output y, output q);
  assign y = s ? b : a;
  always @(posedge clk) q <= y;
endmodule
clock clk 8
wave: clk, s, a, b, y, q
time: 48
```

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.


::: example
## What does `always @(posedge clk) q <= ~q;` build?

::: solution
1. **The flip-flop.** One flip-flop whose input is its own output inverted.
2. **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.
:::
:::

::: example
## What does `assign y = (sel == 2'b10) ? a : b;` build?

::: solution
1. **The comparator.** It checks the 2-bit sel against 10: one AND gate with an inverter, $S_1\bar S_0$.
2. **The multiplexer.** The comparator drives the select of a 2:1 multiplexer: y is a when sel is 2, b otherwise.

**Answer:** a comparator, $S_1\bar S_0$, driving the select of a 2:1 multiplexer.
:::
:::

::: example
## With 4-bit inputs a and b, what does `assign {cout, s} = a + b + cin;` build?

::: solution
1. **Count the bits.** The sum of two 4-bit numbers and a carry can reach 31, which needs five bits.
2. **The concatenation.** `{cout, s}` provides them: s takes the low four and cout the top one.
3. **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.
4. **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
kind: boolean
vars: a, b, c
answer: ~(a | b) & c

## Write the Boolean function described by `assign y = ~(a | b) & c;`.

::: right
Right: NOR of a and b, ANDed with c.
:::

::: misses
- [~a | ~b & c] The NOT applies to the whole (a | b): De Morgan gives ¬a·¬b·c.
- [any] Not yet. Read the brackets carefully.
:::
:::

::: check
given: a = 0 | 1; s = 0 | 1; let b = 1 - a
answer: s*b + (1 - s)*a
tolerance: 0.01

## With s = {s}, a = {a} and b = {b}, what does `assign y = s ? b : a;` give?

::: right
Right: {answer}. s = 1 selects b; s = 0 selects a.
:::

::: misses
- [s*a + (1 - s)*b] It's the other way round: `s ? b : a` picks b when s is 1.
- [any] Not yet. `c ? x : y` is x when c is 1.
:::
:::

::: check
given: n = 2 | 4 | 8 | 16
answer: n
tolerance: 0

## An always block has `reg [{n}-1:0] r;` and `always @(posedge clk) r <= d;`. How many flip-flops does it make?

::: right
Right: {answer}, one per bit.
:::

::: misses
- [1] A vector register is one flip-flop per bit.
- [any] Not yet. Count the bits.
:::
:::

::: check
kind: choice

## In an `always @(posedge clk)` block, `a <= b; b <= a;`. What happens at each clock edge?

- [ ] a and b both get b's old value
- [x] a and b swap values
- [ ] Nothing: they cancel
- [ ] It depends on the order of the lines

::: right
Right. Nonblocking assignments read every right-hand side before any update, just as real flip-flops sample together.
:::

::: misses
- [1] That's what blocking `=` would do, executed in order.
- [3] They do update: to each other's old values.
- [4] With `<=` the order doesn't matter.
:::
:::

::: check
kind: choice

## What does `assign y = a & b;` describe?

- [ ] A line of code run once when the program starts
- [x] An AND gate whose output y always follows a and b
- [ ] A flip-flop storing a AND b
- [ ] A test that a and b are equal

::: right
Right. An assign is permanent wiring.
:::

::: misses
- [1] Verilog describes hardware; an assign holds all the time.
- [3] Flip-flops come from always @(posedge ...) blocks.
- [4] That would be `a == b`.
:::
:::

::: check
kind: boolean
vars: S1, S0, a, b
answer: S1 & ~S0 & a | ~(S1 & ~S0) & b
ops: and, or, not

## Write the logic of `assign y = (sel == 2'b10) ? a : b;`, with sel = {S1, S0}.

::: right
Right: $y = S_1\bar S_0a + \overline{S_1\bar S_0}\,b$.
:::

::: misses
- [any] Not yet. The condition is $S_1\bar S_0$; it selects a, and otherwise b.
:::
:::

::: check
kind: parts
given: N = 4 | 8 | 16 | 24; f = 10 | 50 | 100

## `reg [N-1:0] c; always @(posedge clk) c <= c + 1;` with N = {N} runs from a {f} MHz clock.

::: part
answer: N
tolerance: 0

### How many flip-flops does it build?

::: misses
- [any] One per bit.
:::
:::

::: part
answer: f*1e6/2^N
unit: Hz
tolerance: 0.5%

### At what frequency does its top bit toggle?

::: misses
- [any] $\frac{f}{2^N}$.
:::
:::
:::

::: explain
## Why is Verilog called a hardware description language rather than a programming language?

::: model
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

::: check
given: a = 0 | 1; b = 0 | 1
answer: abs(a - b)
tolerance: 0.01

## `assign y = a ^ b;` with a = {a}, b = {b}. y?

::: misses
- [any] XOR: 1 when they differ.
:::
:::

::: check
kind: boolean
vars: a, b
answer: ~a & b

## The function of `assign y = ~a & b;`?

::: misses
- [any] NOT a, AND b.
:::
:::

::: check
kind: choice

## Which construct makes flip-flops?

- [ ] assign
- [x] always @(posedge clk) with <=
- [ ] wire
- [ ] module

::: right
Right. Each value it assigns becomes a register.
:::

::: misses
- [1] Assign is combinational.
- [3] A wire is a connection.
- [4] A module is a container.
:::
:::
:::
