我需要在我的系统verilog代码中打开一个文件,我需要知道它的相对路径才能使用$ fopen。首先,我需要知道我的立场。有没有知道我当前的目录路径? (通过使用$ display或者其他东西)
基于另一个问题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")
。
SystemVerilog语言中没有任何内容可以直接为您提供此信息。最简单的方法是使用$value$plusargs
检索调用模拟器时从脚本中提供的命令行选项(例如+ current_directory = pathname)。
其他选择包括:
只是为了完成,这是一个如何从中提取路径的示例
`FILE
从该已知路径,用户可以创建到任何地方的相对路径。
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