Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting a list element from a list of bit-vectors returns an int #172

Closed
grahamcx opened this issue Jan 27, 2023 · 3 comments
Closed

Selecting a list element from a list of bit-vectors returns an int #172

grahamcx opened this issue Jan 27, 2023 · 3 comments

Comments

@grahamcx
Copy link

grahamcx commented Jan 27, 2023

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

@mballance
Copy link
Member

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

mballance added a commit that referenced this issue Jan 28, 2023
- (#172) Change list subscript to return a ValueInt such that the
   value returned by a list subscript can, itself, be subscripted

Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
@mballance
Copy link
Member

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:

def test_array_elem_bitselect(self):
list_of_blocks = vsc.list_t(vsc.bit_t(64), 64)
block = vsc.bit_t(64)
for i in range(64):
list_of_blocks[i] = (1 << i)
for i in range(64):
self.assertEqual(list_of_blocks[i], (1 << i))
for i in range(64):
for j in range(64):
if (i == j):
self.assertEqual(list_of_blocks[i][j], 1)
else:
self.assertEqual(list_of_blocks[i][j], 0)

The 0.8.2 release contains the fix, and should be available on PyPi within a few minutes. Thanks again for the issue report!

@grahamcx
Copy link
Author

Thanks for the speedy fix @mballance, that's great news. I'll give that a try.

Graham

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants