【问题标题】:BCD adder of 4-BCD-digit numbers in VHDLVHDL中4位BCD数字的BCD加法器
【发布时间】:2015-02-03 14:39:06
【问题描述】:

我正在尝试使用我找到的here 的 1 位 BCD 加法器的代码来实现两个 4 位数字(即 16 位)的 BCD 加法器。我将此代码用作基本模块,然后创建了一个顶级实体,该实体创建并连接了此基本加法器的 4 个实例。我还在 VHDL 中的不兼容类型之间进行了一些转换。我创建的第三个文件是我模拟以检查实现的测试台。所以,1 位 BCD 加法器是:

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

entity bcd_adder is
    port(
        a,b  : in  unsigned(3 downto 0); -- input numbers.
        carry_in : in std_logic;
        sum  : out  unsigned(3 downto 0); 
        carry : out std_logic  
    );
end bcd_adder;

architecture arch of bcd_adder is


begin

process(a,b)
variable sum_temp : unsigned(4 downto 0);
begin
    sum_temp := ('0' & a) + ('0' & b) + ("0000" & carry_in); 
    if(sum_temp > 9) then
        carry <= '1';
        sum <= resize((sum_temp + "00110"),4);
    else
        carry <= '0';
        sum <= sum_temp(3 downto 0);
    end if; 
end process;   

end arch;

具有四个这些加法器的顶级实体是:

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

entity TopAdder is
port(
    in1: in std_logic_vector(15 downto 0);
    in2: in std_logic_vector(15 downto 0);
    sum: out std_logic_vector(15 downto 0);
    carry: out std_logic);
end TopAdder;

architecture structural of TopAdder is

component bcd_adder is
    port(
        a,b  : in  unsigned(3 downto 0); -- input numbers.
        carry_in : in std_logic;
        sum  : out  unsigned(3 downto 0); 
        carry : out std_logic  
    );
end component;

signal carry1,carry2,carry3: std_logic;
signal in1_s,in2_s,sum_s: unsigned(15 downto 0);

begin
    in1_s <= unsigned(in1);
    in2_s <= unsigned(in2);
    sum <= std_logic_vector(sum_s);

    adder1: bcd_adder
    port map(in1_s(3 downto 0),in2_s(3 downto 0),'0',sum_s(3 downto 0),carry1);

    adder2: bcd_adder
    port map(in1_s(7 downto 4),in2_s(7 downto 4),carry1,sum_s(7 downto 4),carry2);

    adder3: bcd_adder
    port map(in1_s(11 downto 8),in2_s(11 downto 8),carry2,sum_s(11 downto 8),carry3);

    adder4: bcd_adder
    port map(in1_s(15 downto 12),in2_s(15 downto 12),carry3,sum_s(15 downto 12),carry);

end structural;

测试台是:

LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
ENTITY test1 IS
END test1;
ARCHITECTURE behavior OF test1 IS 
    COMPONENT TopAdder
    PORT(
         in1 : IN  std_logic_vector(15 downto 0);
         in2 : IN  std_logic_vector(15 downto 0);
         sum : OUT  std_logic_vector(15 downto 0);
         carry : OUT  std_logic
        );
    END COMPONENT;
   signal in1 : std_logic_vector(15 downto 0) := (others => '0');
   signal in2 : std_logic_vector(15 downto 0) := (others => '0');
   signal sum : std_logic_vector(15 downto 0);
   signal carry : std_logic;

BEGIN
   uut: TopAdder PORT MAP (
          in1 => in1,
          in2 => in2,
          sum => sum,
          carry => carry
        );

   stim_proc: process
   begin        
      wait for 100 ns;  

        in1<="0000000000000001";
        in2<="0000000000000010";
      wait for 100 ns;

        in1<="0000000000001001";
        in2<="0000000000000001";
      wait; 
   end process;
END;

模拟显示如下:

问题在于“大”加法器和“小”加法器不能用于创建必须发送到下一个“小”加法器的进位的加法。结果,测试台中的第一个加法 1+2=3 是正确的,但第二个加法 9+1=0 是错误的。我尝试了一些其他的添加,但是那些产生进位的在模拟中是错误的。这里有什么问题?

澄清一下:图中,carry_in,sum[3:0],carry 信号的 4 次重复表示从最右边到每个小加法器的进位、求和、进位模拟图中最左边的加法器从上到下。

【问题讨论】:

  • 除了 fru1tbat 的回答指出 carry_in 不在 bcd_adder 中未命名进程的敏感度列表中之外,您的 test1 测试台代码与您的波形图像不匹配。您可以在最后的等待语句之前添加wait for 100 ns; 语句,以允许显示最后一次加法 9+1。否则模拟会在黄色光标所在的位置停止。
  • 这可能不是很重要,但您所说的并不正确。在我的例子中,wait 语句使模拟总共保持了 1000 纳秒。 Xilinx 中必须有一个选项可以设置该值,而不管测试台如何。如果我使用wait for 100 ns 而不是wait,总时间将是 300ns,这也足以看到结果。
  • 当 TIME 达到 TIME'HIGH (IEEE Std 1076-2008, 14.7.5.3 a) 时模拟结束,当没有安排更多事件时 TIME 提前到 TIME'HIGH (14.7.5.1 第 2 段, 一种) )。如果您的工具添加了 800 ns,则它添加了一个不在您的代码中的进程,并且最终等待并不暗示它。将最终等待更改为等待 100 ns;执行将在 100 ns 后恢复(14.7.5.1 第 2 段,c))“进程恢复的下一次”)。不保证其他 VHDL 工具具有创造性的实现。 (我正在检查 fru1tbat 的答案,缺乏演示,我对问题和答案都投了赞成票)。

标签: signals add vhdl bcd ieee


【解决方案1】:

您在bcd_adder 中的进程敏感度列表仅列出了ab,但carry_in 也是一个重要的输入。在您的第二组输入值上,您的 adder2 实例中的过程仅在最初分配输入时触发,并且在此模拟时间,该实例的 carry_in 的值仍然是 0,因为增量延迟通过adder1 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多