VHDL比较中Adder的实现

时间:2018-04-12 22:24:59

标签: vhdl

有人能解释一下为什么这个实现有效吗

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

entity Adder is
    port(A: in std_logic_vector(3 downto 0);
    B: in std_logic_vector(3 downto 0);
    SUM: out std_logic_vector(3 downto 0);
    CO: out std_logic);
end;

architecture DescriptionAdders of Adder is

signal temp:  std_logic_vector(4 downto 0);
signal temp1: std_logic_vector(4 downto 0);
signal temp2: std_logic_vector(4 downto 0);

begin

temp1 <= '0' & A;
temp2 <= '0' & B;
temp <= std_logic_vector(unsigned(temp1) + unsigned(temp2));

SUM <= temp(3 downto 0);
CO <= temp(4);

end;

虽然没有(当它开始运行时SUM是XX,然后它总是落后于实际结果的一步,但是temps更新很好)。

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

entity Adder is
    port(A: in std_logic_vector(3 downto 0);
    B: in std_logic_vector(3 downto 0);
    SUM: out std_logic_vector(3 downto 0);
    CO: out std_logic);
end;

architecture DescriptionAdders of Adder is

signal temp:  std_logic_vector(4 downto 0);
signal temp1: std_logic_vector(4 downto 0);
signal temp2: std_logic_vector(4 downto 0);

begin

process(A, B) is
begin
    temp1 <= '0' & A;
    temp2 <= '0' & B;
    temp <= std_logic_vector(unsigned(temp1) + unsigned(temp2));
end process;

SUM <= temp(3 downto 0);
CO <= temp(4);

end;

对不起,如果这个问题太容易了,我就是个傻瓜。

1 个答案:

答案 0 :(得分:2)

这个问题似乎是基于对VHDL仿真如何工作的常见误解。

process(A, B)

表示该活动将在AB的活动中触发。该过程中发生的事情是对其他对象的分配

temp1 <= '0' & A;
temp2 <= '0' & B;
temp <= std_logic_vector(unsigned(temp1) + unsigned(temp2));

这意味着三个活动预定,每个temp1temp2temp都有一个。但是VHDL的工作方式,实际的分配直到下一个增量周期 才会发生。在评估过程之后发生。因此,即使分配到temp1temp2的行位于temp的分配之前,它们的值尚未更改

在流程完成后考虑temp1temp2的值更改,将错过对temp的分配。 除非您重新进入流程,方法是将对象添加到敏感列表中。 E.g。

process(A, B, temp1, temp2) is
begin
    temp1 <= '0' & A;
    temp2 <= '0' & B;
    temp <= std_logic_vector(unsigned(temp1) + unsigned(temp2));
end process;

另一种解决方案是使用变量,这些变量可以在流程内部发生变化。但请注意,如果没有正确使用,变量可能会导致逻辑综合困难。这个例子可行:

process(A, B) is
    variable temp1, temp2 : std_logic_vector(4 downto 0);
begin
    temp1 := '0' & A;
    temp2 := '0' & B;
    temp <= std_logic_vector(unsigned(temp1) + unsigned(temp2));
end process;

但问题是:为什么你需要temp1temp2 。只需写下

process(A, B) is
begin
    temp <= std_logic_vector(unsigned('0' & A) + unsigned('0' & B));
end process;

或者更灵活一点

process(A, B) is
begin
    temp <= std_logic_vector(
        resize(unsigned(A), temp'length) +
        resize(unsigned(B), temp'length));
end process;

甚至使用integer类型(限制为32位!)

process(A, B) is
begin
    temp <= std_logic_vector(to_unsigned(
        to_integer(unsigned(A)) + to_integer(unsigned(B))
        , temp'length));
end process;