|
|
|
|
|
|
|
|
|
|
|
|
Weighted Randomization
|
|
|
While a constraint specifies the range of legal values, a weight specification biases the random value generation process so that some values are generated more often than others. |
|
|
|
|
|
Distributions are specified using the concept of a bag, which represents either a collection of weighted values or a collection of weighted ranges (i.e., a bag of pairs). A bag is similar to a set, except that it can contain duplicated elements. |
|
|
|
|
|
Weighted randomization is done with class scv_bag. It contains following methods |
|
|
|
|
|
- remove() : This method removes one or all of the objects with the same value as the argument.
- add() : This method adds one specified value to the bag.
- push() : This method adds the specified number of objects with the specified value to the bag.
- set_mode() : This method is used for assigning the bag to the data to be randomized.
|
|
|
|
|
|
"scv_bag <pair <int, int> > variable" is used for creating range based weighted distribution. |
|
|
|
|
|
|
|
|
|
|
|
Example : Weighted Randomization
|
|
|
|
|
|
1 #include <scv.h>
2
3 enum packet_types_t { NORMAL, RUNT, OVERSIZE };
4
5 class packet {
6 public:
7 packet_types_t ptype;
8 sc_uint<48> src;
9 sc_uint<48> dest;
10 sc_uint<16> length;
11
12 public:
13 packet();
14 packet(const packet & src);
15 const packet & operator=(const packet & src);
16 void dump(void) const;
17 }; // class packet ..
18
19 // --------------------------------------------------------------
20 // ENUM Extension
21 SCV_ENUM_EXTENSIONS(packet_types_t) {
22 public:
23 SCV_ENUM_CTOR(packet_types_t) {
24 SCV_ENUM(NORMAL);
25 SCV_ENUM(RUNT);
26 SCV_ENUM(OVERSIZE);
27 };
28 };
29 // Packet Extension
30 SCV_EXTENSIONS(packet) {
31 public:
32 scv_extensions< packet_types_t > ptype;
33 scv_extensions< sc_uint<48> > src;
34 scv_extensions< sc_uint<48> > dest;
35 scv_extensions< sc_uint<16> > length;
36
37 SCV_EXTENSIONS_CTOR(packet) {
38 SCV_FIELD(ptype);
39 SCV_FIELD(src);
40 SCV_FIELD(dest);
41 SCV_FIELD(length);
42 }
43 };
44
45 // --------------------------------------------------------------
46 // constructors & copy assignment
47 packet::packet() {
48 ptype = NORMAL;
49 };
50
51 packet::packet(const packet & p) : ptype(p.ptype), src(p.src), dest(p.dest) { };
52
53 const packet & packet::operator=(const packet & p) {
54 ptype = p.ptype;
55 length = p.length;
56 src = p.src;
57 dest = p.dest;
58 return *this;
59 }; // packet & operator=
60
61 // We could use print() (from the scv_extensions utility interface)
62 // in place of this function. However, the interest in this function
63 // is not so much what it does, but to demonstrate how to access
64 // functions within classes from higher levels in the program.
65 void packet::dump(void) const {
66 const scv_extensions<packet> p = scv_get_const_extensions(*this);
67 cout << " " << length;
68 cout << " " << src;
69 cout << " " << dest;
70 cout << " " << p.ptype.get_enum_string(p.ptype) << endl;
71 }
72
73 // --------------------------------------------------------------
74 // Normal Packet Constraint
75 class normal_packet : virtual public scv_constraint_base {
76 public:
77 scv_smart_ptr<packet> sp;
78 public:
79 SCV_CONSTRAINT_CTOR(normal_packet) {
80 SCV_CONSTRAINT(sp->src() <= 0x3FF);
81 SCV_CONSTRAINT(sp->dest() <= 0x3FF);
82 SCV_SOFT_CONSTRAINT(sp->src() ! = sp->dest());
83 SCV_CONSTRAINT(sp->ptype() == NORMAL);
84 SCV_SOFT_CONSTRAINT(sp->length() >= 64 && sp->length() <= 1500);
85 }
86 };
87 // Runt Packet Constraint
88 class runt_packet : virtual public scv_constraint_base {
89 public:
90 scv_smart_ptr<packet> sp;
91 public:
92 SCV_CONSTRAINT_CTOR(runt_packet) {
93 SCV_CONSTRAINT(sp->src() > 0x3FF && sp->src() <= 0x4FF);
94 SCV_CONSTRAINT(sp->dest() > 0x3FF && sp->dest() <= 0x4FF);
95 SCV_SOFT_CONSTRAINT(sp->src() ! = sp->dest());
96 SCV_CONSTRAINT(sp->ptype() == RUNT);
97 SCV_SOFT_CONSTRAINT(sp->length() < 64 && sp->length() > 20);
98 }
99 };
100
101 // Runt Packet Constraint
102 class oversize_packet : virtual public scv_constraint_base {
103 public:
104 scv_smart_ptr<packet> sp;
105 public:
106 SCV_CONSTRAINT_CTOR(oversize_packet) {
107 SCV_CONSTRAINT(sp->src() > 0x4FF && sp->src() <= 0x5FF);
108 SCV_CONSTRAINT(sp->dest() > 0x4FF && sp->dest() <= 0x5FF);
109 SCV_SOFT_CONSTRAINT(sp->src() ! = sp->dest());
110 SCV_CONSTRAINT(sp->ptype() == OVERSIZE);
111 SCV_SOFT_CONSTRAINT(sp->length() < 5000 && sp->length() > 1500);
112 }
113 };
114
115 // --------------------------------------------------------------
116 int sc_main(int argc, char** argv) {
117 // Set the Seed to 1
118 scv_random::set_global_seed(2);
119 scv_smart_ptr<int> type;
120 oversize_packet op ("OVR_PKT");
121 normal_packet np ("NOR_PKT");
122 runt_packet rp ("RNT_PKT");
123 scv_smart_ptr<packet> smart_p_ptr;
124
125 scv_bag<int> dist;
126 dist.add(0, 40); // specify 1 as legal value and 40% weight
127 dist.add(1, 30); // specify 1 as legal value and 30% weight
128 dist.add(2, 30); // specify 2 as legal value and 30% weight
129 type->set_mode(dist); // set distribution of values
130 for (int i = 0; i < 10; i ++) {
131 type->next();
132 //type->print();
133 if (type->read() == 0) {
134 smart_p_ptr->use_constraint(np.sp);
135 } else if (type->read() == 1) {
136 smart_p_ptr->use_constraint(rp.sp);
137 } else if (type->read() == 2) {
138 smart_p_ptr->use_constraint(op.sp);
139 }
140 smart_p_ptr->next();
141 smart_p_ptr->read().dump();
142 }
143 // Demo the scv_bag pair syntax and usage
144 scv_smart_ptr<int> data; // Some data
145 scv_bag<pair <int, int> > field_dist;
146 field_dist.add( pair<int,int>(0,10), 20);
147 field_dist.add( pair<int,int>(11,20), 20);
148 field_dist.add( pair<int,int>(61,80), 30);
149 field_dist.add( pair<int,int>(81,90), 30);
150 data->set_mode(field_dist);
151 for (int i = 0; i < 10; i ++) {
152 data->next();
153 data->print();
154 }
155 return (0);
156 }
You could download file scv_bag_add.cpp here
|
|
|
|
|
|
Simulation Output : Weighted Randomization
|
|
|
|
|
|
46 1084 1124 RUNT
1389 519 206 NORMAL
955 289 466 NORMAL
748 1014 338 NORMAL
3260 1388 1440 OVERSIZE
435 224 432 NORMAL
675 448 474 NORMAL
48 1085 1087 RUNT
56 1111 1074 RUNT
49 1196 1211 RUNT
19
17
11
20
84
72
74
87
89
5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|