zxopenluyutong 发表于 2021-3-11 19:15:20

FPGA编程基础(一)--参数传递与寄存器使用

一、参数映射

参数映射的功能就是实现参数化元件。所谓的”参数化元件“就是指元件的某些参数是可调的,通过调整这些参数从而可实现一类结构类似而功能不同的电路。在应用中,很多电路都可采用参数映射来达到统一设计,如计数器、分频器、不同位宽的加法器以及不同刷新频率的VGA视频接口驱动电路等。

参数传递

参数传递就是在编译时对参数重新赋值而改变其值。传递的参数是子模块中定义的parameter,其传递方法有下面两种。


•时钟”#“符号

在同一模块中使用”#“符号。参数赋值的顺序必须与原始模块中进行参数定义的顺序相同,并不是一定要给所有的参数都赋予新值,但不允许跳过任何一个参数,即使是保持不变的值也要写在相应的位置。

module #(parameter1, parameter2) inst_name(port_map);

module_name #(.parameter_name(para_value), .parameter_name(para_value)) inst_name(port_map);

例:通过”#“字符实现一个模值可调的加1计数器



module cnt(
                input clk,
    input rst,
    output reg cnt_o
    );
    //定义参数化变量
    parameter Cmax = 1024;
   
                always @(posedge clk or negedge rst) begin
                                if(!rst)
                                        cnt_o <= 0;
                                else
                                        if(cnt_o == Cmax)
                                                cnt_o <= 0;
                                        else
                                                cnt_o <= cnt_o + 1;
                end

endmodule
module param_counter(
                input clk,
                input rst,
                output cnt_o
    );
   
    //参数化调用,利用#符号将计数器的模值10传入被调用模块
    cnt #10 inst_cnt(
                    .clk(clk),
                    .rst(rst),
                    .cnt_o(cnt_o)
                    );
endmodule
使用defparam关键字

defparam关键字可以在上层模块去直接修改下层模块的参数值,从而实现参数化调用,其语法格式如下:

defparam heirarchy_path.paramer_name = value;

这种方法与例化分开,参数需要写绝对路径来指定。参数传递时各个参数值的排列次序必须与被调用模块中各个参数的次序保持一致,并且参数值和参数个数也必须相同。

如果只希望对被调用模块内的个别参数进行更改,所有不需要更改的参数值也必须按对应参数的顺序在参数值列表中全部列出(原值拷贝)。

使用defparam语句进行重新赋值时必须参照原参数的名字生成成分级参数名。

例:通过”defparam“实现一个模值可调的加1计数器。

module param_counter(
                input clk,
                input rst,
                output cnt_o
    );
   
    //参数化调用,利用#符号将计数器的模值10传入被调用模块
    cntinst_cnt(
                    .clk(clk),
                    .rst(rst),
                    .cnt_o(cnt_o)
                    );
   //同过defparam修改参数
   defparam inst_cnt.Cmax = 12;
endmodule

hellokity 发表于 2021-3-16 08:28:40

FPGA编程基础(一)--参数传递与寄存器使用
页: [1]
查看完整版本: FPGA编程基础(一)--参数传递与寄存器使用