---
title: Verilog modules, wires and assign
heading: Describing hardware as modules with ports, wires that carry signals and continuous assignments that compute them, multi-bit vectors and concatenation, building bigger modules by instantiating smaller ones, and the full adder and ripple-carry adder as the first examples
eyebrow: D10 · Lesson 4 · Modules, wires and assign
xp: 40
---

::: hook
A Verilog **module** is a box with pins. Inside, wires carry signals and `assign` statements say how each wire is computed from others, and those are all the language needs to describe any combinational circuit. Bigger designs are built the way boards are: by placing copies of smaller modules and wiring their pins together. A full adder takes three lines; four of them chained make a 4-bit adder; the same pattern scales to every adder in every processor.
:::

::: play
- A full adder module: inputs A, B and CIN (carry in), outputs S (sum) and COUT (carry out), with its truth table.
- S is 1 when an odd number of the inputs are 1; COUT is 1 when at least two are.
- The block beside it is the circuit the code describes: two XOR gates for the sum, and an AND-OR network for the carry.
:::

```sim logic
module full_adder(input A, input B, input CIN, output S, output COUT);
assign S = A ^ B ^ CIN;
assign COUT = A & B | CIN & (A ^ B);
endmodule
table: on
```

::: predict
## A 4-bit ripple-carry adder chains four full adders, each adding two gate delays to the carry. How many gate delays does the final carry take after the inputs change?

- [ ] 2
- [ ] 4
- [x] 8
- [ ] 16

::: reveal
Each full adder must wait for the carry from the one below, then add two gate delays (an AND and an OR) before passing its own carry on. Four stages make 4 × 2 = 8 gate delays. A 64-bit ripple adder would take 128, which is why fast adders compute carries in parallel (C7, S3).
:::
:::

::: nameit
## Modules and ports

```verilog
module full_adder(input a, input b, input cin,
                  output s, output cout);
  assign s    = a ^ b ^ cin;
  assign cout = (a & b) | (cin & (a ^ b));
endmodule
```

Ports are declared `input` or `output`, signals inside are `wire`s, and each `assign` continuously drives one wire from an expression of others: `&` AND, `|` OR, `^` XOR, `~` NOT, and `c ? a : b` for a multiplexer.

## Vectors

`wire [7:0] x;` declares an 8-bit bus, bit 7 most significant; an `[N-1:0]` vector has N bits. `x[3]` is one bit, `x[7:4]` a slice, and `{a, b}` **concatenates**, its width the sum of the parts'. Arithmetic works on whole vectors: `assign sum = x + y;` builds an adder of the right width.

## Instantiation

```verilog
module add4(input [3:0] a, input [3:0] b, input cin,
            output [3:0] s, output cout);
  wire c1, c2, c3;
  full_adder fa0(a[0], b[0], cin, s[0], c1);
  full_adder fa1(a[1], b[1], c1,  s[1], c2);
  full_adder fa2(a[2], b[2], c2,  s[2], c3);
  full_adder fa3(a[3], b[3], c3,  s[3], cout);
endmodule
```

Each line places a copy of `full_adder`, and the wires `c1` to `c3` connect them: this is the ripple-carry adder. A **generate** loop can place N copies for any width.
:::

::: example
## What does the 4-bit adder output for a = 1011 and b = 0110 with cin = 0?

::: solution
1. **Add the inputs.**
   $$\begin{aligned} 1011 + 0110 &= 11 + 6 \\ &= 17 \\ &= 1\,0001_2 \end{aligned}$$
2. **Read the outputs.** The low four bits are the sum and the fifth is the carry: s = 0001 and cout = 1.
3. **Follow the carry.** The carry ripples through all four stages, since bits 1, 2 and 3 each produce or pass one.

**Answer:** s = 0001 and cout = 1.
:::
:::

::: example
## How wide is `{a[7:4], b, 2'b01}` if b is a 6-bit vector?

::: solution
1. **Width of each piece.** `a[7:4]` is 4 bits, `b` is 6 and `2'b01` is 2.
2. **Add them.** Concatenation widths simply add:
   $$4 + 6 + 2 = 12\ \text{bits}$$

**Answer:** 12 bits.
:::
:::

::: example
## The 4-bit signed value 1011 is sign-extended to 8 bits. What results, and what number is it?

::: solution
1. **Copy the sign bit into the new bits.** The sign bit of 1011 is 1:
   $$1011 \to 1111\,1011$$
2. **Read the value.** 1111 1011 is −5, the same as 1011 in 4-bit two's complement.
3. **Write it in Verilog.** `{{4{x[3]}}, x}` does it.
4. **Zero extension.** Zero-extending instead would give 0000 1011 = 11, a classic bug when mixing signed and unsigned.

**Answer:** 1111 1011, which is −5, the same number as before.
:::
:::

::: check
kind: boolean
vars: A, B, CIN
answer: A & B | A & CIN | B & CIN
ops: and, or, not, xor

## Write a full adder's carry out, COUT, in terms of A, B and CIN.

::: right
Right. COUT is the majority function: 1 when at least two inputs are 1.
:::

::: misses
- [any] COUT is 1 when two or more of the three inputs are 1.
:::
:::

::: check
kind: boolean
vars: A, B, CIN
answer: A ^ B ^ CIN
ops: and, or, not, xor

## Write the full adder's sum, S.

::: right
Right. S is the parity of the three inputs.
:::

::: misses
- [any] S is 1 when an odd number of inputs are 1.
:::
:::

::: check
given: n = 4 | 16 | 32 | 64; d = 2 | 3
answer: n*d
tolerance: 0.4

## A ripple-carry adder has {n} bits, and each full adder adds {d} gate delays to the carry path. How many gate delays does the final carry take?

::: right
Right. n × d = {answer}.
:::

::: misses
- [d] The carry passes through every stage in turn.
- [any] Not yet. Stages times delay per stage.
:::
:::

::: check
given: a = 4 | 8 | 16; b = 1 | 3 | 6; c = 2 | 4
answer: a + b + c
tolerance: 0.4

## How many bits wide is the concatenation of an {a}-bit, a {b}-bit and a {c}-bit vector?

::: right
Right. {answer} bits.
:::

::: misses
- [any] Not yet. Concatenation widths add.
:::
:::

::: check
kind: parts
given: a = 5 | 11 | 14; b = 3 | 6 | 9

## A 4-bit adder adds {a} and {b} with no carry in.

::: part
answer: a + b - 16*floor((a + b)/16)
tolerance: 0

### What is the 4-bit sum?

::: misses
- [any] Sum modulo 16.
:::
:::

::: part
answer: floor((a + b)/16)
tolerance: 0

### What is the carry out?

::: misses
- [any] 1 if the sum reaches 16.
:::
:::
:::

::: check
kind: parts
given: x = 9 | 11 | 13 | 14

## The 4-bit pattern {x:bin4} is extended to 8 bits.

::: part
answer: x + 240*floor(x/8)
tolerance: 0

### What unsigned 8-bit value does sign extension give?

::: misses
- [x] That's zero extension; sign extension copies bit 3.
- [any] Copy the sign bit into the new top bits.
:::
:::

::: part
answer: x - 16*floor(x/8)
tolerance: 0

### What signed number is it?

::: misses
- [any] Two's complement: subtract 16 when bit 3 is set.
:::
:::
:::

::: explain
## Why is instantiating a module in Verilog different from calling a function in software?

::: model
Calling a function in software reuses one piece of code over and over in time: the processor jumps to it, runs it and returns, and only one call is active at a time on that processor. Instantiating a module in Verilog builds a new, separate copy of its hardware each time, with its own gates and wires, and all the copies work simultaneously. Four full-adder instances are four physical adders sitting side by side, connected by wires, all computing at once. That's why a module's instances cost area in proportion to their number, and why a loop in a generate block builds hardware instead of repeating an action.
:::
:::

::: practice

::: check
kind: boolean
vars: A, B
answer: A & ~B | ~A & B
ops: and, or, not

## Write a half adder's sum using only AND, OR and NOT.

::: misses
- [any] 1 when exactly one input is 1.
:::
:::

::: check
given: k = 2 | 8 | 24 | 64 | 128; d = 1 | 2 | 3 | 4
answer: k*d
tolerance: 0.4

## Carry delay: {k} bits, {d} delays each?

::: misses
- [any] n × d.
:::
:::
:::
