简体   繁体   English

简单Verilog for循环中的错误

[英]Error in simple Verilog for-loop

I'm getting familiar with Verilog doing small exercises and right now I am trying to implement a linear feedback shift register. 我逐渐熟悉Verilog的小练习,现在我正在尝试实现线性反馈移位寄存器。

I'm trying to model the flipflop chain inside an always block using a for-loop, yet iverilog keeps giving me the error register ``i'' unknown in lfsr where "i" is the iteration variable and lfsr is my module. 我正在尝试使用for循环在Always块内对触发器链进行建模,但是iverilog一直为我提供lfsr未知的错误寄存器``i'',其中``i''是迭代变量,而lfsr是我的模块。

always @(posedge clk or negedge res_n) begin
    if(res_n == 0) begin
        // ... implement reset
    end

    else begin
        flops[0] <= input_wire;
        for (i = 0; i <= width-2; i = i+1) begin
            flops[i+1] <= flops[i];
        end
    end

end

Could somebody help me out? 有人可以帮我吗?

Thanks. 谢谢。

You should declare the variable i first, or i will be regarded as an register with no specification. 您应该首先声明变量i,否则我将被视为没有指定的寄存器。 And this will let the compiler returns the unknown register error. 这将使编译器返回unknown register错误。

Declare i as an integer outside the for code block as below: 将我声明为for代码块外部的整数,如下所示:

integer i;

You need to declare the loop variable in a for loop, as another answer has stated. 正如另一个答案所述,您需要在for循环中声明循环变量。 However, this does not need to be outside the always block. 但是,这不必在always块之外。 Instead, if (and only if) you label a begin ... end block, you can declare the loop variable inside it. 相反,如果(且仅当) 标记begin ... end块时,可以在其中声明循环变量。 This declcartion has to be first inside the block. 必须首先在块内进行此操作。 This has the advantage of better encapsulation: 这具有更好的封装的优势:

always @(posedge clk or negedge res_n) begin
    if(res_n == 0) begin
        // ... implement reset
    end

    else begin : SOME_NAME
//                   ^
//                   |
//             this is a label

        integer i;     // declaring i here results in better encapsulation
                       // the declaration HAS to be before "imperative" (ie executable) code

        flops[0] <= input_wire;
        for (i = 0; i <= width-2; i = i+1) begin
            flops[i+1] <= flops[i];
        end
    end

end

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

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