VHDL入门:构建你的第一个与门
VHDL,即超高速集成电路硬件描述语言,是设计FPGA和ASIC的两大主流硬件描述语言之一。虽较软件语言显冗长,但语法精确,旨在极高抽象层级描述数字逻辑。
深入VHDL代码前,了解其使用语境有助。FPGA(现场可编程门阵列)与ASIC(专用集成电路)是现代数字系统基石,从消费电子到航空航天应用。VHDL与Verilog提供文本方式描述电路行为与结构。
本教程将演练用VHDL创建简单与门。与门接受两个二进制输入,仅当两输入均高时输出高。
定义信号与操作
VHDL核心概念是信号,代表可持值的导线或存储元件。信号可用std_logic类型声明,这是二进制信号最常用类型。声明与门操作最小示例:
signal and_gate : std_logic; and_gate <= input_1 and input_2;
第一行声明名为and_gate的信号。第二行将input_1与input_2逻辑与结果赋给该信号。<=是VHDL信号赋值算子,and是逻辑与保留运算符。
创建实体
所有VHDL文件暴露顶层实体定义设计外部接口。与门实体列出两输入一输出端口:
entity example_and is port ( input_1 : in std_logic; input_2 : in std_logic; and_result : out std_logic ); end example_and;
每端口指定方向(in或out)与类型(std_logic)。
定义架构
架构包含实体内部行为。可声明额外内部信号与驱动输出的组合逻辑。典型与门架构如下:
architecture rtl of example_and is signal and_gate : std_logic; begin and_gate <= input_1 and input_2; and_result <= and_gate; end rtl;
架构中,内部信号声明在is与begin之间。驱动信号的可执行语句在begin与end之间。
包含标准库
每个VHDL文件顶部必须引用定义std_logic类型的IEEE标准库:
library ieee; use ieee.std_logic_1164.all;
这两行引入文件其余部分所需类型与运算符。
完整示例
下方是双输入与门完整VHDL源码。复制到VHDL编辑器仿真或综合验证行为。
library ieee; use ieee.std_logic_1164.all; entity example_and is port ( input_1 : in std_logic; input_2 : in std_logic; and_result : out std_logic ); end example_and; architecture rtl of example_and is signal and_gate : std_logic; begin and_gate <= input_1 and input_2; and_result <= and_gate; end rtl;
虽代码较常规编程语言显冗长,但此显式性对可靠硬件综合仿真至关重要。随经验积累,您会发现减少重复同时保持清晰的技巧与抽象。
有此基础,您可探索更复杂电路、学习并发语句、将VHDL设计集成至更大FPGA或ASIC项目。
VHDL