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

set attributes' rand_mode in pre_randomize does not work #136

Open
qijiuliushisan opened this issue Dec 22, 2021 · 1 comment
Open

set attributes' rand_mode in pre_randomize does not work #136

qijiuliushisan opened this issue Dec 22, 2021 · 1 comment

Comments

@qijiuliushisan
Copy link

import vsc

@vsc.randobj
class A():
    def __init__(self):
        self.a=vsc.rand_bit_t(16)
        self.b=vsc.rand_bit_t(20)
    
    def pre_randomize(self):
        with vsc.raw_mode():
            self.a.rand_mode=False
obj=A()
obj.randomize()
print(obj.a,obj.b)

obj.a is expected to be 0. But the output showed that obj.a had been randomized.

@alwilson
Copy link
Contributor

alwilson commented Feb 22, 2023

Before pre_randomize() is called set_used_rand() is called on each field_model. This sets is_used_rand on each field depending on rand_mode and other factors. Disabling rand_mode in pre_randomize() doesn't change the is_used_rand field, which is the field that controls field randomization later.

Setting rand_mode in raw_mode() in turn calls the rand_mode() setter function. One possible fix would be to set is_used_rand here as well, which would probably do the right thing if setting rand_mode to False, but could enable a disabled field if rand_mode gets set to True? We could also allow only setting it to False here to avoid the False->True scenario.

pyvsc/src/vsc/types.py

Lines 414 to 416 in de14431

@rand_mode.setter
def rand_mode(self, v):
self._int_field_info.model.rand_mode = bool(v)

Another option is to call set_used_rand() again after call pre_randomize() to update is_used_rand accordingly. Maybe that's cleaner?

        # First, invoke pre_randomize on all elements
        for fm in field_model_l:
            fm.pre_randomize()
            f.set_used_rand(True, 0)

@mballance Any preference or insight? I can fix it up and add a testcase for it.

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