我有一个可变长度的矢量std_logic_vector(X downto 0).现在我试图在我的包中定义一个常量来进行复位,这样低位X/2就是1,其他位是零.
例如,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 := <???>;
Run Code Online (Sandbox Code Playgroud)
根据需要,至少有两种选择来初始化您的记录类型.一个是使用初始化函数,另一个是在聚合中使用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
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用预先定义的N值用聚合来初始化常量:
constant N: natural := 4;
constant ENTRY_3: entry_type := (
state => (
N-1 downto N/2 => '1',
N/2-1 downto 0 => '0'
)
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19076 次 |
| 最近记录: |