VHDL learning

Posted on Sat, May 31, 2025 VHDL

https://miyo.github.io/learning_fpga/docs/book01/languages/

语法注意点

变量可以具有字母数字名称:它们必须以字母或 “_” 开头,并且不能以 “_” 结尾。 Verilog HDL 区分大小写。

常常使用的数值 包括 0 1 z / Z(高阻关断) x X(0/1)

; 非常重要

library and package

LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_1164.ALL;

entity

entity 定义电路的轮廓,例如连接到外部的 input/output 端口的声明

architecture

architecture 定义电路的内部,例如要使用的函数的定义和处理内容

VHDL 中可以描述的常数的例子

Untitled

说明例子
1 条信号线所取的信号值'1''0''Z''X'
多条信号线所取的信号值"111""0100"X"10"
整数3281000
布尔值truefalse

VHDL 中的变量都有类型。

定义了很多种类型,也可以定义自己的类型。常用的五种类型。

std_logicstd_logic_vector 对应硬件信号类型,

signedunsigned 用于表示可以进行加减运算的值,

integer 可以表示一般数值

Untitled

说明 类型名
std_logic1 位信号线
std_logic_vector(n-1 downto 0)n 位信号线
unsigned(n-1 downto 0)n 位无符号的算术可操作值
signed(n-1 downto 0)n 位有符号的算术可操作值
integer n to m从 n 到 m 的整数

1-bitの信号

std_logic 是 VHDL 的基本 1 位信号类型。 '0''1' ,以及高阻态的 'Z' ,不定值的 'X' 都可以作为其值。这些值直接对应硬件。

n 位信号

std_logic_vector(n downto 0)std_logicstd_logi_vector 并排组成的 n 位信号线类型,称为 n 位 std_logi_vector 类型。

std_logic_vector 类型的变量 a 中的元素可以取出 a(3)a(4 downto 2) 等。前者是 std_logic 类型,后者是 3 位的 std_logic_vector 类型。

downto 表示 std_logic 的序列,从 MSB 降序编号。也就是说,对于 std_logic_vector(n-1 downto 0) 的位序列,MSB 是 std_logic_vector(n-1) ,LSB 是 std_logic_vector(0)

也可以使用 to 逆序排列,这时可以像 std_logic_vector(n to 0) 这样表示。

模块的外部描述 — entity

entity 代表模块的外框,通过模块名称和输入输出信号来定义。例如,以下描述相当于定义了一个名为 test ,输入信号为 pClk 和 pReset,输出信号为 Q 的模块的外框。

entity test is
 port (
   pClk   : in std_logic;
   Q      : out std_logic;
   pReset : in std_logic -- 在最后一个之后不加
 );
end entity;

定义端口

端口是硬件模块的输入输出。在 entityport(~); 中,通过指定信号的方向和类型来定义。

信号方向有 in (输入)、 out (输出)和 inout (输入输出)三种。

每个端口通过“名称 : 方向 类型”来定义。例如

pClk : in std_logic
-- or together definate
pR, pG, pB : in std_logic

这样的描述相当于定义了 pClkstd_logic 输入端口( in )。相同方向和类型的多个端口名也可以用“,”来并列定义。例如,

可以将 3 个输入信号 pRpGpB 一起定义。

定义常量

可以在 entity 中定义模块内使用的常量

entity test is
  generic (
    width  : integer := 640;
    height : integer := 480
  );
  port (
    pClk : in std_logic;
    Q : out std_logic;
    pReset : in std_logic
  );
end test;

这个以 width 的名称定义了一个值为 640 的 integer 类型,即整数的常量。这个常量可以在 entity 内部,以及内部处理描述的 architecture 中使用

内部处理描述 — architecture  

在 VHDL 中, architecture 用于描述模块的处理内容。描述的基本流程如下:

architecture RTL of test is
  (在这里写变量的定义等。)
begin
  (在这里描述处理内容。)
end RTL;

上述是用于描述名为 test 的模块内容的块。

变量的定义

变量的定义

signal 名前 ::= 初期値;
-- second
signal counter : std_logic_vector(9 downto 0);
--third
signal counter : std_logic_vector(width-1 downto 0);

例如,10 位的数组可以这样定义。(—> second)

此外,可以使用 generic 定义的值 width 来定义宽度为 width 的 std_logic_vector 型的信号。这里 width−1 的值在综合时确定.

运算符和运算