Port Mapping By Name
Part of the Fundamentals section of Coddy's Verilog journey — lesson 34 of 90.
In the previous lesson, you learned how to instantiate a module by passing signals in the same order as the ports appear in the module definition. This works, but it has a problem: order matters.
If you accidentally mix up the order, signals connect to the wrong ports. This can be hard to debug.
The Problem with Order Mapping
module or_gate (
input in1,
input in2,
output result
);
assign result = in1 | in2;
endmodule
// Order mapping - order MUST match
or_gate or1 (input_a, input_b, output_y); // Correct
or_gate or1 (input_b, input_a, output_y); // Wrong! Swapped inputsThe second line connects input_b to in1 and input_a to in2 — a subtle bug that is easy to miss.
Solution: Port Mapping By Name
Port mapping by name uses the port name to make connections. The order does not matter because each connection is explicitly labeled.
Syntax:
module_name instance_name (
.port_name(signal),
.port_name(signal)
);The dot . before the port name indicates we are referring to a port inside the module. The signal inside parentheses is what we connect to it.
Example
or_gate or1 (
.in1(input_a),
.in2(input_b),
.result(output_y)
);This explicitly says:
- Port
in1gets signalinput_a - Port
in2gets signalinput_b - Port
resultgets signaloutput_y
Order Does Not Matter
With port mapping by name, you can write the connections in any order:
// All three are IDENTICAL
or_gate or1 (
.in1(input_a),
.in2(input_b),
.result(output_y)
);
or_gate or1 (
.result(output_y),
.in1(input_a),
.in2(input_b)
);
or_gate or1 (
.in2(input_b),
.result(output_y),
.in1(input_a)
);All do exactly the same thing because each connection is labeled.
Challenge
Complete the instantiation by adding the missing port connections using port mapping by name.
What to do:
- Connect port
clkto signalclock_signal - Connect port
data_into signalinput_data - Connect port
data_outto signaloutput_data
Cheat sheet
Port mapping by name uses .port_name(signal) syntax, making order irrelevant:
module_name instance_name (
.port_name(signal),
.port_name(signal)
);Example:
or_gate or1 (
.in1(input_a),
.in2(input_b),
.result(output_y)
);The . before the port name refers to a port inside the module; the signal in parentheses is what connects to it. Unlike order mapping, connections can be listed in any order without causing bugs.
Try it yourself
module register (
input clk,
input [7:0] data_in,
output reg [7:0] data_out
);
always @(posedge clk) begin
data_out <= data_in;
end
endmodule
module top (
input clock_signal,
input [7:0] input_data,
output [7:0] output_data
);
register reg1 (
// TODO: Add port mappings using .port(signal) syntax
);
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic