Showing posts with label System Verilog Constraints. Show all posts
Showing posts with label System Verilog Constraints. Show all posts

Tuesday, September 17, 2024

Constraint to have N elements distributed in M bins

Code to distribute N elements into M bins, you add unique keyword to have each bin will have unique number of elements.

class test;
  parameter int M = 5;
  parameter int N = 100;
  rand bit[31:0] arr[M];
  
  constraint c_arr { arr.sum() == N; foreach(arr[i]) { arr[i] inside {[0:N-1]}; } unique {arr}; }
  
  function void post_randomize();
    foreach(arr[i])
    $display("Array[%01d]:%02d\n",i,arr[i]);
  endfunction
  
endclass

module top;
  test t;
  
  initial begin
    t = new;
    void'(t.randomize());
  end
endmodule

RESULT:

Compiler version U-2023.03-SP2_Full64; Runtime version U-2023.03-SP2_Full64; Sep 17 10:38 2024
Array[0]:23

Array[1]:01

Array[2]:10

Array[3]:11

Array[4]:15

Array[5]:21

Array[6]:14

Array[7]:03

Array[8]:02

Array[9]:00

V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.430 seconds; Data structure size: 0.0Mb
Tue Sep 17 10:38:13 2024

Friday, March 10, 2023

Array with unique elements

One of the common interview question in system verilog is about populating the array with unique elements.

One way to generate is using "unique" keyword.

constraint c_array { unique { arr }; }

Another way to achieve this by using randc.

Check this out .....

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class randm;
  randc bit [15:0] addr;
  constraint c_addr { addr inside {[1:10]}; }
endclass: randm

class test;
  bit [15:0] addr_a[10];
  
  function void post_randomize();
    randm c = new;
    foreach(addr_a[i]) begin
      void'(c.randomize());
      addr_a[i] = c.addr;
    end
    $display("ADDR:%p",addr_a);
  endfunction
  
endclass

module top;
  test t;
  initial begin
    t = new;
    t.randomize();
  end
endmodule


class "randm" contains randc addr, which can be used to generate a unique value every time class_handle.randomize is called.

instead of using any constraint on array, we can use the post_randomize method to populate the array with unique elements.


Results:

ADDR:'{'h2c, 'h34, 'h47, 'h6, 'h4f, 'h3c, 'h21, 'h29, 'h16, 'h5f}
xmsim: *W,RNQUIE: Simulation is complete.


Thursday, January 26, 2023

SELECTING NON-OVERLAPPING MEMORY BANKS FROM A MEMORY

 PROBLEM STATEMENT:

  • Suppose you have a memory of depth = 100, you are tasked to generate 4 memory banks of size =10.
  • Condition, memory banks must be non-overlapping.

ANALYSIS:

  1. The trick here is not get carried by the simplicity of the question and start coding away.
  2. The question though sounds easy at first, will get complicated when you start coding the constraints.
  3. We should first start with marking boundary for the banks
  4. In this case we started with first bank and made sure that the start address is always between 0-60
  5. why 60 ? The answer is simple, memory depth is 100, number of banks are 4, and size of each bank is 10. Therefore if the start address of first bank is <= 60, we can have rest of the banks in the other remaining locations. There won't be an overflow.
  6. Once the start address of first bank is generated, it is easy to mark the end address, start_address+bank_size-1
  7. The start address for the second bank must be greater than the end address of first bank and the range must be between 0:70. Reason? Similar to point (5).
  8. The other 2 banks follow the suite.
  9. Now comes the question of limiting the position of each bank, first bank always occupy the lower locations and last one will occupy higher locations.
  10. This can be solved by simply shuffling the queue/array in which we store the start_address during post_randomization().
  11. There can be many other ways to write the constraint, but I found this quite simple and will not stress of the constraint solver.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class mem_banks;
  parameter MEMORY      = 100;
  parameter BANK_SIZE   = 10;
  parameter NO_OF_BANKS = 4;
  
  rand bit [31:0] addr_min[NO_OF_BANKS];
  rand bit [31:0] addr_max[NO_OF_BANKS];
  int total_mem = BANK_SIZE * NO_OF_BANKS;
  
  constraint a_addr { 
    foreach (addr_min[i]) { 
      if(i==0) addr_min[i] inside {[0:(MEMORY-total_mem)]};
      else { 
        addr_min[i] inside {[0:(MEMORY-total_mem)+(BANK_SIZE*i)]};
        addr_min[i] > addr_max[i-1];
      }
      addr_max[i] == addr_min[i]+BANK_SIZE-1;  
    }
        
  }
      
  function void post_randomize();
    addr_min.shuffle();
    foreach(addr_min[i])
      $display("ADDR_MIN:%d ADDR_MAX:%d",addr_min[i],addr_min[i]+BANK_SIZE-1);
  endfunction: post_randomize
    
    
  
endclass: mem_banks

module top;
  mem_banks m;  
  initial begin
    
    m = new;
    repeat(2) begin
      void'(m.randomize());
      $display("======================================");
    end

  end
endmodule: top

RESULTS:

Chronologic VCS simulator copyright 1991-2021
Contains Synopsys proprietary information.
Compiler version S-2021.09; Runtime version S-2021.09; Jan 26 09:47 2023
ADDR_MIN: 5 ADDR_MAX: 14
ADDR_MIN: 69 ADDR_MAX: 78
ADDR_MIN: 39 ADDR_MAX: 48
ADDR_MIN: 15 ADDR_MAX: 24
======================================
ADDR_MIN: 90 ADDR_MAX: 99
ADDR_MIN: 40 ADDR_MAX: 49
ADDR_MIN: 63 ADDR_MAX: 72
ADDR_MIN: 13 ADDR_MAX: 22
======================================
V C S S i m u l a t i o n R e p o r t
Time: 0 ns
CPU Time: 0.480 seconds; Data structure size: 0.0Mb
Thu Jan 26 09:47:17 2023

Wednesday, April 21, 2021

Another system verilog constraint problem - Base class contraint becoming invisible

 Let us walk through the following code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class base;
  rand bit [4:0] c;
  constraint c_x { c == 'd1; }
  
  function void display();
    this.randomize();
    $display("Base    c:%0d",c);
  endfunction
  
endclass

class derived extends base;
  rand bit [4:0] c;
  constraint c_x { c == 'd2; }
  
  function void display();
    this.randomize();
    $display("Derived c:%0d",c);
    super.display();
  endfunction
  
endclass

module top;
  derived b;
  
  initial begin
    b = new;
    repeat(2)
    b.display();
  end
endmodule
    


In the above code, we have 2 classes, base and derived.

Base class has variable 'c' and constraint 'c_x', the same is replicated in the derived class as well.

we have 'display' function which is used to randomize the variable and print the value. In the derived in addition to it , a super.display() was called to print the base class variable value.

When you have 2 constraints with the same name on the same variable, the constraint in the base class becomes invisible and is not applied when the variable is randomized.

Simulation results:

Contains Synopsys proprietary information.
Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Apr 22 00:47 2021
Derived c:2
Base c:27
Derived c:2
Base c:7
V C S S i m u l a t i o n R e p o r t

Reference ( comments by experts )

https://verificationacademy.com/forums/systemverilog/how-constraint-executed

Saturday, November 7, 2020

System verilog constraints and OOPs

Does constraints of a parent class apply when child class handle is passed on to the later?

Code : 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class parent;
  rand int a;
  rand int b;
  constraint c_a { a == 10; }
  constraint c_b { b == 100; }
  
  function void display();
    $display("Parent - A - %0d B - %0d ",a,b);
  endfunction
endclass: parent

class child extends parent;
  rand int a;
  constraint c_a { a == 20; }
  
  virtual function void display();
    $display("Child - A - %0d B - %0d ",a,b);
  endfunction
endclass

class grand_child extends child;
  
  constraint c_a { a == 30; }
  
  function void display();
    $display("Grand Child - A - %0d B- %0d ",a,b);
  endfunction
endclass


module top;
  parent      p;
  child       c;
  grand_child gc;
  
  initial begin
    p = new;
    c = new;
    gc= new;
    
    p = gc;    
    
    void'(p.randomize());
    void'(c.randomize());
    void'(gc.randomize());
    p.display();
    c.display();
    gc.display();
  end
    
  
endmodule

 

Here we have 2 cases, 

1. Case - 1 : Grand child handle 'gc' is assigned to parent handle 'p'. We have not used virtual key word in display function in parent, therefore limiting the scope to the parent int variables.

2. Case - 2 : Using virtual keyword in parent class function display(); 

Observation : 

  • You can see from the print logs, that constraint of parent class on integer 'a' is not applied. we see a random integer value displayed instead of 10.
  • Integer 'b' has constraint applied and therefore you see 100 in the print statement.
  • Reason : since parent handle now contains the handle of grand child, the handling of both constraints differ, a is overridden ( in child class you have 'int a' re-declared )
  • What if we use virtual keyword before display ? Now the scope of the display function is in grand child class, and there fore you can see 30 - 100
  • What if you remove 'int a' in child class?

Without virtual keyword ( in parent display function ):

Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Nov 7 23:53 2020
Parent - A - -1360295855 B - 100
Child - A - 20 B - 100
Grand Child - A - 30 B- 100
V C S S i m u l a t i o n R e p o r t  

With virtual keyword ( in parent display function )

Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Nov 7 23:55 2020
Grand Child - A - 30 B- 100
Child - A - 20 B - 100
Grand Child - A - 30 B- 100
V C S S i m u l a t i o n R e p o r t  

Removing 'int a' in child class ( keep the constraint )

Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Nov 8 00:15 2020
Parent - A - 30 B - 100
Child - A - 20 B - 100
Grand Child - A - 30 B- 100
V C S S i m u l a t i o n R e p o r t

Sunday, August 23, 2020

Driving chunks of data using verilog classes

 Problem Statement :

  • Consider a DUT which asks for say n chunks of data (n <=200 bytes). 
  • The driver needs to drive these chunks on the interface. 
  • Write a transaction class which randomly generates these chunks of data. 
  • Each chunk of data has to be a collection of beats where the size of all beats except the last one is a multiple of 4 and maximum beat size is 64.

Code :

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class chunk_data #(parameter N =200);

  rand bit [7:0] data[$];
  rand bit [7:0] chunk_size[$];

  constraint data_size  { data.size == N; }
  constraint chuuk_size { chunk_size.size inside {[3:25]}; 
                          int'(chunk_size.sum) == N-(N%4);
                          foreach(chunk_size[i]) { 
                            chunk_size[i]%4 == 0; 
                            chunk_size[i] inside {[4:64]}; }
                        }

  function void post_randomize();
    int diff_chunk;
    diff_chunk = N - chunk_size.sum();
    if(diff_chunk != 0) chunk_size.push_back(diff_chunk);
  endfunction: post_randomize

  function void display();
    $display("==============================");
    foreach(chunk_size[i])
    $display("Chunk_Size:%0d",chunk_size[i]);
  endfunction: display
endclass: chunk_data


module top;
  parameter N = 199;
  chunk_data#(N) c;

  initial begin
    c = new;
    repeat(2) begin
      void'(c.randomize());
      c.display();
    end
  end
endmodule: top

 

Results :

Compiler version L-2016.06; Runtime version L-2016.06;  Aug 23 22:38 2020
==============================
Chunk_Size:4
Chunk_Size:4
Chunk_Size:24
Chunk_Size:4
Chunk_Size:20
Chunk_Size:4
Chunk_Size:4
Chunk_Size:36
Chunk_Size:20
Chunk_Size:24
Chunk_Size:4
Chunk_Size:4
Chunk_Size:36
Chunk_Size:8
Chunk_Size:3
==============================
Chunk_Size:56
Chunk_Size:4
Chunk_Size:20
Chunk_Size:8
Chunk_Size:20
Chunk_Size:8
Chunk_Size:4
Chunk_Size:52
Chunk_Size:24
Chunk_Size:3
           V C S   S i m u l a t i o n   R e p o r t
Time: 0
CPU Time:      1.780 seconds;       Data structure size:   0.0Mb
 

 

Saturday, August 22, 2020

Using System Verilog Constraints judiciously

System verilog constraints take up lot of simulation time if not used properly.

Let me try to illustrate it with an example.

Take a scenario where we need to generate unique address ( for burst traffic ) each time we randomize.

Configuration is as follows....

1. Page - 4096 ( 4K) 

2. Address range - 63: 0

3. Align signal is used to take either offset range ( range starts from offset value to the end of the page )

or to take the whole page as a burst ( 4096 in our case ).

align == 1 -> no page offset

align == 0 -> some page offset

Since the address is 64 bits and Page is 12 bits, I took a variable page_num ( 64 -12 = 52 sized vector ) to represent the pages that can be addressed with the address range ( 63:0 ).

Lets take 2 approaches to prove our point ( to pick the random address )

1. With Constraints

2. Without using constraints  


CASE 1 :

In this case, we simply used a queue to push all the used page numbers in post randomize.

For constraints we used unique keyword to make sure that we do not repeat the same value.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class address_selector;
  rand bit [51:0] page_num;
  rand bit [11:0] page_offset;
  rand bit align;
  bit [51:0] used_pages[$];
  constraint c_addr {
    unique { used_pages, page_num }; 
  }
  constraint c_align {
    solve align before page_offset;
    (align == 1) -> page_offset == 0;
  }
  function void post_randomize();
    used_pages.push_back(page_num);
  endfunction: post_randomize
endclass: address_selector

module top;
  address_selector as;
  
  int transfer_size = 1024 * 1000; // Size of 1000K
  initial begin
    as = new;
    while (transfer_size > 0) begin
      void'(as.randomize());
      transfer_size = transfer_size - (4096 - as.page_offset);
      $display("TRANSFER SIZE:%0x ALIGN:%0d ADDR:%0x PAGE:%0x OFFSET:%0x",transfer_size,as.align, {as.page_num,as.page_offset},as.page_num,as.page_offset);
    end
  end
endmodule

Simulation results:

V C S   S i m u l a t i o n   R e p o r t
Time: 0
CPU Time:    143.710 seconds;       Data structure size:   0.0Mb
Sat Aug 22 04:39:05 2020
CPU time: .134 seconds to compile + .023 seconds to elab + .392 seconds to link + 144.630 seconds in simulation

 

CASE 2:

In this case, we used an associative array instead of  queue.

We removed the constraint for generating unique page numbers, instead we used logic to look for the address used ( page_num) inside the post_randomize() function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class address_selector;
  rand bit [51:0] page_num;
  rand bit [11:0] page_offset;
  rand bit align;
  bit [51:0] used_pages[*];
  constraint c_align {
    solve align before page_offset;
    (align == 1) -> page_offset == 0;
  }
  function void post_randomize();
    while(used_pages.exists(page_num)) begin
      std::randomize(page_num);
    end
    used_pages[page_num] = 1;
  endfunction: post_randomize
endclass: address_selector

module top;
  address_selector as;
  
  int transfer_size = 1024 * 1000;// 128 * 1024 * 100000;
  initial begin
    as = new;
    while (transfer_size > 0) begin
      void'(as.randomize());
      transfer_size = transfer_size - (4096 - as.page_offset);
      $display("TRANSFER SIZE:%0x ALIGN:%0d ADDR:%0x PAGE:%0x OFFSET:%0x",transfer_size,as.align, {as.page_num,as.page_offset},as.page_num,as.page_offset);
    end
  end
endmodule


Simulation results :

  V C S   S i m u l a t i o n   R e p o r t
Time: 0
CPU Time:      0.430 seconds;       Data structure size:   0.0Mb
Sat Aug 22 04:39:35 2020
CPU time: .141 seconds to compile + .022 seconds to elab + .460 seconds to link + .568 seconds in simulation


Conclusion :

See the difference 143 seconds vs .430 seconds.

For smaller address ranges, it doesn't make much of a difference, but for larger footprints it does show a significant improvement in the simulation times.

 


Tuesday, August 18, 2020

System Verilog Constraints for non-overlapping memory allocation

 class ex;
 
  parameter MAX = 1024;
 
  rand int unsigned max_val[4];
  rand int unsigned min_val[4];
  rand int unsigned rng_val[4];
 
  constraint c_min_max {
    rng_val.sum() <= 1024;
    foreach(rng_val[i]) {
      max_val[i] inside { [0:MAX-1] };
      min_val[i] inside { [0:MAX-1] };
      rng_val[i] inside { [1:MAX] };
      max_val[i] == min_val[i] + rng_val[i]-1;
      if(i > 0) min_val[i] > max_val[i-1];
    }
  }
 

 
  function void post_randomize();
    foreach(rng_val[i])
    $display("MAX:%0d | MIN:%0d | RNG:%0d",max_val[i],min_val[i],rng_val[i]);
  endfunction  
 
endclass

module top;
  ex e;
 
  initial begin
    e = new;
    void'(e.randomize());
  end
endmodule

 

Simulation:

ompiler version P-2019.06-1; Runtime version P-2019.06-1; Aug 18 06:16 2020
MAX:144 | MIN:20 | RNG:125
MAX:271 | MIN:151 | RNG:121
MAX:457 | MIN:274 | RNG:184
MAX:574 | MIN:459 | RNG:116

Monday, August 17, 2020

Use System verilog constraints to Divide data of N into chunks of M

 Problem Statement:

You need to come up with the system verilog class < sequence item > which can generate or divide a large size of data N into smaller pieces of equal size, these small sized chunks have to be multiple of 4 - M. Maximum size is 64 and minimum is 4.

The left over data if it does not sum up to N, can be added last which need not match M.


Solution:

class chunks;
  int unsigned c[$];
  parameter SIZE = 200;
  rand bit [6:0] s;
 
  constraint c_size {
    s%4 == 0;
  }
 
  function void post_randomize();
    int tmp;
    tmp = SIZE - (SIZE/s * s); // If chunks doesnot add up to SIZE, collect the difference
   
    for(int i=0; i < (SIZE/s);i++) c.push_back(s);
    if(tmp !=0) c.push_back(tmp); // If non zero tmp is observed, simply add to the queue.
     
   
  endfunction
 
  function void display();
    $display("Size selected:%0d",s);
    $display("Queue value:%p",c);
  endfunction: display
endclass
 
 
  module top;
    chunks cs;
   
    initial begin
      cs = new;
      void'(cs.randomize());
      cs.display();
    end
  endmodule

Sunday, August 9, 2020

System verilog constraints interview question involving multiple variables

 

Sequence item is as follows:

rand unique_bit 

rand num_of_reqs;

rand Bit [10:0] x [];

rand Bit[10:0] y[];

rand Bit [10:0] width[];      

rand Bit [10:0] height[];

rand bit [10:0]  frame_width;

rand bit [10:0]  frame_height;

 

Conditions for constraints.....

  1. each request is combination of x,y, width & height

  2. x+width must be less than or equal to frame width

  3. y+height must be less than or equal to  frame height

  4. if unique bit is set , combination of x,y,w,h must not be equal to any of other x,y,w,h

 

Code::

 

class test;

  rand bit unique_bit;

  rand int unsigned num_of_reqs;

  rand bit [10:0] x[];

  rand bit [10:0] y[];

  rand bit [10:0] w[];      

  rand bit [10:0] h[];

  rand bit [10:0] frame_width;

  rand bit [10:0] frame_height;


  constraint c_num_reqs {

    num_of_reqs inside {[1:5]};

    x.size() == num_of_reqs;

    y.size() == num_of_reqs;

    w.size() == num_of_reqs;

    h.size() == num_of_reqs;

  }

 

  constraint c_frame_width {

    frame_width inside {[0:1023]}; // Constraint will fail , if you don't cap your width

    foreach (x[i]) {

      int'(x[i] + w[i])<= frame_width;

      x[i] inside {[0:frame_width]};

      w[i] inside {[0:frame_width]};

     }

  }

      

  constraint  c_frame_height {

    frame_height inside {[0:1023]}; // Constraint will fail , if you don't cap your height

    foreach (y[i]) {

      solve frame_height before x[i],h[i];

      int'(y[i] + h[i]) <= frame_height;

      y[i] inside {[0:frame_height]};

      h[i] inside {[0:frame_height]};

    }

  }

 

 constraint c_unique {

        solve unique_bit before x,y,w,h,frame_height,frame_width;

        if(unique_bit) {

          unique {x};

          unique {y};

          unique {w};

          unique {h};

        }

      }

        

  function void display();

    $display("Unique Bit:%0d",unique_bit);

    $display("Num of Requests:%0d", num_of_reqs);

    $display("Frame Height:%0d Width:%0d",frame_height,frame_width);

    foreach(x[i])

      $display("X:%04d W:%04d || Y:%04d H:%04d",x[i],w[i],y[i],h[i]);

  endfunction

        


endclass


module top;

 

  test t;

 

  initial begin

    t = new;

    if(!t.randomize()) $error("Randomization failed");

    t.display();

  end

endmodule

Constraint to have N elements distributed in M bins

Code to distribute N elements into M bins, you add unique keyword to have each bin will have unique number of elements. class test; param...