Verification Academy

Search form

My Account Menu

  • Register
  • Log In
  • Topics
  • Courses
  • Forums
  • Patterns Library
  • Cookbooks
  • Events
  • More
  • All Topics
    The Verification Academy offers users multiple entry points to find the information they need. One of these entry points is through Topic collections. These topics are industry standards that all design and verification engineers should recognize. While we continue to add new topics, users are encourage to further refine collection information to meet their specific interests.
    • Languages & Standards

      • Portable Test and Stimulus
      • Functional Safety
      • Design & Verification Languages
    • Methodologies

      • UVM - Universal Verification Methodology
      • UVM Framework
      • UVM Connect
      • FPGA Verification
      • Coverage
    • Techniques & Tools

      • Verification IP
      • Simulation-Based Techniques
      • Planning, Measurement, and Analysis
      • Formal-Based Techniques
      • Debug
      • Clock-Domain Crossing
      • Acceleration
  • All Courses
    The Verification Academy is organized into a collection of free online courses, focusing on various key aspects of advanced functional verification. Each course consists of multiple sessions—allowing the participant to pick and choose specific topics of interest, as well as revisit any specific topics for future reference. After completing a specific course, the participant should be armed with enough knowledge to then understand the necessary steps required for maturing their own organization’s skills and infrastructure on the specific topic of interest. The Verification Academy will provide you with a unique opportunity to develop an understanding of how to mature your organization’s processes so that you can then reap the benefits that advanced functional verification offers.
    • Universal Verification Methodology (UVM)

      • Advanced UVM
      • Basic UVM
      • Introduction to UVM
      • UVM Connect
      • UVM Debug
      • UVMF - One Bite at a Time
    • Featured Courses

      • Introduction to ISO 26262
      • Introduction to DO-254
      • Clock-Domain Crossing Verification
      • Portable Stimulus Basics
      • Power Aware CDC Verification
      • Power Aware Verification
      • SystemVerilog OOP for UVM Verification
    • Additional Courses

      • Assertion-Based Verification
      • An Introduction to Unit Testing with SVUnit
      • Evolving FPGA Verification Capabilities
      • Metrics in SoC Verification
      • SystemVerilog Testbench Acceleration
      • Testbench Co-Emulation: SystemC & TLM-2.0
      • Verification Planning and Management
      • VHDL-2008 Why It Matters
    • Formal-Based Techniques

      • Formal Assertion-Based Verification
      • Formal-Based Technology: Automatic Formal Solutions
      • Formal Coverage
      • Getting Started with Formal-Based Technology
      • Handling Inconclusive Assertions in Formal Verification
      • Sequential Logic Equivalence Checking
    • Analog/Mixed Signal

      • AMS Design Configuration Schemes
      • Improve AMS Verification Performance
      • Improve AMS Verification Quality
  • All Forum Topics
    The Verification Community is eager to answer your UVM, SystemVerilog and Coverage related questions. We encourage you to take an active role in the Forums by answering and commenting to any questions that you are able to.
    • UVM Forum

      • Active Questions
      • Solutions
      • Replies
      • No Replies
      • Search
      • UVM Forum
    • SystemVerilog Forum

      • Active Questions
      • Solutions
      • Replies
      • No Replies
      • Search
      • SystemVerilog Forum
    • Coverage Forum

      • Active Questions
      • Solutions
      • Replies
      • No Replies
      • Search
      • Coverage Forum
    • Additional Forums

      • Announcements
      • Downloads
      • OVM Forum
  • Patterns Library
    The Verification Academy Patterns Library contains a collection of solutions to many of today's verification problems. The patterns contained in the library span across the entire domain of verification (i.e., from specification to methodology to implementation—and across multiple verification engines such as formal, simulation, and emulation).
    • Implementation Patterns

      • Environment Patterns
      • Stimulus Patterns
      • Analysis Patterns
      • All Implementation Patterns
    • Specification Patterns

      • Occurrence Property Patterns
      • Order Property Patterns
      • All Specification Patterns
    • Pattern Resources

      • Start Here - Patterns Library Overview
      • Whitepaper - Taking Reuse to the Next Level
      • Verification Horizons - The Verification Academy Patterns Library
      • Contribute a Pattern to the Library
  • All Cookbooks
    Find all the methodology you need in this comprehensive and vast collection. The UVM and Coverage Cookbooks contain dozens of informative, executable articles covering all aspects of UVM and Coverage.
    • UVM Cookbook

      • UVM Basics
      • Testbench Architecture
      • DUT-Testbench Connections
      • Configuring a Test Environment
      • Analysis Components & Techniques
      • End Of Test Mechanisms
      • Sequences
      • The UVM Messaging System
      • Other Stimulus Techniques
      • Register Abstraction Layer
      • Testbench Acceleration through Co-Emulation
      • Debug of SV and UVM
      • UVM Connect - SV-SystemC interoperability
      • UVM Versions and Compatibility
      • UVM Cookbook
    • Coding Guidelines & Deployment

      • Code Examples
      • UVM Verification Component
      • Package/Organization
      • Questa/Compiling UVM
      • SystemVerilog Guidelines
      • SystemVerilog Performance Guidelines
      • UVM Guidelines
      • UVM Performance Guidelines
    • Coverage Cookbook

      • Introduction
      • What is Coverage?
      • Kinds of Coverage
      • Specification to Testplan
      • Testplan to Functional Coverage
      • Bus Protocol Coverage
      • Block Level Coverage
      • Datapath Coverage
      • SoC Coverage Example
      • Requirements Writing Guidelines
      • Coverage Cookbook
  • All Events
    No one argues that the challenges of verification are growing exponentially. What is needed to meet these challenges are tools, methodologies and processes that can help you transform your verification environment. These recorded seminars from Verification Academy trainers and users provide examples for adoption of new technologies and how to evolve your verification process.
    • Upcoming & Featured Events

      • The Digital Twin: An Aerospace and Defense Revolution - March 9th
      • VIP solutions for Protocol and Memory Verification  - March 11th
      • Advance your Designs with Advances in CDC and RDC - March 23rd
      • Webinar Calendar
    • On Demand Seminars

      • The ABC of Formal Verification
      • I'm Excited About Formal...
      • 2020 Functional Verification Study
      • All On-Demand Seminars
    • Recording Archive

      • Improving Your SystemVerilog & UVM Skills
      • Should I Kill My Formal Run?
      • Visualizer Debug Environment
      • All Recordings
    • Conferences

      • DVCon 2020
      • DAC 2019
      • All Conferences
    • Mentor Learning Center

      • SystemVerilog Fundamentals
      • SystemVerilog UVM
      • View all Learning Paths
  • About Verification Academy
    The Verification Academy will provide you with a unique opportunity to develop an understanding of how to mature your organization's processes so that you can then reap the benefits that advanced functional verification offers.
    • Blog & News

      • Verification Horizons Blog
      • Academy News
      • Academy Newsletter
      • Technical Resources
    • Verification Horizons Publication

      • Verification Horizons - November 2020
      • Verification Horizons - July 2020
      • Verification Horizons - March 2020
      • Issue Archive
    • About Us

      • Verification Academy Overview
      • Subject Matter Experts
      • Contact Us
    • Training

      • Questa® & ModelSim®
      • Questa® inFact
      • Functional Verification Library
Ask a Question
SystemVerilog
  • Home
  • Forums
  • SystemVerilog
  • Object handles and randomization

Object handles and randomization

SystemVerilog 4972
constraint 44 constraint randomization 29
Bahaa Osman
Bahaa Osman
Full Access
11 posts
July 13, 2017 at 3:01 am

I am trying to create a constraint for the following scenario.

typedef enum {TOYOTA, HONDA, FORD} brand_t; // To make dave_59 happy :)
 
class car;
  rand brand_t brand_e;
endclass
 
class toyota extends car;
  constraint my_brand {brand_e == TOYOTA;}
  // Adds other constraints, methods and variables related only to this class
endclass
 
class ford extends car;
  constraint my_brand {brand_e == FORD;}
  // Adds other constraints, methods and variables related only to this class
endclass
 
class txn;
  rand car mycars_arr[];
  // I need to randomize mycar_arr. Here are my constraints:
  // 1- Array size
  // 2- Type of car in each array location
  // 3- Then based on the types i end up in each location, I want to randomize an object of the right type of class and assign that object to the array location. This is the most challenging part.
endclass

Replies

Log In to Reply
Tudor Timi
Tudor Timi
Full Access
333 posts
July 13, 2017 at 3:49 am

Randomization doesn't create new objects, so you can't get what you want. The only way (I know of) to do this in SystemVerilog is to split your randomization into two steps. You first randomize an array containing the kinds of objects to be created. Afterwards you create the objects. Finally, you randomize the newly created objects.

Constrained random thoughts on SystemVerilog and e: http://blog.verificationgentleman.com/

Bahaa Osman
Bahaa Osman
Full Access
11 posts
July 13, 2017 at 4:41 am

In reply to Tudor Timi:

Agreed. The challenge is I want to do all that inside one class using constraints. I can use solve before to ensure the brands are solved first. I can also use foreach to loop over the types and create the right objects, but will randomizing the newly created objects happen then?

If you can show some pseudo code, that might help me understand your proposal?

Thanks
-Bahaa

Tudor Timi
Tudor Timi
Full Access
333 posts
July 13, 2017 at 5:53 am

In reply to Bahaa Osman:

Off the top of my head, this is how you could implement it:

class car;
  // Not rand, since you can't use randomization on a car object to suddenly make it a Toyota
  brand_t brand_e;
 
  // Adds other constraints, methods and variables related to all cars
endclass
 
 
class toyota extends car;
  function new();
    brand_e = TOYOTA;
  endfunction
 
  // Adds other constraints, methods and variables related only to this class
endclass
 
 
class txn;
  rand brand_t brands[];
  rand car mycars_arr[];
 
  function void allocate_cars();
    mycars_arr = new[brands.size()];
    foreach (brands[i])
      case (brands[i])
        TOYOTA: mycars_arr[i] = toyota::new();
        // ...
      endcase
  endfunction
endclass
 
 
class test;
  function void run();
    txn t = new();
    t.randomize(brands) with {
      // whatever constraints you want on brands
    };
    t.allocate_cars();
    t.randomize(mycars_arr) with {
      // whatever constraints you want on cars
    };
  endfunction
endclass

This is really ugly since it's not very intuitive. Users not familiar with your 'txn' class will scratch their heads when they see the steps required to randomize an array of cars.

Constrained random thoughts on SystemVerilog and e: http://blog.verificationgentleman.com/

Tudor Timi
Tudor Timi
Full Access
333 posts
July 13, 2017 at 6:13 am

In reply to Bahaa Osman:

Another approach which works in one step could be the following:

class txn;
  rand car cars[];
 
  local rand toyotas[];
  local rand hondas[];
  local rand fords[];
 
  local const int unsigned MAX_NUM_CARS = 10;
 
 
  constraint legal_num_cars {
    cars.size() <= MAX_NUM_CARS;
  }
 
  function void pre_randomize();
    // randomization doesn't create objects, but it can throw them away
    // create more cars than you need so randomization can throw away excess
    cars = new[MAX_NUM_CARS];
    foreach (cars[i])
      cars[i] = new();
 
    toyotas = new[MAX_NUM_CARS];
    foreach (toyotas[i])
      toyotas[i] = new();
 
    // same for hondas and fords
  endfunction
 
  function void post_randomize();
    foreach (cars[i])
      case (cars[i].brand)
        TOYOTA: cars[i] = toyotas[i];
        // same for hondas and fords
      endcase
  endfunction
endclass
 
 
class test;
  function void run();
    txn t = new();
    t.randomize() with {
      cars.size() == 5;
      cars[1].brand == TOYOTA;
      cars[2].brand == HONDA;
    };
  endfunction
endclass

While this might be more natural to constrain, it's a lot more code. You create more objects than you need, just so you can have enough for randomize to work with.

Both approaches (the two step and this one) suffer from the problem that you need to make modifications to the 'txn' class code when you declare a new brand. The two step is clunkier to randomize, but more efficient memory-wise. The second one (gotta find a catchy name for it) is more natural, but requires much more code and will for sure be slower. It's also going to require a lot more boilerplate code to keep objects in sync if you have any common properties defined in the 'car' class. For example, if you decide to add a 'year_of_manufacture' field to 'car' and want to be able to constrain that irrespective of the car brand, you'd need constraints to keep the 'cars' array in sync with the 'toyotas', 'hondas' and 'fords':

class txn;
 
  constraint keep_common_vars_in_sync {
    foreach cars[i] {
      toyotas[i].year_of_manufacture == cars[i].year_of_manufacture;
      hondas[i].year_of_manufacture == cars[i].year_of_manufacture;
      // ...
    }
  }
 
endclass
 
 
class test;
  function void run();
    txn t = new();
    t.randomize() with {
      cars.size() == 5;
      cars[0].brand == TOYOTA;
      cars[0].year_of_manufacture == 2017;
    };
  endfunction
endclass

Things are even more complicated if you want more funky constraints like "if the car at index i was built in 2017 then it has to be a Toyota and the model should be a Prius" (here, I'm assuming the model is a variable that's defined in the 'toyota' class).

Note: I haven't tested the code, but I think it should work.

Constrained random thoughts on SystemVerilog and e: http://blog.verificationgentleman.com/

Bahaa Osman
Bahaa Osman
Full Access
11 posts
July 13, 2017 at 6:40 am

In reply to Tudor Timi:

Great, now you reached exactly the same point I was at before starting this thread :) In fact, my current implementation is your second proposal. And for the exact reasons you mentioned, I was asking for a better solution here. Thanks for the thoughts!

ssureshgverifier@gmail.com
ssureshgverifier@gmail.com
Full Access
67 posts
July 15, 2017 at 2:14 pm

In reply to Bahaa Osman:

In either case, how to access brand specific field ?

for eg: let's say Toyota has a field called prius

class toyota extends car;
int prius;
endclass

now, if someone wants to hierarchically access the field like
i_txn.mycars_arr[0].prius; //this is going to be an issue right ?

[assuming mycars_arr[0] is randomized to toyota]

i.e user has to typecast mycars_arr[0] back to Toyota handle and access it right ?

Bahaa Osman
Bahaa Osman
Full Access
11 posts
July 16, 2017 at 12:40 am

In reply to ssureshg_:

You will have to cast to the right type to be able to access those fields. But that's expected so I don't see this is as an issue, at least not yet!

ssureshgverifier@gmail.com
ssureshgverifier@gmail.com
Full Access
67 posts
July 21, 2017 at 12:39 am

In reply to Bahaa Osman:
Hi Bahaa Osman, here is some quick code...

#1 not too many unnecessary objects are created(though some redundancy is still there)
#2 No need to keep constraints in sync
#3 adding another brand does not require base class modification
but adding another field to enum does cause issue-
can be fixed by constraining base class to randomize known types at that time

intentionally left size randomization outside the class to keep it simple...
did not check the code well - please point out if there are any glaring issues :)

typedef enum {HONDA, TOYOTA, FORD,TESLA} brand_t; 
 
class car;
  rand brand_t brand_e;
  virtual function void print();
    $display("obj handle of type car ");
  endfunction 
endclass
 
class toyota extends car;
  constraint my_brand {brand_e == TOYOTA;}
  // Adds other constraints, methods and variables related only to this class
  virtual function void print();
    $display("obj handle of type toyota");
  endfunction 
endclass
 
class honda extends car;
  constraint my_brand {brand_e == HONDA;}
  // Adds other constraints, methods and variables related only to this class
  virtual function void print();
    $display("obj handle of type honda");
  endfunction 
endclass
 
class ford extends car;
  constraint my_brand {brand_e == FORD;}
  // Adds other constraints, methods and variables related only to this class
  virtual function void print();
    $display("obj handle of type ford");
  endfunction 
endclass
 
class tesla extends car;
  constraint my_brand {brand_e == TESLA;}
  // Adds other constraints, methods and variables related only to this class
  virtual function void print();
    $display("obj handle of type tesla");
  endfunction 
endclass
 
class txn;
  rand car mycars_arr[];
  int size;
  // I need to randomize mycar_arr. Here are my constraints:
  // 1- Array size
  // 2- Type of car in each array location
  // 3- Then based on the types i end up in each location, I want to randomize an object of the right type of class and assign that object to the array location. 
 
    function void pre_randomize(); //create objects during pre_rand, else randomize() has no impact
      mycars_arr = new[size];
      for (int i=0;i<size;i++)
        begin
	  mycars_arr[i]= new(); //objects are used only to create random list of brand_e, later in post_rand this will be loaded with apprp objects
        end
    endfunction
 
  function void post_randomize(); //randomized value of brand_e is available only at post_randomize
   toyota toyota_;
   honda  honda_;
   ford   ford_;
   car    car_;
   foreach (mycars_arr[i])
   begin
    case(mycars_arr[i].brand_e)
      TOYOTA : begin mycars_arr[i]= toyota::new();  mycars_arr[i].randomize(); end //fixed ptr issue, thnx Bahaa Osman
      HONDA  : begin mycars_arr[i]= honda::new() ;  mycars_arr[i].randomize() ; end
      FORD   : begin mycars_arr[i]= ford::new()  ;  mycars_arr[i].randomize()  ; end
      default: begin default_case(mycars_arr[i]); end
    endcase
   end
  endfunction:post_randomize
 
  virtual function void default_case(ref car car_);
  endfunction: default_case
endclass
 
class txn2 extends txn;
  tesla tesla_;
  virtual function void default_case(ref car car_);
    case(car_.brand_e)
      TESLA: begin car_ = tesla::new(); car_.randomize(); end
    endcase
  endfunction: default_case
endclass
 
module tb();
 
txn  i_txn  = new();
txn2 i_txn2 = new();
 
initial 
begin
 
repeat(1)
begin
  i_txn.size =5; //size of array is to be controlled before randomize, this is to simplify and keep object creation to min
  i_txn.randomize; 
  foreach(i_txn.mycars_arr[i]) i_txn.mycars_arr[i].print(); 
 
  $display("txn2 type randomization, adds tesla to the mix");
  i_txn = i_txn2; //addition of new brand does not need any modification to base class
  i_txn.size =5;
  i_txn.randomize; 
  foreach(i_txn.mycars_arr[i]) i_txn.mycars_arr[i].print(); 
end
 
end
 
endmodule:tb

EDIT: Fixed ptr issue as per Bahaa Osman's input, thnq

Bahaa Osman
Bahaa Osman
Full Access
11 posts
July 23, 2017 at 4:00 am

In reply to ssureshg_:

Thanks for the detailed proposal. While this does work, there is an important limitation. That is controlling this txn from a sequence. I need to be able to send a txn with a specific list of car brands and potentially specific values of each brand's fields. The code in post_randomize breaks this. You also can't move it to pre-randomize.

One more thing, if I understand your code correctly, all the toyotas in mycars_arr will be pointing to the same object (the last one that was created and randomized). I think you need to clone instead of assign

TOYOTA : begin toyota_ = toyota::new();  toyota_.randomize(); $cast(mycars_arr[i], toyota_.clone()); end

Any ideas to control the txn from the sequence?

-Bahaa

ssureshgverifier@gmail.com
ssureshgverifier@gmail.com
Full Access
67 posts
July 23, 2017 at 8:59 pm

In reply to Bahaa Osman:

Quote:
I need to be able to send a txn with a specific list of car brands

both of the below format works for me...
  //i_txn.randomize() with {foreach (i_txn.mycars_arr[i]) i_txn.mycars_arr[i].brand_e == TOYOTA; }; 
  i_txn.randomize() with { i_txn.mycars_arr[0].brand_e == TOYOTA;
i_txn.mycars_arr[1].brand_e == FORD;
i_txn.mycars_arr[2].brand_e == HONDA;
i_txn.mycars_arr[3].brand_e == TOYOTA;
i_txn.mycars_arr[4].brand_e == FORD;
                          }; 

Quote:
and potentially specific values of each brand's fields.

this needs some thinking....let me chk
Siemens Digital Industries Software

Siemens Digital Industries Software

##TodayMeetsTomorrow

Solutions

  • Cloud
  • Mendix
  • Siemens EDA
  • MindSphere
  • Siemens PLM
  • View all portfolio

Explore

  • Digital Journeys
  • Community
  • Blog
  • Online Store

Siemens

  • About Us
  • Careers
  • Events
  • News and Press
  • Newsletter
  • Customer Stories

Contact Us

USA:

phone-office +1 800 547 3000

See our Worldwide Directory

  • Contact Us
  • Support Center
  • Give us Feedback
©2021 Siemens Digital Industries Software. All Rights Reserved.
Terms of Use Privacy Cookie Policy