Draft preview. Lessons are being written and reviewed.

Digital Design / Module 3: Verilog that means hardware

12. Modules, ports and hierarchy

Structure a design the way a chip is structured.

Lecture coming soon

See it move

The idea

Verilog looks like a programming language, yet it describes a circuit. A module is a block of hardware with pins, not a function that gets called. Every instance exists permanently and runs at the same time as every other instance. Instantiating a module twice gives you two physical copies, and it costs twice the area.

module mux2(input a, b, sel, output y);
  assign y = sel ? b : a;
endmodule

module mux4(input [3:0] d, input [1:0] s, output y);
  wire lo, hi;
  mux2 u_lo (.a(d[0]), .b(d[1]), .sel(s[0]), .y(lo));
  mux2 u_hi (.a(d[2]), .b(d[3]), .sel(s[0]), .y(hi));
  mux2 u_top(.a(lo),   .b(hi),   .sel(s[1]), .y(y));
endmodule

Here mux4 holds three real muxes. The order of those lines doesn't matter, since they all exist at once. The animation above shows the same two-level selection.

Hierarchy

Chips are built in layers: a core contains units, units contain blocks, and blocks contain small modules. A good boundary hides detail and makes a piece easy to test alone. Teams usually register outputs at the edges of large blocks, because this keeps timing paths from running across several blocks.

Named connections

Always connect ports by name, as above. With positional connections, adding a port or reordering one can silently rewire a design with no error at all. Named connections state exactly what goes where.

Common mistake

Thinking of instances as sequential steps, as if u_lo "runs before" u_top. Nothing runs first. Signals propagate through all the hardware continuously. Reading RTL from top to bottom as a program is the most common source of confusion for people arriving from software.

Key ideas

  • A module is a block of hardware, not a function call.
  • Use named port connections.
  • Instances all exist at once and run in parallel.

Your exercise

Build a 4:1 mux from instances of a 2:1 mux.

Interview questions

coreNamed or positional port connection?

basicWhat is the difference between a module and an instance?

basicA mux4 module contains three mux2 instances. A design instantiates mux4 four times. How many mux2 circuits end up in the hardware?

coreIn the lesson's mux4, does u_lo run before u_top because it is written first?

coreA submodule's port list is reordered from (a, b, y) to (b, a, y). The parent connects by position, as shown. What happens?

module sub(input b, input a, output y);  // was (a, b, y)
  assign y = a & ~b;
endmodule

sub u0 (x1, x2, o1);   // positional

coreThis compiles, but lo stays at z in simulation. Why, and how can you make the tool catch it?

wire lo;
mux2 u_lo (.a(d0), .b(d1), .sel(s), .y(l0));

coreAn 8-bit signal is connected to a 4-bit input port. Is that an error in Verilog?

advancedWhy do teams usually register the outputs of large blocks?

advancedTwo instances both drive their outputs onto the same wire bus. One drives 0 and the other drives 1. What does simulation show, and what does it mean?

drv ua (.v(v1), .y(bus));   // v1 = 0
drv ub (.v(v2), .y(bus));   // v2 = 1

Practise these 9 questions with animations