简体   繁体   English

如何在FPGA中实现无界循环?

[英]how can I implement unbounded loops in FPGA?

I understand that loops that run for predefined number of iterations are unrolled during HLS. 我了解在HLS期间展开了运行预定义次数的循环的循环。 But what about loops without predefined bounds? 但是没有预定义范围的循环又如何呢? eg, 例如,

for (i = 0; i < j; i++) { ... }

How are such loops implemented in FPGAs? 在FPGA中如何实现这样的循环?

I'm not sure how this is VHDL or Verilog related, since you explicitly state HLS and give a C style question. 我不确定这与VHDL或Verilog有什么关系,因为您明确声明了HLS并提出了C风格的问题。 But certainly in VHDL, unbound loops are not explicitly supported. 但可以肯定的是,在VHDL中,未明确支持未绑定的循环。 There are cases where you can use unbounded loops, but they must terminate at some point. 在某些情况下,您可以使用无界循环,但必须在某些时候终止。

As user1155120 pointed out, dynamic loop constraints are not supported. 如user1155120所指出的,不支持动态循环约束。 But to expand on that a bit. 但是要扩大一点。

Edit: Initially I used the term "unbounded" when I should have said "dynamic range" loops. 编辑:当我应该说“动态范围”循环时,最初使用术语“无界”。 I've updated the answer with that language. 我已经用该语言更新了答案。

So, first, some types of loops with pseudo-dynamic ranges are supported. 因此,首先,支持某些具有伪动态范围的循环。 For example: 例如:

for i in 0 to 7 loop
  for j in 0 to i loop
    ...
  end loop;
end loop;

This really isn't dynamic, since these are easily unrolled. 这确实不是动态的,因为它们很容易展开。 The synthesizer can determine the bounds on this loop. 合成器可以确定此循环的界限。 Clearly j can only go from 0 to 7. But other kinds of dynamic loops are not supported: 显然j只能从0到7。但是不支持其他类型的动态循环:

signal a : natural;
...
for i in 0 to a loop
...
end loop;

In this case, the range of the loop is not known statically. 在这种情况下,循环范围不是静态已知的。

But there is a way to dynamically "bound" loops based on inputs, but it does require knowledge of the bounds of the input. 但是有一种方法可以基于输入来动态地“绑定”循环,但是它确实需要了解输入的边界。 One can do something like: 一个人可以做类似的事情:

signal a : natural range 0 to 7;
...
for i in 0 to 7 loop
  if ( i < a ) then
    ...
  end if;
end loop;

Now the loop effectively only operates up to the value of a . 现在循环实际上仅在高达价值a (Some caution here. When doing this, memory--flops or latches depending on the context--may be inferred inside the if / end if for those cases where the the loop terminates early. There are ways to avoid it, but that's outside the scope of this answer.) (此处有些警告。执行此操作时, end if情况早于循环终止, if可以在if / end if内部推断内存(取决于上下文,是触发器还是锁存器)。有一些避免方法,但是那是在外部此答案的范围。)

RTL cannot be synthesized to dynamic structures. RTL无法合成为动态结构。 So your design will have to be sized for the largest possible case, and then dynamically choose how many iterations up to the maximum supported will be done. 因此,您的设计将必须针对最大可能的情况进行大小调整,然后动态选择要达到最大支持次数的迭代次数。

One final note. 最后一点。 There is support for unbounded loops (while loops), though it does require that the loop be terminated. 支持无界循环(while循环),尽管它确实要求终止循环。 Let's take the typical free running counter example: 让我们以典型的自由运行计数器示例为例:

signal ctr : unsigned(31 downto 0) := (others => '0');
...
process(clk)
begin
  if ( rising_edge(clk) ) then
    ctr <= ctr + 1;
  end if;
end process;

This can also be expressed in another manner: 这也可以用另一种方式表示:

process
begin
  wait until rising_edge(clk);
  ctr <= ctr + 1;
end process;

And it is possible to use an unbounded loop, for example: 并且可以使用无界循环,例如:

process
begin
  while true loop
    wait until rising_edge(clk);
    ctr <= ctr + 1;
  end loop;
end process;

All of these are synthesizable. 所有这些都是可综合的。

WARNING Discussion below is crazy stuff and should not be used as an example of code. 警告以下讨论是疯狂的事情,不应用作代码示例。 The examples are for discussion purposes only. 这些示例仅用于讨论目的。

And dynamic ranged loops are synthesizable, though their functionality isn't clear. 动态范围循环是可合成的,尽管其功能尚不清楚。 For example: 例如:

signal a   : natural := 33;
signal ctr : natural := 0;
...
process
begin
  for i in 0 to a-1 loop
    wait until rising_edge(clk);
    ctr <= ctr + 1;
  end loop;
end process;

Or even an unbounded loop: 甚至是无限循环:

process
  variable i : natural := 0;
begin
  while i < a loop
    wait until rising_edge(clk);
    ctr <= ctr + 1;
  end loop;
end process;

These both synthesize. 这些都合成。 But the latter is especially problematic as it does not simulate well, especially when i >= a . 但是后者尤其有问题,因为它不能很好地模拟,尤其是当i > = a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM