+ Post New Thread
Results 1 to 12 of 12
  1. #1
    Full Member level 5
    Points: 2,286, Level: 11

    Join Date
    May 2014
    Posts
    269
    Helped
    28 / 28
    Points
    2,286
    Level
    11

    Creating verilog define for filename based on input file path, string concatenation

    I want to be able to run a simulation with a test vector located in a repository. The repository will be determined by users credentials. The repository will not necessarily be a relative path to where the script is executed from.

    I've read the following
    https://stackoverflow.com/questions/...-concatenation
    It didn't help.

    Questasim command has the following

    vlog (stuff) +define+FILENAME_PATH="/home/path" "+incdir+..etc/etc"

    (tried with and without quotation)

    Then in a Verilog file I have the following

    Code Verilog - [expand]
    1
    2
    3
    4
    5
    6
    
    `ifdef FILENAME_PATH
      `define FILENAME1 ``FILENAME_PATH/test1.txt     // fail - what does double `` even do?
      `define FILENAME2 {`FILENAME_PATH, /test2.txt} // fail
      `define FILENAME3 {"`FILENAME_PATH`","/test3.txt"} // compiles but gives vsim-pli-3084 ($feof, fscanf etc argument 1 not valid file descriptor)
    `else
      `define FILENAME1 /home/hard/coded/path/test1.txt //works, but not flexible for different users.

    Questasim bombs out with
    # ** Error (suppressible): (vopt-7063) ../../../../../../../../source/PCIe_blk/bench/tb_PCIe_lib/read_file_tasks.vh(4): Failed to find 'FILENAME_PATH' in hierarchical name '/FILENAME_PATH'

    Basically the end goal is to make the stimulus script path agnostic. Relative path could be one solution, however I prefer a solution that is loaded/set during compile based on user credentials.

    Regards,

  2. #2
    Advanced Member level 4
    Points: 8,402, Level: 21
    Achievements:
    7 years registered Created Blog entry
    dpaul's Avatar
    Join Date
    Jan 2008
    Location
    Germany
    Posts
    1,252
    Helped
    272 / 272
    Points
    8,402
    Level
    21
    Blog Entries
    1

    Re: Creating verilog define for filename based on input file path, string concatenati

    What happens if you use the absolute path inside the RTL?
    FPGA enthusiast!



    •   AltAdvertisement

        
       

  3. #3
    Full Member level 5
    Points: 2,286, Level: 11

    Join Date
    May 2014
    Posts
    269
    Helped
    28 / 28
    Points
    2,286
    Level
    11

    Re: Creating verilog define for filename based on input file path, string concatenati

    #2 As per the `else example, it works, however it's not flexible for other users.

    Situation is : User checkout repo from git. Within their workspace is a test_vectors directory. They develop tests there. They checkin complete test vectors

    -------Edit.
    It would be preferred if the a script wasn't required to overwrite the text in the rtl itself. I could do a sed command, but would prefer not to, because people have a habit of checking in their butchered versions.



  4. #4
    Super Moderator
    Points: 31,151, Level: 43
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    7,188
    Helped
    1705 / 1705
    Points
    31,151
    Level
    43

    Re: Creating verilog define for filename based on input file path, string concatenati

    The vlog command line probably requires an escape character on the / in the directory name, so it doesn't get interpreted as a directory separator.

    - - - Updated - - -

    Escape doesn't work.

    I suggest adding a parameter to your code
    Code Verilog - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    module dir_path;
     
    parameter string MY_PATH = "/default/path";
     
      string my_str;
     
      assign my_str = {MY_PATH, "/test.txt"};
     
      initial begin
        $display ("start test");
        #10;
        $display ("%s", {MY_PATH, "/test.txt"});
        $display ("%s", my_str);
        #10;
        $display ("end test");
      end
     
    endmodule
    using the command line...
    Code:
    vsim -G MY_PATH = "/my_new_home/path"
    will override the parameter with a new value, you can also define the parameter with no value and have it set with -g instead. Using a parameter also has a benefit of not requiring that you recompile the source file to change where the directory is pointing to. You just rerun vsim with a new directory location.

    This is the method I've used for this type of string/directory manipulation in the past.

    From what I could glean from some experiments was that the +define+ is treating the value after the = as an integer (e.g. =45 is a '-') and won't accept a string.



  5. #5
    Advanced Member level 4
    Points: 6,170, Level: 18

    Join Date
    Feb 2015
    Posts
    1,016
    Helped
    286 / 286
    Points
    6,170
    Level
    18

    Re: Creating verilog define for filename based on input file path, string concatenati

    apparently you can use #sformat. I've never done this, so no idea if it actually works. either way, you can probably provide this to $display or such in order to see what prints out.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 3
    Points: 6,444, Level: 19
    Achievements:
    Created Blog entry 7 years registered

    Join Date
    Dec 2011
    Location
    Fremont, CA, USA
    Posts
    779
    Helped
    355 / 355
    Points
    6,444
    Level
    19
    Blog Entries
    4

    Re: Creating verilog define for filename based on input file path, string concatenati

    The `` and `" are SystemVerilog token pasting operator. Normally, the macro pre-processor only works with inseparable tokens like identifiers, numbers and strings. If you want a macro argument substituted in the middle of one of the tokens, you need these pasting operators. What you want is

    Code:
    `ifdef FILENAME_PATH
      `define FILENAME1 `"FILENAME_PATH/test1.txt`"
    And then use +define+FILENAME_PATH=/home/path without the quotes.

    I suggest displaying the resulting FILENAME to see the actual path it created, and checking the result from $fopen.
    Dave Rich
    Senior Verification Consultant
    Mentor Graphics Corporation



  7. #7
    Full Member level 5
    Points: 2,286, Level: 11

    Join Date
    May 2014
    Posts
    269
    Helped
    28 / 28
    Points
    2,286
    Level
    11

    Re: Creating verilog define for filename based on input file path, string concatenati

    #6 This is what I got with `define in quotes as per your comment

    Sim path = /home/P_1559_10030/iss_working/workspace/taylow00/simulation/sim_stimulus
    Result path = /home/P_1559_10030/iss_working/workspace/taylow00/simulation/sim_result
    start test
    `FILENAME_PATH`
    `FILENAME_PATH

    For the for
    Code SystemVerilog - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    module fml;
     
    parameter string sim_stimulus_path = "This is my path" ;
    parameter string sim_result_path = "This is my path" ;
     
     
      initial begin
        $display("Sim path = %s", sim_stimulus_path);
        $display("Result path = %s", sim_result_path);
        $display("start test");
        #10;
        $display("%s", "`FILENAME_PATH`");
        $display("%s", "`FILENAME_PATH");
        
        #10;
        $finish(2);
    end
     
    endmodule

    Where I provide parameter/generic values in as per vsim -G,

    however the +define doesn't seem to get incorporated.

    Furthermore I had to change the filetype to systemverilog which preferably I'd prefer NOT doing.



  8. #8
    Super Moderator
    Points: 31,151, Level: 43
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    7,188
    Helped
    1705 / 1705
    Points
    31,151
    Level
    43

    Re: Creating verilog define for filename based on input file path, string concatenati

    Quote Originally Posted by wesleytaylor View Post
    Where I provide parameter/generic values in as per vsim -G,

    however the +define doesn't seem to get incorporated.

    Furthermore I had to change the filetype to systemverilog which preferably I'd prefer NOT doing.
    You didn't have to use systemverilog it was just cleaner...
    Using only Verilog 2001 compatibility...

    Code Verilog - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    module dir_path_verilog;
     
    // parameter [64*8-1:0] MY_PATH; // this results in a string with lots of leading spaces
    parameter MY_PATH;
     
      wire [64*8-1:0] my_str;
     
      assign my_str = {MY_PATH, "/test.txt"};     // adds leading spaces due to my_str definition
     
      initial begin
        $display ("start test");
        #10;
        $display ("%s", {MY_PATH, "/test.txt"});  // no leading spaces
        $display ("%s", my_str);                  // leading spaces
        #10;
        $display ("end test");
      end
     
    endmodule

    Results in the following output results:
    Code:
    # start test
    # /this/is/a/new/path/test.txt
    #                                     /this/is/a/new/path/test.txt
    # end test
    and this result if the commented width defined parameter is used:
    Code:
    # start test
    #                                        /my_new_home/path/is/this/test.txt
    #                               /my_new_home/path/is/this/test.txt
    # end test
    As I already mentioned, I've used this method previously to generate directory strings, and this was prior to SystemVerilog even existing.

    It's been a while since I've used the non-string version but you may need to use a function to left justify the string if it ends up with leading spaces. I recall using such a function.



  9. #9
    Full Member level 5
    Points: 2,286, Level: 11

    Join Date
    May 2014
    Posts
    269
    Helped
    28 / 28
    Points
    2,286
    Level
    11

    Re: Creating verilog define for filename based on input file path, string concatenati

    Quote Originally Posted by ads-ee View Post
    It's been a while since I've used the non-string version but you may need to use a function to left justify the string if it ends up with leading spaces. I recall using such a function.
    Looking at std 1364-2001 section 17. IS the function you're talking about sformat? I tried
    $display("%s", $sformat(my_str, "%s"));

    Needless to say this was a total failure. sim tool was expecting a system function, not a system task.

    -------------------------

    A recap of the situation. The testbench code is Verilog. I wasn't intending to change it to system Verilog.

    The define doesn't work the way I have it.

    The parameter works with sv, but not with Verilog.



  10. #10
    Super Moderator
    Points: 31,151, Level: 43
    ads-ee's Avatar
    Join Date
    Sep 2013
    Location
    USA
    Posts
    7,188
    Helped
    1705 / 1705
    Points
    31,151
    Level
    43

    Re: Creating verilog define for filename based on input file path, string concatenati

    Quote Originally Posted by wesleytaylor View Post
    Looking at std 1364-2001 section 17. IS the function you're talking about sformat? I tried
    $display("%s", $sformat(my_str, "%s"));

    Needless to say this was a total failure. sim tool was expecting a system function, not a system task.
    No I wasn't talking about sformat, I was talking about a custom function I wrote to left justify an array

    Quote Originally Posted by wesleytaylor View Post
    A recap of the situation. The testbench code is Verilog. I wasn't intending to change it to system Verilog.

    The define doesn't work the way I have it.

    The parameter works with sv, but not with Verilog.
    Did you try the example in #8 it isn't the same as the first SV version as it uses a verilog wire vector instead of a string type. I compiled it with -vlog01compat in Modelsim to force it to use Std 1364-2001 and you shouldn't even have to do that if you compile it with a .v file extension.

    Like I've already stated...This IS how I've done this in the past before I switched to the SV as the majority of simulators started to support SV features.

    I also don't get the issue with using SV vs Verilog. SV IS Verilog you can write purely 2001 Verilog complaint code and compile it with your compilers SV switch turned on and nothing should change (as I recall only if you write problematic code would you have a chance for running across anything they actual changed i.e. improved/fixed)

    - - - Updated - - -

    Wait....maybe you are using Icarus Verilog. Last time I checked, that simulator isn't even fully 2001 compliant, which would explain why you are having problems.



    •   AltAdvertisement

        
       

  11. #11
    Advanced Member level 4
    Points: 6,170, Level: 18

    Join Date
    Feb 2015
    Posts
    1,016
    Helped
    286 / 286
    Points
    6,170
    Level
    18

    Re: Creating verilog define for filename based on input file path, string concatenati

    $sformat(my_str, "%s") means "place the result in my_str", the formatting is "%s", (error) the string to use to replace the first %s isn't provided.



  12. #12
    Full Member level 5
    Points: 2,286, Level: 11

    Join Date
    May 2014
    Posts
    269
    Helped
    28 / 28
    Points
    2,286
    Level
    11

    Re: Creating verilog define for filename based on input file path, string concatenati

    I would like to confirm #8 worked for me.

    Code Verilog - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    
    module x #(
    parameter sim_path = "string",
    ...
    )(
    ...
    )
     
    wire [128*8:0] stim;
    assign stim = {sim_path, "/test.txt"}
     
    initial begin
      if test==x begin .. end
      `include sample.vh
    end
     
    .........
    `define ORIGIN sim.path.to.module.x
    .......
    task TSK_OPENSTIM;
      integer data_file;
      begin
         data_file = $fopen(`ORIGIN.stim, "r");
      end
    endtask
    .....
     
    task TSK_READ_FILE
    integer scan_file;
    //blah
    begin
      while (!feof(`ORIGIN.TSK_OPENSTIM.data_file)) begin
     
    //magic



--[[ ]]--