Skip to question
asic.fyi
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
asic.fyi/Interview questions/Constrain exactly five changed bit positions

Hardware interview practice

Constrain exactly five changed bit positions

EasyConstrained RandomSystemVerilog

Two randomized bytes must differ in exactly five bit locations. Write the single constraint that expresses that Hamming-distance relation.

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 distance_pair;
  rand bit [7:0] a;
  rand bit [7:0] b;
endclass
Reviewed example

Work through one case

Input
a=8'h00, b=8'h1F
Expected output
legal: $countones(a ^ b) = 5

a XOR b equals 8'b0001_1111, which contains exactly five set bits.

What to cover

Requirements

  1. Count differing positions rather than counting ones in each operand independently.
  2. Require exactly five differing bits.
  3. Allow every pair that satisfies the relation.
  4. Check whether randomize() succeeds.
Continue practicing

Related questions

Constrained RandomConstrain ten unique multiples of three→Constrained RandomConstrain a nonzero 32-bit power of two→ConstraintsConstrain three disjoint child arrays→
asic.fyi · Learn silicon end to end.info@asic.fyi