构建VHDL自检测试平台实战指南
自检测试平台是无需人工检查即可验证被测器件(DUT)正确性的VHDL程序。执行后自动报告OK或Failed状态,是可靠VHDL开发的必备工具。
每个VHDL模块都应有配套自检测试平台。确保任何变更——无论是DUT本身、子模块还是接口——不会静默引入回归。精心设计的测试平台是抵御隐蔽bug的第一道防线。
被测器件
我们用简单且具说明性的DUT:二进制转格雷码转换器。下面是完整RTL实现。
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;
格雷码是一种二进制编码方案,相邻值仅一位变化,降低串行通信和数字计数器的错误概率。下表演示4位值映射。
| 十进制 | 二进制 | 格雷码 |
|---|---|---|
| 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并声明必要信号的最小测试平台。注意包含std.env.finish,这是VHDL-2008特性,可干净终止仿真。
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); DUT : entity work.gray_converter(rtl) port map ( bin => bin, gray => gray ); end architecture;
在ModelSim等VHDL-2008兼容仿真器中编译此文件,可能需设置文件语言版本为2008。ModelSim中右键文件,选Properties → VHDL → Use 1076‑2008。
生成穷举输入
下一步产生所有可能4位值的完整扫描。穷举测试更佳,因它保证覆盖每个角落情况。
PROC_SEQUENCE : process begin -- Iterate over every 4‑bit value for i in 0 to 2**bin'length - 1 loop bin <= std_logic_vector(to_unsigned(i, bin'length)); wait for 10 ns; end loop; -- Wrap‑around to the initial state bin <= (others => '0'); wait for 10 ns; report "Test: OK"; finish; end process;
10ns延迟任意;任何正值均可,因DUT纯组合。最终finish语句优雅结束仿真。
验证DUT输出
若输出错误仅打印“Test: OK”毫无意义。为强制正确性,我们将DUT的格雷输出与格雷码基本特性比较:相邻输出必须恰好相差一位。
PROC_CHECKER : process variable prev : std_logic_vector(gray'range); variable count : integer; begin wait on bin; prev := gray; -- Allow combinational logic to resolve wait for 1 ns; count := 0; for i in gray'range loop if gray(i) /= prev(i) then count := count + 1; end if; end loop; assert count = 1 report integer'image(count) & " bits changed, should have been 1" severity failure; end process;
此进程监视bin变化,捕获前一个格雷输出,等待传播,计数不同位,断言计数恰为1。断言失败时仿真立即停止,防止出现“Test: OK”消息。
运行测试平台
ModelSim中启动仿真执行run -all。成功运行以此结束:
VSIM 1> run -all # ** Note: Test: OK # Time: 170 ns Iteration: 0 Instance: /gray_converter_tb
测试平台鲁棒性验证
为确认检查器能检测故障,暂在DUT引入错误——如强制gray(3)为零。
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; -- Emulate a stuck‑at‑zero fault gray(3) <= '0'; end process;
再次运行测试平台触发断言失败,显示确切变化位数:
VSIM 2> run -all # ** Failure: 0 bits changed, should have been 1 # Time: 81 ns Iteration: 0 Process: /gray_converter_tb/PROC_CHECKER File: gray_converter_tb.vhd # Break in Process PROC_CHECKER at ray_converter_tb.vhd line 61
自检测试平台入门
养成为每个VHDL模块编写自检测试平台的习惯。它们通过早期捕获回归节省时间,提供清晰的设计意图记录。测试平台虽无需可综合,但应清晰、可维护,并使用最新VHDL结构以提高可读性和效率。
深入学习可考虑进阶课程,涵盖从规格到FPGA原型的完整设计流程,强调严格验证实践。
2020年10月12日更新: VHDL和FPGA课程已完成。点击下方图片了解更多。
点击了解点阵VHDL和FPGA课程详情!
开放时间:
待定。
VHDL