pyvsc icon indicating copy to clipboard operation
pyvsc copied to clipboard

Cannot use random value as list index in a constraint

Open felixdube opened this issue 1 year ago • 2 comments

I am wondering if it is possible to use a random value as a list index inside a constraint. It does not seem to work as I expect. Here is a minimal example, which produce the following error: TypeError: list indices must be integers or slices, not rand_uint32_t

import vsc

@vsc.randobj
class my_item():
    def __init__(self):
        self.a = vsc.rand_uint32_t()
        self.b = vsc.rand_uint32_t()
        self.c = [2,4,6,8]        

    @vsc.constraint
    def ab_c(self):
        vsc.solve_order(self.a, self.b)
        self.a < len(self.c)
        self.b < self.c[self.a]	

item = my_item()

I have found a workaround, but it might not be very efficient depending on the size of the list.

    @vsc.constraint
    def ab_c(self):
        vsc.solve_order(self.a, self.b)
        self.a < len(self.c)
        for i in range(len(self.c)):
            with vsc.if_then(self.a == i):
                self.b < self.c[i]

felixdube avatar Feb 16 '23 20:02 felixdube