0% found this document useful (0 votes)
15 views

Constraints

The document contains a series of programming tasks involving constraints, random number generation, and array manipulations in a programming language. It includes functions to find Armstrong numbers, calculate factorials, and generate specific patterns in arrays. Various constraints are defined for generating random numbers and ensuring specific properties in arrays, such as uniqueness and even/odd placements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Constraints

The document contains a series of programming tasks involving constraints, random number generation, and array manipulations in a programming language. It includes functions to find Armstrong numbers, calculate factorials, and generate specific patterns in arrays. Various constraints are defined for generating random numbers and ensuring specific properties in arrays, such as uniqueness and even/odd placements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1 //1.

Write a constraint to find factorial for first 5 armstrong numbers


2 class test;
3 int val,sum,temp;
4 int a=1;
5 bit[8:0] z;
6 int value[];
7 randc int unsigned n;
8 rand logic[8191:0] fact;
9 constraint c {n inside {value};}
10 constraint c1 {fact == factorial(n);}
11 function automatic logic[8191:0] factorial (int unsigned x);
12 if (x==1)
13 factorial = 1;
14 else
15 factorial = x*factorial (x-1);
16 // $display ("factorial of %d is: %0d",x,factorial);
17 endfunction
18 function void arm_number;
19 begin
20 while(val<5)
21 begin
22 temp = a;sum=0;
23 while(temp!=0)
24 begin
25 sum=sum+(temp%10)**3;
26 temp=temp/10;
27 end
28 if(a==sum)
29 begin
30 value[val]=a;
31 a=a+1;
32 val=val+1;
33 end
34 else
35 a=a+1;
36 end
37 foreach(value[i])
38 begin
39 temp=value[i];
40 z=1;
41 while(temp!=1)
42 begin
43 z=z*temp;
44 temp=1;
45 end
46 value[i]=z;
47 $display("The %0d armstrong number is %d",i+1,value[i]);
48 end
49 end
50 endfunction
51 function void post_randomize();
52 $display ("factorial of %d is: %0d",n,fact);
53 endfunction
54 endclass
55
56 test t1;
57 module top;
58 initial
59 begin
60 t1 = new();
61 t1.value = new[5];
62 t1.arm_number;
63 repeat(5)
64 begin
65 assert(t1.randomize());
66 end
67 end
68 endmodule
69
70
71 int getCubicSumOfDigits(int number);
72 int main(){
73 int number, sum, counter;
74 printf("Enter a number : ");
75 scanf("%d", &number);
76 printf("Armstrong numbers between 0 and %d\n", number);
77 /* Iterate from 0 till N, and check for Armstrong number */
78 for(counter = 0; counter <= number; counter++){
79 sum = getCubicSumOfDigits(counter);
80 if(sum == counter){
81 printf("%d\n", counter);
82 }
83 }
84 getch();
85 return 0;
86 }
87 /*
88 * Funtion to calculate the sum of cubes of digits of a number
89 * getCubicSumOfDigits(123) = 1*1*1 + 2*2*2 + 3*3*3;
90 */
91 int getCubicSumOfDigits(int number){
92 int lastDigit, sum = 0;
93 while(number != 0){
94 lastDigit = number%10;
95 sum = sum + lastDigit*lastDigit*lastDigit;
96 number = number/10;
97 }
98 return sum;
99 }
100
101
102 //2.Write a constraint to randomly generate 10 unique numbers between 99 to 100
103
104
105
106
107
108
109
110 /*3.Write a constraint to generate 10 random numbers in an array. The range of
111 each element should be between 10 to 30
112 Write a code to generate 10 elements in a queue with the following conditions
113 queue[0] = array[0] - 0
114 queue[1] = array[1] - array[0] and so on*/
115 class transaction;
116 rand int unsigned d_array [];
117 rand bit[5:0] m_queue [$];
118 constraint ARRAY_SIZE {d_array.size == 10;}
119 constraint QUEUEY_SIZE {m_queue.size == d_array.size;}
120 constraint ARRAY_UNIQUE {foreach(d_array[i])
121 d_array[i] inside {[10:30]};}
122 constraint QUEUE_ORDER {foreach (d_array[i])
123 if(i == 0)
124 m_queue[i] == d_array[i] - 0;
125 else
126 m_queue[i] == d_array [i] - d_array[i-1];}
127
128 endclass : transaction
129
130
131 //4. Write a constraint to generate the following pattern 1122334455 in an array
132 named da[ ]
133 class example1;
134 rand int da[];
135 constraint a_size{da.size == 10;}
136 constraint a_size1{da[0] == 1;}
137 constraint a_value{foreach (da[i])
138 if(i%2==0)
139 da[i]==da[i+1];
140 else if(i%2 !=0 && i<(da.size-1))
141 da[i+1]==da[i]+1;
142 }
143 function void post_randomize();
144 $display("The result is: %p",da);
145 endfunction
146 endclass
147 (or)
148 count = 1
149 foreach array(i)
150 if( i%2 == 0)
151 array(i)=count;
152 else
153 array(i) = count ;
154 count = count+1
155 //6.Write a code to generate a random number between 1.35 to 2.57
156 rand int b;
157 real num;
158 constraint value {b inside {[135:257]};}
159 function void post_randomize();
160 num = b/100.0;
161 endfunction
162
163
164 //7.Write constraint to generate random values 25,27,30,36,40,45 for the integer
165 "a" without using "set membership".
166 constraint value{ b>24;b<46;(a%9==0||a%5==0); a!=35);
167
168 /*8.Assume that you have two properties
169 rand bit [7:0]a;
170 rand bit [2:0]b;
171 The range of values for b are 3,4,7
172 If b==3, the number of ones in the binary number 'a' must be 4.
173 If b==4, the number of ones in the binary number 'a' must be 5.
174 If b==7, the number of ones in the binary number 'a' must be 8.
175 Write constraints for above scenario.*/
176 bit a[8];
177 constraint value_b{b inside{3,4,7};}
178 constraint array{ if (b ==3)
179 array.sum with (item ==1) == 4;
180 else if(b ==4)
181 array.sum with (item ==1) ==5;
182 else
183 array.sum with (item ==1) ==8;}
184 function post randomize();
185 a = {a[7],a[6],a[5],a[4],a[3],a[2],a[1],a[0]};
186 endfunction
187 (or)
188 constraint range {b dist {7:=2,4:=2,3:=2};}
189 constraint count {$countones(a)==(b+1); }
190
191
192 //9.Without using randomize method for randomizing "b", generate some random
193 values for the property "b". Explain with an example
194
195
196 /*10.Consider the the size of a synchronous fifo is 32 x 16.
197 Read and write enable are active low signals.
198 Write a constraint for this fifo such that the read enable must be asserted once
199 after the fifo becomes full.*/
200 constraint wr_enb1{wr_enb==0;}
201 constraint rd_enb1{rd_enb==0;}
202 function void post_randomize();
203 x++;
204 if(x>=32)
205 this.rd_enb=0;
206 endfunction
207 endclass
208 /*11. write a snippet of code to randomize an 8 bit dynamic array with following
209 constraints.
210 1. The size of an array should be in between 10 to 20.
211 2. The sum of any three consecutive elements of an array should be an even
212 number.*/
213 constraint size{array.size() inside{[10:20];}
214 constraint value {foreach(array(i))
215 array(i)+array(i-1)+array(i-2) %2 ==0
216
217
218 //12.Write a logic such that every time object "t" is randomized the value of
219 "b" should be equal to previous randomized value of "a"
220 int t;
221 constraint value{ b == t;}
222 function post_randomized
223 t= a;
224 endfunction
225
226
227 //13. Write a constraint on a 16 bit rand vector to generate alternate pairs of
228 0's & 1's (for ex:value should be 16'b 00110011... or 16'b 11001100...)
229 rand bit[15:0] a
230 constraint value_2 { foreach(a[i])
231 if ({a[i-2],a[i-1]} == 00 || 11 && i>1)
232 a[i] == ~a[i-1] ;
233 else if ( i%2 == 0)
234 a[i] = a[i+1];
235 }
236 (or)
237 constraint C1 {foreach(a[i])
238 if(i<15)
239 if(i%2==0)
240 a[i]==a[i+1];
241 else
242 a[i]!=a[i+1];}
243
244
245
246 /*14. Define following constraints for a 4 bit dynamic array
247 1. The size of array should be in between 15 to 20
248 2. There should be even numbers in odd locations & odd numbers in even
249 locations.*/
250 rand bit[4:0] array[];
251 constraint size {array.size() inside{[15:20]};}
252 constraint value { foreach(i)
253 if(i%2==0)
254 array[i]%2 == 1
255 else
256 array[i]%2 == 0;
257
258 //15.when the transaction class object is randomized, the value of a should not
259 be same as previous 5 occurrences of the value of a
260 class transaction;
261 rand bit [3:0]a;
262 bit[3:0]q[$:4];
263 constraint C1 {foreach(q[i]) a != q[i];}
264 function void pre_randomize();
265 if(q.size >= 5)
266 q.pop_back();
267 endfunction
268 function void post_randomize();
269 q.push_front(a);
270 endfunction
271 endclass
272 //16. Write the constraint such that size of first dimension of array should be
273 inside [5 : 10] & 2nd dimension of array should be 5.
274 bit a1[][];
275 constraint array { a1.size() inside {{5:10]};
276 foreach(a[i])
277 a1[i].size() ==5;}
278
279 17.Write a constraint for an array. Two consecutive elements in the array should
280 not differ by more than two bits.
281 int a [10];
282 constraint value { foreach(a[i])
283 if(i > 0)
284 $countones(a[i]^a[i-1]) <= 2;}
285 (or)
286 foreach(a[i])
287 if(i > 0){
288 ($countones(a[i]) -$countones(a[i-1]) ) <= 2;
289 ($countones(a[i]) -$countones(a[i-1]) ) >= 0;
290 }
291 }
292
293
294 /*18.Write a snippet of code to find out the sum of even numbers in an array
295 using array methods. The array should be a dynamic array of int data type which
296 should be randomized with the following constraints:
297 1. Size of the array should be between 10 to 20
298 2. All the elements of the array should be in between 0 to 30.*/
299 int a[]
300 constraint value { a.size inside {[10:20]};
301 foreach(a[i])
302 a[i] inside {{0:30]};}
303 post randomize();
304 summation = a.sum with (item%2 == 0);
305
306
307 //19.There is an 8 bit vector (bit[7:0] data_in) which takes some random value.
308 Write a constraint in such a way that every time it is randomized , total no. of
309 bits toggled (in data_in vector) should be 5 with respect to the previous value
310 of data_in .
311 bit [7:0]a,b;
312 constraint value {$countones(a^b) == 5;};
313 post randomize();
314 b = a;
315
316
317 //20.constraint to add last two consecutive elements from an array size
318 constraint value { foreach(a[i])
319 if(i>1)
320 a[i] = a[i-1]+a[i-2];}
321
322
323
324
325
326
327 //21.write a constraint to generate below pattern 01010101...
328 constraint value { foreach(a[i])
329 if(i==0)
330 a[i]=0;
331 else
332 a[i] = ~a[i-1]; }
333 (or )
334 for even location "0"
335 for odd locations "1"
336
337 //22.write a constraint to generate below pattern 1234554321
338 count = 1
339 constraint value { foreach(a[i])
340 if(i <5)
341 a[i] = count;
342 count = count +1;
343 if(i=5)
344 a[i] = 5;
345 else
346 a[i] = count-1;}
347
348
349 //23.write a constraint to generate below pattern 5 -10 15 -20 25 -30
350 int da[];
351 constraint {da.size==6;
352 foreach da[i]
353 if(i%2==1)
354 da[i]=5*(-(i+1));
355 else
356 da[i]=5*(i+1); }
357
358
359 //24.constraint to generate the below pattern 9 19 29 39 49 59 69 79
360 a[];
361 constraint value { a.size() == 8;
362 foreach(a[i])
363 if(i==0)
364 a[i]=9;
365 else
366 a[i]=a[i-1]+10; }
367
368
369 //25.constraint to generate the below pattern 9 7 5 3 1 8 6 4 2 0
370 a[];
371 count1 = 11;
372 count2 = 10;
373 constraint value {a.size() == 10;
374 foreach(a[i])
375 if(i<5)
376 a[i] = count1 -2;
377 count = a[i];
378 else
379 a[i] = count2 - 2;
380 count2 = a[i]; }
381
382
383 //26.constraint to generate the below pattern 7 17 27 37 47 57 67
384
385
386 //27.constraint to generate the below pattern 1 2 4 3 5 6 8 7
387 a[];
388 constraint value { a.size() == 8;
389 foreach(a[i])
390 a[i] = i+1;
391 post randomize();
392 temp = a[3]
393 a[3] = a[2];
394 a[2] = temp;
395 temp = a[7];
396 a[7] = a[6];
397 a[6] = temp;
398 endfunction
399
400
401 //28.constraint to generate 10 leap years from 2020 to 3030
402 randc int a[];
403 int b[];
404 function void leap year();
405 b = new[10];
406 count = 0;
407 for( i=2020;i<=3030;i++);
408 if(count ==10);
409 begin
410 return;
411 end
412 if(i%4 == 0)
413 begin
414 a[count] = i;
415 count = count +1;
416 end
417 endfunction
418 pre randomize();
419 leap year();
420 endfunction
421 constarint value { a == b;}
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437 //29.write a constraint to generate prime numbers from 1 to 20
438 rand int a;
439 int b[];
440 function void prime number();
441 b = new[8];
442 count = 0;
443
444 for( i=1;i<=20;i++);
445 if(!((i%2==0 && i!=2) || (i%3==0 && i!=3) || (i%4==0 && i!=4) || (i%5==0 &&
446 i!=5) ||
447 (i%6==0 && i!=6) || (i%7==0 && i!=7) || (i%8==0 && i!=8) || (i%9==0 && i!=9)))
448 begin
449 b[count] = i;
450 count = count +1;
451 end
452 endfunction
453 pre randomize();
454 prime number();
455 endfunction
456 constarint value { a inside {[b]};}
457
458
459 //30.constraint to generate 32 bit variable with alternate 0's and 1's
460 rand bit[31:0] a;
461 constraint value{ foreach(a[i])
462 if(i>0)
463 a[i] = ~ a[i-1]; }
464
465
466 //31.constraint to generate 32 bit variable with 8 continous 0's and 8 continous
467 1's
468 rand bit[31:0] a;
469 count = 0;
470 int q = 0;
471 constraint value{ foreach(a[i])
472 if(count <8)
473 begin
474 a[i] == q;
475 count = count +1;
476 end
477 if (count == 8)begin
478 count = 0;
479 q = ~q;
480 end}
481
482
483 //32.constraint to generate 32 bit variable with everytime only 1 bit should be
484 1 and remaining is 0
485 rand bit[31:0] a;
486 constraint value {$countones(a) == 1;}
487
488
489
490
491 //33.constraint to generate 10 random perfect numbers
492 int a[];
493 sum = 0;
494 int b[];
495 function void perfect numbers();
496 for(i = 0; i<100000; i++);
497 begin
498 if(count == 10)
499 return;
500 for(j=0;j<i;j++)
501 begin
502 if(i%j == 0)
503 sum = sum+j;
504 end
505 if(sum == i)
506 begin
507 b[count] == i;
508 count = vount+1;
509 end
510 end
511
512
513 //34.constraint to find 3rd largest element from an given array
514 rand int a[];
515 int b;
516 constarint value { b == third_largest(a);}
517 function int third_largest( input int a[]);
518 a.rsort;
519 return a[2];
520 endfunction
521
522
523
524
525
526
527 class constraint_test;
528
529 rand bit [4:0] var_q1[5][5];
530
531 rand bit [4:0] var_q2[5][5];
532
533 rand bit [9:0] var_q3_a;
534 rand bit [9:0] var_q3_b;
535
536 rand bit [9:0] var_q4_x;
537 rand bit [9:0] var_q4_y;
538
539 rand bit [7:0] var_q5_a;
540 rand bit [2:0] var_q5_b;
541
542 rand bit [9:0] var_q6_da[];
543
544 rand bit [4:0] var_q7_arr[10];
545 rand bit [4:0] var_q7_q[$];
546
547 rand bit [5:0] var_q8_a;
548
549 rand bit [9:0] var_q9_da[];
550
551 function new();
552 endfunction : new
553
554
555
556
557 /*1. Write a piece of code to randomize a 2 dimensional array to generate the
558 below pattern:
559 12345
560 22345
561 33345
562 44445
563 55555
564 //----------------------------------------------------------------------------
565 ----*/
566 constraint q1
567 {
568 foreach(var_q1[i])
569 {
570 foreach(var_q1[i][j])
571 {
572 if(j <= i)
573 var_q1[i][j] == i+1;
574 else
575 var_q1[i][j] == var_q1[i][j-1] + 1;
576 }
577 }
578 }
579
580 /*2. Write a piece of code to randomize a 2 dimensional array to generate the
581 below pattern:
582 12345
583 23456
584 34567
585 45678
586 56789
587 //----------------------------------------------------------------------------
588 ----*/
589 constraint q2
590 {
591 foreach(var_q2[i])
592 {
593 foreach(var_q2[i][j])
594 {
595 if(j == 0)
596 var_q2[i][0] == i+1;
597 else
598 var_q2[i][j] == var_q2[i][j-1] + 1;
599 }
600 }
601 }
602
603 /*3. Write a constraint for bit[9:0]a & b, where each bit of a should be the
604 inverted value od a.
605 Eg: a = 1010100 then b = 0101011
606
607 //----------------------------------------------------------------------------
608 ----*/
609
610 constraint q3
611 {
612 var_q3_a == ~var_q3_b;
613 }
614
615
616
617
618
619 /*4. Write a class with two variable x and y. Constraint the randomization of
620 these variables as follows:
621 if the value of x is ranging from 5 to 10, then value of y should be less
622 than 20
623 if the value of x is ranging from 20 to 40, then value of y should be
624 greater than 30 and less than 70
625 otherwise the value of y should be greater then 70
626
627 //----------------------------------------------------------------------------
628 ----*/
629
630 constraint q4
631 {
632 if(var_q4_x inside {[5:10]})
633 var_q4_y < 20;
634 else if(var_q4_x inside {[20:40]})
635 var_q4_y inside {[31:69]};
636 else
637 var_q4_y > 70;
638 }
639
640 /*5. Assume that you have two properties
641 rand bit [7:0]a;
642 rand bit [2:0]b;
643 The range of values for b are 3,4,7
644 If b==3, the number of ones in the binary number 'a' must be 4.
645 If b==4, the number of ones in the binary number 'a' must be 5.
646 If b==7, the number of ones in the binary number 'a' must be 8.
647 Write constraints for above scenario.
648 //----------------------------------------------------------------------------
649 ----*/
650
651
652 constraint q5
653 {
654 var_q5_b inside {3, 4, 7};
655 $countones(var_q5_a) == var_q5_b;
656 }
657
658 /*6. write a snippet of code to randomize an 8 bit dynamic array with following
659 constraints.
660 1. The size of an array should be in between 10 to 20.
661 2. The sum of any three consecutive elements of an array should be an even
662 number.
663
664 --------------------------------------------------------------------------------
665 ------------*/
666
667 constraint q6
668 {
669 var_q6_da.size inside {[10:20]};
670
671 foreach(var_q6_da[i])
672 {
673 if(i >= 2)
674 (var_q6_da[i] + var_q6_da[i-1] + var_q6_da[i-2])%2 == 0;
675
676 //Added coz VCS was generating all even values.
677 if(i%3 == 0)
678 var_q6_da[i]%2 != 0;
679 }
680 }
681
682 /*7. Write a constraint to generate 10 random numbers in an array. The range of
683 each element should be between 10 to 30.
684 Write a code to generate 10 elements in a queue with the following
685 conditions,
686 Example: queue[0] = array[0] - 0 and
687 queue[1] = array[1] - array[0] and so on.
688 //----------------------------------------------------------------------------
689 ----*/
690
691
692 constraint q7
693 {
694 var_q7_q.size == var_q7_arr.size;
695
696 foreach(var_q7_arr[i])
697 {
698 var_q7_arr[i] inside {[10:30]};
699 var_q7_q[i] == var_q7_arr[i] - i;
700 }
701
702 }
703
704 /*8. Write constraint to generate random values 25,27,30,36,40,45 for the
705 integer "a" without using "set membership".
706 //----------------------------------------------------------------------------
707 ----*/
708
709 constraint q8
710 {
711 var_q8_a dist {25 : = 1, 27 : = 1,30 : = 1,36 : = 1,40 : = 1,45 : = 1};
712 }
713
714 /*9. Write a constraint to generate the following pattern
715 1122334455 in an array named da[]
716 //----------------------------------------------------------------------------
717 ----*/
718
719 constraint q9
720 {
721 //constrining to avoid very large value
722 var_q9_da.size inside {[10:30]};
723
724 foreach(var_q9_da[i])
725 if(i == 0)
726 var_q9_da[i] == 1;
727 else if(i%2 == 0)
728 var_q9_da[i] == var_q9_da[i-2] + 1;
729 else
730 var_q9_da[i] == var_q9_da[i-1];
731 }
732
733 //----------------------------------------------------------------------------
734 ----
735
736
737
738
739
740
741
742
743
744 function void print();
745
746 $display("Q1:");
747 foreach(var_q1[i])
748 begin
749 foreach(var_q1[i][j])
750 $write(var_q1[i][j]);
751 $display(" ");
752 end
753
754 $display("\n\nQ2:");
755 foreach(var_q2[i])
756 begin
757 foreach(var_q2[i][j])
758 $write(var_q2[i][j]);
759 $display(" ");
760 end
761
762 $display("\n\nQ3:");
763 $display("var_q3_a : %10b",var_q3_a);
764 $display("var_q3_b : %10b",var_q3_b);
765
766 $display("\n\nQ4:");
767 $display("var_q4_x : %0d",var_q4_x);
768 $display("var_q4_y : %0d",var_q4_y);
769
770 $display("\n\nQ5:");
771 $display("var_q4_b : %0d",var_q5_b);
772 $display("var_q4_a : %8b",var_q5_a);
773
774 $display("\n\nQ6:");
775 foreach(var_q6_da[i])
776 $display("var_q6_da[%0d]=%0d", i, var_q6_da[i]);
777
778 $display("\n\nQ7:");
779 foreach(var_q7_arr[i])
780 $display("var_q7_arr[%0d]=%0d | var_q7_q[%0d]=%0d", i, var_q7_arr[i], i,
781 var_q7_q[i]);
782
783 $display("\n\nQ8:");
784 $display("var_q8_a=%0d", var_q8_a);
785
786 $display("\n\nQ9:");
787 foreach(var_q9_da[i])
788 $write("%0d",var_q9_da[i]);
789 $display(" ");
790 endfunction : print
791
792 endclass
793
794 module const_test;
795
796 initial
797 begin
798 constraint_test c;
799 c = new();
800 c.randomize();
801 c.print();
802 end
803
804 endmodule
805
806
807
808
809
810

You might also like