如何创建自检测试平台
自检测试台是一个 VHDL 程序,它可以验证被测设备 (DUT) 的正确性,而无需操作员手动检查输出。自检测试台完全独立运行,最后打印“OK”或“Failed”消息。
每个 VHDL 模块都应该有一个相关的自检测试平台。能够随时验证所有模块是否具有预期行为非常重要。例如,当您对 DUT、子模块或接口模块进行更改时。我们都知道东西会坏掉,而解决这些问题的最佳工具就是自检测试台。
被测设备
让我们直接进入并创建一个自检测试平台的示例。首先,我们需要测试的东西,一个 DUT。为此,我在下面的代码中创建了模块。这是一个二进制到格雷码的转换器。
library ieee; use ieee.std_logic_1164.all; entity gray_converter is port ( bin : in std_logic_vector; gray : out std_logic_vector ); end gray_converter; architecture rtl of gray_converter is begin process(bin) is begin gray(gray'high) <= bin(bin'high); for i in bin'high - 1 downto bin'low loop gray(i) <= bin(i + 1) xor bin(i); end loop; end process; end architecture;
格雷码是一种替代数字编码方案,不同于常规的二进制编码。格雷码的主要属性和用途是在相邻数值之间计数时只改变一位。
十进制 | 二进制 | 灰色 |
---|---|---|
0 | 0000 | 0000 |
1 | 0001 | 0001 |
2 | 0010 | 0011 |
3 | 0011 | 0010 | 4 | 0100 | 0110 | 5 | 0101 | 0111 | 6 | 0110 | 0101 | 7 | 0111 | 0100 | 8 | 1000 | 1100 | 9 | 1001 | 1101 | 10 | 1010 | 1111 | 11 | 1011 | 1110 | 12 | 1100 | 1010 | 13 | 1101 | 1011 | 14 | 1110 | 1001 | 15 | 1111 | 1000 |
上表显示了格雷码与二进制码的区别。
测试台
我们将首先创建基本测试平台并在其中实例化 DUT。下面的代码显示了 DUT 实例化和所有必要导入的测试台文件。
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; use std.env.finish; entity gray_converter_tb is end gray_converter_tb; architecture sim of gray_converter_tb is signal bin : std_logic_vector(3 downto 0) := (others => '0'); signal gray : std_logic_vector(3 downto 0); begin DUT : entity work.gray_converter(rtl) port map ( bin => bin, gray => gray ); end architecture;
请注意,我们正在导入 std.env.finish
这需要 VHDL-2008。如果您尝试在 ModelSim 中编译测试台而不做任何更改,您将收到以下错误:
# ** Warning: gray_converter_tb.vhd(6): (vcom-1516) Package "STD.ENV" does not exist in this language version.
VHDL