Search code examples
system-verilog

Send transactions using test cases to random channels


I have 16 channels to which I need to send data randomly. The channels shall be selected randomly. Eg: In the first iteration channel 0, Channel 3 are selected i.e two channels are selected In the second iteration channel 9, 10 , 12, 15 are selected i.e four channels

The channels can be repeated in each iteration. The number of channels selected should be different for each iteration and accordingly channels should be selected between 0 to 15.

How do I create a logic for the same using SV Constraints?


Solution

  • You can use a dynamic array

    module top;
      class A;
        rand int no_of_channels;
        rand int channels[];
        
        constraint ch_size {
          no_of_channels inside {[1:16]}; // presumably dont want 0
          no_of_channels == channels.size();
          no_of_channels != const'(no_of_channels); // be different from last
        }
        constraint ch_values {
          foreach(channels[i]) channels[i] inside {[0:15]};
          unique {channels}; // each channel value different
        }
      endclass
      
      A a = new;
      initial repeat(10) begin
        assert(a.randomize());
        $display("%p",a);
      end
    endmodule