[sv-ec] randomize() is virtual, but what about constraint_mode() ??

From: Jonathan Bromley <jonathan.bromley_at_.....>
Date: Fri May 23 2008 - 12:16:27 PDT
One of the really good things about constraints in SV
is the way you can override them in derived classes.
But what happens if you invoke constraint_mode() on
an overridden constraint, but your method call is 
through a base class reference?  Of the three SV
simulators I can try, two think it's non-virtual
(i.e. the base class constraint is controlled,
vacuously, and the derived class constraint remains
untouched) and a third treats it as virtual.
Here's a toy example to try it on:

program p;
 
  class C;
    rand byte x,y;
    constraint c {x < 0;}
  endclass : C
  
  class D extends C;
    constraint c {y > 0;}
  endclass : D
  
  initial begin
    D d;
    C c;
    C r;
    
    d = new;
    c = new;
    r = d;
    
    $display("constrained");
    repeat (10) begin
      assert (r.randomize());
      $display("r = {x:%0d, y:%0d}", r.x, r.y);
    end

    $display("r constraint off");
    r.c.constraint_mode(0);
    repeat (10) begin
      assert (r.randomize());
      $display("r = {x:%0d, y:%0d}", r.x, r.y);
    end
    
  end
  
endprogram

Supplementary question: If I use a derived-class
reference to switch off a derived-class constraint,
does the base-class constraint then become active
(i.e. does it reappear from beneath the derived-class
constraint that was hiding/overriding it)?
-- 
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd. Church Hatch, 22 Market Place, Ringwood, Hampshire, BH24 1AW, UK
Tel: +44 (0)1425 471223                   Email: jonathan.bromley@doulos.com
Fax: +44 (0)1425 471573                           Web: http://www.doulos.com

The contents of this message may contain personal views which 
are not the views of Doulos Ltd., unless specifically stated.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Received on Fri May 23 12:17:25 2008

This archive was generated by hypermail 2.1.8 : Fri May 23 2008 - 12:18:15 PDT