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

D10 · Lesson 4 · Modules, wires and assign

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

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.

  • 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.

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?

Name it

Modules and ports

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 wires, 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

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.

Worked example

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

Show the solution
  1. Add the inputs. 1011+0110=11+6=17=100012
  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.

Worked example

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

Show the 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=12bits

Answer 12 bits.

Worked example

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

Show the solution
  1. Copy the sign bit into the new bits. The sign bit of 1011 is 1: 1011→11111011
  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

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

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

Check

Write the full adder's sum, S.

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

Check

A ripple-carry adder has 16 bits, and each full adder adds 2 gate delays to the carry path. How many gate delays does the final carry take?

Check

How many bits wide is the concatenation of an 8-bit, a 1-bit and a 4-bit vector?

Check

A 4-bit adder adds 11 and 3 with no carry in.

(a)

What is the 4-bit sum?

(b)

What is the carry out?

Check

The 4-bit pattern 1011 is extended to 8 bits.

(a)

What unsigned 8-bit value does sign extension give?

(b)

What signed number is it?

Explain why

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

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.