Passing Modules as parameters in Verilog ?

Status
Not open for further replies.

Gpanos

Newbie level 1
Joined
Feb 17, 2016
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
Athens
Activity points
16
Hi all,

I've recently been trying to write [in Verilog] generic modules that will work as "wrappers" on top of other modules.
e.g. a serial interface module that could contain a basic math module like an adder, multiplier or shift/roll etc.

So, instead of creating a new module for each of them (e.g. serial_adder, serial_multiplier etc),
I was thinking of passing the basic module as a parameter to the generic module (@ instantiation).

I think the following example illustrates the idea (here, the generic module is a register for recursive additions, multiplications etc).

Code:
module Adder(S, A, B);
  output [3:0] S;
  input [3:0] A;
  input [3:0] B;
  
  assign S = A + B;
endmodule

module Serial_calc #(parameter aModule = Adder) (Result, D, CLK);
  output reg [3:0] S;
  input      [3:0] D;

  input CLK;

  wire [3:0] S_temp;
  
  aModule M0(S_temp, S, D);

  always @(posedge CLK) begin
    S <= S_temp;
  end
endmodule

//--- Instantiation ---
  Serial_Calc #(Adder) M0(Result0, D, CLK0);
  Serial_Calc #(Multiplier) M1(Result1, D, CLK1);

This code does not work however, it gives errors like "Identifier 'Adder' has not been declared yet".

My Question: What is the proper way to do this ? or is it not possible at all ?

[Note that I'm new to Verilog, so please excuse if there is something obvious I don't see]

All answers highly appreciated!
 

This is not possible at all - modules cannot be passed around anywhere. They can only be instantiated.

Have a look at generate - that does what you want (based on some paramter, you can chose what to instantiate).
 
Reactions: Gpanos

    Gpanos

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…