
Constraints
In last page we saw how to generate radom values. This random values if total random does not do much good in verification. To control the value range that a variable takes we constrain it.
This is what we do in Specman, VERA and SystemVerilog. SystemC also provids ways to constrain the random values generated.
A constraint is derived from the scv_constraint_base class; the data to be randomized is specified as scv_smart_ptr class variable(s). The basic components of an expression can be created from scv_smart_ptr objects through operator()(), which can then be composed into more complicated expressions by using the following operators:
- Arithmetic operators +, -, *
- Relational operators ==, !=, >, >=, <, <=
- Logical operators !, &&, ||
In general, operator() is used to create expressions that can be analyzed or evaluated at the later point. Both the scv_extensions classes and the scv_smart_ptr templates from the data introspection facility implement this operator.
Basic Constraints
Basic Constrains are constructed with operators show above. SCV provides macros like SCV_CONSTRAINT_CTOR and SCV_CONSTRAINT for constraining a data object. There are two types of constaints.
- Hard Constraints : This is are hard rules, which should be always true when a object is randomized. Hard Constraints can not be bypassed.
- Soft Constraints : This are soft rules, this can be bypassed with Hard Constraints
We can have have multiple contraints and then select the contraint to use. This can be done with using use_constraint() method.
There are other basic constraints types which allow to specify range. You can use keep_only/keep_out only if the element you are constraining is NOT involved in SCV_CONSTRAINTS in any way.
- keep_only : This constraint is used for specifying range in which the random value should be generated.
- keep_out : This constraint is used for specifying range in which the random value should not be generated.
Example : Constraints
1 #include
2
3 struct packet_t {
4 sc_uint<32> src_addr;
5 sc_uint<32> dest_addr;
6 sc_uint<16> length;
7 };
8 // nbcode "packet" end
9
10 //define an ostream for a packet object
11 ostream& operator<<(ostream& os, const packet_t& p) {
12 os << " src_addr : " << p.src_addr << "\n"
13 << " dest_addr: " << p.dest_addr << "\n"
14 << " length : " << p.length << endl;
15 return os;
16 }
17
18 // Create packet extension
19 template
20 class scv_extensions : public scv_extensions_base {
21 public:
22 scv_extensions<sc_uint<32> > src_addr;
23 scv_extensions<sc_uint<32> > dest_addr;
24 scv_extensions<sc_uint<16> > length;
25
26 SCV_EXTENSIONS_CTOR(packet_t) {
27 //must be in order
28 SCV_FIELD(src_addr);
29 SCV_FIELD(dest_addr);
30 SCV_FIELD(length);
31 }
32 };
33
34 //Create a basic default constraint for the packet generator
35 struct packet_base_constraint : public scv_constraint_base {
36 //create a packet object
37 scv_smart_ptr packet;
38 //put the base constraints on the packet variables
39 SCV_CONSTRAINT_CTOR(packet_base_constraint) {
40 // Soft Constraint
41 SCV_SOFT_CONSTRAINT ( packet->length() < 1500 ); // Max Frame Size
42 SCV_SOFT_CONSTRAINT ( packet->length() > 64 ); // Mix Frame Size
43 // Hard Constraint
44 SCV_CONSTRAINT ( packet->src_addr() ! = packet->dest_addr());
45 // Hard limit on min frame size
46 SCV_CONSTRAINT ( packet->length() > 20 );
47 }
48 };
49
50 // Create a actual contraint for the testcase
51 struct packet_basic_constraint : public packet_base_constraint {
52 //add config variable
53 scv_smart_ptr<sc_uint<32> > dest_min;
54 scv_smart_ptr<sc_uint<32> > dest_max;
55
56 SCV_CONSTRAINT_CTOR(packet_basic_constraint) {
57 //use the base constraint
58 SCV_BASE_CONSTRAINT(packet_base_constraint);
59 //add extra constraints
60 SCV_CONSTRAINT ((packet->dest_addr() > dest_min()) &&
61 (packet->dest_addr() < dest_max()) );
62 SCV_CONSTRAINT (
63 ((packet->src_addr() > (packet->dest_addr() + 0x100000) ) &&
64 (packet->src_addr() < (packet->dest_addr() + 0x200000) ) ) ||
65 ((packet->src_addr() < (packet->dest_addr() - 0x10000) )) &&
66 (packet->src_addr() > (packet->dest_addr() - 0xfffff) ) );
67 SCV_CONSTRAINT ( packet->length() == 64 );
68 }
69 };
70
71 int sc_main(int argc, char** argv) {
72 // Set the Seed to 1
73 scv_random::set_global_seed(1);
74 //instatiate test specific constraints
75 packet_basic_constraint pkt("Constrained Packet");
76 // Disable randomization
77 pkt.dest_min->disable_randomization();
78 pkt.dest_max->disable_randomization();
79 // Set the values manually
80 *pkt.dest_min = 0x100000;
81 *pkt.dest_max = 0x800000;
82 for(int i=0; i<5; ++i) {
83 pkt.next();
84 cout << pkt.packet->get_name() << *(pkt.packet) << endl;
85 }
86 cout << endl;
87 return (0);
88 }
Simulation Output : Constraints
src_addr : 6975369
dest_addr: 5279859
length : 64
src_addr : 4482536
dest_addr: 3148491
length : 64
src_addr : 8796910
dest_addr: 7006346
length : 64
src_addr : 7331724
dest_addr: 7924165
length : 64
src_addr : 5611803
dest_addr: 3737188
length : 64
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn