SystemVerilog Struct To Python Script

Hello !

Wanted to pass a systemverilog struct to a python script to process some information.

  1. Is there a way we can directly pass the SV Struct and get parsed inside the Python script ?
  2. Or do we need to pass the SV struct to C/C++ Model via DPI call and then call the python script inside the C-Code ?

Please share your experience. Thanks !

In reply to desperadorocks:

If you want to call a Python script from SystemVerilog and don’t need the results back in SystemVerilog, you can use $system(“myPythonScript.py structAgs”);. You’ll need to format the structArgs as a recognizable tuple as there is no struct datatype in Python. The only way to get results back into SystemVerilog is through File I/O (Python script writes data to a file, SystemVerilog reads the file).

You can use SystemVerilog’s DPI to call C, and then use the Python’s C/C++ interface to call a Python script and set/get references to variables in the script so that you can exchange data between SystemVerilog and Python. See Python/C API Reference Manual — Python 3.11.4 documentation

In reply to dave_59:

Hello Dave !

Thanks a lot for your response…

  1. If its a simple structure then it might be easy to form the structArgs and pass as tuple format to the python script.
  2. But lets say if the struct is bit complex with more variables and sub-structs, then in that case what would you suggest ?

Essentially,

  1. I wanted to use this struct and generate an yaml file formatted output.
  2. And then again call a different python script which uses this yaml file for some data processing.
  3. So, thought pass this struct as input to a python script and then generate the yaml file output. And then do the step 2.

In reply to desperadorocks:

You could always write out a YAML file from SystemVerilog first; there’s no reason that has to be done in Python (although admittedly much easier)

You could also format your struct with the “%p” specifier. This is remarkably close to JSON format.

In reply to dave_59:

Hello Dave ! Thanks for the suggestions. Do you mean something like this with custom prints ?

Basic Example:


module hello_system;

int fd;

typedef struct {
    integer c;
    int b;
    string p;
} sub_sv_st;

typedef struct {
    int a;
    string s;
    sub_sv_st sst;
} sv_st;

    initial begin
        sub_sv_st s_d;
        sv_st     p_d;
        
        s_d.c = -20;
        s_d.b = 6;
        s_d.p = "panther";
        
        p_d.a = 3;
        p_d.s = "lion";
        p_d.sst = s_d;

        fd = $fopen("animal.yaml");
        
        $fdisplay(fd, "p_d:");
        $fdisplay(fd, " %0s: %0d", "a", p_d.a);
        $fdisplay(fd, " %0s: %0s", "s", p_d.s);
        $fdisplay(fd, " s_d:");
        $fdisplay(fd, "     %0s: %0d", "c", p_d.sst.c);
        $fdisplay(fd, "     %0s: %0d", "b", p_d.sst.b);
        $fdisplay(fd, "     %0s: %0s", "p", p_d.sst.p);
       

        $fclose(fd);
    end

endmodule: hello_system

Output:


p_d:
 a: 3
 s: lion
 s_d:
     c: -20
     b: 6
     p: panther

In reply to desperadorocks:
If that works for you, go for it!

In reply to dave_59:

Thanks Dave. But wondering if there is any better way of formatting allowed in SV? Because this is a very basic example, but the complexity of the struct might be even more with multiple sub-structs, strings, arrays etc.

Thinking if we can write some functions which takes in a struct as input and print all the fields of the struct recursively [and for sub-structs re-call the function] or any such ideal flow which you will follow if you have to write something like this. :-)

Any comments is highly appreciated. Thanks !

In reply to desperadorocks:
If you wrote

$fdisplay(fd, "p_d: %p",p_d);

You get:

p_d: '{a:3, s:"lion", sst:'{c:-20, b:6, p:"panther"}}

If you want any other kind of formatting, you need introspection. And you only get introspection by using the C-based VPI.

In reply to dave_59:

Thanks Dave for your inputs. Appreciate that.