# Concatenation of instance name with loop index

**URL:** <https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, concatenation\
**Created:** [November 6, 2014, 4:48am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104 "2014-11-06T04:48:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chandrashekhar\_Goudar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@Chandrashekhar\_Goudar](https://verificationacademy.com/forums/u/Chandrashekhar_Goudar)\
**Post date:** [November 6, 2014, 4:48am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/1 "2014-11-06T04:48:29Z")

</div>

Hello,

I have a for loop inside which I am trying to concatenate a string with the loop index as below,

string str;  
for(i=0;i\<244; i++)  
begin  
str = {A\_inst,i};  
if(str.sig == 1)  
begin end  
end

Where, A\_inst is instance path name and “sig” is a signal name in that instance.I am getting a compilation error. Let me know where I am going wrong.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [November 6, 2014, 6:01am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/2 "2014-11-06T06:01:52Z")

</div>

It is not possible within the SystemVerilog language to access an identifier using a dynamic string name. Some program created these 244 unique instance names in your source file. Or maybe it was generated manually. You will have to use a program or script to generate the code here, or create it by hand.

---

<div class="post-metadata">

**Author:** ![Naven8](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Naven8](https://verificationacademy.com/forums/u/Naven8)\
**Post date:** [November 6, 2014, 6:25am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/3 "2014-11-06T06:25:27Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/2):_

If the code is simple enough then you can use define to do the same job.

```verilog

`define MOD(inst) mod``inst``.sig

....
for(i=0;i<244;i++) begin
 if(`MOD(i) == 1'b0) begin
 end
....
end

```

This code is working for me on questa & modelsim

---

<div class="post-metadata">

**Author:** ![Chandrashekhar\_Goudar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@Chandrashekhar\_Goudar](https://verificationacademy.com/forums/u/Chandrashekhar_Goudar)\
**Post date:** [November 7, 2014, 6:25am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/4 "2014-11-07T06:25:02Z")

</div>

_In reply to [Naven8](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/3):_

`define MOD(inst) mod`inst`.sig

I tried to use above defines, ie: MOD(0),MOD(1),MOD(2)… But I get warnings saying MOD is getting redefined and it takes the value of last define.

EX:  
`define MOD(0) module_ex_inst_``0`` `define MOD(1) module\_ex\_inst\_`1`  
`define MOD(2) module_ex_inst_``2`` `define MOD(3) module\_ex\_inst\_`3`  
`define MOD(4) module\_ex\_inst\_`4`

Let me know what is the problem.

---

<div class="post-metadata">

**Author:** ![Naven8](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Naven8](https://verificationacademy.com/forums/u/Naven8)\
**Post date:** [November 7, 2014, 7:06am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/5 "2014-11-07T07:06:52Z")

</div>

_In reply to [Chandrashekhar Goudar](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/4):_

There is no need to re-define the macro in your case. Just define the macro once and pass different values to access different sub modules. I have given proper code in my last example. Just use the same code.

---

<div class="post-metadata">

**Author:** ![Chandrashekhar\_Goudar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/3e96dc/32.png) [@Chandrashekhar\_Goudar](https://verificationacademy.com/forums/u/Chandrashekhar_Goudar)\
**Post date:** [November 7, 2014, 9:30am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/6 "2014-11-07T09:30:27Z")

</div>

_In reply to [Naven8](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/5):_

Its replacing “inst” with i but not the value of i.

---

<div class="post-metadata">

**Author:** ![Naven8](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Naven8](https://verificationacademy.com/forums/u/Naven8)\
**Post date:** [November 7, 2014, 2:28pm UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/7 "2014-11-07T14:28:32Z")

</div>

_In reply to [Chandrashekhar Goudar](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/6):_

Please share your code.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [November 7, 2014, 3:13pm UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/8 "2014-11-07T15:13:14Z")

</div>

_In reply to [Naven8](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/7):_

It would really help if BOTH of you shared a small COMPLETE example of your code.

---

<div class="post-metadata">

**Author:** ![Naven8](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Naven8](https://verificationacademy.com/forums/u/Naven8)\
**Post date:** [November 8, 2014, 5:48am UTC](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/9 "2014-11-08T05:48:35Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/concatenation-of-instance-name-with-loop-index/30104/8):_

Sorry. Looks like i have understood the requirement wrongly. I didn’t see any other way other than replicating the code for each module instance.

Below is a small example which is doing the same.

```verilog
module m1;
  `define D(x) mod``x``.sig
  m2#(4) mod0();
  m2#(5) mod1();
  initial begin
    #5;
    for(int j=0;j<2;j++) begin
      case(j)
        0: $display("Print in case0 %d", `D(0)); // Put a function call instead of $display if some common thing should be done.
        1: $display("Print in case1 %d", `D(1));
      endcase
    end
  end
endmodule

module m2;
  parameter m1=1;
  reg [10:0]sig;
  initial begin
    sig =m1;
  end
endmodule

```
