Module instantiation lets you use one design inside another. It helps build bigger circuits from smaller parts.
Module instantiation syntax in Verilog
module_name instance_name (port_connections); // or with named ports module_name instance_name ( .port1(signal1), .port2(signal2), ... );
The module_name is the name of the module you want to use.
The instance_name is a unique name for this copy of the module.
adder my_adder (a, b, sum);adder my_adder (
.input1(a),
.input2(b),
.output_sum(sum)
);counter cnt1 (clk, reset, count);
This program defines a simple adder module and uses module instantiation to include it inside a top module. It changes inputs and shows the sum output.
module adder(input wire a, input wire b, output wire sum); assign sum = a ^ b; // corrected to XOR for 1-bit addition without carry endmodule module top; reg a, b; wire sum; adder my_adder(.a(a), .b(b), .sum(sum)); initial begin a = 1'b0; b = 1'b0; #10 a = 1'b1; b = 1'b0; #10 a = 1'b1; b = 1'b1; #10 $finish; end initial begin $monitor("At time %0t: a=%b b=%b sum=%b", $time, a, b, sum); end endmodule
Using named port connections helps avoid mistakes when ports change order.
Each instance name must be unique inside the parent module.
Module instantiation is like plugging a smaller circuit into a bigger one.
Module instantiation lets you reuse and connect modules inside other modules.
You can connect ports by order or by name; named connections are safer.
Each instance needs a unique name to avoid confusion.
Practice
What is the main purpose of module instantiation in Verilog?
Solution
Step 1: Understand module instantiation concept
Module instantiation allows you to use one module inside another, making design reusable.Step 2: Identify the correct purpose
Among the options, only reusing and connecting modules matches the purpose of instantiation.Final Answer:
To reuse and connect modules inside other modules -> Option BQuick Check:
Module instantiation = reuse modules [OK]
- Confusing instantiation with variable declaration
- Thinking instantiation is for comments
- Mixing instantiation with clock setup
Which of the following is the correct syntax for instantiating a module named adder with ports a, b, and sum using named port connections?
module adder(input a, input b, output sum); ... endmodule
Solution
Step 1: Recall named port connection syntax
Named connections use dot before port name and parentheses for signal, like .port(signal).Step 2: Check instance name placement
The instance name comes after the module name, soadder my_adder(...);is correct.Final Answer:
adder my_adder(.a(a), .b(b), .sum(sum)); -> Option DQuick Check:
ModuleName InstanceName(.port(signal)) [OK]
- Placing instance name before module name
- Using = instead of parentheses in port connections
- Omitting instance name
Given the module and instantiation below, what is the value of out after simulation?
module inverter(input in, output out); assign out = ~in; endmodule module top; reg a = 1'b0; wire b; inverter inv1(.in(a), .out(b)); endmodule
Solution
Step 1: Understand inverter module behavior
The inverter outputs the bitwise NOT of inputin. Ifinis 0, output is 1.Step 2: Check input value and connection
Inputais initialized to 0, connected to inverter inputin. So outputb= ~0 = 1.Final Answer:
b = 1'b1 -> Option AQuick Check:
~0 = 1, so output = 1 [OK]
- Confusing input and output signals
- Assuming output equals input
- Missing initial value for input
Identify the error in the following module instantiation:
module counter(input clk, output reg [3:0] count); ... endmodule module top; wire clk; wire [3:0] cnt; counter cnt_inst(clk, cnt); counter cnt_inst(clk, cnt); endmodule
Solution
Step 1: Check instance names in top module
Two instances namedcnt_instare declared, which is not allowed.Step 2: Verify other syntax elements
Semicolons and port order appear correct; output port type mismatch is allowed since output is connected to wire.Final Answer:
Duplicate instance name 'cnt_inst' used -> Option CQuick Check:
Instance names must be unique [OK]
- Using same instance name twice
- Confusing port order with named connections
- Thinking output port must be reg in top
You want to instantiate a module mux2to1 twice inside a top module, connecting inputs a, b, select sel, and outputs out1, out2. Which is the correct way to instantiate both with unique instance names and named port connections?
Solution
Step 1: Confirm named port connection syntax
Named ports use dot before port name and parentheses around signal, e.g., .a(a).Step 2: Check instance names and separation
Each instance must have a unique name and be separated by semicolons.Step 3: Validate options
mux2to1 mux1(.a(a), .b(b), .sel(sel), .out(out1)); mux2to1 mux2(.a(a), .b(b), .sel(sel), .out(out2)); correctly names instances mux1 and mux2, uses named ports, and separates with semicolons.Final Answer:
mux2to1 mux1(.a(a), .b(b), .sel(sel), .out(out1)); mux2to1 mux2(.a(a), .b(b), .sel(sel), .out(out2)); -> Option AQuick Check:
Unique instance names + named ports + semicolons = correct [OK]
- Using same instance name twice
- Separating instances with commas instead of semicolons
- Placing instance name after port list
Ready to prove it?
Pass the test and this pattern moves unknown
