How to read environment variable in Verilog / System Verilog?

How to read environment variable in Verilog? (Run on VCS simulator)

I'm trying to execute

File=$fopen("$PATH/FileName","r");

$ PATH is an environment variable.

+5
source share
3 answers

You can simply use SystemVerilog DPI to get the environment. And since it getenvis the standard C library for each POSIX platform, you do not need to use your equivalent function again getenv()to define the function.

Sample code in SV.

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

module top;

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

Launch

ncverilog -sv dpi.v

or

vcs -sverilog dpi.v

He will show

env = /home/user/FileName

, PATH - ":". , , "PATH". fopen "/bin:/usr/bin:/usr/local/bin/FileName", .

+12

PLI . - :

#include <stdlib.h>
#include <string.h>

#include "vpi_user.h"

PLI_INT32 pli_getenv (PLI_BYTE8 * arg) {

    vpiHandle tf_obj = vpi_handle (vpiSysTfCall, NULL);
    vpiHandle arg_iter = vpi_iterate (vpiArgument, tf_obj);

    vpiHandle arg1, arg2;
    arg1 = vpi_scan (arg_iter);
    arg2 = vpi_scan (arg_iter);

    s_vpi_value vi, vo;
    vi.format = vpiStringVal;
    vpi_get_value (arg2, &vi);

    vo.format = vpiStringVal;
    vo.value.str = strdup (getenv (vi.value.str));
    vpi_put_value (arg1, &vo, NULL, vpiNoDelay);

    return 0;
}

VCS , .

+2

Verilog

File = $fopen(`PATH_FILENAME, "r");

Makefile/shell script

$(SIM) -DPATH_FILENAME=\"$PATH/FileName\" blah.v ...

Icarus 'iverilog , vsim, , , .

, , . , :

File = $fopen("`PATH_FILENAME", "r");

...

`$(SIM) -DPATH_FILENAME=$PATH/FileName blah.v ...`
0

All Articles