如何在测试台VHDL语言中使用for循环来完成多个输入组合? [英] How to go through multiple input combinations with a for loop in a testbench VHDL?

查看:0
本文介绍了如何在测试台VHDL语言中使用for循环来完成多个输入组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VHDL语言的新手,我正在为XNOR门编写一个测试台。简单的解决方案是手动检查两个输入的每个组合,但如果输入更多,这将花费太长时间。如何在VHDL语言中将其编写为for循环?

process
begin
p0 <= '1';
p1 <= '0';
wait for 1 ns;
if (pout = '1') then
    error <= '1';
end if;
wait for 200 ns;
p0 <= '1';
p1 <= '1';
wait for 1 ns;
if (pout = '0') then
    error <= '1';
end if;
wait for 200 ns;
p0 <= '0';
p1 <= '1';
wait for 1 ns;
if (pout = '1') then
    error <= '1';
end if;
wait for 200 ns;
p0 <= '0';
p1 <= '0';
wait for 1 ns;
if (pout = '0') then
    error <= '1';
end if;
wait for 200 ns;
end process;

推荐答案

ifp0p1是被测设备的输入,且它们的基类型与unsigned类型的元素类型兼容:

library ieee;
use ieee.std_logic_1164.all;

entity xnor2 is
    port (
        p0:     in  std_logic;
        p1:     in  std_logic;
        pout:   out std_logic
    );
end entity;

architecture foo of xnor2 is
begin
    pout <= not (p0 xor p1);
end architecture;

library ieee;
use ieee.std_logic_1164.all;

entity inputs is
end entity;

architecture foo of inputs is
    signal p0, p1, pout:    std_logic;
    signal error:           std_logic := '0';
begin
DUT:
    entity work.xnor2
        port map (
            p0 => p0,
            p1 => p1,
            pout => pout
        );

    process
        use ieee.numeric_std.all;  -- for example, if not already visible
        variable elements: unsigned (1 downto 0);
    begin
        elements := (others => '0');
        for i in 0 to 2 ** elements'length  - 1 loop
            
            p0 <= elements(0);
            p1 <= elements(1);
            wait for 1 ns;
            report LF & "i = " & integer'image(i) &
                LF & HT & "p0 = " & std_ulogic'image(p0) & 
                         " p1 = " & std_ulogic'image(p1) &
                         " error = " & std_ulogic'image(error);
            if pout = '0' then
                error <= '1';
            end if;
            wait for 200 ns;
            elements := elements + 1;
        end loop;
        wait;
    end process;
end architecture;

哪些报告:

ghdl -r inputs
inputs.vhdl:45:13:@1ns:(report note):
i = 0
    p0 = '0' p1 = '0' error = '0'
inputs.vhdl:45:13:@202ns:(report note):
i = 1
    p0 = '1' p1 = '0' error = '0'
inputs.vhdl:45:13:@403ns:(report note):
i = 2
    p0 = '0' p1 = '1' error = '1'
inputs.vhdl:45:13:@604ns:(report note):
i = 3
    p0 = '1' p1 = '1' error = '1'

我们也看到error的地方没有明显的意义。

如果不在问题中提供最小、完整和可验证的示例,答案可能会有一个或多个错误,并且未来的读者不能轻松验证解决方案。

这里的想法是使用具有与输入一样多的位(元素)的二进制表示计数器,并在每次循环迭代中将位(元素)的值分配给各自的输入。

二进制值也可以直接从循环参数的整数值提供。请参阅How to easily group and drive signals in VHDL testbench,它也使用聚合赋值:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity agg_assign is
end entity;

architecture foo of agg_assign is
    signal A, B, C: std_logic;
begin
    process
    begin
        wait for 10 ns;
        for i in 0 to 7 loop
            (A, B, C) <= std_logic_vector(to_unsigned(i, 3));
            wait for 10 ns;
        end loop;
        wait;
    end process;
end architecture;

您还可以创建一个记录子类型来混合元素和数组赋值:

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

entity aggr_rec_assign is
end entity;

architecture foo of aggr_rec_assign is
    signal A, B, C: std_logic;
    signal D:       std_logic_vector (2 downto 0);
    
    function to_string (inp: std_logic_vector) return string is
        variable image_str: string (1 to inp'length);
        alias input_str:  std_logic_vector (1 to inp'length) is inp;
    begin
        for i in input_str'range loop
            image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
        end loop;
        return image_str;
    end function;
begin
    process
        type inputs_rec is
        record
                A:  std_logic;
                B:  std_logic;
                C:  std_logic;
                D:  std_logic_vector (2 downto 0);
            end record;
            variable elements:  unsigned (5 downto 0);
    begin
        wait for 10 ns;
        for i in 0 to 2 ** elements'length - 1 loop
            elements := to_unsigned(i, elements'length);
            (A, B, C, D) <= 
                inputs_rec'(
                    elements(5),
                    elements(4),
                    elements(3),
                    std_logic_vector(elements(2 downto 0))
                );
            wait for 10 ns;
            report LF & HT & "i =  "& integer'image(i) & " (A, B, C, D) = " & 
                std_ulogic'image(A) & " " &
                std_ulogic'image(B) & " " &
                std_ulogic'image(C) & " " &
                to_string(D);
        end loop;
        wait;
    end process;
end architecture;

在这两种情况下,聚合赋值将是选择从二进制值提取输入的顺序的位置。

这篇关于如何在测试台VHDL语言中使用for循环来完成多个输入组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆