Is it possible to access the index in a for loop in vhdl ? error: indexed name prefix type natrual is not an array type

Is it possible to access the index in a for loop in vhdl ? error: indexed name prefix type natrual is not an array type

我想写一个小程序,将大端转换为小端。因此我需要使用 for 循环。因为我只想每 8 位(1 字节)进行一次切换,所以我需要使用某种模数。我不喜欢使用预定义的方法,并希望尽可能将其编写为硬件。因此我想访问迭代器变量并检查最后三位是否为零。因此,我知道完成了 8 次迭代。可悲,我收到错误信息:

Indexed name prefix type natural is not an array type

我的代码如下:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;


entity TOP is
    Port ( INPUT : in STD_LOGIC_VECTOR (32 downto 0);
           CLK : in STD_LOGIC;
           OUTPUT : out STD_LOGIC_VECTOR (32 downto 0));

end TOP;

architecture Behavioral of TOP is

begin
S: process(CLK)
begin
    if rising_edge(CLK) then
        for I in INPUT' range loop
            --every 8 bit
            if I(3 downto 0) = "000" then
                OUTPUT(OUTPUT'left -I*8 downto OUTPUT'left - (I+1)*8) <= INPUT((I+1)*7 downto I*7); 
            end if;
        end loop;
    end if;
end process S;
end Behavioral;

现在显而易见的答案是 if I mod 8 = 0 then 但您不想那样做。

你在行 if I(3 downto 0) = "000" then 上所做的是混合抽象级别:一个整数,以及一个整数的低级表示作为一包位。或者严格来说,一个可索引类型——即一个数组。

这是一个坏主意,因为它对可能有效或可能无效的整数表示进行了假设……具有讽刺意味的是,包括 endian-ness。所以,VHDL 不会让你那样做。

你可以做的是明确 "convert" 一个整数的表示,你可以确定哪个位对应什么,然后对该具体表示执行低级操作,而不是抽象 Integer。 (假设您正在使用的计算机无论如何都具有该表示,"conversion" 在时间或硬件资源方面没有损失)。

有两种标准的此类表示。在 numeric_std 库中,分别称为 signedunsigned。 'signed' 一个是明确的 2-s 补码,而 unsigned 很简单 - 两者都是 std_logic 类型的数组,因此可索引。

use IEEE.numeric_std.all;

而且你可以阅读这个包的源代码,所以没有什么可以做的假设。 (或者你可以编写一个不同的包来体现你需要的约定。但是没有人这样做......)

因此您可以将循环变量转换为 5 位 unsigned 并对其进行索引...

if to_unsigned(I,5)(2 downto 0) = "000" then

请注意,由于数组长度不匹配,原始比较总是 return 错误。

最后,综合工具并不愚蠢;无论如何,他们安全地对 mod power-of-2 进行了明显的优化。