How to NOT use while() loops in verilog (for synthesis)?

大兔子大兔子 提交于 2019-12-02 00:48:24

Synthesis tools vary but generally a loop can be synthesized so long as the number of iterations is known a to the synthesis tool. So,

for ( i = 0; i < 10; i = i + 1 )

is OK because the tool knows there are 10 loop iterations. But

reg [10:0] r;
for ( i = 0; i < r; i = i + 1 )

is not OK because r is a variable r's value is unknown at synthesis time.

Think of loops in RTL code as creating a known fixed number of copies of a piece of logic.

You need to have a clock to control it to start.

always @(posedge clk or negedge rst_n)
  if (!rst_n)
     num <= 32'b0; // or whatever your width is.
  else
     if (num < test_number)
       num <= num + 1'b1;

If your synthesis tool does not support while or for loops, then don't use a loop. Just expand your code out.

wire [1:0] addr;
reg  [3:0] wren;

always @(posedge clk) begin
    wren[0] <= (addr == 2'd0);
    wren[1] <= (addr == 2'd1);
    wren[2] <= (addr == 2'd2);
    wren[3] <= (addr == 2'd3);
end

I am unfamiliar with XST, but some synthesis tools do support loops (Synopsys, for example).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!