在VHDL中,如何检测二进制输入是否可被3或4整除?

时间:2016-02-24 18:23:56

标签: vhdl fpga electronics

我的代码将输入作为数字的二进制表示,介于0到15之间(包括两者)。

我的目标是检查输入是否可以被3或4整除,如果是:输出1并使用 ONLY NOR GATES 实现此功能。

我使用了K-Map方法并导出了函数公式。以下是: ABCD是我的输入,4位,

F =(A'+ C'+ D)(A'+ B + C')(A + C + D')(A + B + C'+ D)(A + B'+ C'+ d ')(A' + B '+ C + d')

我已按如下方式实施我的公式:

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

entity kod is
   Port ( X : in  STD_LOGIC_VECTOR (3 downto 0);
          O : out  STD_LOGIC);
end kod;

architecture Behavioral of kod is

    -- Following are intermediate signals
    signal CCC : STD_LOGIC;
    signal MMM : STD_LOGIC;
    signal EEE : STD_LOGIC;
    signal RRR : STD_LOGIC;
    signal TTT : STD_LOGIC;
    signal DDD : STD_LOGIC;

begin

    CCC <= NOT ((NOT X(3)) OR (NOT X(1)) OR X(0)) ;
    MMM <= NOT ((NOT X(3)) OR X(2) OR (NOT X(1))) ;
    EEE <= NOT (X(3) OR X(1) OR (NOT X(0))) ;
    RRR <= NOT (X(3) OR X(2) OR (NOT X(1)) OR X(0));
    TTT <= NOT (X(3) OR (NOT X(2)) OR (NOT X(1)) OR (NOT X(0)));
    DDD <= NOT ((NOT X(3)) OR (NOT X(2)) OR X(1) OR (NOT X(0)));

    O <= NOT (CCC OR MMM OR EEE OR RRR OR TTT OR DDD) ;


end Behavioral;

当我模拟我的代码时,我发现我的所有信号都未初始化。当我尝试将输入初始化为:

entity kod is
   Port ( X : in  STD_LOGIC_VECTOR (3 downto 0) := "0000";
          O : out  STD_LOGIC);
end kod;

我只输出1作为输出。 我的错在哪里,我该如何解决?

2 个答案:

答案 0 :(得分:1)

你的等式接受&#34; 0000&#34;:

kod_tb.png

我添加了一个术语来排除&#34; 0000&#34;以及一个小测试平台:

library ieee;
use ieee.std_logic_1164.all;

entity kod is  -- is x divisble by 3 or 4?
    port (
        x:  in  std_logic_vector (3 downto 0);
        o:  out  std_logic
    );
end entity kod;

architecture behavioral of kod is

    -- following are intermediate signals
    signal ccc:    std_logic;
    signal mmm:    std_logic;
    signal eee:    std_logic;
    signal rrr:    std_logic;
    signal ttt:    std_logic;
    signal ddd:    std_logic;

    signal zero:   std_logic;  -- added

begin

    ccc  <= not (not x(3) or             not x(1) or     x(0));
    mmm  <= not (not x(3) or     x(2) or not x(1)            );
    eee  <= not (    x(3) or                 x(1) or not x(0));
    rrr  <= not (    x(3) or     x(2) or not x(1) or     x(0));
    ttt  <= not (    x(3) or not x(2) or not x(1) or not x(0));
    ddd  <= not (not x(3) or not x(2) or     x(1) or not x(0));

    zero <= not (    x(3) or     x(2) or     x(1) or     x(0)); -- added term

    o <= not (ccc or mmm or eee or rrr or ttt or ddd or zero); -- ddd);

end architecture behavioral;

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

entity kod_tb is
end entity;

architecture foo of kod_tb is
    signal x:   std_logic_vector (3 downto 0);
    signal o:   std_logic;
begin
DUT: 
    entity work.kod
        port map (
            x => x,
            o => o
        );
STIMULIS:
    process
    begin
        wait for 10 ns;
        for i in 0 to 15 loop
            x <= std_logic_vector(to_unsigned(i,4));
            wait for 10 ns;
        end loop;
        wait;
    end process;
end architecture;

(请原谅我删除多余的括号和格式以便于阅读)。

这给出了:

kod_tb_fixed.png

答案 1 :(得分:1)

如果直接模拟实体kod而未指定输入的默认值(如第一次尝试),那么输入X的值将为&#34; UUUU&#34; 。价值&#39; U&#39;是std_logic类型信号的默认值。类型std_logic是9个可能值的枚举,例如,&#39; 0&#39;对于(强)逻辑低和&#39; 1&#39;对于(强)逻辑高。价值&#39; U&#39;表示信号尚未初始化。要么它还没有被分配,要么就是&#39; U&#39;由于表达式而被分配。您可以在VHDL标准之一或VHDL工具附带的包std_logic的文件中查找std_logic_1164上的布尔运算符的thruth表。 (例如,它在Quartus工具链中命名为std_1164.vhd。)

您可以使用更简单的示例重现观察到的行为:

library ieee;
use ieee.std_logic_1164.all;

entity test1 is
  port (
    x : in std_logic;
    y : out std_logic);
end test1;

architecture rtl of test1 is
begin
  y <= x;
end rtl;

这是模拟输出:

simulation output

要删除未初始化的值,您必须为X指定另一个默认值。在您的示例中,您指定了&#34; 0000&#34;为您的4位输入向量。输出O不再是未定义的。由于方程式中的错误,这是错误的(&#39; 1&#39;)。您可以修改它,通过扩展最后一个等式将输出设置为&#39; 0&#39;当输入全为零时,如#34; user1155120&#34;。

所示