管道的VHDL实现计算Y = A * B + C

时间:2017-07-05 20:22:26

标签: vhdl

我有VHDL代码,它分三个阶段实现Y=A*B+C管道,如下所示enter image description here

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity pipeline is
   Port (A : in std_logic_vector (15 downto 0);
      B : in std_logic_vector (15 downto 0);
      C : in std_logic_vector (15 downto 0);
      Y : out std_logic_vector (32 downto 0);
      CLK : in std_logic;
      RESET : in std_logic);
end pipeline;

architecture Behavioral3 of pipeline is
   signal OUT_A, OUT_B, OUT_C : std_logic_vector (15 downto 0);
   signal OUT_MUL_2, OUT_C_2 : std_logic_vector (31 downto 0);

   begin
      Stage1: process(CLK, RESET)
         begin
            if RESET ='0' then
               OUT_A <= x"0000";
               OUT_B <= x"0000";
               OUT_C <= x"0000";
            elsif CLK ='1' and CLK'event then
               OUT_A <= A;
               OUT_B <= B;
               OUT_C <= C;
            end if;
       end process;

       Stage2: process(CLK, RESET)
          begin
             if RESET = '0' then
                OUT_MUL_2 <= x"00000000";
                OUT_C_2 <= x"00000000";
             elsif CLK ='1' and CLK'event then
                OUT_MUL_2 <= OUT_A * OUT_B ;
                OUT_C_2 <= x"0000" & OUT_C ;
             end if;
       end process;

       Stage3: process(CLK, RESET)
          begin
             if RESET ='0' then
                Y <= (others => ‘0’);
             elsif CLK ='1' and CLK'event then
                Y <= (‘0’ & OUT_MUL_2) + (‘0’ & OUT_C_2);
             end if;
       end process;
end Behavioral3;

问题: 为什么在这个过程中&#34; Stage3&#34;代码是:

  

Y&lt; =('0'&amp; OUT_MUL_2)+('0'&amp; OUT_C_2);

而不是:

  

Y&lt; = OUT_MUL_2 + OUT_C_2;

将写入的实现不会产生OUT信号Y始终为0(低),因为任何&amp; 0始终为0而0 + 0再次为0?

2 个答案:

答案 0 :(得分:2)

有一点需要考虑。加法器可以是已签名的加法器。为避免误解32位的正数被解释为负数,您可以添加“0”和“0”。之前要确保添加将考虑2个正数。

答案 1 :(得分:1)

Y是添加两个32位字的结果。这意味着Y必须在33位上才能获得进位。所以你添加一个&#39; 0&#39;作为信号的MSB,为了计算这个信号,可以正确地计算结果并适合结果大小。