HOME

    Electronics Directory Articles/ Tutorials eBooks

About Us

FORUM Links Contact Us
   

 SystemVerilog Tutorial PART IX: by Abhiram Rao

SystemVerilog Queues and Mailbox Examples

 <Previous    Page       Next>

 

 

 

Queue Examples

 This is an example to demonstrate the use of Queues. A queue is created in the program block, it gets passed to methods and manipulated.

 

 

 

 

program tb_top;

// tasks

task double_down(ref reg [31:0] array_data[$]) ;

// argument takes in a queue by reference,

// modifies it and returns it

int i, orig_size;

// double the size of the array

orig_size = array_data.size();

for ( i = 0 ; i < orig_size; i ++ )

     array_data.push_back(i);

endtask

// program global vars

reg [31:0] my_array[$] = { 1,2,3,4,6,7} ;

int i;

initial

begin

   // pass the array into a method

   double_down(my_array) ;

   $display("done");

   // show the results of the transformation

   for ( i = 0 ; i < my_array.size(); i ++ )

   $display ("%d: %d \n" , i, my_array[i]);

 end

endprogram

 

Usage of Queue Methods:( Insert, Delete, Pop_front, Pop_back, Push_front, Push_back )

 

// this small example shows the use of the built in queue methods program examples ;

 

int intque[$] = { 1, 2, 3 };

string strque [$] = {"first","second","third","forth"};

initial

begin

  

   // example of the use of size

   for (int i = 0 ; i < intque.size(); i++ )

   $display(intque[i]);

  

   // example of the use of insert

  strque.insert(1,"next"); // insert "next" into element 1, former element 1 is now element2.

   strque.insert(2,"somewhere");

   for (int i = 0; i < strque.size; i++)

   $write(strque[i]," ");

   $display(" ");

  

   // example of the use of delete

   strque.delete(1); // delete the element

   strque.delete(3);

   for (int i = 0; i < strque.size; i++)

   $write(strque[i]," ");

   $display(" ");

 

   // example of the use of pop_front

   // deletes the front of the queue

   strque.pop_front();

   for (int i = 0; i < strque.size; i++)

   $write(strque[i]," ");

   $display(" ");

 

   // example of the use of pop_back

   // deletes the back of the queue

   strque.pop_back();

   for (int i = 0; i < strque.size; i++)

   $write(strque[i]," ");

   $display(" &qu0��) V0��) V�+�) V�C�) V���) VP��) V@P��) Ve="margin-top: 0; margin-bottom: 0">    // example of the use of push_front and push_back

   // grows the queue

   strque.push_front("in-front");

   strque.push_back("in-back");

   for (int i = 0; i < strque.size; i++)

   $write(strque[i]," ");

   $display(" ");

 

 end

 endprogram

 

Mailbox Examples

This is an example of using the mailboxes and events  it presumes you know about fork join since mailboxes are  primarily used to pass data from one running thread to another and to show event synchronization.

Example 1:

program example();

mailbox xfer_data;

int done = 0, i = 0, j;

event handshake;

initial

begin

   xfer_data= new();

   fork

   begin

      /// here is one parallel thread

      while(!done)

      begin

           xfer_data.put(i); // you can put any datatype in a mailbox

           // including object handles

           i++; 

           @(handshake); // wait for a handshake from other thread

      end

   end

   // here is a second parallel thread

   begin

      while(!done)

      begin

          xfer_data.get(j); // here we get the data and put it into j (its a ref argument)

          if (j > 100) // check to see we have got 101+ xfers

          done = 1;

          // pull the handshake trigger

          -> handshake;

      end

   end

 join

 $display ("done -- sent %0d out\n", j);

 end

endprogram

 

 

Example 2

 

program prog;


class A;

rand bit[7:0] k;

endclass

 

A a = new;

mailbox mbx = new ();

 

bit[7:0] j;

integer i,k=0;

initial

begin

   repeat(256)

   begin

     a.randomize();

     k = a.k;

     #5 mbx.put(k); ///fill the mailbox with randomly generated k

   end

  

   /// Display the number of messages in Mailbox using mbx.num()

   $display(" Total No. of msgs in mbx = %0d ",mbx.num());

 

   /// Display the first message in Mailbox using mbx.peek(j)

  

   #5 mbx.peek(j);

   $display("First Msg in mbx = %0d",j);

   /// Reading the Mailbox using mbx.get(j) till all the messages are read out.

  

   repeat(256)

   begin

     mbx.get(j);

#5 $display("No. of msgs in mbx left = %0d; Message = %0d at time %0t",mbx.num(),j,$time);

   end

  

  /// Check for the messages left in the MBOX

  $display("No of Msgs Left in MBOX = %0d",mbx.try_peek(j));

  /// Try reading an Empty MBOX which will return 0

 

  if (mbx.try_get(j)==0)

  $display("CANNOT READ AS MAILBOX IS EXHAUSTED \n");

 end

 

endprogram

 

 <Previous    Page       Next>

 

Home   |    Articles/ Tutorials   |   Downloads   |   Feedback   |   Links   |   eBooks   |   Privacy Policy
Copyright � 2005-2007 electroSofts.com.
[email protected]