Kuree / pysv

Running Python code in SystemVerilog
BSD 2-Clause "Simplified" License
62 stars 13 forks source link

Unable to cast Python instance of type <class 'list'> to C++ type 'int' #24

Closed ZhouXiaoLiu closed 10 months ago

ZhouXiaoLiu commented 1 year ago

error report:

Chronologic VCS simulator copyright 1991-2019
Contains Synopsys proprietary information.
Compiler version P-2019.06-SP1_Full64; Runtime version P-2019.06-SP1_Full64;  Aug 28 10:49 2023
terminate called after throwing an instance of 'pybind11::cast_error'
  what():  Unable to cast Python instance of type <class 'list'> to C++ type 'int'

Error-[DPI-UED] C++ Exception detected
  Import DPI routine invoked at file './array_pkg.sv'(line 30) has C++ 
  exceptions not caught. C++ exceptions shall not propagate out of any 
  imported subroutine.
  Fix it in DPI-C code before running simulation.

py code:

class Array:
    def __init__(self):
        # constructor without any extra argument is exported to SV directly
        self.__array = []

    @sv()
    def get_array(self):
        return self.__array

    @sv()
    def add_element(self, i):
        self.__array.append(i)

    @sv()
    def min(self):
        # call the numpy function
        return np.min(self.__array)

    @sv(return_type=DataType.Bit)
    def exists(self, value):
        return self.__exists(value)

    def __exists(self, value):
        # this function is not exposed to SystemVerilog
        return value in self.__value

if __name__ == "__main__":
    test_generate_c_header()

    # compile the code into a shared library for DPI to load
    # build the lib inside the ./build folder
    # lib_path is the path to the shared library file
    lib_path = compile_lib([Array], cwd="build")
    # generate SV bindings
    generate_sv_binding([Array], filename="array_pkg.sv", pkg_name="demo")

sv code:

module top;
// import from the pysv package
import demo::*;
Array a;

initial begin
    a = new();
    a.add_element(12);
    $display($sformatf("array=%p", a.get_array()));
end
endmodule
ZhouXiaoLiu commented 1 year ago

array_pkg:

`ifndef PYSV_DEMO
`define PYSV_DEMO
package demo;
import "DPI-C" function chandle Array_pysv_init();
import "DPI-C" function void Array_add_element(input chandle self,
                                               input int i);
import "DPI-C" function void Array_destroy(input chandle self);
import "DPI-C" function bit Array_exists(input chandle self,
                                         input int value);
import "DPI-C" function int Array_get_array(input chandle self);
import "DPI-C" function int Array_min(input chandle self);
import "DPI-C" function void pysv_finalize();
class PySVObject;
chandle pysv_ptr;
endclass
class Array extends PySVObject;
  function new();
    pysv_ptr = Array_pysv_init();
  endfunction
  function void add_element(input int i);
    Array_add_element(pysv_ptr, i);
  endfunction
  function void destroy();
    Array_destroy(pysv_ptr);
  endfunction
  function bit exists(input int value);
    return Array_exists(pysv_ptr, value);
  endfunction
  function int get_array();
    return Array_get_array(pysv_ptr);
  endfunction
  function int min();
    return Array_min(pysv_ptr);
  endfunction
endclass
endpackage
`endif // PYSV_DEMO
Kuree commented 10 months ago

You're trying to return a python array to SV, which is not supported due to memory allocation representation compatibility issues. As of v0.3.0, SV array is supported as function arg. Please use it instead.