Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verilog: is it possible to do indexed instantiation?

Tags:

verilog

I have a file, that is something similar to

module AB(A,B,Out);
  input A,B;
  output Out;

  wire Out;
  assign Out = A & B;
endmodule

I need to use N number of this calculation. ie i have a=1001; b=0001, I need to do something like bitwise AND, and I have N bits.

I have used it as an instantiation:

op[0] = a[0] & b[0];
op[1] = a[1] & b[1];
op[2] = a[2] & b[2];
op[3] = a[3] & b[3];
op[4] = a[4] & b[4];

When I'm trying to do this with an index i, I have:

AB g(a[i],b[i],Op[i]) for i = 0 to N-1. 

If I do this, it says AB is undeclared.

Is this impossible? If so, what is the alternative?

like image 315
Nandhini Avatar asked Dec 09 '22 09:12

Nandhini


1 Answers

You've a few options:

  • Parameterise bus sizes in your module
  • Array of instances
  • generate statements

But to answer the question, it is possible to do arrays of instances. Here's what the syntax looks like for your AB module.

module testbench ();
   localparam WIDTH = 4;

   reg [WIDTH-1:0] a_in, b_in;
   wire [WIDTH-1:0] out_a;

   AB u0[WIDTH-1:0]
     (
      .A(a_in),
      .B(b_in),
      .Out(out_a)
     );

   initial begin
      ...
   end

endmodule

Here, a_in[3], b_in[3] and out_a[3] are mapped to the ports of u0[3].

like image 120
Marty Avatar answered Dec 29 '22 05:12

Marty