【问题标题】:Simple adding don't work, timming issue简单的添加不起作用,计时问题
【发布时间】:2012-05-14 19:57:09
【问题描述】:

首先是这个简单的加法器实体:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_misc.all;
use ieee.numeric_std.all;

entity adder is
    Port ( a : in  STD_LOGIC_VECTOR(31 downto 0);
           b : in  STD_LOGIC_VECTOR(31 downto 0);
           alu_out : out  STD_LOGIC_VECTOR(31 downto 0) := (others => '0')
           );
end adder;

architecture Behavioral of adder is
  signal result_ext:STD_LOGIC_VECTOR(32 downto 0) := (others => '0');
  signal result:STD_LOGIC_VECTOR(31 downto 0)     := (others => '0');
begin

  process (a,b)
  begin
    result_ext <= std_logic_vector(signed('0' & a) + signed('0' & b));
    result     <= result_ext(result'left downto 0);

    alu_out <= result;
  end process;
end Behavioral;

和测试台:

-- TestBench Template 

  LIBRARY ieee;
  USE ieee.std_logic_1164.ALL;
  USE ieee.numeric_std.ALL;

  ENTITY testbench IS
  END testbench;

  ARCHITECTURE behavior OF testbench IS         

    signal a : STD_LOGIC_VECTOR(31 downto 0);
    signal b : STD_LOGIC_VECTOR(31 downto 0);
    signal alu_out : STD_LOGIC_VECTOR(31 downto 0);
  BEGIN
    ADDER : entity work.adder port map(
      a => a,
      b => b,
      alu_out => alu_out
    );



  --  Test Bench Statements
     tb : PROCESS
     BEGIN

        a <= B"0000_0000_0000_0000_0000_0000_0111_1010";
        b <= B"0000_0000_0000_0000_0000_0000_0000_1011";

        wait for 100 ns; -- wait until global set/reset completes

        report "alu_out = " & integer'image(to_integer(signed(alu_out)));

        wait; -- will wait forever
     END PROCESS tb;
  --  End Test Bench 

  END;

我得到报告输出:

Finished circuit initialization process.
at 100 ns: Note: alu_out = 0 (/testbench/).

如果我不初始化 result 信号,我会得到未定义。所以问题是我不明白 结果到最后。

我使用 iSimXilinx

另外,如果有人有一些关于 VHDL 的简短有效材料的良好链接,请随时发布。

【问题讨论】:

    标签: vhdl xilinx


    【解决方案1】:

    获取彼得的代码并:

    • 更新输入宽度的灵活性
    • numeric_std
    • 保留进位位

    给出这个:

    library IEEE;
    use IEEE.STD_LOGIC_1164.ALL;
    use ieee.numeric_std.all;
    
    entity adder is
        generic (width : integer := 32)
        Port ( a       : in  signed(width-1 downto 0);
               b       : in  signed(width-1 downto 0);
               alu_out : out signed(width downto 0) := (others => '0');
               );
    end adder;
    
    architecture synth of adder is
    begin
      process (a,b)
      begin
        alu_out <= resize(a,alu_out'length) + b;
      end process;
    end Behavioral;
    

    【讨论】:

    • 是的,这是更好的方法。我想向 XoR 展示他们的原始代码由于在需要变量的地方使用信号而无法工作的原因。
    • 好吧,我的 ALU 将有 S、V、Z、N 位输出,所以我不需要这样的东西。我只是放了一个失败的最简单的例子,所以我可以把它放在stackoverflow帖子中。我们也不应该添加 a(a'left) & a 而不是 '0' & a,因为那样我们会丢失符号。
    • @XoR:很好的收获——非常正确。实际上我应该已经使用了调整大小的功能,那么您不必考虑它。我会更新帖子。
    【解决方案2】:

    问题在于,当您希望它们在该过程循环中的即时值时,您正在使用 resultresult_ext 的信号。变量会立即更新,您可以在流程的当前周期中访问它们的值。

    试试这个,我认为它可以解决您遇到的问题:

    library IEEE;
    use IEEE.STD_LOGIC_1164.ALL;
    use ieee.std_logic_unsigned.all;
    use ieee.numeric_std.all;
    
    entity adder is
        Port ( a : in  STD_LOGIC_VECTOR(31 downto 0);
               b : in  STD_LOGIC_VECTOR(31 downto 0);
               alu_out : out  STD_LOGIC_VECTOR(31 downto 0) := (others => '0')
               );
    end adder;
    
    architecture Behavioral of adder is
    
    
    begin
    
      process (a,b)
        variable result_ext:STD_LOGIC_VECTOR(32 downto 0) := (others => '0');
        variable result:STD_LOGIC_VECTOR(31 downto 0);
      begin
        result_ext := std_logic_vector(signed('0' & a) + signed('0' & b));
        result     := result_ext(result'left downto 0);
    
        alu_out <= result;
      end process;
    end Behavioral;
    

    至于阅读材料,VHDL 食谱还不错:VHDL Cookbook

    【讨论】:

    • 只需使用 numeric_std 和带符号的数据类型。
    • 那行得通,我也得到一些迹象表明我的测试台应该有时钟,因此这不起作用,我会尝试它,如果它有效,我会更新帖子: )。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多