$system output

Hi, all!
i want to call function $system(…) from my verilog code, like this:


string history;

$system("history 10");

eventually, i want the history of commands to be stored in

string history;

,
but, as writtten in standard,

When called as a function, it returns the return value of the call to system() with data type int.

Does anybody know, how can i obtain the result of command? I know, i can write c function, then transmit data i need from it using DPI, but is there a way to stay inside systemverilog language?

Thank you!

In reply to boryah:

Hi,

please try.


module test;
   
  function get_command_his();
    int    file_pointer;
    
    //Stores command history into file cmd_history
    void'($system("history 10 > cmd_history"));

    //Open the file cmd_history with read access
    file_pointer = $fopen("cmd_history","r");

    //assin the value from file to variable
    // You can store into queue or array.
    for(int i=0;i<10;i++) begin
      string str;
      void'($fscanf(file_pointer,"%s",str));
      $display("[%0d] Command = %s",i,str);
    end

    //close the file
    $fclose(file_pointer);
  endfunction
   
  initial begin
    get_command_his();
  end
endmodule

Regards,
Harsh

In reply to harsh pandya:

this is tricky and pretty cool decision:)
Thank you!

In reply to harsh pandya:

harsh, do you have an idea why calling this command

$system("history 10 > cmd_history")

from modelsim leads to creation of an empty file? when i run it in terminal, everything’s fine

In reply to boryah:
Hi,

I haven’t test this code through Linux machine.(as I do not have right now).
But I have checked similar kind of example with EDA.
Please check below link.it will help to resolve your issue.

Thanks

The “history” command is usually a shell builtin, not a standalone binary. So I imagine the command is failing. What’s the return value from the system call - I bet it’s negative indicating a failure.

Regards,
Mark

In reply to Mark Curry:

This example works with Questa on EDAPlayground. There are a number of issues here that could be tool/OS/shell specific. You may want to contact your tool vendor directly for support. This Mentor sponsored public forum is not for tool specific issues.