Verilog HDL 简介与基本结构
了解 Verilog HDL 的历史、抽象层次与模块基本组成
什么是 Verilog HDL#
Verilog HDL 是一种用于数字逻辑电路设计的硬件描述语言(Hardware Description Language),可以用来进行数字电路的仿真验证、时序分析、逻辑综合。
- 用 Verilog HDL 描述的电路设计就是该电路的 Verilog HDL 模型
- Verilog HDL 既是一种行为描述语言也是一种结构描述语言
发展历史#
| 年份 | 事件 |
|---|---|
| 1983 | 由 GDA(GateWay Design Automation)公司的 Phil Moorby 首创 |
| 1989 | Cadence 公司收购了 GDA 公司 |
| 1990 | Cadence 公司公开发表 Verilog HDL |
| 1995 | IEEE 制定并公开发表 Verilog HDL 1364-1995 标准 |
| 1999 | 模拟和数字电路都适用的 Verilog 标准公开发表 |
不同层次的抽象#
Verilog HDL 模型可以是实际电路不同级别的抽象,共分为四级:
| 抽象级别 | 描述方式 |
|---|---|
| 行为级 | 用高级语言结构描述逻辑功能和算法 |
| RTL 级 | 描述数据在寄存器之间流动和处理方式 |
| 门级 | 描述逻辑门(与、非、或、与非、三态门等)及其连接 |
| 开关级 | 描述三极管和储存节点及其连接 |
Verilog HDL 的特点#
- 形式化地表示电路的行为和结构
- 借用 C 语言的结构和语句
- 可在多个层次描述系统,对设计规模不加限制
- 具有混合建模能力:同一设计中各子模块可用不同抽象级别描述
- 基本逻辑门、开关级结构模型均内置于语言中,可直接调用
- 易创建用户定义原语(UDP,User Defined Primitive)
模块基本结构#
Verilog 的基本设计单元是模块(module),由 module 和 endmodule 关键词之间的四个主要部分组成:
Verilog HDL
1 module block1 (a, b, c, d); ① 端口定义
2 input a, b, c; // ① 端口定义 ② I/O 说明
3 output d; // ② I/O 说明
4 wire x; // ③ 信号类型声明 ③ 信号类型声明
5 assign d = a | x; // ④ 功能描述 ④ 功能描述
6 assign x = b & ~c;
7 endmodule
① 端口定义 列出模块的所有端口名称
② I/O 说明 声明每个端口的方向:input / output / inout
③ 信号类型声明 声明内部信号类型:wire、reg 等
④ 功能描述 用 assign、always 或门例化描述逻辑
典型例子#
例 1:8 位全加器#
module adder8 (cout, sum, a, b, cin);
output cout;
output [7:0] sum;
input [7:0] a, b;
input cin;
assign {cout, sum} = a + b + cin;
endmoduleverilog
{cout, sum}是位拼接,将进位与和拼成 9 位结果。
例 2:8 位计数器#
module counter8 (out, data, load, clk);
output [7:0] out;
input [7:0] data;
input load, clk;
reg [7:0] out;
always @(posedge clk) begin
if (load)
out <= data; // 同步预置数据
else
out <= out + 1; // 加 1 计数
end
endmoduleverilog例 3:三态驱动器(门级)#
module trist2 (out, in, enable);
output out;
input in, enable;
bufif1 mybuf (out, in, enable); // 调用内置门元件
endmoduleverilog bufif1 真值表 三态缓冲器:enable=0 时输出高阻 z
| in | enable | out |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
注:enable=0 时实际输出为高阻态
z(表中以 0 表示,仿真中为 z)
例 4:三态驱动器(模块调用)#
// 顶层模块
module trist1 (out, in, enable);
output out;
input in, enable;
mytri tri_inst (out, in, enable); // 调用子模块
endmodule
// 子模块
module mytri (out, in, enable);
output out;
input in, enable;
assign out = enable ? in : 'bz; // enable=1 输出 in,否则高阻
endmoduleverilog关键词与标识符#
关键词(小写字母):事先定义好的确认符,用来组织语言结构或定义内置门元件。
例如:always、assign、begin、case、if、input、output、wire
标识符规则:
- 可由字母、数字、下划线和
$构成 - 第一个字符必须是字母或下划线(不能是数字或
$) - 区分大小写,不能与关键词同名
| 合法标识符 | 非法标识符 |
|---|---|
A_99_Z | 123a(以数字开头) |
Reset | $data(以 $ 开头) |
_54MHz_Clock$ | module(与关键词同名) |
Module | 7seg.v(含非法字符) |
逻辑功能的三种定义方式#
| 方式 | 语句 | 适用场景 |
|---|---|---|
| 连续赋值 | assign x = b & ~c; | 组合逻辑 |
| 门元件例化 | and myand3(f, a, b, c); | 结构化描述 |
| always 块 | always @(posedge clk) begin ... end | 时序 / 组合逻辑 |