Differences and examples of `define, parameter, and localparam in Verilog

1. Overview
define: function -> commonly used to define constants across modules and files;
scope -> the entire project;
parameter: function -> commonly used for parameter transfer between modules;
scope -> valid definitions in this module;
localparam function - > Parameter definitions commonly used in state machines;
scope -> Valid definitions in this module, not available for parameter passing

2. Application example

(1) define
concept: It can be defined across modules, written on the module name, and is valid in the entire design project.
Once a define directive is compiled, it is valid throughout the compilation process. For example,
through the define command in another file, the defined constants can be called by other files
until `undef is encountered;
for example: define define UART_CNT 10'd1024
use UART_CNT

(2) parameter
concept: valid definitions in this module can be used for parameter transfer;
if parameters cannot be passed when they are defined inside the module, they can be passed
if they are written as follows after the module name.
Example : Definition ->

module video_in  
           #(  
          parameter MEM_DATA_BITS = 64,  
          parameter INTERLACE     = 1      // 0  
          )  
          (  
           input     clk,  
           input     rst_n,  

           output    burst_finsh  
          );  

When using -> to call this module, parameters can be passed like port signal transmission

video_in  
       #( .MEM_DATA_BITS ( 64 ),  
          .INTERLACE     ( 1  )  
        )  
       u_video_in (  
        .clk             (clk_50m),  
        .rst_n          (rst_n),  

        .burst_finsh (burst_finsh)  
        );  

(3) localparam:
Concept: a valid definition in this module and cannot be used for parameter transfer;
localparam cannot be used within the module port parameter list.
In general, the parameters of the state machine use localparam.
Example:

localparam BURST_LEN               = 10'd64;     /*一次写操作数据长度 */  

localparam BURST_IDLE              = 3'd0;       /*状态机状态:空闲 */  
localparam BURST_ONE_LINE_START    = 3'd1;       /*状态机状态:视频数据一行写开始 */  
localparam BURSTING                = 3'd2;       /*状态机状态:正在处理一次ddr2写操作 */  
localparam BURST_END               = 3'd3;       /*状态机状态:一次ddr2写操作完成*/  
localparam BURST_ONE_LINE_END      = 3'd4;       /*状态机状态:视频数据一行写完成*/  

reg[2:0]  burst_state              = 3'd0;       /*状态机状态:当前状态 */  
reg[2:0]  burst_state_next         = 3'd0;       /*状态机状态:下一个状态*/  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566488&siteId=291194637