def randomize(*args, **kwargs): """Randomize a list of variables""" frame = inspect.stack()[1] fields = [] for v in args: if hasattr(v, "get_model"): fields.append(v.get_model()) else: raise Exception("Parameter \"" + str(v) + " to randomize is not a vsc object") debug = 0 if "debug" in kwargs.keys(): debug = kwargs["debug"] solve_fail_debug = False if "solve_fail_debug" in kwargs: solve_fail_debug = kwargs["solve_fail_debug"] randstate = None if "randstate" in kwargs: randstate = kwargs["randstate"] else: randstate = RandState(random.randint(0, 0xFFFFFFFF)) Randomizer.do_randomize(randstate, SourceInfo(frame.filename, frame.lineno), fields, solve_fail_debug=solve_fail_debug, debug=debug)
def test_incr(self): obj = FieldCompositeModel("obj") arr = obj.add_field( FieldArrayModel( "arr", None, # type_t True, # is_scalar None, # not an enum-type list 32, False, True, False)) for i in range(10): arr.add_field() obj.add_constraint( ConstraintBlockModel("XX", [ ConstraintExprModel( ExprBinModel(ExprFieldRefModel(arr.size), BinExprType.Eq, ExprLiteralModel(10, False, 32))) ])) foreach = ConstraintForeachModel(ExprFieldRefModel(arr)) foreach.addConstraint( ConstraintImpliesModel( ExprBinModel(ExprFieldRefModel(foreach.index), BinExprType.Gt, ExprLiteralModel(0, False, 32)), [ ConstraintExprModel( ExprBinModel( ExprArraySubscriptModel( ExprFieldRefModel(arr), ExprFieldRefModel(foreach.index)), BinExprType.Eq, ExprBinModel( ExprArraySubscriptModel( ExprFieldRefModel(arr), ExprBinModel( ExprFieldRefModel(foreach.index), BinExprType.Sub, ExprLiteralModel(1, False, 32))), BinExprType.Add, ExprLiteralModel( 1, False, 32)))) ])) obj.add_constraint(ConstraintBlockModel("c", [foreach])) # print("Object: " + ModelPrettyPrinter.print(obj)) # # constraints = ArrayConstraintBuilder.build(obj) # for c in constraints: # print("Constraint: " + ModelPrettyPrinter.print(c)) # print("Object(1): " + ModelPrettyPrinter.print(obj)) # # ConstraintOverrideRollbackVisitor.rollback(obj) # print("Object(2): " + ModelPrettyPrinter.print(obj)) Randomizer.do_randomize([obj]) for f in arr.field_l: print("" + f.name + ": " + str(int(f.get_val())))
def __exit__(self, t, v, tb): c = pop_constraint_scope() leave_expr_mode() try: Randomizer.do_randomize(self.field_l, [c]) except SolveFailure as e: if _solve_fail_debug or self.solve_fail_debug: print("Solve Failure") raise e
def randomize(*args): """Randomize a list of variables""" fields = [] for v in args: if hasattr(v, "get_model"): fields.append(v.get_model()) else: raise Exception("Parameter \"" + str(v) + " to randomize is not a vsc object") Randomizer.do_randomize(fields)
def test_cross(self): stim = FieldCompositeModel("stim", True) f = FieldScalarModel("a", 16, False, True) stim.add_field(f) f2 = FieldScalarModel("b", 16, False, True) stim.add_field(f2) cg = CovergroupModel("cg") cp = CoverpointModel(ExprFieldRefModel(f), "cp1", CoverageOptionsModel()) cg.add_coverpoint(cp) bn = CoverpointBinArrayModel("cp", 0, 1, 16) cp.add_bin_model(bn) cp2 = CoverpointModel(ExprFieldRefModel(f2), "cp2", CoverageOptionsModel()) cg.add_coverpoint(cp2) bn = CoverpointBinArrayModel("cp", 0, 1, 16) cp2.add_bin_model(bn) cr = CoverpointCrossModel("aXb", CoverageOptionsModel()) cr.add_coverpoint(cp) cr.add_coverpoint(cp2) cg.add_coverpoint(cr) gen = GeneratorModel("top") gen.add_field(stim) gen.add_covergroup(cg) gen.finalize() # Need a special randomizer to deal with generators r = Randomizer() count = 0 for i in range(1000): r.do_randomize([gen]) cg.sample() count += 1 cov = cg.get_coverage() print("Coverage: (" + str(i) + ") " + str(cov)) if cov == 100: break self.assertEqual(cg.get_coverage(), 100) # Ensure that we converge relatively quickly self.assertLessEqual(count, (256+16+16))
def __exit__(self, t, v, tb): frame = inspect.stack()[1] c = pop_constraint_scope() leave_expr_mode() try: Randomizer.do_randomize(self.randstate, SourceInfo(frame.filename, frame.lineno), self.field_l, [c], debug=debug) except SolveFailure as e: if _solve_fail_debug or self.solve_fail_debug: print("Solve Failure") raise e
def test_smoke(self): obj = FieldCompositeModel("obj") a = obj.add_field(FieldScalarModel("a", 8, False, True)) b = obj.add_field(FieldScalarModel("a", 8, False, True)) obj.add_constraint( ConstraintBlockModel("c", [ ConstraintExprModel( ExprBinModel(a.expr(), BinExprType.Lt, b.expr())) ])) rand = Randomizer(RandState(0)) randstate = RandState(0) rand.do_randomize(randstate, SourceInfo("", -1), [obj]) self.assertLess(a.val, b.val)
def test_coverpoint_bins(self): stim = FieldCompositeModel("stim", True) f = FieldScalarModel("a", 16, False, True) stim.add_field(f) f2 = FieldScalarModel("b", 16, False, True) stim.add_field(f2) cg = CovergroupModel("cg") cp = CoverpointModel(ExprFieldRefModel(f), "cp1", CoverageOptionsModel()) cg.add_coverpoint(cp) cp.add_bin_model(CoverpointBinArrayModel("bn1", 0, 1, 16)) cp.add_bin_model(CoverpointBinCollectionModel.mk_collection("bn2", RangelistModel([ [17,65535-16-1] ]), 16)) cp.add_bin_model(CoverpointBinArrayModel("bn3", 0, 65535-16, 65535)) cp2 = CoverpointModel(ExprFieldRefModel(f2), "cp2", CoverageOptionsModel()) cg.add_coverpoint(cp2) bn = CoverpointBinArrayModel("cp", 0, 1, 16) cp2.add_bin_model(bn) gen = GeneratorModel("top") gen.add_field(stim) gen.add_covergroup(cg) gen.finalize() # Need a special randomizer to deal with generators r = Randomizer() count = 0 for i in range(1000): r.do_randomize([gen]) cg.sample() count += 1 cov = cg.get_coverage() if cov == 100: break self.assertEqual(cg.get_coverage(), 100) # Ensure that we converge relatively quickly self.assertLessEqual(count, 64)
def test_smoke(self): obj = FieldCompositeModel("obj") arr = obj.add_field( FieldArrayModel( "arr", None, # type_t True, None, # not an enum-type 32, False, True, True)) # for i in range(10): # arr.add_field() obj.add_constraint( ConstraintBlockModel("XX", [ ConstraintExprModel( ExprBinModel(ExprFieldRefModel(arr.size), BinExprType.Eq, ExprLiteralModel(10, False, 32))) ])) foreach = ConstraintForeachModel(ExprFieldRefModel(arr)) foreach.addConstraint( ConstraintExprModel( ExprBinModel( ExprArraySubscriptModel(ExprFieldRefModel(arr), ExprFieldRefModel(foreach.index)), BinExprType.Lt, ExprLiteralModel(10, False, 32)))) obj.add_constraint(ConstraintBlockModel("c", [foreach])) # print("Object: " + ModelPrettyPrinter.print(obj)) # # constraints = ArrayConstraintBuilder.build(obj) # for c in constraints: # print("Constraint: " + ModelPrettyPrinter.print(c)) # print("Object(1): " + ModelPrettyPrinter.print(obj)) # # ConstraintOverrideRollbackVisitor.rollback(obj) # print("Object(2): " + ModelPrettyPrinter.print(obj)) randstate = RandState(0) Randomizer.do_randomize(randstate, SourceInfo("", -1), [obj]) for f in arr.field_l: print("" + f.name + ": " + str(int(f.get_val())))
def test_wide_var(self): obj = FieldCompositeModel("obj") a = obj.add_field(FieldScalarModel("a", 1024, False, True)) obj.add_constraint( ConstraintBlockModel("c", [ ConstraintExprModel( ExprBinModel( a.expr(), BinExprType.Gt, ExprLiteralModel(0x80000000000000000, False, 72))) ])) randstate = RandState(0) rand = Randomizer(randstate) rand.do_randomize(randstate, SourceInfo("", -1), [obj]) print("a=" + hex(int(a.val))) self.assertGreater(a.val, ValueScalar(0x80000000000000000))
def test_smoke(self): stim = FieldCompositeModel("stim", True) f = FieldScalarModel("a", 16, False, True) stim.add_field(f) f2 = FieldScalarModel("b", 16, False, True) stim.add_field(f2) cg = CovergroupModel("cg") cp = CoverpointModel(ExprFieldRefModel(f), "cp1", CoverageOptionsModel()) cg.add_coverpoint(cp) bn = CoverpointBinArrayModel("cp", 1, 16) cp.add_bin_model(bn) cp2 = CoverpointModel(ExprFieldRefModel(f2), "cp2", CoverageOptionsModel()) cg.add_coverpoint(cp2) bn = CoverpointBinArrayModel("cp", 1, 16) cp2.add_bin_model(bn) gen = GeneratorModel("top") gen.add_field(stim) gen.add_covergroup(cg) gen.finalize() # Need a special randomizer to deal with generators r = Randomizer(RandState(0)) randstate = RandState(0) count = 0 for i in range(1000): r.do_randomize(randstate, SourceInfo("", -1), [gen]) cg.sample() count += 1 cov = cg.get_coverage() if cov == 100: break self.assertEqual(cg.get_coverage(), 100) # Ensure that we converge relatively quickly self.assertLessEqual(count, 32)
def __exit__(self, t, v, tb): c = pop_constraint_scope() leave_expr_mode() Randomizer.do_randomize(self.field_l, [c])