• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Functional Verification
  3. randomize() question

Stats

  • Locked Locked
  • Replies 3
  • Subscribers 64
  • Views 14028
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

randomize() question

archive
archive over 18 years ago

Hi all,

There is a randomize() problem.
I want to randomize a Packet_seq which includes two Packet object.

First, I call Packet_seq randomize() to get pkt_seq. From pkt_seq to get Packet's cmd by constraint.
Second, I call Packet randomize() from Packet_seq's post_randomize() to make Packet's other attribute randomized.

But the random solver said there is a error when solve my constraint?
Any suggestions are welcome!


// Code listed below (and is attached)
module top;

typedef enum { WRITE=1,READ=2,IDLE=4} cmd_t;
typedef enum { WRITE_IDLE, READ_IDLE} packet_seq_t;
   
    class Packet;
       
        rand bit [7:0] addr;
        rand bit [31:0] data;
        cmd_t cmd;
        rand bit [2:0] dly; 
        
        function new (cmd_t cmd_new = IDLE,
                      bit [31:0] data_new = 0,
                      bit [7:0] addr_new = 0,
                      bit [2:0] dly_new = 1);
            cmd = cmd_new;
            data = data_new;
            addr = addr_new;
            dly = dly_new;
        endfunction

        function void do_print ();
        begin
            $display("Packet_tr: cmd=%s, addr=%0d, data=%0d, dly=%0d", cmd.name(), addr, data, dly);
        end
        endfunction
   endclass : Packet

   class Packet_seq;
       rand packet_seq_t pkt_seq;
       Packet pkt[2];
      
       constraint pkt_seq_1_cst { pkt_seq == WRITE_IDLE -> (pkt[0].cmd == WRITE && pkt[1].cmd == IDLE); }
       constraint pkt_seq_2_cst { pkt_seq == READ_IDLE  -> (pkt[0].cmd == READ  && pkt[1].cmd == IDLE); }
      
       function new ();
           for (int i=0;i
               pkt[i] = new();
           end
       endfunction

       function void post_randomize();
           int random;
           for (int i=0;i
               random = pkt[i].randomize();
           end
       endfunction
      
       function void do_print ();
           $display ("Packet Sequence is %s", pkt_seq.name());
           for (int i=0;i
               pkt[i].do_print();
           end
       endfunction
  
   endclass : Packet_seq

  //------------------
  // Main routain 
  //------------------
  Packet_seq pkt_seq[3];
   initial begin
       int random;
       for(int i=0;i
           pkt_seq[i] = new();
           random = pkt_seq[i].randomize();
           pkt_seq[i].do_print();
       end
   end
  
 
endmodule


Originally posted in cdnusers.org by davyzhu
  • test_rand_sv.txt
  • View
  • Hide
  • Cancel
  • archive
    archive over 18 years ago

    Davy,
         What you are looking for is so called "E-Sequences" or in SV, it is better called "Scenarios" (as the term sequence is used inside SVA domain). Having said that, the way you are approaching does seem the best/optimal way to me atleast. You should try using an array of "Pkt_Cmd" and use SV's "foreach" constraints. Now I'm not sure of tool support for this in your tool set, check with the vendor. We showed how to build a custom scenario for a Vending machine example in our VMM adoption book (www.noveldvocm or www.systemverilog.us). To be honest and fair, it is not that trivial to do this. eRM has sequences precisely for this and perhaps equivalent exists in URM. FWIW, VMM scenario does precisely this and you get this in few lines of code.

    I know I didn't give you direct answer, but hopefully a right direction.

    Good Luck
    Ajeetha, CVC
    www.noveldv.com  


    Originally posted in cdnusers.org by ajeetha
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • archive
    archive over 18 years ago

    Sorry, forgot to add that calling randomize() inside post_randomize is a bad style, avoid it if you can

    Ajeetha, CVC
    www.noveldv.com


    Originally posted in cdnusers.org by ajeetha
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • archive
    archive over 18 years ago

    Hi Ajeetha,

    I have solve the problem by just add rand to Packet object and use randomize only one time at pkt_seq[i].randomize().
    Thanks :)

    Bests regards,
    Davy


    Originally posted in cdnusers.org by davyzhu
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information