我如何才能知道系统verilog中的当前路径?

时间:2015-10-28 15:22:05

标签: fopen relative-path system-verilog

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

其他选项包括:

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

答案 2 :(得分:1)

仅供参考,这是有关如何从

提取路径的示例

`文件

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

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

相关问题