# String concatenation inside generate block

**URL:** <https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818>\
**Category:** SystemVerilog\
**Tags:** generate-block, string-concatenation, Assignment-inside-generate-block, SystemVerilog, generate-macro-define, define-macro\
**Created:** [December 12, 2017, 6:51am UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818 "2017-12-12T06:51:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![randomcoder](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@randomcoder](https://verificationacademy.com/forums/u/randomcoder)\
**Post date:** [December 12, 2017, 6:51am UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/1 "2017-12-12T06:51:58Z")

</div>

Hi,  
I’m trying to simulate something like this:

`

```verilog
define myreg(name) \
logic b_``name``_int;

generate 
for (genvar i=0; i<8; i++) begin

modulexyz xyz ( .a(a),
                .b(`myreg(i))
);

end
endgenerate

```

To the submodule xyz , the input b is to be passed with b\_0\_int , b\_1\_int ,… b\_7\_int . What is the best way of using the `define macro for this usage ?

The above , during the compilation gives b\_i\_int instead of b\_0\_int, b\_1\_int , etc.,.  
I understand that `define are compiled earlier than the generate blocks. Then how should be above usage be coded ? I donot want to use an array as it adds a lot of code lines.

---

<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:** [December 12, 2017, 7:37am UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/2 "2017-12-12T07:37:27Z")

</div>

_In reply to [randomcoder](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818):_

You need to explain why you think using an array is more code.

Your alternatives are:

- unrolling the generate loop manually

```verilog
modulexyz xyz_0 ( .a(a),
.b(b_0_int));
modulexyz xyz_1 ( .a(a),
.b(b_1_int));
modulexyz xyz_2 ( .a(a),
.b(b_2_int));
modulexyz xyz_3 ( .a(a),
.b(b_3_int));
modulexyz xyz_4 ( .a(a),
.b(b_4_int));
modulexyz xyz_5 ( .a(a),
.b(b_5_int));
modulexyz xyz_6 ( .a(a),
.b(b_6_int));
modulexyz xyz_7 ( .a(a),
.b(b_7_int));

```

- packing the variables into an array

```verilog
wire [0:7] my_reg = {b_0_int,b_1_int,b_2_int,b_3_int,b_4_int,b_5_int,b_6_int,b_7_int};
generate
for (genvar i=0; i<8; i++) begin
modulexyz xyz ( .a(a),
.b(my_reg[i]
);
end
endgenerate

```

---

<div class="post-metadata">

**Author:** ![randomcoder](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@randomcoder](https://verificationacademy.com/forums/u/randomcoder)\
**Post date:** [December 12, 2017, 8:15am UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/3 "2017-12-12T08:15:40Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/2):_  
Hi dave,  
Thanks for your quick response.

Each b\__int is of say 256 bits, and say i have variables from b\_0\_int to b\_127\_int , I will have to do an assign such as :  
assign b\_int[0] = b\_0\_int ;  
.  
.  
.  
.  
.  
assign b\_int[127] = b\_127\_int;  
This adds about 128 buffers just for the usage of the modulexyz 128 times. In the above example, I have the b_\_int mentioned as 8 bits, but how about when the width of it increases. I want to have variability on fly. This is how i meant more code (as a result more buffers).

Thankyou for the alternatives suggested!  
Although , Option 1) is not feasible when the variable used is of higher width. and Option 2) woudn’t be right as i want “b” of modulexyz to receive just b\_0\_int and not the concatenated value.

Hence I see the need to use `define macros. Is the usage of `define not possible ?

---

<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:** [December 12, 2017, 5:51pm UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/4 "2017-12-12T17:51:17Z")

</div>

_In reply to [randomcoder](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/3):_

You still did not explain why you need individually named signals instead of using an array in the first place.

As you are already aware, `define macros get expanded before any parsing any SystemVerilog code. There is no way to have a macro loop. Your only other option is using an external macro processor. That can make debugging much more difficult.

---

<div class="post-metadata">

**Author:** ![randomcoder](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/96bed5/32.png) [@randomcoder](https://verificationacademy.com/forums/u/randomcoder)\
**Post date:** [December 14, 2017, 4:35am UTC](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/5 "2017-12-14T04:35:36Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/string-concatenation-inside-generate-block/33818/4):_

Well, i have my signal named as b\_\*\_int . That is why i want to have individually named signals. The only solution i see is FORM AN ARRAY from those individual signals like :

reg [127:0] b\_int [127:0] ;  
assign b\_int[0] = b\_0\_int ;  
.  
.  
.  
.  
.  
assign b\_int[127] = b\_127\_int;

and then use it in my module as :

generate  
for (genvar i=0; i\<128; i++) begin  
modulexyz xyz ( .a(a),  
.b(b\_int[i])  
);

Do you see a more cleaner way of this usage, without the assign statements ?
