生成 "loop" 变量缺少 genvar:verilog

genvar is missing for generate "loop" variable : verilog

获取错误9: error: genvar is missing for generate "loop" variable 'r'. 1 error(s) during elaboration.

完整代码:

module divider (dividend, divisor, quotient, remainder ) ;
    input [7:0] dividend ; // eight input lines modeled as a bus
    input [7:0] divisor ; // select lines bundled as a bus
    output reg [7:0] quotient ;
    output reg [7:0] remainder ;
    reg [7:0] r;
    reg [7:0] q;
    assign q = 0;
    for(r = dividend; r >= divisor; r = r - divisor)
        assign q = q + 1;
    assign remainder = r;
    assign quotient = q;
endmodule

module main;
    reg [7:0] dd;
    assign dd = 12;
    reg [7:0] dr;
    assign dr = 5;
    reg [7:0] q;
    reg [7:0] r;
    wire a = divider(dd, dr, q, r);
    initial begin
    $display("quotient %d", q);
    $display("remainder %d",r);
    end
endmodule

我正在尝试编写一个模块,通过使用 verilog 中的行为建模重复减法来计算商和余数。这是我的第一个verilog程序,我无法修复这些错误,如果我的代码中还有其他错误,请指出。

问题出在 for 循环上。您可以使用 generate 块或 always 块来使用它。其中一种方法如下:

module divider (dividend, divisor,quotient, remainder ) ;
input [7:0] dividend ; // eight input lines modeled as a bus
input [7:0] divisor ; // select lines bundled as a bus

output reg [7:0] quotient ;
output reg[7:0] remainder ;

 always @(*) 
  begin
       quotient=0;
       for(remainder = dividend; remainder >= divisor; remainder = remainder - divisor)
          quotient = quotient + 1;            
  end

endmodule



module main;

reg[7:0] dd; 
reg[7:0] dr;

wire [7:0] q;
wire [7:0] r;

divider d0( .dividend(dd), .divisor(dr), .quotient(q), .remainder(r) ) ;

initial begin
   dd=12;
   dr=5;
end

initial begin
 #20  $display("quotient %d", q);
 #25    $display("remainder %d",r);
end

endmodule

注意事项:

  1. 如果您想使用 assign 语句分配一个变量,将该变量声明为 wire.
  2. 在测试台中,您需要将输入定义为"reg",将输出定义为"wire"。
  3. 您不能在 for 循环中使用 assign