mciepluc / cocotb-coverage

Functional Coverage and Constrained Randomization Extensions for Cocotb
BSD 2-Clause "Simplified" License
100 stars 15 forks source link

Question:How to constrain two-dimensional arrays? #85

Open hardy-hq opened 1 year ago

hardy-hq commented 1 year ago

hello!I want to implement a constraint as follows,but it can't find a suitable solution

class test;
    rand bit [15:0] total_phynum;
    rand bit        vc_bond_en;
    rand bit [15:0] a1_en;
    rand bit [ 2:0] a2_en;
    rand bit [63:0] a3_en;

    constraints c1 {
        total_phynum <= 2000;
        if (vc_bond_en == 0) {
            32'b0 + a1_en.sum + a2_en.sum + a3_en.sum == total_phynum
        } else {
            32'b0 + a1_en.sum + a2_en.sum + a3_en.sum >= total_phynum
        }
    }
endclass

this is my py code using crv,but “ Could not resolve constraints!” error is reported after execution.

class RandomizedTrasaction(crv.Randomized):

    def __init__(self):
        crv.Randomized.__init__(self)
        # au4_en = [au4_en]
        # tu3_en = [tu3_en]
        m = 16
        n = 16
        self.total_phynum = 0
        self.vc_bond_en = 0
        self.a1_en = np.random.randint(2,size=(m, n))
        self.a2_en = np.random.randint(2,size=(m, n,3))
        self.a3_en = np.random.randint(2,size=(m, n,63))
        self.add_rand("total_phynum", list(range(2000))) # full 8-bit space
        self.add_rand("vc_bond_en", list(range(1))) # full 1-bit space
        # au4_en_list = [tuple(x) for x in self.au4_en]
        # self.add_rand("au4_en",au4_en_list)
        # self.add_rand("a3_en",self.a3_en)

        def c1(a1_en,a2_en,a3_en,total_phynum):
            if(self.vc_bond_en == 0):
                return np.sum(self.a1_en == 1) + np.sum(self.a2_en == 1) + np.sum(self.a3_en == 1) == self.total_phynum
            else:
                return np.sum(self.a1_en == 1) + np.sum(self.a2_en == 1) + np.sum(self.a3_en == 1) >= self.total_phynum

        self.add_constraint(c1)

Is there another way to write this to implement sv constraints?Thank you very much

mciepluc commented 1 year ago

Looks like you have two random variables here, but 'vc_bond_en' is always '0'. Variables 'a*n' are multidimensional arrays - please use just int as it is extremely difficult to debug.

hardy-hq commented 1 year ago

But I want to constrain the array sum, isn't there a way to do that?

mciepluc commented 1 year ago

Constraints works on random variables - you defined random variables only "total_phynum" and "vc_bond_en".

hardy-hq commented 1 year ago

I tried to add a random two-dimensional array (a1_en) to the constraint, but the domain of the add_rand function can only be a one-dimensional list?

        self.a1_en = 0
        self.add_rand("a1_en",np.random.randint(2,size=(4,4)))
mciepluc commented 1 year ago

Please have a look into documentation how to deal with such costraints: https://cocotb-coverage.readthedocs.io/en/latest/tutorials.html#randomization-order-and-performance-issues

mciepluc commented 9 months ago

@hardy-hq was your problem resolved?