fvutils / pyvsc

Python packages providing a library for Verification Stimulus and Coverage
https://fvutils.github.io/pyvsc
Apache License 2.0
113 stars 26 forks source link

Issue with randselect #106

Closed aneels3 closed 3 years ago

aneels3 commented 3 years ago

Hi @mballance I am trying to implement randselect case like below

import vsc

@vsc.randobj
class my_class:
    def __init__(self):
        self.value = vsc.rand_bit_t(32)
        self.randselect = self.get_val()

    def task(self, a, b):
        with vsc.randomize_with(self.value):
            self.value in vsc.rangelist(a, b)

    def get_val(self):
        vsc.randselect([
        (1, lambda: self.task(hex(0x12345678), hex(0x9abcdef0))),
                (0, lambda: self.task(hex(0x232456ab), hex(0x00000000)))])

obj = my_class()
print("self.randselect", obj.value)

I am getting the below error while executing

Traceback (most recent call last):
  File "rand_select.py", line 19, in <module>
    obj = my_class()
  File "/home/anil/.local/lib/python3.6/site-packages/vsc/rand_obj.py", line 60, in __init__
    super().__init__(*args, **kwargs)
  File "rand_select.py", line 7, in __init__
    self.randselect = self.get_val()
  File "rand_select.py", line 16, in get_val
    (0, lambda: self.task(hex(0x232456ab), hex(0x00000000)))])
  File "/home/anil/.local/lib/python3.6/site-packages/vsc/methods.py", line 134, in randselect
    sel_l[idx][1]()
  File "rand_select.py", line 15, in <lambda>
    (1, lambda: self.task(hex(0x12345678), hex(0x9abcdef0))),
  File "rand_select.py", line 10, in task
    with vsc.randomize_with(self.value):
  File "/home/anil/.local/lib/python3.6/site-packages/vsc/methods.py", line 46, in randomize_with
    raise Exception("Parameter \"" + str(v) + " to randomize is not a vsc object")
Exception: Parameter "0 to randomize is not a vsc object

Can you look into it?

Thanks and regards, Anil

aneels3 commented 3 years ago

Hi @mballance Did you get a chance to look into this?

mballance commented 3 years ago

Hi @aneels3, Just got a chance. The issue results because of the operator overloading that PyVSC uses to make it easy to get values from scalar fields in the user's Python code. When 'randomize_with' is called, it is passed the value of the 'value' field, and not a reference to the field object. There are two things you can do here:

Randomize the containing object instead:

    with vsc.randomize_with(self):
        self.value in ...

Surround the randomize call with a 'raw_mode()' to turn off the operator overloading

    with vsc.raw_mode():
        with vsc.randomize_with(self.value):
            ...

Please note that you'll need to remove the 'hex' calls in the calls to 'task'. These convert the integer to a string, causing a different failure.

Best Regards, Matthew

aneels3 commented 3 years ago

Hi @mballance This is working fine now. Thanks for the help.