2016-08-25 91 views
1
--in the package 
type t_array is array (natural range <>) of std_logic_vector (7 downto 0); 
type p_array is access t_array; 

--in my testbench 
variable my_array : p_array := null; 
begin 
my_array := new t_array(0 to 19); 
my_array := (X"00",X"00",X"00",X"00",X"FF", 
       X"FF",X"FF",X"FF",X"00",X"00", 
       X"FF",X"FF",X"FF",X"FF",X"FF", 
       X"FF",X"FF",X"FF",X"FF",X"FF"); 

Error: Target type util_lib.tb_pkg.p_array in variable assignment is different from expression type util_lib.tb_pkg.t_array.初始化動態數組VHDL

我怎樣才能緊湊分配陣列中的所有元素? (1)

+0

'my_array(my_array'range):=(X 「00」,X 「00」,X「00」,X「00」,X「FF」,「... – user1155120

+0

這個問題是區分訪問和它訪問的東西 – user1155120

回答

3

(1)。取消您的指點咳嗽訪問類型。

my_array.all := (...); 

(2)從一個函數初始化它

begin 
    my_array := new_array(20); 

初始化可以埋在功能,這可能算法計算所述值的底層細節,將它們複製從恆定陣列,或甚至從文件中讀取內容。

constant initialiser : t_array := (...); 

function new_array(length : natural range initialiser'range) return t_array is 
    variable temp : p_array := new t_array(0 to length - 1); 
begin 
    -- either this 
    for i in temp'range loop 
     temp(i) := initialiser(i); 
    end loop; 
    -- or simply this 
    temp.all := initialiser(temp'range); 
    return temp; 
end new_array; 

(注意參數的約束new_array:確保它不會產生比初始化器大的陣列)

+0

這是完美的,非常感謝 – StanOverflow

1

你如何訪問和它所指向的東西區別?

library ieee; 
use ieee.std_logic_1164.all; 

package initialize is 
--in the package 
type t_array is array (natural range <>) of std_logic_vector (7 downto 0); 
type p_array is access t_array; 
end package; 

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

entity testbench is 
end entity; 

architecture fum of testbench is 

begin 
    process 
    --in my testbench 
    variable my_array : p_array := null; 
    begin 
     my_array := new t_array(0 to 19); 
     my_array (my_array'range) := (X"00",X"00",X"00",X"00",X"FF", 
         X"FF",X"FF",X"FF",X"00",X"00", 
         X"FF",X"FF",X"FF",X"FF",X"FF", 
         X"FF",X"FF",X"FF",X"FF",X"FF"); 
     wait; 
    end process; 
end architecture; 

IEEE標準1076至2008年8名稱8.1一般第3段 - 4:

Certain forms of name (indexed and selected names, slice names, and attribute names) include a prefix that is a name or a function call. If the prefix of a name is a function call, then the name denotes an element, a slice, or an attribute, either of the result of the function call, or (if the result is an access value) of the object designated by the result. Function calls are defined in 9.3.4.

A prefix is said to be appropriate for a type in either of the following cases:
— The type of the prefix is the type considered.
— The type of the prefix is an access type whose designated type is the type considered.

這有助於理解是用於描述之間的關係爲的同義詞表示訪問類型的值及其引用的對象。

第5款:

The evaluation of a name determines the named entity denoted by the name. The evaluation of a name that has a prefix includes the evaluation of the prefix, that is, of the corresponding name or function call. If the type of the prefix is an access type, the evaluation of the prefix includes the determination of the object designated by the corresponding access value. In such a case, it is an error if the value of the prefix is a null access value. It is an error if, after all type analysis (including overload resolution), the name is ambiguous.

在這種情況下,你可以使用包含整個陣列片名稱。

此外訪問元素,可以使用所選擇的名稱:

  my_array.all := (X"00",X"00",X"00",X"00",X"FF", 

8.3選定的名稱第5段:

For a selected name that is used to denote the object designated by an access value, the suffix shall be the reserved word all. The prefix shall belong to an access type.