【问题标题】:Vhdl compare a std_logic_vectorVhdl 比较 std_logic_vector
【发布时间】:2021-04-12 19:02:31
【问题描述】:
library IEEE;
use IEEE.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;

entity converter is
port(sign_mag : in std_logic_vector(3 downto 0);
        twos_com : out std_logic_vector(3 downto 0));
end;

architecture converter_arch of converter is

begin
if to_integer(signed(sign_mag)) > 0 then
        twos_com <= sign_mag;
else
        twos_com<= "0000";
end if;
end converter_arch;


我收到错误非法并发语句。无法找出比较向量的 msb 的方法。我也尝试过类似的东西(如果 sign_mag(3) = 0),但我也得到了同样的错误信息。

【问题讨论】:

  • If 语句是顺序语句,可以出现在流程语句或子程序(过程、函数)中。两个简单的修复。将 if 语句放在流程语句中或使用并发条件赋值语句(例如twos_com &lt;= sign_mag when to_integer(signed(sign_mag)) &gt; 0 else "0000";),这将被详细说明为带有 if 语句的等效流程。条件signed(sign_mag) &gt; 0 应该足够了,无需使用包 numeric_std 转换为整数。此处不要使用 Synopsys 包 ieee.std_logic_unsigned 中的任何声明。

标签: vhdl


【解决方案1】:

如评论中所述,if-then-else 是一个顺序语句,它必须在进程(或函数或过程 = 子程序)内。因此,您缺少架构中的流程块。

正如您所说,您可以像您建议的那样仅比较 MSB if sign_mag(3) = '0'(注意单引号,因为您正在比较 std_logic 类型)。这消除了使用 IEEE 库中除 std_logic_1164 之外的其他包的需要。

architecture converter_arch of converter is

begin
  process(sign_mag)
  begin

    if sign_mag(3) = '0' then
        twos_com <= sign_mag;
    else
        twos_com <= "0000";
    end if;

  end process;

end converter_arch;

在 VHDL 中使用条件语句的另一种可能性是使用 case 语句(也在进程内部)。但是,这里比较的是sign_mag(3) 的值,因此您可能无法像使用 if-then-else 结构那样表达条件。

case sign_mag(3) is
  when '0' =>
    twos_com <= sign_mag;
  when others =>
    twos_com <= "0000";
end case;

...或没有进程信号可以用带有条件信号分配的when--else结构分配,这是一个并发语句:

twos_com <= sign_mag when sign_mag(3) = '0' else (others => '0');

【讨论】:

    【解决方案2】:

    也许这有帮助:

    library ieee;
    USE ieee.std_logic_1164.all;
    use ieee.numeric_std.all;
    
    entity Converter is
      generic(
                num_length : integer := 4
             ); 
        port(
                number  : in std_logic_vector((num_length - 1) downto 0);
                result  : out std_logic_vector((num_length - 1) downto 0));
    end Converter ;
    
    architecture Beh of Converter is
        signal temp : std_logic_vector((num_length - 1) downto 0) := (others => '0');
     begin
        
        result <= temp;
        
        process(number) is
        begin
            if signed(number) > 0 then
                temp <= number;
            else
                temp <= (others => '0');
            end if;
        end process;
    end Beh;
    
    

    实际上我无法批准给定的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-14
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多