gpt4 book ai didi

vhdl - VHDL 保护类型数组

转载 作者:行者123 更新时间:2023-11-30 23:49:08 25 4
gpt4 key购买 nike

我试图更好地使用 VHDL protected 类型,所以我将以下测试放在一起(当然只是为了说明 - 我的实际用例要复杂得多):

type prot_type1 is protected
procedure set (new_data : integer);
impure function get return integer;
end protected prot_type1;

type prot_type1 is protected body
variable data : integer := 0;

procedure set (new_data : integer) is
begin
data := new_data;
end procedure set;

impure function get return integer is
begin
return data;
end function get;
end protected body prot_type1;

这编译。但是,以下行不会:

type prot_type1_array is array (natural range <>) of prot_type1;

Ashenden 说(第 3 版,第 589 页)“ protected 类型不能用作……复合类型的元素”。这很不幸。我希望能够用 body 创建另一个 protected 类型:

type prot_type2 is protected body
variable data : prot_type1_array(0 to 3);

procedure set (idx : natural; new_data : integer) is
begin
data(idx).set(new_data);
end procedure set;

...
end protected body prot_type2;

并避免重复 prot_type1.set() 中的代码(这在这种情况下是微不足道的,但在我的实际用例中会复杂得多)。不过,我唯一的选择似乎是 (1) 基本上重写整个 prot_type1,除了我的私有(private)变量的数组类型。或者 (2),在内部展平数组,例如:

type prot_type2 is protected body
variable data0 : prot_type1;
variable data1 : prot_type1;

procedure set (idx : natural; new_data : integer) is
begin
case idx is
when 0 =>
data0.set(new_data);
when 1 =>
data1.set(new_data);
when others =>
-- handle exceptions here
end case;
end procedure set;

...
end protected body prot_type2;

这行得通,但对于小型阵列来说有点不受欢迎,而且对于大型阵列来说极度不受欢迎。还有别的办法吗?

最佳答案

这是基于 Morten Zilmer 评论的建议。 prot1_type 可以访问整数而不是唯一整数。我使用函数 append、remove 和 get 来管理整数值。

代码如下:

type array_int is array (natural range <>) of integer;
type a_integer is access array_int;

type prot_type1 is protected
-- add a new value at the end of the vector
procedure append (new_data : integer);
-- remove a value from the vector, return 0 ik OK, -1 is the item doesn't exist
impure function remove (index : integer) return integer;
-- return the integer value of the item
impure function get(index : integer) return integer;
end protected prot_type1;

type prot_type1 is protected body

variable data : a_integer;

procedure append(new_data : integer) is
variable temp : a_integer;
begin
-- create a temporary vector with the new values
temp := new array_int'(data.all & new_data);
-- free memory of the real vector
Deallocate(data);
-- reallocate the real vector with the good values
data := new array_int'(temp.all);
-- free memory of the temporary vector
Deallocate(temp);
end procedure append;

impure function remove(index : integer) return integer is
variable temp : a_integer;
begin
if (index > data'length-1 or index < 0) then -- not sure if the vector is (0 to length -1) or (1 to length). to be tested !!!
return -1;
else
-- create a temporary vector with the new values
temp := new array_int'(data(0 to index-1) & data(index+1 to data'length-1));
-- free memory of the real vector
Deallocate(data);
-- reallocate the real vector with the good values
data := new array_int'(temp.all);
-- free memory of the temporary vector
Deallocate(temp);
return 0;
end if;
end function remove;

impure function get(index : integer) return integer is
begin
return data(index);
end function get;

end protected body prot_type1;

关于vhdl - VHDL 保护类型数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com