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

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

Open
hardy-hq opened this issue Jun 13, 2023 · 6 comments
Open

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

hardy-hq opened this issue Jun 13, 2023 · 6 comments
Labels
question Further information is requested

Comments

@hardy-hq
Copy link

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
Copy link
Owner

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
Copy link
Author

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

@mciepluc
Copy link
Owner

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

@hardy-hq
Copy link
Author

hardy-hq commented Jun 14, 2023

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
Copy link
Owner

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
Copy link
Owner

@hardy-hq was your problem resolved?

@mciepluc mciepluc added the question Further information is requested label Nov 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants