2013-10-13 58 views
3

我有一个可变长度向量std_logic_vector(X downto 0)。现在我试图在我的软件包中定义一个常量来进行重置,因此X/2位为低位,其他为零。可变长度std_logic_vector在VHDL中初始化

例如,3位矢量(X=3)将使常数"011"和4位矢量将给出常数"0011"

我该如何在VHDL包中做到这一点?下面的代码解释了我想要做的事情。

type Entry_Type is record 
    state : std_logic_vector(X-1 downto 0); 
end record; 
constant Entry_Constant : Entry_Type := <???>; 

回答

5

至少有两种选择可以根据需要初始化记录类型。一个使用初始化函数,另一个使用聚合中的N值。

函数是初始化自定义数据类型的好方法。在你的情况,你可以创建一个功能default_entry_from_width(n),返回一个entry_type值:

type entry_type is record 
    state: std_logic_vector; 
end record; 

function default_entry_from_width(width: natural) return entry_type is 
    variable return_vector: std_logic_vector(width-1 downto 0); 
begin 
    for i in return_vector'range loop 
     return_vector(i) := '1' when i <= width/2 else '0'; 
    end loop; 
    return (state => return_vector); 
end; 

constant ENTRY_1: entry_type := default_entry_from_width(3); -- return 011 
constant ENTRY_2: entry_type := default_entry_from_width(4); -- return 0011 

另一种方法是初始化常量与集料,使用N的previsouly定义的值:

constant N: natural := 4; 
constant ENTRY_3: entry_type := (
    state => (
     N-1 downto N/2 => '1', 
     N/2-1 downto 0 => '0' 
    ) 
); 
2

你的意思是这样的:

library ieee; 
use ieee.std_logic_1164.all; 
package vector_length is 
    constant X: natural := 3; -- Entry_Type.state length 
    type Entry_Type is 
     record 
      state : std_logic_vector(X-1 downto 0); 
     end record; 
    constant entry_default: Entry_Type := 
      (state => 
       (X-1 downto NATURAL(REAL((X-1)/2) + 0.5) =>'0', others => '1') 
      ); 
end package vector_length; 

library ieee; 
use ieee.std_logic_1164.all; 
use work.vector_length.all; 

entity fum is 
end entity; 

architecture foo of fum is 
    signal entry: Entry_Type := entry_default; 
    signal default: std_logic_vector (X-1 downto 0); 
begin 
TEST: 
    process 
    begin 
     default <= entry.state; 
     wait for 100 ns; -- so it will show up in a waveform display 
     wait; 
    end process; 
end architecture; 

其满足您的条件X = 3的默认值是“011”,对于X = 4的默认值是“0011”。

请注意,默认值是在声明了子类型(条目)的地方分配的,而不是在类型声明中。

(这是一个收尾痛苦)。

+0

如果您为记录类型Entry_Type添加其他元素,则需要将这些元素的任何默认值添加到entry_default,其缺省值为聚合。 – user1155120