# Advice for simplifying constraint

**URL:** <https://verificationacademy.com/forums/t/advice-for-simplifying-constraint/39881>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, constraint-randomization\
**Created:** [December 10, 2021, 7:29am UTC](https://verificationacademy.com/forums/t/advice-for-simplifying-constraint/39881 "2021-12-10T07:29:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![bhupeshpaliwal](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/ee59a6/32.png) [@bhupeshpaliwal](https://verificationacademy.com/forums/u/bhupeshpaliwal)\
**Post date:** [December 13, 2021, 12:33am UTC](https://verificationacademy.com/forums/t/advice-for-simplifying-constraint/39881/3 "2021-12-13T00:33:37Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/advice-for-simplifying-constraint/39881/2):_  
Thanks Dave, without above suggestions, especially using dynamic array instead of queues has improved performance a lot.  
Also since end address we were only using to calculate for 4kb boundary, so removed it altogether.  
However considering for me num value is in range of close to more than 'd3000+ so still seeing some hangs on max values.

One trivial query:

> > You could calculate bsz, bl, and the array sizes in pre\_randomize to simplify the constraint equations.  
> > By this you meant that in pre\_randomize() following needs to be done :

```verilog

class tr_gen;
  rand bit[1:0] bsz; 
  rand bit[31:0] s_addr[];

  rand bit[2:0] bl;
  bit[31:0] addr_4KB_msk = ~((32'('h1) << 12)-1);
 
  rand bit[5:0] num;
 
  constraint c_tr_n {
    s_addr.size() == (num+1); // number of transactions
  }
 
  constraint c_s_addrs {
    foreach(s_addr[it]) if(it>0) {
      s_addr[it] == s_addr[it-1]+((bl+1)*(2**bsz)); // consecutive start addr = addr + total num bytes in transasction
    }
  }
 
  constraint c_4kb_boundary {
    foreach(s_addr[it]) {
      (s_addr[it] & addr_4KB_msk) == (s_addr[it]+((bl+1)*(2**bsz)-1)) & addr_4KB_msk); // for every transaction ensure 4kb boundary
    }
  } 

  // constraint only s_addr[0] 
  constraint c_s_addr_byte_align {
    bsz == 1 -> s_addr[0][0:0] == 0; // 2 byte
    bsz == 2 -> s_addr[0][1:0] == 0; // 4 byte
    bsz == 3 -> s_addr[0][2:0] == 0; // 8 byte
  }

  function void pre_randomize();
    randomize(bsz,bl); // randomize
    bsz.rand_mode(0); // no further randomize required for bsz
    bl.rand_mode(0); // no further randomize required for bl

    // num is passed as inline constraint will reflect here ?
    s_addr = new [num+1];
    c_tr_n.constraint_mode(0); // no further constraint required
  endfunction

endclass
 
module try;
 
initial begin
  tr_gen tr;
  tr = new();
  void'(tr.randomize() with { 
   num == 5;
  });
 
  // use tr 
end
 
endmodule

```

---

_[View the full topic](https://verificationacademy.com/forums/t/advice-for-simplifying-constraint/39881)._
