# Constraint random generation for ISS

**URL:** <https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307>\
**Category:** SystemVerilog\
**Tags:** constarint, SystemVerilog\
**Created:** [November 12, 2018, 7:51am UTC](https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307 "2018-11-12T07:51:06Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![rkg1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/d6d6ee/32.png) [@rkg1](https://verificationacademy.com/forums/u/rkg1)\
**Post date:** [November 16, 2018, 2:24am UTC](https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307/10 "2018-11-16T02:24:54Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307/9):_

> _In reply to [rupeshblr](https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307/9):_  
> Now what you are doing makes more more sense. You are trying to write code that outputs assembly language text that will be compiled in a later step. What you have shown is informal syntax.  
> You need to understand the format of the assembly code from the the perspective of the compiler and express it in formal [BNF](http://www.garshol.priv.no/download/text/bnf.html) production terms. Only then you can begin to use randsequence to generate random sequences of instructions based on the BNF rules. The LRM has a very simple example of this

```verilog
randsequence( main )
main : first second gen ;
first : add | dec ;
second : pop | push ;
add : gen("add") ;
dec : gen("dec") ;
pop : gen("pop") ;
push : gen("push") ;
gen( string s = "done" ) : { $display( s ); } ;
endsequence

```

You can add random repetition and registers to this with

```verilog
randsequence( main )
main : statements;
statements: repeat ($urandom_range(100,1) statement;
statement: first second gen ;
first : add | dec ;
second : pop | push ;
add : gen("add") sReg dReg endline;
dec : gen("dec") dReg endline;
pop : gen("pop\n") ;
push : gen("push\n") ;
sReg : gen($sformatf("[s%0d]",$urandom_range(127,1))) ;
dReg : gen($sformatf("[d%0d]",$urandom_range(16,1))) ;
endline : gen("\n"); 
gen( string s = "done" ) : { $write( s ); } ;
endsequence

```

Thanks Dave .

i am implementing above scenario in the below manner.

Is it not the right approach.

1. created the base class.  
typedef enum bit [4:0] {ADD=’h00, SUB, MUL, DIV, …} compute\_path\_pipe ;  
typedef enum bit [3:0] { IF=’h20, ELSE, ENDIF, …} flow\_control\_pipe;

typedef enum bit [1:0] {MOVI=’h40, …} move\_pipe;

…like i classified all the instruction

typedef enum bit [1:0] {vec1\_64,…} instruction\_length ;

//TYPE FORMAT  
typedef enum bit[3:0] [u8,u16,…] format;

//TYPE OF REGISTER  
typedef enum {sR, mR} RegType;

// 1D and 2D  
typedef enum [one\_D, two\_D] dimesion ;

//dstmod  
typedef enum {nml, sat} dstmod

//variable for declare for DDL (0 to 63)  
rand logic[63:0] DDL ;

// variable for src0mod (nml | neg | abs | nabs},)  
typedef enum bit[1:0] {nml, neg,…} src0mod;

//variable for emd (0,1)  
rand logic emd ;

// variable for bd  
rand logic bd;

class operand; (BAse class definition )  
rand format format\_t;  
rand RegType RegType\_t;  
rand int Regnum;  
rand int x,y,dispX,dispY,p;  
rand int w,z ;  
rand compute\_path\_pipe compute\_path\_pipe\_t;  
rand flow\_control\_pipe flow\_control\_pipe\_t;  
…

```
    ........
  rand dimesion dimesion_t;
  rand src0mod src0mod_t ;
  rand dstmod dstmod_t;

```

 

constraint operand\_constraint {  
// x inside {[0:31]}; y inside {[0:31]}; dispX inside {[0:31]}; dispY inside {[0:31]};  
// knobs is a packagege  
format dist {u8:=knobs::W1,s8:= knobs::W1,u16 := knobs::W1,u32:= knobs::W1,s16:= knobs::W1,s32:= knobs::W1,f32:= knobs::W1,u64:= knobs::W1,f64 := knobs::W1};

```
// write constraint for regnumber wrt mR and sR
constraint regnumber_range_c { 
   solve Regtype before Regnum;
   (Regtype_t == sR) -> Regnum inside {[0:31]};
   (Regtype_t == mR) -> Regnum inside {[0:127]};
}

constraint xandy_c {
 if (dimesion_t == one_D) 
       x inside {[0:63]};
	   Y
	   = 0;
	   
 else if (dimesion_t == two_D)
       x inside {[0:7]};
	   y inside {[0:7]};

}

```

endclass

now i am creating the child component based the instruction (who has similar instruction field grouped all gather )

Example :-  
class add\_type extends operand ;  
  
…  
…

```
 endclass

```

so like that creating the multiple child class based the instruction.

After that from top level, will create the object of child class and will randomize and will display based on Instruction mnemonics.

add\_type C1;  
initial begin  
c1=new;  
repeat(1) begin  
c1.randomize();  
…  
$display(“c1.format=%d c1.Regtype=%d, c1.Regnum=%d, c1.x=%d,  
c1.y=%d\n” ,c1.format,c1.Regtype,c1.Regnum,c1.x,c1.y);  
end  
end

So is this approach is correct way to achieve that.

Kindly suggest me.  
Looking forward to your reply :)

---

_[View the full topic](https://verificationacademy.com/forums/t/constraint-random-generation-for-iss/35307)._
