constraint_array - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

204


88
 
1
//----------------------------------------------------------------------
2
//  Copyright (c) 2018 by Doulos Ltd.
3
//
4
//  Licensed under the Apache License, Version 2.0 (the "License");
5
//  you may not use this file except in compliance with the License.
6
//  You may obtain a copy of the License at
7
//
8
//  http://www.apache.org/licenses/LICENSE-2.0
9
//
10
//  Unless required by applicable law or agreed to in writing, software
11
//  distributed under the License is distributed on an "AS IS" BASIS,
12
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
//  See the License for the specific language governing permissions and
14
//  limitations under the License.
15
//----------------------------------------------------------------------
16
17
package my_package;
18
19
class Base;
20
  // Properties
21
  rand bit parity_ok;
22
  rand bit [4:0] a, b, c, i, len;
23
  rand bit [1:0] aa, bb;
24
  typedef enum {SMALL, MEDIUM, BIG} size_t;
25
  rand size_t size;
26
  rand int test;
27
  int test_range[] = {1,2,3,5,6,9};
28
  
29
  rand int test1;
30
  int test1_range[] = {55,44,33,22};
31
  
32
  constraint c1 {a == 4;}
33
  constraint c2 {b > 3; c > 10;}
34
  constraint valid_parity {parity_ok dist {0:=1, 1:=9};}
35
  constraint c3 {size == BIG -> len > 20;}
36
  constraint c4 {i inside {1,[2:3]};} // Equiv. to i==1 || i==2 || i==3
37
  constraint c5 {solve b,c before a;}
38
  constraint c6 {unique{a,b,c};} // (a != b) && (b != c) && (c != a)
39
  constraint c7 {soft aa[0]==0; soft bb[0]==1;} // lower priority
40
  
41
  constraint c8 {
42
    test  == get_the_value(test_range);
43
  }
44
  
45
  constraint c9 {
46
    test1  == get_the_value(test1_range);
47
  }
48
  
49
  function int get_the_value(int range[]);
50
    
51
    randcase
52
      1: get_the_value = 0;
53
      1: begin
54
            int i = $urandom_range(0, range.size()-1);
55
            get_the_value = range[i];
56
          end
57
      1: get_the_value = 100;
58
    endcase
59
    
60
  endfunction
61
  
62
endclass
63
64
endpackage
65
66
67
module GRG_constraint;
68
  import my_package::*;
69
70
  Base  B_h;
71
72
initial begin
73
  B_h = new;
74
  for (int j = 0; j < 15; j++)
75
  begin
76
    if (B_h.randomize() with {soft aa[0]==1; bb[0]==0;}) // higher priority
77
      //$display("a = %2d, b = %2d, c = %2d, parity_ok = %0d, size = %6s, len = %2d, i = %2d, aa = %b, bb = %b",
78
       //B_h.a, B_h.b, B_h.c, B_h.parity_ok, B_h.size.name(), B_h.len, B_h.i, B_h.aa, B_h.bb);
79
      $display("test = %2d, test1 = %2d", B_h.test, B_h.test1);
80
              
81
    else
82
      $error("Failed to randomize Base");
83
    #10;
84
  end
85
end
86
87
endmodule
88
1
 
1
64 views and 0 likes     
 
Constraints are used to determine the values of random variables. Constraint blocks are class members.
This feature was added in SystemVerilog 2005.
**soft** constraints were added in SystemVerilog 2012.

Constraints are used to determine the values of random variables. Constraint blocks are class members.

This feature was added in SystemVerilog 2005.

soft constraints were added in SystemVerilog 2012.

5290:0