来自 IEEE 标准 1076-2008:
5.3.2 数组类型
5.3.2.1 常规
数组对象是由具有相同子类型的元素组成的复合对象。
子类型提供了一个约束。在作为数组约束的数组类型的上下文中(参见6.3 子类型声明和5.3.2 数组类型),它是一个范围。
因此,作为数组元素的数组必须与任何其他元素具有相同的范围,并且您的方法无法工作(正如 Paebbels 指出的那样)。
至少有一种替代方法可以索引字符串
如果类型标记是基类型,则函数可以返回其返回类型的任何子类型的任何值。
这可以通过以下方式证明:
package my_package is
constant my_constant: integer := 4;
function my_string (index: natural) return string;
end package;
package body my_package is
function my_string (index: natural) return string is
begin
assert index < my_constant
report "my_string(" & integer'image(index) &") provides a range of elements greater than my_constant"
severity ERROR;
case index is
when 0 =>
return "Hello my name is Doron and I'm the developer of this project.";
when 1 =>
return "Another sentence, with 47 characters at total.";
when 2 =>
return "So it's range is '1 to 47'.";
when 3 =>
return "And the range of this sentence is: '1 to <last-number-in-constant_array1>'.";
when others =>
return "<ERROR>";
end case;
end function;
end package body;
use work.my_package.all;
entity foo is
end entity;
architecture fum of foo is
constant my_string0: string (my_string(0)'range) := my_string(0);
begin
process
begin
report my_string0;
report "my_string0'length = " &integer'image(my_string0'length);
for i in 1 to my_constant loop -- this will provide a call out of range
report my_string(i);
report "my_string(" &integer'image(i) &") length = " &integer'image(my_string(i)'length);
end loop;
wait;
end process;
end architecture;
包和演示VHDL代码分析、阐述和运行时产生:
ghdl -r foo
my_package.vhdl:37:9:@0ms:(report note): 大家好,我叫 Doron,我是这个项目的开发者。
my_package.vhdl:38:9:@0ms:(报告说明): my_string0'length = 61
my_package.vhdl:40:13:@0ms:(report note):另外一句,共47个字符。
my_package.vhdl:41:13:@0ms:(报告说明): my_string(1) 长度 = 46
my_package.vhdl:40:13:@0ms:(report note): 所以它的范围是 '1 到 47'。
my_package.vhdl:41:13:@0ms:(报告说明): my_string(2) 长度 = 27
my_package.vhdl:40:13:@0ms:(report note): 而这句话的范围是:'1 to '。
my_package.vhdl:41:13:@0ms:(报告说明): my_string(3) 长度 = 75
my_package.vhdl:9:13:@0ms:(断言错误): my_string(4) 提供了一个
大于 my_constant 的元素范围
my_package.vhdl:40:13:@0ms:(报告说明):
my_package.vhdl:9:13:@0ms:(断言错误): my_string(4) 提供的元素范围大于 my_constant
my_package.vhdl:41:13:@0ms:(report note): my_string(4) length = 7
my_string0 的常量声明展示了如何在对象声明中使用函数调用。
您可能会注意到您对字符串长度的期望似乎与 VHDL 不匹配,您始终表示它们长 1。VHDL 没有带内字符串信号结束。
冒险猜测,您无需集中定义一些可以索引以用于特定用途的字符串。上面的函数可以做到这一点。
使用恒定长度字符串数组
如果您要设置一个字符串数组,其最大长度为最长常量字符串,您可以使用函数将返回字符串缩减为长度:
package my_package is
constant my_constant: natural := 4;
constant LONGEST_STRING: natural := 75;
function my_string (index: natural) return string;
end package;
package body my_package is
type array_type1 is array(0 to my_constant - 1) of integer;
constant constant_array1: array_type1 := (62, 47, 28, 76);
type array_type2 is array (natural range 0 to my_constant - 1) of string (1 to LONGEST_STRING);
constant constant_array: array_type2 := (
0 => ("Hello my name is Doron and I'm the developer of this project."
& string'(constant_array1(0) to LONGEST_STRING => ' ')),
1 => ("Another sentence, with 47 characters at total."
& string'(constant_array1(1) to LONGEST_STRING => ' ')),
2 => ("So it's range is '1 to 47'."
& string'(constant_array1(2) to LONGEST_STRING => ' ')),
3 => ("And the range of this sentence is: '1 to <last-number-in-constant_array1>'.")
);
function my_string (index: natural) return string is
begin
assert index < my_constant
report "my_string(" & integer'image(index) &") provides a range of elements greater than my_constant"
severity ERROR;
if index >= my_constant then
return "<ERROR>";
else
return constant_array(index)(1 to constant_array1(index) - 1);
end if;
end function;
end package body;
use work.my_package.all;
entity foo is
end entity;
architecture fum of foo is
constant my_string0: string (my_string(0)'range) := my_string(0);
begin
process
begin
report my_string0;
report "my_string0'length = " &integer'image(my_string0'length);
for i in 1 to my_constant loop -- this will provide a call out of range
report my_string(i);
report "my_string(" &integer'image(i) &") length = " &integer'image(my_string(i)'length);
end loop;
wait;
end process;
end architecture;
运行时,这将产生与第一个示例基本相同的输出,尽管不同报告语句的标准输出中的行号和字符指针不同。