|
|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
In this section we will covering randomization feature of Vera language. |
|
|
|
|
|
- Random Variables
- Constraint block
- Dynamic constraint modification
- Out of body constraints
|
|
|
|
|
|
In earlier chapter we had used method random() for generating random integer values, this is vera build in method which returns 32 bit random value. This is no good for object randomization (here object mean class based). To help with class based object's to be randomized, vera supports rand variables and randomize() method, we will be looking this in detail below. |
|
|
|
|
|
|
|
|
|
|
|
Random Variables
|
|
|
Class properties can be declared random using the rand and randc modifiers. |
|
|
|
|
|
- Vera can randomize scalar variables of type integer, reg, and enumerated type. Bit variables can be any size supported by Vera.
- Arrays can be declared rand or randc, in which case all of their member elements are treated as rand or randc.
- Associative arrays, dynamic arrays and SmartQs can be declared rand or randc.
- Associative arrays can be declared rand or randc, however, only the elements in the numeric key range of 0 to n-1 will be randomized, where n is declared using the optional assoc_size keyword.
|
|
|
|
|
|
As seen above variables can be declared as |
|
|
|
|
|
|
|
|
|
|
|
rand |
|
|
Variables declared with the rand keyword are standard random variables. Their values are uniformly distributed over their range. |
|
|
|
|
|
|
|
|
randc |
|
|
Variables declared with the randc keyword are random-cyclic variables that cycle through all the values in a random permutation of their declared range. Random-cyclic variables can only be reg or enumerated types, and are limited to a maximum size of 16 bits, so the maximum range for any randc variable is 0 to 65535. |
|
|
|
|
|
The basic idea is that randc randomly iterates over all the values in the range and that no value is repeated within an iteration. When the iteration is finished, a new iteration is automatically started. |
|
|
|
|
|
Example : Random Variables
|
|
|
|
|
|
1 enum pkt_type {UNICAST=11,MULTICAST,BROADCAST};
2
3 class frame_t {
4 rand pkt_type type;
5 rand integer len;
6 randc reg [1:0] no_repeat;
7 rand reg [7:0] payload [];
8 // Constraint the members
9 constraint legal {
10 len >= 2;
11 len <= 5;
12 payload.size() == len;
13 }
14 // Print the members of the class
15 task print() {
16 integer i =0;
17 printf("Packet type %s\n",type);
18 printf("Size of frame is %0d\n",len);
19 printf("Payload is ");
20 for (i=0; i < len; i++) {
21 printf(" %2x",payload[i]);
22 }
23 printf("\n");
24 printf("no_repeat is %d\n",no_repeat);
25 }
26 }
27
28 program rand_ex {
29 frame_t frame = new();
30 integer j = 0;
31 // Print frame before randomize
32 printf("-------------------------------\n");
33 frame.print();
34 printf("-------------------------------\n");
35 for (j = 0 ; j < 10;j++) {
36 if (frame.randomize() == OK) {
37 // Print frame after randomize
38 frame.print();
39 } else {
40 printf("Failed to randomize frame\n");
41 }
42 printf("-------------------------------\n");
43 }
44 }
You could download file rand_ex.vr here
|
|
|
|
|
|
Simulation : Random Variables
|
|
|
|
|
|
-------------------------------
Packet type
Size of frame is x
Payload is
no_repeat is x
-------------------------------
Packet type MULTICAST
Size of frame is 3
Payload is 0b df 40
no_repeat is 0
-------------------------------
Packet type BROADCAST
Size of frame is 3
Payload is fa 4e 15
no_repeat is 1
-------------------------------
Packet type BROADCAST
Size of frame is 5
Payload is c4 aa c4 cf 4f
no_repeat is 2
-------------------------------
Packet type UNICAST
Size of frame is 3
Payload is 2c ce 05
no_repeat is 3
-------------------------------
Packet type UNICAST
Size of frame is 4
Payload is 7a a2 f0 c9
no_repeat is 0
-------------------------------
Packet type MULTICAST
Size of frame is 4
Payload is df c5 d7 94
no_repeat is 3
-------------------------------
Packet type BROADCAST
Size of frame is 4
Payload is f4 d9 4f 00
no_repeat is 1
-------------------------------
Packet type UNICAST
Size of frame is 5
Payload is 45 f2 a2 a1 fd
no_repeat is 2
-------------------------------
Packet type UNICAST
Size of frame is 4
Payload is 20 e1 97 c6
no_repeat is 3
-------------------------------
Packet type UNICAST
Size of frame is 2
Payload is b8 1c
no_repeat is 0
-------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|