简体   繁体   English

"“参数”和“本地参数”之间的区别"

[英]Difference between "parameter" and "localparam"

I'm writing a project with Verilog and want to use parameter<\/code> to define some parameter in my module.我正在用 Verilog 编写一个项目,并希望使用parameter<\/code>在我的模块中定义一些参数。 But when I read in some source code, localparam<\/code> sometimes is used instead of parameter<\/code> .但是当我阅读一些源代码时,有时会使用localparam<\/code>而不是parameter<\/code> 。

What's difference between them?他们之间有什么区别?

"

Generally, the idea behind the localparam<\/code> (added to the Verilog-2001 standard) is to protect value of localparam<\/code> from accidental or incorrect redefinition by an end-user (unlike a parameter<\/code> value, this value can't be modified by parameter redefinition or by a defparam<\/code> statement).通常, localparam<\/code>背后的想法(添加到 Verilog-2001 标准)是为了保护localparam<\/code>的值免受最终用户意外或不正确的重新定义(与parameter<\/code>值不同,此值不能通过参数重新定义或通过defparam<\/code>声明)。

Based on IEEE 1364-2005 (ch. 4.10.2):基于 IEEE 1364-2005(第 4.10.2 章):

Verilog HDL local parameters are identical to parameters except that they cannot directly be modified by defparam statements or module instance parameter value assignments<\/strong> . Verilog HDL 本地参数与参数相同,只是它们不能通过 defparam 语句或模块实例参数值分配直接修改<\/strong>。 Local parameters can be assigned constant expressions containing parameters, which can be modified with defparam statements or module instance parameter value assignments.可以为局部参数分配包含参数的常量表达式,可以使用 defparam 语句或模块实例参数值分配来修改。

<\/blockquote>

Additionally, in SystemVerilog ( IEEE 1800-2012<\/a> (ch. 6.20.4)):此外,在 SystemVerilog ( IEEE 1800-2012<\/a> (ch. 6.20.4)) 中:

Unlike nonlocal parameters, local parameters can be declared in a generate block, package, class body, or compilation-unit scope.与非本地参数不同,本地参数可以在生成块、包、类主体或编译单元范围内声明。 In these contexts, the parameter keyword shall be a synonym for the localparam keyword.在这些上下文中,参数关键字应是 localparam 关键字的同义词。

Local parameters may be declared in a module's parameter_port_list.本地参数可以在模块的 parameter_port_list 中声明。 Any parameter declaration appearing in such a list between a localparam keyword and the next parameter keyword (or the end of the list, if there is no next parameter keyword) shall be a local parameter.任何出现在 localparam 关键字和下一个参数关键字(或列表末尾,如果没有下一个参数关键字)之间的列表中的参数声明都应该是本地参数。 Any other parameter declaration in such a list shall be a nonlocal parameter that may be overridden.此类列表中的任何其他参数声明都应是可以被覆盖的非本地参数。

<\/blockquote>

If you want to learn more about this topic, I'd recommend you Clifford E. Cummings paper " New Verilog-2001 Techniques for Creating Parameterized Models (or Down With `define and Death of a defparam!)<\/a> ".如果您想了解有关此主题的更多信息,我建议您阅读 Clifford E. Cummings 的论文“ 用于创建参数化模型的新 Verilog-2001 技术(或 Down With `define and Death of a defparam!)<\/a> ”。

"

Minimal example<\/strong>最小的例子<\/strong>

Here is an example of what Qiu mentioned.这是邱提到的一个例子。

In a RAM, the memory size is a function of the word and address sizes.在 RAM 中,内存大小是字和地址大小的函数。

So if the parent module specifies word and address size, it should not be able to specify the memory size as well.因此,如果父模块指定字和地址大小,它应该也不能指定内存大小。

module myram #(
    parameter WORD_SIZE = 1,
    parameter ADDR_SIZE = 1
) (
    input wire [ADDR_SIZE-1:0] addr,
    inout wire [WORD_SIZE-1:0] data,
    // ...
);
    localparam MEM_SIZE = WORD_SIZE * (1 << ADDR_SIZE);
    // Use MEM_SIZE several times in block.
...

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

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