Selecting a list element from a list of bit-vectors returns an int
grahamcx opened this issue · 3 comments
Hi @mballance
First up I'd like to say that pyvsc is amazing. Having tried python-based verification before, the main blocker that has stopped me moving away from systemverilog has always been decent constrained random generation, which pyvsc has totally solved.
However, I'm having an issue with lists of bit-vectors. Essentially, if I have a variable of type vsc.bit_t(x), I can pick out arbitrary bits or bit-ranges from that vector, just like I can in systemverilog. But if I create a list of elements of type vsc.bit_t(x), then select one of those elements, I get an int back instead, meaning I can no longer select indvidual bits any more.
list_of_blocks = vsc.list_t(vsc.bit_t(64), 16)
block = vsc_bit_t(64)
type(block) # - returns <class 'vsc.model.value_scalar.ValueInt'>
type(list_of_blocks[0]) # - returns <class 'int'>
block[3] # returns bit 3 of the 64b vector
list_of_blocks[0][3] # fails with: TypeError: 'int' object is not subscriptable
Looking through the code it seems the is_scalar flag is causing this behaviour, which I appreciate may be useful in some circumstances, but without a way to override it, is quite limiting in others.
Thanks,
Graham
Hi @grahamcx, Thanks for raising this issue, and very happy to hear that you're finding value in PyVSC.
Unfortunately, this example may be running into limits (or, at least, corner cases) of Python's operator overloading capabilities. When you index list_of_blocks to read an element, PyVSC currently returns the value of the element which, most of the time, we want to see as a Python int value. In this case, we want to be able to further slice it to get a specific bit or bits.
Let me play with this a bit. There still may be a way to get this to work as-is. If not, I should be better informed as to the options.
Best Regards,
Matthew
Hi @grahamcx, good news! This issue did end up being an operator-overloading issue. The subscript of the array returned the array element as an 'int' value. It should have returned a ValueInt object in order to allow the value to subsequently be subscripted. I made the change, and now an expanded version of your testcase runs:
pyvsc/ve/unit/test_partselect.py
Lines 61 to 77 in 743b6d3
The 0.8.2 release contains the fix, and should be available on PyPi within a few minutes. Thanks again for the issue report!
Thanks for the speedy fix @mballance, that's great news. I'll give that a try.
Graham