简体   繁体   English

将模块名称作为参数传递

[英]Passing a module name as parameter

I want to create this generic wrapper for a bunch of modules I am writing. 我想为我正在编写的一堆模块创建这个通用包装器。 The wrapper should provide the ability to connect these modules to different type of NoCs without having to change the behavior of the inner modules. 包装器应该能够将这些模块连接到不同类型的NoC,而无需改变内部模块的行为。

I thought that one way to do this would be the following. 我认为这样做的一种方法如下。 Considering a very simple module to wrap: 考虑一个非常简单的包装模块:

module add #(                                                                                                                       
             parameter COLUMN_WIDTH     = 32                                                                                        
             )

   (
    //data in                                                                                                                       
    input logic [COLUMN_WIDTH-1:0]  col_1,
    input logic [COLUMN_WIDTH-1:0]  col_2,
    //data out                                                                                                                      
    output logic [COLUMN_WIDTH-1:0] col_o

    );

   assign col_o = col_1 + col_2;

endmodule

The wrapper should be the following: 包装器应该如下:

module wrapper #(                                                                                                                   
                 parameter COLUMN_WIDTH     = 32,                                                                                   
                 parameter WRAPPED_MODULE   = add                                                                                   
             )
   (
    //data in                                                                                                                       
    input logic [COLUMN_WIDTH-1:0]  col_1,
    input logic [COLUMN_WIDTH-1:0]  col_2,
    //data out                                                                                                                      
    output logic [COLUMN_WIDTH-1:0] col_o,
    /* std signals */
    input logic                     clk,
    input logic                     reset_i // reset everything                                                                     
    );

   logic [COLUMN_WIDTH-1:0]         max_result;

   WRAPPED_MODULE #(.COLUMN_WDITH(COLUMN_WIDTH),
                    ) a(
                        .*
                        );

   always @(posedge clk) begin
      if (reset_i)
        max_result <= 0;
      else
        max_result <= (col_o > max_result) ? col_o : max_result;
   end

endmodule

The error I get is the following: 我得到的错误如下:

Error-[IND] Identifier not declared
wrapper.sv, 4
  Identifier 'add' has not been declared yet. If this error is not expected, 
  please check if you have set `default_nettype to none.

Which makes sense since a parameter is not the same as a macro. 这是有道理的,因为参数与宏不同。 A complete design should possibly instantiate a bunch of wrapped modules and I don't want to duplicate code by creating a wrapper for each inner module. 一个完整的设计应该可以实例化一堆包装的模块,我不想通过为每个内部模块创建一个包装来复制代码。 How can I do that? 我怎样才能做到这一点?

A parameter cannot be a module name. 参数不能是模块名称。 It can be a data_type, implicit data_type, or type 它可以是data_type,隐式data_type或type

IEEE Std 1800-2012 § A.2.1.1 Module parameter declarations : IEEE Std1800-2012§A.2.1.1 模块参数声明

parameter_declaration ::= parameter_declaration :: =
parameter data_type_or_implicit list_of_param_assignments 参数data_type_or_implicit list_of_param_assignments
| | parameter type list_of_type_assignments 参数类型list_of_type_assignments

A workaround is to use a generate block and compare the value of the parameter. 解决方法是使用生成块并比较参数的值。

module wrapper #(
    parameter        COLUMN_WIDTH     = 32,
    parameter string WRAPPED_MODULE   = "add"
  )
  (
    // ...
  );
  // ...
  generate
    if (WRAPPED_MODULE=="add") begin
      add #(.COLUMN_WDITH(COLUMN_WIDTH) ) a( .* );
    end
    else begin
     // ...
    end
  endgenerate
  // ...
endmodule

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

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