VHDL中的串联运算符:比较数组元素并生成向量

VHDL中的串联运算符:比较数组元素并生成向量,vhdl,Vhdl,我想做的是: 我将数组中的几个元素与一个固定值进行比较,并尝试从中创建一个向量 下面是一段代码: architecture behav of main_ent is ... type f_array is array(0 to 8) of std_logic_vector(7 downto 0); signal ins_f_array: f_array; signal sel_sig_cmd : std_logic_vector(3 downto 0); ... process begin

我想做的是: 我将数组中的几个元素与一个固定值进行比较,并尝试从中创建一个向量

下面是一段代码:

architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process begin
sel_sig_cmd <= ((ins_f_array(4) = x"3A")&(ins_f_array(3)= x"3A")&(ins_f_array(2)= x"3A")&(ins_f_array(1)= x"3A"));

....
end process;
...

这应该给出sel_sig_cmd=1000或可能是1011等值,但这不起作用。除了这个代码,还有其他选择吗?这是因为VHDL中的=函数返回的是布尔值,而不是std_逻辑。 在VHDL'93中,除了手动设置每一位之外,没有一种整洁的方法可以做到这一点:

sel_sig_cmd(3) <= '1' when (ins_f_array(4) = x"3A") else '0'
sel_sig_cmd(2) <= '1' when (ins_f_array(3) = x"3A") else '0'
-- etc
但在VHDL 2008中,有关系运算符?=?/=etc,它们在比较时返回std_逻辑。因此,您的代码变成:

sel_sig_cmd <= (   (ins_f_array(4) ?= x"3A")
                 & (ins_f_array(3) ?= x"3A")
                 & (ins_f_array(2) ?= x"3A")
                 & (ins_f_array(1) ?= x"3A") );

这是因为VHDL中的=函数返回的是布尔值,而不是std_逻辑。 在VHDL'93中,除了手动设置每一位之外,没有一种整洁的方法可以做到这一点:

sel_sig_cmd(3) <= '1' when (ins_f_array(4) = x"3A") else '0'
sel_sig_cmd(2) <= '1' when (ins_f_array(3) = x"3A") else '0'
-- etc
但在VHDL 2008中,有关系运算符?=?/=etc,它们在比较时返回std_逻辑。因此,您的代码变成:

sel_sig_cmd <= (   (ins_f_array(4) ?= x"3A")
                 & (ins_f_array(3) ?= x"3A")
                 & (ins_f_array(2) ?= x"3A")
                 & (ins_f_array(1) ?= x"3A") );

来自Tricky的答案是一个很好的答案。但是,如果您希望在流程中实现它,则可以按如下方式重写流程:

architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if ((ins_f_array(4) = x"3A")&(ins_f_array(3)= x"3A")&
              (ins_f_array(2)= x"3A")&(ins_f_array(1)= x"3A")) then
sel_sig_cmd <= "XXXX" -- Enter your desired value
....
end process;
...
architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if (ins_f_array(4) = x"3A") then
sel_sig_cmd(3) <= "X" -- Enter your desired value
else
sel_sig_cmd(3) <= "X" -- Enter your desired value
end if;
-- Repeat for other bits
....
end process;
...
尽管这个过程很乏味,因为它必须涵盖if条件的所有16种可能性

另一种实现是对每个位使用if条件,如下所示:

architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if ((ins_f_array(4) = x"3A")&(ins_f_array(3)= x"3A")&
              (ins_f_array(2)= x"3A")&(ins_f_array(1)= x"3A")) then
sel_sig_cmd <= "XXXX" -- Enter your desired value
....
end process;
...
architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if (ins_f_array(4) = x"3A") then
sel_sig_cmd(3) <= "X" -- Enter your desired value
else
sel_sig_cmd(3) <= "X" -- Enter your desired value
end if;
-- Repeat for other bits
....
end process;
...

来自Tricky的答案是一个很好的答案。但是,如果您希望在流程中实现它,则可以按如下方式重写流程:

architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if ((ins_f_array(4) = x"3A")&(ins_f_array(3)= x"3A")&
              (ins_f_array(2)= x"3A")&(ins_f_array(1)= x"3A")) then
sel_sig_cmd <= "XXXX" -- Enter your desired value
....
end process;
...
architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if (ins_f_array(4) = x"3A") then
sel_sig_cmd(3) <= "X" -- Enter your desired value
else
sel_sig_cmd(3) <= "X" -- Enter your desired value
end if;
-- Repeat for other bits
....
end process;
...
尽管这个过程很乏味,因为它必须涵盖if条件的所有16种可能性

另一种实现是对每个位使用if条件,如下所示:

architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if ((ins_f_array(4) = x"3A")&(ins_f_array(3)= x"3A")&
              (ins_f_array(2)= x"3A")&(ins_f_array(1)= x"3A")) then
sel_sig_cmd <= "XXXX" -- Enter your desired value
....
end process;
...
architecture behav of main_ent is
... 
type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
signal ins_f_array: f_array;
signal sel_sig_cmd : std_logic_vector(3 downto 0); 
...
process(ins_f_array(4 downto 1)) begin
if (ins_f_array(4) = x"3A") then
sel_sig_cmd(3) <= "X" -- Enter your desired value
else
sel_sig_cmd(3) <= "X" -- Enter your desired value
end if;
-- Repeat for other bits
....
end process;
...

您可以重载=运算符:

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

entity tb is
end entity;

architecture behav of tb is

  function "=" (Left, Right: std_logic_vector) return std_logic is
  begin
    if (Left = Right) then
      return '1';
    else
      return '0';
    end if;
  end function "=";


  type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
  signal ins_f_array: f_array := (x"00",x"01",x"02",x"03",x"04",x"05",x"06",x"07",x"08");
  signal sel_sig_cmd : std_logic_vector(3 downto 0); 

  begin

    process (ins_f_array(1 to 4)) begin
      sel_sig_cmd <= ((ins_f_array(4) = x"3A")&(ins_f_array(3) = x"3A")&(ins_f_array(2) = x"3A")&(ins_f_array(1) = x"3A"));
    end process;

    process
    begin
      wait for 10 us;
      for i in 0 to 8 loop  
        ins_f_array(i) <= std_logic_vector(unsigned(ins_f_array(i)) + 1);
      end loop;
    end process;

end architecture;

您可以重载=运算符:

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

entity tb is
end entity;

architecture behav of tb is

  function "=" (Left, Right: std_logic_vector) return std_logic is
  begin
    if (Left = Right) then
      return '1';
    else
      return '0';
    end if;
  end function "=";


  type f_array is array(0 to 8) of std_logic_vector(7 downto 0);
  signal ins_f_array: f_array := (x"00",x"01",x"02",x"03",x"04",x"05",x"06",x"07",x"08");
  signal sel_sig_cmd : std_logic_vector(3 downto 0); 

  begin

    process (ins_f_array(1 to 4)) begin
      sel_sig_cmd <= ((ins_f_array(4) = x"3A")&(ins_f_array(3) = x"3A")&(ins_f_array(2) = x"3A")&(ins_f_array(1) = x"3A"));
    end process;

    process
    begin
      wait for 10 us;
      for i in 0 to 8 loop  
        ins_f_array(i) <= std_logic_vector(unsigned(ins_f_array(i)) + 1);
      end loop;
    end process;

end architecture;

另一方面,如果您是Xilinx用户,这些操作符在旧版本的Vivado中是不可用的。您需要2017.4或更新版本才能使其真正工作!我想到了几个与1993年兼容的方法。向_stdulogicinp添加了一个转换函数:boolean return std _ulogic是begin,如果inp=TRUE,则返回“1”;否则返回“0”;如果结束;末端功能;另一方面,如果您是Xilinx用户,这些操作符在旧版本的Vivado中是不可用的。您需要2017.4或更新版本才能使其真正工作!我想到了几个与1993年兼容的方法。向_stdulogicinp添加了一个转换函数:boolean return std _ulogic是begin,如果inp=TRUE,则返回“1”;否则返回“0”;如果结束;末端功能;和sel_sig_cmd,但这不起作用,不是一个特定的问题,一个基本的部分,见最后一个标题可验证。您应该产生一个分析错误,结果是没有可见的函数&。MCVe帮助未来的读者复制一个特定的问题并演示解决方案。不工作意味着这是不正确的,因为经验丰富的人可能已经知道了。这不会编译,我想,对于有经验的人来说,这个错误已经很清楚了,即使我能看到它,但无法解决它。这就是为什么我写了一些我想从这段代码中实现的东西。所以我决定最终制作一个函数,它采用Boolean和=运算符进行比较,并返回std_逻辑。但这不起作用不是一个特定的问题a的一个基本部分,请参阅最后一个标题“可验证”。您应该产生一个分析错误,结果是没有可见的函数&。MCVe帮助未来的读者复制一个特定的问题并演示解决方案。不工作意味着这是不正确的,因为经验丰富的人可能已经知道了。这不会编译,我想,对于有经验的人来说,这个错误已经很清楚了,即使我能看到它,但无法解决它。这就是为什么我写了一些我想从这段代码中实现的东西,所以我决定在最后做一个函数,这个函数使用boolean和=运算符进行比较,并返回std_逻辑。