理解 Verilog 端口:输入、输出与模块通信
端口是作为特定模块输入和输出的一组信号,是与模块通信的主要方式。将模块视为放置在 PCB 上的芯片,很明显与芯片通信只能通过其引脚。端口如同引脚,设计用于向外部发送和接收信号。
端口类型
| 端口 | 描述 |
|---|---|
| Input | 设计模块仅通过 input 端口从外部接收值 |
| Output | 设计模块仅通过 output 端口向外部发送值 |
| Inout | 设计模块可通过 inout 端口发送或接收值 |
端口默认视为 wire 类型线网。
语法
inout 声明的端口可同时作为输入和输出。
input [net_type] [range] list_of_names; // 输入端口
inout [net_type] [range] list_of_names; // 输入输出端口
output [net_type] [range] list_of_names; // 由 wire 驱动的输出端口
output [var_type] [range] list_of_names; // 由变量驱动的输出端口
示例
下面代码有三个 input 端口、一个 output 端口和一个 inout 端口。
module my_design (
input wire clk,
input en,
input rw,
inout [15:0] data,
output int
);
// Design behavior as Verilog code
endmodule
对多个端口使用相同 名称 是非法的。
input aport; // 第一次声明 - 合法
input aport; // Error - already declared
output aport; // Error - already declared
带符号端口
signed 属性可附加到端口声明或线网/变量声明或两者。隐式线网默认 无符号。
module (input a, b, output c);
// ports a, b, and c are by default unsigned
endmodule
若线网/变量声明有 signed 属性,则另一端也视为带符号。
module (input signed a, b, output c);
wire a, b; // a, b 从端口声明获得 signed 属性
reg signed c; // c 从 reg 声明获得 signed 属性
endmodule
端口声明变体
Verilog 1995
原始 IEEE 1995 版本端口声明方式:模块声明先在括号内列出端口名,再在模块体内定义方向。
module test (a, b, c);
input [7:0] a; // inputs "a" and "b" are wires
input [7:0] b;
output [7:0] c; // output "c" by default is a wire
// Still, you can declare them again as wires to avoid confusion
wire [7:0] a;
wire [7:0] b;
wire [7:0] c;
endmodule
module test (a, b, c);
input [7:0] a, b;
output [7:0] c; // By default c is of type wire
// port "c" is changed to a reg type
reg [7:0] c;
endmodule
Verilog 2001 及以后
2001 年引入 ANSI-C 风格端口命名,允许在端口列表中指定类型。
module test (
input [7:0] a, b, // "b" is considered an 8-bit input
output [7:0] c
);
// Design content
endmodule
module test (
input wire [7:0] a,
input wire [7:0] b,
output reg [7:0] c
);
// Design content
endmodule
若端口声明包含线网或变量类型,则该端口视为完全声明。在线网或变量类型声明中重复声明同一端口是非法的。
module test (
input [7:0] a, // a, e 隐式声明为 wire 类型
output reg [7:0] e
);
wire signed [7:0] a; // illegal - declaration of a is already complete -> simulator dependent
wire [7:0] e; // illegal - declaration of e is already complete
// Rest of the design code
endmodule
若端口声明不包含线网或变量类型,则可在线网或变量类型声明中再次声明该端口。
module test (
input [7:0] a,
output [7:0] e
);
reg [7:0] e; // Okay - net_type was not declared before
// Rest of the design code
endmodule
Verilog
- Verilog基础:设计你的第一个与门
- Verilog 入门:数字电路设计完整教程
- 掌握 Verilog 拼接操作:实用指南
- 深入理解 Verilog 赋值语句:类型、语法与最佳实践
- Verilog阻塞与非阻塞赋值:关键区别详解
- 掌握Verilog函数:高效RTL设计的复用利器
- 理解Verilog任务:函数vs任务、语法与用法全解析
- Verilog 时钟发生器设计与实现指南
- 掌握 Verilog 数学函数:高效硬件设计的利器
- 掌握 Verilog $timeformat:精度控制与显示格式化
- 理解与打印 Verilog 任意作用域下的 Timescale
- 掌握 Verilog 文件 I/O:打开、读取、写入和关闭