2

Assume we have the following arbitrary parameterized module

module module_x #(parameter WIDTH = 1) (in_a, in_b, out);
    input [WIDTH - 1] in_a, in_b;
    output out;

    // Some module instantiation here
endmodule

How do I instantiate another based on the value of WIDTH ? like if it's 5 I instantiate it 5 times on each bit, is it possible to do this in Verilog ?

0

1 Answer 1

4

Generate statements are a common approach to this: Section 27 page 749 of IEEE 1800-1012.

A quick example :

logic [WIDTH-1:0] a;
logic [WIDTH-1:0] b; 

genvar i;
generate
for(i=0; i<WIDTH; i++) begin
  module_name instance_name(
    .a(a[i]), 
    .b(a[i])
  );
end
endgenerate 

As @toolic has pointed out instance arrays are also possible, and simpler.

logic clk;
logic [WIDTH-1:0] a_i;
logic [WIDTH-1:0] b_i; 

module_name instance_name[WIDTH-1:0] (
 .clk ( clk ), //Single bit is replicated across instance array
 .a   ( a_i ), //connected wire a_i is wider than port so split across instances
 .b   ( b_i )
);
Sign up to request clarification or add additional context in comments.

2 Comments

What would happen if we have WIDTH > input > 1 ?
@3bdalla it is my understanding that if the connected wire is wider than the port it is split and repeated to fill the array. input of 2 bits. wire 3 bits with an instance array of WIDTH 3. wire 3'bjkl would be repeated to 6'bjkljkl, which would be split up into jk, lj, kl to the 3 instances. jklused as place holders valid values are from 01zx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.