2015-05-17 17 views
9

Tôi đang viết một dự án với Verilog và muốn sử dụng parameter để xác định một số tham số trong mô-đun của tôi. Nhưng khi tôi đọc trong một số mã nguồn, localparam đôi khi được sử dụng thay vì parameter.Sự khác biệt giữa "tham số" và "localparam"

Sự khác nhau giữa chúng là gì?

Trả lời

19

Nói chung, ý tưởng đằng sau các localparam (bổ sung vào tiêu chuẩn Verilog-2001) là để bảo vệ giá trị của localparam từ định nghĩa lại vô tình hoặc không chính xác bởi một người dùng cuối (không giống như một giá trị parameter, giá trị này không thể được sửa đổi bởi định nghĩa lại tham số hoặc theo câu lệnh defparam).

Dựa trên chuẩn IEEE 1364-2005 (ch 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. Local parameters can be assigned constant expressions containing parameters, which can be modified with defparam statements or module instance parameter value assignments.

Thêm vào đó, trong SystemVerilog (IEEE 1800-2012 (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.

Local parameters may be declared in a module’s 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. Any other parameter declaration in such a list shall be a nonlocal parameter that may be overridden.

Nếu bạn muốn để tìm hiểu thêm về chủ đề này, tôi khuyên bạn nên dùng giấy Clifford E. Cummings "New Verilog-2001 Techniques for Creating Parameterized Models (or Down With `define and Death of a defparam!)".

0

Minimal dụ

Dưới đây là một ví dụ về những gì Qiu đề cập.

Trong RAM, kích thước bộ nhớ là một chức năng của kích thước từ và địa chỉ.

Vì vậy, nếu mô-đun chính xác định kích thước từ và địa chỉ, nó cũng không thể chỉ định kích thước bộ nhớ.

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. 
... 

Và trên mô-đun phụ huynh, điều này là tốt:

module myram_tb; 
    myram #(
     .ADDR_SIZE(2), 
     .WORD_SIZE(2) 
    ) top (
     /* wires */ 
    ) 

nhưng này nên là một lỗi:

module myram_tb; 
    myram #(
     .ADDR_SIZE(2), 
     .WORD_SIZE(2), 
     .MEM_SIZE(2) 
    ) top (
     /* wires */ 
    ) 

iverilog không thất bại, và tôi tin rằng điều này là lỗi: https://github.com/steveicarus/iverilog/issues/157

Incisive cho một lỗi như mong đợi.

Các vấn đề liên quan