0

我写了这个简单的过程。它应该累加 ((ba)/n)*yi 项,其中 yi 是每个时钟周期更新的输入,然后输出总和的结果。

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all; 
entity Integrator is
    Port ( a : in STD_LOGIC_VECTOR(7 downto 0);
           b : in STD_LOGIC_VECTOR(7 downto 0);
           n : in STD_LOGIC_VECTOR(7 downto 0);
           yi : in STD_LOGIC_VECTOR (15 downto 0);
           start : in std_logic;
           clk : in std_logic;
           output : out signed (15 downto 0);
           done : out STD_LOGIC);
end Integrator;

architecture Integrator_arch of Integrator is
signal do : std_logic;
signal i : unsigned(7 downto 0);
signal tmp1, tmp2, tmp3, res : signed(15 downto 0);
begin
    process(clk)
    begin
        if(clk'event and clk='1') then
            if(start='1') then
                do <= '1';
                i <= (others=>'0');
                done <= '0';
                res <= (others=>'0');
            elsif(do='1' and done='0') then   
                if(i=unsigned(n)) then
                    output <= res;
                    do <= '0';
                    done <= '1';
                else                    
                    tmp1 <= resize(signed(b)-signed(a),16);
                    tmp2 <= resize(tmp1/signed(n),16);
                    tmp3 <= resize(tmp2*signed(yi),16);
                    res <= res + tmp3;
                    i <= i+1;
                end if;
            end if;  
        end if;         
    end process;
end Integrator_arch;

这是测试台:

architecture Behavioral of Integrator_Testbench is
signal start : std_logic;
signal clk, done : std_logic;
signal yi : std_logic_vector(15 downto 0);
signal O : signed(15 downto 0);
begin
    uut: entity work.integrator(integrator_arch)
            port map(a=>"11111110", b=>"00000010", n=>"00000100", yi=>yi
                ,start=>start, clk=>clk, done=>done, output=>O);
    process
    begin
        clk<='0';
        start<='1';
        wait for 200ns;
        
        clk<='1';
        wait for 200ns;
        
        start<='0';
        clk<='0';
        yi<=x"0003";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0003";
        wait for 200ns;
        
        clk<='0';
        yi<=x"0001";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0001";
        wait for 200ns;
        
        clk<='0';
        yi<=x"0000";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0000";
        wait for 200ns;
        
        clk<='0';
        yi<=x"0002";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0002";
        wait for 200ns;
        
        clk<='0';
        yi<=x"0000";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0000";
        wait for 200ns;
        
        clk<='0';
        yi<=x"0000";
        wait for 200ns;
        
        clk<='1';
        yi<=x"0000";
        wait for 200ns;
    end process;
end Behavioral;

但问题是,当我运行行为模拟时,输出总是报告为未知(红色 X)。更奇怪的是,它在调试会话中运行得非常好,并且通过运行逐行调试会话,我能够得到正确的输出,即 x"0006"!!!

行为模拟结果

经过两个小时的尝试寻找问题后,我终于决定问。特别是因为它运行得很好,并且在逐行调试中输出了正确的结果,所以我不知道为什么模拟一直在起作用。

非常感谢您的时间。

4

2 回答 2

0

通过更多的反复试验找到了我自己的答案

似乎一行numeric_std表达式的结果需要一个时钟周期才能稳定下来,所以当我试图通过编写使其更具可读性时

tmp1 <= resize(signed(b)-signed(a),16);
tmp2 <= resize(tmp1/signed(n),16);
tmp3 <= resize(tmp2*signed(yi),16);
res <= res + tmp3;

稳定下来需要 4 个时钟周期,在这 4 个时钟周期完成之前,端口设置为未知。所以我将所有这些行缩短为一行

res <= res + resize((resize(b-a,16)/signed(n))*yi,16);

现在结果在 1 个时钟周期内为我准备好了,就像它应该的那样。

于 2020-11-20T14:45:39.703 回答
0

你的开始信号应该被断言更长的时间。在仿真开始时 tmp1、tmp2、tmp3 未初始化,需要 3 个时钟周期才能获得正确计算的 tmp3 值(因为您在时钟过程中使用 <= 信号分配)。如果在模拟开始时您的启动信号仅被断言一个周期,则在其取消断言之后的周期中,res 会与仍未初始化的 tmp3 值一起累加,因此会给出未知的 res。

解决方案:

  • 在开始时,断言再开始 3 个周期,
  • 或在声明时初始化tmp1、tmp2、tmp3
  • 或在 start = 1 时将它们与 res 一起初始化

您可以更改 tmp1、tmp2、tmp3 变量的代码。时钟过程中的信号表现得像一个管道。只是它是行为代码还是可综合代码,是否满足时序约束的问题。VHDL是一种描述语言;从功能的角度来看,没有一个好的答案。

于 2020-11-20T20:57:36.980 回答