1

Let say I have these two classes:

class random_packet extends uvm_sequence_item;
    rand int cmd;
    ...
endclass

and its extension:

class good_packet extends random_packet;
    constraint good_constr {
    cmd inside {0,1,2};
    }
    ...
endclass

(Here I am not going to create good_packet object, but I just want to use its constraint as a reference) Then I instantiate random_packet object and randomize it:

random_packet pkt;
pkt = random_packet::type_id::create("pkt");
pkt.randomize();

My question: Is there a simpler way by utilizing its derived class good_packet's constraint to check whether the resultant pkt.cmd fall in category of good_packet constraint ? It would be better than writing redundant codes like:

if (pkt.cmd == 0 || pkt.cmd == 1 || pkt.cmd == 2) $display("good");
else $display("bad");
AldoT
  • 913
  • 1
  • 8
  • 34

1 Answers1

6

You could copy the contents of pkt into a new packet of type good_packet and then check if the constraint holds.

First, you'll need a function that can update the fields of a good_packet based on the fields of a random_packet:

class random_packet extends uvm_sequence_item;
  // ...

  virtual function void update(random_packet source);
    this.cmd = source.cmd;
  endfunction
endclass

Using this function, you can update the fields of a good_packet that you've created earlier:

// ... randomization of 'pkt' happened earlier

good_packet g_pkt = new();
g_pkt.update(pkt);

Now that g_pkt contains the same values as pkt, you can use the inline constraint checker construct to check if the constraints defined in the good_packet class hold:

if (g_pkt.randomize(null))
  $display("good");
else
  $display("bad");

The call to randomize(null) won't actually randomize anything inside g_pkt (it's kind of like setting every field to rand_mode(0)).

You can find more info on this construct in section 18.11.1 In-line constraint checker of the IEEE 1800-2012 standard.

Tudor Timi
  • 7,453
  • 1
  • 24
  • 53
  • Even though we set the constraint mode to 0, does the the call to randomize() still try to solve the constraints ? – Vineeth VS Jan 21 '15 at 13:18
  • 2
    @VineethVS We don't set `constraint_mode` to 0. I said it's like setting `rand_mode` to 0. The solver will take the values that are already set and check if they match **all** of the constraints that are enabled. – Tudor Timi Jan 21 '15 at 15:23
  • @Tudor This is exactly what I want, Thanks! randomize(null) is to force the randomize() method to behave as a checker. – AldoT Jan 22 '15 at 01:19
  • @Tudor Sorry that was a typo. I understand what you were saying about `constraint_mode`. What I meant to ask was that if we set the `rand_mode` to 0 and then do `randomize(null)` ;do the solver try to solve the constraints. Anyways I just tried and figured out the difference. Thanks for the reply. – Vineeth VS Feb 04 '15 at 06:52