0

Is there a way to populate a queue using elements from another random queue in a constraint-based way? For example,

class some_class;
  
  rand bit [7:0] choices [$];
  rand bit [7:0] chosen [$];
  
  int num_choices = 20;
  int num_chosen = 5;
  
  function new();
  endfunction
  
  constraint choices_size_c { choices.size() == num_choices; }
  
  constraint chosen_c {
    chosen.size() == num_chosen;
    foreach (chosen[i]) {
      // check chosen[i] exists somewhere within choices
    }
  }
  
endclass

The idea is we have a number of valid choices that, for example, get programmed somewhere. Then we choose 5 of those valid choices for testing. Can this be done with constraints, or do I need to do this manually post-randomization?

1 Answer 1

1

Use the set membership operator inside

 constraint chosen_c {
    chosen.size() == num_chosen;
    foreach (chosen[i]) {
       chosen[i] inside {choices};
    }
  }

You can also add unique constraints to prevent repetition if that is a requirement you failed to mention

constraint uniq {
     unique {choices};
     unique {chosen};
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.