【问题标题】:how can I know my current path in system verilog?我如何知道我在系统 verilog 中的当前路径?
【发布时间】:2015-10-28 15:22:04
【问题描述】:

我需要在我的系统 verilog 代码中打开一个文件,并且我需要知道它的相对路径才能使用 $fopen。 所以首先,我需要知道我的立场。 有没有办法知道我当前的目录路径? (通过使用 $display 或其他东西)

【问题讨论】:

    标签: fopen relative-path system-verilog


    【解决方案1】:

    基于另一个问题how-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
    

    工作目录应位于 $PWD 中,因此对于您的问题,您可以使用 getenv("PWD")

    【讨论】:

      【解决方案2】:

      SystemVerilog 语言中没有任何内容可以直接为您提供此信息。最简单的做法是使用 $value$plusargs 检索您在调用模拟器时从脚本中提供的命令行选项(例如 +current_directory=pathname)。

      其他选项包括:

      • 导入返回当前工作目录的 DPI C 例程。 该例程将是特定于操作系统的。
      • 使用工具特定的结构,让您可以访问模拟命令行。 ModelSim 有一个可以导入的 mti_fli 包,它允许您执行 Tcl 命令并将结果作为字符串返回。
      • 使用
        `__ FILE__
        宏来获取它作为字符串出现的文件的路径,并以某种方式将其剥离到正在寻找的目录路径。

      【讨论】:

        【解决方案3】:

        只是为了完成,这是一个关于如何从中提取路径的示例

        `文件

        从该已知路径,用户可能可以创建到任何地方的相对路径。

        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

        另一个简单的方法是使用 systemverilog $system 函数。 如果你在 LINUX 中,那么你可以做 PWD 将输出写入一个文件,然后在 systemverilog 中读回。

        使用 %system 假设 LINUX 提取 Systemverilog 的时间、密码和可用空间的以下 3 个函数

            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
        

        【讨论】:

          猜你喜欢
          • 2010-10-25
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 1970-01-01
          • 1970-01-01
          • 2012-11-29
          • 1970-01-01
          • 2011-06-27
          相关资源
          最近更新 更多