步长不等于1的vhdl“for loop”

时间:2013-12-05 06:04:26

标签: for-loop vhdl low-level

我有一个简单的问题。是否可以编写一个步长大小不等于1的循环VHDL,例如16

循环应该像

0 - > 16 - > 32 - > 48 ....到一些价值

任何直接的帮助表示赞赏

5 个答案:

答案 0 :(得分:4)

一种可能的解决方案是使用所需范围的1/16范围,并在其中展开循环以生成所需范围:

for i in 0 to 3 -- Actually 0 to 48
loop
    x(16*i) <= ...
    x((16*i)+1) <= ...
    (...)
    x((16*i)+15) <= ...
end loop;

另一种解决方案是使用一段时间。假设你的count变量是一个整数:

while (i < 48)
loop
   --Do something
   i := count + 16;
end loop;

编辑:我没有测试上面的代码,你可能无法更改循环内的变量计数,我不确定。也许第一个解决方案是最好的解决方案。

不可能有一个步骤不同于1的for循环。你甚至不允许在for中更改它,如下所示:

--THIS WILL NOT WORK
for i in 0 to 48 loop
    --Do Something
    i := i + 15; -- This will NOT increment the loop index by 16
end loop; 

最后,对于2或3的步骤,您可以使用嵌套的。

但无论如何,你想要完成什么? VHDL是一种低级硬件描述语言,你应该能够实现你想要的任何东西,而不需要花哨的循环。

答案 1 :(得分:1)

VHDL在for循环中没有步骤参数,因此to的步长始终为1 范围方向和-1 downto范围方向。

因此,如果您需要具有开始和步骤值的循环,您可以执行以下操作:

...
constant FOR_START : natural := 1;
constant FOR_STEP  : natural := 2;
variable idx_v     : natural;       -- Support index variable
...
for idx_pre in 0 to 3 loop
  idx_v := FOR_START + FOR_STEP * idx_pre;
  z_o(idx_v) <= a_i(idx_v);
end loop;

while循环也可以用作替代方法:

constant FOR_START : natural := 1;
constant FOR_STEP  : natural := 2;
constant FOR_MAX   : natural := 7;
variable idx_v     : natural;
...
idx_v := FOR_START;
while idx_v <= FOR_MAX loop
  z_o(idx_v) <= a_i(idx_v);
  idx_v := idx_v + FOR_STEP;
end loop;

答案 2 :(得分:1)

如何遍历整个范围,然后使用“ if”语句仅对每个第16个值起作用?

for i in start_thing to end_thing loop
  if i mod 16 = 0 then
    do things(i)
  end if;
end loop; -- i

或交替使用下一步:

for i in start_thing to end_thing loop
  next when i mod 16 /= 0 ;
  do_things(i)
end loop; -- i

答案 3 :(得分:0)

我的研究说没有,但你可以声明第二个变量,它在你的循环中充当变量的倍数。

答案 4 :(得分:-1)

是的,可以以不等于1的步长“进行循环”。

for i in range 1 downto 0 loop
    foo(i) <= bar(1-i);
end
loop;