2

I need to open a file in my system verilog code , and I need to know its relative path to me in order to use $fopen. so first , I need to know where I stand . Is there a away to know my current directory path ? ( by using $display or something)

Adi
  • 81
  • 3
  • 4
  • 12

3 Answers3

5

Based on another questionhow-do-i-read-an-environment-variable-in-verilog-system-verilog:

import "DPI-C" function string getenv(input string env_name);

module top;
  initial begin
    $write("env = %s\n", {getenv("HOME"), "/FileName"});
  end
endmodule

The working directory should be in $PWD so for your question you could use getenv("PWD").

Community
  • 1
  • 1
Morgan
  • 19,934
  • 8
  • 58
  • 84
3

There is nothing in the SystemVerilog language to give you this information directly. The easiest thing to do is use $value$plusargs to retrieve a command line option (e.g. +current_directory=pathname) that you provide from the script when you invoke the simulator.

Other options include:

  • importing a DPI C routine that returns the current working directory. That routine would be OS specific.
  • Using a tool specific construct that gives you access to the simulation command line. ModelSim has a mti_fli package that you can import and it allows you to execute a Tcl command and get the result back as a string.
  • Using the
    `__ FILE__
    macro to get the path of the file it appears in as a string and somehow stripping that down to the directory path out are looking for.
dave_59
  • 39,096
  • 3
  • 24
  • 63
1

Just for completion, this is an example on how to extract the path from

`FILE

From that known path, then the user can probably create relative paths to anywhere.

path=get_path_from_file(`__FILE__);

function string get_path_from_file(string fullpath_filename);
    int i;
    int str_index;
    logic found_path;
    string ret="";


    for (i = fullpath_filename.len()-1; i>0; i=i-1) begin
        if (fullpath_filename[i] == "/") begin
            found_path=1;
            str_index=i;
            break;
        end
    end
    if (found_path==1) begin
        ret=fullpath_filename.substr(0,str_index);
    end else begin
       // `uvm_error("pve_get_path_from_file-1", $sformatf("Not found a valid path for this file: %s",fullpath_filename));
    end
        

    return ret;
endfunction

Another easy way is to use the systemverilog $system function. If you are in LINUX, then you can just do PWD write the output in a file and then read it back in systemverilog.

Below 3 functions to extract time, pwd and available space for Systemverilog using %system assuming LINUX

    static function string pve_get_systemtime(bit epoch = 0);
        int fd;
        string localtime;
        string cmd = "date";

        if(epoch)
            cmd = {cmd, " +%s"};

        void'($system({cmd, " > localtime.23025524522"})); // temp file
        fd = $fopen("localtime.23025524522", "r");
        void'($fgets (localtime, fd));

        if(localtime.substr(localtime.len()-1, localtime.len()-1) == "\n")
            localtime = localtime.substr(0, localtime.len()-2);

        $fclose(fd);
        void'($system("rm localtime.23025524522")); // delete file
        return localtime;
    endfunction


    static function string pve_get_systempwd();
        int fd;
        string pwd_str;
        string cmd = "pwd";

        void'($system({cmd, " > pwd.23025524522"})); // temp file
        fd = $fopen("pwd.23025524522", "r");
        void'($fgets (pwd_str, fd));


        if(pwd_str.substr(pwd_str.len()-1, pwd_str.len()-1) == "\n")
            pwd_str = pwd_str.substr(0, pwd_str.len()-2);

        $fclose(fd);
        void'($system("rm pwd.23025524522")); // delete file
        return pwd_str;
    endfunction

    static function string pve_get_systemfreespace(string on_current_path="");
        int fd;
        automatic int prevpos = 0;
        string freespace_str;
        string cmd = "df -kh --output=avail";

        cmd = {cmd, " ",on_current_path, " | grep -v Avail"};

        void'($system({cmd, " > availspace.23025524522"})); // temp file
        fd = $fopen("availspace.23025524522", "r");
        void'($fgets (freespace_str, fd));


        if(freespace_str.substr(freespace_str.len()-1, freespace_str.len()-1) == "\n")
            freespace_str = freespace_str.substr(0, freespace_str.len()-2);


        $fclose(fd);
        void'($system("rm availspace.23025524522")); // delete file
        return freespace_str;
    endfunction
Joniale
  • 515
  • 4
  • 17