18verilog模块与端口
Verilog模块与端口详解
📑 目录
1. 结构化建模概述
结构化建模是Verilog HDL实现层次化、模块化设计的基础,主要包括:
- 门级例化(Primitive Instantiation)
- UDP用户定义原语(User Defined Primitive)
- module模块例化语句(Module Instantiation)
通过结构化建模,可以实现电路的分层、复用和可维护性。
2. 门级例化
门级例化直接调用Verilog内置的基本门(如and、or、not等)实现组合逻辑。
示例:
module gate_example(
input wire a, b, c,
output wire y
);
wire n1;
and u1(n1, a, b); // 与门例化
or u2(y, n1, c); // 或门例化
endmodule
3. UDP用户定义原语
UDP(User Defined Primitive)允许用户自定义基本逻辑单元,适用于特殊逻辑建模。
示例:
primitive my_and(y, a, b);
output y;
input a, b;
table
// a b : y
0 0 : 0;
0 1 : 0;
1 0 : 0;
1 1 : 1;
endtable
endprimitive
4. module模块例化语句
模块例化是结构化建模的核心,详见模块例化章节。
示例:
module top;
wire [3:0] a, b;
wire [4:0] sum;
adder u_adder(
.a(a),
.b(b),
.sum(sum)
);
endmodule
5. 端口声明与连接
5.1 端口声明方式
input
/output
/inout
三种类型- 支持向量端口(如
input [7:0] data_in
)
示例:
module port_example(
input wire clk,
input wire [7:0] data_in,
output reg [7:0] data_out
);
always @(posedge clk) begin
data_out <= data_in;
end
endmodule
5.2 端口连接方式
- 顺序端口连接(位置关联)
- 显式端口连接(命名关联,推荐)
6. PAD及IO端口建模
6.1 PAD建模简介
PAD(IO端口)用于描述芯片与外部的物理连接。
示例:
module pad_example(
inout wire pad,
input wire oe, din,
output wire dout
);
assign pad = oe ? din : 1'bz; // 输出使能
assign dout = pad; // 读回数据
endmodule
7. 最佳实践与常见问题
- 优先使用显式端口声明和连接,提升可读性
- 端口宽度、方向要严格匹配
- 门级例化适合小型组合逻辑,复杂电路建议用模块例化
- UDP适合特殊场景,实际工程中较少用
- PAD建模需注意三态和总线冲突
💡 总结:结构化建模是Verilog工程设计的基础,合理使用门级例化、UDP和模块例化可提升设计的层次性和可维护性。