user9362958
user9362958

Reputation:

Verilog, How to pass different parameters when I use generate to instantiation module?

I have a question about parameters passing. I used generate for to do module instantiation. But how to pass different parameters to each module? For example:

generate
   for (i=0;i<N;i=i+1) begin:ModIns
       Mod #(.p1(?),.p2(?)) M (
       // Signal connection
       );
   end
endgenerate

For N modules, each with different p1 and p2. How to do that? By the way, the number of parameters is very large, can I pass parameters as a file? Thanks!

Upvotes: 1

Views: 3148

Answers (4)

Dustin
Dustin

Reputation: 11

This compiles with VCS:

genvar port_idx;
generate
for (port_idx = 0; port_idx < 5; port_idx++) begin : intf
    xactor_t xactor (core_clk, core_rst);
    defparam xactor.intf_id = 18 + port_idx;
end

Upvotes: 0

Pradyuman Bissa
Pradyuman Bissa

Reputation: 191

Here are my 2 cents. Declare a 2-D register and store all the parameters there.

reg [31:0] param_mem1 [N-1:0]; //Assuming parameter size to be 32 bit wide
reg [31:0] param_mem2 [N-1:0];

always@(posedge clk)
begin
 for(i=0;i<N;i=i+1)
 begin
   param_mem1[i] <= i; //Here replace 'i' with your actual parameter value
   param_mem2[i] <= i+1; //Dummy assignment, please use intended values
 end
end

generate
 for (i=0;i<N;i=i+1) begin:ModIns
   Mod #(.p1(param_mem1[i]),.p2(param_mem2[i])) M (
   // Signal connection
   );
end
endgenerate

Upvotes: 1

dave_59
dave_59

Reputation: 42673

It would be easier to do this in SystemVerilog because a parameter can be an array, and your generate loop could select an element for each iteration of the loop. In Verilog, you can pack your elements into a bit-vector, and your generate loop could select a slice for each iteration of the loop.

parameter A1={8'd1, 8'd2, 8'd3, 8'd4, ...};
parameter A2={8'd9, 8'd8, 8'd7, 8'd6, ...};
generate
   for (i=0;i<N;i=i+1) begin:ModIns
       Mod #(.p1(A1[i*8+:8),.p2(A2[i*8+:8])) M (
       // Signal connection
       );
   end
endgenerate

If you want to define the parameters from a file, you can put the parameter declarations in a separate file and `include the file.

Upvotes: 0

Oldfart
Oldfart

Reputation: 6259

First: No you can't read it from a file. Parameters have to be known at compile time and a file is only readable in run time.

You can derive them from a genvar or pass them through a hierarchy, but then it more or less stops. Maybe what you are trying to do can be solved in a different way but the scope of the problem you have outlined for us here is rater limited.

As always: Tell us your problem, not your solution.

Upvotes: 0

Related Questions