Kuree / pysv

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

return type = list of int ? also input as list of int ? #20

Closed shobhitkapoor closed 10 months ago

shobhitkapoor commented 1 year ago

Hi

I am trying to use your library , can we use list on python and dynamic array from SV side ? if yes then please guide how can I do it ?

Thanks Shobhit

LIU-Yinyi commented 1 year ago

Hi there,

Inspired by this wonderful project, I develop another sibling project that supports dynamic array pysv-numpy. Check to see if it meets your requirements.

Kuree commented 1 year ago

Sorry for the late response. For some reason I didn't get the notification.

Open array is on the roadmap and I didn't have enough bandwidth to add support for it. It should be fairly straight to support since we can use pybind's buffer protocol. I will try to get this feature done as soon as possible.

IDjamKE commented 1 year ago

Online reminder Open array function

hmomkargit commented 1 year ago

Hi, I am looking forward for the same update. It would be great if we could pass an array from python or sv and vice-versa.

FpgaJohn commented 10 months ago

If anybody is still watching, you can make your own 'array' type and pass that in, populate it inside your Python code and use the results:

class MyList:
    @sv()
    def __init__(self):
        self._data = []

    @sv()
    def get_idx(self, idx):
        return self._data[idx]

    @sv
    def set_idx(self, idx, value):
        if len(self._data) > idx:
            self._data[idx] = value

    @sv()
    def append(self, value):
        self._data.append(value)

    @sv()
    def get_length(self):
        return len(self._data)

    def from_bytearray(self, in_byte_array):
        self._data = in_byte_array

@sv(out_list=MyList, return_type=DataType.Int)
def my_function(out_list: MyList) -> int:
    print(f'out_list.get_idx(100): {out_list.get_idx(0)}')
    out_list.set_idx(10, 100)
    ...
Kuree commented 10 months ago

Added as of v0.3.0. It uses memoryview from pybind, which is a non-copy mutable view of the underlying array. As a result, returning an array allocated from the python is not supported (please change it to the function input arg).