Modelsim 中的参数问题

Issue with parameters in Modelsim

最近我遇到了以下问题:在 Quartus 软件中我定义了我的 Verilog 模块如下:

module module_name(
  input     [w1-1:0]    in1,
  input     [w2-1:0]    in2,
  output    [w1-1:0]    out1
);

  parameter w1 = 16;
  parameter w2 = 8;

  ...

endmodule

这个模块编译没有任何问题。但是,当我尝试在 Modelsim(-Altera) 10.3d 中模拟该代码时,出现以下错误:

(vlog-2730) Undefined variable: 'w1'.
(vlog-2388) 'in1' already declared in this scope (module_name)
(vlog-2730) Undefined variable: 'w2'.
...
Identifier must be declared with a port mode: in1
Identifier must be declared with a port mode: in2

我找到了使用以下代码绕过此问题的方法:

module module_name(
  in1,
  in2,
  out1
);

  parameter w1 = 16;
  parameter w2 = 8;

  input     [w1-1:0]    in1;
  input     [w2-1:0]    in2;
  output    [w1-1:0]    out1;

  ...

endmodule

另一种方法也是使用以下构造:

module module_name #(parameter w1 = 16, parameter w2 = 8)(
  input     [w1-1:0]    in1,
  input     [w2-1:0]    in2,
  output    [w1-1:0]    out1
);

...

endmodule

但是:在使用该参数的 input/output 信号之后定义参数是否非法(Quartus 认为不是)?

IEEE Std 1800-2012 § 23.2.1 模块 header 定义 状态两个 header 类型:

There are two styles of module header definitions, the non-ANSI header and the ANSI header.

The non-ANSI header style separates the definition of the module header from the declarations of the module ports and internal data. The informal syntax of a non-ANSI style module header is as follows:

module_name ( port_list ) ;
  parameter_declaration_list
  port_direction_and_size_declarations
  port_type_declarations

The module header definition is syntactically completed by the semicolon after the closing parenthesis of the port list. Declarations that define the characteristics of the ports (direction, size, data type, signedness, etc.) are local definitions within the module.

The ANSI header style makes the declarations of the port characteristics part of the module header (which is still terminated by a semicolon). The informal general syntax of an ANSI style module header is as follows:

module_name #( parameter_port_list )
             ( port_direction_and_type_list ) ;

没有提到 header 语法匹配:

 module_name ( port_direction_and_type_list ) ;
    parameter_declaration_list

根据 LRM,您的样本 header 没有投诉。任何支持该语法的东西都来自标准之外。

在 header 之后定义 parameters 和 localparams 是合法的,只要 header 符合 ANSI 风格 headers不引用它们。 IEEE Std 1800-2012 § 23.10 中的示例 覆盖模块参数