HOME

    Electronics Directory Articles/ Tutorials eBooks

About Us

FORUM Links Contact Us
   

SystemVerilog Tutorial PART 17: by Abhiram Rao

Examples of Threads in SystemVerilog-2

 

<Previous     TOC      Next >


 

 


This is an example of using threads, same as 1 but using  fork join_any and terminate

 

 


 

program example();

// automatic tasks don't share common variables

// so its ok for multiple threads to access them

// without collision of data in the task

task write_bus(int addr, int data);

  $display("here is a write to the bus at addr %d", addr);

endtask

 

initial

 begin

  fork

   begin

   // here is one parallel thread competing

   repeat (100)

   begin

    // wait for some random time

    #($random() % 100);

    write_bus(200, $random());

   end

 end

 

 // here is a second parallel thread

 begin

  repeat (100)

   begin

    // wait for some random time

     #($random() % 100);

     write_bus(100, $random());

   end

 end

join_any

 

// here we are back in the main thread, because one of the  threads //running in parallel is finished. So lets  terminate the other and keep //going

 

 disable fork;

   $display ("done -- out\n");

 end

endprogram

 

This is an example of using threads, same as 1 but using  fork join_none and terminate.

 

program example();

  // semaphores are a unique datatype in SV semaphore want_to_print;

  // this is the "shared resource", would be a bus,

  // but in this case its a $display

  task write_bus(int addr, int data);

       $display("here is a write to the bus at addr %d", addr);

       #100;

  endtask

  int done = 0, i = 0;

  initial

   begin

    // here we initialize the semaphore with one token

    // available to share. We could initialize

    // with more, if there were an interleaved bus, for example

 

    want_to_print = new(1);

    fork

      begin

        // here is one parallel thread competing

        while(1)

        begin

        // wait for some random time

        #($random() % 100);

        // attempt to get the one semaphore token want_to_print.get(1);

        // got it!, lets use the bus

        write_bus(200, $random());

        i++;

        // we must return the semaphore token want_to_print.put(1);

        end

      end

      // here is a second parallel thread

      begin

        while(1)

         begin

           // wait for some random time

           #($random() % 100);

           // attempt to get the one semaphore token

           //want_to_print.get(1);

           // got it!, lets use the bus to the exclusion of others

           write_bus(100, $random());

           i++;

           // we must return the semaphore token

           want_to_print.put(1);

         end

      end

     join_none

 

     // this is main thread, running in parallel with the 2 others in

     // the fork join_none.

     while(i < 100)

      begin

        @(i);

      end

     // ok , now lets terminate the other 2 threads!

     disable fork;

          $display ("done -- out\n");

     end

 endprogram

 

<Previous     TOC        Next >

 

Home   |    About Us   |   Articles/ Tutorials   |   Downloads   |   Feedback   |   Links   |   eBooks   |   Privacy Policy
Copyright 2005-2007 electroSofts.com.
webmaster@electroSofts.com