3

I would like to instantiate an array of systemverilog interfaces where each array element uses a different input.

If all the elements use the same input, then the instantiation is simple:

x_if x_IF[`NUM_INTERFACES](clk);

Here, if `NUM_INTERFACES is 2, then the clk input goes to both x_IF[0] and x_IF[1].

But if I also have

reg clk[`NUM_INTERFACES];

how do I instantiate x_IF so that clk[0] is input to x_IF[0] and clk[1] is input to x_IF[1]?

This is a simple example; I am looking forward to implement this in some sort of loop (probably using generate) for an array of 12 interfaces.

2
  • 1
    If you implement it using a generate loop, you won't have an array, but 12 different instances each in its own scope. Commented Dec 1, 2014 at 18:52
  • 1
    Exactly, that's why I was wondering how to get this done in a loop. Finally got a solution; I left the interface inputs unconnected in the declaration and later assigned values to them in the generate loop. Commented Dec 1, 2014 at 20:37

2 Answers 2

4

I would avoid using assign statements, especially with hierarchical references; it makes the code much more difficult to read and maintain.

You could have just done

reg clk[`NUM_INTERFACES];
x_if x_IF[`NUM_INTERFACES](clk);

A feature with arrays of instances is that if the width of a signal being connected to a port is a multiple of the port width, then each instance sill get a slice of the the signal.

See LRM 1800-2012 section 28.3.6 Primitive instance connection list that applies to module ports as well.

If you use a generate loop instead of an array of instances, then I would do

reg  clk[`NUM_INTERFACES];

generate
   for (genvar i=0; i<`NUM_INTERFACES; i++) begin :loop
      x_if x_IF(clk[i]);
   end
endgenerate
Sign up to request clarification or add additional context in comments.

4 Comments

Did you mean x_if x_IF[i](clk[i]);? Even then, wouldn't that create multiple interface arrays of incremental lengths up to NUM_INTERFACES? Or does it work because of (clk[i])?
No I did not mean that. Each iteration of the generate for-loop will create one instance named loop[i].x_IF because the begin block named loop will get elaborated into loop[0], loop[1], etc.
Thanks for the explanation. I was unaware of the ability to reference signals using loop[i].
Note that you can only reference loop[i] if i is a genvar or constant.
0

I was able to implement this by assigning the interface inputs from inside the generate loop and leaving them unassigned in the declaration.

x_if x_IF[`NUM_INTERFACES]();
reg  clk[`NUM_INTERFACES];

// Code to create clk[`NUM_INTERFACES]

generate
   for (genvar i=0; i<`NUM_INTERFACES; i++) begin
      // Assume that the input port of x_if is called `inp`
      assign x_IF[i].inp = clk[i];
   end
endgenerate

Comments

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.