1

I need to generate filenames with variables in the name. Eg. filename needs to be wave_mon[0]_ch0_dat.out The 0 in this string will vary based on a for loop. My code is as below:

for (i=0; i<16; i=i+1) begin
    fname = {"wave_mon[",i,"]_ch",i,"_dat.out"};
    $display("Filename created is %s \n", fname);
    #1us;
end 

The output from this code is always as below. What am I missing that it is not able to print value of i in the string name?

Filename created is wave_mon[]_channel__data.out                                     

Filename created is wave_mon[]_channel__data.out 

Filename created is wave_mon[]_channel__data.out 

1 Answer 1

2

Method 1: using $sformat

$sformat(fname,"wave_mon[%0d]_ch%0d_dat.out",i,i);
$display("Filename created is %s \n",fname);

Method 2: using $sformatf

fname={$sformatf("wave_mon[%0d]_ch%0d_dat.out",i,i)};
$display("Filename created is %s \n",fname);

I created code snippet for you to try: https://www.edaplayground.com/x/5uQD

There is more to read about string manipulations in SV here if you wish: https://www.chipverify.com/systemverilog/systemverilog-strings

Sign up to request clarification or add additional context in comments.

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.