0
0
VerilogConceptBeginner · 3 min read

What is Port Mapping in Verilog: Explanation and Example

In Verilog, port mapping is the process of connecting the ports of a module instance to signals or other ports in the parent module. It defines how inputs and outputs of a module are linked when you use that module inside another module.
⚙️

How It Works

Port mapping in Verilog works like connecting wires between two devices. Imagine you have a small box (a module) with pins (ports) on it. To use this box inside a bigger box, you need to connect each pin to the right wire so signals can flow correctly.

When you create an instance of a module, port mapping tells the Verilog compiler which signals in the bigger module connect to which ports of the smaller module. This can be done by position (ordered list) or by name (explicitly naming each connection), making it clear and organized.

💻

Example

This example shows a simple module called and_gate with two inputs and one output. We create an instance of it inside another module and connect its ports using port mapping by name.

verilog
module and_gate(input a, input b, output y);
  assign y = a & b;
endmodule

module top_module();
  wire x, z, out;
  assign x = 1'b1;
  assign z = 1'b0;

  and_gate u1(.a(x), .b(z), .y(out));
endmodule
🎯

When to Use

Use port mapping whenever you want to include one module inside another in Verilog. It helps you build complex designs by connecting smaller building blocks together. For example, when designing a CPU, you connect ALU modules, registers, and control units using port mapping.

Port mapping is essential for reusing modules and making your code organized and easy to understand.

Key Points

  • Port mapping connects module ports to signals in the parent module.
  • It can be done by position or by naming ports explicitly.
  • It enables modular and reusable design in Verilog.
  • Clear port mapping improves code readability and maintenance.

Key Takeaways

Port mapping links module ports to signals when instantiating modules in Verilog.
Use named port mapping for clearer and safer connections.
It helps build complex designs by connecting smaller modules.
Port mapping improves code reuse and readability.