代码之家  ›  专栏  ›  技术社区  ›  Anish Das

为什么我的代码在case语句中不断触发默认条件?

  •  1
  • Anish Das  · 技术社区  · 2 年前

    我正在学习Verilog,并试图构建一个32位Galois LFSR,其位位置分别为32、22、2和1。

    这是我的代码:

    module top_module(
        input clk,
        input reset,    // Active-high synchronous reset to 32'h1
        output [31:0] q
    ); 
        always @(posedge clk) begin
            if (reset)
                q <= 32'h1;
            else begin
                integer i;
                for (i=0; i<32; i=i+1) begin
                    case (i)
                        0 : q[i] <= q[0] ^ q[i+1];
                        1 : q[i] <= q[0] ^ q[i+1];
                        21 : q[i] <= q[0] ^ q[i+1];
                        31 : q[i] <= q[0];
                        default : q[i] <= q[i+1];
                    endcase
                end
            end
        end
    endmodule
    

    我面临以下错误:

    Error (10232): Verilog HDL error at top_module.v(17): index 32 cannot fall outside the declared range [31:0] for vector "q" File: /home/h/work/hdlbits.14205831/top_module.v Line: 17
    

    很可能是因为,它正在触发 default 的条件 case 语句何时 i=31 ,但这是不应该的,因为我已经为此创建了一个单独的条件。

    有人能告诉我我做错了什么吗?

    我正在粘贴HDLB的网页链接: https://hdlbits.01xz.net/wiki/Lfsr32

    1 回复  |  直到 2 年前
        1
  •  0
  •   toolic    2 年前

    Verilog代码很好;你的 case 语句可以和其他模拟器一起干净地编译。

    HDLB网站使用的工具中存在一个错误:

    Info: *******************************************************************
    Info: Running Quartus Prime Shell
        Info: Version 20.1.1 Build 720 11/11/2020 SJ Lite Edition
        Info: Copyright (C) 2020  Intel Corporation. All rights reserved.
    

    您可以尝试将其报告为上的错误 Contact page .

    你需要找到另一种方法来编写代码,以便在该网站上运行它。或者,您可以使用另一个模拟器,例如上的模拟器 EDA Playground .

    这里有一个痛苦的方法来绕过这个bug。更改:

                    default : q[i] <= q[i+1];
    

    至:

                    2,3,4,5,6,7,8,9,
                    10,11,12,13,14,15,16,17,18,19,
                    20,22,23,24,25,26,27,28,29,30 : q[i] <= q[i+1];
    

    我还尝试使用 case/inside 语法,但Quartus工具也不支持这一点。