Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Constrain a nonzero 32-bit power of two

Hardware interview practice

Constrain a nonzero 32-bit power of two

EasyConstrained RandomSystemVerilog

Write a solver-friendly SystemVerilog constraint for a 32-bit randomized field that contains exactly one set bit and therefore represents a nonzero power of two.

Try it in the question bankReason first. Then compare.

Keep this exact question selected while you check your answer and review the full solution.

Practice this question →
Starting point

Question code

class one_hot_value;
  rand bit [31:0] value;
endclass
Reviewed example

Work through one case

Input
value=32'h0001_0000
Expected output
legal: $countones(value) = 1

Exactly bit 16 is set, so $countones(value) equals one.

What to cover

Requirements

  1. Allow every value from 32'h0000_0001 through 32'h8000_0000 that has one set bit.
  2. Reject zero.
  3. Reject values with two or more set bits.
  4. Express the condition as an integral constraint without post_randomize repair.
Continue practicing

Related questions

Constrained RandomConstrain ten unique multiples of three→Constrained RandomConstrain exactly five changed bit positions→ConstraintsConstrain three disjoint child arrays→
asic.fyi · Learn silicon end to end.info@asic.fyi