SystemVerilog Constraints for Queue with Power of 2 Values

Write SystemVerilog Constraints to Generate a Queue with Unique Power of 2 ValuesWelcome back to VLSI 360, your trusted resource for VLSI design, verification, and SystemVerilog tutorials! Today, we’re diving into an intriguing problem: generating a queue of random size with unique values, where each value is a power of 2, using SystemVerilog constraints. This exercise is perfect for mastering randomization, constraints, and queue manipulation, skills frequently tested in VLSI verification interviews or academic projects. We’ll explore the provided working code, explain its logic, and share tips to help you excel in your interviews or coursework. Let’s dive in!


Question: Write SystemVerilog Constraints to Generate a Queue with Unique Power of 2 Values
We need to generate a queue (a) in SystemVerilog with the following properties:
  • Random size: The queue size must be between 6 and 12 elements (inclusive).
  • Unique values: All elements in the queue must be distinct.
  • Power of 2: Each element must be a power of 2 (e.g., 1, 2, 4, 8, 16, ..., 8192).
  • Maximum value: Elements should be less than 8193 to keep values within a reasonable range (e.g., up to 2^{12} = 8192).
Our goal is to write SystemVerilog constraints to generate this queue and demonstrate it with multiple randomizations. The provided code is verified to work, so let’s analyze it.

SystemVerilog Constraints for a Queue with Unique Power of 2 Values

Below is the working SystemVerilog code to generate a queue with the specified properties using a class-based approach with constraints.
class sample;
  rand int a[$];  // Dynamic queue for unique power-of-2 values
  rand int temp;  // Unused in constraints but declared in class
  
  function void print();
    $display("a: %p", a);
    $display("******************");
  endfunction 
  
  constraint size_cons {
    a.size < 13;  // Maximum size is 12
    a.size > 5;   // Minimum size is 6
    
    foreach (a[i]) {
      a[i] < 8193;               // Values less than 8193
      a[i] != 0;                 // Non-zero values
      (a[i] & (a[i]-1)) == 0;   // Power-of-2 check
    }
    unique {a};                  // All elements unique
  }
endclass
   
module tb;
  sample s;
  initial begin
    s = new();
    repeat (5) begin
      assert (s.randomize());
      s.print();
    end        
  end  
endmodule

How It Works
Let’s break down the code to understand how it generates a queue with unique power-of-2 values.
Class Definition (sample):
  • The sample class encapsulates the logic for generating the queue.
Dynamic Queue
  • rand int a[$] is a queue (dynamic array) to store the power-of-2 values. The rand keyword enables randomization with constraints.
Variable temp
  • Declared but unused in the constraints or print function, likely a placeholder for future extensions.
Size Constraint (size_cons):
  • a.size < 13: Ensures the queue size is at most 12.
  • a.size > 5: Ensures the queue size is at least 6.
  • Together, these constraints enforce a queue size between 6 and 12 (inclusive).
Value Constraint for Queue a:
  • foreach (a[i]): Iterates over each element in the queue.
  • a[i] < 8193: Restricts values to be less than 8193, ensuring they don’t exceed
  • 2^{12} = 8192
  • a[i] != 0: Ensures no element is zero, as zero is not a power of 2.
  • (a[i] & (a[i]-1)) == 0: Ensures each element is a power of 2.
  • Explanation: A number is a power of 2 if it has exactly one bit set in its binary representation (e.g., 2^3 = 8 = 1000_2). The expression a[i] & (a[i]-1) clears the least significant set bit. For a power of 2, this results in 0. For example:
    For a[i] = 8 (1000_2), a[i]-1 = 7 (0111_2), so 8 & 7 = 1000_2 & 0111_2 = 0000_2 = 0.
  • For a non-power-of-2 like 6 (0110_2), 6-1 = 5 (0101_2), so 6 & 5 = 0110_2 & 0101_2 = 0100_2 = 4 \neq 0.
  • unique {a}: Ensures all elements in the queue are distinct (no duplicates).
Print Function:
  • Displays the queue a using $display("a: %p", a).
  • Adds a separator line (******************) for readability.
Top Module (tb):
  • Instantiates the sample class.
  • Calls randomize() five times in a repeat loop to generate different valid queues.
  • Prints each randomized queue using the print function.

Output
Running the code produces five different random queues, each with 6 to 12 unique power-of-2 values. A sample output might look like (actual values depend on the randomization seed):
a: {1, 4, 16, 64, 256, 1024, 4096}
******************
a: {2, 8, 32, 128, 512, 2048}
******************
a: {1, 2, 4, 8, 16, 32, 64, 256}
******************
a: {4, 16, 64, 256, 1024}
******************
a: {1, 8, 32, 128, 512, 2048, 8192}
******************
Each queue has:
  • A size between 6 and 12.
  • Unique values, all powers of 2 (e.g.,
    2^0 = 1, 2^1 = 2, 2^2 = 4, ..., 2^{12} = 8192
    ).
  • No zero or non-power-of-2 values.

Tips for Interviews
This problem is a great fit for VLSI verification interviews, as it tests:
  • Constraint writing for random size and unique values.
  • Queue manipulation in SystemVerilog.
  • Mathematical logic for power-of-2 checks.
  • Randomization techniques for generating constrained patterns.
Potential follow-up questions include:
  • Can you generate the queue with a specific power-of-2 range (e.g., only
    2^0
    to
    2^5
    )?
  • How would you verify the queue’s correctness programmatically?
  • Can you modify the code to use a fixed size instead of a random size?
  • How would you handle larger powers of 2 (e.g., use longint for values beyond
    2^{31}
    )?

Conclusion
Generating a queue with a random size (6 to 12) containing unique power-of-2 values using SystemVerilog constraints is an excellent way to practice randomization, queue manipulation, and class-based programming. The provided code is verified to work and is ideal for VLSI verification interviews or academic projects. Experiment with it by adjusting the size range or adding verification checks to deepen your skills.
Keep following VLSI 360 for more SystemVerilog tutorials and VLSI insights! Drop a comment if you have questions or want another topic covered. Happy coding!

Post a Comment

0 Comments